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-dpdk.h"
47 #include "netdev-vport.h"
49 #include "odp-execute.h"
51 #include "ofp-print.h"
55 #include "poll-loop.h"
65 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
67 /* By default, choose a priority in the middle. */
68 #define NETDEV_RULE_PRIORITY 0x8000
71 /* Use per thread recirc_depth to prevent recirculation loop. */
72 #define MAX_RECIRC_DEPTH 5
73 DEFINE_STATIC_PER_THREAD_DATA(uint32_t, recirc_depth, 0)
75 /* Configuration parameters. */
76 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
79 enum { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
80 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
81 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
83 /* Protects against changes to 'dp_netdevs'. */
84 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
86 /* Contains all 'struct dp_netdev's. */
87 static struct shash dp_netdevs OVS_GUARDED_BY(dp_netdev_mutex)
88 = SHASH_INITIALIZER(&dp_netdevs);
90 struct dp_netdev_upcall {
91 struct dpif_upcall upcall; /* Queued upcall information. */
92 struct ofpbuf buf; /* ofpbuf instance for upcall.packet. */
95 /* A queue passing packets from a struct dp_netdev to its clients (handlers).
101 * Any access at all requires the owning 'dp_netdev''s queue_rwlock and
103 struct dp_netdev_queue {
104 struct ovs_mutex mutex;
105 struct seq *seq; /* Incremented whenever a packet is queued. */
106 struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN] OVS_GUARDED;
107 unsigned int head OVS_GUARDED;
108 unsigned int tail OVS_GUARDED;
111 /* Datapath based on the network device interface from netdev.h.
117 * Some members, marked 'const', are immutable. Accessing other members
118 * requires synchronization, as noted in more detail below.
120 * Acquisition order is, from outermost to innermost:
122 * dp_netdev_mutex (global)
129 const struct dpif_class *const class;
130 const char *const name;
131 struct ovs_refcount ref_cnt;
132 atomic_flag destroyed;
136 * Readers of 'cls' and 'flow_table' must take a 'cls->rwlock' read lock.
138 * Writers of 'cls' and 'flow_table' must take the 'flow_mutex' and then
139 * the 'cls->rwlock' write lock. (The outer 'flow_mutex' allows writers to
140 * atomically perform multiple operations on 'cls' and 'flow_table'.)
142 struct ovs_mutex flow_mutex;
143 struct classifier cls; /* Classifier. Protected by cls.rwlock. */
144 struct hmap flow_table OVS_GUARDED; /* Flow table. */
148 * 'queue_rwlock' protects the modification of 'handler_queues' and
149 * 'n_handlers'. The queue elements are protected by its
150 * 'handler_queues''s mutex. */
151 struct fat_rwlock queue_rwlock;
152 struct dp_netdev_queue *handler_queues;
157 * ovsthread_stats is internally synchronized. */
158 struct ovsthread_stats stats; /* Contains 'struct dp_netdev_stats *'. */
162 * Any lookup into 'ports' or any access to the dp_netdev_ports found
163 * through 'ports' requires taking 'port_rwlock'. */
164 struct ovs_rwlock port_rwlock;
165 struct hmap ports OVS_GUARDED;
166 struct seq *port_seq; /* Incremented whenever a port changes. */
168 /* Forwarding threads. */
169 struct latch exit_latch;
170 struct pmd_thread *pmd_threads;
171 size_t n_pmd_threads;
175 static struct dp_netdev_port *dp_netdev_lookup_port(const struct dp_netdev *dp,
177 OVS_REQ_RDLOCK(dp->port_rwlock);
180 DP_STAT_HIT, /* Packets that matched in the flow table. */
181 DP_STAT_MISS, /* Packets that did not match. */
182 DP_STAT_LOST, /* Packets not passed up to the client. */
186 /* Contained by struct dp_netdev's 'stats' member. */
187 struct dp_netdev_stats {
188 struct ovs_mutex mutex; /* Protects 'n'. */
190 /* Indexed by DP_STAT_*, protected by 'mutex'. */
191 unsigned long long int n[DP_N_STATS] OVS_GUARDED;
195 /* A port in a netdev-based datapath. */
196 struct dp_netdev_port {
197 struct hmap_node node; /* Node in dp_netdev's 'ports'. */
199 struct netdev *netdev;
200 struct netdev_saved_flags *sf;
201 struct netdev_rxq **rxq;
202 struct ovs_refcount ref_cnt;
203 char *type; /* Port type as requested by user. */
206 /* A flow in dp_netdev's 'flow_table'.
212 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
213 * its dp_netdev's classifier. The text below calls this classifier 'cls'.
218 * The thread safety rules described here for "struct dp_netdev_flow" are
219 * motivated by two goals:
221 * - Prevent threads that read members of "struct dp_netdev_flow" from
222 * reading bad data due to changes by some thread concurrently modifying
225 * - Prevent two threads making changes to members of a given "struct
226 * dp_netdev_flow" from interfering with each other.
232 * A flow 'flow' may be accessed without a risk of being freed by code that
233 * holds a read-lock or write-lock on 'cls->rwlock' or that owns a reference to
234 * 'flow->ref_cnt' (or both). Code that needs to hold onto a flow for a while
235 * should take 'cls->rwlock', find the flow it needs, increment 'flow->ref_cnt'
236 * with dpif_netdev_flow_ref(), and drop 'cls->rwlock'.
238 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
239 * flow from being deleted from 'cls' (that's 'cls->rwlock') and it doesn't
240 * protect members of 'flow' from modification (that's 'flow->mutex').
242 * 'flow->mutex' protects the members of 'flow' from modification. It doesn't
243 * protect the flow from being deleted from 'cls' (that's 'cls->rwlock') and it
244 * doesn't prevent the flow from being freed (that's 'flow->ref_cnt').
246 * Some members, marked 'const', are immutable. Accessing other members
247 * requires synchronization, as noted in more detail below.
249 struct dp_netdev_flow {
250 /* Packet classification. */
251 const struct cls_rule cr; /* In owning dp_netdev's 'cls'. */
253 /* Hash table index by unmasked flow. */
254 const struct hmap_node node; /* In owning dp_netdev's 'flow_table'. */
255 const struct flow flow; /* The flow that created this entry. */
257 /* Protects members marked OVS_GUARDED.
259 * Acquire after datapath's flow_mutex. */
260 struct ovs_mutex mutex OVS_ACQ_AFTER(dp_netdev_mutex);
264 * Reading or writing these members requires 'mutex'. */
265 struct ovsthread_stats stats; /* Contains "struct dp_netdev_flow_stats". */
269 * Reading 'actions' requires 'mutex'.
270 * Writing 'actions' requires 'mutex' and (to allow for transactions) the
271 * datapath's flow_mutex. */
272 OVSRCU_TYPE(struct dp_netdev_actions *) actions;
275 static void dp_netdev_flow_free(struct dp_netdev_flow *);
277 /* Contained by struct dp_netdev_flow's 'stats' member. */
278 struct dp_netdev_flow_stats {
279 struct ovs_mutex mutex; /* Guards all the other members. */
281 long long int used OVS_GUARDED; /* Last used time, in monotonic msecs. */
282 long long int packet_count OVS_GUARDED; /* Number of packets matched. */
283 long long int byte_count OVS_GUARDED; /* Number of bytes matched. */
284 uint16_t tcp_flags OVS_GUARDED; /* Bitwise-OR of seen tcp_flags values. */
287 /* A set of datapath actions within a "struct dp_netdev_flow".
293 * A struct dp_netdev_actions 'actions' may be accessed without a risk of being
294 * freed by code that holds a read-lock or write-lock on 'flow->mutex' (where
295 * 'flow' is the dp_netdev_flow for which 'flow->actions == actions') or that
296 * owns a reference to 'actions->ref_cnt' (or both). */
297 struct dp_netdev_actions {
298 /* These members are immutable: they do not change during the struct's
300 struct nlattr *actions; /* Sequence of OVS_ACTION_ATTR_* attributes. */
301 unsigned int size; /* Size of 'actions', in bytes. */
304 struct dp_netdev_actions *dp_netdev_actions_create(const struct nlattr *,
306 struct dp_netdev_actions *dp_netdev_flow_get_actions(
307 const struct dp_netdev_flow *);
308 static void dp_netdev_actions_free(struct dp_netdev_actions *);
310 /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
311 * the performance overhead of interrupt processing. Therefore netdev can
312 * not implement rx-wait for these devices. dpif-netdev needs to poll
313 * these device to check for recv buffer. pmd-thread does polling for
314 * devices assigned to itself thread.
316 * DPDK used PMD for accessing NIC.
318 * A thread that receives packets from PMD ports, looks them up in the flow
319 * table, and executes the actions it finds.
322 struct dp_netdev *dp;
325 atomic_uint change_seq;
329 /* Interface to netdev-based datapath. */
332 struct dp_netdev *dp;
333 uint64_t last_port_seq;
336 static int get_port_by_number(struct dp_netdev *dp, odp_port_t port_no,
337 struct dp_netdev_port **portp)
338 OVS_REQ_RDLOCK(dp->port_rwlock);
339 static int get_port_by_name(struct dp_netdev *dp, const char *devname,
340 struct dp_netdev_port **portp)
341 OVS_REQ_RDLOCK(dp->port_rwlock);
342 static void dp_netdev_free(struct dp_netdev *)
343 OVS_REQUIRES(dp_netdev_mutex);
344 static void dp_netdev_flow_flush(struct dp_netdev *);
345 static int do_add_port(struct dp_netdev *dp, const char *devname,
346 const char *type, odp_port_t port_no)
347 OVS_REQ_WRLOCK(dp->port_rwlock);
348 static int do_del_port(struct dp_netdev *dp, odp_port_t port_no)
349 OVS_REQ_WRLOCK(dp->port_rwlock);
350 static void dp_netdev_destroy_all_queues(struct dp_netdev *dp)
351 OVS_REQ_WRLOCK(dp->queue_rwlock);
352 static int dpif_netdev_open(const struct dpif_class *, const char *name,
353 bool create, struct dpif **);
354 static int dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *,
355 int queue_no, int type,
357 const struct nlattr *userdata);
358 static void dp_netdev_execute_actions(struct dp_netdev *dp,
359 const struct flow *, struct ofpbuf *, bool may_steal,
360 struct pkt_metadata *,
361 const struct nlattr *actions,
363 static void dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
364 struct pkt_metadata *);
366 static void dp_netdev_set_pmd_threads(struct dp_netdev *, int n);
368 static struct dpif_netdev *
369 dpif_netdev_cast(const struct dpif *dpif)
371 ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
372 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
375 static struct dp_netdev *
376 get_dp_netdev(const struct dpif *dpif)
378 return dpif_netdev_cast(dpif)->dp;
382 dpif_netdev_enumerate(struct sset *all_dps)
384 struct shash_node *node;
386 ovs_mutex_lock(&dp_netdev_mutex);
387 SHASH_FOR_EACH(node, &dp_netdevs) {
388 sset_add(all_dps, node->name);
390 ovs_mutex_unlock(&dp_netdev_mutex);
396 dpif_netdev_class_is_dummy(const struct dpif_class *class)
398 return class != &dpif_netdev_class;
402 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
404 return strcmp(type, "internal") ? type
405 : dpif_netdev_class_is_dummy(class) ? "dummy"
410 create_dpif_netdev(struct dp_netdev *dp)
412 uint16_t netflow_id = hash_string(dp->name, 0);
413 struct dpif_netdev *dpif;
415 ovs_refcount_ref(&dp->ref_cnt);
417 dpif = xmalloc(sizeof *dpif);
418 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
420 dpif->last_port_seq = seq_read(dp->port_seq);
425 /* Choose an unused, non-zero port number and return it on success.
426 * Return ODPP_NONE on failure. */
428 choose_port(struct dp_netdev *dp, const char *name)
429 OVS_REQ_RDLOCK(dp->port_rwlock)
433 if (dp->class != &dpif_netdev_class) {
437 /* If the port name begins with "br", start the number search at
438 * 100 to make writing tests easier. */
439 if (!strncmp(name, "br", 2)) {
443 /* If the port name contains a number, try to assign that port number.
444 * This can make writing unit tests easier because port numbers are
446 for (p = name; *p != '\0'; p++) {
447 if (isdigit((unsigned char) *p)) {
448 port_no = start_no + strtol(p, NULL, 10);
449 if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE)
450 && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
451 return u32_to_odp(port_no);
458 for (port_no = 1; port_no <= UINT16_MAX; port_no++) {
459 if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
460 return u32_to_odp(port_no);
468 create_dp_netdev(const char *name, const struct dpif_class *class,
469 struct dp_netdev **dpp)
470 OVS_REQUIRES(dp_netdev_mutex)
472 struct dp_netdev *dp;
475 dp = xzalloc(sizeof *dp);
476 shash_add(&dp_netdevs, name, dp);
478 *CONST_CAST(const struct dpif_class **, &dp->class) = class;
479 *CONST_CAST(const char **, &dp->name) = xstrdup(name);
480 ovs_refcount_init(&dp->ref_cnt);
481 atomic_flag_clear(&dp->destroyed);
483 ovs_mutex_init(&dp->flow_mutex);
484 classifier_init(&dp->cls, NULL);
485 hmap_init(&dp->flow_table);
487 fat_rwlock_init(&dp->queue_rwlock);
489 ovsthread_stats_init(&dp->stats);
491 ovs_rwlock_init(&dp->port_rwlock);
492 hmap_init(&dp->ports);
493 dp->port_seq = seq_create();
494 latch_init(&dp->exit_latch);
496 ovs_rwlock_wrlock(&dp->port_rwlock);
497 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
498 ovs_rwlock_unlock(&dp->port_rwlock);
509 dpif_netdev_open(const struct dpif_class *class, const char *name,
510 bool create, struct dpif **dpifp)
512 struct dp_netdev *dp;
515 ovs_mutex_lock(&dp_netdev_mutex);
516 dp = shash_find_data(&dp_netdevs, name);
518 error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
520 error = (dp->class != class ? EINVAL
525 *dpifp = create_dpif_netdev(dp);
527 ovs_mutex_unlock(&dp_netdev_mutex);
533 dp_netdev_purge_queues(struct dp_netdev *dp)
534 OVS_REQ_WRLOCK(dp->queue_rwlock)
538 for (i = 0; i < dp->n_handlers; i++) {
539 struct dp_netdev_queue *q = &dp->handler_queues[i];
541 ovs_mutex_lock(&q->mutex);
542 while (q->tail != q->head) {
543 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
544 ofpbuf_uninit(&u->upcall.packet);
545 ofpbuf_uninit(&u->buf);
547 ovs_mutex_unlock(&q->mutex);
551 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
552 * through the 'dp_netdevs' shash while freeing 'dp'. */
554 dp_netdev_free(struct dp_netdev *dp)
555 OVS_REQUIRES(dp_netdev_mutex)
557 struct dp_netdev_port *port, *next;
558 struct dp_netdev_stats *bucket;
561 shash_find_and_delete(&dp_netdevs, dp->name);
563 dp_netdev_set_pmd_threads(dp, 0);
564 free(dp->pmd_threads);
566 dp_netdev_flow_flush(dp);
567 ovs_rwlock_wrlock(&dp->port_rwlock);
568 HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) {
569 do_del_port(dp, port->port_no);
571 ovs_rwlock_unlock(&dp->port_rwlock);
573 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
574 ovs_mutex_destroy(&bucket->mutex);
575 free_cacheline(bucket);
577 ovsthread_stats_destroy(&dp->stats);
579 fat_rwlock_wrlock(&dp->queue_rwlock);
580 dp_netdev_destroy_all_queues(dp);
581 fat_rwlock_unlock(&dp->queue_rwlock);
583 fat_rwlock_destroy(&dp->queue_rwlock);
585 classifier_destroy(&dp->cls);
586 hmap_destroy(&dp->flow_table);
587 ovs_mutex_destroy(&dp->flow_mutex);
588 seq_destroy(dp->port_seq);
589 hmap_destroy(&dp->ports);
590 latch_destroy(&dp->exit_latch);
591 free(CONST_CAST(char *, dp->name));
596 dp_netdev_unref(struct dp_netdev *dp)
599 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
600 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
601 ovs_mutex_lock(&dp_netdev_mutex);
602 if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
605 ovs_mutex_unlock(&dp_netdev_mutex);
610 dpif_netdev_close(struct dpif *dpif)
612 struct dp_netdev *dp = get_dp_netdev(dpif);
619 dpif_netdev_destroy(struct dpif *dpif)
621 struct dp_netdev *dp = get_dp_netdev(dpif);
623 if (!atomic_flag_test_and_set(&dp->destroyed)) {
624 if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
625 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
634 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
636 struct dp_netdev *dp = get_dp_netdev(dpif);
637 struct dp_netdev_stats *bucket;
640 fat_rwlock_rdlock(&dp->cls.rwlock);
641 stats->n_flows = hmap_count(&dp->flow_table);
642 fat_rwlock_unlock(&dp->cls.rwlock);
644 stats->n_hit = stats->n_missed = stats->n_lost = 0;
645 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
646 ovs_mutex_lock(&bucket->mutex);
647 stats->n_hit += bucket->n[DP_STAT_HIT];
648 stats->n_missed += bucket->n[DP_STAT_MISS];
649 stats->n_lost += bucket->n[DP_STAT_LOST];
650 ovs_mutex_unlock(&bucket->mutex);
652 stats->n_masks = UINT32_MAX;
653 stats->n_mask_hit = UINT64_MAX;
659 dp_netdev_reload_pmd_threads(struct dp_netdev *dp)
663 for (i = 0; i < dp->n_pmd_threads; i++) {
664 struct pmd_thread *f = &dp->pmd_threads[i];
667 atomic_add(&f->change_seq, 1, &id);
672 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
674 OVS_REQ_WRLOCK(dp->port_rwlock)
676 struct netdev_saved_flags *sf;
677 struct dp_netdev_port *port;
678 struct netdev *netdev;
679 enum netdev_flags flags;
680 const char *open_type;
684 /* XXX reject devices already in some dp_netdev. */
686 /* Open and validate network device. */
687 open_type = dpif_netdev_port_open_type(dp->class, type);
688 error = netdev_open(devname, open_type, &netdev);
692 /* XXX reject non-Ethernet devices */
694 netdev_get_flags(netdev, &flags);
695 if (flags & NETDEV_LOOPBACK) {
696 VLOG_ERR("%s: cannot add a loopback device", devname);
697 netdev_close(netdev);
701 port = xzalloc(sizeof *port);
702 port->port_no = port_no;
703 port->netdev = netdev;
704 port->rxq = xmalloc(sizeof *port->rxq * netdev_n_rxq(netdev));
705 port->type = xstrdup(type);
706 for (i = 0; i < netdev_n_rxq(netdev); i++) {
707 error = netdev_rxq_open(netdev, &port->rxq[i], i);
709 && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
710 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
711 devname, ovs_strerror(errno));
712 netdev_close(netdev);
717 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
719 for (i = 0; i < netdev_n_rxq(netdev); i++) {
720 netdev_rxq_close(port->rxq[i]);
722 netdev_close(netdev);
729 if (netdev_is_pmd(netdev)) {
731 dp_netdev_set_pmd_threads(dp, NR_THREADS);
732 dp_netdev_reload_pmd_threads(dp);
734 ovs_refcount_init(&port->ref_cnt);
736 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
737 seq_change(dp->port_seq);
743 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
744 odp_port_t *port_nop)
746 struct dp_netdev *dp = get_dp_netdev(dpif);
747 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
748 const char *dpif_port;
752 ovs_rwlock_wrlock(&dp->port_rwlock);
753 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
754 if (*port_nop != ODPP_NONE) {
756 error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0;
758 port_no = choose_port(dp, dpif_port);
759 error = port_no == ODPP_NONE ? EFBIG : 0;
763 error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
765 ovs_rwlock_unlock(&dp->port_rwlock);
771 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
773 struct dp_netdev *dp = get_dp_netdev(dpif);
776 ovs_rwlock_wrlock(&dp->port_rwlock);
777 error = port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
778 ovs_rwlock_unlock(&dp->port_rwlock);
784 is_valid_port_number(odp_port_t port_no)
786 return port_no != ODPP_NONE;
789 static struct dp_netdev_port *
790 dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no)
791 OVS_REQ_RDLOCK(dp->port_rwlock)
793 struct dp_netdev_port *port;
795 HMAP_FOR_EACH_IN_BUCKET (port, node, hash_int(odp_to_u32(port_no), 0),
797 if (port->port_no == port_no) {
805 get_port_by_number(struct dp_netdev *dp,
806 odp_port_t port_no, struct dp_netdev_port **portp)
807 OVS_REQ_RDLOCK(dp->port_rwlock)
809 if (!is_valid_port_number(port_no)) {
813 *portp = dp_netdev_lookup_port(dp, port_no);
814 return *portp ? 0 : ENOENT;
819 port_ref(struct dp_netdev_port *port)
822 ovs_refcount_ref(&port->ref_cnt);
827 port_unref(struct dp_netdev_port *port)
829 if (port && ovs_refcount_unref(&port->ref_cnt) == 1) {
832 netdev_close(port->netdev);
833 netdev_restore_flags(port->sf);
835 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
836 netdev_rxq_close(port->rxq[i]);
844 get_port_by_name(struct dp_netdev *dp,
845 const char *devname, struct dp_netdev_port **portp)
846 OVS_REQ_RDLOCK(dp->port_rwlock)
848 struct dp_netdev_port *port;
850 HMAP_FOR_EACH (port, node, &dp->ports) {
851 if (!strcmp(netdev_get_name(port->netdev), devname)) {
860 do_del_port(struct dp_netdev *dp, odp_port_t port_no)
861 OVS_REQ_WRLOCK(dp->port_rwlock)
863 struct dp_netdev_port *port;
866 error = get_port_by_number(dp, port_no, &port);
871 hmap_remove(&dp->ports, &port->node);
872 seq_change(dp->port_seq);
873 if (netdev_is_pmd(port->netdev)) {
874 dp_netdev_reload_pmd_threads(dp);
882 answer_port_query(const struct dp_netdev_port *port,
883 struct dpif_port *dpif_port)
885 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
886 dpif_port->type = xstrdup(port->type);
887 dpif_port->port_no = port->port_no;
891 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
892 struct dpif_port *dpif_port)
894 struct dp_netdev *dp = get_dp_netdev(dpif);
895 struct dp_netdev_port *port;
898 ovs_rwlock_rdlock(&dp->port_rwlock);
899 error = get_port_by_number(dp, port_no, &port);
900 if (!error && dpif_port) {
901 answer_port_query(port, dpif_port);
903 ovs_rwlock_unlock(&dp->port_rwlock);
909 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
910 struct dpif_port *dpif_port)
912 struct dp_netdev *dp = get_dp_netdev(dpif);
913 struct dp_netdev_port *port;
916 ovs_rwlock_rdlock(&dp->port_rwlock);
917 error = get_port_by_name(dp, devname, &port);
918 if (!error && dpif_port) {
919 answer_port_query(port, dpif_port);
921 ovs_rwlock_unlock(&dp->port_rwlock);
927 dp_netdev_flow_free(struct dp_netdev_flow *flow)
929 struct dp_netdev_flow_stats *bucket;
932 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &flow->stats) {
933 ovs_mutex_destroy(&bucket->mutex);
934 free_cacheline(bucket);
936 ovsthread_stats_destroy(&flow->stats);
938 cls_rule_destroy(CONST_CAST(struct cls_rule *, &flow->cr));
939 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow));
940 ovs_mutex_destroy(&flow->mutex);
945 dp_netdev_remove_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
946 OVS_REQ_WRLOCK(dp->cls.rwlock)
947 OVS_REQUIRES(dp->flow_mutex)
949 struct cls_rule *cr = CONST_CAST(struct cls_rule *, &flow->cr);
950 struct hmap_node *node = CONST_CAST(struct hmap_node *, &flow->node);
952 classifier_remove(&dp->cls, cr);
953 hmap_remove(&dp->flow_table, node);
954 ovsrcu_postpone(dp_netdev_flow_free, flow);
958 dp_netdev_flow_flush(struct dp_netdev *dp)
960 struct dp_netdev_flow *netdev_flow, *next;
962 ovs_mutex_lock(&dp->flow_mutex);
963 fat_rwlock_wrlock(&dp->cls.rwlock);
964 HMAP_FOR_EACH_SAFE (netdev_flow, next, node, &dp->flow_table) {
965 dp_netdev_remove_flow(dp, netdev_flow);
967 fat_rwlock_unlock(&dp->cls.rwlock);
968 ovs_mutex_unlock(&dp->flow_mutex);
972 dpif_netdev_flow_flush(struct dpif *dpif)
974 struct dp_netdev *dp = get_dp_netdev(dpif);
976 dp_netdev_flow_flush(dp);
980 struct dp_netdev_port_state {
987 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
989 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
994 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
995 struct dpif_port *dpif_port)
997 struct dp_netdev_port_state *state = state_;
998 struct dp_netdev *dp = get_dp_netdev(dpif);
999 struct hmap_node *node;
1002 ovs_rwlock_rdlock(&dp->port_rwlock);
1003 node = hmap_at_position(&dp->ports, &state->bucket, &state->offset);
1005 struct dp_netdev_port *port;
1007 port = CONTAINER_OF(node, struct dp_netdev_port, node);
1010 state->name = xstrdup(netdev_get_name(port->netdev));
1011 dpif_port->name = state->name;
1012 dpif_port->type = port->type;
1013 dpif_port->port_no = port->port_no;
1019 ovs_rwlock_unlock(&dp->port_rwlock);
1025 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1027 struct dp_netdev_port_state *state = state_;
1034 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
1036 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
1037 uint64_t new_port_seq;
1040 new_port_seq = seq_read(dpif->dp->port_seq);
1041 if (dpif->last_port_seq != new_port_seq) {
1042 dpif->last_port_seq = new_port_seq;
1052 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
1054 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
1056 seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
1059 static struct dp_netdev_flow *
1060 dp_netdev_flow_cast(const struct cls_rule *cr)
1062 return cr ? CONTAINER_OF(cr, struct dp_netdev_flow, cr) : NULL;
1065 static struct dp_netdev_flow *
1066 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *flow)
1067 OVS_EXCLUDED(dp->cls.rwlock)
1069 struct dp_netdev_flow *netdev_flow;
1071 fat_rwlock_rdlock(&dp->cls.rwlock);
1072 netdev_flow = dp_netdev_flow_cast(classifier_lookup(&dp->cls, flow, NULL));
1073 fat_rwlock_unlock(&dp->cls.rwlock);
1078 static struct dp_netdev_flow *
1079 dp_netdev_find_flow(const struct dp_netdev *dp, const struct flow *flow)
1080 OVS_REQ_RDLOCK(dp->cls.rwlock)
1082 struct dp_netdev_flow *netdev_flow;
1084 HMAP_FOR_EACH_WITH_HASH (netdev_flow, node, flow_hash(flow, 0),
1086 if (flow_equal(&netdev_flow->flow, flow)) {
1095 get_dpif_flow_stats(struct dp_netdev_flow *netdev_flow,
1096 struct dpif_flow_stats *stats)
1098 struct dp_netdev_flow_stats *bucket;
1101 memset(stats, 0, sizeof *stats);
1102 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1103 ovs_mutex_lock(&bucket->mutex);
1104 stats->n_packets += bucket->packet_count;
1105 stats->n_bytes += bucket->byte_count;
1106 stats->used = MAX(stats->used, bucket->used);
1107 stats->tcp_flags |= bucket->tcp_flags;
1108 ovs_mutex_unlock(&bucket->mutex);
1113 dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1114 const struct nlattr *mask_key,
1115 uint32_t mask_key_len, const struct flow *flow,
1119 enum odp_key_fitness fitness;
1121 fitness = odp_flow_key_to_mask(mask_key, mask_key_len, mask, flow);
1123 /* This should not happen: it indicates that
1124 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
1125 * disagree on the acceptable form of a mask. Log the problem
1126 * as an error, with enough details to enable debugging. */
1127 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1129 if (!VLOG_DROP_ERR(&rl)) {
1133 odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s,
1135 VLOG_ERR("internal error parsing flow mask %s (%s)",
1136 ds_cstr(&s), odp_key_fitness_to_string(fitness));
1143 enum mf_field_id id;
1144 /* No mask key, unwildcard everything except fields whose
1145 * prerequisities are not met. */
1146 memset(mask, 0x0, sizeof *mask);
1148 for (id = 0; id < MFF_N_IDS; ++id) {
1149 /* Skip registers and metadata. */
1150 if (!(id >= MFF_REG0 && id < MFF_REG0 + FLOW_N_REGS)
1151 && id != MFF_METADATA) {
1152 const struct mf_field *mf = mf_from_id(id);
1153 if (mf_are_prereqs_ok(mf, flow)) {
1154 mf_mask_field(mf, mask);
1160 /* Force unwildcard the in_port.
1162 * We need to do this even in the case where we unwildcard "everything"
1163 * above because "everything" only includes the 16-bit OpenFlow port number
1164 * mask->in_port.ofp_port, which only covers half of the 32-bit datapath
1165 * port number mask->in_port.odp_port. */
1166 mask->in_port.odp_port = u32_to_odp(UINT32_MAX);
1172 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1177 if (odp_flow_key_to_flow(key, key_len, flow)) {
1178 /* This should not happen: it indicates that odp_flow_key_from_flow()
1179 * and odp_flow_key_to_flow() disagree on the acceptable form of a
1180 * flow. Log the problem as an error, with enough details to enable
1182 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1184 if (!VLOG_DROP_ERR(&rl)) {
1188 odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
1189 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
1196 in_port = flow->in_port.odp_port;
1197 if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
1205 dpif_netdev_flow_get(const struct dpif *dpif,
1206 const struct nlattr *nl_key, size_t nl_key_len,
1207 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
1209 struct dp_netdev *dp = get_dp_netdev(dpif);
1210 struct dp_netdev_flow *netdev_flow;
1214 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
1219 fat_rwlock_rdlock(&dp->cls.rwlock);
1220 netdev_flow = dp_netdev_find_flow(dp, &key);
1221 fat_rwlock_unlock(&dp->cls.rwlock);
1225 get_dpif_flow_stats(netdev_flow, stats);
1229 struct dp_netdev_actions *actions;
1231 actions = dp_netdev_flow_get_actions(netdev_flow);
1232 *actionsp = ofpbuf_clone_data(actions->actions, actions->size);
1242 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *flow,
1243 const struct flow_wildcards *wc,
1244 const struct nlattr *actions,
1246 OVS_REQUIRES(dp->flow_mutex)
1248 struct dp_netdev_flow *netdev_flow;
1251 netdev_flow = xzalloc(sizeof *netdev_flow);
1252 *CONST_CAST(struct flow *, &netdev_flow->flow) = *flow;
1254 ovs_mutex_init(&netdev_flow->mutex);
1256 ovsthread_stats_init(&netdev_flow->stats);
1258 ovsrcu_set(&netdev_flow->actions,
1259 dp_netdev_actions_create(actions, actions_len));
1261 match_init(&match, flow, wc);
1262 cls_rule_init(CONST_CAST(struct cls_rule *, &netdev_flow->cr),
1263 &match, NETDEV_RULE_PRIORITY);
1264 fat_rwlock_wrlock(&dp->cls.rwlock);
1265 classifier_insert(&dp->cls,
1266 CONST_CAST(struct cls_rule *, &netdev_flow->cr));
1267 hmap_insert(&dp->flow_table,
1268 CONST_CAST(struct hmap_node *, &netdev_flow->node),
1269 flow_hash(flow, 0));
1270 fat_rwlock_unlock(&dp->cls.rwlock);
1276 clear_stats(struct dp_netdev_flow *netdev_flow)
1278 struct dp_netdev_flow_stats *bucket;
1281 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1282 ovs_mutex_lock(&bucket->mutex);
1284 bucket->packet_count = 0;
1285 bucket->byte_count = 0;
1286 bucket->tcp_flags = 0;
1287 ovs_mutex_unlock(&bucket->mutex);
1292 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
1294 struct dp_netdev *dp = get_dp_netdev(dpif);
1295 struct dp_netdev_flow *netdev_flow;
1297 struct flow_wildcards wc;
1300 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &flow);
1304 error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
1305 put->mask, put->mask_len,
1311 ovs_mutex_lock(&dp->flow_mutex);
1312 netdev_flow = dp_netdev_lookup_flow(dp, &flow);
1314 if (put->flags & DPIF_FP_CREATE) {
1315 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
1317 memset(put->stats, 0, sizeof *put->stats);
1319 error = dp_netdev_flow_add(dp, &flow, &wc, put->actions,
1328 if (put->flags & DPIF_FP_MODIFY
1329 && flow_equal(&flow, &netdev_flow->flow)) {
1330 struct dp_netdev_actions *new_actions;
1331 struct dp_netdev_actions *old_actions;
1333 new_actions = dp_netdev_actions_create(put->actions,
1336 old_actions = dp_netdev_flow_get_actions(netdev_flow);
1337 ovsrcu_set(&netdev_flow->actions, new_actions);
1340 get_dpif_flow_stats(netdev_flow, put->stats);
1342 if (put->flags & DPIF_FP_ZERO_STATS) {
1343 clear_stats(netdev_flow);
1346 ovsrcu_postpone(dp_netdev_actions_free, old_actions);
1347 } else if (put->flags & DPIF_FP_CREATE) {
1350 /* Overlapping flow. */
1354 ovs_mutex_unlock(&dp->flow_mutex);
1360 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
1362 struct dp_netdev *dp = get_dp_netdev(dpif);
1363 struct dp_netdev_flow *netdev_flow;
1367 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
1372 ovs_mutex_lock(&dp->flow_mutex);
1373 fat_rwlock_wrlock(&dp->cls.rwlock);
1374 netdev_flow = dp_netdev_find_flow(dp, &key);
1377 get_dpif_flow_stats(netdev_flow, del->stats);
1379 dp_netdev_remove_flow(dp, netdev_flow);
1383 fat_rwlock_unlock(&dp->cls.rwlock);
1384 ovs_mutex_unlock(&dp->flow_mutex);
1389 struct dp_netdev_flow_state {
1390 struct dp_netdev_actions *actions;
1391 struct odputil_keybuf keybuf;
1392 struct odputil_keybuf maskbuf;
1393 struct dpif_flow_stats stats;
1396 struct dp_netdev_flow_iter {
1400 struct ovs_mutex mutex;
1404 dpif_netdev_flow_dump_state_init(void **statep)
1406 struct dp_netdev_flow_state *state;
1408 *statep = state = xmalloc(sizeof *state);
1409 state->actions = NULL;
1413 dpif_netdev_flow_dump_state_uninit(void *state_)
1415 struct dp_netdev_flow_state *state = state_;
1421 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **iterp)
1423 struct dp_netdev_flow_iter *iter;
1425 *iterp = iter = xmalloc(sizeof *iter);
1429 ovs_mutex_init(&iter->mutex);
1433 /* XXX the caller must use 'actions' without quiescing */
1435 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *iter_, void *state_,
1436 const struct nlattr **key, size_t *key_len,
1437 const struct nlattr **mask, size_t *mask_len,
1438 const struct nlattr **actions, size_t *actions_len,
1439 const struct dpif_flow_stats **stats)
1441 struct dp_netdev_flow_iter *iter = iter_;
1442 struct dp_netdev_flow_state *state = state_;
1443 struct dp_netdev *dp = get_dp_netdev(dpif);
1444 struct dp_netdev_flow *netdev_flow;
1447 ovs_mutex_lock(&iter->mutex);
1448 error = iter->status;
1450 struct hmap_node *node;
1452 fat_rwlock_rdlock(&dp->cls.rwlock);
1453 node = hmap_at_position(&dp->flow_table, &iter->bucket, &iter->offset);
1455 netdev_flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
1457 fat_rwlock_unlock(&dp->cls.rwlock);
1459 iter->status = error = EOF;
1462 ovs_mutex_unlock(&iter->mutex);
1470 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
1471 odp_flow_key_from_flow(&buf, &netdev_flow->flow,
1472 netdev_flow->flow.in_port.odp_port);
1474 *key = ofpbuf_data(&buf);
1475 *key_len = ofpbuf_size(&buf);
1480 struct flow_wildcards wc;
1482 ofpbuf_use_stack(&buf, &state->maskbuf, sizeof state->maskbuf);
1483 minimask_expand(&netdev_flow->cr.match.mask, &wc);
1484 odp_flow_key_from_mask(&buf, &wc.masks, &netdev_flow->flow,
1485 odp_to_u32(wc.masks.in_port.odp_port),
1488 *mask = ofpbuf_data(&buf);
1489 *mask_len = ofpbuf_size(&buf);
1492 if (actions || stats) {
1493 state->actions = NULL;
1496 state->actions = dp_netdev_flow_get_actions(netdev_flow);
1497 *actions = state->actions->actions;
1498 *actions_len = state->actions->size;
1502 get_dpif_flow_stats(netdev_flow, &state->stats);
1503 *stats = &state->stats;
1511 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *iter_)
1513 struct dp_netdev_flow_iter *iter = iter_;
1515 ovs_mutex_destroy(&iter->mutex);
1521 dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
1523 struct dp_netdev *dp = get_dp_netdev(dpif);
1524 struct pkt_metadata *md = &execute->md;
1527 if (ofpbuf_size(execute->packet) < ETH_HEADER_LEN ||
1528 ofpbuf_size(execute->packet) > UINT16_MAX) {
1532 /* Extract flow key. */
1533 flow_extract(execute->packet, md, &key);
1535 ovs_rwlock_rdlock(&dp->port_rwlock);
1536 dp_netdev_execute_actions(dp, &key, execute->packet, false, md,
1537 execute->actions, execute->actions_len);
1538 ovs_rwlock_unlock(&dp->port_rwlock);
1544 dp_netdev_destroy_all_queues(struct dp_netdev *dp)
1545 OVS_REQ_WRLOCK(dp->queue_rwlock)
1549 dp_netdev_purge_queues(dp);
1551 for (i = 0; i < dp->n_handlers; i++) {
1552 struct dp_netdev_queue *q = &dp->handler_queues[i];
1554 ovs_mutex_destroy(&q->mutex);
1555 seq_destroy(q->seq);
1557 free(dp->handler_queues);
1558 dp->handler_queues = NULL;
1563 dp_netdev_refresh_queues(struct dp_netdev *dp, uint32_t n_handlers)
1564 OVS_REQ_WRLOCK(dp->queue_rwlock)
1566 if (dp->n_handlers != n_handlers) {
1569 dp_netdev_destroy_all_queues(dp);
1571 dp->n_handlers = n_handlers;
1572 dp->handler_queues = xzalloc(n_handlers * sizeof *dp->handler_queues);
1574 for (i = 0; i < n_handlers; i++) {
1575 struct dp_netdev_queue *q = &dp->handler_queues[i];
1577 ovs_mutex_init(&q->mutex);
1578 q->seq = seq_create();
1584 dpif_netdev_recv_set(struct dpif *dpif, bool enable)
1586 struct dp_netdev *dp = get_dp_netdev(dpif);
1588 if ((dp->handler_queues != NULL) == enable) {
1592 fat_rwlock_wrlock(&dp->queue_rwlock);
1594 dp_netdev_destroy_all_queues(dp);
1596 dp_netdev_refresh_queues(dp, 1);
1598 fat_rwlock_unlock(&dp->queue_rwlock);
1604 dpif_netdev_handlers_set(struct dpif *dpif, uint32_t n_handlers)
1606 struct dp_netdev *dp = get_dp_netdev(dpif);
1608 fat_rwlock_wrlock(&dp->queue_rwlock);
1609 if (dp->handler_queues) {
1610 dp_netdev_refresh_queues(dp, n_handlers);
1612 fat_rwlock_unlock(&dp->queue_rwlock);
1618 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1619 uint32_t queue_id, uint32_t *priority)
1621 *priority = queue_id;
1626 dp_netdev_recv_check(const struct dp_netdev *dp, const uint32_t handler_id)
1627 OVS_REQ_RDLOCK(dp->queue_rwlock)
1629 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1631 if (!dp->handler_queues) {
1632 VLOG_WARN_RL(&rl, "receiving upcall disabled");
1636 if (handler_id >= dp->n_handlers) {
1637 VLOG_WARN_RL(&rl, "handler index out of bound");
1645 dpif_netdev_recv(struct dpif *dpif, uint32_t handler_id,
1646 struct dpif_upcall *upcall, struct ofpbuf *buf)
1648 struct dp_netdev *dp = get_dp_netdev(dpif);
1649 struct dp_netdev_queue *q;
1652 fat_rwlock_rdlock(&dp->queue_rwlock);
1654 if (!dp_netdev_recv_check(dp, handler_id)) {
1659 q = &dp->handler_queues[handler_id];
1660 ovs_mutex_lock(&q->mutex);
1661 if (q->head != q->tail) {
1662 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1664 *upcall = u->upcall;
1671 ovs_mutex_unlock(&q->mutex);
1674 fat_rwlock_unlock(&dp->queue_rwlock);
1680 dpif_netdev_recv_wait(struct dpif *dpif, uint32_t handler_id)
1682 struct dp_netdev *dp = get_dp_netdev(dpif);
1683 struct dp_netdev_queue *q;
1686 fat_rwlock_rdlock(&dp->queue_rwlock);
1688 if (!dp_netdev_recv_check(dp, handler_id)) {
1692 q = &dp->handler_queues[handler_id];
1693 ovs_mutex_lock(&q->mutex);
1694 seq = seq_read(q->seq);
1695 if (q->head != q->tail) {
1696 poll_immediate_wake();
1698 seq_wait(q->seq, seq);
1701 ovs_mutex_unlock(&q->mutex);
1704 fat_rwlock_unlock(&dp->queue_rwlock);
1708 dpif_netdev_recv_purge(struct dpif *dpif)
1710 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1712 fat_rwlock_wrlock(&dpif_netdev->dp->queue_rwlock);
1713 dp_netdev_purge_queues(dpif_netdev->dp);
1714 fat_rwlock_unlock(&dpif_netdev->dp->queue_rwlock);
1717 /* Creates and returns a new 'struct dp_netdev_actions', with a reference count
1718 * of 1, whose actions are a copy of from the 'ofpacts_len' bytes of
1720 struct dp_netdev_actions *
1721 dp_netdev_actions_create(const struct nlattr *actions, size_t size)
1723 struct dp_netdev_actions *netdev_actions;
1725 netdev_actions = xmalloc(sizeof *netdev_actions);
1726 netdev_actions->actions = xmemdup(actions, size);
1727 netdev_actions->size = size;
1729 return netdev_actions;
1732 struct dp_netdev_actions *
1733 dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow)
1735 return ovsrcu_get(struct dp_netdev_actions *, &flow->actions);
1739 dp_netdev_actions_free(struct dp_netdev_actions *actions)
1741 free(actions->actions);
1747 dp_netdev_process_rxq_port(struct dp_netdev *dp,
1748 struct dp_netdev_port *port,
1749 struct netdev_rxq *rxq)
1751 struct ofpbuf *packet[NETDEV_MAX_RX_BATCH];
1754 error = netdev_rxq_recv(rxq, packet, &c);
1756 struct pkt_metadata md = PKT_METADATA_INITIALIZER(port->port_no);
1759 for (i = 0; i < c; i++) {
1760 dp_netdev_port_input(dp, packet[i], &md);
1762 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1763 static struct vlog_rate_limit rl
1764 = VLOG_RATE_LIMIT_INIT(1, 5);
1766 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1767 netdev_get_name(port->netdev),
1768 ovs_strerror(error));
1773 dpif_netdev_run(struct dpif *dpif)
1775 struct dp_netdev_port *port;
1776 struct dp_netdev *dp = get_dp_netdev(dpif);
1778 ovs_rwlock_rdlock(&dp->port_rwlock);
1780 HMAP_FOR_EACH (port, node, &dp->ports) {
1781 if (!netdev_is_pmd(port->netdev)) {
1784 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1785 dp_netdev_process_rxq_port(dp, port, port->rxq[i]);
1790 ovs_rwlock_unlock(&dp->port_rwlock);
1794 dpif_netdev_wait(struct dpif *dpif)
1796 struct dp_netdev_port *port;
1797 struct dp_netdev *dp = get_dp_netdev(dpif);
1799 ovs_rwlock_rdlock(&dp->port_rwlock);
1801 HMAP_FOR_EACH (port, node, &dp->ports) {
1802 if (!netdev_is_pmd(port->netdev)) {
1805 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1806 netdev_rxq_wait(port->rxq[i]);
1810 ovs_rwlock_unlock(&dp->port_rwlock);
1814 struct dp_netdev_port *port;
1815 struct netdev_rxq *rx;
1819 pmd_load_queues(struct pmd_thread *f,
1820 struct rxq_poll **ppoll_list, int poll_cnt)
1822 struct dp_netdev *dp = f->dp;
1823 struct rxq_poll *poll_list = *ppoll_list;
1824 struct dp_netdev_port *port;
1829 /* Simple scheduler for netdev rx polling. */
1830 ovs_rwlock_rdlock(&dp->port_rwlock);
1831 for (i = 0; i < poll_cnt; i++) {
1832 port_unref(poll_list[i].port);
1838 HMAP_FOR_EACH (port, node, &f->dp->ports) {
1839 if (netdev_is_pmd(port->netdev)) {
1842 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1843 if ((index % dp->n_pmd_threads) == id) {
1844 poll_list = xrealloc(poll_list, sizeof *poll_list * (poll_cnt + 1));
1847 poll_list[poll_cnt].port = port;
1848 poll_list[poll_cnt].rx = port->rxq[i];
1856 ovs_rwlock_unlock(&dp->port_rwlock);
1857 *ppoll_list = poll_list;
1862 pmd_thread_main(void *f_)
1864 struct pmd_thread *f = f_;
1865 struct dp_netdev *dp = f->dp;
1866 unsigned int lc = 0;
1867 struct rxq_poll *poll_list;
1868 unsigned int port_seq;
1872 f->name = xasprintf("pmd_%u", ovsthread_id_self());
1873 set_subprogram_name("%s", f->name);
1877 pmd_thread_setaffinity_cpu(f->id);
1879 poll_cnt = pmd_load_queues(f, &poll_list, poll_cnt);
1880 atomic_read(&f->change_seq, &port_seq);
1883 unsigned int c_port_seq;
1886 for (i = 0; i < poll_cnt; i++) {
1887 dp_netdev_process_rxq_port(dp, poll_list[i].port, poll_list[i].rx);
1893 /* TODO: need completely userspace based signaling method.
1894 * to keep this thread entirely in userspace.
1895 * For now using atomic counter. */
1897 atomic_read_explicit(&f->change_seq, &c_port_seq, memory_order_consume);
1898 if (c_port_seq != port_seq) {
1904 if (!latch_is_set(&f->dp->exit_latch)){
1908 for (i = 0; i < poll_cnt; i++) {
1909 port_unref(poll_list[i].port);
1918 dp_netdev_set_pmd_threads(struct dp_netdev *dp, int n)
1922 if (n == dp->n_pmd_threads) {
1926 /* Stop existing threads. */
1927 latch_set(&dp->exit_latch);
1928 dp_netdev_reload_pmd_threads(dp);
1929 for (i = 0; i < dp->n_pmd_threads; i++) {
1930 struct pmd_thread *f = &dp->pmd_threads[i];
1932 xpthread_join(f->thread, NULL);
1934 latch_poll(&dp->exit_latch);
1935 free(dp->pmd_threads);
1937 /* Start new threads. */
1938 dp->pmd_threads = xmalloc(n * sizeof *dp->pmd_threads);
1939 dp->n_pmd_threads = n;
1941 for (i = 0; i < n; i++) {
1942 struct pmd_thread *f = &dp->pmd_threads[i];
1946 atomic_store(&f->change_seq, 1);
1948 /* Each thread will distribute all devices rx-queues among
1950 xpthread_create(&f->thread, NULL, pmd_thread_main, f);
1956 dp_netdev_flow_stats_new_cb(void)
1958 struct dp_netdev_flow_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1959 ovs_mutex_init(&bucket->mutex);
1964 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow,
1965 const struct ofpbuf *packet,
1966 const struct flow *key)
1968 uint16_t tcp_flags = ntohs(key->tcp_flags);
1969 long long int now = time_msec();
1970 struct dp_netdev_flow_stats *bucket;
1972 bucket = ovsthread_stats_bucket_get(&netdev_flow->stats,
1973 dp_netdev_flow_stats_new_cb);
1975 ovs_mutex_lock(&bucket->mutex);
1976 bucket->used = MAX(now, bucket->used);
1977 bucket->packet_count++;
1978 bucket->byte_count += ofpbuf_size(packet);
1979 bucket->tcp_flags |= tcp_flags;
1980 ovs_mutex_unlock(&bucket->mutex);
1984 dp_netdev_stats_new_cb(void)
1986 struct dp_netdev_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1987 ovs_mutex_init(&bucket->mutex);
1992 dp_netdev_count_packet(struct dp_netdev *dp, enum dp_stat_type type)
1994 struct dp_netdev_stats *bucket;
1996 bucket = ovsthread_stats_bucket_get(&dp->stats, dp_netdev_stats_new_cb);
1997 ovs_mutex_lock(&bucket->mutex);
1999 ovs_mutex_unlock(&bucket->mutex);
2003 dp_netdev_input(struct dp_netdev *dp, struct ofpbuf *packet,
2004 struct pkt_metadata *md)
2005 OVS_REQ_RDLOCK(dp->port_rwlock)
2007 struct dp_netdev_flow *netdev_flow;
2010 if (ofpbuf_size(packet) < ETH_HEADER_LEN) {
2011 ofpbuf_delete(packet);
2014 flow_extract(packet, md, &key);
2015 netdev_flow = dp_netdev_lookup_flow(dp, &key);
2017 struct dp_netdev_actions *actions;
2019 dp_netdev_flow_used(netdev_flow, packet, &key);
2021 actions = dp_netdev_flow_get_actions(netdev_flow);
2022 dp_netdev_execute_actions(dp, &key, packet, true, md,
2023 actions->actions, actions->size);
2024 dp_netdev_count_packet(dp, DP_STAT_HIT);
2025 } else if (dp->handler_queues) {
2026 dp_netdev_count_packet(dp, DP_STAT_MISS);
2027 dp_netdev_output_userspace(dp, packet,
2028 flow_hash_5tuple(&key, 0) % dp->n_handlers,
2029 DPIF_UC_MISS, &key, NULL);
2030 ofpbuf_delete(packet);
2035 dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
2036 struct pkt_metadata *md)
2037 OVS_REQ_RDLOCK(dp->port_rwlock)
2039 uint32_t *recirc_depth = recirc_depth_get();
2042 dp_netdev_input(dp, packet, md);
2046 dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *packet,
2047 int queue_no, int type, const struct flow *flow,
2048 const struct nlattr *userdata)
2050 struct dp_netdev_queue *q;
2053 fat_rwlock_rdlock(&dp->queue_rwlock);
2054 q = &dp->handler_queues[queue_no];
2055 ovs_mutex_lock(&q->mutex);
2056 if (q->head - q->tail < MAX_QUEUE_LEN) {
2057 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
2058 struct dpif_upcall *upcall = &u->upcall;
2059 struct ofpbuf *buf = &u->buf;
2062 upcall->type = type;
2064 /* Allocate buffer big enough for everything. */
2065 buf_size = ODPUTIL_FLOW_KEY_BYTES;
2067 buf_size += NLA_ALIGN(userdata->nla_len);
2069 buf_size += ofpbuf_size(packet);
2070 ofpbuf_init(buf, buf_size);
2073 odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
2074 upcall->key = ofpbuf_data(buf);
2075 upcall->key_len = ofpbuf_size(buf);
2079 upcall->userdata = ofpbuf_put(buf, userdata,
2080 NLA_ALIGN(userdata->nla_len));
2083 ofpbuf_set_data(&upcall->packet,
2084 ofpbuf_put(buf, ofpbuf_data(packet), ofpbuf_size(packet)));
2085 ofpbuf_set_size(&upcall->packet, ofpbuf_size(packet));
2091 dp_netdev_count_packet(dp, DP_STAT_LOST);
2094 ovs_mutex_unlock(&q->mutex);
2095 fat_rwlock_unlock(&dp->queue_rwlock);
2100 struct dp_netdev_execute_aux {
2101 struct dp_netdev *dp;
2102 const struct flow *key;
2106 dp_execute_cb(void *aux_, struct ofpbuf *packet,
2107 struct pkt_metadata *md,
2108 const struct nlattr *a, bool may_steal)
2109 OVS_NO_THREAD_SAFETY_ANALYSIS
2111 struct dp_netdev_execute_aux *aux = aux_;
2112 int type = nl_attr_type(a);
2113 struct dp_netdev_port *p;
2114 uint32_t *depth = recirc_depth_get();
2116 switch ((enum ovs_action_attr)type) {
2117 case OVS_ACTION_ATTR_OUTPUT:
2118 p = dp_netdev_lookup_port(aux->dp, u32_to_odp(nl_attr_get_u32(a)));
2120 netdev_send(p->netdev, packet, may_steal);
2124 case OVS_ACTION_ATTR_USERSPACE: {
2125 const struct nlattr *userdata;
2127 userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
2129 dp_netdev_output_userspace(aux->dp, packet,
2130 flow_hash_5tuple(aux->key, 0)
2131 % aux->dp->n_handlers,
2132 DPIF_UC_ACTION, aux->key,
2136 ofpbuf_delete(packet);
2141 case OVS_ACTION_ATTR_HASH: {
2142 const struct ovs_action_hash *hash_act;
2145 hash_act = nl_attr_get(a);
2146 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
2148 hash = flow_hash_symmetric_l4(aux->key, hash_act->hash_bias);
2150 hash = 1; /* 0 is not valid */
2154 VLOG_WARN("Unknown hash algorithm specified for the hash action.");
2162 case OVS_ACTION_ATTR_RECIRC:
2163 if (*depth < MAX_RECIRC_DEPTH) {
2164 struct pkt_metadata recirc_md = *md;
2165 struct ofpbuf *recirc_packet;
2167 recirc_packet = may_steal ? packet : ofpbuf_clone(packet);
2168 recirc_md.recirc_id = nl_attr_get_u32(a);
2171 dp_netdev_input(aux->dp, recirc_packet, &recirc_md);
2176 VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
2180 case OVS_ACTION_ATTR_PUSH_VLAN:
2181 case OVS_ACTION_ATTR_POP_VLAN:
2182 case OVS_ACTION_ATTR_PUSH_MPLS:
2183 case OVS_ACTION_ATTR_POP_MPLS:
2184 case OVS_ACTION_ATTR_SET:
2185 case OVS_ACTION_ATTR_SAMPLE:
2186 case OVS_ACTION_ATTR_UNSPEC:
2187 case __OVS_ACTION_ATTR_MAX:
2193 dp_netdev_execute_actions(struct dp_netdev *dp, const struct flow *key,
2194 struct ofpbuf *packet, bool may_steal,
2195 struct pkt_metadata *md,
2196 const struct nlattr *actions, size_t actions_len)
2198 struct dp_netdev_execute_aux aux = {dp, key};
2200 odp_execute_actions(&aux, packet, may_steal, md,
2201 actions, actions_len, dp_execute_cb);
2204 const struct dpif_class dpif_netdev_class = {
2206 dpif_netdev_enumerate,
2207 dpif_netdev_port_open_type,
2210 dpif_netdev_destroy,
2213 dpif_netdev_get_stats,
2214 dpif_netdev_port_add,
2215 dpif_netdev_port_del,
2216 dpif_netdev_port_query_by_number,
2217 dpif_netdev_port_query_by_name,
2218 NULL, /* port_get_pid */
2219 dpif_netdev_port_dump_start,
2220 dpif_netdev_port_dump_next,
2221 dpif_netdev_port_dump_done,
2222 dpif_netdev_port_poll,
2223 dpif_netdev_port_poll_wait,
2224 dpif_netdev_flow_get,
2225 dpif_netdev_flow_put,
2226 dpif_netdev_flow_del,
2227 dpif_netdev_flow_flush,
2228 dpif_netdev_flow_dump_state_init,
2229 dpif_netdev_flow_dump_start,
2230 dpif_netdev_flow_dump_next,
2232 dpif_netdev_flow_dump_done,
2233 dpif_netdev_flow_dump_state_uninit,
2234 dpif_netdev_execute,
2236 dpif_netdev_recv_set,
2237 dpif_netdev_handlers_set,
2238 dpif_netdev_queue_to_priority,
2240 dpif_netdev_recv_wait,
2241 dpif_netdev_recv_purge,
2245 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
2246 const char *argv[], void *aux OVS_UNUSED)
2248 struct dp_netdev_port *port;
2249 struct dp_netdev *dp;
2252 ovs_mutex_lock(&dp_netdev_mutex);
2253 dp = shash_find_data(&dp_netdevs, argv[1]);
2254 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
2255 ovs_mutex_unlock(&dp_netdev_mutex);
2256 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
2259 ovs_refcount_ref(&dp->ref_cnt);
2260 ovs_mutex_unlock(&dp_netdev_mutex);
2262 ovs_rwlock_wrlock(&dp->port_rwlock);
2263 if (get_port_by_name(dp, argv[2], &port)) {
2264 unixctl_command_reply_error(conn, "unknown port");
2268 port_no = u32_to_odp(atoi(argv[3]));
2269 if (!port_no || port_no == ODPP_NONE) {
2270 unixctl_command_reply_error(conn, "bad port number");
2273 if (dp_netdev_lookup_port(dp, port_no)) {
2274 unixctl_command_reply_error(conn, "port number already in use");
2277 hmap_remove(&dp->ports, &port->node);
2278 port->port_no = port_no;
2279 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
2280 seq_change(dp->port_seq);
2281 unixctl_command_reply(conn, NULL);
2284 ovs_rwlock_unlock(&dp->port_rwlock);
2285 dp_netdev_unref(dp);
2289 dpif_dummy_register__(const char *type)
2291 struct dpif_class *class;
2293 class = xmalloc(sizeof *class);
2294 *class = dpif_netdev_class;
2295 class->type = xstrdup(type);
2296 dp_register_provider(class);
2300 dpif_dummy_register(bool override)
2307 dp_enumerate_types(&types);
2308 SSET_FOR_EACH (type, &types) {
2309 if (!dp_unregister_provider(type)) {
2310 dpif_dummy_register__(type);
2313 sset_destroy(&types);
2316 dpif_dummy_register__("dummy");
2318 unixctl_command_register("dpif-dummy/change-port-number",
2319 "DP PORT NEW-NUMBER",
2320 3, 3, dpif_dummy_change_port_number, NULL);