2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2016 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.
18 #include "dpif-netdev.h"
25 #include <netinet/in.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
38 #include "dp-packet.h"
40 #include "dpif-provider.h"
42 #include "fat-rwlock.h"
47 #include "netdev-dpdk.h"
48 #include "netdev-vport.h"
50 #include "odp-execute.h"
52 #include "openvswitch/dynamic-string.h"
53 #include "openvswitch/list.h"
54 #include "openvswitch/match.h"
55 #include "openvswitch/ofp-print.h"
56 #include "openvswitch/ofpbuf.h"
57 #include "openvswitch/vlog.h"
61 #include "poll-loop.h"
68 #include "tnl-neigh-cache.h"
69 #include "tnl-ports.h"
73 VLOG_DEFINE_THIS_MODULE(dpif_netdev
);
75 #define FLOW_DUMP_MAX_BATCH 50
76 /* Use per thread recirc_depth to prevent recirculation loop. */
77 #define MAX_RECIRC_DEPTH 5
78 DEFINE_STATIC_PER_THREAD_DATA(uint32_t, recirc_depth
, 0)
80 /* Configuration parameters. */
81 enum { MAX_FLOWS
= 65536 }; /* Maximum number of flows in flow table. */
83 /* Protects against changes to 'dp_netdevs'. */
84 static struct ovs_mutex dp_netdev_mutex
= OVS_MUTEX_INITIALIZER
;
86 /* Contains all 'struct dp_netdev's. */
87 static struct shash dp_netdevs
OVS_GUARDED_BY(dp_netdev_mutex
)
88 = SHASH_INITIALIZER(&dp_netdevs
);
90 static struct vlog_rate_limit upcall_rl
= VLOG_RATE_LIMIT_INIT(600, 600);
92 static struct odp_support dp_netdev_support
= {
93 .max_mpls_depth
= SIZE_MAX
,
97 /* Stores a miniflow with inline values */
99 struct netdev_flow_key
{
100 uint32_t hash
; /* Hash function differs for different users. */
101 uint32_t len
; /* Length of the following miniflow (incl. map). */
103 uint64_t buf
[FLOW_MAX_PACKET_U64S
];
106 /* Exact match cache for frequently used flows
108 * The cache uses a 32-bit hash of the packet (which can be the RSS hash) to
109 * search its entries for a miniflow that matches exactly the miniflow of the
110 * packet. It stores the 'dpcls_rule' (rule) that matches the miniflow.
112 * A cache entry holds a reference to its 'dp_netdev_flow'.
114 * A miniflow with a given hash can be in one of EM_FLOW_HASH_SEGS different
115 * entries. The 32-bit hash is split into EM_FLOW_HASH_SEGS values (each of
116 * them is EM_FLOW_HASH_SHIFT bits wide and the remainder is thrown away). Each
117 * value is the index of a cache entry where the miniflow could be.
123 * Each pmd_thread has its own private exact match cache.
124 * If dp_netdev_input is not called from a pmd thread, a mutex is used.
127 #define EM_FLOW_HASH_SHIFT 13
128 #define EM_FLOW_HASH_ENTRIES (1u << EM_FLOW_HASH_SHIFT)
129 #define EM_FLOW_HASH_MASK (EM_FLOW_HASH_ENTRIES - 1)
130 #define EM_FLOW_HASH_SEGS 2
133 struct dp_netdev_flow
*flow
;
134 struct netdev_flow_key key
; /* key.hash used for emc hash value. */
138 struct emc_entry entries
[EM_FLOW_HASH_ENTRIES
];
139 int sweep_idx
; /* For emc_cache_slow_sweep(). */
142 /* Iterate in the exact match cache through every entry that might contain a
143 * miniflow with hash 'HASH'. */
144 #define EMC_FOR_EACH_POS_WITH_HASH(EMC, CURRENT_ENTRY, HASH) \
145 for (uint32_t i__ = 0, srch_hash__ = (HASH); \
146 (CURRENT_ENTRY) = &(EMC)->entries[srch_hash__ & EM_FLOW_HASH_MASK], \
147 i__ < EM_FLOW_HASH_SEGS; \
148 i__++, srch_hash__ >>= EM_FLOW_HASH_SHIFT)
150 /* Simple non-wildcarding single-priority classifier. */
153 struct cmap subtables_map
;
154 struct pvector subtables
;
157 /* A rule to be inserted to the classifier. */
159 struct cmap_node cmap_node
; /* Within struct dpcls_subtable 'rules'. */
160 struct netdev_flow_key
*mask
; /* Subtable's mask. */
161 struct netdev_flow_key flow
; /* Matching key. */
162 /* 'flow' must be the last field, additional space is allocated here. */
165 static void dpcls_init(struct dpcls
*);
166 static void dpcls_destroy(struct dpcls
*);
167 static void dpcls_insert(struct dpcls
*, struct dpcls_rule
*,
168 const struct netdev_flow_key
*mask
);
169 static void dpcls_remove(struct dpcls
*, struct dpcls_rule
*);
170 static bool dpcls_lookup(const struct dpcls
*cls
,
171 const struct netdev_flow_key keys
[],
172 struct dpcls_rule
**rules
, size_t cnt
);
174 /* Datapath based on the network device interface from netdev.h.
180 * Some members, marked 'const', are immutable. Accessing other members
181 * requires synchronization, as noted in more detail below.
183 * Acquisition order is, from outermost to innermost:
185 * dp_netdev_mutex (global)
190 const struct dpif_class
*const class;
191 const char *const name
;
193 struct ovs_refcount ref_cnt
;
194 atomic_flag destroyed
;
198 * Any lookup into 'ports' or any access to the dp_netdev_ports found
199 * through 'ports' requires taking 'port_mutex'. */
200 struct ovs_mutex port_mutex
;
202 struct seq
*port_seq
; /* Incremented whenever a port changes. */
204 /* Protects access to ofproto-dpif-upcall interface during revalidator
205 * thread synchronization. */
206 struct fat_rwlock upcall_rwlock
;
207 upcall_callback
*upcall_cb
; /* Callback function for executing upcalls. */
210 /* Callback function for notifying the purging of dp flows (during
211 * reseting pmd deletion). */
212 dp_purge_callback
*dp_purge_cb
;
215 /* Stores all 'struct dp_netdev_pmd_thread's. */
216 struct cmap poll_threads
;
218 /* Protects the access of the 'struct dp_netdev_pmd_thread'
219 * instance for non-pmd thread. */
220 struct ovs_mutex non_pmd_mutex
;
222 /* Each pmd thread will store its pointer to
223 * 'struct dp_netdev_pmd_thread' in 'per_pmd_key'. */
224 ovsthread_key_t per_pmd_key
;
226 /* Cpu mask for pin of pmd threads. */
227 char *requested_pmd_cmask
;
230 uint64_t last_tnl_conf_seq
;
233 static struct dp_netdev_port
*dp_netdev_lookup_port(const struct dp_netdev
*dp
,
235 OVS_REQUIRES(dp
->port_mutex
);
238 DP_STAT_EXACT_HIT
, /* Packets that had an exact match (emc). */
239 DP_STAT_MASKED_HIT
, /* Packets that matched in the flow table. */
240 DP_STAT_MISS
, /* Packets that did not match. */
241 DP_STAT_LOST
, /* Packets not passed up to the client. */
245 enum pmd_cycles_counter_type
{
246 PMD_CYCLES_POLLING
, /* Cycles spent polling NICs. */
247 PMD_CYCLES_PROCESSING
, /* Cycles spent processing packets */
251 /* A port in a netdev-based datapath. */
252 struct dp_netdev_port
{
254 struct netdev
*netdev
;
255 struct hmap_node node
; /* Node in dp_netdev's 'ports'. */
256 struct netdev_saved_flags
*sf
;
257 unsigned n_rxq
; /* Number of elements in 'rxq' */
258 struct netdev_rxq
**rxq
;
259 char *type
; /* Port type as requested by user. */
262 /* Contained by struct dp_netdev_flow's 'stats' member. */
263 struct dp_netdev_flow_stats
{
264 atomic_llong used
; /* Last used time, in monotonic msecs. */
265 atomic_ullong packet_count
; /* Number of packets matched. */
266 atomic_ullong byte_count
; /* Number of bytes matched. */
267 atomic_uint16_t tcp_flags
; /* Bitwise-OR of seen tcp_flags values. */
270 /* A flow in 'dp_netdev_pmd_thread's 'flow_table'.
276 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
277 * its pmd thread's classifier. The text below calls this classifier 'cls'.
282 * The thread safety rules described here for "struct dp_netdev_flow" are
283 * motivated by two goals:
285 * - Prevent threads that read members of "struct dp_netdev_flow" from
286 * reading bad data due to changes by some thread concurrently modifying
289 * - Prevent two threads making changes to members of a given "struct
290 * dp_netdev_flow" from interfering with each other.
296 * A flow 'flow' may be accessed without a risk of being freed during an RCU
297 * grace period. Code that needs to hold onto a flow for a while
298 * should try incrementing 'flow->ref_cnt' with dp_netdev_flow_ref().
300 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
301 * flow from being deleted from 'cls' and it doesn't protect members of 'flow'
304 * Some members, marked 'const', are immutable. Accessing other members
305 * requires synchronization, as noted in more detail below.
307 struct dp_netdev_flow
{
308 const struct flow flow
; /* Unmasked flow that created this entry. */
309 /* Hash table index by unmasked flow. */
310 const struct cmap_node node
; /* In owning dp_netdev_pmd_thread's */
312 const ovs_u128 ufid
; /* Unique flow identifier. */
313 const unsigned pmd_id
; /* The 'core_id' of pmd thread owning this */
316 /* Number of references.
317 * The classifier owns one reference.
318 * Any thread trying to keep a rule from being freed should hold its own
320 struct ovs_refcount ref_cnt
;
325 struct dp_netdev_flow_stats stats
;
328 OVSRCU_TYPE(struct dp_netdev_actions
*) actions
;
330 /* While processing a group of input packets, the datapath uses the next
331 * member to store a pointer to the output batch for the flow. It is
332 * reset after the batch has been sent out (See dp_netdev_queue_batches(),
333 * packet_batch_per_flow_init() and packet_batch_per_flow_execute()). */
334 struct packet_batch_per_flow
*batch
;
336 /* Packet classification. */
337 struct dpcls_rule cr
; /* In owning dp_netdev's 'cls'. */
338 /* 'cr' must be the last member. */
341 static void dp_netdev_flow_unref(struct dp_netdev_flow
*);
342 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*);
343 static int dpif_netdev_flow_from_nlattrs(const struct nlattr
*, uint32_t,
346 /* A set of datapath actions within a "struct dp_netdev_flow".
352 * A struct dp_netdev_actions 'actions' is protected with RCU. */
353 struct dp_netdev_actions
{
354 /* These members are immutable: they do not change during the struct's
356 unsigned int size
; /* Size of 'actions', in bytes. */
357 struct nlattr actions
[]; /* Sequence of OVS_ACTION_ATTR_* attributes. */
360 struct dp_netdev_actions
*dp_netdev_actions_create(const struct nlattr
*,
362 struct dp_netdev_actions
*dp_netdev_flow_get_actions(
363 const struct dp_netdev_flow
*);
364 static void dp_netdev_actions_free(struct dp_netdev_actions
*);
366 /* Contained by struct dp_netdev_pmd_thread's 'stats' member. */
367 struct dp_netdev_pmd_stats
{
368 /* Indexed by DP_STAT_*. */
369 atomic_ullong n
[DP_N_STATS
];
372 /* Contained by struct dp_netdev_pmd_thread's 'cycle' member. */
373 struct dp_netdev_pmd_cycles
{
374 /* Indexed by PMD_CYCLES_*. */
375 atomic_ullong n
[PMD_N_CYCLES
];
378 /* Contained by struct dp_netdev_pmd_thread's 'poll_list' member. */
380 struct dp_netdev_port
*port
;
381 struct netdev_rxq
*rx
;
382 struct ovs_list node
;
385 /* Contained by struct dp_netdev_pmd_thread's 'port_cache' or 'tx_ports'. */
388 struct netdev
*netdev
;
389 struct hmap_node node
;
392 /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
393 * the performance overhead of interrupt processing. Therefore netdev can
394 * not implement rx-wait for these devices. dpif-netdev needs to poll
395 * these device to check for recv buffer. pmd-thread does polling for
396 * devices assigned to itself.
398 * DPDK used PMD for accessing NIC.
400 * Note, instance with cpu core id NON_PMD_CORE_ID will be reserved for
401 * I/O of all non-pmd threads. There will be no actual thread created
404 * Each struct has its own flow table and classifier. Packets received
405 * from managed ports are looked up in the corresponding pmd thread's
406 * flow table, and are executed with the found actions.
408 struct dp_netdev_pmd_thread
{
409 struct dp_netdev
*dp
;
410 struct ovs_refcount ref_cnt
; /* Every reference must be refcount'ed. */
411 struct cmap_node node
; /* In 'dp->poll_threads'. */
413 pthread_cond_t cond
; /* For synchronizing pmd thread reload. */
414 struct ovs_mutex cond_mutex
; /* Mutex for condition variable. */
416 /* Per thread exact-match cache. Note, the instance for cpu core
417 * NON_PMD_CORE_ID can be accessed by multiple threads, and thusly
418 * need to be protected by 'non_pmd_mutex'. Every other instance
419 * will only be accessed by its own pmd thread. */
420 struct emc_cache flow_cache
;
422 /* Classifier and Flow-Table.
424 * Writers of 'flow_table' must take the 'flow_mutex'. Corresponding
425 * changes to 'cls' must be made while still holding the 'flow_mutex'.
427 struct ovs_mutex flow_mutex
;
429 struct cmap flow_table OVS_GUARDED
; /* Flow table. */
432 struct dp_netdev_pmd_stats stats
;
434 /* Cycles counters */
435 struct dp_netdev_pmd_cycles cycles
;
437 /* Used to count cicles. See 'cycles_counter_end()' */
438 unsigned long long last_cycles
;
440 struct latch exit_latch
; /* For terminating the pmd thread. */
441 atomic_uint change_seq
; /* For reloading pmd ports. */
443 unsigned core_id
; /* CPU core id of this pmd thread. */
444 int numa_id
; /* numa node id of this pmd thread. */
445 atomic_int tx_qid
; /* Queue id used by this pmd thread to
446 * send packets on all netdevs */
448 struct ovs_mutex port_mutex
; /* Mutex for 'poll_list' and 'tx_ports'. */
449 /* List of rx queues to poll. */
450 struct ovs_list poll_list OVS_GUARDED
;
451 /* Number of elements in 'poll_list' */
453 /* Map of 'tx_port's used for transmission. Written by the main thread,
454 * read by the pmd thread. */
455 struct hmap tx_ports OVS_GUARDED
;
457 /* Map of 'tx_port' used in the fast path. This is a thread-local copy of
458 * 'tx_ports'. The instance for cpu core NON_PMD_CORE_ID can be accessed
459 * by multiple threads, and thusly need to be protected by 'non_pmd_mutex'.
460 * Every other instance will only be accessed by its own pmd thread. */
461 struct hmap port_cache
;
463 /* Only a pmd thread can write on its own 'cycles' and 'stats'.
464 * The main thread keeps 'stats_zero' and 'cycles_zero' as base
465 * values and subtracts them from 'stats' and 'cycles' before
466 * reporting to the user */
467 unsigned long long stats_zero
[DP_N_STATS
];
468 uint64_t cycles_zero
[PMD_N_CYCLES
];
471 #define PMD_INITIAL_SEQ 1
473 /* Interface to netdev-based datapath. */
476 struct dp_netdev
*dp
;
477 uint64_t last_port_seq
;
480 static int get_port_by_number(struct dp_netdev
*dp
, odp_port_t port_no
,
481 struct dp_netdev_port
**portp
)
482 OVS_REQUIRES(dp
->port_mutex
);
483 static int get_port_by_name(struct dp_netdev
*dp
, const char *devname
,
484 struct dp_netdev_port
**portp
)
485 OVS_REQUIRES(dp
->port_mutex
);
486 static void dp_netdev_free(struct dp_netdev
*)
487 OVS_REQUIRES(dp_netdev_mutex
);
488 static int do_add_port(struct dp_netdev
*dp
, const char *devname
,
489 const char *type
, odp_port_t port_no
)
490 OVS_REQUIRES(dp
->port_mutex
);
491 static void do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*)
492 OVS_REQUIRES(dp
->port_mutex
);
493 static int dpif_netdev_open(const struct dpif_class
*, const char *name
,
494 bool create
, struct dpif
**);
495 static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
496 struct dp_packet_batch
*,
498 const struct nlattr
*actions
,
500 static void dp_netdev_input(struct dp_netdev_pmd_thread
*,
501 struct dp_packet_batch
*, odp_port_t port_no
);
502 static void dp_netdev_recirculate(struct dp_netdev_pmd_thread
*,
503 struct dp_packet_batch
*);
505 static void dp_netdev_disable_upcall(struct dp_netdev
*);
506 static void dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
);
507 static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
,
508 struct dp_netdev
*dp
, unsigned core_id
,
510 static void dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
);
511 static void dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
512 OVS_REQUIRES(dp
->port_mutex
);
514 static struct dp_netdev_pmd_thread
*dp_netdev_get_pmd(struct dp_netdev
*dp
,
516 static struct dp_netdev_pmd_thread
*
517 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
);
518 static void dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
);
519 static void dp_netdev_del_pmds_on_numa(struct dp_netdev
*dp
, int numa_id
);
520 static void dp_netdev_set_pmds_on_numa(struct dp_netdev
*dp
, int numa_id
)
521 OVS_REQUIRES(dp
->port_mutex
);
522 static void dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
);
523 static void dp_netdev_del_port_from_all_pmds(struct dp_netdev
*dp
,
524 struct dp_netdev_port
*port
);
525 static void dp_netdev_add_port_to_pmds(struct dp_netdev
*dp
,
526 struct dp_netdev_port
*port
);
527 static void dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
528 struct dp_netdev_port
*port
);
529 static void dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
530 struct dp_netdev_port
*port
,
531 struct netdev_rxq
*rx
);
532 static struct dp_netdev_pmd_thread
*
533 dp_netdev_less_loaded_pmd_on_numa(struct dp_netdev
*dp
, int numa_id
);
534 static void dp_netdev_reset_pmd_threads(struct dp_netdev
*dp
)
535 OVS_REQUIRES(dp
->port_mutex
);
536 static bool dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
);
537 static void dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
);
538 static void dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
);
539 static void pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
540 OVS_REQUIRES(pmd
->port_mutex
);
542 static inline bool emc_entry_alive(struct emc_entry
*ce
);
543 static void emc_clear_entry(struct emc_entry
*ce
);
546 emc_cache_init(struct emc_cache
*flow_cache
)
550 flow_cache
->sweep_idx
= 0;
551 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
552 flow_cache
->entries
[i
].flow
= NULL
;
553 flow_cache
->entries
[i
].key
.hash
= 0;
554 flow_cache
->entries
[i
].key
.len
= sizeof(struct miniflow
);
555 flowmap_init(&flow_cache
->entries
[i
].key
.mf
.map
);
560 emc_cache_uninit(struct emc_cache
*flow_cache
)
564 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
565 emc_clear_entry(&flow_cache
->entries
[i
]);
569 /* Check and clear dead flow references slowly (one entry at each
572 emc_cache_slow_sweep(struct emc_cache
*flow_cache
)
574 struct emc_entry
*entry
= &flow_cache
->entries
[flow_cache
->sweep_idx
];
576 if (!emc_entry_alive(entry
)) {
577 emc_clear_entry(entry
);
579 flow_cache
->sweep_idx
= (flow_cache
->sweep_idx
+ 1) & EM_FLOW_HASH_MASK
;
582 /* Returns true if 'dpif' is a netdev or dummy dpif, false otherwise. */
584 dpif_is_netdev(const struct dpif
*dpif
)
586 return dpif
->dpif_class
->open
== dpif_netdev_open
;
589 static struct dpif_netdev
*
590 dpif_netdev_cast(const struct dpif
*dpif
)
592 ovs_assert(dpif_is_netdev(dpif
));
593 return CONTAINER_OF(dpif
, struct dpif_netdev
, dpif
);
596 static struct dp_netdev
*
597 get_dp_netdev(const struct dpif
*dpif
)
599 return dpif_netdev_cast(dpif
)->dp
;
603 PMD_INFO_SHOW_STATS
, /* Show how cpu cycles are spent. */
604 PMD_INFO_CLEAR_STATS
, /* Set the cycles count to 0. */
605 PMD_INFO_SHOW_RXQ
/* Show poll-lists of pmd threads. */
609 pmd_info_show_stats(struct ds
*reply
,
610 struct dp_netdev_pmd_thread
*pmd
,
611 unsigned long long stats
[DP_N_STATS
],
612 uint64_t cycles
[PMD_N_CYCLES
])
614 unsigned long long total_packets
= 0;
615 uint64_t total_cycles
= 0;
618 /* These loops subtracts reference values ('*_zero') from the counters.
619 * Since loads and stores are relaxed, it might be possible for a '*_zero'
620 * value to be more recent than the current value we're reading from the
621 * counter. This is not a big problem, since these numbers are not
622 * supposed to be too accurate, but we should at least make sure that
623 * the result is not negative. */
624 for (i
= 0; i
< DP_N_STATS
; i
++) {
625 if (stats
[i
] > pmd
->stats_zero
[i
]) {
626 stats
[i
] -= pmd
->stats_zero
[i
];
631 if (i
!= DP_STAT_LOST
) {
632 /* Lost packets are already included in DP_STAT_MISS */
633 total_packets
+= stats
[i
];
637 for (i
= 0; i
< PMD_N_CYCLES
; i
++) {
638 if (cycles
[i
] > pmd
->cycles_zero
[i
]) {
639 cycles
[i
] -= pmd
->cycles_zero
[i
];
644 total_cycles
+= cycles
[i
];
647 ds_put_cstr(reply
, (pmd
->core_id
== NON_PMD_CORE_ID
)
648 ? "main thread" : "pmd thread");
650 if (pmd
->numa_id
!= OVS_NUMA_UNSPEC
) {
651 ds_put_format(reply
, " numa_id %d", pmd
->numa_id
);
653 if (pmd
->core_id
!= OVS_CORE_UNSPEC
&& pmd
->core_id
!= NON_PMD_CORE_ID
) {
654 ds_put_format(reply
, " core_id %u", pmd
->core_id
);
656 ds_put_cstr(reply
, ":\n");
659 "\temc hits:%llu\n\tmegaflow hits:%llu\n"
660 "\tmiss:%llu\n\tlost:%llu\n",
661 stats
[DP_STAT_EXACT_HIT
], stats
[DP_STAT_MASKED_HIT
],
662 stats
[DP_STAT_MISS
], stats
[DP_STAT_LOST
]);
664 if (total_cycles
== 0) {
669 "\tpolling cycles:%"PRIu64
" (%.02f%%)\n"
670 "\tprocessing cycles:%"PRIu64
" (%.02f%%)\n",
671 cycles
[PMD_CYCLES_POLLING
],
672 cycles
[PMD_CYCLES_POLLING
] / (double)total_cycles
* 100,
673 cycles
[PMD_CYCLES_PROCESSING
],
674 cycles
[PMD_CYCLES_PROCESSING
] / (double)total_cycles
* 100);
676 if (total_packets
== 0) {
681 "\tavg cycles per packet: %.02f (%"PRIu64
"/%llu)\n",
682 total_cycles
/ (double)total_packets
,
683 total_cycles
, total_packets
);
686 "\tavg processing cycles per packet: "
687 "%.02f (%"PRIu64
"/%llu)\n",
688 cycles
[PMD_CYCLES_PROCESSING
] / (double)total_packets
,
689 cycles
[PMD_CYCLES_PROCESSING
], total_packets
);
693 pmd_info_clear_stats(struct ds
*reply OVS_UNUSED
,
694 struct dp_netdev_pmd_thread
*pmd
,
695 unsigned long long stats
[DP_N_STATS
],
696 uint64_t cycles
[PMD_N_CYCLES
])
700 /* We cannot write 'stats' and 'cycles' (because they're written by other
701 * threads) and we shouldn't change 'stats' (because they're used to count
702 * datapath stats, which must not be cleared here). Instead, we save the
703 * current values and subtract them from the values to be displayed in the
705 for (i
= 0; i
< DP_N_STATS
; i
++) {
706 pmd
->stats_zero
[i
] = stats
[i
];
708 for (i
= 0; i
< PMD_N_CYCLES
; i
++) {
709 pmd
->cycles_zero
[i
] = cycles
[i
];
714 pmd_info_show_rxq(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
716 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
717 struct rxq_poll
*poll
;
718 const char *prev_name
= NULL
;
720 ds_put_format(reply
, "pmd thread numa_id %d core_id %u:\n",
721 pmd
->numa_id
, pmd
->core_id
);
723 ovs_mutex_lock(&pmd
->port_mutex
);
724 LIST_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
725 const char *name
= netdev_get_name(poll
->port
->netdev
);
727 if (!prev_name
|| strcmp(name
, prev_name
)) {
729 ds_put_cstr(reply
, "\n");
731 ds_put_format(reply
, "\tport: %s\tqueue-id:",
732 netdev_get_name(poll
->port
->netdev
));
734 ds_put_format(reply
, " %d", netdev_rxq_get_queue_id(poll
->rx
));
737 ovs_mutex_unlock(&pmd
->port_mutex
);
738 ds_put_cstr(reply
, "\n");
743 dpif_netdev_pmd_info(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
746 struct ds reply
= DS_EMPTY_INITIALIZER
;
747 struct dp_netdev_pmd_thread
*pmd
;
748 struct dp_netdev
*dp
= NULL
;
749 enum pmd_info_type type
= *(enum pmd_info_type
*) aux
;
751 ovs_mutex_lock(&dp_netdev_mutex
);
754 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
755 } else if (shash_count(&dp_netdevs
) == 1) {
756 /* There's only one datapath */
757 dp
= shash_first(&dp_netdevs
)->data
;
761 ovs_mutex_unlock(&dp_netdev_mutex
);
762 unixctl_command_reply_error(conn
,
763 "please specify an existing datapath");
767 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
768 if (type
== PMD_INFO_SHOW_RXQ
) {
769 pmd_info_show_rxq(&reply
, pmd
);
771 unsigned long long stats
[DP_N_STATS
];
772 uint64_t cycles
[PMD_N_CYCLES
];
775 /* Read current stats and cycle counters */
776 for (i
= 0; i
< ARRAY_SIZE(stats
); i
++) {
777 atomic_read_relaxed(&pmd
->stats
.n
[i
], &stats
[i
]);
779 for (i
= 0; i
< ARRAY_SIZE(cycles
); i
++) {
780 atomic_read_relaxed(&pmd
->cycles
.n
[i
], &cycles
[i
]);
783 if (type
== PMD_INFO_CLEAR_STATS
) {
784 pmd_info_clear_stats(&reply
, pmd
, stats
, cycles
);
785 } else if (type
== PMD_INFO_SHOW_STATS
) {
786 pmd_info_show_stats(&reply
, pmd
, stats
, cycles
);
791 ovs_mutex_unlock(&dp_netdev_mutex
);
793 unixctl_command_reply(conn
, ds_cstr(&reply
));
798 dpif_netdev_init(void)
800 static enum pmd_info_type show_aux
= PMD_INFO_SHOW_STATS
,
801 clear_aux
= PMD_INFO_CLEAR_STATS
,
802 poll_aux
= PMD_INFO_SHOW_RXQ
;
804 unixctl_command_register("dpif-netdev/pmd-stats-show", "[dp]",
805 0, 1, dpif_netdev_pmd_info
,
807 unixctl_command_register("dpif-netdev/pmd-stats-clear", "[dp]",
808 0, 1, dpif_netdev_pmd_info
,
810 unixctl_command_register("dpif-netdev/pmd-rxq-show", "[dp]",
811 0, 1, dpif_netdev_pmd_info
,
817 dpif_netdev_enumerate(struct sset
*all_dps
,
818 const struct dpif_class
*dpif_class
)
820 struct shash_node
*node
;
822 ovs_mutex_lock(&dp_netdev_mutex
);
823 SHASH_FOR_EACH(node
, &dp_netdevs
) {
824 struct dp_netdev
*dp
= node
->data
;
825 if (dpif_class
!= dp
->class) {
826 /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs.
827 * If the class doesn't match, skip this dpif. */
830 sset_add(all_dps
, node
->name
);
832 ovs_mutex_unlock(&dp_netdev_mutex
);
838 dpif_netdev_class_is_dummy(const struct dpif_class
*class)
840 return class != &dpif_netdev_class
;
844 dpif_netdev_port_open_type(const struct dpif_class
*class, const char *type
)
846 return strcmp(type
, "internal") ? type
847 : dpif_netdev_class_is_dummy(class) ? "dummy"
852 create_dpif_netdev(struct dp_netdev
*dp
)
854 uint16_t netflow_id
= hash_string(dp
->name
, 0);
855 struct dpif_netdev
*dpif
;
857 ovs_refcount_ref(&dp
->ref_cnt
);
859 dpif
= xmalloc(sizeof *dpif
);
860 dpif_init(&dpif
->dpif
, dp
->class, dp
->name
, netflow_id
>> 8, netflow_id
);
862 dpif
->last_port_seq
= seq_read(dp
->port_seq
);
867 /* Choose an unused, non-zero port number and return it on success.
868 * Return ODPP_NONE on failure. */
870 choose_port(struct dp_netdev
*dp
, const char *name
)
871 OVS_REQUIRES(dp
->port_mutex
)
875 if (dp
->class != &dpif_netdev_class
) {
879 /* If the port name begins with "br", start the number search at
880 * 100 to make writing tests easier. */
881 if (!strncmp(name
, "br", 2)) {
885 /* If the port name contains a number, try to assign that port number.
886 * This can make writing unit tests easier because port numbers are
888 for (p
= name
; *p
!= '\0'; p
++) {
889 if (isdigit((unsigned char) *p
)) {
890 port_no
= start_no
+ strtol(p
, NULL
, 10);
891 if (port_no
> 0 && port_no
!= odp_to_u32(ODPP_NONE
)
892 && !dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
893 return u32_to_odp(port_no
);
900 for (port_no
= 1; port_no
<= UINT16_MAX
; port_no
++) {
901 if (!dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
902 return u32_to_odp(port_no
);
910 create_dp_netdev(const char *name
, const struct dpif_class
*class,
911 struct dp_netdev
**dpp
)
912 OVS_REQUIRES(dp_netdev_mutex
)
914 struct dp_netdev
*dp
;
917 dp
= xzalloc(sizeof *dp
);
918 shash_add(&dp_netdevs
, name
, dp
);
920 *CONST_CAST(const struct dpif_class
**, &dp
->class) = class;
921 *CONST_CAST(const char **, &dp
->name
) = xstrdup(name
);
922 ovs_refcount_init(&dp
->ref_cnt
);
923 atomic_flag_clear(&dp
->destroyed
);
925 ovs_mutex_init(&dp
->port_mutex
);
926 hmap_init(&dp
->ports
);
927 dp
->port_seq
= seq_create();
928 fat_rwlock_init(&dp
->upcall_rwlock
);
930 /* Disable upcalls by default. */
931 dp_netdev_disable_upcall(dp
);
932 dp
->upcall_aux
= NULL
;
933 dp
->upcall_cb
= NULL
;
935 cmap_init(&dp
->poll_threads
);
936 ovs_mutex_init_recursive(&dp
->non_pmd_mutex
);
937 ovsthread_key_create(&dp
->per_pmd_key
, NULL
);
939 ovs_mutex_lock(&dp
->port_mutex
);
940 dp_netdev_set_nonpmd(dp
);
942 error
= do_add_port(dp
, name
, "internal", ODPP_LOCAL
);
943 ovs_mutex_unlock(&dp
->port_mutex
);
949 dp
->last_tnl_conf_seq
= seq_read(tnl_conf_seq
);
955 dpif_netdev_open(const struct dpif_class
*class, const char *name
,
956 bool create
, struct dpif
**dpifp
)
958 struct dp_netdev
*dp
;
961 ovs_mutex_lock(&dp_netdev_mutex
);
962 dp
= shash_find_data(&dp_netdevs
, name
);
964 error
= create
? create_dp_netdev(name
, class, &dp
) : ENODEV
;
966 error
= (dp
->class != class ? EINVAL
971 *dpifp
= create_dpif_netdev(dp
);
974 ovs_mutex_unlock(&dp_netdev_mutex
);
980 dp_netdev_destroy_upcall_lock(struct dp_netdev
*dp
)
981 OVS_NO_THREAD_SAFETY_ANALYSIS
983 /* Check that upcalls are disabled, i.e. that the rwlock is taken */
984 ovs_assert(fat_rwlock_tryrdlock(&dp
->upcall_rwlock
));
986 /* Before freeing a lock we should release it */
987 fat_rwlock_unlock(&dp
->upcall_rwlock
);
988 fat_rwlock_destroy(&dp
->upcall_rwlock
);
991 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
992 * through the 'dp_netdevs' shash while freeing 'dp'. */
994 dp_netdev_free(struct dp_netdev
*dp
)
995 OVS_REQUIRES(dp_netdev_mutex
)
997 struct dp_netdev_port
*port
, *next
;
999 shash_find_and_delete(&dp_netdevs
, dp
->name
);
1001 dp_netdev_destroy_all_pmds(dp
);
1002 ovs_mutex_destroy(&dp
->non_pmd_mutex
);
1003 ovsthread_key_delete(dp
->per_pmd_key
);
1005 ovs_mutex_lock(&dp
->port_mutex
);
1006 HMAP_FOR_EACH_SAFE (port
, next
, node
, &dp
->ports
) {
1007 do_del_port(dp
, port
);
1009 ovs_mutex_unlock(&dp
->port_mutex
);
1010 cmap_destroy(&dp
->poll_threads
);
1012 seq_destroy(dp
->port_seq
);
1013 hmap_destroy(&dp
->ports
);
1014 ovs_mutex_destroy(&dp
->port_mutex
);
1016 /* Upcalls must be disabled at this point */
1017 dp_netdev_destroy_upcall_lock(dp
);
1019 free(dp
->pmd_cmask
);
1020 free(CONST_CAST(char *, dp
->name
));
1025 dp_netdev_unref(struct dp_netdev
*dp
)
1028 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
1029 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
1030 ovs_mutex_lock(&dp_netdev_mutex
);
1031 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1034 ovs_mutex_unlock(&dp_netdev_mutex
);
1039 dpif_netdev_close(struct dpif
*dpif
)
1041 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1043 dp_netdev_unref(dp
);
1048 dpif_netdev_destroy(struct dpif
*dpif
)
1050 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1052 if (!atomic_flag_test_and_set(&dp
->destroyed
)) {
1053 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1054 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
1062 /* Add 'n' to the atomic variable 'var' non-atomically and using relaxed
1063 * load/store semantics. While the increment is not atomic, the load and
1064 * store operations are, making it impossible to read inconsistent values.
1066 * This is used to update thread local stats counters. */
1068 non_atomic_ullong_add(atomic_ullong
*var
, unsigned long long n
)
1070 unsigned long long tmp
;
1072 atomic_read_relaxed(var
, &tmp
);
1074 atomic_store_relaxed(var
, tmp
);
1078 dpif_netdev_get_stats(const struct dpif
*dpif
, struct dpif_dp_stats
*stats
)
1080 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1081 struct dp_netdev_pmd_thread
*pmd
;
1083 stats
->n_flows
= stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
1084 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1085 unsigned long long n
;
1086 stats
->n_flows
+= cmap_count(&pmd
->flow_table
);
1088 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_MASKED_HIT
], &n
);
1090 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_EXACT_HIT
], &n
);
1092 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_MISS
], &n
);
1093 stats
->n_missed
+= n
;
1094 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_LOST
], &n
);
1097 stats
->n_masks
= UINT32_MAX
;
1098 stats
->n_mask_hit
= UINT64_MAX
;
1104 dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread
*pmd
)
1108 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
1109 ovs_mutex_lock(&pmd
->dp
->non_pmd_mutex
);
1110 ovs_mutex_lock(&pmd
->port_mutex
);
1111 pmd_load_cached_ports(pmd
);
1112 ovs_mutex_unlock(&pmd
->port_mutex
);
1113 ovs_mutex_unlock(&pmd
->dp
->non_pmd_mutex
);
1117 ovs_mutex_lock(&pmd
->cond_mutex
);
1118 atomic_add_relaxed(&pmd
->change_seq
, 1, &old_seq
);
1119 ovs_mutex_cond_wait(&pmd
->cond
, &pmd
->cond_mutex
);
1120 ovs_mutex_unlock(&pmd
->cond_mutex
);
1124 hash_port_no(odp_port_t port_no
)
1126 return hash_int(odp_to_u32(port_no
), 0);
1130 port_create(const char *devname
, const char *open_type
, const char *type
,
1131 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1133 struct netdev_saved_flags
*sf
;
1134 struct dp_netdev_port
*port
;
1135 enum netdev_flags flags
;
1136 struct netdev
*netdev
;
1137 int n_open_rxqs
= 0;
1142 /* Open and validate network device. */
1143 error
= netdev_open(devname
, open_type
, &netdev
);
1147 /* XXX reject non-Ethernet devices */
1149 netdev_get_flags(netdev
, &flags
);
1150 if (flags
& NETDEV_LOOPBACK
) {
1151 VLOG_ERR("%s: cannot add a loopback device", devname
);
1156 if (netdev_is_pmd(netdev
)) {
1157 int n_cores
= ovs_numa_get_n_cores();
1159 if (n_cores
== OVS_CORE_UNSPEC
) {
1160 VLOG_ERR("%s, cannot get cpu core info", devname
);
1164 /* There can only be ovs_numa_get_n_cores() pmd threads,
1165 * so creates a txq for each, and one extra for the non
1167 error
= netdev_set_tx_multiq(netdev
, n_cores
+ 1);
1168 if (error
&& (error
!= EOPNOTSUPP
)) {
1169 VLOG_ERR("%s, cannot set multiq", devname
);
1174 if (netdev_is_reconf_required(netdev
)) {
1175 error
= netdev_reconfigure(netdev
);
1181 port
= xzalloc(sizeof *port
);
1182 port
->port_no
= port_no
;
1183 port
->netdev
= netdev
;
1184 port
->n_rxq
= netdev_n_rxq(netdev
);
1185 port
->rxq
= xcalloc(port
->n_rxq
, sizeof *port
->rxq
);
1186 port
->type
= xstrdup(type
);
1188 for (i
= 0; i
< port
->n_rxq
; i
++) {
1189 error
= netdev_rxq_open(netdev
, &port
->rxq
[i
], i
);
1191 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
1192 devname
, ovs_strerror(errno
));
1198 error
= netdev_turn_flags_on(netdev
, NETDEV_PROMISC
, &sf
);
1209 for (i
= 0; i
< n_open_rxqs
; i
++) {
1210 netdev_rxq_close(port
->rxq
[i
]);
1217 netdev_close(netdev
);
1222 do_add_port(struct dp_netdev
*dp
, const char *devname
, const char *type
,
1224 OVS_REQUIRES(dp
->port_mutex
)
1226 struct dp_netdev_port
*port
;
1229 /* Reject devices already in 'dp'. */
1230 if (!get_port_by_name(dp
, devname
, &port
)) {
1234 error
= port_create(devname
, dpif_netdev_port_open_type(dp
->class, type
),
1235 type
, port_no
, &port
);
1240 if (netdev_is_pmd(port
->netdev
)) {
1241 int numa_id
= netdev_get_numa_id(port
->netdev
);
1243 ovs_assert(ovs_numa_numa_id_is_valid(numa_id
));
1244 dp_netdev_set_pmds_on_numa(dp
, numa_id
);
1247 dp_netdev_add_port_to_pmds(dp
, port
);
1249 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
1250 seq_change(dp
->port_seq
);
1256 dpif_netdev_port_add(struct dpif
*dpif
, struct netdev
*netdev
,
1257 odp_port_t
*port_nop
)
1259 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1260 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1261 const char *dpif_port
;
1265 ovs_mutex_lock(&dp
->port_mutex
);
1266 dpif_port
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1267 if (*port_nop
!= ODPP_NONE
) {
1268 port_no
= *port_nop
;
1269 error
= dp_netdev_lookup_port(dp
, *port_nop
) ? EBUSY
: 0;
1271 port_no
= choose_port(dp
, dpif_port
);
1272 error
= port_no
== ODPP_NONE
? EFBIG
: 0;
1275 *port_nop
= port_no
;
1276 error
= do_add_port(dp
, dpif_port
, netdev_get_type(netdev
), port_no
);
1278 ovs_mutex_unlock(&dp
->port_mutex
);
1284 dpif_netdev_port_del(struct dpif
*dpif
, odp_port_t port_no
)
1286 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1289 ovs_mutex_lock(&dp
->port_mutex
);
1290 if (port_no
== ODPP_LOCAL
) {
1293 struct dp_netdev_port
*port
;
1295 error
= get_port_by_number(dp
, port_no
, &port
);
1297 do_del_port(dp
, port
);
1300 ovs_mutex_unlock(&dp
->port_mutex
);
1306 is_valid_port_number(odp_port_t port_no
)
1308 return port_no
!= ODPP_NONE
;
1311 static struct dp_netdev_port
*
1312 dp_netdev_lookup_port(const struct dp_netdev
*dp
, odp_port_t port_no
)
1313 OVS_REQUIRES(dp
->port_mutex
)
1315 struct dp_netdev_port
*port
;
1317 HMAP_FOR_EACH_WITH_HASH (port
, node
, hash_port_no(port_no
), &dp
->ports
) {
1318 if (port
->port_no
== port_no
) {
1326 get_port_by_number(struct dp_netdev
*dp
,
1327 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1328 OVS_REQUIRES(dp
->port_mutex
)
1330 if (!is_valid_port_number(port_no
)) {
1334 *portp
= dp_netdev_lookup_port(dp
, port_no
);
1335 return *portp
? 0 : ENOENT
;
1340 port_destroy(struct dp_netdev_port
*port
)
1346 netdev_close(port
->netdev
);
1347 netdev_restore_flags(port
->sf
);
1349 for (unsigned i
= 0; i
< port
->n_rxq
; i
++) {
1350 netdev_rxq_close(port
->rxq
[i
]);
1359 get_port_by_name(struct dp_netdev
*dp
,
1360 const char *devname
, struct dp_netdev_port
**portp
)
1361 OVS_REQUIRES(dp
->port_mutex
)
1363 struct dp_netdev_port
*port
;
1365 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1366 if (!strcmp(netdev_get_name(port
->netdev
), devname
)) {
1375 get_n_pmd_threads(struct dp_netdev
*dp
)
1377 /* There is one non pmd thread in dp->poll_threads */
1378 return cmap_count(&dp
->poll_threads
) - 1;
1382 get_n_pmd_threads_on_numa(struct dp_netdev
*dp
, int numa_id
)
1384 struct dp_netdev_pmd_thread
*pmd
;
1387 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1388 if (pmd
->numa_id
== numa_id
) {
1396 /* Returns 'true' if there is a port with pmd netdev and the netdev
1397 * is on numa node 'numa_id'. */
1399 has_pmd_port_for_numa(struct dp_netdev
*dp
, int numa_id
)
1400 OVS_REQUIRES(dp
->port_mutex
)
1402 struct dp_netdev_port
*port
;
1404 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1405 if (netdev_is_pmd(port
->netdev
)
1406 && netdev_get_numa_id(port
->netdev
) == numa_id
) {
1416 do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*port
)
1417 OVS_REQUIRES(dp
->port_mutex
)
1419 hmap_remove(&dp
->ports
, &port
->node
);
1420 seq_change(dp
->port_seq
);
1422 dp_netdev_del_port_from_all_pmds(dp
, port
);
1424 if (netdev_is_pmd(port
->netdev
)) {
1425 int numa_id
= netdev_get_numa_id(port
->netdev
);
1427 /* PMD threads can not be on invalid numa node. */
1428 ovs_assert(ovs_numa_numa_id_is_valid(numa_id
));
1429 /* If there is no netdev on the numa node, deletes the pmd threads
1431 if (!has_pmd_port_for_numa(dp
, numa_id
)) {
1432 dp_netdev_del_pmds_on_numa(dp
, numa_id
);
1440 answer_port_query(const struct dp_netdev_port
*port
,
1441 struct dpif_port
*dpif_port
)
1443 dpif_port
->name
= xstrdup(netdev_get_name(port
->netdev
));
1444 dpif_port
->type
= xstrdup(port
->type
);
1445 dpif_port
->port_no
= port
->port_no
;
1449 dpif_netdev_port_query_by_number(const struct dpif
*dpif
, odp_port_t port_no
,
1450 struct dpif_port
*dpif_port
)
1452 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1453 struct dp_netdev_port
*port
;
1456 ovs_mutex_lock(&dp
->port_mutex
);
1457 error
= get_port_by_number(dp
, port_no
, &port
);
1458 if (!error
&& dpif_port
) {
1459 answer_port_query(port
, dpif_port
);
1461 ovs_mutex_unlock(&dp
->port_mutex
);
1467 dpif_netdev_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
1468 struct dpif_port
*dpif_port
)
1470 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1471 struct dp_netdev_port
*port
;
1474 ovs_mutex_lock(&dp
->port_mutex
);
1475 error
= get_port_by_name(dp
, devname
, &port
);
1476 if (!error
&& dpif_port
) {
1477 answer_port_query(port
, dpif_port
);
1479 ovs_mutex_unlock(&dp
->port_mutex
);
1485 dp_netdev_flow_free(struct dp_netdev_flow
*flow
)
1487 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow
));
1491 static void dp_netdev_flow_unref(struct dp_netdev_flow
*flow
)
1493 if (ovs_refcount_unref_relaxed(&flow
->ref_cnt
) == 1) {
1494 ovsrcu_postpone(dp_netdev_flow_free
, flow
);
1499 dp_netdev_flow_hash(const ovs_u128
*ufid
)
1501 return ufid
->u32
[0];
1505 dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread
*pmd
,
1506 struct dp_netdev_flow
*flow
)
1507 OVS_REQUIRES(pmd
->flow_mutex
)
1509 struct cmap_node
*node
= CONST_CAST(struct cmap_node
*, &flow
->node
);
1511 dpcls_remove(&pmd
->cls
, &flow
->cr
);
1512 cmap_remove(&pmd
->flow_table
, node
, dp_netdev_flow_hash(&flow
->ufid
));
1515 dp_netdev_flow_unref(flow
);
1519 dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
)
1521 struct dp_netdev_flow
*netdev_flow
;
1523 ovs_mutex_lock(&pmd
->flow_mutex
);
1524 CMAP_FOR_EACH (netdev_flow
, node
, &pmd
->flow_table
) {
1525 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
1527 ovs_mutex_unlock(&pmd
->flow_mutex
);
1531 dpif_netdev_flow_flush(struct dpif
*dpif
)
1533 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1534 struct dp_netdev_pmd_thread
*pmd
;
1536 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1537 dp_netdev_pmd_flow_flush(pmd
);
1543 struct dp_netdev_port_state
{
1544 struct hmap_position position
;
1549 dpif_netdev_port_dump_start(const struct dpif
*dpif OVS_UNUSED
, void **statep
)
1551 *statep
= xzalloc(sizeof(struct dp_netdev_port_state
));
1556 dpif_netdev_port_dump_next(const struct dpif
*dpif
, void *state_
,
1557 struct dpif_port
*dpif_port
)
1559 struct dp_netdev_port_state
*state
= state_
;
1560 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1561 struct hmap_node
*node
;
1564 ovs_mutex_lock(&dp
->port_mutex
);
1565 node
= hmap_at_position(&dp
->ports
, &state
->position
);
1567 struct dp_netdev_port
*port
;
1569 port
= CONTAINER_OF(node
, struct dp_netdev_port
, node
);
1572 state
->name
= xstrdup(netdev_get_name(port
->netdev
));
1573 dpif_port
->name
= state
->name
;
1574 dpif_port
->type
= port
->type
;
1575 dpif_port
->port_no
= port
->port_no
;
1581 ovs_mutex_unlock(&dp
->port_mutex
);
1587 dpif_netdev_port_dump_done(const struct dpif
*dpif OVS_UNUSED
, void *state_
)
1589 struct dp_netdev_port_state
*state
= state_
;
1596 dpif_netdev_port_poll(const struct dpif
*dpif_
, char **devnamep OVS_UNUSED
)
1598 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
1599 uint64_t new_port_seq
;
1602 new_port_seq
= seq_read(dpif
->dp
->port_seq
);
1603 if (dpif
->last_port_seq
!= new_port_seq
) {
1604 dpif
->last_port_seq
= new_port_seq
;
1614 dpif_netdev_port_poll_wait(const struct dpif
*dpif_
)
1616 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
1618 seq_wait(dpif
->dp
->port_seq
, dpif
->last_port_seq
);
1621 static struct dp_netdev_flow
*
1622 dp_netdev_flow_cast(const struct dpcls_rule
*cr
)
1624 return cr
? CONTAINER_OF(cr
, struct dp_netdev_flow
, cr
) : NULL
;
1627 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*flow
)
1629 return ovs_refcount_try_ref_rcu(&flow
->ref_cnt
);
1632 /* netdev_flow_key utilities.
1634 * netdev_flow_key is basically a miniflow. We use these functions
1635 * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow
1636 * functions (miniflow_clone_inline, miniflow_equal, ...), because:
1638 * - Since we are dealing exclusively with miniflows created by
1639 * miniflow_extract(), if the map is different the miniflow is different.
1640 * Therefore we can be faster by comparing the map and the miniflow in a
1642 * - These functions can be inlined by the compiler. */
1644 /* Given the number of bits set in miniflow's maps, returns the size of the
1645 * 'netdev_flow_key.mf' */
1646 static inline size_t
1647 netdev_flow_key_size(size_t flow_u64s
)
1649 return sizeof(struct miniflow
) + MINIFLOW_VALUES_SIZE(flow_u64s
);
1653 netdev_flow_key_equal(const struct netdev_flow_key
*a
,
1654 const struct netdev_flow_key
*b
)
1656 /* 'b->len' may be not set yet. */
1657 return a
->hash
== b
->hash
&& !memcmp(&a
->mf
, &b
->mf
, a
->len
);
1660 /* Used to compare 'netdev_flow_key' in the exact match cache to a miniflow.
1661 * The maps are compared bitwise, so both 'key->mf' and 'mf' must have been
1662 * generated by miniflow_extract. */
1664 netdev_flow_key_equal_mf(const struct netdev_flow_key
*key
,
1665 const struct miniflow
*mf
)
1667 return !memcmp(&key
->mf
, mf
, key
->len
);
1671 netdev_flow_key_clone(struct netdev_flow_key
*dst
,
1672 const struct netdev_flow_key
*src
)
1675 offsetof(struct netdev_flow_key
, mf
) + src
->len
);
1680 netdev_flow_key_from_flow(struct netdev_flow_key
*dst
,
1681 const struct flow
*src
)
1683 struct dp_packet packet
;
1684 uint64_t buf_stub
[512 / 8];
1686 dp_packet_use_stub(&packet
, buf_stub
, sizeof buf_stub
);
1687 pkt_metadata_from_flow(&packet
.md
, src
);
1688 flow_compose(&packet
, src
);
1689 miniflow_extract(&packet
, &dst
->mf
);
1690 dp_packet_uninit(&packet
);
1692 dst
->len
= netdev_flow_key_size(miniflow_n_values(&dst
->mf
));
1693 dst
->hash
= 0; /* Not computed yet. */
1696 /* Initialize a netdev_flow_key 'mask' from 'match'. */
1698 netdev_flow_mask_init(struct netdev_flow_key
*mask
,
1699 const struct match
*match
)
1701 uint64_t *dst
= miniflow_values(&mask
->mf
);
1702 struct flowmap fmap
;
1706 /* Only check masks that make sense for the flow. */
1707 flow_wc_map(&match
->flow
, &fmap
);
1708 flowmap_init(&mask
->mf
.map
);
1710 FLOWMAP_FOR_EACH_INDEX(idx
, fmap
) {
1711 uint64_t mask_u64
= flow_u64_value(&match
->wc
.masks
, idx
);
1714 flowmap_set(&mask
->mf
.map
, idx
, 1);
1716 hash
= hash_add64(hash
, mask_u64
);
1722 FLOWMAP_FOR_EACH_MAP (map
, mask
->mf
.map
) {
1723 hash
= hash_add64(hash
, map
);
1726 size_t n
= dst
- miniflow_get_values(&mask
->mf
);
1728 mask
->hash
= hash_finish(hash
, n
* 8);
1729 mask
->len
= netdev_flow_key_size(n
);
1732 /* Initializes 'dst' as a copy of 'flow' masked with 'mask'. */
1734 netdev_flow_key_init_masked(struct netdev_flow_key
*dst
,
1735 const struct flow
*flow
,
1736 const struct netdev_flow_key
*mask
)
1738 uint64_t *dst_u64
= miniflow_values(&dst
->mf
);
1739 const uint64_t *mask_u64
= miniflow_get_values(&mask
->mf
);
1743 dst
->len
= mask
->len
;
1744 dst
->mf
= mask
->mf
; /* Copy maps. */
1746 FLOW_FOR_EACH_IN_MAPS(value
, flow
, mask
->mf
.map
) {
1747 *dst_u64
= value
& *mask_u64
++;
1748 hash
= hash_add64(hash
, *dst_u64
++);
1750 dst
->hash
= hash_finish(hash
,
1751 (dst_u64
- miniflow_get_values(&dst
->mf
)) * 8);
1754 /* Iterate through netdev_flow_key TNL u64 values specified by 'FLOWMAP'. */
1755 #define NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(VALUE, KEY, FLOWMAP) \
1756 MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, &(KEY)->mf, FLOWMAP)
1758 /* Returns a hash value for the bits of 'key' where there are 1-bits in
1760 static inline uint32_t
1761 netdev_flow_key_hash_in_mask(const struct netdev_flow_key
*key
,
1762 const struct netdev_flow_key
*mask
)
1764 const uint64_t *p
= miniflow_get_values(&mask
->mf
);
1768 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, key
, mask
->mf
.map
) {
1769 hash
= hash_add64(hash
, value
& *p
++);
1772 return hash_finish(hash
, (p
- miniflow_get_values(&mask
->mf
)) * 8);
1776 emc_entry_alive(struct emc_entry
*ce
)
1778 return ce
->flow
&& !ce
->flow
->dead
;
1782 emc_clear_entry(struct emc_entry
*ce
)
1785 dp_netdev_flow_unref(ce
->flow
);
1791 emc_change_entry(struct emc_entry
*ce
, struct dp_netdev_flow
*flow
,
1792 const struct netdev_flow_key
*key
)
1794 if (ce
->flow
!= flow
) {
1796 dp_netdev_flow_unref(ce
->flow
);
1799 if (dp_netdev_flow_ref(flow
)) {
1806 netdev_flow_key_clone(&ce
->key
, key
);
1811 emc_insert(struct emc_cache
*cache
, const struct netdev_flow_key
*key
,
1812 struct dp_netdev_flow
*flow
)
1814 struct emc_entry
*to_be_replaced
= NULL
;
1815 struct emc_entry
*current_entry
;
1817 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
1818 if (netdev_flow_key_equal(¤t_entry
->key
, key
)) {
1819 /* We found the entry with the 'mf' miniflow */
1820 emc_change_entry(current_entry
, flow
, NULL
);
1824 /* Replacement policy: put the flow in an empty (not alive) entry, or
1825 * in the first entry where it can be */
1827 || (emc_entry_alive(to_be_replaced
)
1828 && !emc_entry_alive(current_entry
))
1829 || current_entry
->key
.hash
< to_be_replaced
->key
.hash
) {
1830 to_be_replaced
= current_entry
;
1833 /* We didn't find the miniflow in the cache.
1834 * The 'to_be_replaced' entry is where the new flow will be stored */
1836 emc_change_entry(to_be_replaced
, flow
, key
);
1839 static inline struct dp_netdev_flow
*
1840 emc_lookup(struct emc_cache
*cache
, const struct netdev_flow_key
*key
)
1842 struct emc_entry
*current_entry
;
1844 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
1845 if (current_entry
->key
.hash
== key
->hash
1846 && emc_entry_alive(current_entry
)
1847 && netdev_flow_key_equal_mf(¤t_entry
->key
, &key
->mf
)) {
1849 /* We found the entry with the 'key->mf' miniflow */
1850 return current_entry
->flow
;
1857 static struct dp_netdev_flow
*
1858 dp_netdev_pmd_lookup_flow(const struct dp_netdev_pmd_thread
*pmd
,
1859 const struct netdev_flow_key
*key
)
1861 struct dp_netdev_flow
*netdev_flow
;
1862 struct dpcls_rule
*rule
;
1864 dpcls_lookup(&pmd
->cls
, key
, &rule
, 1);
1865 netdev_flow
= dp_netdev_flow_cast(rule
);
1870 static struct dp_netdev_flow
*
1871 dp_netdev_pmd_find_flow(const struct dp_netdev_pmd_thread
*pmd
,
1872 const ovs_u128
*ufidp
, const struct nlattr
*key
,
1875 struct dp_netdev_flow
*netdev_flow
;
1879 /* If a UFID is not provided, determine one based on the key. */
1880 if (!ufidp
&& key
&& key_len
1881 && !dpif_netdev_flow_from_nlattrs(key
, key_len
, &flow
)) {
1882 dpif_flow_hash(pmd
->dp
->dpif
, &flow
, sizeof flow
, &ufid
);
1887 CMAP_FOR_EACH_WITH_HASH (netdev_flow
, node
, dp_netdev_flow_hash(ufidp
),
1889 if (ovs_u128_equals(netdev_flow
->ufid
, *ufidp
)) {
1899 get_dpif_flow_stats(const struct dp_netdev_flow
*netdev_flow_
,
1900 struct dpif_flow_stats
*stats
)
1902 struct dp_netdev_flow
*netdev_flow
;
1903 unsigned long long n
;
1907 netdev_flow
= CONST_CAST(struct dp_netdev_flow
*, netdev_flow_
);
1909 atomic_read_relaxed(&netdev_flow
->stats
.packet_count
, &n
);
1910 stats
->n_packets
= n
;
1911 atomic_read_relaxed(&netdev_flow
->stats
.byte_count
, &n
);
1913 atomic_read_relaxed(&netdev_flow
->stats
.used
, &used
);
1915 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
1916 stats
->tcp_flags
= flags
;
1919 /* Converts to the dpif_flow format, using 'key_buf' and 'mask_buf' for
1920 * storing the netlink-formatted key/mask. 'key_buf' may be the same as
1921 * 'mask_buf'. Actions will be returned without copying, by relying on RCU to
1924 dp_netdev_flow_to_dpif_flow(const struct dp_netdev_flow
*netdev_flow
,
1925 struct ofpbuf
*key_buf
, struct ofpbuf
*mask_buf
,
1926 struct dpif_flow
*flow
, bool terse
)
1929 memset(flow
, 0, sizeof *flow
);
1931 struct flow_wildcards wc
;
1932 struct dp_netdev_actions
*actions
;
1934 struct odp_flow_key_parms odp_parms
= {
1935 .flow
= &netdev_flow
->flow
,
1937 .support
= dp_netdev_support
,
1940 miniflow_expand(&netdev_flow
->cr
.mask
->mf
, &wc
.masks
);
1943 offset
= key_buf
->size
;
1944 flow
->key
= ofpbuf_tail(key_buf
);
1945 odp_parms
.odp_in_port
= netdev_flow
->flow
.in_port
.odp_port
;
1946 odp_flow_key_from_flow(&odp_parms
, key_buf
);
1947 flow
->key_len
= key_buf
->size
- offset
;
1950 offset
= mask_buf
->size
;
1951 flow
->mask
= ofpbuf_tail(mask_buf
);
1952 odp_parms
.odp_in_port
= wc
.masks
.in_port
.odp_port
;
1953 odp_parms
.key_buf
= key_buf
;
1954 odp_flow_key_from_mask(&odp_parms
, mask_buf
);
1955 flow
->mask_len
= mask_buf
->size
- offset
;
1958 actions
= dp_netdev_flow_get_actions(netdev_flow
);
1959 flow
->actions
= actions
->actions
;
1960 flow
->actions_len
= actions
->size
;
1963 flow
->ufid
= netdev_flow
->ufid
;
1964 flow
->ufid_present
= true;
1965 flow
->pmd_id
= netdev_flow
->pmd_id
;
1966 get_dpif_flow_stats(netdev_flow
, &flow
->stats
);
1970 dpif_netdev_mask_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
1971 const struct nlattr
*mask_key
,
1972 uint32_t mask_key_len
, const struct flow
*flow
,
1973 struct flow_wildcards
*wc
)
1975 enum odp_key_fitness fitness
;
1977 fitness
= odp_flow_key_to_mask_udpif(mask_key
, mask_key_len
, key
,
1980 /* This should not happen: it indicates that
1981 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
1982 * disagree on the acceptable form of a mask. Log the problem
1983 * as an error, with enough details to enable debugging. */
1984 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1986 if (!VLOG_DROP_ERR(&rl
)) {
1990 odp_flow_format(key
, key_len
, mask_key
, mask_key_len
, NULL
, &s
,
1992 VLOG_ERR("internal error parsing flow mask %s (%s)",
1993 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
2004 dpif_netdev_flow_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
2009 if (odp_flow_key_to_flow_udpif(key
, key_len
, flow
)) {
2010 /* This should not happen: it indicates that odp_flow_key_from_flow()
2011 * and odp_flow_key_to_flow() disagree on the acceptable form of a
2012 * flow. Log the problem as an error, with enough details to enable
2014 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2016 if (!VLOG_DROP_ERR(&rl
)) {
2020 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
2021 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
2028 in_port
= flow
->in_port
.odp_port
;
2029 if (!is_valid_port_number(in_port
) && in_port
!= ODPP_NONE
) {
2033 /* Userspace datapath doesn't support conntrack. */
2034 if (flow
->ct_state
|| flow
->ct_zone
|| flow
->ct_mark
2035 || !ovs_u128_is_zero(flow
->ct_label
)) {
2043 dpif_netdev_flow_get(const struct dpif
*dpif
, const struct dpif_flow_get
*get
)
2045 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2046 struct dp_netdev_flow
*netdev_flow
;
2047 struct dp_netdev_pmd_thread
*pmd
;
2048 struct hmapx to_find
= HMAPX_INITIALIZER(&to_find
);
2049 struct hmapx_node
*node
;
2052 if (get
->pmd_id
== PMD_ID_NULL
) {
2053 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2054 if (dp_netdev_pmd_try_ref(pmd
) && !hmapx_add(&to_find
, pmd
)) {
2055 dp_netdev_pmd_unref(pmd
);
2059 pmd
= dp_netdev_get_pmd(dp
, get
->pmd_id
);
2063 hmapx_add(&to_find
, pmd
);
2066 if (!hmapx_count(&to_find
)) {
2070 HMAPX_FOR_EACH (node
, &to_find
) {
2071 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
2072 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, get
->ufid
, get
->key
,
2075 dp_netdev_flow_to_dpif_flow(netdev_flow
, get
->buffer
, get
->buffer
,
2084 HMAPX_FOR_EACH (node
, &to_find
) {
2085 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
2086 dp_netdev_pmd_unref(pmd
);
2089 hmapx_destroy(&to_find
);
2093 static struct dp_netdev_flow
*
2094 dp_netdev_flow_add(struct dp_netdev_pmd_thread
*pmd
,
2095 struct match
*match
, const ovs_u128
*ufid
,
2096 const struct nlattr
*actions
, size_t actions_len
)
2097 OVS_REQUIRES(pmd
->flow_mutex
)
2099 struct dp_netdev_flow
*flow
;
2100 struct netdev_flow_key mask
;
2102 netdev_flow_mask_init(&mask
, match
);
2103 /* Make sure wc does not have metadata. */
2104 ovs_assert(!FLOWMAP_HAS_FIELD(&mask
.mf
.map
, metadata
)
2105 && !FLOWMAP_HAS_FIELD(&mask
.mf
.map
, regs
));
2107 /* Do not allocate extra space. */
2108 flow
= xmalloc(sizeof *flow
- sizeof flow
->cr
.flow
.mf
+ mask
.len
);
2109 memset(&flow
->stats
, 0, sizeof flow
->stats
);
2112 *CONST_CAST(unsigned *, &flow
->pmd_id
) = pmd
->core_id
;
2113 *CONST_CAST(struct flow
*, &flow
->flow
) = match
->flow
;
2114 *CONST_CAST(ovs_u128
*, &flow
->ufid
) = *ufid
;
2115 ovs_refcount_init(&flow
->ref_cnt
);
2116 ovsrcu_set(&flow
->actions
, dp_netdev_actions_create(actions
, actions_len
));
2118 netdev_flow_key_init_masked(&flow
->cr
.flow
, &match
->flow
, &mask
);
2119 dpcls_insert(&pmd
->cls
, &flow
->cr
, &mask
);
2121 cmap_insert(&pmd
->flow_table
, CONST_CAST(struct cmap_node
*, &flow
->node
),
2122 dp_netdev_flow_hash(&flow
->ufid
));
2124 if (OVS_UNLIKELY(VLOG_IS_DBG_ENABLED())) {
2126 struct ds ds
= DS_EMPTY_INITIALIZER
;
2128 match
.tun_md
.valid
= false;
2129 match
.flow
= flow
->flow
;
2130 miniflow_expand(&flow
->cr
.mask
->mf
, &match
.wc
.masks
);
2132 ds_put_cstr(&ds
, "flow_add: ");
2133 odp_format_ufid(ufid
, &ds
);
2134 ds_put_cstr(&ds
, " ");
2135 match_format(&match
, &ds
, OFP_DEFAULT_PRIORITY
);
2136 ds_put_cstr(&ds
, ", actions:");
2137 format_odp_actions(&ds
, actions
, actions_len
);
2139 VLOG_DBG_RL(&upcall_rl
, "%s", ds_cstr(&ds
));
2148 dpif_netdev_flow_put(struct dpif
*dpif
, const struct dpif_flow_put
*put
)
2150 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2151 struct dp_netdev_flow
*netdev_flow
;
2152 struct netdev_flow_key key
;
2153 struct dp_netdev_pmd_thread
*pmd
;
2156 unsigned pmd_id
= put
->pmd_id
== PMD_ID_NULL
2157 ? NON_PMD_CORE_ID
: put
->pmd_id
;
2160 error
= dpif_netdev_flow_from_nlattrs(put
->key
, put
->key_len
, &match
.flow
);
2164 error
= dpif_netdev_mask_from_nlattrs(put
->key
, put
->key_len
,
2165 put
->mask
, put
->mask_len
,
2166 &match
.flow
, &match
.wc
);
2171 pmd
= dp_netdev_get_pmd(dp
, pmd_id
);
2176 /* Must produce a netdev_flow_key for lookup.
2177 * This interface is no longer performance critical, since it is not used
2178 * for upcall processing any more. */
2179 netdev_flow_key_from_flow(&key
, &match
.flow
);
2184 dpif_flow_hash(dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
2187 ovs_mutex_lock(&pmd
->flow_mutex
);
2188 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, &key
);
2190 if (put
->flags
& DPIF_FP_CREATE
) {
2191 if (cmap_count(&pmd
->flow_table
) < MAX_FLOWS
) {
2193 memset(put
->stats
, 0, sizeof *put
->stats
);
2195 dp_netdev_flow_add(pmd
, &match
, &ufid
, put
->actions
,
2205 if (put
->flags
& DPIF_FP_MODIFY
2206 && flow_equal(&match
.flow
, &netdev_flow
->flow
)) {
2207 struct dp_netdev_actions
*new_actions
;
2208 struct dp_netdev_actions
*old_actions
;
2210 new_actions
= dp_netdev_actions_create(put
->actions
,
2213 old_actions
= dp_netdev_flow_get_actions(netdev_flow
);
2214 ovsrcu_set(&netdev_flow
->actions
, new_actions
);
2217 get_dpif_flow_stats(netdev_flow
, put
->stats
);
2219 if (put
->flags
& DPIF_FP_ZERO_STATS
) {
2220 /* XXX: The userspace datapath uses thread local statistics
2221 * (for flows), which should be updated only by the owning
2222 * thread. Since we cannot write on stats memory here,
2223 * we choose not to support this flag. Please note:
2224 * - This feature is currently used only by dpctl commands with
2226 * - Should the need arise, this operation can be implemented
2227 * by keeping a base value (to be update here) for each
2228 * counter, and subtracting it before outputting the stats */
2232 ovsrcu_postpone(dp_netdev_actions_free
, old_actions
);
2233 } else if (put
->flags
& DPIF_FP_CREATE
) {
2236 /* Overlapping flow. */
2240 ovs_mutex_unlock(&pmd
->flow_mutex
);
2241 dp_netdev_pmd_unref(pmd
);
2247 dpif_netdev_flow_del(struct dpif
*dpif
, const struct dpif_flow_del
*del
)
2249 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2250 struct dp_netdev_flow
*netdev_flow
;
2251 struct dp_netdev_pmd_thread
*pmd
;
2252 unsigned pmd_id
= del
->pmd_id
== PMD_ID_NULL
2253 ? NON_PMD_CORE_ID
: del
->pmd_id
;
2256 pmd
= dp_netdev_get_pmd(dp
, pmd_id
);
2261 ovs_mutex_lock(&pmd
->flow_mutex
);
2262 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, del
->ufid
, del
->key
,
2266 get_dpif_flow_stats(netdev_flow
, del
->stats
);
2268 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
2272 ovs_mutex_unlock(&pmd
->flow_mutex
);
2273 dp_netdev_pmd_unref(pmd
);
2278 struct dpif_netdev_flow_dump
{
2279 struct dpif_flow_dump up
;
2280 struct cmap_position poll_thread_pos
;
2281 struct cmap_position flow_pos
;
2282 struct dp_netdev_pmd_thread
*cur_pmd
;
2284 struct ovs_mutex mutex
;
2287 static struct dpif_netdev_flow_dump
*
2288 dpif_netdev_flow_dump_cast(struct dpif_flow_dump
*dump
)
2290 return CONTAINER_OF(dump
, struct dpif_netdev_flow_dump
, up
);
2293 static struct dpif_flow_dump
*
2294 dpif_netdev_flow_dump_create(const struct dpif
*dpif_
, bool terse
)
2296 struct dpif_netdev_flow_dump
*dump
;
2298 dump
= xzalloc(sizeof *dump
);
2299 dpif_flow_dump_init(&dump
->up
, dpif_
);
2300 dump
->up
.terse
= terse
;
2301 ovs_mutex_init(&dump
->mutex
);
2307 dpif_netdev_flow_dump_destroy(struct dpif_flow_dump
*dump_
)
2309 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
2311 ovs_mutex_destroy(&dump
->mutex
);
2316 struct dpif_netdev_flow_dump_thread
{
2317 struct dpif_flow_dump_thread up
;
2318 struct dpif_netdev_flow_dump
*dump
;
2319 struct odputil_keybuf keybuf
[FLOW_DUMP_MAX_BATCH
];
2320 struct odputil_keybuf maskbuf
[FLOW_DUMP_MAX_BATCH
];
2323 static struct dpif_netdev_flow_dump_thread
*
2324 dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread
*thread
)
2326 return CONTAINER_OF(thread
, struct dpif_netdev_flow_dump_thread
, up
);
2329 static struct dpif_flow_dump_thread
*
2330 dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump
*dump_
)
2332 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
2333 struct dpif_netdev_flow_dump_thread
*thread
;
2335 thread
= xmalloc(sizeof *thread
);
2336 dpif_flow_dump_thread_init(&thread
->up
, &dump
->up
);
2337 thread
->dump
= dump
;
2342 dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread
*thread_
)
2344 struct dpif_netdev_flow_dump_thread
*thread
2345 = dpif_netdev_flow_dump_thread_cast(thread_
);
2351 dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread
*thread_
,
2352 struct dpif_flow
*flows
, int max_flows
)
2354 struct dpif_netdev_flow_dump_thread
*thread
2355 = dpif_netdev_flow_dump_thread_cast(thread_
);
2356 struct dpif_netdev_flow_dump
*dump
= thread
->dump
;
2357 struct dp_netdev_flow
*netdev_flows
[FLOW_DUMP_MAX_BATCH
];
2361 ovs_mutex_lock(&dump
->mutex
);
2362 if (!dump
->status
) {
2363 struct dpif_netdev
*dpif
= dpif_netdev_cast(thread
->up
.dpif
);
2364 struct dp_netdev
*dp
= get_dp_netdev(&dpif
->dpif
);
2365 struct dp_netdev_pmd_thread
*pmd
= dump
->cur_pmd
;
2366 int flow_limit
= MIN(max_flows
, FLOW_DUMP_MAX_BATCH
);
2368 /* First call to dump_next(), extracts the first pmd thread.
2369 * If there is no pmd thread, returns immediately. */
2371 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
2373 ovs_mutex_unlock(&dump
->mutex
);
2380 for (n_flows
= 0; n_flows
< flow_limit
; n_flows
++) {
2381 struct cmap_node
*node
;
2383 node
= cmap_next_position(&pmd
->flow_table
, &dump
->flow_pos
);
2387 netdev_flows
[n_flows
] = CONTAINER_OF(node
,
2388 struct dp_netdev_flow
,
2391 /* When finishing dumping the current pmd thread, moves to
2393 if (n_flows
< flow_limit
) {
2394 memset(&dump
->flow_pos
, 0, sizeof dump
->flow_pos
);
2395 dp_netdev_pmd_unref(pmd
);
2396 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
2402 /* Keeps the reference to next caller. */
2403 dump
->cur_pmd
= pmd
;
2405 /* If the current dump is empty, do not exit the loop, since the
2406 * remaining pmds could have flows to be dumped. Just dumps again
2407 * on the new 'pmd'. */
2410 ovs_mutex_unlock(&dump
->mutex
);
2412 for (i
= 0; i
< n_flows
; i
++) {
2413 struct odputil_keybuf
*maskbuf
= &thread
->maskbuf
[i
];
2414 struct odputil_keybuf
*keybuf
= &thread
->keybuf
[i
];
2415 struct dp_netdev_flow
*netdev_flow
= netdev_flows
[i
];
2416 struct dpif_flow
*f
= &flows
[i
];
2417 struct ofpbuf key
, mask
;
2419 ofpbuf_use_stack(&key
, keybuf
, sizeof *keybuf
);
2420 ofpbuf_use_stack(&mask
, maskbuf
, sizeof *maskbuf
);
2421 dp_netdev_flow_to_dpif_flow(netdev_flow
, &key
, &mask
, f
,
2429 dpif_netdev_execute(struct dpif
*dpif
, struct dpif_execute
*execute
)
2430 OVS_NO_THREAD_SAFETY_ANALYSIS
2432 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2433 struct dp_netdev_pmd_thread
*pmd
;
2434 struct dp_packet_batch pp
;
2436 if (dp_packet_size(execute
->packet
) < ETH_HEADER_LEN
||
2437 dp_packet_size(execute
->packet
) > UINT16_MAX
) {
2441 /* Tries finding the 'pmd'. If NULL is returned, that means
2442 * the current thread is a non-pmd thread and should use
2443 * dp_netdev_get_pmd(dp, NON_PMD_CORE_ID). */
2444 pmd
= ovsthread_getspecific(dp
->per_pmd_key
);
2446 pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
2449 /* If the current thread is non-pmd thread, acquires
2450 * the 'non_pmd_mutex'. */
2451 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
2452 ovs_mutex_lock(&dp
->non_pmd_mutex
);
2455 /* The action processing expects the RSS hash to be valid, because
2456 * it's always initialized at the beginning of datapath processing.
2457 * In this case, though, 'execute->packet' may not have gone through
2458 * the datapath at all, it may have been generated by the upper layer
2459 * (OpenFlow packet-out, BFD frame, ...). */
2460 if (!dp_packet_rss_valid(execute
->packet
)) {
2461 dp_packet_set_rss_hash(execute
->packet
,
2462 flow_hash_5tuple(execute
->flow
, 0));
2465 packet_batch_init_packet(&pp
, execute
->packet
);
2466 dp_netdev_execute_actions(pmd
, &pp
, false, execute
->actions
,
2467 execute
->actions_len
);
2469 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
2470 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
2471 dp_netdev_pmd_unref(pmd
);
2478 dpif_netdev_operate(struct dpif
*dpif
, struct dpif_op
**ops
, size_t n_ops
)
2482 for (i
= 0; i
< n_ops
; i
++) {
2483 struct dpif_op
*op
= ops
[i
];
2486 case DPIF_OP_FLOW_PUT
:
2487 op
->error
= dpif_netdev_flow_put(dpif
, &op
->u
.flow_put
);
2490 case DPIF_OP_FLOW_DEL
:
2491 op
->error
= dpif_netdev_flow_del(dpif
, &op
->u
.flow_del
);
2494 case DPIF_OP_EXECUTE
:
2495 op
->error
= dpif_netdev_execute(dpif
, &op
->u
.execute
);
2498 case DPIF_OP_FLOW_GET
:
2499 op
->error
= dpif_netdev_flow_get(dpif
, &op
->u
.flow_get
);
2506 cmask_equals(const char *a
, const char *b
)
2509 return !strcmp(a
, b
);
2512 return a
== NULL
&& b
== NULL
;
2515 /* Changes the number or the affinity of pmd threads. The changes are actually
2516 * applied in dpif_netdev_run(). */
2518 dpif_netdev_pmd_set(struct dpif
*dpif
, const char *cmask
)
2520 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2522 if (!cmask_equals(dp
->requested_pmd_cmask
, cmask
)) {
2523 free(dp
->requested_pmd_cmask
);
2524 dp
->requested_pmd_cmask
= cmask
? xstrdup(cmask
) : NULL
;
2531 dpif_netdev_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
2532 uint32_t queue_id
, uint32_t *priority
)
2534 *priority
= queue_id
;
2539 /* Creates and returns a new 'struct dp_netdev_actions', whose actions are
2540 * a copy of the 'ofpacts_len' bytes of 'ofpacts'. */
2541 struct dp_netdev_actions
*
2542 dp_netdev_actions_create(const struct nlattr
*actions
, size_t size
)
2544 struct dp_netdev_actions
*netdev_actions
;
2546 netdev_actions
= xmalloc(sizeof *netdev_actions
+ size
);
2547 memcpy(netdev_actions
->actions
, actions
, size
);
2548 netdev_actions
->size
= size
;
2550 return netdev_actions
;
2553 struct dp_netdev_actions
*
2554 dp_netdev_flow_get_actions(const struct dp_netdev_flow
*flow
)
2556 return ovsrcu_get(struct dp_netdev_actions
*, &flow
->actions
);
2560 dp_netdev_actions_free(struct dp_netdev_actions
*actions
)
2565 static inline unsigned long long
2566 cycles_counter(void)
2569 return rte_get_tsc_cycles();
2575 /* Fake mutex to make sure that the calls to cycles_count_* are balanced */
2576 extern struct ovs_mutex cycles_counter_fake_mutex
;
2578 /* Start counting cycles. Must be followed by 'cycles_count_end()' */
2580 cycles_count_start(struct dp_netdev_pmd_thread
*pmd
)
2581 OVS_ACQUIRES(&cycles_counter_fake_mutex
)
2582 OVS_NO_THREAD_SAFETY_ANALYSIS
2584 pmd
->last_cycles
= cycles_counter();
2587 /* Stop counting cycles and add them to the counter 'type' */
2589 cycles_count_end(struct dp_netdev_pmd_thread
*pmd
,
2590 enum pmd_cycles_counter_type type
)
2591 OVS_RELEASES(&cycles_counter_fake_mutex
)
2592 OVS_NO_THREAD_SAFETY_ANALYSIS
2594 unsigned long long interval
= cycles_counter() - pmd
->last_cycles
;
2596 non_atomic_ullong_add(&pmd
->cycles
.n
[type
], interval
);
2600 dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread
*pmd
,
2601 struct dp_netdev_port
*port
,
2602 struct netdev_rxq
*rxq
)
2604 struct dp_packet_batch batch
;
2607 dp_packet_batch_init(&batch
);
2608 cycles_count_start(pmd
);
2609 error
= netdev_rxq_recv(rxq
, &batch
);
2610 cycles_count_end(pmd
, PMD_CYCLES_POLLING
);
2612 *recirc_depth_get() = 0;
2614 cycles_count_start(pmd
);
2615 dp_netdev_input(pmd
, &batch
, port
->port_no
);
2616 cycles_count_end(pmd
, PMD_CYCLES_PROCESSING
);
2617 } else if (error
!= EAGAIN
&& error
!= EOPNOTSUPP
) {
2618 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2620 VLOG_ERR_RL(&rl
, "error receiving data from %s: %s",
2621 netdev_get_name(port
->netdev
), ovs_strerror(error
));
2626 port_reconfigure(struct dp_netdev_port
*port
)
2628 struct netdev
*netdev
= port
->netdev
;
2631 if (!netdev_is_reconf_required(netdev
)) {
2635 /* Closes the existing 'rxq's. */
2636 for (i
= 0; i
< port
->n_rxq
; i
++) {
2637 netdev_rxq_close(port
->rxq
[i
]);
2638 port
->rxq
[i
] = NULL
;
2642 /* Allows 'netdev' to apply the pending configuration changes. */
2643 err
= netdev_reconfigure(netdev
);
2644 if (err
&& (err
!= EOPNOTSUPP
)) {
2645 VLOG_ERR("Failed to set interface %s new configuration",
2646 netdev_get_name(netdev
));
2649 /* If the netdev_reconfigure() above succeeds, reopens the 'rxq's. */
2650 port
->rxq
= xrealloc(port
->rxq
, sizeof *port
->rxq
* netdev_n_rxq(netdev
));
2651 for (i
= 0; i
< netdev_n_rxq(netdev
); i
++) {
2652 err
= netdev_rxq_open(netdev
, &port
->rxq
[i
], i
);
2663 reconfigure_pmd_threads(struct dp_netdev
*dp
)
2664 OVS_REQUIRES(dp
->port_mutex
)
2666 struct dp_netdev_port
*port
, *next
;
2668 dp_netdev_destroy_all_pmds(dp
);
2670 HMAP_FOR_EACH_SAFE (port
, next
, node
, &dp
->ports
) {
2673 err
= port_reconfigure(port
);
2675 hmap_remove(&dp
->ports
, &port
->node
);
2676 seq_change(dp
->port_seq
);
2680 /* Reconfigures the cpu mask. */
2681 ovs_numa_set_cpu_mask(dp
->requested_pmd_cmask
);
2682 free(dp
->pmd_cmask
);
2683 dp
->pmd_cmask
= dp
->requested_pmd_cmask
2684 ? xstrdup(dp
->requested_pmd_cmask
)
2687 /* Restores the non-pmd. */
2688 dp_netdev_set_nonpmd(dp
);
2689 /* Restores all pmd threads. */
2690 dp_netdev_reset_pmd_threads(dp
);
2693 /* Returns true if one of the netdevs in 'dp' requires a reconfiguration */
2695 ports_require_restart(const struct dp_netdev
*dp
)
2696 OVS_REQUIRES(dp
->port_mutex
)
2698 struct dp_netdev_port
*port
;
2700 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
2701 if (netdev_is_reconf_required(port
->netdev
)) {
2709 /* Return true if needs to revalidate datapath flows. */
2711 dpif_netdev_run(struct dpif
*dpif
)
2713 struct dp_netdev_port
*port
;
2714 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2715 struct dp_netdev_pmd_thread
*non_pmd
= dp_netdev_get_pmd(dp
,
2717 uint64_t new_tnl_seq
;
2719 ovs_mutex_lock(&dp
->port_mutex
);
2720 ovs_mutex_lock(&dp
->non_pmd_mutex
);
2721 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
2722 if (!netdev_is_pmd(port
->netdev
)) {
2725 for (i
= 0; i
< port
->n_rxq
; i
++) {
2726 dp_netdev_process_rxq_port(non_pmd
, port
, port
->rxq
[i
]);
2730 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
2732 dp_netdev_pmd_unref(non_pmd
);
2734 if (!cmask_equals(dp
->pmd_cmask
, dp
->requested_pmd_cmask
)
2735 || ports_require_restart(dp
)) {
2736 reconfigure_pmd_threads(dp
);
2738 ovs_mutex_unlock(&dp
->port_mutex
);
2740 tnl_neigh_cache_run();
2742 new_tnl_seq
= seq_read(tnl_conf_seq
);
2744 if (dp
->last_tnl_conf_seq
!= new_tnl_seq
) {
2745 dp
->last_tnl_conf_seq
= new_tnl_seq
;
2752 dpif_netdev_wait(struct dpif
*dpif
)
2754 struct dp_netdev_port
*port
;
2755 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2757 ovs_mutex_lock(&dp_netdev_mutex
);
2758 ovs_mutex_lock(&dp
->port_mutex
);
2759 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
2760 netdev_wait_reconf_required(port
->netdev
);
2761 if (!netdev_is_pmd(port
->netdev
)) {
2764 for (i
= 0; i
< port
->n_rxq
; i
++) {
2765 netdev_rxq_wait(port
->rxq
[i
]);
2769 ovs_mutex_unlock(&dp
->port_mutex
);
2770 ovs_mutex_unlock(&dp_netdev_mutex
);
2771 seq_wait(tnl_conf_seq
, dp
->last_tnl_conf_seq
);
2775 pmd_free_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
2777 struct tx_port
*tx_port_cached
;
2779 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->port_cache
) {
2780 free(tx_port_cached
);
2784 /* Copies ports from 'pmd->tx_ports' (shared with the main thread) to
2785 * 'pmd->port_cache' (thread local) */
2787 pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
2788 OVS_REQUIRES(pmd
->port_mutex
)
2790 struct tx_port
*tx_port
, *tx_port_cached
;
2792 pmd_free_cached_ports(pmd
);
2793 hmap_shrink(&pmd
->port_cache
);
2795 HMAP_FOR_EACH (tx_port
, node
, &pmd
->tx_ports
) {
2796 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
2797 hmap_insert(&pmd
->port_cache
, &tx_port_cached
->node
,
2798 hash_port_no(tx_port_cached
->port_no
));
2803 pmd_load_queues_and_ports(struct dp_netdev_pmd_thread
*pmd
,
2804 struct rxq_poll
**ppoll_list
)
2806 struct rxq_poll
*poll_list
= *ppoll_list
;
2807 struct rxq_poll
*poll
;
2810 ovs_mutex_lock(&pmd
->port_mutex
);
2811 poll_list
= xrealloc(poll_list
, pmd
->poll_cnt
* sizeof *poll_list
);
2814 LIST_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
2815 poll_list
[i
++] = *poll
;
2818 pmd_load_cached_ports(pmd
);
2820 ovs_mutex_unlock(&pmd
->port_mutex
);
2822 *ppoll_list
= poll_list
;
2827 pmd_thread_main(void *f_
)
2829 struct dp_netdev_pmd_thread
*pmd
= f_
;
2830 unsigned int lc
= 0;
2831 struct rxq_poll
*poll_list
;
2832 unsigned int port_seq
= PMD_INITIAL_SEQ
;
2840 /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */
2841 ovsthread_setspecific(pmd
->dp
->per_pmd_key
, pmd
);
2842 pmd_thread_setaffinity_cpu(pmd
->core_id
);
2843 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
2845 emc_cache_init(&pmd
->flow_cache
);
2847 /* List port/core affinity */
2848 for (i
= 0; i
< poll_cnt
; i
++) {
2849 VLOG_DBG("Core %d processing port \'%s\' with queue-id %d\n",
2850 pmd
->core_id
, netdev_get_name(poll_list
[i
].port
->netdev
),
2851 netdev_rxq_get_queue_id(poll_list
[i
].rx
));
2855 for (i
= 0; i
< poll_cnt
; i
++) {
2856 dp_netdev_process_rxq_port(pmd
, poll_list
[i
].port
, poll_list
[i
].rx
);
2864 emc_cache_slow_sweep(&pmd
->flow_cache
);
2865 coverage_try_clear();
2868 atomic_read_relaxed(&pmd
->change_seq
, &seq
);
2869 if (seq
!= port_seq
) {
2876 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
2877 exiting
= latch_is_set(&pmd
->exit_latch
);
2878 /* Signal here to make sure the pmd finishes
2879 * reloading the updated configuration. */
2880 dp_netdev_pmd_reload_done(pmd
);
2882 emc_cache_uninit(&pmd
->flow_cache
);
2889 pmd_free_cached_ports(pmd
);
2894 dp_netdev_disable_upcall(struct dp_netdev
*dp
)
2895 OVS_ACQUIRES(dp
->upcall_rwlock
)
2897 fat_rwlock_wrlock(&dp
->upcall_rwlock
);
2901 dpif_netdev_disable_upcall(struct dpif
*dpif
)
2902 OVS_NO_THREAD_SAFETY_ANALYSIS
2904 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2905 dp_netdev_disable_upcall(dp
);
2909 dp_netdev_enable_upcall(struct dp_netdev
*dp
)
2910 OVS_RELEASES(dp
->upcall_rwlock
)
2912 fat_rwlock_unlock(&dp
->upcall_rwlock
);
2916 dpif_netdev_enable_upcall(struct dpif
*dpif
)
2917 OVS_NO_THREAD_SAFETY_ANALYSIS
2919 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2920 dp_netdev_enable_upcall(dp
);
2924 dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
)
2926 ovs_mutex_lock(&pmd
->cond_mutex
);
2927 xpthread_cond_signal(&pmd
->cond
);
2928 ovs_mutex_unlock(&pmd
->cond_mutex
);
2931 /* Finds and refs the dp_netdev_pmd_thread on core 'core_id'. Returns
2932 * the pointer if succeeds, otherwise, NULL.
2934 * Caller must unrefs the returned reference. */
2935 static struct dp_netdev_pmd_thread
*
2936 dp_netdev_get_pmd(struct dp_netdev
*dp
, unsigned core_id
)
2938 struct dp_netdev_pmd_thread
*pmd
;
2939 const struct cmap_node
*pnode
;
2941 pnode
= cmap_find(&dp
->poll_threads
, hash_int(core_id
, 0));
2945 pmd
= CONTAINER_OF(pnode
, struct dp_netdev_pmd_thread
, node
);
2947 return dp_netdev_pmd_try_ref(pmd
) ? pmd
: NULL
;
2950 /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */
2952 dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
2953 OVS_REQUIRES(dp
->port_mutex
)
2955 struct dp_netdev_pmd_thread
*non_pmd
;
2956 struct dp_netdev_port
*port
;
2958 non_pmd
= xzalloc(sizeof *non_pmd
);
2959 dp_netdev_configure_pmd(non_pmd
, dp
, NON_PMD_CORE_ID
, OVS_NUMA_UNSPEC
);
2961 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
2962 dp_netdev_add_port_tx_to_pmd(non_pmd
, port
);
2965 dp_netdev_reload_pmd__(non_pmd
);
2968 /* Caller must have valid pointer to 'pmd'. */
2970 dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
)
2972 return ovs_refcount_try_ref_rcu(&pmd
->ref_cnt
);
2976 dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
)
2978 if (pmd
&& ovs_refcount_unref(&pmd
->ref_cnt
) == 1) {
2979 ovsrcu_postpone(dp_netdev_destroy_pmd
, pmd
);
2983 /* Given cmap position 'pos', tries to ref the next node. If try_ref()
2984 * fails, keeps checking for next node until reaching the end of cmap.
2986 * Caller must unrefs the returned reference. */
2987 static struct dp_netdev_pmd_thread
*
2988 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
)
2990 struct dp_netdev_pmd_thread
*next
;
2993 struct cmap_node
*node
;
2995 node
= cmap_next_position(&dp
->poll_threads
, pos
);
2996 next
= node
? CONTAINER_OF(node
, struct dp_netdev_pmd_thread
, node
)
2998 } while (next
&& !dp_netdev_pmd_try_ref(next
));
3003 /* Configures the 'pmd' based on the input argument. */
3005 dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
, struct dp_netdev
*dp
,
3006 unsigned core_id
, int numa_id
)
3009 pmd
->core_id
= core_id
;
3010 pmd
->numa_id
= numa_id
;
3013 atomic_init(&pmd
->tx_qid
,
3014 (core_id
== NON_PMD_CORE_ID
)
3015 ? ovs_numa_get_n_cores()
3016 : get_n_pmd_threads(dp
));
3018 ovs_refcount_init(&pmd
->ref_cnt
);
3019 latch_init(&pmd
->exit_latch
);
3020 atomic_init(&pmd
->change_seq
, PMD_INITIAL_SEQ
);
3021 xpthread_cond_init(&pmd
->cond
, NULL
);
3022 ovs_mutex_init(&pmd
->cond_mutex
);
3023 ovs_mutex_init(&pmd
->flow_mutex
);
3024 ovs_mutex_init(&pmd
->port_mutex
);
3025 dpcls_init(&pmd
->cls
);
3026 cmap_init(&pmd
->flow_table
);
3027 ovs_list_init(&pmd
->poll_list
);
3028 hmap_init(&pmd
->tx_ports
);
3029 hmap_init(&pmd
->port_cache
);
3030 /* init the 'flow_cache' since there is no
3031 * actual thread created for NON_PMD_CORE_ID. */
3032 if (core_id
== NON_PMD_CORE_ID
) {
3033 emc_cache_init(&pmd
->flow_cache
);
3035 cmap_insert(&dp
->poll_threads
, CONST_CAST(struct cmap_node
*, &pmd
->node
),
3036 hash_int(core_id
, 0));
3040 dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
)
3042 dp_netdev_pmd_flow_flush(pmd
);
3043 dpcls_destroy(&pmd
->cls
);
3044 hmap_destroy(&pmd
->port_cache
);
3045 hmap_destroy(&pmd
->tx_ports
);
3046 cmap_destroy(&pmd
->flow_table
);
3047 ovs_mutex_destroy(&pmd
->flow_mutex
);
3048 latch_destroy(&pmd
->exit_latch
);
3049 xpthread_cond_destroy(&pmd
->cond
);
3050 ovs_mutex_destroy(&pmd
->cond_mutex
);
3051 ovs_mutex_destroy(&pmd
->port_mutex
);
3055 /* Stops the pmd thread, removes it from the 'dp->poll_threads',
3056 * and unrefs the struct. */
3058 dp_netdev_del_pmd(struct dp_netdev
*dp
, struct dp_netdev_pmd_thread
*pmd
)
3060 /* NON_PMD_CORE_ID doesn't have a thread, so we don't have to synchronize,
3061 * but extra cleanup is necessary */
3062 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3063 emc_cache_uninit(&pmd
->flow_cache
);
3064 pmd_free_cached_ports(pmd
);
3066 latch_set(&pmd
->exit_latch
);
3067 dp_netdev_reload_pmd__(pmd
);
3068 ovs_numa_unpin_core(pmd
->core_id
);
3069 xpthread_join(pmd
->thread
, NULL
);
3072 dp_netdev_pmd_clear_ports(pmd
);
3074 /* Purges the 'pmd''s flows after stopping the thread, but before
3075 * destroying the flows, so that the flow stats can be collected. */
3076 if (dp
->dp_purge_cb
) {
3077 dp
->dp_purge_cb(dp
->dp_purge_aux
, pmd
->core_id
);
3079 cmap_remove(&pmd
->dp
->poll_threads
, &pmd
->node
, hash_int(pmd
->core_id
, 0));
3080 dp_netdev_pmd_unref(pmd
);
3083 /* Destroys all pmd threads. */
3085 dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
)
3087 struct dp_netdev_pmd_thread
*pmd
;
3088 struct dp_netdev_pmd_thread
**pmd_list
;
3089 size_t k
= 0, n_pmds
;
3091 n_pmds
= cmap_count(&dp
->poll_threads
);
3092 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
3094 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3095 /* We cannot call dp_netdev_del_pmd(), since it alters
3096 * 'dp->poll_threads' (while we're iterating it) and it
3098 ovs_assert(k
< n_pmds
);
3099 pmd_list
[k
++] = pmd
;
3102 for (size_t i
= 0; i
< k
; i
++) {
3103 dp_netdev_del_pmd(dp
, pmd_list
[i
]);
3108 /* Deletes all pmd threads on numa node 'numa_id' and
3109 * fixes tx_qids of other threads to keep them sequential. */
3111 dp_netdev_del_pmds_on_numa(struct dp_netdev
*dp
, int numa_id
)
3113 struct dp_netdev_pmd_thread
*pmd
;
3114 int n_pmds_on_numa
, n_pmds
;
3115 int *free_idx
, k
= 0;
3116 struct dp_netdev_pmd_thread
**pmd_list
;
3118 n_pmds_on_numa
= get_n_pmd_threads_on_numa(dp
, numa_id
);
3119 free_idx
= xcalloc(n_pmds_on_numa
, sizeof *free_idx
);
3120 pmd_list
= xcalloc(n_pmds_on_numa
, sizeof *pmd_list
);
3122 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3123 /* We cannot call dp_netdev_del_pmd(), since it alters
3124 * 'dp->poll_threads' (while we're iterating it) and it
3126 if (pmd
->numa_id
== numa_id
) {
3127 atomic_read_relaxed(&pmd
->tx_qid
, &free_idx
[k
]);
3129 ovs_assert(k
< n_pmds_on_numa
);
3134 for (int i
= 0; i
< k
; i
++) {
3135 dp_netdev_del_pmd(dp
, pmd_list
[i
]);
3138 n_pmds
= get_n_pmd_threads(dp
);
3139 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3142 atomic_read_relaxed(&pmd
->tx_qid
, &old_tx_qid
);
3144 if (old_tx_qid
>= n_pmds
) {
3145 int new_tx_qid
= free_idx
[--k
];
3147 atomic_store_relaxed(&pmd
->tx_qid
, new_tx_qid
);
3155 /* Deletes all rx queues from pmd->poll_list and all the ports from
3158 dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
)
3160 struct rxq_poll
*poll
;
3161 struct tx_port
*port
;
3163 ovs_mutex_lock(&pmd
->port_mutex
);
3164 LIST_FOR_EACH_POP (poll
, node
, &pmd
->poll_list
) {
3168 HMAP_FOR_EACH_POP (port
, node
, &pmd
->tx_ports
) {
3171 ovs_mutex_unlock(&pmd
->port_mutex
);
3174 static struct tx_port
*
3175 tx_port_lookup(const struct hmap
*hmap
, odp_port_t port_no
)
3179 HMAP_FOR_EACH_IN_BUCKET (tx
, node
, hash_port_no(port_no
), hmap
) {
3180 if (tx
->port_no
== port_no
) {
3188 /* Deletes all rx queues of 'port' from 'poll_list', and the 'port' from
3189 * 'tx_ports' of 'pmd' thread. Returns true if 'port' was found in 'pmd'
3190 * (therefore a restart is required). */
3192 dp_netdev_del_port_from_pmd__(struct dp_netdev_port
*port
,
3193 struct dp_netdev_pmd_thread
*pmd
)
3195 struct rxq_poll
*poll
, *next
;
3199 ovs_mutex_lock(&pmd
->port_mutex
);
3200 LIST_FOR_EACH_SAFE (poll
, next
, node
, &pmd
->poll_list
) {
3201 if (poll
->port
== port
) {
3203 ovs_list_remove(&poll
->node
);
3209 tx
= tx_port_lookup(&pmd
->tx_ports
, port
->port_no
);
3211 hmap_remove(&pmd
->tx_ports
, &tx
->node
);
3215 ovs_mutex_unlock(&pmd
->port_mutex
);
3220 /* Deletes 'port' from the 'poll_list' and from the 'tx_ports' of all the pmd
3221 * threads. The pmd threads that need to be restarted are inserted in
3224 dp_netdev_del_port_from_all_pmds__(struct dp_netdev
*dp
,
3225 struct dp_netdev_port
*port
,
3226 struct hmapx
*to_reload
)
3228 struct dp_netdev_pmd_thread
*pmd
;
3230 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3233 found
= dp_netdev_del_port_from_pmd__(port
, pmd
);
3236 hmapx_add(to_reload
, pmd
);
3241 /* Deletes 'port' from the 'poll_list' and from the 'tx_ports' of all the pmd
3242 * threads. Reloads the threads if needed. */
3244 dp_netdev_del_port_from_all_pmds(struct dp_netdev
*dp
,
3245 struct dp_netdev_port
*port
)
3247 struct dp_netdev_pmd_thread
*pmd
;
3248 struct hmapx to_reload
= HMAPX_INITIALIZER(&to_reload
);
3249 struct hmapx_node
*node
;
3251 dp_netdev_del_port_from_all_pmds__(dp
, port
, &to_reload
);
3253 HMAPX_FOR_EACH (node
, &to_reload
) {
3254 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3255 dp_netdev_reload_pmd__(pmd
);
3258 hmapx_destroy(&to_reload
);
3262 /* Returns PMD thread from this numa node with fewer rx queues to poll.
3263 * Returns NULL if there is no PMD threads on this numa node.
3264 * Can be called safely only by main thread. */
3265 static struct dp_netdev_pmd_thread
*
3266 dp_netdev_less_loaded_pmd_on_numa(struct dp_netdev
*dp
, int numa_id
)
3269 struct dp_netdev_pmd_thread
*pmd
, *res
= NULL
;
3271 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3272 if (pmd
->numa_id
== numa_id
3273 && (min_cnt
> pmd
->poll_cnt
|| res
== NULL
)) {
3274 min_cnt
= pmd
->poll_cnt
;
3282 /* Adds rx queue to poll_list of PMD thread. */
3284 dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
3285 struct dp_netdev_port
*port
, struct netdev_rxq
*rx
)
3286 OVS_REQUIRES(pmd
->port_mutex
)
3288 struct rxq_poll
*poll
= xmalloc(sizeof *poll
);
3293 ovs_list_push_back(&pmd
->poll_list
, &poll
->node
);
3297 /* Add 'port' to the tx port cache of 'pmd', which must be reloaded for the
3298 * changes to take effect. */
3300 dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
3301 struct dp_netdev_port
*port
)
3303 struct tx_port
*tx
= xzalloc(sizeof *tx
);
3305 tx
->netdev
= port
->netdev
;
3306 tx
->port_no
= port
->port_no
;
3308 ovs_mutex_lock(&pmd
->port_mutex
);
3309 hmap_insert(&pmd
->tx_ports
, &tx
->node
, hash_port_no(tx
->port_no
));
3310 ovs_mutex_unlock(&pmd
->port_mutex
);
3313 /* Distribute all rx queues of 'port' between PMD threads in 'dp'. The pmd
3314 * threads that need to be restarted are inserted in 'to_reload'. */
3316 dp_netdev_add_port_rx_to_pmds(struct dp_netdev
*dp
,
3317 struct dp_netdev_port
*port
,
3318 struct hmapx
*to_reload
)
3320 int numa_id
= netdev_get_numa_id(port
->netdev
);
3323 if (!netdev_is_pmd(port
->netdev
)) {
3327 for (i
= 0; i
< port
->n_rxq
; i
++) {
3328 struct dp_netdev_pmd_thread
*pmd
;
3330 pmd
= dp_netdev_less_loaded_pmd_on_numa(dp
, numa_id
);
3332 VLOG_WARN("There's no pmd thread on numa node %d", numa_id
);
3336 ovs_mutex_lock(&pmd
->port_mutex
);
3337 dp_netdev_add_rxq_to_pmd(pmd
, port
, port
->rxq
[i
]);
3338 ovs_mutex_unlock(&pmd
->port_mutex
);
3340 hmapx_add(to_reload
, pmd
);
3344 /* Distributes all rx queues of 'port' between all PMD threads in 'dp' and
3345 * inserts 'port' in the PMD threads 'tx_ports'. The pmd threads that need to
3346 * be restarted are inserted in 'to_reload'. */
3348 dp_netdev_add_port_to_pmds__(struct dp_netdev
*dp
, struct dp_netdev_port
*port
,
3349 struct hmapx
*to_reload
)
3351 struct dp_netdev_pmd_thread
*pmd
;
3353 dp_netdev_add_port_rx_to_pmds(dp
, port
, to_reload
);
3355 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3356 dp_netdev_add_port_tx_to_pmd(pmd
, port
);
3357 hmapx_add(to_reload
, pmd
);
3361 /* Distributes all rx queues of 'port' between all PMD threads in 'dp', inserts
3362 * 'port' in the PMD threads 'tx_ports' and reloads them, if needed. */
3364 dp_netdev_add_port_to_pmds(struct dp_netdev
*dp
, struct dp_netdev_port
*port
)
3366 struct dp_netdev_pmd_thread
*pmd
;
3367 struct hmapx to_reload
= HMAPX_INITIALIZER(&to_reload
);
3368 struct hmapx_node
*node
;
3370 dp_netdev_add_port_to_pmds__(dp
, port
, &to_reload
);
3372 HMAPX_FOR_EACH (node
, &to_reload
) {
3373 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3374 dp_netdev_reload_pmd__(pmd
);
3377 hmapx_destroy(&to_reload
);
3380 /* Starts pmd threads for the numa node 'numa_id', if not already started.
3381 * The function takes care of filling the threads tx port cache. */
3383 dp_netdev_set_pmds_on_numa(struct dp_netdev
*dp
, int numa_id
)
3384 OVS_REQUIRES(dp
->port_mutex
)
3388 if (!ovs_numa_numa_id_is_valid(numa_id
)) {
3389 VLOG_WARN("Cannot create pmd threads due to numa id (%d) invalid",
3394 n_pmds
= get_n_pmd_threads_on_numa(dp
, numa_id
);
3396 /* If there are already pmd threads created for the numa node
3397 * in which 'netdev' is on, do nothing. Else, creates the
3398 * pmd threads for the numa node. */
3400 int can_have
, n_unpinned
, i
;
3402 n_unpinned
= ovs_numa_get_n_unpinned_cores_on_numa(numa_id
);
3404 VLOG_WARN("Cannot create pmd threads due to out of unpinned "
3405 "cores on numa node %d", numa_id
);
3409 /* If cpu mask is specified, uses all unpinned cores, otherwise
3410 * tries creating NR_PMD_THREADS pmd threads. */
3411 can_have
= dp
->pmd_cmask
? n_unpinned
: MIN(n_unpinned
, NR_PMD_THREADS
);
3412 for (i
= 0; i
< can_have
; i
++) {
3413 unsigned core_id
= ovs_numa_get_unpinned_core_on_numa(numa_id
);
3414 struct dp_netdev_pmd_thread
*pmd
= xzalloc(sizeof *pmd
);
3415 struct dp_netdev_port
*port
;
3417 dp_netdev_configure_pmd(pmd
, dp
, core_id
, numa_id
);
3419 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3420 dp_netdev_add_port_tx_to_pmd(pmd
, port
);
3423 pmd
->thread
= ovs_thread_create("pmd", pmd_thread_main
, pmd
);
3425 VLOG_INFO("Created %d pmd threads on numa node %d", can_have
, numa_id
);
3430 /* Called after pmd threads config change. Restarts pmd threads with
3431 * new configuration. */
3433 dp_netdev_reset_pmd_threads(struct dp_netdev
*dp
)
3434 OVS_REQUIRES(dp
->port_mutex
)
3436 struct hmapx to_reload
= HMAPX_INITIALIZER(&to_reload
);
3437 struct dp_netdev_pmd_thread
*pmd
;
3438 struct dp_netdev_port
*port
;
3439 struct hmapx_node
*node
;
3441 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3442 if (netdev_is_pmd(port
->netdev
)) {
3443 int numa_id
= netdev_get_numa_id(port
->netdev
);
3445 dp_netdev_set_pmds_on_numa(dp
, numa_id
);
3447 dp_netdev_add_port_rx_to_pmds(dp
, port
, &to_reload
);
3450 HMAPX_FOR_EACH (node
, &to_reload
) {
3451 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3452 dp_netdev_reload_pmd__(pmd
);
3455 hmapx_destroy(&to_reload
);
3459 dpif_netdev_get_datapath_version(void)
3461 return xstrdup("<built-in>");
3465 dp_netdev_flow_used(struct dp_netdev_flow
*netdev_flow
, int cnt
, int size
,
3466 uint16_t tcp_flags
, long long now
)
3470 atomic_store_relaxed(&netdev_flow
->stats
.used
, now
);
3471 non_atomic_ullong_add(&netdev_flow
->stats
.packet_count
, cnt
);
3472 non_atomic_ullong_add(&netdev_flow
->stats
.byte_count
, size
);
3473 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
3475 atomic_store_relaxed(&netdev_flow
->stats
.tcp_flags
, flags
);
3479 dp_netdev_count_packet(struct dp_netdev_pmd_thread
*pmd
,
3480 enum dp_stat_type type
, int cnt
)
3482 non_atomic_ullong_add(&pmd
->stats
.n
[type
], cnt
);
3486 dp_netdev_upcall(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet
*packet_
,
3487 struct flow
*flow
, struct flow_wildcards
*wc
, ovs_u128
*ufid
,
3488 enum dpif_upcall_type type
, const struct nlattr
*userdata
,
3489 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
3491 struct dp_netdev
*dp
= pmd
->dp
;
3492 struct flow_tnl orig_tunnel
;
3495 if (OVS_UNLIKELY(!dp
->upcall_cb
)) {
3499 /* Upcall processing expects the Geneve options to be in the translated
3500 * format but we need to retain the raw format for datapath use. */
3501 orig_tunnel
.flags
= flow
->tunnel
.flags
;
3502 if (flow
->tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
3503 orig_tunnel
.metadata
.present
.len
= flow
->tunnel
.metadata
.present
.len
;
3504 memcpy(orig_tunnel
.metadata
.opts
.gnv
, flow
->tunnel
.metadata
.opts
.gnv
,
3505 flow
->tunnel
.metadata
.present
.len
);
3506 err
= tun_metadata_from_geneve_udpif(&orig_tunnel
, &orig_tunnel
,
3513 if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl
))) {
3514 struct ds ds
= DS_EMPTY_INITIALIZER
;
3517 struct odp_flow_key_parms odp_parms
= {
3520 .odp_in_port
= flow
->in_port
.odp_port
,
3521 .support
= dp_netdev_support
,
3524 ofpbuf_init(&key
, 0);
3525 odp_flow_key_from_flow(&odp_parms
, &key
);
3526 packet_str
= ofp_packet_to_string(dp_packet_data(packet_
),
3527 dp_packet_size(packet_
));
3529 odp_flow_key_format(key
.data
, key
.size
, &ds
);
3531 VLOG_DBG("%s: %s upcall:\n%s\n%s", dp
->name
,
3532 dpif_upcall_type_to_string(type
), ds_cstr(&ds
), packet_str
);
3534 ofpbuf_uninit(&key
);
3540 err
= dp
->upcall_cb(packet_
, flow
, ufid
, pmd
->core_id
, type
, userdata
,
3541 actions
, wc
, put_actions
, dp
->upcall_aux
);
3542 if (err
&& err
!= ENOSPC
) {
3546 /* Translate tunnel metadata masks to datapath format. */
3548 if (wc
->masks
.tunnel
.metadata
.present
.map
) {
3549 struct geneve_opt opts
[TLV_TOT_OPT_SIZE
/
3550 sizeof(struct geneve_opt
)];
3552 if (orig_tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
3553 tun_metadata_to_geneve_udpif_mask(&flow
->tunnel
,
3555 orig_tunnel
.metadata
.opts
.gnv
,
3556 orig_tunnel
.metadata
.present
.len
,
3559 orig_tunnel
.metadata
.present
.len
= 0;
3562 memset(&wc
->masks
.tunnel
.metadata
, 0,
3563 sizeof wc
->masks
.tunnel
.metadata
);
3564 memcpy(&wc
->masks
.tunnel
.metadata
.opts
.gnv
, opts
,
3565 orig_tunnel
.metadata
.present
.len
);
3567 wc
->masks
.tunnel
.metadata
.present
.len
= 0xff;
3570 /* Restore tunnel metadata. We need to use the saved options to ensure
3571 * that any unknown options are not lost. The generated mask will have
3572 * the same structure, matching on types and lengths but wildcarding
3573 * option data we don't care about. */
3574 if (orig_tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
3575 memcpy(&flow
->tunnel
.metadata
.opts
.gnv
, orig_tunnel
.metadata
.opts
.gnv
,
3576 orig_tunnel
.metadata
.present
.len
);
3577 flow
->tunnel
.metadata
.present
.len
= orig_tunnel
.metadata
.present
.len
;
3578 flow
->tunnel
.flags
|= FLOW_TNL_F_UDPIF
;
3584 static inline uint32_t
3585 dpif_netdev_packet_get_rss_hash(struct dp_packet
*packet
,
3586 const struct miniflow
*mf
)
3588 uint32_t hash
, recirc_depth
;
3590 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
3591 hash
= dp_packet_get_rss_hash(packet
);
3593 hash
= miniflow_hash_5tuple(mf
, 0);
3594 dp_packet_set_rss_hash(packet
, hash
);
3597 /* The RSS hash must account for the recirculation depth to avoid
3598 * collisions in the exact match cache */
3599 recirc_depth
= *recirc_depth_get_unsafe();
3600 if (OVS_UNLIKELY(recirc_depth
)) {
3601 hash
= hash_finish(hash
, recirc_depth
);
3602 dp_packet_set_rss_hash(packet
, hash
);
3607 struct packet_batch_per_flow
{
3608 unsigned int byte_count
;
3610 struct dp_netdev_flow
*flow
;
3612 struct dp_packet_batch array
;
3616 packet_batch_per_flow_update(struct packet_batch_per_flow
*batch
,
3617 struct dp_packet
*packet
,
3618 const struct miniflow
*mf
)
3620 batch
->byte_count
+= dp_packet_size(packet
);
3621 batch
->tcp_flags
|= miniflow_get_tcp_flags(mf
);
3622 batch
->array
.packets
[batch
->array
.count
++] = packet
;
3626 packet_batch_per_flow_init(struct packet_batch_per_flow
*batch
,
3627 struct dp_netdev_flow
*flow
)
3629 flow
->batch
= batch
;
3632 dp_packet_batch_init(&batch
->array
);
3633 batch
->byte_count
= 0;
3634 batch
->tcp_flags
= 0;
3638 packet_batch_per_flow_execute(struct packet_batch_per_flow
*batch
,
3639 struct dp_netdev_pmd_thread
*pmd
,
3642 struct dp_netdev_actions
*actions
;
3643 struct dp_netdev_flow
*flow
= batch
->flow
;
3645 dp_netdev_flow_used(flow
, batch
->array
.count
, batch
->byte_count
,
3646 batch
->tcp_flags
, now
);
3648 actions
= dp_netdev_flow_get_actions(flow
);
3650 dp_netdev_execute_actions(pmd
, &batch
->array
, true,
3651 actions
->actions
, actions
->size
);
3655 dp_netdev_queue_batches(struct dp_packet
*pkt
,
3656 struct dp_netdev_flow
*flow
, const struct miniflow
*mf
,
3657 struct packet_batch_per_flow
*batches
, size_t *n_batches
)
3659 struct packet_batch_per_flow
*batch
= flow
->batch
;
3661 if (OVS_UNLIKELY(!batch
)) {
3662 batch
= &batches
[(*n_batches
)++];
3663 packet_batch_per_flow_init(batch
, flow
);
3666 packet_batch_per_flow_update(batch
, pkt
, mf
);
3669 /* Try to process all ('cnt') the 'packets' using only the exact match cache
3670 * 'pmd->flow_cache'. If a flow is not found for a packet 'packets[i]', the
3671 * miniflow is copied into 'keys' and the packet pointer is moved at the
3672 * beginning of the 'packets' array.
3674 * The function returns the number of packets that needs to be processed in the
3675 * 'packets' array (they have been moved to the beginning of the vector).
3677 * If 'md_is_valid' is false, the metadata in 'packets' is not valid and must be
3678 * initialized by this function using 'port_no'.
3680 static inline size_t
3681 emc_processing(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet_batch
*packets_
,
3682 struct netdev_flow_key
*keys
,
3683 struct packet_batch_per_flow batches
[], size_t *n_batches
,
3684 bool md_is_valid
, odp_port_t port_no
)
3686 struct emc_cache
*flow_cache
= &pmd
->flow_cache
;
3687 struct netdev_flow_key
*key
= &keys
[0];
3688 size_t i
, n_missed
= 0, n_dropped
= 0;
3689 struct dp_packet
**packets
= packets_
->packets
;
3690 int cnt
= packets_
->count
;
3692 for (i
= 0; i
< cnt
; i
++) {
3693 struct dp_netdev_flow
*flow
;
3694 struct dp_packet
*packet
= packets
[i
];
3696 if (OVS_UNLIKELY(dp_packet_size(packet
) < ETH_HEADER_LEN
)) {
3697 dp_packet_delete(packet
);
3703 /* Prefetch next packet data and metadata. */
3704 OVS_PREFETCH(dp_packet_data(packets
[i
+1]));
3705 pkt_metadata_prefetch_init(&packets
[i
+1]->md
);
3709 pkt_metadata_init(&packet
->md
, port_no
);
3711 miniflow_extract(packet
, &key
->mf
);
3712 key
->len
= 0; /* Not computed yet. */
3713 key
->hash
= dpif_netdev_packet_get_rss_hash(packet
, &key
->mf
);
3715 flow
= emc_lookup(flow_cache
, key
);
3716 if (OVS_LIKELY(flow
)) {
3717 dp_netdev_queue_batches(packet
, flow
, &key
->mf
, batches
,
3720 /* Exact match cache missed. Group missed packets together at
3721 * the beginning of the 'packets' array. */
3722 packets
[n_missed
] = packet
;
3723 /* 'key[n_missed]' contains the key of the current packet and it
3724 * must be returned to the caller. The next key should be extracted
3725 * to 'keys[n_missed + 1]'. */
3726 key
= &keys
[++n_missed
];
3730 dp_netdev_count_packet(pmd
, DP_STAT_EXACT_HIT
, cnt
- n_dropped
- n_missed
);
3736 handle_packet_upcall(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet
*packet
,
3737 const struct netdev_flow_key
*key
,
3738 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
,
3741 struct ofpbuf
*add_actions
;
3742 struct dp_packet_batch b
;
3747 match
.tun_md
.valid
= false;
3748 miniflow_expand(&key
->mf
, &match
.flow
);
3750 ofpbuf_clear(actions
);
3751 ofpbuf_clear(put_actions
);
3753 dpif_flow_hash(pmd
->dp
->dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
3754 error
= dp_netdev_upcall(pmd
, packet
, &match
.flow
, &match
.wc
,
3755 &ufid
, DPIF_UC_MISS
, NULL
, actions
,
3757 if (OVS_UNLIKELY(error
&& error
!= ENOSPC
)) {
3758 dp_packet_delete(packet
);
3763 /* The Netlink encoding of datapath flow keys cannot express
3764 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
3765 * tag is interpreted as exact match on the fact that there is no
3766 * VLAN. Unless we refactor a lot of code that translates between
3767 * Netlink and struct flow representations, we have to do the same
3769 if (!match
.wc
.masks
.vlan_tci
) {
3770 match
.wc
.masks
.vlan_tci
= htons(0xffff);
3773 /* We can't allow the packet batching in the next loop to execute
3774 * the actions. Otherwise, if there are any slow path actions,
3775 * we'll send the packet up twice. */
3776 packet_batch_init_packet(&b
, packet
);
3777 dp_netdev_execute_actions(pmd
, &b
, true,
3778 actions
->data
, actions
->size
);
3780 add_actions
= put_actions
->size
? put_actions
: actions
;
3781 if (OVS_LIKELY(error
!= ENOSPC
)) {
3782 struct dp_netdev_flow
*netdev_flow
;
3784 /* XXX: There's a race window where a flow covering this packet
3785 * could have already been installed since we last did the flow
3786 * lookup before upcall. This could be solved by moving the
3787 * mutex lock outside the loop, but that's an awful long time
3788 * to be locking everyone out of making flow installs. If we
3789 * move to a per-core classifier, it would be reasonable. */
3790 ovs_mutex_lock(&pmd
->flow_mutex
);
3791 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
);
3792 if (OVS_LIKELY(!netdev_flow
)) {
3793 netdev_flow
= dp_netdev_flow_add(pmd
, &match
, &ufid
,
3797 ovs_mutex_unlock(&pmd
->flow_mutex
);
3799 emc_insert(&pmd
->flow_cache
, key
, netdev_flow
);
3804 fast_path_processing(struct dp_netdev_pmd_thread
*pmd
,
3805 struct dp_packet_batch
*packets_
,
3806 struct netdev_flow_key
*keys
,
3807 struct packet_batch_per_flow batches
[], size_t *n_batches
)
3809 int cnt
= packets_
->count
;
3810 #if !defined(__CHECKER__) && !defined(_WIN32)
3811 const size_t PKT_ARRAY_SIZE
= cnt
;
3813 /* Sparse or MSVC doesn't like variable length array. */
3814 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
3816 struct dp_packet
**packets
= packets_
->packets
;
3817 struct dpcls_rule
*rules
[PKT_ARRAY_SIZE
];
3818 struct dp_netdev
*dp
= pmd
->dp
;
3819 struct emc_cache
*flow_cache
= &pmd
->flow_cache
;
3820 int miss_cnt
= 0, lost_cnt
= 0;
3824 for (i
= 0; i
< cnt
; i
++) {
3825 /* Key length is needed in all the cases, hash computed on demand. */
3826 keys
[i
].len
= netdev_flow_key_size(miniflow_n_values(&keys
[i
].mf
));
3828 any_miss
= !dpcls_lookup(&pmd
->cls
, keys
, rules
, cnt
);
3829 if (OVS_UNLIKELY(any_miss
) && !fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
3830 uint64_t actions_stub
[512 / 8], slow_stub
[512 / 8];
3831 struct ofpbuf actions
, put_actions
;
3833 ofpbuf_use_stub(&actions
, actions_stub
, sizeof actions_stub
);
3834 ofpbuf_use_stub(&put_actions
, slow_stub
, sizeof slow_stub
);
3836 for (i
= 0; i
< cnt
; i
++) {
3837 struct dp_netdev_flow
*netdev_flow
;
3839 if (OVS_LIKELY(rules
[i
])) {
3843 /* It's possible that an earlier slow path execution installed
3844 * a rule covering this flow. In this case, it's a lot cheaper
3845 * to catch it here than execute a miss. */
3846 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, &keys
[i
]);
3848 rules
[i
] = &netdev_flow
->cr
;
3853 handle_packet_upcall(pmd
, packets
[i
], &keys
[i
], &actions
, &put_actions
,
3857 ofpbuf_uninit(&actions
);
3858 ofpbuf_uninit(&put_actions
);
3859 fat_rwlock_unlock(&dp
->upcall_rwlock
);
3860 dp_netdev_count_packet(pmd
, DP_STAT_LOST
, lost_cnt
);
3861 } else if (OVS_UNLIKELY(any_miss
)) {
3862 for (i
= 0; i
< cnt
; i
++) {
3863 if (OVS_UNLIKELY(!rules
[i
])) {
3864 dp_packet_delete(packets
[i
]);
3871 for (i
= 0; i
< cnt
; i
++) {
3872 struct dp_packet
*packet
= packets
[i
];
3873 struct dp_netdev_flow
*flow
;
3875 if (OVS_UNLIKELY(!rules
[i
])) {
3879 flow
= dp_netdev_flow_cast(rules
[i
]);
3881 emc_insert(flow_cache
, &keys
[i
], flow
);
3882 dp_netdev_queue_batches(packet
, flow
, &keys
[i
].mf
, batches
, n_batches
);
3885 dp_netdev_count_packet(pmd
, DP_STAT_MASKED_HIT
, cnt
- miss_cnt
);
3886 dp_netdev_count_packet(pmd
, DP_STAT_MISS
, miss_cnt
);
3887 dp_netdev_count_packet(pmd
, DP_STAT_LOST
, lost_cnt
);
3890 /* Packets enter the datapath from a port (or from recirculation) here.
3892 * For performance reasons a caller may choose not to initialize the metadata
3893 * in 'packets': in this case 'mdinit' is false and this function needs to
3894 * initialize it using 'port_no'. If the metadata in 'packets' is already
3895 * valid, 'md_is_valid' must be true and 'port_no' will be ignored. */
3897 dp_netdev_input__(struct dp_netdev_pmd_thread
*pmd
,
3898 struct dp_packet_batch
*packets
,
3899 bool md_is_valid
, odp_port_t port_no
)
3901 int cnt
= packets
->count
;
3902 #if !defined(__CHECKER__) && !defined(_WIN32)
3903 const size_t PKT_ARRAY_SIZE
= cnt
;
3905 /* Sparse or MSVC doesn't like variable length array. */
3906 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
3908 struct netdev_flow_key keys
[PKT_ARRAY_SIZE
];
3909 struct packet_batch_per_flow batches
[PKT_ARRAY_SIZE
];
3910 long long now
= time_msec();
3911 size_t newcnt
, n_batches
, i
;
3914 newcnt
= emc_processing(pmd
, packets
, keys
, batches
, &n_batches
,
3915 md_is_valid
, port_no
);
3916 if (OVS_UNLIKELY(newcnt
)) {
3917 packets
->count
= newcnt
;
3918 fast_path_processing(pmd
, packets
, keys
, batches
, &n_batches
);
3921 for (i
= 0; i
< n_batches
; i
++) {
3922 batches
[i
].flow
->batch
= NULL
;
3925 for (i
= 0; i
< n_batches
; i
++) {
3926 packet_batch_per_flow_execute(&batches
[i
], pmd
, now
);
3931 dp_netdev_input(struct dp_netdev_pmd_thread
*pmd
,
3932 struct dp_packet_batch
*packets
,
3935 dp_netdev_input__(pmd
, packets
, false, port_no
);
3939 dp_netdev_recirculate(struct dp_netdev_pmd_thread
*pmd
,
3940 struct dp_packet_batch
*packets
)
3942 dp_netdev_input__(pmd
, packets
, true, 0);
3945 struct dp_netdev_execute_aux
{
3946 struct dp_netdev_pmd_thread
*pmd
;
3950 dpif_netdev_register_dp_purge_cb(struct dpif
*dpif
, dp_purge_callback
*cb
,
3953 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3954 dp
->dp_purge_aux
= aux
;
3955 dp
->dp_purge_cb
= cb
;
3959 dpif_netdev_register_upcall_cb(struct dpif
*dpif
, upcall_callback
*cb
,
3962 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3963 dp
->upcall_aux
= aux
;
3967 static struct tx_port
*
3968 pmd_tx_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
3971 return tx_port_lookup(&pmd
->port_cache
, port_no
);
3975 push_tnl_action(const struct dp_netdev_pmd_thread
*pmd
,
3976 const struct nlattr
*attr
,
3977 struct dp_packet_batch
*batch
)
3979 struct tx_port
*tun_port
;
3980 const struct ovs_action_push_tnl
*data
;
3983 data
= nl_attr_get(attr
);
3985 tun_port
= pmd_tx_port_cache_lookup(pmd
, u32_to_odp(data
->tnl_port
));
3990 err
= netdev_push_header(tun_port
->netdev
, batch
, data
);
3995 dp_packet_delete_batch(batch
, true);
4000 dp_execute_userspace_action(struct dp_netdev_pmd_thread
*pmd
,
4001 struct dp_packet
*packet
, bool may_steal
,
4002 struct flow
*flow
, ovs_u128
*ufid
,
4003 struct ofpbuf
*actions
,
4004 const struct nlattr
*userdata
)
4006 struct dp_packet_batch b
;
4009 ofpbuf_clear(actions
);
4011 error
= dp_netdev_upcall(pmd
, packet
, flow
, NULL
, ufid
,
4012 DPIF_UC_ACTION
, userdata
, actions
,
4014 if (!error
|| error
== ENOSPC
) {
4015 packet_batch_init_packet(&b
, packet
);
4016 dp_netdev_execute_actions(pmd
, &b
, may_steal
,
4017 actions
->data
, actions
->size
);
4018 } else if (may_steal
) {
4019 dp_packet_delete(packet
);
4024 dp_execute_cb(void *aux_
, struct dp_packet_batch
*packets_
,
4025 const struct nlattr
*a
, bool may_steal
)
4027 struct dp_netdev_execute_aux
*aux
= aux_
;
4028 uint32_t *depth
= recirc_depth_get();
4029 struct dp_netdev_pmd_thread
*pmd
= aux
->pmd
;
4030 struct dp_netdev
*dp
= pmd
->dp
;
4031 int type
= nl_attr_type(a
);
4034 switch ((enum ovs_action_attr
)type
) {
4035 case OVS_ACTION_ATTR_OUTPUT
:
4036 p
= pmd_tx_port_cache_lookup(pmd
, u32_to_odp(nl_attr_get_u32(a
)));
4037 if (OVS_LIKELY(p
)) {
4040 atomic_read_relaxed(&pmd
->tx_qid
, &tx_qid
);
4042 netdev_send(p
->netdev
, tx_qid
, packets_
, may_steal
);
4047 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
4048 if (*depth
< MAX_RECIRC_DEPTH
) {
4049 struct dp_packet_batch tnl_pkt
;
4053 dp_packet_batch_clone(&tnl_pkt
, packets_
);
4054 packets_
= &tnl_pkt
;
4057 err
= push_tnl_action(pmd
, a
, packets_
);
4060 dp_netdev_recirculate(pmd
, packets_
);
4067 case OVS_ACTION_ATTR_TUNNEL_POP
:
4068 if (*depth
< MAX_RECIRC_DEPTH
) {
4069 odp_port_t portno
= u32_to_odp(nl_attr_get_u32(a
));
4071 p
= pmd_tx_port_cache_lookup(pmd
, portno
);
4073 struct dp_packet_batch tnl_pkt
;
4077 dp_packet_batch_clone(&tnl_pkt
, packets_
);
4078 packets_
= &tnl_pkt
;
4081 netdev_pop_header(p
->netdev
, packets_
);
4082 if (!packets_
->count
) {
4086 for (i
= 0; i
< packets_
->count
; i
++) {
4087 packets_
->packets
[i
]->md
.in_port
.odp_port
= portno
;
4091 dp_netdev_recirculate(pmd
, packets_
);
4098 case OVS_ACTION_ATTR_USERSPACE
:
4099 if (!fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
4100 struct dp_packet
**packets
= packets_
->packets
;
4101 const struct nlattr
*userdata
;
4102 struct ofpbuf actions
;
4107 userdata
= nl_attr_find_nested(a
, OVS_USERSPACE_ATTR_USERDATA
);
4108 ofpbuf_init(&actions
, 0);
4110 for (i
= 0; i
< packets_
->count
; i
++) {
4111 flow_extract(packets
[i
], &flow
);
4112 dpif_flow_hash(dp
->dpif
, &flow
, sizeof flow
, &ufid
);
4113 dp_execute_userspace_action(pmd
, packets
[i
], may_steal
, &flow
,
4114 &ufid
, &actions
, userdata
);
4116 ofpbuf_uninit(&actions
);
4117 fat_rwlock_unlock(&dp
->upcall_rwlock
);
4123 case OVS_ACTION_ATTR_RECIRC
:
4124 if (*depth
< MAX_RECIRC_DEPTH
) {
4125 struct dp_packet_batch recirc_pkts
;
4129 dp_packet_batch_clone(&recirc_pkts
, packets_
);
4130 packets_
= &recirc_pkts
;
4133 for (i
= 0; i
< packets_
->count
; i
++) {
4134 packets_
->packets
[i
]->md
.recirc_id
= nl_attr_get_u32(a
);
4138 dp_netdev_recirculate(pmd
, packets_
);
4144 VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
4147 case OVS_ACTION_ATTR_CT
:
4148 /* If a flow with this action is slow-pathed, datapath assistance is
4149 * required to implement it. However, we don't support this action
4150 * in the userspace datapath. */
4151 VLOG_WARN("Cannot execute conntrack action in userspace.");
4154 case OVS_ACTION_ATTR_PUSH_VLAN
:
4155 case OVS_ACTION_ATTR_POP_VLAN
:
4156 case OVS_ACTION_ATTR_PUSH_MPLS
:
4157 case OVS_ACTION_ATTR_POP_MPLS
:
4158 case OVS_ACTION_ATTR_SET
:
4159 case OVS_ACTION_ATTR_SET_MASKED
:
4160 case OVS_ACTION_ATTR_SAMPLE
:
4161 case OVS_ACTION_ATTR_HASH
:
4162 case OVS_ACTION_ATTR_UNSPEC
:
4163 case __OVS_ACTION_ATTR_MAX
:
4167 dp_packet_delete_batch(packets_
, may_steal
);
4171 dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
4172 struct dp_packet_batch
*packets
,
4174 const struct nlattr
*actions
, size_t actions_len
)
4176 struct dp_netdev_execute_aux aux
= { pmd
};
4178 odp_execute_actions(&aux
, packets
, may_steal
, actions
,
4179 actions_len
, dp_execute_cb
);
4182 const struct dpif_class dpif_netdev_class
= {
4185 dpif_netdev_enumerate
,
4186 dpif_netdev_port_open_type
,
4189 dpif_netdev_destroy
,
4192 dpif_netdev_get_stats
,
4193 dpif_netdev_port_add
,
4194 dpif_netdev_port_del
,
4195 dpif_netdev_port_query_by_number
,
4196 dpif_netdev_port_query_by_name
,
4197 NULL
, /* port_get_pid */
4198 dpif_netdev_port_dump_start
,
4199 dpif_netdev_port_dump_next
,
4200 dpif_netdev_port_dump_done
,
4201 dpif_netdev_port_poll
,
4202 dpif_netdev_port_poll_wait
,
4203 dpif_netdev_flow_flush
,
4204 dpif_netdev_flow_dump_create
,
4205 dpif_netdev_flow_dump_destroy
,
4206 dpif_netdev_flow_dump_thread_create
,
4207 dpif_netdev_flow_dump_thread_destroy
,
4208 dpif_netdev_flow_dump_next
,
4209 dpif_netdev_operate
,
4210 NULL
, /* recv_set */
4211 NULL
, /* handlers_set */
4212 dpif_netdev_pmd_set
,
4213 dpif_netdev_queue_to_priority
,
4215 NULL
, /* recv_wait */
4216 NULL
, /* recv_purge */
4217 dpif_netdev_register_dp_purge_cb
,
4218 dpif_netdev_register_upcall_cb
,
4219 dpif_netdev_enable_upcall
,
4220 dpif_netdev_disable_upcall
,
4221 dpif_netdev_get_datapath_version
,
4222 NULL
, /* ct_dump_start */
4223 NULL
, /* ct_dump_next */
4224 NULL
, /* ct_dump_done */
4225 NULL
, /* ct_flush */
4229 dpif_dummy_change_port_number(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
4230 const char *argv
[], void *aux OVS_UNUSED
)
4232 struct dp_netdev_port
*port
;
4233 struct dp_netdev
*dp
;
4236 ovs_mutex_lock(&dp_netdev_mutex
);
4237 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
4238 if (!dp
|| !dpif_netdev_class_is_dummy(dp
->class)) {
4239 ovs_mutex_unlock(&dp_netdev_mutex
);
4240 unixctl_command_reply_error(conn
, "unknown datapath or not a dummy");
4243 ovs_refcount_ref(&dp
->ref_cnt
);
4244 ovs_mutex_unlock(&dp_netdev_mutex
);
4246 ovs_mutex_lock(&dp
->port_mutex
);
4247 if (get_port_by_name(dp
, argv
[2], &port
)) {
4248 unixctl_command_reply_error(conn
, "unknown port");
4252 port_no
= u32_to_odp(atoi(argv
[3]));
4253 if (!port_no
|| port_no
== ODPP_NONE
) {
4254 unixctl_command_reply_error(conn
, "bad port number");
4257 if (dp_netdev_lookup_port(dp
, port_no
)) {
4258 unixctl_command_reply_error(conn
, "port number already in use");
4263 hmap_remove(&dp
->ports
, &port
->node
);
4264 dp_netdev_del_port_from_all_pmds(dp
, port
);
4266 /* Reinsert with new port number. */
4267 port
->port_no
= port_no
;
4268 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
4269 dp_netdev_add_port_to_pmds(dp
, port
);
4271 seq_change(dp
->port_seq
);
4272 unixctl_command_reply(conn
, NULL
);
4275 ovs_mutex_unlock(&dp
->port_mutex
);
4276 dp_netdev_unref(dp
);
4280 dpif_dummy_register__(const char *type
)
4282 struct dpif_class
*class;
4284 class = xmalloc(sizeof *class);
4285 *class = dpif_netdev_class
;
4286 class->type
= xstrdup(type
);
4287 dp_register_provider(class);
4291 dpif_dummy_override(const char *type
)
4296 * Ignore EAFNOSUPPORT to allow --enable-dummy=system with
4297 * a userland-only build. It's useful for testsuite.
4299 error
= dp_unregister_provider(type
);
4300 if (error
== 0 || error
== EAFNOSUPPORT
) {
4301 dpif_dummy_register__(type
);
4306 dpif_dummy_register(enum dummy_level level
)
4308 if (level
== DUMMY_OVERRIDE_ALL
) {
4313 dp_enumerate_types(&types
);
4314 SSET_FOR_EACH (type
, &types
) {
4315 dpif_dummy_override(type
);
4317 sset_destroy(&types
);
4318 } else if (level
== DUMMY_OVERRIDE_SYSTEM
) {
4319 dpif_dummy_override("system");
4322 dpif_dummy_register__("dummy");
4324 unixctl_command_register("dpif-dummy/change-port-number",
4325 "dp port new-number",
4326 3, 3, dpif_dummy_change_port_number
, NULL
);
4329 /* Datapath Classifier. */
4331 /* A set of rules that all have the same fields wildcarded. */
4332 struct dpcls_subtable
{
4333 /* The fields are only used by writers. */
4334 struct cmap_node cmap_node OVS_GUARDED
; /* Within dpcls 'subtables_map'. */
4336 /* These fields are accessed by readers. */
4337 struct cmap rules
; /* Contains "struct dpcls_rule"s. */
4338 struct netdev_flow_key mask
; /* Wildcards for fields (const). */
4339 /* 'mask' must be the last field, additional space is allocated here. */
4342 /* Initializes 'cls' as a classifier that initially contains no classification
4345 dpcls_init(struct dpcls
*cls
)
4347 cmap_init(&cls
->subtables_map
);
4348 pvector_init(&cls
->subtables
);
4352 dpcls_destroy_subtable(struct dpcls
*cls
, struct dpcls_subtable
*subtable
)
4354 pvector_remove(&cls
->subtables
, subtable
);
4355 cmap_remove(&cls
->subtables_map
, &subtable
->cmap_node
,
4356 subtable
->mask
.hash
);
4357 cmap_destroy(&subtable
->rules
);
4358 ovsrcu_postpone(free
, subtable
);
4361 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
4362 * caller's responsibility.
4363 * May only be called after all the readers have been terminated. */
4365 dpcls_destroy(struct dpcls
*cls
)
4368 struct dpcls_subtable
*subtable
;
4370 CMAP_FOR_EACH (subtable
, cmap_node
, &cls
->subtables_map
) {
4371 ovs_assert(cmap_count(&subtable
->rules
) == 0);
4372 dpcls_destroy_subtable(cls
, subtable
);
4374 cmap_destroy(&cls
->subtables_map
);
4375 pvector_destroy(&cls
->subtables
);
4379 static struct dpcls_subtable
*
4380 dpcls_create_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
4382 struct dpcls_subtable
*subtable
;
4384 /* Need to add one. */
4385 subtable
= xmalloc(sizeof *subtable
4386 - sizeof subtable
->mask
.mf
+ mask
->len
);
4387 cmap_init(&subtable
->rules
);
4388 netdev_flow_key_clone(&subtable
->mask
, mask
);
4389 cmap_insert(&cls
->subtables_map
, &subtable
->cmap_node
, mask
->hash
);
4390 pvector_insert(&cls
->subtables
, subtable
, 0);
4391 pvector_publish(&cls
->subtables
);
4396 static inline struct dpcls_subtable
*
4397 dpcls_find_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
4399 struct dpcls_subtable
*subtable
;
4401 CMAP_FOR_EACH_WITH_HASH (subtable
, cmap_node
, mask
->hash
,
4402 &cls
->subtables_map
) {
4403 if (netdev_flow_key_equal(&subtable
->mask
, mask
)) {
4407 return dpcls_create_subtable(cls
, mask
);
4410 /* Insert 'rule' into 'cls'. */
4412 dpcls_insert(struct dpcls
*cls
, struct dpcls_rule
*rule
,
4413 const struct netdev_flow_key
*mask
)
4415 struct dpcls_subtable
*subtable
= dpcls_find_subtable(cls
, mask
);
4417 rule
->mask
= &subtable
->mask
;
4418 cmap_insert(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
);
4421 /* Removes 'rule' from 'cls', also destructing the 'rule'. */
4423 dpcls_remove(struct dpcls
*cls
, struct dpcls_rule
*rule
)
4425 struct dpcls_subtable
*subtable
;
4427 ovs_assert(rule
->mask
);
4429 INIT_CONTAINER(subtable
, rule
->mask
, mask
);
4431 if (cmap_remove(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
)
4433 dpcls_destroy_subtable(cls
, subtable
);
4434 pvector_publish(&cls
->subtables
);
4438 /* Returns true if 'target' satisfies 'key' in 'mask', that is, if each 1-bit
4439 * in 'mask' the values in 'key' and 'target' are the same. */
4441 dpcls_rule_matches_key(const struct dpcls_rule
*rule
,
4442 const struct netdev_flow_key
*target
)
4444 const uint64_t *keyp
= miniflow_get_values(&rule
->flow
.mf
);
4445 const uint64_t *maskp
= miniflow_get_values(&rule
->mask
->mf
);
4448 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, target
, rule
->flow
.mf
.map
) {
4449 if (OVS_UNLIKELY((value
& *maskp
++) != *keyp
++)) {
4456 /* For each miniflow in 'flows' performs a classifier lookup writing the result
4457 * into the corresponding slot in 'rules'. If a particular entry in 'flows' is
4458 * NULL it is skipped.
4460 * This function is optimized for use in the userspace datapath and therefore
4461 * does not implement a lot of features available in the standard
4462 * classifier_lookup() function. Specifically, it does not implement
4463 * priorities, instead returning any rule which matches the flow.
4465 * Returns true if all flows found a corresponding rule. */
4467 dpcls_lookup(const struct dpcls
*cls
, const struct netdev_flow_key keys
[],
4468 struct dpcls_rule
**rules
, const size_t cnt
)
4470 /* The batch size 16 was experimentally found faster than 8 or 32. */
4471 typedef uint16_t map_type
;
4472 #define MAP_BITS (sizeof(map_type) * CHAR_BIT)
4474 #if !defined(__CHECKER__) && !defined(_WIN32)
4475 const int N_MAPS
= DIV_ROUND_UP(cnt
, MAP_BITS
);
4477 enum { N_MAPS
= DIV_ROUND_UP(NETDEV_MAX_BURST
, MAP_BITS
) };
4479 map_type maps
[N_MAPS
];
4480 struct dpcls_subtable
*subtable
;
4482 memset(maps
, 0xff, sizeof maps
);
4483 if (cnt
% MAP_BITS
) {
4484 maps
[N_MAPS
- 1] >>= MAP_BITS
- cnt
% MAP_BITS
; /* Clear extra bits. */
4486 memset(rules
, 0, cnt
* sizeof *rules
);
4488 PVECTOR_FOR_EACH (subtable
, &cls
->subtables
) {
4489 const struct netdev_flow_key
*mkeys
= keys
;
4490 struct dpcls_rule
**mrules
= rules
;
4491 map_type remains
= 0;
4494 BUILD_ASSERT_DECL(sizeof remains
== sizeof *maps
);
4496 for (m
= 0; m
< N_MAPS
; m
++, mkeys
+= MAP_BITS
, mrules
+= MAP_BITS
) {
4497 uint32_t hashes
[MAP_BITS
];
4498 const struct cmap_node
*nodes
[MAP_BITS
];
4499 unsigned long map
= maps
[m
];
4503 continue; /* Skip empty maps. */
4506 /* Compute hashes for the remaining keys. */
4507 ULLONG_FOR_EACH_1(i
, map
) {
4508 hashes
[i
] = netdev_flow_key_hash_in_mask(&mkeys
[i
],
4512 map
= cmap_find_batch(&subtable
->rules
, map
, hashes
, nodes
);
4513 /* Check results. */
4514 ULLONG_FOR_EACH_1(i
, map
) {
4515 struct dpcls_rule
*rule
;
4517 CMAP_NODE_FOR_EACH (rule
, cmap_node
, nodes
[i
]) {
4518 if (OVS_LIKELY(dpcls_rule_matches_key(rule
, &mkeys
[i
]))) {
4523 ULLONG_SET0(map
, i
); /* Did not match. */
4525 ; /* Keep Sparse happy. */
4527 maps
[m
] &= ~map
; /* Clear the found rules. */
4531 return true; /* All found. */
4534 return false; /* Some misses. */