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
71 /* Configuration parameters. */
72 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
75 enum { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
76 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
77 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
79 /* Protects against changes to 'dp_netdevs'. */
80 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
82 /* Contains all 'struct dp_netdev's. */
83 static struct shash dp_netdevs OVS_GUARDED_BY(dp_netdev_mutex)
84 = SHASH_INITIALIZER(&dp_netdevs);
86 struct dp_netdev_upcall {
87 struct dpif_upcall upcall; /* Queued upcall information. */
88 struct ofpbuf buf; /* ofpbuf instance for upcall.packet. */
91 /* A queue passing packets from a struct dp_netdev to its clients (handlers).
97 * Any access at all requires the owning 'dp_netdev''s queue_rwlock and
99 struct dp_netdev_queue {
100 struct ovs_mutex mutex;
101 struct seq *seq; /* Incremented whenever a packet is queued. */
102 struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN] OVS_GUARDED;
103 unsigned int head OVS_GUARDED;
104 unsigned int tail OVS_GUARDED;
107 /* Datapath based on the network device interface from netdev.h.
113 * Some members, marked 'const', are immutable. Accessing other members
114 * requires synchronization, as noted in more detail below.
116 * Acquisition order is, from outermost to innermost:
118 * dp_netdev_mutex (global)
125 const struct dpif_class *const class;
126 const char *const name;
127 struct ovs_refcount ref_cnt;
128 atomic_flag destroyed;
132 * Readers of 'cls' and 'flow_table' must take a 'cls->rwlock' read lock.
134 * Writers of 'cls' and 'flow_table' must take the 'flow_mutex' and then
135 * the 'cls->rwlock' write lock. (The outer 'flow_mutex' allows writers to
136 * atomically perform multiple operations on 'cls' and 'flow_table'.)
138 struct ovs_mutex flow_mutex;
139 struct classifier cls; /* Classifier. Protected by cls.rwlock. */
140 struct hmap flow_table OVS_GUARDED; /* Flow table. */
144 * 'queue_rwlock' protects the modification of 'handler_queues' and
145 * 'n_handlers'. The queue elements are protected by its
146 * 'handler_queues''s mutex. */
147 struct fat_rwlock queue_rwlock;
148 struct dp_netdev_queue *handler_queues;
153 * ovsthread_stats is internally synchronized. */
154 struct ovsthread_stats stats; /* Contains 'struct dp_netdev_stats *'. */
158 * Any lookup into 'ports' or any access to the dp_netdev_ports found
159 * through 'ports' requires taking 'port_rwlock'. */
160 struct ovs_rwlock port_rwlock;
161 struct hmap ports OVS_GUARDED;
162 struct seq *port_seq; /* Incremented whenever a port changes. */
164 /* Forwarding threads. */
165 struct latch exit_latch;
166 struct pmd_thread *pmd_threads;
167 size_t n_pmd_threads;
171 static struct dp_netdev_port *dp_netdev_lookup_port(const struct dp_netdev *dp,
173 OVS_REQ_RDLOCK(dp->port_rwlock);
176 DP_STAT_HIT, /* Packets that matched in the flow table. */
177 DP_STAT_MISS, /* Packets that did not match. */
178 DP_STAT_LOST, /* Packets not passed up to the client. */
182 /* Contained by struct dp_netdev's 'stats' member. */
183 struct dp_netdev_stats {
184 struct ovs_mutex mutex; /* Protects 'n'. */
186 /* Indexed by DP_STAT_*, protected by 'mutex'. */
187 unsigned long long int n[DP_N_STATS] OVS_GUARDED;
191 /* A port in a netdev-based datapath. */
192 struct dp_netdev_port {
193 struct hmap_node node; /* Node in dp_netdev's 'ports'. */
195 struct netdev *netdev;
196 struct netdev_saved_flags *sf;
197 struct netdev_rx *rx;
198 struct ovs_refcount ref_cnt;
199 char *type; /* Port type as requested by user. */
202 /* A flow in dp_netdev's 'flow_table'.
208 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
209 * its dp_netdev's classifier. The text below calls this classifier 'cls'.
214 * The thread safety rules described here for "struct dp_netdev_flow" are
215 * motivated by two goals:
217 * - Prevent threads that read members of "struct dp_netdev_flow" from
218 * reading bad data due to changes by some thread concurrently modifying
221 * - Prevent two threads making changes to members of a given "struct
222 * dp_netdev_flow" from interfering with each other.
228 * A flow 'flow' may be accessed without a risk of being freed by code that
229 * holds a read-lock or write-lock on 'cls->rwlock' or that owns a reference to
230 * 'flow->ref_cnt' (or both). Code that needs to hold onto a flow for a while
231 * should take 'cls->rwlock', find the flow it needs, increment 'flow->ref_cnt'
232 * with dpif_netdev_flow_ref(), and drop 'cls->rwlock'.
234 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
235 * flow from being deleted from 'cls' (that's 'cls->rwlock') and it doesn't
236 * protect members of 'flow' from modification (that's 'flow->mutex').
238 * 'flow->mutex' protects the members of 'flow' from modification. It doesn't
239 * protect the flow from being deleted from 'cls' (that's 'cls->rwlock') and it
240 * doesn't prevent the flow from being freed (that's 'flow->ref_cnt').
242 * Some members, marked 'const', are immutable. Accessing other members
243 * requires synchronization, as noted in more detail below.
245 struct dp_netdev_flow {
246 /* Packet classification. */
247 const struct cls_rule cr; /* In owning dp_netdev's 'cls'. */
249 /* Hash table index by unmasked flow. */
250 const struct hmap_node node; /* In owning dp_netdev's 'flow_table'. */
251 const struct flow flow; /* The flow that created this entry. */
253 /* Protects members marked OVS_GUARDED.
255 * Acquire after datapath's flow_mutex. */
256 struct ovs_mutex mutex OVS_ACQ_AFTER(dp_netdev_mutex);
260 * Reading or writing these members requires 'mutex'. */
261 struct ovsthread_stats stats; /* Contains "struct dp_netdev_flow_stats". */
265 * Reading 'actions' requires 'mutex'.
266 * Writing 'actions' requires 'mutex' and (to allow for transactions) the
267 * datapath's flow_mutex. */
268 OVSRCU_TYPE(struct dp_netdev_actions *) actions;
271 static void dp_netdev_flow_free(struct dp_netdev_flow *);
273 /* Contained by struct dp_netdev_flow's 'stats' member. */
274 struct dp_netdev_flow_stats {
275 struct ovs_mutex mutex; /* Guards all the other members. */
277 long long int used OVS_GUARDED; /* Last used time, in monotonic msecs. */
278 long long int packet_count OVS_GUARDED; /* Number of packets matched. */
279 long long int byte_count OVS_GUARDED; /* Number of bytes matched. */
280 uint16_t tcp_flags OVS_GUARDED; /* Bitwise-OR of seen tcp_flags values. */
283 /* A set of datapath actions within a "struct dp_netdev_flow".
289 * A struct dp_netdev_actions 'actions' may be accessed without a risk of being
290 * freed by code that holds a read-lock or write-lock on 'flow->mutex' (where
291 * 'flow' is the dp_netdev_flow for which 'flow->actions == actions') or that
292 * owns a reference to 'actions->ref_cnt' (or both). */
293 struct dp_netdev_actions {
294 /* These members are immutable: they do not change during the struct's
296 struct nlattr *actions; /* Sequence of OVS_ACTION_ATTR_* attributes. */
297 unsigned int size; /* Size of 'actions', in bytes. */
300 struct dp_netdev_actions *dp_netdev_actions_create(const struct nlattr *,
302 struct dp_netdev_actions *dp_netdev_flow_get_actions(
303 const struct dp_netdev_flow *);
304 static void dp_netdev_actions_free(struct dp_netdev_actions *);
306 /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
307 * the performance overhead of interrupt processing. Therefore netdev can
308 * not implement rx-wait for these devices. dpif-netdev needs to poll
309 * these device to check for recv buffer. pmd-thread does polling for
310 * devices assigned to itself thread.
312 * DPDK used PMD for accessing NIC.
314 * A thread that receives packets from PMD ports, looks them up in the flow
315 * table, and executes the actions it finds.
318 struct dp_netdev *dp;
321 atomic_uint change_seq;
325 /* Interface to netdev-based datapath. */
328 struct dp_netdev *dp;
329 uint64_t last_port_seq;
332 static int get_port_by_number(struct dp_netdev *dp, odp_port_t port_no,
333 struct dp_netdev_port **portp)
334 OVS_REQ_RDLOCK(dp->port_rwlock);
335 static int get_port_by_name(struct dp_netdev *dp, const char *devname,
336 struct dp_netdev_port **portp)
337 OVS_REQ_RDLOCK(dp->port_rwlock);
338 static void dp_netdev_free(struct dp_netdev *)
339 OVS_REQUIRES(dp_netdev_mutex);
340 static void dp_netdev_flow_flush(struct dp_netdev *);
341 static int do_add_port(struct dp_netdev *dp, const char *devname,
342 const char *type, odp_port_t port_no)
343 OVS_REQ_WRLOCK(dp->port_rwlock);
344 static int do_del_port(struct dp_netdev *dp, odp_port_t port_no)
345 OVS_REQ_WRLOCK(dp->port_rwlock);
346 static void dp_netdev_destroy_all_queues(struct dp_netdev *dp)
347 OVS_REQ_WRLOCK(dp->queue_rwlock);
348 static int dpif_netdev_open(const struct dpif_class *, const char *name,
349 bool create, struct dpif **);
350 static int dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *,
351 int queue_no, int type,
353 const struct nlattr *userdata);
354 static void dp_netdev_execute_actions(struct dp_netdev *dp,
355 const struct flow *, struct ofpbuf *, bool may_steal,
356 struct pkt_metadata *,
357 const struct nlattr *actions,
359 static void dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
360 struct pkt_metadata *);
362 static void dp_netdev_set_pmd_threads(struct dp_netdev *, int n);
364 static struct dpif_netdev *
365 dpif_netdev_cast(const struct dpif *dpif)
367 ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
368 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
371 static struct dp_netdev *
372 get_dp_netdev(const struct dpif *dpif)
374 return dpif_netdev_cast(dpif)->dp;
378 dpif_netdev_enumerate(struct sset *all_dps)
380 struct shash_node *node;
382 ovs_mutex_lock(&dp_netdev_mutex);
383 SHASH_FOR_EACH(node, &dp_netdevs) {
384 sset_add(all_dps, node->name);
386 ovs_mutex_unlock(&dp_netdev_mutex);
392 dpif_netdev_class_is_dummy(const struct dpif_class *class)
394 return class != &dpif_netdev_class;
398 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
400 return strcmp(type, "internal") ? type
401 : dpif_netdev_class_is_dummy(class) ? "dummy"
406 create_dpif_netdev(struct dp_netdev *dp)
408 uint16_t netflow_id = hash_string(dp->name, 0);
409 struct dpif_netdev *dpif;
411 ovs_refcount_ref(&dp->ref_cnt);
413 dpif = xmalloc(sizeof *dpif);
414 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
416 dpif->last_port_seq = seq_read(dp->port_seq);
421 /* Choose an unused, non-zero port number and return it on success.
422 * Return ODPP_NONE on failure. */
424 choose_port(struct dp_netdev *dp, const char *name)
425 OVS_REQ_RDLOCK(dp->port_rwlock)
429 if (dp->class != &dpif_netdev_class) {
433 /* If the port name begins with "br", start the number search at
434 * 100 to make writing tests easier. */
435 if (!strncmp(name, "br", 2)) {
439 /* If the port name contains a number, try to assign that port number.
440 * This can make writing unit tests easier because port numbers are
442 for (p = name; *p != '\0'; p++) {
443 if (isdigit((unsigned char) *p)) {
444 port_no = start_no + strtol(p, NULL, 10);
445 if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE)
446 && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
447 return u32_to_odp(port_no);
454 for (port_no = 1; port_no <= UINT16_MAX; port_no++) {
455 if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
456 return u32_to_odp(port_no);
464 create_dp_netdev(const char *name, const struct dpif_class *class,
465 struct dp_netdev **dpp)
466 OVS_REQUIRES(dp_netdev_mutex)
468 struct dp_netdev *dp;
471 dp = xzalloc(sizeof *dp);
472 shash_add(&dp_netdevs, name, dp);
474 *CONST_CAST(const struct dpif_class **, &dp->class) = class;
475 *CONST_CAST(const char **, &dp->name) = xstrdup(name);
476 ovs_refcount_init(&dp->ref_cnt);
477 atomic_flag_clear(&dp->destroyed);
479 ovs_mutex_init(&dp->flow_mutex);
480 classifier_init(&dp->cls, NULL);
481 hmap_init(&dp->flow_table);
483 fat_rwlock_init(&dp->queue_rwlock);
485 ovsthread_stats_init(&dp->stats);
487 ovs_rwlock_init(&dp->port_rwlock);
488 hmap_init(&dp->ports);
489 dp->port_seq = seq_create();
490 latch_init(&dp->exit_latch);
492 ovs_rwlock_wrlock(&dp->port_rwlock);
493 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
494 ovs_rwlock_unlock(&dp->port_rwlock);
505 dpif_netdev_open(const struct dpif_class *class, const char *name,
506 bool create, struct dpif **dpifp)
508 struct dp_netdev *dp;
511 ovs_mutex_lock(&dp_netdev_mutex);
512 dp = shash_find_data(&dp_netdevs, name);
514 error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
516 error = (dp->class != class ? EINVAL
521 *dpifp = create_dpif_netdev(dp);
523 ovs_mutex_unlock(&dp_netdev_mutex);
529 dp_netdev_purge_queues(struct dp_netdev *dp)
530 OVS_REQ_WRLOCK(dp->queue_rwlock)
534 for (i = 0; i < dp->n_handlers; i++) {
535 struct dp_netdev_queue *q = &dp->handler_queues[i];
537 ovs_mutex_lock(&q->mutex);
538 while (q->tail != q->head) {
539 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
540 ofpbuf_uninit(&u->upcall.packet);
541 ofpbuf_uninit(&u->buf);
543 ovs_mutex_unlock(&q->mutex);
547 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
548 * through the 'dp_netdevs' shash while freeing 'dp'. */
550 dp_netdev_free(struct dp_netdev *dp)
551 OVS_REQUIRES(dp_netdev_mutex)
553 struct dp_netdev_port *port, *next;
554 struct dp_netdev_stats *bucket;
557 shash_find_and_delete(&dp_netdevs, dp->name);
559 dp_netdev_set_pmd_threads(dp, 0);
560 free(dp->pmd_threads);
562 dp_netdev_flow_flush(dp);
563 ovs_rwlock_wrlock(&dp->port_rwlock);
564 HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) {
565 do_del_port(dp, port->port_no);
567 ovs_rwlock_unlock(&dp->port_rwlock);
569 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
570 ovs_mutex_destroy(&bucket->mutex);
571 free_cacheline(bucket);
573 ovsthread_stats_destroy(&dp->stats);
575 fat_rwlock_wrlock(&dp->queue_rwlock);
576 dp_netdev_destroy_all_queues(dp);
577 fat_rwlock_unlock(&dp->queue_rwlock);
579 fat_rwlock_destroy(&dp->queue_rwlock);
581 classifier_destroy(&dp->cls);
582 hmap_destroy(&dp->flow_table);
583 ovs_mutex_destroy(&dp->flow_mutex);
584 seq_destroy(dp->port_seq);
585 hmap_destroy(&dp->ports);
586 latch_destroy(&dp->exit_latch);
587 free(CONST_CAST(char *, dp->name));
592 dp_netdev_unref(struct dp_netdev *dp)
595 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
596 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
597 ovs_mutex_lock(&dp_netdev_mutex);
598 if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
601 ovs_mutex_unlock(&dp_netdev_mutex);
606 dpif_netdev_close(struct dpif *dpif)
608 struct dp_netdev *dp = get_dp_netdev(dpif);
615 dpif_netdev_destroy(struct dpif *dpif)
617 struct dp_netdev *dp = get_dp_netdev(dpif);
619 if (!atomic_flag_test_and_set(&dp->destroyed)) {
620 if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
621 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
630 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
632 struct dp_netdev *dp = get_dp_netdev(dpif);
633 struct dp_netdev_stats *bucket;
636 fat_rwlock_rdlock(&dp->cls.rwlock);
637 stats->n_flows = hmap_count(&dp->flow_table);
638 fat_rwlock_unlock(&dp->cls.rwlock);
640 stats->n_hit = stats->n_missed = stats->n_lost = 0;
641 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
642 ovs_mutex_lock(&bucket->mutex);
643 stats->n_hit += bucket->n[DP_STAT_HIT];
644 stats->n_missed += bucket->n[DP_STAT_MISS];
645 stats->n_lost += bucket->n[DP_STAT_LOST];
646 ovs_mutex_unlock(&bucket->mutex);
648 stats->n_masks = UINT32_MAX;
649 stats->n_mask_hit = UINT64_MAX;
655 dp_netdev_reload_pmd_threads(struct dp_netdev *dp)
659 for (i = 0; i < dp->n_pmd_threads; i++) {
660 struct pmd_thread *f = &dp->pmd_threads[i];
663 atomic_add(&f->change_seq, 1, &id);
668 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
670 OVS_REQ_WRLOCK(dp->port_rwlock)
672 struct netdev_saved_flags *sf;
673 struct dp_netdev_port *port;
674 struct netdev *netdev;
675 enum netdev_flags flags;
676 const char *open_type;
679 /* XXX reject devices already in some dp_netdev. */
681 /* Open and validate network device. */
682 open_type = dpif_netdev_port_open_type(dp->class, type);
683 error = netdev_open(devname, open_type, &netdev);
687 /* XXX reject non-Ethernet devices */
689 netdev_get_flags(netdev, &flags);
690 if (flags & NETDEV_LOOPBACK) {
691 VLOG_ERR("%s: cannot add a loopback device", devname);
692 netdev_close(netdev);
696 port = xzalloc(sizeof *port);
697 port->port_no = port_no;
698 port->netdev = netdev;
699 port->type = xstrdup(type);
700 error = netdev_rx_open(netdev, &port->rx);
702 && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
703 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
704 devname, ovs_strerror(errno));
705 netdev_close(netdev);
709 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
711 netdev_rx_close(port->rx);
712 netdev_close(netdev);
719 if (netdev_is_pmd(netdev)) {
721 dp_netdev_set_pmd_threads(dp, NR_THREADS);
722 dp_netdev_reload_pmd_threads(dp);
724 ovs_refcount_init(&port->ref_cnt);
726 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
727 seq_change(dp->port_seq);
733 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
734 odp_port_t *port_nop)
736 struct dp_netdev *dp = get_dp_netdev(dpif);
737 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
738 const char *dpif_port;
742 ovs_rwlock_wrlock(&dp->port_rwlock);
743 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
744 if (*port_nop != ODPP_NONE) {
746 error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0;
748 port_no = choose_port(dp, dpif_port);
749 error = port_no == ODPP_NONE ? EFBIG : 0;
753 error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
755 ovs_rwlock_unlock(&dp->port_rwlock);
761 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
763 struct dp_netdev *dp = get_dp_netdev(dpif);
766 ovs_rwlock_wrlock(&dp->port_rwlock);
767 error = port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
768 ovs_rwlock_unlock(&dp->port_rwlock);
774 is_valid_port_number(odp_port_t port_no)
776 return port_no != ODPP_NONE;
779 static struct dp_netdev_port *
780 dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no)
781 OVS_REQ_RDLOCK(dp->port_rwlock)
783 struct dp_netdev_port *port;
785 HMAP_FOR_EACH_IN_BUCKET (port, node, hash_int(odp_to_u32(port_no), 0),
787 if (port->port_no == port_no) {
795 get_port_by_number(struct dp_netdev *dp,
796 odp_port_t port_no, struct dp_netdev_port **portp)
797 OVS_REQ_RDLOCK(dp->port_rwlock)
799 if (!is_valid_port_number(port_no)) {
803 *portp = dp_netdev_lookup_port(dp, port_no);
804 return *portp ? 0 : ENOENT;
809 port_ref(struct dp_netdev_port *port)
812 ovs_refcount_ref(&port->ref_cnt);
817 port_unref(struct dp_netdev_port *port)
819 if (port && ovs_refcount_unref(&port->ref_cnt) == 1) {
820 netdev_close(port->netdev);
821 netdev_restore_flags(port->sf);
822 netdev_rx_close(port->rx);
829 get_port_by_name(struct dp_netdev *dp,
830 const char *devname, struct dp_netdev_port **portp)
831 OVS_REQ_RDLOCK(dp->port_rwlock)
833 struct dp_netdev_port *port;
835 HMAP_FOR_EACH (port, node, &dp->ports) {
836 if (!strcmp(netdev_get_name(port->netdev), devname)) {
845 do_del_port(struct dp_netdev *dp, odp_port_t port_no)
846 OVS_REQ_WRLOCK(dp->port_rwlock)
848 struct dp_netdev_port *port;
851 error = get_port_by_number(dp, port_no, &port);
856 hmap_remove(&dp->ports, &port->node);
857 seq_change(dp->port_seq);
858 if (netdev_is_pmd(port->netdev)) {
859 dp_netdev_reload_pmd_threads(dp);
867 answer_port_query(const struct dp_netdev_port *port,
868 struct dpif_port *dpif_port)
870 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
871 dpif_port->type = xstrdup(port->type);
872 dpif_port->port_no = port->port_no;
876 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
877 struct dpif_port *dpif_port)
879 struct dp_netdev *dp = get_dp_netdev(dpif);
880 struct dp_netdev_port *port;
883 ovs_rwlock_rdlock(&dp->port_rwlock);
884 error = get_port_by_number(dp, port_no, &port);
885 if (!error && dpif_port) {
886 answer_port_query(port, dpif_port);
888 ovs_rwlock_unlock(&dp->port_rwlock);
894 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
895 struct dpif_port *dpif_port)
897 struct dp_netdev *dp = get_dp_netdev(dpif);
898 struct dp_netdev_port *port;
901 ovs_rwlock_rdlock(&dp->port_rwlock);
902 error = get_port_by_name(dp, devname, &port);
903 if (!error && dpif_port) {
904 answer_port_query(port, dpif_port);
906 ovs_rwlock_unlock(&dp->port_rwlock);
912 dp_netdev_flow_free(struct dp_netdev_flow *flow)
914 struct dp_netdev_flow_stats *bucket;
917 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &flow->stats) {
918 ovs_mutex_destroy(&bucket->mutex);
919 free_cacheline(bucket);
921 ovsthread_stats_destroy(&flow->stats);
923 cls_rule_destroy(CONST_CAST(struct cls_rule *, &flow->cr));
924 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow));
925 ovs_mutex_destroy(&flow->mutex);
930 dp_netdev_remove_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
931 OVS_REQ_WRLOCK(dp->cls.rwlock)
932 OVS_REQUIRES(dp->flow_mutex)
934 struct cls_rule *cr = CONST_CAST(struct cls_rule *, &flow->cr);
935 struct hmap_node *node = CONST_CAST(struct hmap_node *, &flow->node);
937 classifier_remove(&dp->cls, cr);
938 hmap_remove(&dp->flow_table, node);
939 ovsrcu_postpone(dp_netdev_flow_free, flow);
943 dp_netdev_flow_flush(struct dp_netdev *dp)
945 struct dp_netdev_flow *netdev_flow, *next;
947 ovs_mutex_lock(&dp->flow_mutex);
948 fat_rwlock_wrlock(&dp->cls.rwlock);
949 HMAP_FOR_EACH_SAFE (netdev_flow, next, node, &dp->flow_table) {
950 dp_netdev_remove_flow(dp, netdev_flow);
952 fat_rwlock_unlock(&dp->cls.rwlock);
953 ovs_mutex_unlock(&dp->flow_mutex);
957 dpif_netdev_flow_flush(struct dpif *dpif)
959 struct dp_netdev *dp = get_dp_netdev(dpif);
961 dp_netdev_flow_flush(dp);
965 struct dp_netdev_port_state {
972 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
974 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
979 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
980 struct dpif_port *dpif_port)
982 struct dp_netdev_port_state *state = state_;
983 struct dp_netdev *dp = get_dp_netdev(dpif);
984 struct hmap_node *node;
987 ovs_rwlock_rdlock(&dp->port_rwlock);
988 node = hmap_at_position(&dp->ports, &state->bucket, &state->offset);
990 struct dp_netdev_port *port;
992 port = CONTAINER_OF(node, struct dp_netdev_port, node);
995 state->name = xstrdup(netdev_get_name(port->netdev));
996 dpif_port->name = state->name;
997 dpif_port->type = port->type;
998 dpif_port->port_no = port->port_no;
1004 ovs_rwlock_unlock(&dp->port_rwlock);
1010 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1012 struct dp_netdev_port_state *state = state_;
1019 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
1021 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
1022 uint64_t new_port_seq;
1025 new_port_seq = seq_read(dpif->dp->port_seq);
1026 if (dpif->last_port_seq != new_port_seq) {
1027 dpif->last_port_seq = new_port_seq;
1037 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
1039 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
1041 seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
1044 static struct dp_netdev_flow *
1045 dp_netdev_flow_cast(const struct cls_rule *cr)
1047 return cr ? CONTAINER_OF(cr, struct dp_netdev_flow, cr) : NULL;
1050 static struct dp_netdev_flow *
1051 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *flow)
1052 OVS_EXCLUDED(dp->cls.rwlock)
1054 struct dp_netdev_flow *netdev_flow;
1056 fat_rwlock_rdlock(&dp->cls.rwlock);
1057 netdev_flow = dp_netdev_flow_cast(classifier_lookup(&dp->cls, flow, NULL));
1058 fat_rwlock_unlock(&dp->cls.rwlock);
1063 static struct dp_netdev_flow *
1064 dp_netdev_find_flow(const struct dp_netdev *dp, const struct flow *flow)
1065 OVS_REQ_RDLOCK(dp->cls.rwlock)
1067 struct dp_netdev_flow *netdev_flow;
1069 HMAP_FOR_EACH_WITH_HASH (netdev_flow, node, flow_hash(flow, 0),
1071 if (flow_equal(&netdev_flow->flow, flow)) {
1080 get_dpif_flow_stats(struct dp_netdev_flow *netdev_flow,
1081 struct dpif_flow_stats *stats)
1083 struct dp_netdev_flow_stats *bucket;
1086 memset(stats, 0, sizeof *stats);
1087 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1088 ovs_mutex_lock(&bucket->mutex);
1089 stats->n_packets += bucket->packet_count;
1090 stats->n_bytes += bucket->byte_count;
1091 stats->used = MAX(stats->used, bucket->used);
1092 stats->tcp_flags |= bucket->tcp_flags;
1093 ovs_mutex_unlock(&bucket->mutex);
1098 dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1099 const struct nlattr *mask_key,
1100 uint32_t mask_key_len, const struct flow *flow,
1104 enum odp_key_fitness fitness;
1106 fitness = odp_flow_key_to_mask(mask_key, mask_key_len, mask, flow);
1108 /* This should not happen: it indicates that
1109 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
1110 * disagree on the acceptable form of a mask. Log the problem
1111 * as an error, with enough details to enable debugging. */
1112 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1114 if (!VLOG_DROP_ERR(&rl)) {
1118 odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s,
1120 VLOG_ERR("internal error parsing flow mask %s (%s)",
1121 ds_cstr(&s), odp_key_fitness_to_string(fitness));
1127 /* Force unwildcard the in_port. */
1128 mask->in_port.odp_port = u32_to_odp(UINT32_MAX);
1130 enum mf_field_id id;
1131 /* No mask key, unwildcard everything except fields whose
1132 * prerequisities are not met. */
1133 memset(mask, 0x0, sizeof *mask);
1135 for (id = 0; id < MFF_N_IDS; ++id) {
1136 /* Skip registers and metadata. */
1137 if (!(id >= MFF_REG0 && id < MFF_REG0 + FLOW_N_REGS)
1138 && id != MFF_METADATA) {
1139 const struct mf_field *mf = mf_from_id(id);
1140 if (mf_are_prereqs_ok(mf, flow)) {
1141 mf_mask_field(mf, mask);
1151 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1156 if (odp_flow_key_to_flow(key, key_len, flow)) {
1157 /* This should not happen: it indicates that odp_flow_key_from_flow()
1158 * and odp_flow_key_to_flow() disagree on the acceptable form of a
1159 * flow. Log the problem as an error, with enough details to enable
1161 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1163 if (!VLOG_DROP_ERR(&rl)) {
1167 odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
1168 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
1175 in_port = flow->in_port.odp_port;
1176 if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
1184 dpif_netdev_flow_get(const struct dpif *dpif,
1185 const struct nlattr *nl_key, size_t nl_key_len,
1186 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
1188 struct dp_netdev *dp = get_dp_netdev(dpif);
1189 struct dp_netdev_flow *netdev_flow;
1193 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
1198 fat_rwlock_rdlock(&dp->cls.rwlock);
1199 netdev_flow = dp_netdev_find_flow(dp, &key);
1200 fat_rwlock_unlock(&dp->cls.rwlock);
1204 get_dpif_flow_stats(netdev_flow, stats);
1208 struct dp_netdev_actions *actions;
1210 actions = dp_netdev_flow_get_actions(netdev_flow);
1211 *actionsp = ofpbuf_clone_data(actions->actions, actions->size);
1221 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *flow,
1222 const struct flow_wildcards *wc,
1223 const struct nlattr *actions,
1225 OVS_REQUIRES(dp->flow_mutex)
1227 struct dp_netdev_flow *netdev_flow;
1230 netdev_flow = xzalloc(sizeof *netdev_flow);
1231 *CONST_CAST(struct flow *, &netdev_flow->flow) = *flow;
1233 ovs_mutex_init(&netdev_flow->mutex);
1235 ovsthread_stats_init(&netdev_flow->stats);
1237 ovsrcu_set(&netdev_flow->actions,
1238 dp_netdev_actions_create(actions, actions_len));
1240 match_init(&match, flow, wc);
1241 cls_rule_init(CONST_CAST(struct cls_rule *, &netdev_flow->cr),
1242 &match, NETDEV_RULE_PRIORITY);
1243 fat_rwlock_wrlock(&dp->cls.rwlock);
1244 classifier_insert(&dp->cls,
1245 CONST_CAST(struct cls_rule *, &netdev_flow->cr));
1246 hmap_insert(&dp->flow_table,
1247 CONST_CAST(struct hmap_node *, &netdev_flow->node),
1248 flow_hash(flow, 0));
1249 fat_rwlock_unlock(&dp->cls.rwlock);
1255 clear_stats(struct dp_netdev_flow *netdev_flow)
1257 struct dp_netdev_flow_stats *bucket;
1260 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1261 ovs_mutex_lock(&bucket->mutex);
1263 bucket->packet_count = 0;
1264 bucket->byte_count = 0;
1265 bucket->tcp_flags = 0;
1266 ovs_mutex_unlock(&bucket->mutex);
1271 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
1273 struct dp_netdev *dp = get_dp_netdev(dpif);
1274 struct dp_netdev_flow *netdev_flow;
1276 struct flow_wildcards wc;
1279 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &flow);
1283 error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
1284 put->mask, put->mask_len,
1290 ovs_mutex_lock(&dp->flow_mutex);
1291 netdev_flow = dp_netdev_lookup_flow(dp, &flow);
1293 if (put->flags & DPIF_FP_CREATE) {
1294 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
1296 memset(put->stats, 0, sizeof *put->stats);
1298 error = dp_netdev_flow_add(dp, &flow, &wc, put->actions,
1307 if (put->flags & DPIF_FP_MODIFY
1308 && flow_equal(&flow, &netdev_flow->flow)) {
1309 struct dp_netdev_actions *new_actions;
1310 struct dp_netdev_actions *old_actions;
1312 new_actions = dp_netdev_actions_create(put->actions,
1315 old_actions = dp_netdev_flow_get_actions(netdev_flow);
1316 ovsrcu_set(&netdev_flow->actions, new_actions);
1319 get_dpif_flow_stats(netdev_flow, put->stats);
1321 if (put->flags & DPIF_FP_ZERO_STATS) {
1322 clear_stats(netdev_flow);
1325 ovsrcu_postpone(dp_netdev_actions_free, old_actions);
1326 } else if (put->flags & DPIF_FP_CREATE) {
1329 /* Overlapping flow. */
1333 ovs_mutex_unlock(&dp->flow_mutex);
1339 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
1341 struct dp_netdev *dp = get_dp_netdev(dpif);
1342 struct dp_netdev_flow *netdev_flow;
1346 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
1351 ovs_mutex_lock(&dp->flow_mutex);
1352 fat_rwlock_wrlock(&dp->cls.rwlock);
1353 netdev_flow = dp_netdev_find_flow(dp, &key);
1356 get_dpif_flow_stats(netdev_flow, del->stats);
1358 dp_netdev_remove_flow(dp, netdev_flow);
1362 fat_rwlock_unlock(&dp->cls.rwlock);
1363 ovs_mutex_unlock(&dp->flow_mutex);
1368 struct dp_netdev_flow_state {
1369 struct dp_netdev_actions *actions;
1370 struct odputil_keybuf keybuf;
1371 struct odputil_keybuf maskbuf;
1372 struct dpif_flow_stats stats;
1375 struct dp_netdev_flow_iter {
1379 struct ovs_mutex mutex;
1383 dpif_netdev_flow_dump_state_init(void **statep)
1385 struct dp_netdev_flow_state *state;
1387 *statep = state = xmalloc(sizeof *state);
1388 state->actions = NULL;
1392 dpif_netdev_flow_dump_state_uninit(void *state_)
1394 struct dp_netdev_flow_state *state = state_;
1400 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **iterp)
1402 struct dp_netdev_flow_iter *iter;
1404 *iterp = iter = xmalloc(sizeof *iter);
1408 ovs_mutex_init(&iter->mutex);
1412 /* XXX the caller must use 'actions' without quiescing */
1414 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *iter_, void *state_,
1415 const struct nlattr **key, size_t *key_len,
1416 const struct nlattr **mask, size_t *mask_len,
1417 const struct nlattr **actions, size_t *actions_len,
1418 const struct dpif_flow_stats **stats)
1420 struct dp_netdev_flow_iter *iter = iter_;
1421 struct dp_netdev_flow_state *state = state_;
1422 struct dp_netdev *dp = get_dp_netdev(dpif);
1423 struct dp_netdev_flow *netdev_flow;
1426 ovs_mutex_lock(&iter->mutex);
1427 error = iter->status;
1429 struct hmap_node *node;
1431 fat_rwlock_rdlock(&dp->cls.rwlock);
1432 node = hmap_at_position(&dp->flow_table, &iter->bucket, &iter->offset);
1434 netdev_flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
1436 fat_rwlock_unlock(&dp->cls.rwlock);
1438 iter->status = error = EOF;
1441 ovs_mutex_unlock(&iter->mutex);
1449 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
1450 odp_flow_key_from_flow(&buf, &netdev_flow->flow,
1451 netdev_flow->flow.in_port.odp_port);
1454 *key_len = buf.size;
1459 struct flow_wildcards wc;
1461 ofpbuf_use_stack(&buf, &state->maskbuf, sizeof state->maskbuf);
1462 minimask_expand(&netdev_flow->cr.match.mask, &wc);
1463 odp_flow_key_from_mask(&buf, &wc.masks, &netdev_flow->flow,
1464 odp_to_u32(wc.masks.in_port.odp_port),
1468 *mask_len = buf.size;
1471 if (actions || stats) {
1472 state->actions = NULL;
1475 state->actions = dp_netdev_flow_get_actions(netdev_flow);
1476 *actions = state->actions->actions;
1477 *actions_len = state->actions->size;
1481 get_dpif_flow_stats(netdev_flow, &state->stats);
1482 *stats = &state->stats;
1490 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *iter_)
1492 struct dp_netdev_flow_iter *iter = iter_;
1494 ovs_mutex_destroy(&iter->mutex);
1500 dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
1502 struct dp_netdev *dp = get_dp_netdev(dpif);
1503 struct pkt_metadata *md = &execute->md;
1506 if (execute->packet->size < ETH_HEADER_LEN ||
1507 execute->packet->size > UINT16_MAX) {
1511 /* Extract flow key. */
1512 flow_extract(execute->packet, md, &key);
1514 ovs_rwlock_rdlock(&dp->port_rwlock);
1515 dp_netdev_execute_actions(dp, &key, execute->packet, false, md,
1516 execute->actions, execute->actions_len);
1517 ovs_rwlock_unlock(&dp->port_rwlock);
1523 dp_netdev_destroy_all_queues(struct dp_netdev *dp)
1524 OVS_REQ_WRLOCK(dp->queue_rwlock)
1528 dp_netdev_purge_queues(dp);
1530 for (i = 0; i < dp->n_handlers; i++) {
1531 struct dp_netdev_queue *q = &dp->handler_queues[i];
1533 ovs_mutex_destroy(&q->mutex);
1534 seq_destroy(q->seq);
1536 free(dp->handler_queues);
1537 dp->handler_queues = NULL;
1542 dp_netdev_refresh_queues(struct dp_netdev *dp, uint32_t n_handlers)
1543 OVS_REQ_WRLOCK(dp->queue_rwlock)
1545 if (dp->n_handlers != n_handlers) {
1548 dp_netdev_destroy_all_queues(dp);
1550 dp->n_handlers = n_handlers;
1551 dp->handler_queues = xzalloc(n_handlers * sizeof *dp->handler_queues);
1553 for (i = 0; i < n_handlers; i++) {
1554 struct dp_netdev_queue *q = &dp->handler_queues[i];
1556 ovs_mutex_init(&q->mutex);
1557 q->seq = seq_create();
1563 dpif_netdev_recv_set(struct dpif *dpif, bool enable)
1565 struct dp_netdev *dp = get_dp_netdev(dpif);
1567 if ((dp->handler_queues != NULL) == enable) {
1571 fat_rwlock_wrlock(&dp->queue_rwlock);
1573 dp_netdev_destroy_all_queues(dp);
1575 dp_netdev_refresh_queues(dp, 1);
1577 fat_rwlock_unlock(&dp->queue_rwlock);
1583 dpif_netdev_handlers_set(struct dpif *dpif, uint32_t n_handlers)
1585 struct dp_netdev *dp = get_dp_netdev(dpif);
1587 fat_rwlock_wrlock(&dp->queue_rwlock);
1588 if (dp->handler_queues) {
1589 dp_netdev_refresh_queues(dp, n_handlers);
1591 fat_rwlock_unlock(&dp->queue_rwlock);
1597 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1598 uint32_t queue_id, uint32_t *priority)
1600 *priority = queue_id;
1605 dp_netdev_recv_check(const struct dp_netdev *dp, const uint32_t handler_id)
1606 OVS_REQ_RDLOCK(dp->queue_rwlock)
1608 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1610 if (!dp->handler_queues) {
1611 VLOG_WARN_RL(&rl, "receiving upcall disabled");
1615 if (handler_id >= dp->n_handlers) {
1616 VLOG_WARN_RL(&rl, "handler index out of bound");
1624 dpif_netdev_recv(struct dpif *dpif, uint32_t handler_id,
1625 struct dpif_upcall *upcall, struct ofpbuf *buf)
1627 struct dp_netdev *dp = get_dp_netdev(dpif);
1628 struct dp_netdev_queue *q;
1631 fat_rwlock_rdlock(&dp->queue_rwlock);
1633 if (!dp_netdev_recv_check(dp, handler_id)) {
1638 q = &dp->handler_queues[handler_id];
1639 ovs_mutex_lock(&q->mutex);
1640 if (q->head != q->tail) {
1641 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1643 *upcall = u->upcall;
1650 ovs_mutex_unlock(&q->mutex);
1653 fat_rwlock_unlock(&dp->queue_rwlock);
1659 dpif_netdev_recv_wait(struct dpif *dpif, uint32_t handler_id)
1661 struct dp_netdev *dp = get_dp_netdev(dpif);
1662 struct dp_netdev_queue *q;
1665 fat_rwlock_rdlock(&dp->queue_rwlock);
1667 if (!dp_netdev_recv_check(dp, handler_id)) {
1671 q = &dp->handler_queues[handler_id];
1672 ovs_mutex_lock(&q->mutex);
1673 seq = seq_read(q->seq);
1674 if (q->head != q->tail) {
1675 poll_immediate_wake();
1677 seq_wait(q->seq, seq);
1680 ovs_mutex_unlock(&q->mutex);
1683 fat_rwlock_unlock(&dp->queue_rwlock);
1687 dpif_netdev_recv_purge(struct dpif *dpif)
1689 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1691 fat_rwlock_wrlock(&dpif_netdev->dp->queue_rwlock);
1692 dp_netdev_purge_queues(dpif_netdev->dp);
1693 fat_rwlock_unlock(&dpif_netdev->dp->queue_rwlock);
1696 /* Creates and returns a new 'struct dp_netdev_actions', with a reference count
1697 * of 1, whose actions are a copy of from the 'ofpacts_len' bytes of
1699 struct dp_netdev_actions *
1700 dp_netdev_actions_create(const struct nlattr *actions, size_t size)
1702 struct dp_netdev_actions *netdev_actions;
1704 netdev_actions = xmalloc(sizeof *netdev_actions);
1705 netdev_actions->actions = xmemdup(actions, size);
1706 netdev_actions->size = size;
1708 return netdev_actions;
1711 struct dp_netdev_actions *
1712 dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow)
1714 return ovsrcu_get(struct dp_netdev_actions *, &flow->actions);
1718 dp_netdev_actions_free(struct dp_netdev_actions *actions)
1720 free(actions->actions);
1726 dp_netdev_process_rx_port(struct dp_netdev *dp,
1727 struct dp_netdev_port *port,
1728 struct netdev_rx *queue)
1730 struct ofpbuf *packet[NETDEV_MAX_RX_BATCH];
1733 error = netdev_rx_recv(queue, packet, &c);
1735 struct pkt_metadata md = PKT_METADATA_INITIALIZER(port->port_no);
1738 for (i = 0; i < c; i++) {
1739 dp_netdev_port_input(dp, packet[i], &md);
1741 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1742 static struct vlog_rate_limit rl
1743 = VLOG_RATE_LIMIT_INIT(1, 5);
1745 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1746 netdev_get_name(port->netdev),
1747 ovs_strerror(error));
1752 dpif_netdev_run(struct dpif *dpif)
1754 struct dp_netdev_port *port;
1755 struct dp_netdev *dp = get_dp_netdev(dpif);
1757 ovs_rwlock_rdlock(&dp->port_rwlock);
1759 HMAP_FOR_EACH (port, node, &dp->ports) {
1760 if (port->rx && !netdev_is_pmd(port->netdev)) {
1761 dp_netdev_process_rx_port(dp, port, port->rx);
1765 ovs_rwlock_unlock(&dp->port_rwlock);
1769 dpif_netdev_wait(struct dpif *dpif)
1771 struct dp_netdev_port *port;
1772 struct dp_netdev *dp = get_dp_netdev(dpif);
1774 ovs_rwlock_rdlock(&dp->port_rwlock);
1776 HMAP_FOR_EACH (port, node, &dp->ports) {
1777 if (port->rx && !netdev_is_pmd(port->netdev)) {
1778 netdev_rx_wait(port->rx);
1781 ovs_rwlock_unlock(&dp->port_rwlock);
1785 struct dp_netdev_port *port;
1789 pmd_load_queues(struct pmd_thread *f,
1790 struct rx_poll **ppoll_list, int poll_cnt)
1792 struct dp_netdev *dp = f->dp;
1793 struct rx_poll *poll_list = *ppoll_list;
1794 struct dp_netdev_port *port;
1799 /* Simple scheduler for netdev rx polling. */
1800 ovs_rwlock_rdlock(&dp->port_rwlock);
1801 for (i = 0; i < poll_cnt; i++) {
1802 port_unref(poll_list[i].port);
1808 HMAP_FOR_EACH (port, node, &f->dp->ports) {
1809 if (netdev_is_pmd(port->netdev)) {
1810 if ((index % dp->n_pmd_threads) == id) {
1811 poll_list = xrealloc(poll_list, sizeof *poll_list * (poll_cnt + 1));
1814 poll_list[poll_cnt++].port = port;
1820 ovs_rwlock_unlock(&dp->port_rwlock);
1821 *ppoll_list = poll_list;
1826 pmd_thread_main(void *f_)
1828 struct pmd_thread *f = f_;
1829 struct dp_netdev *dp = f->dp;
1830 unsigned int lc = 0;
1831 struct rx_poll *poll_list;
1832 unsigned int port_seq;
1836 f->name = xasprintf("pmd_%u", ovsthread_id_self());
1837 set_subprogram_name("%s", f->name);
1842 poll_cnt = pmd_load_queues(f, &poll_list, poll_cnt);
1843 atomic_read(&f->change_seq, &port_seq);
1846 unsigned int c_port_seq;
1849 for (i = 0; i < poll_cnt; i++) {
1850 dp_netdev_process_rx_port(dp, poll_list[i].port, poll_list[i].port->rx);
1856 /* TODO: need completely userspace based signaling method.
1857 * to keep this thread entirely in userspace.
1858 * For now using atomic counter. */
1860 atomic_read_explicit(&f->change_seq, &c_port_seq, memory_order_consume);
1861 if (c_port_seq != port_seq) {
1867 if (!latch_is_set(&f->dp->exit_latch)){
1871 for (i = 0; i < poll_cnt; i++) {
1872 port_unref(poll_list[i].port);
1881 dp_netdev_set_pmd_threads(struct dp_netdev *dp, int n)
1885 if (n == dp->n_pmd_threads) {
1889 /* Stop existing threads. */
1890 latch_set(&dp->exit_latch);
1891 dp_netdev_reload_pmd_threads(dp);
1892 for (i = 0; i < dp->n_pmd_threads; i++) {
1893 struct pmd_thread *f = &dp->pmd_threads[i];
1895 xpthread_join(f->thread, NULL);
1897 latch_poll(&dp->exit_latch);
1898 free(dp->pmd_threads);
1900 /* Start new threads. */
1901 dp->pmd_threads = xmalloc(n * sizeof *dp->pmd_threads);
1902 dp->n_pmd_threads = n;
1904 for (i = 0; i < n; i++) {
1905 struct pmd_thread *f = &dp->pmd_threads[i];
1909 atomic_store(&f->change_seq, 1);
1911 /* Each thread will distribute all devices rx-queues among
1913 xpthread_create(&f->thread, NULL, pmd_thread_main, f);
1919 dp_netdev_flow_stats_new_cb(void)
1921 struct dp_netdev_flow_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1922 ovs_mutex_init(&bucket->mutex);
1927 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow,
1928 const struct ofpbuf *packet,
1929 const struct flow *key)
1931 uint16_t tcp_flags = ntohs(key->tcp_flags);
1932 long long int now = time_msec();
1933 struct dp_netdev_flow_stats *bucket;
1935 bucket = ovsthread_stats_bucket_get(&netdev_flow->stats,
1936 dp_netdev_flow_stats_new_cb);
1938 ovs_mutex_lock(&bucket->mutex);
1939 bucket->used = MAX(now, bucket->used);
1940 bucket->packet_count++;
1941 bucket->byte_count += packet->size;
1942 bucket->tcp_flags |= tcp_flags;
1943 ovs_mutex_unlock(&bucket->mutex);
1947 dp_netdev_stats_new_cb(void)
1949 struct dp_netdev_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1950 ovs_mutex_init(&bucket->mutex);
1955 dp_netdev_count_packet(struct dp_netdev *dp, enum dp_stat_type type)
1957 struct dp_netdev_stats *bucket;
1959 bucket = ovsthread_stats_bucket_get(&dp->stats, dp_netdev_stats_new_cb);
1960 ovs_mutex_lock(&bucket->mutex);
1962 ovs_mutex_unlock(&bucket->mutex);
1966 dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
1967 struct pkt_metadata *md)
1969 struct dp_netdev_flow *netdev_flow;
1972 if (packet->size < ETH_HEADER_LEN) {
1973 ofpbuf_delete(packet);
1976 flow_extract(packet, md, &key);
1977 netdev_flow = dp_netdev_lookup_flow(dp, &key);
1979 struct dp_netdev_actions *actions;
1981 dp_netdev_flow_used(netdev_flow, packet, &key);
1983 actions = dp_netdev_flow_get_actions(netdev_flow);
1984 dp_netdev_execute_actions(dp, &key, packet, true, md,
1985 actions->actions, actions->size);
1986 dp_netdev_count_packet(dp, DP_STAT_HIT);
1987 } else if (dp->handler_queues) {
1988 dp_netdev_count_packet(dp, DP_STAT_MISS);
1989 dp_netdev_output_userspace(dp, packet,
1990 flow_hash_5tuple(&key, 0) % dp->n_handlers,
1991 DPIF_UC_MISS, &key, NULL);
1992 ofpbuf_delete(packet);
1997 dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *packet,
1998 int queue_no, int type, const struct flow *flow,
1999 const struct nlattr *userdata)
2001 struct dp_netdev_queue *q;
2004 fat_rwlock_rdlock(&dp->queue_rwlock);
2005 q = &dp->handler_queues[queue_no];
2006 ovs_mutex_lock(&q->mutex);
2007 if (q->head - q->tail < MAX_QUEUE_LEN) {
2008 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
2009 struct dpif_upcall *upcall = &u->upcall;
2010 struct ofpbuf *buf = &u->buf;
2013 upcall->type = type;
2015 /* Allocate buffer big enough for everything. */
2016 buf_size = ODPUTIL_FLOW_KEY_BYTES;
2018 buf_size += NLA_ALIGN(userdata->nla_len);
2020 buf_size += packet->size;
2021 ofpbuf_init(buf, buf_size);
2024 odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
2025 upcall->key = buf->data;
2026 upcall->key_len = buf->size;
2030 upcall->userdata = ofpbuf_put(buf, userdata,
2031 NLA_ALIGN(userdata->nla_len));
2034 upcall->packet.data = ofpbuf_put(buf, packet->data, packet->size);
2035 upcall->packet.size = packet->size;
2041 dp_netdev_count_packet(dp, DP_STAT_LOST);
2044 ovs_mutex_unlock(&q->mutex);
2045 fat_rwlock_unlock(&dp->queue_rwlock);
2050 struct dp_netdev_execute_aux {
2051 struct dp_netdev *dp;
2052 const struct flow *key;
2056 dp_execute_cb(void *aux_, struct ofpbuf *packet,
2057 const struct pkt_metadata *md OVS_UNUSED,
2058 const struct nlattr *a, bool may_steal)
2059 OVS_NO_THREAD_SAFETY_ANALYSIS
2061 struct dp_netdev_execute_aux *aux = aux_;
2062 int type = nl_attr_type(a);
2063 struct dp_netdev_port *p;
2065 switch ((enum ovs_action_attr)type) {
2066 case OVS_ACTION_ATTR_OUTPUT:
2067 p = dp_netdev_lookup_port(aux->dp, u32_to_odp(nl_attr_get_u32(a)));
2069 netdev_send(p->netdev, packet, may_steal);
2073 case OVS_ACTION_ATTR_USERSPACE: {
2074 const struct nlattr *userdata;
2076 userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
2078 dp_netdev_output_userspace(aux->dp, packet,
2079 flow_hash_5tuple(aux->key, 0)
2080 % aux->dp->n_handlers,
2081 DPIF_UC_ACTION, aux->key,
2085 ofpbuf_delete(packet);
2089 case OVS_ACTION_ATTR_PUSH_VLAN:
2090 case OVS_ACTION_ATTR_POP_VLAN:
2091 case OVS_ACTION_ATTR_PUSH_MPLS:
2092 case OVS_ACTION_ATTR_POP_MPLS:
2093 case OVS_ACTION_ATTR_SET:
2094 case OVS_ACTION_ATTR_SAMPLE:
2095 case OVS_ACTION_ATTR_UNSPEC:
2096 case __OVS_ACTION_ATTR_MAX:
2103 dp_netdev_execute_actions(struct dp_netdev *dp, const struct flow *key,
2104 struct ofpbuf *packet, bool may_steal,
2105 struct pkt_metadata *md,
2106 const struct nlattr *actions, size_t actions_len)
2108 struct dp_netdev_execute_aux aux = {dp, key};
2110 odp_execute_actions(&aux, packet, may_steal, md,
2111 actions, actions_len, dp_execute_cb);
2114 const struct dpif_class dpif_netdev_class = {
2116 dpif_netdev_enumerate,
2117 dpif_netdev_port_open_type,
2120 dpif_netdev_destroy,
2123 dpif_netdev_get_stats,
2124 dpif_netdev_port_add,
2125 dpif_netdev_port_del,
2126 dpif_netdev_port_query_by_number,
2127 dpif_netdev_port_query_by_name,
2128 NULL, /* port_get_pid */
2129 dpif_netdev_port_dump_start,
2130 dpif_netdev_port_dump_next,
2131 dpif_netdev_port_dump_done,
2132 dpif_netdev_port_poll,
2133 dpif_netdev_port_poll_wait,
2134 dpif_netdev_flow_get,
2135 dpif_netdev_flow_put,
2136 dpif_netdev_flow_del,
2137 dpif_netdev_flow_flush,
2138 dpif_netdev_flow_dump_state_init,
2139 dpif_netdev_flow_dump_start,
2140 dpif_netdev_flow_dump_next,
2142 dpif_netdev_flow_dump_done,
2143 dpif_netdev_flow_dump_state_uninit,
2144 dpif_netdev_execute,
2146 dpif_netdev_recv_set,
2147 dpif_netdev_handlers_set,
2148 dpif_netdev_queue_to_priority,
2150 dpif_netdev_recv_wait,
2151 dpif_netdev_recv_purge,
2155 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
2156 const char *argv[], void *aux OVS_UNUSED)
2158 struct dp_netdev_port *port;
2159 struct dp_netdev *dp;
2162 ovs_mutex_lock(&dp_netdev_mutex);
2163 dp = shash_find_data(&dp_netdevs, argv[1]);
2164 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
2165 ovs_mutex_unlock(&dp_netdev_mutex);
2166 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
2169 ovs_refcount_ref(&dp->ref_cnt);
2170 ovs_mutex_unlock(&dp_netdev_mutex);
2172 ovs_rwlock_wrlock(&dp->port_rwlock);
2173 if (get_port_by_name(dp, argv[2], &port)) {
2174 unixctl_command_reply_error(conn, "unknown port");
2178 port_no = u32_to_odp(atoi(argv[3]));
2179 if (!port_no || port_no == ODPP_NONE) {
2180 unixctl_command_reply_error(conn, "bad port number");
2183 if (dp_netdev_lookup_port(dp, port_no)) {
2184 unixctl_command_reply_error(conn, "port number already in use");
2187 hmap_remove(&dp->ports, &port->node);
2188 port->port_no = port_no;
2189 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
2190 seq_change(dp->port_seq);
2191 unixctl_command_reply(conn, NULL);
2194 ovs_rwlock_unlock(&dp->port_rwlock);
2195 dp_netdev_unref(dp);
2199 dpif_dummy_register__(const char *type)
2201 struct dpif_class *class;
2203 class = xmalloc(sizeof *class);
2204 *class = dpif_netdev_class;
2205 class->type = xstrdup(type);
2206 dp_register_provider(class);
2210 dpif_dummy_register(bool override)
2217 dp_enumerate_types(&types);
2218 SSET_FOR_EACH (type, &types) {
2219 if (!dp_unregister_provider(type)) {
2220 dpif_dummy_register__(type);
2223 sset_destroy(&types);
2226 dpif_dummy_register__("dummy");
2228 unixctl_command_register("dpif-dummy/change-port-number",
2229 "DP PORT NEW-NUMBER",
2230 3, 3, dpif_dummy_change_port_number, NULL);