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. */
73 enum { MAX_QUEUE_LEN = 128 }; /* Maximum number of packets per queue. */
74 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
75 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
77 /* Protects against changes to 'dp_netdevs'. */
78 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
80 /* Contains all 'struct dp_netdev's. */
81 static struct shash dp_netdevs OVS_GUARDED_BY(dp_netdev_mutex)
82 = SHASH_INITIALIZER(&dp_netdevs);
84 struct dp_netdev_upcall {
85 struct dpif_upcall upcall; /* Queued upcall information. */
86 struct ofpbuf buf; /* ofpbuf instance for upcall.packet. */
89 /* A queue passing packets from a struct dp_netdev to its clients (handlers).
95 * Any access at all requires the owning 'dp_netdev''s queue_rwlock and
97 struct dp_netdev_queue {
98 struct ovs_mutex mutex;
99 struct seq *seq; /* Incremented whenever a packet is queued. */
100 struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN] OVS_GUARDED;
101 unsigned int head OVS_GUARDED;
102 unsigned int tail OVS_GUARDED;
105 /* Datapath based on the network device interface from netdev.h.
111 * Some members, marked 'const', are immutable. Accessing other members
112 * requires synchronization, as noted in more detail below.
114 * Acquisition order is, from outermost to innermost:
116 * dp_netdev_mutex (global)
123 const struct dpif_class *const class;
124 const char *const name;
125 struct ovs_refcount ref_cnt;
126 atomic_flag destroyed;
130 * Readers of 'cls' and 'flow_table' must take a 'cls->rwlock' read lock.
132 * Writers of 'cls' and 'flow_table' must take the 'flow_mutex' and then
133 * the 'cls->rwlock' write lock. (The outer 'flow_mutex' allows writers to
134 * atomically perform multiple operations on 'cls' and 'flow_table'.)
136 struct ovs_mutex flow_mutex;
137 struct classifier cls; /* Classifier. Protected by cls.rwlock. */
138 struct hmap flow_table OVS_GUARDED; /* Flow table. */
142 * 'queue_rwlock' protects the modification of 'handler_queues' and
143 * 'n_handlers'. The queue elements are protected by its
144 * 'handler_queues''s mutex. */
145 struct fat_rwlock queue_rwlock;
146 struct dp_netdev_queue *handler_queues;
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 void dp_netdev_destroy_all_queues(struct dp_netdev *dp)
333 OVS_REQ_WRLOCK(dp->queue_rwlock);
334 static int dpif_netdev_open(const struct dpif_class *, const char *name,
335 bool create, struct dpif **);
336 static int dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *,
337 int queue_no, int type,
339 const struct nlattr *userdata)
340 OVS_EXCLUDED(dp->queue_rwlock);
341 static void dp_netdev_execute_actions(struct dp_netdev *dp,
342 const struct flow *, struct ofpbuf *, bool may_steal,
343 struct pkt_metadata *,
344 const struct nlattr *actions,
346 OVS_REQ_RDLOCK(dp->port_rwlock);
347 static void dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
348 struct pkt_metadata *)
349 OVS_REQ_RDLOCK(dp->port_rwlock);
350 static void dp_netdev_set_threads(struct dp_netdev *, int n);
352 static struct dpif_netdev *
353 dpif_netdev_cast(const struct dpif *dpif)
355 ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
356 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
359 static struct dp_netdev *
360 get_dp_netdev(const struct dpif *dpif)
362 return dpif_netdev_cast(dpif)->dp;
366 dpif_netdev_enumerate(struct sset *all_dps)
368 struct shash_node *node;
370 ovs_mutex_lock(&dp_netdev_mutex);
371 SHASH_FOR_EACH(node, &dp_netdevs) {
372 sset_add(all_dps, node->name);
374 ovs_mutex_unlock(&dp_netdev_mutex);
380 dpif_netdev_class_is_dummy(const struct dpif_class *class)
382 return class != &dpif_netdev_class;
386 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
388 return strcmp(type, "internal") ? type
389 : dpif_netdev_class_is_dummy(class) ? "dummy"
394 create_dpif_netdev(struct dp_netdev *dp)
396 uint16_t netflow_id = hash_string(dp->name, 0);
397 struct dpif_netdev *dpif;
399 ovs_refcount_ref(&dp->ref_cnt);
401 dpif = xmalloc(sizeof *dpif);
402 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
404 dpif->last_port_seq = seq_read(dp->port_seq);
409 /* Choose an unused, non-zero port number and return it on success.
410 * Return ODPP_NONE on failure. */
412 choose_port(struct dp_netdev *dp, const char *name)
413 OVS_REQ_RDLOCK(dp->port_rwlock)
417 if (dp->class != &dpif_netdev_class) {
421 /* If the port name begins with "br", start the number search at
422 * 100 to make writing tests easier. */
423 if (!strncmp(name, "br", 2)) {
427 /* If the port name contains a number, try to assign that port number.
428 * This can make writing unit tests easier because port numbers are
430 for (p = name; *p != '\0'; p++) {
431 if (isdigit((unsigned char) *p)) {
432 port_no = start_no + strtol(p, NULL, 10);
433 if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE)
434 && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
435 return u32_to_odp(port_no);
442 for (port_no = 1; port_no <= UINT16_MAX; port_no++) {
443 if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
444 return u32_to_odp(port_no);
452 create_dp_netdev(const char *name, const struct dpif_class *class,
453 struct dp_netdev **dpp)
454 OVS_REQUIRES(dp_netdev_mutex)
456 struct dp_netdev *dp;
459 dp = xzalloc(sizeof *dp);
460 shash_add(&dp_netdevs, name, dp);
462 *CONST_CAST(const struct dpif_class **, &dp->class) = class;
463 *CONST_CAST(const char **, &dp->name) = xstrdup(name);
464 ovs_refcount_init(&dp->ref_cnt);
465 atomic_flag_clear(&dp->destroyed);
467 ovs_mutex_init(&dp->flow_mutex);
468 classifier_init(&dp->cls, NULL);
469 hmap_init(&dp->flow_table);
471 fat_rwlock_init(&dp->queue_rwlock);
473 ovsthread_stats_init(&dp->stats);
475 ovs_rwlock_init(&dp->port_rwlock);
476 hmap_init(&dp->ports);
477 dp->port_seq = seq_create();
478 latch_init(&dp->exit_latch);
480 ovs_rwlock_wrlock(&dp->port_rwlock);
481 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
482 ovs_rwlock_unlock(&dp->port_rwlock);
487 dp_netdev_set_threads(dp, 2);
494 dpif_netdev_open(const struct dpif_class *class, const char *name,
495 bool create, struct dpif **dpifp)
497 struct dp_netdev *dp;
500 ovs_mutex_lock(&dp_netdev_mutex);
501 dp = shash_find_data(&dp_netdevs, name);
503 error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
505 error = (dp->class != class ? EINVAL
510 *dpifp = create_dpif_netdev(dp);
512 ovs_mutex_unlock(&dp_netdev_mutex);
518 dp_netdev_purge_queues(struct dp_netdev *dp)
519 OVS_REQ_WRLOCK(dp->queue_rwlock)
523 for (i = 0; i < dp->n_handlers; i++) {
524 struct dp_netdev_queue *q = &dp->handler_queues[i];
526 ovs_mutex_lock(&q->mutex);
527 while (q->tail != q->head) {
528 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
529 ofpbuf_uninit(&u->upcall.packet);
530 ofpbuf_uninit(&u->buf);
532 ovs_mutex_unlock(&q->mutex);
536 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
537 * through the 'dp_netdevs' shash while freeing 'dp'. */
539 dp_netdev_free(struct dp_netdev *dp)
540 OVS_REQUIRES(dp_netdev_mutex)
542 struct dp_netdev_port *port, *next;
543 struct dp_netdev_stats *bucket;
546 shash_find_and_delete(&dp_netdevs, dp->name);
548 dp_netdev_set_threads(dp, 0);
549 free(dp->forwarders);
551 dp_netdev_flow_flush(dp);
552 ovs_rwlock_wrlock(&dp->port_rwlock);
553 HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) {
554 do_del_port(dp, port->port_no);
556 ovs_rwlock_unlock(&dp->port_rwlock);
558 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
559 ovs_mutex_destroy(&bucket->mutex);
560 free_cacheline(bucket);
562 ovsthread_stats_destroy(&dp->stats);
564 fat_rwlock_wrlock(&dp->queue_rwlock);
565 dp_netdev_destroy_all_queues(dp);
566 fat_rwlock_unlock(&dp->queue_rwlock);
568 fat_rwlock_destroy(&dp->queue_rwlock);
570 classifier_destroy(&dp->cls);
571 hmap_destroy(&dp->flow_table);
572 ovs_mutex_destroy(&dp->flow_mutex);
573 seq_destroy(dp->port_seq);
574 hmap_destroy(&dp->ports);
575 latch_destroy(&dp->exit_latch);
576 free(CONST_CAST(char *, dp->name));
581 dp_netdev_unref(struct dp_netdev *dp)
584 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
585 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
586 ovs_mutex_lock(&dp_netdev_mutex);
587 if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
590 ovs_mutex_unlock(&dp_netdev_mutex);
595 dpif_netdev_close(struct dpif *dpif)
597 struct dp_netdev *dp = get_dp_netdev(dpif);
604 dpif_netdev_destroy(struct dpif *dpif)
606 struct dp_netdev *dp = get_dp_netdev(dpif);
608 if (!atomic_flag_test_and_set(&dp->destroyed)) {
609 if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
610 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
619 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
621 struct dp_netdev *dp = get_dp_netdev(dpif);
622 struct dp_netdev_stats *bucket;
625 fat_rwlock_rdlock(&dp->cls.rwlock);
626 stats->n_flows = hmap_count(&dp->flow_table);
627 fat_rwlock_unlock(&dp->cls.rwlock);
629 stats->n_hit = stats->n_missed = stats->n_lost = 0;
630 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
631 ovs_mutex_lock(&bucket->mutex);
632 stats->n_hit += bucket->n[DP_STAT_HIT];
633 stats->n_missed += bucket->n[DP_STAT_MISS];
634 stats->n_lost += bucket->n[DP_STAT_LOST];
635 ovs_mutex_unlock(&bucket->mutex);
637 stats->n_masks = UINT32_MAX;
638 stats->n_mask_hit = UINT64_MAX;
644 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
646 OVS_REQ_WRLOCK(dp->port_rwlock)
648 struct netdev_saved_flags *sf;
649 struct dp_netdev_port *port;
650 struct netdev *netdev;
651 struct netdev_rx *rx;
652 enum netdev_flags flags;
653 const char *open_type;
656 /* XXX reject devices already in some dp_netdev. */
658 /* Open and validate network device. */
659 open_type = dpif_netdev_port_open_type(dp->class, type);
660 error = netdev_open(devname, open_type, &netdev);
664 /* XXX reject non-Ethernet devices */
666 netdev_get_flags(netdev, &flags);
667 if (flags & NETDEV_LOOPBACK) {
668 VLOG_ERR("%s: cannot add a loopback device", devname);
669 netdev_close(netdev);
673 error = netdev_rx_open(netdev, &rx);
675 && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
676 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
677 devname, ovs_strerror(errno));
678 netdev_close(netdev);
682 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
685 netdev_close(netdev);
689 port = xmalloc(sizeof *port);
690 port->port_no = port_no;
691 port->netdev = netdev;
694 port->type = xstrdup(type);
696 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
697 seq_change(dp->port_seq);
703 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
704 odp_port_t *port_nop)
706 struct dp_netdev *dp = get_dp_netdev(dpif);
707 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
708 const char *dpif_port;
712 ovs_rwlock_wrlock(&dp->port_rwlock);
713 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
714 if (*port_nop != ODPP_NONE) {
716 error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0;
718 port_no = choose_port(dp, dpif_port);
719 error = port_no == ODPP_NONE ? EFBIG : 0;
723 error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
725 ovs_rwlock_unlock(&dp->port_rwlock);
731 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
733 struct dp_netdev *dp = get_dp_netdev(dpif);
736 ovs_rwlock_wrlock(&dp->port_rwlock);
737 error = port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
738 ovs_rwlock_unlock(&dp->port_rwlock);
744 is_valid_port_number(odp_port_t port_no)
746 return port_no != ODPP_NONE;
749 static struct dp_netdev_port *
750 dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no)
751 OVS_REQ_RDLOCK(dp->port_rwlock)
753 struct dp_netdev_port *port;
755 HMAP_FOR_EACH_IN_BUCKET (port, node, hash_int(odp_to_u32(port_no), 0),
757 if (port->port_no == port_no) {
765 get_port_by_number(struct dp_netdev *dp,
766 odp_port_t port_no, struct dp_netdev_port **portp)
767 OVS_REQ_RDLOCK(dp->port_rwlock)
769 if (!is_valid_port_number(port_no)) {
773 *portp = dp_netdev_lookup_port(dp, port_no);
774 return *portp ? 0 : ENOENT;
779 get_port_by_name(struct dp_netdev *dp,
780 const char *devname, struct dp_netdev_port **portp)
781 OVS_REQ_RDLOCK(dp->port_rwlock)
783 struct dp_netdev_port *port;
785 HMAP_FOR_EACH (port, node, &dp->ports) {
786 if (!strcmp(netdev_get_name(port->netdev), devname)) {
795 do_del_port(struct dp_netdev *dp, odp_port_t port_no)
796 OVS_REQ_WRLOCK(dp->port_rwlock)
798 struct dp_netdev_port *port;
801 error = get_port_by_number(dp, port_no, &port);
806 hmap_remove(&dp->ports, &port->node);
807 seq_change(dp->port_seq);
809 netdev_close(port->netdev);
810 netdev_restore_flags(port->sf);
811 netdev_rx_close(port->rx);
819 answer_port_query(const struct dp_netdev_port *port,
820 struct dpif_port *dpif_port)
822 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
823 dpif_port->type = xstrdup(port->type);
824 dpif_port->port_no = port->port_no;
828 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
829 struct dpif_port *dpif_port)
831 struct dp_netdev *dp = get_dp_netdev(dpif);
832 struct dp_netdev_port *port;
835 ovs_rwlock_rdlock(&dp->port_rwlock);
836 error = get_port_by_number(dp, port_no, &port);
837 if (!error && dpif_port) {
838 answer_port_query(port, dpif_port);
840 ovs_rwlock_unlock(&dp->port_rwlock);
846 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
847 struct dpif_port *dpif_port)
849 struct dp_netdev *dp = get_dp_netdev(dpif);
850 struct dp_netdev_port *port;
853 ovs_rwlock_rdlock(&dp->port_rwlock);
854 error = get_port_by_name(dp, devname, &port);
855 if (!error && dpif_port) {
856 answer_port_query(port, dpif_port);
858 ovs_rwlock_unlock(&dp->port_rwlock);
864 dp_netdev_flow_free(struct dp_netdev_flow *flow)
866 struct dp_netdev_flow_stats *bucket;
869 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &flow->stats) {
870 ovs_mutex_destroy(&bucket->mutex);
871 free_cacheline(bucket);
873 ovsthread_stats_destroy(&flow->stats);
875 cls_rule_destroy(CONST_CAST(struct cls_rule *, &flow->cr));
876 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow));
877 ovs_mutex_destroy(&flow->mutex);
882 dp_netdev_remove_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
883 OVS_REQ_WRLOCK(dp->cls.rwlock)
884 OVS_REQUIRES(dp->flow_mutex)
886 struct cls_rule *cr = CONST_CAST(struct cls_rule *, &flow->cr);
887 struct hmap_node *node = CONST_CAST(struct hmap_node *, &flow->node);
889 classifier_remove(&dp->cls, cr);
890 hmap_remove(&dp->flow_table, node);
891 ovsrcu_postpone(dp_netdev_flow_free, flow);
895 dp_netdev_flow_flush(struct dp_netdev *dp)
897 struct dp_netdev_flow *netdev_flow, *next;
899 ovs_mutex_lock(&dp->flow_mutex);
900 fat_rwlock_wrlock(&dp->cls.rwlock);
901 HMAP_FOR_EACH_SAFE (netdev_flow, next, node, &dp->flow_table) {
902 dp_netdev_remove_flow(dp, netdev_flow);
904 fat_rwlock_unlock(&dp->cls.rwlock);
905 ovs_mutex_unlock(&dp->flow_mutex);
909 dpif_netdev_flow_flush(struct dpif *dpif)
911 struct dp_netdev *dp = get_dp_netdev(dpif);
913 dp_netdev_flow_flush(dp);
917 struct dp_netdev_port_state {
924 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
926 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
931 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
932 struct dpif_port *dpif_port)
934 struct dp_netdev_port_state *state = state_;
935 struct dp_netdev *dp = get_dp_netdev(dpif);
936 struct hmap_node *node;
939 ovs_rwlock_rdlock(&dp->port_rwlock);
940 node = hmap_at_position(&dp->ports, &state->bucket, &state->offset);
942 struct dp_netdev_port *port;
944 port = CONTAINER_OF(node, struct dp_netdev_port, node);
947 state->name = xstrdup(netdev_get_name(port->netdev));
948 dpif_port->name = state->name;
949 dpif_port->type = port->type;
950 dpif_port->port_no = port->port_no;
956 ovs_rwlock_unlock(&dp->port_rwlock);
962 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
964 struct dp_netdev_port_state *state = state_;
971 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
973 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
974 uint64_t new_port_seq;
977 new_port_seq = seq_read(dpif->dp->port_seq);
978 if (dpif->last_port_seq != new_port_seq) {
979 dpif->last_port_seq = new_port_seq;
989 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
991 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
993 seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
996 static struct dp_netdev_flow *
997 dp_netdev_flow_cast(const struct cls_rule *cr)
999 return cr ? CONTAINER_OF(cr, struct dp_netdev_flow, cr) : NULL;
1002 static struct dp_netdev_flow *
1003 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *flow)
1004 OVS_EXCLUDED(dp->cls.rwlock)
1006 struct dp_netdev_flow *netdev_flow;
1008 fat_rwlock_rdlock(&dp->cls.rwlock);
1009 netdev_flow = dp_netdev_flow_cast(classifier_lookup(&dp->cls, flow, NULL));
1010 fat_rwlock_unlock(&dp->cls.rwlock);
1015 static struct dp_netdev_flow *
1016 dp_netdev_find_flow(const struct dp_netdev *dp, const struct flow *flow)
1017 OVS_REQ_RDLOCK(dp->cls.rwlock)
1019 struct dp_netdev_flow *netdev_flow;
1021 HMAP_FOR_EACH_WITH_HASH (netdev_flow, node, flow_hash(flow, 0),
1023 if (flow_equal(&netdev_flow->flow, flow)) {
1032 get_dpif_flow_stats(struct dp_netdev_flow *netdev_flow,
1033 struct dpif_flow_stats *stats)
1035 struct dp_netdev_flow_stats *bucket;
1038 memset(stats, 0, sizeof *stats);
1039 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1040 ovs_mutex_lock(&bucket->mutex);
1041 stats->n_packets += bucket->packet_count;
1042 stats->n_bytes += bucket->byte_count;
1043 stats->used = MAX(stats->used, bucket->used);
1044 stats->tcp_flags |= bucket->tcp_flags;
1045 ovs_mutex_unlock(&bucket->mutex);
1050 dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1051 const struct nlattr *mask_key,
1052 uint32_t mask_key_len, const struct flow *flow,
1056 enum odp_key_fitness fitness;
1058 fitness = odp_flow_key_to_mask(mask_key, mask_key_len, mask, flow);
1060 /* This should not happen: it indicates that
1061 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
1062 * disagree on the acceptable form of a mask. Log the problem
1063 * as an error, with enough details to enable debugging. */
1064 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1066 if (!VLOG_DROP_ERR(&rl)) {
1070 odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s,
1072 VLOG_ERR("internal error parsing flow mask %s (%s)",
1073 ds_cstr(&s), odp_key_fitness_to_string(fitness));
1079 /* Force unwildcard the in_port. */
1080 mask->in_port.odp_port = u32_to_odp(UINT32_MAX);
1082 enum mf_field_id id;
1083 /* No mask key, unwildcard everything except fields whose
1084 * prerequisities are not met. */
1085 memset(mask, 0x0, sizeof *mask);
1087 for (id = 0; id < MFF_N_IDS; ++id) {
1088 /* Skip registers and metadata. */
1089 if (!(id >= MFF_REG0 && id < MFF_REG0 + FLOW_N_REGS)
1090 && id != MFF_METADATA) {
1091 const struct mf_field *mf = mf_from_id(id);
1092 if (mf_are_prereqs_ok(mf, flow)) {
1093 mf_mask_field(mf, mask);
1103 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1108 if (odp_flow_key_to_flow(key, key_len, flow)) {
1109 /* This should not happen: it indicates that odp_flow_key_from_flow()
1110 * and odp_flow_key_to_flow() disagree on the acceptable form of a
1111 * flow. Log the problem as an error, with enough details to enable
1113 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1115 if (!VLOG_DROP_ERR(&rl)) {
1119 odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
1120 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
1127 in_port = flow->in_port.odp_port;
1128 if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
1136 dpif_netdev_flow_get(const struct dpif *dpif,
1137 const struct nlattr *nl_key, size_t nl_key_len,
1138 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
1140 struct dp_netdev *dp = get_dp_netdev(dpif);
1141 struct dp_netdev_flow *netdev_flow;
1145 error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
1150 fat_rwlock_rdlock(&dp->cls.rwlock);
1151 netdev_flow = dp_netdev_find_flow(dp, &key);
1152 fat_rwlock_unlock(&dp->cls.rwlock);
1156 get_dpif_flow_stats(netdev_flow, stats);
1160 struct dp_netdev_actions *actions;
1162 actions = dp_netdev_flow_get_actions(netdev_flow);
1163 *actionsp = ofpbuf_clone_data(actions->actions, actions->size);
1173 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *flow,
1174 const struct flow_wildcards *wc,
1175 const struct nlattr *actions,
1177 OVS_REQUIRES(dp->flow_mutex)
1179 struct dp_netdev_flow *netdev_flow;
1182 netdev_flow = xzalloc(sizeof *netdev_flow);
1183 *CONST_CAST(struct flow *, &netdev_flow->flow) = *flow;
1185 ovs_mutex_init(&netdev_flow->mutex);
1187 ovsthread_stats_init(&netdev_flow->stats);
1189 ovsrcu_set(&netdev_flow->actions,
1190 dp_netdev_actions_create(actions, actions_len));
1192 match_init(&match, flow, wc);
1193 cls_rule_init(CONST_CAST(struct cls_rule *, &netdev_flow->cr),
1194 &match, NETDEV_RULE_PRIORITY);
1195 fat_rwlock_wrlock(&dp->cls.rwlock);
1196 classifier_insert(&dp->cls,
1197 CONST_CAST(struct cls_rule *, &netdev_flow->cr));
1198 hmap_insert(&dp->flow_table,
1199 CONST_CAST(struct hmap_node *, &netdev_flow->node),
1200 flow_hash(flow, 0));
1201 fat_rwlock_unlock(&dp->cls.rwlock);
1207 clear_stats(struct dp_netdev_flow *netdev_flow)
1209 struct dp_netdev_flow_stats *bucket;
1212 OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1213 ovs_mutex_lock(&bucket->mutex);
1215 bucket->packet_count = 0;
1216 bucket->byte_count = 0;
1217 bucket->tcp_flags = 0;
1218 ovs_mutex_unlock(&bucket->mutex);
1223 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
1225 struct dp_netdev *dp = get_dp_netdev(dpif);
1226 struct dp_netdev_flow *netdev_flow;
1228 struct flow_wildcards wc;
1231 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &flow);
1235 error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
1236 put->mask, put->mask_len,
1242 ovs_mutex_lock(&dp->flow_mutex);
1243 netdev_flow = dp_netdev_lookup_flow(dp, &flow);
1245 if (put->flags & DPIF_FP_CREATE) {
1246 if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
1248 memset(put->stats, 0, sizeof *put->stats);
1250 error = dp_netdev_flow_add(dp, &flow, &wc, put->actions,
1259 if (put->flags & DPIF_FP_MODIFY
1260 && flow_equal(&flow, &netdev_flow->flow)) {
1261 struct dp_netdev_actions *new_actions;
1262 struct dp_netdev_actions *old_actions;
1264 new_actions = dp_netdev_actions_create(put->actions,
1267 old_actions = dp_netdev_flow_get_actions(netdev_flow);
1268 ovsrcu_set(&netdev_flow->actions, new_actions);
1271 get_dpif_flow_stats(netdev_flow, put->stats);
1273 if (put->flags & DPIF_FP_ZERO_STATS) {
1274 clear_stats(netdev_flow);
1277 ovsrcu_postpone(dp_netdev_actions_free, old_actions);
1278 } else if (put->flags & DPIF_FP_CREATE) {
1281 /* Overlapping flow. */
1285 ovs_mutex_unlock(&dp->flow_mutex);
1291 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
1293 struct dp_netdev *dp = get_dp_netdev(dpif);
1294 struct dp_netdev_flow *netdev_flow;
1298 error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
1303 ovs_mutex_lock(&dp->flow_mutex);
1304 fat_rwlock_wrlock(&dp->cls.rwlock);
1305 netdev_flow = dp_netdev_find_flow(dp, &key);
1308 get_dpif_flow_stats(netdev_flow, del->stats);
1310 dp_netdev_remove_flow(dp, netdev_flow);
1314 fat_rwlock_unlock(&dp->cls.rwlock);
1315 ovs_mutex_unlock(&dp->flow_mutex);
1320 struct dp_netdev_flow_state {
1321 struct dp_netdev_actions *actions;
1322 struct odputil_keybuf keybuf;
1323 struct odputil_keybuf maskbuf;
1324 struct dpif_flow_stats stats;
1327 struct dp_netdev_flow_iter {
1331 struct ovs_mutex mutex;
1335 dpif_netdev_flow_dump_state_init(void **statep)
1337 struct dp_netdev_flow_state *state;
1339 *statep = state = xmalloc(sizeof *state);
1340 state->actions = NULL;
1344 dpif_netdev_flow_dump_state_uninit(void *state_)
1346 struct dp_netdev_flow_state *state = state_;
1352 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **iterp)
1354 struct dp_netdev_flow_iter *iter;
1356 *iterp = iter = xmalloc(sizeof *iter);
1360 ovs_mutex_init(&iter->mutex);
1364 /* XXX the caller must use 'actions' without quiescing */
1366 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *iter_, void *state_,
1367 const struct nlattr **key, size_t *key_len,
1368 const struct nlattr **mask, size_t *mask_len,
1369 const struct nlattr **actions, size_t *actions_len,
1370 const struct dpif_flow_stats **stats)
1372 struct dp_netdev_flow_iter *iter = iter_;
1373 struct dp_netdev_flow_state *state = state_;
1374 struct dp_netdev *dp = get_dp_netdev(dpif);
1375 struct dp_netdev_flow *netdev_flow;
1378 ovs_mutex_lock(&iter->mutex);
1379 error = iter->status;
1381 struct hmap_node *node;
1383 fat_rwlock_rdlock(&dp->cls.rwlock);
1384 node = hmap_at_position(&dp->flow_table, &iter->bucket, &iter->offset);
1386 netdev_flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
1388 fat_rwlock_unlock(&dp->cls.rwlock);
1390 iter->status = error = EOF;
1393 ovs_mutex_unlock(&iter->mutex);
1401 ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
1402 odp_flow_key_from_flow(&buf, &netdev_flow->flow,
1403 netdev_flow->flow.in_port.odp_port);
1406 *key_len = buf.size;
1411 struct flow_wildcards wc;
1413 ofpbuf_use_stack(&buf, &state->maskbuf, sizeof state->maskbuf);
1414 minimask_expand(&netdev_flow->cr.match.mask, &wc);
1415 odp_flow_key_from_mask(&buf, &wc.masks, &netdev_flow->flow,
1416 odp_to_u32(wc.masks.in_port.odp_port),
1420 *mask_len = buf.size;
1423 if (actions || stats) {
1424 state->actions = NULL;
1427 state->actions = dp_netdev_flow_get_actions(netdev_flow);
1428 *actions = state->actions->actions;
1429 *actions_len = state->actions->size;
1433 get_dpif_flow_stats(netdev_flow, &state->stats);
1434 *stats = &state->stats;
1442 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *iter_)
1444 struct dp_netdev_flow_iter *iter = iter_;
1446 ovs_mutex_destroy(&iter->mutex);
1452 dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
1454 struct dp_netdev *dp = get_dp_netdev(dpif);
1455 struct pkt_metadata *md = &execute->md;
1458 if (execute->packet->size < ETH_HEADER_LEN ||
1459 execute->packet->size > UINT16_MAX) {
1463 /* Extract flow key. */
1464 flow_extract(execute->packet, md, &key);
1466 ovs_rwlock_rdlock(&dp->port_rwlock);
1467 dp_netdev_execute_actions(dp, &key, execute->packet, false, md,
1468 execute->actions, execute->actions_len);
1469 ovs_rwlock_unlock(&dp->port_rwlock);
1475 dp_netdev_destroy_all_queues(struct dp_netdev *dp)
1476 OVS_REQ_WRLOCK(dp->queue_rwlock)
1480 dp_netdev_purge_queues(dp);
1482 for (i = 0; i < dp->n_handlers; i++) {
1483 struct dp_netdev_queue *q = &dp->handler_queues[i];
1485 ovs_mutex_destroy(&q->mutex);
1486 seq_destroy(q->seq);
1488 free(dp->handler_queues);
1489 dp->handler_queues = NULL;
1494 dp_netdev_refresh_queues(struct dp_netdev *dp, uint32_t n_handlers)
1495 OVS_REQ_WRLOCK(dp->queue_rwlock)
1497 if (dp->n_handlers != n_handlers) {
1500 dp_netdev_destroy_all_queues(dp);
1502 dp->n_handlers = n_handlers;
1503 dp->handler_queues = xzalloc(n_handlers * sizeof *dp->handler_queues);
1505 for (i = 0; i < n_handlers; i++) {
1506 struct dp_netdev_queue *q = &dp->handler_queues[i];
1508 ovs_mutex_init(&q->mutex);
1509 q->seq = seq_create();
1515 dpif_netdev_recv_set(struct dpif *dpif, bool enable)
1517 struct dp_netdev *dp = get_dp_netdev(dpif);
1519 if ((dp->handler_queues != NULL) == enable) {
1523 fat_rwlock_wrlock(&dp->queue_rwlock);
1525 dp_netdev_destroy_all_queues(dp);
1527 dp_netdev_refresh_queues(dp, 1);
1529 fat_rwlock_unlock(&dp->queue_rwlock);
1535 dpif_netdev_handlers_set(struct dpif *dpif, uint32_t n_handlers)
1537 struct dp_netdev *dp = get_dp_netdev(dpif);
1539 fat_rwlock_wrlock(&dp->queue_rwlock);
1540 if (dp->handler_queues) {
1541 dp_netdev_refresh_queues(dp, n_handlers);
1543 fat_rwlock_unlock(&dp->queue_rwlock);
1549 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1550 uint32_t queue_id, uint32_t *priority)
1552 *priority = queue_id;
1557 dp_netdev_recv_check(const struct dp_netdev *dp, const uint32_t handler_id)
1558 OVS_REQ_RDLOCK(dp->queue_rwlock)
1560 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1562 if (!dp->handler_queues) {
1563 VLOG_WARN_RL(&rl, "receiving upcall disabled");
1567 if (handler_id >= dp->n_handlers) {
1568 VLOG_WARN_RL(&rl, "handler index out of bound");
1576 dpif_netdev_recv(struct dpif *dpif, uint32_t handler_id,
1577 struct dpif_upcall *upcall, struct ofpbuf *buf)
1579 struct dp_netdev *dp = get_dp_netdev(dpif);
1580 struct dp_netdev_queue *q;
1583 fat_rwlock_rdlock(&dp->queue_rwlock);
1585 if (!dp_netdev_recv_check(dp, handler_id)) {
1590 q = &dp->handler_queues[handler_id];
1591 ovs_mutex_lock(&q->mutex);
1592 if (q->head != q->tail) {
1593 struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1595 *upcall = u->upcall;
1602 ovs_mutex_unlock(&q->mutex);
1605 fat_rwlock_unlock(&dp->queue_rwlock);
1611 dpif_netdev_recv_wait(struct dpif *dpif, uint32_t handler_id)
1613 struct dp_netdev *dp = get_dp_netdev(dpif);
1614 struct dp_netdev_queue *q;
1617 fat_rwlock_rdlock(&dp->queue_rwlock);
1619 if (!dp_netdev_recv_check(dp, handler_id)) {
1623 q = &dp->handler_queues[handler_id];
1624 ovs_mutex_lock(&q->mutex);
1625 seq = seq_read(q->seq);
1626 if (q->head != q->tail) {
1627 poll_immediate_wake();
1629 seq_wait(q->seq, seq);
1632 ovs_mutex_unlock(&q->mutex);
1635 fat_rwlock_unlock(&dp->queue_rwlock);
1639 dpif_netdev_recv_purge(struct dpif *dpif)
1641 struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1643 fat_rwlock_wrlock(&dpif_netdev->dp->queue_rwlock);
1644 dp_netdev_purge_queues(dpif_netdev->dp);
1645 fat_rwlock_unlock(&dpif_netdev->dp->queue_rwlock);
1648 /* Creates and returns a new 'struct dp_netdev_actions', with a reference count
1649 * of 1, whose actions are a copy of from the 'ofpacts_len' bytes of
1651 struct dp_netdev_actions *
1652 dp_netdev_actions_create(const struct nlattr *actions, size_t size)
1654 struct dp_netdev_actions *netdev_actions;
1656 netdev_actions = xmalloc(sizeof *netdev_actions);
1657 netdev_actions->actions = xmemdup(actions, size);
1658 netdev_actions->size = size;
1660 return netdev_actions;
1663 struct dp_netdev_actions *
1664 dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow)
1666 return ovsrcu_get(struct dp_netdev_actions *, &flow->actions);
1670 dp_netdev_actions_free(struct dp_netdev_actions *actions)
1672 free(actions->actions);
1677 dp_forwarder_main(void *f_)
1679 struct dp_forwarder *f = f_;
1680 struct dp_netdev *dp = f->dp;
1682 f->name = xasprintf("forwarder_%u", ovsthread_id_self());
1683 set_subprogram_name("%s", f->name);
1685 while (!latch_is_set(&dp->exit_latch)) {
1686 bool received_anything;
1689 ovs_rwlock_rdlock(&dp->port_rwlock);
1690 for (i = 0; i < 50; i++) {
1691 struct dp_netdev_port *port;
1693 received_anything = false;
1694 HMAP_FOR_EACH (port, node, &f->dp->ports) {
1696 && port->node.hash >= f->min_hash
1697 && port->node.hash <= f->max_hash) {
1698 struct ofpbuf *packets[NETDEV_MAX_RX_BATCH];
1702 error = netdev_rx_recv(port->rx, packets, &count);
1705 struct pkt_metadata md
1706 = PKT_METADATA_INITIALIZER(port->port_no);
1708 for (i = 0; i < count; i++) {
1709 dp_netdev_port_input(dp, packets[i], &md);
1711 received_anything = true;
1712 } else if (error != EAGAIN && error != EOPNOTSUPP) {
1713 static struct vlog_rate_limit rl
1714 = VLOG_RATE_LIMIT_INIT(1, 5);
1716 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1717 netdev_get_name(port->netdev),
1718 ovs_strerror(error));
1723 if (!received_anything) {
1728 if (received_anything) {
1729 poll_immediate_wake();
1731 struct dp_netdev_port *port;
1733 HMAP_FOR_EACH (port, node, &f->dp->ports)
1735 && port->node.hash >= f->min_hash
1736 && port->node.hash <= f->max_hash) {
1737 netdev_rx_wait(port->rx);
1739 seq_wait(dp->port_seq, seq_read(dp->port_seq));
1740 latch_wait(&dp->exit_latch);
1742 ovs_rwlock_unlock(&dp->port_rwlock);
1753 dp_netdev_set_threads(struct dp_netdev *dp, int n)
1757 if (n == dp->n_forwarders) {
1761 /* Stop existing threads. */
1762 latch_set(&dp->exit_latch);
1763 for (i = 0; i < dp->n_forwarders; i++) {
1764 struct dp_forwarder *f = &dp->forwarders[i];
1766 xpthread_join(f->thread, NULL);
1768 latch_poll(&dp->exit_latch);
1769 free(dp->forwarders);
1771 /* Start new threads. */
1772 dp->forwarders = xmalloc(n * sizeof *dp->forwarders);
1773 dp->n_forwarders = n;
1774 for (i = 0; i < n; i++) {
1775 struct dp_forwarder *f = &dp->forwarders[i];
1778 f->min_hash = UINT32_MAX / n * i;
1779 f->max_hash = UINT32_MAX / n * (i + 1) - 1;
1781 f->max_hash = UINT32_MAX;
1783 xpthread_create(&f->thread, NULL, dp_forwarder_main, f);
1788 dp_netdev_flow_stats_new_cb(void)
1790 struct dp_netdev_flow_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1791 ovs_mutex_init(&bucket->mutex);
1796 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow,
1797 const struct ofpbuf *packet,
1798 const struct flow *key)
1800 uint16_t tcp_flags = ntohs(key->tcp_flags);
1801 long long int now = time_msec();
1802 struct dp_netdev_flow_stats *bucket;
1804 bucket = ovsthread_stats_bucket_get(&netdev_flow->stats,
1805 dp_netdev_flow_stats_new_cb);
1807 ovs_mutex_lock(&bucket->mutex);
1808 bucket->used = MAX(now, bucket->used);
1809 bucket->packet_count++;
1810 bucket->byte_count += packet->size;
1811 bucket->tcp_flags |= tcp_flags;
1812 ovs_mutex_unlock(&bucket->mutex);
1816 dp_netdev_stats_new_cb(void)
1818 struct dp_netdev_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1819 ovs_mutex_init(&bucket->mutex);
1824 dp_netdev_count_packet(struct dp_netdev *dp, enum dp_stat_type type)
1826 struct dp_netdev_stats *bucket;
1828 bucket = ovsthread_stats_bucket_get(&dp->stats, dp_netdev_stats_new_cb);
1829 ovs_mutex_lock(&bucket->mutex);
1831 ovs_mutex_unlock(&bucket->mutex);
1835 dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
1836 struct pkt_metadata *md)
1837 OVS_REQ_RDLOCK(dp->port_rwlock)
1839 struct dp_netdev_flow *netdev_flow;
1842 if (packet->size < ETH_HEADER_LEN) {
1843 ofpbuf_delete(packet);
1846 flow_extract(packet, md, &key);
1847 netdev_flow = dp_netdev_lookup_flow(dp, &key);
1849 struct dp_netdev_actions *actions;
1851 dp_netdev_flow_used(netdev_flow, packet, &key);
1853 actions = dp_netdev_flow_get_actions(netdev_flow);
1854 dp_netdev_execute_actions(dp, &key, packet, true, md,
1855 actions->actions, actions->size);
1856 dp_netdev_count_packet(dp, DP_STAT_HIT);
1857 } else if (dp->handler_queues) {
1858 dp_netdev_count_packet(dp, DP_STAT_MISS);
1859 dp_netdev_output_userspace(dp, packet,
1860 flow_hash_5tuple(&key, 0) % dp->n_handlers,
1861 DPIF_UC_MISS, &key, NULL);
1862 ofpbuf_delete(packet);
1867 dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *packet,
1868 int queue_no, int type, const struct flow *flow,
1869 const struct nlattr *userdata)
1870 OVS_EXCLUDED(dp->queue_rwlock)
1872 struct dp_netdev_queue *q;
1875 fat_rwlock_rdlock(&dp->queue_rwlock);
1876 q = &dp->handler_queues[queue_no];
1877 ovs_mutex_lock(&q->mutex);
1878 if (q->head - q->tail < MAX_QUEUE_LEN) {
1879 struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
1880 struct dpif_upcall *upcall = &u->upcall;
1881 struct ofpbuf *buf = &u->buf;
1884 upcall->type = type;
1886 /* Allocate buffer big enough for everything. */
1887 buf_size = ODPUTIL_FLOW_KEY_BYTES;
1889 buf_size += NLA_ALIGN(userdata->nla_len);
1891 buf_size += packet->size;
1892 ofpbuf_init(buf, buf_size);
1895 odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
1896 upcall->key = buf->data;
1897 upcall->key_len = buf->size;
1901 upcall->userdata = ofpbuf_put(buf, userdata,
1902 NLA_ALIGN(userdata->nla_len));
1905 upcall->packet.data = ofpbuf_put(buf, packet->data, packet->size);
1906 upcall->packet.size = packet->size;
1912 dp_netdev_count_packet(dp, DP_STAT_LOST);
1915 ovs_mutex_unlock(&q->mutex);
1916 fat_rwlock_unlock(&dp->queue_rwlock);
1921 struct dp_netdev_execute_aux {
1922 struct dp_netdev *dp;
1923 const struct flow *key;
1927 dp_execute_cb(void *aux_, struct ofpbuf *packet,
1928 const struct pkt_metadata *md OVS_UNUSED,
1929 const struct nlattr *a, bool may_steal)
1930 OVS_NO_THREAD_SAFETY_ANALYSIS
1932 struct dp_netdev_execute_aux *aux = aux_;
1933 int type = nl_attr_type(a);
1934 struct dp_netdev_port *p;
1936 switch ((enum ovs_action_attr)type) {
1937 case OVS_ACTION_ATTR_OUTPUT:
1938 p = dp_netdev_lookup_port(aux->dp, u32_to_odp(nl_attr_get_u32(a)));
1940 netdev_send(p->netdev, packet);
1944 case OVS_ACTION_ATTR_USERSPACE: {
1945 const struct nlattr *userdata;
1947 userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
1949 dp_netdev_output_userspace(aux->dp, packet,
1950 flow_hash_5tuple(aux->key, 0)
1951 % aux->dp->n_handlers,
1952 DPIF_UC_ACTION, aux->key,
1956 case OVS_ACTION_ATTR_PUSH_VLAN:
1957 case OVS_ACTION_ATTR_POP_VLAN:
1958 case OVS_ACTION_ATTR_PUSH_MPLS:
1959 case OVS_ACTION_ATTR_POP_MPLS:
1960 case OVS_ACTION_ATTR_SET:
1961 case OVS_ACTION_ATTR_SAMPLE:
1962 case OVS_ACTION_ATTR_UNSPEC:
1963 case __OVS_ACTION_ATTR_MAX:
1968 ofpbuf_delete(packet);
1973 dp_netdev_execute_actions(struct dp_netdev *dp, const struct flow *key,
1974 struct ofpbuf *packet, bool may_steal,
1975 struct pkt_metadata *md,
1976 const struct nlattr *actions, size_t actions_len)
1977 OVS_REQ_RDLOCK(dp->port_rwlock)
1979 struct dp_netdev_execute_aux aux = {dp, key};
1981 odp_execute_actions(&aux, packet, may_steal, md,
1982 actions, actions_len, dp_execute_cb);
1985 const struct dpif_class dpif_netdev_class = {
1987 dpif_netdev_enumerate,
1988 dpif_netdev_port_open_type,
1991 dpif_netdev_destroy,
1994 dpif_netdev_get_stats,
1995 dpif_netdev_port_add,
1996 dpif_netdev_port_del,
1997 dpif_netdev_port_query_by_number,
1998 dpif_netdev_port_query_by_name,
1999 NULL, /* port_get_pid */
2000 dpif_netdev_port_dump_start,
2001 dpif_netdev_port_dump_next,
2002 dpif_netdev_port_dump_done,
2003 dpif_netdev_port_poll,
2004 dpif_netdev_port_poll_wait,
2005 dpif_netdev_flow_get,
2006 dpif_netdev_flow_put,
2007 dpif_netdev_flow_del,
2008 dpif_netdev_flow_flush,
2009 dpif_netdev_flow_dump_state_init,
2010 dpif_netdev_flow_dump_start,
2011 dpif_netdev_flow_dump_next,
2013 dpif_netdev_flow_dump_done,
2014 dpif_netdev_flow_dump_state_uninit,
2015 dpif_netdev_execute,
2017 dpif_netdev_recv_set,
2018 dpif_netdev_handlers_set,
2019 dpif_netdev_queue_to_priority,
2021 dpif_netdev_recv_wait,
2022 dpif_netdev_recv_purge,
2026 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
2027 const char *argv[], void *aux OVS_UNUSED)
2029 struct dp_netdev_port *port;
2030 struct dp_netdev *dp;
2033 ovs_mutex_lock(&dp_netdev_mutex);
2034 dp = shash_find_data(&dp_netdevs, argv[1]);
2035 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
2036 ovs_mutex_unlock(&dp_netdev_mutex);
2037 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
2040 ovs_refcount_ref(&dp->ref_cnt);
2041 ovs_mutex_unlock(&dp_netdev_mutex);
2043 ovs_rwlock_wrlock(&dp->port_rwlock);
2044 if (get_port_by_name(dp, argv[2], &port)) {
2045 unixctl_command_reply_error(conn, "unknown port");
2049 port_no = u32_to_odp(atoi(argv[3]));
2050 if (!port_no || port_no == ODPP_NONE) {
2051 unixctl_command_reply_error(conn, "bad port number");
2054 if (dp_netdev_lookup_port(dp, port_no)) {
2055 unixctl_command_reply_error(conn, "port number already in use");
2058 hmap_remove(&dp->ports, &port->node);
2059 port->port_no = port_no;
2060 hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
2061 seq_change(dp->port_seq);
2062 unixctl_command_reply(conn, NULL);
2065 ovs_rwlock_unlock(&dp->port_rwlock);
2066 dp_netdev_unref(dp);
2070 dpif_dummy_register__(const char *type)
2072 struct dpif_class *class;
2074 class = xmalloc(sizeof *class);
2075 *class = dpif_netdev_class;
2076 class->type = xstrdup(type);
2077 dp_register_provider(class);
2081 dpif_dummy_register(bool override)
2088 dp_enumerate_types(&types);
2089 SSET_FOR_EACH (type, &types) {
2090 if (!dp_unregister_provider(type)) {
2091 dpif_dummy_register__(type);
2094 sset_destroy(&types);
2097 dpif_dummy_register__("dummy");
2099 unixctl_command_register("dpif-dummy/change-port-number",
2100 "DP PORT NEW-NUMBER",
2101 3, 3, dpif_dummy_change_port_number, NULL);