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"
59 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
61 /* Configuration parameters. */
62 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
63 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
65 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
66 * headers to be aligned on a 4-byte boundary. */
67 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
70 enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
71 enum { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
72 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
73 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
75 struct dp_netdev_upcall {
76 struct dpif_upcall upcall; /* Queued upcall information. */
77 struct ofpbuf buf; /* ofpbuf instance for upcall.packet. */
80 struct dp_netdev_queue {
81 struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN];
82 unsigned int head, tail;
85 /* Datapath based on the network device interface from netdev.h. */
87 const struct dpif_class *class;
91 int max_mtu; /* Maximum MTU of any port added so far. */
93 struct dp_netdev_queue queues[N_QUEUES];
94 struct hmap flow_table; /* Flow table. */
97 long long int n_hit; /* Number of flow table matches. */
98 long long int n_missed; /* Number of flow table misses. */
99 long long int n_lost; /* Number of misses not passed to client. */
102 struct dp_netdev_port *ports[MAX_PORTS];
103 struct list port_list;
107 /* A port in a netdev-based datapath. */
108 struct dp_netdev_port {
109 odp_port_t port_no; /* Index into dp_netdev's 'ports'. */
110 struct list node; /* Element in dp_netdev's 'port_list'. */
111 struct netdev *netdev;
112 struct netdev_saved_flags *sf;
113 struct netdev_rx *rx;
114 char *type; /* Port type as requested by user. */
117 /* A flow in dp_netdev's 'flow_table'. */
118 struct dp_netdev_flow {
119 struct hmap_node node; /* Element in dp_netdev's 'flow_table'. */
123 long long int used; /* Last used time, in monotonic msecs. */
124 long long int packet_count; /* Number of packets matched. */
125 long long int byte_count; /* Number of bytes matched. */
126 uint8_t tcp_flags; /* Bitwise-OR of seen tcp_flags values. */
129 struct nlattr *actions;
133 /* Interface to netdev-based datapath. */
136 struct dp_netdev *dp;
137 unsigned int dp_serial;
140 /* All netdev-based datapaths. */
141 static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
143 /* Global lock for all data. */
144 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
146 static int get_port_by_number(struct dp_netdev *, odp_port_t port_no,
147 struct dp_netdev_port **portp);
148 static int get_port_by_name(struct dp_netdev *, const char *devname,
149 struct dp_netdev_port **portp);
150 static void dp_netdev_free(struct dp_netdev *);
151 static void dp_netdev_flow_flush(struct dp_netdev *);
152 static int do_add_port(struct dp_netdev *, const char *devname,
153 const char *type, odp_port_t port_no);
154 static int do_del_port(struct dp_netdev *, odp_port_t port_no);
155 static int dpif_netdev_open(const struct dpif_class *, const char *name,
156 bool create, struct dpif **);
157 static int dp_netdev_output_userspace(struct dp_netdev *, const struct ofpbuf *,
158 int queue_no, const struct flow *,
159 const struct nlattr *userdata);
160 static void dp_netdev_execute_actions(struct dp_netdev *,
161 struct ofpbuf *, struct flow *,
162 const struct nlattr *actions,
164 static void dp_netdev_port_input(struct dp_netdev *dp,
165 struct dp_netdev_port *port,
166 struct ofpbuf *packet, uint32_t skb_priority,
167 uint32_t skb_mark, const struct flow_tnl *tnl);
169 static struct dpif_netdev *
170 dpif_netdev_cast(const struct dpif *dpif)
172 ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
173 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
176 static struct dp_netdev *
177 get_dp_netdev(const struct dpif *dpif)
179 return dpif_netdev_cast(dpif)->dp;
183 dpif_netdev_enumerate(struct sset *all_dps)
185 struct shash_node *node;
187 ovs_mutex_lock(&dp_netdev_mutex);
188 SHASH_FOR_EACH(node, &dp_netdevs) {
189 sset_add(all_dps, node->name);
191 ovs_mutex_unlock(&dp_netdev_mutex);
197 dpif_netdev_class_is_dummy(const struct dpif_class *class)
199 return class != &dpif_netdev_class;
203 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
205 return strcmp(type, "internal") ? type
206 : dpif_netdev_class_is_dummy(class) ? "dummy"
211 create_dpif_netdev(struct dp_netdev *dp)
213 uint16_t netflow_id = hash_string(dp->name, 0);
214 struct dpif_netdev *dpif;
218 dpif = xmalloc(sizeof *dpif);
219 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
221 dpif->dp_serial = dp->serial;
226 /* Choose an unused, non-zero port number and return it on success.
227 * Return ODPP_NONE on failure. */
229 choose_port(struct dp_netdev *dp, const char *name)
233 if (dp->class != &dpif_netdev_class) {
237 /* If the port name begins with "br", start the number search at
238 * 100 to make writing tests easier. */
239 if (!strncmp(name, "br", 2)) {
243 /* If the port name contains a number, try to assign that port number.
244 * This can make writing unit tests easier because port numbers are
246 for (p = name; *p != '\0'; p++) {
247 if (isdigit((unsigned char) *p)) {
248 port_no = start_no + strtol(p, NULL, 10);
249 if (port_no > 0 && port_no < MAX_PORTS
250 && !dp->ports[port_no]) {
251 return u32_to_odp(port_no);
258 for (port_no = 1; port_no < MAX_PORTS; port_no++) {
259 if (!dp->ports[port_no]) {
260 return u32_to_odp(port_no);
268 create_dp_netdev(const char *name, const struct dpif_class *class,
269 struct dp_netdev **dpp)
271 struct dp_netdev *dp;
275 dp = xzalloc(sizeof *dp);
277 dp->name = xstrdup(name);
279 dp->max_mtu = ETH_PAYLOAD_MAX;
280 for (i = 0; i < N_QUEUES; i++) {
281 dp->queues[i].head = dp->queues[i].tail = 0;
283 hmap_init(&dp->flow_table);
284 list_init(&dp->port_list);
286 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
292 shash_add(&dp_netdevs, name, dp);
299 dpif_netdev_open(const struct dpif_class *class, const char *name,
300 bool create, struct dpif **dpifp)
302 struct dp_netdev *dp;
305 ovs_mutex_lock(&dp_netdev_mutex);
306 dp = shash_find_data(&dp_netdevs, name);
308 error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
310 error = (dp->class != class ? EINVAL
315 *dpifp = create_dpif_netdev(dp);
317 ovs_mutex_unlock(&dp_netdev_mutex);
323 dp_netdev_purge_queues(struct dp_netdev *dp)
327 for (i = 0; i < N_QUEUES; i++) {
328 struct dp_netdev_queue *q = &dp->queues[i];
330 while (q->tail != q->head) {
331 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
332 ofpbuf_uninit(&u->buf);
338 dp_netdev_free(struct dp_netdev *dp)
340 struct dp_netdev_port *port, *next;
342 dp_netdev_flow_flush(dp);
343 LIST_FOR_EACH_SAFE (port, next, node, &dp->port_list) {
344 do_del_port(dp, port->port_no);
346 dp_netdev_purge_queues(dp);
347 hmap_destroy(&dp->flow_table);
353 dpif_netdev_close(struct dpif *dpif)
355 struct dp_netdev *dp = get_dp_netdev(dpif);
357 ovs_mutex_lock(&dp_netdev_mutex);
359 ovs_assert(dp->open_cnt > 0);
360 if (--dp->open_cnt == 0 && dp->destroyed) {
361 shash_find_and_delete(&dp_netdevs, dp->name);
366 ovs_mutex_unlock(&dp_netdev_mutex);
370 dpif_netdev_destroy(struct dpif *dpif)
372 struct dp_netdev *dp = get_dp_netdev(dpif);
374 ovs_mutex_lock(&dp_netdev_mutex);
375 dp->destroyed = true;
376 ovs_mutex_unlock(&dp_netdev_mutex);
382 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
384 struct dp_netdev *dp = get_dp_netdev(dpif);
386 ovs_mutex_lock(&dp_netdev_mutex);
387 stats->n_flows = hmap_count(&dp->flow_table);
388 stats->n_hit = dp->n_hit;
389 stats->n_missed = dp->n_missed;
390 stats->n_lost = dp->n_lost;
391 ovs_mutex_unlock(&dp_netdev_mutex);
397 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
400 struct netdev_saved_flags *sf;
401 struct dp_netdev_port *port;
402 struct netdev *netdev;
403 struct netdev_rx *rx;
404 const char *open_type;
408 /* XXX reject devices already in some dp_netdev. */
410 /* Open and validate network device. */
411 open_type = dpif_netdev_port_open_type(dp->class, type);
412 error = netdev_open(devname, open_type, &netdev);
416 /* XXX reject loopback devices */
417 /* XXX reject non-Ethernet devices */
419 error = netdev_rx_open(netdev, &rx);
421 && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
422 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
423 devname, ovs_strerror(errno));
424 netdev_close(netdev);
428 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
431 netdev_close(netdev);
435 port = xmalloc(sizeof *port);
436 port->port_no = port_no;
437 port->netdev = netdev;
440 port->type = xstrdup(type);
442 error = netdev_get_mtu(netdev, &mtu);
443 if (!error && mtu > dp->max_mtu) {
447 list_push_back(&dp->port_list, &port->node);
448 dp->ports[odp_to_u32(port_no)] = port;
455 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
456 odp_port_t *port_nop)
458 struct dp_netdev *dp = get_dp_netdev(dpif);
459 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
460 const char *dpif_port;
464 ovs_mutex_lock(&dp_netdev_mutex);
465 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
466 if (*port_nop != ODPP_NONE) {
467 uint32_t port_idx = odp_to_u32(*port_nop);
468 if (port_idx >= MAX_PORTS) {
470 } else if (dp->ports[port_idx]) {
477 port_no = choose_port(dp, dpif_port);
478 error = port_no == ODPP_NONE ? EFBIG : 0;
482 error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
484 ovs_mutex_unlock(&dp_netdev_mutex);
490 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
492 struct dp_netdev *dp = get_dp_netdev(dpif);
495 ovs_mutex_lock(&dp_netdev_mutex);
496 error = port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
497 ovs_mutex_unlock(&dp_netdev_mutex);
503 is_valid_port_number(odp_port_t port_no)
505 return odp_to_u32(port_no) < MAX_PORTS;
509 get_port_by_number(struct dp_netdev *dp,
510 odp_port_t port_no, struct dp_netdev_port **portp)
512 if (!is_valid_port_number(port_no)) {
516 *portp = dp->ports[odp_to_u32(port_no)];
517 return *portp ? 0 : ENOENT;
522 get_port_by_name(struct dp_netdev *dp,
523 const char *devname, struct dp_netdev_port **portp)
525 struct dp_netdev_port *port;
527 LIST_FOR_EACH (port, node, &dp->port_list) {
528 if (!strcmp(netdev_get_name(port->netdev), devname)) {
537 do_del_port(struct dp_netdev *dp, odp_port_t port_no)
539 struct dp_netdev_port *port;
542 error = get_port_by_number(dp, port_no, &port);
547 list_remove(&port->node);
548 dp->ports[odp_to_u32(port_no)] = NULL;
551 netdev_close(port->netdev);
552 netdev_restore_flags(port->sf);
553 netdev_rx_close(port->rx);
561 answer_port_query(const struct dp_netdev_port *port,
562 struct dpif_port *dpif_port)
564 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
565 dpif_port->type = xstrdup(port->type);
566 dpif_port->port_no = port->port_no;
570 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
571 struct dpif_port *dpif_port)
573 struct dp_netdev *dp = get_dp_netdev(dpif);
574 struct dp_netdev_port *port;
577 ovs_mutex_lock(&dp_netdev_mutex);
578 error = get_port_by_number(dp, port_no, &port);
579 if (!error && dpif_port) {
580 answer_port_query(port, dpif_port);
582 ovs_mutex_unlock(&dp_netdev_mutex);
588 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
589 struct dpif_port *dpif_port)
591 struct dp_netdev *dp = get_dp_netdev(dpif);
592 struct dp_netdev_port *port;
595 ovs_mutex_lock(&dp_netdev_mutex);
596 error = get_port_by_name(dp, devname, &port);
597 if (!error && dpif_port) {
598 answer_port_query(port, dpif_port);
600 ovs_mutex_unlock(&dp_netdev_mutex);
606 dpif_netdev_get_max_ports(const struct dpif *dpif OVS_UNUSED)
608 return u32_to_odp(MAX_PORTS);
612 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
614 hmap_remove(&dp->flow_table, &flow->node);
620 dp_netdev_flow_flush(struct dp_netdev *dp)
622 struct dp_netdev_flow *flow, *next;
624 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
625 dp_netdev_free_flow(dp, flow);
630 dpif_netdev_flow_flush(struct dpif *dpif)
632 struct dp_netdev *dp = get_dp_netdev(dpif);
634 ovs_mutex_lock(&dp_netdev_mutex);
635 dp_netdev_flow_flush(dp);
636 ovs_mutex_unlock(&dp_netdev_mutex);
641 struct dp_netdev_port_state {
647 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
649 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
654 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
655 struct dpif_port *dpif_port)
657 struct dp_netdev_port_state *state = state_;
658 struct dp_netdev *dp = get_dp_netdev(dpif);
661 ovs_mutex_lock(&dp_netdev_mutex);
662 for (port_idx = odp_to_u32(state->port_no);
663 port_idx < MAX_PORTS; port_idx++) {
664 struct dp_netdev_port *port = dp->ports[port_idx];
667 state->name = xstrdup(netdev_get_name(port->netdev));
668 dpif_port->name = state->name;
669 dpif_port->type = port->type;
670 dpif_port->port_no = port->port_no;
671 state->port_no = u32_to_odp(port_idx + 1);
672 ovs_mutex_unlock(&dp_netdev_mutex);
677 ovs_mutex_unlock(&dp_netdev_mutex);
683 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
685 struct dp_netdev_port_state *state = state_;
692 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
694 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
697 ovs_mutex_lock(&dp_netdev_mutex);
698 if (dpif->dp_serial != dpif->dp->serial) {
699 dpif->dp_serial = dpif->dp->serial;
704 ovs_mutex_unlock(&dp_netdev_mutex);
710 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
712 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
714 /* XXX In a multithreaded process, there is a race window between this
715 * function and the poll_block() in one thread and a change in
716 * dpif->dp->serial in another thread. */
718 ovs_mutex_lock(&dp_netdev_mutex);
719 if (dpif->dp_serial != dpif->dp->serial) {
720 poll_immediate_wake();
722 ovs_mutex_unlock(&dp_netdev_mutex);
725 static struct dp_netdev_flow *
726 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
728 struct dp_netdev_flow *flow;
730 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
731 if (flow_equal(&flow->key, key)) {
739 get_dpif_flow_stats(struct dp_netdev_flow *flow, struct dpif_flow_stats *stats)
741 stats->n_packets = flow->packet_count;
742 stats->n_bytes = flow->byte_count;
743 stats->used = flow->used;
744 stats->tcp_flags = flow->tcp_flags;
748 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
753 if (odp_flow_key_to_flow(key, key_len, flow) != ODP_FIT_PERFECT) {
754 /* This should not happen: it indicates that odp_flow_key_from_flow()
755 * and odp_flow_key_to_flow() disagree on the acceptable form of a
756 * flow. Log the problem as an error, with enough details to enable
758 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
760 if (!VLOG_DROP_ERR(&rl)) {
764 odp_flow_key_format(key, key_len, &s);
765 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
772 in_port = flow->in_port.odp_port;
773 if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
781 dpif_netdev_flow_get(const struct dpif *dpif,
782 const struct nlattr *nl_key, size_t nl_key_len,
783 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
785 struct dp_netdev *dp = get_dp_netdev(dpif);
786 struct dp_netdev_flow *flow;
790 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
795 ovs_mutex_lock(&dp_netdev_mutex);
796 flow = dp_netdev_lookup_flow(dp, &key);
799 get_dpif_flow_stats(flow, stats);
802 *actionsp = ofpbuf_clone_data(flow->actions, flow->actions_len);
807 ovs_mutex_unlock(&dp_netdev_mutex);
813 set_flow_actions(struct dp_netdev_flow *flow,
814 const struct nlattr *actions, size_t actions_len)
816 flow->actions = xrealloc(flow->actions, actions_len);
817 flow->actions_len = actions_len;
818 memcpy(flow->actions, actions, actions_len);
823 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *key,
824 const struct nlattr *actions, size_t actions_len)
826 struct dp_netdev_flow *flow;
829 flow = xzalloc(sizeof *flow);
832 error = set_flow_actions(flow, actions, actions_len);
838 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
843 clear_stats(struct dp_netdev_flow *flow)
846 flow->packet_count = 0;
847 flow->byte_count = 0;
852 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
854 struct dp_netdev *dp = get_dp_netdev(dpif);
855 struct dp_netdev_flow *flow;
859 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &key);
864 ovs_mutex_lock(&dp_netdev_mutex);
865 flow = dp_netdev_lookup_flow(dp, &key);
867 if (put->flags & DPIF_FP_CREATE) {
868 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
870 memset(put->stats, 0, sizeof *put->stats);
872 error = dp_netdev_flow_add(dp, &key, put->actions,
881 if (put->flags & DPIF_FP_MODIFY) {
882 error = set_flow_actions(flow, put->actions, put->actions_len);
885 get_dpif_flow_stats(flow, put->stats);
887 if (put->flags & DPIF_FP_ZERO_STATS) {
895 ovs_mutex_unlock(&dp_netdev_mutex);
901 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
903 struct dp_netdev *dp = get_dp_netdev(dpif);
904 struct dp_netdev_flow *flow;
908 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
913 ovs_mutex_lock(&dp_netdev_mutex);
914 flow = dp_netdev_lookup_flow(dp, &key);
917 get_dpif_flow_stats(flow, del->stats);
919 dp_netdev_free_flow(dp, flow);
923 ovs_mutex_unlock(&dp_netdev_mutex);
928 struct dp_netdev_flow_state {
931 struct nlattr *actions;
932 struct odputil_keybuf keybuf;
933 struct dpif_flow_stats stats;
937 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
939 struct dp_netdev_flow_state *state;
941 *statep = state = xmalloc(sizeof *state);
944 state->actions = NULL;
949 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
950 const struct nlattr **key, size_t *key_len,
951 const struct nlattr **mask, size_t *mask_len,
952 const struct nlattr **actions, size_t *actions_len,
953 const struct dpif_flow_stats **stats)
955 struct dp_netdev_flow_state *state = state_;
956 struct dp_netdev *dp = get_dp_netdev(dpif);
957 struct dp_netdev_flow *flow;
958 struct hmap_node *node;
960 ovs_mutex_lock(&dp_netdev_mutex);
961 node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
963 ovs_mutex_unlock(&dp_netdev_mutex);
967 flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
972 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
973 odp_flow_key_from_flow(&buf, &flow->key, flow->key.in_port.odp_port);
985 free(state->actions);
986 state->actions = xmemdup(flow->actions, flow->actions_len);
988 *actions = state->actions;
989 *actions_len = flow->actions_len;
993 get_dpif_flow_stats(flow, &state->stats);
994 *stats = &state->stats;
997 ovs_mutex_unlock(&dp_netdev_mutex);
1002 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1004 struct dp_netdev_flow_state *state = state_;
1006 free(state->actions);
1012 dpif_netdev_execute(struct dpif *dpif, const struct dpif_execute *execute)
1014 struct dp_netdev *dp = get_dp_netdev(dpif);
1019 if (execute->packet->size < ETH_HEADER_LEN ||
1020 execute->packet->size > UINT16_MAX) {
1024 /* Make a deep copy of 'packet', because we might modify its data. */
1025 ofpbuf_init(©, DP_NETDEV_HEADROOM + execute->packet->size);
1026 ofpbuf_reserve(©, DP_NETDEV_HEADROOM);
1027 ofpbuf_put(©, execute->packet->data, execute->packet->size);
1029 flow_extract(©, 0, 0, NULL, NULL, &key);
1030 error = dpif_netdev_flow_from_nlattrs(execute->key, execute->key_len,
1033 ovs_mutex_lock(&dp_netdev_mutex);
1034 dp_netdev_execute_actions(dp, ©, &key,
1035 execute->actions, execute->actions_len);
1036 ovs_mutex_unlock(&dp_netdev_mutex);
1039 ofpbuf_uninit(©);
1044 dpif_netdev_recv_set(struct dpif *dpif OVS_UNUSED, bool enable OVS_UNUSED)
1050 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1051 uint32_t queue_id, uint32_t *priority)
1053 *priority = queue_id;
1057 static struct dp_netdev_queue *
1058 find_nonempty_queue(struct dpif *dpif)
1060 struct dp_netdev *dp = get_dp_netdev(dpif);
1063 for (i = 0; i < N_QUEUES; i++) {
1064 struct dp_netdev_queue *q = &dp->queues[i];
1065 if (q->head != q->tail) {
1073 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
1076 struct dp_netdev_queue *q;
1079 ovs_mutex_lock(&dp_netdev_mutex);
1080 q = find_nonempty_queue(dpif);
1082 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1084 *upcall = u->upcall;
1085 upcall->packet = buf;
1094 ovs_mutex_unlock(&dp_netdev_mutex);
1100 dpif_netdev_recv_wait(struct dpif *dpif)
1102 /* XXX In a multithreaded process, there is a race window between this
1103 * function and the poll_block() in one thread and a packet being queued in
1104 * another thread. */
1106 ovs_mutex_lock(&dp_netdev_mutex);
1107 if (find_nonempty_queue(dpif)) {
1108 poll_immediate_wake();
1110 ovs_mutex_unlock(&dp_netdev_mutex);
1114 dpif_netdev_recv_purge(struct dpif *dpif)
1116 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1117 ovs_mutex_lock(&dp_netdev_mutex);
1118 dp_netdev_purge_queues(dpif_netdev->dp);
1119 ovs_mutex_unlock(&dp_netdev_mutex);
1123 dp_netdev_flow_used(struct dp_netdev_flow *flow, const struct ofpbuf *packet)
1125 flow->used = time_msec();
1126 flow->packet_count++;
1127 flow->byte_count += packet->size;
1128 flow->tcp_flags |= packet_get_tcp_flags(packet, &flow->key);
1132 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1133 struct ofpbuf *packet, uint32_t skb_priority,
1134 uint32_t skb_mark, const struct flow_tnl *tnl)
1136 struct dp_netdev_flow *flow;
1138 union flow_in_port in_port_;
1140 if (packet->size < ETH_HEADER_LEN) {
1143 in_port_.odp_port = port->port_no;
1144 flow_extract(packet, skb_priority, skb_mark, tnl, &in_port_, &key);
1145 flow = dp_netdev_lookup_flow(dp, &key);
1147 dp_netdev_flow_used(flow, packet);
1148 dp_netdev_execute_actions(dp, packet, &key,
1149 flow->actions, flow->actions_len);
1153 dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, NULL);
1158 dpif_netdev_run(struct dpif *dpif)
1160 struct dp_netdev_port *port;
1161 struct dp_netdev *dp;
1162 struct ofpbuf packet;
1164 ovs_mutex_lock(&dp_netdev_mutex);
1165 dp = get_dp_netdev(dpif);
1166 ofpbuf_init(&packet,
1167 DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + dp->max_mtu);
1169 LIST_FOR_EACH (port, node, &dp->port_list) {
1172 /* Reset packet contents. */
1173 ofpbuf_clear(&packet);
1174 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1176 error = port->rx ? netdev_rx_recv(port->rx, &packet) : EOPNOTSUPP;
1178 dp_netdev_port_input(dp, port, &packet, 0, 0, NULL);
1179 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1180 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1182 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1183 netdev_get_name(port->netdev), ovs_strerror(error));
1186 ofpbuf_uninit(&packet);
1187 ovs_mutex_unlock(&dp_netdev_mutex);
1191 dpif_netdev_wait(struct dpif *dpif)
1193 struct dp_netdev_port *port;
1195 /* There is a race here, if thread A calls dpif_netdev_wait(dpif) and
1196 * thread B calls dpif_port_add(dpif) or dpif_port_remove(dpif) before
1197 * A makes it to poll_block().
1199 * But I think it doesn't matter:
1201 * - In the dpif_port_add() case, A will not wake up when a packet
1202 * arrives on the new port, but this would also happen if the
1203 * ordering were reversed.
1205 * - In the dpif_port_remove() case, A might wake up spuriously, but
1206 * that is harmless. */
1208 ovs_mutex_lock(&dp_netdev_mutex);
1209 LIST_FOR_EACH (port, node, &get_dp_netdev(dpif)->port_list) {
1211 netdev_rx_wait(port->rx);
1214 ovs_mutex_unlock(&dp_netdev_mutex);
1218 dp_netdev_output_port(void *dp_, struct ofpbuf *packet, uint32_t out_port)
1220 struct dp_netdev *dp = dp_;
1221 struct dp_netdev_port *p = dp->ports[out_port];
1223 netdev_send(p->netdev, packet);
1228 dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
1229 int queue_no, const struct flow *flow,
1230 const struct nlattr *userdata)
1232 struct dp_netdev_queue *q = &dp->queues[queue_no];
1233 if (q->head - q->tail < MAX_QUEUE_LEN) {
1234 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
1235 struct dpif_upcall *upcall = &u->upcall;
1236 struct ofpbuf *buf = &u->buf;
1239 upcall->type = queue_no;
1241 /* Allocate buffer big enough for everything. */
1242 buf_size = ODPUTIL_FLOW_KEY_BYTES + 2 + packet->size;
1244 buf_size += NLA_ALIGN(userdata->nla_len);
1246 ofpbuf_init(buf, buf_size);
1249 odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
1250 upcall->key = buf->data;
1251 upcall->key_len = buf->size;
1255 upcall->userdata = ofpbuf_put(buf, userdata,
1256 NLA_ALIGN(userdata->nla_len));
1261 * We adjust 'data' and 'size' in 'buf' so that only the packet itself
1262 * is visible in 'upcall->packet'. The ODP flow and (if present)
1263 * userdata become part of the headroom. */
1264 ofpbuf_put_zeros(buf, 2);
1265 buf->data = ofpbuf_put(buf, packet->data, packet->size);
1266 buf->size = packet->size;
1267 upcall->packet = buf;
1277 dp_netdev_action_userspace(void *dp, struct ofpbuf *packet,
1278 const struct flow *key,
1279 const struct nlattr *userdata)
1281 dp_netdev_output_userspace(dp, packet, DPIF_UC_ACTION, key, userdata);
1285 dp_netdev_execute_actions(struct dp_netdev *dp,
1286 struct ofpbuf *packet, struct flow *key,
1287 const struct nlattr *actions,
1290 odp_execute_actions(dp, packet, key, actions, actions_len,
1291 dp_netdev_output_port, dp_netdev_action_userspace);
1294 const struct dpif_class dpif_netdev_class = {
1296 dpif_netdev_enumerate,
1297 dpif_netdev_port_open_type,
1300 dpif_netdev_destroy,
1303 dpif_netdev_get_stats,
1304 dpif_netdev_port_add,
1305 dpif_netdev_port_del,
1306 dpif_netdev_port_query_by_number,
1307 dpif_netdev_port_query_by_name,
1308 dpif_netdev_get_max_ports,
1309 NULL, /* port_get_pid */
1310 dpif_netdev_port_dump_start,
1311 dpif_netdev_port_dump_next,
1312 dpif_netdev_port_dump_done,
1313 dpif_netdev_port_poll,
1314 dpif_netdev_port_poll_wait,
1315 dpif_netdev_flow_get,
1316 dpif_netdev_flow_put,
1317 dpif_netdev_flow_del,
1318 dpif_netdev_flow_flush,
1319 dpif_netdev_flow_dump_start,
1320 dpif_netdev_flow_dump_next,
1321 dpif_netdev_flow_dump_done,
1322 dpif_netdev_execute,
1324 dpif_netdev_recv_set,
1325 dpif_netdev_queue_to_priority,
1327 dpif_netdev_recv_wait,
1328 dpif_netdev_recv_purge,
1332 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
1333 const char *argv[], void *aux OVS_UNUSED)
1335 struct dp_netdev_port *port;
1336 struct dp_netdev *dp;
1339 dp = shash_find_data(&dp_netdevs, argv[1]);
1340 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
1341 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
1345 if (get_port_by_name(dp, argv[2], &port)) {
1346 unixctl_command_reply_error(conn, "unknown port");
1350 port_no = atoi(argv[3]);
1351 if (port_no <= 0 || port_no >= MAX_PORTS) {
1352 unixctl_command_reply_error(conn, "bad port number");
1355 if (dp->ports[port_no]) {
1356 unixctl_command_reply_error(conn, "port number already in use");
1359 dp->ports[odp_to_u32(port->port_no)] = NULL;
1360 dp->ports[port_no] = port;
1361 port->port_no = u32_to_odp(port_no);
1363 unixctl_command_reply(conn, NULL);
1367 dpif_dummy_register__(const char *type)
1369 struct dpif_class *class;
1371 class = xmalloc(sizeof *class);
1372 *class = dpif_netdev_class;
1373 class->type = xstrdup(type);
1374 dp_register_provider(class);
1378 dpif_dummy_register(bool override)
1385 dp_enumerate_types(&types);
1386 SSET_FOR_EACH (type, &types) {
1387 if (!dp_unregister_provider(type)) {
1388 dpif_dummy_register__(type);
1391 sset_destroy(&types);
1394 dpif_dummy_register__("dummy");
1396 unixctl_command_register("dpif-dummy/change-port-number",
1397 "DP PORT NEW-NUMBER",
1398 3, 3, dpif_dummy_change_port_number, NULL);