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));
1139 /* Force unwildcard the in_port. */
1140 mask->in_port.odp_port = u32_to_odp(UINT32_MAX);
1142 enum mf_field_id id;
1143 /* No mask key, unwildcard everything except fields whose
1144 * prerequisities are not met. */
1145 memset(mask, 0x0, sizeof *mask);
1147 for (id = 0; id < MFF_N_IDS; ++id) {
1148 /* Skip registers and metadata. */
1149 if (!(id >= MFF_REG0 && id < MFF_REG0 + FLOW_N_REGS)
1150 && id != MFF_METADATA) {
1151 const struct mf_field *mf = mf_from_id(id);
1152 if (mf_are_prereqs_ok(mf, flow)) {
1153 mf_mask_field(mf, mask);
1163 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1168 if (odp_flow_key_to_flow(key, key_len, flow)) {
1169 /* This should not happen: it indicates that odp_flow_key_from_flow()
1170 * and odp_flow_key_to_flow() disagree on the acceptable form of a
1171 * flow. Log the problem as an error, with enough details to enable
1173 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1175 if (!VLOG_DROP_ERR(&rl)) {
1179 odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
1180 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
1187 in_port = flow->in_port.odp_port;
1188 if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
1196 dpif_netdev_flow_get(const struct dpif *dpif,
1197 const struct nlattr *nl_key, size_t nl_key_len,
1198 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
1200 struct dp_netdev *dp = get_dp_netdev(dpif);
1201 struct dp_netdev_flow *netdev_flow;
1205 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
1210 fat_rwlock_rdlock(&dp->cls.rwlock);
1211 netdev_flow = dp_netdev_find_flow(dp, &key);
1212 fat_rwlock_unlock(&dp->cls.rwlock);
1216 get_dpif_flow_stats(netdev_flow, stats);
1220 struct dp_netdev_actions *actions;
1222 actions = dp_netdev_flow_get_actions(netdev_flow);
1223 *actionsp = ofpbuf_clone_data(actions->actions, actions->size);
1233 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *flow,
1234 const struct flow_wildcards *wc,
1235 const struct nlattr *actions,
1237 OVS_REQUIRES(dp->flow_mutex)
1239 struct dp_netdev_flow *netdev_flow;
1242 netdev_flow = xzalloc(sizeof *netdev_flow);
1243 *CONST_CAST(struct flow *, &netdev_flow->flow) = *flow;
1245 ovs_mutex_init(&netdev_flow->mutex);
1247 ovsthread_stats_init(&netdev_flow->stats);
1249 ovsrcu_set(&netdev_flow->actions,
1250 dp_netdev_actions_create(actions, actions_len));
1252 match_init(&match, flow, wc);
1253 cls_rule_init(CONST_CAST(struct cls_rule *, &netdev_flow->cr),
1254 &match, NETDEV_RULE_PRIORITY);
1255 fat_rwlock_wrlock(&dp->cls.rwlock);
1256 classifier_insert(&dp->cls,
1257 CONST_CAST(struct cls_rule *, &netdev_flow->cr));
1258 hmap_insert(&dp->flow_table,
1259 CONST_CAST(struct hmap_node *, &netdev_flow->node),
1260 flow_hash(flow, 0));
1261 fat_rwlock_unlock(&dp->cls.rwlock);
1267 clear_stats(struct dp_netdev_flow *netdev_flow)
1269 struct dp_netdev_flow_stats *bucket;
1272 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1273 ovs_mutex_lock(&bucket->mutex);
1275 bucket->packet_count = 0;
1276 bucket->byte_count = 0;
1277 bucket->tcp_flags = 0;
1278 ovs_mutex_unlock(&bucket->mutex);
1283 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
1285 struct dp_netdev *dp = get_dp_netdev(dpif);
1286 struct dp_netdev_flow *netdev_flow;
1288 struct flow_wildcards wc;
1291 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &flow);
1295 error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
1296 put->mask, put->mask_len,
1302 ovs_mutex_lock(&dp->flow_mutex);
1303 netdev_flow = dp_netdev_lookup_flow(dp, &flow);
1305 if (put->flags & DPIF_FP_CREATE) {
1306 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
1308 memset(put->stats, 0, sizeof *put->stats);
1310 error = dp_netdev_flow_add(dp, &flow, &wc, put->actions,
1319 if (put->flags & DPIF_FP_MODIFY
1320 && flow_equal(&flow, &netdev_flow->flow)) {
1321 struct dp_netdev_actions *new_actions;
1322 struct dp_netdev_actions *old_actions;
1324 new_actions = dp_netdev_actions_create(put->actions,
1327 old_actions = dp_netdev_flow_get_actions(netdev_flow);
1328 ovsrcu_set(&netdev_flow->actions, new_actions);
1331 get_dpif_flow_stats(netdev_flow, put->stats);
1333 if (put->flags & DPIF_FP_ZERO_STATS) {
1334 clear_stats(netdev_flow);
1337 ovsrcu_postpone(dp_netdev_actions_free, old_actions);
1338 } else if (put->flags & DPIF_FP_CREATE) {
1341 /* Overlapping flow. */
1345 ovs_mutex_unlock(&dp->flow_mutex);
1351 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
1353 struct dp_netdev *dp = get_dp_netdev(dpif);
1354 struct dp_netdev_flow *netdev_flow;
1358 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
1363 ovs_mutex_lock(&dp->flow_mutex);
1364 fat_rwlock_wrlock(&dp->cls.rwlock);
1365 netdev_flow = dp_netdev_find_flow(dp, &key);
1368 get_dpif_flow_stats(netdev_flow, del->stats);
1370 dp_netdev_remove_flow(dp, netdev_flow);
1374 fat_rwlock_unlock(&dp->cls.rwlock);
1375 ovs_mutex_unlock(&dp->flow_mutex);
1380 struct dp_netdev_flow_state {
1381 struct dp_netdev_actions *actions;
1382 struct odputil_keybuf keybuf;
1383 struct odputil_keybuf maskbuf;
1384 struct dpif_flow_stats stats;
1387 struct dp_netdev_flow_iter {
1391 struct ovs_mutex mutex;
1395 dpif_netdev_flow_dump_state_init(void **statep)
1397 struct dp_netdev_flow_state *state;
1399 *statep = state = xmalloc(sizeof *state);
1400 state->actions = NULL;
1404 dpif_netdev_flow_dump_state_uninit(void *state_)
1406 struct dp_netdev_flow_state *state = state_;
1412 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **iterp)
1414 struct dp_netdev_flow_iter *iter;
1416 *iterp = iter = xmalloc(sizeof *iter);
1420 ovs_mutex_init(&iter->mutex);
1424 /* XXX the caller must use 'actions' without quiescing */
1426 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *iter_, void *state_,
1427 const struct nlattr **key, size_t *key_len,
1428 const struct nlattr **mask, size_t *mask_len,
1429 const struct nlattr **actions, size_t *actions_len,
1430 const struct dpif_flow_stats **stats)
1432 struct dp_netdev_flow_iter *iter = iter_;
1433 struct dp_netdev_flow_state *state = state_;
1434 struct dp_netdev *dp = get_dp_netdev(dpif);
1435 struct dp_netdev_flow *netdev_flow;
1438 ovs_mutex_lock(&iter->mutex);
1439 error = iter->status;
1441 struct hmap_node *node;
1443 fat_rwlock_rdlock(&dp->cls.rwlock);
1444 node = hmap_at_position(&dp->flow_table, &iter->bucket, &iter->offset);
1446 netdev_flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
1448 fat_rwlock_unlock(&dp->cls.rwlock);
1450 iter->status = error = EOF;
1453 ovs_mutex_unlock(&iter->mutex);
1461 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
1462 odp_flow_key_from_flow(&buf, &netdev_flow->flow,
1463 netdev_flow->flow.in_port.odp_port);
1466 *key_len = buf.size;
1471 struct flow_wildcards wc;
1473 ofpbuf_use_stack(&buf, &state->maskbuf, sizeof state->maskbuf);
1474 minimask_expand(&netdev_flow->cr.match.mask, &wc);
1475 odp_flow_key_from_mask(&buf, &wc.masks, &netdev_flow->flow,
1476 odp_to_u32(wc.masks.in_port.odp_port),
1480 *mask_len = buf.size;
1483 if (actions || stats) {
1484 state->actions = NULL;
1487 state->actions = dp_netdev_flow_get_actions(netdev_flow);
1488 *actions = state->actions->actions;
1489 *actions_len = state->actions->size;
1493 get_dpif_flow_stats(netdev_flow, &state->stats);
1494 *stats = &state->stats;
1502 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *iter_)
1504 struct dp_netdev_flow_iter *iter = iter_;
1506 ovs_mutex_destroy(&iter->mutex);
1512 dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
1514 struct dp_netdev *dp = get_dp_netdev(dpif);
1515 struct pkt_metadata *md = &execute->md;
1518 if (execute->packet->size < ETH_HEADER_LEN ||
1519 execute->packet->size > UINT16_MAX) {
1523 /* Extract flow key. */
1524 flow_extract(execute->packet, md, &key);
1526 ovs_rwlock_rdlock(&dp->port_rwlock);
1527 dp_netdev_execute_actions(dp, &key, execute->packet, false, md,
1528 execute->actions, execute->actions_len);
1529 ovs_rwlock_unlock(&dp->port_rwlock);
1535 dp_netdev_destroy_all_queues(struct dp_netdev *dp)
1536 OVS_REQ_WRLOCK(dp->queue_rwlock)
1540 dp_netdev_purge_queues(dp);
1542 for (i = 0; i < dp->n_handlers; i++) {
1543 struct dp_netdev_queue *q = &dp->handler_queues[i];
1545 ovs_mutex_destroy(&q->mutex);
1546 seq_destroy(q->seq);
1548 free(dp->handler_queues);
1549 dp->handler_queues = NULL;
1554 dp_netdev_refresh_queues(struct dp_netdev *dp, uint32_t n_handlers)
1555 OVS_REQ_WRLOCK(dp->queue_rwlock)
1557 if (dp->n_handlers != n_handlers) {
1560 dp_netdev_destroy_all_queues(dp);
1562 dp->n_handlers = n_handlers;
1563 dp->handler_queues = xzalloc(n_handlers * sizeof *dp->handler_queues);
1565 for (i = 0; i < n_handlers; i++) {
1566 struct dp_netdev_queue *q = &dp->handler_queues[i];
1568 ovs_mutex_init(&q->mutex);
1569 q->seq = seq_create();
1575 dpif_netdev_recv_set(struct dpif *dpif, bool enable)
1577 struct dp_netdev *dp = get_dp_netdev(dpif);
1579 if ((dp->handler_queues != NULL) == enable) {
1583 fat_rwlock_wrlock(&dp->queue_rwlock);
1585 dp_netdev_destroy_all_queues(dp);
1587 dp_netdev_refresh_queues(dp, 1);
1589 fat_rwlock_unlock(&dp->queue_rwlock);
1595 dpif_netdev_handlers_set(struct dpif *dpif, uint32_t n_handlers)
1597 struct dp_netdev *dp = get_dp_netdev(dpif);
1599 fat_rwlock_wrlock(&dp->queue_rwlock);
1600 if (dp->handler_queues) {
1601 dp_netdev_refresh_queues(dp, n_handlers);
1603 fat_rwlock_unlock(&dp->queue_rwlock);
1609 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1610 uint32_t queue_id, uint32_t *priority)
1612 *priority = queue_id;
1617 dp_netdev_recv_check(const struct dp_netdev *dp, const uint32_t handler_id)
1618 OVS_REQ_RDLOCK(dp->queue_rwlock)
1620 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1622 if (!dp->handler_queues) {
1623 VLOG_WARN_RL(&rl, "receiving upcall disabled");
1627 if (handler_id >= dp->n_handlers) {
1628 VLOG_WARN_RL(&rl, "handler index out of bound");
1636 dpif_netdev_recv(struct dpif *dpif, uint32_t handler_id,
1637 struct dpif_upcall *upcall, struct ofpbuf *buf)
1639 struct dp_netdev *dp = get_dp_netdev(dpif);
1640 struct dp_netdev_queue *q;
1643 fat_rwlock_rdlock(&dp->queue_rwlock);
1645 if (!dp_netdev_recv_check(dp, handler_id)) {
1650 q = &dp->handler_queues[handler_id];
1651 ovs_mutex_lock(&q->mutex);
1652 if (q->head != q->tail) {
1653 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1655 *upcall = u->upcall;
1662 ovs_mutex_unlock(&q->mutex);
1665 fat_rwlock_unlock(&dp->queue_rwlock);
1671 dpif_netdev_recv_wait(struct dpif *dpif, uint32_t handler_id)
1673 struct dp_netdev *dp = get_dp_netdev(dpif);
1674 struct dp_netdev_queue *q;
1677 fat_rwlock_rdlock(&dp->queue_rwlock);
1679 if (!dp_netdev_recv_check(dp, handler_id)) {
1683 q = &dp->handler_queues[handler_id];
1684 ovs_mutex_lock(&q->mutex);
1685 seq = seq_read(q->seq);
1686 if (q->head != q->tail) {
1687 poll_immediate_wake();
1689 seq_wait(q->seq, seq);
1692 ovs_mutex_unlock(&q->mutex);
1695 fat_rwlock_unlock(&dp->queue_rwlock);
1699 dpif_netdev_recv_purge(struct dpif *dpif)
1701 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1703 fat_rwlock_wrlock(&dpif_netdev->dp->queue_rwlock);
1704 dp_netdev_purge_queues(dpif_netdev->dp);
1705 fat_rwlock_unlock(&dpif_netdev->dp->queue_rwlock);
1708 /* Creates and returns a new 'struct dp_netdev_actions', with a reference count
1709 * of 1, whose actions are a copy of from the 'ofpacts_len' bytes of
1711 struct dp_netdev_actions *
1712 dp_netdev_actions_create(const struct nlattr *actions, size_t size)
1714 struct dp_netdev_actions *netdev_actions;
1716 netdev_actions = xmalloc(sizeof *netdev_actions);
1717 netdev_actions->actions = xmemdup(actions, size);
1718 netdev_actions->size = size;
1720 return netdev_actions;
1723 struct dp_netdev_actions *
1724 dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow)
1726 return ovsrcu_get(struct dp_netdev_actions *, &flow->actions);
1730 dp_netdev_actions_free(struct dp_netdev_actions *actions)
1732 free(actions->actions);
1738 dp_netdev_process_rxq_port(struct dp_netdev *dp,
1739 struct dp_netdev_port *port,
1740 struct netdev_rxq *rxq)
1742 struct ofpbuf *packet[NETDEV_MAX_RX_BATCH];
1745 error = netdev_rxq_recv(rxq, packet, &c);
1747 struct pkt_metadata md = PKT_METADATA_INITIALIZER(port->port_no);
1750 for (i = 0; i < c; i++) {
1751 dp_netdev_port_input(dp, packet[i], &md);
1753 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1754 static struct vlog_rate_limit rl
1755 = VLOG_RATE_LIMIT_INIT(1, 5);
1757 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1758 netdev_get_name(port->netdev),
1759 ovs_strerror(error));
1764 dpif_netdev_run(struct dpif *dpif)
1766 struct dp_netdev_port *port;
1767 struct dp_netdev *dp = get_dp_netdev(dpif);
1769 ovs_rwlock_rdlock(&dp->port_rwlock);
1771 HMAP_FOR_EACH (port, node, &dp->ports) {
1772 if (!netdev_is_pmd(port->netdev)) {
1775 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1776 dp_netdev_process_rxq_port(dp, port, port->rxq[i]);
1781 ovs_rwlock_unlock(&dp->port_rwlock);
1785 dpif_netdev_wait(struct dpif *dpif)
1787 struct dp_netdev_port *port;
1788 struct dp_netdev *dp = get_dp_netdev(dpif);
1790 ovs_rwlock_rdlock(&dp->port_rwlock);
1792 HMAP_FOR_EACH (port, node, &dp->ports) {
1793 if (!netdev_is_pmd(port->netdev)) {
1796 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1797 netdev_rxq_wait(port->rxq[i]);
1801 ovs_rwlock_unlock(&dp->port_rwlock);
1805 struct dp_netdev_port *port;
1806 struct netdev_rxq *rx;
1810 pmd_load_queues(struct pmd_thread *f,
1811 struct rxq_poll **ppoll_list, int poll_cnt)
1813 struct dp_netdev *dp = f->dp;
1814 struct rxq_poll *poll_list = *ppoll_list;
1815 struct dp_netdev_port *port;
1820 /* Simple scheduler for netdev rx polling. */
1821 ovs_rwlock_rdlock(&dp->port_rwlock);
1822 for (i = 0; i < poll_cnt; i++) {
1823 port_unref(poll_list[i].port);
1829 HMAP_FOR_EACH (port, node, &f->dp->ports) {
1830 if (netdev_is_pmd(port->netdev)) {
1833 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1834 if ((index % dp->n_pmd_threads) == id) {
1835 poll_list = xrealloc(poll_list, sizeof *poll_list * (poll_cnt + 1));
1838 poll_list[poll_cnt].port = port;
1839 poll_list[poll_cnt].rx = port->rxq[i];
1847 ovs_rwlock_unlock(&dp->port_rwlock);
1848 *ppoll_list = poll_list;
1853 pmd_thread_main(void *f_)
1855 struct pmd_thread *f = f_;
1856 struct dp_netdev *dp = f->dp;
1857 unsigned int lc = 0;
1858 struct rxq_poll *poll_list;
1859 unsigned int port_seq;
1863 f->name = xasprintf("pmd_%u", ovsthread_id_self());
1864 set_subprogram_name("%s", f->name);
1868 pmd_thread_setaffinity_cpu(f->id);
1870 poll_cnt = pmd_load_queues(f, &poll_list, poll_cnt);
1871 atomic_read(&f->change_seq, &port_seq);
1874 unsigned int c_port_seq;
1877 for (i = 0; i < poll_cnt; i++) {
1878 dp_netdev_process_rxq_port(dp, poll_list[i].port, poll_list[i].rx);
1884 /* TODO: need completely userspace based signaling method.
1885 * to keep this thread entirely in userspace.
1886 * For now using atomic counter. */
1888 atomic_read_explicit(&f->change_seq, &c_port_seq, memory_order_consume);
1889 if (c_port_seq != port_seq) {
1895 if (!latch_is_set(&f->dp->exit_latch)){
1899 for (i = 0; i < poll_cnt; i++) {
1900 port_unref(poll_list[i].port);
1909 dp_netdev_set_pmd_threads(struct dp_netdev *dp, int n)
1913 if (n == dp->n_pmd_threads) {
1917 /* Stop existing threads. */
1918 latch_set(&dp->exit_latch);
1919 dp_netdev_reload_pmd_threads(dp);
1920 for (i = 0; i < dp->n_pmd_threads; i++) {
1921 struct pmd_thread *f = &dp->pmd_threads[i];
1923 xpthread_join(f->thread, NULL);
1925 latch_poll(&dp->exit_latch);
1926 free(dp->pmd_threads);
1928 /* Start new threads. */
1929 dp->pmd_threads = xmalloc(n * sizeof *dp->pmd_threads);
1930 dp->n_pmd_threads = n;
1932 for (i = 0; i < n; i++) {
1933 struct pmd_thread *f = &dp->pmd_threads[i];
1937 atomic_store(&f->change_seq, 1);
1939 /* Each thread will distribute all devices rx-queues among
1941 xpthread_create(&f->thread, NULL, pmd_thread_main, f);
1947 dp_netdev_flow_stats_new_cb(void)
1949 struct dp_netdev_flow_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1950 ovs_mutex_init(&bucket->mutex);
1955 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow,
1956 const struct ofpbuf *packet,
1957 const struct flow *key)
1959 uint16_t tcp_flags = ntohs(key->tcp_flags);
1960 long long int now = time_msec();
1961 struct dp_netdev_flow_stats *bucket;
1963 bucket = ovsthread_stats_bucket_get(&netdev_flow->stats,
1964 dp_netdev_flow_stats_new_cb);
1966 ovs_mutex_lock(&bucket->mutex);
1967 bucket->used = MAX(now, bucket->used);
1968 bucket->packet_count++;
1969 bucket->byte_count += packet->size;
1970 bucket->tcp_flags |= tcp_flags;
1971 ovs_mutex_unlock(&bucket->mutex);
1975 dp_netdev_stats_new_cb(void)
1977 struct dp_netdev_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1978 ovs_mutex_init(&bucket->mutex);
1983 dp_netdev_count_packet(struct dp_netdev *dp, enum dp_stat_type type)
1985 struct dp_netdev_stats *bucket;
1987 bucket = ovsthread_stats_bucket_get(&dp->stats, dp_netdev_stats_new_cb);
1988 ovs_mutex_lock(&bucket->mutex);
1990 ovs_mutex_unlock(&bucket->mutex);
1994 dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
1995 struct pkt_metadata *md)
1997 struct dp_netdev_flow *netdev_flow;
2000 if (packet->size < ETH_HEADER_LEN) {
2001 ofpbuf_delete(packet);
2004 flow_extract(packet, md, &key);
2005 netdev_flow = dp_netdev_lookup_flow(dp, &key);
2007 struct dp_netdev_actions *actions;
2009 dp_netdev_flow_used(netdev_flow, packet, &key);
2011 actions = dp_netdev_flow_get_actions(netdev_flow);
2012 dp_netdev_execute_actions(dp, &key, packet, true, md,
2013 actions->actions, actions->size);
2014 dp_netdev_count_packet(dp, DP_STAT_HIT);
2015 } else if (dp->handler_queues) {
2016 dp_netdev_count_packet(dp, DP_STAT_MISS);
2017 dp_netdev_output_userspace(dp, packet,
2018 flow_hash_5tuple(&key, 0) % dp->n_handlers,
2019 DPIF_UC_MISS, &key, NULL);
2020 ofpbuf_delete(packet);
2025 dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *packet,
2026 int queue_no, int type, const struct flow *flow,
2027 const struct nlattr *userdata)
2029 struct dp_netdev_queue *q;
2032 fat_rwlock_rdlock(&dp->queue_rwlock);
2033 q = &dp->handler_queues[queue_no];
2034 ovs_mutex_lock(&q->mutex);
2035 if (q->head - q->tail < MAX_QUEUE_LEN) {
2036 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
2037 struct dpif_upcall *upcall = &u->upcall;
2038 struct ofpbuf *buf = &u->buf;
2041 upcall->type = type;
2043 /* Allocate buffer big enough for everything. */
2044 buf_size = ODPUTIL_FLOW_KEY_BYTES;
2046 buf_size += NLA_ALIGN(userdata->nla_len);
2048 buf_size += packet->size;
2049 ofpbuf_init(buf, buf_size);
2052 odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
2053 upcall->key = buf->data;
2054 upcall->key_len = buf->size;
2058 upcall->userdata = ofpbuf_put(buf, userdata,
2059 NLA_ALIGN(userdata->nla_len));
2062 upcall->packet.data = ofpbuf_put(buf, packet->data, packet->size);
2063 upcall->packet.size = packet->size;
2069 dp_netdev_count_packet(dp, DP_STAT_LOST);
2072 ovs_mutex_unlock(&q->mutex);
2073 fat_rwlock_unlock(&dp->queue_rwlock);
2078 struct dp_netdev_execute_aux {
2079 struct dp_netdev *dp;
2080 const struct flow *key;
2084 dp_execute_cb(void *aux_, struct ofpbuf *packet,
2085 struct pkt_metadata *md,
2086 const struct nlattr *a, bool may_steal)
2087 OVS_NO_THREAD_SAFETY_ANALYSIS
2089 struct dp_netdev_execute_aux *aux = aux_;
2090 int type = nl_attr_type(a);
2091 struct dp_netdev_port *p;
2093 switch ((enum ovs_action_attr)type) {
2094 case OVS_ACTION_ATTR_OUTPUT:
2095 p = dp_netdev_lookup_port(aux->dp, u32_to_odp(nl_attr_get_u32(a)));
2097 netdev_send(p->netdev, packet, may_steal);
2101 case OVS_ACTION_ATTR_USERSPACE: {
2102 const struct nlattr *userdata;
2104 userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
2106 dp_netdev_output_userspace(aux->dp, packet,
2107 flow_hash_5tuple(aux->key, 0)
2108 % aux->dp->n_handlers,
2109 DPIF_UC_ACTION, aux->key,
2113 ofpbuf_delete(packet);
2118 case OVS_ACTION_ATTR_RECIRC: {
2119 const struct ovs_action_recirc *act;
2121 act = nl_attr_get(a);
2122 md->recirc_id = act->recirc_id;
2125 if (act->hash_alg == OVS_RECIRC_HASH_ALG_L4) {
2128 flow_extract(packet, md, &flow);
2129 md->dp_hash = flow_hash_symmetric_l4(&flow, act->hash_bias);
2132 dp_netdev_port_input(aux->dp, packet, md);
2136 case OVS_ACTION_ATTR_PUSH_VLAN:
2137 case OVS_ACTION_ATTR_POP_VLAN:
2138 case OVS_ACTION_ATTR_PUSH_MPLS:
2139 case OVS_ACTION_ATTR_POP_MPLS:
2140 case OVS_ACTION_ATTR_SET:
2141 case OVS_ACTION_ATTR_SAMPLE:
2142 case OVS_ACTION_ATTR_UNSPEC:
2143 case __OVS_ACTION_ATTR_MAX:
2150 dp_netdev_execute_actions(struct dp_netdev *dp, const struct flow *key,
2151 struct ofpbuf *packet, bool may_steal,
2152 struct pkt_metadata *md,
2153 const struct nlattr *actions, size_t actions_len)
2155 struct dp_netdev_execute_aux aux = {dp, key};
2157 odp_execute_actions(&aux, packet, may_steal, md,
2158 actions, actions_len, dp_execute_cb);
2161 const struct dpif_class dpif_netdev_class = {
2163 dpif_netdev_enumerate,
2164 dpif_netdev_port_open_type,
2167 dpif_netdev_destroy,
2170 dpif_netdev_get_stats,
2171 dpif_netdev_port_add,
2172 dpif_netdev_port_del,
2173 dpif_netdev_port_query_by_number,
2174 dpif_netdev_port_query_by_name,
2175 NULL, /* port_get_pid */
2176 dpif_netdev_port_dump_start,
2177 dpif_netdev_port_dump_next,
2178 dpif_netdev_port_dump_done,
2179 dpif_netdev_port_poll,
2180 dpif_netdev_port_poll_wait,
2181 dpif_netdev_flow_get,
2182 dpif_netdev_flow_put,
2183 dpif_netdev_flow_del,
2184 dpif_netdev_flow_flush,
2185 dpif_netdev_flow_dump_state_init,
2186 dpif_netdev_flow_dump_start,
2187 dpif_netdev_flow_dump_next,
2189 dpif_netdev_flow_dump_done,
2190 dpif_netdev_flow_dump_state_uninit,
2191 dpif_netdev_execute,
2193 dpif_netdev_recv_set,
2194 dpif_netdev_handlers_set,
2195 dpif_netdev_queue_to_priority,
2197 dpif_netdev_recv_wait,
2198 dpif_netdev_recv_purge,
2202 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
2203 const char *argv[], void *aux OVS_UNUSED)
2205 struct dp_netdev_port *port;
2206 struct dp_netdev *dp;
2209 ovs_mutex_lock(&dp_netdev_mutex);
2210 dp = shash_find_data(&dp_netdevs, argv[1]);
2211 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
2212 ovs_mutex_unlock(&dp_netdev_mutex);
2213 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
2216 ovs_refcount_ref(&dp->ref_cnt);
2217 ovs_mutex_unlock(&dp_netdev_mutex);
2219 ovs_rwlock_wrlock(&dp->port_rwlock);
2220 if (get_port_by_name(dp, argv[2], &port)) {
2221 unixctl_command_reply_error(conn, "unknown port");
2225 port_no = u32_to_odp(atoi(argv[3]));
2226 if (!port_no || port_no == ODPP_NONE) {
2227 unixctl_command_reply_error(conn, "bad port number");
2230 if (dp_netdev_lookup_port(dp, port_no)) {
2231 unixctl_command_reply_error(conn, "port number already in use");
2234 hmap_remove(&dp->ports, &port->node);
2235 port->port_no = port_no;
2236 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
2237 seq_change(dp->port_seq);
2238 unixctl_command_reply(conn, NULL);
2241 ovs_rwlock_unlock(&dp->port_rwlock);
2242 dp_netdev_unref(dp);
2246 dpif_dummy_register__(const char *type)
2248 struct dpif_class *class;
2250 class = xmalloc(sizeof *class);
2251 *class = dpif_netdev_class;
2252 class->type = xstrdup(type);
2253 dp_register_provider(class);
2257 dpif_dummy_register(bool override)
2264 dp_enumerate_types(&types);
2265 SSET_FOR_EACH (type, &types) {
2266 if (!dp_unregister_provider(type)) {
2267 dpif_dummy_register__(type);
2270 sset_destroy(&types);
2273 dpif_dummy_register__("dummy");
2275 unixctl_command_register("dpif-dummy/change-port-number",
2276 "DP PORT NEW-NUMBER",
2277 3, 3, dpif_dummy_change_port_number, NULL);