2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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.
24 #include <netinet/in.h>
25 #include <sys/socket.h>
30 #include <sys/ioctl.h>
36 #include "dpif-provider.h"
38 #include "dynamic-string.h"
43 #include "netdev-vport.h"
45 #include "odp-execute.h"
47 #include "ofp-print.h"
50 #include "poll-loop.h"
58 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
60 /* Configuration parameters. */
61 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
62 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
64 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
65 * headers to be aligned on a 4-byte boundary. */
66 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
69 enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
70 enum { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
71 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
72 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
74 struct dp_netdev_upcall {
75 struct dpif_upcall upcall; /* Queued upcall information. */
76 struct ofpbuf buf; /* ofpbuf instance for upcall.packet. */
79 struct dp_netdev_queue {
80 struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN];
81 unsigned int head, tail;
84 /* Datapath based on the network device interface from netdev.h. */
86 const struct dpif_class *class;
91 struct dp_netdev_queue queues[N_QUEUES];
92 struct hmap flow_table; /* Flow table. */
95 long long int n_hit; /* Number of flow table matches. */
96 long long int n_missed; /* Number of flow table misses. */
97 long long int n_lost; /* Number of misses not passed to client. */
100 struct dp_netdev_port *ports[MAX_PORTS];
101 struct list port_list;
105 /* A port in a netdev-based datapath. */
106 struct dp_netdev_port {
107 int port_no; /* Index into dp_netdev's 'ports'. */
108 struct list node; /* Element in dp_netdev's 'port_list'. */
109 struct netdev *netdev;
110 struct netdev_saved_flags *sf;
111 struct netdev_rx *rx;
112 char *type; /* Port type as requested by user. */
115 /* A flow in dp_netdev's 'flow_table'. */
116 struct dp_netdev_flow {
117 struct hmap_node node; /* Element in dp_netdev's 'flow_table'. */
121 long long int used; /* Last used time, in monotonic msecs. */
122 long long int packet_count; /* Number of packets matched. */
123 long long int byte_count; /* Number of bytes matched. */
124 uint8_t tcp_flags; /* Bitwise-OR of seen tcp_flags values. */
127 struct nlattr *actions;
131 /* Interface to netdev-based datapath. */
134 struct dp_netdev *dp;
135 unsigned int dp_serial;
138 /* All netdev-based datapaths. */
139 static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
141 /* Maximum port MTU seen so far. */
142 static int max_mtu = ETH_PAYLOAD_MAX;
144 static int get_port_by_number(struct dp_netdev *, uint32_t port_no,
145 struct dp_netdev_port **portp);
146 static int get_port_by_name(struct dp_netdev *, const char *devname,
147 struct dp_netdev_port **portp);
148 static void dp_netdev_free(struct dp_netdev *);
149 static void dp_netdev_flow_flush(struct dp_netdev *);
150 static int do_add_port(struct dp_netdev *, const char *devname,
151 const char *type, uint32_t port_no);
152 static int do_del_port(struct dp_netdev *, uint32_t port_no);
153 static int dpif_netdev_open(const struct dpif_class *, const char *name,
154 bool create, struct dpif **);
155 static int dp_netdev_output_userspace(struct dp_netdev *, const struct ofpbuf *,
156 int queue_no, const struct flow *,
157 const struct nlattr *userdata);
158 static void dp_netdev_execute_actions(struct dp_netdev *,
159 struct ofpbuf *, struct flow *,
160 const struct nlattr *actions,
162 static void dp_netdev_port_input(struct dp_netdev *dp,
163 struct dp_netdev_port *port,
164 struct ofpbuf *packet, uint32_t skb_priority,
165 uint32_t skb_mark, const struct flow_tnl *tnl);
167 static struct dpif_netdev *
168 dpif_netdev_cast(const struct dpif *dpif)
170 ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
171 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
174 static struct dp_netdev *
175 get_dp_netdev(const struct dpif *dpif)
177 return dpif_netdev_cast(dpif)->dp;
181 dpif_netdev_enumerate(struct sset *all_dps)
183 struct shash_node *node;
185 SHASH_FOR_EACH(node, &dp_netdevs) {
186 sset_add(all_dps, node->name);
192 dpif_netdev_class_is_dummy(const struct dpif_class *class)
194 return class != &dpif_netdev_class;
198 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
200 return strcmp(type, "internal") ? type
201 : dpif_netdev_class_is_dummy(class) ? "dummy"
206 create_dpif_netdev(struct dp_netdev *dp)
208 uint16_t netflow_id = hash_string(dp->name, 0);
209 struct dpif_netdev *dpif;
213 dpif = xmalloc(sizeof *dpif);
214 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
216 dpif->dp_serial = dp->serial;
222 choose_port(struct dp_netdev *dp, const char *name)
226 if (dp->class != &dpif_netdev_class) {
230 /* If the port name begins with "br", start the number search at
231 * 100 to make writing tests easier. */
232 if (!strncmp(name, "br", 2)) {
236 /* If the port name contains a number, try to assign that port number.
237 * This can make writing unit tests easier because port numbers are
239 for (p = name; *p != '\0'; p++) {
240 if (isdigit((unsigned char) *p)) {
241 port_no = start_no + strtol(p, NULL, 10);
242 if (port_no > 0 && port_no < MAX_PORTS
243 && !dp->ports[port_no]) {
251 for (port_no = 1; port_no < MAX_PORTS; port_no++) {
252 if (!dp->ports[port_no]) {
261 create_dp_netdev(const char *name, const struct dpif_class *class,
262 struct dp_netdev **dpp)
264 struct dp_netdev *dp;
268 dp = xzalloc(sizeof *dp);
270 dp->name = xstrdup(name);
272 for (i = 0; i < N_QUEUES; i++) {
273 dp->queues[i].head = dp->queues[i].tail = 0;
275 hmap_init(&dp->flow_table);
276 list_init(&dp->port_list);
278 error = do_add_port(dp, name, "internal", OVSP_LOCAL);
284 shash_add(&dp_netdevs, name, dp);
291 dpif_netdev_open(const struct dpif_class *class, const char *name,
292 bool create, struct dpif **dpifp)
294 struct dp_netdev *dp;
296 dp = shash_find_data(&dp_netdevs, name);
301 int error = create_dp_netdev(name, class, &dp);
305 ovs_assert(dp != NULL);
308 if (dp->class != class) {
315 *dpifp = create_dpif_netdev(dp);
320 dp_netdev_purge_queues(struct dp_netdev *dp)
324 for (i = 0; i < N_QUEUES; i++) {
325 struct dp_netdev_queue *q = &dp->queues[i];
327 while (q->tail != q->head) {
328 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
329 ofpbuf_uninit(&u->buf);
335 dp_netdev_free(struct dp_netdev *dp)
337 struct dp_netdev_port *port, *next;
339 dp_netdev_flow_flush(dp);
340 LIST_FOR_EACH_SAFE (port, next, node, &dp->port_list) {
341 do_del_port(dp, port->port_no);
343 dp_netdev_purge_queues(dp);
344 hmap_destroy(&dp->flow_table);
350 dpif_netdev_close(struct dpif *dpif)
352 struct dp_netdev *dp = get_dp_netdev(dpif);
353 ovs_assert(dp->open_cnt > 0);
354 if (--dp->open_cnt == 0 && dp->destroyed) {
355 shash_find_and_delete(&dp_netdevs, dp->name);
362 dpif_netdev_destroy(struct dpif *dpif)
364 struct dp_netdev *dp = get_dp_netdev(dpif);
365 dp->destroyed = true;
370 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
372 struct dp_netdev *dp = get_dp_netdev(dpif);
373 stats->n_flows = hmap_count(&dp->flow_table);
374 stats->n_hit = dp->n_hit;
375 stats->n_missed = dp->n_missed;
376 stats->n_lost = dp->n_lost;
381 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
384 struct netdev_saved_flags *sf;
385 struct dp_netdev_port *port;
386 struct netdev *netdev;
387 struct netdev_rx *rx;
388 const char *open_type;
392 /* XXX reject devices already in some dp_netdev. */
394 /* Open and validate network device. */
395 open_type = dpif_netdev_port_open_type(dp->class, type);
396 error = netdev_open(devname, open_type, &netdev);
400 /* XXX reject loopback devices */
401 /* XXX reject non-Ethernet devices */
403 error = netdev_rx_open(netdev, &rx);
405 && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
406 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
407 devname, strerror(errno));
408 netdev_close(netdev);
412 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
415 netdev_close(netdev);
419 port = xmalloc(sizeof *port);
420 port->port_no = port_no;
421 port->netdev = netdev;
424 port->type = xstrdup(type);
426 error = netdev_get_mtu(netdev, &mtu);
427 if (!error && mtu > max_mtu) {
431 list_push_back(&dp->port_list, &port->node);
432 dp->ports[port_no] = port;
439 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
442 struct dp_netdev *dp = get_dp_netdev(dpif);
443 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
444 const char *dpif_port;
447 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
448 if (*port_nop != UINT32_MAX) {
449 if (*port_nop >= MAX_PORTS) {
451 } else if (dp->ports[*port_nop]) {
456 port_no = choose_port(dp, dpif_port);
460 return do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
466 dpif_netdev_port_del(struct dpif *dpif, uint32_t port_no)
468 struct dp_netdev *dp = get_dp_netdev(dpif);
469 return port_no == OVSP_LOCAL ? EINVAL : do_del_port(dp, port_no);
473 is_valid_port_number(uint32_t port_no)
475 return port_no < MAX_PORTS;
479 get_port_by_number(struct dp_netdev *dp,
480 uint32_t port_no, struct dp_netdev_port **portp)
482 if (!is_valid_port_number(port_no)) {
486 *portp = dp->ports[port_no];
487 return *portp ? 0 : ENOENT;
492 get_port_by_name(struct dp_netdev *dp,
493 const char *devname, struct dp_netdev_port **portp)
495 struct dp_netdev_port *port;
497 LIST_FOR_EACH (port, node, &dp->port_list) {
498 if (!strcmp(netdev_get_name(port->netdev), devname)) {
507 do_del_port(struct dp_netdev *dp, uint32_t port_no)
509 struct dp_netdev_port *port;
512 error = get_port_by_number(dp, port_no, &port);
517 list_remove(&port->node);
518 dp->ports[port->port_no] = NULL;
521 netdev_close(port->netdev);
522 netdev_restore_flags(port->sf);
523 netdev_rx_close(port->rx);
531 answer_port_query(const struct dp_netdev_port *port,
532 struct dpif_port *dpif_port)
534 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
535 dpif_port->type = xstrdup(port->type);
536 dpif_port->port_no = port->port_no;
540 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint32_t port_no,
541 struct dpif_port *dpif_port)
543 struct dp_netdev *dp = get_dp_netdev(dpif);
544 struct dp_netdev_port *port;
547 error = get_port_by_number(dp, port_no, &port);
548 if (!error && dpif_port) {
549 answer_port_query(port, dpif_port);
555 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
556 struct dpif_port *dpif_port)
558 struct dp_netdev *dp = get_dp_netdev(dpif);
559 struct dp_netdev_port *port;
562 error = get_port_by_name(dp, devname, &port);
563 if (!error && dpif_port) {
564 answer_port_query(port, dpif_port);
570 dpif_netdev_get_max_ports(const struct dpif *dpif OVS_UNUSED)
576 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
578 hmap_remove(&dp->flow_table, &flow->node);
584 dp_netdev_flow_flush(struct dp_netdev *dp)
586 struct dp_netdev_flow *flow, *next;
588 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
589 dp_netdev_free_flow(dp, flow);
594 dpif_netdev_flow_flush(struct dpif *dpif)
596 struct dp_netdev *dp = get_dp_netdev(dpif);
597 dp_netdev_flow_flush(dp);
601 struct dp_netdev_port_state {
607 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
609 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
614 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
615 struct dpif_port *dpif_port)
617 struct dp_netdev_port_state *state = state_;
618 struct dp_netdev *dp = get_dp_netdev(dpif);
621 for (port_no = state->port_no; port_no < MAX_PORTS; port_no++) {
622 struct dp_netdev_port *port = dp->ports[port_no];
625 state->name = xstrdup(netdev_get_name(port->netdev));
626 dpif_port->name = state->name;
627 dpif_port->type = port->type;
628 dpif_port->port_no = port->port_no;
629 state->port_no = port_no + 1;
637 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
639 struct dp_netdev_port_state *state = state_;
646 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
648 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
649 if (dpif->dp_serial != dpif->dp->serial) {
650 dpif->dp_serial = dpif->dp->serial;
658 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
660 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
661 if (dpif->dp_serial != dpif->dp->serial) {
662 poll_immediate_wake();
666 static struct dp_netdev_flow *
667 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
669 struct dp_netdev_flow *flow;
671 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
672 if (flow_equal(&flow->key, key)) {
680 get_dpif_flow_stats(struct dp_netdev_flow *flow, struct dpif_flow_stats *stats)
682 stats->n_packets = flow->packet_count;
683 stats->n_bytes = flow->byte_count;
684 stats->used = flow->used;
685 stats->tcp_flags = flow->tcp_flags;
689 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
692 if (odp_flow_key_to_flow(key, key_len, flow) != ODP_FIT_PERFECT) {
693 /* This should not happen: it indicates that odp_flow_key_from_flow()
694 * and odp_flow_key_to_flow() disagree on the acceptable form of a
695 * flow. Log the problem as an error, with enough details to enable
697 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
699 if (!VLOG_DROP_ERR(&rl)) {
703 odp_flow_key_format(key, key_len, &s);
704 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
711 if (flow->in_port < OFPP_MAX
712 ? flow->in_port >= MAX_PORTS
713 : flow->in_port != OFPP_LOCAL && flow->in_port != OFPP_NONE) {
721 dpif_netdev_flow_get(const struct dpif *dpif,
722 const struct nlattr *nl_key, size_t nl_key_len,
723 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
725 struct dp_netdev *dp = get_dp_netdev(dpif);
726 struct dp_netdev_flow *flow;
730 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
735 flow = dp_netdev_lookup_flow(dp, &key);
741 get_dpif_flow_stats(flow, stats);
744 *actionsp = ofpbuf_clone_data(flow->actions, flow->actions_len);
750 set_flow_actions(struct dp_netdev_flow *flow,
751 const struct nlattr *actions, size_t actions_len)
753 flow->actions = xrealloc(flow->actions, actions_len);
754 flow->actions_len = actions_len;
755 memcpy(flow->actions, actions, actions_len);
760 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *key,
761 const struct nlattr *actions, size_t actions_len)
763 struct dp_netdev_flow *flow;
766 flow = xzalloc(sizeof *flow);
769 error = set_flow_actions(flow, actions, actions_len);
775 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
780 clear_stats(struct dp_netdev_flow *flow)
783 flow->packet_count = 0;
784 flow->byte_count = 0;
789 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
791 struct dp_netdev *dp = get_dp_netdev(dpif);
792 struct dp_netdev_flow *flow;
796 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &key);
801 flow = dp_netdev_lookup_flow(dp, &key);
803 if (put->flags & DPIF_FP_CREATE) {
804 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
806 memset(put->stats, 0, sizeof *put->stats);
808 return dp_netdev_flow_add(dp, &key, put->actions,
817 if (put->flags & DPIF_FP_MODIFY) {
818 int error = set_flow_actions(flow, put->actions, put->actions_len);
821 get_dpif_flow_stats(flow, put->stats);
823 if (put->flags & DPIF_FP_ZERO_STATS) {
835 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
837 struct dp_netdev *dp = get_dp_netdev(dpif);
838 struct dp_netdev_flow *flow;
842 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
847 flow = dp_netdev_lookup_flow(dp, &key);
850 get_dpif_flow_stats(flow, del->stats);
852 dp_netdev_free_flow(dp, flow);
859 struct dp_netdev_flow_state {
862 struct nlattr *actions;
863 struct odputil_keybuf keybuf;
864 struct dpif_flow_stats stats;
868 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
870 struct dp_netdev_flow_state *state;
872 *statep = state = xmalloc(sizeof *state);
875 state->actions = NULL;
880 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
881 const struct nlattr **key, size_t *key_len,
882 const struct nlattr **actions, size_t *actions_len,
883 const struct dpif_flow_stats **stats)
885 struct dp_netdev_flow_state *state = state_;
886 struct dp_netdev *dp = get_dp_netdev(dpif);
887 struct dp_netdev_flow *flow;
888 struct hmap_node *node;
890 node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
895 flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
900 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
901 odp_flow_key_from_flow(&buf, &flow->key, flow->key.in_port);
908 free(state->actions);
909 state->actions = xmemdup(flow->actions, flow->actions_len);
911 *actions = state->actions;
912 *actions_len = flow->actions_len;
916 get_dpif_flow_stats(flow, &state->stats);
917 *stats = &state->stats;
924 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
926 struct dp_netdev_flow_state *state = state_;
928 free(state->actions);
934 dpif_netdev_execute(struct dpif *dpif, const struct dpif_execute *execute)
936 struct dp_netdev *dp = get_dp_netdev(dpif);
941 if (execute->packet->size < ETH_HEADER_LEN ||
942 execute->packet->size > UINT16_MAX) {
946 /* Make a deep copy of 'packet', because we might modify its data. */
947 ofpbuf_init(©, DP_NETDEV_HEADROOM + execute->packet->size);
948 ofpbuf_reserve(©, DP_NETDEV_HEADROOM);
949 ofpbuf_put(©, execute->packet->data, execute->packet->size);
951 flow_extract(©, 0, 0, NULL, -1, &key);
952 error = dpif_netdev_flow_from_nlattrs(execute->key, execute->key_len,
955 dp_netdev_execute_actions(dp, ©, &key,
956 execute->actions, execute->actions_len);
959 ofpbuf_uninit(©);
964 dpif_netdev_recv_set(struct dpif *dpif OVS_UNUSED, bool enable OVS_UNUSED)
970 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
971 uint32_t queue_id, uint32_t *priority)
973 *priority = queue_id;
977 static struct dp_netdev_queue *
978 find_nonempty_queue(struct dpif *dpif)
980 struct dp_netdev *dp = get_dp_netdev(dpif);
983 for (i = 0; i < N_QUEUES; i++) {
984 struct dp_netdev_queue *q = &dp->queues[i];
985 if (q->head != q->tail) {
993 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
996 struct dp_netdev_queue *q = find_nonempty_queue(dpif);
998 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1000 *upcall = u->upcall;
1001 upcall->packet = buf;
1013 dpif_netdev_recv_wait(struct dpif *dpif)
1015 if (find_nonempty_queue(dpif)) {
1016 poll_immediate_wake();
1018 /* No messages ready to be received, and dp_wait() will ensure that we
1019 * wake up to queue new messages, so there is nothing to do. */
1024 dpif_netdev_recv_purge(struct dpif *dpif)
1026 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1027 dp_netdev_purge_queues(dpif_netdev->dp);
1031 dp_netdev_flow_used(struct dp_netdev_flow *flow, const struct ofpbuf *packet)
1033 flow->used = time_msec();
1034 flow->packet_count++;
1035 flow->byte_count += packet->size;
1036 flow->tcp_flags |= packet_get_tcp_flags(packet, &flow->key);
1040 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1041 struct ofpbuf *packet, uint32_t skb_priority,
1042 uint32_t skb_mark, const struct flow_tnl *tnl)
1044 struct dp_netdev_flow *flow;
1047 if (packet->size < ETH_HEADER_LEN) {
1050 flow_extract(packet, skb_priority, skb_mark, tnl, port->port_no, &key);
1051 flow = dp_netdev_lookup_flow(dp, &key);
1053 dp_netdev_flow_used(flow, packet);
1054 dp_netdev_execute_actions(dp, packet, &key,
1055 flow->actions, flow->actions_len);
1059 dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, NULL);
1064 dpif_netdev_run(struct dpif *dpif)
1066 struct dp_netdev *dp = get_dp_netdev(dpif);
1067 struct dp_netdev_port *port;
1068 struct ofpbuf packet;
1070 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
1072 LIST_FOR_EACH (port, node, &dp->port_list) {
1075 /* Reset packet contents. */
1076 ofpbuf_clear(&packet);
1077 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1079 error = port->rx ? netdev_rx_recv(port->rx, &packet) : EOPNOTSUPP;
1081 dp_netdev_port_input(dp, port, &packet, 0, 0, NULL);
1082 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1083 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1085 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1086 netdev_get_name(port->netdev), strerror(error));
1089 ofpbuf_uninit(&packet);
1093 dpif_netdev_wait(struct dpif *dpif)
1095 struct dp_netdev *dp = get_dp_netdev(dpif);
1096 struct dp_netdev_port *port;
1098 LIST_FOR_EACH (port, node, &dp->port_list) {
1100 netdev_rx_wait(port->rx);
1106 dp_netdev_output_port(void *dp_, struct ofpbuf *packet, uint32_t out_port)
1108 struct dp_netdev *dp = dp_;
1109 struct dp_netdev_port *p = dp->ports[out_port];
1111 netdev_send(p->netdev, packet);
1116 dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
1117 int queue_no, const struct flow *flow,
1118 const struct nlattr *userdata)
1120 struct dp_netdev_queue *q = &dp->queues[queue_no];
1121 if (q->head - q->tail < MAX_QUEUE_LEN) {
1122 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
1123 struct dpif_upcall *upcall = &u->upcall;
1124 struct ofpbuf *buf = &u->buf;
1127 upcall->type = queue_no;
1129 /* Allocate buffer big enough for everything. */
1130 buf_size = ODPUTIL_FLOW_KEY_BYTES + 2 + packet->size;
1132 buf_size += NLA_ALIGN(userdata->nla_len);
1134 ofpbuf_init(buf, buf_size);
1137 odp_flow_key_from_flow(buf, flow, flow->in_port);
1138 upcall->key = buf->data;
1139 upcall->key_len = buf->size;
1143 upcall->userdata = ofpbuf_put(buf, userdata,
1144 NLA_ALIGN(userdata->nla_len));
1149 * We adjust 'data' and 'size' in 'buf' so that only the packet itself
1150 * is visible in 'upcall->packet'. The ODP flow and (if present)
1151 * userdata become part of the headroom. */
1152 ofpbuf_put_zeros(buf, 2);
1153 buf->data = ofpbuf_put(buf, packet->data, packet->size);
1154 buf->size = packet->size;
1155 upcall->packet = buf;
1165 dp_netdev_action_userspace(void *dp, struct ofpbuf *packet,
1166 const struct flow *key,
1167 const struct nlattr *userdata)
1169 dp_netdev_output_userspace(dp, packet, DPIF_UC_ACTION, key, userdata);
1173 dp_netdev_execute_actions(struct dp_netdev *dp,
1174 struct ofpbuf *packet, struct flow *key,
1175 const struct nlattr *actions,
1178 odp_execute_actions(dp, packet, key, actions, actions_len,
1179 dp_netdev_output_port, dp_netdev_action_userspace);
1182 const struct dpif_class dpif_netdev_class = {
1184 dpif_netdev_enumerate,
1185 dpif_netdev_port_open_type,
1188 dpif_netdev_destroy,
1191 dpif_netdev_get_stats,
1192 dpif_netdev_port_add,
1193 dpif_netdev_port_del,
1194 dpif_netdev_port_query_by_number,
1195 dpif_netdev_port_query_by_name,
1196 dpif_netdev_get_max_ports,
1197 NULL, /* port_get_pid */
1198 dpif_netdev_port_dump_start,
1199 dpif_netdev_port_dump_next,
1200 dpif_netdev_port_dump_done,
1201 dpif_netdev_port_poll,
1202 dpif_netdev_port_poll_wait,
1203 dpif_netdev_flow_get,
1204 dpif_netdev_flow_put,
1205 dpif_netdev_flow_del,
1206 dpif_netdev_flow_flush,
1207 dpif_netdev_flow_dump_start,
1208 dpif_netdev_flow_dump_next,
1209 dpif_netdev_flow_dump_done,
1210 dpif_netdev_execute,
1212 dpif_netdev_recv_set,
1213 dpif_netdev_queue_to_priority,
1215 dpif_netdev_recv_wait,
1216 dpif_netdev_recv_purge,
1220 dpif_dummy_register__(const char *type)
1222 struct dpif_class *class;
1224 class = xmalloc(sizeof *class);
1225 *class = dpif_netdev_class;
1226 class->type = xstrdup(type);
1227 dp_register_provider(class);
1231 dpif_dummy_register(bool override)
1238 dp_enumerate_types(&types);
1239 SSET_FOR_EACH (type, &types) {
1240 if (!dp_unregister_provider(type)) {
1241 dpif_dummy_register__(type);
1244 sset_destroy(&types);
1247 dpif_dummy_register__("dummy");