2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 #include <netinet/in.h>
26 #include <sys/socket.h>
31 #include <sys/ioctl.h>
37 #include "dpif-provider.h"
39 #include "dynamic-string.h"
46 #include "ofp-print.h"
49 #include "poll-loop.h"
57 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
59 /* Configuration parameters. */
60 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
61 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
63 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
64 * headers to be aligned on a 4-byte boundary. */
65 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
68 enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
69 enum { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
70 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
71 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
73 struct dp_netdev_queue {
74 struct dpif_upcall *upcalls[MAX_QUEUE_LEN];
75 unsigned int head, tail;
78 /* Datapath based on the network device interface from netdev.h. */
80 const struct dpif_class *class;
85 struct dp_netdev_queue queues[N_QUEUES];
86 struct hmap flow_table; /* Flow table. */
89 long long int n_hit; /* Number of flow table matches. */
90 long long int n_missed; /* Number of flow table misses. */
91 long long int n_lost; /* Number of misses not passed to client. */
94 struct dp_netdev_port *ports[MAX_PORTS];
95 struct list port_list;
99 /* A port in a netdev-based datapath. */
100 struct dp_netdev_port {
101 int port_no; /* Index into dp_netdev's 'ports'. */
102 struct list node; /* Element in dp_netdev's 'port_list'. */
103 struct netdev *netdev;
104 char *type; /* Port type as requested by user. */
107 /* A flow in dp_netdev's 'flow_table'. */
108 struct dp_netdev_flow {
109 struct hmap_node node; /* Element in dp_netdev's 'flow_table'. */
113 long long int used; /* Last used time, in monotonic msecs. */
114 long long int packet_count; /* Number of packets matched. */
115 long long int byte_count; /* Number of bytes matched. */
116 uint8_t tcp_flags; /* Bitwise-OR of seen tcp_flags values. */
119 struct nlattr *actions;
123 /* Interface to netdev-based datapath. */
126 struct dp_netdev *dp;
127 unsigned int dp_serial;
130 /* All netdev-based datapaths. */
131 static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
133 /* Maximum port MTU seen so far. */
134 static int max_mtu = ETH_PAYLOAD_MAX;
136 static int get_port_by_number(struct dp_netdev *, uint16_t port_no,
137 struct dp_netdev_port **portp);
138 static int get_port_by_name(struct dp_netdev *, const char *devname,
139 struct dp_netdev_port **portp);
140 static void dp_netdev_free(struct dp_netdev *);
141 static void dp_netdev_flow_flush(struct dp_netdev *);
142 static int do_add_port(struct dp_netdev *, const char *devname,
143 const char *type, uint16_t port_no);
144 static int do_del_port(struct dp_netdev *, uint16_t port_no);
145 static int dpif_netdev_open(const struct dpif_class *, const char *name,
146 bool create, struct dpif **);
147 static int dp_netdev_output_userspace(struct dp_netdev *, const struct ofpbuf *,
148 int queue_no, const struct flow *,
150 static void dp_netdev_execute_actions(struct dp_netdev *,
151 struct ofpbuf *, struct flow *,
152 const struct nlattr *actions,
155 static struct dpif_netdev *
156 dpif_netdev_cast(const struct dpif *dpif)
158 assert(dpif->dpif_class->open == dpif_netdev_open);
159 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
162 static struct dp_netdev *
163 get_dp_netdev(const struct dpif *dpif)
165 return dpif_netdev_cast(dpif)->dp;
169 dpif_netdev_enumerate(struct sset *all_dps)
171 struct shash_node *node;
173 SHASH_FOR_EACH(node, &dp_netdevs) {
174 sset_add(all_dps, node->name);
180 create_dpif_netdev(struct dp_netdev *dp)
182 uint16_t netflow_id = hash_string(dp->name, 0);
183 struct dpif_netdev *dpif;
187 dpif = xmalloc(sizeof *dpif);
188 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
190 dpif->dp_serial = dp->serial;
196 create_dp_netdev(const char *name, const struct dpif_class *class,
197 struct dp_netdev **dpp)
199 struct dp_netdev *dp;
203 dp = xzalloc(sizeof *dp);
205 dp->name = xstrdup(name);
207 for (i = 0; i < N_QUEUES; i++) {
208 dp->queues[i].head = dp->queues[i].tail = 0;
210 hmap_init(&dp->flow_table);
211 list_init(&dp->port_list);
212 error = do_add_port(dp, name, "internal", OVSP_LOCAL);
218 shash_add(&dp_netdevs, name, dp);
225 dpif_netdev_open(const struct dpif_class *class, const char *name,
226 bool create, struct dpif **dpifp)
228 struct dp_netdev *dp;
230 dp = shash_find_data(&dp_netdevs, name);
235 int error = create_dp_netdev(name, class, &dp);
242 if (dp->class != class) {
249 *dpifp = create_dpif_netdev(dp);
254 dp_netdev_purge_queues(struct dp_netdev *dp)
258 for (i = 0; i < N_QUEUES; i++) {
259 struct dp_netdev_queue *q = &dp->queues[i];
261 while (q->tail != q->head) {
262 struct dpif_upcall *upcall = q->upcalls[q->tail++ & QUEUE_MASK];
264 ofpbuf_delete(upcall->packet);
271 dp_netdev_free(struct dp_netdev *dp)
273 struct dp_netdev_port *port, *next;
275 dp_netdev_flow_flush(dp);
276 LIST_FOR_EACH_SAFE (port, next, node, &dp->port_list) {
277 do_del_port(dp, port->port_no);
279 dp_netdev_purge_queues(dp);
280 hmap_destroy(&dp->flow_table);
286 dpif_netdev_close(struct dpif *dpif)
288 struct dp_netdev *dp = get_dp_netdev(dpif);
289 assert(dp->open_cnt > 0);
290 if (--dp->open_cnt == 0 && dp->destroyed) {
291 shash_find_and_delete(&dp_netdevs, dp->name);
298 dpif_netdev_destroy(struct dpif *dpif)
300 struct dp_netdev *dp = get_dp_netdev(dpif);
301 dp->destroyed = true;
306 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
308 struct dp_netdev *dp = get_dp_netdev(dpif);
309 stats->n_flows = hmap_count(&dp->flow_table);
310 stats->n_hit = dp->n_hit;
311 stats->n_missed = dp->n_missed;
312 stats->n_lost = dp->n_lost;
317 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
320 struct dp_netdev_port *port;
321 struct netdev *netdev;
322 const char *open_type;
326 /* XXX reject devices already in some dp_netdev. */
328 /* Open and validate network device. */
329 open_type = (strcmp(type, "internal") ? type
330 : dp->class != &dpif_netdev_class ? "dummy"
332 error = netdev_open(devname, open_type, &netdev);
336 /* XXX reject loopback devices */
337 /* XXX reject non-Ethernet devices */
339 error = netdev_listen(netdev);
341 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
342 devname, strerror(errno));
343 netdev_close(netdev);
347 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
349 netdev_close(netdev);
353 port = xmalloc(sizeof *port);
354 port->port_no = port_no;
355 port->netdev = netdev;
356 port->type = xstrdup(type);
358 error = netdev_get_mtu(netdev, &mtu);
363 list_push_back(&dp->port_list, &port->node);
364 dp->ports[port_no] = port;
371 choose_port(struct dpif *dpif, struct netdev *netdev)
373 struct dp_netdev *dp = get_dp_netdev(dpif);
376 if (dpif->dpif_class != &dpif_netdev_class) {
377 /* If the port name contains a number, try to assign that port number.
378 * This can make writing unit tests easier because port numbers are
382 for (p = netdev_get_name(netdev); *p != '\0'; p++) {
383 if (isdigit((unsigned char) *p)) {
384 port_no = strtol(p, NULL, 10);
385 if (port_no > 0 && port_no < MAX_PORTS
386 && !dp->ports[port_no]) {
394 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
395 if (!dp->ports[port_no]) {
404 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
407 struct dp_netdev *dp = get_dp_netdev(dpif);
410 port_no = choose_port(dpif, netdev);
413 return do_add_port(dp, netdev_get_name(netdev),
414 netdev_get_type(netdev), port_no);
420 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
422 struct dp_netdev *dp = get_dp_netdev(dpif);
423 return port_no == OVSP_LOCAL ? EINVAL : do_del_port(dp, port_no);
427 is_valid_port_number(uint16_t port_no)
429 return port_no < MAX_PORTS;
433 get_port_by_number(struct dp_netdev *dp,
434 uint16_t port_no, struct dp_netdev_port **portp)
436 if (!is_valid_port_number(port_no)) {
440 *portp = dp->ports[port_no];
441 return *portp ? 0 : ENOENT;
446 get_port_by_name(struct dp_netdev *dp,
447 const char *devname, struct dp_netdev_port **portp)
449 struct dp_netdev_port *port;
451 LIST_FOR_EACH (port, node, &dp->port_list) {
452 if (!strcmp(netdev_get_name(port->netdev), devname)) {
461 do_del_port(struct dp_netdev *dp, uint16_t port_no)
463 struct dp_netdev_port *port;
467 error = get_port_by_number(dp, port_no, &port);
472 list_remove(&port->node);
473 dp->ports[port->port_no] = NULL;
476 name = xstrdup(netdev_get_name(port->netdev));
477 netdev_close(port->netdev);
487 answer_port_query(const struct dp_netdev_port *port,
488 struct dpif_port *dpif_port)
490 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
491 dpif_port->type = xstrdup(port->type);
492 dpif_port->port_no = port->port_no;
496 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
497 struct dpif_port *dpif_port)
499 struct dp_netdev *dp = get_dp_netdev(dpif);
500 struct dp_netdev_port *port;
503 error = get_port_by_number(dp, port_no, &port);
505 answer_port_query(port, dpif_port);
511 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
512 struct dpif_port *dpif_port)
514 struct dp_netdev *dp = get_dp_netdev(dpif);
515 struct dp_netdev_port *port;
518 error = get_port_by_name(dp, devname, &port);
520 answer_port_query(port, dpif_port);
526 dpif_netdev_get_max_ports(const struct dpif *dpif OVS_UNUSED)
532 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
534 hmap_remove(&dp->flow_table, &flow->node);
540 dp_netdev_flow_flush(struct dp_netdev *dp)
542 struct dp_netdev_flow *flow, *next;
544 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
545 dp_netdev_free_flow(dp, flow);
550 dpif_netdev_flow_flush(struct dpif *dpif)
552 struct dp_netdev *dp = get_dp_netdev(dpif);
553 dp_netdev_flow_flush(dp);
557 struct dp_netdev_port_state {
563 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
565 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
570 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
571 struct dpif_port *dpif_port)
573 struct dp_netdev_port_state *state = state_;
574 struct dp_netdev *dp = get_dp_netdev(dpif);
577 for (port_no = state->port_no; port_no < MAX_PORTS; port_no++) {
578 struct dp_netdev_port *port = dp->ports[port_no];
581 state->name = xstrdup(netdev_get_name(port->netdev));
582 dpif_port->name = state->name;
583 dpif_port->type = port->type;
584 dpif_port->port_no = port->port_no;
585 state->port_no = port_no + 1;
593 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
595 struct dp_netdev_port_state *state = state_;
602 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
604 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
605 if (dpif->dp_serial != dpif->dp->serial) {
606 dpif->dp_serial = dpif->dp->serial;
614 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
616 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
617 if (dpif->dp_serial != dpif->dp->serial) {
618 poll_immediate_wake();
622 static struct dp_netdev_flow *
623 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
625 struct dp_netdev_flow *flow;
627 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
628 if (flow_equal(&flow->key, key)) {
636 get_dpif_flow_stats(struct dp_netdev_flow *flow, struct dpif_flow_stats *stats)
638 stats->n_packets = flow->packet_count;
639 stats->n_bytes = flow->byte_count;
640 stats->used = flow->used;
641 stats->tcp_flags = flow->tcp_flags;
645 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
648 if (odp_flow_key_to_flow(key, key_len, flow)) {
649 /* This should not happen: it indicates that odp_flow_key_from_flow()
650 * and odp_flow_key_to_flow() disagree on the acceptable form of a
651 * flow. Log the problem as an error, with enough details to enable
653 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
655 if (!VLOG_DROP_ERR(&rl)) {
659 odp_flow_key_format(key, key_len, &s);
660 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
667 if (flow->in_port < OFPP_MAX
668 ? flow->in_port >= MAX_PORTS
669 : flow->in_port != OFPP_LOCAL && flow->in_port != OFPP_NONE) {
677 dpif_netdev_flow_get(const struct dpif *dpif,
678 const struct nlattr *nl_key, size_t nl_key_len,
679 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
681 struct dp_netdev *dp = get_dp_netdev(dpif);
682 struct dp_netdev_flow *flow;
686 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
691 flow = dp_netdev_lookup_flow(dp, &key);
697 get_dpif_flow_stats(flow, stats);
700 *actionsp = ofpbuf_clone_data(flow->actions, flow->actions_len);
706 set_flow_actions(struct dp_netdev_flow *flow,
707 const struct nlattr *actions, size_t actions_len)
709 flow->actions = xrealloc(flow->actions, actions_len);
710 flow->actions_len = actions_len;
711 memcpy(flow->actions, actions, actions_len);
716 add_flow(struct dpif *dpif, const struct flow *key,
717 const struct nlattr *actions, size_t actions_len)
719 struct dp_netdev *dp = get_dp_netdev(dpif);
720 struct dp_netdev_flow *flow;
723 flow = xzalloc(sizeof *flow);
726 error = set_flow_actions(flow, actions, actions_len);
732 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
737 clear_stats(struct dp_netdev_flow *flow)
740 flow->packet_count = 0;
741 flow->byte_count = 0;
746 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
748 struct dp_netdev *dp = get_dp_netdev(dpif);
749 struct dp_netdev_flow *flow;
753 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &key);
758 flow = dp_netdev_lookup_flow(dp, &key);
760 if (put->flags & DPIF_FP_CREATE) {
761 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
763 memset(put->stats, 0, sizeof *put->stats);
765 return add_flow(dpif, &key, put->actions, put->actions_len);
773 if (put->flags & DPIF_FP_MODIFY) {
774 int error = set_flow_actions(flow, put->actions, put->actions_len);
777 get_dpif_flow_stats(flow, put->stats);
779 if (put->flags & DPIF_FP_ZERO_STATS) {
791 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
793 struct dp_netdev *dp = get_dp_netdev(dpif);
794 struct dp_netdev_flow *flow;
798 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
803 flow = dp_netdev_lookup_flow(dp, &key);
806 get_dpif_flow_stats(flow, del->stats);
808 dp_netdev_free_flow(dp, flow);
815 struct dp_netdev_flow_state {
818 struct nlattr *actions;
819 struct odputil_keybuf keybuf;
820 struct dpif_flow_stats stats;
824 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
826 struct dp_netdev_flow_state *state;
828 *statep = state = xmalloc(sizeof *state);
831 state->actions = NULL;
836 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
837 const struct nlattr **key, size_t *key_len,
838 const struct nlattr **actions, size_t *actions_len,
839 const struct dpif_flow_stats **stats)
841 struct dp_netdev_flow_state *state = state_;
842 struct dp_netdev *dp = get_dp_netdev(dpif);
843 struct dp_netdev_flow *flow;
844 struct hmap_node *node;
846 node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
851 flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
856 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
857 odp_flow_key_from_flow(&buf, &flow->key);
864 free(state->actions);
865 state->actions = xmemdup(flow->actions, flow->actions_len);
867 *actions = state->actions;
868 *actions_len = flow->actions_len;
872 get_dpif_flow_stats(flow, &state->stats);
873 *stats = &state->stats;
880 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
882 struct dp_netdev_flow_state *state = state_;
884 free(state->actions);
890 dpif_netdev_execute(struct dpif *dpif, const struct dpif_execute *execute)
892 struct dp_netdev *dp = get_dp_netdev(dpif);
897 if (execute->packet->size < ETH_HEADER_LEN ||
898 execute->packet->size > UINT16_MAX) {
902 /* Make a deep copy of 'packet', because we might modify its data. */
903 ofpbuf_init(©, DP_NETDEV_HEADROOM + execute->packet->size);
904 ofpbuf_reserve(©, DP_NETDEV_HEADROOM);
905 ofpbuf_put(©, execute->packet->data, execute->packet->size);
907 flow_extract(©, 0, 0, -1, &key);
908 error = dpif_netdev_flow_from_nlattrs(execute->key, execute->key_len,
911 dp_netdev_execute_actions(dp, ©, &key,
912 execute->actions, execute->actions_len);
915 ofpbuf_uninit(©);
920 dpif_netdev_recv_set(struct dpif *dpif OVS_UNUSED, bool enable OVS_UNUSED)
926 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
927 uint32_t queue_id, uint32_t *priority)
929 *priority = queue_id;
933 static struct dp_netdev_queue *
934 find_nonempty_queue(struct dpif *dpif)
936 struct dp_netdev *dp = get_dp_netdev(dpif);
939 for (i = 0; i < N_QUEUES; i++) {
940 struct dp_netdev_queue *q = &dp->queues[i];
941 if (q->head != q->tail) {
949 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
952 struct dp_netdev_queue *q = find_nonempty_queue(dpif);
954 struct dpif_upcall *u = q->upcalls[q->tail++ & QUEUE_MASK];
959 *buf = *upcall->packet;
968 dpif_netdev_recv_wait(struct dpif *dpif)
970 if (find_nonempty_queue(dpif)) {
971 poll_immediate_wake();
973 /* No messages ready to be received, and dp_wait() will ensure that we
974 * wake up to queue new messages, so there is nothing to do. */
979 dpif_netdev_recv_purge(struct dpif *dpif)
981 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
982 dp_netdev_purge_queues(dpif_netdev->dp);
986 dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key,
987 const struct ofpbuf *packet)
989 flow->used = time_msec();
990 flow->packet_count++;
991 flow->byte_count += packet->size;
992 flow->tcp_flags |= packet_get_tcp_flags(packet, key);
996 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
997 struct ofpbuf *packet)
999 struct dp_netdev_flow *flow;
1002 if (packet->size < ETH_HEADER_LEN) {
1005 flow_extract(packet, 0, 0, port->port_no, &key);
1006 flow = dp_netdev_lookup_flow(dp, &key);
1008 dp_netdev_flow_used(flow, &key, packet);
1009 dp_netdev_execute_actions(dp, packet, &key,
1010 flow->actions, flow->actions_len);
1014 dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, 0);
1019 dpif_netdev_run(struct dpif *dpif)
1021 struct dp_netdev *dp = get_dp_netdev(dpif);
1022 struct dp_netdev_port *port;
1023 struct ofpbuf packet;
1025 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
1027 LIST_FOR_EACH (port, node, &dp->port_list) {
1030 /* Reset packet contents. */
1031 ofpbuf_clear(&packet);
1032 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1034 error = netdev_recv(port->netdev, &packet);
1036 dp_netdev_port_input(dp, port, &packet);
1037 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1038 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1039 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1040 netdev_get_name(port->netdev), strerror(error));
1043 ofpbuf_uninit(&packet);
1047 dpif_netdev_wait(struct dpif *dpif)
1049 struct dp_netdev *dp = get_dp_netdev(dpif);
1050 struct dp_netdev_port *port;
1052 LIST_FOR_EACH (port, node, &dp->port_list) {
1053 netdev_recv_wait(port->netdev);
1058 dp_netdev_set_dl(struct ofpbuf *packet, const struct ovs_key_ethernet *eth_key)
1060 struct eth_header *eh = packet->l2;
1062 memcpy(eh->eth_src, eth_key->eth_src, sizeof eh->eth_src);
1063 memcpy(eh->eth_dst, eth_key->eth_dst, sizeof eh->eth_dst);
1067 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1070 struct dp_netdev_port *p = dp->ports[out_port];
1072 netdev_send(p->netdev, packet);
1077 dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
1078 int queue_no, const struct flow *flow, uint64_t arg)
1080 struct dp_netdev_queue *q = &dp->queues[queue_no];
1081 struct dpif_upcall *upcall;
1085 if (q->head - q->tail >= MAX_QUEUE_LEN) {
1090 buf = ofpbuf_new(ODPUTIL_FLOW_KEY_BYTES + 2 + packet->size);
1091 odp_flow_key_from_flow(buf, flow);
1092 key_len = buf->size;
1093 ofpbuf_pull(buf, key_len);
1094 ofpbuf_reserve(buf, 2);
1095 ofpbuf_put(buf, packet->data, packet->size);
1097 upcall = xzalloc(sizeof *upcall);
1098 upcall->type = queue_no;
1099 upcall->packet = buf;
1100 upcall->key = buf->base;
1101 upcall->key_len = key_len;
1102 upcall->userdata = arg;
1104 q->upcalls[q->head++ & QUEUE_MASK] = upcall;
1110 dp_netdev_sample(struct dp_netdev *dp,
1111 struct ofpbuf *packet, struct flow *key,
1112 const struct nlattr *action)
1114 const struct nlattr *subactions = NULL;
1115 const struct nlattr *a;
1118 NL_NESTED_FOR_EACH_UNSAFE (a, left, action) {
1119 int type = nl_attr_type(a);
1121 switch ((enum ovs_sample_attr) type) {
1122 case OVS_SAMPLE_ATTR_PROBABILITY:
1123 if (random_uint32() >= nl_attr_get_u32(a)) {
1128 case OVS_SAMPLE_ATTR_ACTIONS:
1132 case OVS_SAMPLE_ATTR_UNSPEC:
1133 case __OVS_SAMPLE_ATTR_MAX:
1139 dp_netdev_execute_actions(dp, packet, key, nl_attr_get(subactions),
1140 nl_attr_get_size(subactions));
1144 dp_netdev_action_userspace(struct dp_netdev *dp,
1145 struct ofpbuf *packet, struct flow *key,
1146 const struct nlattr *a)
1148 const struct nlattr *userdata_attr;
1151 userdata_attr = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
1152 userdata = userdata_attr ? nl_attr_get_u64(userdata_attr) : 0;
1153 dp_netdev_output_userspace(dp, packet, DPIF_UC_ACTION, key, userdata);
1157 execute_set_action(struct ofpbuf *packet, const struct nlattr *a)
1159 enum ovs_key_attr type = nl_attr_type(a);
1160 const struct ovs_key_ipv4 *ipv4_key;
1161 const struct ovs_key_tcp *tcp_key;
1162 const struct ovs_key_udp *udp_key;
1165 case OVS_KEY_ATTR_TUN_ID:
1166 case OVS_KEY_ATTR_PRIORITY:
1167 case OVS_KEY_ATTR_IPV6:
1168 /* not implemented */
1171 case OVS_KEY_ATTR_ETHERNET:
1172 dp_netdev_set_dl(packet,
1173 nl_attr_get_unspec(a, sizeof(struct ovs_key_ethernet)));
1176 case OVS_KEY_ATTR_IPV4:
1177 ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4));
1178 packet_set_ipv4(packet, ipv4_key->ipv4_src, ipv4_key->ipv4_dst,
1179 ipv4_key->ipv4_tos, ipv4_key->ipv4_ttl);
1182 case OVS_KEY_ATTR_TCP:
1183 tcp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
1184 packet_set_tcp_port(packet, tcp_key->tcp_src, tcp_key->tcp_dst);
1187 case OVS_KEY_ATTR_UDP:
1188 udp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
1189 packet_set_udp_port(packet, udp_key->udp_src, udp_key->udp_dst);
1192 case OVS_KEY_ATTR_UNSPEC:
1193 case OVS_KEY_ATTR_ENCAP:
1194 case OVS_KEY_ATTR_ETHERTYPE:
1195 case OVS_KEY_ATTR_IN_PORT:
1196 case OVS_KEY_ATTR_VLAN:
1197 case OVS_KEY_ATTR_ICMP:
1198 case OVS_KEY_ATTR_ICMPV6:
1199 case OVS_KEY_ATTR_ARP:
1200 case OVS_KEY_ATTR_ND:
1201 case __OVS_KEY_ATTR_MAX:
1208 dp_netdev_execute_actions(struct dp_netdev *dp,
1209 struct ofpbuf *packet, struct flow *key,
1210 const struct nlattr *actions,
1213 const struct nlattr *a;
1216 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
1217 const struct ovs_action_push_vlan *vlan;
1218 int type = nl_attr_type(a);
1220 switch ((enum ovs_action_attr) type) {
1221 case OVS_ACTION_ATTR_OUTPUT:
1222 dp_netdev_output_port(dp, packet, nl_attr_get_u32(a));
1225 case OVS_ACTION_ATTR_USERSPACE:
1226 dp_netdev_action_userspace(dp, packet, key, a);
1229 case OVS_ACTION_ATTR_PUSH_VLAN:
1230 vlan = nl_attr_get(a);
1231 eth_push_vlan(packet, vlan->vlan_tci);
1234 case OVS_ACTION_ATTR_POP_VLAN:
1235 eth_pop_vlan(packet);
1238 case OVS_ACTION_ATTR_SET:
1239 execute_set_action(packet, nl_attr_get(a));
1242 case OVS_ACTION_ATTR_SAMPLE:
1243 dp_netdev_sample(dp, packet, key, a);
1246 case OVS_ACTION_ATTR_UNSPEC:
1247 case __OVS_ACTION_ATTR_MAX:
1253 const struct dpif_class dpif_netdev_class = {
1255 dpif_netdev_enumerate,
1258 dpif_netdev_destroy,
1261 dpif_netdev_get_stats,
1262 dpif_netdev_port_add,
1263 dpif_netdev_port_del,
1264 dpif_netdev_port_query_by_number,
1265 dpif_netdev_port_query_by_name,
1266 dpif_netdev_get_max_ports,
1267 NULL, /* port_get_pid */
1268 dpif_netdev_port_dump_start,
1269 dpif_netdev_port_dump_next,
1270 dpif_netdev_port_dump_done,
1271 dpif_netdev_port_poll,
1272 dpif_netdev_port_poll_wait,
1273 dpif_netdev_flow_get,
1274 dpif_netdev_flow_put,
1275 dpif_netdev_flow_del,
1276 dpif_netdev_flow_flush,
1277 dpif_netdev_flow_dump_start,
1278 dpif_netdev_flow_dump_next,
1279 dpif_netdev_flow_dump_done,
1280 dpif_netdev_execute,
1282 dpif_netdev_recv_set,
1283 dpif_netdev_queue_to_priority,
1285 dpif_netdev_recv_wait,
1286 dpif_netdev_recv_purge,
1290 dpif_dummy_register__(const char *type)
1292 struct dpif_class *class;
1294 class = xmalloc(sizeof *class);
1295 *class = dpif_netdev_class;
1296 class->type = xstrdup(type);
1297 dp_register_provider(class);
1301 dpif_dummy_register(bool override)
1308 dp_enumerate_types(&types);
1309 SSET_FOR_EACH (type, &types) {
1310 if (!dp_unregister_provider(type)) {
1311 dpif_dummy_register__(type);
1314 sset_destroy(&types);
1317 dpif_dummy_register__("dummy");