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>
34 #include "classifier.h"
37 #include "dpif-provider.h"
39 #include "dynamic-string.h"
44 #include "netdev-vport.h"
46 #include "odp-execute.h"
48 #include "ofp-print.h"
51 #include "poll-loop.h"
61 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
63 /* By default, choose a priority in the middle. */
64 #define NETDEV_RULE_PRIORITY 0x8000
66 /* Configuration parameters. */
67 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
68 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
70 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
71 * headers to be aligned on a 4-byte boundary. */
72 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
75 enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
76 enum { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
77 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
78 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
80 struct dp_netdev_upcall {
81 struct dpif_upcall upcall; /* Queued upcall information. */
82 struct ofpbuf buf; /* ofpbuf instance for upcall.packet. */
85 struct dp_netdev_queue {
86 struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN];
87 unsigned int head, tail;
90 /* Datapath based on the network device interface from netdev.h. */
92 const struct dpif_class *class;
96 int max_mtu; /* Maximum MTU of any port added so far. */
98 struct dp_netdev_queue queues[N_QUEUES];
99 struct classifier cls; /* Classifier. */
100 struct hmap flow_table; /* Flow table. */
101 struct seq *queue_seq; /* Incremented whenever a packet is queued. */
104 long long int n_hit; /* Number of flow table matches. */
105 long long int n_missed; /* Number of flow table misses. */
106 long long int n_lost; /* Number of misses not passed to client. */
109 struct dp_netdev_port *ports[MAX_PORTS];
110 struct list port_list;
111 struct seq *port_seq; /* Incremented whenever a port changes. */
114 /* A port in a netdev-based datapath. */
115 struct dp_netdev_port {
116 odp_port_t port_no; /* Index into dp_netdev's 'ports'. */
117 struct list node; /* Element in dp_netdev's 'port_list'. */
118 struct netdev *netdev;
119 struct netdev_saved_flags *sf;
120 struct netdev_rx *rx;
121 char *type; /* Port type as requested by user. */
124 /* A flow in dp_netdev's 'flow_table'. */
125 struct dp_netdev_flow {
126 /* Packet classification. */
127 struct cls_rule cr; /* In owning dp_netdev's 'cls'. */
129 /* Hash table index by unmasked flow.*/
130 struct hmap_node node; /* In owning dp_netdev's 'flow_table'. */
131 struct flow flow; /* The flow that created this entry. */
134 long long int used; /* Last used time, in monotonic msecs. */
135 long long int packet_count; /* Number of packets matched. */
136 long long int byte_count; /* Number of bytes matched. */
137 uint16_t tcp_flags; /* Bitwise-OR of seen tcp_flags values. */
140 struct nlattr *actions;
144 /* Interface to netdev-based datapath. */
147 struct dp_netdev *dp;
148 uint64_t last_port_seq;
151 /* All netdev-based datapaths. */
152 static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
154 /* Global lock for all data. */
155 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
157 static int get_port_by_number(struct dp_netdev *, odp_port_t port_no,
158 struct dp_netdev_port **portp);
159 static int get_port_by_name(struct dp_netdev *, const char *devname,
160 struct dp_netdev_port **portp);
161 static void dp_netdev_free(struct dp_netdev *);
162 static void dp_netdev_flow_flush(struct dp_netdev *);
163 static int do_add_port(struct dp_netdev *, const char *devname,
164 const char *type, odp_port_t port_no);
165 static int do_del_port(struct dp_netdev *, odp_port_t port_no);
166 static int dpif_netdev_open(const struct dpif_class *, const char *name,
167 bool create, struct dpif **);
168 static int dp_netdev_output_userspace(struct dp_netdev *, const struct ofpbuf *,
169 int queue_no, const struct flow *,
170 const struct nlattr *userdata);
171 static void dp_netdev_execute_actions(struct dp_netdev *, const struct flow *,
173 const struct nlattr *actions,
175 static void dp_netdev_port_input(struct dp_netdev *dp,
176 struct dp_netdev_port *port,
177 struct ofpbuf *packet, uint32_t skb_priority,
178 uint32_t pkt_mark, const struct flow_tnl *tnl);
180 static struct dpif_netdev *
181 dpif_netdev_cast(const struct dpif *dpif)
183 ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
184 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
187 static struct dp_netdev *
188 get_dp_netdev(const struct dpif *dpif)
190 return dpif_netdev_cast(dpif)->dp;
194 dpif_netdev_enumerate(struct sset *all_dps)
196 struct shash_node *node;
198 ovs_mutex_lock(&dp_netdev_mutex);
199 SHASH_FOR_EACH(node, &dp_netdevs) {
200 sset_add(all_dps, node->name);
202 ovs_mutex_unlock(&dp_netdev_mutex);
208 dpif_netdev_class_is_dummy(const struct dpif_class *class)
210 return class != &dpif_netdev_class;
214 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
216 return strcmp(type, "internal") ? type
217 : dpif_netdev_class_is_dummy(class) ? "dummy"
222 create_dpif_netdev(struct dp_netdev *dp)
224 uint16_t netflow_id = hash_string(dp->name, 0);
225 struct dpif_netdev *dpif;
229 dpif = xmalloc(sizeof *dpif);
230 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
232 dpif->last_port_seq = seq_read(dp->port_seq);
237 /* Choose an unused, non-zero port number and return it on success.
238 * Return ODPP_NONE on failure. */
240 choose_port(struct dp_netdev *dp, const char *name)
244 if (dp->class != &dpif_netdev_class) {
248 /* If the port name begins with "br", start the number search at
249 * 100 to make writing tests easier. */
250 if (!strncmp(name, "br", 2)) {
254 /* If the port name contains a number, try to assign that port number.
255 * This can make writing unit tests easier because port numbers are
257 for (p = name; *p != '\0'; p++) {
258 if (isdigit((unsigned char) *p)) {
259 port_no = start_no + strtol(p, NULL, 10);
260 if (port_no > 0 && port_no < MAX_PORTS
261 && !dp->ports[port_no]) {
262 return u32_to_odp(port_no);
269 for (port_no = 1; port_no < MAX_PORTS; port_no++) {
270 if (!dp->ports[port_no]) {
271 return u32_to_odp(port_no);
279 create_dp_netdev(const char *name, const struct dpif_class *class,
280 struct dp_netdev **dpp)
282 struct dp_netdev *dp;
286 dp = xzalloc(sizeof *dp);
288 dp->name = xstrdup(name);
290 dp->max_mtu = ETH_PAYLOAD_MAX;
291 for (i = 0; i < N_QUEUES; i++) {
292 dp->queues[i].head = dp->queues[i].tail = 0;
294 dp->queue_seq = seq_create();
295 classifier_init(&dp->cls);
296 hmap_init(&dp->flow_table);
297 list_init(&dp->port_list);
298 dp->port_seq = seq_create();
300 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
306 shash_add(&dp_netdevs, name, dp);
313 dpif_netdev_open(const struct dpif_class *class, const char *name,
314 bool create, struct dpif **dpifp)
316 struct dp_netdev *dp;
319 ovs_mutex_lock(&dp_netdev_mutex);
320 dp = shash_find_data(&dp_netdevs, name);
322 error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
324 error = (dp->class != class ? EINVAL
329 *dpifp = create_dpif_netdev(dp);
331 ovs_mutex_unlock(&dp_netdev_mutex);
337 dp_netdev_purge_queues(struct dp_netdev *dp)
341 for (i = 0; i < N_QUEUES; i++) {
342 struct dp_netdev_queue *q = &dp->queues[i];
344 while (q->tail != q->head) {
345 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
346 ofpbuf_uninit(&u->buf);
352 dp_netdev_free(struct dp_netdev *dp)
354 struct dp_netdev_port *port, *next;
356 dp_netdev_flow_flush(dp);
357 LIST_FOR_EACH_SAFE (port, next, node, &dp->port_list) {
358 do_del_port(dp, port->port_no);
360 dp_netdev_purge_queues(dp);
361 seq_destroy(dp->queue_seq);
362 classifier_destroy(&dp->cls);
363 hmap_destroy(&dp->flow_table);
364 seq_destroy(dp->port_seq);
370 dpif_netdev_close(struct dpif *dpif)
372 struct dp_netdev *dp = get_dp_netdev(dpif);
374 ovs_mutex_lock(&dp_netdev_mutex);
376 ovs_assert(dp->open_cnt > 0);
377 if (--dp->open_cnt == 0 && dp->destroyed) {
378 shash_find_and_delete(&dp_netdevs, dp->name);
383 ovs_mutex_unlock(&dp_netdev_mutex);
387 dpif_netdev_destroy(struct dpif *dpif)
389 struct dp_netdev *dp = get_dp_netdev(dpif);
391 ovs_mutex_lock(&dp_netdev_mutex);
392 dp->destroyed = true;
393 ovs_mutex_unlock(&dp_netdev_mutex);
399 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
401 struct dp_netdev *dp = get_dp_netdev(dpif);
403 ovs_mutex_lock(&dp_netdev_mutex);
404 stats->n_flows = hmap_count(&dp->flow_table);
405 stats->n_hit = dp->n_hit;
406 stats->n_missed = dp->n_missed;
407 stats->n_lost = dp->n_lost;
408 stats->n_masks = UINT64_MAX;
409 stats->n_mask_hit = UINT64_MAX;
410 ovs_mutex_unlock(&dp_netdev_mutex);
416 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
419 struct netdev_saved_flags *sf;
420 struct dp_netdev_port *port;
421 struct netdev *netdev;
422 struct netdev_rx *rx;
423 enum netdev_flags flags;
424 const char *open_type;
428 /* XXX reject devices already in some dp_netdev. */
430 /* Open and validate network device. */
431 open_type = dpif_netdev_port_open_type(dp->class, type);
432 error = netdev_open(devname, open_type, &netdev);
436 /* XXX reject non-Ethernet devices */
438 netdev_get_flags(netdev, &flags);
439 if (flags & NETDEV_LOOPBACK) {
440 VLOG_ERR("%s: cannot add a loopback device", devname);
441 netdev_close(netdev);
445 error = netdev_rx_open(netdev, &rx);
447 && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
448 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
449 devname, ovs_strerror(errno));
450 netdev_close(netdev);
454 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
457 netdev_close(netdev);
461 port = xmalloc(sizeof *port);
462 port->port_no = port_no;
463 port->netdev = netdev;
466 port->type = xstrdup(type);
468 error = netdev_get_mtu(netdev, &mtu);
469 if (!error && mtu > dp->max_mtu) {
473 list_push_back(&dp->port_list, &port->node);
474 dp->ports[odp_to_u32(port_no)] = port;
475 seq_change(dp->port_seq);
481 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
482 odp_port_t *port_nop)
484 struct dp_netdev *dp = get_dp_netdev(dpif);
485 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
486 const char *dpif_port;
490 ovs_mutex_lock(&dp_netdev_mutex);
491 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
492 if (*port_nop != ODPP_NONE) {
493 uint32_t port_idx = odp_to_u32(*port_nop);
494 if (port_idx >= MAX_PORTS) {
496 } else if (dp->ports[port_idx]) {
503 port_no = choose_port(dp, dpif_port);
504 error = port_no == ODPP_NONE ? EFBIG : 0;
508 error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
510 ovs_mutex_unlock(&dp_netdev_mutex);
516 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
518 struct dp_netdev *dp = get_dp_netdev(dpif);
521 ovs_mutex_lock(&dp_netdev_mutex);
522 error = port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
523 ovs_mutex_unlock(&dp_netdev_mutex);
529 is_valid_port_number(odp_port_t port_no)
531 return odp_to_u32(port_no) < MAX_PORTS;
535 get_port_by_number(struct dp_netdev *dp,
536 odp_port_t port_no, struct dp_netdev_port **portp)
538 if (!is_valid_port_number(port_no)) {
542 *portp = dp->ports[odp_to_u32(port_no)];
543 return *portp ? 0 : ENOENT;
548 get_port_by_name(struct dp_netdev *dp,
549 const char *devname, struct dp_netdev_port **portp)
551 struct dp_netdev_port *port;
553 LIST_FOR_EACH (port, node, &dp->port_list) {
554 if (!strcmp(netdev_get_name(port->netdev), devname)) {
563 do_del_port(struct dp_netdev *dp, odp_port_t port_no)
565 struct dp_netdev_port *port;
568 error = get_port_by_number(dp, port_no, &port);
573 list_remove(&port->node);
574 dp->ports[odp_to_u32(port_no)] = NULL;
575 seq_change(dp->port_seq);
577 netdev_close(port->netdev);
578 netdev_restore_flags(port->sf);
579 netdev_rx_close(port->rx);
587 answer_port_query(const struct dp_netdev_port *port,
588 struct dpif_port *dpif_port)
590 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
591 dpif_port->type = xstrdup(port->type);
592 dpif_port->port_no = port->port_no;
596 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
597 struct dpif_port *dpif_port)
599 struct dp_netdev *dp = get_dp_netdev(dpif);
600 struct dp_netdev_port *port;
603 ovs_mutex_lock(&dp_netdev_mutex);
604 error = get_port_by_number(dp, port_no, &port);
605 if (!error && dpif_port) {
606 answer_port_query(port, dpif_port);
608 ovs_mutex_unlock(&dp_netdev_mutex);
614 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
615 struct dpif_port *dpif_port)
617 struct dp_netdev *dp = get_dp_netdev(dpif);
618 struct dp_netdev_port *port;
621 ovs_mutex_lock(&dp_netdev_mutex);
622 error = get_port_by_name(dp, devname, &port);
623 if (!error && dpif_port) {
624 answer_port_query(port, dpif_port);
626 ovs_mutex_unlock(&dp_netdev_mutex);
632 dpif_netdev_get_max_ports(const struct dpif *dpif OVS_UNUSED)
638 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *netdev_flow)
640 ovs_rwlock_wrlock(&dp->cls.rwlock);
641 classifier_remove(&dp->cls, &netdev_flow->cr);
642 ovs_rwlock_unlock(&dp->cls.rwlock);
643 cls_rule_destroy(&netdev_flow->cr);
645 hmap_remove(&dp->flow_table, &netdev_flow->node);
646 free(netdev_flow->actions);
651 dp_netdev_flow_flush(struct dp_netdev *dp)
653 struct dp_netdev_flow *netdev_flow, *next;
655 HMAP_FOR_EACH_SAFE (netdev_flow, next, node, &dp->flow_table) {
656 dp_netdev_free_flow(dp, netdev_flow);
661 dpif_netdev_flow_flush(struct dpif *dpif)
663 struct dp_netdev *dp = get_dp_netdev(dpif);
665 ovs_mutex_lock(&dp_netdev_mutex);
666 dp_netdev_flow_flush(dp);
667 ovs_mutex_unlock(&dp_netdev_mutex);
672 struct dp_netdev_port_state {
678 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
680 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
685 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
686 struct dpif_port *dpif_port)
688 struct dp_netdev_port_state *state = state_;
689 struct dp_netdev *dp = get_dp_netdev(dpif);
692 ovs_mutex_lock(&dp_netdev_mutex);
693 for (port_idx = odp_to_u32(state->port_no);
694 port_idx < MAX_PORTS; port_idx++) {
695 struct dp_netdev_port *port = dp->ports[port_idx];
698 state->name = xstrdup(netdev_get_name(port->netdev));
699 dpif_port->name = state->name;
700 dpif_port->type = port->type;
701 dpif_port->port_no = port->port_no;
702 state->port_no = u32_to_odp(port_idx + 1);
703 ovs_mutex_unlock(&dp_netdev_mutex);
708 ovs_mutex_unlock(&dp_netdev_mutex);
714 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
716 struct dp_netdev_port_state *state = state_;
723 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
725 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
726 uint64_t new_port_seq;
729 ovs_mutex_lock(&dp_netdev_mutex);
730 new_port_seq = seq_read(dpif->dp->port_seq);
731 if (dpif->last_port_seq != new_port_seq) {
732 dpif->last_port_seq = new_port_seq;
737 ovs_mutex_unlock(&dp_netdev_mutex);
743 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
745 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
747 ovs_mutex_lock(&dp_netdev_mutex);
748 seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
749 ovs_mutex_unlock(&dp_netdev_mutex);
752 static struct dp_netdev_flow *
753 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *flow)
757 ovs_rwlock_wrlock(&dp->cls.rwlock);
758 cr = classifier_lookup(&dp->cls, flow, NULL);
759 ovs_rwlock_unlock(&dp->cls.rwlock);
762 ? CONTAINER_OF(cr, struct dp_netdev_flow, cr)
766 static struct dp_netdev_flow *
767 dp_netdev_find_flow(const struct dp_netdev *dp, const struct flow *flow)
769 struct dp_netdev_flow *netdev_flow;
771 HMAP_FOR_EACH_WITH_HASH (netdev_flow, node, flow_hash(flow, 0),
773 if (flow_equal(&netdev_flow->flow, flow)) {
781 get_dpif_flow_stats(struct dp_netdev_flow *netdev_flow,
782 struct dpif_flow_stats *stats)
784 stats->n_packets = netdev_flow->packet_count;
785 stats->n_bytes = netdev_flow->byte_count;
786 stats->used = netdev_flow->used;
787 stats->tcp_flags = netdev_flow->tcp_flags;
791 dpif_netdev_flow_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len,
792 const struct nlattr *mask_key,
793 uint32_t mask_key_len, struct flow *flow,
798 if (odp_flow_key_to_flow(key, key_len, flow)
800 && odp_flow_key_to_mask(mask_key, mask_key_len, mask, flow))) {
801 /* This should not happen: it indicates that odp_flow_key_from_flow()
802 * and odp_flow_key_to_flow() disagree on the acceptable form of a flow
803 * or odp_flow_key_from_mask() and odp_flow_key_to_mask() disagree on
804 * the acceptable form of a mask. Log the problem as an error, with
805 * enough details to enable debugging. */
806 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
808 if (!VLOG_DROP_ERR(&rl)) {
812 odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s,
814 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
822 /* Force unwildcard the in_port. */
823 mask->in_port.odp_port = u32_to_odp(UINT32_MAX);
826 in_port = flow->in_port.odp_port;
827 if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
835 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
838 return dpif_netdev_flow_mask_from_nlattrs(key, key_len, NULL, 0, flow,
843 dpif_netdev_flow_get(const struct dpif *dpif,
844 const struct nlattr *nl_key, size_t nl_key_len,
845 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
847 struct dp_netdev *dp = get_dp_netdev(dpif);
848 struct dp_netdev_flow *netdev_flow;
852 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
857 ovs_mutex_lock(&dp_netdev_mutex);
858 netdev_flow = dp_netdev_find_flow(dp, &key);
861 get_dpif_flow_stats(netdev_flow, stats);
864 *actionsp = ofpbuf_clone_data(netdev_flow->actions,
865 netdev_flow->actions_len);
870 ovs_mutex_unlock(&dp_netdev_mutex);
876 set_flow_actions(struct dp_netdev_flow *netdev_flow,
877 const struct nlattr *actions, size_t actions_len)
879 netdev_flow->actions = xrealloc(netdev_flow->actions, actions_len);
880 netdev_flow->actions_len = actions_len;
881 memcpy(netdev_flow->actions, actions, actions_len);
886 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *flow,
887 const struct flow_wildcards *wc,
888 const struct nlattr *actions,
891 struct dp_netdev_flow *netdev_flow;
895 netdev_flow = xzalloc(sizeof *netdev_flow);
896 netdev_flow->flow = *flow;
898 match_init(&match, flow, wc);
899 cls_rule_init(&netdev_flow->cr, &match, NETDEV_RULE_PRIORITY);
900 ovs_rwlock_wrlock(&dp->cls.rwlock);
901 classifier_insert(&dp->cls, &netdev_flow->cr);
902 ovs_rwlock_unlock(&dp->cls.rwlock);
904 error = set_flow_actions(netdev_flow, actions, actions_len);
906 ovs_rwlock_wrlock(&dp->cls.rwlock);
907 classifier_remove(&dp->cls, &netdev_flow->cr);
908 ovs_rwlock_unlock(&dp->cls.rwlock);
909 cls_rule_destroy(&netdev_flow->cr);
915 hmap_insert(&dp->flow_table, &netdev_flow->node, flow_hash(flow, 0));
920 clear_stats(struct dp_netdev_flow *netdev_flow)
922 netdev_flow->used = 0;
923 netdev_flow->packet_count = 0;
924 netdev_flow->byte_count = 0;
925 netdev_flow->tcp_flags = 0;
929 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
931 struct dp_netdev *dp = get_dp_netdev(dpif);
932 struct dp_netdev_flow *netdev_flow;
934 struct flow_wildcards wc;
937 error = dpif_netdev_flow_mask_from_nlattrs(put->key, put->key_len,
938 put->mask, put->mask_len, &flow, &wc.masks);
943 ovs_mutex_lock(&dp_netdev_mutex);
944 netdev_flow = dp_netdev_lookup_flow(dp, &flow);
946 if (put->flags & DPIF_FP_CREATE) {
947 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
949 memset(put->stats, 0, sizeof *put->stats);
951 error = dp_netdev_flow_add(dp, &flow, &wc, put->actions,
960 if (put->flags & DPIF_FP_MODIFY
961 && flow_equal(&flow, &netdev_flow->flow)) {
962 error = set_flow_actions(netdev_flow, put->actions,
966 get_dpif_flow_stats(netdev_flow, put->stats);
968 if (put->flags & DPIF_FP_ZERO_STATS) {
969 clear_stats(netdev_flow);
972 } else if (put->flags & DPIF_FP_CREATE) {
975 /* Overlapping flow. */
979 ovs_mutex_unlock(&dp_netdev_mutex);
985 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
987 struct dp_netdev *dp = get_dp_netdev(dpif);
988 struct dp_netdev_flow *netdev_flow;
992 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
997 ovs_mutex_lock(&dp_netdev_mutex);
998 netdev_flow = dp_netdev_find_flow(dp, &key);
1001 get_dpif_flow_stats(netdev_flow, del->stats);
1003 dp_netdev_free_flow(dp, netdev_flow);
1007 ovs_mutex_unlock(&dp_netdev_mutex);
1012 struct dp_netdev_flow_state {
1015 struct nlattr *actions;
1016 struct odputil_keybuf keybuf;
1017 struct odputil_keybuf maskbuf;
1018 struct dpif_flow_stats stats;
1022 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
1024 struct dp_netdev_flow_state *state;
1026 *statep = state = xmalloc(sizeof *state);
1029 state->actions = NULL;
1034 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
1035 const struct nlattr **key, size_t *key_len,
1036 const struct nlattr **mask, size_t *mask_len,
1037 const struct nlattr **actions, size_t *actions_len,
1038 const struct dpif_flow_stats **stats)
1040 struct dp_netdev_flow_state *state = state_;
1041 struct dp_netdev *dp = get_dp_netdev(dpif);
1042 struct dp_netdev_flow *netdev_flow;
1043 struct hmap_node *node;
1045 ovs_mutex_lock(&dp_netdev_mutex);
1046 node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
1048 ovs_mutex_unlock(&dp_netdev_mutex);
1052 netdev_flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
1057 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
1058 odp_flow_key_from_flow(&buf, &netdev_flow->flow,
1059 netdev_flow->flow.in_port.odp_port);
1062 *key_len = buf.size;
1067 struct flow_wildcards wc;
1069 ofpbuf_use_stack(&buf, &state->maskbuf, sizeof state->maskbuf);
1070 minimask_expand(&netdev_flow->cr.match.mask, &wc);
1071 odp_flow_key_from_mask(&buf, &wc.masks, &netdev_flow->flow,
1072 odp_to_u32(wc.masks.in_port.odp_port));
1075 *mask_len = buf.size;
1079 free(state->actions);
1080 state->actions = xmemdup(netdev_flow->actions,
1081 netdev_flow->actions_len);
1083 *actions = state->actions;
1084 *actions_len = netdev_flow->actions_len;
1088 get_dpif_flow_stats(netdev_flow, &state->stats);
1089 *stats = &state->stats;
1092 ovs_mutex_unlock(&dp_netdev_mutex);
1097 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1099 struct dp_netdev_flow_state *state = state_;
1101 free(state->actions);
1107 dpif_netdev_execute(struct dpif *dpif, const struct dpif_execute *execute)
1109 struct dp_netdev *dp = get_dp_netdev(dpif);
1113 if (execute->packet->size < ETH_HEADER_LEN ||
1114 execute->packet->size > UINT16_MAX) {
1118 /* Get packet metadata. */
1119 error = dpif_netdev_flow_from_nlattrs(execute->key, execute->key_len, &md);
1121 struct ofpbuf *copy;
1124 /* Make a deep copy of 'packet', because we might modify its data. */
1125 copy = ofpbuf_clone_with_headroom(execute->packet, DP_NETDEV_HEADROOM);
1127 /* Extract flow key. */
1128 flow_extract(copy, md.skb_priority, md.pkt_mark, &md.tunnel,
1130 ovs_mutex_lock(&dp_netdev_mutex);
1131 dp_netdev_execute_actions(dp, &key, copy,
1132 execute->actions, execute->actions_len);
1133 ovs_mutex_unlock(&dp_netdev_mutex);
1134 ofpbuf_delete(copy);
1140 dpif_netdev_recv_set(struct dpif *dpif OVS_UNUSED, bool enable OVS_UNUSED)
1146 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1147 uint32_t queue_id, uint32_t *priority)
1149 *priority = queue_id;
1153 static struct dp_netdev_queue *
1154 find_nonempty_queue(struct dpif *dpif)
1156 struct dp_netdev *dp = get_dp_netdev(dpif);
1159 for (i = 0; i < N_QUEUES; i++) {
1160 struct dp_netdev_queue *q = &dp->queues[i];
1161 if (q->head != q->tail) {
1169 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
1172 struct dp_netdev_queue *q;
1175 ovs_mutex_lock(&dp_netdev_mutex);
1176 q = find_nonempty_queue(dpif);
1178 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1180 *upcall = u->upcall;
1181 upcall->packet = buf;
1190 ovs_mutex_unlock(&dp_netdev_mutex);
1196 dpif_netdev_recv_wait(struct dpif *dpif)
1198 struct dp_netdev *dp = get_dp_netdev(dpif);
1201 ovs_mutex_lock(&dp_netdev_mutex);
1202 seq = seq_read(dp->queue_seq);
1203 if (find_nonempty_queue(dpif)) {
1204 poll_immediate_wake();
1206 seq_wait(dp->queue_seq, seq);
1208 ovs_mutex_unlock(&dp_netdev_mutex);
1212 dpif_netdev_recv_purge(struct dpif *dpif)
1214 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1215 ovs_mutex_lock(&dp_netdev_mutex);
1216 dp_netdev_purge_queues(dpif_netdev->dp);
1217 ovs_mutex_unlock(&dp_netdev_mutex);
1221 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow,
1222 const struct ofpbuf *packet)
1224 netdev_flow->used = time_msec();
1225 netdev_flow->packet_count++;
1226 netdev_flow->byte_count += packet->size;
1227 netdev_flow->tcp_flags |= packet_get_tcp_flags(packet, &netdev_flow->flow);
1231 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1232 struct ofpbuf *packet, uint32_t skb_priority,
1233 uint32_t pkt_mark, const struct flow_tnl *tnl)
1235 struct dp_netdev_flow *netdev_flow;
1237 union flow_in_port in_port_;
1239 if (packet->size < ETH_HEADER_LEN) {
1242 in_port_.odp_port = port->port_no;
1243 flow_extract(packet, skb_priority, pkt_mark, tnl, &in_port_, &key);
1244 netdev_flow = dp_netdev_lookup_flow(dp, &key);
1246 dp_netdev_flow_used(netdev_flow, packet);
1247 dp_netdev_execute_actions(dp, &key, packet,
1248 netdev_flow->actions,
1249 netdev_flow->actions_len);
1253 dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, NULL);
1258 dpif_netdev_run(struct dpif *dpif)
1260 struct dp_netdev_port *port;
1261 struct dp_netdev *dp;
1262 struct ofpbuf packet;
1264 ovs_mutex_lock(&dp_netdev_mutex);
1265 dp = get_dp_netdev(dpif);
1266 ofpbuf_init(&packet,
1267 DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + dp->max_mtu);
1269 LIST_FOR_EACH (port, node, &dp->port_list) {
1272 /* Reset packet contents. */
1273 ofpbuf_clear(&packet);
1274 ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1276 error = port->rx ? netdev_rx_recv(port->rx, &packet) : EOPNOTSUPP;
1278 dp_netdev_port_input(dp, port, &packet, 0, 0, NULL);
1279 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1280 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1282 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1283 netdev_get_name(port->netdev), ovs_strerror(error));
1286 ofpbuf_uninit(&packet);
1287 ovs_mutex_unlock(&dp_netdev_mutex);
1291 dpif_netdev_wait(struct dpif *dpif)
1293 struct dp_netdev_port *port;
1295 /* There is a race here, if thread A calls dpif_netdev_wait(dpif) and
1296 * thread B calls dpif_port_add(dpif) or dpif_port_remove(dpif) before
1297 * A makes it to poll_block().
1299 * But I think it doesn't matter:
1301 * - In the dpif_port_add() case, A will not wake up when a packet
1302 * arrives on the new port, but this would also happen if the
1303 * ordering were reversed.
1305 * - In the dpif_port_remove() case, A might wake up spuriously, but
1306 * that is harmless. */
1308 ovs_mutex_lock(&dp_netdev_mutex);
1309 LIST_FOR_EACH (port, node, &get_dp_netdev(dpif)->port_list) {
1311 netdev_rx_wait(port->rx);
1314 ovs_mutex_unlock(&dp_netdev_mutex);
1318 dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
1319 int queue_no, const struct flow *flow,
1320 const struct nlattr *userdata)
1322 struct dp_netdev_queue *q = &dp->queues[queue_no];
1323 if (q->head - q->tail < MAX_QUEUE_LEN) {
1324 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
1325 struct dpif_upcall *upcall = &u->upcall;
1326 struct ofpbuf *buf = &u->buf;
1329 upcall->type = queue_no;
1331 /* Allocate buffer big enough for everything. */
1332 buf_size = ODPUTIL_FLOW_KEY_BYTES + 2 + packet->size;
1334 buf_size += NLA_ALIGN(userdata->nla_len);
1336 ofpbuf_init(buf, buf_size);
1339 odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
1340 upcall->key = buf->data;
1341 upcall->key_len = buf->size;
1345 upcall->userdata = ofpbuf_put(buf, userdata,
1346 NLA_ALIGN(userdata->nla_len));
1351 * We adjust 'data' and 'size' in 'buf' so that only the packet itself
1352 * is visible in 'upcall->packet'. The ODP flow and (if present)
1353 * userdata become part of the headroom. */
1354 ofpbuf_put_zeros(buf, 2);
1355 buf->data = ofpbuf_put(buf, packet->data, packet->size);
1356 buf->size = packet->size;
1357 upcall->packet = buf;
1359 seq_change(dp->queue_seq);
1368 struct dp_netdev_execute_aux {
1369 struct dp_netdev *dp;
1370 const struct flow *key;
1374 dp_netdev_action_output(void *aux_, struct ofpbuf *packet,
1375 const struct flow *flow OVS_UNUSED,
1376 odp_port_t out_port)
1378 struct dp_netdev_execute_aux *aux = aux_;
1379 struct dp_netdev_port *p = aux->dp->ports[odp_to_u32(out_port)];
1381 netdev_send(p->netdev, packet);
1386 dp_netdev_action_userspace(void *aux_, struct ofpbuf *packet,
1387 const struct flow *flow OVS_UNUSED,
1388 const struct nlattr *a)
1390 struct dp_netdev_execute_aux *aux = aux_;
1391 const struct nlattr *userdata;
1393 userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
1394 dp_netdev_output_userspace(aux->dp, packet, DPIF_UC_ACTION, aux->key,
1399 dp_netdev_execute_actions(struct dp_netdev *dp, const struct flow *key,
1400 struct ofpbuf *packet,
1401 const struct nlattr *actions, size_t actions_len)
1403 struct dp_netdev_execute_aux aux = {dp, key};
1404 struct flow md = *key; /* Packet metadata, may be modified by actions. */
1406 odp_execute_actions(&aux, packet, &md, actions, actions_len,
1407 dp_netdev_action_output, dp_netdev_action_userspace);
1410 const struct dpif_class dpif_netdev_class = {
1412 dpif_netdev_enumerate,
1413 dpif_netdev_port_open_type,
1416 dpif_netdev_destroy,
1419 dpif_netdev_get_stats,
1420 dpif_netdev_port_add,
1421 dpif_netdev_port_del,
1422 dpif_netdev_port_query_by_number,
1423 dpif_netdev_port_query_by_name,
1424 dpif_netdev_get_max_ports,
1425 NULL, /* port_get_pid */
1426 dpif_netdev_port_dump_start,
1427 dpif_netdev_port_dump_next,
1428 dpif_netdev_port_dump_done,
1429 dpif_netdev_port_poll,
1430 dpif_netdev_port_poll_wait,
1431 dpif_netdev_flow_get,
1432 dpif_netdev_flow_put,
1433 dpif_netdev_flow_del,
1434 dpif_netdev_flow_flush,
1435 dpif_netdev_flow_dump_start,
1436 dpif_netdev_flow_dump_next,
1437 dpif_netdev_flow_dump_done,
1438 dpif_netdev_execute,
1440 dpif_netdev_recv_set,
1441 dpif_netdev_queue_to_priority,
1443 dpif_netdev_recv_wait,
1444 dpif_netdev_recv_purge,
1448 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
1449 const char *argv[], void *aux OVS_UNUSED)
1451 struct dp_netdev_port *port;
1452 struct dp_netdev *dp;
1455 dp = shash_find_data(&dp_netdevs, argv[1]);
1456 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
1457 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
1461 if (get_port_by_name(dp, argv[2], &port)) {
1462 unixctl_command_reply_error(conn, "unknown port");
1466 port_no = atoi(argv[3]);
1467 if (port_no <= 0 || port_no >= MAX_PORTS) {
1468 unixctl_command_reply_error(conn, "bad port number");
1471 if (dp->ports[port_no]) {
1472 unixctl_command_reply_error(conn, "port number already in use");
1475 dp->ports[odp_to_u32(port->port_no)] = NULL;
1476 dp->ports[port_no] = port;
1477 port->port_no = u32_to_odp(port_no);
1478 seq_change(dp->port_seq);
1479 unixctl_command_reply(conn, NULL);
1483 dpif_dummy_register__(const char *type)
1485 struct dpif_class *class;
1487 class = xmalloc(sizeof *class);
1488 *class = dpif_netdev_class;
1489 class->type = xstrdup(type);
1490 dp_register_provider(class);
1494 dpif_dummy_register(bool override)
1501 dp_enumerate_types(&types);
1502 SSET_FOR_EACH (type, &types) {
1503 if (!dp_unregister_provider(type)) {
1504 dpif_dummy_register__(type);
1507 sset_destroy(&types);
1510 dpif_dummy_register__("dummy");
1512 unixctl_command_register("dpif-dummy/change-port-number",
1513 "DP PORT NEW-NUMBER",
1514 3, 3, dpif_dummy_change_port_number, NULL);