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"
60 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
62 /* Configuration parameters. */
63 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
64 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
66 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
67 * headers to be aligned on a 4-byte boundary. */
68 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
71 enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
72 enum { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
73 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
74 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
76 struct dp_netdev_upcall {
77 struct dpif_upcall upcall; /* Queued upcall information. */
78 struct ofpbuf buf; /* ofpbuf instance for upcall.packet. */
81 struct dp_netdev_queue {
82 struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN];
83 unsigned int head, tail;
86 /* Datapath based on the network device interface from netdev.h. */
88 const struct dpif_class *class;
92 int max_mtu; /* Maximum MTU of any port added so far. */
94 struct dp_netdev_queue queues[N_QUEUES];
95 struct hmap flow_table; /* Flow table. */
96 struct seq *queue_seq; /* Incremented whenever a packet is queued. */
99 long long int n_hit; /* Number of flow table matches. */
100 long long int n_missed; /* Number of flow table misses. */
101 long long int n_lost; /* Number of misses not passed to client. */
104 struct dp_netdev_port *ports[MAX_PORTS];
105 struct list port_list;
106 struct seq *port_seq; /* Incremented whenever a port changes. */
109 /* A port in a netdev-based datapath. */
110 struct dp_netdev_port {
111 odp_port_t port_no; /* Index into dp_netdev's 'ports'. */
112 struct list node; /* Element in dp_netdev's 'port_list'. */
113 struct netdev *netdev;
114 struct netdev_saved_flags *sf;
115 struct netdev_rx *rx;
116 char *type; /* Port type as requested by user. */
119 /* A flow in dp_netdev's 'flow_table'. */
120 struct dp_netdev_flow {
121 struct hmap_node node; /* Element in dp_netdev's 'flow_table'. */
125 long long int used; /* Last used time, in monotonic msecs. */
126 long long int packet_count; /* Number of packets matched. */
127 long long int byte_count; /* Number of bytes matched. */
128 uint8_t tcp_flags; /* Bitwise-OR of seen tcp_flags values. */
131 struct nlattr *actions;
135 /* Interface to netdev-based datapath. */
138 struct dp_netdev *dp;
139 uint64_t last_port_seq;
142 /* All netdev-based datapaths. */
143 static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
145 /* Global lock for all data. */
146 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
148 static int get_port_by_number(struct dp_netdev *, odp_port_t port_no,
149 struct dp_netdev_port **portp);
150 static int get_port_by_name(struct dp_netdev *, const char *devname,
151 struct dp_netdev_port **portp);
152 static void dp_netdev_free(struct dp_netdev *);
153 static void dp_netdev_flow_flush(struct dp_netdev *);
154 static int do_add_port(struct dp_netdev *, const char *devname,
155 const char *type, odp_port_t port_no);
156 static int do_del_port(struct dp_netdev *, odp_port_t port_no);
157 static int dpif_netdev_open(const struct dpif_class *, const char *name,
158 bool create, struct dpif **);
159 static int dp_netdev_output_userspace(struct dp_netdev *, const struct ofpbuf *,
160 int queue_no, const struct flow *,
161 const struct nlattr *userdata);
162 static void dp_netdev_execute_actions(struct dp_netdev *,
163 struct ofpbuf *, struct flow *,
164 const struct nlattr *actions,
166 static void dp_netdev_port_input(struct dp_netdev *dp,
167 struct dp_netdev_port *port,
168 struct ofpbuf *packet, uint32_t skb_priority,
169 uint32_t pkt_mark, const struct flow_tnl *tnl);
171 static struct dpif_netdev *
172 dpif_netdev_cast(const struct dpif *dpif)
174 ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
175 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
178 static struct dp_netdev *
179 get_dp_netdev(const struct dpif *dpif)
181 return dpif_netdev_cast(dpif)->dp;
185 dpif_netdev_enumerate(struct sset *all_dps)
187 struct shash_node *node;
189 ovs_mutex_lock(&dp_netdev_mutex);
190 SHASH_FOR_EACH(node, &dp_netdevs) {
191 sset_add(all_dps, node->name);
193 ovs_mutex_unlock(&dp_netdev_mutex);
199 dpif_netdev_class_is_dummy(const struct dpif_class *class)
201 return class != &dpif_netdev_class;
205 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
207 return strcmp(type, "internal") ? type
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->last_port_seq = seq_read(dp->port_seq);
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) {
239 /* If the port name begins with "br", start the number search at
240 * 100 to make writing tests easier. */
241 if (!strncmp(name, "br", 2)) {
245 /* If the port name contains a number, try to assign that port number.
246 * This can make writing unit tests easier because port numbers are
248 for (p = name; *p != '\0'; p++) {
249 if (isdigit((unsigned char) *p)) {
250 port_no = start_no + strtol(p, NULL, 10);
251 if (port_no > 0 && port_no < MAX_PORTS
252 && !dp->ports[port_no]) {
253 return u32_to_odp(port_no);
260 for (port_no = 1; port_no < MAX_PORTS; port_no++) {
261 if (!dp->ports[port_no]) {
262 return u32_to_odp(port_no);
270 create_dp_netdev(const char *name, const struct dpif_class *class,
271 struct dp_netdev **dpp)
273 struct dp_netdev *dp;
277 dp = xzalloc(sizeof *dp);
279 dp->name = xstrdup(name);
281 dp->max_mtu = ETH_PAYLOAD_MAX;
282 for (i = 0; i < N_QUEUES; i++) {
283 dp->queues[i].head = dp->queues[i].tail = 0;
285 dp->queue_seq = seq_create();
286 hmap_init(&dp->flow_table);
287 list_init(&dp->port_list);
288 dp->port_seq = seq_create();
290 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
296 shash_add(&dp_netdevs, name, dp);
303 dpif_netdev_open(const struct dpif_class *class, const char *name,
304 bool create, struct dpif **dpifp)
306 struct dp_netdev *dp;
309 ovs_mutex_lock(&dp_netdev_mutex);
310 dp = shash_find_data(&dp_netdevs, name);
312 error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
314 error = (dp->class != class ? EINVAL
319 *dpifp = create_dpif_netdev(dp);
321 ovs_mutex_unlock(&dp_netdev_mutex);
327 dp_netdev_purge_queues(struct dp_netdev *dp)
331 for (i = 0; i < N_QUEUES; i++) {
332 struct dp_netdev_queue *q = &dp->queues[i];
334 while (q->tail != q->head) {
335 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
336 ofpbuf_uninit(&u->buf);
342 dp_netdev_free(struct dp_netdev *dp)
344 struct dp_netdev_port *port, *next;
346 dp_netdev_flow_flush(dp);
347 LIST_FOR_EACH_SAFE (port, next, node, &dp->port_list) {
348 do_del_port(dp, port->port_no);
350 dp_netdev_purge_queues(dp);
351 seq_destroy(dp->queue_seq);
352 hmap_destroy(&dp->flow_table);
353 seq_destroy(dp->port_seq);
359 dpif_netdev_close(struct dpif *dpif)
361 struct dp_netdev *dp = get_dp_netdev(dpif);
363 ovs_mutex_lock(&dp_netdev_mutex);
365 ovs_assert(dp->open_cnt > 0);
366 if (--dp->open_cnt == 0 && dp->destroyed) {
367 shash_find_and_delete(&dp_netdevs, dp->name);
372 ovs_mutex_unlock(&dp_netdev_mutex);
376 dpif_netdev_destroy(struct dpif *dpif)
378 struct dp_netdev *dp = get_dp_netdev(dpif);
380 ovs_mutex_lock(&dp_netdev_mutex);
381 dp->destroyed = true;
382 ovs_mutex_unlock(&dp_netdev_mutex);
388 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
390 struct dp_netdev *dp = get_dp_netdev(dpif);
392 ovs_mutex_lock(&dp_netdev_mutex);
393 stats->n_flows = hmap_count(&dp->flow_table);
394 stats->n_hit = dp->n_hit;
395 stats->n_missed = dp->n_missed;
396 stats->n_lost = dp->n_lost;
397 ovs_mutex_unlock(&dp_netdev_mutex);
403 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
406 struct netdev_saved_flags *sf;
407 struct dp_netdev_port *port;
408 struct netdev *netdev;
409 struct netdev_rx *rx;
410 enum netdev_flags flags;
411 const char *open_type;
415 /* XXX reject devices already in some dp_netdev. */
417 /* Open and validate network device. */
418 open_type = dpif_netdev_port_open_type(dp->class, type);
419 error = netdev_open(devname, open_type, &netdev);
423 /* XXX reject non-Ethernet devices */
425 netdev_get_flags(netdev, &flags);
426 if (flags & NETDEV_LOOPBACK) {
427 VLOG_ERR("%s: cannot add a loopback device", devname);
428 netdev_close(netdev);
432 error = netdev_rx_open(netdev, &rx);
434 && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
435 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
436 devname, ovs_strerror(errno));
437 netdev_close(netdev);
441 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
444 netdev_close(netdev);
448 port = xmalloc(sizeof *port);
449 port->port_no = port_no;
450 port->netdev = netdev;
453 port->type = xstrdup(type);
455 error = netdev_get_mtu(netdev, &mtu);
456 if (!error && mtu > dp->max_mtu) {
460 list_push_back(&dp->port_list, &port->node);
461 dp->ports[odp_to_u32(port_no)] = port;
462 seq_change(dp->port_seq);
468 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
469 odp_port_t *port_nop)
471 struct dp_netdev *dp = get_dp_netdev(dpif);
472 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
473 const char *dpif_port;
477 ovs_mutex_lock(&dp_netdev_mutex);
478 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
479 if (*port_nop != ODPP_NONE) {
480 uint32_t port_idx = odp_to_u32(*port_nop);
481 if (port_idx >= MAX_PORTS) {
483 } else if (dp->ports[port_idx]) {
490 port_no = choose_port(dp, dpif_port);
491 error = port_no == ODPP_NONE ? EFBIG : 0;
495 error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
497 ovs_mutex_unlock(&dp_netdev_mutex);
503 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
505 struct dp_netdev *dp = get_dp_netdev(dpif);
508 ovs_mutex_lock(&dp_netdev_mutex);
509 error = port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
510 ovs_mutex_unlock(&dp_netdev_mutex);
516 is_valid_port_number(odp_port_t port_no)
518 return odp_to_u32(port_no) < MAX_PORTS;
522 get_port_by_number(struct dp_netdev *dp,
523 odp_port_t port_no, struct dp_netdev_port **portp)
525 if (!is_valid_port_number(port_no)) {
529 *portp = dp->ports[odp_to_u32(port_no)];
530 return *portp ? 0 : ENOENT;
535 get_port_by_name(struct dp_netdev *dp,
536 const char *devname, struct dp_netdev_port **portp)
538 struct dp_netdev_port *port;
540 LIST_FOR_EACH (port, node, &dp->port_list) {
541 if (!strcmp(netdev_get_name(port->netdev), devname)) {
550 do_del_port(struct dp_netdev *dp, odp_port_t port_no)
552 struct dp_netdev_port *port;
555 error = get_port_by_number(dp, port_no, &port);
560 list_remove(&port->node);
561 dp->ports[odp_to_u32(port_no)] = NULL;
562 seq_change(dp->port_seq);
564 netdev_close(port->netdev);
565 netdev_restore_flags(port->sf);
566 netdev_rx_close(port->rx);
574 answer_port_query(const struct dp_netdev_port *port,
575 struct dpif_port *dpif_port)
577 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
578 dpif_port->type = xstrdup(port->type);
579 dpif_port->port_no = port->port_no;
583 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
584 struct dpif_port *dpif_port)
586 struct dp_netdev *dp = get_dp_netdev(dpif);
587 struct dp_netdev_port *port;
590 ovs_mutex_lock(&dp_netdev_mutex);
591 error = get_port_by_number(dp, port_no, &port);
592 if (!error && dpif_port) {
593 answer_port_query(port, dpif_port);
595 ovs_mutex_unlock(&dp_netdev_mutex);
601 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
602 struct dpif_port *dpif_port)
604 struct dp_netdev *dp = get_dp_netdev(dpif);
605 struct dp_netdev_port *port;
608 ovs_mutex_lock(&dp_netdev_mutex);
609 error = get_port_by_name(dp, devname, &port);
610 if (!error && dpif_port) {
611 answer_port_query(port, dpif_port);
613 ovs_mutex_unlock(&dp_netdev_mutex);
619 dpif_netdev_get_max_ports(const struct dpif *dpif OVS_UNUSED)
625 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
627 hmap_remove(&dp->flow_table, &flow->node);
633 dp_netdev_flow_flush(struct dp_netdev *dp)
635 struct dp_netdev_flow *flow, *next;
637 HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
638 dp_netdev_free_flow(dp, flow);
643 dpif_netdev_flow_flush(struct dpif *dpif)
645 struct dp_netdev *dp = get_dp_netdev(dpif);
647 ovs_mutex_lock(&dp_netdev_mutex);
648 dp_netdev_flow_flush(dp);
649 ovs_mutex_unlock(&dp_netdev_mutex);
654 struct dp_netdev_port_state {
660 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
662 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
667 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
668 struct dpif_port *dpif_port)
670 struct dp_netdev_port_state *state = state_;
671 struct dp_netdev *dp = get_dp_netdev(dpif);
674 ovs_mutex_lock(&dp_netdev_mutex);
675 for (port_idx = odp_to_u32(state->port_no);
676 port_idx < MAX_PORTS; port_idx++) {
677 struct dp_netdev_port *port = dp->ports[port_idx];
680 state->name = xstrdup(netdev_get_name(port->netdev));
681 dpif_port->name = state->name;
682 dpif_port->type = port->type;
683 dpif_port->port_no = port->port_no;
684 state->port_no = u32_to_odp(port_idx + 1);
685 ovs_mutex_unlock(&dp_netdev_mutex);
690 ovs_mutex_unlock(&dp_netdev_mutex);
696 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
698 struct dp_netdev_port_state *state = state_;
705 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
707 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
708 uint64_t new_port_seq;
711 ovs_mutex_lock(&dp_netdev_mutex);
712 new_port_seq = seq_read(dpif->dp->port_seq);
713 if (dpif->last_port_seq != new_port_seq) {
714 dpif->last_port_seq = new_port_seq;
719 ovs_mutex_unlock(&dp_netdev_mutex);
725 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
727 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
729 ovs_mutex_lock(&dp_netdev_mutex);
730 seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
731 ovs_mutex_unlock(&dp_netdev_mutex);
734 static struct dp_netdev_flow *
735 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
737 struct dp_netdev_flow *flow;
739 HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
740 if (flow_equal(&flow->key, key)) {
748 get_dpif_flow_stats(struct dp_netdev_flow *flow, struct dpif_flow_stats *stats)
750 stats->n_packets = flow->packet_count;
751 stats->n_bytes = flow->byte_count;
752 stats->used = flow->used;
753 stats->tcp_flags = flow->tcp_flags;
757 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
762 if (odp_flow_key_to_flow(key, key_len, flow) != ODP_FIT_PERFECT) {
763 /* This should not happen: it indicates that odp_flow_key_from_flow()
764 * and odp_flow_key_to_flow() disagree on the acceptable form of a
765 * flow. Log the problem as an error, with enough details to enable
767 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
769 if (!VLOG_DROP_ERR(&rl)) {
773 odp_flow_key_format(key, key_len, &s);
774 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
781 in_port = flow->in_port.odp_port;
782 if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
790 dpif_netdev_flow_get(const struct dpif *dpif,
791 const struct nlattr *nl_key, size_t nl_key_len,
792 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
794 struct dp_netdev *dp = get_dp_netdev(dpif);
795 struct dp_netdev_flow *flow;
799 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
804 ovs_mutex_lock(&dp_netdev_mutex);
805 flow = dp_netdev_lookup_flow(dp, &key);
808 get_dpif_flow_stats(flow, stats);
811 *actionsp = ofpbuf_clone_data(flow->actions, flow->actions_len);
816 ovs_mutex_unlock(&dp_netdev_mutex);
822 set_flow_actions(struct dp_netdev_flow *flow,
823 const struct nlattr *actions, size_t actions_len)
825 flow->actions = xrealloc(flow->actions, actions_len);
826 flow->actions_len = actions_len;
827 memcpy(flow->actions, actions, actions_len);
832 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *key,
833 const struct nlattr *actions, size_t actions_len)
835 struct dp_netdev_flow *flow;
838 flow = xzalloc(sizeof *flow);
841 error = set_flow_actions(flow, actions, actions_len);
847 hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
852 clear_stats(struct dp_netdev_flow *flow)
855 flow->packet_count = 0;
856 flow->byte_count = 0;
861 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
863 struct dp_netdev *dp = get_dp_netdev(dpif);
864 struct dp_netdev_flow *flow;
868 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &key);
873 ovs_mutex_lock(&dp_netdev_mutex);
874 flow = dp_netdev_lookup_flow(dp, &key);
876 if (put->flags & DPIF_FP_CREATE) {
877 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
879 memset(put->stats, 0, sizeof *put->stats);
881 error = dp_netdev_flow_add(dp, &key, put->actions,
890 if (put->flags & DPIF_FP_MODIFY) {
891 error = set_flow_actions(flow, put->actions, put->actions_len);
894 get_dpif_flow_stats(flow, put->stats);
896 if (put->flags & DPIF_FP_ZERO_STATS) {
904 ovs_mutex_unlock(&dp_netdev_mutex);
910 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
912 struct dp_netdev *dp = get_dp_netdev(dpif);
913 struct dp_netdev_flow *flow;
917 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
922 ovs_mutex_lock(&dp_netdev_mutex);
923 flow = dp_netdev_lookup_flow(dp, &key);
926 get_dpif_flow_stats(flow, del->stats);
928 dp_netdev_free_flow(dp, flow);
932 ovs_mutex_unlock(&dp_netdev_mutex);
937 struct dp_netdev_flow_state {
940 struct nlattr *actions;
941 struct odputil_keybuf keybuf;
942 struct dpif_flow_stats stats;
946 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
948 struct dp_netdev_flow_state *state;
950 *statep = state = xmalloc(sizeof *state);
953 state->actions = NULL;
958 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
959 const struct nlattr **key, size_t *key_len,
960 const struct nlattr **mask, size_t *mask_len,
961 const struct nlattr **actions, size_t *actions_len,
962 const struct dpif_flow_stats **stats)
964 struct dp_netdev_flow_state *state = state_;
965 struct dp_netdev *dp = get_dp_netdev(dpif);
966 struct dp_netdev_flow *flow;
967 struct hmap_node *node;
969 ovs_mutex_lock(&dp_netdev_mutex);
970 node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
972 ovs_mutex_unlock(&dp_netdev_mutex);
976 flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
981 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
982 odp_flow_key_from_flow(&buf, &flow->key, flow->key.in_port.odp_port);
994 free(state->actions);
995 state->actions = xmemdup(flow->actions, flow->actions_len);
997 *actions = state->actions;
998 *actions_len = flow->actions_len;
1002 get_dpif_flow_stats(flow, &state->stats);
1003 *stats = &state->stats;
1006 ovs_mutex_unlock(&dp_netdev_mutex);
1011 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1013 struct dp_netdev_flow_state *state = state_;
1015 free(state->actions);
1021 dpif_netdev_execute(struct dpif *dpif, const struct dpif_execute *execute)
1023 struct dp_netdev *dp = get_dp_netdev(dpif);
1028 if (execute->packet->size < ETH_HEADER_LEN ||
1029 execute->packet->size > UINT16_MAX) {
1033 /* Make a deep copy of 'packet', because we might modify its data. */
1034 ofpbuf_init(©, DP_NETDEV_HEADROOM + execute->packet->size);
1035 ofpbuf_reserve(©, DP_NETDEV_HEADROOM);
1036 ofpbuf_put(©, execute->packet->data, execute->packet->size);
1038 flow_extract(©, 0, 0, NULL, NULL, &key);
1039 error = dpif_netdev_flow_from_nlattrs(execute->key, execute->key_len,
1042 ovs_mutex_lock(&dp_netdev_mutex);
1043 dp_netdev_execute_actions(dp, ©, &key,
1044 execute->actions, execute->actions_len);
1045 ovs_mutex_unlock(&dp_netdev_mutex);
1048 ofpbuf_uninit(©);
1053 dpif_netdev_recv_set(struct dpif *dpif OVS_UNUSED, bool enable OVS_UNUSED)
1059 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1060 uint32_t queue_id, uint32_t *priority)
1062 *priority = queue_id;
1066 static struct dp_netdev_queue *
1067 find_nonempty_queue(struct dpif *dpif)
1069 struct dp_netdev *dp = get_dp_netdev(dpif);
1072 for (i = 0; i < N_QUEUES; i++) {
1073 struct dp_netdev_queue *q = &dp->queues[i];
1074 if (q->head != q->tail) {
1082 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
1085 struct dp_netdev_queue *q;
1088 ovs_mutex_lock(&dp_netdev_mutex);
1089 q = find_nonempty_queue(dpif);
1091 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1093 *upcall = u->upcall;
1094 upcall->packet = buf;
1103 ovs_mutex_unlock(&dp_netdev_mutex);
1109 dpif_netdev_recv_wait(struct dpif *dpif)
1111 struct dp_netdev *dp = get_dp_netdev(dpif);
1114 ovs_mutex_lock(&dp_netdev_mutex);
1115 seq = seq_read(dp->queue_seq);
1116 if (find_nonempty_queue(dpif)) {
1117 poll_immediate_wake();
1119 seq_wait(dp->queue_seq, seq);
1121 ovs_mutex_unlock(&dp_netdev_mutex);
1125 dpif_netdev_recv_purge(struct dpif *dpif)
1127 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1128 ovs_mutex_lock(&dp_netdev_mutex);
1129 dp_netdev_purge_queues(dpif_netdev->dp);
1130 ovs_mutex_unlock(&dp_netdev_mutex);
1134 dp_netdev_flow_used(struct dp_netdev_flow *flow, const struct ofpbuf *packet)
1136 flow->used = time_msec();
1137 flow->packet_count++;
1138 flow->byte_count += packet->size;
1139 flow->tcp_flags |= packet_get_tcp_flags(packet, &flow->key);
1143 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1144 struct ofpbuf *packet, uint32_t skb_priority,
1145 uint32_t pkt_mark, const struct flow_tnl *tnl)
1147 struct dp_netdev_flow *flow;
1149 union flow_in_port in_port_;
1151 if (packet->size < ETH_HEADER_LEN) {
1154 in_port_.odp_port = port->port_no;
1155 flow_extract(packet, skb_priority, pkt_mark, tnl, &in_port_, &key);
1156 flow = dp_netdev_lookup_flow(dp, &key);
1158 dp_netdev_flow_used(flow, packet);
1159 dp_netdev_execute_actions(dp, packet, &key,
1160 flow->actions, flow->actions_len);
1164 dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, NULL);
1169 dpif_netdev_run(struct dpif *dpif)
1171 struct dp_netdev_port *port;
1172 struct dp_netdev *dp;
1173 struct ofpbuf packet;
1175 ovs_mutex_lock(&dp_netdev_mutex);
1176 dp = get_dp_netdev(dpif);
1177 ofpbuf_init(&packet,
1178 DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + dp->max_mtu);
1180 LIST_FOR_EACH (port, node, &dp->port_list) {
1183 /* Reset packet contents. */
1184 ofpbuf_clear(&packet);
1185 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1187 error = port->rx ? netdev_rx_recv(port->rx, &packet) : EOPNOTSUPP;
1189 dp_netdev_port_input(dp, port, &packet, 0, 0, NULL);
1190 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1191 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1193 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1194 netdev_get_name(port->netdev), ovs_strerror(error));
1197 ofpbuf_uninit(&packet);
1198 ovs_mutex_unlock(&dp_netdev_mutex);
1202 dpif_netdev_wait(struct dpif *dpif)
1204 struct dp_netdev_port *port;
1206 /* There is a race here, if thread A calls dpif_netdev_wait(dpif) and
1207 * thread B calls dpif_port_add(dpif) or dpif_port_remove(dpif) before
1208 * A makes it to poll_block().
1210 * But I think it doesn't matter:
1212 * - In the dpif_port_add() case, A will not wake up when a packet
1213 * arrives on the new port, but this would also happen if the
1214 * ordering were reversed.
1216 * - In the dpif_port_remove() case, A might wake up spuriously, but
1217 * that is harmless. */
1219 ovs_mutex_lock(&dp_netdev_mutex);
1220 LIST_FOR_EACH (port, node, &get_dp_netdev(dpif)->port_list) {
1222 netdev_rx_wait(port->rx);
1225 ovs_mutex_unlock(&dp_netdev_mutex);
1229 dp_netdev_output_port(void *dp_, struct ofpbuf *packet, uint32_t out_port)
1231 struct dp_netdev *dp = dp_;
1232 struct dp_netdev_port *p = dp->ports[out_port];
1234 netdev_send(p->netdev, packet);
1239 dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
1240 int queue_no, const struct flow *flow,
1241 const struct nlattr *userdata)
1243 struct dp_netdev_queue *q = &dp->queues[queue_no];
1244 if (q->head - q->tail < MAX_QUEUE_LEN) {
1245 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
1246 struct dpif_upcall *upcall = &u->upcall;
1247 struct ofpbuf *buf = &u->buf;
1250 upcall->type = queue_no;
1252 /* Allocate buffer big enough for everything. */
1253 buf_size = ODPUTIL_FLOW_KEY_BYTES + 2 + packet->size;
1255 buf_size += NLA_ALIGN(userdata->nla_len);
1257 ofpbuf_init(buf, buf_size);
1260 odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
1261 upcall->key = buf->data;
1262 upcall->key_len = buf->size;
1266 upcall->userdata = ofpbuf_put(buf, userdata,
1267 NLA_ALIGN(userdata->nla_len));
1272 * We adjust 'data' and 'size' in 'buf' so that only the packet itself
1273 * is visible in 'upcall->packet'. The ODP flow and (if present)
1274 * userdata become part of the headroom. */
1275 ofpbuf_put_zeros(buf, 2);
1276 buf->data = ofpbuf_put(buf, packet->data, packet->size);
1277 buf->size = packet->size;
1278 upcall->packet = buf;
1280 seq_change(dp->queue_seq);
1290 dp_netdev_action_userspace(void *dp, struct ofpbuf *packet,
1291 const struct flow *key,
1292 const struct nlattr *userdata)
1294 dp_netdev_output_userspace(dp, packet, DPIF_UC_ACTION, key, userdata);
1298 dp_netdev_execute_actions(struct dp_netdev *dp,
1299 struct ofpbuf *packet, struct flow *key,
1300 const struct nlattr *actions,
1303 odp_execute_actions(dp, packet, key, actions, actions_len,
1304 dp_netdev_output_port, dp_netdev_action_userspace);
1307 const struct dpif_class dpif_netdev_class = {
1309 dpif_netdev_enumerate,
1310 dpif_netdev_port_open_type,
1313 dpif_netdev_destroy,
1316 dpif_netdev_get_stats,
1317 dpif_netdev_port_add,
1318 dpif_netdev_port_del,
1319 dpif_netdev_port_query_by_number,
1320 dpif_netdev_port_query_by_name,
1321 dpif_netdev_get_max_ports,
1322 NULL, /* port_get_pid */
1323 dpif_netdev_port_dump_start,
1324 dpif_netdev_port_dump_next,
1325 dpif_netdev_port_dump_done,
1326 dpif_netdev_port_poll,
1327 dpif_netdev_port_poll_wait,
1328 dpif_netdev_flow_get,
1329 dpif_netdev_flow_put,
1330 dpif_netdev_flow_del,
1331 dpif_netdev_flow_flush,
1332 dpif_netdev_flow_dump_start,
1333 dpif_netdev_flow_dump_next,
1334 dpif_netdev_flow_dump_done,
1335 dpif_netdev_execute,
1337 dpif_netdev_recv_set,
1338 dpif_netdev_queue_to_priority,
1340 dpif_netdev_recv_wait,
1341 dpif_netdev_recv_purge,
1345 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
1346 const char *argv[], void *aux OVS_UNUSED)
1348 struct dp_netdev_port *port;
1349 struct dp_netdev *dp;
1352 dp = shash_find_data(&dp_netdevs, argv[1]);
1353 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
1354 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
1358 if (get_port_by_name(dp, argv[2], &port)) {
1359 unixctl_command_reply_error(conn, "unknown port");
1363 port_no = atoi(argv[3]);
1364 if (port_no <= 0 || port_no >= MAX_PORTS) {
1365 unixctl_command_reply_error(conn, "bad port number");
1368 if (dp->ports[port_no]) {
1369 unixctl_command_reply_error(conn, "port number already in use");
1372 dp->ports[odp_to_u32(port->port_no)] = NULL;
1373 dp->ports[port_no] = port;
1374 port->port_no = u32_to_odp(port_no);
1375 seq_change(dp->port_seq);
1376 unixctl_command_reply(conn, NULL);
1380 dpif_dummy_register__(const char *type)
1382 struct dpif_class *class;
1384 class = xmalloc(sizeof *class);
1385 *class = dpif_netdev_class;
1386 class->type = xstrdup(type);
1387 dp_register_provider(class);
1391 dpif_dummy_register(bool override)
1398 dp_enumerate_types(&types);
1399 SSET_FOR_EACH (type, &types) {
1400 if (!dp_unregister_provider(type)) {
1401 dpif_dummy_register__(type);
1404 sset_destroy(&types);
1407 dpif_dummy_register__("dummy");
1409 unixctl_command_register("dpif-dummy/change-port-number",
1410 "DP PORT NEW-NUMBER",
1411 3, 3, dpif_dummy_change_port_number, NULL);