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 odp_port_t 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 *, odp_port_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, odp_port_t port_no);
152 static int do_del_port(struct dp_netdev *, odp_port_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;
221 /* Choose an unused, non-zero port number and return it on success.
222 * Return ODPP_NONE on failure. */
224 choose_port(struct dp_netdev *dp, const char *name)
228 if (dp->class != &dpif_netdev_class) {
232 /* If the port name begins with "br", start the number search at
233 * 100 to make writing tests easier. */
234 if (!strncmp(name, "br", 2)) {
238 /* If the port name contains a number, try to assign that port number.
239 * This can make writing unit tests easier because port numbers are
241 for (p = name; *p != '\0'; p++) {
242 if (isdigit((unsigned char) *p)) {
243 port_no = start_no + strtol(p, NULL, 10);
244 if (port_no > 0 && port_no < MAX_PORTS
245 && !dp->ports[port_no]) {
246 return u32_to_odp(port_no);
253 for (port_no = 1; port_no < MAX_PORTS; port_no++) {
254 if (!dp->ports[port_no]) {
255 return u32_to_odp(port_no);
263 create_dp_netdev(const char *name, const struct dpif_class *class,
264 struct dp_netdev **dpp)
266 struct dp_netdev *dp;
270 dp = xzalloc(sizeof *dp);
272 dp->name = xstrdup(name);
274 for (i = 0; i < N_QUEUES; i++) {
275 dp->queues[i].head = dp->queues[i].tail = 0;
277 hmap_init(&dp->flow_table);
278 list_init(&dp->port_list);
280 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
286 shash_add(&dp_netdevs, name, dp);
293 dpif_netdev_open(const struct dpif_class *class, const char *name,
294 bool create, struct dpif **dpifp)
296 struct dp_netdev *dp;
298 dp = shash_find_data(&dp_netdevs, name);
303 int error = create_dp_netdev(name, class, &dp);
307 ovs_assert(dp != NULL);
310 if (dp->class != class) {
317 *dpifp = create_dpif_netdev(dp);
322 dp_netdev_purge_queues(struct dp_netdev *dp)
326 for (i = 0; i < N_QUEUES; i++) {
327 struct dp_netdev_queue *q = &dp->queues[i];
329 while (q->tail != q->head) {
330 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
331 ofpbuf_uninit(&u->buf);
337 dp_netdev_free(struct dp_netdev *dp)
339 struct dp_netdev_port *port, *next;
341 dp_netdev_flow_flush(dp);
342 LIST_FOR_EACH_SAFE (port, next, node, &dp->port_list) {
343 do_del_port(dp, port->port_no);
345 dp_netdev_purge_queues(dp);
346 hmap_destroy(&dp->flow_table);
352 dpif_netdev_close(struct dpif *dpif)
354 struct dp_netdev *dp = get_dp_netdev(dpif);
355 ovs_assert(dp->open_cnt > 0);
356 if (--dp->open_cnt == 0 && dp->destroyed) {
357 shash_find_and_delete(&dp_netdevs, dp->name);
364 dpif_netdev_destroy(struct dpif *dpif)
366 struct dp_netdev *dp = get_dp_netdev(dpif);
367 dp->destroyed = true;
372 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
374 struct dp_netdev *dp = get_dp_netdev(dpif);
375 stats->n_flows = hmap_count(&dp->flow_table);
376 stats->n_hit = dp->n_hit;
377 stats->n_missed = dp->n_missed;
378 stats->n_lost = dp->n_lost;
383 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
386 struct netdev_saved_flags *sf;
387 struct dp_netdev_port *port;
388 struct netdev *netdev;
389 struct netdev_rx *rx;
390 const char *open_type;
394 /* XXX reject devices already in some dp_netdev. */
396 /* Open and validate network device. */
397 open_type = dpif_netdev_port_open_type(dp->class, type);
398 error = netdev_open(devname, open_type, &netdev);
402 /* XXX reject loopback devices */
403 /* XXX reject non-Ethernet devices */
405 error = netdev_rx_open(netdev, &rx);
407 && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
408 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
409 devname, ovs_strerror(errno));
410 netdev_close(netdev);
414 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
417 netdev_close(netdev);
421 port = xmalloc(sizeof *port);
422 port->port_no = port_no;
423 port->netdev = netdev;
426 port->type = xstrdup(type);
428 error = netdev_get_mtu(netdev, &mtu);
429 if (!error && mtu > max_mtu) {
433 list_push_back(&dp->port_list, &port->node);
434 dp->ports[odp_to_u32(port_no)] = port;
441 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
442 odp_port_t *port_nop)
444 struct dp_netdev *dp = get_dp_netdev(dpif);
445 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
446 const char *dpif_port;
449 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
450 if (*port_nop != ODPP_NONE) {
451 uint32_t port_idx = odp_to_u32(*port_nop);
452 if (port_idx >= MAX_PORTS) {
454 } else if (dp->ports[port_idx]) {
459 port_no = choose_port(dp, dpif_port);
461 if (port_no != ODPP_NONE) {
463 return do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
469 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
471 struct dp_netdev *dp = get_dp_netdev(dpif);
472 return (port_no == ODPP_LOCAL ?
473 EINVAL : do_del_port(dp, port_no));
477 is_valid_port_number(odp_port_t port_no)
479 return odp_to_u32(port_no) < MAX_PORTS;
483 get_port_by_number(struct dp_netdev *dp,
484 odp_port_t port_no, struct dp_netdev_port **portp)
486 if (!is_valid_port_number(port_no)) {
490 *portp = dp->ports[odp_to_u32(port_no)];
491 return *portp ? 0 : ENOENT;
496 get_port_by_name(struct dp_netdev *dp,
497 const char *devname, struct dp_netdev_port **portp)
499 struct dp_netdev_port *port;
501 LIST_FOR_EACH (port, node, &dp->port_list) {
502 if (!strcmp(netdev_get_name(port->netdev), devname)) {
511 do_del_port(struct dp_netdev *dp, odp_port_t port_no)
513 struct dp_netdev_port *port;
516 error = get_port_by_number(dp, port_no, &port);
521 list_remove(&port->node);
522 dp->ports[odp_to_u32(port_no)] = NULL;
525 netdev_close(port->netdev);
526 netdev_restore_flags(port->sf);
527 netdev_rx_close(port->rx);
535 answer_port_query(const struct dp_netdev_port *port,
536 struct dpif_port *dpif_port)
538 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
539 dpif_port->type = xstrdup(port->type);
540 dpif_port->port_no = port->port_no;
544 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
545 struct dpif_port *dpif_port)
547 struct dp_netdev *dp = get_dp_netdev(dpif);
548 struct dp_netdev_port *port;
551 error = get_port_by_number(dp, port_no, &port);
552 if (!error && dpif_port) {
553 answer_port_query(port, dpif_port);
559 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
560 struct dpif_port *dpif_port)
562 struct dp_netdev *dp = get_dp_netdev(dpif);
563 struct dp_netdev_port *port;
566 error = get_port_by_name(dp, devname, &port);
567 if (!error && dpif_port) {
568 answer_port_query(port, dpif_port);
574 dpif_netdev_get_max_ports(const struct dpif *dpif OVS_UNUSED)
576 return u32_to_odp(MAX_PORTS);
580 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
582 hmap_remove(&dp->flow_table, &flow->node);
588 dp_netdev_flow_flush(struct dp_netdev *dp)
590 struct dp_netdev_flow *flow, *next;
592 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
593 dp_netdev_free_flow(dp, flow);
598 dpif_netdev_flow_flush(struct dpif *dpif)
600 struct dp_netdev *dp = get_dp_netdev(dpif);
601 dp_netdev_flow_flush(dp);
605 struct dp_netdev_port_state {
611 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
613 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
618 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
619 struct dpif_port *dpif_port)
621 struct dp_netdev_port_state *state = state_;
622 struct dp_netdev *dp = get_dp_netdev(dpif);
625 for (port_idx = odp_to_u32(state->port_no);
626 port_idx < MAX_PORTS; port_idx++) {
627 struct dp_netdev_port *port = dp->ports[port_idx];
630 state->name = xstrdup(netdev_get_name(port->netdev));
631 dpif_port->name = state->name;
632 dpif_port->type = port->type;
633 dpif_port->port_no = port->port_no;
634 state->port_no = u32_to_odp(port_idx + 1);
642 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
644 struct dp_netdev_port_state *state = state_;
651 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
653 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
654 if (dpif->dp_serial != dpif->dp->serial) {
655 dpif->dp_serial = dpif->dp->serial;
663 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
665 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
666 if (dpif->dp_serial != dpif->dp->serial) {
667 poll_immediate_wake();
671 static struct dp_netdev_flow *
672 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
674 struct dp_netdev_flow *flow;
676 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
677 if (flow_equal(&flow->key, key)) {
685 get_dpif_flow_stats(struct dp_netdev_flow *flow, struct dpif_flow_stats *stats)
687 stats->n_packets = flow->packet_count;
688 stats->n_bytes = flow->byte_count;
689 stats->used = flow->used;
690 stats->tcp_flags = flow->tcp_flags;
694 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
699 if (odp_flow_key_to_flow(key, key_len, flow) != ODP_FIT_PERFECT) {
700 /* This should not happen: it indicates that odp_flow_key_from_flow()
701 * and odp_flow_key_to_flow() disagree on the acceptable form of a
702 * flow. Log the problem as an error, with enough details to enable
704 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
706 if (!VLOG_DROP_ERR(&rl)) {
710 odp_flow_key_format(key, key_len, &s);
711 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
718 in_port = flow->in_port.odp_port;
719 if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
727 dpif_netdev_flow_get(const struct dpif *dpif,
728 const struct nlattr *nl_key, size_t nl_key_len,
729 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
731 struct dp_netdev *dp = get_dp_netdev(dpif);
732 struct dp_netdev_flow *flow;
736 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
741 flow = dp_netdev_lookup_flow(dp, &key);
747 get_dpif_flow_stats(flow, stats);
750 *actionsp = ofpbuf_clone_data(flow->actions, flow->actions_len);
756 set_flow_actions(struct dp_netdev_flow *flow,
757 const struct nlattr *actions, size_t actions_len)
759 flow->actions = xrealloc(flow->actions, actions_len);
760 flow->actions_len = actions_len;
761 memcpy(flow->actions, actions, actions_len);
766 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *key,
767 const struct nlattr *actions, size_t actions_len)
769 struct dp_netdev_flow *flow;
772 flow = xzalloc(sizeof *flow);
775 error = set_flow_actions(flow, actions, actions_len);
781 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
786 clear_stats(struct dp_netdev_flow *flow)
789 flow->packet_count = 0;
790 flow->byte_count = 0;
795 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
797 struct dp_netdev *dp = get_dp_netdev(dpif);
798 struct dp_netdev_flow *flow;
802 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &key);
807 flow = dp_netdev_lookup_flow(dp, &key);
809 if (put->flags & DPIF_FP_CREATE) {
810 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
812 memset(put->stats, 0, sizeof *put->stats);
814 return dp_netdev_flow_add(dp, &key, put->actions,
823 if (put->flags & DPIF_FP_MODIFY) {
824 int error = set_flow_actions(flow, put->actions, put->actions_len);
827 get_dpif_flow_stats(flow, put->stats);
829 if (put->flags & DPIF_FP_ZERO_STATS) {
841 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
843 struct dp_netdev *dp = get_dp_netdev(dpif);
844 struct dp_netdev_flow *flow;
848 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
853 flow = dp_netdev_lookup_flow(dp, &key);
856 get_dpif_flow_stats(flow, del->stats);
858 dp_netdev_free_flow(dp, flow);
865 struct dp_netdev_flow_state {
868 struct nlattr *actions;
869 struct odputil_keybuf keybuf;
870 struct dpif_flow_stats stats;
874 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
876 struct dp_netdev_flow_state *state;
878 *statep = state = xmalloc(sizeof *state);
881 state->actions = NULL;
886 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
887 const struct nlattr **key, size_t *key_len,
888 const struct nlattr **mask, size_t *mask_len,
889 const struct nlattr **actions, size_t *actions_len,
890 const struct dpif_flow_stats **stats)
892 struct dp_netdev_flow_state *state = state_;
893 struct dp_netdev *dp = get_dp_netdev(dpif);
894 struct dp_netdev_flow *flow;
895 struct hmap_node *node;
897 node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
902 flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
907 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
908 odp_flow_key_from_flow(&buf, &flow->key, flow->key.in_port.odp_port);
920 free(state->actions);
921 state->actions = xmemdup(flow->actions, flow->actions_len);
923 *actions = state->actions;
924 *actions_len = flow->actions_len;
928 get_dpif_flow_stats(flow, &state->stats);
929 *stats = &state->stats;
936 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
938 struct dp_netdev_flow_state *state = state_;
940 free(state->actions);
946 dpif_netdev_execute(struct dpif *dpif, const struct dpif_execute *execute)
948 struct dp_netdev *dp = get_dp_netdev(dpif);
953 if (execute->packet->size < ETH_HEADER_LEN ||
954 execute->packet->size > UINT16_MAX) {
958 /* Make a deep copy of 'packet', because we might modify its data. */
959 ofpbuf_init(©, DP_NETDEV_HEADROOM + execute->packet->size);
960 ofpbuf_reserve(©, DP_NETDEV_HEADROOM);
961 ofpbuf_put(©, execute->packet->data, execute->packet->size);
963 flow_extract(©, 0, 0, NULL, NULL, &key);
964 error = dpif_netdev_flow_from_nlattrs(execute->key, execute->key_len,
967 dp_netdev_execute_actions(dp, ©, &key,
968 execute->actions, execute->actions_len);
971 ofpbuf_uninit(©);
976 dpif_netdev_recv_set(struct dpif *dpif OVS_UNUSED, bool enable OVS_UNUSED)
982 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
983 uint32_t queue_id, uint32_t *priority)
985 *priority = queue_id;
989 static struct dp_netdev_queue *
990 find_nonempty_queue(struct dpif *dpif)
992 struct dp_netdev *dp = get_dp_netdev(dpif);
995 for (i = 0; i < N_QUEUES; i++) {
996 struct dp_netdev_queue *q = &dp->queues[i];
997 if (q->head != q->tail) {
1005 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
1008 struct dp_netdev_queue *q = find_nonempty_queue(dpif);
1010 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1012 *upcall = u->upcall;
1013 upcall->packet = buf;
1025 dpif_netdev_recv_wait(struct dpif *dpif)
1027 if (find_nonempty_queue(dpif)) {
1028 poll_immediate_wake();
1030 /* No messages ready to be received, and dp_wait() will ensure that we
1031 * wake up to queue new messages, so there is nothing to do. */
1036 dpif_netdev_recv_purge(struct dpif *dpif)
1038 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1039 dp_netdev_purge_queues(dpif_netdev->dp);
1043 dp_netdev_flow_used(struct dp_netdev_flow *flow, const struct ofpbuf *packet)
1045 flow->used = time_msec();
1046 flow->packet_count++;
1047 flow->byte_count += packet->size;
1048 flow->tcp_flags |= packet_get_tcp_flags(packet, &flow->key);
1052 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1053 struct ofpbuf *packet, uint32_t skb_priority,
1054 uint32_t skb_mark, const struct flow_tnl *tnl)
1056 struct dp_netdev_flow *flow;
1058 union flow_in_port in_port_;
1060 if (packet->size < ETH_HEADER_LEN) {
1063 in_port_.odp_port = port->port_no;
1064 flow_extract(packet, skb_priority, skb_mark, tnl, &in_port_, &key);
1065 flow = dp_netdev_lookup_flow(dp, &key);
1067 dp_netdev_flow_used(flow, packet);
1068 dp_netdev_execute_actions(dp, packet, &key,
1069 flow->actions, flow->actions_len);
1073 dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, NULL);
1078 dpif_netdev_run(struct dpif *dpif)
1080 struct dp_netdev *dp = get_dp_netdev(dpif);
1081 struct dp_netdev_port *port;
1082 struct ofpbuf packet;
1084 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
1086 LIST_FOR_EACH (port, node, &dp->port_list) {
1089 /* Reset packet contents. */
1090 ofpbuf_clear(&packet);
1091 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1093 error = port->rx ? netdev_rx_recv(port->rx, &packet) : EOPNOTSUPP;
1095 dp_netdev_port_input(dp, port, &packet, 0, 0, NULL);
1096 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1097 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1099 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1100 netdev_get_name(port->netdev), ovs_strerror(error));
1103 ofpbuf_uninit(&packet);
1107 dpif_netdev_wait(struct dpif *dpif)
1109 struct dp_netdev *dp = get_dp_netdev(dpif);
1110 struct dp_netdev_port *port;
1112 LIST_FOR_EACH (port, node, &dp->port_list) {
1114 netdev_rx_wait(port->rx);
1120 dp_netdev_output_port(void *dp_, struct ofpbuf *packet, uint32_t out_port)
1122 struct dp_netdev *dp = dp_;
1123 struct dp_netdev_port *p = dp->ports[out_port];
1125 netdev_send(p->netdev, packet);
1130 dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
1131 int queue_no, const struct flow *flow,
1132 const struct nlattr *userdata)
1134 struct dp_netdev_queue *q = &dp->queues[queue_no];
1135 if (q->head - q->tail < MAX_QUEUE_LEN) {
1136 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
1137 struct dpif_upcall *upcall = &u->upcall;
1138 struct ofpbuf *buf = &u->buf;
1141 upcall->type = queue_no;
1143 /* Allocate buffer big enough for everything. */
1144 buf_size = ODPUTIL_FLOW_KEY_BYTES + 2 + packet->size;
1146 buf_size += NLA_ALIGN(userdata->nla_len);
1148 ofpbuf_init(buf, buf_size);
1151 odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
1152 upcall->key = buf->data;
1153 upcall->key_len = buf->size;
1157 upcall->userdata = ofpbuf_put(buf, userdata,
1158 NLA_ALIGN(userdata->nla_len));
1163 * We adjust 'data' and 'size' in 'buf' so that only the packet itself
1164 * is visible in 'upcall->packet'. The ODP flow and (if present)
1165 * userdata become part of the headroom. */
1166 ofpbuf_put_zeros(buf, 2);
1167 buf->data = ofpbuf_put(buf, packet->data, packet->size);
1168 buf->size = packet->size;
1169 upcall->packet = buf;
1179 dp_netdev_action_userspace(void *dp, struct ofpbuf *packet,
1180 const struct flow *key,
1181 const struct nlattr *userdata)
1183 dp_netdev_output_userspace(dp, packet, DPIF_UC_ACTION, key, userdata);
1187 dp_netdev_execute_actions(struct dp_netdev *dp,
1188 struct ofpbuf *packet, struct flow *key,
1189 const struct nlattr *actions,
1192 odp_execute_actions(dp, packet, key, actions, actions_len,
1193 dp_netdev_output_port, dp_netdev_action_userspace);
1196 const struct dpif_class dpif_netdev_class = {
1198 dpif_netdev_enumerate,
1199 dpif_netdev_port_open_type,
1202 dpif_netdev_destroy,
1205 dpif_netdev_get_stats,
1206 dpif_netdev_port_add,
1207 dpif_netdev_port_del,
1208 dpif_netdev_port_query_by_number,
1209 dpif_netdev_port_query_by_name,
1210 dpif_netdev_get_max_ports,
1211 NULL, /* port_get_pid */
1212 dpif_netdev_port_dump_start,
1213 dpif_netdev_port_dump_next,
1214 dpif_netdev_port_dump_done,
1215 dpif_netdev_port_poll,
1216 dpif_netdev_port_poll_wait,
1217 dpif_netdev_flow_get,
1218 dpif_netdev_flow_put,
1219 dpif_netdev_flow_del,
1220 dpif_netdev_flow_flush,
1221 dpif_netdev_flow_dump_start,
1222 dpif_netdev_flow_dump_next,
1223 dpif_netdev_flow_dump_done,
1224 dpif_netdev_execute,
1226 dpif_netdev_recv_set,
1227 dpif_netdev_queue_to_priority,
1229 dpif_netdev_recv_wait,
1230 dpif_netdev_recv_purge,
1234 dpif_dummy_register__(const char *type)
1236 struct dpif_class *class;
1238 class = xmalloc(sizeof *class);
1239 *class = dpif_netdev_class;
1240 class->type = xstrdup(type);
1241 dp_register_provider(class);
1245 dpif_dummy_register(bool override)
1252 dp_enumerate_types(&types);
1253 SSET_FOR_EACH (type, &types) {
1254 if (!dp_unregister_provider(type)) {
1255 dpif_dummy_register__(type);
1258 sset_destroy(&types);
1261 dpif_dummy_register__("dummy");