2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2016, 2017 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 <sys/types.h>
26 #include <netinet/in.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
37 #include "conntrack.h"
41 #include "dp-packet.h"
43 #include "dpif-netdev-perf.h"
44 #include "dpif-provider.h"
46 #include "fat-rwlock.h"
52 #include "netdev-provider.h"
53 #include "netdev-vport.h"
55 #include "odp-execute.h"
57 #include "openvswitch/dynamic-string.h"
58 #include "openvswitch/list.h"
59 #include "openvswitch/match.h"
60 #include "openvswitch/ofp-parse.h"
61 #include "openvswitch/ofp-print.h"
62 #include "openvswitch/ofpbuf.h"
63 #include "openvswitch/shash.h"
64 #include "openvswitch/vlog.h"
68 #include "openvswitch/poll-loop.h"
75 #include "tnl-neigh-cache.h"
76 #include "tnl-ports.h"
81 VLOG_DEFINE_THIS_MODULE(dpif_netdev
);
83 #define FLOW_DUMP_MAX_BATCH 50
84 /* Use per thread recirc_depth to prevent recirculation loop. */
85 #define MAX_RECIRC_DEPTH 6
86 DEFINE_STATIC_PER_THREAD_DATA(uint32_t, recirc_depth
, 0)
88 /* Use instant packet send by default. */
89 #define DEFAULT_TX_FLUSH_INTERVAL 0
91 /* Configuration parameters. */
92 enum { MAX_FLOWS
= 65536 }; /* Maximum number of flows in flow table. */
93 enum { MAX_METERS
= 65536 }; /* Maximum number of meters. */
94 enum { MAX_BANDS
= 8 }; /* Maximum number of bands / meter. */
95 enum { N_METER_LOCKS
= 64 }; /* Maximum number of meters. */
97 /* Protects against changes to 'dp_netdevs'. */
98 static struct ovs_mutex dp_netdev_mutex
= OVS_MUTEX_INITIALIZER
;
100 /* Contains all 'struct dp_netdev's. */
101 static struct shash dp_netdevs
OVS_GUARDED_BY(dp_netdev_mutex
)
102 = SHASH_INITIALIZER(&dp_netdevs
);
104 static struct vlog_rate_limit upcall_rl
= VLOG_RATE_LIMIT_INIT(600, 600);
106 #define DP_NETDEV_CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \
107 | CS_INVALID | CS_REPLY_DIR | CS_TRACKED \
108 | CS_SRC_NAT | CS_DST_NAT)
109 #define DP_NETDEV_CS_UNSUPPORTED_MASK (~(uint32_t)DP_NETDEV_CS_SUPPORTED_MASK)
111 static struct odp_support dp_netdev_support
= {
112 .max_vlan_headers
= SIZE_MAX
,
113 .max_mpls_depth
= SIZE_MAX
,
119 .ct_state_nat
= true,
120 .ct_orig_tuple
= true,
121 .ct_orig_tuple6
= true,
124 /* Stores a miniflow with inline values */
126 struct netdev_flow_key
{
127 uint32_t hash
; /* Hash function differs for different users. */
128 uint32_t len
; /* Length of the following miniflow (incl. map). */
130 uint64_t buf
[FLOW_MAX_PACKET_U64S
];
133 /* EMC cache and SMC cache compose the datapath flow cache (DFC)
135 * Exact match cache for frequently used flows
137 * The cache uses a 32-bit hash of the packet (which can be the RSS hash) to
138 * search its entries for a miniflow that matches exactly the miniflow of the
139 * packet. It stores the 'dpcls_rule' (rule) that matches the miniflow.
141 * A cache entry holds a reference to its 'dp_netdev_flow'.
143 * A miniflow with a given hash can be in one of EM_FLOW_HASH_SEGS different
144 * entries. The 32-bit hash is split into EM_FLOW_HASH_SEGS values (each of
145 * them is EM_FLOW_HASH_SHIFT bits wide and the remainder is thrown away). Each
146 * value is the index of a cache entry where the miniflow could be.
149 * Signature match cache (SMC)
151 * This cache stores a 16-bit signature for each flow without storing keys, and
152 * stores the corresponding 16-bit flow_table index to the 'dp_netdev_flow'.
153 * Each flow thus occupies 32bit which is much more memory efficient than EMC.
154 * SMC uses a set-associative design that each bucket contains
155 * SMC_ENTRY_PER_BUCKET number of entries.
156 * Since 16-bit flow_table index is used, if there are more than 2^16
157 * dp_netdev_flow, SMC will miss them that cannot be indexed by a 16-bit value.
163 * Each pmd_thread has its own private exact match cache.
164 * If dp_netdev_input is not called from a pmd thread, a mutex is used.
167 #define EM_FLOW_HASH_SHIFT 13
168 #define EM_FLOW_HASH_ENTRIES (1u << EM_FLOW_HASH_SHIFT)
169 #define EM_FLOW_HASH_MASK (EM_FLOW_HASH_ENTRIES - 1)
170 #define EM_FLOW_HASH_SEGS 2
172 /* SMC uses a set-associative design. A bucket contains a set of entries that
173 * a flow item can occupy. For now, it uses one hash function rather than two
174 * as for the EMC design. */
175 #define SMC_ENTRY_PER_BUCKET 4
176 #define SMC_ENTRIES (1u << 20)
177 #define SMC_BUCKET_CNT (SMC_ENTRIES / SMC_ENTRY_PER_BUCKET)
178 #define SMC_MASK (SMC_BUCKET_CNT - 1)
180 /* Default EMC insert probability is 1 / DEFAULT_EM_FLOW_INSERT_INV_PROB */
181 #define DEFAULT_EM_FLOW_INSERT_INV_PROB 100
182 #define DEFAULT_EM_FLOW_INSERT_MIN (UINT32_MAX / \
183 DEFAULT_EM_FLOW_INSERT_INV_PROB)
186 struct dp_netdev_flow
*flow
;
187 struct netdev_flow_key key
; /* key.hash used for emc hash value. */
191 struct emc_entry entries
[EM_FLOW_HASH_ENTRIES
];
192 int sweep_idx
; /* For emc_cache_slow_sweep(). */
196 uint16_t sig
[SMC_ENTRY_PER_BUCKET
];
197 uint16_t flow_idx
[SMC_ENTRY_PER_BUCKET
];
200 /* Signature match cache, differentiate from EMC cache */
202 struct smc_bucket buckets
[SMC_BUCKET_CNT
];
206 struct emc_cache emc_cache
;
207 struct smc_cache smc_cache
;
210 /* Iterate in the exact match cache through every entry that might contain a
211 * miniflow with hash 'HASH'. */
212 #define EMC_FOR_EACH_POS_WITH_HASH(EMC, CURRENT_ENTRY, HASH) \
213 for (uint32_t i__ = 0, srch_hash__ = (HASH); \
214 (CURRENT_ENTRY) = &(EMC)->entries[srch_hash__ & EM_FLOW_HASH_MASK], \
215 i__ < EM_FLOW_HASH_SEGS; \
216 i__++, srch_hash__ >>= EM_FLOW_HASH_SHIFT)
218 /* Simple non-wildcarding single-priority classifier. */
220 /* Time in microseconds between successive optimizations of the dpcls
222 #define DPCLS_OPTIMIZATION_INTERVAL 1000000LL
224 /* Time in microseconds of the interval in which rxq processing cycles used
225 * in rxq to pmd assignments is measured and stored. */
226 #define PMD_RXQ_INTERVAL_LEN 10000000LL
228 /* Number of intervals for which cycles are stored
229 * and used during rxq to pmd assignment. */
230 #define PMD_RXQ_INTERVAL_MAX 6
233 struct cmap_node node
; /* Within dp_netdev_pmd_thread.classifiers */
235 struct cmap subtables_map
;
236 struct pvector subtables
;
239 /* A rule to be inserted to the classifier. */
241 struct cmap_node cmap_node
; /* Within struct dpcls_subtable 'rules'. */
242 struct netdev_flow_key
*mask
; /* Subtable's mask. */
243 struct netdev_flow_key flow
; /* Matching key. */
244 /* 'flow' must be the last field, additional space is allocated here. */
247 static void dpcls_init(struct dpcls
*);
248 static void dpcls_destroy(struct dpcls
*);
249 static void dpcls_sort_subtable_vector(struct dpcls
*);
250 static void dpcls_insert(struct dpcls
*, struct dpcls_rule
*,
251 const struct netdev_flow_key
*mask
);
252 static void dpcls_remove(struct dpcls
*, struct dpcls_rule
*);
253 static bool dpcls_lookup(struct dpcls
*cls
,
254 const struct netdev_flow_key
*keys
[],
255 struct dpcls_rule
**rules
, size_t cnt
,
257 static bool dpcls_rule_matches_key(const struct dpcls_rule
*rule
,
258 const struct netdev_flow_key
*target
);
259 /* Set of supported meter flags */
260 #define DP_SUPPORTED_METER_FLAGS_MASK \
261 (OFPMF13_STATS | OFPMF13_PKTPS | OFPMF13_KBPS | OFPMF13_BURST)
263 /* Set of supported meter band types */
264 #define DP_SUPPORTED_METER_BAND_TYPES \
265 ( 1 << OFPMBT13_DROP )
267 struct dp_meter_band
{
268 struct ofputil_meter_band up
; /* type, prec_level, pad, rate, burst_size */
269 uint32_t bucket
; /* In 1/1000 packets (for PKTPS), or in bits (for KBPS) */
270 uint64_t packet_count
;
277 uint32_t max_delta_t
;
279 uint64_t packet_count
;
281 struct dp_meter_band bands
[];
284 /* Datapath based on the network device interface from netdev.h.
290 * Some members, marked 'const', are immutable. Accessing other members
291 * requires synchronization, as noted in more detail below.
293 * Acquisition order is, from outermost to innermost:
295 * dp_netdev_mutex (global)
300 const struct dpif_class
*const class;
301 const char *const name
;
303 struct ovs_refcount ref_cnt
;
304 atomic_flag destroyed
;
308 * Any lookup into 'ports' or any access to the dp_netdev_ports found
309 * through 'ports' requires taking 'port_mutex'. */
310 struct ovs_mutex port_mutex
;
312 struct seq
*port_seq
; /* Incremented whenever a port changes. */
314 /* The time that a packet can wait in output batch for sending. */
315 atomic_uint32_t tx_flush_interval
;
318 struct ovs_mutex meter_locks
[N_METER_LOCKS
];
319 struct dp_meter
*meters
[MAX_METERS
]; /* Meter bands. */
321 /* Probability of EMC insertions is a factor of 'emc_insert_min'.*/
322 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
) atomic_uint32_t emc_insert_min
;
323 /* Enable collection of PMD performance metrics. */
324 atomic_bool pmd_perf_metrics
;
325 /* Enable the SMC cache from ovsdb config */
326 atomic_bool smc_enable_db
;
328 /* Protects access to ofproto-dpif-upcall interface during revalidator
329 * thread synchronization. */
330 struct fat_rwlock upcall_rwlock
;
331 upcall_callback
*upcall_cb
; /* Callback function for executing upcalls. */
334 /* Callback function for notifying the purging of dp flows (during
335 * reseting pmd deletion). */
336 dp_purge_callback
*dp_purge_cb
;
339 /* Stores all 'struct dp_netdev_pmd_thread's. */
340 struct cmap poll_threads
;
341 /* id pool for per thread static_tx_qid. */
342 struct id_pool
*tx_qid_pool
;
343 struct ovs_mutex tx_qid_pool_mutex
;
345 /* Protects the access of the 'struct dp_netdev_pmd_thread'
346 * instance for non-pmd thread. */
347 struct ovs_mutex non_pmd_mutex
;
349 /* Each pmd thread will store its pointer to
350 * 'struct dp_netdev_pmd_thread' in 'per_pmd_key'. */
351 ovsthread_key_t per_pmd_key
;
353 struct seq
*reconfigure_seq
;
354 uint64_t last_reconfigure_seq
;
356 /* Cpu mask for pin of pmd threads. */
359 uint64_t last_tnl_conf_seq
;
361 struct conntrack conntrack
;
364 static void meter_lock(const struct dp_netdev
*dp
, uint32_t meter_id
)
365 OVS_ACQUIRES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
367 ovs_mutex_lock(&dp
->meter_locks
[meter_id
% N_METER_LOCKS
]);
370 static void meter_unlock(const struct dp_netdev
*dp
, uint32_t meter_id
)
371 OVS_RELEASES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
373 ovs_mutex_unlock(&dp
->meter_locks
[meter_id
% N_METER_LOCKS
]);
377 static struct dp_netdev_port
*dp_netdev_lookup_port(const struct dp_netdev
*dp
,
379 OVS_REQUIRES(dp
->port_mutex
);
381 enum rxq_cycles_counter_type
{
382 RXQ_CYCLES_PROC_CURR
, /* Cycles spent successfully polling and
383 processing packets during the current
385 RXQ_CYCLES_PROC_HIST
, /* Total cycles of all intervals that are used
386 during rxq to pmd assignment. */
391 DP_NETDEV_FLOW_OFFLOAD_OP_ADD
,
392 DP_NETDEV_FLOW_OFFLOAD_OP_MOD
,
393 DP_NETDEV_FLOW_OFFLOAD_OP_DEL
,
396 struct dp_flow_offload_item
{
397 struct dp_netdev_pmd_thread
*pmd
;
398 struct dp_netdev_flow
*flow
;
401 struct nlattr
*actions
;
404 struct ovs_list node
;
407 struct dp_flow_offload
{
408 struct ovs_mutex mutex
;
409 struct ovs_list list
;
413 static struct dp_flow_offload dp_flow_offload
= {
414 .mutex
= OVS_MUTEX_INITIALIZER
,
415 .list
= OVS_LIST_INITIALIZER(&dp_flow_offload
.list
),
418 static struct ovsthread_once offload_thread_once
419 = OVSTHREAD_ONCE_INITIALIZER
;
421 #define XPS_TIMEOUT 500000LL /* In microseconds. */
423 /* Contained by struct dp_netdev_port's 'rxqs' member. */
424 struct dp_netdev_rxq
{
425 struct dp_netdev_port
*port
;
426 struct netdev_rxq
*rx
;
427 unsigned core_id
; /* Core to which this queue should be
428 pinned. OVS_CORE_UNSPEC if the
429 queue doesn't need to be pinned to a
431 unsigned intrvl_idx
; /* Write index for 'cycles_intrvl'. */
432 struct dp_netdev_pmd_thread
*pmd
; /* pmd thread that polls this queue. */
433 bool is_vhost
; /* Is rxq of a vhost port. */
435 /* Counters of cycles spent successfully polling and processing pkts. */
436 atomic_ullong cycles
[RXQ_N_CYCLES
];
437 /* We store PMD_RXQ_INTERVAL_MAX intervals of data for an rxq and then
438 sum them to yield the cycles used for an rxq. */
439 atomic_ullong cycles_intrvl
[PMD_RXQ_INTERVAL_MAX
];
442 /* A port in a netdev-based datapath. */
443 struct dp_netdev_port
{
445 bool dynamic_txqs
; /* If true XPS will be used. */
446 bool need_reconfigure
; /* True if we should reconfigure netdev. */
447 struct netdev
*netdev
;
448 struct hmap_node node
; /* Node in dp_netdev's 'ports'. */
449 struct netdev_saved_flags
*sf
;
450 struct dp_netdev_rxq
*rxqs
;
451 unsigned n_rxq
; /* Number of elements in 'rxqs' */
452 unsigned *txq_used
; /* Number of threads that use each tx queue. */
453 struct ovs_mutex txq_used_mutex
;
454 char *type
; /* Port type as requested by user. */
455 char *rxq_affinity_list
; /* Requested affinity of rx queues. */
458 /* Contained by struct dp_netdev_flow's 'stats' member. */
459 struct dp_netdev_flow_stats
{
460 atomic_llong used
; /* Last used time, in monotonic msecs. */
461 atomic_ullong packet_count
; /* Number of packets matched. */
462 atomic_ullong byte_count
; /* Number of bytes matched. */
463 atomic_uint16_t tcp_flags
; /* Bitwise-OR of seen tcp_flags values. */
466 /* A flow in 'dp_netdev_pmd_thread's 'flow_table'.
472 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
473 * its pmd thread's classifier. The text below calls this classifier 'cls'.
478 * The thread safety rules described here for "struct dp_netdev_flow" are
479 * motivated by two goals:
481 * - Prevent threads that read members of "struct dp_netdev_flow" from
482 * reading bad data due to changes by some thread concurrently modifying
485 * - Prevent two threads making changes to members of a given "struct
486 * dp_netdev_flow" from interfering with each other.
492 * A flow 'flow' may be accessed without a risk of being freed during an RCU
493 * grace period. Code that needs to hold onto a flow for a while
494 * should try incrementing 'flow->ref_cnt' with dp_netdev_flow_ref().
496 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
497 * flow from being deleted from 'cls' and it doesn't protect members of 'flow'
500 * Some members, marked 'const', are immutable. Accessing other members
501 * requires synchronization, as noted in more detail below.
503 struct dp_netdev_flow
{
504 const struct flow flow
; /* Unmasked flow that created this entry. */
505 /* Hash table index by unmasked flow. */
506 const struct cmap_node node
; /* In owning dp_netdev_pmd_thread's */
508 const struct cmap_node mark_node
; /* In owning flow_mark's mark_to_flow */
509 const ovs_u128 ufid
; /* Unique flow identifier. */
510 const ovs_u128 mega_ufid
; /* Unique mega flow identifier. */
511 const unsigned pmd_id
; /* The 'core_id' of pmd thread owning this */
514 /* Number of references.
515 * The classifier owns one reference.
516 * Any thread trying to keep a rule from being freed should hold its own
518 struct ovs_refcount ref_cnt
;
521 uint32_t mark
; /* Unique flow mark assigned to a flow */
524 struct dp_netdev_flow_stats stats
;
527 OVSRCU_TYPE(struct dp_netdev_actions
*) actions
;
529 /* While processing a group of input packets, the datapath uses the next
530 * member to store a pointer to the output batch for the flow. It is
531 * reset after the batch has been sent out (See dp_netdev_queue_batches(),
532 * packet_batch_per_flow_init() and packet_batch_per_flow_execute()). */
533 struct packet_batch_per_flow
*batch
;
535 /* Packet classification. */
536 struct dpcls_rule cr
; /* In owning dp_netdev's 'cls'. */
537 /* 'cr' must be the last member. */
540 static void dp_netdev_flow_unref(struct dp_netdev_flow
*);
541 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*);
542 static int dpif_netdev_flow_from_nlattrs(const struct nlattr
*, uint32_t,
543 struct flow
*, bool);
545 /* A set of datapath actions within a "struct dp_netdev_flow".
551 * A struct dp_netdev_actions 'actions' is protected with RCU. */
552 struct dp_netdev_actions
{
553 /* These members are immutable: they do not change during the struct's
555 unsigned int size
; /* Size of 'actions', in bytes. */
556 struct nlattr actions
[]; /* Sequence of OVS_ACTION_ATTR_* attributes. */
559 struct dp_netdev_actions
*dp_netdev_actions_create(const struct nlattr
*,
561 struct dp_netdev_actions
*dp_netdev_flow_get_actions(
562 const struct dp_netdev_flow
*);
563 static void dp_netdev_actions_free(struct dp_netdev_actions
*);
565 struct polled_queue
{
566 struct dp_netdev_rxq
*rxq
;
570 /* Contained by struct dp_netdev_pmd_thread's 'poll_list' member. */
572 struct dp_netdev_rxq
*rxq
;
573 struct hmap_node node
;
576 /* Contained by struct dp_netdev_pmd_thread's 'send_port_cache',
577 * 'tnl_port_cache' or 'tx_ports'. */
579 struct dp_netdev_port
*port
;
582 struct hmap_node node
;
583 long long flush_time
;
584 struct dp_packet_batch output_pkts
;
585 struct dp_netdev_rxq
*output_pkts_rxqs
[NETDEV_MAX_BURST
];
588 /* A set of properties for the current processing loop that is not directly
589 * associated with the pmd thread itself, but with the packets being
590 * processed or the short-term system configuration (for example, time).
591 * Contained by struct dp_netdev_pmd_thread's 'ctx' member. */
592 struct dp_netdev_pmd_thread_ctx
{
593 /* Latest measured time. See 'pmd_thread_ctx_time_update()'. */
595 /* RX queue from which last packet was received. */
596 struct dp_netdev_rxq
*last_rxq
;
599 /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
600 * the performance overhead of interrupt processing. Therefore netdev can
601 * not implement rx-wait for these devices. dpif-netdev needs to poll
602 * these device to check for recv buffer. pmd-thread does polling for
603 * devices assigned to itself.
605 * DPDK used PMD for accessing NIC.
607 * Note, instance with cpu core id NON_PMD_CORE_ID will be reserved for
608 * I/O of all non-pmd threads. There will be no actual thread created
611 * Each struct has its own flow cache and classifier per managed ingress port.
612 * For packets received on ingress port, a look up is done on corresponding PMD
613 * thread's flow cache and in case of a miss, lookup is performed in the
614 * corresponding classifier of port. Packets are executed with the found
615 * actions in either case.
617 struct dp_netdev_pmd_thread
{
618 struct dp_netdev
*dp
;
619 struct ovs_refcount ref_cnt
; /* Every reference must be refcount'ed. */
620 struct cmap_node node
; /* In 'dp->poll_threads'. */
622 pthread_cond_t cond
; /* For synchronizing pmd thread reload. */
623 struct ovs_mutex cond_mutex
; /* Mutex for condition variable. */
625 /* Per thread exact-match cache. Note, the instance for cpu core
626 * NON_PMD_CORE_ID can be accessed by multiple threads, and thusly
627 * need to be protected by 'non_pmd_mutex'. Every other instance
628 * will only be accessed by its own pmd thread. */
629 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
) struct dfc_cache flow_cache
;
631 /* Flow-Table and classifiers
633 * Writers of 'flow_table' must take the 'flow_mutex'. Corresponding
634 * changes to 'classifiers' must be made while still holding the
637 struct ovs_mutex flow_mutex
;
638 struct cmap flow_table OVS_GUARDED
; /* Flow table. */
640 /* One classifier per in_port polled by the pmd */
641 struct cmap classifiers
;
642 /* Periodically sort subtable vectors according to hit frequencies */
643 long long int next_optimization
;
644 /* End of the next time interval for which processing cycles
645 are stored for each polled rxq. */
646 long long int rxq_next_cycle_store
;
648 /* Last interval timestamp. */
649 uint64_t intrvl_tsc_prev
;
650 /* Last interval cycles. */
651 atomic_ullong intrvl_cycles
;
653 /* Current context of the PMD thread. */
654 struct dp_netdev_pmd_thread_ctx ctx
;
656 struct latch exit_latch
; /* For terminating the pmd thread. */
657 struct seq
*reload_seq
;
658 uint64_t last_reload_seq
;
659 atomic_bool reload
; /* Do we need to reload ports? */
661 unsigned core_id
; /* CPU core id of this pmd thread. */
662 int numa_id
; /* numa node id of this pmd thread. */
665 /* Queue id used by this pmd thread to send packets on all netdevs if
666 * XPS disabled for this netdev. All static_tx_qid's are unique and less
667 * than 'cmap_count(dp->poll_threads)'. */
668 uint32_t static_tx_qid
;
670 /* Number of filled output batches. */
671 int n_output_batches
;
673 struct ovs_mutex port_mutex
; /* Mutex for 'poll_list' and 'tx_ports'. */
674 /* List of rx queues to poll. */
675 struct hmap poll_list OVS_GUARDED
;
676 /* Map of 'tx_port's used for transmission. Written by the main thread,
677 * read by the pmd thread. */
678 struct hmap tx_ports OVS_GUARDED
;
680 /* These are thread-local copies of 'tx_ports'. One contains only tunnel
681 * ports (that support push_tunnel/pop_tunnel), the other contains ports
682 * with at least one txq (that support send). A port can be in both.
684 * There are two separate maps to make sure that we don't try to execute
685 * OUTPUT on a device which has 0 txqs or PUSH/POP on a non-tunnel device.
687 * The instances for cpu core NON_PMD_CORE_ID can be accessed by multiple
688 * threads, and thusly need to be protected by 'non_pmd_mutex'. Every
689 * other instance will only be accessed by its own pmd thread. */
690 struct hmap tnl_port_cache
;
691 struct hmap send_port_cache
;
693 /* Keep track of detailed PMD performance statistics. */
694 struct pmd_perf_stats perf_stats
;
696 /* Set to true if the pmd thread needs to be reloaded. */
700 /* Interface to netdev-based datapath. */
703 struct dp_netdev
*dp
;
704 uint64_t last_port_seq
;
707 static int get_port_by_number(struct dp_netdev
*dp
, odp_port_t port_no
,
708 struct dp_netdev_port
**portp
)
709 OVS_REQUIRES(dp
->port_mutex
);
710 static int get_port_by_name(struct dp_netdev
*dp
, const char *devname
,
711 struct dp_netdev_port
**portp
)
712 OVS_REQUIRES(dp
->port_mutex
);
713 static void dp_netdev_free(struct dp_netdev
*)
714 OVS_REQUIRES(dp_netdev_mutex
);
715 static int do_add_port(struct dp_netdev
*dp
, const char *devname
,
716 const char *type
, odp_port_t port_no
)
717 OVS_REQUIRES(dp
->port_mutex
);
718 static void do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*)
719 OVS_REQUIRES(dp
->port_mutex
);
720 static int dpif_netdev_open(const struct dpif_class
*, const char *name
,
721 bool create
, struct dpif
**);
722 static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
723 struct dp_packet_batch
*,
725 const struct flow
*flow
,
726 const struct nlattr
*actions
,
728 static void dp_netdev_input(struct dp_netdev_pmd_thread
*,
729 struct dp_packet_batch
*, odp_port_t port_no
);
730 static void dp_netdev_recirculate(struct dp_netdev_pmd_thread
*,
731 struct dp_packet_batch
*);
733 static void dp_netdev_disable_upcall(struct dp_netdev
*);
734 static void dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
);
735 static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
,
736 struct dp_netdev
*dp
, unsigned core_id
,
738 static void dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
);
739 static void dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
740 OVS_REQUIRES(dp
->port_mutex
);
742 static void *pmd_thread_main(void *);
743 static struct dp_netdev_pmd_thread
*dp_netdev_get_pmd(struct dp_netdev
*dp
,
745 static struct dp_netdev_pmd_thread
*
746 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
);
747 static void dp_netdev_del_pmd(struct dp_netdev
*dp
,
748 struct dp_netdev_pmd_thread
*pmd
);
749 static void dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
, bool non_pmd
);
750 static void dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
);
751 static void dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
752 struct dp_netdev_port
*port
)
753 OVS_REQUIRES(pmd
->port_mutex
);
754 static void dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
756 OVS_REQUIRES(pmd
->port_mutex
);
757 static void dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
758 struct dp_netdev_rxq
*rxq
)
759 OVS_REQUIRES(pmd
->port_mutex
);
760 static void dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
761 struct rxq_poll
*poll
)
762 OVS_REQUIRES(pmd
->port_mutex
);
764 dp_netdev_pmd_flush_output_packets(struct dp_netdev_pmd_thread
*pmd
,
767 static void reconfigure_datapath(struct dp_netdev
*dp
)
768 OVS_REQUIRES(dp
->port_mutex
);
769 static bool dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
);
770 static void dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
);
771 static void dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
);
772 static void pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
773 OVS_REQUIRES(pmd
->port_mutex
);
775 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread
*pmd
,
776 struct polled_queue
*poll_list
, int poll_cnt
);
778 dp_netdev_rxq_set_cycles(struct dp_netdev_rxq
*rx
,
779 enum rxq_cycles_counter_type type
,
780 unsigned long long cycles
);
782 dp_netdev_rxq_get_cycles(struct dp_netdev_rxq
*rx
,
783 enum rxq_cycles_counter_type type
);
785 dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq
*rx
,
786 unsigned long long cycles
);
788 dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq
*rx
, unsigned idx
);
790 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
792 static int dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
795 static inline bool emc_entry_alive(struct emc_entry
*ce
);
796 static void emc_clear_entry(struct emc_entry
*ce
);
797 static void smc_clear_entry(struct smc_bucket
*b
, int idx
);
799 static void dp_netdev_request_reconfigure(struct dp_netdev
*dp
);
801 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread
*pmd
);
802 static void queue_netdev_flow_del(struct dp_netdev_pmd_thread
*pmd
,
803 struct dp_netdev_flow
*flow
);
806 emc_cache_init(struct emc_cache
*flow_cache
)
810 flow_cache
->sweep_idx
= 0;
811 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
812 flow_cache
->entries
[i
].flow
= NULL
;
813 flow_cache
->entries
[i
].key
.hash
= 0;
814 flow_cache
->entries
[i
].key
.len
= sizeof(struct miniflow
);
815 flowmap_init(&flow_cache
->entries
[i
].key
.mf
.map
);
820 smc_cache_init(struct smc_cache
*smc_cache
)
823 for (i
= 0; i
< SMC_BUCKET_CNT
; i
++) {
824 for (j
= 0; j
< SMC_ENTRY_PER_BUCKET
; j
++) {
825 smc_cache
->buckets
[i
].flow_idx
[j
] = UINT16_MAX
;
831 dfc_cache_init(struct dfc_cache
*flow_cache
)
833 emc_cache_init(&flow_cache
->emc_cache
);
834 smc_cache_init(&flow_cache
->smc_cache
);
838 emc_cache_uninit(struct emc_cache
*flow_cache
)
842 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
843 emc_clear_entry(&flow_cache
->entries
[i
]);
848 smc_cache_uninit(struct smc_cache
*smc
)
852 for (i
= 0; i
< SMC_BUCKET_CNT
; i
++) {
853 for (j
= 0; j
< SMC_ENTRY_PER_BUCKET
; j
++) {
854 smc_clear_entry(&(smc
->buckets
[i
]), j
);
860 dfc_cache_uninit(struct dfc_cache
*flow_cache
)
862 smc_cache_uninit(&flow_cache
->smc_cache
);
863 emc_cache_uninit(&flow_cache
->emc_cache
);
866 /* Check and clear dead flow references slowly (one entry at each
869 emc_cache_slow_sweep(struct emc_cache
*flow_cache
)
871 struct emc_entry
*entry
= &flow_cache
->entries
[flow_cache
->sweep_idx
];
873 if (!emc_entry_alive(entry
)) {
874 emc_clear_entry(entry
);
876 flow_cache
->sweep_idx
= (flow_cache
->sweep_idx
+ 1) & EM_FLOW_HASH_MASK
;
879 /* Updates the time in PMD threads context and should be called in three cases:
881 * 1. PMD structure initialization:
882 * - dp_netdev_configure_pmd()
884 * 2. Before processing of the new packet batch:
885 * - dpif_netdev_execute()
886 * - dp_netdev_process_rxq_port()
888 * 3. At least once per polling iteration in main polling threads if no
889 * packets received on current iteration:
890 * - dpif_netdev_run()
891 * - pmd_thread_main()
893 * 'pmd->ctx.now' should be used without update in all other cases if possible.
896 pmd_thread_ctx_time_update(struct dp_netdev_pmd_thread
*pmd
)
898 pmd
->ctx
.now
= time_usec();
901 /* Returns true if 'dpif' is a netdev or dummy dpif, false otherwise. */
903 dpif_is_netdev(const struct dpif
*dpif
)
905 return dpif
->dpif_class
->open
== dpif_netdev_open
;
908 static struct dpif_netdev
*
909 dpif_netdev_cast(const struct dpif
*dpif
)
911 ovs_assert(dpif_is_netdev(dpif
));
912 return CONTAINER_OF(dpif
, struct dpif_netdev
, dpif
);
915 static struct dp_netdev
*
916 get_dp_netdev(const struct dpif
*dpif
)
918 return dpif_netdev_cast(dpif
)->dp
;
922 PMD_INFO_SHOW_STATS
, /* Show how cpu cycles are spent. */
923 PMD_INFO_CLEAR_STATS
, /* Set the cycles count to 0. */
924 PMD_INFO_SHOW_RXQ
, /* Show poll lists of pmd threads. */
925 PMD_INFO_PERF_SHOW
, /* Show pmd performance details. */
929 format_pmd_thread(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
931 ds_put_cstr(reply
, (pmd
->core_id
== NON_PMD_CORE_ID
)
932 ? "main thread" : "pmd thread");
933 if (pmd
->numa_id
!= OVS_NUMA_UNSPEC
) {
934 ds_put_format(reply
, " numa_id %d", pmd
->numa_id
);
936 if (pmd
->core_id
!= OVS_CORE_UNSPEC
&& pmd
->core_id
!= NON_PMD_CORE_ID
) {
937 ds_put_format(reply
, " core_id %u", pmd
->core_id
);
939 ds_put_cstr(reply
, ":\n");
943 pmd_info_show_stats(struct ds
*reply
,
944 struct dp_netdev_pmd_thread
*pmd
)
946 uint64_t stats
[PMD_N_STATS
];
947 uint64_t total_cycles
, total_packets
;
948 double passes_per_pkt
= 0;
949 double lookups_per_hit
= 0;
950 double packets_per_batch
= 0;
952 pmd_perf_read_counters(&pmd
->perf_stats
, stats
);
953 total_cycles
= stats
[PMD_CYCLES_ITER_IDLE
]
954 + stats
[PMD_CYCLES_ITER_BUSY
];
955 total_packets
= stats
[PMD_STAT_RECV
];
957 format_pmd_thread(reply
, pmd
);
959 if (total_packets
> 0) {
960 passes_per_pkt
= (total_packets
+ stats
[PMD_STAT_RECIRC
])
961 / (double) total_packets
;
963 if (stats
[PMD_STAT_MASKED_HIT
] > 0) {
964 lookups_per_hit
= stats
[PMD_STAT_MASKED_LOOKUP
]
965 / (double) stats
[PMD_STAT_MASKED_HIT
];
967 if (stats
[PMD_STAT_SENT_BATCHES
] > 0) {
968 packets_per_batch
= stats
[PMD_STAT_SENT_PKTS
]
969 / (double) stats
[PMD_STAT_SENT_BATCHES
];
973 " packets received: %"PRIu64
"\n"
974 " packet recirculations: %"PRIu64
"\n"
975 " avg. datapath passes per packet: %.02f\n"
976 " emc hits: %"PRIu64
"\n"
977 " smc hits: %"PRIu64
"\n"
978 " megaflow hits: %"PRIu64
"\n"
979 " avg. subtable lookups per megaflow hit: %.02f\n"
980 " miss with success upcall: %"PRIu64
"\n"
981 " miss with failed upcall: %"PRIu64
"\n"
982 " avg. packets per output batch: %.02f\n",
983 total_packets
, stats
[PMD_STAT_RECIRC
],
984 passes_per_pkt
, stats
[PMD_STAT_EXACT_HIT
],
985 stats
[PMD_STAT_SMC_HIT
],
986 stats
[PMD_STAT_MASKED_HIT
], lookups_per_hit
,
987 stats
[PMD_STAT_MISS
], stats
[PMD_STAT_LOST
],
990 if (total_cycles
== 0) {
995 " idle cycles: %"PRIu64
" (%.02f%%)\n"
996 " processing cycles: %"PRIu64
" (%.02f%%)\n",
997 stats
[PMD_CYCLES_ITER_IDLE
],
998 stats
[PMD_CYCLES_ITER_IDLE
] / (double) total_cycles
* 100,
999 stats
[PMD_CYCLES_ITER_BUSY
],
1000 stats
[PMD_CYCLES_ITER_BUSY
] / (double) total_cycles
* 100);
1002 if (total_packets
== 0) {
1006 ds_put_format(reply
,
1007 " avg cycles per packet: %.02f (%"PRIu64
"/%"PRIu64
")\n",
1008 total_cycles
/ (double) total_packets
,
1009 total_cycles
, total_packets
);
1011 ds_put_format(reply
,
1012 " avg processing cycles per packet: "
1013 "%.02f (%"PRIu64
"/%"PRIu64
")\n",
1014 stats
[PMD_CYCLES_ITER_BUSY
] / (double) total_packets
,
1015 stats
[PMD_CYCLES_ITER_BUSY
], total_packets
);
1019 pmd_info_show_perf(struct ds
*reply
,
1020 struct dp_netdev_pmd_thread
*pmd
,
1021 struct pmd_perf_params
*par
)
1023 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
1025 xastrftime_msec("%H:%M:%S.###", time_wall_msec(), true);
1026 long long now
= time_msec();
1027 double duration
= (now
- pmd
->perf_stats
.start_ms
) / 1000.0;
1029 ds_put_cstr(reply
, "\n");
1030 ds_put_format(reply
, "Time: %s\n", time_str
);
1031 ds_put_format(reply
, "Measurement duration: %.3f s\n", duration
);
1032 ds_put_cstr(reply
, "\n");
1033 format_pmd_thread(reply
, pmd
);
1034 ds_put_cstr(reply
, "\n");
1035 pmd_perf_format_overall_stats(reply
, &pmd
->perf_stats
, duration
);
1036 if (pmd_perf_metrics_enabled(pmd
)) {
1037 /* Prevent parallel clearing of perf metrics. */
1038 ovs_mutex_lock(&pmd
->perf_stats
.clear_mutex
);
1039 if (par
->histograms
) {
1040 ds_put_cstr(reply
, "\n");
1041 pmd_perf_format_histograms(reply
, &pmd
->perf_stats
);
1043 if (par
->iter_hist_len
> 0) {
1044 ds_put_cstr(reply
, "\n");
1045 pmd_perf_format_iteration_history(reply
, &pmd
->perf_stats
,
1046 par
->iter_hist_len
);
1048 if (par
->ms_hist_len
> 0) {
1049 ds_put_cstr(reply
, "\n");
1050 pmd_perf_format_ms_history(reply
, &pmd
->perf_stats
,
1053 ovs_mutex_unlock(&pmd
->perf_stats
.clear_mutex
);
1060 compare_poll_list(const void *a_
, const void *b_
)
1062 const struct rxq_poll
*a
= a_
;
1063 const struct rxq_poll
*b
= b_
;
1065 const char *namea
= netdev_rxq_get_name(a
->rxq
->rx
);
1066 const char *nameb
= netdev_rxq_get_name(b
->rxq
->rx
);
1068 int cmp
= strcmp(namea
, nameb
);
1070 return netdev_rxq_get_queue_id(a
->rxq
->rx
)
1071 - netdev_rxq_get_queue_id(b
->rxq
->rx
);
1078 sorted_poll_list(struct dp_netdev_pmd_thread
*pmd
, struct rxq_poll
**list
,
1081 struct rxq_poll
*ret
, *poll
;
1084 *n
= hmap_count(&pmd
->poll_list
);
1088 ret
= xcalloc(*n
, sizeof *ret
);
1090 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
1094 ovs_assert(i
== *n
);
1095 qsort(ret
, *n
, sizeof *ret
, compare_poll_list
);
1102 pmd_info_show_rxq(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
1104 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
1105 struct rxq_poll
*list
;
1107 uint64_t total_cycles
= 0;
1109 ds_put_format(reply
,
1110 "pmd thread numa_id %d core_id %u:\n isolated : %s\n",
1111 pmd
->numa_id
, pmd
->core_id
, (pmd
->isolated
)
1112 ? "true" : "false");
1114 ovs_mutex_lock(&pmd
->port_mutex
);
1115 sorted_poll_list(pmd
, &list
, &n_rxq
);
1117 /* Get the total pmd cycles for an interval. */
1118 atomic_read_relaxed(&pmd
->intrvl_cycles
, &total_cycles
);
1119 /* Estimate the cycles to cover all intervals. */
1120 total_cycles
*= PMD_RXQ_INTERVAL_MAX
;
1122 for (int i
= 0; i
< n_rxq
; i
++) {
1123 struct dp_netdev_rxq
*rxq
= list
[i
].rxq
;
1124 const char *name
= netdev_rxq_get_name(rxq
->rx
);
1125 uint64_t proc_cycles
= 0;
1127 for (int j
= 0; j
< PMD_RXQ_INTERVAL_MAX
; j
++) {
1128 proc_cycles
+= dp_netdev_rxq_get_intrvl_cycles(rxq
, j
);
1130 ds_put_format(reply
, " port: %-16s queue-id: %2d", name
,
1131 netdev_rxq_get_queue_id(list
[i
].rxq
->rx
));
1132 ds_put_format(reply
, " pmd usage: ");
1134 ds_put_format(reply
, "%2"PRIu64
"",
1135 proc_cycles
* 100 / total_cycles
);
1136 ds_put_cstr(reply
, " %");
1138 ds_put_format(reply
, "%s", "NOT AVAIL");
1140 ds_put_cstr(reply
, "\n");
1142 ovs_mutex_unlock(&pmd
->port_mutex
);
1148 compare_poll_thread_list(const void *a_
, const void *b_
)
1150 const struct dp_netdev_pmd_thread
*a
, *b
;
1152 a
= *(struct dp_netdev_pmd_thread
**)a_
;
1153 b
= *(struct dp_netdev_pmd_thread
**)b_
;
1155 if (a
->core_id
< b
->core_id
) {
1158 if (a
->core_id
> b
->core_id
) {
1164 /* Create a sorted list of pmd's from the dp->poll_threads cmap. We can use
1165 * this list, as long as we do not go to quiescent state. */
1167 sorted_poll_thread_list(struct dp_netdev
*dp
,
1168 struct dp_netdev_pmd_thread
***list
,
1171 struct dp_netdev_pmd_thread
*pmd
;
1172 struct dp_netdev_pmd_thread
**pmd_list
;
1173 size_t k
= 0, n_pmds
;
1175 n_pmds
= cmap_count(&dp
->poll_threads
);
1176 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
1178 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1182 pmd_list
[k
++] = pmd
;
1185 qsort(pmd_list
, k
, sizeof *pmd_list
, compare_poll_thread_list
);
1192 dpif_netdev_pmd_rebalance(struct unixctl_conn
*conn
, int argc
,
1193 const char *argv
[], void *aux OVS_UNUSED
)
1195 struct ds reply
= DS_EMPTY_INITIALIZER
;
1196 struct dp_netdev
*dp
= NULL
;
1198 ovs_mutex_lock(&dp_netdev_mutex
);
1201 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
1202 } else if (shash_count(&dp_netdevs
) == 1) {
1203 /* There's only one datapath */
1204 dp
= shash_first(&dp_netdevs
)->data
;
1208 ovs_mutex_unlock(&dp_netdev_mutex
);
1209 unixctl_command_reply_error(conn
,
1210 "please specify an existing datapath");
1214 dp_netdev_request_reconfigure(dp
);
1215 ovs_mutex_unlock(&dp_netdev_mutex
);
1216 ds_put_cstr(&reply
, "pmd rxq rebalance requested.\n");
1217 unixctl_command_reply(conn
, ds_cstr(&reply
));
1222 dpif_netdev_pmd_info(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
1225 struct ds reply
= DS_EMPTY_INITIALIZER
;
1226 struct dp_netdev_pmd_thread
**pmd_list
;
1227 struct dp_netdev
*dp
= NULL
;
1228 enum pmd_info_type type
= *(enum pmd_info_type
*) aux
;
1229 unsigned int core_id
;
1230 bool filter_on_pmd
= false;
1233 ovs_mutex_lock(&dp_netdev_mutex
);
1236 if (!strcmp(argv
[1], "-pmd") && argc
> 2) {
1237 if (str_to_uint(argv
[2], 10, &core_id
)) {
1238 filter_on_pmd
= true;
1243 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
1250 if (shash_count(&dp_netdevs
) == 1) {
1251 /* There's only one datapath */
1252 dp
= shash_first(&dp_netdevs
)->data
;
1254 ovs_mutex_unlock(&dp_netdev_mutex
);
1255 unixctl_command_reply_error(conn
,
1256 "please specify an existing datapath");
1261 sorted_poll_thread_list(dp
, &pmd_list
, &n
);
1262 for (size_t i
= 0; i
< n
; i
++) {
1263 struct dp_netdev_pmd_thread
*pmd
= pmd_list
[i
];
1267 if (filter_on_pmd
&& pmd
->core_id
!= core_id
) {
1270 if (type
== PMD_INFO_SHOW_RXQ
) {
1271 pmd_info_show_rxq(&reply
, pmd
);
1272 } else if (type
== PMD_INFO_CLEAR_STATS
) {
1273 pmd_perf_stats_clear(&pmd
->perf_stats
);
1274 } else if (type
== PMD_INFO_SHOW_STATS
) {
1275 pmd_info_show_stats(&reply
, pmd
);
1276 } else if (type
== PMD_INFO_PERF_SHOW
) {
1277 pmd_info_show_perf(&reply
, pmd
, (struct pmd_perf_params
*)aux
);
1282 ovs_mutex_unlock(&dp_netdev_mutex
);
1284 unixctl_command_reply(conn
, ds_cstr(&reply
));
1289 pmd_perf_show_cmd(struct unixctl_conn
*conn
, int argc
,
1291 void *aux OVS_UNUSED
)
1293 struct pmd_perf_params par
;
1294 long int it_hist
= 0, ms_hist
= 0;
1295 par
.histograms
= true;
1298 if (!strcmp(argv
[1], "-nh")) {
1299 par
.histograms
= false;
1302 } else if (!strcmp(argv
[1], "-it") && argc
> 2) {
1303 it_hist
= strtol(argv
[2], NULL
, 10);
1306 } else if (it_hist
> HISTORY_LEN
) {
1307 it_hist
= HISTORY_LEN
;
1311 } else if (!strcmp(argv
[1], "-ms") && argc
> 2) {
1312 ms_hist
= strtol(argv
[2], NULL
, 10);
1315 } else if (ms_hist
> HISTORY_LEN
) {
1316 ms_hist
= HISTORY_LEN
;
1324 par
.iter_hist_len
= it_hist
;
1325 par
.ms_hist_len
= ms_hist
;
1326 par
.command_type
= PMD_INFO_PERF_SHOW
;
1327 dpif_netdev_pmd_info(conn
, argc
, argv
, &par
);
1331 dpif_netdev_init(void)
1333 static enum pmd_info_type show_aux
= PMD_INFO_SHOW_STATS
,
1334 clear_aux
= PMD_INFO_CLEAR_STATS
,
1335 poll_aux
= PMD_INFO_SHOW_RXQ
;
1337 unixctl_command_register("dpif-netdev/pmd-stats-show", "[-pmd core] [dp]",
1338 0, 3, dpif_netdev_pmd_info
,
1340 unixctl_command_register("dpif-netdev/pmd-stats-clear", "[-pmd core] [dp]",
1341 0, 3, dpif_netdev_pmd_info
,
1342 (void *)&clear_aux
);
1343 unixctl_command_register("dpif-netdev/pmd-rxq-show", "[-pmd core] [dp]",
1344 0, 3, dpif_netdev_pmd_info
,
1346 unixctl_command_register("dpif-netdev/pmd-perf-show",
1347 "[-nh] [-it iter-history-len]"
1348 " [-ms ms-history-len]"
1349 " [-pmd core] [dp]",
1350 0, 8, pmd_perf_show_cmd
,
1352 unixctl_command_register("dpif-netdev/pmd-rxq-rebalance", "[dp]",
1353 0, 1, dpif_netdev_pmd_rebalance
,
1355 unixctl_command_register("dpif-netdev/pmd-perf-log-set",
1356 "on|off [-b before] [-a after] [-e|-ne] "
1357 "[-us usec] [-q qlen]",
1358 0, 10, pmd_perf_log_set_cmd
,
1364 dpif_netdev_enumerate(struct sset
*all_dps
,
1365 const struct dpif_class
*dpif_class
)
1367 struct shash_node
*node
;
1369 ovs_mutex_lock(&dp_netdev_mutex
);
1370 SHASH_FOR_EACH(node
, &dp_netdevs
) {
1371 struct dp_netdev
*dp
= node
->data
;
1372 if (dpif_class
!= dp
->class) {
1373 /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs.
1374 * If the class doesn't match, skip this dpif. */
1377 sset_add(all_dps
, node
->name
);
1379 ovs_mutex_unlock(&dp_netdev_mutex
);
1385 dpif_netdev_class_is_dummy(const struct dpif_class
*class)
1387 return class != &dpif_netdev_class
;
1391 dpif_netdev_port_open_type(const struct dpif_class
*class, const char *type
)
1393 return strcmp(type
, "internal") ? type
1394 : dpif_netdev_class_is_dummy(class) ? "dummy-internal"
1398 static struct dpif
*
1399 create_dpif_netdev(struct dp_netdev
*dp
)
1401 uint16_t netflow_id
= hash_string(dp
->name
, 0);
1402 struct dpif_netdev
*dpif
;
1404 ovs_refcount_ref(&dp
->ref_cnt
);
1406 dpif
= xmalloc(sizeof *dpif
);
1407 dpif_init(&dpif
->dpif
, dp
->class, dp
->name
, netflow_id
>> 8, netflow_id
);
1409 dpif
->last_port_seq
= seq_read(dp
->port_seq
);
1414 /* Choose an unused, non-zero port number and return it on success.
1415 * Return ODPP_NONE on failure. */
1417 choose_port(struct dp_netdev
*dp
, const char *name
)
1418 OVS_REQUIRES(dp
->port_mutex
)
1422 if (dp
->class != &dpif_netdev_class
) {
1426 /* If the port name begins with "br", start the number search at
1427 * 100 to make writing tests easier. */
1428 if (!strncmp(name
, "br", 2)) {
1432 /* If the port name contains a number, try to assign that port number.
1433 * This can make writing unit tests easier because port numbers are
1435 for (p
= name
; *p
!= '\0'; p
++) {
1436 if (isdigit((unsigned char) *p
)) {
1437 port_no
= start_no
+ strtol(p
, NULL
, 10);
1438 if (port_no
> 0 && port_no
!= odp_to_u32(ODPP_NONE
)
1439 && !dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
1440 return u32_to_odp(port_no
);
1447 for (port_no
= 1; port_no
<= UINT16_MAX
; port_no
++) {
1448 if (!dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
1449 return u32_to_odp(port_no
);
1457 create_dp_netdev(const char *name
, const struct dpif_class
*class,
1458 struct dp_netdev
**dpp
)
1459 OVS_REQUIRES(dp_netdev_mutex
)
1461 struct dp_netdev
*dp
;
1464 dp
= xzalloc(sizeof *dp
);
1465 shash_add(&dp_netdevs
, name
, dp
);
1467 *CONST_CAST(const struct dpif_class
**, &dp
->class) = class;
1468 *CONST_CAST(const char **, &dp
->name
) = xstrdup(name
);
1469 ovs_refcount_init(&dp
->ref_cnt
);
1470 atomic_flag_clear(&dp
->destroyed
);
1472 ovs_mutex_init(&dp
->port_mutex
);
1473 hmap_init(&dp
->ports
);
1474 dp
->port_seq
= seq_create();
1475 fat_rwlock_init(&dp
->upcall_rwlock
);
1477 dp
->reconfigure_seq
= seq_create();
1478 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
1480 for (int i
= 0; i
< N_METER_LOCKS
; ++i
) {
1481 ovs_mutex_init_adaptive(&dp
->meter_locks
[i
]);
1484 /* Disable upcalls by default. */
1485 dp_netdev_disable_upcall(dp
);
1486 dp
->upcall_aux
= NULL
;
1487 dp
->upcall_cb
= NULL
;
1489 conntrack_init(&dp
->conntrack
);
1491 atomic_init(&dp
->emc_insert_min
, DEFAULT_EM_FLOW_INSERT_MIN
);
1492 atomic_init(&dp
->tx_flush_interval
, DEFAULT_TX_FLUSH_INTERVAL
);
1494 cmap_init(&dp
->poll_threads
);
1496 ovs_mutex_init(&dp
->tx_qid_pool_mutex
);
1497 /* We need 1 Tx queue for each possible core + 1 for non-PMD threads. */
1498 dp
->tx_qid_pool
= id_pool_create(0, ovs_numa_get_n_cores() + 1);
1500 ovs_mutex_init_recursive(&dp
->non_pmd_mutex
);
1501 ovsthread_key_create(&dp
->per_pmd_key
, NULL
);
1503 ovs_mutex_lock(&dp
->port_mutex
);
1504 /* non-PMD will be created before all other threads and will
1505 * allocate static_tx_qid = 0. */
1506 dp_netdev_set_nonpmd(dp
);
1508 error
= do_add_port(dp
, name
, dpif_netdev_port_open_type(dp
->class,
1511 ovs_mutex_unlock(&dp
->port_mutex
);
1517 dp
->last_tnl_conf_seq
= seq_read(tnl_conf_seq
);
1523 dp_netdev_request_reconfigure(struct dp_netdev
*dp
)
1525 seq_change(dp
->reconfigure_seq
);
1529 dp_netdev_is_reconf_required(struct dp_netdev
*dp
)
1531 return seq_read(dp
->reconfigure_seq
) != dp
->last_reconfigure_seq
;
1535 dpif_netdev_open(const struct dpif_class
*class, const char *name
,
1536 bool create
, struct dpif
**dpifp
)
1538 struct dp_netdev
*dp
;
1541 ovs_mutex_lock(&dp_netdev_mutex
);
1542 dp
= shash_find_data(&dp_netdevs
, name
);
1544 error
= create
? create_dp_netdev(name
, class, &dp
) : ENODEV
;
1546 error
= (dp
->class != class ? EINVAL
1551 *dpifp
= create_dpif_netdev(dp
);
1554 ovs_mutex_unlock(&dp_netdev_mutex
);
1560 dp_netdev_destroy_upcall_lock(struct dp_netdev
*dp
)
1561 OVS_NO_THREAD_SAFETY_ANALYSIS
1563 /* Check that upcalls are disabled, i.e. that the rwlock is taken */
1564 ovs_assert(fat_rwlock_tryrdlock(&dp
->upcall_rwlock
));
1566 /* Before freeing a lock we should release it */
1567 fat_rwlock_unlock(&dp
->upcall_rwlock
);
1568 fat_rwlock_destroy(&dp
->upcall_rwlock
);
1572 dp_delete_meter(struct dp_netdev
*dp
, uint32_t meter_id
)
1573 OVS_REQUIRES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
1575 if (dp
->meters
[meter_id
]) {
1576 free(dp
->meters
[meter_id
]);
1577 dp
->meters
[meter_id
] = NULL
;
1581 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
1582 * through the 'dp_netdevs' shash while freeing 'dp'. */
1584 dp_netdev_free(struct dp_netdev
*dp
)
1585 OVS_REQUIRES(dp_netdev_mutex
)
1587 struct dp_netdev_port
*port
, *next
;
1589 shash_find_and_delete(&dp_netdevs
, dp
->name
);
1591 ovs_mutex_lock(&dp
->port_mutex
);
1592 HMAP_FOR_EACH_SAFE (port
, next
, node
, &dp
->ports
) {
1593 do_del_port(dp
, port
);
1595 ovs_mutex_unlock(&dp
->port_mutex
);
1597 dp_netdev_destroy_all_pmds(dp
, true);
1598 cmap_destroy(&dp
->poll_threads
);
1600 ovs_mutex_destroy(&dp
->tx_qid_pool_mutex
);
1601 id_pool_destroy(dp
->tx_qid_pool
);
1603 ovs_mutex_destroy(&dp
->non_pmd_mutex
);
1604 ovsthread_key_delete(dp
->per_pmd_key
);
1606 conntrack_destroy(&dp
->conntrack
);
1609 seq_destroy(dp
->reconfigure_seq
);
1611 seq_destroy(dp
->port_seq
);
1612 hmap_destroy(&dp
->ports
);
1613 ovs_mutex_destroy(&dp
->port_mutex
);
1615 /* Upcalls must be disabled at this point */
1616 dp_netdev_destroy_upcall_lock(dp
);
1620 for (i
= 0; i
< MAX_METERS
; ++i
) {
1622 dp_delete_meter(dp
, i
);
1623 meter_unlock(dp
, i
);
1625 for (i
= 0; i
< N_METER_LOCKS
; ++i
) {
1626 ovs_mutex_destroy(&dp
->meter_locks
[i
]);
1629 free(dp
->pmd_cmask
);
1630 free(CONST_CAST(char *, dp
->name
));
1635 dp_netdev_unref(struct dp_netdev
*dp
)
1638 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
1639 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
1640 ovs_mutex_lock(&dp_netdev_mutex
);
1641 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1644 ovs_mutex_unlock(&dp_netdev_mutex
);
1649 dpif_netdev_close(struct dpif
*dpif
)
1651 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1653 dp_netdev_unref(dp
);
1658 dpif_netdev_destroy(struct dpif
*dpif
)
1660 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1662 if (!atomic_flag_test_and_set(&dp
->destroyed
)) {
1663 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1664 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
1672 /* Add 'n' to the atomic variable 'var' non-atomically and using relaxed
1673 * load/store semantics. While the increment is not atomic, the load and
1674 * store operations are, making it impossible to read inconsistent values.
1676 * This is used to update thread local stats counters. */
1678 non_atomic_ullong_add(atomic_ullong
*var
, unsigned long long n
)
1680 unsigned long long tmp
;
1682 atomic_read_relaxed(var
, &tmp
);
1684 atomic_store_relaxed(var
, tmp
);
1688 dpif_netdev_get_stats(const struct dpif
*dpif
, struct dpif_dp_stats
*stats
)
1690 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1691 struct dp_netdev_pmd_thread
*pmd
;
1692 uint64_t pmd_stats
[PMD_N_STATS
];
1694 stats
->n_flows
= stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
1695 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1696 stats
->n_flows
+= cmap_count(&pmd
->flow_table
);
1697 pmd_perf_read_counters(&pmd
->perf_stats
, pmd_stats
);
1698 stats
->n_hit
+= pmd_stats
[PMD_STAT_EXACT_HIT
];
1699 stats
->n_hit
+= pmd_stats
[PMD_STAT_SMC_HIT
];
1700 stats
->n_hit
+= pmd_stats
[PMD_STAT_MASKED_HIT
];
1701 stats
->n_missed
+= pmd_stats
[PMD_STAT_MISS
];
1702 stats
->n_lost
+= pmd_stats
[PMD_STAT_LOST
];
1704 stats
->n_masks
= UINT32_MAX
;
1705 stats
->n_mask_hit
= UINT64_MAX
;
1711 dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread
*pmd
)
1713 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
1714 ovs_mutex_lock(&pmd
->dp
->non_pmd_mutex
);
1715 ovs_mutex_lock(&pmd
->port_mutex
);
1716 pmd_load_cached_ports(pmd
);
1717 ovs_mutex_unlock(&pmd
->port_mutex
);
1718 ovs_mutex_unlock(&pmd
->dp
->non_pmd_mutex
);
1722 ovs_mutex_lock(&pmd
->cond_mutex
);
1723 seq_change(pmd
->reload_seq
);
1724 atomic_store_relaxed(&pmd
->reload
, true);
1725 ovs_mutex_cond_wait(&pmd
->cond
, &pmd
->cond_mutex
);
1726 ovs_mutex_unlock(&pmd
->cond_mutex
);
1730 hash_port_no(odp_port_t port_no
)
1732 return hash_int(odp_to_u32(port_no
), 0);
1736 port_create(const char *devname
, const char *type
,
1737 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1739 struct netdev_saved_flags
*sf
;
1740 struct dp_netdev_port
*port
;
1741 enum netdev_flags flags
;
1742 struct netdev
*netdev
;
1747 /* Open and validate network device. */
1748 error
= netdev_open(devname
, type
, &netdev
);
1752 /* XXX reject non-Ethernet devices */
1754 netdev_get_flags(netdev
, &flags
);
1755 if (flags
& NETDEV_LOOPBACK
) {
1756 VLOG_ERR("%s: cannot add a loopback device", devname
);
1761 error
= netdev_turn_flags_on(netdev
, NETDEV_PROMISC
, &sf
);
1763 VLOG_ERR("%s: cannot set promisc flag", devname
);
1767 port
= xzalloc(sizeof *port
);
1768 port
->port_no
= port_no
;
1769 port
->netdev
= netdev
;
1770 port
->type
= xstrdup(type
);
1772 port
->need_reconfigure
= true;
1773 ovs_mutex_init(&port
->txq_used_mutex
);
1780 netdev_close(netdev
);
1785 do_add_port(struct dp_netdev
*dp
, const char *devname
, const char *type
,
1787 OVS_REQUIRES(dp
->port_mutex
)
1789 struct dp_netdev_port
*port
;
1792 /* Reject devices already in 'dp'. */
1793 if (!get_port_by_name(dp
, devname
, &port
)) {
1797 error
= port_create(devname
, type
, port_no
, &port
);
1802 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
1803 seq_change(dp
->port_seq
);
1805 reconfigure_datapath(dp
);
1811 dpif_netdev_port_add(struct dpif
*dpif
, struct netdev
*netdev
,
1812 odp_port_t
*port_nop
)
1814 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1815 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1816 const char *dpif_port
;
1820 ovs_mutex_lock(&dp
->port_mutex
);
1821 dpif_port
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1822 if (*port_nop
!= ODPP_NONE
) {
1823 port_no
= *port_nop
;
1824 error
= dp_netdev_lookup_port(dp
, *port_nop
) ? EBUSY
: 0;
1826 port_no
= choose_port(dp
, dpif_port
);
1827 error
= port_no
== ODPP_NONE
? EFBIG
: 0;
1830 *port_nop
= port_no
;
1831 error
= do_add_port(dp
, dpif_port
, netdev_get_type(netdev
), port_no
);
1833 ovs_mutex_unlock(&dp
->port_mutex
);
1839 dpif_netdev_port_del(struct dpif
*dpif
, odp_port_t port_no
)
1841 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1844 ovs_mutex_lock(&dp
->port_mutex
);
1845 if (port_no
== ODPP_LOCAL
) {
1848 struct dp_netdev_port
*port
;
1850 error
= get_port_by_number(dp
, port_no
, &port
);
1852 do_del_port(dp
, port
);
1855 ovs_mutex_unlock(&dp
->port_mutex
);
1861 is_valid_port_number(odp_port_t port_no
)
1863 return port_no
!= ODPP_NONE
;
1866 static struct dp_netdev_port
*
1867 dp_netdev_lookup_port(const struct dp_netdev
*dp
, odp_port_t port_no
)
1868 OVS_REQUIRES(dp
->port_mutex
)
1870 struct dp_netdev_port
*port
;
1872 HMAP_FOR_EACH_WITH_HASH (port
, node
, hash_port_no(port_no
), &dp
->ports
) {
1873 if (port
->port_no
== port_no
) {
1881 get_port_by_number(struct dp_netdev
*dp
,
1882 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1883 OVS_REQUIRES(dp
->port_mutex
)
1885 if (!is_valid_port_number(port_no
)) {
1889 *portp
= dp_netdev_lookup_port(dp
, port_no
);
1890 return *portp
? 0 : ENODEV
;
1895 port_destroy(struct dp_netdev_port
*port
)
1901 netdev_close(port
->netdev
);
1902 netdev_restore_flags(port
->sf
);
1904 for (unsigned i
= 0; i
< port
->n_rxq
; i
++) {
1905 netdev_rxq_close(port
->rxqs
[i
].rx
);
1907 ovs_mutex_destroy(&port
->txq_used_mutex
);
1908 free(port
->rxq_affinity_list
);
1909 free(port
->txq_used
);
1916 get_port_by_name(struct dp_netdev
*dp
,
1917 const char *devname
, struct dp_netdev_port
**portp
)
1918 OVS_REQUIRES(dp
->port_mutex
)
1920 struct dp_netdev_port
*port
;
1922 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1923 if (!strcmp(netdev_get_name(port
->netdev
), devname
)) {
1929 /* Callers of dpif_netdev_port_query_by_name() expect ENODEV for a non
1934 /* Returns 'true' if there is a port with pmd netdev. */
1936 has_pmd_port(struct dp_netdev
*dp
)
1937 OVS_REQUIRES(dp
->port_mutex
)
1939 struct dp_netdev_port
*port
;
1941 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1942 if (netdev_is_pmd(port
->netdev
)) {
1951 do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*port
)
1952 OVS_REQUIRES(dp
->port_mutex
)
1954 hmap_remove(&dp
->ports
, &port
->node
);
1955 seq_change(dp
->port_seq
);
1957 reconfigure_datapath(dp
);
1963 answer_port_query(const struct dp_netdev_port
*port
,
1964 struct dpif_port
*dpif_port
)
1966 dpif_port
->name
= xstrdup(netdev_get_name(port
->netdev
));
1967 dpif_port
->type
= xstrdup(port
->type
);
1968 dpif_port
->port_no
= port
->port_no
;
1972 dpif_netdev_port_query_by_number(const struct dpif
*dpif
, odp_port_t port_no
,
1973 struct dpif_port
*dpif_port
)
1975 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1976 struct dp_netdev_port
*port
;
1979 ovs_mutex_lock(&dp
->port_mutex
);
1980 error
= get_port_by_number(dp
, port_no
, &port
);
1981 if (!error
&& dpif_port
) {
1982 answer_port_query(port
, dpif_port
);
1984 ovs_mutex_unlock(&dp
->port_mutex
);
1990 dpif_netdev_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
1991 struct dpif_port
*dpif_port
)
1993 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1994 struct dp_netdev_port
*port
;
1997 ovs_mutex_lock(&dp
->port_mutex
);
1998 error
= get_port_by_name(dp
, devname
, &port
);
1999 if (!error
&& dpif_port
) {
2000 answer_port_query(port
, dpif_port
);
2002 ovs_mutex_unlock(&dp
->port_mutex
);
2008 dp_netdev_flow_free(struct dp_netdev_flow
*flow
)
2010 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow
));
2014 static void dp_netdev_flow_unref(struct dp_netdev_flow
*flow
)
2016 if (ovs_refcount_unref_relaxed(&flow
->ref_cnt
) == 1) {
2017 ovsrcu_postpone(dp_netdev_flow_free
, flow
);
2022 dp_netdev_flow_hash(const ovs_u128
*ufid
)
2024 return ufid
->u32
[0];
2027 static inline struct dpcls
*
2028 dp_netdev_pmd_lookup_dpcls(struct dp_netdev_pmd_thread
*pmd
,
2032 uint32_t hash
= hash_port_no(in_port
);
2033 CMAP_FOR_EACH_WITH_HASH (cls
, node
, hash
, &pmd
->classifiers
) {
2034 if (cls
->in_port
== in_port
) {
2035 /* Port classifier exists already */
2042 static inline struct dpcls
*
2043 dp_netdev_pmd_find_dpcls(struct dp_netdev_pmd_thread
*pmd
,
2045 OVS_REQUIRES(pmd
->flow_mutex
)
2047 struct dpcls
*cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
2048 uint32_t hash
= hash_port_no(in_port
);
2051 /* Create new classifier for in_port */
2052 cls
= xmalloc(sizeof(*cls
));
2054 cls
->in_port
= in_port
;
2055 cmap_insert(&pmd
->classifiers
, &cls
->node
, hash
);
2056 VLOG_DBG("Creating dpcls %p for in_port %d", cls
, in_port
);
2061 #define MAX_FLOW_MARK (UINT32_MAX - 1)
2062 #define INVALID_FLOW_MARK (UINT32_MAX)
2064 struct megaflow_to_mark_data
{
2065 const struct cmap_node node
;
2071 struct cmap megaflow_to_mark
;
2072 struct cmap mark_to_flow
;
2073 struct id_pool
*pool
;
2076 static struct flow_mark flow_mark
= {
2077 .megaflow_to_mark
= CMAP_INITIALIZER
,
2078 .mark_to_flow
= CMAP_INITIALIZER
,
2082 flow_mark_alloc(void)
2086 if (!flow_mark
.pool
) {
2087 /* Haven't initiated yet, do it here */
2088 flow_mark
.pool
= id_pool_create(0, MAX_FLOW_MARK
);
2091 if (id_pool_alloc_id(flow_mark
.pool
, &mark
)) {
2095 return INVALID_FLOW_MARK
;
2099 flow_mark_free(uint32_t mark
)
2101 id_pool_free_id(flow_mark
.pool
, mark
);
2104 /* associate megaflow with a mark, which is a 1:1 mapping */
2106 megaflow_to_mark_associate(const ovs_u128
*mega_ufid
, uint32_t mark
)
2108 size_t hash
= dp_netdev_flow_hash(mega_ufid
);
2109 struct megaflow_to_mark_data
*data
= xzalloc(sizeof(*data
));
2111 data
->mega_ufid
= *mega_ufid
;
2114 cmap_insert(&flow_mark
.megaflow_to_mark
,
2115 CONST_CAST(struct cmap_node
*, &data
->node
), hash
);
2118 /* disassociate meagaflow with a mark */
2120 megaflow_to_mark_disassociate(const ovs_u128
*mega_ufid
)
2122 size_t hash
= dp_netdev_flow_hash(mega_ufid
);
2123 struct megaflow_to_mark_data
*data
;
2125 CMAP_FOR_EACH_WITH_HASH (data
, node
, hash
, &flow_mark
.megaflow_to_mark
) {
2126 if (ovs_u128_equals(*mega_ufid
, data
->mega_ufid
)) {
2127 cmap_remove(&flow_mark
.megaflow_to_mark
,
2128 CONST_CAST(struct cmap_node
*, &data
->node
), hash
);
2134 VLOG_WARN("Masked ufid "UUID_FMT
" is not associated with a mark?\n",
2135 UUID_ARGS((struct uuid
*)mega_ufid
));
2138 static inline uint32_t
2139 megaflow_to_mark_find(const ovs_u128
*mega_ufid
)
2141 size_t hash
= dp_netdev_flow_hash(mega_ufid
);
2142 struct megaflow_to_mark_data
*data
;
2144 CMAP_FOR_EACH_WITH_HASH (data
, node
, hash
, &flow_mark
.megaflow_to_mark
) {
2145 if (ovs_u128_equals(*mega_ufid
, data
->mega_ufid
)) {
2150 VLOG_WARN("Mark id for ufid "UUID_FMT
" was not found\n",
2151 UUID_ARGS((struct uuid
*)mega_ufid
));
2152 return INVALID_FLOW_MARK
;
2155 /* associate mark with a flow, which is 1:N mapping */
2157 mark_to_flow_associate(const uint32_t mark
, struct dp_netdev_flow
*flow
)
2159 dp_netdev_flow_ref(flow
);
2161 cmap_insert(&flow_mark
.mark_to_flow
,
2162 CONST_CAST(struct cmap_node
*, &flow
->mark_node
),
2166 VLOG_DBG("Associated dp_netdev flow %p with mark %u\n", flow
, mark
);
2170 flow_mark_has_no_ref(uint32_t mark
)
2172 struct dp_netdev_flow
*flow
;
2174 CMAP_FOR_EACH_WITH_HASH (flow
, mark_node
, hash_int(mark
, 0),
2175 &flow_mark
.mark_to_flow
) {
2176 if (flow
->mark
== mark
) {
2185 mark_to_flow_disassociate(struct dp_netdev_pmd_thread
*pmd
,
2186 struct dp_netdev_flow
*flow
)
2189 uint32_t mark
= flow
->mark
;
2190 struct cmap_node
*mark_node
= CONST_CAST(struct cmap_node
*,
2193 cmap_remove(&flow_mark
.mark_to_flow
, mark_node
, hash_int(mark
, 0));
2194 flow
->mark
= INVALID_FLOW_MARK
;
2197 * no flow is referencing the mark any more? If so, let's
2198 * remove the flow from hardware and free the mark.
2200 if (flow_mark_has_no_ref(mark
)) {
2201 struct dp_netdev_port
*port
;
2202 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
2204 ovs_mutex_lock(&pmd
->dp
->port_mutex
);
2205 port
= dp_netdev_lookup_port(pmd
->dp
, in_port
);
2207 ret
= netdev_flow_del(port
->netdev
, &flow
->mega_ufid
, NULL
);
2209 ovs_mutex_unlock(&pmd
->dp
->port_mutex
);
2211 flow_mark_free(mark
);
2212 VLOG_DBG("Freed flow mark %u\n", mark
);
2214 megaflow_to_mark_disassociate(&flow
->mega_ufid
);
2216 dp_netdev_flow_unref(flow
);
2222 flow_mark_flush(struct dp_netdev_pmd_thread
*pmd
)
2224 struct dp_netdev_flow
*flow
;
2226 CMAP_FOR_EACH (flow
, mark_node
, &flow_mark
.mark_to_flow
) {
2227 if (flow
->pmd_id
== pmd
->core_id
) {
2228 queue_netdev_flow_del(pmd
, flow
);
2233 static struct dp_netdev_flow
*
2234 mark_to_flow_find(const struct dp_netdev_pmd_thread
*pmd
,
2235 const uint32_t mark
)
2237 struct dp_netdev_flow
*flow
;
2239 CMAP_FOR_EACH_WITH_HASH (flow
, mark_node
, hash_int(mark
, 0),
2240 &flow_mark
.mark_to_flow
) {
2241 if (flow
->mark
== mark
&& flow
->pmd_id
== pmd
->core_id
&&
2242 flow
->dead
== false) {
2250 static struct dp_flow_offload_item
*
2251 dp_netdev_alloc_flow_offload(struct dp_netdev_pmd_thread
*pmd
,
2252 struct dp_netdev_flow
*flow
,
2255 struct dp_flow_offload_item
*offload
;
2257 offload
= xzalloc(sizeof(*offload
));
2259 offload
->flow
= flow
;
2262 dp_netdev_flow_ref(flow
);
2263 dp_netdev_pmd_try_ref(pmd
);
2269 dp_netdev_free_flow_offload(struct dp_flow_offload_item
*offload
)
2271 dp_netdev_pmd_unref(offload
->pmd
);
2272 dp_netdev_flow_unref(offload
->flow
);
2274 free(offload
->actions
);
2279 dp_netdev_append_flow_offload(struct dp_flow_offload_item
*offload
)
2281 ovs_mutex_lock(&dp_flow_offload
.mutex
);
2282 ovs_list_push_back(&dp_flow_offload
.list
, &offload
->node
);
2283 xpthread_cond_signal(&dp_flow_offload
.cond
);
2284 ovs_mutex_unlock(&dp_flow_offload
.mutex
);
2288 dp_netdev_flow_offload_del(struct dp_flow_offload_item
*offload
)
2290 return mark_to_flow_disassociate(offload
->pmd
, offload
->flow
);
2294 * There are two flow offload operations here: addition and modification.
2296 * For flow addition, this function does:
2297 * - allocate a new flow mark id
2298 * - perform hardware flow offload
2299 * - associate the flow mark with flow and mega flow
2301 * For flow modification, both flow mark and the associations are still
2302 * valid, thus only item 2 needed.
2305 dp_netdev_flow_offload_put(struct dp_flow_offload_item
*offload
)
2307 struct dp_netdev_port
*port
;
2308 struct dp_netdev_pmd_thread
*pmd
= offload
->pmd
;
2309 struct dp_netdev_flow
*flow
= offload
->flow
;
2310 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
2311 bool modification
= offload
->op
== DP_NETDEV_FLOW_OFFLOAD_OP_MOD
;
2312 struct offload_info info
;
2322 ovs_assert(mark
!= INVALID_FLOW_MARK
);
2325 * If a mega flow has already been offloaded (from other PMD
2326 * instances), do not offload it again.
2328 mark
= megaflow_to_mark_find(&flow
->mega_ufid
);
2329 if (mark
!= INVALID_FLOW_MARK
) {
2330 VLOG_DBG("Flow has already been offloaded with mark %u\n", mark
);
2331 if (flow
->mark
!= INVALID_FLOW_MARK
) {
2332 ovs_assert(flow
->mark
== mark
);
2334 mark_to_flow_associate(mark
, flow
);
2339 mark
= flow_mark_alloc();
2340 if (mark
== INVALID_FLOW_MARK
) {
2341 VLOG_ERR("Failed to allocate flow mark!\n");
2344 info
.flow_mark
= mark
;
2346 ovs_mutex_lock(&pmd
->dp
->port_mutex
);
2347 port
= dp_netdev_lookup_port(pmd
->dp
, in_port
);
2349 ovs_mutex_unlock(&pmd
->dp
->port_mutex
);
2352 ret
= netdev_flow_put(port
->netdev
, &offload
->match
,
2353 CONST_CAST(struct nlattr
*, offload
->actions
),
2354 offload
->actions_len
, &flow
->mega_ufid
, &info
,
2356 ovs_mutex_unlock(&pmd
->dp
->port_mutex
);
2359 if (!modification
) {
2360 flow_mark_free(mark
);
2362 mark_to_flow_disassociate(pmd
, flow
);
2367 if (!modification
) {
2368 megaflow_to_mark_associate(&flow
->mega_ufid
, mark
);
2369 mark_to_flow_associate(mark
, flow
);
2376 dp_netdev_flow_offload_main(void *data OVS_UNUSED
)
2378 struct dp_flow_offload_item
*offload
;
2379 struct ovs_list
*list
;
2384 ovs_mutex_lock(&dp_flow_offload
.mutex
);
2385 if (ovs_list_is_empty(&dp_flow_offload
.list
)) {
2386 ovsrcu_quiesce_start();
2387 ovs_mutex_cond_wait(&dp_flow_offload
.cond
,
2388 &dp_flow_offload
.mutex
);
2390 list
= ovs_list_pop_front(&dp_flow_offload
.list
);
2391 offload
= CONTAINER_OF(list
, struct dp_flow_offload_item
, node
);
2392 ovs_mutex_unlock(&dp_flow_offload
.mutex
);
2394 switch (offload
->op
) {
2395 case DP_NETDEV_FLOW_OFFLOAD_OP_ADD
:
2397 ret
= dp_netdev_flow_offload_put(offload
);
2399 case DP_NETDEV_FLOW_OFFLOAD_OP_MOD
:
2401 ret
= dp_netdev_flow_offload_put(offload
);
2403 case DP_NETDEV_FLOW_OFFLOAD_OP_DEL
:
2405 ret
= dp_netdev_flow_offload_del(offload
);
2411 VLOG_DBG("%s to %s netdev flow\n",
2412 ret
== 0 ? "succeed" : "failed", op
);
2413 dp_netdev_free_flow_offload(offload
);
2420 queue_netdev_flow_del(struct dp_netdev_pmd_thread
*pmd
,
2421 struct dp_netdev_flow
*flow
)
2423 struct dp_flow_offload_item
*offload
;
2425 if (ovsthread_once_start(&offload_thread_once
)) {
2426 xpthread_cond_init(&dp_flow_offload
.cond
, NULL
);
2427 ovs_thread_create("dp_netdev_flow_offload",
2428 dp_netdev_flow_offload_main
, NULL
);
2429 ovsthread_once_done(&offload_thread_once
);
2432 offload
= dp_netdev_alloc_flow_offload(pmd
, flow
,
2433 DP_NETDEV_FLOW_OFFLOAD_OP_DEL
);
2434 dp_netdev_append_flow_offload(offload
);
2438 queue_netdev_flow_put(struct dp_netdev_pmd_thread
*pmd
,
2439 struct dp_netdev_flow
*flow
, struct match
*match
,
2440 const struct nlattr
*actions
, size_t actions_len
)
2442 struct dp_flow_offload_item
*offload
;
2445 if (!netdev_is_flow_api_enabled()) {
2449 if (ovsthread_once_start(&offload_thread_once
)) {
2450 xpthread_cond_init(&dp_flow_offload
.cond
, NULL
);
2451 ovs_thread_create("dp_netdev_flow_offload",
2452 dp_netdev_flow_offload_main
, NULL
);
2453 ovsthread_once_done(&offload_thread_once
);
2456 if (flow
->mark
!= INVALID_FLOW_MARK
) {
2457 op
= DP_NETDEV_FLOW_OFFLOAD_OP_MOD
;
2459 op
= DP_NETDEV_FLOW_OFFLOAD_OP_ADD
;
2461 offload
= dp_netdev_alloc_flow_offload(pmd
, flow
, op
);
2462 offload
->match
= *match
;
2463 offload
->actions
= xmalloc(actions_len
);
2464 memcpy(offload
->actions
, actions
, actions_len
);
2465 offload
->actions_len
= actions_len
;
2467 dp_netdev_append_flow_offload(offload
);
2471 dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread
*pmd
,
2472 struct dp_netdev_flow
*flow
)
2473 OVS_REQUIRES(pmd
->flow_mutex
)
2475 struct cmap_node
*node
= CONST_CAST(struct cmap_node
*, &flow
->node
);
2477 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
2479 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
2480 ovs_assert(cls
!= NULL
);
2481 dpcls_remove(cls
, &flow
->cr
);
2482 cmap_remove(&pmd
->flow_table
, node
, dp_netdev_flow_hash(&flow
->ufid
));
2483 if (flow
->mark
!= INVALID_FLOW_MARK
) {
2484 queue_netdev_flow_del(pmd
, flow
);
2488 dp_netdev_flow_unref(flow
);
2492 dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
)
2494 struct dp_netdev_flow
*netdev_flow
;
2496 ovs_mutex_lock(&pmd
->flow_mutex
);
2497 CMAP_FOR_EACH (netdev_flow
, node
, &pmd
->flow_table
) {
2498 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
2500 ovs_mutex_unlock(&pmd
->flow_mutex
);
2504 dpif_netdev_flow_flush(struct dpif
*dpif
)
2506 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2507 struct dp_netdev_pmd_thread
*pmd
;
2509 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2510 dp_netdev_pmd_flow_flush(pmd
);
2516 struct dp_netdev_port_state
{
2517 struct hmap_position position
;
2522 dpif_netdev_port_dump_start(const struct dpif
*dpif OVS_UNUSED
, void **statep
)
2524 *statep
= xzalloc(sizeof(struct dp_netdev_port_state
));
2529 dpif_netdev_port_dump_next(const struct dpif
*dpif
, void *state_
,
2530 struct dpif_port
*dpif_port
)
2532 struct dp_netdev_port_state
*state
= state_
;
2533 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2534 struct hmap_node
*node
;
2537 ovs_mutex_lock(&dp
->port_mutex
);
2538 node
= hmap_at_position(&dp
->ports
, &state
->position
);
2540 struct dp_netdev_port
*port
;
2542 port
= CONTAINER_OF(node
, struct dp_netdev_port
, node
);
2545 state
->name
= xstrdup(netdev_get_name(port
->netdev
));
2546 dpif_port
->name
= state
->name
;
2547 dpif_port
->type
= port
->type
;
2548 dpif_port
->port_no
= port
->port_no
;
2554 ovs_mutex_unlock(&dp
->port_mutex
);
2560 dpif_netdev_port_dump_done(const struct dpif
*dpif OVS_UNUSED
, void *state_
)
2562 struct dp_netdev_port_state
*state
= state_
;
2569 dpif_netdev_port_poll(const struct dpif
*dpif_
, char **devnamep OVS_UNUSED
)
2571 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
2572 uint64_t new_port_seq
;
2575 new_port_seq
= seq_read(dpif
->dp
->port_seq
);
2576 if (dpif
->last_port_seq
!= new_port_seq
) {
2577 dpif
->last_port_seq
= new_port_seq
;
2587 dpif_netdev_port_poll_wait(const struct dpif
*dpif_
)
2589 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
2591 seq_wait(dpif
->dp
->port_seq
, dpif
->last_port_seq
);
2594 static struct dp_netdev_flow
*
2595 dp_netdev_flow_cast(const struct dpcls_rule
*cr
)
2597 return cr
? CONTAINER_OF(cr
, struct dp_netdev_flow
, cr
) : NULL
;
2600 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*flow
)
2602 return ovs_refcount_try_ref_rcu(&flow
->ref_cnt
);
2605 /* netdev_flow_key utilities.
2607 * netdev_flow_key is basically a miniflow. We use these functions
2608 * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow
2609 * functions (miniflow_clone_inline, miniflow_equal, ...), because:
2611 * - Since we are dealing exclusively with miniflows created by
2612 * miniflow_extract(), if the map is different the miniflow is different.
2613 * Therefore we can be faster by comparing the map and the miniflow in a
2615 * - These functions can be inlined by the compiler. */
2617 /* Given the number of bits set in miniflow's maps, returns the size of the
2618 * 'netdev_flow_key.mf' */
2619 static inline size_t
2620 netdev_flow_key_size(size_t flow_u64s
)
2622 return sizeof(struct miniflow
) + MINIFLOW_VALUES_SIZE(flow_u64s
);
2626 netdev_flow_key_equal(const struct netdev_flow_key
*a
,
2627 const struct netdev_flow_key
*b
)
2629 /* 'b->len' may be not set yet. */
2630 return a
->hash
== b
->hash
&& !memcmp(&a
->mf
, &b
->mf
, a
->len
);
2633 /* Used to compare 'netdev_flow_key' in the exact match cache to a miniflow.
2634 * The maps are compared bitwise, so both 'key->mf' and 'mf' must have been
2635 * generated by miniflow_extract. */
2637 netdev_flow_key_equal_mf(const struct netdev_flow_key
*key
,
2638 const struct miniflow
*mf
)
2640 return !memcmp(&key
->mf
, mf
, key
->len
);
2644 netdev_flow_key_clone(struct netdev_flow_key
*dst
,
2645 const struct netdev_flow_key
*src
)
2648 offsetof(struct netdev_flow_key
, mf
) + src
->len
);
2651 /* Initialize a netdev_flow_key 'mask' from 'match'. */
2653 netdev_flow_mask_init(struct netdev_flow_key
*mask
,
2654 const struct match
*match
)
2656 uint64_t *dst
= miniflow_values(&mask
->mf
);
2657 struct flowmap fmap
;
2661 /* Only check masks that make sense for the flow. */
2662 flow_wc_map(&match
->flow
, &fmap
);
2663 flowmap_init(&mask
->mf
.map
);
2665 FLOWMAP_FOR_EACH_INDEX(idx
, fmap
) {
2666 uint64_t mask_u64
= flow_u64_value(&match
->wc
.masks
, idx
);
2669 flowmap_set(&mask
->mf
.map
, idx
, 1);
2671 hash
= hash_add64(hash
, mask_u64
);
2677 FLOWMAP_FOR_EACH_MAP (map
, mask
->mf
.map
) {
2678 hash
= hash_add64(hash
, map
);
2681 size_t n
= dst
- miniflow_get_values(&mask
->mf
);
2683 mask
->hash
= hash_finish(hash
, n
* 8);
2684 mask
->len
= netdev_flow_key_size(n
);
2687 /* Initializes 'dst' as a copy of 'flow' masked with 'mask'. */
2689 netdev_flow_key_init_masked(struct netdev_flow_key
*dst
,
2690 const struct flow
*flow
,
2691 const struct netdev_flow_key
*mask
)
2693 uint64_t *dst_u64
= miniflow_values(&dst
->mf
);
2694 const uint64_t *mask_u64
= miniflow_get_values(&mask
->mf
);
2698 dst
->len
= mask
->len
;
2699 dst
->mf
= mask
->mf
; /* Copy maps. */
2701 FLOW_FOR_EACH_IN_MAPS(value
, flow
, mask
->mf
.map
) {
2702 *dst_u64
= value
& *mask_u64
++;
2703 hash
= hash_add64(hash
, *dst_u64
++);
2705 dst
->hash
= hash_finish(hash
,
2706 (dst_u64
- miniflow_get_values(&dst
->mf
)) * 8);
2709 /* Iterate through netdev_flow_key TNL u64 values specified by 'FLOWMAP'. */
2710 #define NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(VALUE, KEY, FLOWMAP) \
2711 MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, &(KEY)->mf, FLOWMAP)
2713 /* Returns a hash value for the bits of 'key' where there are 1-bits in
2715 static inline uint32_t
2716 netdev_flow_key_hash_in_mask(const struct netdev_flow_key
*key
,
2717 const struct netdev_flow_key
*mask
)
2719 const uint64_t *p
= miniflow_get_values(&mask
->mf
);
2723 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, key
, mask
->mf
.map
) {
2724 hash
= hash_add64(hash
, value
& *p
++);
2727 return hash_finish(hash
, (p
- miniflow_get_values(&mask
->mf
)) * 8);
2731 emc_entry_alive(struct emc_entry
*ce
)
2733 return ce
->flow
&& !ce
->flow
->dead
;
2737 emc_clear_entry(struct emc_entry
*ce
)
2740 dp_netdev_flow_unref(ce
->flow
);
2746 emc_change_entry(struct emc_entry
*ce
, struct dp_netdev_flow
*flow
,
2747 const struct netdev_flow_key
*key
)
2749 if (ce
->flow
!= flow
) {
2751 dp_netdev_flow_unref(ce
->flow
);
2754 if (dp_netdev_flow_ref(flow
)) {
2761 netdev_flow_key_clone(&ce
->key
, key
);
2766 emc_insert(struct emc_cache
*cache
, const struct netdev_flow_key
*key
,
2767 struct dp_netdev_flow
*flow
)
2769 struct emc_entry
*to_be_replaced
= NULL
;
2770 struct emc_entry
*current_entry
;
2772 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
2773 if (netdev_flow_key_equal(¤t_entry
->key
, key
)) {
2774 /* We found the entry with the 'mf' miniflow */
2775 emc_change_entry(current_entry
, flow
, NULL
);
2779 /* Replacement policy: put the flow in an empty (not alive) entry, or
2780 * in the first entry where it can be */
2782 || (emc_entry_alive(to_be_replaced
)
2783 && !emc_entry_alive(current_entry
))
2784 || current_entry
->key
.hash
< to_be_replaced
->key
.hash
) {
2785 to_be_replaced
= current_entry
;
2788 /* We didn't find the miniflow in the cache.
2789 * The 'to_be_replaced' entry is where the new flow will be stored */
2791 emc_change_entry(to_be_replaced
, flow
, key
);
2795 emc_probabilistic_insert(struct dp_netdev_pmd_thread
*pmd
,
2796 const struct netdev_flow_key
*key
,
2797 struct dp_netdev_flow
*flow
)
2799 /* Insert an entry into the EMC based on probability value 'min'. By
2800 * default the value is UINT32_MAX / 100 which yields an insertion
2801 * probability of 1/100 ie. 1% */
2805 atomic_read_relaxed(&pmd
->dp
->emc_insert_min
, &min
);
2807 if (min
&& random_uint32() <= min
) {
2808 emc_insert(&(pmd
->flow_cache
).emc_cache
, key
, flow
);
2812 static inline struct dp_netdev_flow
*
2813 emc_lookup(struct emc_cache
*cache
, const struct netdev_flow_key
*key
)
2815 struct emc_entry
*current_entry
;
2817 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
2818 if (current_entry
->key
.hash
== key
->hash
2819 && emc_entry_alive(current_entry
)
2820 && netdev_flow_key_equal_mf(¤t_entry
->key
, &key
->mf
)) {
2822 /* We found the entry with the 'key->mf' miniflow */
2823 return current_entry
->flow
;
2830 static inline const struct cmap_node
*
2831 smc_entry_get(struct dp_netdev_pmd_thread
*pmd
, const uint32_t hash
)
2833 struct smc_cache
*cache
= &(pmd
->flow_cache
).smc_cache
;
2834 struct smc_bucket
*bucket
= &cache
->buckets
[hash
& SMC_MASK
];
2835 uint16_t sig
= hash
>> 16;
2836 uint16_t index
= UINT16_MAX
;
2838 for (int i
= 0; i
< SMC_ENTRY_PER_BUCKET
; i
++) {
2839 if (bucket
->sig
[i
] == sig
) {
2840 index
= bucket
->flow_idx
[i
];
2844 if (index
!= UINT16_MAX
) {
2845 return cmap_find_by_index(&pmd
->flow_table
, index
);
2851 smc_clear_entry(struct smc_bucket
*b
, int idx
)
2853 b
->flow_idx
[idx
] = UINT16_MAX
;
2856 /* Insert the flow_table index into SMC. Insertion may fail when 1) SMC is
2857 * turned off, 2) the flow_table index is larger than uint16_t can handle.
2858 * If there is already an SMC entry having same signature, the index will be
2859 * updated. If there is no existing entry, but an empty entry is available,
2860 * the empty entry will be taken. If no empty entry or existing same signature,
2861 * a random entry from the hashed bucket will be picked. */
2863 smc_insert(struct dp_netdev_pmd_thread
*pmd
,
2864 const struct netdev_flow_key
*key
,
2867 struct smc_cache
*smc_cache
= &(pmd
->flow_cache
).smc_cache
;
2868 struct smc_bucket
*bucket
= &smc_cache
->buckets
[key
->hash
& SMC_MASK
];
2870 uint32_t cmap_index
;
2874 atomic_read_relaxed(&pmd
->dp
->smc_enable_db
, &smc_enable_db
);
2875 if (!smc_enable_db
) {
2879 cmap_index
= cmap_find_index(&pmd
->flow_table
, hash
);
2880 index
= (cmap_index
>= UINT16_MAX
) ? UINT16_MAX
: (uint16_t)cmap_index
;
2882 /* If the index is larger than SMC can handle (uint16_t), we don't
2884 if (index
== UINT16_MAX
) {
2888 /* If an entry with same signature already exists, update the index */
2889 uint16_t sig
= key
->hash
>> 16;
2890 for (i
= 0; i
< SMC_ENTRY_PER_BUCKET
; i
++) {
2891 if (bucket
->sig
[i
] == sig
) {
2892 bucket
->flow_idx
[i
] = index
;
2896 /* If there is an empty entry, occupy it. */
2897 for (i
= 0; i
< SMC_ENTRY_PER_BUCKET
; i
++) {
2898 if (bucket
->flow_idx
[i
] == UINT16_MAX
) {
2899 bucket
->sig
[i
] = sig
;
2900 bucket
->flow_idx
[i
] = index
;
2904 /* Otherwise, pick a random entry. */
2905 i
= random_uint32() % SMC_ENTRY_PER_BUCKET
;
2906 bucket
->sig
[i
] = sig
;
2907 bucket
->flow_idx
[i
] = index
;
2910 static struct dp_netdev_flow
*
2911 dp_netdev_pmd_lookup_flow(struct dp_netdev_pmd_thread
*pmd
,
2912 const struct netdev_flow_key
*key
,
2916 struct dpcls_rule
*rule
;
2917 odp_port_t in_port
= u32_to_odp(MINIFLOW_GET_U32(&key
->mf
,
2919 struct dp_netdev_flow
*netdev_flow
= NULL
;
2921 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
2922 if (OVS_LIKELY(cls
)) {
2923 dpcls_lookup(cls
, &key
, &rule
, 1, lookup_num_p
);
2924 netdev_flow
= dp_netdev_flow_cast(rule
);
2929 static struct dp_netdev_flow
*
2930 dp_netdev_pmd_find_flow(const struct dp_netdev_pmd_thread
*pmd
,
2931 const ovs_u128
*ufidp
, const struct nlattr
*key
,
2934 struct dp_netdev_flow
*netdev_flow
;
2938 /* If a UFID is not provided, determine one based on the key. */
2939 if (!ufidp
&& key
&& key_len
2940 && !dpif_netdev_flow_from_nlattrs(key
, key_len
, &flow
, false)) {
2941 dpif_flow_hash(pmd
->dp
->dpif
, &flow
, sizeof flow
, &ufid
);
2946 CMAP_FOR_EACH_WITH_HASH (netdev_flow
, node
, dp_netdev_flow_hash(ufidp
),
2948 if (ovs_u128_equals(netdev_flow
->ufid
, *ufidp
)) {
2958 get_dpif_flow_stats(const struct dp_netdev_flow
*netdev_flow_
,
2959 struct dpif_flow_stats
*stats
)
2961 struct dp_netdev_flow
*netdev_flow
;
2962 unsigned long long n
;
2966 netdev_flow
= CONST_CAST(struct dp_netdev_flow
*, netdev_flow_
);
2968 atomic_read_relaxed(&netdev_flow
->stats
.packet_count
, &n
);
2969 stats
->n_packets
= n
;
2970 atomic_read_relaxed(&netdev_flow
->stats
.byte_count
, &n
);
2972 atomic_read_relaxed(&netdev_flow
->stats
.used
, &used
);
2974 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
2975 stats
->tcp_flags
= flags
;
2978 /* Converts to the dpif_flow format, using 'key_buf' and 'mask_buf' for
2979 * storing the netlink-formatted key/mask. 'key_buf' may be the same as
2980 * 'mask_buf'. Actions will be returned without copying, by relying on RCU to
2983 dp_netdev_flow_to_dpif_flow(const struct dp_netdev_flow
*netdev_flow
,
2984 struct ofpbuf
*key_buf
, struct ofpbuf
*mask_buf
,
2985 struct dpif_flow
*flow
, bool terse
)
2988 memset(flow
, 0, sizeof *flow
);
2990 struct flow_wildcards wc
;
2991 struct dp_netdev_actions
*actions
;
2993 struct odp_flow_key_parms odp_parms
= {
2994 .flow
= &netdev_flow
->flow
,
2996 .support
= dp_netdev_support
,
2999 miniflow_expand(&netdev_flow
->cr
.mask
->mf
, &wc
.masks
);
3000 /* in_port is exact matched, but we have left it out from the mask for
3001 * optimnization reasons. Add in_port back to the mask. */
3002 wc
.masks
.in_port
.odp_port
= ODPP_NONE
;
3005 offset
= key_buf
->size
;
3006 flow
->key
= ofpbuf_tail(key_buf
);
3007 odp_flow_key_from_flow(&odp_parms
, key_buf
);
3008 flow
->key_len
= key_buf
->size
- offset
;
3011 offset
= mask_buf
->size
;
3012 flow
->mask
= ofpbuf_tail(mask_buf
);
3013 odp_parms
.key_buf
= key_buf
;
3014 odp_flow_key_from_mask(&odp_parms
, mask_buf
);
3015 flow
->mask_len
= mask_buf
->size
- offset
;
3018 actions
= dp_netdev_flow_get_actions(netdev_flow
);
3019 flow
->actions
= actions
->actions
;
3020 flow
->actions_len
= actions
->size
;
3023 flow
->ufid
= netdev_flow
->ufid
;
3024 flow
->ufid_present
= true;
3025 flow
->pmd_id
= netdev_flow
->pmd_id
;
3026 get_dpif_flow_stats(netdev_flow
, &flow
->stats
);
3030 dpif_netdev_mask_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
3031 const struct nlattr
*mask_key
,
3032 uint32_t mask_key_len
, const struct flow
*flow
,
3033 struct flow_wildcards
*wc
, bool probe
)
3035 enum odp_key_fitness fitness
;
3037 fitness
= odp_flow_key_to_mask(mask_key
, mask_key_len
, wc
, flow
);
3040 /* This should not happen: it indicates that
3041 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
3042 * disagree on the acceptable form of a mask. Log the problem
3043 * as an error, with enough details to enable debugging. */
3044 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3046 if (!VLOG_DROP_ERR(&rl
)) {
3050 odp_flow_format(key
, key_len
, mask_key
, mask_key_len
, NULL
, &s
,
3052 VLOG_ERR("internal error parsing flow mask %s (%s)",
3053 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
3065 dpif_netdev_flow_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
3066 struct flow
*flow
, bool probe
)
3068 if (odp_flow_key_to_flow(key
, key_len
, flow
)) {
3070 /* This should not happen: it indicates that
3071 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
3072 * the acceptable form of a flow. Log the problem as an error,
3073 * with enough details to enable debugging. */
3074 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3076 if (!VLOG_DROP_ERR(&rl
)) {
3080 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
3081 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
3089 if (flow
->ct_state
& DP_NETDEV_CS_UNSUPPORTED_MASK
) {
3097 dpif_netdev_flow_get(const struct dpif
*dpif
, const struct dpif_flow_get
*get
)
3099 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3100 struct dp_netdev_flow
*netdev_flow
;
3101 struct dp_netdev_pmd_thread
*pmd
;
3102 struct hmapx to_find
= HMAPX_INITIALIZER(&to_find
);
3103 struct hmapx_node
*node
;
3106 if (get
->pmd_id
== PMD_ID_NULL
) {
3107 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3108 if (dp_netdev_pmd_try_ref(pmd
) && !hmapx_add(&to_find
, pmd
)) {
3109 dp_netdev_pmd_unref(pmd
);
3113 pmd
= dp_netdev_get_pmd(dp
, get
->pmd_id
);
3117 hmapx_add(&to_find
, pmd
);
3120 if (!hmapx_count(&to_find
)) {
3124 HMAPX_FOR_EACH (node
, &to_find
) {
3125 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3126 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, get
->ufid
, get
->key
,
3129 dp_netdev_flow_to_dpif_flow(netdev_flow
, get
->buffer
, get
->buffer
,
3138 HMAPX_FOR_EACH (node
, &to_find
) {
3139 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3140 dp_netdev_pmd_unref(pmd
);
3143 hmapx_destroy(&to_find
);
3148 dp_netdev_get_mega_ufid(const struct match
*match
, ovs_u128
*mega_ufid
)
3150 struct flow masked_flow
;
3153 for (i
= 0; i
< sizeof(struct flow
); i
++) {
3154 ((uint8_t *)&masked_flow
)[i
] = ((uint8_t *)&match
->flow
)[i
] &
3155 ((uint8_t *)&match
->wc
)[i
];
3157 dpif_flow_hash(NULL
, &masked_flow
, sizeof(struct flow
), mega_ufid
);
3160 static struct dp_netdev_flow
*
3161 dp_netdev_flow_add(struct dp_netdev_pmd_thread
*pmd
,
3162 struct match
*match
, const ovs_u128
*ufid
,
3163 const struct nlattr
*actions
, size_t actions_len
)
3164 OVS_REQUIRES(pmd
->flow_mutex
)
3166 struct dp_netdev_flow
*flow
;
3167 struct netdev_flow_key mask
;
3170 /* Make sure in_port is exact matched before we read it. */
3171 ovs_assert(match
->wc
.masks
.in_port
.odp_port
== ODPP_NONE
);
3172 odp_port_t in_port
= match
->flow
.in_port
.odp_port
;
3174 /* As we select the dpcls based on the port number, each netdev flow
3175 * belonging to the same dpcls will have the same odp_port value.
3176 * For performance reasons we wildcard odp_port here in the mask. In the
3177 * typical case dp_hash is also wildcarded, and the resulting 8-byte
3178 * chunk {dp_hash, in_port} will be ignored by netdev_flow_mask_init() and
3179 * will not be part of the subtable mask.
3180 * This will speed up the hash computation during dpcls_lookup() because
3181 * there is one less call to hash_add64() in this case. */
3182 match
->wc
.masks
.in_port
.odp_port
= 0;
3183 netdev_flow_mask_init(&mask
, match
);
3184 match
->wc
.masks
.in_port
.odp_port
= ODPP_NONE
;
3186 /* Make sure wc does not have metadata. */
3187 ovs_assert(!FLOWMAP_HAS_FIELD(&mask
.mf
.map
, metadata
)
3188 && !FLOWMAP_HAS_FIELD(&mask
.mf
.map
, regs
));
3190 /* Do not allocate extra space. */
3191 flow
= xmalloc(sizeof *flow
- sizeof flow
->cr
.flow
.mf
+ mask
.len
);
3192 memset(&flow
->stats
, 0, sizeof flow
->stats
);
3195 flow
->mark
= INVALID_FLOW_MARK
;
3196 *CONST_CAST(unsigned *, &flow
->pmd_id
) = pmd
->core_id
;
3197 *CONST_CAST(struct flow
*, &flow
->flow
) = match
->flow
;
3198 *CONST_CAST(ovs_u128
*, &flow
->ufid
) = *ufid
;
3199 ovs_refcount_init(&flow
->ref_cnt
);
3200 ovsrcu_set(&flow
->actions
, dp_netdev_actions_create(actions
, actions_len
));
3202 dp_netdev_get_mega_ufid(match
, CONST_CAST(ovs_u128
*, &flow
->mega_ufid
));
3203 netdev_flow_key_init_masked(&flow
->cr
.flow
, &match
->flow
, &mask
);
3205 /* Select dpcls for in_port. Relies on in_port to be exact match. */
3206 cls
= dp_netdev_pmd_find_dpcls(pmd
, in_port
);
3207 dpcls_insert(cls
, &flow
->cr
, &mask
);
3209 cmap_insert(&pmd
->flow_table
, CONST_CAST(struct cmap_node
*, &flow
->node
),
3210 dp_netdev_flow_hash(&flow
->ufid
));
3212 queue_netdev_flow_put(pmd
, flow
, match
, actions
, actions_len
);
3214 if (OVS_UNLIKELY(!VLOG_DROP_DBG((&upcall_rl
)))) {
3215 struct ds ds
= DS_EMPTY_INITIALIZER
;
3216 struct ofpbuf key_buf
, mask_buf
;
3217 struct odp_flow_key_parms odp_parms
= {
3218 .flow
= &match
->flow
,
3219 .mask
= &match
->wc
.masks
,
3220 .support
= dp_netdev_support
,
3223 ofpbuf_init(&key_buf
, 0);
3224 ofpbuf_init(&mask_buf
, 0);
3226 odp_flow_key_from_flow(&odp_parms
, &key_buf
);
3227 odp_parms
.key_buf
= &key_buf
;
3228 odp_flow_key_from_mask(&odp_parms
, &mask_buf
);
3230 ds_put_cstr(&ds
, "flow_add: ");
3231 odp_format_ufid(ufid
, &ds
);
3232 ds_put_cstr(&ds
, " ");
3233 odp_flow_format(key_buf
.data
, key_buf
.size
,
3234 mask_buf
.data
, mask_buf
.size
,
3236 ds_put_cstr(&ds
, ", actions:");
3237 format_odp_actions(&ds
, actions
, actions_len
, NULL
);
3239 VLOG_DBG("%s", ds_cstr(&ds
));
3241 ofpbuf_uninit(&key_buf
);
3242 ofpbuf_uninit(&mask_buf
);
3244 /* Add a printout of the actual match installed. */
3247 ds_put_cstr(&ds
, "flow match: ");
3248 miniflow_expand(&flow
->cr
.flow
.mf
, &m
.flow
);
3249 miniflow_expand(&flow
->cr
.mask
->mf
, &m
.wc
.masks
);
3250 memset(&m
.tun_md
, 0, sizeof m
.tun_md
);
3251 match_format(&m
, NULL
, &ds
, OFP_DEFAULT_PRIORITY
);
3253 VLOG_DBG("%s", ds_cstr(&ds
));
3262 flow_put_on_pmd(struct dp_netdev_pmd_thread
*pmd
,
3263 struct netdev_flow_key
*key
,
3264 struct match
*match
,
3266 const struct dpif_flow_put
*put
,
3267 struct dpif_flow_stats
*stats
)
3269 struct dp_netdev_flow
*netdev_flow
;
3273 memset(stats
, 0, sizeof *stats
);
3276 ovs_mutex_lock(&pmd
->flow_mutex
);
3277 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
, NULL
);
3279 if (put
->flags
& DPIF_FP_CREATE
) {
3280 if (cmap_count(&pmd
->flow_table
) < MAX_FLOWS
) {
3281 dp_netdev_flow_add(pmd
, match
, ufid
, put
->actions
,
3291 if (put
->flags
& DPIF_FP_MODIFY
) {
3292 struct dp_netdev_actions
*new_actions
;
3293 struct dp_netdev_actions
*old_actions
;
3295 new_actions
= dp_netdev_actions_create(put
->actions
,
3298 old_actions
= dp_netdev_flow_get_actions(netdev_flow
);
3299 ovsrcu_set(&netdev_flow
->actions
, new_actions
);
3301 queue_netdev_flow_put(pmd
, netdev_flow
, match
,
3302 put
->actions
, put
->actions_len
);
3305 get_dpif_flow_stats(netdev_flow
, stats
);
3307 if (put
->flags
& DPIF_FP_ZERO_STATS
) {
3308 /* XXX: The userspace datapath uses thread local statistics
3309 * (for flows), which should be updated only by the owning
3310 * thread. Since we cannot write on stats memory here,
3311 * we choose not to support this flag. Please note:
3312 * - This feature is currently used only by dpctl commands with
3314 * - Should the need arise, this operation can be implemented
3315 * by keeping a base value (to be update here) for each
3316 * counter, and subtracting it before outputting the stats */
3320 ovsrcu_postpone(dp_netdev_actions_free
, old_actions
);
3321 } else if (put
->flags
& DPIF_FP_CREATE
) {
3324 /* Overlapping flow. */
3328 ovs_mutex_unlock(&pmd
->flow_mutex
);
3333 dpif_netdev_flow_put(struct dpif
*dpif
, const struct dpif_flow_put
*put
)
3335 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3336 struct netdev_flow_key key
, mask
;
3337 struct dp_netdev_pmd_thread
*pmd
;
3341 bool probe
= put
->flags
& DPIF_FP_PROBE
;
3344 memset(put
->stats
, 0, sizeof *put
->stats
);
3346 error
= dpif_netdev_flow_from_nlattrs(put
->key
, put
->key_len
, &match
.flow
,
3351 error
= dpif_netdev_mask_from_nlattrs(put
->key
, put
->key_len
,
3352 put
->mask
, put
->mask_len
,
3353 &match
.flow
, &match
.wc
, probe
);
3361 dpif_flow_hash(dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
3364 /* Must produce a netdev_flow_key for lookup.
3365 * Use the same method as employed to create the key when adding
3366 * the flow to the dplcs to make sure they match. */
3367 netdev_flow_mask_init(&mask
, &match
);
3368 netdev_flow_key_init_masked(&key
, &match
.flow
, &mask
);
3370 if (put
->pmd_id
== PMD_ID_NULL
) {
3371 if (cmap_count(&dp
->poll_threads
) == 0) {
3374 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3375 struct dpif_flow_stats pmd_stats
;
3378 pmd_error
= flow_put_on_pmd(pmd
, &key
, &match
, &ufid
, put
,
3382 } else if (put
->stats
) {
3383 put
->stats
->n_packets
+= pmd_stats
.n_packets
;
3384 put
->stats
->n_bytes
+= pmd_stats
.n_bytes
;
3385 put
->stats
->used
= MAX(put
->stats
->used
, pmd_stats
.used
);
3386 put
->stats
->tcp_flags
|= pmd_stats
.tcp_flags
;
3390 pmd
= dp_netdev_get_pmd(dp
, put
->pmd_id
);
3394 error
= flow_put_on_pmd(pmd
, &key
, &match
, &ufid
, put
, put
->stats
);
3395 dp_netdev_pmd_unref(pmd
);
3402 flow_del_on_pmd(struct dp_netdev_pmd_thread
*pmd
,
3403 struct dpif_flow_stats
*stats
,
3404 const struct dpif_flow_del
*del
)
3406 struct dp_netdev_flow
*netdev_flow
;
3409 ovs_mutex_lock(&pmd
->flow_mutex
);
3410 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, del
->ufid
, del
->key
,
3414 get_dpif_flow_stats(netdev_flow
, stats
);
3416 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
3420 ovs_mutex_unlock(&pmd
->flow_mutex
);
3426 dpif_netdev_flow_del(struct dpif
*dpif
, const struct dpif_flow_del
*del
)
3428 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3429 struct dp_netdev_pmd_thread
*pmd
;
3433 memset(del
->stats
, 0, sizeof *del
->stats
);
3436 if (del
->pmd_id
== PMD_ID_NULL
) {
3437 if (cmap_count(&dp
->poll_threads
) == 0) {
3440 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3441 struct dpif_flow_stats pmd_stats
;
3444 pmd_error
= flow_del_on_pmd(pmd
, &pmd_stats
, del
);
3447 } else if (del
->stats
) {
3448 del
->stats
->n_packets
+= pmd_stats
.n_packets
;
3449 del
->stats
->n_bytes
+= pmd_stats
.n_bytes
;
3450 del
->stats
->used
= MAX(del
->stats
->used
, pmd_stats
.used
);
3451 del
->stats
->tcp_flags
|= pmd_stats
.tcp_flags
;
3455 pmd
= dp_netdev_get_pmd(dp
, del
->pmd_id
);
3459 error
= flow_del_on_pmd(pmd
, del
->stats
, del
);
3460 dp_netdev_pmd_unref(pmd
);
3467 struct dpif_netdev_flow_dump
{
3468 struct dpif_flow_dump up
;
3469 struct cmap_position poll_thread_pos
;
3470 struct cmap_position flow_pos
;
3471 struct dp_netdev_pmd_thread
*cur_pmd
;
3473 struct ovs_mutex mutex
;
3476 static struct dpif_netdev_flow_dump
*
3477 dpif_netdev_flow_dump_cast(struct dpif_flow_dump
*dump
)
3479 return CONTAINER_OF(dump
, struct dpif_netdev_flow_dump
, up
);
3482 static struct dpif_flow_dump
*
3483 dpif_netdev_flow_dump_create(const struct dpif
*dpif_
, bool terse
,
3484 char *type OVS_UNUSED
)
3486 struct dpif_netdev_flow_dump
*dump
;
3488 dump
= xzalloc(sizeof *dump
);
3489 dpif_flow_dump_init(&dump
->up
, dpif_
);
3490 dump
->up
.terse
= terse
;
3491 ovs_mutex_init(&dump
->mutex
);
3497 dpif_netdev_flow_dump_destroy(struct dpif_flow_dump
*dump_
)
3499 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
3501 ovs_mutex_destroy(&dump
->mutex
);
3506 struct dpif_netdev_flow_dump_thread
{
3507 struct dpif_flow_dump_thread up
;
3508 struct dpif_netdev_flow_dump
*dump
;
3509 struct odputil_keybuf keybuf
[FLOW_DUMP_MAX_BATCH
];
3510 struct odputil_keybuf maskbuf
[FLOW_DUMP_MAX_BATCH
];
3513 static struct dpif_netdev_flow_dump_thread
*
3514 dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread
*thread
)
3516 return CONTAINER_OF(thread
, struct dpif_netdev_flow_dump_thread
, up
);
3519 static struct dpif_flow_dump_thread
*
3520 dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump
*dump_
)
3522 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
3523 struct dpif_netdev_flow_dump_thread
*thread
;
3525 thread
= xmalloc(sizeof *thread
);
3526 dpif_flow_dump_thread_init(&thread
->up
, &dump
->up
);
3527 thread
->dump
= dump
;
3532 dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread
*thread_
)
3534 struct dpif_netdev_flow_dump_thread
*thread
3535 = dpif_netdev_flow_dump_thread_cast(thread_
);
3541 dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread
*thread_
,
3542 struct dpif_flow
*flows
, int max_flows
)
3544 struct dpif_netdev_flow_dump_thread
*thread
3545 = dpif_netdev_flow_dump_thread_cast(thread_
);
3546 struct dpif_netdev_flow_dump
*dump
= thread
->dump
;
3547 struct dp_netdev_flow
*netdev_flows
[FLOW_DUMP_MAX_BATCH
];
3551 ovs_mutex_lock(&dump
->mutex
);
3552 if (!dump
->status
) {
3553 struct dpif_netdev
*dpif
= dpif_netdev_cast(thread
->up
.dpif
);
3554 struct dp_netdev
*dp
= get_dp_netdev(&dpif
->dpif
);
3555 struct dp_netdev_pmd_thread
*pmd
= dump
->cur_pmd
;
3556 int flow_limit
= MIN(max_flows
, FLOW_DUMP_MAX_BATCH
);
3558 /* First call to dump_next(), extracts the first pmd thread.
3559 * If there is no pmd thread, returns immediately. */
3561 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
3563 ovs_mutex_unlock(&dump
->mutex
);
3570 for (n_flows
= 0; n_flows
< flow_limit
; n_flows
++) {
3571 struct cmap_node
*node
;
3573 node
= cmap_next_position(&pmd
->flow_table
, &dump
->flow_pos
);
3577 netdev_flows
[n_flows
] = CONTAINER_OF(node
,
3578 struct dp_netdev_flow
,
3581 /* When finishing dumping the current pmd thread, moves to
3583 if (n_flows
< flow_limit
) {
3584 memset(&dump
->flow_pos
, 0, sizeof dump
->flow_pos
);
3585 dp_netdev_pmd_unref(pmd
);
3586 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
3592 /* Keeps the reference to next caller. */
3593 dump
->cur_pmd
= pmd
;
3595 /* If the current dump is empty, do not exit the loop, since the
3596 * remaining pmds could have flows to be dumped. Just dumps again
3597 * on the new 'pmd'. */
3600 ovs_mutex_unlock(&dump
->mutex
);
3602 for (i
= 0; i
< n_flows
; i
++) {
3603 struct odputil_keybuf
*maskbuf
= &thread
->maskbuf
[i
];
3604 struct odputil_keybuf
*keybuf
= &thread
->keybuf
[i
];
3605 struct dp_netdev_flow
*netdev_flow
= netdev_flows
[i
];
3606 struct dpif_flow
*f
= &flows
[i
];
3607 struct ofpbuf key
, mask
;
3609 ofpbuf_use_stack(&key
, keybuf
, sizeof *keybuf
);
3610 ofpbuf_use_stack(&mask
, maskbuf
, sizeof *maskbuf
);
3611 dp_netdev_flow_to_dpif_flow(netdev_flow
, &key
, &mask
, f
,
3619 dpif_netdev_execute(struct dpif
*dpif
, struct dpif_execute
*execute
)
3620 OVS_NO_THREAD_SAFETY_ANALYSIS
3622 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3623 struct dp_netdev_pmd_thread
*pmd
;
3624 struct dp_packet_batch pp
;
3626 if (dp_packet_size(execute
->packet
) < ETH_HEADER_LEN
||
3627 dp_packet_size(execute
->packet
) > UINT16_MAX
) {
3631 /* Tries finding the 'pmd'. If NULL is returned, that means
3632 * the current thread is a non-pmd thread and should use
3633 * dp_netdev_get_pmd(dp, NON_PMD_CORE_ID). */
3634 pmd
= ovsthread_getspecific(dp
->per_pmd_key
);
3636 pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
3642 if (execute
->probe
) {
3643 /* If this is part of a probe, Drop the packet, since executing
3644 * the action may actually cause spurious packets be sent into
3646 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3647 dp_netdev_pmd_unref(pmd
);
3652 /* If the current thread is non-pmd thread, acquires
3653 * the 'non_pmd_mutex'. */
3654 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3655 ovs_mutex_lock(&dp
->non_pmd_mutex
);
3658 /* Update current time in PMD context. */
3659 pmd_thread_ctx_time_update(pmd
);
3661 /* The action processing expects the RSS hash to be valid, because
3662 * it's always initialized at the beginning of datapath processing.
3663 * In this case, though, 'execute->packet' may not have gone through
3664 * the datapath at all, it may have been generated by the upper layer
3665 * (OpenFlow packet-out, BFD frame, ...). */
3666 if (!dp_packet_rss_valid(execute
->packet
)) {
3667 dp_packet_set_rss_hash(execute
->packet
,
3668 flow_hash_5tuple(execute
->flow
, 0));
3671 dp_packet_batch_init_packet(&pp
, execute
->packet
);
3672 dp_netdev_execute_actions(pmd
, &pp
, false, execute
->flow
,
3673 execute
->actions
, execute
->actions_len
);
3674 dp_netdev_pmd_flush_output_packets(pmd
, true);
3676 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3677 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
3678 dp_netdev_pmd_unref(pmd
);
3685 dpif_netdev_operate(struct dpif
*dpif
, struct dpif_op
**ops
, size_t n_ops
)
3689 for (i
= 0; i
< n_ops
; i
++) {
3690 struct dpif_op
*op
= ops
[i
];
3693 case DPIF_OP_FLOW_PUT
:
3694 op
->error
= dpif_netdev_flow_put(dpif
, &op
->flow_put
);
3697 case DPIF_OP_FLOW_DEL
:
3698 op
->error
= dpif_netdev_flow_del(dpif
, &op
->flow_del
);
3701 case DPIF_OP_EXECUTE
:
3702 op
->error
= dpif_netdev_execute(dpif
, &op
->execute
);
3705 case DPIF_OP_FLOW_GET
:
3706 op
->error
= dpif_netdev_flow_get(dpif
, &op
->flow_get
);
3712 /* Applies datapath configuration from the database. Some of the changes are
3713 * actually applied in dpif_netdev_run(). */
3715 dpif_netdev_set_config(struct dpif
*dpif
, const struct smap
*other_config
)
3717 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3718 const char *cmask
= smap_get(other_config
, "pmd-cpu-mask");
3719 unsigned long long insert_prob
=
3720 smap_get_ullong(other_config
, "emc-insert-inv-prob",
3721 DEFAULT_EM_FLOW_INSERT_INV_PROB
);
3722 uint32_t insert_min
, cur_min
;
3723 uint32_t tx_flush_interval
, cur_tx_flush_interval
;
3725 tx_flush_interval
= smap_get_int(other_config
, "tx-flush-interval",
3726 DEFAULT_TX_FLUSH_INTERVAL
);
3727 atomic_read_relaxed(&dp
->tx_flush_interval
, &cur_tx_flush_interval
);
3728 if (tx_flush_interval
!= cur_tx_flush_interval
) {
3729 atomic_store_relaxed(&dp
->tx_flush_interval
, tx_flush_interval
);
3730 VLOG_INFO("Flushing interval for tx queues set to %"PRIu32
" us",
3734 if (!nullable_string_is_equal(dp
->pmd_cmask
, cmask
)) {
3735 free(dp
->pmd_cmask
);
3736 dp
->pmd_cmask
= nullable_xstrdup(cmask
);
3737 dp_netdev_request_reconfigure(dp
);
3740 atomic_read_relaxed(&dp
->emc_insert_min
, &cur_min
);
3741 if (insert_prob
<= UINT32_MAX
) {
3742 insert_min
= insert_prob
== 0 ? 0 : UINT32_MAX
/ insert_prob
;
3744 insert_min
= DEFAULT_EM_FLOW_INSERT_MIN
;
3745 insert_prob
= DEFAULT_EM_FLOW_INSERT_INV_PROB
;
3748 if (insert_min
!= cur_min
) {
3749 atomic_store_relaxed(&dp
->emc_insert_min
, insert_min
);
3750 if (insert_min
== 0) {
3751 VLOG_INFO("EMC has been disabled");
3753 VLOG_INFO("EMC insertion probability changed to 1/%llu (~%.2f%%)",
3754 insert_prob
, (100 / (float)insert_prob
));
3758 bool perf_enabled
= smap_get_bool(other_config
, "pmd-perf-metrics", false);
3759 bool cur_perf_enabled
;
3760 atomic_read_relaxed(&dp
->pmd_perf_metrics
, &cur_perf_enabled
);
3761 if (perf_enabled
!= cur_perf_enabled
) {
3762 atomic_store_relaxed(&dp
->pmd_perf_metrics
, perf_enabled
);
3764 VLOG_INFO("PMD performance metrics collection enabled");
3766 VLOG_INFO("PMD performance metrics collection disabled");
3770 bool smc_enable
= smap_get_bool(other_config
, "smc-enable", false);
3772 atomic_read_relaxed(&dp
->smc_enable_db
, &cur_smc
);
3773 if (smc_enable
!= cur_smc
) {
3774 atomic_store_relaxed(&dp
->smc_enable_db
, smc_enable
);
3776 VLOG_INFO("SMC cache is enabled");
3778 VLOG_INFO("SMC cache is disabled");
3784 /* Parses affinity list and returns result in 'core_ids'. */
3786 parse_affinity_list(const char *affinity_list
, unsigned *core_ids
, int n_rxq
)
3789 char *list
, *copy
, *key
, *value
;
3792 for (i
= 0; i
< n_rxq
; i
++) {
3793 core_ids
[i
] = OVS_CORE_UNSPEC
;
3796 if (!affinity_list
) {
3800 list
= copy
= xstrdup(affinity_list
);
3802 while (ofputil_parse_key_value(&list
, &key
, &value
)) {
3803 int rxq_id
, core_id
;
3805 if (!str_to_int(key
, 0, &rxq_id
) || rxq_id
< 0
3806 || !str_to_int(value
, 0, &core_id
) || core_id
< 0) {
3811 if (rxq_id
< n_rxq
) {
3812 core_ids
[rxq_id
] = core_id
;
3820 /* Parses 'affinity_list' and applies configuration if it is valid. */
3822 dpif_netdev_port_set_rxq_affinity(struct dp_netdev_port
*port
,
3823 const char *affinity_list
)
3825 unsigned *core_ids
, i
;
3828 core_ids
= xmalloc(port
->n_rxq
* sizeof *core_ids
);
3829 if (parse_affinity_list(affinity_list
, core_ids
, port
->n_rxq
)) {
3834 for (i
= 0; i
< port
->n_rxq
; i
++) {
3835 port
->rxqs
[i
].core_id
= core_ids
[i
];
3843 /* Changes the affinity of port's rx queues. The changes are actually applied
3844 * in dpif_netdev_run(). */
3846 dpif_netdev_port_set_config(struct dpif
*dpif
, odp_port_t port_no
,
3847 const struct smap
*cfg
)
3849 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3850 struct dp_netdev_port
*port
;
3852 const char *affinity_list
= smap_get(cfg
, "pmd-rxq-affinity");
3854 ovs_mutex_lock(&dp
->port_mutex
);
3855 error
= get_port_by_number(dp
, port_no
, &port
);
3856 if (error
|| !netdev_is_pmd(port
->netdev
)
3857 || nullable_string_is_equal(affinity_list
, port
->rxq_affinity_list
)) {
3861 error
= dpif_netdev_port_set_rxq_affinity(port
, affinity_list
);
3865 free(port
->rxq_affinity_list
);
3866 port
->rxq_affinity_list
= nullable_xstrdup(affinity_list
);
3868 dp_netdev_request_reconfigure(dp
);
3870 ovs_mutex_unlock(&dp
->port_mutex
);
3875 dpif_netdev_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
3876 uint32_t queue_id
, uint32_t *priority
)
3878 *priority
= queue_id
;
3883 /* Creates and returns a new 'struct dp_netdev_actions', whose actions are
3884 * a copy of the 'size' bytes of 'actions' input parameters. */
3885 struct dp_netdev_actions
*
3886 dp_netdev_actions_create(const struct nlattr
*actions
, size_t size
)
3888 struct dp_netdev_actions
*netdev_actions
;
3890 netdev_actions
= xmalloc(sizeof *netdev_actions
+ size
);
3891 memcpy(netdev_actions
->actions
, actions
, size
);
3892 netdev_actions
->size
= size
;
3894 return netdev_actions
;
3897 struct dp_netdev_actions
*
3898 dp_netdev_flow_get_actions(const struct dp_netdev_flow
*flow
)
3900 return ovsrcu_get(struct dp_netdev_actions
*, &flow
->actions
);
3904 dp_netdev_actions_free(struct dp_netdev_actions
*actions
)
3910 dp_netdev_rxq_set_cycles(struct dp_netdev_rxq
*rx
,
3911 enum rxq_cycles_counter_type type
,
3912 unsigned long long cycles
)
3914 atomic_store_relaxed(&rx
->cycles
[type
], cycles
);
3918 dp_netdev_rxq_add_cycles(struct dp_netdev_rxq
*rx
,
3919 enum rxq_cycles_counter_type type
,
3920 unsigned long long cycles
)
3922 non_atomic_ullong_add(&rx
->cycles
[type
], cycles
);
3926 dp_netdev_rxq_get_cycles(struct dp_netdev_rxq
*rx
,
3927 enum rxq_cycles_counter_type type
)
3929 unsigned long long processing_cycles
;
3930 atomic_read_relaxed(&rx
->cycles
[type
], &processing_cycles
);
3931 return processing_cycles
;
3935 dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq
*rx
,
3936 unsigned long long cycles
)
3938 unsigned int idx
= rx
->intrvl_idx
++ % PMD_RXQ_INTERVAL_MAX
;
3939 atomic_store_relaxed(&rx
->cycles_intrvl
[idx
], cycles
);
3943 dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq
*rx
, unsigned idx
)
3945 unsigned long long processing_cycles
;
3946 atomic_read_relaxed(&rx
->cycles_intrvl
[idx
], &processing_cycles
);
3947 return processing_cycles
;
3950 #if ATOMIC_ALWAYS_LOCK_FREE_8B
3952 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread
*pmd
)
3954 bool pmd_perf_enabled
;
3955 atomic_read_relaxed(&pmd
->dp
->pmd_perf_metrics
, &pmd_perf_enabled
);
3956 return pmd_perf_enabled
;
3959 /* If stores and reads of 64-bit integers are not atomic, the full PMD
3960 * performance metrics are not available as locked access to 64 bit
3961 * integers would be prohibitively expensive. */
3963 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread
*pmd OVS_UNUSED
)
3970 dp_netdev_pmd_flush_output_on_port(struct dp_netdev_pmd_thread
*pmd
,
3977 struct cycle_timer timer
;
3979 uint32_t tx_flush_interval
;
3981 cycle_timer_start(&pmd
->perf_stats
, &timer
);
3983 dynamic_txqs
= p
->port
->dynamic_txqs
;
3985 tx_qid
= dpif_netdev_xps_get_tx_qid(pmd
, p
);
3987 tx_qid
= pmd
->static_tx_qid
;
3990 output_cnt
= dp_packet_batch_size(&p
->output_pkts
);
3991 ovs_assert(output_cnt
> 0);
3993 netdev_send(p
->port
->netdev
, tx_qid
, &p
->output_pkts
, dynamic_txqs
);
3994 dp_packet_batch_init(&p
->output_pkts
);
3996 /* Update time of the next flush. */
3997 atomic_read_relaxed(&pmd
->dp
->tx_flush_interval
, &tx_flush_interval
);
3998 p
->flush_time
= pmd
->ctx
.now
+ tx_flush_interval
;
4000 ovs_assert(pmd
->n_output_batches
> 0);
4001 pmd
->n_output_batches
--;
4003 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_SENT_PKTS
, output_cnt
);
4004 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_SENT_BATCHES
, 1);
4006 /* Distribute send cycles evenly among transmitted packets and assign to
4007 * their respective rx queues. */
4008 cycles
= cycle_timer_stop(&pmd
->perf_stats
, &timer
) / output_cnt
;
4009 for (i
= 0; i
< output_cnt
; i
++) {
4010 if (p
->output_pkts_rxqs
[i
]) {
4011 dp_netdev_rxq_add_cycles(p
->output_pkts_rxqs
[i
],
4012 RXQ_CYCLES_PROC_CURR
, cycles
);
4020 dp_netdev_pmd_flush_output_packets(struct dp_netdev_pmd_thread
*pmd
,
4026 if (!pmd
->n_output_batches
) {
4030 HMAP_FOR_EACH (p
, node
, &pmd
->send_port_cache
) {
4031 if (!dp_packet_batch_is_empty(&p
->output_pkts
)
4032 && (force
|| pmd
->ctx
.now
>= p
->flush_time
)) {
4033 output_cnt
+= dp_netdev_pmd_flush_output_on_port(pmd
, p
);
4040 dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread
*pmd
,
4041 struct dp_netdev_rxq
*rxq
,
4044 struct pmd_perf_stats
*s
= &pmd
->perf_stats
;
4045 struct dp_packet_batch batch
;
4046 struct cycle_timer timer
;
4049 int rem_qlen
= 0, *qlen_p
= NULL
;
4052 /* Measure duration for polling and processing rx burst. */
4053 cycle_timer_start(&pmd
->perf_stats
, &timer
);
4055 pmd
->ctx
.last_rxq
= rxq
;
4056 dp_packet_batch_init(&batch
);
4058 /* Fetch the rx queue length only for vhostuser ports. */
4059 if (pmd_perf_metrics_enabled(pmd
) && rxq
->is_vhost
) {
4063 error
= netdev_rxq_recv(rxq
->rx
, &batch
, qlen_p
);
4065 /* At least one packet received. */
4066 *recirc_depth_get() = 0;
4067 pmd_thread_ctx_time_update(pmd
);
4068 batch_cnt
= batch
.count
;
4069 if (pmd_perf_metrics_enabled(pmd
)) {
4070 /* Update batch histogram. */
4071 s
->current
.batches
++;
4072 histogram_add_sample(&s
->pkts_per_batch
, batch_cnt
);
4073 /* Update the maximum vhost rx queue fill level. */
4074 if (rxq
->is_vhost
&& rem_qlen
>= 0) {
4075 uint32_t qfill
= batch_cnt
+ rem_qlen
;
4076 if (qfill
> s
->current
.max_vhost_qfill
) {
4077 s
->current
.max_vhost_qfill
= qfill
;
4081 /* Process packet batch. */
4082 dp_netdev_input(pmd
, &batch
, port_no
);
4084 /* Assign processing cycles to rx queue. */
4085 cycles
= cycle_timer_stop(&pmd
->perf_stats
, &timer
);
4086 dp_netdev_rxq_add_cycles(rxq
, RXQ_CYCLES_PROC_CURR
, cycles
);
4088 dp_netdev_pmd_flush_output_packets(pmd
, false);
4090 /* Discard cycles. */
4091 cycle_timer_stop(&pmd
->perf_stats
, &timer
);
4092 if (error
!= EAGAIN
&& error
!= EOPNOTSUPP
) {
4093 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4095 VLOG_ERR_RL(&rl
, "error receiving data from %s: %s",
4096 netdev_rxq_get_name(rxq
->rx
), ovs_strerror(error
));
4100 pmd
->ctx
.last_rxq
= NULL
;
4105 static struct tx_port
*
4106 tx_port_lookup(const struct hmap
*hmap
, odp_port_t port_no
)
4110 HMAP_FOR_EACH_IN_BUCKET (tx
, node
, hash_port_no(port_no
), hmap
) {
4111 if (tx
->port
->port_no
== port_no
) {
4120 port_reconfigure(struct dp_netdev_port
*port
)
4122 struct netdev
*netdev
= port
->netdev
;
4125 /* Closes the existing 'rxq's. */
4126 for (i
= 0; i
< port
->n_rxq
; i
++) {
4127 netdev_rxq_close(port
->rxqs
[i
].rx
);
4128 port
->rxqs
[i
].rx
= NULL
;
4130 unsigned last_nrxq
= port
->n_rxq
;
4133 /* Allows 'netdev' to apply the pending configuration changes. */
4134 if (netdev_is_reconf_required(netdev
) || port
->need_reconfigure
) {
4135 err
= netdev_reconfigure(netdev
);
4136 if (err
&& (err
!= EOPNOTSUPP
)) {
4137 VLOG_ERR("Failed to set interface %s new configuration",
4138 netdev_get_name(netdev
));
4142 /* If the netdev_reconfigure() above succeeds, reopens the 'rxq's. */
4143 port
->rxqs
= xrealloc(port
->rxqs
,
4144 sizeof *port
->rxqs
* netdev_n_rxq(netdev
));
4145 /* Realloc 'used' counters for tx queues. */
4146 free(port
->txq_used
);
4147 port
->txq_used
= xcalloc(netdev_n_txq(netdev
), sizeof *port
->txq_used
);
4149 for (i
= 0; i
< netdev_n_rxq(netdev
); i
++) {
4150 bool new_queue
= i
>= last_nrxq
;
4152 memset(&port
->rxqs
[i
], 0, sizeof port
->rxqs
[i
]);
4155 port
->rxqs
[i
].port
= port
;
4156 port
->rxqs
[i
].is_vhost
= !strncmp(port
->type
, "dpdkvhost", 9);
4158 err
= netdev_rxq_open(netdev
, &port
->rxqs
[i
].rx
, i
);
4165 /* Parse affinity list to apply configuration for new queues. */
4166 dpif_netdev_port_set_rxq_affinity(port
, port
->rxq_affinity_list
);
4168 /* If reconfiguration was successful mark it as such, so we can use it */
4169 port
->need_reconfigure
= false;
4174 struct rr_numa_list
{
4175 struct hmap numas
; /* Contains 'struct rr_numa' */
4179 struct hmap_node node
;
4183 /* Non isolated pmds on numa node 'numa_id' */
4184 struct dp_netdev_pmd_thread
**pmds
;
4191 static struct rr_numa
*
4192 rr_numa_list_lookup(struct rr_numa_list
*rr
, int numa_id
)
4194 struct rr_numa
*numa
;
4196 HMAP_FOR_EACH_WITH_HASH (numa
, node
, hash_int(numa_id
, 0), &rr
->numas
) {
4197 if (numa
->numa_id
== numa_id
) {
4205 /* Returns the next node in numa list following 'numa' in round-robin fashion.
4206 * Returns first node if 'numa' is a null pointer or the last node in 'rr'.
4207 * Returns NULL if 'rr' numa list is empty. */
4208 static struct rr_numa
*
4209 rr_numa_list_next(struct rr_numa_list
*rr
, const struct rr_numa
*numa
)
4211 struct hmap_node
*node
= NULL
;
4214 node
= hmap_next(&rr
->numas
, &numa
->node
);
4217 node
= hmap_first(&rr
->numas
);
4220 return (node
) ? CONTAINER_OF(node
, struct rr_numa
, node
) : NULL
;
4224 rr_numa_list_populate(struct dp_netdev
*dp
, struct rr_numa_list
*rr
)
4226 struct dp_netdev_pmd_thread
*pmd
;
4227 struct rr_numa
*numa
;
4229 hmap_init(&rr
->numas
);
4231 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4232 if (pmd
->core_id
== NON_PMD_CORE_ID
|| pmd
->isolated
) {
4236 numa
= rr_numa_list_lookup(rr
, pmd
->numa_id
);
4238 numa
= xzalloc(sizeof *numa
);
4239 numa
->numa_id
= pmd
->numa_id
;
4240 hmap_insert(&rr
->numas
, &numa
->node
, hash_int(pmd
->numa_id
, 0));
4243 numa
->pmds
= xrealloc(numa
->pmds
, numa
->n_pmds
* sizeof *numa
->pmds
);
4244 numa
->pmds
[numa
->n_pmds
- 1] = pmd
;
4245 /* At least one pmd so initialise curr_idx and idx_inc. */
4246 numa
->cur_index
= 0;
4247 numa
->idx_inc
= true;
4251 /* Returns the next pmd from the numa node in
4252 * incrementing or decrementing order. */
4253 static struct dp_netdev_pmd_thread
*
4254 rr_numa_get_pmd(struct rr_numa
*numa
)
4256 int numa_idx
= numa
->cur_index
;
4258 if (numa
->idx_inc
== true) {
4259 /* Incrementing through list of pmds. */
4260 if (numa
->cur_index
== numa
->n_pmds
-1) {
4261 /* Reached the last pmd. */
4262 numa
->idx_inc
= false;
4267 /* Decrementing through list of pmds. */
4268 if (numa
->cur_index
== 0) {
4269 /* Reached the first pmd. */
4270 numa
->idx_inc
= true;
4275 return numa
->pmds
[numa_idx
];
4279 rr_numa_list_destroy(struct rr_numa_list
*rr
)
4281 struct rr_numa
*numa
;
4283 HMAP_FOR_EACH_POP (numa
, node
, &rr
->numas
) {
4287 hmap_destroy(&rr
->numas
);
4290 /* Sort Rx Queues by the processing cycles they are consuming. */
4292 compare_rxq_cycles(const void *a
, const void *b
)
4294 struct dp_netdev_rxq
*qa
;
4295 struct dp_netdev_rxq
*qb
;
4296 uint64_t cycles_qa
, cycles_qb
;
4298 qa
= *(struct dp_netdev_rxq
**) a
;
4299 qb
= *(struct dp_netdev_rxq
**) b
;
4301 cycles_qa
= dp_netdev_rxq_get_cycles(qa
, RXQ_CYCLES_PROC_HIST
);
4302 cycles_qb
= dp_netdev_rxq_get_cycles(qb
, RXQ_CYCLES_PROC_HIST
);
4304 if (cycles_qa
!= cycles_qb
) {
4305 return (cycles_qa
< cycles_qb
) ? 1 : -1;
4307 /* Cycles are the same so tiebreak on port/queue id.
4308 * Tiebreaking (as opposed to return 0) ensures consistent
4309 * sort results across multiple OS's. */
4310 uint32_t port_qa
= odp_to_u32(qa
->port
->port_no
);
4311 uint32_t port_qb
= odp_to_u32(qb
->port
->port_no
);
4312 if (port_qa
!= port_qb
) {
4313 return port_qa
> port_qb
? 1 : -1;
4315 return netdev_rxq_get_queue_id(qa
->rx
)
4316 - netdev_rxq_get_queue_id(qb
->rx
);
4321 /* Assign pmds to queues. If 'pinned' is true, assign pmds to pinned
4322 * queues and marks the pmds as isolated. Otherwise, assign non isolated
4323 * pmds to unpinned queues.
4325 * If 'pinned' is false queues will be sorted by processing cycles they are
4326 * consuming and then assigned to pmds in round robin order.
4328 * The function doesn't touch the pmd threads, it just stores the assignment
4329 * in the 'pmd' member of each rxq. */
4331 rxq_scheduling(struct dp_netdev
*dp
, bool pinned
) OVS_REQUIRES(dp
->port_mutex
)
4333 struct dp_netdev_port
*port
;
4334 struct rr_numa_list rr
;
4335 struct rr_numa
*non_local_numa
= NULL
;
4336 struct dp_netdev_rxq
** rxqs
= NULL
;
4338 struct rr_numa
*numa
= NULL
;
4341 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4342 if (!netdev_is_pmd(port
->netdev
)) {
4346 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
4347 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
4349 if (pinned
&& q
->core_id
!= OVS_CORE_UNSPEC
) {
4350 struct dp_netdev_pmd_thread
*pmd
;
4352 pmd
= dp_netdev_get_pmd(dp
, q
->core_id
);
4354 VLOG_WARN("There is no PMD thread on core %d. Queue "
4355 "%d on port \'%s\' will not be polled.",
4356 q
->core_id
, qid
, netdev_get_name(port
->netdev
));
4359 pmd
->isolated
= true;
4360 dp_netdev_pmd_unref(pmd
);
4362 } else if (!pinned
&& q
->core_id
== OVS_CORE_UNSPEC
) {
4363 uint64_t cycle_hist
= 0;
4366 rxqs
= xmalloc(sizeof *rxqs
);
4368 rxqs
= xrealloc(rxqs
, sizeof *rxqs
* (n_rxqs
+ 1));
4370 /* Sum the queue intervals and store the cycle history. */
4371 for (unsigned i
= 0; i
< PMD_RXQ_INTERVAL_MAX
; i
++) {
4372 cycle_hist
+= dp_netdev_rxq_get_intrvl_cycles(q
, i
);
4374 dp_netdev_rxq_set_cycles(q
, RXQ_CYCLES_PROC_HIST
, cycle_hist
);
4376 /* Store the queue. */
4383 /* Sort the queues in order of the processing cycles
4384 * they consumed during their last pmd interval. */
4385 qsort(rxqs
, n_rxqs
, sizeof *rxqs
, compare_rxq_cycles
);
4388 rr_numa_list_populate(dp
, &rr
);
4389 /* Assign the sorted queues to pmds in round robin. */
4390 for (int i
= 0; i
< n_rxqs
; i
++) {
4391 numa_id
= netdev_get_numa_id(rxqs
[i
]->port
->netdev
);
4392 numa
= rr_numa_list_lookup(&rr
, numa_id
);
4394 /* There are no pmds on the queue's local NUMA node.
4395 Round robin on the NUMA nodes that do have pmds. */
4396 non_local_numa
= rr_numa_list_next(&rr
, non_local_numa
);
4397 if (!non_local_numa
) {
4398 VLOG_ERR("There is no available (non-isolated) pmd "
4399 "thread for port \'%s\' queue %d. This queue "
4400 "will not be polled. Is pmd-cpu-mask set to "
4401 "zero? Or are all PMDs isolated to other "
4402 "queues?", netdev_rxq_get_name(rxqs
[i
]->rx
),
4403 netdev_rxq_get_queue_id(rxqs
[i
]->rx
));
4406 rxqs
[i
]->pmd
= rr_numa_get_pmd(non_local_numa
);
4407 VLOG_WARN("There's no available (non-isolated) pmd thread "
4408 "on numa node %d. Queue %d on port \'%s\' will "
4409 "be assigned to the pmd on core %d "
4410 "(numa node %d). Expect reduced performance.",
4411 numa_id
, netdev_rxq_get_queue_id(rxqs
[i
]->rx
),
4412 netdev_rxq_get_name(rxqs
[i
]->rx
),
4413 rxqs
[i
]->pmd
->core_id
, rxqs
[i
]->pmd
->numa_id
);
4415 rxqs
[i
]->pmd
= rr_numa_get_pmd(numa
);
4416 VLOG_INFO("Core %d on numa node %d assigned port \'%s\' "
4417 "rx queue %d (measured processing cycles %"PRIu64
").",
4418 rxqs
[i
]->pmd
->core_id
, numa_id
,
4419 netdev_rxq_get_name(rxqs
[i
]->rx
),
4420 netdev_rxq_get_queue_id(rxqs
[i
]->rx
),
4421 dp_netdev_rxq_get_cycles(rxqs
[i
], RXQ_CYCLES_PROC_HIST
));
4425 rr_numa_list_destroy(&rr
);
4430 reload_affected_pmds(struct dp_netdev
*dp
)
4432 struct dp_netdev_pmd_thread
*pmd
;
4434 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4435 if (pmd
->need_reload
) {
4436 flow_mark_flush(pmd
);
4437 dp_netdev_reload_pmd__(pmd
);
4438 pmd
->need_reload
= false;
4444 reconfigure_pmd_threads(struct dp_netdev
*dp
)
4445 OVS_REQUIRES(dp
->port_mutex
)
4447 struct dp_netdev_pmd_thread
*pmd
;
4448 struct ovs_numa_dump
*pmd_cores
;
4449 struct ovs_numa_info_core
*core
;
4450 struct hmapx to_delete
= HMAPX_INITIALIZER(&to_delete
);
4451 struct hmapx_node
*node
;
4452 bool changed
= false;
4453 bool need_to_adjust_static_tx_qids
= false;
4455 /* The pmd threads should be started only if there's a pmd port in the
4456 * datapath. If the user didn't provide any "pmd-cpu-mask", we start
4457 * NR_PMD_THREADS per numa node. */
4458 if (!has_pmd_port(dp
)) {
4459 pmd_cores
= ovs_numa_dump_n_cores_per_numa(0);
4460 } else if (dp
->pmd_cmask
&& dp
->pmd_cmask
[0]) {
4461 pmd_cores
= ovs_numa_dump_cores_with_cmask(dp
->pmd_cmask
);
4463 pmd_cores
= ovs_numa_dump_n_cores_per_numa(NR_PMD_THREADS
);
4466 /* We need to adjust 'static_tx_qid's only if we're reducing number of
4467 * PMD threads. Otherwise, new threads will allocate all the freed ids. */
4468 if (ovs_numa_dump_count(pmd_cores
) < cmap_count(&dp
->poll_threads
) - 1) {
4469 /* Adjustment is required to keep 'static_tx_qid's sequential and
4470 * avoid possible issues, for example, imbalanced tx queue usage
4471 * and unnecessary locking caused by remapping on netdev level. */
4472 need_to_adjust_static_tx_qids
= true;
4475 /* Check for unwanted pmd threads */
4476 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4477 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
4480 if (!ovs_numa_dump_contains_core(pmd_cores
, pmd
->numa_id
,
4482 hmapx_add(&to_delete
, pmd
);
4483 } else if (need_to_adjust_static_tx_qids
) {
4484 pmd
->need_reload
= true;
4488 HMAPX_FOR_EACH (node
, &to_delete
) {
4489 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
4490 VLOG_INFO("PMD thread on numa_id: %d, core id: %2d destroyed.",
4491 pmd
->numa_id
, pmd
->core_id
);
4492 dp_netdev_del_pmd(dp
, pmd
);
4494 changed
= !hmapx_is_empty(&to_delete
);
4495 hmapx_destroy(&to_delete
);
4497 if (need_to_adjust_static_tx_qids
) {
4498 /* 'static_tx_qid's are not sequential now.
4499 * Reload remaining threads to fix this. */
4500 reload_affected_pmds(dp
);
4503 /* Check for required new pmd threads */
4504 FOR_EACH_CORE_ON_DUMP(core
, pmd_cores
) {
4505 pmd
= dp_netdev_get_pmd(dp
, core
->core_id
);
4507 pmd
= xzalloc(sizeof *pmd
);
4508 dp_netdev_configure_pmd(pmd
, dp
, core
->core_id
, core
->numa_id
);
4509 pmd
->thread
= ovs_thread_create("pmd", pmd_thread_main
, pmd
);
4510 VLOG_INFO("PMD thread on numa_id: %d, core id: %2d created.",
4511 pmd
->numa_id
, pmd
->core_id
);
4514 dp_netdev_pmd_unref(pmd
);
4519 struct ovs_numa_info_numa
*numa
;
4521 /* Log the number of pmd threads per numa node. */
4522 FOR_EACH_NUMA_ON_DUMP (numa
, pmd_cores
) {
4523 VLOG_INFO("There are %"PRIuSIZE
" pmd threads on numa node %d",
4524 numa
->n_cores
, numa
->numa_id
);
4528 ovs_numa_dump_destroy(pmd_cores
);
4532 pmd_remove_stale_ports(struct dp_netdev
*dp
,
4533 struct dp_netdev_pmd_thread
*pmd
)
4534 OVS_EXCLUDED(pmd
->port_mutex
)
4535 OVS_REQUIRES(dp
->port_mutex
)
4537 struct rxq_poll
*poll
, *poll_next
;
4538 struct tx_port
*tx
, *tx_next
;
4540 ovs_mutex_lock(&pmd
->port_mutex
);
4541 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
4542 struct dp_netdev_port
*port
= poll
->rxq
->port
;
4544 if (port
->need_reconfigure
4545 || !hmap_contains(&dp
->ports
, &port
->node
)) {
4546 dp_netdev_del_rxq_from_pmd(pmd
, poll
);
4549 HMAP_FOR_EACH_SAFE (tx
, tx_next
, node
, &pmd
->tx_ports
) {
4550 struct dp_netdev_port
*port
= tx
->port
;
4552 if (port
->need_reconfigure
4553 || !hmap_contains(&dp
->ports
, &port
->node
)) {
4554 dp_netdev_del_port_tx_from_pmd(pmd
, tx
);
4557 ovs_mutex_unlock(&pmd
->port_mutex
);
4560 /* Must be called each time a port is added/removed or the cmask changes.
4561 * This creates and destroys pmd threads, reconfigures ports, opens their
4562 * rxqs and assigns all rxqs/txqs to pmd threads. */
4564 reconfigure_datapath(struct dp_netdev
*dp
)
4565 OVS_REQUIRES(dp
->port_mutex
)
4567 struct dp_netdev_pmd_thread
*pmd
;
4568 struct dp_netdev_port
*port
;
4571 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
4573 /* Step 1: Adjust the pmd threads based on the datapath ports, the cores
4574 * on the system and the user configuration. */
4575 reconfigure_pmd_threads(dp
);
4577 wanted_txqs
= cmap_count(&dp
->poll_threads
);
4579 /* The number of pmd threads might have changed, or a port can be new:
4580 * adjust the txqs. */
4581 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4582 netdev_set_tx_multiq(port
->netdev
, wanted_txqs
);
4585 /* Step 2: Remove from the pmd threads ports that have been removed or
4586 * need reconfiguration. */
4588 /* Check for all the ports that need reconfiguration. We cache this in
4589 * 'port->need_reconfigure', because netdev_is_reconf_required() can
4590 * change at any time. */
4591 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4592 if (netdev_is_reconf_required(port
->netdev
)) {
4593 port
->need_reconfigure
= true;
4597 /* Remove from the pmd threads all the ports that have been deleted or
4598 * need reconfiguration. */
4599 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4600 pmd_remove_stale_ports(dp
, pmd
);
4603 /* Reload affected pmd threads. We must wait for the pmd threads before
4604 * reconfiguring the ports, because a port cannot be reconfigured while
4605 * it's being used. */
4606 reload_affected_pmds(dp
);
4608 /* Step 3: Reconfigure ports. */
4610 /* We only reconfigure the ports that we determined above, because they're
4611 * not being used by any pmd thread at the moment. If a port fails to
4612 * reconfigure we remove it from the datapath. */
4613 struct dp_netdev_port
*next_port
;
4614 HMAP_FOR_EACH_SAFE (port
, next_port
, node
, &dp
->ports
) {
4617 if (!port
->need_reconfigure
) {
4621 err
= port_reconfigure(port
);
4623 hmap_remove(&dp
->ports
, &port
->node
);
4624 seq_change(dp
->port_seq
);
4627 port
->dynamic_txqs
= netdev_n_txq(port
->netdev
) < wanted_txqs
;
4631 /* Step 4: Compute new rxq scheduling. We don't touch the pmd threads
4632 * for now, we just update the 'pmd' pointer in each rxq to point to the
4633 * wanted thread according to the scheduling policy. */
4635 /* Reset all the pmd threads to non isolated. */
4636 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4637 pmd
->isolated
= false;
4640 /* Reset all the queues to unassigned */
4641 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4642 for (int i
= 0; i
< port
->n_rxq
; i
++) {
4643 port
->rxqs
[i
].pmd
= NULL
;
4647 /* Add pinned queues and mark pmd threads isolated. */
4648 rxq_scheduling(dp
, true);
4650 /* Add non-pinned queues. */
4651 rxq_scheduling(dp
, false);
4653 /* Step 5: Remove queues not compliant with new scheduling. */
4654 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4655 struct rxq_poll
*poll
, *poll_next
;
4657 ovs_mutex_lock(&pmd
->port_mutex
);
4658 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
4659 if (poll
->rxq
->pmd
!= pmd
) {
4660 dp_netdev_del_rxq_from_pmd(pmd
, poll
);
4663 ovs_mutex_unlock(&pmd
->port_mutex
);
4666 /* Reload affected pmd threads. We must wait for the pmd threads to remove
4667 * the old queues before readding them, otherwise a queue can be polled by
4668 * two threads at the same time. */
4669 reload_affected_pmds(dp
);
4671 /* Step 6: Add queues from scheduling, if they're not there already. */
4672 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4673 if (!netdev_is_pmd(port
->netdev
)) {
4677 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
4678 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
4681 ovs_mutex_lock(&q
->pmd
->port_mutex
);
4682 dp_netdev_add_rxq_to_pmd(q
->pmd
, q
);
4683 ovs_mutex_unlock(&q
->pmd
->port_mutex
);
4688 /* Add every port to the tx cache of every pmd thread, if it's not
4689 * there already and if this pmd has at least one rxq to poll. */
4690 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4691 ovs_mutex_lock(&pmd
->port_mutex
);
4692 if (hmap_count(&pmd
->poll_list
) || pmd
->core_id
== NON_PMD_CORE_ID
) {
4693 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4694 dp_netdev_add_port_tx_to_pmd(pmd
, port
);
4697 ovs_mutex_unlock(&pmd
->port_mutex
);
4700 /* Reload affected pmd threads. */
4701 reload_affected_pmds(dp
);
4704 /* Returns true if one of the netdevs in 'dp' requires a reconfiguration */
4706 ports_require_restart(const struct dp_netdev
*dp
)
4707 OVS_REQUIRES(dp
->port_mutex
)
4709 struct dp_netdev_port
*port
;
4711 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4712 if (netdev_is_reconf_required(port
->netdev
)) {
4720 /* Return true if needs to revalidate datapath flows. */
4722 dpif_netdev_run(struct dpif
*dpif
)
4724 struct dp_netdev_port
*port
;
4725 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4726 struct dp_netdev_pmd_thread
*non_pmd
;
4727 uint64_t new_tnl_seq
;
4728 bool need_to_flush
= true;
4730 ovs_mutex_lock(&dp
->port_mutex
);
4731 non_pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
4733 ovs_mutex_lock(&dp
->non_pmd_mutex
);
4734 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4735 if (!netdev_is_pmd(port
->netdev
)) {
4738 for (i
= 0; i
< port
->n_rxq
; i
++) {
4739 if (dp_netdev_process_rxq_port(non_pmd
,
4742 need_to_flush
= false;
4747 if (need_to_flush
) {
4748 /* We didn't receive anything in the process loop.
4749 * Check if we need to send something.
4750 * There was no time updates on current iteration. */
4751 pmd_thread_ctx_time_update(non_pmd
);
4752 dp_netdev_pmd_flush_output_packets(non_pmd
, false);
4755 dpif_netdev_xps_revalidate_pmd(non_pmd
, false);
4756 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
4758 dp_netdev_pmd_unref(non_pmd
);
4761 if (dp_netdev_is_reconf_required(dp
) || ports_require_restart(dp
)) {
4762 reconfigure_datapath(dp
);
4764 ovs_mutex_unlock(&dp
->port_mutex
);
4766 tnl_neigh_cache_run();
4768 new_tnl_seq
= seq_read(tnl_conf_seq
);
4770 if (dp
->last_tnl_conf_seq
!= new_tnl_seq
) {
4771 dp
->last_tnl_conf_seq
= new_tnl_seq
;
4778 dpif_netdev_wait(struct dpif
*dpif
)
4780 struct dp_netdev_port
*port
;
4781 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4783 ovs_mutex_lock(&dp_netdev_mutex
);
4784 ovs_mutex_lock(&dp
->port_mutex
);
4785 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4786 netdev_wait_reconf_required(port
->netdev
);
4787 if (!netdev_is_pmd(port
->netdev
)) {
4790 for (i
= 0; i
< port
->n_rxq
; i
++) {
4791 netdev_rxq_wait(port
->rxqs
[i
].rx
);
4795 ovs_mutex_unlock(&dp
->port_mutex
);
4796 ovs_mutex_unlock(&dp_netdev_mutex
);
4797 seq_wait(tnl_conf_seq
, dp
->last_tnl_conf_seq
);
4801 pmd_free_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
4803 struct tx_port
*tx_port_cached
;
4805 /* Flush all the queued packets. */
4806 dp_netdev_pmd_flush_output_packets(pmd
, true);
4807 /* Free all used tx queue ids. */
4808 dpif_netdev_xps_revalidate_pmd(pmd
, true);
4810 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->tnl_port_cache
) {
4811 free(tx_port_cached
);
4813 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->send_port_cache
) {
4814 free(tx_port_cached
);
4818 /* Copies ports from 'pmd->tx_ports' (shared with the main thread) to
4819 * thread-local copies. Copy to 'pmd->tnl_port_cache' if it is a tunnel
4820 * device, otherwise to 'pmd->send_port_cache' if the port has at least
4823 pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
4824 OVS_REQUIRES(pmd
->port_mutex
)
4826 struct tx_port
*tx_port
, *tx_port_cached
;
4828 pmd_free_cached_ports(pmd
);
4829 hmap_shrink(&pmd
->send_port_cache
);
4830 hmap_shrink(&pmd
->tnl_port_cache
);
4832 HMAP_FOR_EACH (tx_port
, node
, &pmd
->tx_ports
) {
4833 if (netdev_has_tunnel_push_pop(tx_port
->port
->netdev
)) {
4834 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
4835 hmap_insert(&pmd
->tnl_port_cache
, &tx_port_cached
->node
,
4836 hash_port_no(tx_port_cached
->port
->port_no
));
4839 if (netdev_n_txq(tx_port
->port
->netdev
)) {
4840 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
4841 hmap_insert(&pmd
->send_port_cache
, &tx_port_cached
->node
,
4842 hash_port_no(tx_port_cached
->port
->port_no
));
4848 pmd_alloc_static_tx_qid(struct dp_netdev_pmd_thread
*pmd
)
4850 ovs_mutex_lock(&pmd
->dp
->tx_qid_pool_mutex
);
4851 if (!id_pool_alloc_id(pmd
->dp
->tx_qid_pool
, &pmd
->static_tx_qid
)) {
4852 VLOG_ABORT("static_tx_qid allocation failed for PMD on core %2d"
4853 ", numa_id %d.", pmd
->core_id
, pmd
->numa_id
);
4855 ovs_mutex_unlock(&pmd
->dp
->tx_qid_pool_mutex
);
4857 VLOG_DBG("static_tx_qid = %d allocated for PMD thread on core %2d"
4858 ", numa_id %d.", pmd
->static_tx_qid
, pmd
->core_id
, pmd
->numa_id
);
4862 pmd_free_static_tx_qid(struct dp_netdev_pmd_thread
*pmd
)
4864 ovs_mutex_lock(&pmd
->dp
->tx_qid_pool_mutex
);
4865 id_pool_free_id(pmd
->dp
->tx_qid_pool
, pmd
->static_tx_qid
);
4866 ovs_mutex_unlock(&pmd
->dp
->tx_qid_pool_mutex
);
4870 pmd_load_queues_and_ports(struct dp_netdev_pmd_thread
*pmd
,
4871 struct polled_queue
**ppoll_list
)
4873 struct polled_queue
*poll_list
= *ppoll_list
;
4874 struct rxq_poll
*poll
;
4877 ovs_mutex_lock(&pmd
->port_mutex
);
4878 poll_list
= xrealloc(poll_list
, hmap_count(&pmd
->poll_list
)
4879 * sizeof *poll_list
);
4882 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
4883 poll_list
[i
].rxq
= poll
->rxq
;
4884 poll_list
[i
].port_no
= poll
->rxq
->port
->port_no
;
4888 pmd_load_cached_ports(pmd
);
4890 ovs_mutex_unlock(&pmd
->port_mutex
);
4892 *ppoll_list
= poll_list
;
4897 pmd_thread_main(void *f_
)
4899 struct dp_netdev_pmd_thread
*pmd
= f_
;
4900 struct pmd_perf_stats
*s
= &pmd
->perf_stats
;
4901 unsigned int lc
= 0;
4902 struct polled_queue
*poll_list
;
4906 int process_packets
= 0;
4910 /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */
4911 ovsthread_setspecific(pmd
->dp
->per_pmd_key
, pmd
);
4912 ovs_numa_thread_setaffinity_core(pmd
->core_id
);
4913 dpdk_set_lcore_id(pmd
->core_id
);
4914 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
4915 dfc_cache_init(&pmd
->flow_cache
);
4917 pmd_alloc_static_tx_qid(pmd
);
4919 /* List port/core affinity */
4920 for (i
= 0; i
< poll_cnt
; i
++) {
4921 VLOG_DBG("Core %d processing port \'%s\' with queue-id %d\n",
4922 pmd
->core_id
, netdev_rxq_get_name(poll_list
[i
].rxq
->rx
),
4923 netdev_rxq_get_queue_id(poll_list
[i
].rxq
->rx
));
4924 /* Reset the rxq current cycles counter. */
4925 dp_netdev_rxq_set_cycles(poll_list
[i
].rxq
, RXQ_CYCLES_PROC_CURR
, 0);
4929 while (seq_read(pmd
->reload_seq
) == pmd
->last_reload_seq
) {
4930 seq_wait(pmd
->reload_seq
, pmd
->last_reload_seq
);
4936 pmd
->intrvl_tsc_prev
= 0;
4937 atomic_store_relaxed(&pmd
->intrvl_cycles
, 0);
4938 cycles_counter_update(s
);
4939 /* Protect pmd stats from external clearing while polling. */
4940 ovs_mutex_lock(&pmd
->perf_stats
.stats_mutex
);
4942 uint64_t rx_packets
= 0, tx_packets
= 0;
4944 pmd_perf_start_iteration(s
);
4946 for (i
= 0; i
< poll_cnt
; i
++) {
4948 dp_netdev_process_rxq_port(pmd
, poll_list
[i
].rxq
,
4949 poll_list
[i
].port_no
);
4950 rx_packets
+= process_packets
;
4954 /* We didn't receive anything in the process loop.
4955 * Check if we need to send something.
4956 * There was no time updates on current iteration. */
4957 pmd_thread_ctx_time_update(pmd
);
4958 tx_packets
= dp_netdev_pmd_flush_output_packets(pmd
, false);
4966 coverage_try_clear();
4967 dp_netdev_pmd_try_optimize(pmd
, poll_list
, poll_cnt
);
4968 if (!ovsrcu_try_quiesce()) {
4969 emc_cache_slow_sweep(&((pmd
->flow_cache
).emc_cache
));
4972 atomic_read_relaxed(&pmd
->reload
, &reload
);
4977 pmd_perf_end_iteration(s
, rx_packets
, tx_packets
,
4978 pmd_perf_metrics_enabled(pmd
));
4980 ovs_mutex_unlock(&pmd
->perf_stats
.stats_mutex
);
4982 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
4983 exiting
= latch_is_set(&pmd
->exit_latch
);
4984 /* Signal here to make sure the pmd finishes
4985 * reloading the updated configuration. */
4986 dp_netdev_pmd_reload_done(pmd
);
4988 pmd_free_static_tx_qid(pmd
);
4994 dfc_cache_uninit(&pmd
->flow_cache
);
4996 pmd_free_cached_ports(pmd
);
5001 dp_netdev_disable_upcall(struct dp_netdev
*dp
)
5002 OVS_ACQUIRES(dp
->upcall_rwlock
)
5004 fat_rwlock_wrlock(&dp
->upcall_rwlock
);
5010 dpif_netdev_meter_get_features(const struct dpif
* dpif OVS_UNUSED
,
5011 struct ofputil_meter_features
*features
)
5013 features
->max_meters
= MAX_METERS
;
5014 features
->band_types
= DP_SUPPORTED_METER_BAND_TYPES
;
5015 features
->capabilities
= DP_SUPPORTED_METER_FLAGS_MASK
;
5016 features
->max_bands
= MAX_BANDS
;
5017 features
->max_color
= 0;
5020 /* Applies the meter identified by 'meter_id' to 'packets_'. Packets
5021 * that exceed a band are dropped in-place. */
5023 dp_netdev_run_meter(struct dp_netdev
*dp
, struct dp_packet_batch
*packets_
,
5024 uint32_t meter_id
, long long int now
)
5026 struct dp_meter
*meter
;
5027 struct dp_meter_band
*band
;
5028 struct dp_packet
*packet
;
5029 long long int long_delta_t
; /* msec */
5030 uint32_t delta_t
; /* msec */
5031 const size_t cnt
= dp_packet_batch_size(packets_
);
5032 uint32_t bytes
, volume
;
5033 int exceeded_band
[NETDEV_MAX_BURST
];
5034 uint32_t exceeded_rate
[NETDEV_MAX_BURST
];
5035 int exceeded_pkt
= cnt
; /* First packet that exceeded a band rate. */
5037 if (meter_id
>= MAX_METERS
) {
5041 meter_lock(dp
, meter_id
);
5042 meter
= dp
->meters
[meter_id
];
5047 /* Initialize as negative values. */
5048 memset(exceeded_band
, 0xff, cnt
* sizeof *exceeded_band
);
5049 /* Initialize as zeroes. */
5050 memset(exceeded_rate
, 0, cnt
* sizeof *exceeded_rate
);
5052 /* All packets will hit the meter at the same time. */
5053 long_delta_t
= (now
- meter
->used
) / 1000; /* msec */
5055 /* Make sure delta_t will not be too large, so that bucket will not
5056 * wrap around below. */
5057 delta_t
= (long_delta_t
> (long long int)meter
->max_delta_t
)
5058 ? meter
->max_delta_t
: (uint32_t)long_delta_t
;
5060 /* Update meter stats. */
5062 meter
->packet_count
+= cnt
;
5064 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
5065 bytes
+= dp_packet_size(packet
);
5067 meter
->byte_count
+= bytes
;
5069 /* Meters can operate in terms of packets per second or kilobits per
5071 if (meter
->flags
& OFPMF13_PKTPS
) {
5072 /* Rate in packets/second, bucket 1/1000 packets. */
5073 /* msec * packets/sec = 1/1000 packets. */
5074 volume
= cnt
* 1000; /* Take 'cnt' packets from the bucket. */
5076 /* Rate in kbps, bucket in bits. */
5077 /* msec * kbps = bits */
5081 /* Update all bands and find the one hit with the highest rate for each
5082 * packet (if any). */
5083 for (int m
= 0; m
< meter
->n_bands
; ++m
) {
5084 band
= &meter
->bands
[m
];
5086 /* Update band's bucket. */
5087 band
->bucket
+= delta_t
* band
->up
.rate
;
5088 if (band
->bucket
> band
->up
.burst_size
) {
5089 band
->bucket
= band
->up
.burst_size
;
5092 /* Drain the bucket for all the packets, if possible. */
5093 if (band
->bucket
>= volume
) {
5094 band
->bucket
-= volume
;
5096 int band_exceeded_pkt
;
5098 /* Band limit hit, must process packet-by-packet. */
5099 if (meter
->flags
& OFPMF13_PKTPS
) {
5100 band_exceeded_pkt
= band
->bucket
/ 1000;
5101 band
->bucket
%= 1000; /* Remainder stays in bucket. */
5103 /* Update the exceeding band for each exceeding packet.
5104 * (Only one band will be fired by a packet, and that
5105 * can be different for each packet.) */
5106 for (int i
= band_exceeded_pkt
; i
< cnt
; i
++) {
5107 if (band
->up
.rate
> exceeded_rate
[i
]) {
5108 exceeded_rate
[i
] = band
->up
.rate
;
5109 exceeded_band
[i
] = m
;
5113 /* Packet sizes differ, must process one-by-one. */
5114 band_exceeded_pkt
= cnt
;
5115 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
5116 uint32_t bits
= dp_packet_size(packet
) * 8;
5118 if (band
->bucket
>= bits
) {
5119 band
->bucket
-= bits
;
5121 if (i
< band_exceeded_pkt
) {
5122 band_exceeded_pkt
= i
;
5124 /* Update the exceeding band for the exceeding packet.
5125 * (Only one band will be fired by a packet, and that
5126 * can be different for each packet.) */
5127 if (band
->up
.rate
> exceeded_rate
[i
]) {
5128 exceeded_rate
[i
] = band
->up
.rate
;
5129 exceeded_band
[i
] = m
;
5134 /* Remember the first exceeding packet. */
5135 if (exceeded_pkt
> band_exceeded_pkt
) {
5136 exceeded_pkt
= band_exceeded_pkt
;
5141 /* Fire the highest rate band exceeded by each packet, and drop
5142 * packets if needed. */
5144 DP_PACKET_BATCH_REFILL_FOR_EACH (j
, cnt
, packet
, packets_
) {
5145 if (exceeded_band
[j
] >= 0) {
5146 /* Meter drop packet. */
5147 band
= &meter
->bands
[exceeded_band
[j
]];
5148 band
->packet_count
+= 1;
5149 band
->byte_count
+= dp_packet_size(packet
);
5151 dp_packet_delete(packet
);
5153 /* Meter accepts packet. */
5154 dp_packet_batch_refill(packets_
, packet
, j
);
5158 meter_unlock(dp
, meter_id
);
5161 /* Meter set/get/del processing is still single-threaded. */
5163 dpif_netdev_meter_set(struct dpif
*dpif
, ofproto_meter_id
*meter_id
,
5164 struct ofputil_meter_config
*config
)
5166 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5167 uint32_t mid
= meter_id
->uint32
;
5168 struct dp_meter
*meter
;
5171 if (mid
>= MAX_METERS
) {
5172 return EFBIG
; /* Meter_id out of range. */
5175 if (config
->flags
& ~DP_SUPPORTED_METER_FLAGS_MASK
) {
5176 return EBADF
; /* Unsupported flags set */
5179 if (config
->n_bands
> MAX_BANDS
) {
5183 for (i
= 0; i
< config
->n_bands
; ++i
) {
5184 switch (config
->bands
[i
].type
) {
5188 return ENODEV
; /* Unsupported band type */
5192 /* Allocate meter */
5193 meter
= xzalloc(sizeof *meter
5194 + config
->n_bands
* sizeof(struct dp_meter_band
));
5196 meter
->flags
= config
->flags
;
5197 meter
->n_bands
= config
->n_bands
;
5198 meter
->max_delta_t
= 0;
5199 meter
->used
= time_usec();
5202 for (i
= 0; i
< config
->n_bands
; ++i
) {
5203 uint32_t band_max_delta_t
;
5205 /* Set burst size to a workable value if none specified. */
5206 if (config
->bands
[i
].burst_size
== 0) {
5207 config
->bands
[i
].burst_size
= config
->bands
[i
].rate
;
5210 meter
->bands
[i
].up
= config
->bands
[i
];
5211 /* Convert burst size to the bucket units: */
5212 /* pkts => 1/1000 packets, kilobits => bits. */
5213 meter
->bands
[i
].up
.burst_size
*= 1000;
5214 /* Initialize bucket to empty. */
5215 meter
->bands
[i
].bucket
= 0;
5217 /* Figure out max delta_t that is enough to fill any bucket. */
5219 = meter
->bands
[i
].up
.burst_size
/ meter
->bands
[i
].up
.rate
;
5220 if (band_max_delta_t
> meter
->max_delta_t
) {
5221 meter
->max_delta_t
= band_max_delta_t
;
5225 meter_lock(dp
, mid
);
5226 dp_delete_meter(dp
, mid
); /* Free existing meter, if any */
5227 dp
->meters
[mid
] = meter
;
5228 meter_unlock(dp
, mid
);
5236 dpif_netdev_meter_get(const struct dpif
*dpif
,
5237 ofproto_meter_id meter_id_
,
5238 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
5240 const struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5241 const struct dp_meter
*meter
;
5242 uint32_t meter_id
= meter_id_
.uint32
;
5244 if (meter_id
>= MAX_METERS
) {
5247 meter
= dp
->meters
[meter_id
];
5254 meter_lock(dp
, meter_id
);
5255 stats
->packet_in_count
= meter
->packet_count
;
5256 stats
->byte_in_count
= meter
->byte_count
;
5258 for (i
= 0; i
< n_bands
&& i
< meter
->n_bands
; ++i
) {
5259 stats
->bands
[i
].packet_count
= meter
->bands
[i
].packet_count
;
5260 stats
->bands
[i
].byte_count
= meter
->bands
[i
].byte_count
;
5262 meter_unlock(dp
, meter_id
);
5270 dpif_netdev_meter_del(struct dpif
*dpif
,
5271 ofproto_meter_id meter_id_
,
5272 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
5274 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5277 error
= dpif_netdev_meter_get(dpif
, meter_id_
, stats
, n_bands
);
5279 uint32_t meter_id
= meter_id_
.uint32
;
5281 meter_lock(dp
, meter_id
);
5282 dp_delete_meter(dp
, meter_id
);
5283 meter_unlock(dp
, meter_id
);
5290 dpif_netdev_disable_upcall(struct dpif
*dpif
)
5291 OVS_NO_THREAD_SAFETY_ANALYSIS
5293 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5294 dp_netdev_disable_upcall(dp
);
5298 dp_netdev_enable_upcall(struct dp_netdev
*dp
)
5299 OVS_RELEASES(dp
->upcall_rwlock
)
5301 fat_rwlock_unlock(&dp
->upcall_rwlock
);
5305 dpif_netdev_enable_upcall(struct dpif
*dpif
)
5306 OVS_NO_THREAD_SAFETY_ANALYSIS
5308 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5309 dp_netdev_enable_upcall(dp
);
5313 dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
)
5315 ovs_mutex_lock(&pmd
->cond_mutex
);
5316 atomic_store_relaxed(&pmd
->reload
, false);
5317 pmd
->last_reload_seq
= seq_read(pmd
->reload_seq
);
5318 xpthread_cond_signal(&pmd
->cond
);
5319 ovs_mutex_unlock(&pmd
->cond_mutex
);
5322 /* Finds and refs the dp_netdev_pmd_thread on core 'core_id'. Returns
5323 * the pointer if succeeds, otherwise, NULL (it can return NULL even if
5324 * 'core_id' is NON_PMD_CORE_ID).
5326 * Caller must unrefs the returned reference. */
5327 static struct dp_netdev_pmd_thread
*
5328 dp_netdev_get_pmd(struct dp_netdev
*dp
, unsigned core_id
)
5330 struct dp_netdev_pmd_thread
*pmd
;
5331 const struct cmap_node
*pnode
;
5333 pnode
= cmap_find(&dp
->poll_threads
, hash_int(core_id
, 0));
5337 pmd
= CONTAINER_OF(pnode
, struct dp_netdev_pmd_thread
, node
);
5339 return dp_netdev_pmd_try_ref(pmd
) ? pmd
: NULL
;
5342 /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */
5344 dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
5345 OVS_REQUIRES(dp
->port_mutex
)
5347 struct dp_netdev_pmd_thread
*non_pmd
;
5349 non_pmd
= xzalloc(sizeof *non_pmd
);
5350 dp_netdev_configure_pmd(non_pmd
, dp
, NON_PMD_CORE_ID
, OVS_NUMA_UNSPEC
);
5353 /* Caller must have valid pointer to 'pmd'. */
5355 dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
)
5357 return ovs_refcount_try_ref_rcu(&pmd
->ref_cnt
);
5361 dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
)
5363 if (pmd
&& ovs_refcount_unref(&pmd
->ref_cnt
) == 1) {
5364 ovsrcu_postpone(dp_netdev_destroy_pmd
, pmd
);
5368 /* Given cmap position 'pos', tries to ref the next node. If try_ref()
5369 * fails, keeps checking for next node until reaching the end of cmap.
5371 * Caller must unrefs the returned reference. */
5372 static struct dp_netdev_pmd_thread
*
5373 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
)
5375 struct dp_netdev_pmd_thread
*next
;
5378 struct cmap_node
*node
;
5380 node
= cmap_next_position(&dp
->poll_threads
, pos
);
5381 next
= node
? CONTAINER_OF(node
, struct dp_netdev_pmd_thread
, node
)
5383 } while (next
&& !dp_netdev_pmd_try_ref(next
));
5388 /* Configures the 'pmd' based on the input argument. */
5390 dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
, struct dp_netdev
*dp
,
5391 unsigned core_id
, int numa_id
)
5394 pmd
->core_id
= core_id
;
5395 pmd
->numa_id
= numa_id
;
5396 pmd
->need_reload
= false;
5397 pmd
->n_output_batches
= 0;
5399 ovs_refcount_init(&pmd
->ref_cnt
);
5400 latch_init(&pmd
->exit_latch
);
5401 pmd
->reload_seq
= seq_create();
5402 pmd
->last_reload_seq
= seq_read(pmd
->reload_seq
);
5403 atomic_init(&pmd
->reload
, false);
5404 xpthread_cond_init(&pmd
->cond
, NULL
);
5405 ovs_mutex_init(&pmd
->cond_mutex
);
5406 ovs_mutex_init(&pmd
->flow_mutex
);
5407 ovs_mutex_init(&pmd
->port_mutex
);
5408 cmap_init(&pmd
->flow_table
);
5409 cmap_init(&pmd
->classifiers
);
5410 pmd
->ctx
.last_rxq
= NULL
;
5411 pmd_thread_ctx_time_update(pmd
);
5412 pmd
->next_optimization
= pmd
->ctx
.now
+ DPCLS_OPTIMIZATION_INTERVAL
;
5413 pmd
->rxq_next_cycle_store
= pmd
->ctx
.now
+ PMD_RXQ_INTERVAL_LEN
;
5414 hmap_init(&pmd
->poll_list
);
5415 hmap_init(&pmd
->tx_ports
);
5416 hmap_init(&pmd
->tnl_port_cache
);
5417 hmap_init(&pmd
->send_port_cache
);
5418 /* init the 'flow_cache' since there is no
5419 * actual thread created for NON_PMD_CORE_ID. */
5420 if (core_id
== NON_PMD_CORE_ID
) {
5421 dfc_cache_init(&pmd
->flow_cache
);
5422 pmd_alloc_static_tx_qid(pmd
);
5424 pmd_perf_stats_init(&pmd
->perf_stats
);
5425 cmap_insert(&dp
->poll_threads
, CONST_CAST(struct cmap_node
*, &pmd
->node
),
5426 hash_int(core_id
, 0));
5430 dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
)
5434 dp_netdev_pmd_flow_flush(pmd
);
5435 hmap_destroy(&pmd
->send_port_cache
);
5436 hmap_destroy(&pmd
->tnl_port_cache
);
5437 hmap_destroy(&pmd
->tx_ports
);
5438 hmap_destroy(&pmd
->poll_list
);
5439 /* All flows (including their dpcls_rules) have been deleted already */
5440 CMAP_FOR_EACH (cls
, node
, &pmd
->classifiers
) {
5442 ovsrcu_postpone(free
, cls
);
5444 cmap_destroy(&pmd
->classifiers
);
5445 cmap_destroy(&pmd
->flow_table
);
5446 ovs_mutex_destroy(&pmd
->flow_mutex
);
5447 latch_destroy(&pmd
->exit_latch
);
5448 seq_destroy(pmd
->reload_seq
);
5449 xpthread_cond_destroy(&pmd
->cond
);
5450 ovs_mutex_destroy(&pmd
->cond_mutex
);
5451 ovs_mutex_destroy(&pmd
->port_mutex
);
5455 /* Stops the pmd thread, removes it from the 'dp->poll_threads',
5456 * and unrefs the struct. */
5458 dp_netdev_del_pmd(struct dp_netdev
*dp
, struct dp_netdev_pmd_thread
*pmd
)
5460 /* NON_PMD_CORE_ID doesn't have a thread, so we don't have to synchronize,
5461 * but extra cleanup is necessary */
5462 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
5463 ovs_mutex_lock(&dp
->non_pmd_mutex
);
5464 dfc_cache_uninit(&pmd
->flow_cache
);
5465 pmd_free_cached_ports(pmd
);
5466 pmd_free_static_tx_qid(pmd
);
5467 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
5469 latch_set(&pmd
->exit_latch
);
5470 dp_netdev_reload_pmd__(pmd
);
5471 xpthread_join(pmd
->thread
, NULL
);
5474 dp_netdev_pmd_clear_ports(pmd
);
5476 /* Purges the 'pmd''s flows after stopping the thread, but before
5477 * destroying the flows, so that the flow stats can be collected. */
5478 if (dp
->dp_purge_cb
) {
5479 dp
->dp_purge_cb(dp
->dp_purge_aux
, pmd
->core_id
);
5481 cmap_remove(&pmd
->dp
->poll_threads
, &pmd
->node
, hash_int(pmd
->core_id
, 0));
5482 dp_netdev_pmd_unref(pmd
);
5485 /* Destroys all pmd threads. If 'non_pmd' is true it also destroys the non pmd
5488 dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
, bool non_pmd
)
5490 struct dp_netdev_pmd_thread
*pmd
;
5491 struct dp_netdev_pmd_thread
**pmd_list
;
5492 size_t k
= 0, n_pmds
;
5494 n_pmds
= cmap_count(&dp
->poll_threads
);
5495 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
5497 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5498 if (!non_pmd
&& pmd
->core_id
== NON_PMD_CORE_ID
) {
5501 /* We cannot call dp_netdev_del_pmd(), since it alters
5502 * 'dp->poll_threads' (while we're iterating it) and it
5504 ovs_assert(k
< n_pmds
);
5505 pmd_list
[k
++] = pmd
;
5508 for (size_t i
= 0; i
< k
; i
++) {
5509 dp_netdev_del_pmd(dp
, pmd_list
[i
]);
5514 /* Deletes all rx queues from pmd->poll_list and all the ports from
5517 dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
)
5519 struct rxq_poll
*poll
;
5520 struct tx_port
*port
;
5522 ovs_mutex_lock(&pmd
->port_mutex
);
5523 HMAP_FOR_EACH_POP (poll
, node
, &pmd
->poll_list
) {
5526 HMAP_FOR_EACH_POP (port
, node
, &pmd
->tx_ports
) {
5529 ovs_mutex_unlock(&pmd
->port_mutex
);
5532 /* Adds rx queue to poll_list of PMD thread, if it's not there already. */
5534 dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
5535 struct dp_netdev_rxq
*rxq
)
5536 OVS_REQUIRES(pmd
->port_mutex
)
5538 int qid
= netdev_rxq_get_queue_id(rxq
->rx
);
5539 uint32_t hash
= hash_2words(odp_to_u32(rxq
->port
->port_no
), qid
);
5540 struct rxq_poll
*poll
;
5542 HMAP_FOR_EACH_WITH_HASH (poll
, node
, hash
, &pmd
->poll_list
) {
5543 if (poll
->rxq
== rxq
) {
5544 /* 'rxq' is already polled by this thread. Do nothing. */
5549 poll
= xmalloc(sizeof *poll
);
5551 hmap_insert(&pmd
->poll_list
, &poll
->node
, hash
);
5553 pmd
->need_reload
= true;
5556 /* Delete 'poll' from poll_list of PMD thread. */
5558 dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
5559 struct rxq_poll
*poll
)
5560 OVS_REQUIRES(pmd
->port_mutex
)
5562 hmap_remove(&pmd
->poll_list
, &poll
->node
);
5565 pmd
->need_reload
= true;
5568 /* Add 'port' to the tx port cache of 'pmd', which must be reloaded for the
5569 * changes to take effect. */
5571 dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
5572 struct dp_netdev_port
*port
)
5573 OVS_REQUIRES(pmd
->port_mutex
)
5577 tx
= tx_port_lookup(&pmd
->tx_ports
, port
->port_no
);
5579 /* 'port' is already on this thread tx cache. Do nothing. */
5583 tx
= xzalloc(sizeof *tx
);
5587 tx
->flush_time
= 0LL;
5588 dp_packet_batch_init(&tx
->output_pkts
);
5590 hmap_insert(&pmd
->tx_ports
, &tx
->node
, hash_port_no(tx
->port
->port_no
));
5591 pmd
->need_reload
= true;
5594 /* Del 'tx' from the tx port cache of 'pmd', which must be reloaded for the
5595 * changes to take effect. */
5597 dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
5599 OVS_REQUIRES(pmd
->port_mutex
)
5601 hmap_remove(&pmd
->tx_ports
, &tx
->node
);
5603 pmd
->need_reload
= true;
5607 dpif_netdev_get_datapath_version(void)
5609 return xstrdup("<built-in>");
5613 dp_netdev_flow_used(struct dp_netdev_flow
*netdev_flow
, int cnt
, int size
,
5614 uint16_t tcp_flags
, long long now
)
5618 atomic_store_relaxed(&netdev_flow
->stats
.used
, now
);
5619 non_atomic_ullong_add(&netdev_flow
->stats
.packet_count
, cnt
);
5620 non_atomic_ullong_add(&netdev_flow
->stats
.byte_count
, size
);
5621 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
5623 atomic_store_relaxed(&netdev_flow
->stats
.tcp_flags
, flags
);
5627 dp_netdev_upcall(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet
*packet_
,
5628 struct flow
*flow
, struct flow_wildcards
*wc
, ovs_u128
*ufid
,
5629 enum dpif_upcall_type type
, const struct nlattr
*userdata
,
5630 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
5632 struct dp_netdev
*dp
= pmd
->dp
;
5634 if (OVS_UNLIKELY(!dp
->upcall_cb
)) {
5638 if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl
))) {
5639 struct ds ds
= DS_EMPTY_INITIALIZER
;
5642 struct odp_flow_key_parms odp_parms
= {
5644 .mask
= wc
? &wc
->masks
: NULL
,
5645 .support
= dp_netdev_support
,
5648 ofpbuf_init(&key
, 0);
5649 odp_flow_key_from_flow(&odp_parms
, &key
);
5650 packet_str
= ofp_dp_packet_to_string(packet_
);
5652 odp_flow_key_format(key
.data
, key
.size
, &ds
);
5654 VLOG_DBG("%s: %s upcall:\n%s\n%s", dp
->name
,
5655 dpif_upcall_type_to_string(type
), ds_cstr(&ds
), packet_str
);
5657 ofpbuf_uninit(&key
);
5663 return dp
->upcall_cb(packet_
, flow
, ufid
, pmd
->core_id
, type
, userdata
,
5664 actions
, wc
, put_actions
, dp
->upcall_aux
);
5667 static inline uint32_t
5668 dpif_netdev_packet_get_rss_hash_orig_pkt(struct dp_packet
*packet
,
5669 const struct miniflow
*mf
)
5673 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
5674 hash
= dp_packet_get_rss_hash(packet
);
5676 hash
= miniflow_hash_5tuple(mf
, 0);
5677 dp_packet_set_rss_hash(packet
, hash
);
5683 static inline uint32_t
5684 dpif_netdev_packet_get_rss_hash(struct dp_packet
*packet
,
5685 const struct miniflow
*mf
)
5687 uint32_t hash
, recirc_depth
;
5689 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
5690 hash
= dp_packet_get_rss_hash(packet
);
5692 hash
= miniflow_hash_5tuple(mf
, 0);
5693 dp_packet_set_rss_hash(packet
, hash
);
5696 /* The RSS hash must account for the recirculation depth to avoid
5697 * collisions in the exact match cache */
5698 recirc_depth
= *recirc_depth_get_unsafe();
5699 if (OVS_UNLIKELY(recirc_depth
)) {
5700 hash
= hash_finish(hash
, recirc_depth
);
5701 dp_packet_set_rss_hash(packet
, hash
);
5706 struct packet_batch_per_flow
{
5707 unsigned int byte_count
;
5709 struct dp_netdev_flow
*flow
;
5711 struct dp_packet_batch array
;
5715 packet_batch_per_flow_update(struct packet_batch_per_flow
*batch
,
5716 struct dp_packet
*packet
,
5719 batch
->byte_count
+= dp_packet_size(packet
);
5720 batch
->tcp_flags
|= tcp_flags
;
5721 batch
->array
.packets
[batch
->array
.count
++] = packet
;
5725 packet_batch_per_flow_init(struct packet_batch_per_flow
*batch
,
5726 struct dp_netdev_flow
*flow
)
5728 flow
->batch
= batch
;
5731 dp_packet_batch_init(&batch
->array
);
5732 batch
->byte_count
= 0;
5733 batch
->tcp_flags
= 0;
5737 packet_batch_per_flow_execute(struct packet_batch_per_flow
*batch
,
5738 struct dp_netdev_pmd_thread
*pmd
)
5740 struct dp_netdev_actions
*actions
;
5741 struct dp_netdev_flow
*flow
= batch
->flow
;
5743 dp_netdev_flow_used(flow
, batch
->array
.count
, batch
->byte_count
,
5744 batch
->tcp_flags
, pmd
->ctx
.now
/ 1000);
5746 actions
= dp_netdev_flow_get_actions(flow
);
5748 dp_netdev_execute_actions(pmd
, &batch
->array
, true, &flow
->flow
,
5749 actions
->actions
, actions
->size
);
5753 dp_netdev_queue_batches(struct dp_packet
*pkt
,
5754 struct dp_netdev_flow
*flow
, uint16_t tcp_flags
,
5755 struct packet_batch_per_flow
*batches
,
5758 struct packet_batch_per_flow
*batch
= flow
->batch
;
5760 if (OVS_UNLIKELY(!batch
)) {
5761 batch
= &batches
[(*n_batches
)++];
5762 packet_batch_per_flow_init(batch
, flow
);
5765 packet_batch_per_flow_update(batch
, pkt
, tcp_flags
);
5768 /* SMC lookup function for a batch of packets.
5769 * By doing batching SMC lookup, we can use prefetch
5770 * to hide memory access latency.
5773 smc_lookup_batch(struct dp_netdev_pmd_thread
*pmd
,
5774 struct netdev_flow_key
*keys
,
5775 struct netdev_flow_key
**missed_keys
,
5776 struct dp_packet_batch
*packets_
,
5777 struct packet_batch_per_flow batches
[],
5778 size_t *n_batches
, const int cnt
)
5781 struct dp_packet
*packet
;
5782 size_t n_smc_hit
= 0, n_missed
= 0;
5783 struct dfc_cache
*cache
= &pmd
->flow_cache
;
5784 struct smc_cache
*smc_cache
= &cache
->smc_cache
;
5785 const struct cmap_node
*flow_node
;
5787 /* Prefetch buckets for all packets */
5788 for (i
= 0; i
< cnt
; i
++) {
5789 OVS_PREFETCH(&smc_cache
->buckets
[keys
[i
].hash
& SMC_MASK
]);
5792 DP_PACKET_BATCH_REFILL_FOR_EACH (i
, cnt
, packet
, packets_
) {
5793 struct dp_netdev_flow
*flow
= NULL
;
5794 flow_node
= smc_entry_get(pmd
, keys
[i
].hash
);
5797 if (OVS_LIKELY(flow_node
!= NULL
)) {
5798 CMAP_NODE_FOR_EACH (flow
, node
, flow_node
) {
5799 /* Since we dont have per-port megaflow to check the port
5800 * number, we need to verify that the input ports match. */
5801 if (OVS_LIKELY(dpcls_rule_matches_key(&flow
->cr
, &keys
[i
]) &&
5802 flow
->flow
.in_port
.odp_port
== packet
->md
.in_port
.odp_port
)) {
5803 /* SMC hit and emc miss, we insert into EMC */
5805 netdev_flow_key_size(miniflow_n_values(&keys
[i
].mf
));
5806 emc_probabilistic_insert(pmd
, &keys
[i
], flow
);
5807 dp_netdev_queue_batches(packet
, flow
,
5808 miniflow_get_tcp_flags(&keys
[i
].mf
), batches
, n_batches
);
5819 /* SMC missed. Group missed packets together at
5820 * the beginning of the 'packets' array. */
5821 dp_packet_batch_refill(packets_
, packet
, i
);
5822 /* Put missed keys to the pointer arrays return to the caller */
5823 missed_keys
[n_missed
++] = &keys
[i
];
5826 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_SMC_HIT
, n_smc_hit
);
5829 /* Try to process all ('cnt') the 'packets' using only the datapath flow cache
5830 * 'pmd->flow_cache'. If a flow is not found for a packet 'packets[i]', the
5831 * miniflow is copied into 'keys' and the packet pointer is moved at the
5832 * beginning of the 'packets' array. The pointers of missed keys are put in the
5833 * missed_keys pointer array for future processing.
5835 * The function returns the number of packets that needs to be processed in the
5836 * 'packets' array (they have been moved to the beginning of the vector).
5838 * For performance reasons a caller may choose not to initialize the metadata
5839 * in 'packets_'. If 'md_is_valid' is false, the metadata in 'packets'
5840 * is not valid and must be initialized by this function using 'port_no'.
5841 * If 'md_is_valid' is true, the metadata is already valid and 'port_no'
5844 static inline size_t
5845 dfc_processing(struct dp_netdev_pmd_thread
*pmd
,
5846 struct dp_packet_batch
*packets_
,
5847 struct netdev_flow_key
*keys
,
5848 struct netdev_flow_key
**missed_keys
,
5849 struct packet_batch_per_flow batches
[], size_t *n_batches
,
5850 bool md_is_valid
, odp_port_t port_no
)
5852 struct netdev_flow_key
*key
= &keys
[0];
5853 size_t n_missed
= 0, n_emc_hit
= 0;
5854 struct dfc_cache
*cache
= &pmd
->flow_cache
;
5855 struct dp_packet
*packet
;
5856 const size_t cnt
= dp_packet_batch_size(packets_
);
5862 atomic_read_relaxed(&pmd
->dp
->smc_enable_db
, &smc_enable_db
);
5863 atomic_read_relaxed(&pmd
->dp
->emc_insert_min
, &cur_min
);
5864 pmd_perf_update_counter(&pmd
->perf_stats
,
5865 md_is_valid
? PMD_STAT_RECIRC
: PMD_STAT_RECV
,
5868 DP_PACKET_BATCH_REFILL_FOR_EACH (i
, cnt
, packet
, packets_
) {
5869 struct dp_netdev_flow
*flow
;
5872 if (OVS_UNLIKELY(dp_packet_size(packet
) < ETH_HEADER_LEN
)) {
5873 dp_packet_delete(packet
);
5878 struct dp_packet
**packets
= packets_
->packets
;
5879 /* Prefetch next packet data and metadata. */
5880 OVS_PREFETCH(dp_packet_data(packets
[i
+1]));
5881 pkt_metadata_prefetch_init(&packets
[i
+1]->md
);
5885 pkt_metadata_init(&packet
->md
, port_no
);
5888 if ((*recirc_depth_get() == 0) &&
5889 dp_packet_has_flow_mark(packet
, &mark
)) {
5890 flow
= mark_to_flow_find(pmd
, mark
);
5892 tcp_flags
= parse_tcp_flags(packet
);
5893 dp_netdev_queue_batches(packet
, flow
, tcp_flags
, batches
,
5899 miniflow_extract(packet
, &key
->mf
);
5900 key
->len
= 0; /* Not computed yet. */
5901 /* If EMC and SMC disabled skip hash computation */
5902 if (smc_enable_db
== true || cur_min
!= 0) {
5904 key
->hash
= dpif_netdev_packet_get_rss_hash_orig_pkt(packet
,
5907 key
->hash
= dpif_netdev_packet_get_rss_hash(packet
, &key
->mf
);
5911 flow
= emc_lookup(&cache
->emc_cache
, key
);
5915 if (OVS_LIKELY(flow
)) {
5916 tcp_flags
= miniflow_get_tcp_flags(&key
->mf
);
5917 dp_netdev_queue_batches(packet
, flow
, tcp_flags
, batches
,
5921 /* Exact match cache missed. Group missed packets together at
5922 * the beginning of the 'packets' array. */
5923 dp_packet_batch_refill(packets_
, packet
, i
);
5924 /* 'key[n_missed]' contains the key of the current packet and it
5925 * will be passed to SMC lookup. The next key should be extracted
5926 * to 'keys[n_missed + 1]'.
5927 * We also maintain a pointer array to keys missed both SMC and EMC
5928 * which will be returned to the caller for future processing. */
5929 missed_keys
[n_missed
] = key
;
5930 key
= &keys
[++n_missed
];
5934 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_EXACT_HIT
, n_emc_hit
);
5936 if (!smc_enable_db
) {
5937 return dp_packet_batch_size(packets_
);
5940 /* Packets miss EMC will do a batch lookup in SMC if enabled */
5941 smc_lookup_batch(pmd
, keys
, missed_keys
, packets_
, batches
,
5942 n_batches
, n_missed
);
5944 return dp_packet_batch_size(packets_
);
5948 handle_packet_upcall(struct dp_netdev_pmd_thread
*pmd
,
5949 struct dp_packet
*packet
,
5950 const struct netdev_flow_key
*key
,
5951 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
5953 struct ofpbuf
*add_actions
;
5954 struct dp_packet_batch b
;
5958 uint64_t cycles
= cycles_counter_update(&pmd
->perf_stats
);
5960 match
.tun_md
.valid
= false;
5961 miniflow_expand(&key
->mf
, &match
.flow
);
5963 ofpbuf_clear(actions
);
5964 ofpbuf_clear(put_actions
);
5966 dpif_flow_hash(pmd
->dp
->dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
5967 error
= dp_netdev_upcall(pmd
, packet
, &match
.flow
, &match
.wc
,
5968 &ufid
, DPIF_UC_MISS
, NULL
, actions
,
5970 if (OVS_UNLIKELY(error
&& error
!= ENOSPC
)) {
5971 dp_packet_delete(packet
);
5975 /* The Netlink encoding of datapath flow keys cannot express
5976 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
5977 * tag is interpreted as exact match on the fact that there is no
5978 * VLAN. Unless we refactor a lot of code that translates between
5979 * Netlink and struct flow representations, we have to do the same
5981 if (!match
.wc
.masks
.vlans
[0].tci
) {
5982 match
.wc
.masks
.vlans
[0].tci
= htons(0xffff);
5985 /* We can't allow the packet batching in the next loop to execute
5986 * the actions. Otherwise, if there are any slow path actions,
5987 * we'll send the packet up twice. */
5988 dp_packet_batch_init_packet(&b
, packet
);
5989 dp_netdev_execute_actions(pmd
, &b
, true, &match
.flow
,
5990 actions
->data
, actions
->size
);
5992 add_actions
= put_actions
->size
? put_actions
: actions
;
5993 if (OVS_LIKELY(error
!= ENOSPC
)) {
5994 struct dp_netdev_flow
*netdev_flow
;
5996 /* XXX: There's a race window where a flow covering this packet
5997 * could have already been installed since we last did the flow
5998 * lookup before upcall. This could be solved by moving the
5999 * mutex lock outside the loop, but that's an awful long time
6000 * to be locking everyone out of making flow installs. If we
6001 * move to a per-core classifier, it would be reasonable. */
6002 ovs_mutex_lock(&pmd
->flow_mutex
);
6003 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
, NULL
);
6004 if (OVS_LIKELY(!netdev_flow
)) {
6005 netdev_flow
= dp_netdev_flow_add(pmd
, &match
, &ufid
,
6009 ovs_mutex_unlock(&pmd
->flow_mutex
);
6010 uint32_t hash
= dp_netdev_flow_hash(&netdev_flow
->ufid
);
6011 smc_insert(pmd
, key
, hash
);
6012 emc_probabilistic_insert(pmd
, key
, netdev_flow
);
6014 if (pmd_perf_metrics_enabled(pmd
)) {
6015 /* Update upcall stats. */
6016 cycles
= cycles_counter_update(&pmd
->perf_stats
) - cycles
;
6017 struct pmd_perf_stats
*s
= &pmd
->perf_stats
;
6018 s
->current
.upcalls
++;
6019 s
->current
.upcall_cycles
+= cycles
;
6020 histogram_add_sample(&s
->cycles_per_upcall
, cycles
);
6026 fast_path_processing(struct dp_netdev_pmd_thread
*pmd
,
6027 struct dp_packet_batch
*packets_
,
6028 struct netdev_flow_key
**keys
,
6029 struct packet_batch_per_flow batches
[],
6033 const size_t cnt
= dp_packet_batch_size(packets_
);
6034 #if !defined(__CHECKER__) && !defined(_WIN32)
6035 const size_t PKT_ARRAY_SIZE
= cnt
;
6037 /* Sparse or MSVC doesn't like variable length array. */
6038 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
6040 struct dp_packet
*packet
;
6042 struct dpcls_rule
*rules
[PKT_ARRAY_SIZE
];
6043 struct dp_netdev
*dp
= pmd
->dp
;
6044 int upcall_ok_cnt
= 0, upcall_fail_cnt
= 0;
6045 int lookup_cnt
= 0, add_lookup_cnt
;
6048 for (size_t i
= 0; i
< cnt
; i
++) {
6049 /* Key length is needed in all the cases, hash computed on demand. */
6050 keys
[i
]->len
= netdev_flow_key_size(miniflow_n_values(&keys
[i
]->mf
));
6052 /* Get the classifier for the in_port */
6053 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
6054 if (OVS_LIKELY(cls
)) {
6055 any_miss
= !dpcls_lookup(cls
, (const struct netdev_flow_key
**)keys
,
6056 rules
, cnt
, &lookup_cnt
);
6059 memset(rules
, 0, sizeof(rules
));
6061 if (OVS_UNLIKELY(any_miss
) && !fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
6062 uint64_t actions_stub
[512 / 8], slow_stub
[512 / 8];
6063 struct ofpbuf actions
, put_actions
;
6065 ofpbuf_use_stub(&actions
, actions_stub
, sizeof actions_stub
);
6066 ofpbuf_use_stub(&put_actions
, slow_stub
, sizeof slow_stub
);
6068 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6069 struct dp_netdev_flow
*netdev_flow
;
6071 if (OVS_LIKELY(rules
[i
])) {
6075 /* It's possible that an earlier slow path execution installed
6076 * a rule covering this flow. In this case, it's a lot cheaper
6077 * to catch it here than execute a miss. */
6078 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, keys
[i
],
6081 lookup_cnt
+= add_lookup_cnt
;
6082 rules
[i
] = &netdev_flow
->cr
;
6086 int error
= handle_packet_upcall(pmd
, packet
, keys
[i
],
6087 &actions
, &put_actions
);
6089 if (OVS_UNLIKELY(error
)) {
6096 ofpbuf_uninit(&actions
);
6097 ofpbuf_uninit(&put_actions
);
6098 fat_rwlock_unlock(&dp
->upcall_rwlock
);
6099 } else if (OVS_UNLIKELY(any_miss
)) {
6100 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6101 if (OVS_UNLIKELY(!rules
[i
])) {
6102 dp_packet_delete(packet
);
6108 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6109 struct dp_netdev_flow
*flow
;
6111 if (OVS_UNLIKELY(!rules
[i
])) {
6115 flow
= dp_netdev_flow_cast(rules
[i
]);
6116 uint32_t hash
= dp_netdev_flow_hash(&flow
->ufid
);
6117 smc_insert(pmd
, keys
[i
], hash
);
6119 emc_probabilistic_insert(pmd
, keys
[i
], flow
);
6120 dp_netdev_queue_batches(packet
, flow
,
6121 miniflow_get_tcp_flags(&keys
[i
]->mf
),
6122 batches
, n_batches
);
6125 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MASKED_HIT
,
6126 cnt
- upcall_ok_cnt
- upcall_fail_cnt
);
6127 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MASKED_LOOKUP
,
6129 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MISS
,
6131 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_LOST
,
6135 /* Packets enter the datapath from a port (or from recirculation) here.
6137 * When 'md_is_valid' is true the metadata in 'packets' are already valid.
6138 * When false the metadata in 'packets' need to be initialized. */
6140 dp_netdev_input__(struct dp_netdev_pmd_thread
*pmd
,
6141 struct dp_packet_batch
*packets
,
6142 bool md_is_valid
, odp_port_t port_no
)
6144 #if !defined(__CHECKER__) && !defined(_WIN32)
6145 const size_t PKT_ARRAY_SIZE
= dp_packet_batch_size(packets
);
6147 /* Sparse or MSVC doesn't like variable length array. */
6148 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
6150 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
)
6151 struct netdev_flow_key keys
[PKT_ARRAY_SIZE
];
6152 struct netdev_flow_key
*missed_keys
[PKT_ARRAY_SIZE
];
6153 struct packet_batch_per_flow batches
[PKT_ARRAY_SIZE
];
6158 dfc_processing(pmd
, packets
, keys
, missed_keys
, batches
, &n_batches
,
6159 md_is_valid
, port_no
);
6160 if (!dp_packet_batch_is_empty(packets
)) {
6161 /* Get ingress port from first packet's metadata. */
6162 in_port
= packets
->packets
[0]->md
.in_port
.odp_port
;
6163 fast_path_processing(pmd
, packets
, missed_keys
,
6164 batches
, &n_batches
, in_port
);
6167 /* All the flow batches need to be reset before any call to
6168 * packet_batch_per_flow_execute() as it could potentially trigger
6169 * recirculation. When a packet matching flow ‘j’ happens to be
6170 * recirculated, the nested call to dp_netdev_input__() could potentially
6171 * classify the packet as matching another flow - say 'k'. It could happen
6172 * that in the previous call to dp_netdev_input__() that same flow 'k' had
6173 * already its own batches[k] still waiting to be served. So if its
6174 * ‘batch’ member is not reset, the recirculated packet would be wrongly
6175 * appended to batches[k] of the 1st call to dp_netdev_input__(). */
6177 for (i
= 0; i
< n_batches
; i
++) {
6178 batches
[i
].flow
->batch
= NULL
;
6181 for (i
= 0; i
< n_batches
; i
++) {
6182 packet_batch_per_flow_execute(&batches
[i
], pmd
);
6187 dp_netdev_input(struct dp_netdev_pmd_thread
*pmd
,
6188 struct dp_packet_batch
*packets
,
6191 dp_netdev_input__(pmd
, packets
, false, port_no
);
6195 dp_netdev_recirculate(struct dp_netdev_pmd_thread
*pmd
,
6196 struct dp_packet_batch
*packets
)
6198 dp_netdev_input__(pmd
, packets
, true, 0);
6201 struct dp_netdev_execute_aux
{
6202 struct dp_netdev_pmd_thread
*pmd
;
6203 const struct flow
*flow
;
6207 dpif_netdev_register_dp_purge_cb(struct dpif
*dpif
, dp_purge_callback
*cb
,
6210 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6211 dp
->dp_purge_aux
= aux
;
6212 dp
->dp_purge_cb
= cb
;
6216 dpif_netdev_register_upcall_cb(struct dpif
*dpif
, upcall_callback
*cb
,
6219 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6220 dp
->upcall_aux
= aux
;
6225 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
6229 struct dp_netdev_port
*port
;
6232 HMAP_FOR_EACH (tx
, node
, &pmd
->send_port_cache
) {
6233 if (!tx
->port
->dynamic_txqs
) {
6236 interval
= pmd
->ctx
.now
- tx
->last_used
;
6237 if (tx
->qid
>= 0 && (purge
|| interval
>= XPS_TIMEOUT
)) {
6239 ovs_mutex_lock(&port
->txq_used_mutex
);
6240 port
->txq_used
[tx
->qid
]--;
6241 ovs_mutex_unlock(&port
->txq_used_mutex
);
6248 dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
6251 struct dp_netdev_port
*port
;
6253 int i
, min_cnt
, min_qid
;
6255 interval
= pmd
->ctx
.now
- tx
->last_used
;
6256 tx
->last_used
= pmd
->ctx
.now
;
6258 if (OVS_LIKELY(tx
->qid
>= 0 && interval
< XPS_TIMEOUT
)) {
6264 ovs_mutex_lock(&port
->txq_used_mutex
);
6266 port
->txq_used
[tx
->qid
]--;
6272 for (i
= 0; i
< netdev_n_txq(port
->netdev
); i
++) {
6273 if (port
->txq_used
[i
] < min_cnt
|| min_cnt
== -1) {
6274 min_cnt
= port
->txq_used
[i
];
6279 port
->txq_used
[min_qid
]++;
6282 ovs_mutex_unlock(&port
->txq_used_mutex
);
6284 dpif_netdev_xps_revalidate_pmd(pmd
, false);
6286 VLOG_DBG("Core %d: New TX queue ID %d for port \'%s\'.",
6287 pmd
->core_id
, tx
->qid
, netdev_get_name(tx
->port
->netdev
));
6291 static struct tx_port
*
6292 pmd_tnl_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
6295 return tx_port_lookup(&pmd
->tnl_port_cache
, port_no
);
6298 static struct tx_port
*
6299 pmd_send_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
6302 return tx_port_lookup(&pmd
->send_port_cache
, port_no
);
6306 push_tnl_action(const struct dp_netdev_pmd_thread
*pmd
,
6307 const struct nlattr
*attr
,
6308 struct dp_packet_batch
*batch
)
6310 struct tx_port
*tun_port
;
6311 const struct ovs_action_push_tnl
*data
;
6314 data
= nl_attr_get(attr
);
6316 tun_port
= pmd_tnl_port_cache_lookup(pmd
, data
->tnl_port
);
6321 err
= netdev_push_header(tun_port
->port
->netdev
, batch
, data
);
6326 dp_packet_delete_batch(batch
, true);
6331 dp_execute_userspace_action(struct dp_netdev_pmd_thread
*pmd
,
6332 struct dp_packet
*packet
, bool should_steal
,
6333 struct flow
*flow
, ovs_u128
*ufid
,
6334 struct ofpbuf
*actions
,
6335 const struct nlattr
*userdata
)
6337 struct dp_packet_batch b
;
6340 ofpbuf_clear(actions
);
6342 error
= dp_netdev_upcall(pmd
, packet
, flow
, NULL
, ufid
,
6343 DPIF_UC_ACTION
, userdata
, actions
,
6345 if (!error
|| error
== ENOSPC
) {
6346 dp_packet_batch_init_packet(&b
, packet
);
6347 dp_netdev_execute_actions(pmd
, &b
, should_steal
, flow
,
6348 actions
->data
, actions
->size
);
6349 } else if (should_steal
) {
6350 dp_packet_delete(packet
);
6355 dp_execute_cb(void *aux_
, struct dp_packet_batch
*packets_
,
6356 const struct nlattr
*a
, bool should_steal
)
6357 OVS_NO_THREAD_SAFETY_ANALYSIS
6359 struct dp_netdev_execute_aux
*aux
= aux_
;
6360 uint32_t *depth
= recirc_depth_get();
6361 struct dp_netdev_pmd_thread
*pmd
= aux
->pmd
;
6362 struct dp_netdev
*dp
= pmd
->dp
;
6363 int type
= nl_attr_type(a
);
6366 switch ((enum ovs_action_attr
)type
) {
6367 case OVS_ACTION_ATTR_OUTPUT
:
6368 p
= pmd_send_port_cache_lookup(pmd
, nl_attr_get_odp_port(a
));
6369 if (OVS_LIKELY(p
)) {
6370 struct dp_packet
*packet
;
6371 struct dp_packet_batch out
;
6373 if (!should_steal
) {
6374 dp_packet_batch_clone(&out
, packets_
);
6375 dp_packet_batch_reset_cutlen(packets_
);
6378 dp_packet_batch_apply_cutlen(packets_
);
6381 if (OVS_UNLIKELY(!dp_packet_batch_is_empty(&p
->output_pkts
)
6382 && packets_
->packets
[0]->source
6383 != p
->output_pkts
.packets
[0]->source
)) {
6384 /* XXX: netdev-dpdk assumes that all packets in a single
6385 * output batch has the same source. Flush here to
6386 * avoid memory access issues. */
6387 dp_netdev_pmd_flush_output_on_port(pmd
, p
);
6390 if (dp_packet_batch_size(&p
->output_pkts
)
6391 + dp_packet_batch_size(packets_
) > NETDEV_MAX_BURST
) {
6392 /* Flush here to avoid overflow. */
6393 dp_netdev_pmd_flush_output_on_port(pmd
, p
);
6396 if (dp_packet_batch_is_empty(&p
->output_pkts
)) {
6397 pmd
->n_output_batches
++;
6400 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6401 p
->output_pkts_rxqs
[dp_packet_batch_size(&p
->output_pkts
)] =
6403 dp_packet_batch_add(&p
->output_pkts
, packet
);
6409 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
6411 /* We're requested to push tunnel header, but also we need to take
6412 * the ownership of these packets. Thus, we can avoid performing
6413 * the action, because the caller will not use the result anyway.
6414 * Just break to free the batch. */
6417 dp_packet_batch_apply_cutlen(packets_
);
6418 push_tnl_action(pmd
, a
, packets_
);
6421 case OVS_ACTION_ATTR_TUNNEL_POP
:
6422 if (*depth
< MAX_RECIRC_DEPTH
) {
6423 struct dp_packet_batch
*orig_packets_
= packets_
;
6424 odp_port_t portno
= nl_attr_get_odp_port(a
);
6426 p
= pmd_tnl_port_cache_lookup(pmd
, portno
);
6428 struct dp_packet_batch tnl_pkt
;
6430 if (!should_steal
) {
6431 dp_packet_batch_clone(&tnl_pkt
, packets_
);
6432 packets_
= &tnl_pkt
;
6433 dp_packet_batch_reset_cutlen(orig_packets_
);
6436 dp_packet_batch_apply_cutlen(packets_
);
6438 netdev_pop_header(p
->port
->netdev
, packets_
);
6439 if (dp_packet_batch_is_empty(packets_
)) {
6443 struct dp_packet
*packet
;
6444 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6445 packet
->md
.in_port
.odp_port
= portno
;
6449 dp_netdev_recirculate(pmd
, packets_
);
6456 case OVS_ACTION_ATTR_USERSPACE
:
6457 if (!fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
6458 struct dp_packet_batch
*orig_packets_
= packets_
;
6459 const struct nlattr
*userdata
;
6460 struct dp_packet_batch usr_pkt
;
6461 struct ofpbuf actions
;
6466 userdata
= nl_attr_find_nested(a
, OVS_USERSPACE_ATTR_USERDATA
);
6467 ofpbuf_init(&actions
, 0);
6469 if (packets_
->trunc
) {
6470 if (!should_steal
) {
6471 dp_packet_batch_clone(&usr_pkt
, packets_
);
6472 packets_
= &usr_pkt
;
6474 dp_packet_batch_reset_cutlen(orig_packets_
);
6477 dp_packet_batch_apply_cutlen(packets_
);
6480 struct dp_packet
*packet
;
6481 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6482 flow_extract(packet
, &flow
);
6483 dpif_flow_hash(dp
->dpif
, &flow
, sizeof flow
, &ufid
);
6484 dp_execute_userspace_action(pmd
, packet
, should_steal
, &flow
,
6485 &ufid
, &actions
, userdata
);
6489 dp_packet_delete_batch(packets_
, true);
6492 ofpbuf_uninit(&actions
);
6493 fat_rwlock_unlock(&dp
->upcall_rwlock
);
6499 case OVS_ACTION_ATTR_RECIRC
:
6500 if (*depth
< MAX_RECIRC_DEPTH
) {
6501 struct dp_packet_batch recirc_pkts
;
6503 if (!should_steal
) {
6504 dp_packet_batch_clone(&recirc_pkts
, packets_
);
6505 packets_
= &recirc_pkts
;
6508 struct dp_packet
*packet
;
6509 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6510 packet
->md
.recirc_id
= nl_attr_get_u32(a
);
6514 dp_netdev_recirculate(pmd
, packets_
);
6520 VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
6523 case OVS_ACTION_ATTR_CT
: {
6524 const struct nlattr
*b
;
6526 bool commit
= false;
6529 const char *helper
= NULL
;
6530 const uint32_t *setmark
= NULL
;
6531 const struct ovs_key_ct_labels
*setlabel
= NULL
;
6532 struct nat_action_info_t nat_action_info
;
6533 struct nat_action_info_t
*nat_action_info_ref
= NULL
;
6534 bool nat_config
= false;
6536 NL_ATTR_FOR_EACH_UNSAFE (b
, left
, nl_attr_get(a
),
6537 nl_attr_get_size(a
)) {
6538 enum ovs_ct_attr sub_type
= nl_attr_type(b
);
6541 case OVS_CT_ATTR_FORCE_COMMIT
:
6544 case OVS_CT_ATTR_COMMIT
:
6547 case OVS_CT_ATTR_ZONE
:
6548 zone
= nl_attr_get_u16(b
);
6550 case OVS_CT_ATTR_HELPER
:
6551 helper
= nl_attr_get_string(b
);
6553 case OVS_CT_ATTR_MARK
:
6554 setmark
= nl_attr_get(b
);
6556 case OVS_CT_ATTR_LABELS
:
6557 setlabel
= nl_attr_get(b
);
6559 case OVS_CT_ATTR_EVENTMASK
:
6560 /* Silently ignored, as userspace datapath does not generate
6561 * netlink events. */
6563 case OVS_CT_ATTR_NAT
: {
6564 const struct nlattr
*b_nest
;
6565 unsigned int left_nest
;
6566 bool ip_min_specified
= false;
6567 bool proto_num_min_specified
= false;
6568 bool ip_max_specified
= false;
6569 bool proto_num_max_specified
= false;
6570 memset(&nat_action_info
, 0, sizeof nat_action_info
);
6571 nat_action_info_ref
= &nat_action_info
;
6573 NL_NESTED_FOR_EACH_UNSAFE (b_nest
, left_nest
, b
) {
6574 enum ovs_nat_attr sub_type_nest
= nl_attr_type(b_nest
);
6576 switch (sub_type_nest
) {
6577 case OVS_NAT_ATTR_SRC
:
6578 case OVS_NAT_ATTR_DST
:
6580 nat_action_info
.nat_action
|=
6581 ((sub_type_nest
== OVS_NAT_ATTR_SRC
)
6582 ? NAT_ACTION_SRC
: NAT_ACTION_DST
);
6584 case OVS_NAT_ATTR_IP_MIN
:
6585 memcpy(&nat_action_info
.min_addr
,
6586 nl_attr_get(b_nest
),
6587 nl_attr_get_size(b_nest
));
6588 ip_min_specified
= true;
6590 case OVS_NAT_ATTR_IP_MAX
:
6591 memcpy(&nat_action_info
.max_addr
,
6592 nl_attr_get(b_nest
),
6593 nl_attr_get_size(b_nest
));
6594 ip_max_specified
= true;
6596 case OVS_NAT_ATTR_PROTO_MIN
:
6597 nat_action_info
.min_port
=
6598 nl_attr_get_u16(b_nest
);
6599 proto_num_min_specified
= true;
6601 case OVS_NAT_ATTR_PROTO_MAX
:
6602 nat_action_info
.max_port
=
6603 nl_attr_get_u16(b_nest
);
6604 proto_num_max_specified
= true;
6606 case OVS_NAT_ATTR_PERSISTENT
:
6607 case OVS_NAT_ATTR_PROTO_HASH
:
6608 case OVS_NAT_ATTR_PROTO_RANDOM
:
6610 case OVS_NAT_ATTR_UNSPEC
:
6611 case __OVS_NAT_ATTR_MAX
:
6616 if (ip_min_specified
&& !ip_max_specified
) {
6617 nat_action_info
.max_addr
= nat_action_info
.min_addr
;
6619 if (proto_num_min_specified
&& !proto_num_max_specified
) {
6620 nat_action_info
.max_port
= nat_action_info
.min_port
;
6622 if (proto_num_min_specified
|| proto_num_max_specified
) {
6623 if (nat_action_info
.nat_action
& NAT_ACTION_SRC
) {
6624 nat_action_info
.nat_action
|= NAT_ACTION_SRC_PORT
;
6625 } else if (nat_action_info
.nat_action
& NAT_ACTION_DST
) {
6626 nat_action_info
.nat_action
|= NAT_ACTION_DST_PORT
;
6631 case OVS_CT_ATTR_UNSPEC
:
6632 case __OVS_CT_ATTR_MAX
:
6637 /* We won't be able to function properly in this case, hence
6638 * complain loudly. */
6639 if (nat_config
&& !commit
) {
6640 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
6641 VLOG_WARN_RL(&rl
, "NAT specified without commit.");
6644 conntrack_execute(&dp
->conntrack
, packets_
, aux
->flow
->dl_type
, force
,
6645 commit
, zone
, setmark
, setlabel
, aux
->flow
->tp_src
,
6646 aux
->flow
->tp_dst
, helper
, nat_action_info_ref
,
6647 pmd
->ctx
.now
/ 1000);
6651 case OVS_ACTION_ATTR_METER
:
6652 dp_netdev_run_meter(pmd
->dp
, packets_
, nl_attr_get_u32(a
),
6656 case OVS_ACTION_ATTR_PUSH_VLAN
:
6657 case OVS_ACTION_ATTR_POP_VLAN
:
6658 case OVS_ACTION_ATTR_PUSH_MPLS
:
6659 case OVS_ACTION_ATTR_POP_MPLS
:
6660 case OVS_ACTION_ATTR_SET
:
6661 case OVS_ACTION_ATTR_SET_MASKED
:
6662 case OVS_ACTION_ATTR_SAMPLE
:
6663 case OVS_ACTION_ATTR_HASH
:
6664 case OVS_ACTION_ATTR_UNSPEC
:
6665 case OVS_ACTION_ATTR_TRUNC
:
6666 case OVS_ACTION_ATTR_PUSH_ETH
:
6667 case OVS_ACTION_ATTR_POP_ETH
:
6668 case OVS_ACTION_ATTR_CLONE
:
6669 case OVS_ACTION_ATTR_PUSH_NSH
:
6670 case OVS_ACTION_ATTR_POP_NSH
:
6671 case OVS_ACTION_ATTR_CT_CLEAR
:
6672 case __OVS_ACTION_ATTR_MAX
:
6676 dp_packet_delete_batch(packets_
, should_steal
);
6680 dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
6681 struct dp_packet_batch
*packets
,
6682 bool should_steal
, const struct flow
*flow
,
6683 const struct nlattr
*actions
, size_t actions_len
)
6685 struct dp_netdev_execute_aux aux
= { pmd
, flow
};
6687 odp_execute_actions(&aux
, packets
, should_steal
, actions
,
6688 actions_len
, dp_execute_cb
);
6691 struct dp_netdev_ct_dump
{
6692 struct ct_dpif_dump_state up
;
6693 struct conntrack_dump dump
;
6694 struct conntrack
*ct
;
6695 struct dp_netdev
*dp
;
6699 dpif_netdev_ct_dump_start(struct dpif
*dpif
, struct ct_dpif_dump_state
**dump_
,
6700 const uint16_t *pzone
, int *ptot_bkts
)
6702 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6703 struct dp_netdev_ct_dump
*dump
;
6705 dump
= xzalloc(sizeof *dump
);
6707 dump
->ct
= &dp
->conntrack
;
6709 conntrack_dump_start(&dp
->conntrack
, &dump
->dump
, pzone
, ptot_bkts
);
6717 dpif_netdev_ct_dump_next(struct dpif
*dpif OVS_UNUSED
,
6718 struct ct_dpif_dump_state
*dump_
,
6719 struct ct_dpif_entry
*entry
)
6721 struct dp_netdev_ct_dump
*dump
;
6723 INIT_CONTAINER(dump
, dump_
, up
);
6725 return conntrack_dump_next(&dump
->dump
, entry
);
6729 dpif_netdev_ct_dump_done(struct dpif
*dpif OVS_UNUSED
,
6730 struct ct_dpif_dump_state
*dump_
)
6732 struct dp_netdev_ct_dump
*dump
;
6735 INIT_CONTAINER(dump
, dump_
, up
);
6737 err
= conntrack_dump_done(&dump
->dump
);
6745 dpif_netdev_ct_flush(struct dpif
*dpif
, const uint16_t *zone
,
6746 const struct ct_dpif_tuple
*tuple
)
6748 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6751 return conntrack_flush_tuple(&dp
->conntrack
, tuple
, zone
? *zone
: 0);
6753 return conntrack_flush(&dp
->conntrack
, zone
);
6757 dpif_netdev_ct_set_maxconns(struct dpif
*dpif
, uint32_t maxconns
)
6759 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6761 return conntrack_set_maxconns(&dp
->conntrack
, maxconns
);
6765 dpif_netdev_ct_get_maxconns(struct dpif
*dpif
, uint32_t *maxconns
)
6767 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6769 return conntrack_get_maxconns(&dp
->conntrack
, maxconns
);
6773 dpif_netdev_ct_get_nconns(struct dpif
*dpif
, uint32_t *nconns
)
6775 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6777 return conntrack_get_nconns(&dp
->conntrack
, nconns
);
6780 const struct dpif_class dpif_netdev_class
= {
6783 dpif_netdev_enumerate
,
6784 dpif_netdev_port_open_type
,
6787 dpif_netdev_destroy
,
6790 dpif_netdev_get_stats
,
6791 dpif_netdev_port_add
,
6792 dpif_netdev_port_del
,
6793 dpif_netdev_port_set_config
,
6794 dpif_netdev_port_query_by_number
,
6795 dpif_netdev_port_query_by_name
,
6796 NULL
, /* port_get_pid */
6797 dpif_netdev_port_dump_start
,
6798 dpif_netdev_port_dump_next
,
6799 dpif_netdev_port_dump_done
,
6800 dpif_netdev_port_poll
,
6801 dpif_netdev_port_poll_wait
,
6802 dpif_netdev_flow_flush
,
6803 dpif_netdev_flow_dump_create
,
6804 dpif_netdev_flow_dump_destroy
,
6805 dpif_netdev_flow_dump_thread_create
,
6806 dpif_netdev_flow_dump_thread_destroy
,
6807 dpif_netdev_flow_dump_next
,
6808 dpif_netdev_operate
,
6809 NULL
, /* recv_set */
6810 NULL
, /* handlers_set */
6811 dpif_netdev_set_config
,
6812 dpif_netdev_queue_to_priority
,
6814 NULL
, /* recv_wait */
6815 NULL
, /* recv_purge */
6816 dpif_netdev_register_dp_purge_cb
,
6817 dpif_netdev_register_upcall_cb
,
6818 dpif_netdev_enable_upcall
,
6819 dpif_netdev_disable_upcall
,
6820 dpif_netdev_get_datapath_version
,
6821 dpif_netdev_ct_dump_start
,
6822 dpif_netdev_ct_dump_next
,
6823 dpif_netdev_ct_dump_done
,
6824 dpif_netdev_ct_flush
,
6825 dpif_netdev_ct_set_maxconns
,
6826 dpif_netdev_ct_get_maxconns
,
6827 dpif_netdev_ct_get_nconns
,
6828 dpif_netdev_meter_get_features
,
6829 dpif_netdev_meter_set
,
6830 dpif_netdev_meter_get
,
6831 dpif_netdev_meter_del
,
6835 dpif_dummy_change_port_number(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
6836 const char *argv
[], void *aux OVS_UNUSED
)
6838 struct dp_netdev_port
*port
;
6839 struct dp_netdev
*dp
;
6842 ovs_mutex_lock(&dp_netdev_mutex
);
6843 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
6844 if (!dp
|| !dpif_netdev_class_is_dummy(dp
->class)) {
6845 ovs_mutex_unlock(&dp_netdev_mutex
);
6846 unixctl_command_reply_error(conn
, "unknown datapath or not a dummy");
6849 ovs_refcount_ref(&dp
->ref_cnt
);
6850 ovs_mutex_unlock(&dp_netdev_mutex
);
6852 ovs_mutex_lock(&dp
->port_mutex
);
6853 if (get_port_by_name(dp
, argv
[2], &port
)) {
6854 unixctl_command_reply_error(conn
, "unknown port");
6858 port_no
= u32_to_odp(atoi(argv
[3]));
6859 if (!port_no
|| port_no
== ODPP_NONE
) {
6860 unixctl_command_reply_error(conn
, "bad port number");
6863 if (dp_netdev_lookup_port(dp
, port_no
)) {
6864 unixctl_command_reply_error(conn
, "port number already in use");
6869 hmap_remove(&dp
->ports
, &port
->node
);
6870 reconfigure_datapath(dp
);
6872 /* Reinsert with new port number. */
6873 port
->port_no
= port_no
;
6874 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
6875 reconfigure_datapath(dp
);
6877 seq_change(dp
->port_seq
);
6878 unixctl_command_reply(conn
, NULL
);
6881 ovs_mutex_unlock(&dp
->port_mutex
);
6882 dp_netdev_unref(dp
);
6886 dpif_dummy_register__(const char *type
)
6888 struct dpif_class
*class;
6890 class = xmalloc(sizeof *class);
6891 *class = dpif_netdev_class
;
6892 class->type
= xstrdup(type
);
6893 dp_register_provider(class);
6897 dpif_dummy_override(const char *type
)
6902 * Ignore EAFNOSUPPORT to allow --enable-dummy=system with
6903 * a userland-only build. It's useful for testsuite.
6905 error
= dp_unregister_provider(type
);
6906 if (error
== 0 || error
== EAFNOSUPPORT
) {
6907 dpif_dummy_register__(type
);
6912 dpif_dummy_register(enum dummy_level level
)
6914 if (level
== DUMMY_OVERRIDE_ALL
) {
6919 dp_enumerate_types(&types
);
6920 SSET_FOR_EACH (type
, &types
) {
6921 dpif_dummy_override(type
);
6923 sset_destroy(&types
);
6924 } else if (level
== DUMMY_OVERRIDE_SYSTEM
) {
6925 dpif_dummy_override("system");
6928 dpif_dummy_register__("dummy");
6930 unixctl_command_register("dpif-dummy/change-port-number",
6931 "dp port new-number",
6932 3, 3, dpif_dummy_change_port_number
, NULL
);
6935 /* Datapath Classifier. */
6937 /* A set of rules that all have the same fields wildcarded. */
6938 struct dpcls_subtable
{
6939 /* The fields are only used by writers. */
6940 struct cmap_node cmap_node OVS_GUARDED
; /* Within dpcls 'subtables_map'. */
6942 /* These fields are accessed by readers. */
6943 struct cmap rules
; /* Contains "struct dpcls_rule"s. */
6944 uint32_t hit_cnt
; /* Number of match hits in subtable in current
6945 optimization interval. */
6946 struct netdev_flow_key mask
; /* Wildcards for fields (const). */
6947 /* 'mask' must be the last field, additional space is allocated here. */
6950 /* Initializes 'cls' as a classifier that initially contains no classification
6953 dpcls_init(struct dpcls
*cls
)
6955 cmap_init(&cls
->subtables_map
);
6956 pvector_init(&cls
->subtables
);
6960 dpcls_destroy_subtable(struct dpcls
*cls
, struct dpcls_subtable
*subtable
)
6962 VLOG_DBG("Destroying subtable %p for in_port %d", subtable
, cls
->in_port
);
6963 pvector_remove(&cls
->subtables
, subtable
);
6964 cmap_remove(&cls
->subtables_map
, &subtable
->cmap_node
,
6965 subtable
->mask
.hash
);
6966 cmap_destroy(&subtable
->rules
);
6967 ovsrcu_postpone(free
, subtable
);
6970 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
6971 * caller's responsibility.
6972 * May only be called after all the readers have been terminated. */
6974 dpcls_destroy(struct dpcls
*cls
)
6977 struct dpcls_subtable
*subtable
;
6979 CMAP_FOR_EACH (subtable
, cmap_node
, &cls
->subtables_map
) {
6980 ovs_assert(cmap_count(&subtable
->rules
) == 0);
6981 dpcls_destroy_subtable(cls
, subtable
);
6983 cmap_destroy(&cls
->subtables_map
);
6984 pvector_destroy(&cls
->subtables
);
6988 static struct dpcls_subtable
*
6989 dpcls_create_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
6991 struct dpcls_subtable
*subtable
;
6993 /* Need to add one. */
6994 subtable
= xmalloc(sizeof *subtable
6995 - sizeof subtable
->mask
.mf
+ mask
->len
);
6996 cmap_init(&subtable
->rules
);
6997 subtable
->hit_cnt
= 0;
6998 netdev_flow_key_clone(&subtable
->mask
, mask
);
6999 cmap_insert(&cls
->subtables_map
, &subtable
->cmap_node
, mask
->hash
);
7000 /* Add the new subtable at the end of the pvector (with no hits yet) */
7001 pvector_insert(&cls
->subtables
, subtable
, 0);
7002 VLOG_DBG("Creating %"PRIuSIZE
". subtable %p for in_port %d",
7003 cmap_count(&cls
->subtables_map
), subtable
, cls
->in_port
);
7004 pvector_publish(&cls
->subtables
);
7009 static inline struct dpcls_subtable
*
7010 dpcls_find_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
7012 struct dpcls_subtable
*subtable
;
7014 CMAP_FOR_EACH_WITH_HASH (subtable
, cmap_node
, mask
->hash
,
7015 &cls
->subtables_map
) {
7016 if (netdev_flow_key_equal(&subtable
->mask
, mask
)) {
7020 return dpcls_create_subtable(cls
, mask
);
7024 /* Periodically sort the dpcls subtable vectors according to hit counts */
7026 dpcls_sort_subtable_vector(struct dpcls
*cls
)
7028 struct pvector
*pvec
= &cls
->subtables
;
7029 struct dpcls_subtable
*subtable
;
7031 PVECTOR_FOR_EACH (subtable
, pvec
) {
7032 pvector_change_priority(pvec
, subtable
, subtable
->hit_cnt
);
7033 subtable
->hit_cnt
= 0;
7035 pvector_publish(pvec
);
7039 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread
*pmd
,
7040 struct polled_queue
*poll_list
, int poll_cnt
)
7044 if (pmd
->ctx
.now
> pmd
->rxq_next_cycle_store
) {
7046 /* Get the cycles that were used to process each queue and store. */
7047 for (unsigned i
= 0; i
< poll_cnt
; i
++) {
7048 uint64_t rxq_cyc_curr
= dp_netdev_rxq_get_cycles(poll_list
[i
].rxq
,
7049 RXQ_CYCLES_PROC_CURR
);
7050 dp_netdev_rxq_set_intrvl_cycles(poll_list
[i
].rxq
, rxq_cyc_curr
);
7051 dp_netdev_rxq_set_cycles(poll_list
[i
].rxq
, RXQ_CYCLES_PROC_CURR
,
7054 curr_tsc
= cycles_counter_update(&pmd
->perf_stats
);
7055 if (pmd
->intrvl_tsc_prev
) {
7056 /* There is a prev timestamp, store a new intrvl cycle count. */
7057 atomic_store_relaxed(&pmd
->intrvl_cycles
,
7058 curr_tsc
- pmd
->intrvl_tsc_prev
);
7060 pmd
->intrvl_tsc_prev
= curr_tsc
;
7061 /* Start new measuring interval */
7062 pmd
->rxq_next_cycle_store
= pmd
->ctx
.now
+ PMD_RXQ_INTERVAL_LEN
;
7065 if (pmd
->ctx
.now
> pmd
->next_optimization
) {
7066 /* Try to obtain the flow lock to block out revalidator threads.
7067 * If not possible, just try next time. */
7068 if (!ovs_mutex_trylock(&pmd
->flow_mutex
)) {
7069 /* Optimize each classifier */
7070 CMAP_FOR_EACH (cls
, node
, &pmd
->classifiers
) {
7071 dpcls_sort_subtable_vector(cls
);
7073 ovs_mutex_unlock(&pmd
->flow_mutex
);
7074 /* Start new measuring interval */
7075 pmd
->next_optimization
= pmd
->ctx
.now
7076 + DPCLS_OPTIMIZATION_INTERVAL
;
7081 /* Insert 'rule' into 'cls'. */
7083 dpcls_insert(struct dpcls
*cls
, struct dpcls_rule
*rule
,
7084 const struct netdev_flow_key
*mask
)
7086 struct dpcls_subtable
*subtable
= dpcls_find_subtable(cls
, mask
);
7088 /* Refer to subtable's mask, also for later removal. */
7089 rule
->mask
= &subtable
->mask
;
7090 cmap_insert(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
);
7093 /* Removes 'rule' from 'cls', also destructing the 'rule'. */
7095 dpcls_remove(struct dpcls
*cls
, struct dpcls_rule
*rule
)
7097 struct dpcls_subtable
*subtable
;
7099 ovs_assert(rule
->mask
);
7101 /* Get subtable from reference in rule->mask. */
7102 INIT_CONTAINER(subtable
, rule
->mask
, mask
);
7103 if (cmap_remove(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
)
7105 /* Delete empty subtable. */
7106 dpcls_destroy_subtable(cls
, subtable
);
7107 pvector_publish(&cls
->subtables
);
7111 /* Returns true if 'target' satisfies 'key' in 'mask', that is, if each 1-bit
7112 * in 'mask' the values in 'key' and 'target' are the same. */
7114 dpcls_rule_matches_key(const struct dpcls_rule
*rule
,
7115 const struct netdev_flow_key
*target
)
7117 const uint64_t *keyp
= miniflow_get_values(&rule
->flow
.mf
);
7118 const uint64_t *maskp
= miniflow_get_values(&rule
->mask
->mf
);
7121 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, target
, rule
->flow
.mf
.map
) {
7122 if (OVS_UNLIKELY((value
& *maskp
++) != *keyp
++)) {
7129 /* For each miniflow in 'keys' performs a classifier lookup writing the result
7130 * into the corresponding slot in 'rules'. If a particular entry in 'keys' is
7131 * NULL it is skipped.
7133 * This function is optimized for use in the userspace datapath and therefore
7134 * does not implement a lot of features available in the standard
7135 * classifier_lookup() function. Specifically, it does not implement
7136 * priorities, instead returning any rule which matches the flow.
7138 * Returns true if all miniflows found a corresponding rule. */
7140 dpcls_lookup(struct dpcls
*cls
, const struct netdev_flow_key
*keys
[],
7141 struct dpcls_rule
**rules
, const size_t cnt
,
7144 /* The received 'cnt' miniflows are the search-keys that will be processed
7145 * to find a matching entry into the available subtables.
7146 * The number of bits in map_type is equal to NETDEV_MAX_BURST. */
7147 typedef uint32_t map_type
;
7148 #define MAP_BITS (sizeof(map_type) * CHAR_BIT)
7149 BUILD_ASSERT_DECL(MAP_BITS
>= NETDEV_MAX_BURST
);
7151 struct dpcls_subtable
*subtable
;
7153 map_type keys_map
= TYPE_MAXIMUM(map_type
); /* Set all bits. */
7155 uint32_t hashes
[MAP_BITS
];
7156 const struct cmap_node
*nodes
[MAP_BITS
];
7158 if (cnt
!= MAP_BITS
) {
7159 keys_map
>>= MAP_BITS
- cnt
; /* Clear extra bits. */
7161 memset(rules
, 0, cnt
* sizeof *rules
);
7163 int lookups_match
= 0, subtable_pos
= 1;
7165 /* The Datapath classifier - aka dpcls - is composed of subtables.
7166 * Subtables are dynamically created as needed when new rules are inserted.
7167 * Each subtable collects rules with matches on a specific subset of packet
7168 * fields as defined by the subtable's mask. We proceed to process every
7169 * search-key against each subtable, but when a match is found for a
7170 * search-key, the search for that key can stop because the rules are
7171 * non-overlapping. */
7172 PVECTOR_FOR_EACH (subtable
, &cls
->subtables
) {
7175 /* Compute hashes for the remaining keys. Each search-key is
7176 * masked with the subtable's mask to avoid hashing the wildcarded
7178 ULLONG_FOR_EACH_1(i
, keys_map
) {
7179 hashes
[i
] = netdev_flow_key_hash_in_mask(keys
[i
],
7183 found_map
= cmap_find_batch(&subtable
->rules
, keys_map
, hashes
, nodes
);
7184 /* Check results. When the i-th bit of found_map is set, it means
7185 * that a set of nodes with a matching hash value was found for the
7186 * i-th search-key. Due to possible hash collisions we need to check
7187 * which of the found rules, if any, really matches our masked
7189 ULLONG_FOR_EACH_1(i
, found_map
) {
7190 struct dpcls_rule
*rule
;
7192 CMAP_NODE_FOR_EACH (rule
, cmap_node
, nodes
[i
]) {
7193 if (OVS_LIKELY(dpcls_rule_matches_key(rule
, keys
[i
]))) {
7195 /* Even at 20 Mpps the 32-bit hit_cnt cannot wrap
7196 * within one second optimization interval. */
7197 subtable
->hit_cnt
++;
7198 lookups_match
+= subtable_pos
;
7202 /* None of the found rules was a match. Reset the i-th bit to
7203 * keep searching this key in the next subtable. */
7204 ULLONG_SET0(found_map
, i
); /* Did not match. */
7206 ; /* Keep Sparse happy. */
7208 keys_map
&= ~found_map
; /* Clear the found rules. */
7210 if (num_lookups_p
) {
7211 *num_lookups_p
= lookups_match
;
7213 return true; /* All found. */
7217 if (num_lookups_p
) {
7218 *num_lookups_p
= lookups_match
;
7220 return false; /* Some misses. */