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 if (*port_nop != UINT16_MAX) {
411 if (*port_nop >= MAX_PORTS) {
413 } else if (dp->ports[*port_nop]) {
418 port_no = choose_port(dpif, netdev);
422 return do_add_port(dp, netdev_get_name(netdev),
423 netdev_get_type(netdev), port_no);
429 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
431 struct dp_netdev *dp = get_dp_netdev(dpif);
432 return port_no == OVSP_LOCAL ? EINVAL : do_del_port(dp, port_no);
436 is_valid_port_number(uint16_t port_no)
438 return port_no < MAX_PORTS;
442 get_port_by_number(struct dp_netdev *dp,
443 uint16_t port_no, struct dp_netdev_port **portp)
445 if (!is_valid_port_number(port_no)) {
449 *portp = dp->ports[port_no];
450 return *portp ? 0 : ENOENT;
455 get_port_by_name(struct dp_netdev *dp,
456 const char *devname, struct dp_netdev_port **portp)
458 struct dp_netdev_port *port;
460 LIST_FOR_EACH (port, node, &dp->port_list) {
461 if (!strcmp(netdev_get_name(port->netdev), devname)) {
470 do_del_port(struct dp_netdev *dp, uint16_t port_no)
472 struct dp_netdev_port *port;
476 error = get_port_by_number(dp, port_no, &port);
481 list_remove(&port->node);
482 dp->ports[port->port_no] = NULL;
485 name = xstrdup(netdev_get_name(port->netdev));
486 netdev_close(port->netdev);
496 answer_port_query(const struct dp_netdev_port *port,
497 struct dpif_port *dpif_port)
499 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
500 dpif_port->type = xstrdup(port->type);
501 dpif_port->port_no = port->port_no;
505 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
506 struct dpif_port *dpif_port)
508 struct dp_netdev *dp = get_dp_netdev(dpif);
509 struct dp_netdev_port *port;
512 error = get_port_by_number(dp, port_no, &port);
514 answer_port_query(port, dpif_port);
520 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
521 struct dpif_port *dpif_port)
523 struct dp_netdev *dp = get_dp_netdev(dpif);
524 struct dp_netdev_port *port;
527 error = get_port_by_name(dp, devname, &port);
529 answer_port_query(port, dpif_port);
535 dpif_netdev_get_max_ports(const struct dpif *dpif OVS_UNUSED)
541 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
543 hmap_remove(&dp->flow_table, &flow->node);
549 dp_netdev_flow_flush(struct dp_netdev *dp)
551 struct dp_netdev_flow *flow, *next;
553 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
554 dp_netdev_free_flow(dp, flow);
559 dpif_netdev_flow_flush(struct dpif *dpif)
561 struct dp_netdev *dp = get_dp_netdev(dpif);
562 dp_netdev_flow_flush(dp);
566 struct dp_netdev_port_state {
572 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
574 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
579 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
580 struct dpif_port *dpif_port)
582 struct dp_netdev_port_state *state = state_;
583 struct dp_netdev *dp = get_dp_netdev(dpif);
586 for (port_no = state->port_no; port_no < MAX_PORTS; port_no++) {
587 struct dp_netdev_port *port = dp->ports[port_no];
590 state->name = xstrdup(netdev_get_name(port->netdev));
591 dpif_port->name = state->name;
592 dpif_port->type = port->type;
593 dpif_port->port_no = port->port_no;
594 state->port_no = port_no + 1;
602 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
604 struct dp_netdev_port_state *state = state_;
611 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
613 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
614 if (dpif->dp_serial != dpif->dp->serial) {
615 dpif->dp_serial = dpif->dp->serial;
623 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
625 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
626 if (dpif->dp_serial != dpif->dp->serial) {
627 poll_immediate_wake();
631 static struct dp_netdev_flow *
632 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
634 struct dp_netdev_flow *flow;
636 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
637 if (flow_equal(&flow->key, key)) {
645 get_dpif_flow_stats(struct dp_netdev_flow *flow, struct dpif_flow_stats *stats)
647 stats->n_packets = flow->packet_count;
648 stats->n_bytes = flow->byte_count;
649 stats->used = flow->used;
650 stats->tcp_flags = flow->tcp_flags;
654 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
657 if (odp_flow_key_to_flow(key, key_len, flow)) {
658 /* This should not happen: it indicates that odp_flow_key_from_flow()
659 * and odp_flow_key_to_flow() disagree on the acceptable form of a
660 * flow. Log the problem as an error, with enough details to enable
662 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
664 if (!VLOG_DROP_ERR(&rl)) {
668 odp_flow_key_format(key, key_len, &s);
669 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
676 if (flow->in_port < OFPP_MAX
677 ? flow->in_port >= MAX_PORTS
678 : flow->in_port != OFPP_LOCAL && flow->in_port != OFPP_NONE) {
686 dpif_netdev_flow_get(const struct dpif *dpif,
687 const struct nlattr *nl_key, size_t nl_key_len,
688 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
690 struct dp_netdev *dp = get_dp_netdev(dpif);
691 struct dp_netdev_flow *flow;
695 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
700 flow = dp_netdev_lookup_flow(dp, &key);
706 get_dpif_flow_stats(flow, stats);
709 *actionsp = ofpbuf_clone_data(flow->actions, flow->actions_len);
715 set_flow_actions(struct dp_netdev_flow *flow,
716 const struct nlattr *actions, size_t actions_len)
718 flow->actions = xrealloc(flow->actions, actions_len);
719 flow->actions_len = actions_len;
720 memcpy(flow->actions, actions, actions_len);
725 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *key,
726 const struct nlattr *actions, size_t actions_len)
728 struct dp_netdev_flow *flow;
731 flow = xzalloc(sizeof *flow);
734 error = set_flow_actions(flow, actions, actions_len);
740 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
745 clear_stats(struct dp_netdev_flow *flow)
748 flow->packet_count = 0;
749 flow->byte_count = 0;
754 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
756 struct dp_netdev *dp = get_dp_netdev(dpif);
757 struct dp_netdev_flow *flow;
761 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &key);
766 flow = dp_netdev_lookup_flow(dp, &key);
768 if (put->flags & DPIF_FP_CREATE) {
769 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
771 memset(put->stats, 0, sizeof *put->stats);
773 return dp_netdev_flow_add(dp, &key, put->actions,
782 if (put->flags & DPIF_FP_MODIFY) {
783 int error = set_flow_actions(flow, put->actions, put->actions_len);
786 get_dpif_flow_stats(flow, put->stats);
788 if (put->flags & DPIF_FP_ZERO_STATS) {
800 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
802 struct dp_netdev *dp = get_dp_netdev(dpif);
803 struct dp_netdev_flow *flow;
807 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
812 flow = dp_netdev_lookup_flow(dp, &key);
815 get_dpif_flow_stats(flow, del->stats);
817 dp_netdev_free_flow(dp, flow);
824 struct dp_netdev_flow_state {
827 struct nlattr *actions;
828 struct odputil_keybuf keybuf;
829 struct dpif_flow_stats stats;
833 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
835 struct dp_netdev_flow_state *state;
837 *statep = state = xmalloc(sizeof *state);
840 state->actions = NULL;
845 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
846 const struct nlattr **key, size_t *key_len,
847 const struct nlattr **actions, size_t *actions_len,
848 const struct dpif_flow_stats **stats)
850 struct dp_netdev_flow_state *state = state_;
851 struct dp_netdev *dp = get_dp_netdev(dpif);
852 struct dp_netdev_flow *flow;
853 struct hmap_node *node;
855 node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
860 flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
865 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
866 odp_flow_key_from_flow(&buf, &flow->key);
873 free(state->actions);
874 state->actions = xmemdup(flow->actions, flow->actions_len);
876 *actions = state->actions;
877 *actions_len = flow->actions_len;
881 get_dpif_flow_stats(flow, &state->stats);
882 *stats = &state->stats;
889 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
891 struct dp_netdev_flow_state *state = state_;
893 free(state->actions);
899 dpif_netdev_execute(struct dpif *dpif, const struct dpif_execute *execute)
901 struct dp_netdev *dp = get_dp_netdev(dpif);
906 if (execute->packet->size < ETH_HEADER_LEN ||
907 execute->packet->size > UINT16_MAX) {
911 /* Make a deep copy of 'packet', because we might modify its data. */
912 ofpbuf_init(©, DP_NETDEV_HEADROOM + execute->packet->size);
913 ofpbuf_reserve(©, DP_NETDEV_HEADROOM);
914 ofpbuf_put(©, execute->packet->data, execute->packet->size);
916 flow_extract(©, 0, 0, -1, &key);
917 error = dpif_netdev_flow_from_nlattrs(execute->key, execute->key_len,
920 dp_netdev_execute_actions(dp, ©, &key,
921 execute->actions, execute->actions_len);
924 ofpbuf_uninit(©);
929 dpif_netdev_recv_set(struct dpif *dpif OVS_UNUSED, bool enable OVS_UNUSED)
935 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
936 uint32_t queue_id, uint32_t *priority)
938 *priority = queue_id;
942 static struct dp_netdev_queue *
943 find_nonempty_queue(struct dpif *dpif)
945 struct dp_netdev *dp = get_dp_netdev(dpif);
948 for (i = 0; i < N_QUEUES; i++) {
949 struct dp_netdev_queue *q = &dp->queues[i];
950 if (q->head != q->tail) {
958 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
961 struct dp_netdev_queue *q = find_nonempty_queue(dpif);
963 struct dpif_upcall *u = q->upcalls[q->tail++ & QUEUE_MASK];
968 *buf = *upcall->packet;
977 dpif_netdev_recv_wait(struct dpif *dpif)
979 if (find_nonempty_queue(dpif)) {
980 poll_immediate_wake();
982 /* No messages ready to be received, and dp_wait() will ensure that we
983 * wake up to queue new messages, so there is nothing to do. */
988 dpif_netdev_recv_purge(struct dpif *dpif)
990 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
991 dp_netdev_purge_queues(dpif_netdev->dp);
995 dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key,
996 const struct ofpbuf *packet)
998 flow->used = time_msec();
999 flow->packet_count++;
1000 flow->byte_count += packet->size;
1001 flow->tcp_flags |= packet_get_tcp_flags(packet, key);
1005 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1006 struct ofpbuf *packet)
1008 struct dp_netdev_flow *flow;
1011 if (packet->size < ETH_HEADER_LEN) {
1014 flow_extract(packet, 0, 0, port->port_no, &key);
1015 flow = dp_netdev_lookup_flow(dp, &key);
1017 dp_netdev_flow_used(flow, &key, packet);
1018 dp_netdev_execute_actions(dp, packet, &key,
1019 flow->actions, flow->actions_len);
1023 dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, 0);
1028 dpif_netdev_run(struct dpif *dpif)
1030 struct dp_netdev *dp = get_dp_netdev(dpif);
1031 struct dp_netdev_port *port;
1032 struct ofpbuf packet;
1034 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
1036 LIST_FOR_EACH (port, node, &dp->port_list) {
1039 /* Reset packet contents. */
1040 ofpbuf_clear(&packet);
1041 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1043 error = netdev_recv(port->netdev, &packet);
1045 dp_netdev_port_input(dp, port, &packet);
1046 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1047 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1048 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1049 netdev_get_name(port->netdev), strerror(error));
1052 ofpbuf_uninit(&packet);
1056 dpif_netdev_wait(struct dpif *dpif)
1058 struct dp_netdev *dp = get_dp_netdev(dpif);
1059 struct dp_netdev_port *port;
1061 LIST_FOR_EACH (port, node, &dp->port_list) {
1062 netdev_recv_wait(port->netdev);
1067 dp_netdev_set_dl(struct ofpbuf *packet, const struct ovs_key_ethernet *eth_key)
1069 struct eth_header *eh = packet->l2;
1071 memcpy(eh->eth_src, eth_key->eth_src, sizeof eh->eth_src);
1072 memcpy(eh->eth_dst, eth_key->eth_dst, sizeof eh->eth_dst);
1076 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1079 struct dp_netdev_port *p = dp->ports[out_port];
1081 netdev_send(p->netdev, packet);
1086 dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
1087 int queue_no, const struct flow *flow, uint64_t arg)
1089 struct dp_netdev_queue *q = &dp->queues[queue_no];
1090 struct dpif_upcall *upcall;
1094 if (q->head - q->tail >= MAX_QUEUE_LEN) {
1099 buf = ofpbuf_new(ODPUTIL_FLOW_KEY_BYTES + 2 + packet->size);
1100 odp_flow_key_from_flow(buf, flow);
1101 key_len = buf->size;
1102 ofpbuf_pull(buf, key_len);
1103 ofpbuf_reserve(buf, 2);
1104 ofpbuf_put(buf, packet->data, packet->size);
1106 upcall = xzalloc(sizeof *upcall);
1107 upcall->type = queue_no;
1108 upcall->packet = buf;
1109 upcall->key = buf->base;
1110 upcall->key_len = key_len;
1111 upcall->userdata = arg;
1113 q->upcalls[q->head++ & QUEUE_MASK] = upcall;
1119 dp_netdev_sample(struct dp_netdev *dp,
1120 struct ofpbuf *packet, struct flow *key,
1121 const struct nlattr *action)
1123 const struct nlattr *subactions = NULL;
1124 const struct nlattr *a;
1127 NL_NESTED_FOR_EACH_UNSAFE (a, left, action) {
1128 int type = nl_attr_type(a);
1130 switch ((enum ovs_sample_attr) type) {
1131 case OVS_SAMPLE_ATTR_PROBABILITY:
1132 if (random_uint32() >= nl_attr_get_u32(a)) {
1137 case OVS_SAMPLE_ATTR_ACTIONS:
1141 case OVS_SAMPLE_ATTR_UNSPEC:
1142 case __OVS_SAMPLE_ATTR_MAX:
1148 dp_netdev_execute_actions(dp, packet, key, nl_attr_get(subactions),
1149 nl_attr_get_size(subactions));
1153 dp_netdev_action_userspace(struct dp_netdev *dp,
1154 struct ofpbuf *packet, struct flow *key,
1155 const struct nlattr *a)
1157 const struct nlattr *userdata_attr;
1160 userdata_attr = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
1161 userdata = userdata_attr ? nl_attr_get_u64(userdata_attr) : 0;
1162 dp_netdev_output_userspace(dp, packet, DPIF_UC_ACTION, key, userdata);
1166 execute_set_action(struct ofpbuf *packet, const struct nlattr *a)
1168 enum ovs_key_attr type = nl_attr_type(a);
1169 const struct ovs_key_ipv4 *ipv4_key;
1170 const struct ovs_key_tcp *tcp_key;
1171 const struct ovs_key_udp *udp_key;
1174 case OVS_KEY_ATTR_TUN_ID:
1175 case OVS_KEY_ATTR_PRIORITY:
1176 case OVS_KEY_ATTR_IPV6:
1177 /* not implemented */
1180 case OVS_KEY_ATTR_ETHERNET:
1181 dp_netdev_set_dl(packet,
1182 nl_attr_get_unspec(a, sizeof(struct ovs_key_ethernet)));
1185 case OVS_KEY_ATTR_IPV4:
1186 ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4));
1187 packet_set_ipv4(packet, ipv4_key->ipv4_src, ipv4_key->ipv4_dst,
1188 ipv4_key->ipv4_tos, ipv4_key->ipv4_ttl);
1191 case OVS_KEY_ATTR_TCP:
1192 tcp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
1193 packet_set_tcp_port(packet, tcp_key->tcp_src, tcp_key->tcp_dst);
1196 case OVS_KEY_ATTR_UDP:
1197 udp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
1198 packet_set_udp_port(packet, udp_key->udp_src, udp_key->udp_dst);
1201 case OVS_KEY_ATTR_UNSPEC:
1202 case OVS_KEY_ATTR_ENCAP:
1203 case OVS_KEY_ATTR_ETHERTYPE:
1204 case OVS_KEY_ATTR_IN_PORT:
1205 case OVS_KEY_ATTR_VLAN:
1206 case OVS_KEY_ATTR_ICMP:
1207 case OVS_KEY_ATTR_ICMPV6:
1208 case OVS_KEY_ATTR_ARP:
1209 case OVS_KEY_ATTR_ND:
1210 case __OVS_KEY_ATTR_MAX:
1217 dp_netdev_execute_actions(struct dp_netdev *dp,
1218 struct ofpbuf *packet, struct flow *key,
1219 const struct nlattr *actions,
1222 const struct nlattr *a;
1225 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
1226 const struct ovs_action_push_vlan *vlan;
1227 int type = nl_attr_type(a);
1229 switch ((enum ovs_action_attr) type) {
1230 case OVS_ACTION_ATTR_OUTPUT:
1231 dp_netdev_output_port(dp, packet, nl_attr_get_u32(a));
1234 case OVS_ACTION_ATTR_USERSPACE:
1235 dp_netdev_action_userspace(dp, packet, key, a);
1238 case OVS_ACTION_ATTR_PUSH_VLAN:
1239 vlan = nl_attr_get(a);
1240 eth_push_vlan(packet, vlan->vlan_tci);
1243 case OVS_ACTION_ATTR_POP_VLAN:
1244 eth_pop_vlan(packet);
1247 case OVS_ACTION_ATTR_SET:
1248 execute_set_action(packet, nl_attr_get(a));
1251 case OVS_ACTION_ATTR_SAMPLE:
1252 dp_netdev_sample(dp, packet, key, a);
1255 case OVS_ACTION_ATTR_UNSPEC:
1256 case __OVS_ACTION_ATTR_MAX:
1262 const struct dpif_class dpif_netdev_class = {
1264 dpif_netdev_enumerate,
1267 dpif_netdev_destroy,
1270 dpif_netdev_get_stats,
1271 dpif_netdev_port_add,
1272 dpif_netdev_port_del,
1273 dpif_netdev_port_query_by_number,
1274 dpif_netdev_port_query_by_name,
1275 dpif_netdev_get_max_ports,
1276 NULL, /* port_get_pid */
1277 dpif_netdev_port_dump_start,
1278 dpif_netdev_port_dump_next,
1279 dpif_netdev_port_dump_done,
1280 dpif_netdev_port_poll,
1281 dpif_netdev_port_poll_wait,
1282 dpif_netdev_flow_get,
1283 dpif_netdev_flow_put,
1284 dpif_netdev_flow_del,
1285 dpif_netdev_flow_flush,
1286 dpif_netdev_flow_dump_start,
1287 dpif_netdev_flow_dump_next,
1288 dpif_netdev_flow_dump_done,
1289 dpif_netdev_execute,
1291 dpif_netdev_recv_set,
1292 dpif_netdev_queue_to_priority,
1294 dpif_netdev_recv_wait,
1295 dpif_netdev_recv_purge,
1299 dpif_dummy_register__(const char *type)
1301 struct dpif_class *class;
1303 class = xmalloc(sizeof *class);
1304 *class = dpif_netdev_class;
1305 class->type = xstrdup(type);
1306 dp_register_provider(class);
1310 dpif_dummy_register(bool override)
1317 dp_enumerate_types(&types);
1318 SSET_FOR_EACH (type, &types) {
1319 if (!dp_unregister_provider(type)) {
1320 dpif_dummy_register__(type);
1323 sset_destroy(&types);
1326 dpif_dummy_register__("dummy");