2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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 "meta-flow.h"
46 #include "netdev-vport.h"
48 #include "odp-execute.h"
50 #include "ofp-print.h"
54 #include "poll-loop.h"
64 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
66 /* By default, choose a priority in the middle. */
67 #define NETDEV_RULE_PRIORITY 0x8000
69 /* Configuration parameters. */
70 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
73 enum { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
74 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
75 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
77 /* Protects against changes to 'dp_netdevs'. */
78 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
80 /* Contains all 'struct dp_netdev's. */
81 static struct shash dp_netdevs OVS_GUARDED_BY(dp_netdev_mutex)
82 = SHASH_INITIALIZER(&dp_netdevs);
84 struct dp_netdev_upcall {
85 struct dpif_upcall upcall; /* Queued upcall information. */
86 struct ofpbuf buf; /* ofpbuf instance for upcall.packet. */
89 /* A queue passing packets from a struct dp_netdev to its clients (handlers).
95 * Any access at all requires the owning 'dp_netdev''s queue_rwlock and
97 struct dp_netdev_queue {
98 struct ovs_mutex mutex;
99 struct seq *seq; /* Incremented whenever a packet is queued. */
100 struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN] OVS_GUARDED;
101 unsigned int head OVS_GUARDED;
102 unsigned int tail OVS_GUARDED;
105 /* Datapath based on the network device interface from netdev.h.
111 * Some members, marked 'const', are immutable. Accessing other members
112 * requires synchronization, as noted in more detail below.
114 * Acquisition order is, from outermost to innermost:
116 * dp_netdev_mutex (global)
123 const struct dpif_class *const class;
124 const char *const name;
125 struct ovs_refcount ref_cnt;
126 atomic_flag destroyed;
130 * Readers of 'cls' and 'flow_table' must take a 'cls->rwlock' read lock.
132 * Writers of 'cls' and 'flow_table' must take the 'flow_mutex' and then
133 * the 'cls->rwlock' write lock. (The outer 'flow_mutex' allows writers to
134 * atomically perform multiple operations on 'cls' and 'flow_table'.)
136 struct ovs_mutex flow_mutex;
137 struct classifier cls; /* Classifier. Protected by cls.rwlock. */
138 struct hmap flow_table OVS_GUARDED; /* Flow table. */
142 * 'queue_rwlock' protects the modification of 'handler_queues' and
143 * 'n_handlers'. The queue elements are protected by its
144 * 'handler_queues''s mutex. */
145 struct fat_rwlock queue_rwlock;
146 struct dp_netdev_queue *handler_queues;
151 * ovsthread_stats is internally synchronized. */
152 struct ovsthread_stats stats; /* Contains 'struct dp_netdev_stats *'. */
156 * Any lookup into 'ports' or any access to the dp_netdev_ports found
157 * through 'ports' requires taking 'port_rwlock'. */
158 struct ovs_rwlock port_rwlock;
159 struct hmap ports OVS_GUARDED;
160 struct seq *port_seq; /* Incremented whenever a port changes. */
162 /* Forwarding threads. */
163 struct latch exit_latch;
164 struct dp_forwarder *forwarders;
168 static struct dp_netdev_port *dp_netdev_lookup_port(const struct dp_netdev *dp,
170 OVS_REQ_RDLOCK(dp->port_rwlock);
173 DP_STAT_HIT, /* Packets that matched in the flow table. */
174 DP_STAT_MISS, /* Packets that did not match. */
175 DP_STAT_LOST, /* Packets not passed up to the client. */
179 /* Contained by struct dp_netdev's 'stats' member. */
180 struct dp_netdev_stats {
181 struct ovs_mutex mutex; /* Protects 'n'. */
183 /* Indexed by DP_STAT_*, protected by 'mutex'. */
184 unsigned long long int n[DP_N_STATS] OVS_GUARDED;
188 /* A port in a netdev-based datapath. */
189 struct dp_netdev_port {
190 struct hmap_node node; /* Node in dp_netdev's 'ports'. */
192 struct netdev *netdev;
193 struct netdev_saved_flags *sf;
194 struct netdev_rx *rx;
195 struct ovs_refcount ref_cnt;
196 char *type; /* Port type as requested by user. */
199 /* A flow in dp_netdev's 'flow_table'.
205 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
206 * its dp_netdev's classifier. The text below calls this classifier 'cls'.
211 * The thread safety rules described here for "struct dp_netdev_flow" are
212 * motivated by two goals:
214 * - Prevent threads that read members of "struct dp_netdev_flow" from
215 * reading bad data due to changes by some thread concurrently modifying
218 * - Prevent two threads making changes to members of a given "struct
219 * dp_netdev_flow" from interfering with each other.
225 * A flow 'flow' may be accessed without a risk of being freed by code that
226 * holds a read-lock or write-lock on 'cls->rwlock' or that owns a reference to
227 * 'flow->ref_cnt' (or both). Code that needs to hold onto a flow for a while
228 * should take 'cls->rwlock', find the flow it needs, increment 'flow->ref_cnt'
229 * with dpif_netdev_flow_ref(), and drop 'cls->rwlock'.
231 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
232 * flow from being deleted from 'cls' (that's 'cls->rwlock') and it doesn't
233 * protect members of 'flow' from modification (that's 'flow->mutex').
235 * 'flow->mutex' protects the members of 'flow' from modification. It doesn't
236 * protect the flow from being deleted from 'cls' (that's 'cls->rwlock') and it
237 * doesn't prevent the flow from being freed (that's 'flow->ref_cnt').
239 * Some members, marked 'const', are immutable. Accessing other members
240 * requires synchronization, as noted in more detail below.
242 struct dp_netdev_flow {
243 /* Packet classification. */
244 const struct cls_rule cr; /* In owning dp_netdev's 'cls'. */
246 /* Hash table index by unmasked flow. */
247 const struct hmap_node node; /* In owning dp_netdev's 'flow_table'. */
248 const struct flow flow; /* The flow that created this entry. */
250 /* Protects members marked OVS_GUARDED.
252 * Acquire after datapath's flow_mutex. */
253 struct ovs_mutex mutex OVS_ACQ_AFTER(dp_netdev_mutex);
257 * Reading or writing these members requires 'mutex'. */
258 struct ovsthread_stats stats; /* Contains "struct dp_netdev_flow_stats". */
262 * Reading 'actions' requires 'mutex'.
263 * Writing 'actions' requires 'mutex' and (to allow for transactions) the
264 * datapath's flow_mutex. */
265 OVSRCU_TYPE(struct dp_netdev_actions *) actions;
268 static void dp_netdev_flow_free(struct dp_netdev_flow *);
270 /* Contained by struct dp_netdev_flow's 'stats' member. */
271 struct dp_netdev_flow_stats {
272 struct ovs_mutex mutex; /* Guards all the other members. */
274 long long int used OVS_GUARDED; /* Last used time, in monotonic msecs. */
275 long long int packet_count OVS_GUARDED; /* Number of packets matched. */
276 long long int byte_count OVS_GUARDED; /* Number of bytes matched. */
277 uint16_t tcp_flags OVS_GUARDED; /* Bitwise-OR of seen tcp_flags values. */
280 /* A set of datapath actions within a "struct dp_netdev_flow".
286 * A struct dp_netdev_actions 'actions' may be accessed without a risk of being
287 * freed by code that holds a read-lock or write-lock on 'flow->mutex' (where
288 * 'flow' is the dp_netdev_flow for which 'flow->actions == actions') or that
289 * owns a reference to 'actions->ref_cnt' (or both). */
290 struct dp_netdev_actions {
291 /* These members are immutable: they do not change during the struct's
293 struct nlattr *actions; /* Sequence of OVS_ACTION_ATTR_* attributes. */
294 unsigned int size; /* Size of 'actions', in bytes. */
297 struct dp_netdev_actions *dp_netdev_actions_create(const struct nlattr *,
299 struct dp_netdev_actions *dp_netdev_flow_get_actions(
300 const struct dp_netdev_flow *);
301 static void dp_netdev_actions_free(struct dp_netdev_actions *);
303 /* A thread that receives packets from some ports, looks them up in the flow
304 * table, and executes the actions it finds. */
305 struct dp_forwarder {
306 struct dp_netdev *dp;
309 uint32_t min_hash, max_hash;
312 /* Interface to netdev-based datapath. */
315 struct dp_netdev *dp;
316 uint64_t last_port_seq;
319 static int get_port_by_number(struct dp_netdev *dp, odp_port_t port_no,
320 struct dp_netdev_port **portp)
321 OVS_REQ_RDLOCK(dp->port_rwlock);
322 static int get_port_by_name(struct dp_netdev *dp, const char *devname,
323 struct dp_netdev_port **portp)
324 OVS_REQ_RDLOCK(dp->port_rwlock);
325 static void dp_netdev_free(struct dp_netdev *)
326 OVS_REQUIRES(dp_netdev_mutex);
327 static void dp_netdev_flow_flush(struct dp_netdev *);
328 static int do_add_port(struct dp_netdev *dp, const char *devname,
329 const char *type, odp_port_t port_no)
330 OVS_REQ_WRLOCK(dp->port_rwlock);
331 static int do_del_port(struct dp_netdev *dp, odp_port_t port_no)
332 OVS_REQ_WRLOCK(dp->port_rwlock);
333 static void dp_netdev_destroy_all_queues(struct dp_netdev *dp)
334 OVS_REQ_WRLOCK(dp->queue_rwlock);
335 static int dpif_netdev_open(const struct dpif_class *, const char *name,
336 bool create, struct dpif **);
337 static int dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *,
338 int queue_no, int type,
340 const struct nlattr *userdata)
341 OVS_EXCLUDED(dp->queue_rwlock);
342 static void dp_netdev_execute_actions(struct dp_netdev *dp,
343 const struct flow *, struct ofpbuf *, bool may_steal,
344 struct pkt_metadata *,
345 const struct nlattr *actions,
347 OVS_REQ_RDLOCK(dp->port_rwlock);
348 static void dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
349 struct pkt_metadata *)
350 OVS_REQ_RDLOCK(dp->port_rwlock);
351 static void dp_netdev_set_threads(struct dp_netdev *, int n);
353 static struct dpif_netdev *
354 dpif_netdev_cast(const struct dpif *dpif)
356 ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
357 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
360 static struct dp_netdev *
361 get_dp_netdev(const struct dpif *dpif)
363 return dpif_netdev_cast(dpif)->dp;
367 dpif_netdev_enumerate(struct sset *all_dps)
369 struct shash_node *node;
371 ovs_mutex_lock(&dp_netdev_mutex);
372 SHASH_FOR_EACH(node, &dp_netdevs) {
373 sset_add(all_dps, node->name);
375 ovs_mutex_unlock(&dp_netdev_mutex);
381 dpif_netdev_class_is_dummy(const struct dpif_class *class)
383 return class != &dpif_netdev_class;
387 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
389 return strcmp(type, "internal") ? type
390 : dpif_netdev_class_is_dummy(class) ? "dummy"
395 create_dpif_netdev(struct dp_netdev *dp)
397 uint16_t netflow_id = hash_string(dp->name, 0);
398 struct dpif_netdev *dpif;
400 ovs_refcount_ref(&dp->ref_cnt);
402 dpif = xmalloc(sizeof *dpif);
403 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
405 dpif->last_port_seq = seq_read(dp->port_seq);
410 /* Choose an unused, non-zero port number and return it on success.
411 * Return ODPP_NONE on failure. */
413 choose_port(struct dp_netdev *dp, const char *name)
414 OVS_REQ_RDLOCK(dp->port_rwlock)
418 if (dp->class != &dpif_netdev_class) {
422 /* If the port name begins with "br", start the number search at
423 * 100 to make writing tests easier. */
424 if (!strncmp(name, "br", 2)) {
428 /* If the port name contains a number, try to assign that port number.
429 * This can make writing unit tests easier because port numbers are
431 for (p = name; *p != '\0'; p++) {
432 if (isdigit((unsigned char) *p)) {
433 port_no = start_no + strtol(p, NULL, 10);
434 if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE)
435 && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
436 return u32_to_odp(port_no);
443 for (port_no = 1; port_no <= UINT16_MAX; port_no++) {
444 if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
445 return u32_to_odp(port_no);
453 create_dp_netdev(const char *name, const struct dpif_class *class,
454 struct dp_netdev **dpp)
455 OVS_REQUIRES(dp_netdev_mutex)
457 struct dp_netdev *dp;
460 dp = xzalloc(sizeof *dp);
461 shash_add(&dp_netdevs, name, dp);
463 *CONST_CAST(const struct dpif_class **, &dp->class) = class;
464 *CONST_CAST(const char **, &dp->name) = xstrdup(name);
465 ovs_refcount_init(&dp->ref_cnt);
466 atomic_flag_clear(&dp->destroyed);
468 ovs_mutex_init(&dp->flow_mutex);
469 classifier_init(&dp->cls, NULL);
470 hmap_init(&dp->flow_table);
472 fat_rwlock_init(&dp->queue_rwlock);
474 ovsthread_stats_init(&dp->stats);
476 ovs_rwlock_init(&dp->port_rwlock);
477 hmap_init(&dp->ports);
478 dp->port_seq = seq_create();
479 latch_init(&dp->exit_latch);
481 ovs_rwlock_wrlock(&dp->port_rwlock);
482 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
483 ovs_rwlock_unlock(&dp->port_rwlock);
488 dp_netdev_set_threads(dp, 2);
495 dpif_netdev_open(const struct dpif_class *class, const char *name,
496 bool create, struct dpif **dpifp)
498 struct dp_netdev *dp;
501 ovs_mutex_lock(&dp_netdev_mutex);
502 dp = shash_find_data(&dp_netdevs, name);
504 error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
506 error = (dp->class != class ? EINVAL
511 *dpifp = create_dpif_netdev(dp);
513 ovs_mutex_unlock(&dp_netdev_mutex);
519 dp_netdev_purge_queues(struct dp_netdev *dp)
520 OVS_REQ_WRLOCK(dp->queue_rwlock)
524 for (i = 0; i < dp->n_handlers; i++) {
525 struct dp_netdev_queue *q = &dp->handler_queues[i];
527 ovs_mutex_lock(&q->mutex);
528 while (q->tail != q->head) {
529 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
530 ofpbuf_uninit(&u->upcall.packet);
531 ofpbuf_uninit(&u->buf);
533 ovs_mutex_unlock(&q->mutex);
537 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
538 * through the 'dp_netdevs' shash while freeing 'dp'. */
540 dp_netdev_free(struct dp_netdev *dp)
541 OVS_REQUIRES(dp_netdev_mutex)
543 struct dp_netdev_port *port, *next;
544 struct dp_netdev_stats *bucket;
547 shash_find_and_delete(&dp_netdevs, dp->name);
549 dp_netdev_set_threads(dp, 0);
550 free(dp->forwarders);
552 dp_netdev_flow_flush(dp);
553 ovs_rwlock_wrlock(&dp->port_rwlock);
554 HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) {
555 do_del_port(dp, port->port_no);
557 ovs_rwlock_unlock(&dp->port_rwlock);
559 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
560 ovs_mutex_destroy(&bucket->mutex);
561 free_cacheline(bucket);
563 ovsthread_stats_destroy(&dp->stats);
565 fat_rwlock_wrlock(&dp->queue_rwlock);
566 dp_netdev_destroy_all_queues(dp);
567 fat_rwlock_unlock(&dp->queue_rwlock);
569 fat_rwlock_destroy(&dp->queue_rwlock);
571 classifier_destroy(&dp->cls);
572 hmap_destroy(&dp->flow_table);
573 ovs_mutex_destroy(&dp->flow_mutex);
574 seq_destroy(dp->port_seq);
575 hmap_destroy(&dp->ports);
576 latch_destroy(&dp->exit_latch);
577 free(CONST_CAST(char *, dp->name));
582 dp_netdev_unref(struct dp_netdev *dp)
585 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
586 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
587 ovs_mutex_lock(&dp_netdev_mutex);
588 if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
591 ovs_mutex_unlock(&dp_netdev_mutex);
596 dpif_netdev_close(struct dpif *dpif)
598 struct dp_netdev *dp = get_dp_netdev(dpif);
605 dpif_netdev_destroy(struct dpif *dpif)
607 struct dp_netdev *dp = get_dp_netdev(dpif);
609 if (!atomic_flag_test_and_set(&dp->destroyed)) {
610 if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
611 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
620 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
622 struct dp_netdev *dp = get_dp_netdev(dpif);
623 struct dp_netdev_stats *bucket;
626 fat_rwlock_rdlock(&dp->cls.rwlock);
627 stats->n_flows = hmap_count(&dp->flow_table);
628 fat_rwlock_unlock(&dp->cls.rwlock);
630 stats->n_hit = stats->n_missed = stats->n_lost = 0;
631 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
632 ovs_mutex_lock(&bucket->mutex);
633 stats->n_hit += bucket->n[DP_STAT_HIT];
634 stats->n_missed += bucket->n[DP_STAT_MISS];
635 stats->n_lost += bucket->n[DP_STAT_LOST];
636 ovs_mutex_unlock(&bucket->mutex);
638 stats->n_masks = UINT32_MAX;
639 stats->n_mask_hit = UINT64_MAX;
645 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
647 OVS_REQ_WRLOCK(dp->port_rwlock)
649 struct netdev_saved_flags *sf;
650 struct dp_netdev_port *port;
651 struct netdev *netdev;
652 struct netdev_rx *rx;
653 enum netdev_flags flags;
654 const char *open_type;
657 /* XXX reject devices already in some dp_netdev. */
659 /* Open and validate network device. */
660 open_type = dpif_netdev_port_open_type(dp->class, type);
661 error = netdev_open(devname, open_type, &netdev);
665 /* XXX reject non-Ethernet devices */
667 netdev_get_flags(netdev, &flags);
668 if (flags & NETDEV_LOOPBACK) {
669 VLOG_ERR("%s: cannot add a loopback device", devname);
670 netdev_close(netdev);
674 error = netdev_rx_open(netdev, &rx);
676 && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
677 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
678 devname, ovs_strerror(errno));
679 netdev_close(netdev);
683 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
686 netdev_close(netdev);
690 port = xmalloc(sizeof *port);
691 port->port_no = port_no;
692 port->netdev = netdev;
695 port->type = xstrdup(type);
697 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
698 seq_change(dp->port_seq);
699 ovs_refcount_init(&port->ref_cnt);
705 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
706 odp_port_t *port_nop)
708 struct dp_netdev *dp = get_dp_netdev(dpif);
709 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
710 const char *dpif_port;
714 ovs_rwlock_wrlock(&dp->port_rwlock);
715 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
716 if (*port_nop != ODPP_NONE) {
718 error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0;
720 port_no = choose_port(dp, dpif_port);
721 error = port_no == ODPP_NONE ? EFBIG : 0;
725 error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
727 ovs_rwlock_unlock(&dp->port_rwlock);
733 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
735 struct dp_netdev *dp = get_dp_netdev(dpif);
738 ovs_rwlock_wrlock(&dp->port_rwlock);
739 error = port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
740 ovs_rwlock_unlock(&dp->port_rwlock);
746 is_valid_port_number(odp_port_t port_no)
748 return port_no != ODPP_NONE;
751 static struct dp_netdev_port *
752 dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no)
753 OVS_REQ_RDLOCK(dp->port_rwlock)
755 struct dp_netdev_port *port;
757 HMAP_FOR_EACH_IN_BUCKET (port, node, hash_int(odp_to_u32(port_no), 0),
759 if (port->port_no == port_no) {
767 get_port_by_number(struct dp_netdev *dp,
768 odp_port_t port_no, struct dp_netdev_port **portp)
769 OVS_REQ_RDLOCK(dp->port_rwlock)
771 if (!is_valid_port_number(port_no)) {
775 *portp = dp_netdev_lookup_port(dp, port_no);
776 return *portp ? 0 : ENOENT;
781 port_ref(struct dp_netdev_port *port)
784 ovs_refcount_ref(&port->ref_cnt);
789 port_unref(struct dp_netdev_port *port)
791 if (port && ovs_refcount_unref(&port->ref_cnt) == 1) {
792 netdev_close(port->netdev);
793 netdev_restore_flags(port->sf);
794 netdev_rx_close(port->rx);
801 get_port_by_name(struct dp_netdev *dp,
802 const char *devname, struct dp_netdev_port **portp)
803 OVS_REQ_RDLOCK(dp->port_rwlock)
805 struct dp_netdev_port *port;
807 HMAP_FOR_EACH (port, node, &dp->ports) {
808 if (!strcmp(netdev_get_name(port->netdev), devname)) {
817 do_del_port(struct dp_netdev *dp, odp_port_t port_no)
818 OVS_REQ_WRLOCK(dp->port_rwlock)
820 struct dp_netdev_port *port;
823 error = get_port_by_number(dp, port_no, &port);
828 hmap_remove(&dp->ports, &port->node);
829 seq_change(dp->port_seq);
836 answer_port_query(const struct dp_netdev_port *port,
837 struct dpif_port *dpif_port)
839 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
840 dpif_port->type = xstrdup(port->type);
841 dpif_port->port_no = port->port_no;
845 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
846 struct dpif_port *dpif_port)
848 struct dp_netdev *dp = get_dp_netdev(dpif);
849 struct dp_netdev_port *port;
852 ovs_rwlock_rdlock(&dp->port_rwlock);
853 error = get_port_by_number(dp, port_no, &port);
854 if (!error && dpif_port) {
855 answer_port_query(port, dpif_port);
857 ovs_rwlock_unlock(&dp->port_rwlock);
863 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
864 struct dpif_port *dpif_port)
866 struct dp_netdev *dp = get_dp_netdev(dpif);
867 struct dp_netdev_port *port;
870 ovs_rwlock_rdlock(&dp->port_rwlock);
871 error = get_port_by_name(dp, devname, &port);
872 if (!error && dpif_port) {
873 answer_port_query(port, dpif_port);
875 ovs_rwlock_unlock(&dp->port_rwlock);
881 dp_netdev_flow_free(struct dp_netdev_flow *flow)
883 struct dp_netdev_flow_stats *bucket;
886 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &flow->stats) {
887 ovs_mutex_destroy(&bucket->mutex);
888 free_cacheline(bucket);
890 ovsthread_stats_destroy(&flow->stats);
892 cls_rule_destroy(CONST_CAST(struct cls_rule *, &flow->cr));
893 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow));
894 ovs_mutex_destroy(&flow->mutex);
899 dp_netdev_remove_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
900 OVS_REQ_WRLOCK(dp->cls.rwlock)
901 OVS_REQUIRES(dp->flow_mutex)
903 struct cls_rule *cr = CONST_CAST(struct cls_rule *, &flow->cr);
904 struct hmap_node *node = CONST_CAST(struct hmap_node *, &flow->node);
906 classifier_remove(&dp->cls, cr);
907 hmap_remove(&dp->flow_table, node);
908 ovsrcu_postpone(dp_netdev_flow_free, flow);
912 dp_netdev_flow_flush(struct dp_netdev *dp)
914 struct dp_netdev_flow *netdev_flow, *next;
916 ovs_mutex_lock(&dp->flow_mutex);
917 fat_rwlock_wrlock(&dp->cls.rwlock);
918 HMAP_FOR_EACH_SAFE (netdev_flow, next, node, &dp->flow_table) {
919 dp_netdev_remove_flow(dp, netdev_flow);
921 fat_rwlock_unlock(&dp->cls.rwlock);
922 ovs_mutex_unlock(&dp->flow_mutex);
926 dpif_netdev_flow_flush(struct dpif *dpif)
928 struct dp_netdev *dp = get_dp_netdev(dpif);
930 dp_netdev_flow_flush(dp);
934 struct dp_netdev_port_state {
941 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
943 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
948 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
949 struct dpif_port *dpif_port)
951 struct dp_netdev_port_state *state = state_;
952 struct dp_netdev *dp = get_dp_netdev(dpif);
953 struct hmap_node *node;
956 ovs_rwlock_rdlock(&dp->port_rwlock);
957 node = hmap_at_position(&dp->ports, &state->bucket, &state->offset);
959 struct dp_netdev_port *port;
961 port = CONTAINER_OF(node, struct dp_netdev_port, node);
964 state->name = xstrdup(netdev_get_name(port->netdev));
965 dpif_port->name = state->name;
966 dpif_port->type = port->type;
967 dpif_port->port_no = port->port_no;
973 ovs_rwlock_unlock(&dp->port_rwlock);
979 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
981 struct dp_netdev_port_state *state = state_;
988 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
990 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
991 uint64_t new_port_seq;
994 new_port_seq = seq_read(dpif->dp->port_seq);
995 if (dpif->last_port_seq != new_port_seq) {
996 dpif->last_port_seq = new_port_seq;
1006 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
1008 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
1010 seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
1013 static struct dp_netdev_flow *
1014 dp_netdev_flow_cast(const struct cls_rule *cr)
1016 return cr ? CONTAINER_OF(cr, struct dp_netdev_flow, cr) : NULL;
1019 static struct dp_netdev_flow *
1020 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *flow)
1021 OVS_EXCLUDED(dp->cls.rwlock)
1023 struct dp_netdev_flow *netdev_flow;
1025 fat_rwlock_rdlock(&dp->cls.rwlock);
1026 netdev_flow = dp_netdev_flow_cast(classifier_lookup(&dp->cls, flow, NULL));
1027 fat_rwlock_unlock(&dp->cls.rwlock);
1032 static struct dp_netdev_flow *
1033 dp_netdev_find_flow(const struct dp_netdev *dp, const struct flow *flow)
1034 OVS_REQ_RDLOCK(dp->cls.rwlock)
1036 struct dp_netdev_flow *netdev_flow;
1038 HMAP_FOR_EACH_WITH_HASH (netdev_flow, node, flow_hash(flow, 0),
1040 if (flow_equal(&netdev_flow->flow, flow)) {
1049 get_dpif_flow_stats(struct dp_netdev_flow *netdev_flow,
1050 struct dpif_flow_stats *stats)
1052 struct dp_netdev_flow_stats *bucket;
1055 memset(stats, 0, sizeof *stats);
1056 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1057 ovs_mutex_lock(&bucket->mutex);
1058 stats->n_packets += bucket->packet_count;
1059 stats->n_bytes += bucket->byte_count;
1060 stats->used = MAX(stats->used, bucket->used);
1061 stats->tcp_flags |= bucket->tcp_flags;
1062 ovs_mutex_unlock(&bucket->mutex);
1067 dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1068 const struct nlattr *mask_key,
1069 uint32_t mask_key_len, const struct flow *flow,
1073 enum odp_key_fitness fitness;
1075 fitness = odp_flow_key_to_mask(mask_key, mask_key_len, mask, flow);
1077 /* This should not happen: it indicates that
1078 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
1079 * disagree on the acceptable form of a mask. Log the problem
1080 * as an error, with enough details to enable debugging. */
1081 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1083 if (!VLOG_DROP_ERR(&rl)) {
1087 odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s,
1089 VLOG_ERR("internal error parsing flow mask %s (%s)",
1090 ds_cstr(&s), odp_key_fitness_to_string(fitness));
1096 /* Force unwildcard the in_port. */
1097 mask->in_port.odp_port = u32_to_odp(UINT32_MAX);
1099 enum mf_field_id id;
1100 /* No mask key, unwildcard everything except fields whose
1101 * prerequisities are not met. */
1102 memset(mask, 0x0, sizeof *mask);
1104 for (id = 0; id < MFF_N_IDS; ++id) {
1105 /* Skip registers and metadata. */
1106 if (!(id >= MFF_REG0 && id < MFF_REG0 + FLOW_N_REGS)
1107 && id != MFF_METADATA) {
1108 const struct mf_field *mf = mf_from_id(id);
1109 if (mf_are_prereqs_ok(mf, flow)) {
1110 mf_mask_field(mf, mask);
1120 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1125 if (odp_flow_key_to_flow(key, key_len, flow)) {
1126 /* This should not happen: it indicates that odp_flow_key_from_flow()
1127 * and odp_flow_key_to_flow() disagree on the acceptable form of a
1128 * flow. Log the problem as an error, with enough details to enable
1130 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1132 if (!VLOG_DROP_ERR(&rl)) {
1136 odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
1137 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
1144 in_port = flow->in_port.odp_port;
1145 if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
1153 dpif_netdev_flow_get(const struct dpif *dpif,
1154 const struct nlattr *nl_key, size_t nl_key_len,
1155 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
1157 struct dp_netdev *dp = get_dp_netdev(dpif);
1158 struct dp_netdev_flow *netdev_flow;
1162 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
1167 fat_rwlock_rdlock(&dp->cls.rwlock);
1168 netdev_flow = dp_netdev_find_flow(dp, &key);
1169 fat_rwlock_unlock(&dp->cls.rwlock);
1173 get_dpif_flow_stats(netdev_flow, stats);
1177 struct dp_netdev_actions *actions;
1179 actions = dp_netdev_flow_get_actions(netdev_flow);
1180 *actionsp = ofpbuf_clone_data(actions->actions, actions->size);
1190 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *flow,
1191 const struct flow_wildcards *wc,
1192 const struct nlattr *actions,
1194 OVS_REQUIRES(dp->flow_mutex)
1196 struct dp_netdev_flow *netdev_flow;
1199 netdev_flow = xzalloc(sizeof *netdev_flow);
1200 *CONST_CAST(struct flow *, &netdev_flow->flow) = *flow;
1202 ovs_mutex_init(&netdev_flow->mutex);
1204 ovsthread_stats_init(&netdev_flow->stats);
1206 ovsrcu_set(&netdev_flow->actions,
1207 dp_netdev_actions_create(actions, actions_len));
1209 match_init(&match, flow, wc);
1210 cls_rule_init(CONST_CAST(struct cls_rule *, &netdev_flow->cr),
1211 &match, NETDEV_RULE_PRIORITY);
1212 fat_rwlock_wrlock(&dp->cls.rwlock);
1213 classifier_insert(&dp->cls,
1214 CONST_CAST(struct cls_rule *, &netdev_flow->cr));
1215 hmap_insert(&dp->flow_table,
1216 CONST_CAST(struct hmap_node *, &netdev_flow->node),
1217 flow_hash(flow, 0));
1218 fat_rwlock_unlock(&dp->cls.rwlock);
1224 clear_stats(struct dp_netdev_flow *netdev_flow)
1226 struct dp_netdev_flow_stats *bucket;
1229 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1230 ovs_mutex_lock(&bucket->mutex);
1232 bucket->packet_count = 0;
1233 bucket->byte_count = 0;
1234 bucket->tcp_flags = 0;
1235 ovs_mutex_unlock(&bucket->mutex);
1240 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
1242 struct dp_netdev *dp = get_dp_netdev(dpif);
1243 struct dp_netdev_flow *netdev_flow;
1245 struct flow_wildcards wc;
1248 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &flow);
1252 error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
1253 put->mask, put->mask_len,
1259 ovs_mutex_lock(&dp->flow_mutex);
1260 netdev_flow = dp_netdev_lookup_flow(dp, &flow);
1262 if (put->flags & DPIF_FP_CREATE) {
1263 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
1265 memset(put->stats, 0, sizeof *put->stats);
1267 error = dp_netdev_flow_add(dp, &flow, &wc, put->actions,
1276 if (put->flags & DPIF_FP_MODIFY
1277 && flow_equal(&flow, &netdev_flow->flow)) {
1278 struct dp_netdev_actions *new_actions;
1279 struct dp_netdev_actions *old_actions;
1281 new_actions = dp_netdev_actions_create(put->actions,
1284 old_actions = dp_netdev_flow_get_actions(netdev_flow);
1285 ovsrcu_set(&netdev_flow->actions, new_actions);
1288 get_dpif_flow_stats(netdev_flow, put->stats);
1290 if (put->flags & DPIF_FP_ZERO_STATS) {
1291 clear_stats(netdev_flow);
1294 ovsrcu_postpone(dp_netdev_actions_free, old_actions);
1295 } else if (put->flags & DPIF_FP_CREATE) {
1298 /* Overlapping flow. */
1302 ovs_mutex_unlock(&dp->flow_mutex);
1308 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
1310 struct dp_netdev *dp = get_dp_netdev(dpif);
1311 struct dp_netdev_flow *netdev_flow;
1315 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
1320 ovs_mutex_lock(&dp->flow_mutex);
1321 fat_rwlock_wrlock(&dp->cls.rwlock);
1322 netdev_flow = dp_netdev_find_flow(dp, &key);
1325 get_dpif_flow_stats(netdev_flow, del->stats);
1327 dp_netdev_remove_flow(dp, netdev_flow);
1331 fat_rwlock_unlock(&dp->cls.rwlock);
1332 ovs_mutex_unlock(&dp->flow_mutex);
1337 struct dp_netdev_flow_state {
1338 struct dp_netdev_actions *actions;
1339 struct odputil_keybuf keybuf;
1340 struct odputil_keybuf maskbuf;
1341 struct dpif_flow_stats stats;
1344 struct dp_netdev_flow_iter {
1348 struct ovs_mutex mutex;
1352 dpif_netdev_flow_dump_state_init(void **statep)
1354 struct dp_netdev_flow_state *state;
1356 *statep = state = xmalloc(sizeof *state);
1357 state->actions = NULL;
1361 dpif_netdev_flow_dump_state_uninit(void *state_)
1363 struct dp_netdev_flow_state *state = state_;
1369 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **iterp)
1371 struct dp_netdev_flow_iter *iter;
1373 *iterp = iter = xmalloc(sizeof *iter);
1377 ovs_mutex_init(&iter->mutex);
1381 /* XXX the caller must use 'actions' without quiescing */
1383 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *iter_, void *state_,
1384 const struct nlattr **key, size_t *key_len,
1385 const struct nlattr **mask, size_t *mask_len,
1386 const struct nlattr **actions, size_t *actions_len,
1387 const struct dpif_flow_stats **stats)
1389 struct dp_netdev_flow_iter *iter = iter_;
1390 struct dp_netdev_flow_state *state = state_;
1391 struct dp_netdev *dp = get_dp_netdev(dpif);
1392 struct dp_netdev_flow *netdev_flow;
1395 ovs_mutex_lock(&iter->mutex);
1396 error = iter->status;
1398 struct hmap_node *node;
1400 fat_rwlock_rdlock(&dp->cls.rwlock);
1401 node = hmap_at_position(&dp->flow_table, &iter->bucket, &iter->offset);
1403 netdev_flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
1405 fat_rwlock_unlock(&dp->cls.rwlock);
1407 iter->status = error = EOF;
1410 ovs_mutex_unlock(&iter->mutex);
1418 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
1419 odp_flow_key_from_flow(&buf, &netdev_flow->flow,
1420 netdev_flow->flow.in_port.odp_port);
1423 *key_len = buf.size;
1428 struct flow_wildcards wc;
1430 ofpbuf_use_stack(&buf, &state->maskbuf, sizeof state->maskbuf);
1431 minimask_expand(&netdev_flow->cr.match.mask, &wc);
1432 odp_flow_key_from_mask(&buf, &wc.masks, &netdev_flow->flow,
1433 odp_to_u32(wc.masks.in_port.odp_port),
1437 *mask_len = buf.size;
1440 if (actions || stats) {
1441 state->actions = NULL;
1444 state->actions = dp_netdev_flow_get_actions(netdev_flow);
1445 *actions = state->actions->actions;
1446 *actions_len = state->actions->size;
1450 get_dpif_flow_stats(netdev_flow, &state->stats);
1451 *stats = &state->stats;
1459 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *iter_)
1461 struct dp_netdev_flow_iter *iter = iter_;
1463 ovs_mutex_destroy(&iter->mutex);
1469 dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
1471 struct dp_netdev *dp = get_dp_netdev(dpif);
1472 struct pkt_metadata *md = &execute->md;
1475 if (execute->packet->size < ETH_HEADER_LEN ||
1476 execute->packet->size > UINT16_MAX) {
1480 /* Extract flow key. */
1481 flow_extract(execute->packet, md, &key);
1483 ovs_rwlock_rdlock(&dp->port_rwlock);
1484 dp_netdev_execute_actions(dp, &key, execute->packet, false, md,
1485 execute->actions, execute->actions_len);
1486 ovs_rwlock_unlock(&dp->port_rwlock);
1492 dp_netdev_destroy_all_queues(struct dp_netdev *dp)
1493 OVS_REQ_WRLOCK(dp->queue_rwlock)
1497 dp_netdev_purge_queues(dp);
1499 for (i = 0; i < dp->n_handlers; i++) {
1500 struct dp_netdev_queue *q = &dp->handler_queues[i];
1502 ovs_mutex_destroy(&q->mutex);
1503 seq_destroy(q->seq);
1505 free(dp->handler_queues);
1506 dp->handler_queues = NULL;
1511 dp_netdev_refresh_queues(struct dp_netdev *dp, uint32_t n_handlers)
1512 OVS_REQ_WRLOCK(dp->queue_rwlock)
1514 if (dp->n_handlers != n_handlers) {
1517 dp_netdev_destroy_all_queues(dp);
1519 dp->n_handlers = n_handlers;
1520 dp->handler_queues = xzalloc(n_handlers * sizeof *dp->handler_queues);
1522 for (i = 0; i < n_handlers; i++) {
1523 struct dp_netdev_queue *q = &dp->handler_queues[i];
1525 ovs_mutex_init(&q->mutex);
1526 q->seq = seq_create();
1532 dpif_netdev_recv_set(struct dpif *dpif, bool enable)
1534 struct dp_netdev *dp = get_dp_netdev(dpif);
1536 if ((dp->handler_queues != NULL) == enable) {
1540 fat_rwlock_wrlock(&dp->queue_rwlock);
1542 dp_netdev_destroy_all_queues(dp);
1544 dp_netdev_refresh_queues(dp, 1);
1546 fat_rwlock_unlock(&dp->queue_rwlock);
1552 dpif_netdev_handlers_set(struct dpif *dpif, uint32_t n_handlers)
1554 struct dp_netdev *dp = get_dp_netdev(dpif);
1556 fat_rwlock_wrlock(&dp->queue_rwlock);
1557 if (dp->handler_queues) {
1558 dp_netdev_refresh_queues(dp, n_handlers);
1560 fat_rwlock_unlock(&dp->queue_rwlock);
1566 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1567 uint32_t queue_id, uint32_t *priority)
1569 *priority = queue_id;
1574 dp_netdev_recv_check(const struct dp_netdev *dp, const uint32_t handler_id)
1575 OVS_REQ_RDLOCK(dp->queue_rwlock)
1577 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1579 if (!dp->handler_queues) {
1580 VLOG_WARN_RL(&rl, "receiving upcall disabled");
1584 if (handler_id >= dp->n_handlers) {
1585 VLOG_WARN_RL(&rl, "handler index out of bound");
1593 dpif_netdev_recv(struct dpif *dpif, uint32_t handler_id,
1594 struct dpif_upcall *upcall, struct ofpbuf *buf)
1596 struct dp_netdev *dp = get_dp_netdev(dpif);
1597 struct dp_netdev_queue *q;
1600 fat_rwlock_rdlock(&dp->queue_rwlock);
1602 if (!dp_netdev_recv_check(dp, handler_id)) {
1607 q = &dp->handler_queues[handler_id];
1608 ovs_mutex_lock(&q->mutex);
1609 if (q->head != q->tail) {
1610 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1612 *upcall = u->upcall;
1619 ovs_mutex_unlock(&q->mutex);
1622 fat_rwlock_unlock(&dp->queue_rwlock);
1628 dpif_netdev_recv_wait(struct dpif *dpif, uint32_t handler_id)
1630 struct dp_netdev *dp = get_dp_netdev(dpif);
1631 struct dp_netdev_queue *q;
1634 fat_rwlock_rdlock(&dp->queue_rwlock);
1636 if (!dp_netdev_recv_check(dp, handler_id)) {
1640 q = &dp->handler_queues[handler_id];
1641 ovs_mutex_lock(&q->mutex);
1642 seq = seq_read(q->seq);
1643 if (q->head != q->tail) {
1644 poll_immediate_wake();
1646 seq_wait(q->seq, seq);
1649 ovs_mutex_unlock(&q->mutex);
1652 fat_rwlock_unlock(&dp->queue_rwlock);
1656 dpif_netdev_recv_purge(struct dpif *dpif)
1658 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1660 fat_rwlock_wrlock(&dpif_netdev->dp->queue_rwlock);
1661 dp_netdev_purge_queues(dpif_netdev->dp);
1662 fat_rwlock_unlock(&dpif_netdev->dp->queue_rwlock);
1665 /* Creates and returns a new 'struct dp_netdev_actions', with a reference count
1666 * of 1, whose actions are a copy of from the 'ofpacts_len' bytes of
1668 struct dp_netdev_actions *
1669 dp_netdev_actions_create(const struct nlattr *actions, size_t size)
1671 struct dp_netdev_actions *netdev_actions;
1673 netdev_actions = xmalloc(sizeof *netdev_actions);
1674 netdev_actions->actions = xmemdup(actions, size);
1675 netdev_actions->size = size;
1677 return netdev_actions;
1680 struct dp_netdev_actions *
1681 dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow)
1683 return ovsrcu_get(struct dp_netdev_actions *, &flow->actions);
1687 dp_netdev_actions_free(struct dp_netdev_actions *actions)
1689 free(actions->actions);
1694 dp_forwarder_main(void *f_)
1696 struct dp_forwarder *f = f_;
1697 struct dp_netdev *dp = f->dp;
1699 f->name = xasprintf("forwarder_%u", ovsthread_id_self());
1700 set_subprogram_name("%s", f->name);
1702 while (!latch_is_set(&dp->exit_latch)) {
1703 bool received_anything;
1706 ovs_rwlock_rdlock(&dp->port_rwlock);
1707 for (i = 0; i < 50; i++) {
1708 struct dp_netdev_port *port;
1710 received_anything = false;
1711 HMAP_FOR_EACH (port, node, &f->dp->ports) {
1713 && port->node.hash >= f->min_hash
1714 && port->node.hash <= f->max_hash) {
1715 struct ofpbuf *packets[NETDEV_MAX_RX_BATCH];
1719 error = netdev_rx_recv(port->rx, packets, &count);
1722 struct pkt_metadata md
1723 = PKT_METADATA_INITIALIZER(port->port_no);
1725 for (i = 0; i < count; i++) {
1726 dp_netdev_port_input(dp, packets[i], &md);
1728 received_anything = true;
1729 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1730 static struct vlog_rate_limit rl
1731 = VLOG_RATE_LIMIT_INIT(1, 5);
1733 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1734 netdev_get_name(port->netdev),
1735 ovs_strerror(error));
1740 if (!received_anything) {
1745 if (received_anything) {
1746 poll_immediate_wake();
1748 struct dp_netdev_port *port;
1750 HMAP_FOR_EACH (port, node, &f->dp->ports)
1752 && port->node.hash >= f->min_hash
1753 && port->node.hash <= f->max_hash) {
1754 netdev_rx_wait(port->rx);
1756 seq_wait(dp->port_seq, seq_read(dp->port_seq));
1757 latch_wait(&dp->exit_latch);
1759 ovs_rwlock_unlock(&dp->port_rwlock);
1770 dp_netdev_set_threads(struct dp_netdev *dp, int n)
1774 if (n == dp->n_forwarders) {
1778 /* Stop existing threads. */
1779 latch_set(&dp->exit_latch);
1780 for (i = 0; i < dp->n_forwarders; i++) {
1781 struct dp_forwarder *f = &dp->forwarders[i];
1783 xpthread_join(f->thread, NULL);
1785 latch_poll(&dp->exit_latch);
1786 free(dp->forwarders);
1788 /* Start new threads. */
1789 dp->forwarders = xmalloc(n * sizeof *dp->forwarders);
1790 dp->n_forwarders = n;
1791 for (i = 0; i < n; i++) {
1792 struct dp_forwarder *f = &dp->forwarders[i];
1795 f->min_hash = UINT32_MAX / n * i;
1796 f->max_hash = UINT32_MAX / n * (i + 1) - 1;
1798 f->max_hash = UINT32_MAX;
1800 xpthread_create(&f->thread, NULL, dp_forwarder_main, f);
1805 dp_netdev_flow_stats_new_cb(void)
1807 struct dp_netdev_flow_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1808 ovs_mutex_init(&bucket->mutex);
1813 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow,
1814 const struct ofpbuf *packet,
1815 const struct flow *key)
1817 uint16_t tcp_flags = ntohs(key->tcp_flags);
1818 long long int now = time_msec();
1819 struct dp_netdev_flow_stats *bucket;
1821 bucket = ovsthread_stats_bucket_get(&netdev_flow->stats,
1822 dp_netdev_flow_stats_new_cb);
1824 ovs_mutex_lock(&bucket->mutex);
1825 bucket->used = MAX(now, bucket->used);
1826 bucket->packet_count++;
1827 bucket->byte_count += packet->size;
1828 bucket->tcp_flags |= tcp_flags;
1829 ovs_mutex_unlock(&bucket->mutex);
1833 dp_netdev_stats_new_cb(void)
1835 struct dp_netdev_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1836 ovs_mutex_init(&bucket->mutex);
1841 dp_netdev_count_packet(struct dp_netdev *dp, enum dp_stat_type type)
1843 struct dp_netdev_stats *bucket;
1845 bucket = ovsthread_stats_bucket_get(&dp->stats, dp_netdev_stats_new_cb);
1846 ovs_mutex_lock(&bucket->mutex);
1848 ovs_mutex_unlock(&bucket->mutex);
1852 dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
1853 struct pkt_metadata *md)
1854 OVS_REQ_RDLOCK(dp->port_rwlock)
1856 struct dp_netdev_flow *netdev_flow;
1859 if (packet->size < ETH_HEADER_LEN) {
1860 ofpbuf_delete(packet);
1863 flow_extract(packet, md, &key);
1864 netdev_flow = dp_netdev_lookup_flow(dp, &key);
1866 struct dp_netdev_actions *actions;
1868 dp_netdev_flow_used(netdev_flow, packet, &key);
1870 actions = dp_netdev_flow_get_actions(netdev_flow);
1871 dp_netdev_execute_actions(dp, &key, packet, true, md,
1872 actions->actions, actions->size);
1873 dp_netdev_count_packet(dp, DP_STAT_HIT);
1874 } else if (dp->handler_queues) {
1875 dp_netdev_count_packet(dp, DP_STAT_MISS);
1876 dp_netdev_output_userspace(dp, packet,
1877 flow_hash_5tuple(&key, 0) % dp->n_handlers,
1878 DPIF_UC_MISS, &key, NULL);
1879 ofpbuf_delete(packet);
1884 dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *packet,
1885 int queue_no, int type, const struct flow *flow,
1886 const struct nlattr *userdata)
1887 OVS_EXCLUDED(dp->queue_rwlock)
1889 struct dp_netdev_queue *q;
1892 fat_rwlock_rdlock(&dp->queue_rwlock);
1893 q = &dp->handler_queues[queue_no];
1894 ovs_mutex_lock(&q->mutex);
1895 if (q->head - q->tail < MAX_QUEUE_LEN) {
1896 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
1897 struct dpif_upcall *upcall = &u->upcall;
1898 struct ofpbuf *buf = &u->buf;
1901 upcall->type = type;
1903 /* Allocate buffer big enough for everything. */
1904 buf_size = ODPUTIL_FLOW_KEY_BYTES;
1906 buf_size += NLA_ALIGN(userdata->nla_len);
1908 buf_size += packet->size;
1909 ofpbuf_init(buf, buf_size);
1912 odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
1913 upcall->key = buf->data;
1914 upcall->key_len = buf->size;
1918 upcall->userdata = ofpbuf_put(buf, userdata,
1919 NLA_ALIGN(userdata->nla_len));
1922 upcall->packet.data = ofpbuf_put(buf, packet->data, packet->size);
1923 upcall->packet.size = packet->size;
1929 dp_netdev_count_packet(dp, DP_STAT_LOST);
1932 ovs_mutex_unlock(&q->mutex);
1933 fat_rwlock_unlock(&dp->queue_rwlock);
1938 struct dp_netdev_execute_aux {
1939 struct dp_netdev *dp;
1940 const struct flow *key;
1944 dp_execute_cb(void *aux_, struct ofpbuf *packet,
1945 const struct pkt_metadata *md OVS_UNUSED,
1946 const struct nlattr *a, bool may_steal)
1947 OVS_NO_THREAD_SAFETY_ANALYSIS
1949 struct dp_netdev_execute_aux *aux = aux_;
1950 int type = nl_attr_type(a);
1951 struct dp_netdev_port *p;
1953 switch ((enum ovs_action_attr)type) {
1954 case OVS_ACTION_ATTR_OUTPUT:
1955 p = dp_netdev_lookup_port(aux->dp, u32_to_odp(nl_attr_get_u32(a)));
1957 netdev_send(p->netdev, packet, may_steal);
1961 case OVS_ACTION_ATTR_USERSPACE: {
1962 const struct nlattr *userdata;
1964 userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
1966 dp_netdev_output_userspace(aux->dp, packet,
1967 flow_hash_5tuple(aux->key, 0)
1968 % aux->dp->n_handlers,
1969 DPIF_UC_ACTION, aux->key,
1973 ofpbuf_delete(packet);
1977 case OVS_ACTION_ATTR_PUSH_VLAN:
1978 case OVS_ACTION_ATTR_POP_VLAN:
1979 case OVS_ACTION_ATTR_PUSH_MPLS:
1980 case OVS_ACTION_ATTR_POP_MPLS:
1981 case OVS_ACTION_ATTR_SET:
1982 case OVS_ACTION_ATTR_SAMPLE:
1983 case OVS_ACTION_ATTR_UNSPEC:
1984 case __OVS_ACTION_ATTR_MAX:
1991 dp_netdev_execute_actions(struct dp_netdev *dp, const struct flow *key,
1992 struct ofpbuf *packet, bool may_steal,
1993 struct pkt_metadata *md,
1994 const struct nlattr *actions, size_t actions_len)
1995 OVS_REQ_RDLOCK(dp->port_rwlock)
1997 struct dp_netdev_execute_aux aux = {dp, key};
1999 odp_execute_actions(&aux, packet, may_steal, md,
2000 actions, actions_len, dp_execute_cb);
2003 const struct dpif_class dpif_netdev_class = {
2005 dpif_netdev_enumerate,
2006 dpif_netdev_port_open_type,
2009 dpif_netdev_destroy,
2012 dpif_netdev_get_stats,
2013 dpif_netdev_port_add,
2014 dpif_netdev_port_del,
2015 dpif_netdev_port_query_by_number,
2016 dpif_netdev_port_query_by_name,
2017 NULL, /* port_get_pid */
2018 dpif_netdev_port_dump_start,
2019 dpif_netdev_port_dump_next,
2020 dpif_netdev_port_dump_done,
2021 dpif_netdev_port_poll,
2022 dpif_netdev_port_poll_wait,
2023 dpif_netdev_flow_get,
2024 dpif_netdev_flow_put,
2025 dpif_netdev_flow_del,
2026 dpif_netdev_flow_flush,
2027 dpif_netdev_flow_dump_state_init,
2028 dpif_netdev_flow_dump_start,
2029 dpif_netdev_flow_dump_next,
2031 dpif_netdev_flow_dump_done,
2032 dpif_netdev_flow_dump_state_uninit,
2033 dpif_netdev_execute,
2035 dpif_netdev_recv_set,
2036 dpif_netdev_handlers_set,
2037 dpif_netdev_queue_to_priority,
2039 dpif_netdev_recv_wait,
2040 dpif_netdev_recv_purge,
2044 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
2045 const char *argv[], void *aux OVS_UNUSED)
2047 struct dp_netdev_port *port;
2048 struct dp_netdev *dp;
2051 ovs_mutex_lock(&dp_netdev_mutex);
2052 dp = shash_find_data(&dp_netdevs, argv[1]);
2053 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
2054 ovs_mutex_unlock(&dp_netdev_mutex);
2055 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
2058 ovs_refcount_ref(&dp->ref_cnt);
2059 ovs_mutex_unlock(&dp_netdev_mutex);
2061 ovs_rwlock_wrlock(&dp->port_rwlock);
2062 if (get_port_by_name(dp, argv[2], &port)) {
2063 unixctl_command_reply_error(conn, "unknown port");
2067 port_no = u32_to_odp(atoi(argv[3]));
2068 if (!port_no || port_no == ODPP_NONE) {
2069 unixctl_command_reply_error(conn, "bad port number");
2072 if (dp_netdev_lookup_port(dp, port_no)) {
2073 unixctl_command_reply_error(conn, "port number already in use");
2076 hmap_remove(&dp->ports, &port->node);
2077 port->port_no = port_no;
2078 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
2079 seq_change(dp->port_seq);
2080 unixctl_command_reply(conn, NULL);
2083 ovs_rwlock_unlock(&dp->port_rwlock);
2084 dp_netdev_unref(dp);
2088 dpif_dummy_register__(const char *type)
2090 struct dpif_class *class;
2092 class = xmalloc(sizeof *class);
2093 *class = dpif_netdev_class;
2094 class->type = xstrdup(type);
2095 dp_register_provider(class);
2099 dpif_dummy_register(bool override)
2106 dp_enumerate_types(&types);
2107 SSET_FOR_EACH (type, &types) {
2108 if (!dp_unregister_provider(type)) {
2109 dpif_dummy_register__(type);
2112 sset_destroy(&types);
2115 dpif_dummy_register__("dummy");
2117 unixctl_command_register("dpif-dummy/change-port-number",
2118 "DP PORT NEW-NUMBER",
2119 3, 3, dpif_dummy_change_port_number, NULL);