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 { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
78 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
79 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
81 /* Protects against changes to 'dp_netdevs'. */
82 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
84 /* Contains all 'struct dp_netdev's. */
85 static struct shash dp_netdevs OVS_GUARDED_BY(dp_netdev_mutex)
86 = SHASH_INITIALIZER(&dp_netdevs);
88 struct dp_netdev_upcall {
89 struct dpif_upcall upcall; /* Queued upcall information. */
90 struct ofpbuf buf; /* ofpbuf instance for upcall.packet. */
93 /* A queue passing packets from a struct dp_netdev to its clients (handlers).
99 * Any access at all requires the owning 'dp_netdev''s queue_rwlock and
101 struct dp_netdev_queue {
102 struct ovs_mutex mutex;
103 struct seq *seq; /* Incremented whenever a packet is queued. */
104 struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN] OVS_GUARDED;
105 unsigned int head OVS_GUARDED;
106 unsigned int tail OVS_GUARDED;
109 /* Datapath based on the network device interface from netdev.h.
115 * Some members, marked 'const', are immutable. Accessing other members
116 * requires synchronization, as noted in more detail below.
118 * Acquisition order is, from outermost to innermost:
120 * dp_netdev_mutex (global)
127 const struct dpif_class *const class;
128 const char *const name;
129 struct ovs_refcount ref_cnt;
130 atomic_flag destroyed;
134 * Readers of 'cls' and 'flow_table' must take a 'cls->rwlock' read lock.
136 * Writers of 'cls' and 'flow_table' must take the 'flow_mutex' and then
137 * the 'cls->rwlock' write lock. (The outer 'flow_mutex' allows writers to
138 * atomically perform multiple operations on 'cls' and 'flow_table'.)
140 struct ovs_mutex flow_mutex;
141 struct classifier cls; /* Classifier. Protected by cls.rwlock. */
142 struct hmap flow_table OVS_GUARDED; /* Flow table. */
146 * 'queue_rwlock' protects the modification of 'handler_queues' and
147 * 'n_handlers'. The queue elements are protected by its
148 * 'handler_queues''s mutex. */
149 struct fat_rwlock queue_rwlock;
150 struct dp_netdev_queue *handler_queues;
155 * ovsthread_stats is internally synchronized. */
156 struct ovsthread_stats stats; /* Contains 'struct dp_netdev_stats *'. */
160 * Any lookup into 'ports' or any access to the dp_netdev_ports found
161 * through 'ports' requires taking 'port_rwlock'. */
162 struct ovs_rwlock port_rwlock;
163 struct hmap ports OVS_GUARDED;
164 struct seq *port_seq; /* Incremented whenever a port changes. */
166 /* Forwarding threads. */
167 struct latch exit_latch;
168 struct dp_forwarder *forwarders;
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_rx *rx;
199 char *type; /* Port type as requested by user. */
202 /* A flow in dp_netdev's 'flow_table'.
208 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
209 * its dp_netdev's classifier. The text below calls this classifier 'cls'.
214 * The thread safety rules described here for "struct dp_netdev_flow" are
215 * motivated by two goals:
217 * - Prevent threads that read members of "struct dp_netdev_flow" from
218 * reading bad data due to changes by some thread concurrently modifying
221 * - Prevent two threads making changes to members of a given "struct
222 * dp_netdev_flow" from interfering with each other.
228 * A flow 'flow' may be accessed without a risk of being freed by code that
229 * holds a read-lock or write-lock on 'cls->rwlock' or that owns a reference to
230 * 'flow->ref_cnt' (or both). Code that needs to hold onto a flow for a while
231 * should take 'cls->rwlock', find the flow it needs, increment 'flow->ref_cnt'
232 * with dpif_netdev_flow_ref(), and drop 'cls->rwlock'.
234 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
235 * flow from being deleted from 'cls' (that's 'cls->rwlock') and it doesn't
236 * protect members of 'flow' from modification (that's 'flow->mutex').
238 * 'flow->mutex' protects the members of 'flow' from modification. It doesn't
239 * protect the flow from being deleted from 'cls' (that's 'cls->rwlock') and it
240 * doesn't prevent the flow from being freed (that's 'flow->ref_cnt').
242 * Some members, marked 'const', are immutable. Accessing other members
243 * requires synchronization, as noted in more detail below.
245 struct dp_netdev_flow {
246 /* Packet classification. */
247 const struct cls_rule cr; /* In owning dp_netdev's 'cls'. */
249 /* Hash table index by unmasked flow. */
250 const struct hmap_node node; /* In owning dp_netdev's 'flow_table'. */
251 const struct flow flow; /* The flow that created this entry. */
253 /* Protects members marked OVS_GUARDED.
255 * Acquire after datapath's flow_mutex. */
256 struct ovs_mutex mutex OVS_ACQ_AFTER(dp_netdev_mutex);
260 * Reading or writing these members requires 'mutex'. */
261 struct ovsthread_stats stats; /* Contains "struct dp_netdev_flow_stats". */
265 * Reading 'actions' requires 'mutex'.
266 * Writing 'actions' requires 'mutex' and (to allow for transactions) the
267 * datapath's flow_mutex. */
268 OVSRCU_TYPE(struct dp_netdev_actions *) actions;
271 static void dp_netdev_flow_free(struct dp_netdev_flow *);
273 /* Contained by struct dp_netdev_flow's 'stats' member. */
274 struct dp_netdev_flow_stats {
275 struct ovs_mutex mutex; /* Guards all the other members. */
277 long long int used OVS_GUARDED; /* Last used time, in monotonic msecs. */
278 long long int packet_count OVS_GUARDED; /* Number of packets matched. */
279 long long int byte_count OVS_GUARDED; /* Number of bytes matched. */
280 uint16_t tcp_flags OVS_GUARDED; /* Bitwise-OR of seen tcp_flags values. */
283 /* A set of datapath actions within a "struct dp_netdev_flow".
289 * A struct dp_netdev_actions 'actions' may be accessed without a risk of being
290 * freed by code that holds a read-lock or write-lock on 'flow->mutex' (where
291 * 'flow' is the dp_netdev_flow for which 'flow->actions == actions') or that
292 * owns a reference to 'actions->ref_cnt' (or both). */
293 struct dp_netdev_actions {
294 /* These members are immutable: they do not change during the struct's
296 struct nlattr *actions; /* Sequence of OVS_ACTION_ATTR_* attributes. */
297 unsigned int size; /* Size of 'actions', in bytes. */
300 struct dp_netdev_actions *dp_netdev_actions_create(const struct nlattr *,
302 struct dp_netdev_actions *dp_netdev_flow_get_actions(
303 const struct dp_netdev_flow *);
304 static void dp_netdev_actions_free(struct dp_netdev_actions *);
306 /* A thread that receives packets from some ports, looks them up in the flow
307 * table, and executes the actions it finds. */
308 struct dp_forwarder {
309 struct dp_netdev *dp;
312 uint32_t min_hash, max_hash;
315 /* Interface to netdev-based datapath. */
318 struct dp_netdev *dp;
319 uint64_t last_port_seq;
322 static int get_port_by_number(struct dp_netdev *dp, odp_port_t port_no,
323 struct dp_netdev_port **portp)
324 OVS_REQ_RDLOCK(dp->port_rwlock);
325 static int get_port_by_name(struct dp_netdev *dp, const char *devname,
326 struct dp_netdev_port **portp)
327 OVS_REQ_RDLOCK(dp->port_rwlock);
328 static void dp_netdev_free(struct dp_netdev *)
329 OVS_REQUIRES(dp_netdev_mutex);
330 static void dp_netdev_flow_flush(struct dp_netdev *);
331 static int do_add_port(struct dp_netdev *dp, const char *devname,
332 const char *type, odp_port_t port_no)
333 OVS_REQ_WRLOCK(dp->port_rwlock);
334 static int do_del_port(struct dp_netdev *dp, odp_port_t port_no)
335 OVS_REQ_WRLOCK(dp->port_rwlock);
336 static void dp_netdev_destroy_all_queues(struct dp_netdev *dp)
337 OVS_REQ_WRLOCK(dp->queue_rwlock);
338 static int dpif_netdev_open(const struct dpif_class *, const char *name,
339 bool create, struct dpif **);
340 static int dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *,
341 int queue_no, int type,
343 const struct nlattr *userdata)
344 OVS_EXCLUDED(dp->queue_rwlock);
345 static void dp_netdev_execute_actions(struct dp_netdev *dp,
346 const struct flow *, struct ofpbuf *,
347 struct pkt_metadata *,
348 const struct nlattr *actions,
350 OVS_REQ_RDLOCK(dp->port_rwlock);
351 static void dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
352 struct pkt_metadata *)
353 OVS_REQ_RDLOCK(dp->port_rwlock);
354 static void dp_netdev_set_threads(struct dp_netdev *, int n);
356 static struct dpif_netdev *
357 dpif_netdev_cast(const struct dpif *dpif)
359 ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
360 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
363 static struct dp_netdev *
364 get_dp_netdev(const struct dpif *dpif)
366 return dpif_netdev_cast(dpif)->dp;
370 dpif_netdev_enumerate(struct sset *all_dps)
372 struct shash_node *node;
374 ovs_mutex_lock(&dp_netdev_mutex);
375 SHASH_FOR_EACH(node, &dp_netdevs) {
376 sset_add(all_dps, node->name);
378 ovs_mutex_unlock(&dp_netdev_mutex);
384 dpif_netdev_class_is_dummy(const struct dpif_class *class)
386 return class != &dpif_netdev_class;
390 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
392 return strcmp(type, "internal") ? type
393 : dpif_netdev_class_is_dummy(class) ? "dummy"
398 create_dpif_netdev(struct dp_netdev *dp)
400 uint16_t netflow_id = hash_string(dp->name, 0);
401 struct dpif_netdev *dpif;
403 ovs_refcount_ref(&dp->ref_cnt);
405 dpif = xmalloc(sizeof *dpif);
406 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
408 dpif->last_port_seq = seq_read(dp->port_seq);
413 /* Choose an unused, non-zero port number and return it on success.
414 * Return ODPP_NONE on failure. */
416 choose_port(struct dp_netdev *dp, const char *name)
417 OVS_REQ_RDLOCK(dp->port_rwlock)
421 if (dp->class != &dpif_netdev_class) {
425 /* If the port name begins with "br", start the number search at
426 * 100 to make writing tests easier. */
427 if (!strncmp(name, "br", 2)) {
431 /* If the port name contains a number, try to assign that port number.
432 * This can make writing unit tests easier because port numbers are
434 for (p = name; *p != '\0'; p++) {
435 if (isdigit((unsigned char) *p)) {
436 port_no = start_no + strtol(p, NULL, 10);
437 if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE)
438 && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
439 return u32_to_odp(port_no);
446 for (port_no = 1; port_no <= UINT16_MAX; port_no++) {
447 if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
448 return u32_to_odp(port_no);
456 create_dp_netdev(const char *name, const struct dpif_class *class,
457 struct dp_netdev **dpp)
458 OVS_REQUIRES(dp_netdev_mutex)
460 struct dp_netdev *dp;
463 dp = xzalloc(sizeof *dp);
464 shash_add(&dp_netdevs, name, dp);
466 *CONST_CAST(const struct dpif_class **, &dp->class) = class;
467 *CONST_CAST(const char **, &dp->name) = xstrdup(name);
468 ovs_refcount_init(&dp->ref_cnt);
469 atomic_flag_clear(&dp->destroyed);
471 ovs_mutex_init(&dp->flow_mutex);
472 classifier_init(&dp->cls, NULL);
473 hmap_init(&dp->flow_table);
475 fat_rwlock_init(&dp->queue_rwlock);
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)
523 OVS_REQ_WRLOCK(dp->queue_rwlock)
527 for (i = 0; i < dp->n_handlers; i++) {
528 struct dp_netdev_queue *q = &dp->handler_queues[i];
530 ovs_mutex_lock(&q->mutex);
531 while (q->tail != q->head) {
532 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
533 ofpbuf_uninit(&u->upcall.packet);
534 ofpbuf_uninit(&u->buf);
536 ovs_mutex_unlock(&q->mutex);
540 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
541 * through the 'dp_netdevs' shash while freeing 'dp'. */
543 dp_netdev_free(struct dp_netdev *dp)
544 OVS_REQUIRES(dp_netdev_mutex)
546 struct dp_netdev_port *port, *next;
547 struct dp_netdev_stats *bucket;
550 shash_find_and_delete(&dp_netdevs, dp->name);
552 dp_netdev_set_threads(dp, 0);
553 free(dp->forwarders);
555 dp_netdev_flow_flush(dp);
556 ovs_rwlock_wrlock(&dp->port_rwlock);
557 HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) {
558 do_del_port(dp, port->port_no);
560 ovs_rwlock_unlock(&dp->port_rwlock);
562 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
563 ovs_mutex_destroy(&bucket->mutex);
564 free_cacheline(bucket);
566 ovsthread_stats_destroy(&dp->stats);
568 fat_rwlock_wrlock(&dp->queue_rwlock);
569 dp_netdev_destroy_all_queues(dp);
570 fat_rwlock_unlock(&dp->queue_rwlock);
572 fat_rwlock_destroy(&dp->queue_rwlock);
574 classifier_destroy(&dp->cls);
575 hmap_destroy(&dp->flow_table);
576 ovs_mutex_destroy(&dp->flow_mutex);
577 seq_destroy(dp->port_seq);
578 hmap_destroy(&dp->ports);
579 latch_destroy(&dp->exit_latch);
580 free(CONST_CAST(char *, dp->name));
585 dp_netdev_unref(struct dp_netdev *dp)
588 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
589 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
590 ovs_mutex_lock(&dp_netdev_mutex);
591 if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
594 ovs_mutex_unlock(&dp_netdev_mutex);
599 dpif_netdev_close(struct dpif *dpif)
601 struct dp_netdev *dp = get_dp_netdev(dpif);
608 dpif_netdev_destroy(struct dpif *dpif)
610 struct dp_netdev *dp = get_dp_netdev(dpif);
612 if (!atomic_flag_test_and_set(&dp->destroyed)) {
613 if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
614 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
623 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
625 struct dp_netdev *dp = get_dp_netdev(dpif);
626 struct dp_netdev_stats *bucket;
629 fat_rwlock_rdlock(&dp->cls.rwlock);
630 stats->n_flows = hmap_count(&dp->flow_table);
631 fat_rwlock_unlock(&dp->cls.rwlock);
633 stats->n_hit = stats->n_missed = stats->n_lost = 0;
634 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
635 ovs_mutex_lock(&bucket->mutex);
636 stats->n_hit += bucket->n[DP_STAT_HIT];
637 stats->n_missed += bucket->n[DP_STAT_MISS];
638 stats->n_lost += bucket->n[DP_STAT_LOST];
639 ovs_mutex_unlock(&bucket->mutex);
641 stats->n_masks = UINT32_MAX;
642 stats->n_mask_hit = UINT64_MAX;
648 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
650 OVS_REQ_WRLOCK(dp->port_rwlock)
652 struct netdev_saved_flags *sf;
653 struct dp_netdev_port *port;
654 struct netdev *netdev;
655 struct netdev_rx *rx;
656 enum netdev_flags flags;
657 const char *open_type;
660 /* XXX reject devices already in some dp_netdev. */
662 /* Open and validate network device. */
663 open_type = dpif_netdev_port_open_type(dp->class, type);
664 error = netdev_open(devname, open_type, &netdev);
668 /* XXX reject non-Ethernet devices */
670 netdev_get_flags(netdev, &flags);
671 if (flags & NETDEV_LOOPBACK) {
672 VLOG_ERR("%s: cannot add a loopback device", devname);
673 netdev_close(netdev);
677 error = netdev_rx_open(netdev, &rx);
679 && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
680 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
681 devname, ovs_strerror(errno));
682 netdev_close(netdev);
686 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
689 netdev_close(netdev);
693 port = xmalloc(sizeof *port);
694 port->port_no = port_no;
695 port->netdev = netdev;
698 port->type = xstrdup(type);
700 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
701 seq_change(dp->port_seq);
707 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
708 odp_port_t *port_nop)
710 struct dp_netdev *dp = get_dp_netdev(dpif);
711 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
712 const char *dpif_port;
716 ovs_rwlock_wrlock(&dp->port_rwlock);
717 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
718 if (*port_nop != ODPP_NONE) {
720 error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0;
722 port_no = choose_port(dp, dpif_port);
723 error = port_no == ODPP_NONE ? EFBIG : 0;
727 error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
729 ovs_rwlock_unlock(&dp->port_rwlock);
735 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
737 struct dp_netdev *dp = get_dp_netdev(dpif);
740 ovs_rwlock_wrlock(&dp->port_rwlock);
741 error = port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
742 ovs_rwlock_unlock(&dp->port_rwlock);
748 is_valid_port_number(odp_port_t port_no)
750 return port_no != ODPP_NONE;
753 static struct dp_netdev_port *
754 dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no)
755 OVS_REQ_RDLOCK(dp->port_rwlock)
757 struct dp_netdev_port *port;
759 HMAP_FOR_EACH_IN_BUCKET (port, node, hash_int(odp_to_u32(port_no), 0),
761 if (port->port_no == port_no) {
769 get_port_by_number(struct dp_netdev *dp,
770 odp_port_t port_no, struct dp_netdev_port **portp)
771 OVS_REQ_RDLOCK(dp->port_rwlock)
773 if (!is_valid_port_number(port_no)) {
777 *portp = dp_netdev_lookup_port(dp, port_no);
778 return *portp ? 0 : ENOENT;
783 get_port_by_name(struct dp_netdev *dp,
784 const char *devname, struct dp_netdev_port **portp)
785 OVS_REQ_RDLOCK(dp->port_rwlock)
787 struct dp_netdev_port *port;
789 HMAP_FOR_EACH (port, node, &dp->ports) {
790 if (!strcmp(netdev_get_name(port->netdev), devname)) {
799 do_del_port(struct dp_netdev *dp, odp_port_t port_no)
800 OVS_REQ_WRLOCK(dp->port_rwlock)
802 struct dp_netdev_port *port;
805 error = get_port_by_number(dp, port_no, &port);
810 hmap_remove(&dp->ports, &port->node);
811 seq_change(dp->port_seq);
813 netdev_close(port->netdev);
814 netdev_restore_flags(port->sf);
815 netdev_rx_close(port->rx);
823 answer_port_query(const struct dp_netdev_port *port,
824 struct dpif_port *dpif_port)
826 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
827 dpif_port->type = xstrdup(port->type);
828 dpif_port->port_no = port->port_no;
832 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
833 struct dpif_port *dpif_port)
835 struct dp_netdev *dp = get_dp_netdev(dpif);
836 struct dp_netdev_port *port;
839 ovs_rwlock_rdlock(&dp->port_rwlock);
840 error = get_port_by_number(dp, port_no, &port);
841 if (!error && dpif_port) {
842 answer_port_query(port, dpif_port);
844 ovs_rwlock_unlock(&dp->port_rwlock);
850 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
851 struct dpif_port *dpif_port)
853 struct dp_netdev *dp = get_dp_netdev(dpif);
854 struct dp_netdev_port *port;
857 ovs_rwlock_rdlock(&dp->port_rwlock);
858 error = get_port_by_name(dp, devname, &port);
859 if (!error && dpif_port) {
860 answer_port_query(port, dpif_port);
862 ovs_rwlock_unlock(&dp->port_rwlock);
868 dp_netdev_flow_free(struct dp_netdev_flow *flow)
870 struct dp_netdev_flow_stats *bucket;
873 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &flow->stats) {
874 ovs_mutex_destroy(&bucket->mutex);
875 free_cacheline(bucket);
877 ovsthread_stats_destroy(&flow->stats);
879 cls_rule_destroy(CONST_CAST(struct cls_rule *, &flow->cr));
880 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow));
881 ovs_mutex_destroy(&flow->mutex);
886 dp_netdev_remove_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
887 OVS_REQ_WRLOCK(dp->cls.rwlock)
888 OVS_REQUIRES(dp->flow_mutex)
890 struct cls_rule *cr = CONST_CAST(struct cls_rule *, &flow->cr);
891 struct hmap_node *node = CONST_CAST(struct hmap_node *, &flow->node);
893 classifier_remove(&dp->cls, cr);
894 hmap_remove(&dp->flow_table, node);
895 ovsrcu_postpone(dp_netdev_flow_free, flow);
899 dp_netdev_flow_flush(struct dp_netdev *dp)
901 struct dp_netdev_flow *netdev_flow, *next;
903 ovs_mutex_lock(&dp->flow_mutex);
904 fat_rwlock_wrlock(&dp->cls.rwlock);
905 HMAP_FOR_EACH_SAFE (netdev_flow, next, node, &dp->flow_table) {
906 dp_netdev_remove_flow(dp, netdev_flow);
908 fat_rwlock_unlock(&dp->cls.rwlock);
909 ovs_mutex_unlock(&dp->flow_mutex);
913 dpif_netdev_flow_flush(struct dpif *dpif)
915 struct dp_netdev *dp = get_dp_netdev(dpif);
917 dp_netdev_flow_flush(dp);
921 struct dp_netdev_port_state {
928 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
930 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
935 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
936 struct dpif_port *dpif_port)
938 struct dp_netdev_port_state *state = state_;
939 struct dp_netdev *dp = get_dp_netdev(dpif);
940 struct hmap_node *node;
943 ovs_rwlock_rdlock(&dp->port_rwlock);
944 node = hmap_at_position(&dp->ports, &state->bucket, &state->offset);
946 struct dp_netdev_port *port;
948 port = CONTAINER_OF(node, struct dp_netdev_port, node);
951 state->name = xstrdup(netdev_get_name(port->netdev));
952 dpif_port->name = state->name;
953 dpif_port->type = port->type;
954 dpif_port->port_no = port->port_no;
960 ovs_rwlock_unlock(&dp->port_rwlock);
966 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
968 struct dp_netdev_port_state *state = state_;
975 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
977 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
978 uint64_t new_port_seq;
981 new_port_seq = seq_read(dpif->dp->port_seq);
982 if (dpif->last_port_seq != new_port_seq) {
983 dpif->last_port_seq = new_port_seq;
993 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
995 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
997 seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
1000 static struct dp_netdev_flow *
1001 dp_netdev_flow_cast(const struct cls_rule *cr)
1003 return cr ? CONTAINER_OF(cr, struct dp_netdev_flow, cr) : NULL;
1006 static struct dp_netdev_flow *
1007 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *flow)
1008 OVS_EXCLUDED(dp->cls.rwlock)
1010 struct dp_netdev_flow *netdev_flow;
1012 fat_rwlock_rdlock(&dp->cls.rwlock);
1013 netdev_flow = dp_netdev_flow_cast(classifier_lookup(&dp->cls, flow, NULL));
1014 fat_rwlock_unlock(&dp->cls.rwlock);
1019 static struct dp_netdev_flow *
1020 dp_netdev_find_flow(const struct dp_netdev *dp, const struct flow *flow)
1021 OVS_REQ_RDLOCK(dp->cls.rwlock)
1023 struct dp_netdev_flow *netdev_flow;
1025 HMAP_FOR_EACH_WITH_HASH (netdev_flow, node, flow_hash(flow, 0),
1027 if (flow_equal(&netdev_flow->flow, flow)) {
1036 get_dpif_flow_stats(struct dp_netdev_flow *netdev_flow,
1037 struct dpif_flow_stats *stats)
1039 struct dp_netdev_flow_stats *bucket;
1042 memset(stats, 0, sizeof *stats);
1043 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1044 ovs_mutex_lock(&bucket->mutex);
1045 stats->n_packets += bucket->packet_count;
1046 stats->n_bytes += bucket->byte_count;
1047 stats->used = MAX(stats->used, bucket->used);
1048 stats->tcp_flags |= bucket->tcp_flags;
1049 ovs_mutex_unlock(&bucket->mutex);
1054 dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1055 const struct nlattr *mask_key,
1056 uint32_t mask_key_len, const struct flow *flow,
1060 enum odp_key_fitness fitness;
1062 fitness = odp_flow_key_to_mask(mask_key, mask_key_len, mask, flow);
1064 /* This should not happen: it indicates that
1065 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
1066 * disagree on the acceptable form of a mask. Log the problem
1067 * as an error, with enough details to enable debugging. */
1068 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1070 if (!VLOG_DROP_ERR(&rl)) {
1074 odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s,
1076 VLOG_ERR("internal error parsing flow mask %s (%s)",
1077 ds_cstr(&s), odp_key_fitness_to_string(fitness));
1083 /* Force unwildcard the in_port. */
1084 mask->in_port.odp_port = u32_to_odp(UINT32_MAX);
1086 enum mf_field_id id;
1087 /* No mask key, unwildcard everything except fields whose
1088 * prerequisities are not met. */
1089 memset(mask, 0x0, sizeof *mask);
1091 for (id = 0; id < MFF_N_IDS; ++id) {
1092 /* Skip registers and metadata. */
1093 if (!(id >= MFF_REG0 && id < MFF_REG0 + FLOW_N_REGS)
1094 && id != MFF_METADATA) {
1095 const struct mf_field *mf = mf_from_id(id);
1096 if (mf_are_prereqs_ok(mf, flow)) {
1097 mf_mask_field(mf, mask);
1107 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1112 if (odp_flow_key_to_flow(key, key_len, flow)) {
1113 /* This should not happen: it indicates that odp_flow_key_from_flow()
1114 * and odp_flow_key_to_flow() disagree on the acceptable form of a
1115 * flow. Log the problem as an error, with enough details to enable
1117 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1119 if (!VLOG_DROP_ERR(&rl)) {
1123 odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
1124 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
1131 in_port = flow->in_port.odp_port;
1132 if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
1140 dpif_netdev_flow_get(const struct dpif *dpif,
1141 const struct nlattr *nl_key, size_t nl_key_len,
1142 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
1144 struct dp_netdev *dp = get_dp_netdev(dpif);
1145 struct dp_netdev_flow *netdev_flow;
1149 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
1154 fat_rwlock_rdlock(&dp->cls.rwlock);
1155 netdev_flow = dp_netdev_find_flow(dp, &key);
1156 fat_rwlock_unlock(&dp->cls.rwlock);
1160 get_dpif_flow_stats(netdev_flow, stats);
1164 struct dp_netdev_actions *actions;
1166 actions = dp_netdev_flow_get_actions(netdev_flow);
1167 *actionsp = ofpbuf_clone_data(actions->actions, actions->size);
1177 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *flow,
1178 const struct flow_wildcards *wc,
1179 const struct nlattr *actions,
1181 OVS_REQUIRES(dp->flow_mutex)
1183 struct dp_netdev_flow *netdev_flow;
1186 netdev_flow = xzalloc(sizeof *netdev_flow);
1187 *CONST_CAST(struct flow *, &netdev_flow->flow) = *flow;
1189 ovs_mutex_init(&netdev_flow->mutex);
1191 ovsthread_stats_init(&netdev_flow->stats);
1193 ovsrcu_set(&netdev_flow->actions,
1194 dp_netdev_actions_create(actions, actions_len));
1196 match_init(&match, flow, wc);
1197 cls_rule_init(CONST_CAST(struct cls_rule *, &netdev_flow->cr),
1198 &match, NETDEV_RULE_PRIORITY);
1199 fat_rwlock_wrlock(&dp->cls.rwlock);
1200 classifier_insert(&dp->cls,
1201 CONST_CAST(struct cls_rule *, &netdev_flow->cr));
1202 hmap_insert(&dp->flow_table,
1203 CONST_CAST(struct hmap_node *, &netdev_flow->node),
1204 flow_hash(flow, 0));
1205 fat_rwlock_unlock(&dp->cls.rwlock);
1211 clear_stats(struct dp_netdev_flow *netdev_flow)
1213 struct dp_netdev_flow_stats *bucket;
1216 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1217 ovs_mutex_lock(&bucket->mutex);
1219 bucket->packet_count = 0;
1220 bucket->byte_count = 0;
1221 bucket->tcp_flags = 0;
1222 ovs_mutex_unlock(&bucket->mutex);
1227 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
1229 struct dp_netdev *dp = get_dp_netdev(dpif);
1230 struct dp_netdev_flow *netdev_flow;
1232 struct flow_wildcards wc;
1235 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &flow);
1239 error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
1240 put->mask, put->mask_len,
1246 ovs_mutex_lock(&dp->flow_mutex);
1247 netdev_flow = dp_netdev_lookup_flow(dp, &flow);
1249 if (put->flags & DPIF_FP_CREATE) {
1250 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
1252 memset(put->stats, 0, sizeof *put->stats);
1254 error = dp_netdev_flow_add(dp, &flow, &wc, put->actions,
1263 if (put->flags & DPIF_FP_MODIFY
1264 && flow_equal(&flow, &netdev_flow->flow)) {
1265 struct dp_netdev_actions *new_actions;
1266 struct dp_netdev_actions *old_actions;
1268 new_actions = dp_netdev_actions_create(put->actions,
1271 old_actions = dp_netdev_flow_get_actions(netdev_flow);
1272 ovsrcu_set(&netdev_flow->actions, new_actions);
1275 get_dpif_flow_stats(netdev_flow, put->stats);
1277 if (put->flags & DPIF_FP_ZERO_STATS) {
1278 clear_stats(netdev_flow);
1281 ovsrcu_postpone(dp_netdev_actions_free, old_actions);
1282 } else if (put->flags & DPIF_FP_CREATE) {
1285 /* Overlapping flow. */
1289 ovs_mutex_unlock(&dp->flow_mutex);
1295 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
1297 struct dp_netdev *dp = get_dp_netdev(dpif);
1298 struct dp_netdev_flow *netdev_flow;
1302 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
1307 ovs_mutex_lock(&dp->flow_mutex);
1308 fat_rwlock_wrlock(&dp->cls.rwlock);
1309 netdev_flow = dp_netdev_find_flow(dp, &key);
1312 get_dpif_flow_stats(netdev_flow, del->stats);
1314 dp_netdev_remove_flow(dp, netdev_flow);
1318 fat_rwlock_unlock(&dp->cls.rwlock);
1319 ovs_mutex_unlock(&dp->flow_mutex);
1324 struct dp_netdev_flow_state {
1325 struct dp_netdev_actions *actions;
1326 struct odputil_keybuf keybuf;
1327 struct odputil_keybuf maskbuf;
1328 struct dpif_flow_stats stats;
1331 struct dp_netdev_flow_iter {
1335 struct ovs_mutex mutex;
1339 dpif_netdev_flow_dump_state_init(void **statep)
1341 struct dp_netdev_flow_state *state;
1343 *statep = state = xmalloc(sizeof *state);
1344 state->actions = NULL;
1348 dpif_netdev_flow_dump_state_uninit(void *state_)
1350 struct dp_netdev_flow_state *state = state_;
1356 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **iterp)
1358 struct dp_netdev_flow_iter *iter;
1360 *iterp = iter = xmalloc(sizeof *iter);
1364 ovs_mutex_init(&iter->mutex);
1368 /* XXX the caller must use 'actions' without quiescing */
1370 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *iter_, void *state_,
1371 const struct nlattr **key, size_t *key_len,
1372 const struct nlattr **mask, size_t *mask_len,
1373 const struct nlattr **actions, size_t *actions_len,
1374 const struct dpif_flow_stats **stats)
1376 struct dp_netdev_flow_iter *iter = iter_;
1377 struct dp_netdev_flow_state *state = state_;
1378 struct dp_netdev *dp = get_dp_netdev(dpif);
1379 struct dp_netdev_flow *netdev_flow;
1382 ovs_mutex_lock(&iter->mutex);
1383 error = iter->status;
1385 struct hmap_node *node;
1387 fat_rwlock_rdlock(&dp->cls.rwlock);
1388 node = hmap_at_position(&dp->flow_table, &iter->bucket, &iter->offset);
1390 netdev_flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
1392 fat_rwlock_unlock(&dp->cls.rwlock);
1394 iter->status = error = EOF;
1397 ovs_mutex_unlock(&iter->mutex);
1405 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
1406 odp_flow_key_from_flow(&buf, &netdev_flow->flow,
1407 netdev_flow->flow.in_port.odp_port);
1410 *key_len = buf.size;
1415 struct flow_wildcards wc;
1417 ofpbuf_use_stack(&buf, &state->maskbuf, sizeof state->maskbuf);
1418 minimask_expand(&netdev_flow->cr.match.mask, &wc);
1419 odp_flow_key_from_mask(&buf, &wc.masks, &netdev_flow->flow,
1420 odp_to_u32(wc.masks.in_port.odp_port),
1424 *mask_len = buf.size;
1427 if (actions || stats) {
1428 state->actions = NULL;
1431 state->actions = dp_netdev_flow_get_actions(netdev_flow);
1432 *actions = state->actions->actions;
1433 *actions_len = state->actions->size;
1437 get_dpif_flow_stats(netdev_flow, &state->stats);
1438 *stats = &state->stats;
1446 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *iter_)
1448 struct dp_netdev_flow_iter *iter = iter_;
1450 ovs_mutex_destroy(&iter->mutex);
1456 dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
1458 struct dp_netdev *dp = get_dp_netdev(dpif);
1459 struct pkt_metadata *md = &execute->md;
1462 if (execute->packet->size < ETH_HEADER_LEN ||
1463 execute->packet->size > UINT16_MAX) {
1467 /* Extract flow key. */
1468 flow_extract(execute->packet, md, &key);
1470 ovs_rwlock_rdlock(&dp->port_rwlock);
1471 dp_netdev_execute_actions(dp, &key, execute->packet, md, execute->actions,
1472 execute->actions_len);
1473 ovs_rwlock_unlock(&dp->port_rwlock);
1479 dp_netdev_destroy_all_queues(struct dp_netdev *dp)
1480 OVS_REQ_WRLOCK(dp->queue_rwlock)
1484 dp_netdev_purge_queues(dp);
1486 for (i = 0; i < dp->n_handlers; i++) {
1487 struct dp_netdev_queue *q = &dp->handler_queues[i];
1489 ovs_mutex_destroy(&q->mutex);
1490 seq_destroy(q->seq);
1492 free(dp->handler_queues);
1493 dp->handler_queues = NULL;
1498 dp_netdev_refresh_queues(struct dp_netdev *dp, uint32_t n_handlers)
1499 OVS_REQ_WRLOCK(dp->queue_rwlock)
1501 if (dp->n_handlers != n_handlers) {
1504 dp_netdev_destroy_all_queues(dp);
1506 dp->n_handlers = n_handlers;
1507 dp->handler_queues = xzalloc(n_handlers * sizeof *dp->handler_queues);
1509 for (i = 0; i < n_handlers; i++) {
1510 struct dp_netdev_queue *q = &dp->handler_queues[i];
1512 ovs_mutex_init(&q->mutex);
1513 q->seq = seq_create();
1519 dpif_netdev_recv_set(struct dpif *dpif, bool enable)
1521 struct dp_netdev *dp = get_dp_netdev(dpif);
1523 if ((dp->handler_queues != NULL) == enable) {
1527 fat_rwlock_wrlock(&dp->queue_rwlock);
1529 dp_netdev_destroy_all_queues(dp);
1531 dp_netdev_refresh_queues(dp, 1);
1533 fat_rwlock_unlock(&dp->queue_rwlock);
1539 dpif_netdev_handlers_set(struct dpif *dpif, uint32_t n_handlers)
1541 struct dp_netdev *dp = get_dp_netdev(dpif);
1543 fat_rwlock_wrlock(&dp->queue_rwlock);
1544 if (dp->handler_queues) {
1545 dp_netdev_refresh_queues(dp, n_handlers);
1547 fat_rwlock_unlock(&dp->queue_rwlock);
1553 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1554 uint32_t queue_id, uint32_t *priority)
1556 *priority = queue_id;
1561 dp_netdev_recv_check(const struct dp_netdev *dp, const uint32_t handler_id)
1562 OVS_REQ_RDLOCK(dp->queue_rwlock)
1564 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1566 if (!dp->handler_queues) {
1567 VLOG_WARN_RL(&rl, "receiving upcall disabled");
1571 if (handler_id >= dp->n_handlers) {
1572 VLOG_WARN_RL(&rl, "handler index out of bound");
1580 dpif_netdev_recv(struct dpif *dpif, uint32_t handler_id,
1581 struct dpif_upcall *upcall, struct ofpbuf *buf)
1583 struct dp_netdev *dp = get_dp_netdev(dpif);
1584 struct dp_netdev_queue *q;
1587 fat_rwlock_rdlock(&dp->queue_rwlock);
1589 if (!dp_netdev_recv_check(dp, handler_id)) {
1594 q = &dp->handler_queues[handler_id];
1595 ovs_mutex_lock(&q->mutex);
1596 if (q->head != q->tail) {
1597 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1599 *upcall = u->upcall;
1606 ovs_mutex_unlock(&q->mutex);
1609 fat_rwlock_unlock(&dp->queue_rwlock);
1615 dpif_netdev_recv_wait(struct dpif *dpif, uint32_t handler_id)
1617 struct dp_netdev *dp = get_dp_netdev(dpif);
1618 struct dp_netdev_queue *q;
1621 fat_rwlock_rdlock(&dp->queue_rwlock);
1623 if (!dp_netdev_recv_check(dp, handler_id)) {
1627 q = &dp->handler_queues[handler_id];
1628 ovs_mutex_lock(&q->mutex);
1629 seq = seq_read(q->seq);
1630 if (q->head != q->tail) {
1631 poll_immediate_wake();
1633 seq_wait(q->seq, seq);
1636 ovs_mutex_unlock(&q->mutex);
1639 fat_rwlock_unlock(&dp->queue_rwlock);
1643 dpif_netdev_recv_purge(struct dpif *dpif)
1645 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1647 fat_rwlock_wrlock(&dpif_netdev->dp->queue_rwlock);
1648 dp_netdev_purge_queues(dpif_netdev->dp);
1649 fat_rwlock_unlock(&dpif_netdev->dp->queue_rwlock);
1652 /* Creates and returns a new 'struct dp_netdev_actions', with a reference count
1653 * of 1, whose actions are a copy of from the 'ofpacts_len' bytes of
1655 struct dp_netdev_actions *
1656 dp_netdev_actions_create(const struct nlattr *actions, size_t size)
1658 struct dp_netdev_actions *netdev_actions;
1660 netdev_actions = xmalloc(sizeof *netdev_actions);
1661 netdev_actions->actions = xmemdup(actions, size);
1662 netdev_actions->size = size;
1664 return netdev_actions;
1667 struct dp_netdev_actions *
1668 dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow)
1670 return ovsrcu_get(struct dp_netdev_actions *, &flow->actions);
1674 dp_netdev_actions_free(struct dp_netdev_actions *actions)
1676 free(actions->actions);
1681 dp_forwarder_main(void *f_)
1683 struct dp_forwarder *f = f_;
1684 struct dp_netdev *dp = f->dp;
1685 struct ofpbuf packet;
1687 f->name = xasprintf("forwarder_%u", ovsthread_id_self());
1688 set_subprogram_name("%s", f->name);
1690 ofpbuf_init(&packet, 0);
1691 while (!latch_is_set(&dp->exit_latch)) {
1692 bool received_anything;
1695 ovs_rwlock_rdlock(&dp->port_rwlock);
1696 for (i = 0; i < 50; i++) {
1697 struct dp_netdev_port *port;
1699 received_anything = false;
1700 HMAP_FOR_EACH (port, node, &f->dp->ports) {
1702 && port->node.hash >= f->min_hash
1703 && port->node.hash <= f->max_hash) {
1708 if (netdev_get_mtu(port->netdev, &mtu)) {
1709 mtu = ETH_PAYLOAD_MAX;
1711 buf_size = DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + mtu;
1713 ofpbuf_clear(&packet);
1714 ofpbuf_reserve_with_tailroom(&packet, DP_NETDEV_HEADROOM,
1717 error = netdev_rx_recv(port->rx, &packet);
1719 struct pkt_metadata md
1720 = PKT_METADATA_INITIALIZER(port->port_no);
1722 dp_netdev_port_input(dp, &packet, &md);
1723 received_anything = true;
1724 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1725 static struct vlog_rate_limit rl
1726 = VLOG_RATE_LIMIT_INIT(1, 5);
1728 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1729 netdev_get_name(port->netdev),
1730 ovs_strerror(error));
1735 if (!received_anything) {
1740 if (received_anything) {
1741 poll_immediate_wake();
1743 struct dp_netdev_port *port;
1745 HMAP_FOR_EACH (port, node, &f->dp->ports)
1747 && port->node.hash >= f->min_hash
1748 && port->node.hash <= f->max_hash) {
1749 netdev_rx_wait(port->rx);
1751 seq_wait(dp->port_seq, seq_read(dp->port_seq));
1752 latch_wait(&dp->exit_latch);
1754 ovs_rwlock_unlock(&dp->port_rwlock);
1758 ofpbuf_uninit(&packet);
1766 dp_netdev_set_threads(struct dp_netdev *dp, int n)
1770 if (n == dp->n_forwarders) {
1774 /* Stop existing threads. */
1775 latch_set(&dp->exit_latch);
1776 for (i = 0; i < dp->n_forwarders; i++) {
1777 struct dp_forwarder *f = &dp->forwarders[i];
1779 xpthread_join(f->thread, NULL);
1781 latch_poll(&dp->exit_latch);
1782 free(dp->forwarders);
1784 /* Start new threads. */
1785 dp->forwarders = xmalloc(n * sizeof *dp->forwarders);
1786 dp->n_forwarders = n;
1787 for (i = 0; i < n; i++) {
1788 struct dp_forwarder *f = &dp->forwarders[i];
1791 f->min_hash = UINT32_MAX / n * i;
1792 f->max_hash = UINT32_MAX / n * (i + 1) - 1;
1794 f->max_hash = UINT32_MAX;
1796 xpthread_create(&f->thread, NULL, dp_forwarder_main, f);
1801 dp_netdev_flow_stats_new_cb(void)
1803 struct dp_netdev_flow_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1804 ovs_mutex_init(&bucket->mutex);
1809 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow,
1810 const struct ofpbuf *packet,
1811 const struct flow *key)
1813 uint16_t tcp_flags = ntohs(key->tcp_flags);
1814 long long int now = time_msec();
1815 struct dp_netdev_flow_stats *bucket;
1817 bucket = ovsthread_stats_bucket_get(&netdev_flow->stats,
1818 dp_netdev_flow_stats_new_cb);
1820 ovs_mutex_lock(&bucket->mutex);
1821 bucket->used = MAX(now, bucket->used);
1822 bucket->packet_count++;
1823 bucket->byte_count += packet->size;
1824 bucket->tcp_flags |= tcp_flags;
1825 ovs_mutex_unlock(&bucket->mutex);
1829 dp_netdev_stats_new_cb(void)
1831 struct dp_netdev_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1832 ovs_mutex_init(&bucket->mutex);
1837 dp_netdev_count_packet(struct dp_netdev *dp, enum dp_stat_type type)
1839 struct dp_netdev_stats *bucket;
1841 bucket = ovsthread_stats_bucket_get(&dp->stats, dp_netdev_stats_new_cb);
1842 ovs_mutex_lock(&bucket->mutex);
1844 ovs_mutex_unlock(&bucket->mutex);
1848 dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
1849 struct pkt_metadata *md)
1850 OVS_REQ_RDLOCK(dp->port_rwlock)
1852 struct dp_netdev_flow *netdev_flow;
1855 if (packet->size < ETH_HEADER_LEN) {
1858 flow_extract(packet, md, &key);
1859 netdev_flow = dp_netdev_lookup_flow(dp, &key);
1861 struct dp_netdev_actions *actions;
1863 dp_netdev_flow_used(netdev_flow, packet, &key);
1865 actions = dp_netdev_flow_get_actions(netdev_flow);
1866 dp_netdev_execute_actions(dp, &key, packet, md,
1867 actions->actions, actions->size);
1868 dp_netdev_count_packet(dp, DP_STAT_HIT);
1869 } else if (dp->handler_queues) {
1870 dp_netdev_count_packet(dp, DP_STAT_MISS);
1871 dp_netdev_output_userspace(dp, packet,
1872 flow_hash_5tuple(&key, 0) % dp->n_handlers,
1873 DPIF_UC_MISS, &key, NULL);
1878 dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *packet,
1879 int queue_no, int type, const struct flow *flow,
1880 const struct nlattr *userdata)
1881 OVS_EXCLUDED(dp->queue_rwlock)
1883 struct dp_netdev_queue *q;
1886 fat_rwlock_rdlock(&dp->queue_rwlock);
1887 q = &dp->handler_queues[queue_no];
1888 ovs_mutex_lock(&q->mutex);
1889 if (q->head - q->tail < MAX_QUEUE_LEN) {
1890 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
1891 struct dpif_upcall *upcall = &u->upcall;
1892 struct ofpbuf *buf = &u->buf;
1895 upcall->type = type;
1897 /* Allocate buffer big enough for everything. */
1898 buf_size = ODPUTIL_FLOW_KEY_BYTES;
1900 buf_size += NLA_ALIGN(userdata->nla_len);
1902 ofpbuf_init(buf, buf_size);
1905 odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
1906 upcall->key = buf->data;
1907 upcall->key_len = buf->size;
1911 upcall->userdata = ofpbuf_put(buf, userdata,
1912 NLA_ALIGN(userdata->nla_len));
1915 /* Steal packet data. */
1916 ovs_assert(packet->source == OFPBUF_MALLOC);
1917 upcall->packet = *packet;
1918 ofpbuf_use(packet, NULL, 0);
1924 dp_netdev_count_packet(dp, DP_STAT_LOST);
1927 ovs_mutex_unlock(&q->mutex);
1928 fat_rwlock_unlock(&dp->queue_rwlock);
1933 struct dp_netdev_execute_aux {
1934 struct dp_netdev *dp;
1935 const struct flow *key;
1939 dp_execute_cb(void *aux_, struct ofpbuf *packet,
1940 const struct pkt_metadata *md OVS_UNUSED,
1941 const struct nlattr *a, bool may_steal)
1942 OVS_NO_THREAD_SAFETY_ANALYSIS
1944 struct dp_netdev_execute_aux *aux = aux_;
1945 int type = nl_attr_type(a);
1946 struct dp_netdev_port *p;
1948 switch ((enum ovs_action_attr)type) {
1949 case OVS_ACTION_ATTR_OUTPUT:
1950 p = dp_netdev_lookup_port(aux->dp, u32_to_odp(nl_attr_get_u32(a)));
1952 netdev_send(p->netdev, packet);
1956 case OVS_ACTION_ATTR_USERSPACE: {
1957 const struct nlattr *userdata;
1959 userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
1961 /* Make a copy if we are not allowed to steal the packet's data. */
1963 packet = ofpbuf_clone_with_headroom(packet, DP_NETDEV_HEADROOM);
1965 dp_netdev_output_userspace(aux->dp, packet,
1966 flow_hash_5tuple(aux->key, 0)
1967 % aux->dp->n_handlers,
1968 DPIF_UC_ACTION, aux->key,
1971 ofpbuf_uninit(packet);
1975 case OVS_ACTION_ATTR_PUSH_VLAN:
1976 case OVS_ACTION_ATTR_POP_VLAN:
1977 case OVS_ACTION_ATTR_PUSH_MPLS:
1978 case OVS_ACTION_ATTR_POP_MPLS:
1979 case OVS_ACTION_ATTR_SET:
1980 case OVS_ACTION_ATTR_SAMPLE:
1981 case OVS_ACTION_ATTR_UNSPEC:
1982 case __OVS_ACTION_ATTR_MAX:
1988 dp_netdev_execute_actions(struct dp_netdev *dp, const struct flow *key,
1989 struct ofpbuf *packet, struct pkt_metadata *md,
1990 const struct nlattr *actions, size_t actions_len)
1991 OVS_REQ_RDLOCK(dp->port_rwlock)
1993 struct dp_netdev_execute_aux aux = {dp, key};
1995 odp_execute_actions(&aux, packet, md, actions, actions_len, dp_execute_cb);
1998 const struct dpif_class dpif_netdev_class = {
2000 dpif_netdev_enumerate,
2001 dpif_netdev_port_open_type,
2004 dpif_netdev_destroy,
2007 dpif_netdev_get_stats,
2008 dpif_netdev_port_add,
2009 dpif_netdev_port_del,
2010 dpif_netdev_port_query_by_number,
2011 dpif_netdev_port_query_by_name,
2012 NULL, /* port_get_pid */
2013 dpif_netdev_port_dump_start,
2014 dpif_netdev_port_dump_next,
2015 dpif_netdev_port_dump_done,
2016 dpif_netdev_port_poll,
2017 dpif_netdev_port_poll_wait,
2018 dpif_netdev_flow_get,
2019 dpif_netdev_flow_put,
2020 dpif_netdev_flow_del,
2021 dpif_netdev_flow_flush,
2022 dpif_netdev_flow_dump_state_init,
2023 dpif_netdev_flow_dump_start,
2024 dpif_netdev_flow_dump_next,
2026 dpif_netdev_flow_dump_done,
2027 dpif_netdev_flow_dump_state_uninit,
2028 dpif_netdev_execute,
2030 dpif_netdev_recv_set,
2031 dpif_netdev_handlers_set,
2032 dpif_netdev_queue_to_priority,
2034 dpif_netdev_recv_wait,
2035 dpif_netdev_recv_purge,
2039 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
2040 const char *argv[], void *aux OVS_UNUSED)
2042 struct dp_netdev_port *port;
2043 struct dp_netdev *dp;
2046 ovs_mutex_lock(&dp_netdev_mutex);
2047 dp = shash_find_data(&dp_netdevs, argv[1]);
2048 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
2049 ovs_mutex_unlock(&dp_netdev_mutex);
2050 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
2053 ovs_refcount_ref(&dp->ref_cnt);
2054 ovs_mutex_unlock(&dp_netdev_mutex);
2056 ovs_rwlock_wrlock(&dp->port_rwlock);
2057 if (get_port_by_name(dp, argv[2], &port)) {
2058 unixctl_command_reply_error(conn, "unknown port");
2062 port_no = u32_to_odp(atoi(argv[3]));
2063 if (!port_no || port_no == ODPP_NONE) {
2064 unixctl_command_reply_error(conn, "bad port number");
2067 if (dp_netdev_lookup_port(dp, port_no)) {
2068 unixctl_command_reply_error(conn, "port number already in use");
2071 hmap_remove(&dp->ports, &port->node);
2072 port->port_no = port_no;
2073 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
2074 seq_change(dp->port_seq);
2075 unixctl_command_reply(conn, NULL);
2078 ovs_rwlock_unlock(&dp->port_rwlock);
2079 dp_netdev_unref(dp);
2083 dpif_dummy_register__(const char *type)
2085 struct dpif_class *class;
2087 class = xmalloc(sizeof *class);
2088 *class = dpif_netdev_class;
2089 class->type = xstrdup(type);
2090 dp_register_provider(class);
2094 dpif_dummy_register(bool override)
2101 dp_enumerate_types(&types);
2102 SSET_FOR_EACH (type, &types) {
2103 if (!dp_unregister_provider(type)) {
2104 dpif_dummy_register__(type);
2107 sset_destroy(&types);
2110 dpif_dummy_register__("dummy");
2112 unixctl_command_register("dpif-dummy/change-port-number",
2113 "DP PORT NEW-NUMBER",
2114 3, 3, dpif_dummy_change_port_number, NULL);