2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include <netinet/in.h>
25 #include <sys/socket.h>
30 #include <sys/ioctl.h>
34 #include "classifier.h"
37 #include "dpif-provider.h"
39 #include "dynamic-string.h"
44 #include "meta-flow.h"
46 #include "netdev-vport.h"
48 #include "odp-execute.h"
50 #include "ofp-print.h"
54 #include "poll-loop.h"
64 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
66 /* By default, choose a priority in the middle. */
67 #define NETDEV_RULE_PRIORITY 0x8000
69 /* Configuration parameters. */
70 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
72 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
73 * headers to be aligned on a 4-byte boundary. */
74 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
77 enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */
78 enum { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
79 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
80 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
82 /* Protects against changes to 'dp_netdevs'. */
83 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
85 /* Contains all 'struct dp_netdev's. */
86 static struct shash dp_netdevs OVS_GUARDED_BY(dp_netdev_mutex)
87 = SHASH_INITIALIZER(&dp_netdevs);
89 struct dp_netdev_upcall {
90 struct dpif_upcall upcall; /* Queued upcall information. */
91 struct ofpbuf buf; /* ofpbuf instance for upcall.packet. */
94 /* A queue passing packets from a struct dp_netdev to its clients.
100 * Any access at all requires the owning 'dp_netdev''s queue_mutex. */
101 struct dp_netdev_queue {
102 struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN] OVS_GUARDED;
103 unsigned int head OVS_GUARDED;
104 unsigned int tail OVS_GUARDED;
107 /* Datapath based on the network device interface from netdev.h.
113 * Some members, marked 'const', are immutable. Accessing other members
114 * requires synchronization, as noted in more detail below.
116 * Acquisition order is, from outermost to innermost:
118 * dp_netdev_mutex (global)
125 const struct dpif_class *const class;
126 const char *const name;
127 struct ovs_refcount ref_cnt;
128 atomic_flag destroyed;
132 * Readers of 'cls' and 'flow_table' must take a 'cls->rwlock' read lock.
134 * Writers of 'cls' and 'flow_table' must take the 'flow_mutex' and then
135 * the 'cls->rwlock' write lock. (The outer 'flow_mutex' allows writers to
136 * atomically perform multiple operations on 'cls' and 'flow_table'.)
138 struct ovs_mutex flow_mutex;
139 struct classifier cls; /* Classifier. Protected by cls.rwlock. */
140 struct hmap flow_table OVS_GUARDED; /* Flow table. */
144 * Everything in 'queues' is protected by 'queue_mutex'. */
145 struct ovs_mutex queue_mutex;
146 struct dp_netdev_queue queues[N_QUEUES];
147 struct seq *queue_seq; /* Incremented whenever a packet is queued. */
151 * ovsthread_stats is internally synchronized. */
152 struct ovsthread_stats stats; /* Contains 'struct dp_netdev_stats *'. */
156 * Any lookup into 'ports' or any access to the dp_netdev_ports found
157 * through 'ports' requires taking 'port_rwlock'. */
158 struct ovs_rwlock port_rwlock;
159 struct hmap ports OVS_GUARDED;
160 struct seq *port_seq; /* Incremented whenever a port changes. */
162 /* Forwarding threads. */
163 struct latch exit_latch;
164 struct dp_forwarder *forwarders;
168 static struct dp_netdev_port *dp_netdev_lookup_port(const struct dp_netdev *dp,
170 OVS_REQ_RDLOCK(dp->port_rwlock);
173 DP_STAT_HIT, /* Packets that matched in the flow table. */
174 DP_STAT_MISS, /* Packets that did not match. */
175 DP_STAT_LOST, /* Packets not passed up to the client. */
179 /* Contained by struct dp_netdev's 'stats' member. */
180 struct dp_netdev_stats {
181 struct ovs_mutex mutex; /* Protects 'n'. */
183 /* Indexed by DP_STAT_*, protected by 'mutex'. */
184 unsigned long long int n[DP_N_STATS] OVS_GUARDED;
188 /* A port in a netdev-based datapath. */
189 struct dp_netdev_port {
190 struct hmap_node node; /* Node in dp_netdev's 'ports'. */
192 struct netdev *netdev;
193 struct netdev_saved_flags *sf;
194 struct netdev_rx *rx;
195 char *type; /* Port type as requested by user. */
198 /* A flow in dp_netdev's 'flow_table'.
204 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
205 * its dp_netdev's classifier. The text below calls this classifier 'cls'.
210 * The thread safety rules described here for "struct dp_netdev_flow" are
211 * motivated by two goals:
213 * - Prevent threads that read members of "struct dp_netdev_flow" from
214 * reading bad data due to changes by some thread concurrently modifying
217 * - Prevent two threads making changes to members of a given "struct
218 * dp_netdev_flow" from interfering with each other.
224 * A flow 'flow' may be accessed without a risk of being freed by code that
225 * holds a read-lock or write-lock on 'cls->rwlock' or that owns a reference to
226 * 'flow->ref_cnt' (or both). Code that needs to hold onto a flow for a while
227 * should take 'cls->rwlock', find the flow it needs, increment 'flow->ref_cnt'
228 * with dpif_netdev_flow_ref(), and drop 'cls->rwlock'.
230 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
231 * flow from being deleted from 'cls' (that's 'cls->rwlock') and it doesn't
232 * protect members of 'flow' from modification (that's 'flow->mutex').
234 * 'flow->mutex' protects the members of 'flow' from modification. It doesn't
235 * protect the flow from being deleted from 'cls' (that's 'cls->rwlock') and it
236 * doesn't prevent the flow from being freed (that's 'flow->ref_cnt').
238 * Some members, marked 'const', are immutable. Accessing other members
239 * requires synchronization, as noted in more detail below.
241 struct dp_netdev_flow {
242 /* Packet classification. */
243 const struct cls_rule cr; /* In owning dp_netdev's 'cls'. */
245 /* Hash table index by unmasked flow. */
246 const struct hmap_node node; /* In owning dp_netdev's 'flow_table'. */
247 const struct flow flow; /* The flow that created this entry. */
249 /* Protects members marked OVS_GUARDED.
251 * Acquire after datapath's flow_mutex. */
252 struct ovs_mutex mutex OVS_ACQ_AFTER(dp_netdev_mutex);
256 * Reading or writing these members requires 'mutex'. */
257 struct ovsthread_stats stats; /* Contains "struct dp_netdev_flow_stats". */
261 * Reading 'actions' requires 'mutex'.
262 * Writing 'actions' requires 'mutex' and (to allow for transactions) the
263 * datapath's flow_mutex. */
264 OVSRCU_TYPE(struct dp_netdev_actions *) actions;
267 static void dp_netdev_flow_free(struct dp_netdev_flow *);
269 /* Contained by struct dp_netdev_flow's 'stats' member. */
270 struct dp_netdev_flow_stats {
271 struct ovs_mutex mutex; /* Guards all the other members. */
273 long long int used OVS_GUARDED; /* Last used time, in monotonic msecs. */
274 long long int packet_count OVS_GUARDED; /* Number of packets matched. */
275 long long int byte_count OVS_GUARDED; /* Number of bytes matched. */
276 uint16_t tcp_flags OVS_GUARDED; /* Bitwise-OR of seen tcp_flags values. */
279 /* A set of datapath actions within a "struct dp_netdev_flow".
285 * A struct dp_netdev_actions 'actions' may be accessed without a risk of being
286 * freed by code that holds a read-lock or write-lock on 'flow->mutex' (where
287 * 'flow' is the dp_netdev_flow for which 'flow->actions == actions') or that
288 * owns a reference to 'actions->ref_cnt' (or both). */
289 struct dp_netdev_actions {
290 /* These members are immutable: they do not change during the struct's
292 struct nlattr *actions; /* Sequence of OVS_ACTION_ATTR_* attributes. */
293 unsigned int size; /* Size of 'actions', in bytes. */
296 struct dp_netdev_actions *dp_netdev_actions_create(const struct nlattr *,
298 struct dp_netdev_actions *dp_netdev_flow_get_actions(
299 const struct dp_netdev_flow *);
300 static void dp_netdev_actions_free(struct dp_netdev_actions *);
302 /* A thread that receives packets from some ports, looks them up in the flow
303 * table, and executes the actions it finds. */
304 struct dp_forwarder {
305 struct dp_netdev *dp;
308 uint32_t min_hash, max_hash;
311 /* Interface to netdev-based datapath. */
314 struct dp_netdev *dp;
315 uint64_t last_port_seq;
318 static int get_port_by_number(struct dp_netdev *dp, odp_port_t port_no,
319 struct dp_netdev_port **portp)
320 OVS_REQ_RDLOCK(dp->port_rwlock);
321 static int get_port_by_name(struct dp_netdev *dp, const char *devname,
322 struct dp_netdev_port **portp)
323 OVS_REQ_RDLOCK(dp->port_rwlock);
324 static void dp_netdev_free(struct dp_netdev *)
325 OVS_REQUIRES(dp_netdev_mutex);
326 static void dp_netdev_flow_flush(struct dp_netdev *);
327 static int do_add_port(struct dp_netdev *dp, const char *devname,
328 const char *type, odp_port_t port_no)
329 OVS_REQ_WRLOCK(dp->port_rwlock);
330 static int do_del_port(struct dp_netdev *dp, odp_port_t port_no)
331 OVS_REQ_WRLOCK(dp->port_rwlock);
332 static int dpif_netdev_open(const struct dpif_class *, const char *name,
333 bool create, struct dpif **);
334 static int dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *,
335 int queue_no, const struct flow *,
336 const struct nlattr *userdata)
337 OVS_EXCLUDED(dp->queue_mutex);
338 static void dp_netdev_execute_actions(struct dp_netdev *dp,
339 const struct flow *, struct ofpbuf *,
340 struct pkt_metadata *,
341 const struct nlattr *actions,
343 OVS_REQ_RDLOCK(dp->port_rwlock);
344 static void dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
345 struct pkt_metadata *)
346 OVS_REQ_RDLOCK(dp->port_rwlock);
347 static void dp_netdev_set_threads(struct dp_netdev *, int n);
349 static struct dpif_netdev *
350 dpif_netdev_cast(const struct dpif *dpif)
352 ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
353 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
356 static struct dp_netdev *
357 get_dp_netdev(const struct dpif *dpif)
359 return dpif_netdev_cast(dpif)->dp;
363 dpif_netdev_enumerate(struct sset *all_dps)
365 struct shash_node *node;
367 ovs_mutex_lock(&dp_netdev_mutex);
368 SHASH_FOR_EACH(node, &dp_netdevs) {
369 sset_add(all_dps, node->name);
371 ovs_mutex_unlock(&dp_netdev_mutex);
377 dpif_netdev_class_is_dummy(const struct dpif_class *class)
379 return class != &dpif_netdev_class;
383 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
385 return strcmp(type, "internal") ? type
386 : dpif_netdev_class_is_dummy(class) ? "dummy"
391 create_dpif_netdev(struct dp_netdev *dp)
393 uint16_t netflow_id = hash_string(dp->name, 0);
394 struct dpif_netdev *dpif;
396 ovs_refcount_ref(&dp->ref_cnt);
398 dpif = xmalloc(sizeof *dpif);
399 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
401 dpif->last_port_seq = seq_read(dp->port_seq);
406 /* Choose an unused, non-zero port number and return it on success.
407 * Return ODPP_NONE on failure. */
409 choose_port(struct dp_netdev *dp, const char *name)
410 OVS_REQ_RDLOCK(dp->port_rwlock)
414 if (dp->class != &dpif_netdev_class) {
418 /* If the port name begins with "br", start the number search at
419 * 100 to make writing tests easier. */
420 if (!strncmp(name, "br", 2)) {
424 /* If the port name contains a number, try to assign that port number.
425 * This can make writing unit tests easier because port numbers are
427 for (p = name; *p != '\0'; p++) {
428 if (isdigit((unsigned char) *p)) {
429 port_no = start_no + strtol(p, NULL, 10);
430 if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE)
431 && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
432 return u32_to_odp(port_no);
439 for (port_no = 1; port_no <= UINT16_MAX; port_no++) {
440 if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
441 return u32_to_odp(port_no);
449 create_dp_netdev(const char *name, const struct dpif_class *class,
450 struct dp_netdev **dpp)
451 OVS_REQUIRES(dp_netdev_mutex)
453 struct dp_netdev *dp;
457 dp = xzalloc(sizeof *dp);
458 shash_add(&dp_netdevs, name, dp);
460 *CONST_CAST(const struct dpif_class **, &dp->class) = class;
461 *CONST_CAST(const char **, &dp->name) = xstrdup(name);
462 ovs_refcount_init(&dp->ref_cnt);
463 atomic_flag_clear(&dp->destroyed);
465 ovs_mutex_init(&dp->flow_mutex);
466 classifier_init(&dp->cls, NULL);
467 hmap_init(&dp->flow_table);
469 ovs_mutex_init(&dp->queue_mutex);
470 ovs_mutex_lock(&dp->queue_mutex);
471 for (i = 0; i < N_QUEUES; i++) {
472 dp->queues[i].head = dp->queues[i].tail = 0;
474 ovs_mutex_unlock(&dp->queue_mutex);
475 dp->queue_seq = seq_create();
477 ovsthread_stats_init(&dp->stats);
479 ovs_rwlock_init(&dp->port_rwlock);
480 hmap_init(&dp->ports);
481 dp->port_seq = seq_create();
482 latch_init(&dp->exit_latch);
484 ovs_rwlock_wrlock(&dp->port_rwlock);
485 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
486 ovs_rwlock_unlock(&dp->port_rwlock);
491 dp_netdev_set_threads(dp, 2);
498 dpif_netdev_open(const struct dpif_class *class, const char *name,
499 bool create, struct dpif **dpifp)
501 struct dp_netdev *dp;
504 ovs_mutex_lock(&dp_netdev_mutex);
505 dp = shash_find_data(&dp_netdevs, name);
507 error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
509 error = (dp->class != class ? EINVAL
514 *dpifp = create_dpif_netdev(dp);
516 ovs_mutex_unlock(&dp_netdev_mutex);
522 dp_netdev_purge_queues(struct dp_netdev *dp)
526 ovs_mutex_lock(&dp->queue_mutex);
527 for (i = 0; i < N_QUEUES; i++) {
528 struct dp_netdev_queue *q = &dp->queues[i];
530 while (q->tail != q->head) {
531 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
532 ofpbuf_uninit(&u->upcall.packet);
533 ofpbuf_uninit(&u->buf);
536 ovs_mutex_unlock(&dp->queue_mutex);
539 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
540 * through the 'dp_netdevs' shash while freeing 'dp'. */
542 dp_netdev_free(struct dp_netdev *dp)
543 OVS_REQUIRES(dp_netdev_mutex)
545 struct dp_netdev_port *port, *next;
546 struct dp_netdev_stats *bucket;
549 shash_find_and_delete(&dp_netdevs, dp->name);
551 dp_netdev_set_threads(dp, 0);
552 free(dp->forwarders);
554 dp_netdev_flow_flush(dp);
555 ovs_rwlock_wrlock(&dp->port_rwlock);
556 HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) {
557 do_del_port(dp, port->port_no);
559 ovs_rwlock_unlock(&dp->port_rwlock);
561 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
562 ovs_mutex_destroy(&bucket->mutex);
563 free_cacheline(bucket);
565 ovsthread_stats_destroy(&dp->stats);
567 dp_netdev_purge_queues(dp);
568 seq_destroy(dp->queue_seq);
569 ovs_mutex_destroy(&dp->queue_mutex);
571 classifier_destroy(&dp->cls);
572 hmap_destroy(&dp->flow_table);
573 ovs_mutex_destroy(&dp->flow_mutex);
574 seq_destroy(dp->port_seq);
575 hmap_destroy(&dp->ports);
576 latch_destroy(&dp->exit_latch);
577 free(CONST_CAST(char *, dp->name));
582 dp_netdev_unref(struct dp_netdev *dp)
585 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
586 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
587 ovs_mutex_lock(&dp_netdev_mutex);
588 if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
591 ovs_mutex_unlock(&dp_netdev_mutex);
596 dpif_netdev_close(struct dpif *dpif)
598 struct dp_netdev *dp = get_dp_netdev(dpif);
605 dpif_netdev_destroy(struct dpif *dpif)
607 struct dp_netdev *dp = get_dp_netdev(dpif);
609 if (!atomic_flag_test_and_set(&dp->destroyed)) {
610 if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
611 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
620 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
622 struct dp_netdev *dp = get_dp_netdev(dpif);
623 struct dp_netdev_stats *bucket;
626 fat_rwlock_rdlock(&dp->cls.rwlock);
627 stats->n_flows = hmap_count(&dp->flow_table);
628 fat_rwlock_unlock(&dp->cls.rwlock);
630 stats->n_hit = stats->n_missed = stats->n_lost = 0;
631 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
632 ovs_mutex_lock(&bucket->mutex);
633 stats->n_hit += bucket->n[DP_STAT_HIT];
634 stats->n_missed += bucket->n[DP_STAT_MISS];
635 stats->n_lost += bucket->n[DP_STAT_LOST];
636 ovs_mutex_unlock(&bucket->mutex);
638 stats->n_masks = UINT32_MAX;
639 stats->n_mask_hit = UINT64_MAX;
645 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
647 OVS_REQ_WRLOCK(dp->port_rwlock)
649 struct netdev_saved_flags *sf;
650 struct dp_netdev_port *port;
651 struct netdev *netdev;
652 struct netdev_rx *rx;
653 enum netdev_flags flags;
654 const char *open_type;
657 /* XXX reject devices already in some dp_netdev. */
659 /* Open and validate network device. */
660 open_type = dpif_netdev_port_open_type(dp->class, type);
661 error = netdev_open(devname, open_type, &netdev);
665 /* XXX reject non-Ethernet devices */
667 netdev_get_flags(netdev, &flags);
668 if (flags & NETDEV_LOOPBACK) {
669 VLOG_ERR("%s: cannot add a loopback device", devname);
670 netdev_close(netdev);
674 error = netdev_rx_open(netdev, &rx);
676 && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
677 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
678 devname, ovs_strerror(errno));
679 netdev_close(netdev);
683 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
686 netdev_close(netdev);
690 port = xmalloc(sizeof *port);
691 port->port_no = port_no;
692 port->netdev = netdev;
695 port->type = xstrdup(type);
697 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
698 seq_change(dp->port_seq);
704 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
705 odp_port_t *port_nop)
707 struct dp_netdev *dp = get_dp_netdev(dpif);
708 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
709 const char *dpif_port;
713 ovs_rwlock_wrlock(&dp->port_rwlock);
714 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
715 if (*port_nop != ODPP_NONE) {
717 error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0;
719 port_no = choose_port(dp, dpif_port);
720 error = port_no == ODPP_NONE ? EFBIG : 0;
724 error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
726 ovs_rwlock_unlock(&dp->port_rwlock);
732 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
734 struct dp_netdev *dp = get_dp_netdev(dpif);
737 ovs_rwlock_wrlock(&dp->port_rwlock);
738 error = port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
739 ovs_rwlock_unlock(&dp->port_rwlock);
745 is_valid_port_number(odp_port_t port_no)
747 return port_no != ODPP_NONE;
750 static struct dp_netdev_port *
751 dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no)
752 OVS_REQ_RDLOCK(dp->port_rwlock)
754 struct dp_netdev_port *port;
756 HMAP_FOR_EACH_IN_BUCKET (port, node, hash_int(odp_to_u32(port_no), 0),
758 if (port->port_no == port_no) {
766 get_port_by_number(struct dp_netdev *dp,
767 odp_port_t port_no, struct dp_netdev_port **portp)
768 OVS_REQ_RDLOCK(dp->port_rwlock)
770 if (!is_valid_port_number(port_no)) {
774 *portp = dp_netdev_lookup_port(dp, port_no);
775 return *portp ? 0 : ENOENT;
780 get_port_by_name(struct dp_netdev *dp,
781 const char *devname, struct dp_netdev_port **portp)
782 OVS_REQ_RDLOCK(dp->port_rwlock)
784 struct dp_netdev_port *port;
786 HMAP_FOR_EACH (port, node, &dp->ports) {
787 if (!strcmp(netdev_get_name(port->netdev), devname)) {
796 do_del_port(struct dp_netdev *dp, odp_port_t port_no)
797 OVS_REQ_WRLOCK(dp->port_rwlock)
799 struct dp_netdev_port *port;
802 error = get_port_by_number(dp, port_no, &port);
807 hmap_remove(&dp->ports, &port->node);
808 seq_change(dp->port_seq);
810 netdev_close(port->netdev);
811 netdev_restore_flags(port->sf);
812 netdev_rx_close(port->rx);
820 answer_port_query(const struct dp_netdev_port *port,
821 struct dpif_port *dpif_port)
823 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
824 dpif_port->type = xstrdup(port->type);
825 dpif_port->port_no = port->port_no;
829 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
830 struct dpif_port *dpif_port)
832 struct dp_netdev *dp = get_dp_netdev(dpif);
833 struct dp_netdev_port *port;
836 ovs_rwlock_rdlock(&dp->port_rwlock);
837 error = get_port_by_number(dp, port_no, &port);
838 if (!error && dpif_port) {
839 answer_port_query(port, dpif_port);
841 ovs_rwlock_unlock(&dp->port_rwlock);
847 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
848 struct dpif_port *dpif_port)
850 struct dp_netdev *dp = get_dp_netdev(dpif);
851 struct dp_netdev_port *port;
854 ovs_rwlock_rdlock(&dp->port_rwlock);
855 error = get_port_by_name(dp, devname, &port);
856 if (!error && dpif_port) {
857 answer_port_query(port, dpif_port);
859 ovs_rwlock_unlock(&dp->port_rwlock);
865 dp_netdev_flow_free(struct dp_netdev_flow *flow)
867 struct dp_netdev_flow_stats *bucket;
870 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &flow->stats) {
871 ovs_mutex_destroy(&bucket->mutex);
872 free_cacheline(bucket);
874 ovsthread_stats_destroy(&flow->stats);
876 cls_rule_destroy(CONST_CAST(struct cls_rule *, &flow->cr));
877 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow));
878 ovs_mutex_destroy(&flow->mutex);
883 dp_netdev_remove_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
884 OVS_REQ_WRLOCK(dp->cls.rwlock)
885 OVS_REQUIRES(dp->flow_mutex)
887 struct cls_rule *cr = CONST_CAST(struct cls_rule *, &flow->cr);
888 struct hmap_node *node = CONST_CAST(struct hmap_node *, &flow->node);
890 classifier_remove(&dp->cls, cr);
891 hmap_remove(&dp->flow_table, node);
892 ovsrcu_postpone(dp_netdev_flow_free, flow);
896 dp_netdev_flow_flush(struct dp_netdev *dp)
898 struct dp_netdev_flow *netdev_flow, *next;
900 ovs_mutex_lock(&dp->flow_mutex);
901 fat_rwlock_wrlock(&dp->cls.rwlock);
902 HMAP_FOR_EACH_SAFE (netdev_flow, next, node, &dp->flow_table) {
903 dp_netdev_remove_flow(dp, netdev_flow);
905 fat_rwlock_unlock(&dp->cls.rwlock);
906 ovs_mutex_unlock(&dp->flow_mutex);
910 dpif_netdev_flow_flush(struct dpif *dpif)
912 struct dp_netdev *dp = get_dp_netdev(dpif);
914 dp_netdev_flow_flush(dp);
918 struct dp_netdev_port_state {
925 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
927 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
932 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
933 struct dpif_port *dpif_port)
935 struct dp_netdev_port_state *state = state_;
936 struct dp_netdev *dp = get_dp_netdev(dpif);
937 struct hmap_node *node;
940 ovs_rwlock_rdlock(&dp->port_rwlock);
941 node = hmap_at_position(&dp->ports, &state->bucket, &state->offset);
943 struct dp_netdev_port *port;
945 port = CONTAINER_OF(node, struct dp_netdev_port, node);
948 state->name = xstrdup(netdev_get_name(port->netdev));
949 dpif_port->name = state->name;
950 dpif_port->type = port->type;
951 dpif_port->port_no = port->port_no;
957 ovs_rwlock_unlock(&dp->port_rwlock);
963 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
965 struct dp_netdev_port_state *state = state_;
972 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
974 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
975 uint64_t new_port_seq;
978 new_port_seq = seq_read(dpif->dp->port_seq);
979 if (dpif->last_port_seq != new_port_seq) {
980 dpif->last_port_seq = new_port_seq;
990 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
992 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
994 seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
997 static struct dp_netdev_flow *
998 dp_netdev_flow_cast(const struct cls_rule *cr)
1000 return cr ? CONTAINER_OF(cr, struct dp_netdev_flow, cr) : NULL;
1003 static struct dp_netdev_flow *
1004 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *flow)
1005 OVS_EXCLUDED(dp->cls.rwlock)
1007 struct dp_netdev_flow *netdev_flow;
1009 fat_rwlock_rdlock(&dp->cls.rwlock);
1010 netdev_flow = dp_netdev_flow_cast(classifier_lookup(&dp->cls, flow, NULL));
1011 fat_rwlock_unlock(&dp->cls.rwlock);
1016 static struct dp_netdev_flow *
1017 dp_netdev_find_flow(const struct dp_netdev *dp, const struct flow *flow)
1018 OVS_REQ_RDLOCK(dp->cls.rwlock)
1020 struct dp_netdev_flow *netdev_flow;
1022 HMAP_FOR_EACH_WITH_HASH (netdev_flow, node, flow_hash(flow, 0),
1024 if (flow_equal(&netdev_flow->flow, flow)) {
1033 get_dpif_flow_stats(struct dp_netdev_flow *netdev_flow,
1034 struct dpif_flow_stats *stats)
1036 struct dp_netdev_flow_stats *bucket;
1039 memset(stats, 0, sizeof *stats);
1040 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1041 ovs_mutex_lock(&bucket->mutex);
1042 stats->n_packets += bucket->packet_count;
1043 stats->n_bytes += bucket->byte_count;
1044 stats->used = MAX(stats->used, bucket->used);
1045 stats->tcp_flags |= bucket->tcp_flags;
1046 ovs_mutex_unlock(&bucket->mutex);
1051 dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1052 const struct nlattr *mask_key,
1053 uint32_t mask_key_len, const struct flow *flow,
1057 enum odp_key_fitness fitness;
1059 fitness = odp_flow_key_to_mask(mask_key, mask_key_len, mask, flow);
1061 /* This should not happen: it indicates that
1062 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
1063 * disagree on the acceptable form of a mask. Log the problem
1064 * as an error, with enough details to enable debugging. */
1065 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1067 if (!VLOG_DROP_ERR(&rl)) {
1071 odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s,
1073 VLOG_ERR("internal error parsing flow mask %s (%s)",
1074 ds_cstr(&s), odp_key_fitness_to_string(fitness));
1080 /* Force unwildcard the in_port. */
1081 mask->in_port.odp_port = u32_to_odp(UINT32_MAX);
1083 enum mf_field_id id;
1084 /* No mask key, unwildcard everything except fields whose
1085 * prerequisities are not met. */
1086 memset(mask, 0x0, sizeof *mask);
1088 for (id = 0; id < MFF_N_IDS; ++id) {
1089 /* Skip registers and metadata. */
1090 if (!(id >= MFF_REG0 && id < MFF_REG0 + FLOW_N_REGS)
1091 && id != MFF_METADATA) {
1092 const struct mf_field *mf = mf_from_id(id);
1093 if (mf_are_prereqs_ok(mf, flow)) {
1094 mf_mask_field(mf, mask);
1104 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1109 if (odp_flow_key_to_flow(key, key_len, flow)) {
1110 /* This should not happen: it indicates that odp_flow_key_from_flow()
1111 * and odp_flow_key_to_flow() disagree on the acceptable form of a
1112 * flow. Log the problem as an error, with enough details to enable
1114 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1116 if (!VLOG_DROP_ERR(&rl)) {
1120 odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
1121 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
1128 in_port = flow->in_port.odp_port;
1129 if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
1137 dpif_netdev_flow_get(const struct dpif *dpif,
1138 const struct nlattr *nl_key, size_t nl_key_len,
1139 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
1141 struct dp_netdev *dp = get_dp_netdev(dpif);
1142 struct dp_netdev_flow *netdev_flow;
1146 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
1151 fat_rwlock_rdlock(&dp->cls.rwlock);
1152 netdev_flow = dp_netdev_find_flow(dp, &key);
1153 fat_rwlock_unlock(&dp->cls.rwlock);
1157 get_dpif_flow_stats(netdev_flow, stats);
1161 struct dp_netdev_actions *actions;
1163 actions = dp_netdev_flow_get_actions(netdev_flow);
1164 *actionsp = ofpbuf_clone_data(actions->actions, actions->size);
1174 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *flow,
1175 const struct flow_wildcards *wc,
1176 const struct nlattr *actions,
1178 OVS_REQUIRES(dp->flow_mutex)
1180 struct dp_netdev_flow *netdev_flow;
1183 netdev_flow = xzalloc(sizeof *netdev_flow);
1184 *CONST_CAST(struct flow *, &netdev_flow->flow) = *flow;
1186 ovs_mutex_init(&netdev_flow->mutex);
1188 ovsthread_stats_init(&netdev_flow->stats);
1190 ovsrcu_set(&netdev_flow->actions,
1191 dp_netdev_actions_create(actions, actions_len));
1193 match_init(&match, flow, wc);
1194 cls_rule_init(CONST_CAST(struct cls_rule *, &netdev_flow->cr),
1195 &match, NETDEV_RULE_PRIORITY);
1196 fat_rwlock_wrlock(&dp->cls.rwlock);
1197 classifier_insert(&dp->cls,
1198 CONST_CAST(struct cls_rule *, &netdev_flow->cr));
1199 hmap_insert(&dp->flow_table,
1200 CONST_CAST(struct hmap_node *, &netdev_flow->node),
1201 flow_hash(flow, 0));
1202 fat_rwlock_unlock(&dp->cls.rwlock);
1208 clear_stats(struct dp_netdev_flow *netdev_flow)
1210 struct dp_netdev_flow_stats *bucket;
1213 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1214 ovs_mutex_lock(&bucket->mutex);
1216 bucket->packet_count = 0;
1217 bucket->byte_count = 0;
1218 bucket->tcp_flags = 0;
1219 ovs_mutex_unlock(&bucket->mutex);
1224 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
1226 struct dp_netdev *dp = get_dp_netdev(dpif);
1227 struct dp_netdev_flow *netdev_flow;
1229 struct flow_wildcards wc;
1232 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &flow);
1236 error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
1237 put->mask, put->mask_len,
1243 ovs_mutex_lock(&dp->flow_mutex);
1244 netdev_flow = dp_netdev_lookup_flow(dp, &flow);
1246 if (put->flags & DPIF_FP_CREATE) {
1247 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
1249 memset(put->stats, 0, sizeof *put->stats);
1251 error = dp_netdev_flow_add(dp, &flow, &wc, put->actions,
1260 if (put->flags & DPIF_FP_MODIFY
1261 && flow_equal(&flow, &netdev_flow->flow)) {
1262 struct dp_netdev_actions *new_actions;
1263 struct dp_netdev_actions *old_actions;
1265 new_actions = dp_netdev_actions_create(put->actions,
1268 old_actions = dp_netdev_flow_get_actions(netdev_flow);
1269 ovsrcu_set(&netdev_flow->actions, new_actions);
1272 get_dpif_flow_stats(netdev_flow, put->stats);
1274 if (put->flags & DPIF_FP_ZERO_STATS) {
1275 clear_stats(netdev_flow);
1278 ovsrcu_postpone(dp_netdev_actions_free, old_actions);
1279 } else if (put->flags & DPIF_FP_CREATE) {
1282 /* Overlapping flow. */
1286 ovs_mutex_unlock(&dp->flow_mutex);
1292 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
1294 struct dp_netdev *dp = get_dp_netdev(dpif);
1295 struct dp_netdev_flow *netdev_flow;
1299 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
1304 ovs_mutex_lock(&dp->flow_mutex);
1305 fat_rwlock_wrlock(&dp->cls.rwlock);
1306 netdev_flow = dp_netdev_find_flow(dp, &key);
1309 get_dpif_flow_stats(netdev_flow, del->stats);
1311 dp_netdev_remove_flow(dp, netdev_flow);
1315 fat_rwlock_unlock(&dp->cls.rwlock);
1316 ovs_mutex_unlock(&dp->flow_mutex);
1321 struct dp_netdev_flow_state {
1322 struct dp_netdev_actions *actions;
1323 struct odputil_keybuf keybuf;
1324 struct odputil_keybuf maskbuf;
1325 struct dpif_flow_stats stats;
1328 struct dp_netdev_flow_iter {
1332 struct ovs_mutex mutex;
1336 dpif_netdev_flow_dump_state_init(void **statep)
1338 struct dp_netdev_flow_state *state;
1340 *statep = state = xmalloc(sizeof *state);
1341 state->actions = NULL;
1345 dpif_netdev_flow_dump_state_uninit(void *state_)
1347 struct dp_netdev_flow_state *state = state_;
1353 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **iterp)
1355 struct dp_netdev_flow_iter *iter;
1357 *iterp = iter = xmalloc(sizeof *iter);
1361 ovs_mutex_init(&iter->mutex);
1365 /* XXX the caller must use 'actions' without quiescing */
1367 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *iter_, void *state_,
1368 const struct nlattr **key, size_t *key_len,
1369 const struct nlattr **mask, size_t *mask_len,
1370 const struct nlattr **actions, size_t *actions_len,
1371 const struct dpif_flow_stats **stats)
1373 struct dp_netdev_flow_iter *iter = iter_;
1374 struct dp_netdev_flow_state *state = state_;
1375 struct dp_netdev *dp = get_dp_netdev(dpif);
1376 struct dp_netdev_flow *netdev_flow;
1379 ovs_mutex_lock(&iter->mutex);
1380 error = iter->status;
1382 struct hmap_node *node;
1384 fat_rwlock_rdlock(&dp->cls.rwlock);
1385 node = hmap_at_position(&dp->flow_table, &iter->bucket, &iter->offset);
1387 netdev_flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
1389 fat_rwlock_unlock(&dp->cls.rwlock);
1391 iter->status = error = EOF;
1394 ovs_mutex_unlock(&iter->mutex);
1402 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
1403 odp_flow_key_from_flow(&buf, &netdev_flow->flow,
1404 netdev_flow->flow.in_port.odp_port);
1407 *key_len = buf.size;
1412 struct flow_wildcards wc;
1414 ofpbuf_use_stack(&buf, &state->maskbuf, sizeof state->maskbuf);
1415 minimask_expand(&netdev_flow->cr.match.mask, &wc);
1416 odp_flow_key_from_mask(&buf, &wc.masks, &netdev_flow->flow,
1417 odp_to_u32(wc.masks.in_port.odp_port),
1421 *mask_len = buf.size;
1424 if (actions || stats) {
1425 state->actions = NULL;
1428 state->actions = dp_netdev_flow_get_actions(netdev_flow);
1429 *actions = state->actions->actions;
1430 *actions_len = state->actions->size;
1434 get_dpif_flow_stats(netdev_flow, &state->stats);
1435 *stats = &state->stats;
1443 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *iter_)
1445 struct dp_netdev_flow_iter *iter = iter_;
1447 ovs_mutex_destroy(&iter->mutex);
1453 dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
1455 struct dp_netdev *dp = get_dp_netdev(dpif);
1456 struct pkt_metadata *md = &execute->md;
1459 if (execute->packet->size < ETH_HEADER_LEN ||
1460 execute->packet->size > UINT16_MAX) {
1464 /* Extract flow key. */
1465 flow_extract(execute->packet, md, &key);
1467 ovs_rwlock_rdlock(&dp->port_rwlock);
1468 dp_netdev_execute_actions(dp, &key, execute->packet, md, execute->actions,
1469 execute->actions_len);
1470 ovs_rwlock_unlock(&dp->port_rwlock);
1476 dpif_netdev_recv_set(struct dpif *dpif OVS_UNUSED, bool enable OVS_UNUSED)
1482 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1483 uint32_t queue_id, uint32_t *priority)
1485 *priority = queue_id;
1489 static struct dp_netdev_queue *
1490 find_nonempty_queue(struct dp_netdev *dp)
1491 OVS_REQUIRES(dp->queue_mutex)
1495 for (i = 0; i < N_QUEUES; i++) {
1496 struct dp_netdev_queue *q = &dp->queues[i];
1497 if (q->head != q->tail) {
1505 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
1508 struct dp_netdev *dp = get_dp_netdev(dpif);
1509 struct dp_netdev_queue *q;
1512 ovs_mutex_lock(&dp->queue_mutex);
1513 q = find_nonempty_queue(dp);
1515 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1517 *upcall = u->upcall;
1526 ovs_mutex_unlock(&dp->queue_mutex);
1532 dpif_netdev_recv_wait(struct dpif *dpif)
1534 struct dp_netdev *dp = get_dp_netdev(dpif);
1537 ovs_mutex_lock(&dp->queue_mutex);
1538 seq = seq_read(dp->queue_seq);
1539 if (find_nonempty_queue(dp)) {
1540 poll_immediate_wake();
1542 seq_wait(dp->queue_seq, seq);
1544 ovs_mutex_unlock(&dp->queue_mutex);
1548 dpif_netdev_recv_purge(struct dpif *dpif)
1550 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1552 dp_netdev_purge_queues(dpif_netdev->dp);
1555 /* Creates and returns a new 'struct dp_netdev_actions', with a reference count
1556 * of 1, whose actions are a copy of from the 'ofpacts_len' bytes of
1558 struct dp_netdev_actions *
1559 dp_netdev_actions_create(const struct nlattr *actions, size_t size)
1561 struct dp_netdev_actions *netdev_actions;
1563 netdev_actions = xmalloc(sizeof *netdev_actions);
1564 netdev_actions->actions = xmemdup(actions, size);
1565 netdev_actions->size = size;
1567 return netdev_actions;
1570 struct dp_netdev_actions *
1571 dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow)
1573 return ovsrcu_get(struct dp_netdev_actions *, &flow->actions);
1577 dp_netdev_actions_free(struct dp_netdev_actions *actions)
1579 free(actions->actions);
1584 dp_forwarder_main(void *f_)
1586 struct dp_forwarder *f = f_;
1587 struct dp_netdev *dp = f->dp;
1588 struct ofpbuf packet;
1590 f->name = xasprintf("forwarder_%u", ovsthread_id_self());
1591 set_subprogram_name("%s", f->name);
1593 ofpbuf_init(&packet, 0);
1594 while (!latch_is_set(&dp->exit_latch)) {
1595 bool received_anything;
1598 ovs_rwlock_rdlock(&dp->port_rwlock);
1599 for (i = 0; i < 50; i++) {
1600 struct dp_netdev_port *port;
1602 received_anything = false;
1603 HMAP_FOR_EACH (port, node, &f->dp->ports) {
1605 && port->node.hash >= f->min_hash
1606 && port->node.hash <= f->max_hash) {
1611 if (netdev_get_mtu(port->netdev, &mtu)) {
1612 mtu = ETH_PAYLOAD_MAX;
1614 buf_size = DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + mtu;
1616 ofpbuf_clear(&packet);
1617 ofpbuf_reserve_with_tailroom(&packet, DP_NETDEV_HEADROOM,
1620 error = netdev_rx_recv(port->rx, &packet);
1622 struct pkt_metadata md
1623 = PKT_METADATA_INITIALIZER(port->port_no);
1625 dp_netdev_port_input(dp, &packet, &md);
1626 received_anything = true;
1627 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1628 static struct vlog_rate_limit rl
1629 = VLOG_RATE_LIMIT_INIT(1, 5);
1631 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1632 netdev_get_name(port->netdev),
1633 ovs_strerror(error));
1638 if (!received_anything) {
1643 if (received_anything) {
1644 poll_immediate_wake();
1646 struct dp_netdev_port *port;
1648 HMAP_FOR_EACH (port, node, &f->dp->ports)
1650 && port->node.hash >= f->min_hash
1651 && port->node.hash <= f->max_hash) {
1652 netdev_rx_wait(port->rx);
1654 seq_wait(dp->port_seq, seq_read(dp->port_seq));
1655 latch_wait(&dp->exit_latch);
1657 ovs_rwlock_unlock(&dp->port_rwlock);
1661 ofpbuf_uninit(&packet);
1669 dp_netdev_set_threads(struct dp_netdev *dp, int n)
1673 if (n == dp->n_forwarders) {
1677 /* Stop existing threads. */
1678 latch_set(&dp->exit_latch);
1679 for (i = 0; i < dp->n_forwarders; i++) {
1680 struct dp_forwarder *f = &dp->forwarders[i];
1682 xpthread_join(f->thread, NULL);
1684 latch_poll(&dp->exit_latch);
1685 free(dp->forwarders);
1687 /* Start new threads. */
1688 dp->forwarders = xmalloc(n * sizeof *dp->forwarders);
1689 dp->n_forwarders = n;
1690 for (i = 0; i < n; i++) {
1691 struct dp_forwarder *f = &dp->forwarders[i];
1694 f->min_hash = UINT32_MAX / n * i;
1695 f->max_hash = UINT32_MAX / n * (i + 1) - 1;
1697 f->max_hash = UINT32_MAX;
1699 xpthread_create(&f->thread, NULL, dp_forwarder_main, f);
1704 dp_netdev_flow_stats_new_cb(void)
1706 struct dp_netdev_flow_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1707 ovs_mutex_init(&bucket->mutex);
1712 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow,
1713 const struct ofpbuf *packet,
1714 const struct flow *key)
1716 uint16_t tcp_flags = ntohs(key->tcp_flags);
1717 long long int now = time_msec();
1718 struct dp_netdev_flow_stats *bucket;
1720 bucket = ovsthread_stats_bucket_get(&netdev_flow->stats,
1721 dp_netdev_flow_stats_new_cb);
1723 ovs_mutex_lock(&bucket->mutex);
1724 bucket->used = MAX(now, bucket->used);
1725 bucket->packet_count++;
1726 bucket->byte_count += packet->size;
1727 bucket->tcp_flags |= tcp_flags;
1728 ovs_mutex_unlock(&bucket->mutex);
1732 dp_netdev_stats_new_cb(void)
1734 struct dp_netdev_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1735 ovs_mutex_init(&bucket->mutex);
1740 dp_netdev_count_packet(struct dp_netdev *dp, enum dp_stat_type type)
1742 struct dp_netdev_stats *bucket;
1744 bucket = ovsthread_stats_bucket_get(&dp->stats, dp_netdev_stats_new_cb);
1745 ovs_mutex_lock(&bucket->mutex);
1747 ovs_mutex_unlock(&bucket->mutex);
1751 dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
1752 struct pkt_metadata *md)
1753 OVS_REQ_RDLOCK(dp->port_rwlock)
1755 struct dp_netdev_flow *netdev_flow;
1758 if (packet->size < ETH_HEADER_LEN) {
1761 flow_extract(packet, md, &key);
1762 netdev_flow = dp_netdev_lookup_flow(dp, &key);
1764 struct dp_netdev_actions *actions;
1766 dp_netdev_flow_used(netdev_flow, packet, &key);
1768 actions = dp_netdev_flow_get_actions(netdev_flow);
1769 dp_netdev_execute_actions(dp, &key, packet, md,
1770 actions->actions, actions->size);
1771 dp_netdev_count_packet(dp, DP_STAT_HIT);
1773 dp_netdev_count_packet(dp, DP_STAT_MISS);
1774 dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, NULL);
1779 dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *packet,
1780 int queue_no, const struct flow *flow,
1781 const struct nlattr *userdata)
1782 OVS_EXCLUDED(dp->queue_mutex)
1784 struct dp_netdev_queue *q = &dp->queues[queue_no];
1787 ovs_mutex_lock(&dp->queue_mutex);
1788 if (q->head - q->tail < MAX_QUEUE_LEN) {
1789 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
1790 struct dpif_upcall *upcall = &u->upcall;
1791 struct ofpbuf *buf = &u->buf;
1794 upcall->type = queue_no;
1796 /* Allocate buffer big enough for everything. */
1797 buf_size = ODPUTIL_FLOW_KEY_BYTES;
1799 buf_size += NLA_ALIGN(userdata->nla_len);
1801 ofpbuf_init(buf, buf_size);
1804 odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
1805 upcall->key = buf->data;
1806 upcall->key_len = buf->size;
1810 upcall->userdata = ofpbuf_put(buf, userdata,
1811 NLA_ALIGN(userdata->nla_len));
1814 /* Steal packet data. */
1815 ovs_assert(packet->source == OFPBUF_MALLOC);
1816 upcall->packet = *packet;
1817 ofpbuf_use(packet, NULL, 0);
1819 seq_change(dp->queue_seq);
1823 dp_netdev_count_packet(dp, DP_STAT_LOST);
1826 ovs_mutex_unlock(&dp->queue_mutex);
1831 struct dp_netdev_execute_aux {
1832 struct dp_netdev *dp;
1833 const struct flow *key;
1837 dp_execute_cb(void *aux_, struct ofpbuf *packet,
1838 const struct pkt_metadata *md OVS_UNUSED,
1839 const struct nlattr *a, bool may_steal)
1840 OVS_NO_THREAD_SAFETY_ANALYSIS
1842 struct dp_netdev_execute_aux *aux = aux_;
1843 int type = nl_attr_type(a);
1844 struct dp_netdev_port *p;
1846 switch ((enum ovs_action_attr)type) {
1847 case OVS_ACTION_ATTR_OUTPUT:
1848 p = dp_netdev_lookup_port(aux->dp, u32_to_odp(nl_attr_get_u32(a)));
1850 netdev_send(p->netdev, packet);
1854 case OVS_ACTION_ATTR_USERSPACE: {
1855 const struct nlattr *userdata;
1857 userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
1859 /* Make a copy if we are not allowed to steal the packet's data. */
1861 packet = ofpbuf_clone_with_headroom(packet, DP_NETDEV_HEADROOM);
1863 dp_netdev_output_userspace(aux->dp, packet, DPIF_UC_ACTION, aux->key,
1866 ofpbuf_uninit(packet);
1870 case OVS_ACTION_ATTR_PUSH_VLAN:
1871 case OVS_ACTION_ATTR_POP_VLAN:
1872 case OVS_ACTION_ATTR_PUSH_MPLS:
1873 case OVS_ACTION_ATTR_POP_MPLS:
1874 case OVS_ACTION_ATTR_SET:
1875 case OVS_ACTION_ATTR_SAMPLE:
1876 case OVS_ACTION_ATTR_UNSPEC:
1877 case __OVS_ACTION_ATTR_MAX:
1883 dp_netdev_execute_actions(struct dp_netdev *dp, const struct flow *key,
1884 struct ofpbuf *packet, struct pkt_metadata *md,
1885 const struct nlattr *actions, size_t actions_len)
1886 OVS_REQ_RDLOCK(dp->port_rwlock)
1888 struct dp_netdev_execute_aux aux = {dp, key};
1890 odp_execute_actions(&aux, packet, md, actions, actions_len, dp_execute_cb);
1893 const struct dpif_class dpif_netdev_class = {
1895 dpif_netdev_enumerate,
1896 dpif_netdev_port_open_type,
1899 dpif_netdev_destroy,
1902 dpif_netdev_get_stats,
1903 dpif_netdev_port_add,
1904 dpif_netdev_port_del,
1905 dpif_netdev_port_query_by_number,
1906 dpif_netdev_port_query_by_name,
1907 NULL, /* port_get_pid */
1908 dpif_netdev_port_dump_start,
1909 dpif_netdev_port_dump_next,
1910 dpif_netdev_port_dump_done,
1911 dpif_netdev_port_poll,
1912 dpif_netdev_port_poll_wait,
1913 dpif_netdev_flow_get,
1914 dpif_netdev_flow_put,
1915 dpif_netdev_flow_del,
1916 dpif_netdev_flow_flush,
1917 dpif_netdev_flow_dump_state_init,
1918 dpif_netdev_flow_dump_start,
1919 dpif_netdev_flow_dump_next,
1921 dpif_netdev_flow_dump_done,
1922 dpif_netdev_flow_dump_state_uninit,
1923 dpif_netdev_execute,
1925 dpif_netdev_recv_set,
1926 dpif_netdev_queue_to_priority,
1928 dpif_netdev_recv_wait,
1929 dpif_netdev_recv_purge,
1933 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
1934 const char *argv[], void *aux OVS_UNUSED)
1936 struct dp_netdev_port *port;
1937 struct dp_netdev *dp;
1940 ovs_mutex_lock(&dp_netdev_mutex);
1941 dp = shash_find_data(&dp_netdevs, argv[1]);
1942 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
1943 ovs_mutex_unlock(&dp_netdev_mutex);
1944 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
1947 ovs_refcount_ref(&dp->ref_cnt);
1948 ovs_mutex_unlock(&dp_netdev_mutex);
1950 ovs_rwlock_wrlock(&dp->port_rwlock);
1951 if (get_port_by_name(dp, argv[2], &port)) {
1952 unixctl_command_reply_error(conn, "unknown port");
1956 port_no = u32_to_odp(atoi(argv[3]));
1957 if (!port_no || port_no == ODPP_NONE) {
1958 unixctl_command_reply_error(conn, "bad port number");
1961 if (dp_netdev_lookup_port(dp, port_no)) {
1962 unixctl_command_reply_error(conn, "port number already in use");
1965 hmap_remove(&dp->ports, &port->node);
1966 port->port_no = port_no;
1967 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
1968 seq_change(dp->port_seq);
1969 unixctl_command_reply(conn, NULL);
1972 ovs_rwlock_unlock(&dp->port_rwlock);
1973 dp_netdev_unref(dp);
1977 dpif_dummy_register__(const char *type)
1979 struct dpif_class *class;
1981 class = xmalloc(sizeof *class);
1982 *class = dpif_netdev_class;
1983 class->type = xstrdup(type);
1984 dp_register_provider(class);
1988 dpif_dummy_register(bool override)
1995 dp_enumerate_types(&types);
1996 SSET_FOR_EACH (type, &types) {
1997 if (!dp_unregister_provider(type)) {
1998 dpif_dummy_register__(type);
2001 sset_destroy(&types);
2004 dpif_dummy_register__("dummy");
2006 unixctl_command_register("dpif-dummy/change-port-number",
2007 "DP PORT NEW-NUMBER",
2008 3, 3, dpif_dummy_change_port_number, NULL);