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. */
446 /* Queue id used by this pmd thread to send packets on all netdevs.
447 * All tx_qid's are unique and less than 'ovs_numa_get_n_cores() + 1'. */
450 struct ovs_mutex port_mutex; /* Mutex for 'poll_list' and 'tx_ports'. */
451 /* List of rx queues to poll. */
452 struct ovs_list poll_list OVS_GUARDED;
453 /* Number of elements in 'poll_list' */
455 /* Map of 'tx_port's used for transmission. Written by the main thread,
456 * read by the pmd thread. */
457 struct hmap tx_ports OVS_GUARDED;
459 /* Map of 'tx_port' used in the fast path. This is a thread-local copy of
460 * 'tx_ports'. The instance for cpu core NON_PMD_CORE_ID can be accessed
461 * by multiple threads, and thusly need to be protected by 'non_pmd_mutex'.
462 * Every other instance will only be accessed by its own pmd thread. */
463 struct hmap port_cache;
465 /* Only a pmd thread can write on its own 'cycles' and 'stats'.
466 * The main thread keeps 'stats_zero' and 'cycles_zero' as base
467 * values and subtracts them from 'stats' and 'cycles' before
468 * reporting to the user */
469 unsigned long long stats_zero[DP_N_STATS];
470 uint64_t cycles_zero[PMD_N_CYCLES];
473 #define PMD_INITIAL_SEQ 1
475 /* Interface to netdev-based datapath. */
478 struct dp_netdev *dp;
479 uint64_t last_port_seq;
482 static int get_port_by_number(struct dp_netdev *dp, odp_port_t port_no,
483 struct dp_netdev_port **portp)
484 OVS_REQUIRES(dp->port_mutex);
485 static int get_port_by_name(struct dp_netdev *dp, const char *devname,
486 struct dp_netdev_port **portp)
487 OVS_REQUIRES(dp->port_mutex);
488 static void dp_netdev_free(struct dp_netdev *)
489 OVS_REQUIRES(dp_netdev_mutex);
490 static int do_add_port(struct dp_netdev *dp, const char *devname,
491 const char *type, odp_port_t port_no)
492 OVS_REQUIRES(dp->port_mutex);
493 static void do_del_port(struct dp_netdev *dp, struct dp_netdev_port *)
494 OVS_REQUIRES(dp->port_mutex);
495 static int dpif_netdev_open(const struct dpif_class *, const char *name,
496 bool create, struct dpif **);
497 static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread *pmd,
498 struct dp_packet_batch *,
500 const struct nlattr *actions,
502 static void dp_netdev_input(struct dp_netdev_pmd_thread *,
503 struct dp_packet_batch *, odp_port_t port_no);
504 static void dp_netdev_recirculate(struct dp_netdev_pmd_thread *,
505 struct dp_packet_batch *);
507 static void dp_netdev_disable_upcall(struct dp_netdev *);
508 static void dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread *pmd);
509 static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread *pmd,
510 struct dp_netdev *dp, unsigned core_id,
512 static void dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread *pmd);
513 static void dp_netdev_set_nonpmd(struct dp_netdev *dp)
514 OVS_REQUIRES(dp->port_mutex);
516 static struct dp_netdev_pmd_thread *dp_netdev_get_pmd(struct dp_netdev *dp,
518 static struct dp_netdev_pmd_thread *
519 dp_netdev_pmd_get_next(struct dp_netdev *dp, struct cmap_position *pos);
520 static void dp_netdev_destroy_all_pmds(struct dp_netdev *dp);
521 static void dp_netdev_del_pmds_on_numa(struct dp_netdev *dp, int numa_id);
522 static void dp_netdev_set_pmds_on_numa(struct dp_netdev *dp, int numa_id)
523 OVS_REQUIRES(dp->port_mutex);
524 static void dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread *pmd);
525 static void dp_netdev_del_port_from_all_pmds(struct dp_netdev *dp,
526 struct dp_netdev_port *port);
527 static void dp_netdev_add_port_to_pmds(struct dp_netdev *dp,
528 struct dp_netdev_port *port);
529 static void dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread *pmd,
530 struct dp_netdev_port *port);
531 static void dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread *pmd,
532 struct dp_netdev_port *port,
533 struct netdev_rxq *rx);
534 static struct dp_netdev_pmd_thread *
535 dp_netdev_less_loaded_pmd_on_numa(struct dp_netdev *dp, int numa_id);
536 static void dp_netdev_reset_pmd_threads(struct dp_netdev *dp)
537 OVS_REQUIRES(dp->port_mutex);
538 static bool dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread *pmd);
539 static void dp_netdev_pmd_unref(struct dp_netdev_pmd_thread *pmd);
540 static void dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread *pmd);
541 static void pmd_load_cached_ports(struct dp_netdev_pmd_thread *pmd)
542 OVS_REQUIRES(pmd->port_mutex);
544 static inline bool emc_entry_alive(struct emc_entry *ce);
545 static void emc_clear_entry(struct emc_entry *ce);
548 emc_cache_init(struct emc_cache *flow_cache)
552 flow_cache->sweep_idx = 0;
553 for (i = 0; i < ARRAY_SIZE(flow_cache->entries); i++) {
554 flow_cache->entries[i].flow = NULL;
555 flow_cache->entries[i].key.hash = 0;
556 flow_cache->entries[i].key.len = sizeof(struct miniflow);
557 flowmap_init(&flow_cache->entries[i].key.mf.map);
562 emc_cache_uninit(struct emc_cache *flow_cache)
566 for (i = 0; i < ARRAY_SIZE(flow_cache->entries); i++) {
567 emc_clear_entry(&flow_cache->entries[i]);
571 /* Check and clear dead flow references slowly (one entry at each
574 emc_cache_slow_sweep(struct emc_cache *flow_cache)
576 struct emc_entry *entry = &flow_cache->entries[flow_cache->sweep_idx];
578 if (!emc_entry_alive(entry)) {
579 emc_clear_entry(entry);
581 flow_cache->sweep_idx = (flow_cache->sweep_idx + 1) & EM_FLOW_HASH_MASK;
584 /* Returns true if 'dpif' is a netdev or dummy dpif, false otherwise. */
586 dpif_is_netdev(const struct dpif *dpif)
588 return dpif->dpif_class->open == dpif_netdev_open;
591 static struct dpif_netdev *
592 dpif_netdev_cast(const struct dpif *dpif)
594 ovs_assert(dpif_is_netdev(dpif));
595 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
598 static struct dp_netdev *
599 get_dp_netdev(const struct dpif *dpif)
601 return dpif_netdev_cast(dpif)->dp;
605 PMD_INFO_SHOW_STATS, /* Show how cpu cycles are spent. */
606 PMD_INFO_CLEAR_STATS, /* Set the cycles count to 0. */
607 PMD_INFO_SHOW_RXQ /* Show poll-lists of pmd threads. */
611 pmd_info_show_stats(struct ds *reply,
612 struct dp_netdev_pmd_thread *pmd,
613 unsigned long long stats[DP_N_STATS],
614 uint64_t cycles[PMD_N_CYCLES])
616 unsigned long long total_packets = 0;
617 uint64_t total_cycles = 0;
620 /* These loops subtracts reference values ('*_zero') from the counters.
621 * Since loads and stores are relaxed, it might be possible for a '*_zero'
622 * value to be more recent than the current value we're reading from the
623 * counter. This is not a big problem, since these numbers are not
624 * supposed to be too accurate, but we should at least make sure that
625 * the result is not negative. */
626 for (i = 0; i < DP_N_STATS; i++) {
627 if (stats[i] > pmd->stats_zero[i]) {
628 stats[i] -= pmd->stats_zero[i];
633 if (i != DP_STAT_LOST) {
634 /* Lost packets are already included in DP_STAT_MISS */
635 total_packets += stats[i];
639 for (i = 0; i < PMD_N_CYCLES; i++) {
640 if (cycles[i] > pmd->cycles_zero[i]) {
641 cycles[i] -= pmd->cycles_zero[i];
646 total_cycles += cycles[i];
649 ds_put_cstr(reply, (pmd->core_id == NON_PMD_CORE_ID)
650 ? "main thread" : "pmd thread");
652 if (pmd->numa_id != OVS_NUMA_UNSPEC) {
653 ds_put_format(reply, " numa_id %d", pmd->numa_id);
655 if (pmd->core_id != OVS_CORE_UNSPEC && pmd->core_id != NON_PMD_CORE_ID) {
656 ds_put_format(reply, " core_id %u", pmd->core_id);
658 ds_put_cstr(reply, ":\n");
661 "\temc hits:%llu\n\tmegaflow hits:%llu\n"
662 "\tmiss:%llu\n\tlost:%llu\n",
663 stats[DP_STAT_EXACT_HIT], stats[DP_STAT_MASKED_HIT],
664 stats[DP_STAT_MISS], stats[DP_STAT_LOST]);
666 if (total_cycles == 0) {
671 "\tpolling cycles:%"PRIu64" (%.02f%%)\n"
672 "\tprocessing cycles:%"PRIu64" (%.02f%%)\n",
673 cycles[PMD_CYCLES_POLLING],
674 cycles[PMD_CYCLES_POLLING] / (double)total_cycles * 100,
675 cycles[PMD_CYCLES_PROCESSING],
676 cycles[PMD_CYCLES_PROCESSING] / (double)total_cycles * 100);
678 if (total_packets == 0) {
683 "\tavg cycles per packet: %.02f (%"PRIu64"/%llu)\n",
684 total_cycles / (double)total_packets,
685 total_cycles, total_packets);
688 "\tavg processing cycles per packet: "
689 "%.02f (%"PRIu64"/%llu)\n",
690 cycles[PMD_CYCLES_PROCESSING] / (double)total_packets,
691 cycles[PMD_CYCLES_PROCESSING], total_packets);
695 pmd_info_clear_stats(struct ds *reply OVS_UNUSED,
696 struct dp_netdev_pmd_thread *pmd,
697 unsigned long long stats[DP_N_STATS],
698 uint64_t cycles[PMD_N_CYCLES])
702 /* We cannot write 'stats' and 'cycles' (because they're written by other
703 * threads) and we shouldn't change 'stats' (because they're used to count
704 * datapath stats, which must not be cleared here). Instead, we save the
705 * current values and subtract them from the values to be displayed in the
707 for (i = 0; i < DP_N_STATS; i++) {
708 pmd->stats_zero[i] = stats[i];
710 for (i = 0; i < PMD_N_CYCLES; i++) {
711 pmd->cycles_zero[i] = cycles[i];
716 pmd_info_show_rxq(struct ds *reply, struct dp_netdev_pmd_thread *pmd)
718 if (pmd->core_id != NON_PMD_CORE_ID) {
719 struct rxq_poll *poll;
720 const char *prev_name = NULL;
722 ds_put_format(reply, "pmd thread numa_id %d core_id %u:\n",
723 pmd->numa_id, pmd->core_id);
725 ovs_mutex_lock(&pmd->port_mutex);
726 LIST_FOR_EACH (poll, node, &pmd->poll_list) {
727 const char *name = netdev_get_name(poll->port->netdev);
729 if (!prev_name || strcmp(name, prev_name)) {
731 ds_put_cstr(reply, "\n");
733 ds_put_format(reply, "\tport: %s\tqueue-id:",
734 netdev_get_name(poll->port->netdev));
736 ds_put_format(reply, " %d", netdev_rxq_get_queue_id(poll->rx));
739 ovs_mutex_unlock(&pmd->port_mutex);
740 ds_put_cstr(reply, "\n");
745 dpif_netdev_pmd_info(struct unixctl_conn *conn, int argc, const char *argv[],
748 struct ds reply = DS_EMPTY_INITIALIZER;
749 struct dp_netdev_pmd_thread *pmd;
750 struct dp_netdev *dp = NULL;
751 enum pmd_info_type type = *(enum pmd_info_type *) aux;
753 ovs_mutex_lock(&dp_netdev_mutex);
756 dp = shash_find_data(&dp_netdevs, argv[1]);
757 } else if (shash_count(&dp_netdevs) == 1) {
758 /* There's only one datapath */
759 dp = shash_first(&dp_netdevs)->data;
763 ovs_mutex_unlock(&dp_netdev_mutex);
764 unixctl_command_reply_error(conn,
765 "please specify an existing datapath");
769 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
770 if (type == PMD_INFO_SHOW_RXQ) {
771 pmd_info_show_rxq(&reply, pmd);
773 unsigned long long stats[DP_N_STATS];
774 uint64_t cycles[PMD_N_CYCLES];
777 /* Read current stats and cycle counters */
778 for (i = 0; i < ARRAY_SIZE(stats); i++) {
779 atomic_read_relaxed(&pmd->stats.n[i], &stats[i]);
781 for (i = 0; i < ARRAY_SIZE(cycles); i++) {
782 atomic_read_relaxed(&pmd->cycles.n[i], &cycles[i]);
785 if (type == PMD_INFO_CLEAR_STATS) {
786 pmd_info_clear_stats(&reply, pmd, stats, cycles);
787 } else if (type == PMD_INFO_SHOW_STATS) {
788 pmd_info_show_stats(&reply, pmd, stats, cycles);
793 ovs_mutex_unlock(&dp_netdev_mutex);
795 unixctl_command_reply(conn, ds_cstr(&reply));
800 dpif_netdev_init(void)
802 static enum pmd_info_type show_aux = PMD_INFO_SHOW_STATS,
803 clear_aux = PMD_INFO_CLEAR_STATS,
804 poll_aux = PMD_INFO_SHOW_RXQ;
806 unixctl_command_register("dpif-netdev/pmd-stats-show", "[dp]",
807 0, 1, dpif_netdev_pmd_info,
809 unixctl_command_register("dpif-netdev/pmd-stats-clear", "[dp]",
810 0, 1, dpif_netdev_pmd_info,
812 unixctl_command_register("dpif-netdev/pmd-rxq-show", "[dp]",
813 0, 1, dpif_netdev_pmd_info,
819 dpif_netdev_enumerate(struct sset *all_dps,
820 const struct dpif_class *dpif_class)
822 struct shash_node *node;
824 ovs_mutex_lock(&dp_netdev_mutex);
825 SHASH_FOR_EACH(node, &dp_netdevs) {
826 struct dp_netdev *dp = node->data;
827 if (dpif_class != dp->class) {
828 /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs.
829 * If the class doesn't match, skip this dpif. */
832 sset_add(all_dps, node->name);
834 ovs_mutex_unlock(&dp_netdev_mutex);
840 dpif_netdev_class_is_dummy(const struct dpif_class *class)
842 return class != &dpif_netdev_class;
846 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
848 return strcmp(type, "internal") ? type
849 : dpif_netdev_class_is_dummy(class) ? "dummy"
854 create_dpif_netdev(struct dp_netdev *dp)
856 uint16_t netflow_id = hash_string(dp->name, 0);
857 struct dpif_netdev *dpif;
859 ovs_refcount_ref(&dp->ref_cnt);
861 dpif = xmalloc(sizeof *dpif);
862 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
864 dpif->last_port_seq = seq_read(dp->port_seq);
869 /* Choose an unused, non-zero port number and return it on success.
870 * Return ODPP_NONE on failure. */
872 choose_port(struct dp_netdev *dp, const char *name)
873 OVS_REQUIRES(dp->port_mutex)
877 if (dp->class != &dpif_netdev_class) {
881 /* If the port name begins with "br", start the number search at
882 * 100 to make writing tests easier. */
883 if (!strncmp(name, "br", 2)) {
887 /* If the port name contains a number, try to assign that port number.
888 * This can make writing unit tests easier because port numbers are
890 for (p = name; *p != '\0'; p++) {
891 if (isdigit((unsigned char) *p)) {
892 port_no = start_no + strtol(p, NULL, 10);
893 if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE)
894 && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
895 return u32_to_odp(port_no);
902 for (port_no = 1; port_no <= UINT16_MAX; port_no++) {
903 if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
904 return u32_to_odp(port_no);
912 create_dp_netdev(const char *name, const struct dpif_class *class,
913 struct dp_netdev **dpp)
914 OVS_REQUIRES(dp_netdev_mutex)
916 struct dp_netdev *dp;
919 dp = xzalloc(sizeof *dp);
920 shash_add(&dp_netdevs, name, dp);
922 *CONST_CAST(const struct dpif_class **, &dp->class) = class;
923 *CONST_CAST(const char **, &dp->name) = xstrdup(name);
924 ovs_refcount_init(&dp->ref_cnt);
925 atomic_flag_clear(&dp->destroyed);
927 ovs_mutex_init(&dp->port_mutex);
928 hmap_init(&dp->ports);
929 dp->port_seq = seq_create();
930 fat_rwlock_init(&dp->upcall_rwlock);
932 /* Disable upcalls by default. */
933 dp_netdev_disable_upcall(dp);
934 dp->upcall_aux = NULL;
935 dp->upcall_cb = NULL;
937 cmap_init(&dp->poll_threads);
938 ovs_mutex_init_recursive(&dp->non_pmd_mutex);
939 ovsthread_key_create(&dp->per_pmd_key, NULL);
941 ovs_mutex_lock(&dp->port_mutex);
942 dp_netdev_set_nonpmd(dp);
944 error = do_add_port(dp, name, "internal", ODPP_LOCAL);
945 ovs_mutex_unlock(&dp->port_mutex);
951 dp->last_tnl_conf_seq = seq_read(tnl_conf_seq);
957 dpif_netdev_open(const struct dpif_class *class, const char *name,
958 bool create, struct dpif **dpifp)
960 struct dp_netdev *dp;
963 ovs_mutex_lock(&dp_netdev_mutex);
964 dp = shash_find_data(&dp_netdevs, name);
966 error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
968 error = (dp->class != class ? EINVAL
973 *dpifp = create_dpif_netdev(dp);
976 ovs_mutex_unlock(&dp_netdev_mutex);
982 dp_netdev_destroy_upcall_lock(struct dp_netdev *dp)
983 OVS_NO_THREAD_SAFETY_ANALYSIS
985 /* Check that upcalls are disabled, i.e. that the rwlock is taken */
986 ovs_assert(fat_rwlock_tryrdlock(&dp->upcall_rwlock));
988 /* Before freeing a lock we should release it */
989 fat_rwlock_unlock(&dp->upcall_rwlock);
990 fat_rwlock_destroy(&dp->upcall_rwlock);
993 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
994 * through the 'dp_netdevs' shash while freeing 'dp'. */
996 dp_netdev_free(struct dp_netdev *dp)
997 OVS_REQUIRES(dp_netdev_mutex)
999 struct dp_netdev_port *port, *next;
1001 shash_find_and_delete(&dp_netdevs, dp->name);
1003 dp_netdev_destroy_all_pmds(dp);
1004 ovs_mutex_destroy(&dp->non_pmd_mutex);
1005 ovsthread_key_delete(dp->per_pmd_key);
1007 ovs_mutex_lock(&dp->port_mutex);
1008 HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) {
1009 do_del_port(dp, port);
1011 ovs_mutex_unlock(&dp->port_mutex);
1012 cmap_destroy(&dp->poll_threads);
1014 seq_destroy(dp->port_seq);
1015 hmap_destroy(&dp->ports);
1016 ovs_mutex_destroy(&dp->port_mutex);
1018 /* Upcalls must be disabled at this point */
1019 dp_netdev_destroy_upcall_lock(dp);
1021 free(dp->pmd_cmask);
1022 free(CONST_CAST(char *, dp->name));
1027 dp_netdev_unref(struct dp_netdev *dp)
1030 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
1031 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
1032 ovs_mutex_lock(&dp_netdev_mutex);
1033 if (ovs_refcount_unref_relaxed(&dp->ref_cnt) == 1) {
1036 ovs_mutex_unlock(&dp_netdev_mutex);
1041 dpif_netdev_close(struct dpif *dpif)
1043 struct dp_netdev *dp = get_dp_netdev(dpif);
1045 dp_netdev_unref(dp);
1050 dpif_netdev_destroy(struct dpif *dpif)
1052 struct dp_netdev *dp = get_dp_netdev(dpif);
1054 if (!atomic_flag_test_and_set(&dp->destroyed)) {
1055 if (ovs_refcount_unref_relaxed(&dp->ref_cnt) == 1) {
1056 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
1064 /* Add 'n' to the atomic variable 'var' non-atomically and using relaxed
1065 * load/store semantics. While the increment is not atomic, the load and
1066 * store operations are, making it impossible to read inconsistent values.
1068 * This is used to update thread local stats counters. */
1070 non_atomic_ullong_add(atomic_ullong *var, unsigned long long n)
1072 unsigned long long tmp;
1074 atomic_read_relaxed(var, &tmp);
1076 atomic_store_relaxed(var, tmp);
1080 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
1082 struct dp_netdev *dp = get_dp_netdev(dpif);
1083 struct dp_netdev_pmd_thread *pmd;
1085 stats->n_flows = stats->n_hit = stats->n_missed = stats->n_lost = 0;
1086 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
1087 unsigned long long n;
1088 stats->n_flows += cmap_count(&pmd->flow_table);
1090 atomic_read_relaxed(&pmd->stats.n[DP_STAT_MASKED_HIT], &n);
1092 atomic_read_relaxed(&pmd->stats.n[DP_STAT_EXACT_HIT], &n);
1094 atomic_read_relaxed(&pmd->stats.n[DP_STAT_MISS], &n);
1095 stats->n_missed += n;
1096 atomic_read_relaxed(&pmd->stats.n[DP_STAT_LOST], &n);
1099 stats->n_masks = UINT32_MAX;
1100 stats->n_mask_hit = UINT64_MAX;
1106 dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread *pmd)
1110 if (pmd->core_id == NON_PMD_CORE_ID) {
1111 ovs_mutex_lock(&pmd->dp->non_pmd_mutex);
1112 ovs_mutex_lock(&pmd->port_mutex);
1113 pmd_load_cached_ports(pmd);
1114 ovs_mutex_unlock(&pmd->port_mutex);
1115 ovs_mutex_unlock(&pmd->dp->non_pmd_mutex);
1119 ovs_mutex_lock(&pmd->cond_mutex);
1120 atomic_add_relaxed(&pmd->change_seq, 1, &old_seq);
1121 ovs_mutex_cond_wait(&pmd->cond, &pmd->cond_mutex);
1122 ovs_mutex_unlock(&pmd->cond_mutex);
1126 hash_port_no(odp_port_t port_no)
1128 return hash_int(odp_to_u32(port_no), 0);
1132 port_create(const char *devname, const char *open_type, const char *type,
1133 odp_port_t port_no, struct dp_netdev_port **portp)
1135 struct netdev_saved_flags *sf;
1136 struct dp_netdev_port *port;
1137 enum netdev_flags flags;
1138 struct netdev *netdev;
1139 int n_open_rxqs = 0;
1144 /* Open and validate network device. */
1145 error = netdev_open(devname, open_type, &netdev);
1149 /* XXX reject non-Ethernet devices */
1151 netdev_get_flags(netdev, &flags);
1152 if (flags & NETDEV_LOOPBACK) {
1153 VLOG_ERR("%s: cannot add a loopback device", devname);
1158 if (netdev_is_pmd(netdev)) {
1159 int n_cores = ovs_numa_get_n_cores();
1161 if (n_cores == OVS_CORE_UNSPEC) {
1162 VLOG_ERR("%s, cannot get cpu core info", devname);
1166 /* There can only be ovs_numa_get_n_cores() pmd threads,
1167 * so creates a txq for each, and one extra for the non
1169 error = netdev_set_tx_multiq(netdev, n_cores + 1);
1170 if (error && (error != EOPNOTSUPP)) {
1171 VLOG_ERR("%s, cannot set multiq", devname);
1176 if (netdev_is_reconf_required(netdev)) {
1177 error = netdev_reconfigure(netdev);
1183 port = xzalloc(sizeof *port);
1184 port->port_no = port_no;
1185 port->netdev = netdev;
1186 port->n_rxq = netdev_n_rxq(netdev);
1187 port->rxq = xcalloc(port->n_rxq, sizeof *port->rxq);
1188 port->type = xstrdup(type);
1190 for (i = 0; i < port->n_rxq; i++) {
1191 error = netdev_rxq_open(netdev, &port->rxq[i], i);
1193 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
1194 devname, ovs_strerror(errno));
1200 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
1211 for (i = 0; i < n_open_rxqs; i++) {
1212 netdev_rxq_close(port->rxq[i]);
1219 netdev_close(netdev);
1224 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
1226 OVS_REQUIRES(dp->port_mutex)
1228 struct dp_netdev_port *port;
1231 /* Reject devices already in 'dp'. */
1232 if (!get_port_by_name(dp, devname, &port)) {
1236 error = port_create(devname, dpif_netdev_port_open_type(dp->class, type),
1237 type, port_no, &port);
1242 if (netdev_is_pmd(port->netdev)) {
1243 int numa_id = netdev_get_numa_id(port->netdev);
1245 ovs_assert(ovs_numa_numa_id_is_valid(numa_id));
1246 dp_netdev_set_pmds_on_numa(dp, numa_id);
1249 dp_netdev_add_port_to_pmds(dp, port);
1251 hmap_insert(&dp->ports, &port->node, hash_port_no(port_no));
1252 seq_change(dp->port_seq);
1258 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
1259 odp_port_t *port_nop)
1261 struct dp_netdev *dp = get_dp_netdev(dpif);
1262 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1263 const char *dpif_port;
1267 ovs_mutex_lock(&dp->port_mutex);
1268 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1269 if (*port_nop != ODPP_NONE) {
1270 port_no = *port_nop;
1271 error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0;
1273 port_no = choose_port(dp, dpif_port);
1274 error = port_no == ODPP_NONE ? EFBIG : 0;
1277 *port_nop = port_no;
1278 error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
1280 ovs_mutex_unlock(&dp->port_mutex);
1286 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
1288 struct dp_netdev *dp = get_dp_netdev(dpif);
1291 ovs_mutex_lock(&dp->port_mutex);
1292 if (port_no == ODPP_LOCAL) {
1295 struct dp_netdev_port *port;
1297 error = get_port_by_number(dp, port_no, &port);
1299 do_del_port(dp, port);
1302 ovs_mutex_unlock(&dp->port_mutex);
1308 is_valid_port_number(odp_port_t port_no)
1310 return port_no != ODPP_NONE;
1313 static struct dp_netdev_port *
1314 dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no)
1315 OVS_REQUIRES(dp->port_mutex)
1317 struct dp_netdev_port *port;
1319 HMAP_FOR_EACH_WITH_HASH (port, node, hash_port_no(port_no), &dp->ports) {
1320 if (port->port_no == port_no) {
1328 get_port_by_number(struct dp_netdev *dp,
1329 odp_port_t port_no, struct dp_netdev_port **portp)
1330 OVS_REQUIRES(dp->port_mutex)
1332 if (!is_valid_port_number(port_no)) {
1336 *portp = dp_netdev_lookup_port(dp, port_no);
1337 return *portp ? 0 : ENOENT;
1342 port_destroy(struct dp_netdev_port *port)
1348 netdev_close(port->netdev);
1349 netdev_restore_flags(port->sf);
1351 for (unsigned i = 0; i < port->n_rxq; i++) {
1352 netdev_rxq_close(port->rxq[i]);
1361 get_port_by_name(struct dp_netdev *dp,
1362 const char *devname, struct dp_netdev_port **portp)
1363 OVS_REQUIRES(dp->port_mutex)
1365 struct dp_netdev_port *port;
1367 HMAP_FOR_EACH (port, node, &dp->ports) {
1368 if (!strcmp(netdev_get_name(port->netdev), devname)) {
1377 get_n_pmd_threads(struct dp_netdev *dp)
1379 /* There is one non pmd thread in dp->poll_threads */
1380 return cmap_count(&dp->poll_threads) - 1;
1384 get_n_pmd_threads_on_numa(struct dp_netdev *dp, int numa_id)
1386 struct dp_netdev_pmd_thread *pmd;
1389 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
1390 if (pmd->numa_id == numa_id) {
1398 /* Returns 'true' if there is a port with pmd netdev and the netdev
1399 * is on numa node 'numa_id'. */
1401 has_pmd_port_for_numa(struct dp_netdev *dp, int numa_id)
1402 OVS_REQUIRES(dp->port_mutex)
1404 struct dp_netdev_port *port;
1406 HMAP_FOR_EACH (port, node, &dp->ports) {
1407 if (netdev_is_pmd(port->netdev)
1408 && netdev_get_numa_id(port->netdev) == numa_id) {
1418 do_del_port(struct dp_netdev *dp, struct dp_netdev_port *port)
1419 OVS_REQUIRES(dp->port_mutex)
1421 hmap_remove(&dp->ports, &port->node);
1422 seq_change(dp->port_seq);
1424 dp_netdev_del_port_from_all_pmds(dp, port);
1426 if (netdev_is_pmd(port->netdev)) {
1427 int numa_id = netdev_get_numa_id(port->netdev);
1429 /* PMD threads can not be on invalid numa node. */
1430 ovs_assert(ovs_numa_numa_id_is_valid(numa_id));
1431 /* If there is no netdev on the numa node, deletes the pmd threads
1433 if (!has_pmd_port_for_numa(dp, numa_id)) {
1434 dp_netdev_del_pmds_on_numa(dp, numa_id);
1442 answer_port_query(const struct dp_netdev_port *port,
1443 struct dpif_port *dpif_port)
1445 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
1446 dpif_port->type = xstrdup(port->type);
1447 dpif_port->port_no = port->port_no;
1451 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
1452 struct dpif_port *dpif_port)
1454 struct dp_netdev *dp = get_dp_netdev(dpif);
1455 struct dp_netdev_port *port;
1458 ovs_mutex_lock(&dp->port_mutex);
1459 error = get_port_by_number(dp, port_no, &port);
1460 if (!error && dpif_port) {
1461 answer_port_query(port, dpif_port);
1463 ovs_mutex_unlock(&dp->port_mutex);
1469 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
1470 struct dpif_port *dpif_port)
1472 struct dp_netdev *dp = get_dp_netdev(dpif);
1473 struct dp_netdev_port *port;
1476 ovs_mutex_lock(&dp->port_mutex);
1477 error = get_port_by_name(dp, devname, &port);
1478 if (!error && dpif_port) {
1479 answer_port_query(port, dpif_port);
1481 ovs_mutex_unlock(&dp->port_mutex);
1487 dp_netdev_flow_free(struct dp_netdev_flow *flow)
1489 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow));
1493 static void dp_netdev_flow_unref(struct dp_netdev_flow *flow)
1495 if (ovs_refcount_unref_relaxed(&flow->ref_cnt) == 1) {
1496 ovsrcu_postpone(dp_netdev_flow_free, flow);
1501 dp_netdev_flow_hash(const ovs_u128 *ufid)
1503 return ufid->u32[0];
1507 dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread *pmd,
1508 struct dp_netdev_flow *flow)
1509 OVS_REQUIRES(pmd->flow_mutex)
1511 struct cmap_node *node = CONST_CAST(struct cmap_node *, &flow->node);
1513 dpcls_remove(&pmd->cls, &flow->cr);
1514 cmap_remove(&pmd->flow_table, node, dp_netdev_flow_hash(&flow->ufid));
1517 dp_netdev_flow_unref(flow);
1521 dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread *pmd)
1523 struct dp_netdev_flow *netdev_flow;
1525 ovs_mutex_lock(&pmd->flow_mutex);
1526 CMAP_FOR_EACH (netdev_flow, node, &pmd->flow_table) {
1527 dp_netdev_pmd_remove_flow(pmd, netdev_flow);
1529 ovs_mutex_unlock(&pmd->flow_mutex);
1533 dpif_netdev_flow_flush(struct dpif *dpif)
1535 struct dp_netdev *dp = get_dp_netdev(dpif);
1536 struct dp_netdev_pmd_thread *pmd;
1538 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
1539 dp_netdev_pmd_flow_flush(pmd);
1545 struct dp_netdev_port_state {
1546 struct hmap_position position;
1551 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
1553 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
1558 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
1559 struct dpif_port *dpif_port)
1561 struct dp_netdev_port_state *state = state_;
1562 struct dp_netdev *dp = get_dp_netdev(dpif);
1563 struct hmap_node *node;
1566 ovs_mutex_lock(&dp->port_mutex);
1567 node = hmap_at_position(&dp->ports, &state->position);
1569 struct dp_netdev_port *port;
1571 port = CONTAINER_OF(node, struct dp_netdev_port, node);
1574 state->name = xstrdup(netdev_get_name(port->netdev));
1575 dpif_port->name = state->name;
1576 dpif_port->type = port->type;
1577 dpif_port->port_no = port->port_no;
1583 ovs_mutex_unlock(&dp->port_mutex);
1589 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1591 struct dp_netdev_port_state *state = state_;
1598 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
1600 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
1601 uint64_t new_port_seq;
1604 new_port_seq = seq_read(dpif->dp->port_seq);
1605 if (dpif->last_port_seq != new_port_seq) {
1606 dpif->last_port_seq = new_port_seq;
1616 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
1618 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
1620 seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
1623 static struct dp_netdev_flow *
1624 dp_netdev_flow_cast(const struct dpcls_rule *cr)
1626 return cr ? CONTAINER_OF(cr, struct dp_netdev_flow, cr) : NULL;
1629 static bool dp_netdev_flow_ref(struct dp_netdev_flow *flow)
1631 return ovs_refcount_try_ref_rcu(&flow->ref_cnt);
1634 /* netdev_flow_key utilities.
1636 * netdev_flow_key is basically a miniflow. We use these functions
1637 * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow
1638 * functions (miniflow_clone_inline, miniflow_equal, ...), because:
1640 * - Since we are dealing exclusively with miniflows created by
1641 * miniflow_extract(), if the map is different the miniflow is different.
1642 * Therefore we can be faster by comparing the map and the miniflow in a
1644 * - These functions can be inlined by the compiler. */
1646 /* Given the number of bits set in miniflow's maps, returns the size of the
1647 * 'netdev_flow_key.mf' */
1648 static inline size_t
1649 netdev_flow_key_size(size_t flow_u64s)
1651 return sizeof(struct miniflow) + MINIFLOW_VALUES_SIZE(flow_u64s);
1655 netdev_flow_key_equal(const struct netdev_flow_key *a,
1656 const struct netdev_flow_key *b)
1658 /* 'b->len' may be not set yet. */
1659 return a->hash == b->hash && !memcmp(&a->mf, &b->mf, a->len);
1662 /* Used to compare 'netdev_flow_key' in the exact match cache to a miniflow.
1663 * The maps are compared bitwise, so both 'key->mf' and 'mf' must have been
1664 * generated by miniflow_extract. */
1666 netdev_flow_key_equal_mf(const struct netdev_flow_key *key,
1667 const struct miniflow *mf)
1669 return !memcmp(&key->mf, mf, key->len);
1673 netdev_flow_key_clone(struct netdev_flow_key *dst,
1674 const struct netdev_flow_key *src)
1677 offsetof(struct netdev_flow_key, mf) + src->len);
1682 netdev_flow_key_from_flow(struct netdev_flow_key *dst,
1683 const struct flow *src)
1685 struct dp_packet packet;
1686 uint64_t buf_stub[512 / 8];
1688 dp_packet_use_stub(&packet, buf_stub, sizeof buf_stub);
1689 pkt_metadata_from_flow(&packet.md, src);
1690 flow_compose(&packet, src);
1691 miniflow_extract(&packet, &dst->mf);
1692 dp_packet_uninit(&packet);
1694 dst->len = netdev_flow_key_size(miniflow_n_values(&dst->mf));
1695 dst->hash = 0; /* Not computed yet. */
1698 /* Initialize a netdev_flow_key 'mask' from 'match'. */
1700 netdev_flow_mask_init(struct netdev_flow_key *mask,
1701 const struct match *match)
1703 uint64_t *dst = miniflow_values(&mask->mf);
1704 struct flowmap fmap;
1708 /* Only check masks that make sense for the flow. */
1709 flow_wc_map(&match->flow, &fmap);
1710 flowmap_init(&mask->mf.map);
1712 FLOWMAP_FOR_EACH_INDEX(idx, fmap) {
1713 uint64_t mask_u64 = flow_u64_value(&match->wc.masks, idx);
1716 flowmap_set(&mask->mf.map, idx, 1);
1718 hash = hash_add64(hash, mask_u64);
1724 FLOWMAP_FOR_EACH_MAP (map, mask->mf.map) {
1725 hash = hash_add64(hash, map);
1728 size_t n = dst - miniflow_get_values(&mask->mf);
1730 mask->hash = hash_finish(hash, n * 8);
1731 mask->len = netdev_flow_key_size(n);
1734 /* Initializes 'dst' as a copy of 'flow' masked with 'mask'. */
1736 netdev_flow_key_init_masked(struct netdev_flow_key *dst,
1737 const struct flow *flow,
1738 const struct netdev_flow_key *mask)
1740 uint64_t *dst_u64 = miniflow_values(&dst->mf);
1741 const uint64_t *mask_u64 = miniflow_get_values(&mask->mf);
1745 dst->len = mask->len;
1746 dst->mf = mask->mf; /* Copy maps. */
1748 FLOW_FOR_EACH_IN_MAPS(value, flow, mask->mf.map) {
1749 *dst_u64 = value & *mask_u64++;
1750 hash = hash_add64(hash, *dst_u64++);
1752 dst->hash = hash_finish(hash,
1753 (dst_u64 - miniflow_get_values(&dst->mf)) * 8);
1756 /* Iterate through netdev_flow_key TNL u64 values specified by 'FLOWMAP'. */
1757 #define NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(VALUE, KEY, FLOWMAP) \
1758 MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, &(KEY)->mf, FLOWMAP)
1760 /* Returns a hash value for the bits of 'key' where there are 1-bits in
1762 static inline uint32_t
1763 netdev_flow_key_hash_in_mask(const struct netdev_flow_key *key,
1764 const struct netdev_flow_key *mask)
1766 const uint64_t *p = miniflow_get_values(&mask->mf);
1770 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value, key, mask->mf.map) {
1771 hash = hash_add64(hash, value & *p++);
1774 return hash_finish(hash, (p - miniflow_get_values(&mask->mf)) * 8);
1778 emc_entry_alive(struct emc_entry *ce)
1780 return ce->flow && !ce->flow->dead;
1784 emc_clear_entry(struct emc_entry *ce)
1787 dp_netdev_flow_unref(ce->flow);
1793 emc_change_entry(struct emc_entry *ce, struct dp_netdev_flow *flow,
1794 const struct netdev_flow_key *key)
1796 if (ce->flow != flow) {
1798 dp_netdev_flow_unref(ce->flow);
1801 if (dp_netdev_flow_ref(flow)) {
1808 netdev_flow_key_clone(&ce->key, key);
1813 emc_insert(struct emc_cache *cache, const struct netdev_flow_key *key,
1814 struct dp_netdev_flow *flow)
1816 struct emc_entry *to_be_replaced = NULL;
1817 struct emc_entry *current_entry;
1819 EMC_FOR_EACH_POS_WITH_HASH(cache, current_entry, key->hash) {
1820 if (netdev_flow_key_equal(¤t_entry->key, key)) {
1821 /* We found the entry with the 'mf' miniflow */
1822 emc_change_entry(current_entry, flow, NULL);
1826 /* Replacement policy: put the flow in an empty (not alive) entry, or
1827 * in the first entry where it can be */
1829 || (emc_entry_alive(to_be_replaced)
1830 && !emc_entry_alive(current_entry))
1831 || current_entry->key.hash < to_be_replaced->key.hash) {
1832 to_be_replaced = current_entry;
1835 /* We didn't find the miniflow in the cache.
1836 * The 'to_be_replaced' entry is where the new flow will be stored */
1838 emc_change_entry(to_be_replaced, flow, key);
1841 static inline struct dp_netdev_flow *
1842 emc_lookup(struct emc_cache *cache, const struct netdev_flow_key *key)
1844 struct emc_entry *current_entry;
1846 EMC_FOR_EACH_POS_WITH_HASH(cache, current_entry, key->hash) {
1847 if (current_entry->key.hash == key->hash
1848 && emc_entry_alive(current_entry)
1849 && netdev_flow_key_equal_mf(¤t_entry->key, &key->mf)) {
1851 /* We found the entry with the 'key->mf' miniflow */
1852 return current_entry->flow;
1859 static struct dp_netdev_flow *
1860 dp_netdev_pmd_lookup_flow(const struct dp_netdev_pmd_thread *pmd,
1861 const struct netdev_flow_key *key)
1863 struct dp_netdev_flow *netdev_flow;
1864 struct dpcls_rule *rule;
1866 dpcls_lookup(&pmd->cls, key, &rule, 1);
1867 netdev_flow = dp_netdev_flow_cast(rule);
1872 static struct dp_netdev_flow *
1873 dp_netdev_pmd_find_flow(const struct dp_netdev_pmd_thread *pmd,
1874 const ovs_u128 *ufidp, const struct nlattr *key,
1877 struct dp_netdev_flow *netdev_flow;
1881 /* If a UFID is not provided, determine one based on the key. */
1882 if (!ufidp && key && key_len
1883 && !dpif_netdev_flow_from_nlattrs(key, key_len, &flow)) {
1884 dpif_flow_hash(pmd->dp->dpif, &flow, sizeof flow, &ufid);
1889 CMAP_FOR_EACH_WITH_HASH (netdev_flow, node, dp_netdev_flow_hash(ufidp),
1891 if (ovs_u128_equals(netdev_flow->ufid, *ufidp)) {
1901 get_dpif_flow_stats(const struct dp_netdev_flow *netdev_flow_,
1902 struct dpif_flow_stats *stats)
1904 struct dp_netdev_flow *netdev_flow;
1905 unsigned long long n;
1909 netdev_flow = CONST_CAST(struct dp_netdev_flow *, netdev_flow_);
1911 atomic_read_relaxed(&netdev_flow->stats.packet_count, &n);
1912 stats->n_packets = n;
1913 atomic_read_relaxed(&netdev_flow->stats.byte_count, &n);
1915 atomic_read_relaxed(&netdev_flow->stats.used, &used);
1917 atomic_read_relaxed(&netdev_flow->stats.tcp_flags, &flags);
1918 stats->tcp_flags = flags;
1921 /* Converts to the dpif_flow format, using 'key_buf' and 'mask_buf' for
1922 * storing the netlink-formatted key/mask. 'key_buf' may be the same as
1923 * 'mask_buf'. Actions will be returned without copying, by relying on RCU to
1926 dp_netdev_flow_to_dpif_flow(const struct dp_netdev_flow *netdev_flow,
1927 struct ofpbuf *key_buf, struct ofpbuf *mask_buf,
1928 struct dpif_flow *flow, bool terse)
1931 memset(flow, 0, sizeof *flow);
1933 struct flow_wildcards wc;
1934 struct dp_netdev_actions *actions;
1936 struct odp_flow_key_parms odp_parms = {
1937 .flow = &netdev_flow->flow,
1939 .support = dp_netdev_support,
1942 miniflow_expand(&netdev_flow->cr.mask->mf, &wc.masks);
1945 offset = key_buf->size;
1946 flow->key = ofpbuf_tail(key_buf);
1947 odp_flow_key_from_flow(&odp_parms, key_buf);
1948 flow->key_len = key_buf->size - offset;
1951 offset = mask_buf->size;
1952 flow->mask = ofpbuf_tail(mask_buf);
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())) {
2125 struct ds ds = DS_EMPTY_INITIALIZER;
2126 struct ofpbuf key_buf, mask_buf;
2127 struct odp_flow_key_parms odp_parms = {
2128 .flow = &match->flow,
2129 .mask = &match->wc.masks,
2130 .support = dp_netdev_support,
2133 ofpbuf_init(&key_buf, 0);
2134 ofpbuf_init(&mask_buf, 0);
2136 odp_flow_key_from_flow(&odp_parms, &key_buf);
2137 odp_parms.key_buf = &key_buf;
2138 odp_flow_key_from_mask(&odp_parms, &mask_buf);
2140 ds_put_cstr(&ds, "flow_add: ");
2141 odp_format_ufid(ufid, &ds);
2142 ds_put_cstr(&ds, " ");
2143 odp_flow_format(key_buf.data, key_buf.size,
2144 mask_buf.data, mask_buf.size,
2146 ds_put_cstr(&ds, ", actions:");
2147 format_odp_actions(&ds, actions, actions_len);
2149 VLOG_DBG_RL(&upcall_rl, "%s", ds_cstr(&ds));
2151 ofpbuf_uninit(&key_buf);
2152 ofpbuf_uninit(&mask_buf);
2160 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
2162 struct dp_netdev *dp = get_dp_netdev(dpif);
2163 struct dp_netdev_flow *netdev_flow;
2164 struct netdev_flow_key key;
2165 struct dp_netdev_pmd_thread *pmd;
2168 unsigned pmd_id = put->pmd_id == PMD_ID_NULL
2169 ? NON_PMD_CORE_ID : put->pmd_id;
2172 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &match.flow);
2176 error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
2177 put->mask, put->mask_len,
2178 &match.flow, &match.wc);
2183 pmd = dp_netdev_get_pmd(dp, pmd_id);
2188 /* Must produce a netdev_flow_key for lookup.
2189 * This interface is no longer performance critical, since it is not used
2190 * for upcall processing any more. */
2191 netdev_flow_key_from_flow(&key, &match.flow);
2196 dpif_flow_hash(dpif, &match.flow, sizeof match.flow, &ufid);
2199 ovs_mutex_lock(&pmd->flow_mutex);
2200 netdev_flow = dp_netdev_pmd_lookup_flow(pmd, &key);
2202 if (put->flags & DPIF_FP_CREATE) {
2203 if (cmap_count(&pmd->flow_table) < MAX_FLOWS) {
2205 memset(put->stats, 0, sizeof *put->stats);
2207 dp_netdev_flow_add(pmd, &match, &ufid, put->actions,
2217 if (put->flags & DPIF_FP_MODIFY
2218 && flow_equal(&match.flow, &netdev_flow->flow)) {
2219 struct dp_netdev_actions *new_actions;
2220 struct dp_netdev_actions *old_actions;
2222 new_actions = dp_netdev_actions_create(put->actions,
2225 old_actions = dp_netdev_flow_get_actions(netdev_flow);
2226 ovsrcu_set(&netdev_flow->actions, new_actions);
2229 get_dpif_flow_stats(netdev_flow, put->stats);
2231 if (put->flags & DPIF_FP_ZERO_STATS) {
2232 /* XXX: The userspace datapath uses thread local statistics
2233 * (for flows), which should be updated only by the owning
2234 * thread. Since we cannot write on stats memory here,
2235 * we choose not to support this flag. Please note:
2236 * - This feature is currently used only by dpctl commands with
2238 * - Should the need arise, this operation can be implemented
2239 * by keeping a base value (to be update here) for each
2240 * counter, and subtracting it before outputting the stats */
2244 ovsrcu_postpone(dp_netdev_actions_free, old_actions);
2245 } else if (put->flags & DPIF_FP_CREATE) {
2248 /* Overlapping flow. */
2252 ovs_mutex_unlock(&pmd->flow_mutex);
2253 dp_netdev_pmd_unref(pmd);
2259 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
2261 struct dp_netdev *dp = get_dp_netdev(dpif);
2262 struct dp_netdev_flow *netdev_flow;
2263 struct dp_netdev_pmd_thread *pmd;
2264 unsigned pmd_id = del->pmd_id == PMD_ID_NULL
2265 ? NON_PMD_CORE_ID : del->pmd_id;
2268 pmd = dp_netdev_get_pmd(dp, pmd_id);
2273 ovs_mutex_lock(&pmd->flow_mutex);
2274 netdev_flow = dp_netdev_pmd_find_flow(pmd, del->ufid, del->key,
2278 get_dpif_flow_stats(netdev_flow, del->stats);
2280 dp_netdev_pmd_remove_flow(pmd, netdev_flow);
2284 ovs_mutex_unlock(&pmd->flow_mutex);
2285 dp_netdev_pmd_unref(pmd);
2290 struct dpif_netdev_flow_dump {
2291 struct dpif_flow_dump up;
2292 struct cmap_position poll_thread_pos;
2293 struct cmap_position flow_pos;
2294 struct dp_netdev_pmd_thread *cur_pmd;
2296 struct ovs_mutex mutex;
2299 static struct dpif_netdev_flow_dump *
2300 dpif_netdev_flow_dump_cast(struct dpif_flow_dump *dump)
2302 return CONTAINER_OF(dump, struct dpif_netdev_flow_dump, up);
2305 static struct dpif_flow_dump *
2306 dpif_netdev_flow_dump_create(const struct dpif *dpif_, bool terse)
2308 struct dpif_netdev_flow_dump *dump;
2310 dump = xzalloc(sizeof *dump);
2311 dpif_flow_dump_init(&dump->up, dpif_);
2312 dump->up.terse = terse;
2313 ovs_mutex_init(&dump->mutex);
2319 dpif_netdev_flow_dump_destroy(struct dpif_flow_dump *dump_)
2321 struct dpif_netdev_flow_dump *dump = dpif_netdev_flow_dump_cast(dump_);
2323 ovs_mutex_destroy(&dump->mutex);
2328 struct dpif_netdev_flow_dump_thread {
2329 struct dpif_flow_dump_thread up;
2330 struct dpif_netdev_flow_dump *dump;
2331 struct odputil_keybuf keybuf[FLOW_DUMP_MAX_BATCH];
2332 struct odputil_keybuf maskbuf[FLOW_DUMP_MAX_BATCH];
2335 static struct dpif_netdev_flow_dump_thread *
2336 dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
2338 return CONTAINER_OF(thread, struct dpif_netdev_flow_dump_thread, up);
2341 static struct dpif_flow_dump_thread *
2342 dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump *dump_)
2344 struct dpif_netdev_flow_dump *dump = dpif_netdev_flow_dump_cast(dump_);
2345 struct dpif_netdev_flow_dump_thread *thread;
2347 thread = xmalloc(sizeof *thread);
2348 dpif_flow_dump_thread_init(&thread->up, &dump->up);
2349 thread->dump = dump;
2354 dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
2356 struct dpif_netdev_flow_dump_thread *thread
2357 = dpif_netdev_flow_dump_thread_cast(thread_);
2363 dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread *thread_,
2364 struct dpif_flow *flows, int max_flows)
2366 struct dpif_netdev_flow_dump_thread *thread
2367 = dpif_netdev_flow_dump_thread_cast(thread_);
2368 struct dpif_netdev_flow_dump *dump = thread->dump;
2369 struct dp_netdev_flow *netdev_flows[FLOW_DUMP_MAX_BATCH];
2373 ovs_mutex_lock(&dump->mutex);
2374 if (!dump->status) {
2375 struct dpif_netdev *dpif = dpif_netdev_cast(thread->up.dpif);
2376 struct dp_netdev *dp = get_dp_netdev(&dpif->dpif);
2377 struct dp_netdev_pmd_thread *pmd = dump->cur_pmd;
2378 int flow_limit = MIN(max_flows, FLOW_DUMP_MAX_BATCH);
2380 /* First call to dump_next(), extracts the first pmd thread.
2381 * If there is no pmd thread, returns immediately. */
2383 pmd = dp_netdev_pmd_get_next(dp, &dump->poll_thread_pos);
2385 ovs_mutex_unlock(&dump->mutex);
2392 for (n_flows = 0; n_flows < flow_limit; n_flows++) {
2393 struct cmap_node *node;
2395 node = cmap_next_position(&pmd->flow_table, &dump->flow_pos);
2399 netdev_flows[n_flows] = CONTAINER_OF(node,
2400 struct dp_netdev_flow,
2403 /* When finishing dumping the current pmd thread, moves to
2405 if (n_flows < flow_limit) {
2406 memset(&dump->flow_pos, 0, sizeof dump->flow_pos);
2407 dp_netdev_pmd_unref(pmd);
2408 pmd = dp_netdev_pmd_get_next(dp, &dump->poll_thread_pos);
2414 /* Keeps the reference to next caller. */
2415 dump->cur_pmd = pmd;
2417 /* If the current dump is empty, do not exit the loop, since the
2418 * remaining pmds could have flows to be dumped. Just dumps again
2419 * on the new 'pmd'. */
2422 ovs_mutex_unlock(&dump->mutex);
2424 for (i = 0; i < n_flows; i++) {
2425 struct odputil_keybuf *maskbuf = &thread->maskbuf[i];
2426 struct odputil_keybuf *keybuf = &thread->keybuf[i];
2427 struct dp_netdev_flow *netdev_flow = netdev_flows[i];
2428 struct dpif_flow *f = &flows[i];
2429 struct ofpbuf key, mask;
2431 ofpbuf_use_stack(&key, keybuf, sizeof *keybuf);
2432 ofpbuf_use_stack(&mask, maskbuf, sizeof *maskbuf);
2433 dp_netdev_flow_to_dpif_flow(netdev_flow, &key, &mask, f,
2441 dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
2442 OVS_NO_THREAD_SAFETY_ANALYSIS
2444 struct dp_netdev *dp = get_dp_netdev(dpif);
2445 struct dp_netdev_pmd_thread *pmd;
2446 struct dp_packet_batch pp;
2448 if (dp_packet_size(execute->packet) < ETH_HEADER_LEN ||
2449 dp_packet_size(execute->packet) > UINT16_MAX) {
2453 /* Tries finding the 'pmd'. If NULL is returned, that means
2454 * the current thread is a non-pmd thread and should use
2455 * dp_netdev_get_pmd(dp, NON_PMD_CORE_ID). */
2456 pmd = ovsthread_getspecific(dp->per_pmd_key);
2458 pmd = dp_netdev_get_pmd(dp, NON_PMD_CORE_ID);
2461 /* If the current thread is non-pmd thread, acquires
2462 * the 'non_pmd_mutex'. */
2463 if (pmd->core_id == NON_PMD_CORE_ID) {
2464 ovs_mutex_lock(&dp->non_pmd_mutex);
2467 /* The action processing expects the RSS hash to be valid, because
2468 * it's always initialized at the beginning of datapath processing.
2469 * In this case, though, 'execute->packet' may not have gone through
2470 * the datapath at all, it may have been generated by the upper layer
2471 * (OpenFlow packet-out, BFD frame, ...). */
2472 if (!dp_packet_rss_valid(execute->packet)) {
2473 dp_packet_set_rss_hash(execute->packet,
2474 flow_hash_5tuple(execute->flow, 0));
2477 packet_batch_init_packet(&pp, execute->packet);
2478 dp_netdev_execute_actions(pmd, &pp, false, execute->actions,
2479 execute->actions_len);
2481 if (pmd->core_id == NON_PMD_CORE_ID) {
2482 ovs_mutex_unlock(&dp->non_pmd_mutex);
2483 dp_netdev_pmd_unref(pmd);
2490 dpif_netdev_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
2494 for (i = 0; i < n_ops; i++) {
2495 struct dpif_op *op = ops[i];
2498 case DPIF_OP_FLOW_PUT:
2499 op->error = dpif_netdev_flow_put(dpif, &op->u.flow_put);
2502 case DPIF_OP_FLOW_DEL:
2503 op->error = dpif_netdev_flow_del(dpif, &op->u.flow_del);
2506 case DPIF_OP_EXECUTE:
2507 op->error = dpif_netdev_execute(dpif, &op->u.execute);
2510 case DPIF_OP_FLOW_GET:
2511 op->error = dpif_netdev_flow_get(dpif, &op->u.flow_get);
2518 cmask_equals(const char *a, const char *b)
2521 return !strcmp(a, b);
2524 return a == NULL && b == NULL;
2527 /* Changes the number or the affinity of pmd threads. The changes are actually
2528 * applied in dpif_netdev_run(). */
2530 dpif_netdev_pmd_set(struct dpif *dpif, const char *cmask)
2532 struct dp_netdev *dp = get_dp_netdev(dpif);
2534 if (!cmask_equals(dp->requested_pmd_cmask, cmask)) {
2535 free(dp->requested_pmd_cmask);
2536 dp->requested_pmd_cmask = nullable_xstrdup(cmask);
2543 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
2544 uint32_t queue_id, uint32_t *priority)
2546 *priority = queue_id;
2551 /* Creates and returns a new 'struct dp_netdev_actions', whose actions are
2552 * a copy of the 'ofpacts_len' bytes of 'ofpacts'. */
2553 struct dp_netdev_actions *
2554 dp_netdev_actions_create(const struct nlattr *actions, size_t size)
2556 struct dp_netdev_actions *netdev_actions;
2558 netdev_actions = xmalloc(sizeof *netdev_actions + size);
2559 memcpy(netdev_actions->actions, actions, size);
2560 netdev_actions->size = size;
2562 return netdev_actions;
2565 struct dp_netdev_actions *
2566 dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow)
2568 return ovsrcu_get(struct dp_netdev_actions *, &flow->actions);
2572 dp_netdev_actions_free(struct dp_netdev_actions *actions)
2577 static inline unsigned long long
2578 cycles_counter(void)
2581 return rte_get_tsc_cycles();
2587 /* Fake mutex to make sure that the calls to cycles_count_* are balanced */
2588 extern struct ovs_mutex cycles_counter_fake_mutex;
2590 /* Start counting cycles. Must be followed by 'cycles_count_end()' */
2592 cycles_count_start(struct dp_netdev_pmd_thread *pmd)
2593 OVS_ACQUIRES(&cycles_counter_fake_mutex)
2594 OVS_NO_THREAD_SAFETY_ANALYSIS
2596 pmd->last_cycles = cycles_counter();
2599 /* Stop counting cycles and add them to the counter 'type' */
2601 cycles_count_end(struct dp_netdev_pmd_thread *pmd,
2602 enum pmd_cycles_counter_type type)
2603 OVS_RELEASES(&cycles_counter_fake_mutex)
2604 OVS_NO_THREAD_SAFETY_ANALYSIS
2606 unsigned long long interval = cycles_counter() - pmd->last_cycles;
2608 non_atomic_ullong_add(&pmd->cycles.n[type], interval);
2612 dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread *pmd,
2613 struct dp_netdev_port *port,
2614 struct netdev_rxq *rxq)
2616 struct dp_packet_batch batch;
2619 dp_packet_batch_init(&batch);
2620 cycles_count_start(pmd);
2621 error = netdev_rxq_recv(rxq, &batch);
2622 cycles_count_end(pmd, PMD_CYCLES_POLLING);
2624 *recirc_depth_get() = 0;
2626 cycles_count_start(pmd);
2627 dp_netdev_input(pmd, &batch, port->port_no);
2628 cycles_count_end(pmd, PMD_CYCLES_PROCESSING);
2629 } else if (error != EAGAIN && error != EOPNOTSUPP) {
2630 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2632 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
2633 netdev_get_name(port->netdev), ovs_strerror(error));
2638 port_reconfigure(struct dp_netdev_port *port)
2640 struct netdev *netdev = port->netdev;
2643 if (!netdev_is_reconf_required(netdev)) {
2647 /* Closes the existing 'rxq's. */
2648 for (i = 0; i < port->n_rxq; i++) {
2649 netdev_rxq_close(port->rxq[i]);
2650 port->rxq[i] = NULL;
2654 /* Allows 'netdev' to apply the pending configuration changes. */
2655 err = netdev_reconfigure(netdev);
2656 if (err && (err != EOPNOTSUPP)) {
2657 VLOG_ERR("Failed to set interface %s new configuration",
2658 netdev_get_name(netdev));
2661 /* If the netdev_reconfigure() above succeeds, reopens the 'rxq's. */
2662 port->rxq = xrealloc(port->rxq, sizeof *port->rxq * netdev_n_rxq(netdev));
2663 for (i = 0; i < netdev_n_rxq(netdev); i++) {
2664 err = netdev_rxq_open(netdev, &port->rxq[i], i);
2675 reconfigure_pmd_threads(struct dp_netdev *dp)
2676 OVS_REQUIRES(dp->port_mutex)
2678 struct dp_netdev_port *port, *next;
2680 dp_netdev_destroy_all_pmds(dp);
2682 HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) {
2685 err = port_reconfigure(port);
2687 hmap_remove(&dp->ports, &port->node);
2688 seq_change(dp->port_seq);
2692 /* Reconfigures the cpu mask. */
2693 ovs_numa_set_cpu_mask(dp->requested_pmd_cmask);
2694 free(dp->pmd_cmask);
2695 dp->pmd_cmask = nullable_xstrdup(dp->requested_pmd_cmask);
2697 /* Restores the non-pmd. */
2698 dp_netdev_set_nonpmd(dp);
2699 /* Restores all pmd threads. */
2700 dp_netdev_reset_pmd_threads(dp);
2703 /* Returns true if one of the netdevs in 'dp' requires a reconfiguration */
2705 ports_require_restart(const struct dp_netdev *dp)
2706 OVS_REQUIRES(dp->port_mutex)
2708 struct dp_netdev_port *port;
2710 HMAP_FOR_EACH (port, node, &dp->ports) {
2711 if (netdev_is_reconf_required(port->netdev)) {
2719 /* Return true if needs to revalidate datapath flows. */
2721 dpif_netdev_run(struct dpif *dpif)
2723 struct dp_netdev_port *port;
2724 struct dp_netdev *dp = get_dp_netdev(dpif);
2725 struct dp_netdev_pmd_thread *non_pmd = dp_netdev_get_pmd(dp,
2727 uint64_t new_tnl_seq;
2729 ovs_mutex_lock(&dp->port_mutex);
2730 ovs_mutex_lock(&dp->non_pmd_mutex);
2731 HMAP_FOR_EACH (port, node, &dp->ports) {
2732 if (!netdev_is_pmd(port->netdev)) {
2735 for (i = 0; i < port->n_rxq; i++) {
2736 dp_netdev_process_rxq_port(non_pmd, port, port->rxq[i]);
2740 ovs_mutex_unlock(&dp->non_pmd_mutex);
2742 dp_netdev_pmd_unref(non_pmd);
2744 if (!cmask_equals(dp->pmd_cmask, dp->requested_pmd_cmask)
2745 || ports_require_restart(dp)) {
2746 reconfigure_pmd_threads(dp);
2748 ovs_mutex_unlock(&dp->port_mutex);
2750 tnl_neigh_cache_run();
2752 new_tnl_seq = seq_read(tnl_conf_seq);
2754 if (dp->last_tnl_conf_seq != new_tnl_seq) {
2755 dp->last_tnl_conf_seq = new_tnl_seq;
2762 dpif_netdev_wait(struct dpif *dpif)
2764 struct dp_netdev_port *port;
2765 struct dp_netdev *dp = get_dp_netdev(dpif);
2767 ovs_mutex_lock(&dp_netdev_mutex);
2768 ovs_mutex_lock(&dp->port_mutex);
2769 HMAP_FOR_EACH (port, node, &dp->ports) {
2770 netdev_wait_reconf_required(port->netdev);
2771 if (!netdev_is_pmd(port->netdev)) {
2774 for (i = 0; i < port->n_rxq; i++) {
2775 netdev_rxq_wait(port->rxq[i]);
2779 ovs_mutex_unlock(&dp->port_mutex);
2780 ovs_mutex_unlock(&dp_netdev_mutex);
2781 seq_wait(tnl_conf_seq, dp->last_tnl_conf_seq);
2785 pmd_free_cached_ports(struct dp_netdev_pmd_thread *pmd)
2787 struct tx_port *tx_port_cached;
2789 HMAP_FOR_EACH_POP (tx_port_cached, node, &pmd->port_cache) {
2790 free(tx_port_cached);
2794 /* Copies ports from 'pmd->tx_ports' (shared with the main thread) to
2795 * 'pmd->port_cache' (thread local) */
2797 pmd_load_cached_ports(struct dp_netdev_pmd_thread *pmd)
2798 OVS_REQUIRES(pmd->port_mutex)
2800 struct tx_port *tx_port, *tx_port_cached;
2802 pmd_free_cached_ports(pmd);
2803 hmap_shrink(&pmd->port_cache);
2805 HMAP_FOR_EACH (tx_port, node, &pmd->tx_ports) {
2806 tx_port_cached = xmemdup(tx_port, sizeof *tx_port_cached);
2807 hmap_insert(&pmd->port_cache, &tx_port_cached->node,
2808 hash_port_no(tx_port_cached->port_no));
2813 pmd_load_queues_and_ports(struct dp_netdev_pmd_thread *pmd,
2814 struct rxq_poll **ppoll_list)
2816 struct rxq_poll *poll_list = *ppoll_list;
2817 struct rxq_poll *poll;
2820 ovs_mutex_lock(&pmd->port_mutex);
2821 poll_list = xrealloc(poll_list, pmd->poll_cnt * sizeof *poll_list);
2824 LIST_FOR_EACH (poll, node, &pmd->poll_list) {
2825 poll_list[i++] = *poll;
2828 pmd_load_cached_ports(pmd);
2830 ovs_mutex_unlock(&pmd->port_mutex);
2832 *ppoll_list = poll_list;
2837 pmd_thread_main(void *f_)
2839 struct dp_netdev_pmd_thread *pmd = f_;
2840 unsigned int lc = 0;
2841 struct rxq_poll *poll_list;
2842 unsigned int port_seq = PMD_INITIAL_SEQ;
2849 /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */
2850 ovsthread_setspecific(pmd->dp->per_pmd_key, pmd);
2851 ovs_numa_thread_setaffinity_core(pmd->core_id);
2852 dpdk_set_lcore_id(pmd->core_id);
2853 poll_cnt = pmd_load_queues_and_ports(pmd, &poll_list);
2855 emc_cache_init(&pmd->flow_cache);
2857 /* List port/core affinity */
2858 for (i = 0; i < poll_cnt; i++) {
2859 VLOG_DBG("Core %d processing port \'%s\' with queue-id %d\n",
2860 pmd->core_id, netdev_get_name(poll_list[i].port->netdev),
2861 netdev_rxq_get_queue_id(poll_list[i].rx));
2865 for (i = 0; i < poll_cnt; i++) {
2866 dp_netdev_process_rxq_port(pmd, poll_list[i].port, poll_list[i].rx);
2874 coverage_try_clear();
2875 if (!ovsrcu_try_quiesce()) {
2876 emc_cache_slow_sweep(&pmd->flow_cache);
2879 atomic_read_relaxed(&pmd->change_seq, &seq);
2880 if (seq != port_seq) {
2887 poll_cnt = pmd_load_queues_and_ports(pmd, &poll_list);
2888 exiting = latch_is_set(&pmd->exit_latch);
2889 /* Signal here to make sure the pmd finishes
2890 * reloading the updated configuration. */
2891 dp_netdev_pmd_reload_done(pmd);
2893 emc_cache_uninit(&pmd->flow_cache);
2900 pmd_free_cached_ports(pmd);
2905 dp_netdev_disable_upcall(struct dp_netdev *dp)
2906 OVS_ACQUIRES(dp->upcall_rwlock)
2908 fat_rwlock_wrlock(&dp->upcall_rwlock);
2912 dpif_netdev_disable_upcall(struct dpif *dpif)
2913 OVS_NO_THREAD_SAFETY_ANALYSIS
2915 struct dp_netdev *dp = get_dp_netdev(dpif);
2916 dp_netdev_disable_upcall(dp);
2920 dp_netdev_enable_upcall(struct dp_netdev *dp)
2921 OVS_RELEASES(dp->upcall_rwlock)
2923 fat_rwlock_unlock(&dp->upcall_rwlock);
2927 dpif_netdev_enable_upcall(struct dpif *dpif)
2928 OVS_NO_THREAD_SAFETY_ANALYSIS
2930 struct dp_netdev *dp = get_dp_netdev(dpif);
2931 dp_netdev_enable_upcall(dp);
2935 dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread *pmd)
2937 ovs_mutex_lock(&pmd->cond_mutex);
2938 xpthread_cond_signal(&pmd->cond);
2939 ovs_mutex_unlock(&pmd->cond_mutex);
2942 /* Finds and refs the dp_netdev_pmd_thread on core 'core_id'. Returns
2943 * the pointer if succeeds, otherwise, NULL.
2945 * Caller must unrefs the returned reference. */
2946 static struct dp_netdev_pmd_thread *
2947 dp_netdev_get_pmd(struct dp_netdev *dp, unsigned core_id)
2949 struct dp_netdev_pmd_thread *pmd;
2950 const struct cmap_node *pnode;
2952 pnode = cmap_find(&dp->poll_threads, hash_int(core_id, 0));
2956 pmd = CONTAINER_OF(pnode, struct dp_netdev_pmd_thread, node);
2958 return dp_netdev_pmd_try_ref(pmd) ? pmd : NULL;
2961 /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */
2963 dp_netdev_set_nonpmd(struct dp_netdev *dp)
2964 OVS_REQUIRES(dp->port_mutex)
2966 struct dp_netdev_pmd_thread *non_pmd;
2967 struct dp_netdev_port *port;
2969 non_pmd = xzalloc(sizeof *non_pmd);
2970 dp_netdev_configure_pmd(non_pmd, dp, NON_PMD_CORE_ID, OVS_NUMA_UNSPEC);
2972 HMAP_FOR_EACH (port, node, &dp->ports) {
2973 dp_netdev_add_port_tx_to_pmd(non_pmd, port);
2976 dp_netdev_reload_pmd__(non_pmd);
2979 /* Caller must have valid pointer to 'pmd'. */
2981 dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread *pmd)
2983 return ovs_refcount_try_ref_rcu(&pmd->ref_cnt);
2987 dp_netdev_pmd_unref(struct dp_netdev_pmd_thread *pmd)
2989 if (pmd && ovs_refcount_unref(&pmd->ref_cnt) == 1) {
2990 ovsrcu_postpone(dp_netdev_destroy_pmd, pmd);
2994 /* Given cmap position 'pos', tries to ref the next node. If try_ref()
2995 * fails, keeps checking for next node until reaching the end of cmap.
2997 * Caller must unrefs the returned reference. */
2998 static struct dp_netdev_pmd_thread *
2999 dp_netdev_pmd_get_next(struct dp_netdev *dp, struct cmap_position *pos)
3001 struct dp_netdev_pmd_thread *next;
3004 struct cmap_node *node;
3006 node = cmap_next_position(&dp->poll_threads, pos);
3007 next = node ? CONTAINER_OF(node, struct dp_netdev_pmd_thread, node)
3009 } while (next && !dp_netdev_pmd_try_ref(next));
3014 /* Configures the 'pmd' based on the input argument. */
3016 dp_netdev_configure_pmd(struct dp_netdev_pmd_thread *pmd, struct dp_netdev *dp,
3017 unsigned core_id, int numa_id)
3020 pmd->core_id = core_id;
3021 pmd->numa_id = numa_id;
3024 atomic_init(&pmd->tx_qid,
3025 (core_id == NON_PMD_CORE_ID)
3026 ? ovs_numa_get_n_cores()
3027 : get_n_pmd_threads(dp));
3029 ovs_refcount_init(&pmd->ref_cnt);
3030 latch_init(&pmd->exit_latch);
3031 atomic_init(&pmd->change_seq, PMD_INITIAL_SEQ);
3032 xpthread_cond_init(&pmd->cond, NULL);
3033 ovs_mutex_init(&pmd->cond_mutex);
3034 ovs_mutex_init(&pmd->flow_mutex);
3035 ovs_mutex_init(&pmd->port_mutex);
3036 dpcls_init(&pmd->cls);
3037 cmap_init(&pmd->flow_table);
3038 ovs_list_init(&pmd->poll_list);
3039 hmap_init(&pmd->tx_ports);
3040 hmap_init(&pmd->port_cache);
3041 /* init the 'flow_cache' since there is no
3042 * actual thread created for NON_PMD_CORE_ID. */
3043 if (core_id == NON_PMD_CORE_ID) {
3044 emc_cache_init(&pmd->flow_cache);
3046 cmap_insert(&dp->poll_threads, CONST_CAST(struct cmap_node *, &pmd->node),
3047 hash_int(core_id, 0));
3051 dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread *pmd)
3053 dp_netdev_pmd_flow_flush(pmd);
3054 dpcls_destroy(&pmd->cls);
3055 hmap_destroy(&pmd->port_cache);
3056 hmap_destroy(&pmd->tx_ports);
3057 cmap_destroy(&pmd->flow_table);
3058 ovs_mutex_destroy(&pmd->flow_mutex);
3059 latch_destroy(&pmd->exit_latch);
3060 xpthread_cond_destroy(&pmd->cond);
3061 ovs_mutex_destroy(&pmd->cond_mutex);
3062 ovs_mutex_destroy(&pmd->port_mutex);
3066 /* Stops the pmd thread, removes it from the 'dp->poll_threads',
3067 * and unrefs the struct. */
3069 dp_netdev_del_pmd(struct dp_netdev *dp, struct dp_netdev_pmd_thread *pmd)
3071 /* NON_PMD_CORE_ID doesn't have a thread, so we don't have to synchronize,
3072 * but extra cleanup is necessary */
3073 if (pmd->core_id == NON_PMD_CORE_ID) {
3074 emc_cache_uninit(&pmd->flow_cache);
3075 pmd_free_cached_ports(pmd);
3077 latch_set(&pmd->exit_latch);
3078 dp_netdev_reload_pmd__(pmd);
3079 ovs_numa_unpin_core(pmd->core_id);
3080 xpthread_join(pmd->thread, NULL);
3083 dp_netdev_pmd_clear_ports(pmd);
3085 /* Purges the 'pmd''s flows after stopping the thread, but before
3086 * destroying the flows, so that the flow stats can be collected. */
3087 if (dp->dp_purge_cb) {
3088 dp->dp_purge_cb(dp->dp_purge_aux, pmd->core_id);
3090 cmap_remove(&pmd->dp->poll_threads, &pmd->node, hash_int(pmd->core_id, 0));
3091 dp_netdev_pmd_unref(pmd);
3094 /* Destroys all pmd threads. */
3096 dp_netdev_destroy_all_pmds(struct dp_netdev *dp)
3098 struct dp_netdev_pmd_thread *pmd;
3099 struct dp_netdev_pmd_thread **pmd_list;
3100 size_t k = 0, n_pmds;
3102 n_pmds = cmap_count(&dp->poll_threads);
3103 pmd_list = xcalloc(n_pmds, sizeof *pmd_list);
3105 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3106 /* We cannot call dp_netdev_del_pmd(), since it alters
3107 * 'dp->poll_threads' (while we're iterating it) and it
3109 ovs_assert(k < n_pmds);
3110 pmd_list[k++] = pmd;
3113 for (size_t i = 0; i < k; i++) {
3114 dp_netdev_del_pmd(dp, pmd_list[i]);
3119 /* Deletes all pmd threads on numa node 'numa_id' and
3120 * fixes tx_qids of other threads to keep them sequential. */
3122 dp_netdev_del_pmds_on_numa(struct dp_netdev *dp, int numa_id)
3124 struct dp_netdev_pmd_thread *pmd;
3125 int n_pmds_on_numa, n_pmds;
3126 int *free_idx, k = 0;
3127 struct dp_netdev_pmd_thread **pmd_list;
3129 n_pmds_on_numa = get_n_pmd_threads_on_numa(dp, numa_id);
3130 free_idx = xcalloc(n_pmds_on_numa, sizeof *free_idx);
3131 pmd_list = xcalloc(n_pmds_on_numa, sizeof *pmd_list);
3133 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3134 /* We cannot call dp_netdev_del_pmd(), since it alters
3135 * 'dp->poll_threads' (while we're iterating it) and it
3137 if (pmd->numa_id == numa_id) {
3138 atomic_read_relaxed(&pmd->tx_qid, &free_idx[k]);
3140 ovs_assert(k < n_pmds_on_numa);
3145 for (int i = 0; i < k; i++) {
3146 dp_netdev_del_pmd(dp, pmd_list[i]);
3149 n_pmds = get_n_pmd_threads(dp);
3150 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3153 atomic_read_relaxed(&pmd->tx_qid, &old_tx_qid);
3155 if (old_tx_qid >= n_pmds) {
3156 int new_tx_qid = free_idx[--k];
3158 atomic_store_relaxed(&pmd->tx_qid, new_tx_qid);
3166 /* Deletes all rx queues from pmd->poll_list and all the ports from
3169 dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread *pmd)
3171 struct rxq_poll *poll;
3172 struct tx_port *port;
3174 ovs_mutex_lock(&pmd->port_mutex);
3175 LIST_FOR_EACH_POP (poll, node, &pmd->poll_list) {
3179 HMAP_FOR_EACH_POP (port, node, &pmd->tx_ports) {
3182 ovs_mutex_unlock(&pmd->port_mutex);
3185 static struct tx_port *
3186 tx_port_lookup(const struct hmap *hmap, odp_port_t port_no)
3190 HMAP_FOR_EACH_IN_BUCKET (tx, node, hash_port_no(port_no), hmap) {
3191 if (tx->port_no == port_no) {
3199 /* Deletes all rx queues of 'port' from 'poll_list', and the 'port' from
3200 * 'tx_ports' of 'pmd' thread. Returns true if 'port' was found in 'pmd'
3201 * (therefore a restart is required). */
3203 dp_netdev_del_port_from_pmd__(struct dp_netdev_port *port,
3204 struct dp_netdev_pmd_thread *pmd)
3206 struct rxq_poll *poll, *next;
3210 ovs_mutex_lock(&pmd->port_mutex);
3211 LIST_FOR_EACH_SAFE (poll, next, node, &pmd->poll_list) {
3212 if (poll->port == port) {
3214 ovs_list_remove(&poll->node);
3220 tx = tx_port_lookup(&pmd->tx_ports, port->port_no);
3222 hmap_remove(&pmd->tx_ports, &tx->node);
3226 ovs_mutex_unlock(&pmd->port_mutex);
3231 /* Deletes 'port' from the 'poll_list' and from the 'tx_ports' of all the pmd
3232 * threads. The pmd threads that need to be restarted are inserted in
3235 dp_netdev_del_port_from_all_pmds__(struct dp_netdev *dp,
3236 struct dp_netdev_port *port,
3237 struct hmapx *to_reload)
3239 struct dp_netdev_pmd_thread *pmd;
3241 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3244 found = dp_netdev_del_port_from_pmd__(port, pmd);
3247 hmapx_add(to_reload, pmd);
3252 /* Deletes 'port' from the 'poll_list' and from the 'tx_ports' of all the pmd
3253 * threads. Reloads the threads if needed. */
3255 dp_netdev_del_port_from_all_pmds(struct dp_netdev *dp,
3256 struct dp_netdev_port *port)
3258 struct dp_netdev_pmd_thread *pmd;
3259 struct hmapx to_reload = HMAPX_INITIALIZER(&to_reload);
3260 struct hmapx_node *node;
3262 dp_netdev_del_port_from_all_pmds__(dp, port, &to_reload);
3264 HMAPX_FOR_EACH (node, &to_reload) {
3265 pmd = (struct dp_netdev_pmd_thread *) node->data;
3266 dp_netdev_reload_pmd__(pmd);
3269 hmapx_destroy(&to_reload);
3273 /* Returns PMD thread from this numa node with fewer rx queues to poll.
3274 * Returns NULL if there is no PMD threads on this numa node.
3275 * Can be called safely only by main thread. */
3276 static struct dp_netdev_pmd_thread *
3277 dp_netdev_less_loaded_pmd_on_numa(struct dp_netdev *dp, int numa_id)
3280 struct dp_netdev_pmd_thread *pmd, *res = NULL;
3282 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3283 if (pmd->numa_id == numa_id
3284 && (min_cnt > pmd->poll_cnt || res == NULL)) {
3285 min_cnt = pmd->poll_cnt;
3293 /* Adds rx queue to poll_list of PMD thread. */
3295 dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread *pmd,
3296 struct dp_netdev_port *port, struct netdev_rxq *rx)
3297 OVS_REQUIRES(pmd->port_mutex)
3299 struct rxq_poll *poll = xmalloc(sizeof *poll);
3304 ovs_list_push_back(&pmd->poll_list, &poll->node);
3308 /* Add 'port' to the tx port cache of 'pmd', which must be reloaded for the
3309 * changes to take effect. */
3311 dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread *pmd,
3312 struct dp_netdev_port *port)
3314 struct tx_port *tx = xzalloc(sizeof *tx);
3316 tx->netdev = port->netdev;
3317 tx->port_no = port->port_no;
3319 ovs_mutex_lock(&pmd->port_mutex);
3320 hmap_insert(&pmd->tx_ports, &tx->node, hash_port_no(tx->port_no));
3321 ovs_mutex_unlock(&pmd->port_mutex);
3324 /* Distribute all rx queues of 'port' between PMD threads in 'dp'. The pmd
3325 * threads that need to be restarted are inserted in 'to_reload'. */
3327 dp_netdev_add_port_rx_to_pmds(struct dp_netdev *dp,
3328 struct dp_netdev_port *port,
3329 struct hmapx *to_reload)
3331 int numa_id = netdev_get_numa_id(port->netdev);
3334 if (!netdev_is_pmd(port->netdev)) {
3338 for (i = 0; i < port->n_rxq; i++) {
3339 struct dp_netdev_pmd_thread *pmd;
3341 pmd = dp_netdev_less_loaded_pmd_on_numa(dp, numa_id);
3343 VLOG_WARN("There's no pmd thread on numa node %d", numa_id);
3347 ovs_mutex_lock(&pmd->port_mutex);
3348 dp_netdev_add_rxq_to_pmd(pmd, port, port->rxq[i]);
3349 ovs_mutex_unlock(&pmd->port_mutex);
3351 hmapx_add(to_reload, pmd);
3355 /* Distributes all rx queues of 'port' between all PMD threads in 'dp' and
3356 * inserts 'port' in the PMD threads 'tx_ports'. The pmd threads that need to
3357 * be restarted are inserted in 'to_reload'. */
3359 dp_netdev_add_port_to_pmds__(struct dp_netdev *dp, struct dp_netdev_port *port,
3360 struct hmapx *to_reload)
3362 struct dp_netdev_pmd_thread *pmd;
3364 dp_netdev_add_port_rx_to_pmds(dp, port, to_reload);
3366 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3367 dp_netdev_add_port_tx_to_pmd(pmd, port);
3368 hmapx_add(to_reload, pmd);
3372 /* Distributes all rx queues of 'port' between all PMD threads in 'dp', inserts
3373 * 'port' in the PMD threads 'tx_ports' and reloads them, if needed. */
3375 dp_netdev_add_port_to_pmds(struct dp_netdev *dp, struct dp_netdev_port *port)
3377 struct dp_netdev_pmd_thread *pmd;
3378 struct hmapx to_reload = HMAPX_INITIALIZER(&to_reload);
3379 struct hmapx_node *node;
3381 dp_netdev_add_port_to_pmds__(dp, port, &to_reload);
3383 HMAPX_FOR_EACH (node, &to_reload) {
3384 pmd = (struct dp_netdev_pmd_thread *) node->data;
3385 dp_netdev_reload_pmd__(pmd);
3388 hmapx_destroy(&to_reload);
3391 /* Starts pmd threads for the numa node 'numa_id', if not already started.
3392 * The function takes care of filling the threads tx port cache. */
3394 dp_netdev_set_pmds_on_numa(struct dp_netdev *dp, int numa_id)
3395 OVS_REQUIRES(dp->port_mutex)
3399 if (!ovs_numa_numa_id_is_valid(numa_id)) {
3400 VLOG_WARN("Cannot create pmd threads due to numa id (%d) invalid",
3405 n_pmds = get_n_pmd_threads_on_numa(dp, numa_id);
3407 /* If there are already pmd threads created for the numa node
3408 * in which 'netdev' is on, do nothing. Else, creates the
3409 * pmd threads for the numa node. */
3411 int can_have, n_unpinned, i;
3413 n_unpinned = ovs_numa_get_n_unpinned_cores_on_numa(numa_id);
3415 VLOG_WARN("Cannot create pmd threads due to out of unpinned "
3416 "cores on numa node %d", numa_id);
3420 /* If cpu mask is specified, uses all unpinned cores, otherwise
3421 * tries creating NR_PMD_THREADS pmd threads. */
3422 can_have = dp->pmd_cmask ? n_unpinned : MIN(n_unpinned, NR_PMD_THREADS);
3423 for (i = 0; i < can_have; i++) {
3424 unsigned core_id = ovs_numa_get_unpinned_core_on_numa(numa_id);
3425 struct dp_netdev_pmd_thread *pmd = xzalloc(sizeof *pmd);
3426 struct dp_netdev_port *port;
3428 dp_netdev_configure_pmd(pmd, dp, core_id, numa_id);
3430 HMAP_FOR_EACH (port, node, &dp->ports) {
3431 dp_netdev_add_port_tx_to_pmd(pmd, port);
3434 pmd->thread = ovs_thread_create("pmd", pmd_thread_main, pmd);
3436 VLOG_INFO("Created %d pmd threads on numa node %d", can_have, numa_id);
3441 /* Called after pmd threads config change. Restarts pmd threads with
3442 * new configuration. */
3444 dp_netdev_reset_pmd_threads(struct dp_netdev *dp)
3445 OVS_REQUIRES(dp->port_mutex)
3447 struct hmapx to_reload = HMAPX_INITIALIZER(&to_reload);
3448 struct dp_netdev_pmd_thread *pmd;
3449 struct dp_netdev_port *port;
3450 struct hmapx_node *node;
3452 HMAP_FOR_EACH (port, node, &dp->ports) {
3453 if (netdev_is_pmd(port->netdev)) {
3454 int numa_id = netdev_get_numa_id(port->netdev);
3456 dp_netdev_set_pmds_on_numa(dp, numa_id);
3458 dp_netdev_add_port_rx_to_pmds(dp, port, &to_reload);
3461 HMAPX_FOR_EACH (node, &to_reload) {
3462 pmd = (struct dp_netdev_pmd_thread *) node->data;
3463 dp_netdev_reload_pmd__(pmd);
3466 hmapx_destroy(&to_reload);
3470 dpif_netdev_get_datapath_version(void)
3472 return xstrdup("<built-in>");
3476 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow, int cnt, int size,
3477 uint16_t tcp_flags, long long now)
3481 atomic_store_relaxed(&netdev_flow->stats.used, now);
3482 non_atomic_ullong_add(&netdev_flow->stats.packet_count, cnt);
3483 non_atomic_ullong_add(&netdev_flow->stats.byte_count, size);
3484 atomic_read_relaxed(&netdev_flow->stats.tcp_flags, &flags);
3486 atomic_store_relaxed(&netdev_flow->stats.tcp_flags, flags);
3490 dp_netdev_count_packet(struct dp_netdev_pmd_thread *pmd,
3491 enum dp_stat_type type, int cnt)
3493 non_atomic_ullong_add(&pmd->stats.n[type], cnt);
3497 dp_netdev_upcall(struct dp_netdev_pmd_thread *pmd, struct dp_packet *packet_,
3498 struct flow *flow, struct flow_wildcards *wc, ovs_u128 *ufid,
3499 enum dpif_upcall_type type, const struct nlattr *userdata,
3500 struct ofpbuf *actions, struct ofpbuf *put_actions)
3502 struct dp_netdev *dp = pmd->dp;
3503 struct flow_tnl orig_tunnel;
3506 if (OVS_UNLIKELY(!dp->upcall_cb)) {
3510 /* Upcall processing expects the Geneve options to be in the translated
3511 * format but we need to retain the raw format for datapath use. */
3512 orig_tunnel.flags = flow->tunnel.flags;
3513 if (flow->tunnel.flags & FLOW_TNL_F_UDPIF) {
3514 orig_tunnel.metadata.present.len = flow->tunnel.metadata.present.len;
3515 memcpy(orig_tunnel.metadata.opts.gnv, flow->tunnel.metadata.opts.gnv,
3516 flow->tunnel.metadata.present.len);
3517 err = tun_metadata_from_geneve_udpif(&orig_tunnel, &orig_tunnel,
3524 if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl))) {
3525 struct ds ds = DS_EMPTY_INITIALIZER;
3528 struct odp_flow_key_parms odp_parms = {
3531 .support = dp_netdev_support,
3534 ofpbuf_init(&key, 0);
3535 odp_flow_key_from_flow(&odp_parms, &key);
3536 packet_str = ofp_packet_to_string(dp_packet_data(packet_),
3537 dp_packet_size(packet_));
3539 odp_flow_key_format(key.data, key.size, &ds);
3541 VLOG_DBG("%s: %s upcall:\n%s\n%s", dp->name,
3542 dpif_upcall_type_to_string(type), ds_cstr(&ds), packet_str);
3544 ofpbuf_uninit(&key);
3550 err = dp->upcall_cb(packet_, flow, ufid, pmd->core_id, type, userdata,
3551 actions, wc, put_actions, dp->upcall_aux);
3552 if (err && err != ENOSPC) {
3556 /* Translate tunnel metadata masks to datapath format. */
3558 if (wc->masks.tunnel.metadata.present.map) {
3559 struct geneve_opt opts[TLV_TOT_OPT_SIZE /
3560 sizeof(struct geneve_opt)];
3562 if (orig_tunnel.flags & FLOW_TNL_F_UDPIF) {
3563 tun_metadata_to_geneve_udpif_mask(&flow->tunnel,
3565 orig_tunnel.metadata.opts.gnv,
3566 orig_tunnel.metadata.present.len,
3569 orig_tunnel.metadata.present.len = 0;
3572 memset(&wc->masks.tunnel.metadata, 0,
3573 sizeof wc->masks.tunnel.metadata);
3574 memcpy(&wc->masks.tunnel.metadata.opts.gnv, opts,
3575 orig_tunnel.metadata.present.len);
3577 wc->masks.tunnel.metadata.present.len = 0xff;
3580 /* Restore tunnel metadata. We need to use the saved options to ensure
3581 * that any unknown options are not lost. The generated mask will have
3582 * the same structure, matching on types and lengths but wildcarding
3583 * option data we don't care about. */
3584 if (orig_tunnel.flags & FLOW_TNL_F_UDPIF) {
3585 memcpy(&flow->tunnel.metadata.opts.gnv, orig_tunnel.metadata.opts.gnv,
3586 orig_tunnel.metadata.present.len);
3587 flow->tunnel.metadata.present.len = orig_tunnel.metadata.present.len;
3588 flow->tunnel.flags |= FLOW_TNL_F_UDPIF;
3594 static inline uint32_t
3595 dpif_netdev_packet_get_rss_hash(struct dp_packet *packet,
3596 const struct miniflow *mf)
3598 uint32_t hash, recirc_depth;
3600 if (OVS_LIKELY(dp_packet_rss_valid(packet))) {
3601 hash = dp_packet_get_rss_hash(packet);
3603 hash = miniflow_hash_5tuple(mf, 0);
3604 dp_packet_set_rss_hash(packet, hash);
3607 /* The RSS hash must account for the recirculation depth to avoid
3608 * collisions in the exact match cache */
3609 recirc_depth = *recirc_depth_get_unsafe();
3610 if (OVS_UNLIKELY(recirc_depth)) {
3611 hash = hash_finish(hash, recirc_depth);
3612 dp_packet_set_rss_hash(packet, hash);
3617 struct packet_batch_per_flow {
3618 unsigned int byte_count;
3620 struct dp_netdev_flow *flow;
3622 struct dp_packet_batch array;
3626 packet_batch_per_flow_update(struct packet_batch_per_flow *batch,
3627 struct dp_packet *packet,
3628 const struct miniflow *mf)
3630 batch->byte_count += dp_packet_size(packet);
3631 batch->tcp_flags |= miniflow_get_tcp_flags(mf);
3632 batch->array.packets[batch->array.count++] = packet;
3636 packet_batch_per_flow_init(struct packet_batch_per_flow *batch,
3637 struct dp_netdev_flow *flow)
3639 flow->batch = batch;
3642 dp_packet_batch_init(&batch->array);
3643 batch->byte_count = 0;
3644 batch->tcp_flags = 0;
3648 packet_batch_per_flow_execute(struct packet_batch_per_flow *batch,
3649 struct dp_netdev_pmd_thread *pmd,
3652 struct dp_netdev_actions *actions;
3653 struct dp_netdev_flow *flow = batch->flow;
3655 dp_netdev_flow_used(flow, batch->array.count, batch->byte_count,
3656 batch->tcp_flags, now);
3658 actions = dp_netdev_flow_get_actions(flow);
3660 dp_netdev_execute_actions(pmd, &batch->array, true,
3661 actions->actions, actions->size);
3665 dp_netdev_queue_batches(struct dp_packet *pkt,
3666 struct dp_netdev_flow *flow, const struct miniflow *mf,
3667 struct packet_batch_per_flow *batches, size_t *n_batches)
3669 struct packet_batch_per_flow *batch = flow->batch;
3671 if (OVS_UNLIKELY(!batch)) {
3672 batch = &batches[(*n_batches)++];
3673 packet_batch_per_flow_init(batch, flow);
3676 packet_batch_per_flow_update(batch, pkt, mf);
3679 /* Try to process all ('cnt') the 'packets' using only the exact match cache
3680 * 'pmd->flow_cache'. If a flow is not found for a packet 'packets[i]', the
3681 * miniflow is copied into 'keys' and the packet pointer is moved at the
3682 * beginning of the 'packets' array.
3684 * The function returns the number of packets that needs to be processed in the
3685 * 'packets' array (they have been moved to the beginning of the vector).
3687 * If 'md_is_valid' is false, the metadata in 'packets' is not valid and must be
3688 * initialized by this function using 'port_no'.
3690 static inline size_t
3691 emc_processing(struct dp_netdev_pmd_thread *pmd, struct dp_packet_batch *packets_,
3692 struct netdev_flow_key *keys,
3693 struct packet_batch_per_flow batches[], size_t *n_batches,
3694 bool md_is_valid, odp_port_t port_no)
3696 struct emc_cache *flow_cache = &pmd->flow_cache;
3697 struct netdev_flow_key *key = &keys[0];
3698 size_t i, n_missed = 0, n_dropped = 0;
3699 struct dp_packet **packets = packets_->packets;
3700 int cnt = packets_->count;
3702 for (i = 0; i < cnt; i++) {
3703 struct dp_netdev_flow *flow;
3704 struct dp_packet *packet = packets[i];
3706 if (OVS_UNLIKELY(dp_packet_size(packet) < ETH_HEADER_LEN)) {
3707 dp_packet_delete(packet);
3713 /* Prefetch next packet data and metadata. */
3714 OVS_PREFETCH(dp_packet_data(packets[i+1]));
3715 pkt_metadata_prefetch_init(&packets[i+1]->md);
3719 pkt_metadata_init(&packet->md, port_no);
3721 miniflow_extract(packet, &key->mf);
3722 key->len = 0; /* Not computed yet. */
3723 key->hash = dpif_netdev_packet_get_rss_hash(packet, &key->mf);
3725 flow = emc_lookup(flow_cache, key);
3726 if (OVS_LIKELY(flow)) {
3727 dp_netdev_queue_batches(packet, flow, &key->mf, batches,
3730 /* Exact match cache missed. Group missed packets together at
3731 * the beginning of the 'packets' array. */
3732 packets[n_missed] = packet;
3733 /* 'key[n_missed]' contains the key of the current packet and it
3734 * must be returned to the caller. The next key should be extracted
3735 * to 'keys[n_missed + 1]'. */
3736 key = &keys[++n_missed];
3740 dp_netdev_count_packet(pmd, DP_STAT_EXACT_HIT, cnt - n_dropped - n_missed);
3746 handle_packet_upcall(struct dp_netdev_pmd_thread *pmd, struct dp_packet *packet,
3747 const struct netdev_flow_key *key,
3748 struct ofpbuf *actions, struct ofpbuf *put_actions,
3751 struct ofpbuf *add_actions;
3752 struct dp_packet_batch b;
3757 match.tun_md.valid = false;
3758 miniflow_expand(&key->mf, &match.flow);
3760 ofpbuf_clear(actions);
3761 ofpbuf_clear(put_actions);
3763 dpif_flow_hash(pmd->dp->dpif, &match.flow, sizeof match.flow, &ufid);
3764 error = dp_netdev_upcall(pmd, packet, &match.flow, &match.wc,
3765 &ufid, DPIF_UC_MISS, NULL, actions,
3767 if (OVS_UNLIKELY(error && error != ENOSPC)) {
3768 dp_packet_delete(packet);
3773 /* The Netlink encoding of datapath flow keys cannot express
3774 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
3775 * tag is interpreted as exact match on the fact that there is no
3776 * VLAN. Unless we refactor a lot of code that translates between
3777 * Netlink and struct flow representations, we have to do the same
3779 if (!match.wc.masks.vlan_tci) {
3780 match.wc.masks.vlan_tci = htons(0xffff);
3783 /* We can't allow the packet batching in the next loop to execute
3784 * the actions. Otherwise, if there are any slow path actions,
3785 * we'll send the packet up twice. */
3786 packet_batch_init_packet(&b, packet);
3787 dp_netdev_execute_actions(pmd, &b, true,
3788 actions->data, actions->size);
3790 add_actions = put_actions->size ? put_actions : actions;
3791 if (OVS_LIKELY(error != ENOSPC)) {
3792 struct dp_netdev_flow *netdev_flow;
3794 /* XXX: There's a race window where a flow covering this packet
3795 * could have already been installed since we last did the flow
3796 * lookup before upcall. This could be solved by moving the
3797 * mutex lock outside the loop, but that's an awful long time
3798 * to be locking everyone out of making flow installs. If we
3799 * move to a per-core classifier, it would be reasonable. */
3800 ovs_mutex_lock(&pmd->flow_mutex);
3801 netdev_flow = dp_netdev_pmd_lookup_flow(pmd, key);
3802 if (OVS_LIKELY(!netdev_flow)) {
3803 netdev_flow = dp_netdev_flow_add(pmd, &match, &ufid,
3807 ovs_mutex_unlock(&pmd->flow_mutex);
3809 emc_insert(&pmd->flow_cache, key, netdev_flow);
3814 fast_path_processing(struct dp_netdev_pmd_thread *pmd,
3815 struct dp_packet_batch *packets_,
3816 struct netdev_flow_key *keys,
3817 struct packet_batch_per_flow batches[], size_t *n_batches)
3819 int cnt = packets_->count;
3820 #if !defined(__CHECKER__) && !defined(_WIN32)
3821 const size_t PKT_ARRAY_SIZE = cnt;
3823 /* Sparse or MSVC doesn't like variable length array. */
3824 enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST };
3826 struct dp_packet **packets = packets_->packets;
3827 struct dpcls_rule *rules[PKT_ARRAY_SIZE];
3828 struct dp_netdev *dp = pmd->dp;
3829 struct emc_cache *flow_cache = &pmd->flow_cache;
3830 int miss_cnt = 0, lost_cnt = 0;
3834 for (i = 0; i < cnt; i++) {
3835 /* Key length is needed in all the cases, hash computed on demand. */
3836 keys[i].len = netdev_flow_key_size(miniflow_n_values(&keys[i].mf));
3838 any_miss = !dpcls_lookup(&pmd->cls, keys, rules, cnt);
3839 if (OVS_UNLIKELY(any_miss) && !fat_rwlock_tryrdlock(&dp->upcall_rwlock)) {
3840 uint64_t actions_stub[512 / 8], slow_stub[512 / 8];
3841 struct ofpbuf actions, put_actions;
3843 ofpbuf_use_stub(&actions, actions_stub, sizeof actions_stub);
3844 ofpbuf_use_stub(&put_actions, slow_stub, sizeof slow_stub);
3846 for (i = 0; i < cnt; i++) {
3847 struct dp_netdev_flow *netdev_flow;
3849 if (OVS_LIKELY(rules[i])) {
3853 /* It's possible that an earlier slow path execution installed
3854 * a rule covering this flow. In this case, it's a lot cheaper
3855 * to catch it here than execute a miss. */
3856 netdev_flow = dp_netdev_pmd_lookup_flow(pmd, &keys[i]);
3858 rules[i] = &netdev_flow->cr;
3863 handle_packet_upcall(pmd, packets[i], &keys[i], &actions, &put_actions,
3867 ofpbuf_uninit(&actions);
3868 ofpbuf_uninit(&put_actions);
3869 fat_rwlock_unlock(&dp->upcall_rwlock);
3870 dp_netdev_count_packet(pmd, DP_STAT_LOST, lost_cnt);
3871 } else if (OVS_UNLIKELY(any_miss)) {
3872 for (i = 0; i < cnt; i++) {
3873 if (OVS_UNLIKELY(!rules[i])) {
3874 dp_packet_delete(packets[i]);
3881 for (i = 0; i < cnt; i++) {
3882 struct dp_packet *packet = packets[i];
3883 struct dp_netdev_flow *flow;
3885 if (OVS_UNLIKELY(!rules[i])) {
3889 flow = dp_netdev_flow_cast(rules[i]);
3891 emc_insert(flow_cache, &keys[i], flow);
3892 dp_netdev_queue_batches(packet, flow, &keys[i].mf, batches, n_batches);
3895 dp_netdev_count_packet(pmd, DP_STAT_MASKED_HIT, cnt - miss_cnt);
3896 dp_netdev_count_packet(pmd, DP_STAT_MISS, miss_cnt);
3897 dp_netdev_count_packet(pmd, DP_STAT_LOST, lost_cnt);
3900 /* Packets enter the datapath from a port (or from recirculation) here.
3902 * For performance reasons a caller may choose not to initialize the metadata
3903 * in 'packets': in this case 'mdinit' is false and this function needs to
3904 * initialize it using 'port_no'. If the metadata in 'packets' is already
3905 * valid, 'md_is_valid' must be true and 'port_no' will be ignored. */
3907 dp_netdev_input__(struct dp_netdev_pmd_thread *pmd,
3908 struct dp_packet_batch *packets,
3909 bool md_is_valid, odp_port_t port_no)
3911 int cnt = packets->count;
3912 #if !defined(__CHECKER__) && !defined(_WIN32)
3913 const size_t PKT_ARRAY_SIZE = cnt;
3915 /* Sparse or MSVC doesn't like variable length array. */
3916 enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST };
3918 struct netdev_flow_key keys[PKT_ARRAY_SIZE];
3919 struct packet_batch_per_flow batches[PKT_ARRAY_SIZE];
3920 long long now = time_msec();
3921 size_t newcnt, n_batches, i;
3924 newcnt = emc_processing(pmd, packets, keys, batches, &n_batches,
3925 md_is_valid, port_no);
3926 if (OVS_UNLIKELY(newcnt)) {
3927 packets->count = newcnt;
3928 fast_path_processing(pmd, packets, keys, batches, &n_batches);
3931 for (i = 0; i < n_batches; i++) {
3932 batches[i].flow->batch = NULL;
3935 for (i = 0; i < n_batches; i++) {
3936 packet_batch_per_flow_execute(&batches[i], pmd, now);
3941 dp_netdev_input(struct dp_netdev_pmd_thread *pmd,
3942 struct dp_packet_batch *packets,
3945 dp_netdev_input__(pmd, packets, false, port_no);
3949 dp_netdev_recirculate(struct dp_netdev_pmd_thread *pmd,
3950 struct dp_packet_batch *packets)
3952 dp_netdev_input__(pmd, packets, true, 0);
3955 struct dp_netdev_execute_aux {
3956 struct dp_netdev_pmd_thread *pmd;
3960 dpif_netdev_register_dp_purge_cb(struct dpif *dpif, dp_purge_callback *cb,
3963 struct dp_netdev *dp = get_dp_netdev(dpif);
3964 dp->dp_purge_aux = aux;
3965 dp->dp_purge_cb = cb;
3969 dpif_netdev_register_upcall_cb(struct dpif *dpif, upcall_callback *cb,
3972 struct dp_netdev *dp = get_dp_netdev(dpif);
3973 dp->upcall_aux = aux;
3977 static struct tx_port *
3978 pmd_tx_port_cache_lookup(const struct dp_netdev_pmd_thread *pmd,
3981 return tx_port_lookup(&pmd->port_cache, port_no);
3985 push_tnl_action(const struct dp_netdev_pmd_thread *pmd,
3986 const struct nlattr *attr,
3987 struct dp_packet_batch *batch)
3989 struct tx_port *tun_port;
3990 const struct ovs_action_push_tnl *data;
3993 data = nl_attr_get(attr);
3995 tun_port = pmd_tx_port_cache_lookup(pmd, u32_to_odp(data->tnl_port));
4000 err = netdev_push_header(tun_port->netdev, batch, data);
4005 dp_packet_delete_batch(batch, true);
4010 dp_execute_userspace_action(struct dp_netdev_pmd_thread *pmd,
4011 struct dp_packet *packet, bool may_steal,
4012 struct flow *flow, ovs_u128 *ufid,
4013 struct ofpbuf *actions,
4014 const struct nlattr *userdata)
4016 struct dp_packet_batch b;
4019 ofpbuf_clear(actions);
4021 error = dp_netdev_upcall(pmd, packet, flow, NULL, ufid,
4022 DPIF_UC_ACTION, userdata, actions,
4024 if (!error || error == ENOSPC) {
4025 packet_batch_init_packet(&b, packet);
4026 dp_netdev_execute_actions(pmd, &b, may_steal,
4027 actions->data, actions->size);
4028 } else if (may_steal) {
4029 dp_packet_delete(packet);
4034 dp_execute_cb(void *aux_, struct dp_packet_batch *packets_,
4035 const struct nlattr *a, bool may_steal)
4037 struct dp_netdev_execute_aux *aux = aux_;
4038 uint32_t *depth = recirc_depth_get();
4039 struct dp_netdev_pmd_thread *pmd = aux->pmd;
4040 struct dp_netdev *dp = pmd->dp;
4041 int type = nl_attr_type(a);
4044 switch ((enum ovs_action_attr)type) {
4045 case OVS_ACTION_ATTR_OUTPUT:
4046 p = pmd_tx_port_cache_lookup(pmd, u32_to_odp(nl_attr_get_u32(a)));
4047 if (OVS_LIKELY(p)) {
4050 atomic_read_relaxed(&pmd->tx_qid, &tx_qid);
4052 netdev_send(p->netdev, tx_qid, packets_, may_steal);
4057 case OVS_ACTION_ATTR_TUNNEL_PUSH:
4058 if (*depth < MAX_RECIRC_DEPTH) {
4059 struct dp_packet_batch tnl_pkt;
4060 struct dp_packet_batch *orig_packets_ = packets_;
4064 dp_packet_batch_clone(&tnl_pkt, packets_);
4065 packets_ = &tnl_pkt;
4066 dp_packet_batch_reset_cutlen(orig_packets_);
4069 dp_packet_batch_apply_cutlen(packets_);
4071 err = push_tnl_action(pmd, a, packets_);
4074 dp_netdev_recirculate(pmd, packets_);
4081 case OVS_ACTION_ATTR_TUNNEL_POP:
4082 if (*depth < MAX_RECIRC_DEPTH) {
4083 struct dp_packet_batch *orig_packets_ = packets_;
4084 odp_port_t portno = u32_to_odp(nl_attr_get_u32(a));
4086 p = pmd_tx_port_cache_lookup(pmd, portno);
4088 struct dp_packet_batch tnl_pkt;
4092 dp_packet_batch_clone(&tnl_pkt, packets_);
4093 packets_ = &tnl_pkt;
4094 dp_packet_batch_reset_cutlen(orig_packets_);
4097 dp_packet_batch_apply_cutlen(packets_);
4099 netdev_pop_header(p->netdev, packets_);
4100 if (!packets_->count) {
4104 for (i = 0; i < packets_->count; i++) {
4105 packets_->packets[i]->md.in_port.odp_port = portno;
4109 dp_netdev_recirculate(pmd, packets_);
4116 case OVS_ACTION_ATTR_USERSPACE:
4117 if (!fat_rwlock_tryrdlock(&dp->upcall_rwlock)) {
4118 struct dp_packet_batch *orig_packets_ = packets_;
4119 struct dp_packet **packets = packets_->packets;
4120 const struct nlattr *userdata;
4121 struct dp_packet_batch usr_pkt;
4122 struct ofpbuf actions;
4128 userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
4129 ofpbuf_init(&actions, 0);
4131 if (packets_->trunc) {
4133 dp_packet_batch_clone(&usr_pkt, packets_);
4134 packets_ = &usr_pkt;
4135 packets = packets_->packets;
4137 dp_packet_batch_reset_cutlen(orig_packets_);
4140 dp_packet_batch_apply_cutlen(packets_);
4143 for (i = 0; i < packets_->count; i++) {
4144 flow_extract(packets[i], &flow);
4145 dpif_flow_hash(dp->dpif, &flow, sizeof flow, &ufid);
4146 dp_execute_userspace_action(pmd, packets[i], may_steal, &flow,
4147 &ufid, &actions, userdata);
4151 dp_packet_delete_batch(packets_, true);
4154 ofpbuf_uninit(&actions);
4155 fat_rwlock_unlock(&dp->upcall_rwlock);
4161 case OVS_ACTION_ATTR_RECIRC:
4162 if (*depth < MAX_RECIRC_DEPTH) {
4163 struct dp_packet_batch recirc_pkts;
4167 dp_packet_batch_clone(&recirc_pkts, packets_);
4168 packets_ = &recirc_pkts;
4171 for (i = 0; i < packets_->count; i++) {
4172 packets_->packets[i]->md.recirc_id = nl_attr_get_u32(a);
4176 dp_netdev_recirculate(pmd, packets_);
4182 VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
4185 case OVS_ACTION_ATTR_CT:
4186 /* If a flow with this action is slow-pathed, datapath assistance is
4187 * required to implement it. However, we don't support this action
4188 * in the userspace datapath. */
4189 VLOG_WARN("Cannot execute conntrack action in userspace.");
4192 case OVS_ACTION_ATTR_PUSH_VLAN:
4193 case OVS_ACTION_ATTR_POP_VLAN:
4194 case OVS_ACTION_ATTR_PUSH_MPLS:
4195 case OVS_ACTION_ATTR_POP_MPLS:
4196 case OVS_ACTION_ATTR_SET:
4197 case OVS_ACTION_ATTR_SET_MASKED:
4198 case OVS_ACTION_ATTR_SAMPLE:
4199 case OVS_ACTION_ATTR_HASH:
4200 case OVS_ACTION_ATTR_UNSPEC:
4201 case OVS_ACTION_ATTR_TRUNC:
4202 case __OVS_ACTION_ATTR_MAX:
4206 dp_packet_delete_batch(packets_, may_steal);
4210 dp_netdev_execute_actions(struct dp_netdev_pmd_thread *pmd,
4211 struct dp_packet_batch *packets,
4213 const struct nlattr *actions, size_t actions_len)
4215 struct dp_netdev_execute_aux aux = { pmd };
4217 odp_execute_actions(&aux, packets, may_steal, actions,
4218 actions_len, dp_execute_cb);
4221 const struct dpif_class dpif_netdev_class = {
4224 dpif_netdev_enumerate,
4225 dpif_netdev_port_open_type,
4228 dpif_netdev_destroy,
4231 dpif_netdev_get_stats,
4232 dpif_netdev_port_add,
4233 dpif_netdev_port_del,
4234 dpif_netdev_port_query_by_number,
4235 dpif_netdev_port_query_by_name,
4236 NULL, /* port_get_pid */
4237 dpif_netdev_port_dump_start,
4238 dpif_netdev_port_dump_next,
4239 dpif_netdev_port_dump_done,
4240 dpif_netdev_port_poll,
4241 dpif_netdev_port_poll_wait,
4242 dpif_netdev_flow_flush,
4243 dpif_netdev_flow_dump_create,
4244 dpif_netdev_flow_dump_destroy,
4245 dpif_netdev_flow_dump_thread_create,
4246 dpif_netdev_flow_dump_thread_destroy,
4247 dpif_netdev_flow_dump_next,
4248 dpif_netdev_operate,
4249 NULL, /* recv_set */
4250 NULL, /* handlers_set */
4251 dpif_netdev_pmd_set,
4252 dpif_netdev_queue_to_priority,
4254 NULL, /* recv_wait */
4255 NULL, /* recv_purge */
4256 dpif_netdev_register_dp_purge_cb,
4257 dpif_netdev_register_upcall_cb,
4258 dpif_netdev_enable_upcall,
4259 dpif_netdev_disable_upcall,
4260 dpif_netdev_get_datapath_version,
4261 NULL, /* ct_dump_start */
4262 NULL, /* ct_dump_next */
4263 NULL, /* ct_dump_done */
4264 NULL, /* ct_flush */
4268 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
4269 const char *argv[], void *aux OVS_UNUSED)
4271 struct dp_netdev_port *port;
4272 struct dp_netdev *dp;
4275 ovs_mutex_lock(&dp_netdev_mutex);
4276 dp = shash_find_data(&dp_netdevs, argv[1]);
4277 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
4278 ovs_mutex_unlock(&dp_netdev_mutex);
4279 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
4282 ovs_refcount_ref(&dp->ref_cnt);
4283 ovs_mutex_unlock(&dp_netdev_mutex);
4285 ovs_mutex_lock(&dp->port_mutex);
4286 if (get_port_by_name(dp, argv[2], &port)) {
4287 unixctl_command_reply_error(conn, "unknown port");
4291 port_no = u32_to_odp(atoi(argv[3]));
4292 if (!port_no || port_no == ODPP_NONE) {
4293 unixctl_command_reply_error(conn, "bad port number");
4296 if (dp_netdev_lookup_port(dp, port_no)) {
4297 unixctl_command_reply_error(conn, "port number already in use");
4302 hmap_remove(&dp->ports, &port->node);
4303 dp_netdev_del_port_from_all_pmds(dp, port);
4305 /* Reinsert with new port number. */
4306 port->port_no = port_no;
4307 hmap_insert(&dp->ports, &port->node, hash_port_no(port_no));
4308 dp_netdev_add_port_to_pmds(dp, port);
4310 seq_change(dp->port_seq);
4311 unixctl_command_reply(conn, NULL);
4314 ovs_mutex_unlock(&dp->port_mutex);
4315 dp_netdev_unref(dp);
4319 dpif_dummy_register__(const char *type)
4321 struct dpif_class *class;
4323 class = xmalloc(sizeof *class);
4324 *class = dpif_netdev_class;
4325 class->type = xstrdup(type);
4326 dp_register_provider(class);
4330 dpif_dummy_override(const char *type)
4335 * Ignore EAFNOSUPPORT to allow --enable-dummy=system with
4336 * a userland-only build. It's useful for testsuite.
4338 error = dp_unregister_provider(type);
4339 if (error == 0 || error == EAFNOSUPPORT) {
4340 dpif_dummy_register__(type);
4345 dpif_dummy_register(enum dummy_level level)
4347 if (level == DUMMY_OVERRIDE_ALL) {
4352 dp_enumerate_types(&types);
4353 SSET_FOR_EACH (type, &types) {
4354 dpif_dummy_override(type);
4356 sset_destroy(&types);
4357 } else if (level == DUMMY_OVERRIDE_SYSTEM) {
4358 dpif_dummy_override("system");
4361 dpif_dummy_register__("dummy");
4363 unixctl_command_register("dpif-dummy/change-port-number",
4364 "dp port new-number",
4365 3, 3, dpif_dummy_change_port_number, NULL);
4368 /* Datapath Classifier. */
4370 /* A set of rules that all have the same fields wildcarded. */
4371 struct dpcls_subtable {
4372 /* The fields are only used by writers. */
4373 struct cmap_node cmap_node OVS_GUARDED; /* Within dpcls 'subtables_map'. */
4375 /* These fields are accessed by readers. */
4376 struct cmap rules; /* Contains "struct dpcls_rule"s. */
4377 struct netdev_flow_key mask; /* Wildcards for fields (const). */
4378 /* 'mask' must be the last field, additional space is allocated here. */
4381 /* Initializes 'cls' as a classifier that initially contains no classification
4384 dpcls_init(struct dpcls *cls)
4386 cmap_init(&cls->subtables_map);
4387 pvector_init(&cls->subtables);
4391 dpcls_destroy_subtable(struct dpcls *cls, struct dpcls_subtable *subtable)
4393 pvector_remove(&cls->subtables, subtable);
4394 cmap_remove(&cls->subtables_map, &subtable->cmap_node,
4395 subtable->mask.hash);
4396 cmap_destroy(&subtable->rules);
4397 ovsrcu_postpone(free, subtable);
4400 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
4401 * caller's responsibility.
4402 * May only be called after all the readers have been terminated. */
4404 dpcls_destroy(struct dpcls *cls)
4407 struct dpcls_subtable *subtable;
4409 CMAP_FOR_EACH (subtable, cmap_node, &cls->subtables_map) {
4410 ovs_assert(cmap_count(&subtable->rules) == 0);
4411 dpcls_destroy_subtable(cls, subtable);
4413 cmap_destroy(&cls->subtables_map);
4414 pvector_destroy(&cls->subtables);
4418 static struct dpcls_subtable *
4419 dpcls_create_subtable(struct dpcls *cls, const struct netdev_flow_key *mask)
4421 struct dpcls_subtable *subtable;
4423 /* Need to add one. */
4424 subtable = xmalloc(sizeof *subtable
4425 - sizeof subtable->mask.mf + mask->len);
4426 cmap_init(&subtable->rules);
4427 netdev_flow_key_clone(&subtable->mask, mask);
4428 cmap_insert(&cls->subtables_map, &subtable->cmap_node, mask->hash);
4429 pvector_insert(&cls->subtables, subtable, 0);
4430 pvector_publish(&cls->subtables);
4435 static inline struct dpcls_subtable *
4436 dpcls_find_subtable(struct dpcls *cls, const struct netdev_flow_key *mask)
4438 struct dpcls_subtable *subtable;
4440 CMAP_FOR_EACH_WITH_HASH (subtable, cmap_node, mask->hash,
4441 &cls->subtables_map) {
4442 if (netdev_flow_key_equal(&subtable->mask, mask)) {
4446 return dpcls_create_subtable(cls, mask);
4449 /* Insert 'rule' into 'cls'. */
4451 dpcls_insert(struct dpcls *cls, struct dpcls_rule *rule,
4452 const struct netdev_flow_key *mask)
4454 struct dpcls_subtable *subtable = dpcls_find_subtable(cls, mask);
4456 rule->mask = &subtable->mask;
4457 cmap_insert(&subtable->rules, &rule->cmap_node, rule->flow.hash);
4460 /* Removes 'rule' from 'cls', also destructing the 'rule'. */
4462 dpcls_remove(struct dpcls *cls, struct dpcls_rule *rule)
4464 struct dpcls_subtable *subtable;
4466 ovs_assert(rule->mask);
4468 INIT_CONTAINER(subtable, rule->mask, mask);
4470 if (cmap_remove(&subtable->rules, &rule->cmap_node, rule->flow.hash)
4472 dpcls_destroy_subtable(cls, subtable);
4473 pvector_publish(&cls->subtables);
4477 /* Returns true if 'target' satisfies 'key' in 'mask', that is, if each 1-bit
4478 * in 'mask' the values in 'key' and 'target' are the same. */
4480 dpcls_rule_matches_key(const struct dpcls_rule *rule,
4481 const struct netdev_flow_key *target)
4483 const uint64_t *keyp = miniflow_get_values(&rule->flow.mf);
4484 const uint64_t *maskp = miniflow_get_values(&rule->mask->mf);
4487 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value, target, rule->flow.mf.map) {
4488 if (OVS_UNLIKELY((value & *maskp++) != *keyp++)) {
4495 /* For each miniflow in 'flows' performs a classifier lookup writing the result
4496 * into the corresponding slot in 'rules'. If a particular entry in 'flows' is
4497 * NULL it is skipped.
4499 * This function is optimized for use in the userspace datapath and therefore
4500 * does not implement a lot of features available in the standard
4501 * classifier_lookup() function. Specifically, it does not implement
4502 * priorities, instead returning any rule which matches the flow.
4504 * Returns true if all flows found a corresponding rule. */
4506 dpcls_lookup(const struct dpcls *cls, const struct netdev_flow_key keys[],
4507 struct dpcls_rule **rules, const size_t cnt)
4509 /* The batch size 16 was experimentally found faster than 8 or 32. */
4510 typedef uint16_t map_type;
4511 #define MAP_BITS (sizeof(map_type) * CHAR_BIT)
4513 #if !defined(__CHECKER__) && !defined(_WIN32)
4514 const int N_MAPS = DIV_ROUND_UP(cnt, MAP_BITS);
4516 enum { N_MAPS = DIV_ROUND_UP(NETDEV_MAX_BURST, MAP_BITS) };
4518 map_type maps[N_MAPS];
4519 struct dpcls_subtable *subtable;
4521 memset(maps, 0xff, sizeof maps);
4522 if (cnt % MAP_BITS) {
4523 maps[N_MAPS - 1] >>= MAP_BITS - cnt % MAP_BITS; /* Clear extra bits. */
4525 memset(rules, 0, cnt * sizeof *rules);
4527 PVECTOR_FOR_EACH (subtable, &cls->subtables) {
4528 const struct netdev_flow_key *mkeys = keys;
4529 struct dpcls_rule **mrules = rules;
4530 map_type remains = 0;
4533 BUILD_ASSERT_DECL(sizeof remains == sizeof *maps);
4535 for (m = 0; m < N_MAPS; m++, mkeys += MAP_BITS, mrules += MAP_BITS) {
4536 uint32_t hashes[MAP_BITS];
4537 const struct cmap_node *nodes[MAP_BITS];
4538 unsigned long map = maps[m];
4542 continue; /* Skip empty maps. */
4545 /* Compute hashes for the remaining keys. */
4546 ULLONG_FOR_EACH_1(i, map) {
4547 hashes[i] = netdev_flow_key_hash_in_mask(&mkeys[i],
4551 map = cmap_find_batch(&subtable->rules, map, hashes, nodes);
4552 /* Check results. */
4553 ULLONG_FOR_EACH_1(i, map) {
4554 struct dpcls_rule *rule;
4556 CMAP_NODE_FOR_EACH (rule, cmap_node, nodes[i]) {
4557 if (OVS_LIKELY(dpcls_rule_matches_key(rule, &mkeys[i]))) {
4562 ULLONG_SET0(map, i); /* Did not match. */
4564 ; /* Keep Sparse happy. */
4566 maps[m] &= ~map; /* Clear the found rules. */
4570 return true; /* All found. */
4573 return false; /* Some misses. */