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_class_is_planetlab(const struct dpif_class *class)
200 return class == &dpif_planetlab_class;
204 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
206 return strcmp(type, "internal") ? type
207 : dpif_netdev_class_is_planetlab(class) ? "pltap"
208 : dpif_netdev_class_is_dummy(class) ? "dummy"
213 create_dpif_netdev(struct dp_netdev *dp)
215 uint16_t netflow_id = hash_string(dp->name, 0);
216 struct dpif_netdev *dpif;
220 dpif = xmalloc(sizeof *dpif);
221 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
223 dpif->dp_serial = dp->serial;
228 /* Choose an unused, non-zero port number and return it on success.
229 * Return ODPP_NONE on failure. */
231 choose_port(struct dp_netdev *dp, const char *name)
235 if (dp->class != &dpif_netdev_class &&
236 dp->class != &dpif_planetlab_class) {
240 /* If the port name begins with "br", start the number search at
241 * 100 to make writing tests easier. */
242 if (!strncmp(name, "br", 2)) {
246 /* If the port name contains a number, try to assign that port number.
247 * This can make writing unit tests easier because port numbers are
249 for (p = name; *p != '\0'; p++) {
250 if (isdigit((unsigned char) *p)) {
251 port_no = start_no + strtol(p, NULL, 10);
252 if (port_no > 0 && port_no < MAX_PORTS
253 && !dp->ports[port_no]) {
254 return u32_to_odp(port_no);
261 for (port_no = 1; port_no < MAX_PORTS; port_no++) {
262 if (!dp->ports[port_no]) {
263 return u32_to_odp(port_no);
271 create_dp_netdev(const char *name, const struct dpif_class *class,
272 struct dp_netdev **dpp)
274 struct dp_netdev *dp;
278 dp = xzalloc(sizeof *dp);
280 dp->name = xstrdup(name);
282 for (i = 0; i < N_QUEUES; i++) {
283 dp->queues[i].head = dp->queues[i].tail = 0;
285 hmap_init(&dp->flow_table);
286 list_init(&dp->port_list);
288 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
294 shash_add(&dp_netdevs, name, dp);
301 dpif_netdev_open(const struct dpif_class *class, const char *name,
302 bool create, struct dpif **dpifp)
304 struct dp_netdev *dp;
306 dp = shash_find_data(&dp_netdevs, name);
311 int error = create_dp_netdev(name, class, &dp);
315 ovs_assert(dp != NULL);
318 if (dp->class != class) {
325 *dpifp = create_dpif_netdev(dp);
330 dp_netdev_purge_queues(struct dp_netdev *dp)
334 for (i = 0; i < N_QUEUES; i++) {
335 struct dp_netdev_queue *q = &dp->queues[i];
337 while (q->tail != q->head) {
338 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
339 ofpbuf_uninit(&u->buf);
345 dp_netdev_free(struct dp_netdev *dp)
347 struct dp_netdev_port *port, *next;
349 dp_netdev_flow_flush(dp);
350 LIST_FOR_EACH_SAFE (port, next, node, &dp->port_list) {
351 do_del_port(dp, port->port_no);
353 dp_netdev_purge_queues(dp);
354 hmap_destroy(&dp->flow_table);
360 dpif_netdev_close(struct dpif *dpif)
362 struct dp_netdev *dp = get_dp_netdev(dpif);
363 ovs_assert(dp->open_cnt > 0);
364 if (--dp->open_cnt == 0 && dp->destroyed) {
365 shash_find_and_delete(&dp_netdevs, dp->name);
372 dpif_netdev_destroy(struct dpif *dpif)
374 struct dp_netdev *dp = get_dp_netdev(dpif);
375 dp->destroyed = true;
380 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
382 struct dp_netdev *dp = get_dp_netdev(dpif);
383 stats->n_flows = hmap_count(&dp->flow_table);
384 stats->n_hit = dp->n_hit;
385 stats->n_missed = dp->n_missed;
386 stats->n_lost = dp->n_lost;
391 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
394 struct netdev_saved_flags *sf;
395 struct dp_netdev_port *port;
396 struct netdev *netdev;
397 struct netdev_rx *rx;
398 const char *open_type;
402 /* XXX reject devices already in some dp_netdev. */
404 /* Open and validate network device. */
405 open_type = dpif_netdev_port_open_type(dp->class, type);
406 error = netdev_open(devname, open_type, &netdev);
410 /* XXX reject loopback devices */
411 /* XXX reject non-Ethernet devices */
413 error = netdev_rx_open(netdev, &rx);
415 && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
416 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
417 devname, ovs_strerror(errno));
418 netdev_close(netdev);
422 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
425 netdev_close(netdev);
429 port = xmalloc(sizeof *port);
430 port->port_no = port_no;
431 port->netdev = netdev;
434 port->type = xstrdup(type);
436 error = netdev_get_mtu(netdev, &mtu);
437 if (!error && mtu > max_mtu) {
441 list_push_back(&dp->port_list, &port->node);
442 dp->ports[odp_to_u32(port_no)] = port;
449 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
450 odp_port_t *port_nop)
452 struct dp_netdev *dp = get_dp_netdev(dpif);
453 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
454 const char *dpif_port;
457 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
458 if (*port_nop != ODPP_NONE) {
459 uint32_t port_idx = odp_to_u32(*port_nop);
460 if (port_idx >= MAX_PORTS) {
462 } else if (dp->ports[port_idx]) {
467 port_no = choose_port(dp, dpif_port);
469 if (port_no != ODPP_NONE) {
471 return do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
477 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
479 struct dp_netdev *dp = get_dp_netdev(dpif);
480 return (port_no == ODPP_LOCAL ?
481 EINVAL : do_del_port(dp, port_no));
485 is_valid_port_number(odp_port_t port_no)
487 return odp_to_u32(port_no) < MAX_PORTS;
491 get_port_by_number(struct dp_netdev *dp,
492 odp_port_t port_no, struct dp_netdev_port **portp)
494 if (!is_valid_port_number(port_no)) {
498 *portp = dp->ports[odp_to_u32(port_no)];
499 return *portp ? 0 : ENOENT;
504 get_port_by_name(struct dp_netdev *dp,
505 const char *devname, struct dp_netdev_port **portp)
507 struct dp_netdev_port *port;
509 LIST_FOR_EACH (port, node, &dp->port_list) {
510 if (!strcmp(netdev_get_name(port->netdev), devname)) {
519 do_del_port(struct dp_netdev *dp, odp_port_t port_no)
521 struct dp_netdev_port *port;
524 error = get_port_by_number(dp, port_no, &port);
529 list_remove(&port->node);
530 dp->ports[odp_to_u32(port_no)] = NULL;
533 netdev_close(port->netdev);
534 netdev_restore_flags(port->sf);
535 netdev_rx_close(port->rx);
543 answer_port_query(const struct dp_netdev_port *port,
544 struct dpif_port *dpif_port)
546 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
547 dpif_port->type = xstrdup(port->type);
548 dpif_port->port_no = port->port_no;
552 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
553 struct dpif_port *dpif_port)
555 struct dp_netdev *dp = get_dp_netdev(dpif);
556 struct dp_netdev_port *port;
559 error = get_port_by_number(dp, port_no, &port);
560 if (!error && dpif_port) {
561 answer_port_query(port, dpif_port);
567 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
568 struct dpif_port *dpif_port)
570 struct dp_netdev *dp = get_dp_netdev(dpif);
571 struct dp_netdev_port *port;
574 error = get_port_by_name(dp, devname, &port);
575 if (!error && dpif_port) {
576 answer_port_query(port, dpif_port);
582 dpif_netdev_get_max_ports(const struct dpif *dpif OVS_UNUSED)
584 return u32_to_odp(MAX_PORTS);
588 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
590 hmap_remove(&dp->flow_table, &flow->node);
596 dp_netdev_flow_flush(struct dp_netdev *dp)
598 struct dp_netdev_flow *flow, *next;
600 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
601 dp_netdev_free_flow(dp, flow);
606 dpif_netdev_flow_flush(struct dpif *dpif)
608 struct dp_netdev *dp = get_dp_netdev(dpif);
609 dp_netdev_flow_flush(dp);
613 struct dp_netdev_port_state {
619 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
621 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
626 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
627 struct dpif_port *dpif_port)
629 struct dp_netdev_port_state *state = state_;
630 struct dp_netdev *dp = get_dp_netdev(dpif);
633 for (port_idx = odp_to_u32(state->port_no);
634 port_idx < MAX_PORTS; port_idx++) {
635 struct dp_netdev_port *port = dp->ports[port_idx];
638 state->name = xstrdup(netdev_get_name(port->netdev));
639 dpif_port->name = state->name;
640 dpif_port->type = port->type;
641 dpif_port->port_no = port->port_no;
642 state->port_no = u32_to_odp(port_idx + 1);
650 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
652 struct dp_netdev_port_state *state = state_;
659 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
661 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
662 if (dpif->dp_serial != dpif->dp->serial) {
663 dpif->dp_serial = dpif->dp->serial;
671 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
673 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
674 if (dpif->dp_serial != dpif->dp->serial) {
675 poll_immediate_wake();
679 static struct dp_netdev_flow *
680 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
682 struct dp_netdev_flow *flow;
684 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
685 if (flow_equal(&flow->key, key)) {
693 get_dpif_flow_stats(struct dp_netdev_flow *flow, struct dpif_flow_stats *stats)
695 stats->n_packets = flow->packet_count;
696 stats->n_bytes = flow->byte_count;
697 stats->used = flow->used;
698 stats->tcp_flags = flow->tcp_flags;
702 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
705 if (odp_flow_key_to_flow(key, key_len, flow) != ODP_FIT_PERFECT) {
706 /* This should not happen: it indicates that odp_flow_key_from_flow()
707 * and odp_flow_key_to_flow() disagree on the acceptable form of a
708 * flow. Log the problem as an error, with enough details to enable
710 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
712 if (!VLOG_DROP_ERR(&rl)) {
716 odp_flow_key_format(key, key_len, &s);
717 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
724 if (!is_valid_port_number(flow->in_port.odp_port)) {
732 dpif_netdev_flow_get(const struct dpif *dpif,
733 const struct nlattr *nl_key, size_t nl_key_len,
734 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
736 struct dp_netdev *dp = get_dp_netdev(dpif);
737 struct dp_netdev_flow *flow;
741 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
746 flow = dp_netdev_lookup_flow(dp, &key);
752 get_dpif_flow_stats(flow, stats);
755 *actionsp = ofpbuf_clone_data(flow->actions, flow->actions_len);
761 set_flow_actions(struct dp_netdev_flow *flow,
762 const struct nlattr *actions, size_t actions_len)
764 flow->actions = xrealloc(flow->actions, actions_len);
765 flow->actions_len = actions_len;
766 memcpy(flow->actions, actions, actions_len);
771 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *key,
772 const struct nlattr *actions, size_t actions_len)
774 struct dp_netdev_flow *flow;
777 flow = xzalloc(sizeof *flow);
780 error = set_flow_actions(flow, actions, actions_len);
786 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
791 clear_stats(struct dp_netdev_flow *flow)
794 flow->packet_count = 0;
795 flow->byte_count = 0;
800 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
802 struct dp_netdev *dp = get_dp_netdev(dpif);
803 struct dp_netdev_flow *flow;
807 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &key);
812 flow = dp_netdev_lookup_flow(dp, &key);
814 if (put->flags & DPIF_FP_CREATE) {
815 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
817 memset(put->stats, 0, sizeof *put->stats);
819 return dp_netdev_flow_add(dp, &key, put->actions,
828 if (put->flags & DPIF_FP_MODIFY) {
829 int error = set_flow_actions(flow, put->actions, put->actions_len);
832 get_dpif_flow_stats(flow, put->stats);
834 if (put->flags & DPIF_FP_ZERO_STATS) {
846 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
848 struct dp_netdev *dp = get_dp_netdev(dpif);
849 struct dp_netdev_flow *flow;
853 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
858 flow = dp_netdev_lookup_flow(dp, &key);
861 get_dpif_flow_stats(flow, del->stats);
863 dp_netdev_free_flow(dp, flow);
870 struct dp_netdev_flow_state {
873 struct nlattr *actions;
874 struct odputil_keybuf keybuf;
875 struct dpif_flow_stats stats;
879 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
881 struct dp_netdev_flow_state *state;
883 *statep = state = xmalloc(sizeof *state);
886 state->actions = NULL;
891 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
892 const struct nlattr **key, size_t *key_len,
893 const struct nlattr **mask, size_t *mask_len,
894 const struct nlattr **actions, size_t *actions_len,
895 const struct dpif_flow_stats **stats)
897 struct dp_netdev_flow_state *state = state_;
898 struct dp_netdev *dp = get_dp_netdev(dpif);
899 struct dp_netdev_flow *flow;
900 struct hmap_node *node;
902 node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
907 flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
912 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
913 odp_flow_key_from_flow(&buf, &flow->key, flow->key.in_port.odp_port);
925 free(state->actions);
926 state->actions = xmemdup(flow->actions, flow->actions_len);
928 *actions = state->actions;
929 *actions_len = flow->actions_len;
933 get_dpif_flow_stats(flow, &state->stats);
934 *stats = &state->stats;
941 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
943 struct dp_netdev_flow_state *state = state_;
945 free(state->actions);
951 dpif_netdev_execute(struct dpif *dpif, const struct dpif_execute *execute)
953 struct dp_netdev *dp = get_dp_netdev(dpif);
958 if (execute->packet->size < ETH_HEADER_LEN ||
959 execute->packet->size > UINT16_MAX) {
963 /* Make a deep copy of 'packet', because we might modify its data. */
964 ofpbuf_init(©, DP_NETDEV_HEADROOM + execute->packet->size);
965 ofpbuf_reserve(©, DP_NETDEV_HEADROOM);
966 ofpbuf_put(©, execute->packet->data, execute->packet->size);
968 flow_extract(©, 0, 0, NULL, NULL, &key);
969 error = dpif_netdev_flow_from_nlattrs(execute->key, execute->key_len,
972 dp_netdev_execute_actions(dp, ©, &key,
973 execute->actions, execute->actions_len);
976 ofpbuf_uninit(©);
981 dpif_netdev_recv_set(struct dpif *dpif OVS_UNUSED, bool enable OVS_UNUSED)
987 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
988 uint32_t queue_id, uint32_t *priority)
990 *priority = queue_id;
994 static struct dp_netdev_queue *
995 find_nonempty_queue(struct dpif *dpif)
997 struct dp_netdev *dp = get_dp_netdev(dpif);
1000 for (i = 0; i < N_QUEUES; i++) {
1001 struct dp_netdev_queue *q = &dp->queues[i];
1002 if (q->head != q->tail) {
1010 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
1013 struct dp_netdev_queue *q = find_nonempty_queue(dpif);
1015 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1017 *upcall = u->upcall;
1018 upcall->packet = buf;
1030 dpif_netdev_recv_wait(struct dpif *dpif)
1032 if (find_nonempty_queue(dpif)) {
1033 poll_immediate_wake();
1035 /* No messages ready to be received, and dp_wait() will ensure that we
1036 * wake up to queue new messages, so there is nothing to do. */
1041 dpif_netdev_recv_purge(struct dpif *dpif)
1043 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1044 dp_netdev_purge_queues(dpif_netdev->dp);
1048 dp_netdev_flow_used(struct dp_netdev_flow *flow, const struct ofpbuf *packet)
1050 flow->used = time_msec();
1051 flow->packet_count++;
1052 flow->byte_count += packet->size;
1053 flow->tcp_flags |= packet_get_tcp_flags(packet, &flow->key);
1057 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1058 struct ofpbuf *packet, uint32_t skb_priority,
1059 uint32_t skb_mark, const struct flow_tnl *tnl)
1061 struct dp_netdev_flow *flow;
1063 union flow_in_port in_port_;
1065 if (packet->size < ETH_HEADER_LEN) {
1068 in_port_.odp_port = port->port_no;
1069 flow_extract(packet, skb_priority, skb_mark, tnl, &in_port_, &key);
1070 flow = dp_netdev_lookup_flow(dp, &key);
1072 dp_netdev_flow_used(flow, packet);
1073 dp_netdev_execute_actions(dp, packet, &key,
1074 flow->actions, flow->actions_len);
1078 dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, NULL);
1083 dpif_netdev_run(struct dpif *dpif)
1085 struct dp_netdev *dp = get_dp_netdev(dpif);
1086 struct dp_netdev_port *port;
1087 struct ofpbuf packet;
1089 ofpbuf_init(&packet, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
1091 LIST_FOR_EACH (port, node, &dp->port_list) {
1094 /* Reset packet contents. */
1095 ofpbuf_clear(&packet);
1096 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1098 error = port->rx ? netdev_rx_recv(port->rx, &packet) : EOPNOTSUPP;
1100 dp_netdev_port_input(dp, port, &packet, 0, 0, NULL);
1101 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1102 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1104 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1105 netdev_get_name(port->netdev), ovs_strerror(error));
1108 ofpbuf_uninit(&packet);
1112 dpif_netdev_wait(struct dpif *dpif)
1114 struct dp_netdev *dp = get_dp_netdev(dpif);
1115 struct dp_netdev_port *port;
1117 LIST_FOR_EACH (port, node, &dp->port_list) {
1119 netdev_rx_wait(port->rx);
1125 dp_netdev_output_port(void *dp_, struct ofpbuf *packet, uint32_t out_port)
1127 struct dp_netdev *dp = dp_;
1128 struct dp_netdev_port *p = dp->ports[out_port];
1130 netdev_send(p->netdev, packet);
1135 dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
1136 int queue_no, const struct flow *flow,
1137 const struct nlattr *userdata)
1139 struct dp_netdev_queue *q = &dp->queues[queue_no];
1140 if (q->head - q->tail < MAX_QUEUE_LEN) {
1141 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
1142 struct dpif_upcall *upcall = &u->upcall;
1143 struct ofpbuf *buf = &u->buf;
1146 upcall->type = queue_no;
1148 /* Allocate buffer big enough for everything. */
1149 buf_size = ODPUTIL_FLOW_KEY_BYTES + 2 + packet->size;
1151 buf_size += NLA_ALIGN(userdata->nla_len);
1153 ofpbuf_init(buf, buf_size);
1156 odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
1157 upcall->key = buf->data;
1158 upcall->key_len = buf->size;
1162 upcall->userdata = ofpbuf_put(buf, userdata,
1163 NLA_ALIGN(userdata->nla_len));
1168 * We adjust 'data' and 'size' in 'buf' so that only the packet itself
1169 * is visible in 'upcall->packet'. The ODP flow and (if present)
1170 * userdata become part of the headroom. */
1171 ofpbuf_put_zeros(buf, 2);
1172 buf->data = ofpbuf_put(buf, packet->data, packet->size);
1173 buf->size = packet->size;
1174 upcall->packet = buf;
1184 dp_netdev_action_userspace(void *dp, struct ofpbuf *packet,
1185 const struct flow *key,
1186 const struct nlattr *userdata)
1188 dp_netdev_output_userspace(dp, packet, DPIF_UC_ACTION, key, userdata);
1192 dp_netdev_execute_actions(struct dp_netdev *dp,
1193 struct ofpbuf *packet, struct flow *key,
1194 const struct nlattr *actions,
1197 odp_execute_actions(dp, packet, key, actions, actions_len,
1198 dp_netdev_output_port, dp_netdev_action_userspace);
1201 #define DPIF_NETDEV_CLASS_FUNCTIONS \
1202 dpif_netdev_enumerate, \
1203 dpif_netdev_port_open_type, \
1205 dpif_netdev_close, \
1206 dpif_netdev_destroy, \
1209 dpif_netdev_get_stats, \
1210 dpif_netdev_port_add, \
1211 dpif_netdev_port_del, \
1212 dpif_netdev_port_query_by_number, \
1213 dpif_netdev_port_query_by_name, \
1214 dpif_netdev_get_max_ports, \
1215 NULL, /* port_get_pid */ \
1216 dpif_netdev_port_dump_start, \
1217 dpif_netdev_port_dump_next, \
1218 dpif_netdev_port_dump_done, \
1219 dpif_netdev_port_poll, \
1220 dpif_netdev_port_poll_wait, \
1221 dpif_netdev_flow_get, \
1222 dpif_netdev_flow_put, \
1223 dpif_netdev_flow_del, \
1224 dpif_netdev_flow_flush, \
1225 dpif_netdev_flow_dump_start, \
1226 dpif_netdev_flow_dump_next, \
1227 dpif_netdev_flow_dump_done, \
1228 dpif_netdev_execute, \
1229 NULL, /* operate */ \
1230 dpif_netdev_recv_set, \
1231 dpif_netdev_queue_to_priority, \
1233 dpif_netdev_recv_wait, \
1234 dpif_netdev_recv_purge, \
1236 const struct dpif_class dpif_netdev_class = {
1238 DPIF_NETDEV_CLASS_FUNCTIONS
1241 const struct dpif_class dpif_planetlab_class = {
1243 DPIF_NETDEV_CLASS_FUNCTIONS
1247 dpif_dummy_register__(const char *type)
1249 struct dpif_class *class;
1251 class = xmalloc(sizeof *class);
1252 *class = dpif_netdev_class;
1253 class->type = xstrdup(type);
1254 dp_register_provider(class);
1258 dpif_dummy_register(bool override)
1265 dp_enumerate_types(&types);
1266 SSET_FOR_EACH (type, &types) {
1267 if (!dp_unregister_provider(type)) {
1268 dpif_dummy_register__(type);
1271 sset_destroy(&types);
1274 dpif_dummy_register__("dummy");