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;
328 /* Interface to netdev-based datapath. */
331 struct dp_netdev *dp;
332 uint64_t last_port_seq;
335 static int get_port_by_number(struct dp_netdev *dp, odp_port_t port_no,
336 struct dp_netdev_port **portp)
337 OVS_REQ_RDLOCK(dp->port_rwlock);
338 static int get_port_by_name(struct dp_netdev *dp, const char *devname,
339 struct dp_netdev_port **portp)
340 OVS_REQ_RDLOCK(dp->port_rwlock);
341 static void dp_netdev_free(struct dp_netdev *)
342 OVS_REQUIRES(dp_netdev_mutex);
343 static void dp_netdev_flow_flush(struct dp_netdev *);
344 static int do_add_port(struct dp_netdev *dp, const char *devname,
345 const char *type, odp_port_t port_no)
346 OVS_REQ_WRLOCK(dp->port_rwlock);
347 static int do_del_port(struct dp_netdev *dp, odp_port_t port_no)
348 OVS_REQ_WRLOCK(dp->port_rwlock);
349 static void dp_netdev_destroy_all_queues(struct dp_netdev *dp)
350 OVS_REQ_WRLOCK(dp->queue_rwlock);
351 static int dpif_netdev_open(const struct dpif_class *, const char *name,
352 bool create, struct dpif **);
353 static int dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *,
354 int queue_no, int type,
355 const struct miniflow *,
356 const struct nlattr *userdata);
357 static void dp_netdev_execute_actions(struct dp_netdev *dp,
358 const struct miniflow *,
359 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 miniflow *key)
1067 OVS_EXCLUDED(dp->cls.rwlock)
1069 struct dp_netdev_flow *netdev_flow;
1070 struct cls_rule *rule;
1072 fat_rwlock_rdlock(&dp->cls.rwlock);
1073 rule = classifier_lookup_miniflow_first(&dp->cls, key);
1074 netdev_flow = dp_netdev_flow_cast(rule);
1075 fat_rwlock_unlock(&dp->cls.rwlock);
1080 static struct dp_netdev_flow *
1081 dp_netdev_find_flow(const struct dp_netdev *dp, const struct flow *flow)
1082 OVS_REQ_RDLOCK(dp->cls.rwlock)
1084 struct dp_netdev_flow *netdev_flow;
1086 HMAP_FOR_EACH_WITH_HASH (netdev_flow, node, flow_hash(flow, 0),
1088 if (flow_equal(&netdev_flow->flow, flow)) {
1097 get_dpif_flow_stats(struct dp_netdev_flow *netdev_flow,
1098 struct dpif_flow_stats *stats)
1100 struct dp_netdev_flow_stats *bucket;
1103 memset(stats, 0, sizeof *stats);
1104 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1105 ovs_mutex_lock(&bucket->mutex);
1106 stats->n_packets += bucket->packet_count;
1107 stats->n_bytes += bucket->byte_count;
1108 stats->used = MAX(stats->used, bucket->used);
1109 stats->tcp_flags |= bucket->tcp_flags;
1110 ovs_mutex_unlock(&bucket->mutex);
1115 dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1116 const struct nlattr *mask_key,
1117 uint32_t mask_key_len, const struct flow *flow,
1121 enum odp_key_fitness fitness;
1123 fitness = odp_flow_key_to_mask(mask_key, mask_key_len, mask, flow);
1125 /* This should not happen: it indicates that
1126 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
1127 * disagree on the acceptable form of a mask. Log the problem
1128 * as an error, with enough details to enable debugging. */
1129 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1131 if (!VLOG_DROP_ERR(&rl)) {
1135 odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s,
1137 VLOG_ERR("internal error parsing flow mask %s (%s)",
1138 ds_cstr(&s), odp_key_fitness_to_string(fitness));
1145 enum mf_field_id id;
1146 /* No mask key, unwildcard everything except fields whose
1147 * prerequisities are not met. */
1148 memset(mask, 0x0, sizeof *mask);
1150 for (id = 0; id < MFF_N_IDS; ++id) {
1151 /* Skip registers and metadata. */
1152 if (!(id >= MFF_REG0 && id < MFF_REG0 + FLOW_N_REGS)
1153 && id != MFF_METADATA) {
1154 const struct mf_field *mf = mf_from_id(id);
1155 if (mf_are_prereqs_ok(mf, flow)) {
1156 mf_mask_field(mf, mask);
1162 /* Force unwildcard the in_port.
1164 * We need to do this even in the case where we unwildcard "everything"
1165 * above because "everything" only includes the 16-bit OpenFlow port number
1166 * mask->in_port.ofp_port, which only covers half of the 32-bit datapath
1167 * port number mask->in_port.odp_port. */
1168 mask->in_port.odp_port = u32_to_odp(UINT32_MAX);
1174 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1179 if (odp_flow_key_to_flow(key, key_len, flow)) {
1180 /* This should not happen: it indicates that odp_flow_key_from_flow()
1181 * and odp_flow_key_to_flow() disagree on the acceptable form of a
1182 * flow. Log the problem as an error, with enough details to enable
1184 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1186 if (!VLOG_DROP_ERR(&rl)) {
1190 odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
1191 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
1198 in_port = flow->in_port.odp_port;
1199 if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
1207 dpif_netdev_flow_get(const struct dpif *dpif,
1208 const struct nlattr *nl_key, size_t nl_key_len,
1209 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
1211 struct dp_netdev *dp = get_dp_netdev(dpif);
1212 struct dp_netdev_flow *netdev_flow;
1216 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
1221 fat_rwlock_rdlock(&dp->cls.rwlock);
1222 netdev_flow = dp_netdev_find_flow(dp, &key);
1223 fat_rwlock_unlock(&dp->cls.rwlock);
1227 get_dpif_flow_stats(netdev_flow, stats);
1231 struct dp_netdev_actions *actions;
1233 actions = dp_netdev_flow_get_actions(netdev_flow);
1234 *actionsp = ofpbuf_clone_data(actions->actions, actions->size);
1244 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *flow,
1245 const struct flow_wildcards *wc,
1246 const struct nlattr *actions,
1248 OVS_REQUIRES(dp->flow_mutex)
1250 struct dp_netdev_flow *netdev_flow;
1253 netdev_flow = xzalloc(sizeof *netdev_flow);
1254 *CONST_CAST(struct flow *, &netdev_flow->flow) = *flow;
1256 ovs_mutex_init(&netdev_flow->mutex);
1258 ovsthread_stats_init(&netdev_flow->stats);
1260 ovsrcu_set(&netdev_flow->actions,
1261 dp_netdev_actions_create(actions, actions_len));
1263 match_init(&match, flow, wc);
1264 cls_rule_init(CONST_CAST(struct cls_rule *, &netdev_flow->cr),
1265 &match, NETDEV_RULE_PRIORITY);
1266 fat_rwlock_wrlock(&dp->cls.rwlock);
1267 classifier_insert(&dp->cls,
1268 CONST_CAST(struct cls_rule *, &netdev_flow->cr));
1269 hmap_insert(&dp->flow_table,
1270 CONST_CAST(struct hmap_node *, &netdev_flow->node),
1271 flow_hash(flow, 0));
1272 fat_rwlock_unlock(&dp->cls.rwlock);
1278 clear_stats(struct dp_netdev_flow *netdev_flow)
1280 struct dp_netdev_flow_stats *bucket;
1283 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1284 ovs_mutex_lock(&bucket->mutex);
1286 bucket->packet_count = 0;
1287 bucket->byte_count = 0;
1288 bucket->tcp_flags = 0;
1289 ovs_mutex_unlock(&bucket->mutex);
1294 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
1296 struct dp_netdev *dp = get_dp_netdev(dpif);
1297 struct dp_netdev_flow *netdev_flow;
1299 struct miniflow miniflow;
1300 struct flow_wildcards wc;
1303 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &flow);
1307 error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
1308 put->mask, put->mask_len,
1313 miniflow_init(&miniflow, &flow);
1315 ovs_mutex_lock(&dp->flow_mutex);
1316 netdev_flow = dp_netdev_lookup_flow(dp, &miniflow);
1318 if (put->flags & DPIF_FP_CREATE) {
1319 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
1321 memset(put->stats, 0, sizeof *put->stats);
1323 error = dp_netdev_flow_add(dp, &flow, &wc, put->actions,
1332 if (put->flags & DPIF_FP_MODIFY
1333 && flow_equal(&flow, &netdev_flow->flow)) {
1334 struct dp_netdev_actions *new_actions;
1335 struct dp_netdev_actions *old_actions;
1337 new_actions = dp_netdev_actions_create(put->actions,
1340 old_actions = dp_netdev_flow_get_actions(netdev_flow);
1341 ovsrcu_set(&netdev_flow->actions, new_actions);
1344 get_dpif_flow_stats(netdev_flow, put->stats);
1346 if (put->flags & DPIF_FP_ZERO_STATS) {
1347 clear_stats(netdev_flow);
1350 ovsrcu_postpone(dp_netdev_actions_free, old_actions);
1351 } else if (put->flags & DPIF_FP_CREATE) {
1354 /* Overlapping flow. */
1358 ovs_mutex_unlock(&dp->flow_mutex);
1364 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
1366 struct dp_netdev *dp = get_dp_netdev(dpif);
1367 struct dp_netdev_flow *netdev_flow;
1371 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
1376 ovs_mutex_lock(&dp->flow_mutex);
1377 fat_rwlock_wrlock(&dp->cls.rwlock);
1378 netdev_flow = dp_netdev_find_flow(dp, &key);
1381 get_dpif_flow_stats(netdev_flow, del->stats);
1383 dp_netdev_remove_flow(dp, netdev_flow);
1387 fat_rwlock_unlock(&dp->cls.rwlock);
1388 ovs_mutex_unlock(&dp->flow_mutex);
1393 struct dp_netdev_flow_state {
1394 struct dp_netdev_actions *actions;
1395 struct odputil_keybuf keybuf;
1396 struct odputil_keybuf maskbuf;
1397 struct dpif_flow_stats stats;
1400 struct dp_netdev_flow_iter {
1404 struct ovs_mutex mutex;
1408 dpif_netdev_flow_dump_state_init(void **statep)
1410 struct dp_netdev_flow_state *state;
1412 *statep = state = xmalloc(sizeof *state);
1413 state->actions = NULL;
1417 dpif_netdev_flow_dump_state_uninit(void *state_)
1419 struct dp_netdev_flow_state *state = state_;
1425 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **iterp)
1427 struct dp_netdev_flow_iter *iter;
1429 *iterp = iter = xmalloc(sizeof *iter);
1433 ovs_mutex_init(&iter->mutex);
1437 /* XXX the caller must use 'actions' without quiescing */
1439 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *iter_, void *state_,
1440 const struct nlattr **key, size_t *key_len,
1441 const struct nlattr **mask, size_t *mask_len,
1442 const struct nlattr **actions, size_t *actions_len,
1443 const struct dpif_flow_stats **stats)
1445 struct dp_netdev_flow_iter *iter = iter_;
1446 struct dp_netdev_flow_state *state = state_;
1447 struct dp_netdev *dp = get_dp_netdev(dpif);
1448 struct dp_netdev_flow *netdev_flow;
1449 struct flow_wildcards wc;
1452 ovs_mutex_lock(&iter->mutex);
1453 error = iter->status;
1455 struct hmap_node *node;
1457 fat_rwlock_rdlock(&dp->cls.rwlock);
1458 node = hmap_at_position(&dp->flow_table, &iter->bucket, &iter->offset);
1460 netdev_flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
1462 fat_rwlock_unlock(&dp->cls.rwlock);
1464 iter->status = error = EOF;
1467 ovs_mutex_unlock(&iter->mutex);
1472 minimask_expand(&netdev_flow->cr.match.mask, &wc);
1477 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
1478 odp_flow_key_from_flow(&buf, &netdev_flow->flow, &wc.masks,
1479 netdev_flow->flow.in_port.odp_port);
1481 *key = ofpbuf_data(&buf);
1482 *key_len = ofpbuf_size(&buf);
1488 ofpbuf_use_stack(&buf, &state->maskbuf, sizeof state->maskbuf);
1489 odp_flow_key_from_mask(&buf, &wc.masks, &netdev_flow->flow,
1490 odp_to_u32(wc.masks.in_port.odp_port),
1493 *mask = ofpbuf_data(&buf);
1494 *mask_len = ofpbuf_size(&buf);
1497 if (actions || stats) {
1498 state->actions = NULL;
1501 state->actions = dp_netdev_flow_get_actions(netdev_flow);
1502 *actions = state->actions->actions;
1503 *actions_len = state->actions->size;
1507 get_dpif_flow_stats(netdev_flow, &state->stats);
1508 *stats = &state->stats;
1516 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *iter_)
1518 struct dp_netdev_flow_iter *iter = iter_;
1520 ovs_mutex_destroy(&iter->mutex);
1526 dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
1528 struct dp_netdev *dp = get_dp_netdev(dpif);
1529 struct pkt_metadata *md = &execute->md;
1530 struct miniflow key;
1531 uint32_t buf[FLOW_U32S];
1533 if (ofpbuf_size(execute->packet) < ETH_HEADER_LEN ||
1534 ofpbuf_size(execute->packet) > UINT16_MAX) {
1538 /* Extract flow key. */
1539 miniflow_initialize(&key, buf);
1540 miniflow_extract(execute->packet, md, &key);
1542 ovs_rwlock_rdlock(&dp->port_rwlock);
1543 dp_netdev_execute_actions(dp, &key, execute->packet, false, md,
1544 execute->actions, execute->actions_len);
1545 ovs_rwlock_unlock(&dp->port_rwlock);
1551 dp_netdev_destroy_all_queues(struct dp_netdev *dp)
1552 OVS_REQ_WRLOCK(dp->queue_rwlock)
1556 dp_netdev_purge_queues(dp);
1558 for (i = 0; i < dp->n_handlers; i++) {
1559 struct dp_netdev_queue *q = &dp->handler_queues[i];
1561 ovs_mutex_destroy(&q->mutex);
1562 seq_destroy(q->seq);
1564 free(dp->handler_queues);
1565 dp->handler_queues = NULL;
1570 dp_netdev_refresh_queues(struct dp_netdev *dp, uint32_t n_handlers)
1571 OVS_REQ_WRLOCK(dp->queue_rwlock)
1573 if (dp->n_handlers != n_handlers) {
1576 dp_netdev_destroy_all_queues(dp);
1578 dp->n_handlers = n_handlers;
1579 dp->handler_queues = xzalloc(n_handlers * sizeof *dp->handler_queues);
1581 for (i = 0; i < n_handlers; i++) {
1582 struct dp_netdev_queue *q = &dp->handler_queues[i];
1584 ovs_mutex_init(&q->mutex);
1585 q->seq = seq_create();
1591 dpif_netdev_recv_set(struct dpif *dpif, bool enable)
1593 struct dp_netdev *dp = get_dp_netdev(dpif);
1595 if ((dp->handler_queues != NULL) == enable) {
1599 fat_rwlock_wrlock(&dp->queue_rwlock);
1601 dp_netdev_destroy_all_queues(dp);
1603 dp_netdev_refresh_queues(dp, 1);
1605 fat_rwlock_unlock(&dp->queue_rwlock);
1611 dpif_netdev_handlers_set(struct dpif *dpif, uint32_t n_handlers)
1613 struct dp_netdev *dp = get_dp_netdev(dpif);
1615 fat_rwlock_wrlock(&dp->queue_rwlock);
1616 if (dp->handler_queues) {
1617 dp_netdev_refresh_queues(dp, n_handlers);
1619 fat_rwlock_unlock(&dp->queue_rwlock);
1625 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1626 uint32_t queue_id, uint32_t *priority)
1628 *priority = queue_id;
1633 dp_netdev_recv_check(const struct dp_netdev *dp, const uint32_t handler_id)
1634 OVS_REQ_RDLOCK(dp->queue_rwlock)
1636 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1638 if (!dp->handler_queues) {
1639 VLOG_WARN_RL(&rl, "receiving upcall disabled");
1643 if (handler_id >= dp->n_handlers) {
1644 VLOG_WARN_RL(&rl, "handler index out of bound");
1652 dpif_netdev_recv(struct dpif *dpif, uint32_t handler_id,
1653 struct dpif_upcall *upcall, struct ofpbuf *buf)
1655 struct dp_netdev *dp = get_dp_netdev(dpif);
1656 struct dp_netdev_queue *q;
1659 fat_rwlock_rdlock(&dp->queue_rwlock);
1661 if (!dp_netdev_recv_check(dp, handler_id)) {
1666 q = &dp->handler_queues[handler_id];
1667 ovs_mutex_lock(&q->mutex);
1668 if (q->head != q->tail) {
1669 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1671 *upcall = u->upcall;
1678 ovs_mutex_unlock(&q->mutex);
1681 fat_rwlock_unlock(&dp->queue_rwlock);
1687 dpif_netdev_recv_wait(struct dpif *dpif, uint32_t handler_id)
1689 struct dp_netdev *dp = get_dp_netdev(dpif);
1690 struct dp_netdev_queue *q;
1693 fat_rwlock_rdlock(&dp->queue_rwlock);
1695 if (!dp_netdev_recv_check(dp, handler_id)) {
1699 q = &dp->handler_queues[handler_id];
1700 ovs_mutex_lock(&q->mutex);
1701 seq = seq_read(q->seq);
1702 if (q->head != q->tail) {
1703 poll_immediate_wake();
1705 seq_wait(q->seq, seq);
1708 ovs_mutex_unlock(&q->mutex);
1711 fat_rwlock_unlock(&dp->queue_rwlock);
1715 dpif_netdev_recv_purge(struct dpif *dpif)
1717 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1719 fat_rwlock_wrlock(&dpif_netdev->dp->queue_rwlock);
1720 dp_netdev_purge_queues(dpif_netdev->dp);
1721 fat_rwlock_unlock(&dpif_netdev->dp->queue_rwlock);
1724 /* Creates and returns a new 'struct dp_netdev_actions', with a reference count
1725 * of 1, whose actions are a copy of from the 'ofpacts_len' bytes of
1727 struct dp_netdev_actions *
1728 dp_netdev_actions_create(const struct nlattr *actions, size_t size)
1730 struct dp_netdev_actions *netdev_actions;
1732 netdev_actions = xmalloc(sizeof *netdev_actions);
1733 netdev_actions->actions = xmemdup(actions, size);
1734 netdev_actions->size = size;
1736 return netdev_actions;
1739 struct dp_netdev_actions *
1740 dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow)
1742 return ovsrcu_get(struct dp_netdev_actions *, &flow->actions);
1746 dp_netdev_actions_free(struct dp_netdev_actions *actions)
1748 free(actions->actions);
1754 dp_netdev_process_rxq_port(struct dp_netdev *dp,
1755 struct dp_netdev_port *port,
1756 struct netdev_rxq *rxq)
1758 struct ofpbuf *packet[NETDEV_MAX_RX_BATCH];
1761 error = netdev_rxq_recv(rxq, packet, &c);
1763 struct pkt_metadata md = PKT_METADATA_INITIALIZER(port->port_no);
1766 for (i = 0; i < c; i++) {
1767 dp_netdev_port_input(dp, packet[i], &md);
1769 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1770 static struct vlog_rate_limit rl
1771 = VLOG_RATE_LIMIT_INIT(1, 5);
1773 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1774 netdev_get_name(port->netdev),
1775 ovs_strerror(error));
1780 dpif_netdev_run(struct dpif *dpif)
1782 struct dp_netdev_port *port;
1783 struct dp_netdev *dp = get_dp_netdev(dpif);
1785 ovs_rwlock_rdlock(&dp->port_rwlock);
1787 HMAP_FOR_EACH (port, node, &dp->ports) {
1788 if (!netdev_is_pmd(port->netdev)) {
1791 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1792 dp_netdev_process_rxq_port(dp, port, port->rxq[i]);
1797 ovs_rwlock_unlock(&dp->port_rwlock);
1801 dpif_netdev_wait(struct dpif *dpif)
1803 struct dp_netdev_port *port;
1804 struct dp_netdev *dp = get_dp_netdev(dpif);
1806 ovs_rwlock_rdlock(&dp->port_rwlock);
1808 HMAP_FOR_EACH (port, node, &dp->ports) {
1809 if (!netdev_is_pmd(port->netdev)) {
1812 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1813 netdev_rxq_wait(port->rxq[i]);
1817 ovs_rwlock_unlock(&dp->port_rwlock);
1821 struct dp_netdev_port *port;
1822 struct netdev_rxq *rx;
1826 pmd_load_queues(struct pmd_thread *f,
1827 struct rxq_poll **ppoll_list, int poll_cnt)
1829 struct dp_netdev *dp = f->dp;
1830 struct rxq_poll *poll_list = *ppoll_list;
1831 struct dp_netdev_port *port;
1836 /* Simple scheduler for netdev rx polling. */
1837 ovs_rwlock_rdlock(&dp->port_rwlock);
1838 for (i = 0; i < poll_cnt; i++) {
1839 port_unref(poll_list[i].port);
1845 HMAP_FOR_EACH (port, node, &f->dp->ports) {
1846 if (netdev_is_pmd(port->netdev)) {
1849 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1850 if ((index % dp->n_pmd_threads) == id) {
1851 poll_list = xrealloc(poll_list, sizeof *poll_list * (poll_cnt + 1));
1854 poll_list[poll_cnt].port = port;
1855 poll_list[poll_cnt].rx = port->rxq[i];
1863 ovs_rwlock_unlock(&dp->port_rwlock);
1864 *ppoll_list = poll_list;
1869 pmd_thread_main(void *f_)
1871 struct pmd_thread *f = f_;
1872 struct dp_netdev *dp = f->dp;
1873 unsigned int lc = 0;
1874 struct rxq_poll *poll_list;
1875 unsigned int port_seq;
1882 pmd_thread_setaffinity_cpu(f->id);
1884 poll_cnt = pmd_load_queues(f, &poll_list, poll_cnt);
1885 atomic_read(&f->change_seq, &port_seq);
1888 unsigned int c_port_seq;
1891 for (i = 0; i < poll_cnt; i++) {
1892 dp_netdev_process_rxq_port(dp, poll_list[i].port, poll_list[i].rx);
1898 /* TODO: need completely userspace based signaling method.
1899 * to keep this thread entirely in userspace.
1900 * For now using atomic counter. */
1902 atomic_read_explicit(&f->change_seq, &c_port_seq, memory_order_consume);
1903 if (c_port_seq != port_seq) {
1909 if (!latch_is_set(&f->dp->exit_latch)){
1913 for (i = 0; i < poll_cnt; i++) {
1914 port_unref(poll_list[i].port);
1922 dp_netdev_set_pmd_threads(struct dp_netdev *dp, int n)
1926 if (n == dp->n_pmd_threads) {
1930 /* Stop existing threads. */
1931 latch_set(&dp->exit_latch);
1932 dp_netdev_reload_pmd_threads(dp);
1933 for (i = 0; i < dp->n_pmd_threads; i++) {
1934 struct pmd_thread *f = &dp->pmd_threads[i];
1936 xpthread_join(f->thread, NULL);
1938 latch_poll(&dp->exit_latch);
1939 free(dp->pmd_threads);
1941 /* Start new threads. */
1942 dp->pmd_threads = xmalloc(n * sizeof *dp->pmd_threads);
1943 dp->n_pmd_threads = n;
1945 for (i = 0; i < n; i++) {
1946 struct pmd_thread *f = &dp->pmd_threads[i];
1950 atomic_store(&f->change_seq, 1);
1952 /* Each thread will distribute all devices rx-queues among
1954 f->thread = ovs_thread_create("pmd", pmd_thread_main, f);
1960 dp_netdev_flow_stats_new_cb(void)
1962 struct dp_netdev_flow_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1963 ovs_mutex_init(&bucket->mutex);
1968 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow,
1969 const struct ofpbuf *packet,
1970 const struct miniflow *key)
1972 uint16_t tcp_flags = miniflow_get_tcp_flags(key);
1973 long long int now = time_msec();
1974 struct dp_netdev_flow_stats *bucket;
1976 bucket = ovsthread_stats_bucket_get(&netdev_flow->stats,
1977 dp_netdev_flow_stats_new_cb);
1979 ovs_mutex_lock(&bucket->mutex);
1980 bucket->used = MAX(now, bucket->used);
1981 bucket->packet_count++;
1982 bucket->byte_count += ofpbuf_size(packet);
1983 bucket->tcp_flags |= tcp_flags;
1984 ovs_mutex_unlock(&bucket->mutex);
1988 dp_netdev_stats_new_cb(void)
1990 struct dp_netdev_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1991 ovs_mutex_init(&bucket->mutex);
1996 dp_netdev_count_packet(struct dp_netdev *dp, enum dp_stat_type type)
1998 struct dp_netdev_stats *bucket;
2000 bucket = ovsthread_stats_bucket_get(&dp->stats, dp_netdev_stats_new_cb);
2001 ovs_mutex_lock(&bucket->mutex);
2003 ovs_mutex_unlock(&bucket->mutex);
2007 dp_netdev_input(struct dp_netdev *dp, struct ofpbuf *packet,
2008 struct pkt_metadata *md)
2009 OVS_REQ_RDLOCK(dp->port_rwlock)
2011 struct dp_netdev_flow *netdev_flow;
2012 struct miniflow key;
2013 uint32_t buf[FLOW_U32S];
2015 if (ofpbuf_size(packet) < ETH_HEADER_LEN) {
2016 ofpbuf_delete(packet);
2019 miniflow_initialize(&key, buf);
2020 miniflow_extract(packet, md, &key);
2022 netdev_flow = dp_netdev_lookup_flow(dp, &key);
2024 struct dp_netdev_actions *actions;
2026 dp_netdev_flow_used(netdev_flow, packet, &key);
2028 actions = dp_netdev_flow_get_actions(netdev_flow);
2029 dp_netdev_execute_actions(dp, &key, packet, true, md,
2030 actions->actions, actions->size);
2031 dp_netdev_count_packet(dp, DP_STAT_HIT);
2032 } else if (dp->handler_queues) {
2033 dp_netdev_count_packet(dp, DP_STAT_MISS);
2034 dp_netdev_output_userspace(dp, packet,
2035 miniflow_hash_5tuple(&key, 0)
2037 DPIF_UC_MISS, &key, NULL);
2038 ofpbuf_delete(packet);
2043 dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
2044 struct pkt_metadata *md)
2045 OVS_REQ_RDLOCK(dp->port_rwlock)
2047 uint32_t *recirc_depth = recirc_depth_get();
2050 dp_netdev_input(dp, packet, md);
2054 dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *packet,
2055 int queue_no, int type, const struct miniflow *key,
2056 const struct nlattr *userdata)
2058 struct dp_netdev_queue *q;
2061 fat_rwlock_rdlock(&dp->queue_rwlock);
2062 q = &dp->handler_queues[queue_no];
2063 ovs_mutex_lock(&q->mutex);
2064 if (q->head - q->tail < MAX_QUEUE_LEN) {
2065 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
2066 struct dpif_upcall *upcall = &u->upcall;
2067 struct ofpbuf *buf = &u->buf;
2071 upcall->type = type;
2073 /* Allocate buffer big enough for everything. */
2074 buf_size = ODPUTIL_FLOW_KEY_BYTES;
2076 buf_size += NLA_ALIGN(userdata->nla_len);
2078 buf_size += ofpbuf_size(packet);
2079 ofpbuf_init(buf, buf_size);
2082 miniflow_expand(key, &flow);
2083 odp_flow_key_from_flow(buf, &flow, NULL, flow.in_port.odp_port);
2084 upcall->key = ofpbuf_data(buf);
2085 upcall->key_len = ofpbuf_size(buf);
2089 upcall->userdata = ofpbuf_put(buf, userdata,
2090 NLA_ALIGN(userdata->nla_len));
2093 ofpbuf_set_data(&upcall->packet,
2094 ofpbuf_put(buf, ofpbuf_data(packet), ofpbuf_size(packet)));
2095 ofpbuf_set_size(&upcall->packet, ofpbuf_size(packet));
2101 dp_netdev_count_packet(dp, DP_STAT_LOST);
2104 ovs_mutex_unlock(&q->mutex);
2105 fat_rwlock_unlock(&dp->queue_rwlock);
2110 struct dp_netdev_execute_aux {
2111 struct dp_netdev *dp;
2112 const struct miniflow *key;
2116 dp_execute_cb(void *aux_, struct ofpbuf *packet,
2117 struct pkt_metadata *md,
2118 const struct nlattr *a, bool may_steal)
2119 OVS_NO_THREAD_SAFETY_ANALYSIS
2121 struct dp_netdev_execute_aux *aux = aux_;
2122 int type = nl_attr_type(a);
2123 struct dp_netdev_port *p;
2124 uint32_t *depth = recirc_depth_get();
2126 switch ((enum ovs_action_attr)type) {
2127 case OVS_ACTION_ATTR_OUTPUT:
2128 p = dp_netdev_lookup_port(aux->dp, u32_to_odp(nl_attr_get_u32(a)));
2130 netdev_send(p->netdev, packet, may_steal);
2134 case OVS_ACTION_ATTR_USERSPACE: {
2135 const struct nlattr *userdata;
2137 userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
2139 dp_netdev_output_userspace(aux->dp, packet,
2140 miniflow_hash_5tuple(aux->key, 0)
2141 % aux->dp->n_handlers,
2142 DPIF_UC_ACTION, aux->key,
2146 ofpbuf_delete(packet);
2151 case OVS_ACTION_ATTR_HASH: {
2152 const struct ovs_action_hash *hash_act;
2155 hash_act = nl_attr_get(a);
2156 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
2157 /* Hash need not be symmetric, nor does it need to include
2159 hash = miniflow_hash_5tuple(aux->key, hash_act->hash_basis);
2161 hash = 1; /* 0 is not valid */
2165 VLOG_WARN("Unknown hash algorithm specified for the hash action.");
2173 case OVS_ACTION_ATTR_RECIRC:
2174 if (*depth < MAX_RECIRC_DEPTH) {
2175 struct pkt_metadata recirc_md = *md;
2176 struct ofpbuf *recirc_packet;
2178 recirc_packet = may_steal ? packet : ofpbuf_clone(packet);
2179 recirc_md.recirc_id = nl_attr_get_u32(a);
2182 dp_netdev_input(aux->dp, recirc_packet, &recirc_md);
2187 VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
2191 case OVS_ACTION_ATTR_PUSH_VLAN:
2192 case OVS_ACTION_ATTR_POP_VLAN:
2193 case OVS_ACTION_ATTR_PUSH_MPLS:
2194 case OVS_ACTION_ATTR_POP_MPLS:
2195 case OVS_ACTION_ATTR_SET:
2196 case OVS_ACTION_ATTR_SAMPLE:
2197 case OVS_ACTION_ATTR_UNSPEC:
2198 case __OVS_ACTION_ATTR_MAX:
2204 dp_netdev_execute_actions(struct dp_netdev *dp, const struct miniflow *key,
2205 struct ofpbuf *packet, bool may_steal,
2206 struct pkt_metadata *md,
2207 const struct nlattr *actions, size_t actions_len)
2209 struct dp_netdev_execute_aux aux = {dp, key};
2211 odp_execute_actions(&aux, packet, may_steal, md,
2212 actions, actions_len, dp_execute_cb);
2215 const struct dpif_class dpif_netdev_class = {
2217 dpif_netdev_enumerate,
2218 dpif_netdev_port_open_type,
2221 dpif_netdev_destroy,
2224 dpif_netdev_get_stats,
2225 dpif_netdev_port_add,
2226 dpif_netdev_port_del,
2227 dpif_netdev_port_query_by_number,
2228 dpif_netdev_port_query_by_name,
2229 NULL, /* port_get_pid */
2230 dpif_netdev_port_dump_start,
2231 dpif_netdev_port_dump_next,
2232 dpif_netdev_port_dump_done,
2233 dpif_netdev_port_poll,
2234 dpif_netdev_port_poll_wait,
2235 dpif_netdev_flow_get,
2236 dpif_netdev_flow_put,
2237 dpif_netdev_flow_del,
2238 dpif_netdev_flow_flush,
2239 dpif_netdev_flow_dump_state_init,
2240 dpif_netdev_flow_dump_start,
2241 dpif_netdev_flow_dump_next,
2243 dpif_netdev_flow_dump_done,
2244 dpif_netdev_flow_dump_state_uninit,
2245 dpif_netdev_execute,
2247 dpif_netdev_recv_set,
2248 dpif_netdev_handlers_set,
2249 dpif_netdev_queue_to_priority,
2251 dpif_netdev_recv_wait,
2252 dpif_netdev_recv_purge,
2256 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
2257 const char *argv[], void *aux OVS_UNUSED)
2259 struct dp_netdev_port *port;
2260 struct dp_netdev *dp;
2263 ovs_mutex_lock(&dp_netdev_mutex);
2264 dp = shash_find_data(&dp_netdevs, argv[1]);
2265 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
2266 ovs_mutex_unlock(&dp_netdev_mutex);
2267 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
2270 ovs_refcount_ref(&dp->ref_cnt);
2271 ovs_mutex_unlock(&dp_netdev_mutex);
2273 ovs_rwlock_wrlock(&dp->port_rwlock);
2274 if (get_port_by_name(dp, argv[2], &port)) {
2275 unixctl_command_reply_error(conn, "unknown port");
2279 port_no = u32_to_odp(atoi(argv[3]));
2280 if (!port_no || port_no == ODPP_NONE) {
2281 unixctl_command_reply_error(conn, "bad port number");
2284 if (dp_netdev_lookup_port(dp, port_no)) {
2285 unixctl_command_reply_error(conn, "port number already in use");
2288 hmap_remove(&dp->ports, &port->node);
2289 port->port_no = port_no;
2290 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
2291 seq_change(dp->port_seq);
2292 unixctl_command_reply(conn, NULL);
2295 ovs_rwlock_unlock(&dp->port_rwlock);
2296 dp_netdev_unref(dp);
2300 dpif_dummy_register__(const char *type)
2302 struct dpif_class *class;
2304 class = xmalloc(sizeof *class);
2305 *class = dpif_netdev_class;
2306 class->type = xstrdup(type);
2307 dp_register_provider(class);
2311 dpif_dummy_register(bool override)
2318 dp_enumerate_types(&types);
2319 SSET_FOR_EACH (type, &types) {
2320 if (!dp_unregister_provider(type)) {
2321 dpif_dummy_register__(type);
2324 sset_destroy(&types);
2327 dpif_dummy_register__("dummy");
2329 unixctl_command_register("dpif-dummy/change-port-number",
2330 "DP PORT NEW-NUMBER",
2331 3, 3, dpif_dummy_change_port_number, NULL);