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
72 /* Configuration parameters. */
73 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
76 enum { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
77 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
78 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
80 /* Protects against changes to 'dp_netdevs'. */
81 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
83 /* Contains all 'struct dp_netdev's. */
84 static struct shash dp_netdevs OVS_GUARDED_BY(dp_netdev_mutex)
85 = SHASH_INITIALIZER(&dp_netdevs);
87 struct dp_netdev_upcall {
88 struct dpif_upcall upcall; /* Queued upcall information. */
89 struct ofpbuf buf; /* ofpbuf instance for upcall.packet. */
92 /* A queue passing packets from a struct dp_netdev to its clients (handlers).
98 * Any access at all requires the owning 'dp_netdev''s queue_rwlock and
100 struct dp_netdev_queue {
101 struct ovs_mutex mutex;
102 struct seq *seq; /* Incremented whenever a packet is queued. */
103 struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN] OVS_GUARDED;
104 unsigned int head OVS_GUARDED;
105 unsigned int tail OVS_GUARDED;
108 /* Datapath based on the network device interface from netdev.h.
114 * Some members, marked 'const', are immutable. Accessing other members
115 * requires synchronization, as noted in more detail below.
117 * Acquisition order is, from outermost to innermost:
119 * dp_netdev_mutex (global)
126 const struct dpif_class *const class;
127 const char *const name;
128 struct ovs_refcount ref_cnt;
129 atomic_flag destroyed;
133 * Readers of 'cls' and 'flow_table' must take a 'cls->rwlock' read lock.
135 * Writers of 'cls' and 'flow_table' must take the 'flow_mutex' and then
136 * the 'cls->rwlock' write lock. (The outer 'flow_mutex' allows writers to
137 * atomically perform multiple operations on 'cls' and 'flow_table'.)
139 struct ovs_mutex flow_mutex;
140 struct classifier cls; /* Classifier. Protected by cls.rwlock. */
141 struct hmap flow_table OVS_GUARDED; /* Flow table. */
145 * 'queue_rwlock' protects the modification of 'handler_queues' and
146 * 'n_handlers'. The queue elements are protected by its
147 * 'handler_queues''s mutex. */
148 struct fat_rwlock queue_rwlock;
149 struct dp_netdev_queue *handler_queues;
154 * ovsthread_stats is internally synchronized. */
155 struct ovsthread_stats stats; /* Contains 'struct dp_netdev_stats *'. */
159 * Any lookup into 'ports' or any access to the dp_netdev_ports found
160 * through 'ports' requires taking 'port_rwlock'. */
161 struct ovs_rwlock port_rwlock;
162 struct hmap ports OVS_GUARDED;
163 struct seq *port_seq; /* Incremented whenever a port changes. */
165 /* Forwarding threads. */
166 struct latch exit_latch;
167 struct pmd_thread *pmd_threads;
168 size_t n_pmd_threads;
172 static struct dp_netdev_port *dp_netdev_lookup_port(const struct dp_netdev *dp,
174 OVS_REQ_RDLOCK(dp->port_rwlock);
177 DP_STAT_HIT, /* Packets that matched in the flow table. */
178 DP_STAT_MISS, /* Packets that did not match. */
179 DP_STAT_LOST, /* Packets not passed up to the client. */
183 /* Contained by struct dp_netdev's 'stats' member. */
184 struct dp_netdev_stats {
185 struct ovs_mutex mutex; /* Protects 'n'. */
187 /* Indexed by DP_STAT_*, protected by 'mutex'. */
188 unsigned long long int n[DP_N_STATS] OVS_GUARDED;
192 /* A port in a netdev-based datapath. */
193 struct dp_netdev_port {
194 struct hmap_node node; /* Node in dp_netdev's 'ports'. */
196 struct netdev *netdev;
197 struct netdev_saved_flags *sf;
198 struct netdev_rxq **rxq;
199 struct ovs_refcount ref_cnt;
200 char *type; /* Port type as requested by user. */
203 /* A flow in dp_netdev's 'flow_table'.
209 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
210 * its dp_netdev's classifier. The text below calls this classifier 'cls'.
215 * The thread safety rules described here for "struct dp_netdev_flow" are
216 * motivated by two goals:
218 * - Prevent threads that read members of "struct dp_netdev_flow" from
219 * reading bad data due to changes by some thread concurrently modifying
222 * - Prevent two threads making changes to members of a given "struct
223 * dp_netdev_flow" from interfering with each other.
229 * A flow 'flow' may be accessed without a risk of being freed by code that
230 * holds a read-lock or write-lock on 'cls->rwlock' or that owns a reference to
231 * 'flow->ref_cnt' (or both). Code that needs to hold onto a flow for a while
232 * should take 'cls->rwlock', find the flow it needs, increment 'flow->ref_cnt'
233 * with dpif_netdev_flow_ref(), and drop 'cls->rwlock'.
235 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
236 * flow from being deleted from 'cls' (that's 'cls->rwlock') and it doesn't
237 * protect members of 'flow' from modification (that's 'flow->mutex').
239 * 'flow->mutex' protects the members of 'flow' from modification. It doesn't
240 * protect the flow from being deleted from 'cls' (that's 'cls->rwlock') and it
241 * doesn't prevent the flow from being freed (that's 'flow->ref_cnt').
243 * Some members, marked 'const', are immutable. Accessing other members
244 * requires synchronization, as noted in more detail below.
246 struct dp_netdev_flow {
247 /* Packet classification. */
248 const struct cls_rule cr; /* In owning dp_netdev's 'cls'. */
250 /* Hash table index by unmasked flow. */
251 const struct hmap_node node; /* In owning dp_netdev's 'flow_table'. */
252 const struct flow flow; /* The flow that created this entry. */
254 /* Protects members marked OVS_GUARDED.
256 * Acquire after datapath's flow_mutex. */
257 struct ovs_mutex mutex OVS_ACQ_AFTER(dp_netdev_mutex);
261 * Reading or writing these members requires 'mutex'. */
262 struct ovsthread_stats stats; /* Contains "struct dp_netdev_flow_stats". */
266 * Reading 'actions' requires 'mutex'.
267 * Writing 'actions' requires 'mutex' and (to allow for transactions) the
268 * datapath's flow_mutex. */
269 OVSRCU_TYPE(struct dp_netdev_actions *) actions;
272 static void dp_netdev_flow_free(struct dp_netdev_flow *);
274 /* Contained by struct dp_netdev_flow's 'stats' member. */
275 struct dp_netdev_flow_stats {
276 struct ovs_mutex mutex; /* Guards all the other members. */
278 long long int used OVS_GUARDED; /* Last used time, in monotonic msecs. */
279 long long int packet_count OVS_GUARDED; /* Number of packets matched. */
280 long long int byte_count OVS_GUARDED; /* Number of bytes matched. */
281 uint16_t tcp_flags OVS_GUARDED; /* Bitwise-OR of seen tcp_flags values. */
284 /* A set of datapath actions within a "struct dp_netdev_flow".
290 * A struct dp_netdev_actions 'actions' may be accessed without a risk of being
291 * freed by code that holds a read-lock or write-lock on 'flow->mutex' (where
292 * 'flow' is the dp_netdev_flow for which 'flow->actions == actions') or that
293 * owns a reference to 'actions->ref_cnt' (or both). */
294 struct dp_netdev_actions {
295 /* These members are immutable: they do not change during the struct's
297 struct nlattr *actions; /* Sequence of OVS_ACTION_ATTR_* attributes. */
298 unsigned int size; /* Size of 'actions', in bytes. */
301 struct dp_netdev_actions *dp_netdev_actions_create(const struct nlattr *,
303 struct dp_netdev_actions *dp_netdev_flow_get_actions(
304 const struct dp_netdev_flow *);
305 static void dp_netdev_actions_free(struct dp_netdev_actions *);
307 /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
308 * the performance overhead of interrupt processing. Therefore netdev can
309 * not implement rx-wait for these devices. dpif-netdev needs to poll
310 * these device to check for recv buffer. pmd-thread does polling for
311 * devices assigned to itself thread.
313 * DPDK used PMD for accessing NIC.
315 * A thread that receives packets from PMD ports, looks them up in the flow
316 * table, and executes the actions it finds.
319 struct dp_netdev *dp;
322 atomic_uint change_seq;
326 /* Interface to netdev-based datapath. */
329 struct dp_netdev *dp;
330 uint64_t last_port_seq;
333 static int get_port_by_number(struct dp_netdev *dp, odp_port_t port_no,
334 struct dp_netdev_port **portp)
335 OVS_REQ_RDLOCK(dp->port_rwlock);
336 static int get_port_by_name(struct dp_netdev *dp, const char *devname,
337 struct dp_netdev_port **portp)
338 OVS_REQ_RDLOCK(dp->port_rwlock);
339 static void dp_netdev_free(struct dp_netdev *)
340 OVS_REQUIRES(dp_netdev_mutex);
341 static void dp_netdev_flow_flush(struct dp_netdev *);
342 static int do_add_port(struct dp_netdev *dp, const char *devname,
343 const char *type, odp_port_t port_no)
344 OVS_REQ_WRLOCK(dp->port_rwlock);
345 static int do_del_port(struct dp_netdev *dp, odp_port_t port_no)
346 OVS_REQ_WRLOCK(dp->port_rwlock);
347 static void dp_netdev_destroy_all_queues(struct dp_netdev *dp)
348 OVS_REQ_WRLOCK(dp->queue_rwlock);
349 static int dpif_netdev_open(const struct dpif_class *, const char *name,
350 bool create, struct dpif **);
351 static int dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *,
352 int queue_no, int type,
354 const struct nlattr *userdata);
355 static void dp_netdev_execute_actions(struct dp_netdev *dp,
356 const struct flow *, struct ofpbuf *, bool may_steal,
357 struct pkt_metadata *,
358 const struct nlattr *actions,
360 static void dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
361 struct pkt_metadata *);
363 static void dp_netdev_set_pmd_threads(struct dp_netdev *, int n);
365 static struct dpif_netdev *
366 dpif_netdev_cast(const struct dpif *dpif)
368 ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
369 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
372 static struct dp_netdev *
373 get_dp_netdev(const struct dpif *dpif)
375 return dpif_netdev_cast(dpif)->dp;
379 dpif_netdev_enumerate(struct sset *all_dps)
381 struct shash_node *node;
383 ovs_mutex_lock(&dp_netdev_mutex);
384 SHASH_FOR_EACH(node, &dp_netdevs) {
385 sset_add(all_dps, node->name);
387 ovs_mutex_unlock(&dp_netdev_mutex);
393 dpif_netdev_class_is_dummy(const struct dpif_class *class)
395 return class != &dpif_netdev_class;
399 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
401 return strcmp(type, "internal") ? type
402 : dpif_netdev_class_is_dummy(class) ? "dummy"
407 create_dpif_netdev(struct dp_netdev *dp)
409 uint16_t netflow_id = hash_string(dp->name, 0);
410 struct dpif_netdev *dpif;
412 ovs_refcount_ref(&dp->ref_cnt);
414 dpif = xmalloc(sizeof *dpif);
415 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
417 dpif->last_port_seq = seq_read(dp->port_seq);
422 /* Choose an unused, non-zero port number and return it on success.
423 * Return ODPP_NONE on failure. */
425 choose_port(struct dp_netdev *dp, const char *name)
426 OVS_REQ_RDLOCK(dp->port_rwlock)
430 if (dp->class != &dpif_netdev_class) {
434 /* If the port name begins with "br", start the number search at
435 * 100 to make writing tests easier. */
436 if (!strncmp(name, "br", 2)) {
440 /* If the port name contains a number, try to assign that port number.
441 * This can make writing unit tests easier because port numbers are
443 for (p = name; *p != '\0'; p++) {
444 if (isdigit((unsigned char) *p)) {
445 port_no = start_no + strtol(p, NULL, 10);
446 if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE)
447 && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
448 return u32_to_odp(port_no);
455 for (port_no = 1; port_no <= UINT16_MAX; port_no++) {
456 if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
457 return u32_to_odp(port_no);
465 create_dp_netdev(const char *name, const struct dpif_class *class,
466 struct dp_netdev **dpp)
467 OVS_REQUIRES(dp_netdev_mutex)
469 struct dp_netdev *dp;
472 dp = xzalloc(sizeof *dp);
473 shash_add(&dp_netdevs, name, dp);
475 *CONST_CAST(const struct dpif_class **, &dp->class) = class;
476 *CONST_CAST(const char **, &dp->name) = xstrdup(name);
477 ovs_refcount_init(&dp->ref_cnt);
478 atomic_flag_clear(&dp->destroyed);
480 ovs_mutex_init(&dp->flow_mutex);
481 classifier_init(&dp->cls, NULL);
482 hmap_init(&dp->flow_table);
484 fat_rwlock_init(&dp->queue_rwlock);
486 ovsthread_stats_init(&dp->stats);
488 ovs_rwlock_init(&dp->port_rwlock);
489 hmap_init(&dp->ports);
490 dp->port_seq = seq_create();
491 latch_init(&dp->exit_latch);
493 ovs_rwlock_wrlock(&dp->port_rwlock);
494 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
495 ovs_rwlock_unlock(&dp->port_rwlock);
506 dpif_netdev_open(const struct dpif_class *class, const char *name,
507 bool create, struct dpif **dpifp)
509 struct dp_netdev *dp;
512 ovs_mutex_lock(&dp_netdev_mutex);
513 dp = shash_find_data(&dp_netdevs, name);
515 error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
517 error = (dp->class != class ? EINVAL
522 *dpifp = create_dpif_netdev(dp);
524 ovs_mutex_unlock(&dp_netdev_mutex);
530 dp_netdev_purge_queues(struct dp_netdev *dp)
531 OVS_REQ_WRLOCK(dp->queue_rwlock)
535 for (i = 0; i < dp->n_handlers; i++) {
536 struct dp_netdev_queue *q = &dp->handler_queues[i];
538 ovs_mutex_lock(&q->mutex);
539 while (q->tail != q->head) {
540 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
541 ofpbuf_uninit(&u->upcall.packet);
542 ofpbuf_uninit(&u->buf);
544 ovs_mutex_unlock(&q->mutex);
548 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
549 * through the 'dp_netdevs' shash while freeing 'dp'. */
551 dp_netdev_free(struct dp_netdev *dp)
552 OVS_REQUIRES(dp_netdev_mutex)
554 struct dp_netdev_port *port, *next;
555 struct dp_netdev_stats *bucket;
558 shash_find_and_delete(&dp_netdevs, dp->name);
560 dp_netdev_set_pmd_threads(dp, 0);
561 free(dp->pmd_threads);
563 dp_netdev_flow_flush(dp);
564 ovs_rwlock_wrlock(&dp->port_rwlock);
565 HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) {
566 do_del_port(dp, port->port_no);
568 ovs_rwlock_unlock(&dp->port_rwlock);
570 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
571 ovs_mutex_destroy(&bucket->mutex);
572 free_cacheline(bucket);
574 ovsthread_stats_destroy(&dp->stats);
576 fat_rwlock_wrlock(&dp->queue_rwlock);
577 dp_netdev_destroy_all_queues(dp);
578 fat_rwlock_unlock(&dp->queue_rwlock);
580 fat_rwlock_destroy(&dp->queue_rwlock);
582 classifier_destroy(&dp->cls);
583 hmap_destroy(&dp->flow_table);
584 ovs_mutex_destroy(&dp->flow_mutex);
585 seq_destroy(dp->port_seq);
586 hmap_destroy(&dp->ports);
587 latch_destroy(&dp->exit_latch);
588 free(CONST_CAST(char *, dp->name));
593 dp_netdev_unref(struct dp_netdev *dp)
596 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
597 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
598 ovs_mutex_lock(&dp_netdev_mutex);
599 if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
602 ovs_mutex_unlock(&dp_netdev_mutex);
607 dpif_netdev_close(struct dpif *dpif)
609 struct dp_netdev *dp = get_dp_netdev(dpif);
616 dpif_netdev_destroy(struct dpif *dpif)
618 struct dp_netdev *dp = get_dp_netdev(dpif);
620 if (!atomic_flag_test_and_set(&dp->destroyed)) {
621 if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
622 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
631 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
633 struct dp_netdev *dp = get_dp_netdev(dpif);
634 struct dp_netdev_stats *bucket;
637 fat_rwlock_rdlock(&dp->cls.rwlock);
638 stats->n_flows = hmap_count(&dp->flow_table);
639 fat_rwlock_unlock(&dp->cls.rwlock);
641 stats->n_hit = stats->n_missed = stats->n_lost = 0;
642 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
643 ovs_mutex_lock(&bucket->mutex);
644 stats->n_hit += bucket->n[DP_STAT_HIT];
645 stats->n_missed += bucket->n[DP_STAT_MISS];
646 stats->n_lost += bucket->n[DP_STAT_LOST];
647 ovs_mutex_unlock(&bucket->mutex);
649 stats->n_masks = UINT32_MAX;
650 stats->n_mask_hit = UINT64_MAX;
656 dp_netdev_reload_pmd_threads(struct dp_netdev *dp)
660 for (i = 0; i < dp->n_pmd_threads; i++) {
661 struct pmd_thread *f = &dp->pmd_threads[i];
664 atomic_add(&f->change_seq, 1, &id);
669 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
671 OVS_REQ_WRLOCK(dp->port_rwlock)
673 struct netdev_saved_flags *sf;
674 struct dp_netdev_port *port;
675 struct netdev *netdev;
676 enum netdev_flags flags;
677 const char *open_type;
681 /* XXX reject devices already in some dp_netdev. */
683 /* Open and validate network device. */
684 open_type = dpif_netdev_port_open_type(dp->class, type);
685 error = netdev_open(devname, open_type, &netdev);
689 /* XXX reject non-Ethernet devices */
691 netdev_get_flags(netdev, &flags);
692 if (flags & NETDEV_LOOPBACK) {
693 VLOG_ERR("%s: cannot add a loopback device", devname);
694 netdev_close(netdev);
698 port = xzalloc(sizeof *port);
699 port->port_no = port_no;
700 port->netdev = netdev;
701 port->rxq = xmalloc(sizeof *port->rxq * netdev_n_rxq(netdev));
702 port->type = xstrdup(type);
703 for (i = 0; i < netdev_n_rxq(netdev); i++) {
704 error = netdev_rxq_open(netdev, &port->rxq[i], i);
706 && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
707 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
708 devname, ovs_strerror(errno));
709 netdev_close(netdev);
714 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
716 for (i = 0; i < netdev_n_rxq(netdev); i++) {
717 netdev_rxq_close(port->rxq[i]);
719 netdev_close(netdev);
726 if (netdev_is_pmd(netdev)) {
728 dp_netdev_set_pmd_threads(dp, NR_THREADS);
729 dp_netdev_reload_pmd_threads(dp);
731 ovs_refcount_init(&port->ref_cnt);
733 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
734 seq_change(dp->port_seq);
740 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
741 odp_port_t *port_nop)
743 struct dp_netdev *dp = get_dp_netdev(dpif);
744 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
745 const char *dpif_port;
749 ovs_rwlock_wrlock(&dp->port_rwlock);
750 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
751 if (*port_nop != ODPP_NONE) {
753 error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0;
755 port_no = choose_port(dp, dpif_port);
756 error = port_no == ODPP_NONE ? EFBIG : 0;
760 error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
762 ovs_rwlock_unlock(&dp->port_rwlock);
768 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
770 struct dp_netdev *dp = get_dp_netdev(dpif);
773 ovs_rwlock_wrlock(&dp->port_rwlock);
774 error = port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
775 ovs_rwlock_unlock(&dp->port_rwlock);
781 is_valid_port_number(odp_port_t port_no)
783 return port_no != ODPP_NONE;
786 static struct dp_netdev_port *
787 dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no)
788 OVS_REQ_RDLOCK(dp->port_rwlock)
790 struct dp_netdev_port *port;
792 HMAP_FOR_EACH_IN_BUCKET (port, node, hash_int(odp_to_u32(port_no), 0),
794 if (port->port_no == port_no) {
802 get_port_by_number(struct dp_netdev *dp,
803 odp_port_t port_no, struct dp_netdev_port **portp)
804 OVS_REQ_RDLOCK(dp->port_rwlock)
806 if (!is_valid_port_number(port_no)) {
810 *portp = dp_netdev_lookup_port(dp, port_no);
811 return *portp ? 0 : ENOENT;
816 port_ref(struct dp_netdev_port *port)
819 ovs_refcount_ref(&port->ref_cnt);
824 port_unref(struct dp_netdev_port *port)
826 if (port && ovs_refcount_unref(&port->ref_cnt) == 1) {
829 netdev_close(port->netdev);
830 netdev_restore_flags(port->sf);
832 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
833 netdev_rxq_close(port->rxq[i]);
841 get_port_by_name(struct dp_netdev *dp,
842 const char *devname, struct dp_netdev_port **portp)
843 OVS_REQ_RDLOCK(dp->port_rwlock)
845 struct dp_netdev_port *port;
847 HMAP_FOR_EACH (port, node, &dp->ports) {
848 if (!strcmp(netdev_get_name(port->netdev), devname)) {
857 do_del_port(struct dp_netdev *dp, odp_port_t port_no)
858 OVS_REQ_WRLOCK(dp->port_rwlock)
860 struct dp_netdev_port *port;
863 error = get_port_by_number(dp, port_no, &port);
868 hmap_remove(&dp->ports, &port->node);
869 seq_change(dp->port_seq);
870 if (netdev_is_pmd(port->netdev)) {
871 dp_netdev_reload_pmd_threads(dp);
879 answer_port_query(const struct dp_netdev_port *port,
880 struct dpif_port *dpif_port)
882 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
883 dpif_port->type = xstrdup(port->type);
884 dpif_port->port_no = port->port_no;
888 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
889 struct dpif_port *dpif_port)
891 struct dp_netdev *dp = get_dp_netdev(dpif);
892 struct dp_netdev_port *port;
895 ovs_rwlock_rdlock(&dp->port_rwlock);
896 error = get_port_by_number(dp, port_no, &port);
897 if (!error && dpif_port) {
898 answer_port_query(port, dpif_port);
900 ovs_rwlock_unlock(&dp->port_rwlock);
906 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
907 struct dpif_port *dpif_port)
909 struct dp_netdev *dp = get_dp_netdev(dpif);
910 struct dp_netdev_port *port;
913 ovs_rwlock_rdlock(&dp->port_rwlock);
914 error = get_port_by_name(dp, devname, &port);
915 if (!error && dpif_port) {
916 answer_port_query(port, dpif_port);
918 ovs_rwlock_unlock(&dp->port_rwlock);
924 dp_netdev_flow_free(struct dp_netdev_flow *flow)
926 struct dp_netdev_flow_stats *bucket;
929 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &flow->stats) {
930 ovs_mutex_destroy(&bucket->mutex);
931 free_cacheline(bucket);
933 ovsthread_stats_destroy(&flow->stats);
935 cls_rule_destroy(CONST_CAST(struct cls_rule *, &flow->cr));
936 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow));
937 ovs_mutex_destroy(&flow->mutex);
942 dp_netdev_remove_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
943 OVS_REQ_WRLOCK(dp->cls.rwlock)
944 OVS_REQUIRES(dp->flow_mutex)
946 struct cls_rule *cr = CONST_CAST(struct cls_rule *, &flow->cr);
947 struct hmap_node *node = CONST_CAST(struct hmap_node *, &flow->node);
949 classifier_remove(&dp->cls, cr);
950 hmap_remove(&dp->flow_table, node);
951 ovsrcu_postpone(dp_netdev_flow_free, flow);
955 dp_netdev_flow_flush(struct dp_netdev *dp)
957 struct dp_netdev_flow *netdev_flow, *next;
959 ovs_mutex_lock(&dp->flow_mutex);
960 fat_rwlock_wrlock(&dp->cls.rwlock);
961 HMAP_FOR_EACH_SAFE (netdev_flow, next, node, &dp->flow_table) {
962 dp_netdev_remove_flow(dp, netdev_flow);
964 fat_rwlock_unlock(&dp->cls.rwlock);
965 ovs_mutex_unlock(&dp->flow_mutex);
969 dpif_netdev_flow_flush(struct dpif *dpif)
971 struct dp_netdev *dp = get_dp_netdev(dpif);
973 dp_netdev_flow_flush(dp);
977 struct dp_netdev_port_state {
984 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
986 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
991 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
992 struct dpif_port *dpif_port)
994 struct dp_netdev_port_state *state = state_;
995 struct dp_netdev *dp = get_dp_netdev(dpif);
996 struct hmap_node *node;
999 ovs_rwlock_rdlock(&dp->port_rwlock);
1000 node = hmap_at_position(&dp->ports, &state->bucket, &state->offset);
1002 struct dp_netdev_port *port;
1004 port = CONTAINER_OF(node, struct dp_netdev_port, node);
1007 state->name = xstrdup(netdev_get_name(port->netdev));
1008 dpif_port->name = state->name;
1009 dpif_port->type = port->type;
1010 dpif_port->port_no = port->port_no;
1016 ovs_rwlock_unlock(&dp->port_rwlock);
1022 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1024 struct dp_netdev_port_state *state = state_;
1031 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
1033 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
1034 uint64_t new_port_seq;
1037 new_port_seq = seq_read(dpif->dp->port_seq);
1038 if (dpif->last_port_seq != new_port_seq) {
1039 dpif->last_port_seq = new_port_seq;
1049 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
1051 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
1053 seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
1056 static struct dp_netdev_flow *
1057 dp_netdev_flow_cast(const struct cls_rule *cr)
1059 return cr ? CONTAINER_OF(cr, struct dp_netdev_flow, cr) : NULL;
1062 static struct dp_netdev_flow *
1063 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *flow)
1064 OVS_EXCLUDED(dp->cls.rwlock)
1066 struct dp_netdev_flow *netdev_flow;
1068 fat_rwlock_rdlock(&dp->cls.rwlock);
1069 netdev_flow = dp_netdev_flow_cast(classifier_lookup(&dp->cls, flow, NULL));
1070 fat_rwlock_unlock(&dp->cls.rwlock);
1075 static struct dp_netdev_flow *
1076 dp_netdev_find_flow(const struct dp_netdev *dp, const struct flow *flow)
1077 OVS_REQ_RDLOCK(dp->cls.rwlock)
1079 struct dp_netdev_flow *netdev_flow;
1081 HMAP_FOR_EACH_WITH_HASH (netdev_flow, node, flow_hash(flow, 0),
1083 if (flow_equal(&netdev_flow->flow, flow)) {
1092 get_dpif_flow_stats(struct dp_netdev_flow *netdev_flow,
1093 struct dpif_flow_stats *stats)
1095 struct dp_netdev_flow_stats *bucket;
1098 memset(stats, 0, sizeof *stats);
1099 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1100 ovs_mutex_lock(&bucket->mutex);
1101 stats->n_packets += bucket->packet_count;
1102 stats->n_bytes += bucket->byte_count;
1103 stats->used = MAX(stats->used, bucket->used);
1104 stats->tcp_flags |= bucket->tcp_flags;
1105 ovs_mutex_unlock(&bucket->mutex);
1110 dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1111 const struct nlattr *mask_key,
1112 uint32_t mask_key_len, const struct flow *flow,
1116 enum odp_key_fitness fitness;
1118 fitness = odp_flow_key_to_mask(mask_key, mask_key_len, mask, flow);
1120 /* This should not happen: it indicates that
1121 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
1122 * disagree on the acceptable form of a mask. Log the problem
1123 * as an error, with enough details to enable debugging. */
1124 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1126 if (!VLOG_DROP_ERR(&rl)) {
1130 odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s,
1132 VLOG_ERR("internal error parsing flow mask %s (%s)",
1133 ds_cstr(&s), odp_key_fitness_to_string(fitness));
1140 enum mf_field_id id;
1141 /* No mask key, unwildcard everything except fields whose
1142 * prerequisities are not met. */
1143 memset(mask, 0x0, sizeof *mask);
1145 for (id = 0; id < MFF_N_IDS; ++id) {
1146 /* Skip registers and metadata. */
1147 if (!(id >= MFF_REG0 && id < MFF_REG0 + FLOW_N_REGS)
1148 && id != MFF_METADATA) {
1149 const struct mf_field *mf = mf_from_id(id);
1150 if (mf_are_prereqs_ok(mf, flow)) {
1151 mf_mask_field(mf, mask);
1157 /* Force unwildcard the in_port.
1159 * We need to do this even in the case where we unwildcard "everything"
1160 * above because "everything" only includes the 16-bit OpenFlow port number
1161 * mask->in_port.ofp_port, which only covers half of the 32-bit datapath
1162 * port number mask->in_port.odp_port. */
1163 mask->in_port.odp_port = u32_to_odp(UINT32_MAX);
1169 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1174 if (odp_flow_key_to_flow(key, key_len, flow)) {
1175 /* This should not happen: it indicates that odp_flow_key_from_flow()
1176 * and odp_flow_key_to_flow() disagree on the acceptable form of a
1177 * flow. Log the problem as an error, with enough details to enable
1179 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1181 if (!VLOG_DROP_ERR(&rl)) {
1185 odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
1186 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
1193 in_port = flow->in_port.odp_port;
1194 if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
1202 dpif_netdev_flow_get(const struct dpif *dpif,
1203 const struct nlattr *nl_key, size_t nl_key_len,
1204 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
1206 struct dp_netdev *dp = get_dp_netdev(dpif);
1207 struct dp_netdev_flow *netdev_flow;
1211 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
1216 fat_rwlock_rdlock(&dp->cls.rwlock);
1217 netdev_flow = dp_netdev_find_flow(dp, &key);
1218 fat_rwlock_unlock(&dp->cls.rwlock);
1222 get_dpif_flow_stats(netdev_flow, stats);
1226 struct dp_netdev_actions *actions;
1228 actions = dp_netdev_flow_get_actions(netdev_flow);
1229 *actionsp = ofpbuf_clone_data(actions->actions, actions->size);
1239 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *flow,
1240 const struct flow_wildcards *wc,
1241 const struct nlattr *actions,
1243 OVS_REQUIRES(dp->flow_mutex)
1245 struct dp_netdev_flow *netdev_flow;
1248 netdev_flow = xzalloc(sizeof *netdev_flow);
1249 *CONST_CAST(struct flow *, &netdev_flow->flow) = *flow;
1251 ovs_mutex_init(&netdev_flow->mutex);
1253 ovsthread_stats_init(&netdev_flow->stats);
1255 ovsrcu_set(&netdev_flow->actions,
1256 dp_netdev_actions_create(actions, actions_len));
1258 match_init(&match, flow, wc);
1259 cls_rule_init(CONST_CAST(struct cls_rule *, &netdev_flow->cr),
1260 &match, NETDEV_RULE_PRIORITY);
1261 fat_rwlock_wrlock(&dp->cls.rwlock);
1262 classifier_insert(&dp->cls,
1263 CONST_CAST(struct cls_rule *, &netdev_flow->cr));
1264 hmap_insert(&dp->flow_table,
1265 CONST_CAST(struct hmap_node *, &netdev_flow->node),
1266 flow_hash(flow, 0));
1267 fat_rwlock_unlock(&dp->cls.rwlock);
1273 clear_stats(struct dp_netdev_flow *netdev_flow)
1275 struct dp_netdev_flow_stats *bucket;
1278 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1279 ovs_mutex_lock(&bucket->mutex);
1281 bucket->packet_count = 0;
1282 bucket->byte_count = 0;
1283 bucket->tcp_flags = 0;
1284 ovs_mutex_unlock(&bucket->mutex);
1289 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
1291 struct dp_netdev *dp = get_dp_netdev(dpif);
1292 struct dp_netdev_flow *netdev_flow;
1294 struct flow_wildcards wc;
1297 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &flow);
1301 error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
1302 put->mask, put->mask_len,
1308 ovs_mutex_lock(&dp->flow_mutex);
1309 netdev_flow = dp_netdev_lookup_flow(dp, &flow);
1311 if (put->flags & DPIF_FP_CREATE) {
1312 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
1314 memset(put->stats, 0, sizeof *put->stats);
1316 error = dp_netdev_flow_add(dp, &flow, &wc, put->actions,
1325 if (put->flags & DPIF_FP_MODIFY
1326 && flow_equal(&flow, &netdev_flow->flow)) {
1327 struct dp_netdev_actions *new_actions;
1328 struct dp_netdev_actions *old_actions;
1330 new_actions = dp_netdev_actions_create(put->actions,
1333 old_actions = dp_netdev_flow_get_actions(netdev_flow);
1334 ovsrcu_set(&netdev_flow->actions, new_actions);
1337 get_dpif_flow_stats(netdev_flow, put->stats);
1339 if (put->flags & DPIF_FP_ZERO_STATS) {
1340 clear_stats(netdev_flow);
1343 ovsrcu_postpone(dp_netdev_actions_free, old_actions);
1344 } else if (put->flags & DPIF_FP_CREATE) {
1347 /* Overlapping flow. */
1351 ovs_mutex_unlock(&dp->flow_mutex);
1357 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
1359 struct dp_netdev *dp = get_dp_netdev(dpif);
1360 struct dp_netdev_flow *netdev_flow;
1364 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
1369 ovs_mutex_lock(&dp->flow_mutex);
1370 fat_rwlock_wrlock(&dp->cls.rwlock);
1371 netdev_flow = dp_netdev_find_flow(dp, &key);
1374 get_dpif_flow_stats(netdev_flow, del->stats);
1376 dp_netdev_remove_flow(dp, netdev_flow);
1380 fat_rwlock_unlock(&dp->cls.rwlock);
1381 ovs_mutex_unlock(&dp->flow_mutex);
1386 struct dp_netdev_flow_state {
1387 struct dp_netdev_actions *actions;
1388 struct odputil_keybuf keybuf;
1389 struct odputil_keybuf maskbuf;
1390 struct dpif_flow_stats stats;
1393 struct dp_netdev_flow_iter {
1397 struct ovs_mutex mutex;
1401 dpif_netdev_flow_dump_state_init(void **statep)
1403 struct dp_netdev_flow_state *state;
1405 *statep = state = xmalloc(sizeof *state);
1406 state->actions = NULL;
1410 dpif_netdev_flow_dump_state_uninit(void *state_)
1412 struct dp_netdev_flow_state *state = state_;
1418 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **iterp)
1420 struct dp_netdev_flow_iter *iter;
1422 *iterp = iter = xmalloc(sizeof *iter);
1426 ovs_mutex_init(&iter->mutex);
1430 /* XXX the caller must use 'actions' without quiescing */
1432 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *iter_, void *state_,
1433 const struct nlattr **key, size_t *key_len,
1434 const struct nlattr **mask, size_t *mask_len,
1435 const struct nlattr **actions, size_t *actions_len,
1436 const struct dpif_flow_stats **stats)
1438 struct dp_netdev_flow_iter *iter = iter_;
1439 struct dp_netdev_flow_state *state = state_;
1440 struct dp_netdev *dp = get_dp_netdev(dpif);
1441 struct dp_netdev_flow *netdev_flow;
1444 ovs_mutex_lock(&iter->mutex);
1445 error = iter->status;
1447 struct hmap_node *node;
1449 fat_rwlock_rdlock(&dp->cls.rwlock);
1450 node = hmap_at_position(&dp->flow_table, &iter->bucket, &iter->offset);
1452 netdev_flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
1454 fat_rwlock_unlock(&dp->cls.rwlock);
1456 iter->status = error = EOF;
1459 ovs_mutex_unlock(&iter->mutex);
1467 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
1468 odp_flow_key_from_flow(&buf, &netdev_flow->flow,
1469 netdev_flow->flow.in_port.odp_port);
1471 *key = ofpbuf_data(&buf);
1472 *key_len = ofpbuf_size(&buf);
1477 struct flow_wildcards wc;
1479 ofpbuf_use_stack(&buf, &state->maskbuf, sizeof state->maskbuf);
1480 minimask_expand(&netdev_flow->cr.match.mask, &wc);
1481 odp_flow_key_from_mask(&buf, &wc.masks, &netdev_flow->flow,
1482 odp_to_u32(wc.masks.in_port.odp_port),
1485 *mask = ofpbuf_data(&buf);
1486 *mask_len = ofpbuf_size(&buf);
1489 if (actions || stats) {
1490 state->actions = NULL;
1493 state->actions = dp_netdev_flow_get_actions(netdev_flow);
1494 *actions = state->actions->actions;
1495 *actions_len = state->actions->size;
1499 get_dpif_flow_stats(netdev_flow, &state->stats);
1500 *stats = &state->stats;
1508 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *iter_)
1510 struct dp_netdev_flow_iter *iter = iter_;
1512 ovs_mutex_destroy(&iter->mutex);
1518 dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
1520 struct dp_netdev *dp = get_dp_netdev(dpif);
1521 struct pkt_metadata *md = &execute->md;
1524 if (ofpbuf_size(execute->packet) < ETH_HEADER_LEN ||
1525 ofpbuf_size(execute->packet) > UINT16_MAX) {
1529 /* Extract flow key. */
1530 flow_extract(execute->packet, md, &key);
1532 ovs_rwlock_rdlock(&dp->port_rwlock);
1533 dp_netdev_execute_actions(dp, &key, execute->packet, false, md,
1534 execute->actions, execute->actions_len);
1535 ovs_rwlock_unlock(&dp->port_rwlock);
1541 dp_netdev_destroy_all_queues(struct dp_netdev *dp)
1542 OVS_REQ_WRLOCK(dp->queue_rwlock)
1546 dp_netdev_purge_queues(dp);
1548 for (i = 0; i < dp->n_handlers; i++) {
1549 struct dp_netdev_queue *q = &dp->handler_queues[i];
1551 ovs_mutex_destroy(&q->mutex);
1552 seq_destroy(q->seq);
1554 free(dp->handler_queues);
1555 dp->handler_queues = NULL;
1560 dp_netdev_refresh_queues(struct dp_netdev *dp, uint32_t n_handlers)
1561 OVS_REQ_WRLOCK(dp->queue_rwlock)
1563 if (dp->n_handlers != n_handlers) {
1566 dp_netdev_destroy_all_queues(dp);
1568 dp->n_handlers = n_handlers;
1569 dp->handler_queues = xzalloc(n_handlers * sizeof *dp->handler_queues);
1571 for (i = 0; i < n_handlers; i++) {
1572 struct dp_netdev_queue *q = &dp->handler_queues[i];
1574 ovs_mutex_init(&q->mutex);
1575 q->seq = seq_create();
1581 dpif_netdev_recv_set(struct dpif *dpif, bool enable)
1583 struct dp_netdev *dp = get_dp_netdev(dpif);
1585 if ((dp->handler_queues != NULL) == enable) {
1589 fat_rwlock_wrlock(&dp->queue_rwlock);
1591 dp_netdev_destroy_all_queues(dp);
1593 dp_netdev_refresh_queues(dp, 1);
1595 fat_rwlock_unlock(&dp->queue_rwlock);
1601 dpif_netdev_handlers_set(struct dpif *dpif, uint32_t n_handlers)
1603 struct dp_netdev *dp = get_dp_netdev(dpif);
1605 fat_rwlock_wrlock(&dp->queue_rwlock);
1606 if (dp->handler_queues) {
1607 dp_netdev_refresh_queues(dp, n_handlers);
1609 fat_rwlock_unlock(&dp->queue_rwlock);
1615 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1616 uint32_t queue_id, uint32_t *priority)
1618 *priority = queue_id;
1623 dp_netdev_recv_check(const struct dp_netdev *dp, const uint32_t handler_id)
1624 OVS_REQ_RDLOCK(dp->queue_rwlock)
1626 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1628 if (!dp->handler_queues) {
1629 VLOG_WARN_RL(&rl, "receiving upcall disabled");
1633 if (handler_id >= dp->n_handlers) {
1634 VLOG_WARN_RL(&rl, "handler index out of bound");
1642 dpif_netdev_recv(struct dpif *dpif, uint32_t handler_id,
1643 struct dpif_upcall *upcall, struct ofpbuf *buf)
1645 struct dp_netdev *dp = get_dp_netdev(dpif);
1646 struct dp_netdev_queue *q;
1649 fat_rwlock_rdlock(&dp->queue_rwlock);
1651 if (!dp_netdev_recv_check(dp, handler_id)) {
1656 q = &dp->handler_queues[handler_id];
1657 ovs_mutex_lock(&q->mutex);
1658 if (q->head != q->tail) {
1659 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1661 *upcall = u->upcall;
1668 ovs_mutex_unlock(&q->mutex);
1671 fat_rwlock_unlock(&dp->queue_rwlock);
1677 dpif_netdev_recv_wait(struct dpif *dpif, uint32_t handler_id)
1679 struct dp_netdev *dp = get_dp_netdev(dpif);
1680 struct dp_netdev_queue *q;
1683 fat_rwlock_rdlock(&dp->queue_rwlock);
1685 if (!dp_netdev_recv_check(dp, handler_id)) {
1689 q = &dp->handler_queues[handler_id];
1690 ovs_mutex_lock(&q->mutex);
1691 seq = seq_read(q->seq);
1692 if (q->head != q->tail) {
1693 poll_immediate_wake();
1695 seq_wait(q->seq, seq);
1698 ovs_mutex_unlock(&q->mutex);
1701 fat_rwlock_unlock(&dp->queue_rwlock);
1705 dpif_netdev_recv_purge(struct dpif *dpif)
1707 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1709 fat_rwlock_wrlock(&dpif_netdev->dp->queue_rwlock);
1710 dp_netdev_purge_queues(dpif_netdev->dp);
1711 fat_rwlock_unlock(&dpif_netdev->dp->queue_rwlock);
1714 /* Creates and returns a new 'struct dp_netdev_actions', with a reference count
1715 * of 1, whose actions are a copy of from the 'ofpacts_len' bytes of
1717 struct dp_netdev_actions *
1718 dp_netdev_actions_create(const struct nlattr *actions, size_t size)
1720 struct dp_netdev_actions *netdev_actions;
1722 netdev_actions = xmalloc(sizeof *netdev_actions);
1723 netdev_actions->actions = xmemdup(actions, size);
1724 netdev_actions->size = size;
1726 return netdev_actions;
1729 struct dp_netdev_actions *
1730 dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow)
1732 return ovsrcu_get(struct dp_netdev_actions *, &flow->actions);
1736 dp_netdev_actions_free(struct dp_netdev_actions *actions)
1738 free(actions->actions);
1744 dp_netdev_process_rxq_port(struct dp_netdev *dp,
1745 struct dp_netdev_port *port,
1746 struct netdev_rxq *rxq)
1748 struct ofpbuf *packet[NETDEV_MAX_RX_BATCH];
1751 error = netdev_rxq_recv(rxq, packet, &c);
1753 struct pkt_metadata md = PKT_METADATA_INITIALIZER(port->port_no);
1756 for (i = 0; i < c; i++) {
1757 dp_netdev_port_input(dp, packet[i], &md);
1759 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1760 static struct vlog_rate_limit rl
1761 = VLOG_RATE_LIMIT_INIT(1, 5);
1763 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1764 netdev_get_name(port->netdev),
1765 ovs_strerror(error));
1770 dpif_netdev_run(struct dpif *dpif)
1772 struct dp_netdev_port *port;
1773 struct dp_netdev *dp = get_dp_netdev(dpif);
1775 ovs_rwlock_rdlock(&dp->port_rwlock);
1777 HMAP_FOR_EACH (port, node, &dp->ports) {
1778 if (!netdev_is_pmd(port->netdev)) {
1781 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1782 dp_netdev_process_rxq_port(dp, port, port->rxq[i]);
1787 ovs_rwlock_unlock(&dp->port_rwlock);
1791 dpif_netdev_wait(struct dpif *dpif)
1793 struct dp_netdev_port *port;
1794 struct dp_netdev *dp = get_dp_netdev(dpif);
1796 ovs_rwlock_rdlock(&dp->port_rwlock);
1798 HMAP_FOR_EACH (port, node, &dp->ports) {
1799 if (!netdev_is_pmd(port->netdev)) {
1802 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1803 netdev_rxq_wait(port->rxq[i]);
1807 ovs_rwlock_unlock(&dp->port_rwlock);
1811 struct dp_netdev_port *port;
1812 struct netdev_rxq *rx;
1816 pmd_load_queues(struct pmd_thread *f,
1817 struct rxq_poll **ppoll_list, int poll_cnt)
1819 struct dp_netdev *dp = f->dp;
1820 struct rxq_poll *poll_list = *ppoll_list;
1821 struct dp_netdev_port *port;
1826 /* Simple scheduler for netdev rx polling. */
1827 ovs_rwlock_rdlock(&dp->port_rwlock);
1828 for (i = 0; i < poll_cnt; i++) {
1829 port_unref(poll_list[i].port);
1835 HMAP_FOR_EACH (port, node, &f->dp->ports) {
1836 if (netdev_is_pmd(port->netdev)) {
1839 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1840 if ((index % dp->n_pmd_threads) == id) {
1841 poll_list = xrealloc(poll_list, sizeof *poll_list * (poll_cnt + 1));
1844 poll_list[poll_cnt].port = port;
1845 poll_list[poll_cnt].rx = port->rxq[i];
1853 ovs_rwlock_unlock(&dp->port_rwlock);
1854 *ppoll_list = poll_list;
1859 pmd_thread_main(void *f_)
1861 struct pmd_thread *f = f_;
1862 struct dp_netdev *dp = f->dp;
1863 unsigned int lc = 0;
1864 struct rxq_poll *poll_list;
1865 unsigned int port_seq;
1869 f->name = xasprintf("pmd_%u", ovsthread_id_self());
1870 set_subprogram_name("%s", f->name);
1874 pmd_thread_setaffinity_cpu(f->id);
1876 poll_cnt = pmd_load_queues(f, &poll_list, poll_cnt);
1877 atomic_read(&f->change_seq, &port_seq);
1880 unsigned int c_port_seq;
1883 for (i = 0; i < poll_cnt; i++) {
1884 dp_netdev_process_rxq_port(dp, poll_list[i].port, poll_list[i].rx);
1890 /* TODO: need completely userspace based signaling method.
1891 * to keep this thread entirely in userspace.
1892 * For now using atomic counter. */
1894 atomic_read_explicit(&f->change_seq, &c_port_seq, memory_order_consume);
1895 if (c_port_seq != port_seq) {
1901 if (!latch_is_set(&f->dp->exit_latch)){
1905 for (i = 0; i < poll_cnt; i++) {
1906 port_unref(poll_list[i].port);
1915 dp_netdev_set_pmd_threads(struct dp_netdev *dp, int n)
1919 if (n == dp->n_pmd_threads) {
1923 /* Stop existing threads. */
1924 latch_set(&dp->exit_latch);
1925 dp_netdev_reload_pmd_threads(dp);
1926 for (i = 0; i < dp->n_pmd_threads; i++) {
1927 struct pmd_thread *f = &dp->pmd_threads[i];
1929 xpthread_join(f->thread, NULL);
1931 latch_poll(&dp->exit_latch);
1932 free(dp->pmd_threads);
1934 /* Start new threads. */
1935 dp->pmd_threads = xmalloc(n * sizeof *dp->pmd_threads);
1936 dp->n_pmd_threads = n;
1938 for (i = 0; i < n; i++) {
1939 struct pmd_thread *f = &dp->pmd_threads[i];
1943 atomic_store(&f->change_seq, 1);
1945 /* Each thread will distribute all devices rx-queues among
1947 xpthread_create(&f->thread, NULL, pmd_thread_main, f);
1953 dp_netdev_flow_stats_new_cb(void)
1955 struct dp_netdev_flow_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1956 ovs_mutex_init(&bucket->mutex);
1961 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow,
1962 const struct ofpbuf *packet,
1963 const struct flow *key)
1965 uint16_t tcp_flags = ntohs(key->tcp_flags);
1966 long long int now = time_msec();
1967 struct dp_netdev_flow_stats *bucket;
1969 bucket = ovsthread_stats_bucket_get(&netdev_flow->stats,
1970 dp_netdev_flow_stats_new_cb);
1972 ovs_mutex_lock(&bucket->mutex);
1973 bucket->used = MAX(now, bucket->used);
1974 bucket->packet_count++;
1975 bucket->byte_count += ofpbuf_size(packet);
1976 bucket->tcp_flags |= tcp_flags;
1977 ovs_mutex_unlock(&bucket->mutex);
1981 dp_netdev_stats_new_cb(void)
1983 struct dp_netdev_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1984 ovs_mutex_init(&bucket->mutex);
1989 dp_netdev_count_packet(struct dp_netdev *dp, enum dp_stat_type type)
1991 struct dp_netdev_stats *bucket;
1993 bucket = ovsthread_stats_bucket_get(&dp->stats, dp_netdev_stats_new_cb);
1994 ovs_mutex_lock(&bucket->mutex);
1996 ovs_mutex_unlock(&bucket->mutex);
2000 dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
2001 struct pkt_metadata *md)
2003 struct dp_netdev_flow *netdev_flow;
2006 if (ofpbuf_size(packet) < ETH_HEADER_LEN) {
2007 ofpbuf_delete(packet);
2010 flow_extract(packet, md, &key);
2011 netdev_flow = dp_netdev_lookup_flow(dp, &key);
2013 struct dp_netdev_actions *actions;
2015 dp_netdev_flow_used(netdev_flow, packet, &key);
2017 actions = dp_netdev_flow_get_actions(netdev_flow);
2018 dp_netdev_execute_actions(dp, &key, packet, true, md,
2019 actions->actions, actions->size);
2020 dp_netdev_count_packet(dp, DP_STAT_HIT);
2021 } else if (dp->handler_queues) {
2022 dp_netdev_count_packet(dp, DP_STAT_MISS);
2023 dp_netdev_output_userspace(dp, packet,
2024 flow_hash_5tuple(&key, 0) % dp->n_handlers,
2025 DPIF_UC_MISS, &key, NULL);
2026 ofpbuf_delete(packet);
2031 dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *packet,
2032 int queue_no, int type, const struct flow *flow,
2033 const struct nlattr *userdata)
2035 struct dp_netdev_queue *q;
2038 fat_rwlock_rdlock(&dp->queue_rwlock);
2039 q = &dp->handler_queues[queue_no];
2040 ovs_mutex_lock(&q->mutex);
2041 if (q->head - q->tail < MAX_QUEUE_LEN) {
2042 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
2043 struct dpif_upcall *upcall = &u->upcall;
2044 struct ofpbuf *buf = &u->buf;
2047 upcall->type = type;
2049 /* Allocate buffer big enough for everything. */
2050 buf_size = ODPUTIL_FLOW_KEY_BYTES;
2052 buf_size += NLA_ALIGN(userdata->nla_len);
2054 buf_size += ofpbuf_size(packet);
2055 ofpbuf_init(buf, buf_size);
2058 odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
2059 upcall->key = ofpbuf_data(buf);
2060 upcall->key_len = ofpbuf_size(buf);
2064 upcall->userdata = ofpbuf_put(buf, userdata,
2065 NLA_ALIGN(userdata->nla_len));
2068 ofpbuf_set_data(&upcall->packet,
2069 ofpbuf_put(buf, ofpbuf_data(packet), ofpbuf_size(packet)));
2070 ofpbuf_set_size(&upcall->packet, ofpbuf_size(packet));
2076 dp_netdev_count_packet(dp, DP_STAT_LOST);
2079 ovs_mutex_unlock(&q->mutex);
2080 fat_rwlock_unlock(&dp->queue_rwlock);
2085 struct dp_netdev_execute_aux {
2086 struct dp_netdev *dp;
2087 const struct flow *key;
2091 dp_execute_cb(void *aux_, struct ofpbuf *packet,
2092 struct pkt_metadata *md,
2093 const struct nlattr *a, bool may_steal)
2094 OVS_NO_THREAD_SAFETY_ANALYSIS
2096 struct dp_netdev_execute_aux *aux = aux_;
2097 int type = nl_attr_type(a);
2098 struct dp_netdev_port *p;
2100 switch ((enum ovs_action_attr)type) {
2101 case OVS_ACTION_ATTR_OUTPUT:
2102 p = dp_netdev_lookup_port(aux->dp, u32_to_odp(nl_attr_get_u32(a)));
2104 netdev_send(p->netdev, packet, may_steal);
2108 case OVS_ACTION_ATTR_USERSPACE: {
2109 const struct nlattr *userdata;
2111 userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
2113 dp_netdev_output_userspace(aux->dp, packet,
2114 flow_hash_5tuple(aux->key, 0)
2115 % aux->dp->n_handlers,
2116 DPIF_UC_ACTION, aux->key,
2120 ofpbuf_delete(packet);
2125 case OVS_ACTION_ATTR_RECIRC: {
2126 const struct ovs_action_recirc *act;
2128 act = nl_attr_get(a);
2129 md->recirc_id = act->recirc_id;
2132 if (act->hash_alg == OVS_RECIRC_HASH_ALG_L4) {
2135 flow_extract(packet, md, &flow);
2136 md->dp_hash = flow_hash_symmetric_l4(&flow, act->hash_bias);
2139 dp_netdev_port_input(aux->dp, packet, md);
2143 case OVS_ACTION_ATTR_PUSH_VLAN:
2144 case OVS_ACTION_ATTR_POP_VLAN:
2145 case OVS_ACTION_ATTR_PUSH_MPLS:
2146 case OVS_ACTION_ATTR_POP_MPLS:
2147 case OVS_ACTION_ATTR_SET:
2148 case OVS_ACTION_ATTR_SAMPLE:
2149 case OVS_ACTION_ATTR_UNSPEC:
2150 case __OVS_ACTION_ATTR_MAX:
2157 dp_netdev_execute_actions(struct dp_netdev *dp, const struct flow *key,
2158 struct ofpbuf *packet, bool may_steal,
2159 struct pkt_metadata *md,
2160 const struct nlattr *actions, size_t actions_len)
2162 struct dp_netdev_execute_aux aux = {dp, key};
2164 odp_execute_actions(&aux, packet, may_steal, md,
2165 actions, actions_len, dp_execute_cb);
2168 const struct dpif_class dpif_netdev_class = {
2170 dpif_netdev_enumerate,
2171 dpif_netdev_port_open_type,
2174 dpif_netdev_destroy,
2177 dpif_netdev_get_stats,
2178 dpif_netdev_port_add,
2179 dpif_netdev_port_del,
2180 dpif_netdev_port_query_by_number,
2181 dpif_netdev_port_query_by_name,
2182 NULL, /* port_get_pid */
2183 dpif_netdev_port_dump_start,
2184 dpif_netdev_port_dump_next,
2185 dpif_netdev_port_dump_done,
2186 dpif_netdev_port_poll,
2187 dpif_netdev_port_poll_wait,
2188 dpif_netdev_flow_get,
2189 dpif_netdev_flow_put,
2190 dpif_netdev_flow_del,
2191 dpif_netdev_flow_flush,
2192 dpif_netdev_flow_dump_state_init,
2193 dpif_netdev_flow_dump_start,
2194 dpif_netdev_flow_dump_next,
2196 dpif_netdev_flow_dump_done,
2197 dpif_netdev_flow_dump_state_uninit,
2198 dpif_netdev_execute,
2200 dpif_netdev_recv_set,
2201 dpif_netdev_handlers_set,
2202 dpif_netdev_queue_to_priority,
2204 dpif_netdev_recv_wait,
2205 dpif_netdev_recv_purge,
2209 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
2210 const char *argv[], void *aux OVS_UNUSED)
2212 struct dp_netdev_port *port;
2213 struct dp_netdev *dp;
2216 ovs_mutex_lock(&dp_netdev_mutex);
2217 dp = shash_find_data(&dp_netdevs, argv[1]);
2218 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
2219 ovs_mutex_unlock(&dp_netdev_mutex);
2220 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
2223 ovs_refcount_ref(&dp->ref_cnt);
2224 ovs_mutex_unlock(&dp_netdev_mutex);
2226 ovs_rwlock_wrlock(&dp->port_rwlock);
2227 if (get_port_by_name(dp, argv[2], &port)) {
2228 unixctl_command_reply_error(conn, "unknown port");
2232 port_no = u32_to_odp(atoi(argv[3]));
2233 if (!port_no || port_no == ODPP_NONE) {
2234 unixctl_command_reply_error(conn, "bad port number");
2237 if (dp_netdev_lookup_port(dp, port_no)) {
2238 unixctl_command_reply_error(conn, "port number already in use");
2241 hmap_remove(&dp->ports, &port->node);
2242 port->port_no = port_no;
2243 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
2244 seq_change(dp->port_seq);
2245 unixctl_command_reply(conn, NULL);
2248 ovs_rwlock_unlock(&dp->port_rwlock);
2249 dp_netdev_unref(dp);
2253 dpif_dummy_register__(const char *type)
2255 struct dpif_class *class;
2257 class = xmalloc(sizeof *class);
2258 *class = dpif_netdev_class;
2259 class->type = xstrdup(type);
2260 dp_register_provider(class);
2264 dpif_dummy_register(bool override)
2271 dp_enumerate_types(&types);
2272 SSET_FOR_EACH (type, &types) {
2273 if (!dp_unregister_provider(type)) {
2274 dpif_dummy_register__(type);
2277 sset_destroy(&types);
2280 dpif_dummy_register__("dummy");
2282 unixctl_command_register("dpif-dummy/change-port-number",
2283 "DP PORT NEW-NUMBER",
2284 3, 3, dpif_dummy_change_port_number, NULL);