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>
36 #include "conntrack.h"
40 #include "dp-packet.h"
42 #include "dpif-provider.h"
44 #include "fat-rwlock.h"
49 #include "netdev-dpdk.h"
50 #include "netdev-vport.h"
52 #include "odp-execute.h"
54 #include "openvswitch/dynamic-string.h"
55 #include "openvswitch/list.h"
56 #include "openvswitch/match.h"
57 #include "openvswitch/ofp-print.h"
58 #include "openvswitch/ofp-util.h"
59 #include "openvswitch/ofpbuf.h"
60 #include "openvswitch/shash.h"
61 #include "openvswitch/vlog.h"
65 #include "poll-loop.h"
72 #include "tnl-neigh-cache.h"
73 #include "tnl-ports.h"
77 VLOG_DEFINE_THIS_MODULE(dpif_netdev
);
79 #define FLOW_DUMP_MAX_BATCH 50
80 /* Use per thread recirc_depth to prevent recirculation loop. */
81 #define MAX_RECIRC_DEPTH 5
82 DEFINE_STATIC_PER_THREAD_DATA(uint32_t, recirc_depth
, 0)
84 /* Configuration parameters. */
85 enum { MAX_FLOWS
= 65536 }; /* Maximum number of flows in flow table. */
87 /* Protects against changes to 'dp_netdevs'. */
88 static struct ovs_mutex dp_netdev_mutex
= OVS_MUTEX_INITIALIZER
;
90 /* Contains all 'struct dp_netdev's. */
91 static struct shash dp_netdevs
OVS_GUARDED_BY(dp_netdev_mutex
)
92 = SHASH_INITIALIZER(&dp_netdevs
);
94 static struct vlog_rate_limit upcall_rl
= VLOG_RATE_LIMIT_INIT(600, 600);
96 #define DP_NETDEV_CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \
97 | CS_INVALID | CS_REPLY_DIR | CS_TRACKED)
98 #define DP_NETDEV_CS_UNSUPPORTED_MASK (~(uint32_t)DP_NETDEV_CS_SUPPORTED_MASK)
100 static struct odp_support dp_netdev_support
= {
101 .max_mpls_depth
= SIZE_MAX
,
109 /* Stores a miniflow with inline values */
111 struct netdev_flow_key
{
112 uint32_t hash
; /* Hash function differs for different users. */
113 uint32_t len
; /* Length of the following miniflow (incl. map). */
115 uint64_t buf
[FLOW_MAX_PACKET_U64S
];
118 /* Exact match cache for frequently used flows
120 * The cache uses a 32-bit hash of the packet (which can be the RSS hash) to
121 * search its entries for a miniflow that matches exactly the miniflow of the
122 * packet. It stores the 'dpcls_rule' (rule) that matches the miniflow.
124 * A cache entry holds a reference to its 'dp_netdev_flow'.
126 * A miniflow with a given hash can be in one of EM_FLOW_HASH_SEGS different
127 * entries. The 32-bit hash is split into EM_FLOW_HASH_SEGS values (each of
128 * them is EM_FLOW_HASH_SHIFT bits wide and the remainder is thrown away). Each
129 * value is the index of a cache entry where the miniflow could be.
135 * Each pmd_thread has its own private exact match cache.
136 * If dp_netdev_input is not called from a pmd thread, a mutex is used.
139 #define EM_FLOW_HASH_SHIFT 13
140 #define EM_FLOW_HASH_ENTRIES (1u << EM_FLOW_HASH_SHIFT)
141 #define EM_FLOW_HASH_MASK (EM_FLOW_HASH_ENTRIES - 1)
142 #define EM_FLOW_HASH_SEGS 2
145 struct dp_netdev_flow
*flow
;
146 struct netdev_flow_key key
; /* key.hash used for emc hash value. */
150 struct emc_entry entries
[EM_FLOW_HASH_ENTRIES
];
151 int sweep_idx
; /* For emc_cache_slow_sweep(). */
154 /* Iterate in the exact match cache through every entry that might contain a
155 * miniflow with hash 'HASH'. */
156 #define EMC_FOR_EACH_POS_WITH_HASH(EMC, CURRENT_ENTRY, HASH) \
157 for (uint32_t i__ = 0, srch_hash__ = (HASH); \
158 (CURRENT_ENTRY) = &(EMC)->entries[srch_hash__ & EM_FLOW_HASH_MASK], \
159 i__ < EM_FLOW_HASH_SEGS; \
160 i__++, srch_hash__ >>= EM_FLOW_HASH_SHIFT)
162 /* Simple non-wildcarding single-priority classifier. */
165 struct cmap subtables_map
;
166 struct pvector
*subtables
;
169 /* A rule to be inserted to the classifier. */
171 struct cmap_node cmap_node
; /* Within struct dpcls_subtable 'rules'. */
172 struct netdev_flow_key
*mask
; /* Subtable's mask. */
173 struct netdev_flow_key flow
; /* Matching key. */
174 /* 'flow' must be the last field, additional space is allocated here. */
177 static void dpcls_init(struct dpcls
*);
178 static void dpcls_destroy(struct dpcls
*);
179 static void dpcls_insert(struct dpcls
*, struct dpcls_rule
*,
180 const struct netdev_flow_key
*mask
);
181 static void dpcls_remove(struct dpcls
*, struct dpcls_rule
*);
182 static bool dpcls_lookup(const struct dpcls
*cls
,
183 const struct netdev_flow_key keys
[],
184 struct dpcls_rule
**rules
, size_t cnt
);
186 /* Datapath based on the network device interface from netdev.h.
192 * Some members, marked 'const', are immutable. Accessing other members
193 * requires synchronization, as noted in more detail below.
195 * Acquisition order is, from outermost to innermost:
197 * dp_netdev_mutex (global)
202 const struct dpif_class
*const class;
203 const char *const name
;
205 struct ovs_refcount ref_cnt
;
206 atomic_flag destroyed
;
210 * Any lookup into 'ports' or any access to the dp_netdev_ports found
211 * through 'ports' requires taking 'port_mutex'. */
212 struct ovs_mutex port_mutex
;
214 struct seq
*port_seq
; /* Incremented whenever a port changes. */
216 /* Protects access to ofproto-dpif-upcall interface during revalidator
217 * thread synchronization. */
218 struct fat_rwlock upcall_rwlock
;
219 upcall_callback
*upcall_cb
; /* Callback function for executing upcalls. */
222 /* Callback function for notifying the purging of dp flows (during
223 * reseting pmd deletion). */
224 dp_purge_callback
*dp_purge_cb
;
227 /* Stores all 'struct dp_netdev_pmd_thread's. */
228 struct cmap poll_threads
;
230 /* Protects the access of the 'struct dp_netdev_pmd_thread'
231 * instance for non-pmd thread. */
232 struct ovs_mutex non_pmd_mutex
;
234 /* Each pmd thread will store its pointer to
235 * 'struct dp_netdev_pmd_thread' in 'per_pmd_key'. */
236 ovsthread_key_t per_pmd_key
;
238 struct seq
*reconfigure_seq
;
239 uint64_t last_reconfigure_seq
;
241 /* Cpu mask for pin of pmd threads. */
244 uint64_t last_tnl_conf_seq
;
246 struct conntrack conntrack
;
249 static struct dp_netdev_port
*dp_netdev_lookup_port(const struct dp_netdev
*dp
,
251 OVS_REQUIRES(dp
->port_mutex
);
254 DP_STAT_EXACT_HIT
, /* Packets that had an exact match (emc). */
255 DP_STAT_MASKED_HIT
, /* Packets that matched in the flow table. */
256 DP_STAT_MISS
, /* Packets that did not match. */
257 DP_STAT_LOST
, /* Packets not passed up to the client. */
261 enum pmd_cycles_counter_type
{
262 PMD_CYCLES_POLLING
, /* Cycles spent polling NICs. */
263 PMD_CYCLES_PROCESSING
, /* Cycles spent processing packets */
267 #define XPS_TIMEOUT_MS 500LL
269 /* Contained by struct dp_netdev_port's 'rxqs' member. */
270 struct dp_netdev_rxq
{
271 struct netdev_rxq
*rxq
;
272 unsigned core_id
; /* Сore to which this queue is pinned. */
275 /* A port in a netdev-based datapath. */
276 struct dp_netdev_port
{
278 struct netdev
*netdev
;
279 struct hmap_node node
; /* Node in dp_netdev's 'ports'. */
280 struct netdev_saved_flags
*sf
;
281 unsigned n_rxq
; /* Number of elements in 'rxq' */
282 struct dp_netdev_rxq
*rxqs
;
283 bool dynamic_txqs
; /* If true XPS will be used. */
284 unsigned *txq_used
; /* Number of threads that uses each tx queue. */
285 struct ovs_mutex txq_used_mutex
;
286 char *type
; /* Port type as requested by user. */
287 char *rxq_affinity_list
; /* Requested affinity of rx queues. */
290 /* Contained by struct dp_netdev_flow's 'stats' member. */
291 struct dp_netdev_flow_stats
{
292 atomic_llong used
; /* Last used time, in monotonic msecs. */
293 atomic_ullong packet_count
; /* Number of packets matched. */
294 atomic_ullong byte_count
; /* Number of bytes matched. */
295 atomic_uint16_t tcp_flags
; /* Bitwise-OR of seen tcp_flags values. */
298 /* A flow in 'dp_netdev_pmd_thread's 'flow_table'.
304 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
305 * its pmd thread's classifier. The text below calls this classifier 'cls'.
310 * The thread safety rules described here for "struct dp_netdev_flow" are
311 * motivated by two goals:
313 * - Prevent threads that read members of "struct dp_netdev_flow" from
314 * reading bad data due to changes by some thread concurrently modifying
317 * - Prevent two threads making changes to members of a given "struct
318 * dp_netdev_flow" from interfering with each other.
324 * A flow 'flow' may be accessed without a risk of being freed during an RCU
325 * grace period. Code that needs to hold onto a flow for a while
326 * should try incrementing 'flow->ref_cnt' with dp_netdev_flow_ref().
328 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
329 * flow from being deleted from 'cls' and it doesn't protect members of 'flow'
332 * Some members, marked 'const', are immutable. Accessing other members
333 * requires synchronization, as noted in more detail below.
335 struct dp_netdev_flow
{
336 const struct flow flow
; /* Unmasked flow that created this entry. */
337 /* Hash table index by unmasked flow. */
338 const struct cmap_node node
; /* In owning dp_netdev_pmd_thread's */
340 const ovs_u128 ufid
; /* Unique flow identifier. */
341 const unsigned pmd_id
; /* The 'core_id' of pmd thread owning this */
344 /* Number of references.
345 * The classifier owns one reference.
346 * Any thread trying to keep a rule from being freed should hold its own
348 struct ovs_refcount ref_cnt
;
353 struct dp_netdev_flow_stats stats
;
356 OVSRCU_TYPE(struct dp_netdev_actions
*) actions
;
358 /* While processing a group of input packets, the datapath uses the next
359 * member to store a pointer to the output batch for the flow. It is
360 * reset after the batch has been sent out (See dp_netdev_queue_batches(),
361 * packet_batch_per_flow_init() and packet_batch_per_flow_execute()). */
362 struct packet_batch_per_flow
*batch
;
364 /* Packet classification. */
365 struct dpcls_rule cr
; /* In owning dp_netdev's 'cls'. */
366 /* 'cr' must be the last member. */
369 static void dp_netdev_flow_unref(struct dp_netdev_flow
*);
370 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*);
371 static int dpif_netdev_flow_from_nlattrs(const struct nlattr
*, uint32_t,
374 /* A set of datapath actions within a "struct dp_netdev_flow".
380 * A struct dp_netdev_actions 'actions' is protected with RCU. */
381 struct dp_netdev_actions
{
382 /* These members are immutable: they do not change during the struct's
384 unsigned int size
; /* Size of 'actions', in bytes. */
385 struct nlattr actions
[]; /* Sequence of OVS_ACTION_ATTR_* attributes. */
388 struct dp_netdev_actions
*dp_netdev_actions_create(const struct nlattr
*,
390 struct dp_netdev_actions
*dp_netdev_flow_get_actions(
391 const struct dp_netdev_flow
*);
392 static void dp_netdev_actions_free(struct dp_netdev_actions
*);
394 /* Contained by struct dp_netdev_pmd_thread's 'stats' member. */
395 struct dp_netdev_pmd_stats
{
396 /* Indexed by DP_STAT_*. */
397 atomic_ullong n
[DP_N_STATS
];
400 /* Contained by struct dp_netdev_pmd_thread's 'cycle' member. */
401 struct dp_netdev_pmd_cycles
{
402 /* Indexed by PMD_CYCLES_*. */
403 atomic_ullong n
[PMD_N_CYCLES
];
406 /* Contained by struct dp_netdev_pmd_thread's 'poll_list' member. */
408 struct dp_netdev_port
*port
;
409 struct netdev_rxq
*rx
;
410 struct ovs_list node
;
413 /* Contained by struct dp_netdev_pmd_thread's 'port_cache' or 'tx_ports'. */
415 struct dp_netdev_port
*port
;
418 struct hmap_node node
;
421 /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
422 * the performance overhead of interrupt processing. Therefore netdev can
423 * not implement rx-wait for these devices. dpif-netdev needs to poll
424 * these device to check for recv buffer. pmd-thread does polling for
425 * devices assigned to itself.
427 * DPDK used PMD for accessing NIC.
429 * Note, instance with cpu core id NON_PMD_CORE_ID will be reserved for
430 * I/O of all non-pmd threads. There will be no actual thread created
433 * Each struct has its own flow table and classifier. Packets received
434 * from managed ports are looked up in the corresponding pmd thread's
435 * flow table, and are executed with the found actions.
437 struct dp_netdev_pmd_thread
{
438 struct dp_netdev
*dp
;
439 struct ovs_refcount ref_cnt
; /* Every reference must be refcount'ed. */
440 struct cmap_node node
; /* In 'dp->poll_threads'. */
442 pthread_cond_t cond
; /* For synchronizing pmd thread reload. */
443 struct ovs_mutex cond_mutex
; /* Mutex for condition variable. */
445 /* Per thread exact-match cache. Note, the instance for cpu core
446 * NON_PMD_CORE_ID can be accessed by multiple threads, and thusly
447 * need to be protected by 'non_pmd_mutex'. Every other instance
448 * will only be accessed by its own pmd thread. */
449 struct emc_cache flow_cache
;
451 /* Classifier and Flow-Table.
453 * Writers of 'flow_table' must take the 'flow_mutex'. Corresponding
454 * changes to 'cls' must be made while still holding the 'flow_mutex'.
456 struct ovs_mutex flow_mutex
;
458 struct cmap flow_table OVS_GUARDED
; /* Flow table. */
461 struct dp_netdev_pmd_stats stats
;
463 /* Cycles counters */
464 struct dp_netdev_pmd_cycles cycles
;
466 /* Used to count cicles. See 'cycles_counter_end()' */
467 unsigned long long last_cycles
;
469 struct latch exit_latch
; /* For terminating the pmd thread. */
470 atomic_uint change_seq
; /* For reloading pmd ports. */
472 unsigned core_id
; /* CPU core id of this pmd thread. */
473 int numa_id
; /* numa node id of this pmd thread. */
476 /* Queue id used by this pmd thread to send packets on all netdevs if
477 * XPS disabled for this netdev. All static_tx_qid's are unique and less
478 * than 'ovs_numa_get_n_cores() + 1'. */
479 atomic_int static_tx_qid
;
481 struct ovs_mutex port_mutex
; /* Mutex for 'poll_list' and 'tx_ports'. */
482 /* List of rx queues to poll. */
483 struct ovs_list poll_list OVS_GUARDED
;
484 /* Number of elements in 'poll_list' */
486 /* Map of 'tx_port's used for transmission. Written by the main thread,
487 * read by the pmd thread. */
488 struct hmap tx_ports OVS_GUARDED
;
490 /* Map of 'tx_port' used in the fast path. This is a thread-local copy of
491 * 'tx_ports'. The instance for cpu core NON_PMD_CORE_ID can be accessed
492 * by multiple threads, and thusly need to be protected by 'non_pmd_mutex'.
493 * Every other instance will only be accessed by its own pmd thread. */
494 struct hmap port_cache
;
496 /* Only a pmd thread can write on its own 'cycles' and 'stats'.
497 * The main thread keeps 'stats_zero' and 'cycles_zero' as base
498 * values and subtracts them from 'stats' and 'cycles' before
499 * reporting to the user */
500 unsigned long long stats_zero
[DP_N_STATS
];
501 uint64_t cycles_zero
[PMD_N_CYCLES
];
504 #define PMD_INITIAL_SEQ 1
506 /* Interface to netdev-based datapath. */
509 struct dp_netdev
*dp
;
510 uint64_t last_port_seq
;
513 static int get_port_by_number(struct dp_netdev
*dp
, odp_port_t port_no
,
514 struct dp_netdev_port
**portp
)
515 OVS_REQUIRES(dp
->port_mutex
);
516 static int get_port_by_name(struct dp_netdev
*dp
, const char *devname
,
517 struct dp_netdev_port
**portp
)
518 OVS_REQUIRES(dp
->port_mutex
);
519 static void dp_netdev_free(struct dp_netdev
*)
520 OVS_REQUIRES(dp_netdev_mutex
);
521 static int do_add_port(struct dp_netdev
*dp
, const char *devname
,
522 const char *type
, odp_port_t port_no
)
523 OVS_REQUIRES(dp
->port_mutex
);
524 static void do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*)
525 OVS_REQUIRES(dp
->port_mutex
);
526 static int dpif_netdev_open(const struct dpif_class
*, const char *name
,
527 bool create
, struct dpif
**);
528 static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
529 struct dp_packet_batch
*,
530 bool may_steal
, const struct flow
*flow
,
531 const struct nlattr
*actions
,
534 static void dp_netdev_input(struct dp_netdev_pmd_thread
*,
535 struct dp_packet_batch
*, odp_port_t port_no
);
536 static void dp_netdev_recirculate(struct dp_netdev_pmd_thread
*,
537 struct dp_packet_batch
*);
539 static void dp_netdev_disable_upcall(struct dp_netdev
*);
540 static void dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
);
541 static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
,
542 struct dp_netdev
*dp
, unsigned core_id
,
544 static void dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
);
545 static void dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
546 OVS_REQUIRES(dp
->port_mutex
);
548 static struct dp_netdev_pmd_thread
*dp_netdev_get_pmd(struct dp_netdev
*dp
,
550 static struct dp_netdev_pmd_thread
*
551 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
);
552 static void dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
);
553 static void dp_netdev_del_pmds_on_numa(struct dp_netdev
*dp
, int numa_id
);
554 static void dp_netdev_set_pmds_on_numa(struct dp_netdev
*dp
, int numa_id
)
555 OVS_REQUIRES(dp
->port_mutex
);
556 static void dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
);
557 static void dp_netdev_del_port_from_all_pmds(struct dp_netdev
*dp
,
558 struct dp_netdev_port
*port
);
559 static void dp_netdev_add_port_to_pmds(struct dp_netdev
*dp
,
560 struct dp_netdev_port
*port
);
561 static void dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
562 struct dp_netdev_port
*port
);
563 static void dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
564 struct dp_netdev_port
*port
,
565 struct netdev_rxq
*rx
);
566 static struct dp_netdev_pmd_thread
*
567 dp_netdev_less_loaded_pmd_on_numa(struct dp_netdev
*dp
, int numa_id
);
568 static void dp_netdev_reset_pmd_threads(struct dp_netdev
*dp
)
569 OVS_REQUIRES(dp
->port_mutex
);
570 static void reconfigure_pmd_threads(struct dp_netdev
*dp
)
571 OVS_REQUIRES(dp
->port_mutex
);
572 static bool dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
);
573 static void dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
);
574 static void dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
);
575 static void pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
576 OVS_REQUIRES(pmd
->port_mutex
);
579 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
580 long long now
, bool purge
);
581 static int dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
582 struct tx_port
*tx
, long long now
);
584 static inline bool emc_entry_alive(struct emc_entry
*ce
);
585 static void emc_clear_entry(struct emc_entry
*ce
);
588 emc_cache_init(struct emc_cache
*flow_cache
)
592 flow_cache
->sweep_idx
= 0;
593 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
594 flow_cache
->entries
[i
].flow
= NULL
;
595 flow_cache
->entries
[i
].key
.hash
= 0;
596 flow_cache
->entries
[i
].key
.len
= sizeof(struct miniflow
);
597 flowmap_init(&flow_cache
->entries
[i
].key
.mf
.map
);
602 emc_cache_uninit(struct emc_cache
*flow_cache
)
606 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
607 emc_clear_entry(&flow_cache
->entries
[i
]);
611 /* Check and clear dead flow references slowly (one entry at each
614 emc_cache_slow_sweep(struct emc_cache
*flow_cache
)
616 struct emc_entry
*entry
= &flow_cache
->entries
[flow_cache
->sweep_idx
];
618 if (!emc_entry_alive(entry
)) {
619 emc_clear_entry(entry
);
621 flow_cache
->sweep_idx
= (flow_cache
->sweep_idx
+ 1) & EM_FLOW_HASH_MASK
;
624 /* Returns true if 'dpif' is a netdev or dummy dpif, false otherwise. */
626 dpif_is_netdev(const struct dpif
*dpif
)
628 return dpif
->dpif_class
->open
== dpif_netdev_open
;
631 static struct dpif_netdev
*
632 dpif_netdev_cast(const struct dpif
*dpif
)
634 ovs_assert(dpif_is_netdev(dpif
));
635 return CONTAINER_OF(dpif
, struct dpif_netdev
, dpif
);
638 static struct dp_netdev
*
639 get_dp_netdev(const struct dpif
*dpif
)
641 return dpif_netdev_cast(dpif
)->dp
;
645 PMD_INFO_SHOW_STATS
, /* Show how cpu cycles are spent. */
646 PMD_INFO_CLEAR_STATS
, /* Set the cycles count to 0. */
647 PMD_INFO_SHOW_RXQ
/* Show poll-lists of pmd threads. */
651 pmd_info_show_stats(struct ds
*reply
,
652 struct dp_netdev_pmd_thread
*pmd
,
653 unsigned long long stats
[DP_N_STATS
],
654 uint64_t cycles
[PMD_N_CYCLES
])
656 unsigned long long total_packets
= 0;
657 uint64_t total_cycles
= 0;
660 /* These loops subtracts reference values ('*_zero') from the counters.
661 * Since loads and stores are relaxed, it might be possible for a '*_zero'
662 * value to be more recent than the current value we're reading from the
663 * counter. This is not a big problem, since these numbers are not
664 * supposed to be too accurate, but we should at least make sure that
665 * the result is not negative. */
666 for (i
= 0; i
< DP_N_STATS
; i
++) {
667 if (stats
[i
] > pmd
->stats_zero
[i
]) {
668 stats
[i
] -= pmd
->stats_zero
[i
];
673 if (i
!= DP_STAT_LOST
) {
674 /* Lost packets are already included in DP_STAT_MISS */
675 total_packets
+= stats
[i
];
679 for (i
= 0; i
< PMD_N_CYCLES
; i
++) {
680 if (cycles
[i
] > pmd
->cycles_zero
[i
]) {
681 cycles
[i
] -= pmd
->cycles_zero
[i
];
686 total_cycles
+= cycles
[i
];
689 ds_put_cstr(reply
, (pmd
->core_id
== NON_PMD_CORE_ID
)
690 ? "main thread" : "pmd thread");
692 if (pmd
->numa_id
!= OVS_NUMA_UNSPEC
) {
693 ds_put_format(reply
, " numa_id %d", pmd
->numa_id
);
695 if (pmd
->core_id
!= OVS_CORE_UNSPEC
&& pmd
->core_id
!= NON_PMD_CORE_ID
) {
696 ds_put_format(reply
, " core_id %u", pmd
->core_id
);
698 ds_put_cstr(reply
, ":\n");
701 "\temc hits:%llu\n\tmegaflow hits:%llu\n"
702 "\tmiss:%llu\n\tlost:%llu\n",
703 stats
[DP_STAT_EXACT_HIT
], stats
[DP_STAT_MASKED_HIT
],
704 stats
[DP_STAT_MISS
], stats
[DP_STAT_LOST
]);
706 if (total_cycles
== 0) {
711 "\tpolling cycles:%"PRIu64
" (%.02f%%)\n"
712 "\tprocessing cycles:%"PRIu64
" (%.02f%%)\n",
713 cycles
[PMD_CYCLES_POLLING
],
714 cycles
[PMD_CYCLES_POLLING
] / (double)total_cycles
* 100,
715 cycles
[PMD_CYCLES_PROCESSING
],
716 cycles
[PMD_CYCLES_PROCESSING
] / (double)total_cycles
* 100);
718 if (total_packets
== 0) {
723 "\tavg cycles per packet: %.02f (%"PRIu64
"/%llu)\n",
724 total_cycles
/ (double)total_packets
,
725 total_cycles
, total_packets
);
728 "\tavg processing cycles per packet: "
729 "%.02f (%"PRIu64
"/%llu)\n",
730 cycles
[PMD_CYCLES_PROCESSING
] / (double)total_packets
,
731 cycles
[PMD_CYCLES_PROCESSING
], total_packets
);
735 pmd_info_clear_stats(struct ds
*reply OVS_UNUSED
,
736 struct dp_netdev_pmd_thread
*pmd
,
737 unsigned long long stats
[DP_N_STATS
],
738 uint64_t cycles
[PMD_N_CYCLES
])
742 /* We cannot write 'stats' and 'cycles' (because they're written by other
743 * threads) and we shouldn't change 'stats' (because they're used to count
744 * datapath stats, which must not be cleared here). Instead, we save the
745 * current values and subtract them from the values to be displayed in the
747 for (i
= 0; i
< DP_N_STATS
; i
++) {
748 pmd
->stats_zero
[i
] = stats
[i
];
750 for (i
= 0; i
< PMD_N_CYCLES
; i
++) {
751 pmd
->cycles_zero
[i
] = cycles
[i
];
756 pmd_info_show_rxq(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
758 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
759 struct rxq_poll
*poll
;
760 const char *prev_name
= NULL
;
763 "pmd thread numa_id %d core_id %u:\n\tisolated : %s\n",
764 pmd
->numa_id
, pmd
->core_id
, (pmd
->isolated
)
767 ovs_mutex_lock(&pmd
->port_mutex
);
768 LIST_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
769 const char *name
= netdev_get_name(poll
->port
->netdev
);
771 if (!prev_name
|| strcmp(name
, prev_name
)) {
773 ds_put_cstr(reply
, "\n");
775 ds_put_format(reply
, "\tport: %s\tqueue-id:",
776 netdev_get_name(poll
->port
->netdev
));
778 ds_put_format(reply
, " %d", netdev_rxq_get_queue_id(poll
->rx
));
781 ovs_mutex_unlock(&pmd
->port_mutex
);
782 ds_put_cstr(reply
, "\n");
787 dpif_netdev_pmd_info(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
790 struct ds reply
= DS_EMPTY_INITIALIZER
;
791 struct dp_netdev_pmd_thread
*pmd
;
792 struct dp_netdev
*dp
= NULL
;
793 enum pmd_info_type type
= *(enum pmd_info_type
*) aux
;
795 ovs_mutex_lock(&dp_netdev_mutex
);
798 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
799 } else if (shash_count(&dp_netdevs
) == 1) {
800 /* There's only one datapath */
801 dp
= shash_first(&dp_netdevs
)->data
;
805 ovs_mutex_unlock(&dp_netdev_mutex
);
806 unixctl_command_reply_error(conn
,
807 "please specify an existing datapath");
811 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
812 if (type
== PMD_INFO_SHOW_RXQ
) {
813 pmd_info_show_rxq(&reply
, pmd
);
815 unsigned long long stats
[DP_N_STATS
];
816 uint64_t cycles
[PMD_N_CYCLES
];
819 /* Read current stats and cycle counters */
820 for (i
= 0; i
< ARRAY_SIZE(stats
); i
++) {
821 atomic_read_relaxed(&pmd
->stats
.n
[i
], &stats
[i
]);
823 for (i
= 0; i
< ARRAY_SIZE(cycles
); i
++) {
824 atomic_read_relaxed(&pmd
->cycles
.n
[i
], &cycles
[i
]);
827 if (type
== PMD_INFO_CLEAR_STATS
) {
828 pmd_info_clear_stats(&reply
, pmd
, stats
, cycles
);
829 } else if (type
== PMD_INFO_SHOW_STATS
) {
830 pmd_info_show_stats(&reply
, pmd
, stats
, cycles
);
835 ovs_mutex_unlock(&dp_netdev_mutex
);
837 unixctl_command_reply(conn
, ds_cstr(&reply
));
842 dpif_netdev_init(void)
844 static enum pmd_info_type show_aux
= PMD_INFO_SHOW_STATS
,
845 clear_aux
= PMD_INFO_CLEAR_STATS
,
846 poll_aux
= PMD_INFO_SHOW_RXQ
;
848 unixctl_command_register("dpif-netdev/pmd-stats-show", "[dp]",
849 0, 1, dpif_netdev_pmd_info
,
851 unixctl_command_register("dpif-netdev/pmd-stats-clear", "[dp]",
852 0, 1, dpif_netdev_pmd_info
,
854 unixctl_command_register("dpif-netdev/pmd-rxq-show", "[dp]",
855 0, 1, dpif_netdev_pmd_info
,
861 dpif_netdev_enumerate(struct sset
*all_dps
,
862 const struct dpif_class
*dpif_class
)
864 struct shash_node
*node
;
866 ovs_mutex_lock(&dp_netdev_mutex
);
867 SHASH_FOR_EACH(node
, &dp_netdevs
) {
868 struct dp_netdev
*dp
= node
->data
;
869 if (dpif_class
!= dp
->class) {
870 /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs.
871 * If the class doesn't match, skip this dpif. */
874 sset_add(all_dps
, node
->name
);
876 ovs_mutex_unlock(&dp_netdev_mutex
);
882 dpif_netdev_class_is_dummy(const struct dpif_class
*class)
884 return class != &dpif_netdev_class
;
888 dpif_netdev_port_open_type(const struct dpif_class
*class, const char *type
)
890 return strcmp(type
, "internal") ? type
891 : dpif_netdev_class_is_dummy(class) ? "dummy"
896 create_dpif_netdev(struct dp_netdev
*dp
)
898 uint16_t netflow_id
= hash_string(dp
->name
, 0);
899 struct dpif_netdev
*dpif
;
901 ovs_refcount_ref(&dp
->ref_cnt
);
903 dpif
= xmalloc(sizeof *dpif
);
904 dpif_init(&dpif
->dpif
, dp
->class, dp
->name
, netflow_id
>> 8, netflow_id
);
906 dpif
->last_port_seq
= seq_read(dp
->port_seq
);
911 /* Choose an unused, non-zero port number and return it on success.
912 * Return ODPP_NONE on failure. */
914 choose_port(struct dp_netdev
*dp
, const char *name
)
915 OVS_REQUIRES(dp
->port_mutex
)
919 if (dp
->class != &dpif_netdev_class
) {
923 /* If the port name begins with "br", start the number search at
924 * 100 to make writing tests easier. */
925 if (!strncmp(name
, "br", 2)) {
929 /* If the port name contains a number, try to assign that port number.
930 * This can make writing unit tests easier because port numbers are
932 for (p
= name
; *p
!= '\0'; p
++) {
933 if (isdigit((unsigned char) *p
)) {
934 port_no
= start_no
+ strtol(p
, NULL
, 10);
935 if (port_no
> 0 && port_no
!= odp_to_u32(ODPP_NONE
)
936 && !dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
937 return u32_to_odp(port_no
);
944 for (port_no
= 1; port_no
<= UINT16_MAX
; port_no
++) {
945 if (!dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
946 return u32_to_odp(port_no
);
954 create_dp_netdev(const char *name
, const struct dpif_class
*class,
955 struct dp_netdev
**dpp
)
956 OVS_REQUIRES(dp_netdev_mutex
)
958 struct dp_netdev
*dp
;
961 dp
= xzalloc(sizeof *dp
);
962 shash_add(&dp_netdevs
, name
, dp
);
964 *CONST_CAST(const struct dpif_class
**, &dp
->class) = class;
965 *CONST_CAST(const char **, &dp
->name
) = xstrdup(name
);
966 ovs_refcount_init(&dp
->ref_cnt
);
967 atomic_flag_clear(&dp
->destroyed
);
969 ovs_mutex_init(&dp
->port_mutex
);
970 hmap_init(&dp
->ports
);
971 dp
->port_seq
= seq_create();
972 fat_rwlock_init(&dp
->upcall_rwlock
);
974 dp
->reconfigure_seq
= seq_create();
975 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
977 /* Disable upcalls by default. */
978 dp_netdev_disable_upcall(dp
);
979 dp
->upcall_aux
= NULL
;
980 dp
->upcall_cb
= NULL
;
982 conntrack_init(&dp
->conntrack
);
984 cmap_init(&dp
->poll_threads
);
985 ovs_mutex_init_recursive(&dp
->non_pmd_mutex
);
986 ovsthread_key_create(&dp
->per_pmd_key
, NULL
);
988 ovs_mutex_lock(&dp
->port_mutex
);
989 dp_netdev_set_nonpmd(dp
);
991 error
= do_add_port(dp
, name
, dpif_netdev_port_open_type(dp
->class,
994 ovs_mutex_unlock(&dp
->port_mutex
);
1000 dp
->last_tnl_conf_seq
= seq_read(tnl_conf_seq
);
1006 dp_netdev_request_reconfigure(struct dp_netdev
*dp
)
1008 seq_change(dp
->reconfigure_seq
);
1012 dp_netdev_is_reconf_required(struct dp_netdev
*dp
)
1014 return seq_read(dp
->reconfigure_seq
) != dp
->last_reconfigure_seq
;
1018 dpif_netdev_open(const struct dpif_class
*class, const char *name
,
1019 bool create
, struct dpif
**dpifp
)
1021 struct dp_netdev
*dp
;
1024 ovs_mutex_lock(&dp_netdev_mutex
);
1025 dp
= shash_find_data(&dp_netdevs
, name
);
1027 error
= create
? create_dp_netdev(name
, class, &dp
) : ENODEV
;
1029 error
= (dp
->class != class ? EINVAL
1034 *dpifp
= create_dpif_netdev(dp
);
1037 ovs_mutex_unlock(&dp_netdev_mutex
);
1043 dp_netdev_destroy_upcall_lock(struct dp_netdev
*dp
)
1044 OVS_NO_THREAD_SAFETY_ANALYSIS
1046 /* Check that upcalls are disabled, i.e. that the rwlock is taken */
1047 ovs_assert(fat_rwlock_tryrdlock(&dp
->upcall_rwlock
));
1049 /* Before freeing a lock we should release it */
1050 fat_rwlock_unlock(&dp
->upcall_rwlock
);
1051 fat_rwlock_destroy(&dp
->upcall_rwlock
);
1054 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
1055 * through the 'dp_netdevs' shash while freeing 'dp'. */
1057 dp_netdev_free(struct dp_netdev
*dp
)
1058 OVS_REQUIRES(dp_netdev_mutex
)
1060 struct dp_netdev_port
*port
, *next
;
1062 shash_find_and_delete(&dp_netdevs
, dp
->name
);
1064 dp_netdev_destroy_all_pmds(dp
);
1065 ovs_mutex_destroy(&dp
->non_pmd_mutex
);
1066 ovsthread_key_delete(dp
->per_pmd_key
);
1068 conntrack_destroy(&dp
->conntrack
);
1070 ovs_mutex_lock(&dp
->port_mutex
);
1071 HMAP_FOR_EACH_SAFE (port
, next
, node
, &dp
->ports
) {
1072 do_del_port(dp
, port
);
1074 ovs_mutex_unlock(&dp
->port_mutex
);
1075 cmap_destroy(&dp
->poll_threads
);
1077 seq_destroy(dp
->reconfigure_seq
);
1079 seq_destroy(dp
->port_seq
);
1080 hmap_destroy(&dp
->ports
);
1081 ovs_mutex_destroy(&dp
->port_mutex
);
1083 /* Upcalls must be disabled at this point */
1084 dp_netdev_destroy_upcall_lock(dp
);
1086 free(dp
->pmd_cmask
);
1087 free(CONST_CAST(char *, dp
->name
));
1092 dp_netdev_unref(struct dp_netdev
*dp
)
1095 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
1096 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
1097 ovs_mutex_lock(&dp_netdev_mutex
);
1098 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1101 ovs_mutex_unlock(&dp_netdev_mutex
);
1106 dpif_netdev_close(struct dpif
*dpif
)
1108 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1110 dp_netdev_unref(dp
);
1115 dpif_netdev_destroy(struct dpif
*dpif
)
1117 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1119 if (!atomic_flag_test_and_set(&dp
->destroyed
)) {
1120 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1121 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
1129 /* Add 'n' to the atomic variable 'var' non-atomically and using relaxed
1130 * load/store semantics. While the increment is not atomic, the load and
1131 * store operations are, making it impossible to read inconsistent values.
1133 * This is used to update thread local stats counters. */
1135 non_atomic_ullong_add(atomic_ullong
*var
, unsigned long long n
)
1137 unsigned long long tmp
;
1139 atomic_read_relaxed(var
, &tmp
);
1141 atomic_store_relaxed(var
, tmp
);
1145 dpif_netdev_get_stats(const struct dpif
*dpif
, struct dpif_dp_stats
*stats
)
1147 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1148 struct dp_netdev_pmd_thread
*pmd
;
1150 stats
->n_flows
= stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
1151 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1152 unsigned long long n
;
1153 stats
->n_flows
+= cmap_count(&pmd
->flow_table
);
1155 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_MASKED_HIT
], &n
);
1157 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_EXACT_HIT
], &n
);
1159 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_MISS
], &n
);
1160 stats
->n_missed
+= n
;
1161 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_LOST
], &n
);
1164 stats
->n_masks
= UINT32_MAX
;
1165 stats
->n_mask_hit
= UINT64_MAX
;
1171 dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread
*pmd
)
1175 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
1176 ovs_mutex_lock(&pmd
->dp
->non_pmd_mutex
);
1177 ovs_mutex_lock(&pmd
->port_mutex
);
1178 pmd_load_cached_ports(pmd
);
1179 ovs_mutex_unlock(&pmd
->port_mutex
);
1180 ovs_mutex_unlock(&pmd
->dp
->non_pmd_mutex
);
1184 ovs_mutex_lock(&pmd
->cond_mutex
);
1185 atomic_add_relaxed(&pmd
->change_seq
, 1, &old_seq
);
1186 ovs_mutex_cond_wait(&pmd
->cond
, &pmd
->cond_mutex
);
1187 ovs_mutex_unlock(&pmd
->cond_mutex
);
1191 hash_port_no(odp_port_t port_no
)
1193 return hash_int(odp_to_u32(port_no
), 0);
1197 port_create(const char *devname
, const char *type
,
1198 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1200 struct netdev_saved_flags
*sf
;
1201 struct dp_netdev_port
*port
;
1202 enum netdev_flags flags
;
1203 struct netdev
*netdev
;
1204 int n_open_rxqs
= 0;
1207 bool dynamic_txqs
= false;
1211 /* Open and validate network device. */
1212 error
= netdev_open(devname
, type
, &netdev
);
1216 /* XXX reject non-Ethernet devices */
1218 netdev_get_flags(netdev
, &flags
);
1219 if (flags
& NETDEV_LOOPBACK
) {
1220 VLOG_ERR("%s: cannot add a loopback device", devname
);
1225 if (netdev_is_pmd(netdev
)) {
1226 n_cores
= ovs_numa_get_n_cores();
1228 if (n_cores
== OVS_CORE_UNSPEC
) {
1229 VLOG_ERR("%s, cannot get cpu core info", devname
);
1233 /* There can only be ovs_numa_get_n_cores() pmd threads,
1234 * so creates a txq for each, and one extra for the non
1236 error
= netdev_set_tx_multiq(netdev
, n_cores
+ 1);
1237 if (error
&& (error
!= EOPNOTSUPP
)) {
1238 VLOG_ERR("%s, cannot set multiq", devname
);
1243 if (netdev_is_reconf_required(netdev
)) {
1244 error
= netdev_reconfigure(netdev
);
1250 if (netdev_is_pmd(netdev
)) {
1251 if (netdev_n_txq(netdev
) < n_cores
+ 1) {
1252 dynamic_txqs
= true;
1256 port
= xzalloc(sizeof *port
);
1257 port
->port_no
= port_no
;
1258 port
->netdev
= netdev
;
1259 port
->n_rxq
= netdev_n_rxq(netdev
);
1260 port
->rxqs
= xcalloc(port
->n_rxq
, sizeof *port
->rxqs
);
1261 port
->txq_used
= xcalloc(netdev_n_txq(netdev
), sizeof *port
->txq_used
);
1262 port
->type
= xstrdup(type
);
1263 ovs_mutex_init(&port
->txq_used_mutex
);
1264 port
->dynamic_txqs
= dynamic_txqs
;
1266 for (i
= 0; i
< port
->n_rxq
; i
++) {
1267 error
= netdev_rxq_open(netdev
, &port
->rxqs
[i
].rxq
, i
);
1269 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
1270 devname
, ovs_strerror(errno
));
1273 port
->rxqs
[i
].core_id
= -1;
1277 error
= netdev_turn_flags_on(netdev
, NETDEV_PROMISC
, &sf
);
1288 for (i
= 0; i
< n_open_rxqs
; i
++) {
1289 netdev_rxq_close(port
->rxqs
[i
].rxq
);
1291 ovs_mutex_destroy(&port
->txq_used_mutex
);
1293 free(port
->txq_used
);
1298 netdev_close(netdev
);
1303 do_add_port(struct dp_netdev
*dp
, const char *devname
, const char *type
,
1305 OVS_REQUIRES(dp
->port_mutex
)
1307 struct dp_netdev_port
*port
;
1310 /* Reject devices already in 'dp'. */
1311 if (!get_port_by_name(dp
, devname
, &port
)) {
1315 error
= port_create(devname
, type
, port_no
, &port
);
1320 if (netdev_is_pmd(port
->netdev
)) {
1321 int numa_id
= netdev_get_numa_id(port
->netdev
);
1323 ovs_assert(ovs_numa_numa_id_is_valid(numa_id
));
1324 dp_netdev_set_pmds_on_numa(dp
, numa_id
);
1327 dp_netdev_add_port_to_pmds(dp
, port
);
1329 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
1330 seq_change(dp
->port_seq
);
1336 dpif_netdev_port_add(struct dpif
*dpif
, struct netdev
*netdev
,
1337 odp_port_t
*port_nop
)
1339 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1340 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1341 const char *dpif_port
;
1345 ovs_mutex_lock(&dp
->port_mutex
);
1346 dpif_port
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1347 if (*port_nop
!= ODPP_NONE
) {
1348 port_no
= *port_nop
;
1349 error
= dp_netdev_lookup_port(dp
, *port_nop
) ? EBUSY
: 0;
1351 port_no
= choose_port(dp
, dpif_port
);
1352 error
= port_no
== ODPP_NONE
? EFBIG
: 0;
1355 *port_nop
= port_no
;
1356 error
= do_add_port(dp
, dpif_port
, netdev_get_type(netdev
), port_no
);
1358 ovs_mutex_unlock(&dp
->port_mutex
);
1364 dpif_netdev_port_del(struct dpif
*dpif
, odp_port_t port_no
)
1366 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1369 ovs_mutex_lock(&dp
->port_mutex
);
1370 if (port_no
== ODPP_LOCAL
) {
1373 struct dp_netdev_port
*port
;
1375 error
= get_port_by_number(dp
, port_no
, &port
);
1377 do_del_port(dp
, port
);
1380 ovs_mutex_unlock(&dp
->port_mutex
);
1386 is_valid_port_number(odp_port_t port_no
)
1388 return port_no
!= ODPP_NONE
;
1391 static struct dp_netdev_port
*
1392 dp_netdev_lookup_port(const struct dp_netdev
*dp
, odp_port_t port_no
)
1393 OVS_REQUIRES(dp
->port_mutex
)
1395 struct dp_netdev_port
*port
;
1397 HMAP_FOR_EACH_WITH_HASH (port
, node
, hash_port_no(port_no
), &dp
->ports
) {
1398 if (port
->port_no
== port_no
) {
1406 get_port_by_number(struct dp_netdev
*dp
,
1407 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1408 OVS_REQUIRES(dp
->port_mutex
)
1410 if (!is_valid_port_number(port_no
)) {
1414 *portp
= dp_netdev_lookup_port(dp
, port_no
);
1415 return *portp
? 0 : ENOENT
;
1420 port_destroy(struct dp_netdev_port
*port
)
1426 netdev_close(port
->netdev
);
1427 netdev_restore_flags(port
->sf
);
1429 for (unsigned i
= 0; i
< port
->n_rxq
; i
++) {
1430 netdev_rxq_close(port
->rxqs
[i
].rxq
);
1432 ovs_mutex_destroy(&port
->txq_used_mutex
);
1433 free(port
->rxq_affinity_list
);
1434 free(port
->txq_used
);
1441 get_port_by_name(struct dp_netdev
*dp
,
1442 const char *devname
, struct dp_netdev_port
**portp
)
1443 OVS_REQUIRES(dp
->port_mutex
)
1445 struct dp_netdev_port
*port
;
1447 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1448 if (!strcmp(netdev_get_name(port
->netdev
), devname
)) {
1457 get_n_pmd_threads(struct dp_netdev
*dp
)
1459 /* There is one non pmd thread in dp->poll_threads */
1460 return cmap_count(&dp
->poll_threads
) - 1;
1464 get_n_pmd_threads_on_numa(struct dp_netdev
*dp
, int numa_id
)
1466 struct dp_netdev_pmd_thread
*pmd
;
1469 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1470 if (pmd
->numa_id
== numa_id
) {
1478 /* Returns 'true' if there is a port with pmd netdev and the netdev
1479 * is on numa node 'numa_id'. */
1481 has_pmd_port_for_numa(struct dp_netdev
*dp
, int numa_id
)
1482 OVS_REQUIRES(dp
->port_mutex
)
1484 struct dp_netdev_port
*port
;
1486 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1487 if (netdev_is_pmd(port
->netdev
)
1488 && netdev_get_numa_id(port
->netdev
) == numa_id
) {
1498 do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*port
)
1499 OVS_REQUIRES(dp
->port_mutex
)
1501 hmap_remove(&dp
->ports
, &port
->node
);
1502 seq_change(dp
->port_seq
);
1504 dp_netdev_del_port_from_all_pmds(dp
, port
);
1506 if (netdev_is_pmd(port
->netdev
)) {
1507 int numa_id
= netdev_get_numa_id(port
->netdev
);
1509 /* PMD threads can not be on invalid numa node. */
1510 ovs_assert(ovs_numa_numa_id_is_valid(numa_id
));
1511 /* If there is no netdev on the numa node, deletes the pmd threads
1513 if (!has_pmd_port_for_numa(dp
, numa_id
)) {
1514 dp_netdev_del_pmds_on_numa(dp
, numa_id
);
1522 answer_port_query(const struct dp_netdev_port
*port
,
1523 struct dpif_port
*dpif_port
)
1525 dpif_port
->name
= xstrdup(netdev_get_name(port
->netdev
));
1526 dpif_port
->type
= xstrdup(port
->type
);
1527 dpif_port
->port_no
= port
->port_no
;
1531 dpif_netdev_port_query_by_number(const struct dpif
*dpif
, odp_port_t port_no
,
1532 struct dpif_port
*dpif_port
)
1534 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1535 struct dp_netdev_port
*port
;
1538 ovs_mutex_lock(&dp
->port_mutex
);
1539 error
= get_port_by_number(dp
, port_no
, &port
);
1540 if (!error
&& dpif_port
) {
1541 answer_port_query(port
, dpif_port
);
1543 ovs_mutex_unlock(&dp
->port_mutex
);
1549 dpif_netdev_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
1550 struct dpif_port
*dpif_port
)
1552 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1553 struct dp_netdev_port
*port
;
1556 ovs_mutex_lock(&dp
->port_mutex
);
1557 error
= get_port_by_name(dp
, devname
, &port
);
1558 if (!error
&& dpif_port
) {
1559 answer_port_query(port
, dpif_port
);
1561 ovs_mutex_unlock(&dp
->port_mutex
);
1567 dp_netdev_flow_free(struct dp_netdev_flow
*flow
)
1569 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow
));
1573 static void dp_netdev_flow_unref(struct dp_netdev_flow
*flow
)
1575 if (ovs_refcount_unref_relaxed(&flow
->ref_cnt
) == 1) {
1576 ovsrcu_postpone(dp_netdev_flow_free
, flow
);
1581 dp_netdev_flow_hash(const ovs_u128
*ufid
)
1583 return ufid
->u32
[0];
1587 dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread
*pmd
,
1588 struct dp_netdev_flow
*flow
)
1589 OVS_REQUIRES(pmd
->flow_mutex
)
1591 struct cmap_node
*node
= CONST_CAST(struct cmap_node
*, &flow
->node
);
1593 dpcls_remove(&pmd
->cls
, &flow
->cr
);
1594 cmap_remove(&pmd
->flow_table
, node
, dp_netdev_flow_hash(&flow
->ufid
));
1597 dp_netdev_flow_unref(flow
);
1601 dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
)
1603 struct dp_netdev_flow
*netdev_flow
;
1605 ovs_mutex_lock(&pmd
->flow_mutex
);
1606 CMAP_FOR_EACH (netdev_flow
, node
, &pmd
->flow_table
) {
1607 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
1609 ovs_mutex_unlock(&pmd
->flow_mutex
);
1613 dpif_netdev_flow_flush(struct dpif
*dpif
)
1615 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1616 struct dp_netdev_pmd_thread
*pmd
;
1618 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1619 dp_netdev_pmd_flow_flush(pmd
);
1625 struct dp_netdev_port_state
{
1626 struct hmap_position position
;
1631 dpif_netdev_port_dump_start(const struct dpif
*dpif OVS_UNUSED
, void **statep
)
1633 *statep
= xzalloc(sizeof(struct dp_netdev_port_state
));
1638 dpif_netdev_port_dump_next(const struct dpif
*dpif
, void *state_
,
1639 struct dpif_port
*dpif_port
)
1641 struct dp_netdev_port_state
*state
= state_
;
1642 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1643 struct hmap_node
*node
;
1646 ovs_mutex_lock(&dp
->port_mutex
);
1647 node
= hmap_at_position(&dp
->ports
, &state
->position
);
1649 struct dp_netdev_port
*port
;
1651 port
= CONTAINER_OF(node
, struct dp_netdev_port
, node
);
1654 state
->name
= xstrdup(netdev_get_name(port
->netdev
));
1655 dpif_port
->name
= state
->name
;
1656 dpif_port
->type
= port
->type
;
1657 dpif_port
->port_no
= port
->port_no
;
1663 ovs_mutex_unlock(&dp
->port_mutex
);
1669 dpif_netdev_port_dump_done(const struct dpif
*dpif OVS_UNUSED
, void *state_
)
1671 struct dp_netdev_port_state
*state
= state_
;
1678 dpif_netdev_port_poll(const struct dpif
*dpif_
, char **devnamep OVS_UNUSED
)
1680 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
1681 uint64_t new_port_seq
;
1684 new_port_seq
= seq_read(dpif
->dp
->port_seq
);
1685 if (dpif
->last_port_seq
!= new_port_seq
) {
1686 dpif
->last_port_seq
= new_port_seq
;
1696 dpif_netdev_port_poll_wait(const struct dpif
*dpif_
)
1698 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
1700 seq_wait(dpif
->dp
->port_seq
, dpif
->last_port_seq
);
1703 static struct dp_netdev_flow
*
1704 dp_netdev_flow_cast(const struct dpcls_rule
*cr
)
1706 return cr
? CONTAINER_OF(cr
, struct dp_netdev_flow
, cr
) : NULL
;
1709 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*flow
)
1711 return ovs_refcount_try_ref_rcu(&flow
->ref_cnt
);
1714 /* netdev_flow_key utilities.
1716 * netdev_flow_key is basically a miniflow. We use these functions
1717 * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow
1718 * functions (miniflow_clone_inline, miniflow_equal, ...), because:
1720 * - Since we are dealing exclusively with miniflows created by
1721 * miniflow_extract(), if the map is different the miniflow is different.
1722 * Therefore we can be faster by comparing the map and the miniflow in a
1724 * - These functions can be inlined by the compiler. */
1726 /* Given the number of bits set in miniflow's maps, returns the size of the
1727 * 'netdev_flow_key.mf' */
1728 static inline size_t
1729 netdev_flow_key_size(size_t flow_u64s
)
1731 return sizeof(struct miniflow
) + MINIFLOW_VALUES_SIZE(flow_u64s
);
1735 netdev_flow_key_equal(const struct netdev_flow_key
*a
,
1736 const struct netdev_flow_key
*b
)
1738 /* 'b->len' may be not set yet. */
1739 return a
->hash
== b
->hash
&& !memcmp(&a
->mf
, &b
->mf
, a
->len
);
1742 /* Used to compare 'netdev_flow_key' in the exact match cache to a miniflow.
1743 * The maps are compared bitwise, so both 'key->mf' and 'mf' must have been
1744 * generated by miniflow_extract. */
1746 netdev_flow_key_equal_mf(const struct netdev_flow_key
*key
,
1747 const struct miniflow
*mf
)
1749 return !memcmp(&key
->mf
, mf
, key
->len
);
1753 netdev_flow_key_clone(struct netdev_flow_key
*dst
,
1754 const struct netdev_flow_key
*src
)
1757 offsetof(struct netdev_flow_key
, mf
) + src
->len
);
1762 netdev_flow_key_from_flow(struct netdev_flow_key
*dst
,
1763 const struct flow
*src
)
1765 struct dp_packet packet
;
1766 uint64_t buf_stub
[512 / 8];
1768 dp_packet_use_stub(&packet
, buf_stub
, sizeof buf_stub
);
1769 pkt_metadata_from_flow(&packet
.md
, src
);
1770 flow_compose(&packet
, src
);
1771 miniflow_extract(&packet
, &dst
->mf
);
1772 dp_packet_uninit(&packet
);
1774 dst
->len
= netdev_flow_key_size(miniflow_n_values(&dst
->mf
));
1775 dst
->hash
= 0; /* Not computed yet. */
1778 /* Initialize a netdev_flow_key 'mask' from 'match'. */
1780 netdev_flow_mask_init(struct netdev_flow_key
*mask
,
1781 const struct match
*match
)
1783 uint64_t *dst
= miniflow_values(&mask
->mf
);
1784 struct flowmap fmap
;
1788 /* Only check masks that make sense for the flow. */
1789 flow_wc_map(&match
->flow
, &fmap
);
1790 flowmap_init(&mask
->mf
.map
);
1792 FLOWMAP_FOR_EACH_INDEX(idx
, fmap
) {
1793 uint64_t mask_u64
= flow_u64_value(&match
->wc
.masks
, idx
);
1796 flowmap_set(&mask
->mf
.map
, idx
, 1);
1798 hash
= hash_add64(hash
, mask_u64
);
1804 FLOWMAP_FOR_EACH_MAP (map
, mask
->mf
.map
) {
1805 hash
= hash_add64(hash
, map
);
1808 size_t n
= dst
- miniflow_get_values(&mask
->mf
);
1810 mask
->hash
= hash_finish(hash
, n
* 8);
1811 mask
->len
= netdev_flow_key_size(n
);
1814 /* Initializes 'dst' as a copy of 'flow' masked with 'mask'. */
1816 netdev_flow_key_init_masked(struct netdev_flow_key
*dst
,
1817 const struct flow
*flow
,
1818 const struct netdev_flow_key
*mask
)
1820 uint64_t *dst_u64
= miniflow_values(&dst
->mf
);
1821 const uint64_t *mask_u64
= miniflow_get_values(&mask
->mf
);
1825 dst
->len
= mask
->len
;
1826 dst
->mf
= mask
->mf
; /* Copy maps. */
1828 FLOW_FOR_EACH_IN_MAPS(value
, flow
, mask
->mf
.map
) {
1829 *dst_u64
= value
& *mask_u64
++;
1830 hash
= hash_add64(hash
, *dst_u64
++);
1832 dst
->hash
= hash_finish(hash
,
1833 (dst_u64
- miniflow_get_values(&dst
->mf
)) * 8);
1836 /* Iterate through netdev_flow_key TNL u64 values specified by 'FLOWMAP'. */
1837 #define NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(VALUE, KEY, FLOWMAP) \
1838 MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, &(KEY)->mf, FLOWMAP)
1840 /* Returns a hash value for the bits of 'key' where there are 1-bits in
1842 static inline uint32_t
1843 netdev_flow_key_hash_in_mask(const struct netdev_flow_key
*key
,
1844 const struct netdev_flow_key
*mask
)
1846 const uint64_t *p
= miniflow_get_values(&mask
->mf
);
1850 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, key
, mask
->mf
.map
) {
1851 hash
= hash_add64(hash
, value
& *p
++);
1854 return hash_finish(hash
, (p
- miniflow_get_values(&mask
->mf
)) * 8);
1858 emc_entry_alive(struct emc_entry
*ce
)
1860 return ce
->flow
&& !ce
->flow
->dead
;
1864 emc_clear_entry(struct emc_entry
*ce
)
1867 dp_netdev_flow_unref(ce
->flow
);
1873 emc_change_entry(struct emc_entry
*ce
, struct dp_netdev_flow
*flow
,
1874 const struct netdev_flow_key
*key
)
1876 if (ce
->flow
!= flow
) {
1878 dp_netdev_flow_unref(ce
->flow
);
1881 if (dp_netdev_flow_ref(flow
)) {
1888 netdev_flow_key_clone(&ce
->key
, key
);
1893 emc_insert(struct emc_cache
*cache
, const struct netdev_flow_key
*key
,
1894 struct dp_netdev_flow
*flow
)
1896 struct emc_entry
*to_be_replaced
= NULL
;
1897 struct emc_entry
*current_entry
;
1899 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
1900 if (netdev_flow_key_equal(¤t_entry
->key
, key
)) {
1901 /* We found the entry with the 'mf' miniflow */
1902 emc_change_entry(current_entry
, flow
, NULL
);
1906 /* Replacement policy: put the flow in an empty (not alive) entry, or
1907 * in the first entry where it can be */
1909 || (emc_entry_alive(to_be_replaced
)
1910 && !emc_entry_alive(current_entry
))
1911 || current_entry
->key
.hash
< to_be_replaced
->key
.hash
) {
1912 to_be_replaced
= current_entry
;
1915 /* We didn't find the miniflow in the cache.
1916 * The 'to_be_replaced' entry is where the new flow will be stored */
1918 emc_change_entry(to_be_replaced
, flow
, key
);
1921 static inline struct dp_netdev_flow
*
1922 emc_lookup(struct emc_cache
*cache
, const struct netdev_flow_key
*key
)
1924 struct emc_entry
*current_entry
;
1926 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
1927 if (current_entry
->key
.hash
== key
->hash
1928 && emc_entry_alive(current_entry
)
1929 && netdev_flow_key_equal_mf(¤t_entry
->key
, &key
->mf
)) {
1931 /* We found the entry with the 'key->mf' miniflow */
1932 return current_entry
->flow
;
1939 static struct dp_netdev_flow
*
1940 dp_netdev_pmd_lookup_flow(const struct dp_netdev_pmd_thread
*pmd
,
1941 const struct netdev_flow_key
*key
)
1943 struct dp_netdev_flow
*netdev_flow
;
1944 struct dpcls_rule
*rule
;
1946 dpcls_lookup(&pmd
->cls
, key
, &rule
, 1);
1947 netdev_flow
= dp_netdev_flow_cast(rule
);
1952 static struct dp_netdev_flow
*
1953 dp_netdev_pmd_find_flow(const struct dp_netdev_pmd_thread
*pmd
,
1954 const ovs_u128
*ufidp
, const struct nlattr
*key
,
1957 struct dp_netdev_flow
*netdev_flow
;
1961 /* If a UFID is not provided, determine one based on the key. */
1962 if (!ufidp
&& key
&& key_len
1963 && !dpif_netdev_flow_from_nlattrs(key
, key_len
, &flow
)) {
1964 dpif_flow_hash(pmd
->dp
->dpif
, &flow
, sizeof flow
, &ufid
);
1969 CMAP_FOR_EACH_WITH_HASH (netdev_flow
, node
, dp_netdev_flow_hash(ufidp
),
1971 if (ovs_u128_equals(netdev_flow
->ufid
, *ufidp
)) {
1981 get_dpif_flow_stats(const struct dp_netdev_flow
*netdev_flow_
,
1982 struct dpif_flow_stats
*stats
)
1984 struct dp_netdev_flow
*netdev_flow
;
1985 unsigned long long n
;
1989 netdev_flow
= CONST_CAST(struct dp_netdev_flow
*, netdev_flow_
);
1991 atomic_read_relaxed(&netdev_flow
->stats
.packet_count
, &n
);
1992 stats
->n_packets
= n
;
1993 atomic_read_relaxed(&netdev_flow
->stats
.byte_count
, &n
);
1995 atomic_read_relaxed(&netdev_flow
->stats
.used
, &used
);
1997 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
1998 stats
->tcp_flags
= flags
;
2001 /* Converts to the dpif_flow format, using 'key_buf' and 'mask_buf' for
2002 * storing the netlink-formatted key/mask. 'key_buf' may be the same as
2003 * 'mask_buf'. Actions will be returned without copying, by relying on RCU to
2006 dp_netdev_flow_to_dpif_flow(const struct dp_netdev_flow
*netdev_flow
,
2007 struct ofpbuf
*key_buf
, struct ofpbuf
*mask_buf
,
2008 struct dpif_flow
*flow
, bool terse
)
2011 memset(flow
, 0, sizeof *flow
);
2013 struct flow_wildcards wc
;
2014 struct dp_netdev_actions
*actions
;
2016 struct odp_flow_key_parms odp_parms
= {
2017 .flow
= &netdev_flow
->flow
,
2019 .support
= dp_netdev_support
,
2022 miniflow_expand(&netdev_flow
->cr
.mask
->mf
, &wc
.masks
);
2025 offset
= key_buf
->size
;
2026 flow
->key
= ofpbuf_tail(key_buf
);
2027 odp_flow_key_from_flow(&odp_parms
, key_buf
);
2028 flow
->key_len
= key_buf
->size
- offset
;
2031 offset
= mask_buf
->size
;
2032 flow
->mask
= ofpbuf_tail(mask_buf
);
2033 odp_parms
.key_buf
= key_buf
;
2034 odp_flow_key_from_mask(&odp_parms
, mask_buf
);
2035 flow
->mask_len
= mask_buf
->size
- offset
;
2038 actions
= dp_netdev_flow_get_actions(netdev_flow
);
2039 flow
->actions
= actions
->actions
;
2040 flow
->actions_len
= actions
->size
;
2043 flow
->ufid
= netdev_flow
->ufid
;
2044 flow
->ufid_present
= true;
2045 flow
->pmd_id
= netdev_flow
->pmd_id
;
2046 get_dpif_flow_stats(netdev_flow
, &flow
->stats
);
2050 dpif_netdev_mask_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
2051 const struct nlattr
*mask_key
,
2052 uint32_t mask_key_len
, const struct flow
*flow
,
2053 struct flow_wildcards
*wc
)
2055 enum odp_key_fitness fitness
;
2057 fitness
= odp_flow_key_to_mask_udpif(mask_key
, mask_key_len
, key
,
2060 /* This should not happen: it indicates that
2061 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
2062 * disagree on the acceptable form of a mask. Log the problem
2063 * as an error, with enough details to enable debugging. */
2064 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2066 if (!VLOG_DROP_ERR(&rl
)) {
2070 odp_flow_format(key
, key_len
, mask_key
, mask_key_len
, NULL
, &s
,
2072 VLOG_ERR("internal error parsing flow mask %s (%s)",
2073 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
2084 dpif_netdev_flow_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
2089 if (odp_flow_key_to_flow_udpif(key
, key_len
, flow
)) {
2090 /* This should not happen: it indicates that odp_flow_key_from_flow()
2091 * and odp_flow_key_to_flow() disagree on the acceptable form of a
2092 * flow. Log the problem as an error, with enough details to enable
2094 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2096 if (!VLOG_DROP_ERR(&rl
)) {
2100 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
2101 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
2108 in_port
= flow
->in_port
.odp_port
;
2109 if (!is_valid_port_number(in_port
) && in_port
!= ODPP_NONE
) {
2113 if (flow
->ct_state
& DP_NETDEV_CS_UNSUPPORTED_MASK
) {
2121 dpif_netdev_flow_get(const struct dpif
*dpif
, const struct dpif_flow_get
*get
)
2123 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2124 struct dp_netdev_flow
*netdev_flow
;
2125 struct dp_netdev_pmd_thread
*pmd
;
2126 struct hmapx to_find
= HMAPX_INITIALIZER(&to_find
);
2127 struct hmapx_node
*node
;
2130 if (get
->pmd_id
== PMD_ID_NULL
) {
2131 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2132 if (dp_netdev_pmd_try_ref(pmd
) && !hmapx_add(&to_find
, pmd
)) {
2133 dp_netdev_pmd_unref(pmd
);
2137 pmd
= dp_netdev_get_pmd(dp
, get
->pmd_id
);
2141 hmapx_add(&to_find
, pmd
);
2144 if (!hmapx_count(&to_find
)) {
2148 HMAPX_FOR_EACH (node
, &to_find
) {
2149 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
2150 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, get
->ufid
, get
->key
,
2153 dp_netdev_flow_to_dpif_flow(netdev_flow
, get
->buffer
, get
->buffer
,
2162 HMAPX_FOR_EACH (node
, &to_find
) {
2163 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
2164 dp_netdev_pmd_unref(pmd
);
2167 hmapx_destroy(&to_find
);
2171 static struct dp_netdev_flow
*
2172 dp_netdev_flow_add(struct dp_netdev_pmd_thread
*pmd
,
2173 struct match
*match
, const ovs_u128
*ufid
,
2174 const struct nlattr
*actions
, size_t actions_len
)
2175 OVS_REQUIRES(pmd
->flow_mutex
)
2177 struct dp_netdev_flow
*flow
;
2178 struct netdev_flow_key mask
;
2180 netdev_flow_mask_init(&mask
, match
);
2181 /* Make sure wc does not have metadata. */
2182 ovs_assert(!FLOWMAP_HAS_FIELD(&mask
.mf
.map
, metadata
)
2183 && !FLOWMAP_HAS_FIELD(&mask
.mf
.map
, regs
));
2185 /* Do not allocate extra space. */
2186 flow
= xmalloc(sizeof *flow
- sizeof flow
->cr
.flow
.mf
+ mask
.len
);
2187 memset(&flow
->stats
, 0, sizeof flow
->stats
);
2190 *CONST_CAST(unsigned *, &flow
->pmd_id
) = pmd
->core_id
;
2191 *CONST_CAST(struct flow
*, &flow
->flow
) = match
->flow
;
2192 *CONST_CAST(ovs_u128
*, &flow
->ufid
) = *ufid
;
2193 ovs_refcount_init(&flow
->ref_cnt
);
2194 ovsrcu_set(&flow
->actions
, dp_netdev_actions_create(actions
, actions_len
));
2196 netdev_flow_key_init_masked(&flow
->cr
.flow
, &match
->flow
, &mask
);
2197 dpcls_insert(&pmd
->cls
, &flow
->cr
, &mask
);
2199 cmap_insert(&pmd
->flow_table
, CONST_CAST(struct cmap_node
*, &flow
->node
),
2200 dp_netdev_flow_hash(&flow
->ufid
));
2202 if (OVS_UNLIKELY(VLOG_IS_DBG_ENABLED())) {
2203 struct ds ds
= DS_EMPTY_INITIALIZER
;
2204 struct ofpbuf key_buf
, mask_buf
;
2205 struct odp_flow_key_parms odp_parms
= {
2206 .flow
= &match
->flow
,
2207 .mask
= &match
->wc
.masks
,
2208 .support
= dp_netdev_support
,
2211 ofpbuf_init(&key_buf
, 0);
2212 ofpbuf_init(&mask_buf
, 0);
2214 odp_flow_key_from_flow(&odp_parms
, &key_buf
);
2215 odp_parms
.key_buf
= &key_buf
;
2216 odp_flow_key_from_mask(&odp_parms
, &mask_buf
);
2218 ds_put_cstr(&ds
, "flow_add: ");
2219 odp_format_ufid(ufid
, &ds
);
2220 ds_put_cstr(&ds
, " ");
2221 odp_flow_format(key_buf
.data
, key_buf
.size
,
2222 mask_buf
.data
, mask_buf
.size
,
2224 ds_put_cstr(&ds
, ", actions:");
2225 format_odp_actions(&ds
, actions
, actions_len
);
2227 VLOG_DBG_RL(&upcall_rl
, "%s", ds_cstr(&ds
));
2229 ofpbuf_uninit(&key_buf
);
2230 ofpbuf_uninit(&mask_buf
);
2238 dpif_netdev_flow_put(struct dpif
*dpif
, const struct dpif_flow_put
*put
)
2240 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2241 struct dp_netdev_flow
*netdev_flow
;
2242 struct netdev_flow_key key
;
2243 struct dp_netdev_pmd_thread
*pmd
;
2246 unsigned pmd_id
= put
->pmd_id
== PMD_ID_NULL
2247 ? NON_PMD_CORE_ID
: put
->pmd_id
;
2250 error
= dpif_netdev_flow_from_nlattrs(put
->key
, put
->key_len
, &match
.flow
);
2254 error
= dpif_netdev_mask_from_nlattrs(put
->key
, put
->key_len
,
2255 put
->mask
, put
->mask_len
,
2256 &match
.flow
, &match
.wc
);
2261 pmd
= dp_netdev_get_pmd(dp
, pmd_id
);
2266 /* Must produce a netdev_flow_key for lookup.
2267 * This interface is no longer performance critical, since it is not used
2268 * for upcall processing any more. */
2269 netdev_flow_key_from_flow(&key
, &match
.flow
);
2274 dpif_flow_hash(dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
2277 ovs_mutex_lock(&pmd
->flow_mutex
);
2278 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, &key
);
2280 if (put
->flags
& DPIF_FP_CREATE
) {
2281 if (cmap_count(&pmd
->flow_table
) < MAX_FLOWS
) {
2283 memset(put
->stats
, 0, sizeof *put
->stats
);
2285 dp_netdev_flow_add(pmd
, &match
, &ufid
, put
->actions
,
2295 if (put
->flags
& DPIF_FP_MODIFY
2296 && flow_equal(&match
.flow
, &netdev_flow
->flow
)) {
2297 struct dp_netdev_actions
*new_actions
;
2298 struct dp_netdev_actions
*old_actions
;
2300 new_actions
= dp_netdev_actions_create(put
->actions
,
2303 old_actions
= dp_netdev_flow_get_actions(netdev_flow
);
2304 ovsrcu_set(&netdev_flow
->actions
, new_actions
);
2307 get_dpif_flow_stats(netdev_flow
, put
->stats
);
2309 if (put
->flags
& DPIF_FP_ZERO_STATS
) {
2310 /* XXX: The userspace datapath uses thread local statistics
2311 * (for flows), which should be updated only by the owning
2312 * thread. Since we cannot write on stats memory here,
2313 * we choose not to support this flag. Please note:
2314 * - This feature is currently used only by dpctl commands with
2316 * - Should the need arise, this operation can be implemented
2317 * by keeping a base value (to be update here) for each
2318 * counter, and subtracting it before outputting the stats */
2322 ovsrcu_postpone(dp_netdev_actions_free
, old_actions
);
2323 } else if (put
->flags
& DPIF_FP_CREATE
) {
2326 /* Overlapping flow. */
2330 ovs_mutex_unlock(&pmd
->flow_mutex
);
2331 dp_netdev_pmd_unref(pmd
);
2337 dpif_netdev_flow_del(struct dpif
*dpif
, const struct dpif_flow_del
*del
)
2339 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2340 struct dp_netdev_flow
*netdev_flow
;
2341 struct dp_netdev_pmd_thread
*pmd
;
2342 unsigned pmd_id
= del
->pmd_id
== PMD_ID_NULL
2343 ? NON_PMD_CORE_ID
: del
->pmd_id
;
2346 pmd
= dp_netdev_get_pmd(dp
, pmd_id
);
2351 ovs_mutex_lock(&pmd
->flow_mutex
);
2352 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, del
->ufid
, del
->key
,
2356 get_dpif_flow_stats(netdev_flow
, del
->stats
);
2358 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
2362 ovs_mutex_unlock(&pmd
->flow_mutex
);
2363 dp_netdev_pmd_unref(pmd
);
2368 struct dpif_netdev_flow_dump
{
2369 struct dpif_flow_dump up
;
2370 struct cmap_position poll_thread_pos
;
2371 struct cmap_position flow_pos
;
2372 struct dp_netdev_pmd_thread
*cur_pmd
;
2374 struct ovs_mutex mutex
;
2377 static struct dpif_netdev_flow_dump
*
2378 dpif_netdev_flow_dump_cast(struct dpif_flow_dump
*dump
)
2380 return CONTAINER_OF(dump
, struct dpif_netdev_flow_dump
, up
);
2383 static struct dpif_flow_dump
*
2384 dpif_netdev_flow_dump_create(const struct dpif
*dpif_
, bool terse
)
2386 struct dpif_netdev_flow_dump
*dump
;
2388 dump
= xzalloc(sizeof *dump
);
2389 dpif_flow_dump_init(&dump
->up
, dpif_
);
2390 dump
->up
.terse
= terse
;
2391 ovs_mutex_init(&dump
->mutex
);
2397 dpif_netdev_flow_dump_destroy(struct dpif_flow_dump
*dump_
)
2399 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
2401 ovs_mutex_destroy(&dump
->mutex
);
2406 struct dpif_netdev_flow_dump_thread
{
2407 struct dpif_flow_dump_thread up
;
2408 struct dpif_netdev_flow_dump
*dump
;
2409 struct odputil_keybuf keybuf
[FLOW_DUMP_MAX_BATCH
];
2410 struct odputil_keybuf maskbuf
[FLOW_DUMP_MAX_BATCH
];
2413 static struct dpif_netdev_flow_dump_thread
*
2414 dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread
*thread
)
2416 return CONTAINER_OF(thread
, struct dpif_netdev_flow_dump_thread
, up
);
2419 static struct dpif_flow_dump_thread
*
2420 dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump
*dump_
)
2422 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
2423 struct dpif_netdev_flow_dump_thread
*thread
;
2425 thread
= xmalloc(sizeof *thread
);
2426 dpif_flow_dump_thread_init(&thread
->up
, &dump
->up
);
2427 thread
->dump
= dump
;
2432 dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread
*thread_
)
2434 struct dpif_netdev_flow_dump_thread
*thread
2435 = dpif_netdev_flow_dump_thread_cast(thread_
);
2441 dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread
*thread_
,
2442 struct dpif_flow
*flows
, int max_flows
)
2444 struct dpif_netdev_flow_dump_thread
*thread
2445 = dpif_netdev_flow_dump_thread_cast(thread_
);
2446 struct dpif_netdev_flow_dump
*dump
= thread
->dump
;
2447 struct dp_netdev_flow
*netdev_flows
[FLOW_DUMP_MAX_BATCH
];
2451 ovs_mutex_lock(&dump
->mutex
);
2452 if (!dump
->status
) {
2453 struct dpif_netdev
*dpif
= dpif_netdev_cast(thread
->up
.dpif
);
2454 struct dp_netdev
*dp
= get_dp_netdev(&dpif
->dpif
);
2455 struct dp_netdev_pmd_thread
*pmd
= dump
->cur_pmd
;
2456 int flow_limit
= MIN(max_flows
, FLOW_DUMP_MAX_BATCH
);
2458 /* First call to dump_next(), extracts the first pmd thread.
2459 * If there is no pmd thread, returns immediately. */
2461 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
2463 ovs_mutex_unlock(&dump
->mutex
);
2470 for (n_flows
= 0; n_flows
< flow_limit
; n_flows
++) {
2471 struct cmap_node
*node
;
2473 node
= cmap_next_position(&pmd
->flow_table
, &dump
->flow_pos
);
2477 netdev_flows
[n_flows
] = CONTAINER_OF(node
,
2478 struct dp_netdev_flow
,
2481 /* When finishing dumping the current pmd thread, moves to
2483 if (n_flows
< flow_limit
) {
2484 memset(&dump
->flow_pos
, 0, sizeof dump
->flow_pos
);
2485 dp_netdev_pmd_unref(pmd
);
2486 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
2492 /* Keeps the reference to next caller. */
2493 dump
->cur_pmd
= pmd
;
2495 /* If the current dump is empty, do not exit the loop, since the
2496 * remaining pmds could have flows to be dumped. Just dumps again
2497 * on the new 'pmd'. */
2500 ovs_mutex_unlock(&dump
->mutex
);
2502 for (i
= 0; i
< n_flows
; i
++) {
2503 struct odputil_keybuf
*maskbuf
= &thread
->maskbuf
[i
];
2504 struct odputil_keybuf
*keybuf
= &thread
->keybuf
[i
];
2505 struct dp_netdev_flow
*netdev_flow
= netdev_flows
[i
];
2506 struct dpif_flow
*f
= &flows
[i
];
2507 struct ofpbuf key
, mask
;
2509 ofpbuf_use_stack(&key
, keybuf
, sizeof *keybuf
);
2510 ofpbuf_use_stack(&mask
, maskbuf
, sizeof *maskbuf
);
2511 dp_netdev_flow_to_dpif_flow(netdev_flow
, &key
, &mask
, f
,
2519 dpif_netdev_execute(struct dpif
*dpif
, struct dpif_execute
*execute
)
2520 OVS_NO_THREAD_SAFETY_ANALYSIS
2522 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2523 struct dp_netdev_pmd_thread
*pmd
;
2524 struct dp_packet_batch pp
;
2526 if (dp_packet_size(execute
->packet
) < ETH_HEADER_LEN
||
2527 dp_packet_size(execute
->packet
) > UINT16_MAX
) {
2531 /* Tries finding the 'pmd'. If NULL is returned, that means
2532 * the current thread is a non-pmd thread and should use
2533 * dp_netdev_get_pmd(dp, NON_PMD_CORE_ID). */
2534 pmd
= ovsthread_getspecific(dp
->per_pmd_key
);
2536 pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
2539 /* If the current thread is non-pmd thread, acquires
2540 * the 'non_pmd_mutex'. */
2541 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
2542 ovs_mutex_lock(&dp
->non_pmd_mutex
);
2545 /* The action processing expects the RSS hash to be valid, because
2546 * it's always initialized at the beginning of datapath processing.
2547 * In this case, though, 'execute->packet' may not have gone through
2548 * the datapath at all, it may have been generated by the upper layer
2549 * (OpenFlow packet-out, BFD frame, ...). */
2550 if (!dp_packet_rss_valid(execute
->packet
)) {
2551 dp_packet_set_rss_hash(execute
->packet
,
2552 flow_hash_5tuple(execute
->flow
, 0));
2555 packet_batch_init_packet(&pp
, execute
->packet
);
2556 dp_netdev_execute_actions(pmd
, &pp
, false, execute
->flow
,
2557 execute
->actions
, execute
->actions_len
,
2560 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
2561 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
2562 dp_netdev_pmd_unref(pmd
);
2569 dpif_netdev_operate(struct dpif
*dpif
, struct dpif_op
**ops
, size_t n_ops
)
2573 for (i
= 0; i
< n_ops
; i
++) {
2574 struct dpif_op
*op
= ops
[i
];
2577 case DPIF_OP_FLOW_PUT
:
2578 op
->error
= dpif_netdev_flow_put(dpif
, &op
->u
.flow_put
);
2581 case DPIF_OP_FLOW_DEL
:
2582 op
->error
= dpif_netdev_flow_del(dpif
, &op
->u
.flow_del
);
2585 case DPIF_OP_EXECUTE
:
2586 op
->error
= dpif_netdev_execute(dpif
, &op
->u
.execute
);
2589 case DPIF_OP_FLOW_GET
:
2590 op
->error
= dpif_netdev_flow_get(dpif
, &op
->u
.flow_get
);
2596 /* Changes the number or the affinity of pmd threads. The changes are actually
2597 * applied in dpif_netdev_run(). */
2599 dpif_netdev_pmd_set(struct dpif
*dpif
, const char *cmask
)
2601 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2603 if (!nullable_string_is_equal(dp
->pmd_cmask
, cmask
)) {
2604 free(dp
->pmd_cmask
);
2605 dp
->pmd_cmask
= nullable_xstrdup(cmask
);
2606 dp_netdev_request_reconfigure(dp
);
2612 /* Parses affinity list and returns result in 'core_ids'. */
2614 parse_affinity_list(const char *affinity_list
, unsigned *core_ids
, int n_rxq
)
2617 char *list
, *copy
, *key
, *value
;
2620 for (i
= 0; i
< n_rxq
; i
++) {
2624 if (!affinity_list
) {
2628 list
= copy
= xstrdup(affinity_list
);
2630 while (ofputil_parse_key_value(&list
, &key
, &value
)) {
2631 int rxq_id
, core_id
;
2633 if (!str_to_int(key
, 0, &rxq_id
) || rxq_id
< 0
2634 || !str_to_int(value
, 0, &core_id
) || core_id
< 0) {
2639 if (rxq_id
< n_rxq
) {
2640 core_ids
[rxq_id
] = core_id
;
2648 /* Parses 'affinity_list' and applies configuration if it is valid. */
2650 dpif_netdev_port_set_rxq_affinity(struct dp_netdev_port
*port
,
2651 const char *affinity_list
)
2653 unsigned *core_ids
, i
;
2656 core_ids
= xmalloc(port
->n_rxq
* sizeof *core_ids
);
2657 if (parse_affinity_list(affinity_list
, core_ids
, port
->n_rxq
)) {
2662 for (i
= 0; i
< port
->n_rxq
; i
++) {
2663 port
->rxqs
[i
].core_id
= core_ids
[i
];
2671 /* Changes the affinity of port's rx queues. The changes are actually applied
2672 * in dpif_netdev_run(). */
2674 dpif_netdev_port_set_config(struct dpif
*dpif
, odp_port_t port_no
,
2675 const struct smap
*cfg
)
2677 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2678 struct dp_netdev_port
*port
;
2680 const char *affinity_list
= smap_get(cfg
, "pmd-rxq-affinity");
2682 ovs_mutex_lock(&dp
->port_mutex
);
2683 error
= get_port_by_number(dp
, port_no
, &port
);
2684 if (error
|| !netdev_is_pmd(port
->netdev
)
2685 || nullable_string_is_equal(affinity_list
, port
->rxq_affinity_list
)) {
2689 error
= dpif_netdev_port_set_rxq_affinity(port
, affinity_list
);
2693 free(port
->rxq_affinity_list
);
2694 port
->rxq_affinity_list
= nullable_xstrdup(affinity_list
);
2696 dp_netdev_request_reconfigure(dp
);
2698 ovs_mutex_unlock(&dp
->port_mutex
);
2703 dpif_netdev_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
2704 uint32_t queue_id
, uint32_t *priority
)
2706 *priority
= queue_id
;
2711 /* Creates and returns a new 'struct dp_netdev_actions', whose actions are
2712 * a copy of the 'ofpacts_len' bytes of 'ofpacts'. */
2713 struct dp_netdev_actions
*
2714 dp_netdev_actions_create(const struct nlattr
*actions
, size_t size
)
2716 struct dp_netdev_actions
*netdev_actions
;
2718 netdev_actions
= xmalloc(sizeof *netdev_actions
+ size
);
2719 memcpy(netdev_actions
->actions
, actions
, size
);
2720 netdev_actions
->size
= size
;
2722 return netdev_actions
;
2725 struct dp_netdev_actions
*
2726 dp_netdev_flow_get_actions(const struct dp_netdev_flow
*flow
)
2728 return ovsrcu_get(struct dp_netdev_actions
*, &flow
->actions
);
2732 dp_netdev_actions_free(struct dp_netdev_actions
*actions
)
2737 static inline unsigned long long
2738 cycles_counter(void)
2741 return rte_get_tsc_cycles();
2747 /* Fake mutex to make sure that the calls to cycles_count_* are balanced */
2748 extern struct ovs_mutex cycles_counter_fake_mutex
;
2750 /* Start counting cycles. Must be followed by 'cycles_count_end()' */
2752 cycles_count_start(struct dp_netdev_pmd_thread
*pmd
)
2753 OVS_ACQUIRES(&cycles_counter_fake_mutex
)
2754 OVS_NO_THREAD_SAFETY_ANALYSIS
2756 pmd
->last_cycles
= cycles_counter();
2759 /* Stop counting cycles and add them to the counter 'type' */
2761 cycles_count_end(struct dp_netdev_pmd_thread
*pmd
,
2762 enum pmd_cycles_counter_type type
)
2763 OVS_RELEASES(&cycles_counter_fake_mutex
)
2764 OVS_NO_THREAD_SAFETY_ANALYSIS
2766 unsigned long long interval
= cycles_counter() - pmd
->last_cycles
;
2768 non_atomic_ullong_add(&pmd
->cycles
.n
[type
], interval
);
2772 dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread
*pmd
,
2773 struct dp_netdev_port
*port
,
2774 struct netdev_rxq
*rxq
)
2776 struct dp_packet_batch batch
;
2779 dp_packet_batch_init(&batch
);
2780 cycles_count_start(pmd
);
2781 error
= netdev_rxq_recv(rxq
, &batch
);
2782 cycles_count_end(pmd
, PMD_CYCLES_POLLING
);
2784 *recirc_depth_get() = 0;
2786 cycles_count_start(pmd
);
2787 dp_netdev_input(pmd
, &batch
, port
->port_no
);
2788 cycles_count_end(pmd
, PMD_CYCLES_PROCESSING
);
2789 } else if (error
!= EAGAIN
&& error
!= EOPNOTSUPP
) {
2790 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2792 VLOG_ERR_RL(&rl
, "error receiving data from %s: %s",
2793 netdev_get_name(port
->netdev
), ovs_strerror(error
));
2798 port_reconfigure(struct dp_netdev_port
*port
)
2800 struct netdev
*netdev
= port
->netdev
;
2803 if (!netdev_is_reconf_required(netdev
)) {
2807 /* Closes the existing 'rxq's. */
2808 for (i
= 0; i
< port
->n_rxq
; i
++) {
2809 netdev_rxq_close(port
->rxqs
[i
].rxq
);
2810 port
->rxqs
[i
].rxq
= NULL
;
2814 /* Allows 'netdev' to apply the pending configuration changes. */
2815 err
= netdev_reconfigure(netdev
);
2816 if (err
&& (err
!= EOPNOTSUPP
)) {
2817 VLOG_ERR("Failed to set interface %s new configuration",
2818 netdev_get_name(netdev
));
2821 /* If the netdev_reconfigure() above succeeds, reopens the 'rxq's. */
2822 port
->rxqs
= xrealloc(port
->rxqs
,
2823 sizeof *port
->rxqs
* netdev_n_rxq(netdev
));
2824 /* Realloc 'used' counters for tx queues. */
2825 free(port
->txq_used
);
2826 port
->txq_used
= xcalloc(netdev_n_txq(netdev
), sizeof *port
->txq_used
);
2828 for (i
= 0; i
< netdev_n_rxq(netdev
); i
++) {
2829 err
= netdev_rxq_open(netdev
, &port
->rxqs
[i
].rxq
, i
);
2836 /* Parse affinity list to apply configuration for new queues. */
2837 dpif_netdev_port_set_rxq_affinity(port
, port
->rxq_affinity_list
);
2843 reconfigure_pmd_threads(struct dp_netdev
*dp
)
2844 OVS_REQUIRES(dp
->port_mutex
)
2846 struct dp_netdev_port
*port
, *next
;
2849 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
2851 dp_netdev_destroy_all_pmds(dp
);
2853 /* Reconfigures the cpu mask. */
2854 ovs_numa_set_cpu_mask(dp
->pmd_cmask
);
2856 n_cores
= ovs_numa_get_n_cores();
2857 if (n_cores
== OVS_CORE_UNSPEC
) {
2858 VLOG_ERR("Cannot get cpu core info");
2862 HMAP_FOR_EACH_SAFE (port
, next
, node
, &dp
->ports
) {
2865 err
= port_reconfigure(port
);
2867 hmap_remove(&dp
->ports
, &port
->node
);
2868 seq_change(dp
->port_seq
);
2871 port
->dynamic_txqs
= netdev_n_txq(port
->netdev
) < n_cores
+ 1;
2874 /* Restores the non-pmd. */
2875 dp_netdev_set_nonpmd(dp
);
2876 /* Restores all pmd threads. */
2877 dp_netdev_reset_pmd_threads(dp
);
2880 /* Returns true if one of the netdevs in 'dp' requires a reconfiguration */
2882 ports_require_restart(const struct dp_netdev
*dp
)
2883 OVS_REQUIRES(dp
->port_mutex
)
2885 struct dp_netdev_port
*port
;
2887 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
2888 if (netdev_is_reconf_required(port
->netdev
)) {
2896 /* Return true if needs to revalidate datapath flows. */
2898 dpif_netdev_run(struct dpif
*dpif
)
2900 struct dp_netdev_port
*port
;
2901 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2902 struct dp_netdev_pmd_thread
*non_pmd
= dp_netdev_get_pmd(dp
,
2904 uint64_t new_tnl_seq
;
2906 ovs_mutex_lock(&dp
->port_mutex
);
2907 ovs_mutex_lock(&dp
->non_pmd_mutex
);
2908 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
2909 if (!netdev_is_pmd(port
->netdev
)) {
2912 for (i
= 0; i
< port
->n_rxq
; i
++) {
2913 dp_netdev_process_rxq_port(non_pmd
, port
, port
->rxqs
[i
].rxq
);
2917 dpif_netdev_xps_revalidate_pmd(non_pmd
, time_msec(), false);
2918 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
2920 dp_netdev_pmd_unref(non_pmd
);
2922 if (dp_netdev_is_reconf_required(dp
) || ports_require_restart(dp
)) {
2923 reconfigure_pmd_threads(dp
);
2925 ovs_mutex_unlock(&dp
->port_mutex
);
2927 tnl_neigh_cache_run();
2929 new_tnl_seq
= seq_read(tnl_conf_seq
);
2931 if (dp
->last_tnl_conf_seq
!= new_tnl_seq
) {
2932 dp
->last_tnl_conf_seq
= new_tnl_seq
;
2939 dpif_netdev_wait(struct dpif
*dpif
)
2941 struct dp_netdev_port
*port
;
2942 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2944 ovs_mutex_lock(&dp_netdev_mutex
);
2945 ovs_mutex_lock(&dp
->port_mutex
);
2946 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
2947 netdev_wait_reconf_required(port
->netdev
);
2948 if (!netdev_is_pmd(port
->netdev
)) {
2951 for (i
= 0; i
< port
->n_rxq
; i
++) {
2952 netdev_rxq_wait(port
->rxqs
[i
].rxq
);
2956 ovs_mutex_unlock(&dp
->port_mutex
);
2957 ovs_mutex_unlock(&dp_netdev_mutex
);
2958 seq_wait(tnl_conf_seq
, dp
->last_tnl_conf_seq
);
2962 pmd_free_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
2964 struct tx_port
*tx_port_cached
;
2966 /* Free all used tx queue ids. */
2967 dpif_netdev_xps_revalidate_pmd(pmd
, 0, true);
2969 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->port_cache
) {
2970 free(tx_port_cached
);
2974 /* Copies ports from 'pmd->tx_ports' (shared with the main thread) to
2975 * 'pmd->port_cache' (thread local) */
2977 pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
2978 OVS_REQUIRES(pmd
->port_mutex
)
2980 struct tx_port
*tx_port
, *tx_port_cached
;
2982 pmd_free_cached_ports(pmd
);
2983 hmap_shrink(&pmd
->port_cache
);
2985 HMAP_FOR_EACH (tx_port
, node
, &pmd
->tx_ports
) {
2986 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
2987 hmap_insert(&pmd
->port_cache
, &tx_port_cached
->node
,
2988 hash_port_no(tx_port_cached
->port
->port_no
));
2993 pmd_load_queues_and_ports(struct dp_netdev_pmd_thread
*pmd
,
2994 struct rxq_poll
**ppoll_list
)
2996 struct rxq_poll
*poll_list
= *ppoll_list
;
2997 struct rxq_poll
*poll
;
3000 ovs_mutex_lock(&pmd
->port_mutex
);
3001 poll_list
= xrealloc(poll_list
, pmd
->poll_cnt
* sizeof *poll_list
);
3004 LIST_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
3005 poll_list
[i
++] = *poll
;
3008 pmd_load_cached_ports(pmd
);
3010 ovs_mutex_unlock(&pmd
->port_mutex
);
3012 *ppoll_list
= poll_list
;
3017 pmd_thread_main(void *f_
)
3019 struct dp_netdev_pmd_thread
*pmd
= f_
;
3020 unsigned int lc
= 0;
3021 struct rxq_poll
*poll_list
;
3022 unsigned int port_seq
= PMD_INITIAL_SEQ
;
3029 /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */
3030 ovsthread_setspecific(pmd
->dp
->per_pmd_key
, pmd
);
3031 ovs_numa_thread_setaffinity_core(pmd
->core_id
);
3032 dpdk_set_lcore_id(pmd
->core_id
);
3033 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
3035 emc_cache_init(&pmd
->flow_cache
);
3037 /* List port/core affinity */
3038 for (i
= 0; i
< poll_cnt
; i
++) {
3039 VLOG_DBG("Core %d processing port \'%s\' with queue-id %d\n",
3040 pmd
->core_id
, netdev_get_name(poll_list
[i
].port
->netdev
),
3041 netdev_rxq_get_queue_id(poll_list
[i
].rx
));
3045 for (i
= 0; i
< poll_cnt
; i
++) {
3046 dp_netdev_process_rxq_port(pmd
, poll_list
[i
].port
, poll_list
[i
].rx
);
3054 coverage_try_clear();
3055 if (!ovsrcu_try_quiesce()) {
3056 emc_cache_slow_sweep(&pmd
->flow_cache
);
3059 atomic_read_relaxed(&pmd
->change_seq
, &seq
);
3060 if (seq
!= port_seq
) {
3067 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
3068 exiting
= latch_is_set(&pmd
->exit_latch
);
3069 /* Signal here to make sure the pmd finishes
3070 * reloading the updated configuration. */
3071 dp_netdev_pmd_reload_done(pmd
);
3073 emc_cache_uninit(&pmd
->flow_cache
);
3080 pmd_free_cached_ports(pmd
);
3085 dp_netdev_disable_upcall(struct dp_netdev
*dp
)
3086 OVS_ACQUIRES(dp
->upcall_rwlock
)
3088 fat_rwlock_wrlock(&dp
->upcall_rwlock
);
3092 dpif_netdev_disable_upcall(struct dpif
*dpif
)
3093 OVS_NO_THREAD_SAFETY_ANALYSIS
3095 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3096 dp_netdev_disable_upcall(dp
);
3100 dp_netdev_enable_upcall(struct dp_netdev
*dp
)
3101 OVS_RELEASES(dp
->upcall_rwlock
)
3103 fat_rwlock_unlock(&dp
->upcall_rwlock
);
3107 dpif_netdev_enable_upcall(struct dpif
*dpif
)
3108 OVS_NO_THREAD_SAFETY_ANALYSIS
3110 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3111 dp_netdev_enable_upcall(dp
);
3115 dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
)
3117 ovs_mutex_lock(&pmd
->cond_mutex
);
3118 xpthread_cond_signal(&pmd
->cond
);
3119 ovs_mutex_unlock(&pmd
->cond_mutex
);
3122 /* Finds and refs the dp_netdev_pmd_thread on core 'core_id'. Returns
3123 * the pointer if succeeds, otherwise, NULL.
3125 * Caller must unrefs the returned reference. */
3126 static struct dp_netdev_pmd_thread
*
3127 dp_netdev_get_pmd(struct dp_netdev
*dp
, unsigned core_id
)
3129 struct dp_netdev_pmd_thread
*pmd
;
3130 const struct cmap_node
*pnode
;
3132 pnode
= cmap_find(&dp
->poll_threads
, hash_int(core_id
, 0));
3136 pmd
= CONTAINER_OF(pnode
, struct dp_netdev_pmd_thread
, node
);
3138 return dp_netdev_pmd_try_ref(pmd
) ? pmd
: NULL
;
3141 /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */
3143 dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
3144 OVS_REQUIRES(dp
->port_mutex
)
3146 struct dp_netdev_pmd_thread
*non_pmd
;
3147 struct dp_netdev_port
*port
;
3149 non_pmd
= xzalloc(sizeof *non_pmd
);
3150 dp_netdev_configure_pmd(non_pmd
, dp
, NON_PMD_CORE_ID
, OVS_NUMA_UNSPEC
);
3152 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3153 dp_netdev_add_port_tx_to_pmd(non_pmd
, port
);
3156 dp_netdev_reload_pmd__(non_pmd
);
3159 /* Caller must have valid pointer to 'pmd'. */
3161 dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
)
3163 return ovs_refcount_try_ref_rcu(&pmd
->ref_cnt
);
3167 dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
)
3169 if (pmd
&& ovs_refcount_unref(&pmd
->ref_cnt
) == 1) {
3170 ovsrcu_postpone(dp_netdev_destroy_pmd
, pmd
);
3174 /* Given cmap position 'pos', tries to ref the next node. If try_ref()
3175 * fails, keeps checking for next node until reaching the end of cmap.
3177 * Caller must unrefs the returned reference. */
3178 static struct dp_netdev_pmd_thread
*
3179 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
)
3181 struct dp_netdev_pmd_thread
*next
;
3184 struct cmap_node
*node
;
3186 node
= cmap_next_position(&dp
->poll_threads
, pos
);
3187 next
= node
? CONTAINER_OF(node
, struct dp_netdev_pmd_thread
, node
)
3189 } while (next
&& !dp_netdev_pmd_try_ref(next
));
3194 /* Configures the 'pmd' based on the input argument. */
3196 dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
, struct dp_netdev
*dp
,
3197 unsigned core_id
, int numa_id
)
3200 pmd
->core_id
= core_id
;
3201 pmd
->numa_id
= numa_id
;
3204 atomic_init(&pmd
->static_tx_qid
,
3205 (core_id
== NON_PMD_CORE_ID
)
3206 ? ovs_numa_get_n_cores()
3207 : get_n_pmd_threads(dp
));
3209 ovs_refcount_init(&pmd
->ref_cnt
);
3210 latch_init(&pmd
->exit_latch
);
3211 atomic_init(&pmd
->change_seq
, PMD_INITIAL_SEQ
);
3212 xpthread_cond_init(&pmd
->cond
, NULL
);
3213 ovs_mutex_init(&pmd
->cond_mutex
);
3214 ovs_mutex_init(&pmd
->flow_mutex
);
3215 ovs_mutex_init(&pmd
->port_mutex
);
3216 dpcls_init(&pmd
->cls
);
3217 cmap_init(&pmd
->flow_table
);
3218 ovs_list_init(&pmd
->poll_list
);
3219 hmap_init(&pmd
->tx_ports
);
3220 hmap_init(&pmd
->port_cache
);
3221 /* init the 'flow_cache' since there is no
3222 * actual thread created for NON_PMD_CORE_ID. */
3223 if (core_id
== NON_PMD_CORE_ID
) {
3224 emc_cache_init(&pmd
->flow_cache
);
3226 cmap_insert(&dp
->poll_threads
, CONST_CAST(struct cmap_node
*, &pmd
->node
),
3227 hash_int(core_id
, 0));
3231 dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
)
3233 dp_netdev_pmd_flow_flush(pmd
);
3234 dpcls_destroy(&pmd
->cls
);
3235 hmap_destroy(&pmd
->port_cache
);
3236 hmap_destroy(&pmd
->tx_ports
);
3237 cmap_destroy(&pmd
->flow_table
);
3238 ovs_mutex_destroy(&pmd
->flow_mutex
);
3239 latch_destroy(&pmd
->exit_latch
);
3240 xpthread_cond_destroy(&pmd
->cond
);
3241 ovs_mutex_destroy(&pmd
->cond_mutex
);
3242 ovs_mutex_destroy(&pmd
->port_mutex
);
3246 /* Stops the pmd thread, removes it from the 'dp->poll_threads',
3247 * and unrefs the struct. */
3249 dp_netdev_del_pmd(struct dp_netdev
*dp
, struct dp_netdev_pmd_thread
*pmd
)
3251 /* NON_PMD_CORE_ID doesn't have a thread, so we don't have to synchronize,
3252 * but extra cleanup is necessary */
3253 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3254 emc_cache_uninit(&pmd
->flow_cache
);
3255 pmd_free_cached_ports(pmd
);
3257 latch_set(&pmd
->exit_latch
);
3258 dp_netdev_reload_pmd__(pmd
);
3259 ovs_numa_unpin_core(pmd
->core_id
);
3260 xpthread_join(pmd
->thread
, NULL
);
3263 dp_netdev_pmd_clear_ports(pmd
);
3265 /* Purges the 'pmd''s flows after stopping the thread, but before
3266 * destroying the flows, so that the flow stats can be collected. */
3267 if (dp
->dp_purge_cb
) {
3268 dp
->dp_purge_cb(dp
->dp_purge_aux
, pmd
->core_id
);
3270 cmap_remove(&pmd
->dp
->poll_threads
, &pmd
->node
, hash_int(pmd
->core_id
, 0));
3271 dp_netdev_pmd_unref(pmd
);
3274 /* Destroys all pmd threads. */
3276 dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
)
3278 struct dp_netdev_pmd_thread
*pmd
;
3279 struct dp_netdev_pmd_thread
**pmd_list
;
3280 size_t k
= 0, n_pmds
;
3282 n_pmds
= cmap_count(&dp
->poll_threads
);
3283 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
3285 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3286 /* We cannot call dp_netdev_del_pmd(), since it alters
3287 * 'dp->poll_threads' (while we're iterating it) and it
3289 ovs_assert(k
< n_pmds
);
3290 pmd_list
[k
++] = pmd
;
3293 for (size_t i
= 0; i
< k
; i
++) {
3294 dp_netdev_del_pmd(dp
, pmd_list
[i
]);
3299 /* Deletes all pmd threads on numa node 'numa_id' and
3300 * fixes static_tx_qids of other threads to keep them sequential. */
3302 dp_netdev_del_pmds_on_numa(struct dp_netdev
*dp
, int numa_id
)
3304 struct dp_netdev_pmd_thread
*pmd
;
3305 int n_pmds_on_numa
, n_pmds
;
3306 int *free_idx
, k
= 0;
3307 struct dp_netdev_pmd_thread
**pmd_list
;
3309 n_pmds_on_numa
= get_n_pmd_threads_on_numa(dp
, numa_id
);
3310 free_idx
= xcalloc(n_pmds_on_numa
, sizeof *free_idx
);
3311 pmd_list
= xcalloc(n_pmds_on_numa
, sizeof *pmd_list
);
3313 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3314 /* We cannot call dp_netdev_del_pmd(), since it alters
3315 * 'dp->poll_threads' (while we're iterating it) and it
3317 if (pmd
->numa_id
== numa_id
) {
3318 atomic_read_relaxed(&pmd
->static_tx_qid
, &free_idx
[k
]);
3320 ovs_assert(k
< n_pmds_on_numa
);
3325 for (int i
= 0; i
< k
; i
++) {
3326 dp_netdev_del_pmd(dp
, pmd_list
[i
]);
3329 n_pmds
= get_n_pmd_threads(dp
);
3330 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3333 atomic_read_relaxed(&pmd
->static_tx_qid
, &old_tx_qid
);
3335 if (old_tx_qid
>= n_pmds
) {
3336 int new_tx_qid
= free_idx
[--k
];
3338 atomic_store_relaxed(&pmd
->static_tx_qid
, new_tx_qid
);
3346 /* Deletes all rx queues from pmd->poll_list and all the ports from
3349 dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
)
3351 struct rxq_poll
*poll
;
3352 struct tx_port
*port
;
3354 ovs_mutex_lock(&pmd
->port_mutex
);
3355 LIST_FOR_EACH_POP (poll
, node
, &pmd
->poll_list
) {
3359 HMAP_FOR_EACH_POP (port
, node
, &pmd
->tx_ports
) {
3362 ovs_mutex_unlock(&pmd
->port_mutex
);
3365 static struct tx_port
*
3366 tx_port_lookup(const struct hmap
*hmap
, odp_port_t port_no
)
3370 HMAP_FOR_EACH_IN_BUCKET (tx
, node
, hash_port_no(port_no
), hmap
) {
3371 if (tx
->port
->port_no
== port_no
) {
3379 /* Deletes all rx queues of 'port' from 'poll_list', and the 'port' from
3380 * 'tx_ports' of 'pmd' thread. Returns true if 'port' was found in 'pmd'
3381 * (therefore a restart is required). */
3383 dp_netdev_del_port_from_pmd__(struct dp_netdev_port
*port
,
3384 struct dp_netdev_pmd_thread
*pmd
)
3386 struct rxq_poll
*poll
, *next
;
3390 ovs_mutex_lock(&pmd
->port_mutex
);
3391 LIST_FOR_EACH_SAFE (poll
, next
, node
, &pmd
->poll_list
) {
3392 if (poll
->port
== port
) {
3394 ovs_list_remove(&poll
->node
);
3400 tx
= tx_port_lookup(&pmd
->tx_ports
, port
->port_no
);
3402 hmap_remove(&pmd
->tx_ports
, &tx
->node
);
3406 ovs_mutex_unlock(&pmd
->port_mutex
);
3411 /* Deletes 'port' from the 'poll_list' and from the 'tx_ports' of all the pmd
3412 * threads. The pmd threads that need to be restarted are inserted in
3415 dp_netdev_del_port_from_all_pmds__(struct dp_netdev
*dp
,
3416 struct dp_netdev_port
*port
,
3417 struct hmapx
*to_reload
)
3419 struct dp_netdev_pmd_thread
*pmd
;
3421 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3424 found
= dp_netdev_del_port_from_pmd__(port
, pmd
);
3427 hmapx_add(to_reload
, pmd
);
3432 /* Deletes 'port' from the 'poll_list' and from the 'tx_ports' of all the pmd
3433 * threads. Reloads the threads if needed. */
3435 dp_netdev_del_port_from_all_pmds(struct dp_netdev
*dp
,
3436 struct dp_netdev_port
*port
)
3438 struct dp_netdev_pmd_thread
*pmd
;
3439 struct hmapx to_reload
= HMAPX_INITIALIZER(&to_reload
);
3440 struct hmapx_node
*node
;
3442 dp_netdev_del_port_from_all_pmds__(dp
, port
, &to_reload
);
3444 HMAPX_FOR_EACH (node
, &to_reload
) {
3445 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3446 dp_netdev_reload_pmd__(pmd
);
3449 hmapx_destroy(&to_reload
);
3453 /* Returns non-isolated PMD thread from this numa node with fewer
3454 * rx queues to poll. Returns NULL if there is no non-isolated PMD threads
3455 * on this numa node. Can be called safely only by main thread. */
3456 static struct dp_netdev_pmd_thread
*
3457 dp_netdev_less_loaded_pmd_on_numa(struct dp_netdev
*dp
, int numa_id
)
3460 struct dp_netdev_pmd_thread
*pmd
, *res
= NULL
;
3462 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3463 if (!pmd
->isolated
&& pmd
->numa_id
== numa_id
3464 && (min_cnt
> pmd
->poll_cnt
|| res
== NULL
)) {
3465 min_cnt
= pmd
->poll_cnt
;
3473 /* Adds rx queue to poll_list of PMD thread. */
3475 dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
3476 struct dp_netdev_port
*port
, struct netdev_rxq
*rx
)
3477 OVS_REQUIRES(pmd
->port_mutex
)
3479 struct rxq_poll
*poll
= xmalloc(sizeof *poll
);
3484 ovs_list_push_back(&pmd
->poll_list
, &poll
->node
);
3488 /* Add 'port' to the tx port cache of 'pmd', which must be reloaded for the
3489 * changes to take effect. */
3491 dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
3492 struct dp_netdev_port
*port
)
3494 struct tx_port
*tx
= xzalloc(sizeof *tx
);
3499 ovs_mutex_lock(&pmd
->port_mutex
);
3500 hmap_insert(&pmd
->tx_ports
, &tx
->node
, hash_port_no(tx
->port
->port_no
));
3501 ovs_mutex_unlock(&pmd
->port_mutex
);
3504 /* Distribute all {pinned|non-pinned} rx queues of 'port' between PMD
3505 * threads in 'dp'. The pmd threads that need to be restarted are inserted
3506 * in 'to_reload'. PMD threads with pinned queues marked as isolated. */
3508 dp_netdev_add_port_rx_to_pmds(struct dp_netdev
*dp
,
3509 struct dp_netdev_port
*port
,
3510 struct hmapx
*to_reload
, bool pinned
)
3512 int numa_id
= netdev_get_numa_id(port
->netdev
);
3513 struct dp_netdev_pmd_thread
*pmd
;
3516 if (!netdev_is_pmd(port
->netdev
)) {
3520 for (i
= 0; i
< port
->n_rxq
; i
++) {
3522 if (port
->rxqs
[i
].core_id
== -1) {
3525 pmd
= dp_netdev_get_pmd(dp
, port
->rxqs
[i
].core_id
);
3527 VLOG_WARN("There is no PMD thread on core %d. "
3528 "Queue %d on port \'%s\' will not be polled.",
3529 port
->rxqs
[i
].core_id
, i
,
3530 netdev_get_name(port
->netdev
));
3533 pmd
->isolated
= true;
3534 dp_netdev_pmd_unref(pmd
);
3536 if (port
->rxqs
[i
].core_id
!= -1) {
3539 pmd
= dp_netdev_less_loaded_pmd_on_numa(dp
, numa_id
);
3541 VLOG_WARN("There's no available pmd thread on numa node %d",
3547 ovs_mutex_lock(&pmd
->port_mutex
);
3548 dp_netdev_add_rxq_to_pmd(pmd
, port
, port
->rxqs
[i
].rxq
);
3549 ovs_mutex_unlock(&pmd
->port_mutex
);
3551 hmapx_add(to_reload
, pmd
);
3555 /* Distributes all non-pinned rx queues of 'port' between all PMD threads
3556 * in 'dp' and inserts 'port' in the PMD threads 'tx_ports'. The pmd threads
3557 * that need to be restarted are inserted in 'to_reload'. */
3559 dp_netdev_add_port_to_pmds__(struct dp_netdev
*dp
, struct dp_netdev_port
*port
,
3560 struct hmapx
*to_reload
)
3562 struct dp_netdev_pmd_thread
*pmd
;
3564 dp_netdev_add_port_rx_to_pmds(dp
, port
, to_reload
, false);
3566 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3567 dp_netdev_add_port_tx_to_pmd(pmd
, port
);
3568 hmapx_add(to_reload
, pmd
);
3572 /* Distributes all non-pinned rx queues of 'port' between all PMD threads
3573 * in 'dp', inserts 'port' in the PMD threads 'tx_ports' and reloads them,
3576 dp_netdev_add_port_to_pmds(struct dp_netdev
*dp
, struct dp_netdev_port
*port
)
3578 struct dp_netdev_pmd_thread
*pmd
;
3579 struct hmapx to_reload
= HMAPX_INITIALIZER(&to_reload
);
3580 struct hmapx_node
*node
;
3582 dp_netdev_add_port_to_pmds__(dp
, port
, &to_reload
);
3584 HMAPX_FOR_EACH (node
, &to_reload
) {
3585 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3586 dp_netdev_reload_pmd__(pmd
);
3589 hmapx_destroy(&to_reload
);
3592 /* Starts pmd threads for the numa node 'numa_id', if not already started.
3593 * The function takes care of filling the threads tx port cache. */
3595 dp_netdev_set_pmds_on_numa(struct dp_netdev
*dp
, int numa_id
)
3596 OVS_REQUIRES(dp
->port_mutex
)
3600 if (!ovs_numa_numa_id_is_valid(numa_id
)) {
3601 VLOG_WARN("Cannot create pmd threads due to numa id (%d) invalid",
3606 n_pmds
= get_n_pmd_threads_on_numa(dp
, numa_id
);
3608 /* If there are already pmd threads created for the numa node
3609 * in which 'netdev' is on, do nothing. Else, creates the
3610 * pmd threads for the numa node. */
3612 int can_have
, n_unpinned
, i
;
3614 n_unpinned
= ovs_numa_get_n_unpinned_cores_on_numa(numa_id
);
3616 VLOG_WARN("Cannot create pmd threads due to out of unpinned "
3617 "cores on numa node %d", numa_id
);
3621 /* If cpu mask is specified, uses all unpinned cores, otherwise
3622 * tries creating NR_PMD_THREADS pmd threads. */
3623 can_have
= dp
->pmd_cmask
? n_unpinned
: MIN(n_unpinned
, NR_PMD_THREADS
);
3624 for (i
= 0; i
< can_have
; i
++) {
3625 unsigned core_id
= ovs_numa_get_unpinned_core_on_numa(numa_id
);
3626 struct dp_netdev_pmd_thread
*pmd
= xzalloc(sizeof *pmd
);
3627 struct dp_netdev_port
*port
;
3629 dp_netdev_configure_pmd(pmd
, dp
, core_id
, numa_id
);
3631 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3632 dp_netdev_add_port_tx_to_pmd(pmd
, port
);
3635 pmd
->thread
= ovs_thread_create("pmd", pmd_thread_main
, pmd
);
3637 VLOG_INFO("Created %d pmd threads on numa node %d", can_have
, numa_id
);
3642 /* Called after pmd threads config change. Restarts pmd threads with
3643 * new configuration. */
3645 dp_netdev_reset_pmd_threads(struct dp_netdev
*dp
)
3646 OVS_REQUIRES(dp
->port_mutex
)
3648 struct hmapx to_reload
= HMAPX_INITIALIZER(&to_reload
);
3649 struct dp_netdev_pmd_thread
*pmd
;
3650 struct dp_netdev_port
*port
;
3651 struct hmapx_node
*node
;
3653 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3654 if (netdev_is_pmd(port
->netdev
)) {
3655 int numa_id
= netdev_get_numa_id(port
->netdev
);
3657 dp_netdev_set_pmds_on_numa(dp
, numa_id
);
3659 /* Distribute only pinned rx queues first to mark threads as isolated */
3660 dp_netdev_add_port_rx_to_pmds(dp
, port
, &to_reload
, true);
3663 /* Distribute remaining non-pinned rx queues to non-isolated PMD threads. */
3664 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3665 dp_netdev_add_port_rx_to_pmds(dp
, port
, &to_reload
, false);
3668 HMAPX_FOR_EACH (node
, &to_reload
) {
3669 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3670 dp_netdev_reload_pmd__(pmd
);
3673 hmapx_destroy(&to_reload
);
3677 dpif_netdev_get_datapath_version(void)
3679 return xstrdup("<built-in>");
3683 dp_netdev_flow_used(struct dp_netdev_flow
*netdev_flow
, int cnt
, int size
,
3684 uint16_t tcp_flags
, long long now
)
3688 atomic_store_relaxed(&netdev_flow
->stats
.used
, now
);
3689 non_atomic_ullong_add(&netdev_flow
->stats
.packet_count
, cnt
);
3690 non_atomic_ullong_add(&netdev_flow
->stats
.byte_count
, size
);
3691 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
3693 atomic_store_relaxed(&netdev_flow
->stats
.tcp_flags
, flags
);
3697 dp_netdev_count_packet(struct dp_netdev_pmd_thread
*pmd
,
3698 enum dp_stat_type type
, int cnt
)
3700 non_atomic_ullong_add(&pmd
->stats
.n
[type
], cnt
);
3704 dp_netdev_upcall(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet
*packet_
,
3705 struct flow
*flow
, struct flow_wildcards
*wc
, ovs_u128
*ufid
,
3706 enum dpif_upcall_type type
, const struct nlattr
*userdata
,
3707 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
3709 struct dp_netdev
*dp
= pmd
->dp
;
3710 struct flow_tnl orig_tunnel
;
3713 if (OVS_UNLIKELY(!dp
->upcall_cb
)) {
3717 /* Upcall processing expects the Geneve options to be in the translated
3718 * format but we need to retain the raw format for datapath use. */
3719 orig_tunnel
.flags
= flow
->tunnel
.flags
;
3720 if (flow
->tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
3721 orig_tunnel
.metadata
.present
.len
= flow
->tunnel
.metadata
.present
.len
;
3722 memcpy(orig_tunnel
.metadata
.opts
.gnv
, flow
->tunnel
.metadata
.opts
.gnv
,
3723 flow
->tunnel
.metadata
.present
.len
);
3724 err
= tun_metadata_from_geneve_udpif(&orig_tunnel
, &orig_tunnel
,
3731 if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl
))) {
3732 struct ds ds
= DS_EMPTY_INITIALIZER
;
3735 struct odp_flow_key_parms odp_parms
= {
3738 .support
= dp_netdev_support
,
3741 ofpbuf_init(&key
, 0);
3742 odp_flow_key_from_flow(&odp_parms
, &key
);
3743 packet_str
= ofp_packet_to_string(dp_packet_data(packet_
),
3744 dp_packet_size(packet_
));
3746 odp_flow_key_format(key
.data
, key
.size
, &ds
);
3748 VLOG_DBG("%s: %s upcall:\n%s\n%s", dp
->name
,
3749 dpif_upcall_type_to_string(type
), ds_cstr(&ds
), packet_str
);
3751 ofpbuf_uninit(&key
);
3757 err
= dp
->upcall_cb(packet_
, flow
, ufid
, pmd
->core_id
, type
, userdata
,
3758 actions
, wc
, put_actions
, dp
->upcall_aux
);
3759 if (err
&& err
!= ENOSPC
) {
3763 /* Translate tunnel metadata masks to datapath format. */
3765 if (wc
->masks
.tunnel
.metadata
.present
.map
) {
3766 struct geneve_opt opts
[TLV_TOT_OPT_SIZE
/
3767 sizeof(struct geneve_opt
)];
3769 if (orig_tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
3770 tun_metadata_to_geneve_udpif_mask(&flow
->tunnel
,
3772 orig_tunnel
.metadata
.opts
.gnv
,
3773 orig_tunnel
.metadata
.present
.len
,
3776 orig_tunnel
.metadata
.present
.len
= 0;
3779 memset(&wc
->masks
.tunnel
.metadata
, 0,
3780 sizeof wc
->masks
.tunnel
.metadata
);
3781 memcpy(&wc
->masks
.tunnel
.metadata
.opts
.gnv
, opts
,
3782 orig_tunnel
.metadata
.present
.len
);
3784 wc
->masks
.tunnel
.metadata
.present
.len
= 0xff;
3787 /* Restore tunnel metadata. We need to use the saved options to ensure
3788 * that any unknown options are not lost. The generated mask will have
3789 * the same structure, matching on types and lengths but wildcarding
3790 * option data we don't care about. */
3791 if (orig_tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
3792 memcpy(&flow
->tunnel
.metadata
.opts
.gnv
, orig_tunnel
.metadata
.opts
.gnv
,
3793 orig_tunnel
.metadata
.present
.len
);
3794 flow
->tunnel
.metadata
.present
.len
= orig_tunnel
.metadata
.present
.len
;
3795 flow
->tunnel
.flags
|= FLOW_TNL_F_UDPIF
;
3801 static inline uint32_t
3802 dpif_netdev_packet_get_rss_hash(struct dp_packet
*packet
,
3803 const struct miniflow
*mf
)
3805 uint32_t hash
, recirc_depth
;
3807 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
3808 hash
= dp_packet_get_rss_hash(packet
);
3810 hash
= miniflow_hash_5tuple(mf
, 0);
3811 dp_packet_set_rss_hash(packet
, hash
);
3814 /* The RSS hash must account for the recirculation depth to avoid
3815 * collisions in the exact match cache */
3816 recirc_depth
= *recirc_depth_get_unsafe();
3817 if (OVS_UNLIKELY(recirc_depth
)) {
3818 hash
= hash_finish(hash
, recirc_depth
);
3819 dp_packet_set_rss_hash(packet
, hash
);
3824 struct packet_batch_per_flow
{
3825 unsigned int byte_count
;
3827 struct dp_netdev_flow
*flow
;
3829 struct dp_packet_batch array
;
3833 packet_batch_per_flow_update(struct packet_batch_per_flow
*batch
,
3834 struct dp_packet
*packet
,
3835 const struct miniflow
*mf
)
3837 batch
->byte_count
+= dp_packet_size(packet
);
3838 batch
->tcp_flags
|= miniflow_get_tcp_flags(mf
);
3839 batch
->array
.packets
[batch
->array
.count
++] = packet
;
3843 packet_batch_per_flow_init(struct packet_batch_per_flow
*batch
,
3844 struct dp_netdev_flow
*flow
)
3846 flow
->batch
= batch
;
3849 dp_packet_batch_init(&batch
->array
);
3850 batch
->byte_count
= 0;
3851 batch
->tcp_flags
= 0;
3855 packet_batch_per_flow_execute(struct packet_batch_per_flow
*batch
,
3856 struct dp_netdev_pmd_thread
*pmd
,
3859 struct dp_netdev_actions
*actions
;
3860 struct dp_netdev_flow
*flow
= batch
->flow
;
3862 dp_netdev_flow_used(flow
, batch
->array
.count
, batch
->byte_count
,
3863 batch
->tcp_flags
, now
);
3865 actions
= dp_netdev_flow_get_actions(flow
);
3867 dp_netdev_execute_actions(pmd
, &batch
->array
, true, &flow
->flow
,
3868 actions
->actions
, actions
->size
, now
);
3872 dp_netdev_queue_batches(struct dp_packet
*pkt
,
3873 struct dp_netdev_flow
*flow
, const struct miniflow
*mf
,
3874 struct packet_batch_per_flow
*batches
, size_t *n_batches
)
3876 struct packet_batch_per_flow
*batch
= flow
->batch
;
3878 if (OVS_UNLIKELY(!batch
)) {
3879 batch
= &batches
[(*n_batches
)++];
3880 packet_batch_per_flow_init(batch
, flow
);
3883 packet_batch_per_flow_update(batch
, pkt
, mf
);
3886 /* Try to process all ('cnt') the 'packets' using only the exact match cache
3887 * 'pmd->flow_cache'. If a flow is not found for a packet 'packets[i]', the
3888 * miniflow is copied into 'keys' and the packet pointer is moved at the
3889 * beginning of the 'packets' array.
3891 * The function returns the number of packets that needs to be processed in the
3892 * 'packets' array (they have been moved to the beginning of the vector).
3894 * If 'md_is_valid' is false, the metadata in 'packets' is not valid and must be
3895 * initialized by this function using 'port_no'.
3897 static inline size_t
3898 emc_processing(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet_batch
*packets_
,
3899 struct netdev_flow_key
*keys
,
3900 struct packet_batch_per_flow batches
[], size_t *n_batches
,
3901 bool md_is_valid
, odp_port_t port_no
)
3903 struct emc_cache
*flow_cache
= &pmd
->flow_cache
;
3904 struct netdev_flow_key
*key
= &keys
[0];
3905 size_t i
, n_missed
= 0, n_dropped
= 0;
3906 struct dp_packet
**packets
= packets_
->packets
;
3907 int cnt
= packets_
->count
;
3909 for (i
= 0; i
< cnt
; i
++) {
3910 struct dp_netdev_flow
*flow
;
3911 struct dp_packet
*packet
= packets
[i
];
3913 if (OVS_UNLIKELY(dp_packet_size(packet
) < ETH_HEADER_LEN
)) {
3914 dp_packet_delete(packet
);
3920 /* Prefetch next packet data and metadata. */
3921 OVS_PREFETCH(dp_packet_data(packets
[i
+1]));
3922 pkt_metadata_prefetch_init(&packets
[i
+1]->md
);
3926 pkt_metadata_init(&packet
->md
, port_no
);
3928 miniflow_extract(packet
, &key
->mf
);
3929 key
->len
= 0; /* Not computed yet. */
3930 key
->hash
= dpif_netdev_packet_get_rss_hash(packet
, &key
->mf
);
3932 flow
= emc_lookup(flow_cache
, key
);
3933 if (OVS_LIKELY(flow
)) {
3934 dp_netdev_queue_batches(packet
, flow
, &key
->mf
, batches
,
3937 /* Exact match cache missed. Group missed packets together at
3938 * the beginning of the 'packets' array. */
3939 packets
[n_missed
] = packet
;
3940 /* 'key[n_missed]' contains the key of the current packet and it
3941 * must be returned to the caller. The next key should be extracted
3942 * to 'keys[n_missed + 1]'. */
3943 key
= &keys
[++n_missed
];
3947 dp_netdev_count_packet(pmd
, DP_STAT_EXACT_HIT
, cnt
- n_dropped
- n_missed
);
3953 handle_packet_upcall(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet
*packet
,
3954 const struct netdev_flow_key
*key
,
3955 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
,
3956 int *lost_cnt
, long long now
)
3958 struct ofpbuf
*add_actions
;
3959 struct dp_packet_batch b
;
3964 match
.tun_md
.valid
= false;
3965 miniflow_expand(&key
->mf
, &match
.flow
);
3967 ofpbuf_clear(actions
);
3968 ofpbuf_clear(put_actions
);
3970 dpif_flow_hash(pmd
->dp
->dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
3971 error
= dp_netdev_upcall(pmd
, packet
, &match
.flow
, &match
.wc
,
3972 &ufid
, DPIF_UC_MISS
, NULL
, actions
,
3974 if (OVS_UNLIKELY(error
&& error
!= ENOSPC
)) {
3975 dp_packet_delete(packet
);
3980 /* The Netlink encoding of datapath flow keys cannot express
3981 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
3982 * tag is interpreted as exact match on the fact that there is no
3983 * VLAN. Unless we refactor a lot of code that translates between
3984 * Netlink and struct flow representations, we have to do the same
3986 if (!match
.wc
.masks
.vlan_tci
) {
3987 match
.wc
.masks
.vlan_tci
= htons(0xffff);
3990 /* We can't allow the packet batching in the next loop to execute
3991 * the actions. Otherwise, if there are any slow path actions,
3992 * we'll send the packet up twice. */
3993 packet_batch_init_packet(&b
, packet
);
3994 dp_netdev_execute_actions(pmd
, &b
, true, &match
.flow
,
3995 actions
->data
, actions
->size
, now
);
3997 add_actions
= put_actions
->size
? put_actions
: actions
;
3998 if (OVS_LIKELY(error
!= ENOSPC
)) {
3999 struct dp_netdev_flow
*netdev_flow
;
4001 /* XXX: There's a race window where a flow covering this packet
4002 * could have already been installed since we last did the flow
4003 * lookup before upcall. This could be solved by moving the
4004 * mutex lock outside the loop, but that's an awful long time
4005 * to be locking everyone out of making flow installs. If we
4006 * move to a per-core classifier, it would be reasonable. */
4007 ovs_mutex_lock(&pmd
->flow_mutex
);
4008 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
);
4009 if (OVS_LIKELY(!netdev_flow
)) {
4010 netdev_flow
= dp_netdev_flow_add(pmd
, &match
, &ufid
,
4014 ovs_mutex_unlock(&pmd
->flow_mutex
);
4016 emc_insert(&pmd
->flow_cache
, key
, netdev_flow
);
4021 fast_path_processing(struct dp_netdev_pmd_thread
*pmd
,
4022 struct dp_packet_batch
*packets_
,
4023 struct netdev_flow_key
*keys
,
4024 struct packet_batch_per_flow batches
[], size_t *n_batches
,
4027 int cnt
= packets_
->count
;
4028 #if !defined(__CHECKER__) && !defined(_WIN32)
4029 const size_t PKT_ARRAY_SIZE
= cnt
;
4031 /* Sparse or MSVC doesn't like variable length array. */
4032 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
4034 struct dp_packet
**packets
= packets_
->packets
;
4035 struct dpcls_rule
*rules
[PKT_ARRAY_SIZE
];
4036 struct dp_netdev
*dp
= pmd
->dp
;
4037 struct emc_cache
*flow_cache
= &pmd
->flow_cache
;
4038 int miss_cnt
= 0, lost_cnt
= 0;
4042 for (i
= 0; i
< cnt
; i
++) {
4043 /* Key length is needed in all the cases, hash computed on demand. */
4044 keys
[i
].len
= netdev_flow_key_size(miniflow_n_values(&keys
[i
].mf
));
4046 any_miss
= !dpcls_lookup(&pmd
->cls
, keys
, rules
, cnt
);
4047 if (OVS_UNLIKELY(any_miss
) && !fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
4048 uint64_t actions_stub
[512 / 8], slow_stub
[512 / 8];
4049 struct ofpbuf actions
, put_actions
;
4051 ofpbuf_use_stub(&actions
, actions_stub
, sizeof actions_stub
);
4052 ofpbuf_use_stub(&put_actions
, slow_stub
, sizeof slow_stub
);
4054 for (i
= 0; i
< cnt
; i
++) {
4055 struct dp_netdev_flow
*netdev_flow
;
4057 if (OVS_LIKELY(rules
[i
])) {
4061 /* It's possible that an earlier slow path execution installed
4062 * a rule covering this flow. In this case, it's a lot cheaper
4063 * to catch it here than execute a miss. */
4064 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, &keys
[i
]);
4066 rules
[i
] = &netdev_flow
->cr
;
4071 handle_packet_upcall(pmd
, packets
[i
], &keys
[i
], &actions
,
4072 &put_actions
, &lost_cnt
, now
);
4075 ofpbuf_uninit(&actions
);
4076 ofpbuf_uninit(&put_actions
);
4077 fat_rwlock_unlock(&dp
->upcall_rwlock
);
4078 dp_netdev_count_packet(pmd
, DP_STAT_LOST
, lost_cnt
);
4079 } else if (OVS_UNLIKELY(any_miss
)) {
4080 for (i
= 0; i
< cnt
; i
++) {
4081 if (OVS_UNLIKELY(!rules
[i
])) {
4082 dp_packet_delete(packets
[i
]);
4089 for (i
= 0; i
< cnt
; i
++) {
4090 struct dp_packet
*packet
= packets
[i
];
4091 struct dp_netdev_flow
*flow
;
4093 if (OVS_UNLIKELY(!rules
[i
])) {
4097 flow
= dp_netdev_flow_cast(rules
[i
]);
4099 emc_insert(flow_cache
, &keys
[i
], flow
);
4100 dp_netdev_queue_batches(packet
, flow
, &keys
[i
].mf
, batches
, n_batches
);
4103 dp_netdev_count_packet(pmd
, DP_STAT_MASKED_HIT
, cnt
- miss_cnt
);
4104 dp_netdev_count_packet(pmd
, DP_STAT_MISS
, miss_cnt
);
4105 dp_netdev_count_packet(pmd
, DP_STAT_LOST
, lost_cnt
);
4108 /* Packets enter the datapath from a port (or from recirculation) here.
4110 * For performance reasons a caller may choose not to initialize the metadata
4111 * in 'packets': in this case 'mdinit' is false and this function needs to
4112 * initialize it using 'port_no'. If the metadata in 'packets' is already
4113 * valid, 'md_is_valid' must be true and 'port_no' will be ignored. */
4115 dp_netdev_input__(struct dp_netdev_pmd_thread
*pmd
,
4116 struct dp_packet_batch
*packets
,
4117 bool md_is_valid
, odp_port_t port_no
)
4119 int cnt
= packets
->count
;
4120 #if !defined(__CHECKER__) && !defined(_WIN32)
4121 const size_t PKT_ARRAY_SIZE
= cnt
;
4123 /* Sparse or MSVC doesn't like variable length array. */
4124 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
4126 struct netdev_flow_key keys
[PKT_ARRAY_SIZE
];
4127 struct packet_batch_per_flow batches
[PKT_ARRAY_SIZE
];
4128 long long now
= time_msec();
4129 size_t newcnt
, n_batches
, i
;
4132 newcnt
= emc_processing(pmd
, packets
, keys
, batches
, &n_batches
,
4133 md_is_valid
, port_no
);
4134 if (OVS_UNLIKELY(newcnt
)) {
4135 packets
->count
= newcnt
;
4136 fast_path_processing(pmd
, packets
, keys
, batches
, &n_batches
, now
);
4139 for (i
= 0; i
< n_batches
; i
++) {
4140 batches
[i
].flow
->batch
= NULL
;
4143 for (i
= 0; i
< n_batches
; i
++) {
4144 packet_batch_per_flow_execute(&batches
[i
], pmd
, now
);
4149 dp_netdev_input(struct dp_netdev_pmd_thread
*pmd
,
4150 struct dp_packet_batch
*packets
,
4153 dp_netdev_input__(pmd
, packets
, false, port_no
);
4157 dp_netdev_recirculate(struct dp_netdev_pmd_thread
*pmd
,
4158 struct dp_packet_batch
*packets
)
4160 dp_netdev_input__(pmd
, packets
, true, 0);
4163 struct dp_netdev_execute_aux
{
4164 struct dp_netdev_pmd_thread
*pmd
;
4166 const struct flow
*flow
;
4170 dpif_netdev_register_dp_purge_cb(struct dpif
*dpif
, dp_purge_callback
*cb
,
4173 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4174 dp
->dp_purge_aux
= aux
;
4175 dp
->dp_purge_cb
= cb
;
4179 dpif_netdev_register_upcall_cb(struct dpif
*dpif
, upcall_callback
*cb
,
4182 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4183 dp
->upcall_aux
= aux
;
4188 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
4189 long long now
, bool purge
)
4192 struct dp_netdev_port
*port
;
4195 HMAP_FOR_EACH (tx
, node
, &pmd
->port_cache
) {
4196 if (tx
->port
->dynamic_txqs
) {
4199 interval
= now
- tx
->last_used
;
4200 if (tx
->qid
>= 0 && (purge
|| interval
>= XPS_TIMEOUT_MS
)) {
4202 ovs_mutex_lock(&port
->txq_used_mutex
);
4203 port
->txq_used
[tx
->qid
]--;
4204 ovs_mutex_unlock(&port
->txq_used_mutex
);
4211 dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
4212 struct tx_port
*tx
, long long now
)
4214 struct dp_netdev_port
*port
;
4216 int i
, min_cnt
, min_qid
;
4218 if (OVS_UNLIKELY(!now
)) {
4222 interval
= now
- tx
->last_used
;
4223 tx
->last_used
= now
;
4225 if (OVS_LIKELY(tx
->qid
>= 0 && interval
< XPS_TIMEOUT_MS
)) {
4231 ovs_mutex_lock(&port
->txq_used_mutex
);
4233 port
->txq_used
[tx
->qid
]--;
4239 for (i
= 0; i
< netdev_n_txq(port
->netdev
); i
++) {
4240 if (port
->txq_used
[i
] < min_cnt
|| min_cnt
== -1) {
4241 min_cnt
= port
->txq_used
[i
];
4246 port
->txq_used
[min_qid
]++;
4249 ovs_mutex_unlock(&port
->txq_used_mutex
);
4251 dpif_netdev_xps_revalidate_pmd(pmd
, now
, false);
4253 VLOG_DBG("Core %d: New TX queue ID %d for port \'%s\'.",
4254 pmd
->core_id
, tx
->qid
, netdev_get_name(tx
->port
->netdev
));
4258 static struct tx_port
*
4259 pmd_tx_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
4262 return tx_port_lookup(&pmd
->port_cache
, port_no
);
4266 push_tnl_action(const struct dp_netdev_pmd_thread
*pmd
,
4267 const struct nlattr
*attr
,
4268 struct dp_packet_batch
*batch
)
4270 struct tx_port
*tun_port
;
4271 const struct ovs_action_push_tnl
*data
;
4274 data
= nl_attr_get(attr
);
4276 tun_port
= pmd_tx_port_cache_lookup(pmd
, u32_to_odp(data
->tnl_port
));
4281 err
= netdev_push_header(tun_port
->port
->netdev
, batch
, data
);
4286 dp_packet_delete_batch(batch
, true);
4291 dp_execute_userspace_action(struct dp_netdev_pmd_thread
*pmd
,
4292 struct dp_packet
*packet
, bool may_steal
,
4293 struct flow
*flow
, ovs_u128
*ufid
,
4294 struct ofpbuf
*actions
,
4295 const struct nlattr
*userdata
, long long now
)
4297 struct dp_packet_batch b
;
4300 ofpbuf_clear(actions
);
4302 error
= dp_netdev_upcall(pmd
, packet
, flow
, NULL
, ufid
,
4303 DPIF_UC_ACTION
, userdata
, actions
,
4305 if (!error
|| error
== ENOSPC
) {
4306 packet_batch_init_packet(&b
, packet
);
4307 dp_netdev_execute_actions(pmd
, &b
, may_steal
, flow
,
4308 actions
->data
, actions
->size
, now
);
4309 } else if (may_steal
) {
4310 dp_packet_delete(packet
);
4315 dp_execute_cb(void *aux_
, struct dp_packet_batch
*packets_
,
4316 const struct nlattr
*a
, bool may_steal
)
4318 struct dp_netdev_execute_aux
*aux
= aux_
;
4319 uint32_t *depth
= recirc_depth_get();
4320 struct dp_netdev_pmd_thread
*pmd
= aux
->pmd
;
4321 struct dp_netdev
*dp
= pmd
->dp
;
4322 int type
= nl_attr_type(a
);
4323 long long now
= aux
->now
;
4326 switch ((enum ovs_action_attr
)type
) {
4327 case OVS_ACTION_ATTR_OUTPUT
:
4328 p
= pmd_tx_port_cache_lookup(pmd
, u32_to_odp(nl_attr_get_u32(a
)));
4329 if (OVS_LIKELY(p
)) {
4333 dynamic_txqs
= p
->port
->dynamic_txqs
;
4335 tx_qid
= dpif_netdev_xps_get_tx_qid(pmd
, p
, now
);
4337 atomic_read_relaxed(&pmd
->static_tx_qid
, &tx_qid
);
4340 netdev_send(p
->port
->netdev
, tx_qid
, packets_
, may_steal
,
4346 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
4347 if (*depth
< MAX_RECIRC_DEPTH
) {
4348 struct dp_packet_batch tnl_pkt
;
4349 struct dp_packet_batch
*orig_packets_
= packets_
;
4353 dp_packet_batch_clone(&tnl_pkt
, packets_
);
4354 packets_
= &tnl_pkt
;
4355 dp_packet_batch_reset_cutlen(orig_packets_
);
4358 dp_packet_batch_apply_cutlen(packets_
);
4360 err
= push_tnl_action(pmd
, a
, packets_
);
4363 dp_netdev_recirculate(pmd
, packets_
);
4370 case OVS_ACTION_ATTR_TUNNEL_POP
:
4371 if (*depth
< MAX_RECIRC_DEPTH
) {
4372 struct dp_packet_batch
*orig_packets_
= packets_
;
4373 odp_port_t portno
= u32_to_odp(nl_attr_get_u32(a
));
4375 p
= pmd_tx_port_cache_lookup(pmd
, portno
);
4377 struct dp_packet_batch tnl_pkt
;
4381 dp_packet_batch_clone(&tnl_pkt
, packets_
);
4382 packets_
= &tnl_pkt
;
4383 dp_packet_batch_reset_cutlen(orig_packets_
);
4386 dp_packet_batch_apply_cutlen(packets_
);
4388 netdev_pop_header(p
->port
->netdev
, packets_
);
4389 if (!packets_
->count
) {
4393 for (i
= 0; i
< packets_
->count
; i
++) {
4394 packets_
->packets
[i
]->md
.in_port
.odp_port
= portno
;
4398 dp_netdev_recirculate(pmd
, packets_
);
4405 case OVS_ACTION_ATTR_USERSPACE
:
4406 if (!fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
4407 struct dp_packet_batch
*orig_packets_
= packets_
;
4408 struct dp_packet
**packets
= packets_
->packets
;
4409 const struct nlattr
*userdata
;
4410 struct dp_packet_batch usr_pkt
;
4411 struct ofpbuf actions
;
4417 userdata
= nl_attr_find_nested(a
, OVS_USERSPACE_ATTR_USERDATA
);
4418 ofpbuf_init(&actions
, 0);
4420 if (packets_
->trunc
) {
4422 dp_packet_batch_clone(&usr_pkt
, packets_
);
4423 packets_
= &usr_pkt
;
4424 packets
= packets_
->packets
;
4426 dp_packet_batch_reset_cutlen(orig_packets_
);
4429 dp_packet_batch_apply_cutlen(packets_
);
4432 for (i
= 0; i
< packets_
->count
; i
++) {
4433 flow_extract(packets
[i
], &flow
);
4434 dpif_flow_hash(dp
->dpif
, &flow
, sizeof flow
, &ufid
);
4435 dp_execute_userspace_action(pmd
, packets
[i
], may_steal
, &flow
,
4436 &ufid
, &actions
, userdata
, now
);
4440 dp_packet_delete_batch(packets_
, true);
4443 ofpbuf_uninit(&actions
);
4444 fat_rwlock_unlock(&dp
->upcall_rwlock
);
4450 case OVS_ACTION_ATTR_RECIRC
:
4451 if (*depth
< MAX_RECIRC_DEPTH
) {
4452 struct dp_packet_batch recirc_pkts
;
4456 dp_packet_batch_clone(&recirc_pkts
, packets_
);
4457 packets_
= &recirc_pkts
;
4460 for (i
= 0; i
< packets_
->count
; i
++) {
4461 packets_
->packets
[i
]->md
.recirc_id
= nl_attr_get_u32(a
);
4465 dp_netdev_recirculate(pmd
, packets_
);
4471 VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
4474 case OVS_ACTION_ATTR_CT
: {
4475 const struct nlattr
*b
;
4476 bool commit
= false;
4479 const char *helper
= NULL
;
4480 const uint32_t *setmark
= NULL
;
4481 const struct ovs_key_ct_labels
*setlabel
= NULL
;
4483 NL_ATTR_FOR_EACH_UNSAFE (b
, left
, nl_attr_get(a
),
4484 nl_attr_get_size(a
)) {
4485 enum ovs_ct_attr sub_type
= nl_attr_type(b
);
4488 case OVS_CT_ATTR_COMMIT
:
4491 case OVS_CT_ATTR_ZONE
:
4492 zone
= nl_attr_get_u16(b
);
4494 case OVS_CT_ATTR_HELPER
:
4495 helper
= nl_attr_get_string(b
);
4497 case OVS_CT_ATTR_MARK
:
4498 setmark
= nl_attr_get(b
);
4500 case OVS_CT_ATTR_LABELS
:
4501 setlabel
= nl_attr_get(b
);
4503 case OVS_CT_ATTR_NAT
:
4504 case OVS_CT_ATTR_UNSPEC
:
4505 case __OVS_CT_ATTR_MAX
:
4510 conntrack_execute(&dp
->conntrack
, packets_
, aux
->flow
->dl_type
, commit
,
4511 zone
, setmark
, setlabel
, helper
);
4515 case OVS_ACTION_ATTR_PUSH_VLAN
:
4516 case OVS_ACTION_ATTR_POP_VLAN
:
4517 case OVS_ACTION_ATTR_PUSH_MPLS
:
4518 case OVS_ACTION_ATTR_POP_MPLS
:
4519 case OVS_ACTION_ATTR_SET
:
4520 case OVS_ACTION_ATTR_SET_MASKED
:
4521 case OVS_ACTION_ATTR_SAMPLE
:
4522 case OVS_ACTION_ATTR_HASH
:
4523 case OVS_ACTION_ATTR_UNSPEC
:
4524 case OVS_ACTION_ATTR_TRUNC
:
4525 case __OVS_ACTION_ATTR_MAX
:
4529 dp_packet_delete_batch(packets_
, may_steal
);
4533 dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
4534 struct dp_packet_batch
*packets
,
4535 bool may_steal
, const struct flow
*flow
,
4536 const struct nlattr
*actions
, size_t actions_len
,
4539 struct dp_netdev_execute_aux aux
= { pmd
, now
, flow
};
4541 odp_execute_actions(&aux
, packets
, may_steal
, actions
,
4542 actions_len
, dp_execute_cb
);
4545 struct dp_netdev_ct_dump
{
4546 struct ct_dpif_dump_state up
;
4547 struct conntrack_dump dump
;
4548 struct conntrack
*ct
;
4549 struct dp_netdev
*dp
;
4553 dpif_netdev_ct_dump_start(struct dpif
*dpif
, struct ct_dpif_dump_state
**dump_
,
4554 const uint16_t *pzone
)
4556 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4557 struct dp_netdev_ct_dump
*dump
;
4559 dump
= xzalloc(sizeof *dump
);
4561 dump
->ct
= &dp
->conntrack
;
4563 conntrack_dump_start(&dp
->conntrack
, &dump
->dump
, pzone
);
4571 dpif_netdev_ct_dump_next(struct dpif
*dpif OVS_UNUSED
,
4572 struct ct_dpif_dump_state
*dump_
,
4573 struct ct_dpif_entry
*entry
)
4575 struct dp_netdev_ct_dump
*dump
;
4577 INIT_CONTAINER(dump
, dump_
, up
);
4579 return conntrack_dump_next(&dump
->dump
, entry
);
4583 dpif_netdev_ct_dump_done(struct dpif
*dpif OVS_UNUSED
,
4584 struct ct_dpif_dump_state
*dump_
)
4586 struct dp_netdev_ct_dump
*dump
;
4589 INIT_CONTAINER(dump
, dump_
, up
);
4591 err
= conntrack_dump_done(&dump
->dump
);
4599 dpif_netdev_ct_flush(struct dpif
*dpif
, const uint16_t *zone
)
4601 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4603 return conntrack_flush(&dp
->conntrack
, zone
);
4606 const struct dpif_class dpif_netdev_class
= {
4609 dpif_netdev_enumerate
,
4610 dpif_netdev_port_open_type
,
4613 dpif_netdev_destroy
,
4616 dpif_netdev_get_stats
,
4617 dpif_netdev_port_add
,
4618 dpif_netdev_port_del
,
4619 dpif_netdev_port_set_config
,
4620 dpif_netdev_port_query_by_number
,
4621 dpif_netdev_port_query_by_name
,
4622 NULL
, /* port_get_pid */
4623 dpif_netdev_port_dump_start
,
4624 dpif_netdev_port_dump_next
,
4625 dpif_netdev_port_dump_done
,
4626 dpif_netdev_port_poll
,
4627 dpif_netdev_port_poll_wait
,
4628 dpif_netdev_flow_flush
,
4629 dpif_netdev_flow_dump_create
,
4630 dpif_netdev_flow_dump_destroy
,
4631 dpif_netdev_flow_dump_thread_create
,
4632 dpif_netdev_flow_dump_thread_destroy
,
4633 dpif_netdev_flow_dump_next
,
4634 dpif_netdev_operate
,
4635 NULL
, /* recv_set */
4636 NULL
, /* handlers_set */
4637 dpif_netdev_pmd_set
,
4638 dpif_netdev_queue_to_priority
,
4640 NULL
, /* recv_wait */
4641 NULL
, /* recv_purge */
4642 dpif_netdev_register_dp_purge_cb
,
4643 dpif_netdev_register_upcall_cb
,
4644 dpif_netdev_enable_upcall
,
4645 dpif_netdev_disable_upcall
,
4646 dpif_netdev_get_datapath_version
,
4647 dpif_netdev_ct_dump_start
,
4648 dpif_netdev_ct_dump_next
,
4649 dpif_netdev_ct_dump_done
,
4650 dpif_netdev_ct_flush
,
4654 dpif_dummy_change_port_number(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
4655 const char *argv
[], void *aux OVS_UNUSED
)
4657 struct dp_netdev_port
*port
;
4658 struct dp_netdev
*dp
;
4661 ovs_mutex_lock(&dp_netdev_mutex
);
4662 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
4663 if (!dp
|| !dpif_netdev_class_is_dummy(dp
->class)) {
4664 ovs_mutex_unlock(&dp_netdev_mutex
);
4665 unixctl_command_reply_error(conn
, "unknown datapath or not a dummy");
4668 ovs_refcount_ref(&dp
->ref_cnt
);
4669 ovs_mutex_unlock(&dp_netdev_mutex
);
4671 ovs_mutex_lock(&dp
->port_mutex
);
4672 if (get_port_by_name(dp
, argv
[2], &port
)) {
4673 unixctl_command_reply_error(conn
, "unknown port");
4677 port_no
= u32_to_odp(atoi(argv
[3]));
4678 if (!port_no
|| port_no
== ODPP_NONE
) {
4679 unixctl_command_reply_error(conn
, "bad port number");
4682 if (dp_netdev_lookup_port(dp
, port_no
)) {
4683 unixctl_command_reply_error(conn
, "port number already in use");
4688 hmap_remove(&dp
->ports
, &port
->node
);
4689 dp_netdev_del_port_from_all_pmds(dp
, port
);
4691 /* Reinsert with new port number. */
4692 port
->port_no
= port_no
;
4693 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
4694 dp_netdev_add_port_to_pmds(dp
, port
);
4696 seq_change(dp
->port_seq
);
4697 unixctl_command_reply(conn
, NULL
);
4700 ovs_mutex_unlock(&dp
->port_mutex
);
4701 dp_netdev_unref(dp
);
4705 dpif_dummy_register__(const char *type
)
4707 struct dpif_class
*class;
4709 class = xmalloc(sizeof *class);
4710 *class = dpif_netdev_class
;
4711 class->type
= xstrdup(type
);
4712 dp_register_provider(class);
4716 dpif_dummy_override(const char *type
)
4721 * Ignore EAFNOSUPPORT to allow --enable-dummy=system with
4722 * a userland-only build. It's useful for testsuite.
4724 error
= dp_unregister_provider(type
);
4725 if (error
== 0 || error
== EAFNOSUPPORT
) {
4726 dpif_dummy_register__(type
);
4731 dpif_dummy_register(enum dummy_level level
)
4733 if (level
== DUMMY_OVERRIDE_ALL
) {
4738 dp_enumerate_types(&types
);
4739 SSET_FOR_EACH (type
, &types
) {
4740 dpif_dummy_override(type
);
4742 sset_destroy(&types
);
4743 } else if (level
== DUMMY_OVERRIDE_SYSTEM
) {
4744 dpif_dummy_override("system");
4747 dpif_dummy_register__("dummy");
4749 unixctl_command_register("dpif-dummy/change-port-number",
4750 "dp port new-number",
4751 3, 3, dpif_dummy_change_port_number
, NULL
);
4754 /* Datapath Classifier. */
4756 /* A set of rules that all have the same fields wildcarded. */
4757 struct dpcls_subtable
{
4758 /* The fields are only used by writers. */
4759 struct cmap_node cmap_node OVS_GUARDED
; /* Within dpcls 'subtables_map'. */
4761 /* These fields are accessed by readers. */
4762 struct cmap rules
; /* Contains "struct dpcls_rule"s. */
4763 struct netdev_flow_key mask
; /* Wildcards for fields (const). */
4764 /* 'mask' must be the last field, additional space is allocated here. */
4767 /* Initializes 'cls' as a classifier that initially contains no classification
4770 dpcls_init(struct dpcls
*cls
)
4772 cmap_init(&cls
->subtables_map
);
4773 cls
->subtables
= pvector_alloc(4);
4777 dpcls_destroy_subtable(struct dpcls
*cls
, struct dpcls_subtable
*subtable
)
4779 pvector_remove(cls
->subtables
, subtable
);
4780 cmap_remove(&cls
->subtables_map
, &subtable
->cmap_node
,
4781 subtable
->mask
.hash
);
4782 cmap_destroy(&subtable
->rules
);
4783 ovsrcu_postpone(free
, subtable
);
4786 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
4787 * caller's responsibility.
4788 * May only be called after all the readers have been terminated. */
4790 dpcls_destroy(struct dpcls
*cls
)
4793 struct dpcls_subtable
*subtable
;
4795 CMAP_FOR_EACH (subtable
, cmap_node
, &cls
->subtables_map
) {
4796 ovs_assert(cmap_count(&subtable
->rules
) == 0);
4797 dpcls_destroy_subtable(cls
, subtable
);
4799 cmap_destroy(&cls
->subtables_map
);
4800 free(cls
->subtables
);
4804 static struct dpcls_subtable
*
4805 dpcls_create_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
4807 struct dpcls_subtable
*subtable
;
4809 /* Need to add one. */
4810 subtable
= xmalloc(sizeof *subtable
4811 - sizeof subtable
->mask
.mf
+ mask
->len
);
4812 cmap_init(&subtable
->rules
);
4813 netdev_flow_key_clone(&subtable
->mask
, mask
);
4814 cmap_insert(&cls
->subtables_map
, &subtable
->cmap_node
, mask
->hash
);
4815 pvector_push_back(&cls
->subtables
, subtable
, 0);
4820 static inline struct dpcls_subtable
*
4821 dpcls_find_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
4823 struct dpcls_subtable
*subtable
;
4825 CMAP_FOR_EACH_WITH_HASH (subtable
, cmap_node
, mask
->hash
,
4826 &cls
->subtables_map
) {
4827 if (netdev_flow_key_equal(&subtable
->mask
, mask
)) {
4831 return dpcls_create_subtable(cls
, mask
);
4834 /* Insert 'rule' into 'cls'. */
4836 dpcls_insert(struct dpcls
*cls
, struct dpcls_rule
*rule
,
4837 const struct netdev_flow_key
*mask
)
4839 struct dpcls_subtable
*subtable
= dpcls_find_subtable(cls
, mask
);
4841 rule
->mask
= &subtable
->mask
;
4842 cmap_insert(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
);
4845 /* Removes 'rule' from 'cls', also destructing the 'rule'. */
4847 dpcls_remove(struct dpcls
*cls
, struct dpcls_rule
*rule
)
4849 struct dpcls_subtable
*subtable
;
4851 ovs_assert(rule
->mask
);
4853 INIT_CONTAINER(subtable
, rule
->mask
, mask
);
4855 if (cmap_remove(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
)
4857 dpcls_destroy_subtable(cls
, subtable
);
4861 /* Returns true if 'target' satisfies 'key' in 'mask', that is, if each 1-bit
4862 * in 'mask' the values in 'key' and 'target' are the same. */
4864 dpcls_rule_matches_key(const struct dpcls_rule
*rule
,
4865 const struct netdev_flow_key
*target
)
4867 const uint64_t *keyp
= miniflow_get_values(&rule
->flow
.mf
);
4868 const uint64_t *maskp
= miniflow_get_values(&rule
->mask
->mf
);
4871 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, target
, rule
->flow
.mf
.map
) {
4872 if (OVS_UNLIKELY((value
& *maskp
++) != *keyp
++)) {
4879 /* For each miniflow in 'flows' performs a classifier lookup writing the result
4880 * into the corresponding slot in 'rules'. If a particular entry in 'flows' is
4881 * NULL it is skipped.
4883 * This function is optimized for use in the userspace datapath and therefore
4884 * does not implement a lot of features available in the standard
4885 * classifier_lookup() function. Specifically, it does not implement
4886 * priorities, instead returning any rule which matches the flow.
4888 * Returns true if all flows found a corresponding rule. */
4890 dpcls_lookup(const struct dpcls
*cls
, const struct netdev_flow_key keys
[],
4891 struct dpcls_rule
**rules
, const size_t cnt
)
4893 /* The batch size 16 was experimentally found faster than 8 or 32. */
4894 typedef uint16_t map_type
;
4895 #define MAP_BITS (sizeof(map_type) * CHAR_BIT)
4897 #if !defined(__CHECKER__) && !defined(_WIN32)
4898 const int N_MAPS
= DIV_ROUND_UP(cnt
, MAP_BITS
);
4900 enum { N_MAPS
= DIV_ROUND_UP(NETDEV_MAX_BURST
, MAP_BITS
) };
4902 map_type maps
[N_MAPS
];
4903 struct dpcls_subtable
*subtable
;
4905 memset(maps
, 0xff, sizeof maps
);
4906 if (cnt
% MAP_BITS
) {
4907 maps
[N_MAPS
- 1] >>= MAP_BITS
- cnt
% MAP_BITS
; /* Clear extra bits. */
4909 memset(rules
, 0, cnt
* sizeof *rules
);
4911 PVECTOR_FOR_EACH (subtable
, cls
->subtables
) {
4912 const struct netdev_flow_key
*mkeys
= keys
;
4913 struct dpcls_rule
**mrules
= rules
;
4914 map_type remains
= 0;
4917 BUILD_ASSERT_DECL(sizeof remains
== sizeof *maps
);
4919 for (m
= 0; m
< N_MAPS
; m
++, mkeys
+= MAP_BITS
, mrules
+= MAP_BITS
) {
4920 uint32_t hashes
[MAP_BITS
];
4921 const struct cmap_node
*nodes
[MAP_BITS
];
4922 unsigned long map
= maps
[m
];
4926 continue; /* Skip empty maps. */
4929 /* Compute hashes for the remaining keys. */
4930 ULLONG_FOR_EACH_1(i
, map
) {
4931 hashes
[i
] = netdev_flow_key_hash_in_mask(&mkeys
[i
],
4935 map
= cmap_find_batch(&subtable
->rules
, map
, hashes
, nodes
);
4936 /* Check results. */
4937 ULLONG_FOR_EACH_1(i
, map
) {
4938 struct dpcls_rule
*rule
;
4940 CMAP_NODE_FOR_EACH (rule
, cmap_node
, nodes
[i
]) {
4941 if (OVS_LIKELY(dpcls_rule_matches_key(rule
, &mkeys
[i
]))) {
4946 ULLONG_SET0(map
, i
); /* Did not match. */
4948 ; /* Keep Sparse happy. */
4950 maps
[m
] &= ~map
; /* Clear the found rules. */
4954 return true; /* All found. */
4957 return false; /* Some misses. */