2 * Copyright (c) 2009-2014, 2016-2018 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-offload.h"
53 #include "netdev-provider.h"
54 #include "netdev-vport.h"
56 #include "odp-execute.h"
58 #include "openvswitch/dynamic-string.h"
59 #include "openvswitch/list.h"
60 #include "openvswitch/match.h"
61 #include "openvswitch/ofp-parse.h"
62 #include "openvswitch/ofp-print.h"
63 #include "openvswitch/ofpbuf.h"
64 #include "openvswitch/shash.h"
65 #include "openvswitch/vlog.h"
69 #include "openvswitch/poll-loop.h"
76 #include "tnl-neigh-cache.h"
77 #include "tnl-ports.h"
82 VLOG_DEFINE_THIS_MODULE(dpif_netdev
);
84 /* Auto Load Balancing Defaults */
85 #define ALB_ACCEPTABLE_IMPROVEMENT 25
86 #define ALB_PMD_LOAD_THRESHOLD 95
87 #define ALB_PMD_REBALANCE_POLL_INTERVAL 1 /* 1 Min */
88 #define MIN_TO_MSEC 60000
90 #define FLOW_DUMP_MAX_BATCH 50
91 /* Use per thread recirc_depth to prevent recirculation loop. */
92 #define MAX_RECIRC_DEPTH 6
93 DEFINE_STATIC_PER_THREAD_DATA(uint32_t, recirc_depth
, 0)
95 /* Use instant packet send by default. */
96 #define DEFAULT_TX_FLUSH_INTERVAL 0
98 /* Configuration parameters. */
99 enum { MAX_FLOWS
= 65536 }; /* Maximum number of flows in flow table. */
100 enum { MAX_METERS
= 65536 }; /* Maximum number of meters. */
101 enum { MAX_BANDS
= 8 }; /* Maximum number of bands / meter. */
102 enum { N_METER_LOCKS
= 64 }; /* Maximum number of meters. */
104 /* Protects against changes to 'dp_netdevs'. */
105 static struct ovs_mutex dp_netdev_mutex
= OVS_MUTEX_INITIALIZER
;
107 /* Contains all 'struct dp_netdev's. */
108 static struct shash dp_netdevs
OVS_GUARDED_BY(dp_netdev_mutex
)
109 = SHASH_INITIALIZER(&dp_netdevs
);
111 static struct vlog_rate_limit upcall_rl
= VLOG_RATE_LIMIT_INIT(600, 600);
113 #define DP_NETDEV_CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \
114 | CS_INVALID | CS_REPLY_DIR | CS_TRACKED \
115 | CS_SRC_NAT | CS_DST_NAT)
116 #define DP_NETDEV_CS_UNSUPPORTED_MASK (~(uint32_t)DP_NETDEV_CS_SUPPORTED_MASK)
118 static struct odp_support dp_netdev_support
= {
119 .max_vlan_headers
= SIZE_MAX
,
120 .max_mpls_depth
= SIZE_MAX
,
126 .ct_state_nat
= true,
127 .ct_orig_tuple
= true,
128 .ct_orig_tuple6
= true,
131 /* Stores a miniflow with inline values */
133 struct netdev_flow_key
{
134 uint32_t hash
; /* Hash function differs for different users. */
135 uint32_t len
; /* Length of the following miniflow (incl. map). */
137 uint64_t buf
[FLOW_MAX_PACKET_U64S
];
140 /* EMC cache and SMC cache compose the datapath flow cache (DFC)
142 * Exact match cache for frequently used flows
144 * The cache uses a 32-bit hash of the packet (which can be the RSS hash) to
145 * search its entries for a miniflow that matches exactly the miniflow of the
146 * packet. It stores the 'dpcls_rule' (rule) that matches the miniflow.
148 * A cache entry holds a reference to its 'dp_netdev_flow'.
150 * A miniflow with a given hash can be in one of EM_FLOW_HASH_SEGS different
151 * entries. The 32-bit hash is split into EM_FLOW_HASH_SEGS values (each of
152 * them is EM_FLOW_HASH_SHIFT bits wide and the remainder is thrown away). Each
153 * value is the index of a cache entry where the miniflow could be.
156 * Signature match cache (SMC)
158 * This cache stores a 16-bit signature for each flow without storing keys, and
159 * stores the corresponding 16-bit flow_table index to the 'dp_netdev_flow'.
160 * Each flow thus occupies 32bit which is much more memory efficient than EMC.
161 * SMC uses a set-associative design that each bucket contains
162 * SMC_ENTRY_PER_BUCKET number of entries.
163 * Since 16-bit flow_table index is used, if there are more than 2^16
164 * dp_netdev_flow, SMC will miss them that cannot be indexed by a 16-bit value.
170 * Each pmd_thread has its own private exact match cache.
171 * If dp_netdev_input is not called from a pmd thread, a mutex is used.
174 #define EM_FLOW_HASH_SHIFT 13
175 #define EM_FLOW_HASH_ENTRIES (1u << EM_FLOW_HASH_SHIFT)
176 #define EM_FLOW_HASH_MASK (EM_FLOW_HASH_ENTRIES - 1)
177 #define EM_FLOW_HASH_SEGS 2
179 /* SMC uses a set-associative design. A bucket contains a set of entries that
180 * a flow item can occupy. For now, it uses one hash function rather than two
181 * as for the EMC design. */
182 #define SMC_ENTRY_PER_BUCKET 4
183 #define SMC_ENTRIES (1u << 20)
184 #define SMC_BUCKET_CNT (SMC_ENTRIES / SMC_ENTRY_PER_BUCKET)
185 #define SMC_MASK (SMC_BUCKET_CNT - 1)
187 /* Default EMC insert probability is 1 / DEFAULT_EM_FLOW_INSERT_INV_PROB */
188 #define DEFAULT_EM_FLOW_INSERT_INV_PROB 100
189 #define DEFAULT_EM_FLOW_INSERT_MIN (UINT32_MAX / \
190 DEFAULT_EM_FLOW_INSERT_INV_PROB)
193 struct dp_netdev_flow
*flow
;
194 struct netdev_flow_key key
; /* key.hash used for emc hash value. */
198 struct emc_entry entries
[EM_FLOW_HASH_ENTRIES
];
199 int sweep_idx
; /* For emc_cache_slow_sweep(). */
203 uint16_t sig
[SMC_ENTRY_PER_BUCKET
];
204 uint16_t flow_idx
[SMC_ENTRY_PER_BUCKET
];
207 /* Signature match cache, differentiate from EMC cache */
209 struct smc_bucket buckets
[SMC_BUCKET_CNT
];
213 struct emc_cache emc_cache
;
214 struct smc_cache smc_cache
;
217 /* Iterate in the exact match cache through every entry that might contain a
218 * miniflow with hash 'HASH'. */
219 #define EMC_FOR_EACH_POS_WITH_HASH(EMC, CURRENT_ENTRY, HASH) \
220 for (uint32_t i__ = 0, srch_hash__ = (HASH); \
221 (CURRENT_ENTRY) = &(EMC)->entries[srch_hash__ & EM_FLOW_HASH_MASK], \
222 i__ < EM_FLOW_HASH_SEGS; \
223 i__++, srch_hash__ >>= EM_FLOW_HASH_SHIFT)
225 /* Simple non-wildcarding single-priority classifier. */
227 /* Time in microseconds between successive optimizations of the dpcls
229 #define DPCLS_OPTIMIZATION_INTERVAL 1000000LL
231 /* Time in microseconds of the interval in which rxq processing cycles used
232 * in rxq to pmd assignments is measured and stored. */
233 #define PMD_RXQ_INTERVAL_LEN 10000000LL
235 /* Number of intervals for which cycles are stored
236 * and used during rxq to pmd assignment. */
237 #define PMD_RXQ_INTERVAL_MAX 6
240 struct cmap_node node
; /* Within dp_netdev_pmd_thread.classifiers */
242 struct cmap subtables_map
;
243 struct pvector subtables
;
246 /* A rule to be inserted to the classifier. */
248 struct cmap_node cmap_node
; /* Within struct dpcls_subtable 'rules'. */
249 struct netdev_flow_key
*mask
; /* Subtable's mask. */
250 struct netdev_flow_key flow
; /* Matching key. */
251 /* 'flow' must be the last field, additional space is allocated here. */
254 /* Data structure to keep packet order till fastpath processing. */
255 struct dp_packet_flow_map
{
256 struct dp_packet
*packet
;
257 struct dp_netdev_flow
*flow
;
261 static void dpcls_init(struct dpcls
*);
262 static void dpcls_destroy(struct dpcls
*);
263 static void dpcls_sort_subtable_vector(struct dpcls
*);
264 static void dpcls_insert(struct dpcls
*, struct dpcls_rule
*,
265 const struct netdev_flow_key
*mask
);
266 static void dpcls_remove(struct dpcls
*, struct dpcls_rule
*);
267 static bool dpcls_lookup(struct dpcls
*cls
,
268 const struct netdev_flow_key
*keys
[],
269 struct dpcls_rule
**rules
, size_t cnt
,
271 static bool dpcls_rule_matches_key(const struct dpcls_rule
*rule
,
272 const struct netdev_flow_key
*target
);
273 /* Set of supported meter flags */
274 #define DP_SUPPORTED_METER_FLAGS_MASK \
275 (OFPMF13_STATS | OFPMF13_PKTPS | OFPMF13_KBPS | OFPMF13_BURST)
277 /* Set of supported meter band types */
278 #define DP_SUPPORTED_METER_BAND_TYPES \
279 ( 1 << OFPMBT13_DROP )
281 struct dp_meter_band
{
282 struct ofputil_meter_band up
; /* type, prec_level, pad, rate, burst_size */
283 uint32_t bucket
; /* In 1/1000 packets (for PKTPS), or in bits (for KBPS) */
284 uint64_t packet_count
;
291 uint32_t max_delta_t
;
293 uint64_t packet_count
;
295 struct dp_meter_band bands
[];
299 bool auto_lb_requested
; /* Auto load balancing requested by user. */
300 bool is_enabled
; /* Current status of Auto load balancing. */
301 uint64_t rebalance_intvl
;
302 uint64_t rebalance_poll_timer
;
305 /* Datapath based on the network device interface from netdev.h.
311 * Some members, marked 'const', are immutable. Accessing other members
312 * requires synchronization, as noted in more detail below.
314 * Acquisition order is, from outermost to innermost:
316 * dp_netdev_mutex (global)
321 const struct dpif_class
*const class;
322 const char *const name
;
324 struct ovs_refcount ref_cnt
;
325 atomic_flag destroyed
;
329 * Any lookup into 'ports' or any access to the dp_netdev_ports found
330 * through 'ports' requires taking 'port_mutex'. */
331 struct ovs_mutex port_mutex
;
333 struct seq
*port_seq
; /* Incremented whenever a port changes. */
335 /* The time that a packet can wait in output batch for sending. */
336 atomic_uint32_t tx_flush_interval
;
339 struct ovs_mutex meter_locks
[N_METER_LOCKS
];
340 struct dp_meter
*meters
[MAX_METERS
]; /* Meter bands. */
342 /* Probability of EMC insertions is a factor of 'emc_insert_min'.*/
343 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
) atomic_uint32_t emc_insert_min
;
344 /* Enable collection of PMD performance metrics. */
345 atomic_bool pmd_perf_metrics
;
346 /* Enable the SMC cache from ovsdb config */
347 atomic_bool smc_enable_db
;
349 /* Protects access to ofproto-dpif-upcall interface during revalidator
350 * thread synchronization. */
351 struct fat_rwlock upcall_rwlock
;
352 upcall_callback
*upcall_cb
; /* Callback function for executing upcalls. */
355 /* Callback function for notifying the purging of dp flows (during
356 * reseting pmd deletion). */
357 dp_purge_callback
*dp_purge_cb
;
360 /* Stores all 'struct dp_netdev_pmd_thread's. */
361 struct cmap poll_threads
;
362 /* id pool for per thread static_tx_qid. */
363 struct id_pool
*tx_qid_pool
;
364 struct ovs_mutex tx_qid_pool_mutex
;
365 /* Use measured cycles for rxq to pmd assignment. */
366 bool pmd_rxq_assign_cyc
;
368 /* Protects the access of the 'struct dp_netdev_pmd_thread'
369 * instance for non-pmd thread. */
370 struct ovs_mutex non_pmd_mutex
;
372 /* Each pmd thread will store its pointer to
373 * 'struct dp_netdev_pmd_thread' in 'per_pmd_key'. */
374 ovsthread_key_t per_pmd_key
;
376 struct seq
*reconfigure_seq
;
377 uint64_t last_reconfigure_seq
;
379 /* Cpu mask for pin of pmd threads. */
382 uint64_t last_tnl_conf_seq
;
384 struct conntrack
*conntrack
;
385 struct pmd_auto_lb pmd_alb
;
388 static void meter_lock(const struct dp_netdev
*dp
, uint32_t meter_id
)
389 OVS_ACQUIRES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
391 ovs_mutex_lock(&dp
->meter_locks
[meter_id
% N_METER_LOCKS
]);
394 static void meter_unlock(const struct dp_netdev
*dp
, uint32_t meter_id
)
395 OVS_RELEASES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
397 ovs_mutex_unlock(&dp
->meter_locks
[meter_id
% N_METER_LOCKS
]);
401 static struct dp_netdev_port
*dp_netdev_lookup_port(const struct dp_netdev
*dp
,
403 OVS_REQUIRES(dp
->port_mutex
);
405 enum rxq_cycles_counter_type
{
406 RXQ_CYCLES_PROC_CURR
, /* Cycles spent successfully polling and
407 processing packets during the current
409 RXQ_CYCLES_PROC_HIST
, /* Total cycles of all intervals that are used
410 during rxq to pmd assignment. */
415 DP_NETDEV_FLOW_OFFLOAD_OP_ADD
,
416 DP_NETDEV_FLOW_OFFLOAD_OP_MOD
,
417 DP_NETDEV_FLOW_OFFLOAD_OP_DEL
,
420 struct dp_flow_offload_item
{
421 struct dp_netdev_pmd_thread
*pmd
;
422 struct dp_netdev_flow
*flow
;
425 struct nlattr
*actions
;
428 struct ovs_list node
;
431 struct dp_flow_offload
{
432 struct ovs_mutex mutex
;
433 struct ovs_list list
;
437 static struct dp_flow_offload dp_flow_offload
= {
438 .mutex
= OVS_MUTEX_INITIALIZER
,
439 .list
= OVS_LIST_INITIALIZER(&dp_flow_offload
.list
),
442 static struct ovsthread_once offload_thread_once
443 = OVSTHREAD_ONCE_INITIALIZER
;
445 #define XPS_TIMEOUT 500000LL /* In microseconds. */
447 /* Contained by struct dp_netdev_port's 'rxqs' member. */
448 struct dp_netdev_rxq
{
449 struct dp_netdev_port
*port
;
450 struct netdev_rxq
*rx
;
451 unsigned core_id
; /* Core to which this queue should be
452 pinned. OVS_CORE_UNSPEC if the
453 queue doesn't need to be pinned to a
455 unsigned intrvl_idx
; /* Write index for 'cycles_intrvl'. */
456 struct dp_netdev_pmd_thread
*pmd
; /* pmd thread that polls this queue. */
457 bool is_vhost
; /* Is rxq of a vhost port. */
459 /* Counters of cycles spent successfully polling and processing pkts. */
460 atomic_ullong cycles
[RXQ_N_CYCLES
];
461 /* We store PMD_RXQ_INTERVAL_MAX intervals of data for an rxq and then
462 sum them to yield the cycles used for an rxq. */
463 atomic_ullong cycles_intrvl
[PMD_RXQ_INTERVAL_MAX
];
466 /* A port in a netdev-based datapath. */
467 struct dp_netdev_port
{
469 bool dynamic_txqs
; /* If true XPS will be used. */
470 bool need_reconfigure
; /* True if we should reconfigure netdev. */
471 struct netdev
*netdev
;
472 struct hmap_node node
; /* Node in dp_netdev's 'ports'. */
473 struct netdev_saved_flags
*sf
;
474 struct dp_netdev_rxq
*rxqs
;
475 unsigned n_rxq
; /* Number of elements in 'rxqs' */
476 unsigned *txq_used
; /* Number of threads that use each tx queue. */
477 struct ovs_mutex txq_used_mutex
;
478 bool emc_enabled
; /* If true EMC will be used. */
479 char *type
; /* Port type as requested by user. */
480 char *rxq_affinity_list
; /* Requested affinity of rx queues. */
483 /* Contained by struct dp_netdev_flow's 'stats' member. */
484 struct dp_netdev_flow_stats
{
485 atomic_llong used
; /* Last used time, in monotonic msecs. */
486 atomic_ullong packet_count
; /* Number of packets matched. */
487 atomic_ullong byte_count
; /* Number of bytes matched. */
488 atomic_uint16_t tcp_flags
; /* Bitwise-OR of seen tcp_flags values. */
491 /* A flow in 'dp_netdev_pmd_thread's 'flow_table'.
497 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
498 * its pmd thread's classifier. The text below calls this classifier 'cls'.
503 * The thread safety rules described here for "struct dp_netdev_flow" are
504 * motivated by two goals:
506 * - Prevent threads that read members of "struct dp_netdev_flow" from
507 * reading bad data due to changes by some thread concurrently modifying
510 * - Prevent two threads making changes to members of a given "struct
511 * dp_netdev_flow" from interfering with each other.
517 * A flow 'flow' may be accessed without a risk of being freed during an RCU
518 * grace period. Code that needs to hold onto a flow for a while
519 * should try incrementing 'flow->ref_cnt' with dp_netdev_flow_ref().
521 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
522 * flow from being deleted from 'cls' and it doesn't protect members of 'flow'
525 * Some members, marked 'const', are immutable. Accessing other members
526 * requires synchronization, as noted in more detail below.
528 struct dp_netdev_flow
{
529 const struct flow flow
; /* Unmasked flow that created this entry. */
530 /* Hash table index by unmasked flow. */
531 const struct cmap_node node
; /* In owning dp_netdev_pmd_thread's */
533 const struct cmap_node mark_node
; /* In owning flow_mark's mark_to_flow */
534 const ovs_u128 ufid
; /* Unique flow identifier. */
535 const ovs_u128 mega_ufid
; /* Unique mega flow identifier. */
536 const unsigned pmd_id
; /* The 'core_id' of pmd thread owning this */
539 /* Number of references.
540 * The classifier owns one reference.
541 * Any thread trying to keep a rule from being freed should hold its own
543 struct ovs_refcount ref_cnt
;
546 uint32_t mark
; /* Unique flow mark assigned to a flow */
549 struct dp_netdev_flow_stats stats
;
552 OVSRCU_TYPE(struct dp_netdev_actions
*) actions
;
554 /* While processing a group of input packets, the datapath uses the next
555 * member to store a pointer to the output batch for the flow. It is
556 * reset after the batch has been sent out (See dp_netdev_queue_batches(),
557 * packet_batch_per_flow_init() and packet_batch_per_flow_execute()). */
558 struct packet_batch_per_flow
*batch
;
560 /* Packet classification. */
561 struct dpcls_rule cr
; /* In owning dp_netdev's 'cls'. */
562 /* 'cr' must be the last member. */
565 static void dp_netdev_flow_unref(struct dp_netdev_flow
*);
566 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*);
567 static int dpif_netdev_flow_from_nlattrs(const struct nlattr
*, uint32_t,
568 struct flow
*, bool);
570 /* A set of datapath actions within a "struct dp_netdev_flow".
576 * A struct dp_netdev_actions 'actions' is protected with RCU. */
577 struct dp_netdev_actions
{
578 /* These members are immutable: they do not change during the struct's
580 unsigned int size
; /* Size of 'actions', in bytes. */
581 struct nlattr actions
[]; /* Sequence of OVS_ACTION_ATTR_* attributes. */
584 struct dp_netdev_actions
*dp_netdev_actions_create(const struct nlattr
*,
586 struct dp_netdev_actions
*dp_netdev_flow_get_actions(
587 const struct dp_netdev_flow
*);
588 static void dp_netdev_actions_free(struct dp_netdev_actions
*);
590 struct polled_queue
{
591 struct dp_netdev_rxq
*rxq
;
598 /* Contained by struct dp_netdev_pmd_thread's 'poll_list' member. */
600 struct dp_netdev_rxq
*rxq
;
601 struct hmap_node node
;
604 /* Contained by struct dp_netdev_pmd_thread's 'send_port_cache',
605 * 'tnl_port_cache' or 'tx_ports'. */
607 struct dp_netdev_port
*port
;
610 struct hmap_node node
;
611 long long flush_time
;
612 struct dp_packet_batch output_pkts
;
613 struct dp_netdev_rxq
*output_pkts_rxqs
[NETDEV_MAX_BURST
];
616 /* A set of properties for the current processing loop that is not directly
617 * associated with the pmd thread itself, but with the packets being
618 * processed or the short-term system configuration (for example, time).
619 * Contained by struct dp_netdev_pmd_thread's 'ctx' member. */
620 struct dp_netdev_pmd_thread_ctx
{
621 /* Latest measured time. See 'pmd_thread_ctx_time_update()'. */
623 /* RX queue from which last packet was received. */
624 struct dp_netdev_rxq
*last_rxq
;
625 /* EMC insertion probability context for the current processing cycle. */
626 uint32_t emc_insert_min
;
629 /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
630 * the performance overhead of interrupt processing. Therefore netdev can
631 * not implement rx-wait for these devices. dpif-netdev needs to poll
632 * these device to check for recv buffer. pmd-thread does polling for
633 * devices assigned to itself.
635 * DPDK used PMD for accessing NIC.
637 * Note, instance with cpu core id NON_PMD_CORE_ID will be reserved for
638 * I/O of all non-pmd threads. There will be no actual thread created
641 * Each struct has its own flow cache and classifier per managed ingress port.
642 * For packets received on ingress port, a look up is done on corresponding PMD
643 * thread's flow cache and in case of a miss, lookup is performed in the
644 * corresponding classifier of port. Packets are executed with the found
645 * actions in either case.
647 struct dp_netdev_pmd_thread
{
648 struct dp_netdev
*dp
;
649 struct ovs_refcount ref_cnt
; /* Every reference must be refcount'ed. */
650 struct cmap_node node
; /* In 'dp->poll_threads'. */
652 /* Per thread exact-match cache. Note, the instance for cpu core
653 * NON_PMD_CORE_ID can be accessed by multiple threads, and thusly
654 * need to be protected by 'non_pmd_mutex'. Every other instance
655 * will only be accessed by its own pmd thread. */
656 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
) struct dfc_cache flow_cache
;
658 /* Flow-Table and classifiers
660 * Writers of 'flow_table' must take the 'flow_mutex'. Corresponding
661 * changes to 'classifiers' must be made while still holding the
664 struct ovs_mutex flow_mutex
;
665 struct cmap flow_table OVS_GUARDED
; /* Flow table. */
667 /* One classifier per in_port polled by the pmd */
668 struct cmap classifiers
;
669 /* Periodically sort subtable vectors according to hit frequencies */
670 long long int next_optimization
;
671 /* End of the next time interval for which processing cycles
672 are stored for each polled rxq. */
673 long long int rxq_next_cycle_store
;
675 /* Last interval timestamp. */
676 uint64_t intrvl_tsc_prev
;
677 /* Last interval cycles. */
678 atomic_ullong intrvl_cycles
;
680 /* Current context of the PMD thread. */
681 struct dp_netdev_pmd_thread_ctx ctx
;
683 struct seq
*reload_seq
;
684 uint64_t last_reload_seq
;
685 atomic_bool reload
; /* Do we need to reload ports? */
686 atomic_bool exit
; /* For terminating the pmd thread. */
688 unsigned core_id
; /* CPU core id of this pmd thread. */
689 int numa_id
; /* numa node id of this pmd thread. */
692 /* Queue id used by this pmd thread to send packets on all netdevs if
693 * XPS disabled for this netdev. All static_tx_qid's are unique and less
694 * than 'cmap_count(dp->poll_threads)'. */
695 uint32_t static_tx_qid
;
697 /* Number of filled output batches. */
698 int n_output_batches
;
700 struct ovs_mutex port_mutex
; /* Mutex for 'poll_list' and 'tx_ports'. */
701 /* List of rx queues to poll. */
702 struct hmap poll_list OVS_GUARDED
;
703 /* Map of 'tx_port's used for transmission. Written by the main thread,
704 * read by the pmd thread. */
705 struct hmap tx_ports OVS_GUARDED
;
707 /* These are thread-local copies of 'tx_ports'. One contains only tunnel
708 * ports (that support push_tunnel/pop_tunnel), the other contains ports
709 * with at least one txq (that support send). A port can be in both.
711 * There are two separate maps to make sure that we don't try to execute
712 * OUTPUT on a device which has 0 txqs or PUSH/POP on a non-tunnel device.
714 * The instances for cpu core NON_PMD_CORE_ID can be accessed by multiple
715 * threads, and thusly need to be protected by 'non_pmd_mutex'. Every
716 * other instance will only be accessed by its own pmd thread. */
717 struct hmap tnl_port_cache
;
718 struct hmap send_port_cache
;
720 /* Keep track of detailed PMD performance statistics. */
721 struct pmd_perf_stats perf_stats
;
723 /* Stats from previous iteration used by automatic pmd
724 * load balance logic. */
725 uint64_t prev_stats
[PMD_N_STATS
];
726 atomic_count pmd_overloaded
;
728 /* Set to true if the pmd thread needs to be reloaded. */
732 /* Interface to netdev-based datapath. */
735 struct dp_netdev
*dp
;
736 uint64_t last_port_seq
;
739 static int get_port_by_number(struct dp_netdev
*dp
, odp_port_t port_no
,
740 struct dp_netdev_port
**portp
)
741 OVS_REQUIRES(dp
->port_mutex
);
742 static int get_port_by_name(struct dp_netdev
*dp
, const char *devname
,
743 struct dp_netdev_port
**portp
)
744 OVS_REQUIRES(dp
->port_mutex
);
745 static void dp_netdev_free(struct dp_netdev
*)
746 OVS_REQUIRES(dp_netdev_mutex
);
747 static int do_add_port(struct dp_netdev
*dp
, const char *devname
,
748 const char *type
, odp_port_t port_no
)
749 OVS_REQUIRES(dp
->port_mutex
);
750 static void do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*)
751 OVS_REQUIRES(dp
->port_mutex
);
752 static int dpif_netdev_open(const struct dpif_class
*, const char *name
,
753 bool create
, struct dpif
**);
754 static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
755 struct dp_packet_batch
*,
757 const struct flow
*flow
,
758 const struct nlattr
*actions
,
760 static void dp_netdev_input(struct dp_netdev_pmd_thread
*,
761 struct dp_packet_batch
*, odp_port_t port_no
);
762 static void dp_netdev_recirculate(struct dp_netdev_pmd_thread
*,
763 struct dp_packet_batch
*);
765 static void dp_netdev_disable_upcall(struct dp_netdev
*);
766 static void dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
);
767 static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
,
768 struct dp_netdev
*dp
, unsigned core_id
,
770 static void dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
);
771 static void dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
772 OVS_REQUIRES(dp
->port_mutex
);
774 static void *pmd_thread_main(void *);
775 static struct dp_netdev_pmd_thread
*dp_netdev_get_pmd(struct dp_netdev
*dp
,
777 static struct dp_netdev_pmd_thread
*
778 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
);
779 static void dp_netdev_del_pmd(struct dp_netdev
*dp
,
780 struct dp_netdev_pmd_thread
*pmd
);
781 static void dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
, bool non_pmd
);
782 static void dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
);
783 static void dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
784 struct dp_netdev_port
*port
)
785 OVS_REQUIRES(pmd
->port_mutex
);
786 static void dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
788 OVS_REQUIRES(pmd
->port_mutex
);
789 static void dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
790 struct dp_netdev_rxq
*rxq
)
791 OVS_REQUIRES(pmd
->port_mutex
);
792 static void dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
793 struct rxq_poll
*poll
)
794 OVS_REQUIRES(pmd
->port_mutex
);
796 dp_netdev_pmd_flush_output_packets(struct dp_netdev_pmd_thread
*pmd
,
799 static void reconfigure_datapath(struct dp_netdev
*dp
)
800 OVS_REQUIRES(dp
->port_mutex
);
801 static bool dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
);
802 static void dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
);
803 static void dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
);
804 static void pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
805 OVS_REQUIRES(pmd
->port_mutex
);
807 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread
*pmd
,
808 struct polled_queue
*poll_list
, int poll_cnt
);
810 dp_netdev_rxq_set_cycles(struct dp_netdev_rxq
*rx
,
811 enum rxq_cycles_counter_type type
,
812 unsigned long long cycles
);
814 dp_netdev_rxq_get_cycles(struct dp_netdev_rxq
*rx
,
815 enum rxq_cycles_counter_type type
);
817 dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq
*rx
,
818 unsigned long long cycles
);
820 dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq
*rx
, unsigned idx
);
822 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
824 static int dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
827 static inline bool emc_entry_alive(struct emc_entry
*ce
);
828 static void emc_clear_entry(struct emc_entry
*ce
);
829 static void smc_clear_entry(struct smc_bucket
*b
, int idx
);
831 static void dp_netdev_request_reconfigure(struct dp_netdev
*dp
);
833 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread
*pmd
);
834 static void queue_netdev_flow_del(struct dp_netdev_pmd_thread
*pmd
,
835 struct dp_netdev_flow
*flow
);
838 emc_cache_init(struct emc_cache
*flow_cache
)
842 flow_cache
->sweep_idx
= 0;
843 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
844 flow_cache
->entries
[i
].flow
= NULL
;
845 flow_cache
->entries
[i
].key
.hash
= 0;
846 flow_cache
->entries
[i
].key
.len
= sizeof(struct miniflow
);
847 flowmap_init(&flow_cache
->entries
[i
].key
.mf
.map
);
852 smc_cache_init(struct smc_cache
*smc_cache
)
855 for (i
= 0; i
< SMC_BUCKET_CNT
; i
++) {
856 for (j
= 0; j
< SMC_ENTRY_PER_BUCKET
; j
++) {
857 smc_cache
->buckets
[i
].flow_idx
[j
] = UINT16_MAX
;
863 dfc_cache_init(struct dfc_cache
*flow_cache
)
865 emc_cache_init(&flow_cache
->emc_cache
);
866 smc_cache_init(&flow_cache
->smc_cache
);
870 emc_cache_uninit(struct emc_cache
*flow_cache
)
874 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
875 emc_clear_entry(&flow_cache
->entries
[i
]);
880 smc_cache_uninit(struct smc_cache
*smc
)
884 for (i
= 0; i
< SMC_BUCKET_CNT
; i
++) {
885 for (j
= 0; j
< SMC_ENTRY_PER_BUCKET
; j
++) {
886 smc_clear_entry(&(smc
->buckets
[i
]), j
);
892 dfc_cache_uninit(struct dfc_cache
*flow_cache
)
894 smc_cache_uninit(&flow_cache
->smc_cache
);
895 emc_cache_uninit(&flow_cache
->emc_cache
);
898 /* Check and clear dead flow references slowly (one entry at each
901 emc_cache_slow_sweep(struct emc_cache
*flow_cache
)
903 struct emc_entry
*entry
= &flow_cache
->entries
[flow_cache
->sweep_idx
];
905 if (!emc_entry_alive(entry
)) {
906 emc_clear_entry(entry
);
908 flow_cache
->sweep_idx
= (flow_cache
->sweep_idx
+ 1) & EM_FLOW_HASH_MASK
;
911 /* Updates the time in PMD threads context and should be called in three cases:
913 * 1. PMD structure initialization:
914 * - dp_netdev_configure_pmd()
916 * 2. Before processing of the new packet batch:
917 * - dpif_netdev_execute()
918 * - dp_netdev_process_rxq_port()
920 * 3. At least once per polling iteration in main polling threads if no
921 * packets received on current iteration:
922 * - dpif_netdev_run()
923 * - pmd_thread_main()
925 * 'pmd->ctx.now' should be used without update in all other cases if possible.
928 pmd_thread_ctx_time_update(struct dp_netdev_pmd_thread
*pmd
)
930 pmd
->ctx
.now
= time_usec();
933 /* Returns true if 'dpif' is a netdev or dummy dpif, false otherwise. */
935 dpif_is_netdev(const struct dpif
*dpif
)
937 return dpif
->dpif_class
->open
== dpif_netdev_open
;
940 static struct dpif_netdev
*
941 dpif_netdev_cast(const struct dpif
*dpif
)
943 ovs_assert(dpif_is_netdev(dpif
));
944 return CONTAINER_OF(dpif
, struct dpif_netdev
, dpif
);
947 static struct dp_netdev
*
948 get_dp_netdev(const struct dpif
*dpif
)
950 return dpif_netdev_cast(dpif
)->dp
;
954 PMD_INFO_SHOW_STATS
, /* Show how cpu cycles are spent. */
955 PMD_INFO_CLEAR_STATS
, /* Set the cycles count to 0. */
956 PMD_INFO_SHOW_RXQ
, /* Show poll lists of pmd threads. */
957 PMD_INFO_PERF_SHOW
, /* Show pmd performance details. */
961 format_pmd_thread(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
963 ds_put_cstr(reply
, (pmd
->core_id
== NON_PMD_CORE_ID
)
964 ? "main thread" : "pmd thread");
965 if (pmd
->numa_id
!= OVS_NUMA_UNSPEC
) {
966 ds_put_format(reply
, " numa_id %d", pmd
->numa_id
);
968 if (pmd
->core_id
!= OVS_CORE_UNSPEC
&& pmd
->core_id
!= NON_PMD_CORE_ID
) {
969 ds_put_format(reply
, " core_id %u", pmd
->core_id
);
971 ds_put_cstr(reply
, ":\n");
975 pmd_info_show_stats(struct ds
*reply
,
976 struct dp_netdev_pmd_thread
*pmd
)
978 uint64_t stats
[PMD_N_STATS
];
979 uint64_t total_cycles
, total_packets
;
980 double passes_per_pkt
= 0;
981 double lookups_per_hit
= 0;
982 double packets_per_batch
= 0;
984 pmd_perf_read_counters(&pmd
->perf_stats
, stats
);
985 total_cycles
= stats
[PMD_CYCLES_ITER_IDLE
]
986 + stats
[PMD_CYCLES_ITER_BUSY
];
987 total_packets
= stats
[PMD_STAT_RECV
];
989 format_pmd_thread(reply
, pmd
);
991 if (total_packets
> 0) {
992 passes_per_pkt
= (total_packets
+ stats
[PMD_STAT_RECIRC
])
993 / (double) total_packets
;
995 if (stats
[PMD_STAT_MASKED_HIT
] > 0) {
996 lookups_per_hit
= stats
[PMD_STAT_MASKED_LOOKUP
]
997 / (double) stats
[PMD_STAT_MASKED_HIT
];
999 if (stats
[PMD_STAT_SENT_BATCHES
] > 0) {
1000 packets_per_batch
= stats
[PMD_STAT_SENT_PKTS
]
1001 / (double) stats
[PMD_STAT_SENT_BATCHES
];
1004 ds_put_format(reply
,
1005 " packets received: %"PRIu64
"\n"
1006 " packet recirculations: %"PRIu64
"\n"
1007 " avg. datapath passes per packet: %.02f\n"
1008 " emc hits: %"PRIu64
"\n"
1009 " smc hits: %"PRIu64
"\n"
1010 " megaflow hits: %"PRIu64
"\n"
1011 " avg. subtable lookups per megaflow hit: %.02f\n"
1012 " miss with success upcall: %"PRIu64
"\n"
1013 " miss with failed upcall: %"PRIu64
"\n"
1014 " avg. packets per output batch: %.02f\n",
1015 total_packets
, stats
[PMD_STAT_RECIRC
],
1016 passes_per_pkt
, stats
[PMD_STAT_EXACT_HIT
],
1017 stats
[PMD_STAT_SMC_HIT
],
1018 stats
[PMD_STAT_MASKED_HIT
], lookups_per_hit
,
1019 stats
[PMD_STAT_MISS
], stats
[PMD_STAT_LOST
],
1022 if (total_cycles
== 0) {
1026 ds_put_format(reply
,
1027 " idle cycles: %"PRIu64
" (%.02f%%)\n"
1028 " processing cycles: %"PRIu64
" (%.02f%%)\n",
1029 stats
[PMD_CYCLES_ITER_IDLE
],
1030 stats
[PMD_CYCLES_ITER_IDLE
] / (double) total_cycles
* 100,
1031 stats
[PMD_CYCLES_ITER_BUSY
],
1032 stats
[PMD_CYCLES_ITER_BUSY
] / (double) total_cycles
* 100);
1034 if (total_packets
== 0) {
1038 ds_put_format(reply
,
1039 " avg cycles per packet: %.02f (%"PRIu64
"/%"PRIu64
")\n",
1040 total_cycles
/ (double) total_packets
,
1041 total_cycles
, total_packets
);
1043 ds_put_format(reply
,
1044 " avg processing cycles per packet: "
1045 "%.02f (%"PRIu64
"/%"PRIu64
")\n",
1046 stats
[PMD_CYCLES_ITER_BUSY
] / (double) total_packets
,
1047 stats
[PMD_CYCLES_ITER_BUSY
], total_packets
);
1051 pmd_info_show_perf(struct ds
*reply
,
1052 struct dp_netdev_pmd_thread
*pmd
,
1053 struct pmd_perf_params
*par
)
1055 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
1057 xastrftime_msec("%H:%M:%S.###", time_wall_msec(), true);
1058 long long now
= time_msec();
1059 double duration
= (now
- pmd
->perf_stats
.start_ms
) / 1000.0;
1061 ds_put_cstr(reply
, "\n");
1062 ds_put_format(reply
, "Time: %s\n", time_str
);
1063 ds_put_format(reply
, "Measurement duration: %.3f s\n", duration
);
1064 ds_put_cstr(reply
, "\n");
1065 format_pmd_thread(reply
, pmd
);
1066 ds_put_cstr(reply
, "\n");
1067 pmd_perf_format_overall_stats(reply
, &pmd
->perf_stats
, duration
);
1068 if (pmd_perf_metrics_enabled(pmd
)) {
1069 /* Prevent parallel clearing of perf metrics. */
1070 ovs_mutex_lock(&pmd
->perf_stats
.clear_mutex
);
1071 if (par
->histograms
) {
1072 ds_put_cstr(reply
, "\n");
1073 pmd_perf_format_histograms(reply
, &pmd
->perf_stats
);
1075 if (par
->iter_hist_len
> 0) {
1076 ds_put_cstr(reply
, "\n");
1077 pmd_perf_format_iteration_history(reply
, &pmd
->perf_stats
,
1078 par
->iter_hist_len
);
1080 if (par
->ms_hist_len
> 0) {
1081 ds_put_cstr(reply
, "\n");
1082 pmd_perf_format_ms_history(reply
, &pmd
->perf_stats
,
1085 ovs_mutex_unlock(&pmd
->perf_stats
.clear_mutex
);
1092 compare_poll_list(const void *a_
, const void *b_
)
1094 const struct rxq_poll
*a
= a_
;
1095 const struct rxq_poll
*b
= b_
;
1097 const char *namea
= netdev_rxq_get_name(a
->rxq
->rx
);
1098 const char *nameb
= netdev_rxq_get_name(b
->rxq
->rx
);
1100 int cmp
= strcmp(namea
, nameb
);
1102 return netdev_rxq_get_queue_id(a
->rxq
->rx
)
1103 - netdev_rxq_get_queue_id(b
->rxq
->rx
);
1110 sorted_poll_list(struct dp_netdev_pmd_thread
*pmd
, struct rxq_poll
**list
,
1112 OVS_REQUIRES(pmd
->port_mutex
)
1114 struct rxq_poll
*ret
, *poll
;
1117 *n
= hmap_count(&pmd
->poll_list
);
1121 ret
= xcalloc(*n
, sizeof *ret
);
1123 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
1127 ovs_assert(i
== *n
);
1128 qsort(ret
, *n
, sizeof *ret
, compare_poll_list
);
1135 pmd_info_show_rxq(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
1137 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
1138 struct rxq_poll
*list
;
1140 uint64_t total_cycles
= 0;
1142 ds_put_format(reply
,
1143 "pmd thread numa_id %d core_id %u:\n isolated : %s\n",
1144 pmd
->numa_id
, pmd
->core_id
, (pmd
->isolated
)
1145 ? "true" : "false");
1147 ovs_mutex_lock(&pmd
->port_mutex
);
1148 sorted_poll_list(pmd
, &list
, &n_rxq
);
1150 /* Get the total pmd cycles for an interval. */
1151 atomic_read_relaxed(&pmd
->intrvl_cycles
, &total_cycles
);
1152 /* Estimate the cycles to cover all intervals. */
1153 total_cycles
*= PMD_RXQ_INTERVAL_MAX
;
1155 for (int i
= 0; i
< n_rxq
; i
++) {
1156 struct dp_netdev_rxq
*rxq
= list
[i
].rxq
;
1157 const char *name
= netdev_rxq_get_name(rxq
->rx
);
1158 uint64_t proc_cycles
= 0;
1160 for (int j
= 0; j
< PMD_RXQ_INTERVAL_MAX
; j
++) {
1161 proc_cycles
+= dp_netdev_rxq_get_intrvl_cycles(rxq
, j
);
1163 ds_put_format(reply
, " port: %-16s queue-id: %2d", name
,
1164 netdev_rxq_get_queue_id(list
[i
].rxq
->rx
));
1165 ds_put_format(reply
, " %s", netdev_rxq_enabled(list
[i
].rxq
->rx
)
1166 ? "(enabled) " : "(disabled)");
1167 ds_put_format(reply
, " pmd usage: ");
1169 ds_put_format(reply
, "%2"PRIu64
"",
1170 proc_cycles
* 100 / total_cycles
);
1171 ds_put_cstr(reply
, " %");
1173 ds_put_format(reply
, "%s", "NOT AVAIL");
1175 ds_put_cstr(reply
, "\n");
1177 ovs_mutex_unlock(&pmd
->port_mutex
);
1183 compare_poll_thread_list(const void *a_
, const void *b_
)
1185 const struct dp_netdev_pmd_thread
*a
, *b
;
1187 a
= *(struct dp_netdev_pmd_thread
**)a_
;
1188 b
= *(struct dp_netdev_pmd_thread
**)b_
;
1190 if (a
->core_id
< b
->core_id
) {
1193 if (a
->core_id
> b
->core_id
) {
1199 /* Create a sorted list of pmd's from the dp->poll_threads cmap. We can use
1200 * this list, as long as we do not go to quiescent state. */
1202 sorted_poll_thread_list(struct dp_netdev
*dp
,
1203 struct dp_netdev_pmd_thread
***list
,
1206 struct dp_netdev_pmd_thread
*pmd
;
1207 struct dp_netdev_pmd_thread
**pmd_list
;
1208 size_t k
= 0, n_pmds
;
1210 n_pmds
= cmap_count(&dp
->poll_threads
);
1211 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
1213 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1217 pmd_list
[k
++] = pmd
;
1220 qsort(pmd_list
, k
, sizeof *pmd_list
, compare_poll_thread_list
);
1227 dpif_netdev_pmd_rebalance(struct unixctl_conn
*conn
, int argc
,
1228 const char *argv
[], void *aux OVS_UNUSED
)
1230 struct ds reply
= DS_EMPTY_INITIALIZER
;
1231 struct dp_netdev
*dp
= NULL
;
1233 ovs_mutex_lock(&dp_netdev_mutex
);
1236 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
1237 } else if (shash_count(&dp_netdevs
) == 1) {
1238 /* There's only one datapath */
1239 dp
= shash_first(&dp_netdevs
)->data
;
1243 ovs_mutex_unlock(&dp_netdev_mutex
);
1244 unixctl_command_reply_error(conn
,
1245 "please specify an existing datapath");
1249 dp_netdev_request_reconfigure(dp
);
1250 ovs_mutex_unlock(&dp_netdev_mutex
);
1251 ds_put_cstr(&reply
, "pmd rxq rebalance requested.\n");
1252 unixctl_command_reply(conn
, ds_cstr(&reply
));
1257 dpif_netdev_pmd_info(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
1260 struct ds reply
= DS_EMPTY_INITIALIZER
;
1261 struct dp_netdev_pmd_thread
**pmd_list
;
1262 struct dp_netdev
*dp
= NULL
;
1263 enum pmd_info_type type
= *(enum pmd_info_type
*) aux
;
1264 unsigned int core_id
;
1265 bool filter_on_pmd
= false;
1268 ovs_mutex_lock(&dp_netdev_mutex
);
1271 if (!strcmp(argv
[1], "-pmd") && argc
> 2) {
1272 if (str_to_uint(argv
[2], 10, &core_id
)) {
1273 filter_on_pmd
= true;
1278 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
1285 if (shash_count(&dp_netdevs
) == 1) {
1286 /* There's only one datapath */
1287 dp
= shash_first(&dp_netdevs
)->data
;
1289 ovs_mutex_unlock(&dp_netdev_mutex
);
1290 unixctl_command_reply_error(conn
,
1291 "please specify an existing datapath");
1296 sorted_poll_thread_list(dp
, &pmd_list
, &n
);
1297 for (size_t i
= 0; i
< n
; i
++) {
1298 struct dp_netdev_pmd_thread
*pmd
= pmd_list
[i
];
1302 if (filter_on_pmd
&& pmd
->core_id
!= core_id
) {
1305 if (type
== PMD_INFO_SHOW_RXQ
) {
1306 pmd_info_show_rxq(&reply
, pmd
);
1307 } else if (type
== PMD_INFO_CLEAR_STATS
) {
1308 pmd_perf_stats_clear(&pmd
->perf_stats
);
1309 } else if (type
== PMD_INFO_SHOW_STATS
) {
1310 pmd_info_show_stats(&reply
, pmd
);
1311 } else if (type
== PMD_INFO_PERF_SHOW
) {
1312 pmd_info_show_perf(&reply
, pmd
, (struct pmd_perf_params
*)aux
);
1317 ovs_mutex_unlock(&dp_netdev_mutex
);
1319 unixctl_command_reply(conn
, ds_cstr(&reply
));
1324 pmd_perf_show_cmd(struct unixctl_conn
*conn
, int argc
,
1326 void *aux OVS_UNUSED
)
1328 struct pmd_perf_params par
;
1329 long int it_hist
= 0, ms_hist
= 0;
1330 par
.histograms
= true;
1333 if (!strcmp(argv
[1], "-nh")) {
1334 par
.histograms
= false;
1337 } else if (!strcmp(argv
[1], "-it") && argc
> 2) {
1338 it_hist
= strtol(argv
[2], NULL
, 10);
1341 } else if (it_hist
> HISTORY_LEN
) {
1342 it_hist
= HISTORY_LEN
;
1346 } else if (!strcmp(argv
[1], "-ms") && argc
> 2) {
1347 ms_hist
= strtol(argv
[2], NULL
, 10);
1350 } else if (ms_hist
> HISTORY_LEN
) {
1351 ms_hist
= HISTORY_LEN
;
1359 par
.iter_hist_len
= it_hist
;
1360 par
.ms_hist_len
= ms_hist
;
1361 par
.command_type
= PMD_INFO_PERF_SHOW
;
1362 dpif_netdev_pmd_info(conn
, argc
, argv
, &par
);
1366 dpif_netdev_init(void)
1368 static enum pmd_info_type show_aux
= PMD_INFO_SHOW_STATS
,
1369 clear_aux
= PMD_INFO_CLEAR_STATS
,
1370 poll_aux
= PMD_INFO_SHOW_RXQ
;
1372 unixctl_command_register("dpif-netdev/pmd-stats-show", "[-pmd core] [dp]",
1373 0, 3, dpif_netdev_pmd_info
,
1375 unixctl_command_register("dpif-netdev/pmd-stats-clear", "[-pmd core] [dp]",
1376 0, 3, dpif_netdev_pmd_info
,
1377 (void *)&clear_aux
);
1378 unixctl_command_register("dpif-netdev/pmd-rxq-show", "[-pmd core] [dp]",
1379 0, 3, dpif_netdev_pmd_info
,
1381 unixctl_command_register("dpif-netdev/pmd-perf-show",
1382 "[-nh] [-it iter-history-len]"
1383 " [-ms ms-history-len]"
1384 " [-pmd core] [dp]",
1385 0, 8, pmd_perf_show_cmd
,
1387 unixctl_command_register("dpif-netdev/pmd-rxq-rebalance", "[dp]",
1388 0, 1, dpif_netdev_pmd_rebalance
,
1390 unixctl_command_register("dpif-netdev/pmd-perf-log-set",
1391 "on|off [-b before] [-a after] [-e|-ne] "
1392 "[-us usec] [-q qlen]",
1393 0, 10, pmd_perf_log_set_cmd
,
1399 dpif_netdev_enumerate(struct sset
*all_dps
,
1400 const struct dpif_class
*dpif_class
)
1402 struct shash_node
*node
;
1404 ovs_mutex_lock(&dp_netdev_mutex
);
1405 SHASH_FOR_EACH(node
, &dp_netdevs
) {
1406 struct dp_netdev
*dp
= node
->data
;
1407 if (dpif_class
!= dp
->class) {
1408 /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs.
1409 * If the class doesn't match, skip this dpif. */
1412 sset_add(all_dps
, node
->name
);
1414 ovs_mutex_unlock(&dp_netdev_mutex
);
1420 dpif_netdev_class_is_dummy(const struct dpif_class
*class)
1422 return class != &dpif_netdev_class
;
1426 dpif_netdev_port_open_type(const struct dpif_class
*class, const char *type
)
1428 return strcmp(type
, "internal") ? type
1429 : dpif_netdev_class_is_dummy(class) ? "dummy-internal"
1433 static struct dpif
*
1434 create_dpif_netdev(struct dp_netdev
*dp
)
1436 uint16_t netflow_id
= hash_string(dp
->name
, 0);
1437 struct dpif_netdev
*dpif
;
1439 ovs_refcount_ref(&dp
->ref_cnt
);
1441 dpif
= xmalloc(sizeof *dpif
);
1442 dpif_init(&dpif
->dpif
, dp
->class, dp
->name
, netflow_id
>> 8, netflow_id
);
1444 dpif
->last_port_seq
= seq_read(dp
->port_seq
);
1449 /* Choose an unused, non-zero port number and return it on success.
1450 * Return ODPP_NONE on failure. */
1452 choose_port(struct dp_netdev
*dp
, const char *name
)
1453 OVS_REQUIRES(dp
->port_mutex
)
1457 if (dp
->class != &dpif_netdev_class
) {
1461 /* If the port name begins with "br", start the number search at
1462 * 100 to make writing tests easier. */
1463 if (!strncmp(name
, "br", 2)) {
1467 /* If the port name contains a number, try to assign that port number.
1468 * This can make writing unit tests easier because port numbers are
1470 for (p
= name
; *p
!= '\0'; p
++) {
1471 if (isdigit((unsigned char) *p
)) {
1472 port_no
= start_no
+ strtol(p
, NULL
, 10);
1473 if (port_no
> 0 && port_no
!= odp_to_u32(ODPP_NONE
)
1474 && !dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
1475 return u32_to_odp(port_no
);
1482 for (port_no
= 1; port_no
<= UINT16_MAX
; port_no
++) {
1483 if (!dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
1484 return u32_to_odp(port_no
);
1492 create_dp_netdev(const char *name
, const struct dpif_class
*class,
1493 struct dp_netdev
**dpp
)
1494 OVS_REQUIRES(dp_netdev_mutex
)
1496 struct dp_netdev
*dp
;
1499 dp
= xzalloc(sizeof *dp
);
1500 shash_add(&dp_netdevs
, name
, dp
);
1502 *CONST_CAST(const struct dpif_class
**, &dp
->class) = class;
1503 *CONST_CAST(const char **, &dp
->name
) = xstrdup(name
);
1504 ovs_refcount_init(&dp
->ref_cnt
);
1505 atomic_flag_clear(&dp
->destroyed
);
1507 ovs_mutex_init(&dp
->port_mutex
);
1508 hmap_init(&dp
->ports
);
1509 dp
->port_seq
= seq_create();
1510 fat_rwlock_init(&dp
->upcall_rwlock
);
1512 dp
->reconfigure_seq
= seq_create();
1513 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
1515 for (int i
= 0; i
< N_METER_LOCKS
; ++i
) {
1516 ovs_mutex_init_adaptive(&dp
->meter_locks
[i
]);
1519 /* Disable upcalls by default. */
1520 dp_netdev_disable_upcall(dp
);
1521 dp
->upcall_aux
= NULL
;
1522 dp
->upcall_cb
= NULL
;
1524 dp
->conntrack
= conntrack_init();
1526 atomic_init(&dp
->emc_insert_min
, DEFAULT_EM_FLOW_INSERT_MIN
);
1527 atomic_init(&dp
->tx_flush_interval
, DEFAULT_TX_FLUSH_INTERVAL
);
1529 cmap_init(&dp
->poll_threads
);
1530 dp
->pmd_rxq_assign_cyc
= true;
1532 ovs_mutex_init(&dp
->tx_qid_pool_mutex
);
1533 /* We need 1 Tx queue for each possible core + 1 for non-PMD threads. */
1534 dp
->tx_qid_pool
= id_pool_create(0, ovs_numa_get_n_cores() + 1);
1536 ovs_mutex_init_recursive(&dp
->non_pmd_mutex
);
1537 ovsthread_key_create(&dp
->per_pmd_key
, NULL
);
1539 ovs_mutex_lock(&dp
->port_mutex
);
1540 /* non-PMD will be created before all other threads and will
1541 * allocate static_tx_qid = 0. */
1542 dp_netdev_set_nonpmd(dp
);
1544 error
= do_add_port(dp
, name
, dpif_netdev_port_open_type(dp
->class,
1547 ovs_mutex_unlock(&dp
->port_mutex
);
1553 dp
->last_tnl_conf_seq
= seq_read(tnl_conf_seq
);
1559 dp_netdev_request_reconfigure(struct dp_netdev
*dp
)
1561 seq_change(dp
->reconfigure_seq
);
1565 dp_netdev_is_reconf_required(struct dp_netdev
*dp
)
1567 return seq_read(dp
->reconfigure_seq
) != dp
->last_reconfigure_seq
;
1571 dpif_netdev_open(const struct dpif_class
*class, const char *name
,
1572 bool create
, struct dpif
**dpifp
)
1574 struct dp_netdev
*dp
;
1577 ovs_mutex_lock(&dp_netdev_mutex
);
1578 dp
= shash_find_data(&dp_netdevs
, name
);
1580 error
= create
? create_dp_netdev(name
, class, &dp
) : ENODEV
;
1582 error
= (dp
->class != class ? EINVAL
1587 *dpifp
= create_dpif_netdev(dp
);
1590 ovs_mutex_unlock(&dp_netdev_mutex
);
1596 dp_netdev_destroy_upcall_lock(struct dp_netdev
*dp
)
1597 OVS_NO_THREAD_SAFETY_ANALYSIS
1599 /* Check that upcalls are disabled, i.e. that the rwlock is taken */
1600 ovs_assert(fat_rwlock_tryrdlock(&dp
->upcall_rwlock
));
1602 /* Before freeing a lock we should release it */
1603 fat_rwlock_unlock(&dp
->upcall_rwlock
);
1604 fat_rwlock_destroy(&dp
->upcall_rwlock
);
1608 dp_delete_meter(struct dp_netdev
*dp
, uint32_t meter_id
)
1609 OVS_REQUIRES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
1611 if (dp
->meters
[meter_id
]) {
1612 free(dp
->meters
[meter_id
]);
1613 dp
->meters
[meter_id
] = NULL
;
1617 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
1618 * through the 'dp_netdevs' shash while freeing 'dp'. */
1620 dp_netdev_free(struct dp_netdev
*dp
)
1621 OVS_REQUIRES(dp_netdev_mutex
)
1623 struct dp_netdev_port
*port
, *next
;
1625 shash_find_and_delete(&dp_netdevs
, dp
->name
);
1627 ovs_mutex_lock(&dp
->port_mutex
);
1628 HMAP_FOR_EACH_SAFE (port
, next
, node
, &dp
->ports
) {
1629 do_del_port(dp
, port
);
1631 ovs_mutex_unlock(&dp
->port_mutex
);
1633 dp_netdev_destroy_all_pmds(dp
, true);
1634 cmap_destroy(&dp
->poll_threads
);
1636 ovs_mutex_destroy(&dp
->tx_qid_pool_mutex
);
1637 id_pool_destroy(dp
->tx_qid_pool
);
1639 ovs_mutex_destroy(&dp
->non_pmd_mutex
);
1640 ovsthread_key_delete(dp
->per_pmd_key
);
1642 conntrack_destroy(dp
->conntrack
);
1645 seq_destroy(dp
->reconfigure_seq
);
1647 seq_destroy(dp
->port_seq
);
1648 hmap_destroy(&dp
->ports
);
1649 ovs_mutex_destroy(&dp
->port_mutex
);
1651 /* Upcalls must be disabled at this point */
1652 dp_netdev_destroy_upcall_lock(dp
);
1656 for (i
= 0; i
< MAX_METERS
; ++i
) {
1658 dp_delete_meter(dp
, i
);
1659 meter_unlock(dp
, i
);
1661 for (i
= 0; i
< N_METER_LOCKS
; ++i
) {
1662 ovs_mutex_destroy(&dp
->meter_locks
[i
]);
1665 free(dp
->pmd_cmask
);
1666 free(CONST_CAST(char *, dp
->name
));
1671 dp_netdev_unref(struct dp_netdev
*dp
)
1674 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
1675 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
1676 ovs_mutex_lock(&dp_netdev_mutex
);
1677 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1680 ovs_mutex_unlock(&dp_netdev_mutex
);
1685 dpif_netdev_close(struct dpif
*dpif
)
1687 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1689 dp_netdev_unref(dp
);
1694 dpif_netdev_destroy(struct dpif
*dpif
)
1696 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1698 if (!atomic_flag_test_and_set(&dp
->destroyed
)) {
1699 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1700 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
1708 /* Add 'n' to the atomic variable 'var' non-atomically and using relaxed
1709 * load/store semantics. While the increment is not atomic, the load and
1710 * store operations are, making it impossible to read inconsistent values.
1712 * This is used to update thread local stats counters. */
1714 non_atomic_ullong_add(atomic_ullong
*var
, unsigned long long n
)
1716 unsigned long long tmp
;
1718 atomic_read_relaxed(var
, &tmp
);
1720 atomic_store_relaxed(var
, tmp
);
1724 dpif_netdev_get_stats(const struct dpif
*dpif
, struct dpif_dp_stats
*stats
)
1726 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1727 struct dp_netdev_pmd_thread
*pmd
;
1728 uint64_t pmd_stats
[PMD_N_STATS
];
1730 stats
->n_flows
= stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
1731 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1732 stats
->n_flows
+= cmap_count(&pmd
->flow_table
);
1733 pmd_perf_read_counters(&pmd
->perf_stats
, pmd_stats
);
1734 stats
->n_hit
+= pmd_stats
[PMD_STAT_EXACT_HIT
];
1735 stats
->n_hit
+= pmd_stats
[PMD_STAT_SMC_HIT
];
1736 stats
->n_hit
+= pmd_stats
[PMD_STAT_MASKED_HIT
];
1737 stats
->n_missed
+= pmd_stats
[PMD_STAT_MISS
];
1738 stats
->n_lost
+= pmd_stats
[PMD_STAT_LOST
];
1740 stats
->n_masks
= UINT32_MAX
;
1741 stats
->n_mask_hit
= UINT64_MAX
;
1747 dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread
*pmd
)
1749 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
1750 ovs_mutex_lock(&pmd
->dp
->non_pmd_mutex
);
1751 ovs_mutex_lock(&pmd
->port_mutex
);
1752 pmd_load_cached_ports(pmd
);
1753 ovs_mutex_unlock(&pmd
->port_mutex
);
1754 ovs_mutex_unlock(&pmd
->dp
->non_pmd_mutex
);
1758 seq_change(pmd
->reload_seq
);
1759 atomic_store_explicit(&pmd
->reload
, true, memory_order_release
);
1763 hash_port_no(odp_port_t port_no
)
1765 return hash_int(odp_to_u32(port_no
), 0);
1769 port_create(const char *devname
, const char *type
,
1770 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1772 struct netdev_saved_flags
*sf
;
1773 struct dp_netdev_port
*port
;
1774 enum netdev_flags flags
;
1775 struct netdev
*netdev
;
1780 /* Open and validate network device. */
1781 error
= netdev_open(devname
, type
, &netdev
);
1785 /* XXX reject non-Ethernet devices */
1787 netdev_get_flags(netdev
, &flags
);
1788 if (flags
& NETDEV_LOOPBACK
) {
1789 VLOG_ERR("%s: cannot add a loopback device", devname
);
1794 error
= netdev_turn_flags_on(netdev
, NETDEV_PROMISC
, &sf
);
1796 VLOG_ERR("%s: cannot set promisc flag", devname
);
1800 port
= xzalloc(sizeof *port
);
1801 port
->port_no
= port_no
;
1802 port
->netdev
= netdev
;
1803 port
->type
= xstrdup(type
);
1805 port
->emc_enabled
= true;
1806 port
->need_reconfigure
= true;
1807 ovs_mutex_init(&port
->txq_used_mutex
);
1814 netdev_close(netdev
);
1819 do_add_port(struct dp_netdev
*dp
, const char *devname
, const char *type
,
1821 OVS_REQUIRES(dp
->port_mutex
)
1823 struct dp_netdev_port
*port
;
1826 /* Reject devices already in 'dp'. */
1827 if (!get_port_by_name(dp
, devname
, &port
)) {
1831 error
= port_create(devname
, type
, port_no
, &port
);
1836 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
1837 seq_change(dp
->port_seq
);
1839 reconfigure_datapath(dp
);
1845 dpif_netdev_port_add(struct dpif
*dpif
, struct netdev
*netdev
,
1846 odp_port_t
*port_nop
)
1848 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1849 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1850 const char *dpif_port
;
1854 ovs_mutex_lock(&dp
->port_mutex
);
1855 dpif_port
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1856 if (*port_nop
!= ODPP_NONE
) {
1857 port_no
= *port_nop
;
1858 error
= dp_netdev_lookup_port(dp
, *port_nop
) ? EBUSY
: 0;
1860 port_no
= choose_port(dp
, dpif_port
);
1861 error
= port_no
== ODPP_NONE
? EFBIG
: 0;
1864 *port_nop
= port_no
;
1865 error
= do_add_port(dp
, dpif_port
, netdev_get_type(netdev
), port_no
);
1867 ovs_mutex_unlock(&dp
->port_mutex
);
1873 dpif_netdev_port_del(struct dpif
*dpif
, odp_port_t port_no
)
1875 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1878 ovs_mutex_lock(&dp
->port_mutex
);
1879 if (port_no
== ODPP_LOCAL
) {
1882 struct dp_netdev_port
*port
;
1884 error
= get_port_by_number(dp
, port_no
, &port
);
1886 do_del_port(dp
, port
);
1889 ovs_mutex_unlock(&dp
->port_mutex
);
1895 is_valid_port_number(odp_port_t port_no
)
1897 return port_no
!= ODPP_NONE
;
1900 static struct dp_netdev_port
*
1901 dp_netdev_lookup_port(const struct dp_netdev
*dp
, odp_port_t port_no
)
1902 OVS_REQUIRES(dp
->port_mutex
)
1904 struct dp_netdev_port
*port
;
1906 HMAP_FOR_EACH_WITH_HASH (port
, node
, hash_port_no(port_no
), &dp
->ports
) {
1907 if (port
->port_no
== port_no
) {
1915 get_port_by_number(struct dp_netdev
*dp
,
1916 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1917 OVS_REQUIRES(dp
->port_mutex
)
1919 if (!is_valid_port_number(port_no
)) {
1923 *portp
= dp_netdev_lookup_port(dp
, port_no
);
1924 return *portp
? 0 : ENODEV
;
1929 port_destroy(struct dp_netdev_port
*port
)
1935 netdev_close(port
->netdev
);
1936 netdev_restore_flags(port
->sf
);
1938 for (unsigned i
= 0; i
< port
->n_rxq
; i
++) {
1939 netdev_rxq_close(port
->rxqs
[i
].rx
);
1941 ovs_mutex_destroy(&port
->txq_used_mutex
);
1942 free(port
->rxq_affinity_list
);
1943 free(port
->txq_used
);
1950 get_port_by_name(struct dp_netdev
*dp
,
1951 const char *devname
, struct dp_netdev_port
**portp
)
1952 OVS_REQUIRES(dp
->port_mutex
)
1954 struct dp_netdev_port
*port
;
1956 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1957 if (!strcmp(netdev_get_name(port
->netdev
), devname
)) {
1963 /* Callers of dpif_netdev_port_query_by_name() expect ENODEV for a non
1968 /* Returns 'true' if there is a port with pmd netdev. */
1970 has_pmd_port(struct dp_netdev
*dp
)
1971 OVS_REQUIRES(dp
->port_mutex
)
1973 struct dp_netdev_port
*port
;
1975 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1976 if (netdev_is_pmd(port
->netdev
)) {
1985 do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*port
)
1986 OVS_REQUIRES(dp
->port_mutex
)
1988 hmap_remove(&dp
->ports
, &port
->node
);
1989 seq_change(dp
->port_seq
);
1991 reconfigure_datapath(dp
);
1997 answer_port_query(const struct dp_netdev_port
*port
,
1998 struct dpif_port
*dpif_port
)
2000 dpif_port
->name
= xstrdup(netdev_get_name(port
->netdev
));
2001 dpif_port
->type
= xstrdup(port
->type
);
2002 dpif_port
->port_no
= port
->port_no
;
2006 dpif_netdev_port_query_by_number(const struct dpif
*dpif
, odp_port_t port_no
,
2007 struct dpif_port
*dpif_port
)
2009 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2010 struct dp_netdev_port
*port
;
2013 ovs_mutex_lock(&dp
->port_mutex
);
2014 error
= get_port_by_number(dp
, port_no
, &port
);
2015 if (!error
&& dpif_port
) {
2016 answer_port_query(port
, dpif_port
);
2018 ovs_mutex_unlock(&dp
->port_mutex
);
2024 dpif_netdev_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
2025 struct dpif_port
*dpif_port
)
2027 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2028 struct dp_netdev_port
*port
;
2031 ovs_mutex_lock(&dp
->port_mutex
);
2032 error
= get_port_by_name(dp
, devname
, &port
);
2033 if (!error
&& dpif_port
) {
2034 answer_port_query(port
, dpif_port
);
2036 ovs_mutex_unlock(&dp
->port_mutex
);
2042 dp_netdev_flow_free(struct dp_netdev_flow
*flow
)
2044 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow
));
2048 static void dp_netdev_flow_unref(struct dp_netdev_flow
*flow
)
2050 if (ovs_refcount_unref_relaxed(&flow
->ref_cnt
) == 1) {
2051 ovsrcu_postpone(dp_netdev_flow_free
, flow
);
2056 dp_netdev_flow_hash(const ovs_u128
*ufid
)
2058 return ufid
->u32
[0];
2061 static inline struct dpcls
*
2062 dp_netdev_pmd_lookup_dpcls(struct dp_netdev_pmd_thread
*pmd
,
2066 uint32_t hash
= hash_port_no(in_port
);
2067 CMAP_FOR_EACH_WITH_HASH (cls
, node
, hash
, &pmd
->classifiers
) {
2068 if (cls
->in_port
== in_port
) {
2069 /* Port classifier exists already */
2076 static inline struct dpcls
*
2077 dp_netdev_pmd_find_dpcls(struct dp_netdev_pmd_thread
*pmd
,
2079 OVS_REQUIRES(pmd
->flow_mutex
)
2081 struct dpcls
*cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
2082 uint32_t hash
= hash_port_no(in_port
);
2085 /* Create new classifier for in_port */
2086 cls
= xmalloc(sizeof(*cls
));
2088 cls
->in_port
= in_port
;
2089 cmap_insert(&pmd
->classifiers
, &cls
->node
, hash
);
2090 VLOG_DBG("Creating dpcls %p for in_port %d", cls
, in_port
);
2095 #define MAX_FLOW_MARK (UINT32_MAX - 1)
2096 #define INVALID_FLOW_MARK (UINT32_MAX)
2098 struct megaflow_to_mark_data
{
2099 const struct cmap_node node
;
2105 struct cmap megaflow_to_mark
;
2106 struct cmap mark_to_flow
;
2107 struct id_pool
*pool
;
2110 static struct flow_mark flow_mark
= {
2111 .megaflow_to_mark
= CMAP_INITIALIZER
,
2112 .mark_to_flow
= CMAP_INITIALIZER
,
2116 flow_mark_alloc(void)
2120 if (!flow_mark
.pool
) {
2121 /* Haven't initiated yet, do it here */
2122 flow_mark
.pool
= id_pool_create(0, MAX_FLOW_MARK
);
2125 if (id_pool_alloc_id(flow_mark
.pool
, &mark
)) {
2129 return INVALID_FLOW_MARK
;
2133 flow_mark_free(uint32_t mark
)
2135 id_pool_free_id(flow_mark
.pool
, mark
);
2138 /* associate megaflow with a mark, which is a 1:1 mapping */
2140 megaflow_to_mark_associate(const ovs_u128
*mega_ufid
, uint32_t mark
)
2142 size_t hash
= dp_netdev_flow_hash(mega_ufid
);
2143 struct megaflow_to_mark_data
*data
= xzalloc(sizeof(*data
));
2145 data
->mega_ufid
= *mega_ufid
;
2148 cmap_insert(&flow_mark
.megaflow_to_mark
,
2149 CONST_CAST(struct cmap_node
*, &data
->node
), hash
);
2152 /* disassociate meagaflow with a mark */
2154 megaflow_to_mark_disassociate(const ovs_u128
*mega_ufid
)
2156 size_t hash
= dp_netdev_flow_hash(mega_ufid
);
2157 struct megaflow_to_mark_data
*data
;
2159 CMAP_FOR_EACH_WITH_HASH (data
, node
, hash
, &flow_mark
.megaflow_to_mark
) {
2160 if (ovs_u128_equals(*mega_ufid
, data
->mega_ufid
)) {
2161 cmap_remove(&flow_mark
.megaflow_to_mark
,
2162 CONST_CAST(struct cmap_node
*, &data
->node
), hash
);
2163 ovsrcu_postpone(free
, data
);
2168 VLOG_WARN("Masked ufid "UUID_FMT
" is not associated with a mark?\n",
2169 UUID_ARGS((struct uuid
*)mega_ufid
));
2172 static inline uint32_t
2173 megaflow_to_mark_find(const ovs_u128
*mega_ufid
)
2175 size_t hash
= dp_netdev_flow_hash(mega_ufid
);
2176 struct megaflow_to_mark_data
*data
;
2178 CMAP_FOR_EACH_WITH_HASH (data
, node
, hash
, &flow_mark
.megaflow_to_mark
) {
2179 if (ovs_u128_equals(*mega_ufid
, data
->mega_ufid
)) {
2184 VLOG_DBG("Mark id for ufid "UUID_FMT
" was not found\n",
2185 UUID_ARGS((struct uuid
*)mega_ufid
));
2186 return INVALID_FLOW_MARK
;
2189 /* associate mark with a flow, which is 1:N mapping */
2191 mark_to_flow_associate(const uint32_t mark
, struct dp_netdev_flow
*flow
)
2193 dp_netdev_flow_ref(flow
);
2195 cmap_insert(&flow_mark
.mark_to_flow
,
2196 CONST_CAST(struct cmap_node
*, &flow
->mark_node
),
2200 VLOG_DBG("Associated dp_netdev flow %p with mark %u\n", flow
, mark
);
2204 flow_mark_has_no_ref(uint32_t mark
)
2206 struct dp_netdev_flow
*flow
;
2208 CMAP_FOR_EACH_WITH_HASH (flow
, mark_node
, hash_int(mark
, 0),
2209 &flow_mark
.mark_to_flow
) {
2210 if (flow
->mark
== mark
) {
2219 mark_to_flow_disassociate(struct dp_netdev_pmd_thread
*pmd
,
2220 struct dp_netdev_flow
*flow
)
2223 uint32_t mark
= flow
->mark
;
2224 struct cmap_node
*mark_node
= CONST_CAST(struct cmap_node
*,
2227 cmap_remove(&flow_mark
.mark_to_flow
, mark_node
, hash_int(mark
, 0));
2228 flow
->mark
= INVALID_FLOW_MARK
;
2231 * no flow is referencing the mark any more? If so, let's
2232 * remove the flow from hardware and free the mark.
2234 if (flow_mark_has_no_ref(mark
)) {
2235 struct dp_netdev_port
*port
;
2236 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
2238 ovs_mutex_lock(&pmd
->dp
->port_mutex
);
2239 port
= dp_netdev_lookup_port(pmd
->dp
, in_port
);
2241 ret
= netdev_flow_del(port
->netdev
, &flow
->mega_ufid
, NULL
);
2243 ovs_mutex_unlock(&pmd
->dp
->port_mutex
);
2245 flow_mark_free(mark
);
2246 VLOG_DBG("Freed flow mark %u\n", mark
);
2248 megaflow_to_mark_disassociate(&flow
->mega_ufid
);
2250 dp_netdev_flow_unref(flow
);
2256 flow_mark_flush(struct dp_netdev_pmd_thread
*pmd
)
2258 struct dp_netdev_flow
*flow
;
2260 CMAP_FOR_EACH (flow
, mark_node
, &flow_mark
.mark_to_flow
) {
2261 if (flow
->pmd_id
== pmd
->core_id
) {
2262 queue_netdev_flow_del(pmd
, flow
);
2267 static struct dp_netdev_flow
*
2268 mark_to_flow_find(const struct dp_netdev_pmd_thread
*pmd
,
2269 const uint32_t mark
)
2271 struct dp_netdev_flow
*flow
;
2273 CMAP_FOR_EACH_WITH_HASH (flow
, mark_node
, hash_int(mark
, 0),
2274 &flow_mark
.mark_to_flow
) {
2275 if (flow
->mark
== mark
&& flow
->pmd_id
== pmd
->core_id
&&
2276 flow
->dead
== false) {
2284 static struct dp_flow_offload_item
*
2285 dp_netdev_alloc_flow_offload(struct dp_netdev_pmd_thread
*pmd
,
2286 struct dp_netdev_flow
*flow
,
2289 struct dp_flow_offload_item
*offload
;
2291 offload
= xzalloc(sizeof(*offload
));
2293 offload
->flow
= flow
;
2296 dp_netdev_flow_ref(flow
);
2297 dp_netdev_pmd_try_ref(pmd
);
2303 dp_netdev_free_flow_offload(struct dp_flow_offload_item
*offload
)
2305 dp_netdev_pmd_unref(offload
->pmd
);
2306 dp_netdev_flow_unref(offload
->flow
);
2308 free(offload
->actions
);
2313 dp_netdev_append_flow_offload(struct dp_flow_offload_item
*offload
)
2315 ovs_mutex_lock(&dp_flow_offload
.mutex
);
2316 ovs_list_push_back(&dp_flow_offload
.list
, &offload
->node
);
2317 xpthread_cond_signal(&dp_flow_offload
.cond
);
2318 ovs_mutex_unlock(&dp_flow_offload
.mutex
);
2322 dp_netdev_flow_offload_del(struct dp_flow_offload_item
*offload
)
2324 return mark_to_flow_disassociate(offload
->pmd
, offload
->flow
);
2328 * There are two flow offload operations here: addition and modification.
2330 * For flow addition, this function does:
2331 * - allocate a new flow mark id
2332 * - perform hardware flow offload
2333 * - associate the flow mark with flow and mega flow
2335 * For flow modification, both flow mark and the associations are still
2336 * valid, thus only item 2 needed.
2339 dp_netdev_flow_offload_put(struct dp_flow_offload_item
*offload
)
2341 struct dp_netdev_port
*port
;
2342 struct dp_netdev_pmd_thread
*pmd
= offload
->pmd
;
2343 struct dp_netdev_flow
*flow
= offload
->flow
;
2344 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
2345 bool modification
= offload
->op
== DP_NETDEV_FLOW_OFFLOAD_OP_MOD
;
2346 struct offload_info info
;
2356 ovs_assert(mark
!= INVALID_FLOW_MARK
);
2359 * If a mega flow has already been offloaded (from other PMD
2360 * instances), do not offload it again.
2362 mark
= megaflow_to_mark_find(&flow
->mega_ufid
);
2363 if (mark
!= INVALID_FLOW_MARK
) {
2364 VLOG_DBG("Flow has already been offloaded with mark %u\n", mark
);
2365 if (flow
->mark
!= INVALID_FLOW_MARK
) {
2366 ovs_assert(flow
->mark
== mark
);
2368 mark_to_flow_associate(mark
, flow
);
2373 mark
= flow_mark_alloc();
2374 if (mark
== INVALID_FLOW_MARK
) {
2375 VLOG_ERR("Failed to allocate flow mark!\n");
2378 info
.flow_mark
= mark
;
2380 ovs_mutex_lock(&pmd
->dp
->port_mutex
);
2381 port
= dp_netdev_lookup_port(pmd
->dp
, in_port
);
2382 if (!port
|| netdev_vport_is_vport_class(port
->netdev
->netdev_class
)) {
2383 ovs_mutex_unlock(&pmd
->dp
->port_mutex
);
2386 ret
= netdev_flow_put(port
->netdev
, &offload
->match
,
2387 CONST_CAST(struct nlattr
*, offload
->actions
),
2388 offload
->actions_len
, &flow
->mega_ufid
, &info
,
2390 ovs_mutex_unlock(&pmd
->dp
->port_mutex
);
2396 if (!modification
) {
2397 megaflow_to_mark_associate(&flow
->mega_ufid
, mark
);
2398 mark_to_flow_associate(mark
, flow
);
2403 if (!modification
) {
2404 flow_mark_free(mark
);
2406 mark_to_flow_disassociate(pmd
, flow
);
2412 dp_netdev_flow_offload_main(void *data OVS_UNUSED
)
2414 struct dp_flow_offload_item
*offload
;
2415 struct ovs_list
*list
;
2420 ovs_mutex_lock(&dp_flow_offload
.mutex
);
2421 if (ovs_list_is_empty(&dp_flow_offload
.list
)) {
2422 ovsrcu_quiesce_start();
2423 ovs_mutex_cond_wait(&dp_flow_offload
.cond
,
2424 &dp_flow_offload
.mutex
);
2425 ovsrcu_quiesce_end();
2427 list
= ovs_list_pop_front(&dp_flow_offload
.list
);
2428 offload
= CONTAINER_OF(list
, struct dp_flow_offload_item
, node
);
2429 ovs_mutex_unlock(&dp_flow_offload
.mutex
);
2431 switch (offload
->op
) {
2432 case DP_NETDEV_FLOW_OFFLOAD_OP_ADD
:
2434 ret
= dp_netdev_flow_offload_put(offload
);
2436 case DP_NETDEV_FLOW_OFFLOAD_OP_MOD
:
2438 ret
= dp_netdev_flow_offload_put(offload
);
2440 case DP_NETDEV_FLOW_OFFLOAD_OP_DEL
:
2442 ret
= dp_netdev_flow_offload_del(offload
);
2448 VLOG_DBG("%s to %s netdev flow\n",
2449 ret
== 0 ? "succeed" : "failed", op
);
2450 dp_netdev_free_flow_offload(offload
);
2457 queue_netdev_flow_del(struct dp_netdev_pmd_thread
*pmd
,
2458 struct dp_netdev_flow
*flow
)
2460 struct dp_flow_offload_item
*offload
;
2462 if (ovsthread_once_start(&offload_thread_once
)) {
2463 xpthread_cond_init(&dp_flow_offload
.cond
, NULL
);
2464 ovs_thread_create("dp_netdev_flow_offload",
2465 dp_netdev_flow_offload_main
, NULL
);
2466 ovsthread_once_done(&offload_thread_once
);
2469 offload
= dp_netdev_alloc_flow_offload(pmd
, flow
,
2470 DP_NETDEV_FLOW_OFFLOAD_OP_DEL
);
2471 dp_netdev_append_flow_offload(offload
);
2475 queue_netdev_flow_put(struct dp_netdev_pmd_thread
*pmd
,
2476 struct dp_netdev_flow
*flow
, struct match
*match
,
2477 const struct nlattr
*actions
, size_t actions_len
)
2479 struct dp_flow_offload_item
*offload
;
2482 if (!netdev_is_flow_api_enabled()) {
2486 if (ovsthread_once_start(&offload_thread_once
)) {
2487 xpthread_cond_init(&dp_flow_offload
.cond
, NULL
);
2488 ovs_thread_create("dp_netdev_flow_offload",
2489 dp_netdev_flow_offload_main
, NULL
);
2490 ovsthread_once_done(&offload_thread_once
);
2493 if (flow
->mark
!= INVALID_FLOW_MARK
) {
2494 op
= DP_NETDEV_FLOW_OFFLOAD_OP_MOD
;
2496 op
= DP_NETDEV_FLOW_OFFLOAD_OP_ADD
;
2498 offload
= dp_netdev_alloc_flow_offload(pmd
, flow
, op
);
2499 offload
->match
= *match
;
2500 offload
->actions
= xmalloc(actions_len
);
2501 memcpy(offload
->actions
, actions
, actions_len
);
2502 offload
->actions_len
= actions_len
;
2504 dp_netdev_append_flow_offload(offload
);
2508 dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread
*pmd
,
2509 struct dp_netdev_flow
*flow
)
2510 OVS_REQUIRES(pmd
->flow_mutex
)
2512 struct cmap_node
*node
= CONST_CAST(struct cmap_node
*, &flow
->node
);
2514 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
2516 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
2517 ovs_assert(cls
!= NULL
);
2518 dpcls_remove(cls
, &flow
->cr
);
2519 cmap_remove(&pmd
->flow_table
, node
, dp_netdev_flow_hash(&flow
->ufid
));
2520 if (flow
->mark
!= INVALID_FLOW_MARK
) {
2521 queue_netdev_flow_del(pmd
, flow
);
2525 dp_netdev_flow_unref(flow
);
2529 dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
)
2531 struct dp_netdev_flow
*netdev_flow
;
2533 ovs_mutex_lock(&pmd
->flow_mutex
);
2534 CMAP_FOR_EACH (netdev_flow
, node
, &pmd
->flow_table
) {
2535 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
2537 ovs_mutex_unlock(&pmd
->flow_mutex
);
2541 dpif_netdev_flow_flush(struct dpif
*dpif
)
2543 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2544 struct dp_netdev_pmd_thread
*pmd
;
2546 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2547 dp_netdev_pmd_flow_flush(pmd
);
2553 struct dp_netdev_port_state
{
2554 struct hmap_position position
;
2559 dpif_netdev_port_dump_start(const struct dpif
*dpif OVS_UNUSED
, void **statep
)
2561 *statep
= xzalloc(sizeof(struct dp_netdev_port_state
));
2566 dpif_netdev_port_dump_next(const struct dpif
*dpif
, void *state_
,
2567 struct dpif_port
*dpif_port
)
2569 struct dp_netdev_port_state
*state
= state_
;
2570 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2571 struct hmap_node
*node
;
2574 ovs_mutex_lock(&dp
->port_mutex
);
2575 node
= hmap_at_position(&dp
->ports
, &state
->position
);
2577 struct dp_netdev_port
*port
;
2579 port
= CONTAINER_OF(node
, struct dp_netdev_port
, node
);
2582 state
->name
= xstrdup(netdev_get_name(port
->netdev
));
2583 dpif_port
->name
= state
->name
;
2584 dpif_port
->type
= port
->type
;
2585 dpif_port
->port_no
= port
->port_no
;
2591 ovs_mutex_unlock(&dp
->port_mutex
);
2597 dpif_netdev_port_dump_done(const struct dpif
*dpif OVS_UNUSED
, void *state_
)
2599 struct dp_netdev_port_state
*state
= state_
;
2606 dpif_netdev_port_poll(const struct dpif
*dpif_
, char **devnamep OVS_UNUSED
)
2608 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
2609 uint64_t new_port_seq
;
2612 new_port_seq
= seq_read(dpif
->dp
->port_seq
);
2613 if (dpif
->last_port_seq
!= new_port_seq
) {
2614 dpif
->last_port_seq
= new_port_seq
;
2624 dpif_netdev_port_poll_wait(const struct dpif
*dpif_
)
2626 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
2628 seq_wait(dpif
->dp
->port_seq
, dpif
->last_port_seq
);
2631 static struct dp_netdev_flow
*
2632 dp_netdev_flow_cast(const struct dpcls_rule
*cr
)
2634 return cr
? CONTAINER_OF(cr
, struct dp_netdev_flow
, cr
) : NULL
;
2637 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*flow
)
2639 return ovs_refcount_try_ref_rcu(&flow
->ref_cnt
);
2642 /* netdev_flow_key utilities.
2644 * netdev_flow_key is basically a miniflow. We use these functions
2645 * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow
2646 * functions (miniflow_clone_inline, miniflow_equal, ...), because:
2648 * - Since we are dealing exclusively with miniflows created by
2649 * miniflow_extract(), if the map is different the miniflow is different.
2650 * Therefore we can be faster by comparing the map and the miniflow in a
2652 * - These functions can be inlined by the compiler. */
2654 /* Given the number of bits set in miniflow's maps, returns the size of the
2655 * 'netdev_flow_key.mf' */
2656 static inline size_t
2657 netdev_flow_key_size(size_t flow_u64s
)
2659 return sizeof(struct miniflow
) + MINIFLOW_VALUES_SIZE(flow_u64s
);
2663 netdev_flow_key_equal(const struct netdev_flow_key
*a
,
2664 const struct netdev_flow_key
*b
)
2666 /* 'b->len' may be not set yet. */
2667 return a
->hash
== b
->hash
&& !memcmp(&a
->mf
, &b
->mf
, a
->len
);
2670 /* Used to compare 'netdev_flow_key' in the exact match cache to a miniflow.
2671 * The maps are compared bitwise, so both 'key->mf' and 'mf' must have been
2672 * generated by miniflow_extract. */
2674 netdev_flow_key_equal_mf(const struct netdev_flow_key
*key
,
2675 const struct miniflow
*mf
)
2677 return !memcmp(&key
->mf
, mf
, key
->len
);
2681 netdev_flow_key_clone(struct netdev_flow_key
*dst
,
2682 const struct netdev_flow_key
*src
)
2685 offsetof(struct netdev_flow_key
, mf
) + src
->len
);
2688 /* Initialize a netdev_flow_key 'mask' from 'match'. */
2690 netdev_flow_mask_init(struct netdev_flow_key
*mask
,
2691 const struct match
*match
)
2693 uint64_t *dst
= miniflow_values(&mask
->mf
);
2694 struct flowmap fmap
;
2698 /* Only check masks that make sense for the flow. */
2699 flow_wc_map(&match
->flow
, &fmap
);
2700 flowmap_init(&mask
->mf
.map
);
2702 FLOWMAP_FOR_EACH_INDEX(idx
, fmap
) {
2703 uint64_t mask_u64
= flow_u64_value(&match
->wc
.masks
, idx
);
2706 flowmap_set(&mask
->mf
.map
, idx
, 1);
2708 hash
= hash_add64(hash
, mask_u64
);
2714 FLOWMAP_FOR_EACH_MAP (map
, mask
->mf
.map
) {
2715 hash
= hash_add64(hash
, map
);
2718 size_t n
= dst
- miniflow_get_values(&mask
->mf
);
2720 mask
->hash
= hash_finish(hash
, n
* 8);
2721 mask
->len
= netdev_flow_key_size(n
);
2724 /* Initializes 'dst' as a copy of 'flow' masked with 'mask'. */
2726 netdev_flow_key_init_masked(struct netdev_flow_key
*dst
,
2727 const struct flow
*flow
,
2728 const struct netdev_flow_key
*mask
)
2730 uint64_t *dst_u64
= miniflow_values(&dst
->mf
);
2731 const uint64_t *mask_u64
= miniflow_get_values(&mask
->mf
);
2735 dst
->len
= mask
->len
;
2736 dst
->mf
= mask
->mf
; /* Copy maps. */
2738 FLOW_FOR_EACH_IN_MAPS(value
, flow
, mask
->mf
.map
) {
2739 *dst_u64
= value
& *mask_u64
++;
2740 hash
= hash_add64(hash
, *dst_u64
++);
2742 dst
->hash
= hash_finish(hash
,
2743 (dst_u64
- miniflow_get_values(&dst
->mf
)) * 8);
2746 /* Iterate through netdev_flow_key TNL u64 values specified by 'FLOWMAP'. */
2747 #define NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(VALUE, KEY, FLOWMAP) \
2748 MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, &(KEY)->mf, FLOWMAP)
2750 /* Returns a hash value for the bits of 'key' where there are 1-bits in
2752 static inline uint32_t
2753 netdev_flow_key_hash_in_mask(const struct netdev_flow_key
*key
,
2754 const struct netdev_flow_key
*mask
)
2756 const uint64_t *p
= miniflow_get_values(&mask
->mf
);
2760 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, key
, mask
->mf
.map
) {
2761 hash
= hash_add64(hash
, value
& *p
++);
2764 return hash_finish(hash
, (p
- miniflow_get_values(&mask
->mf
)) * 8);
2768 emc_entry_alive(struct emc_entry
*ce
)
2770 return ce
->flow
&& !ce
->flow
->dead
;
2774 emc_clear_entry(struct emc_entry
*ce
)
2777 dp_netdev_flow_unref(ce
->flow
);
2783 emc_change_entry(struct emc_entry
*ce
, struct dp_netdev_flow
*flow
,
2784 const struct netdev_flow_key
*key
)
2786 if (ce
->flow
!= flow
) {
2788 dp_netdev_flow_unref(ce
->flow
);
2791 if (dp_netdev_flow_ref(flow
)) {
2798 netdev_flow_key_clone(&ce
->key
, key
);
2803 emc_insert(struct emc_cache
*cache
, const struct netdev_flow_key
*key
,
2804 struct dp_netdev_flow
*flow
)
2806 struct emc_entry
*to_be_replaced
= NULL
;
2807 struct emc_entry
*current_entry
;
2809 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
2810 if (netdev_flow_key_equal(¤t_entry
->key
, key
)) {
2811 /* We found the entry with the 'mf' miniflow */
2812 emc_change_entry(current_entry
, flow
, NULL
);
2816 /* Replacement policy: put the flow in an empty (not alive) entry, or
2817 * in the first entry where it can be */
2819 || (emc_entry_alive(to_be_replaced
)
2820 && !emc_entry_alive(current_entry
))
2821 || current_entry
->key
.hash
< to_be_replaced
->key
.hash
) {
2822 to_be_replaced
= current_entry
;
2825 /* We didn't find the miniflow in the cache.
2826 * The 'to_be_replaced' entry is where the new flow will be stored */
2828 emc_change_entry(to_be_replaced
, flow
, key
);
2832 emc_probabilistic_insert(struct dp_netdev_pmd_thread
*pmd
,
2833 const struct netdev_flow_key
*key
,
2834 struct dp_netdev_flow
*flow
)
2836 /* Insert an entry into the EMC based on probability value 'min'. By
2837 * default the value is UINT32_MAX / 100 which yields an insertion
2838 * probability of 1/100 ie. 1% */
2840 uint32_t min
= pmd
->ctx
.emc_insert_min
;
2842 if (min
&& random_uint32() <= min
) {
2843 emc_insert(&(pmd
->flow_cache
).emc_cache
, key
, flow
);
2847 static inline struct dp_netdev_flow
*
2848 emc_lookup(struct emc_cache
*cache
, const struct netdev_flow_key
*key
)
2850 struct emc_entry
*current_entry
;
2852 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
2853 if (current_entry
->key
.hash
== key
->hash
2854 && emc_entry_alive(current_entry
)
2855 && netdev_flow_key_equal_mf(¤t_entry
->key
, &key
->mf
)) {
2857 /* We found the entry with the 'key->mf' miniflow */
2858 return current_entry
->flow
;
2865 static inline const struct cmap_node
*
2866 smc_entry_get(struct dp_netdev_pmd_thread
*pmd
, const uint32_t hash
)
2868 struct smc_cache
*cache
= &(pmd
->flow_cache
).smc_cache
;
2869 struct smc_bucket
*bucket
= &cache
->buckets
[hash
& SMC_MASK
];
2870 uint16_t sig
= hash
>> 16;
2871 uint16_t index
= UINT16_MAX
;
2873 for (int i
= 0; i
< SMC_ENTRY_PER_BUCKET
; i
++) {
2874 if (bucket
->sig
[i
] == sig
) {
2875 index
= bucket
->flow_idx
[i
];
2879 if (index
!= UINT16_MAX
) {
2880 return cmap_find_by_index(&pmd
->flow_table
, index
);
2886 smc_clear_entry(struct smc_bucket
*b
, int idx
)
2888 b
->flow_idx
[idx
] = UINT16_MAX
;
2891 /* Insert the flow_table index into SMC. Insertion may fail when 1) SMC is
2892 * turned off, 2) the flow_table index is larger than uint16_t can handle.
2893 * If there is already an SMC entry having same signature, the index will be
2894 * updated. If there is no existing entry, but an empty entry is available,
2895 * the empty entry will be taken. If no empty entry or existing same signature,
2896 * a random entry from the hashed bucket will be picked. */
2898 smc_insert(struct dp_netdev_pmd_thread
*pmd
,
2899 const struct netdev_flow_key
*key
,
2902 struct smc_cache
*smc_cache
= &(pmd
->flow_cache
).smc_cache
;
2903 struct smc_bucket
*bucket
= &smc_cache
->buckets
[key
->hash
& SMC_MASK
];
2905 uint32_t cmap_index
;
2909 atomic_read_relaxed(&pmd
->dp
->smc_enable_db
, &smc_enable_db
);
2910 if (!smc_enable_db
) {
2914 cmap_index
= cmap_find_index(&pmd
->flow_table
, hash
);
2915 index
= (cmap_index
>= UINT16_MAX
) ? UINT16_MAX
: (uint16_t)cmap_index
;
2917 /* If the index is larger than SMC can handle (uint16_t), we don't
2919 if (index
== UINT16_MAX
) {
2923 /* If an entry with same signature already exists, update the index */
2924 uint16_t sig
= key
->hash
>> 16;
2925 for (i
= 0; i
< SMC_ENTRY_PER_BUCKET
; i
++) {
2926 if (bucket
->sig
[i
] == sig
) {
2927 bucket
->flow_idx
[i
] = index
;
2931 /* If there is an empty entry, occupy it. */
2932 for (i
= 0; i
< SMC_ENTRY_PER_BUCKET
; i
++) {
2933 if (bucket
->flow_idx
[i
] == UINT16_MAX
) {
2934 bucket
->sig
[i
] = sig
;
2935 bucket
->flow_idx
[i
] = index
;
2939 /* Otherwise, pick a random entry. */
2940 i
= random_uint32() % SMC_ENTRY_PER_BUCKET
;
2941 bucket
->sig
[i
] = sig
;
2942 bucket
->flow_idx
[i
] = index
;
2945 static struct dp_netdev_flow
*
2946 dp_netdev_pmd_lookup_flow(struct dp_netdev_pmd_thread
*pmd
,
2947 const struct netdev_flow_key
*key
,
2951 struct dpcls_rule
*rule
;
2952 odp_port_t in_port
= u32_to_odp(MINIFLOW_GET_U32(&key
->mf
,
2954 struct dp_netdev_flow
*netdev_flow
= NULL
;
2956 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
2957 if (OVS_LIKELY(cls
)) {
2958 dpcls_lookup(cls
, &key
, &rule
, 1, lookup_num_p
);
2959 netdev_flow
= dp_netdev_flow_cast(rule
);
2964 static struct dp_netdev_flow
*
2965 dp_netdev_pmd_find_flow(const struct dp_netdev_pmd_thread
*pmd
,
2966 const ovs_u128
*ufidp
, const struct nlattr
*key
,
2969 struct dp_netdev_flow
*netdev_flow
;
2973 /* If a UFID is not provided, determine one based on the key. */
2974 if (!ufidp
&& key
&& key_len
2975 && !dpif_netdev_flow_from_nlattrs(key
, key_len
, &flow
, false)) {
2976 dpif_flow_hash(pmd
->dp
->dpif
, &flow
, sizeof flow
, &ufid
);
2981 CMAP_FOR_EACH_WITH_HASH (netdev_flow
, node
, dp_netdev_flow_hash(ufidp
),
2983 if (ovs_u128_equals(netdev_flow
->ufid
, *ufidp
)) {
2993 get_dpif_flow_stats(const struct dp_netdev_flow
*netdev_flow_
,
2994 struct dpif_flow_stats
*stats
)
2996 struct dp_netdev_flow
*netdev_flow
;
2997 unsigned long long n
;
3001 netdev_flow
= CONST_CAST(struct dp_netdev_flow
*, netdev_flow_
);
3003 atomic_read_relaxed(&netdev_flow
->stats
.packet_count
, &n
);
3004 stats
->n_packets
= n
;
3005 atomic_read_relaxed(&netdev_flow
->stats
.byte_count
, &n
);
3007 atomic_read_relaxed(&netdev_flow
->stats
.used
, &used
);
3009 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
3010 stats
->tcp_flags
= flags
;
3013 /* Converts to the dpif_flow format, using 'key_buf' and 'mask_buf' for
3014 * storing the netlink-formatted key/mask. 'key_buf' may be the same as
3015 * 'mask_buf'. Actions will be returned without copying, by relying on RCU to
3018 dp_netdev_flow_to_dpif_flow(const struct dp_netdev_flow
*netdev_flow
,
3019 struct ofpbuf
*key_buf
, struct ofpbuf
*mask_buf
,
3020 struct dpif_flow
*flow
, bool terse
)
3023 memset(flow
, 0, sizeof *flow
);
3025 struct flow_wildcards wc
;
3026 struct dp_netdev_actions
*actions
;
3028 struct odp_flow_key_parms odp_parms
= {
3029 .flow
= &netdev_flow
->flow
,
3031 .support
= dp_netdev_support
,
3034 miniflow_expand(&netdev_flow
->cr
.mask
->mf
, &wc
.masks
);
3035 /* in_port is exact matched, but we have left it out from the mask for
3036 * optimnization reasons. Add in_port back to the mask. */
3037 wc
.masks
.in_port
.odp_port
= ODPP_NONE
;
3040 offset
= key_buf
->size
;
3041 flow
->key
= ofpbuf_tail(key_buf
);
3042 odp_flow_key_from_flow(&odp_parms
, key_buf
);
3043 flow
->key_len
= key_buf
->size
- offset
;
3046 offset
= mask_buf
->size
;
3047 flow
->mask
= ofpbuf_tail(mask_buf
);
3048 odp_parms
.key_buf
= key_buf
;
3049 odp_flow_key_from_mask(&odp_parms
, mask_buf
);
3050 flow
->mask_len
= mask_buf
->size
- offset
;
3053 actions
= dp_netdev_flow_get_actions(netdev_flow
);
3054 flow
->actions
= actions
->actions
;
3055 flow
->actions_len
= actions
->size
;
3058 flow
->ufid
= netdev_flow
->ufid
;
3059 flow
->ufid_present
= true;
3060 flow
->pmd_id
= netdev_flow
->pmd_id
;
3061 get_dpif_flow_stats(netdev_flow
, &flow
->stats
);
3063 flow
->attrs
.offloaded
= false;
3064 flow
->attrs
.dp_layer
= "ovs";
3068 dpif_netdev_mask_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
3069 const struct nlattr
*mask_key
,
3070 uint32_t mask_key_len
, const struct flow
*flow
,
3071 struct flow_wildcards
*wc
, bool probe
)
3073 enum odp_key_fitness fitness
;
3075 fitness
= odp_flow_key_to_mask(mask_key
, mask_key_len
, wc
, flow
, NULL
);
3078 /* This should not happen: it indicates that
3079 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
3080 * disagree on the acceptable form of a mask. Log the problem
3081 * as an error, with enough details to enable debugging. */
3082 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3084 if (!VLOG_DROP_ERR(&rl
)) {
3088 odp_flow_format(key
, key_len
, mask_key
, mask_key_len
, NULL
, &s
,
3090 VLOG_ERR("internal error parsing flow mask %s (%s)",
3091 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
3103 dpif_netdev_flow_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
3104 struct flow
*flow
, bool probe
)
3106 if (odp_flow_key_to_flow(key
, key_len
, flow
, NULL
)) {
3108 /* This should not happen: it indicates that
3109 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
3110 * the acceptable form of a flow. Log the problem as an error,
3111 * with enough details to enable debugging. */
3112 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3114 if (!VLOG_DROP_ERR(&rl
)) {
3118 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
3119 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
3127 if (flow
->ct_state
& DP_NETDEV_CS_UNSUPPORTED_MASK
) {
3135 dpif_netdev_flow_get(const struct dpif
*dpif
, const struct dpif_flow_get
*get
)
3137 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3138 struct dp_netdev_flow
*netdev_flow
;
3139 struct dp_netdev_pmd_thread
*pmd
;
3140 struct hmapx to_find
= HMAPX_INITIALIZER(&to_find
);
3141 struct hmapx_node
*node
;
3144 if (get
->pmd_id
== PMD_ID_NULL
) {
3145 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3146 if (dp_netdev_pmd_try_ref(pmd
) && !hmapx_add(&to_find
, pmd
)) {
3147 dp_netdev_pmd_unref(pmd
);
3151 pmd
= dp_netdev_get_pmd(dp
, get
->pmd_id
);
3155 hmapx_add(&to_find
, pmd
);
3158 if (!hmapx_count(&to_find
)) {
3162 HMAPX_FOR_EACH (node
, &to_find
) {
3163 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3164 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, get
->ufid
, get
->key
,
3167 dp_netdev_flow_to_dpif_flow(netdev_flow
, get
->buffer
, get
->buffer
,
3176 HMAPX_FOR_EACH (node
, &to_find
) {
3177 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3178 dp_netdev_pmd_unref(pmd
);
3181 hmapx_destroy(&to_find
);
3186 dp_netdev_get_mega_ufid(const struct match
*match
, ovs_u128
*mega_ufid
)
3188 struct flow masked_flow
;
3191 for (i
= 0; i
< sizeof(struct flow
); i
++) {
3192 ((uint8_t *)&masked_flow
)[i
] = ((uint8_t *)&match
->flow
)[i
] &
3193 ((uint8_t *)&match
->wc
)[i
];
3195 dpif_flow_hash(NULL
, &masked_flow
, sizeof(struct flow
), mega_ufid
);
3198 static struct dp_netdev_flow
*
3199 dp_netdev_flow_add(struct dp_netdev_pmd_thread
*pmd
,
3200 struct match
*match
, const ovs_u128
*ufid
,
3201 const struct nlattr
*actions
, size_t actions_len
)
3202 OVS_REQUIRES(pmd
->flow_mutex
)
3204 struct dp_netdev_flow
*flow
;
3205 struct netdev_flow_key mask
;
3208 /* Make sure in_port is exact matched before we read it. */
3209 ovs_assert(match
->wc
.masks
.in_port
.odp_port
== ODPP_NONE
);
3210 odp_port_t in_port
= match
->flow
.in_port
.odp_port
;
3212 /* As we select the dpcls based on the port number, each netdev flow
3213 * belonging to the same dpcls will have the same odp_port value.
3214 * For performance reasons we wildcard odp_port here in the mask. In the
3215 * typical case dp_hash is also wildcarded, and the resulting 8-byte
3216 * chunk {dp_hash, in_port} will be ignored by netdev_flow_mask_init() and
3217 * will not be part of the subtable mask.
3218 * This will speed up the hash computation during dpcls_lookup() because
3219 * there is one less call to hash_add64() in this case. */
3220 match
->wc
.masks
.in_port
.odp_port
= 0;
3221 netdev_flow_mask_init(&mask
, match
);
3222 match
->wc
.masks
.in_port
.odp_port
= ODPP_NONE
;
3224 /* Make sure wc does not have metadata. */
3225 ovs_assert(!FLOWMAP_HAS_FIELD(&mask
.mf
.map
, metadata
)
3226 && !FLOWMAP_HAS_FIELD(&mask
.mf
.map
, regs
));
3228 /* Do not allocate extra space. */
3229 flow
= xmalloc(sizeof *flow
- sizeof flow
->cr
.flow
.mf
+ mask
.len
);
3230 memset(&flow
->stats
, 0, sizeof flow
->stats
);
3233 flow
->mark
= INVALID_FLOW_MARK
;
3234 *CONST_CAST(unsigned *, &flow
->pmd_id
) = pmd
->core_id
;
3235 *CONST_CAST(struct flow
*, &flow
->flow
) = match
->flow
;
3236 *CONST_CAST(ovs_u128
*, &flow
->ufid
) = *ufid
;
3237 ovs_refcount_init(&flow
->ref_cnt
);
3238 ovsrcu_set(&flow
->actions
, dp_netdev_actions_create(actions
, actions_len
));
3240 dp_netdev_get_mega_ufid(match
, CONST_CAST(ovs_u128
*, &flow
->mega_ufid
));
3241 netdev_flow_key_init_masked(&flow
->cr
.flow
, &match
->flow
, &mask
);
3243 /* Select dpcls for in_port. Relies on in_port to be exact match. */
3244 cls
= dp_netdev_pmd_find_dpcls(pmd
, in_port
);
3245 dpcls_insert(cls
, &flow
->cr
, &mask
);
3247 cmap_insert(&pmd
->flow_table
, CONST_CAST(struct cmap_node
*, &flow
->node
),
3248 dp_netdev_flow_hash(&flow
->ufid
));
3250 queue_netdev_flow_put(pmd
, flow
, match
, actions
, actions_len
);
3252 if (OVS_UNLIKELY(!VLOG_DROP_DBG((&upcall_rl
)))) {
3253 struct ds ds
= DS_EMPTY_INITIALIZER
;
3254 struct ofpbuf key_buf
, mask_buf
;
3255 struct odp_flow_key_parms odp_parms
= {
3256 .flow
= &match
->flow
,
3257 .mask
= &match
->wc
.masks
,
3258 .support
= dp_netdev_support
,
3261 ofpbuf_init(&key_buf
, 0);
3262 ofpbuf_init(&mask_buf
, 0);
3264 odp_flow_key_from_flow(&odp_parms
, &key_buf
);
3265 odp_parms
.key_buf
= &key_buf
;
3266 odp_flow_key_from_mask(&odp_parms
, &mask_buf
);
3268 ds_put_cstr(&ds
, "flow_add: ");
3269 odp_format_ufid(ufid
, &ds
);
3270 ds_put_cstr(&ds
, " ");
3271 odp_flow_format(key_buf
.data
, key_buf
.size
,
3272 mask_buf
.data
, mask_buf
.size
,
3274 ds_put_cstr(&ds
, ", actions:");
3275 format_odp_actions(&ds
, actions
, actions_len
, NULL
);
3277 VLOG_DBG("%s", ds_cstr(&ds
));
3279 ofpbuf_uninit(&key_buf
);
3280 ofpbuf_uninit(&mask_buf
);
3282 /* Add a printout of the actual match installed. */
3285 ds_put_cstr(&ds
, "flow match: ");
3286 miniflow_expand(&flow
->cr
.flow
.mf
, &m
.flow
);
3287 miniflow_expand(&flow
->cr
.mask
->mf
, &m
.wc
.masks
);
3288 memset(&m
.tun_md
, 0, sizeof m
.tun_md
);
3289 match_format(&m
, NULL
, &ds
, OFP_DEFAULT_PRIORITY
);
3291 VLOG_DBG("%s", ds_cstr(&ds
));
3300 flow_put_on_pmd(struct dp_netdev_pmd_thread
*pmd
,
3301 struct netdev_flow_key
*key
,
3302 struct match
*match
,
3304 const struct dpif_flow_put
*put
,
3305 struct dpif_flow_stats
*stats
)
3307 struct dp_netdev_flow
*netdev_flow
;
3311 memset(stats
, 0, sizeof *stats
);
3314 ovs_mutex_lock(&pmd
->flow_mutex
);
3315 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
, NULL
);
3317 if (put
->flags
& DPIF_FP_CREATE
) {
3318 if (cmap_count(&pmd
->flow_table
) < MAX_FLOWS
) {
3319 dp_netdev_flow_add(pmd
, match
, ufid
, put
->actions
,
3329 if (put
->flags
& DPIF_FP_MODIFY
) {
3330 struct dp_netdev_actions
*new_actions
;
3331 struct dp_netdev_actions
*old_actions
;
3333 new_actions
= dp_netdev_actions_create(put
->actions
,
3336 old_actions
= dp_netdev_flow_get_actions(netdev_flow
);
3337 ovsrcu_set(&netdev_flow
->actions
, new_actions
);
3339 queue_netdev_flow_put(pmd
, netdev_flow
, match
,
3340 put
->actions
, put
->actions_len
);
3343 get_dpif_flow_stats(netdev_flow
, stats
);
3345 if (put
->flags
& DPIF_FP_ZERO_STATS
) {
3346 /* XXX: The userspace datapath uses thread local statistics
3347 * (for flows), which should be updated only by the owning
3348 * thread. Since we cannot write on stats memory here,
3349 * we choose not to support this flag. Please note:
3350 * - This feature is currently used only by dpctl commands with
3352 * - Should the need arise, this operation can be implemented
3353 * by keeping a base value (to be update here) for each
3354 * counter, and subtracting it before outputting the stats */
3358 ovsrcu_postpone(dp_netdev_actions_free
, old_actions
);
3359 } else if (put
->flags
& DPIF_FP_CREATE
) {
3362 /* Overlapping flow. */
3366 ovs_mutex_unlock(&pmd
->flow_mutex
);
3371 dpif_netdev_flow_put(struct dpif
*dpif
, const struct dpif_flow_put
*put
)
3373 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3374 struct netdev_flow_key key
, mask
;
3375 struct dp_netdev_pmd_thread
*pmd
;
3379 bool probe
= put
->flags
& DPIF_FP_PROBE
;
3382 memset(put
->stats
, 0, sizeof *put
->stats
);
3384 error
= dpif_netdev_flow_from_nlattrs(put
->key
, put
->key_len
, &match
.flow
,
3389 error
= dpif_netdev_mask_from_nlattrs(put
->key
, put
->key_len
,
3390 put
->mask
, put
->mask_len
,
3391 &match
.flow
, &match
.wc
, probe
);
3399 dpif_flow_hash(dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
3402 /* The Netlink encoding of datapath flow keys cannot express
3403 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
3404 * tag is interpreted as exact match on the fact that there is no
3405 * VLAN. Unless we refactor a lot of code that translates between
3406 * Netlink and struct flow representations, we have to do the same
3407 * here. This must be in sync with 'match' in handle_packet_upcall(). */
3408 if (!match
.wc
.masks
.vlans
[0].tci
) {
3409 match
.wc
.masks
.vlans
[0].tci
= htons(0xffff);
3412 /* Must produce a netdev_flow_key for lookup.
3413 * Use the same method as employed to create the key when adding
3414 * the flow to the dplcs to make sure they match. */
3415 netdev_flow_mask_init(&mask
, &match
);
3416 netdev_flow_key_init_masked(&key
, &match
.flow
, &mask
);
3418 if (put
->pmd_id
== PMD_ID_NULL
) {
3419 if (cmap_count(&dp
->poll_threads
) == 0) {
3422 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3423 struct dpif_flow_stats pmd_stats
;
3426 pmd_error
= flow_put_on_pmd(pmd
, &key
, &match
, &ufid
, put
,
3430 } else if (put
->stats
) {
3431 put
->stats
->n_packets
+= pmd_stats
.n_packets
;
3432 put
->stats
->n_bytes
+= pmd_stats
.n_bytes
;
3433 put
->stats
->used
= MAX(put
->stats
->used
, pmd_stats
.used
);
3434 put
->stats
->tcp_flags
|= pmd_stats
.tcp_flags
;
3438 pmd
= dp_netdev_get_pmd(dp
, put
->pmd_id
);
3442 error
= flow_put_on_pmd(pmd
, &key
, &match
, &ufid
, put
, put
->stats
);
3443 dp_netdev_pmd_unref(pmd
);
3450 flow_del_on_pmd(struct dp_netdev_pmd_thread
*pmd
,
3451 struct dpif_flow_stats
*stats
,
3452 const struct dpif_flow_del
*del
)
3454 struct dp_netdev_flow
*netdev_flow
;
3457 ovs_mutex_lock(&pmd
->flow_mutex
);
3458 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, del
->ufid
, del
->key
,
3462 get_dpif_flow_stats(netdev_flow
, stats
);
3464 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
3468 ovs_mutex_unlock(&pmd
->flow_mutex
);
3474 dpif_netdev_flow_del(struct dpif
*dpif
, const struct dpif_flow_del
*del
)
3476 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3477 struct dp_netdev_pmd_thread
*pmd
;
3481 memset(del
->stats
, 0, sizeof *del
->stats
);
3484 if (del
->pmd_id
== PMD_ID_NULL
) {
3485 if (cmap_count(&dp
->poll_threads
) == 0) {
3488 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3489 struct dpif_flow_stats pmd_stats
;
3492 pmd_error
= flow_del_on_pmd(pmd
, &pmd_stats
, del
);
3495 } else if (del
->stats
) {
3496 del
->stats
->n_packets
+= pmd_stats
.n_packets
;
3497 del
->stats
->n_bytes
+= pmd_stats
.n_bytes
;
3498 del
->stats
->used
= MAX(del
->stats
->used
, pmd_stats
.used
);
3499 del
->stats
->tcp_flags
|= pmd_stats
.tcp_flags
;
3503 pmd
= dp_netdev_get_pmd(dp
, del
->pmd_id
);
3507 error
= flow_del_on_pmd(pmd
, del
->stats
, del
);
3508 dp_netdev_pmd_unref(pmd
);
3515 struct dpif_netdev_flow_dump
{
3516 struct dpif_flow_dump up
;
3517 struct cmap_position poll_thread_pos
;
3518 struct cmap_position flow_pos
;
3519 struct dp_netdev_pmd_thread
*cur_pmd
;
3521 struct ovs_mutex mutex
;
3524 static struct dpif_netdev_flow_dump
*
3525 dpif_netdev_flow_dump_cast(struct dpif_flow_dump
*dump
)
3527 return CONTAINER_OF(dump
, struct dpif_netdev_flow_dump
, up
);
3530 static struct dpif_flow_dump
*
3531 dpif_netdev_flow_dump_create(const struct dpif
*dpif_
, bool terse
,
3532 struct dpif_flow_dump_types
*types OVS_UNUSED
)
3534 struct dpif_netdev_flow_dump
*dump
;
3536 dump
= xzalloc(sizeof *dump
);
3537 dpif_flow_dump_init(&dump
->up
, dpif_
);
3538 dump
->up
.terse
= terse
;
3539 ovs_mutex_init(&dump
->mutex
);
3545 dpif_netdev_flow_dump_destroy(struct dpif_flow_dump
*dump_
)
3547 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
3549 ovs_mutex_destroy(&dump
->mutex
);
3554 struct dpif_netdev_flow_dump_thread
{
3555 struct dpif_flow_dump_thread up
;
3556 struct dpif_netdev_flow_dump
*dump
;
3557 struct odputil_keybuf keybuf
[FLOW_DUMP_MAX_BATCH
];
3558 struct odputil_keybuf maskbuf
[FLOW_DUMP_MAX_BATCH
];
3561 static struct dpif_netdev_flow_dump_thread
*
3562 dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread
*thread
)
3564 return CONTAINER_OF(thread
, struct dpif_netdev_flow_dump_thread
, up
);
3567 static struct dpif_flow_dump_thread
*
3568 dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump
*dump_
)
3570 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
3571 struct dpif_netdev_flow_dump_thread
*thread
;
3573 thread
= xmalloc(sizeof *thread
);
3574 dpif_flow_dump_thread_init(&thread
->up
, &dump
->up
);
3575 thread
->dump
= dump
;
3580 dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread
*thread_
)
3582 struct dpif_netdev_flow_dump_thread
*thread
3583 = dpif_netdev_flow_dump_thread_cast(thread_
);
3589 dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread
*thread_
,
3590 struct dpif_flow
*flows
, int max_flows
)
3592 struct dpif_netdev_flow_dump_thread
*thread
3593 = dpif_netdev_flow_dump_thread_cast(thread_
);
3594 struct dpif_netdev_flow_dump
*dump
= thread
->dump
;
3595 struct dp_netdev_flow
*netdev_flows
[FLOW_DUMP_MAX_BATCH
];
3599 ovs_mutex_lock(&dump
->mutex
);
3600 if (!dump
->status
) {
3601 struct dpif_netdev
*dpif
= dpif_netdev_cast(thread
->up
.dpif
);
3602 struct dp_netdev
*dp
= get_dp_netdev(&dpif
->dpif
);
3603 struct dp_netdev_pmd_thread
*pmd
= dump
->cur_pmd
;
3604 int flow_limit
= MIN(max_flows
, FLOW_DUMP_MAX_BATCH
);
3606 /* First call to dump_next(), extracts the first pmd thread.
3607 * If there is no pmd thread, returns immediately. */
3609 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
3611 ovs_mutex_unlock(&dump
->mutex
);
3618 for (n_flows
= 0; n_flows
< flow_limit
; n_flows
++) {
3619 struct cmap_node
*node
;
3621 node
= cmap_next_position(&pmd
->flow_table
, &dump
->flow_pos
);
3625 netdev_flows
[n_flows
] = CONTAINER_OF(node
,
3626 struct dp_netdev_flow
,
3629 /* When finishing dumping the current pmd thread, moves to
3631 if (n_flows
< flow_limit
) {
3632 memset(&dump
->flow_pos
, 0, sizeof dump
->flow_pos
);
3633 dp_netdev_pmd_unref(pmd
);
3634 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
3640 /* Keeps the reference to next caller. */
3641 dump
->cur_pmd
= pmd
;
3643 /* If the current dump is empty, do not exit the loop, since the
3644 * remaining pmds could have flows to be dumped. Just dumps again
3645 * on the new 'pmd'. */
3648 ovs_mutex_unlock(&dump
->mutex
);
3650 for (i
= 0; i
< n_flows
; i
++) {
3651 struct odputil_keybuf
*maskbuf
= &thread
->maskbuf
[i
];
3652 struct odputil_keybuf
*keybuf
= &thread
->keybuf
[i
];
3653 struct dp_netdev_flow
*netdev_flow
= netdev_flows
[i
];
3654 struct dpif_flow
*f
= &flows
[i
];
3655 struct ofpbuf key
, mask
;
3657 ofpbuf_use_stack(&key
, keybuf
, sizeof *keybuf
);
3658 ofpbuf_use_stack(&mask
, maskbuf
, sizeof *maskbuf
);
3659 dp_netdev_flow_to_dpif_flow(netdev_flow
, &key
, &mask
, f
,
3667 dpif_netdev_execute(struct dpif
*dpif
, struct dpif_execute
*execute
)
3668 OVS_NO_THREAD_SAFETY_ANALYSIS
3670 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3671 struct dp_netdev_pmd_thread
*pmd
;
3672 struct dp_packet_batch pp
;
3674 if (dp_packet_size(execute
->packet
) < ETH_HEADER_LEN
||
3675 dp_packet_size(execute
->packet
) > UINT16_MAX
) {
3679 /* Tries finding the 'pmd'. If NULL is returned, that means
3680 * the current thread is a non-pmd thread and should use
3681 * dp_netdev_get_pmd(dp, NON_PMD_CORE_ID). */
3682 pmd
= ovsthread_getspecific(dp
->per_pmd_key
);
3684 pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
3690 if (execute
->probe
) {
3691 /* If this is part of a probe, Drop the packet, since executing
3692 * the action may actually cause spurious packets be sent into
3694 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3695 dp_netdev_pmd_unref(pmd
);
3700 /* If the current thread is non-pmd thread, acquires
3701 * the 'non_pmd_mutex'. */
3702 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3703 ovs_mutex_lock(&dp
->non_pmd_mutex
);
3706 /* Update current time in PMD context. We don't care about EMC insertion
3707 * probability, because we are on a slow path. */
3708 pmd_thread_ctx_time_update(pmd
);
3710 /* The action processing expects the RSS hash to be valid, because
3711 * it's always initialized at the beginning of datapath processing.
3712 * In this case, though, 'execute->packet' may not have gone through
3713 * the datapath at all, it may have been generated by the upper layer
3714 * (OpenFlow packet-out, BFD frame, ...). */
3715 if (!dp_packet_rss_valid(execute
->packet
)) {
3716 dp_packet_set_rss_hash(execute
->packet
,
3717 flow_hash_5tuple(execute
->flow
, 0));
3720 dp_packet_batch_init_packet(&pp
, execute
->packet
);
3721 pp
.do_not_steal
= true;
3722 dp_netdev_execute_actions(pmd
, &pp
, false, execute
->flow
,
3723 execute
->actions
, execute
->actions_len
);
3724 dp_netdev_pmd_flush_output_packets(pmd
, true);
3726 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3727 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
3728 dp_netdev_pmd_unref(pmd
);
3735 dpif_netdev_operate(struct dpif
*dpif
, struct dpif_op
**ops
, size_t n_ops
,
3736 enum dpif_offload_type offload_type OVS_UNUSED
)
3740 for (i
= 0; i
< n_ops
; i
++) {
3741 struct dpif_op
*op
= ops
[i
];
3744 case DPIF_OP_FLOW_PUT
:
3745 op
->error
= dpif_netdev_flow_put(dpif
, &op
->flow_put
);
3748 case DPIF_OP_FLOW_DEL
:
3749 op
->error
= dpif_netdev_flow_del(dpif
, &op
->flow_del
);
3752 case DPIF_OP_EXECUTE
:
3753 op
->error
= dpif_netdev_execute(dpif
, &op
->execute
);
3756 case DPIF_OP_FLOW_GET
:
3757 op
->error
= dpif_netdev_flow_get(dpif
, &op
->flow_get
);
3763 /* Enable or Disable PMD auto load balancing. */
3765 set_pmd_auto_lb(struct dp_netdev
*dp
)
3767 unsigned int cnt
= 0;
3768 struct dp_netdev_pmd_thread
*pmd
;
3769 struct pmd_auto_lb
*pmd_alb
= &dp
->pmd_alb
;
3771 bool enable_alb
= false;
3772 bool multi_rxq
= false;
3773 bool pmd_rxq_assign_cyc
= dp
->pmd_rxq_assign_cyc
;
3775 /* Ensure that there is at least 2 non-isolated PMDs and
3776 * one of them is polling more than one rxq. */
3777 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3778 if (pmd
->core_id
== NON_PMD_CORE_ID
|| pmd
->isolated
) {
3782 if (hmap_count(&pmd
->poll_list
) > 1) {
3785 if (cnt
&& multi_rxq
) {
3792 /* Enable auto LB if it is requested and cycle based assignment is true. */
3793 enable_alb
= enable_alb
&& pmd_rxq_assign_cyc
&&
3794 pmd_alb
->auto_lb_requested
;
3796 if (pmd_alb
->is_enabled
!= enable_alb
) {
3797 pmd_alb
->is_enabled
= enable_alb
;
3798 if (pmd_alb
->is_enabled
) {
3799 VLOG_INFO("PMD auto load balance is enabled "
3800 "(with rebalance interval:%"PRIu64
" msec)",
3801 pmd_alb
->rebalance_intvl
);
3803 pmd_alb
->rebalance_poll_timer
= 0;
3804 VLOG_INFO("PMD auto load balance is disabled");
3810 /* Applies datapath configuration from the database. Some of the changes are
3811 * actually applied in dpif_netdev_run(). */
3813 dpif_netdev_set_config(struct dpif
*dpif
, const struct smap
*other_config
)
3815 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3816 const char *cmask
= smap_get(other_config
, "pmd-cpu-mask");
3817 const char *pmd_rxq_assign
= smap_get_def(other_config
, "pmd-rxq-assign",
3819 unsigned long long insert_prob
=
3820 smap_get_ullong(other_config
, "emc-insert-inv-prob",
3821 DEFAULT_EM_FLOW_INSERT_INV_PROB
);
3822 uint32_t insert_min
, cur_min
;
3823 uint32_t tx_flush_interval
, cur_tx_flush_interval
;
3824 uint64_t rebalance_intvl
;
3826 tx_flush_interval
= smap_get_int(other_config
, "tx-flush-interval",
3827 DEFAULT_TX_FLUSH_INTERVAL
);
3828 atomic_read_relaxed(&dp
->tx_flush_interval
, &cur_tx_flush_interval
);
3829 if (tx_flush_interval
!= cur_tx_flush_interval
) {
3830 atomic_store_relaxed(&dp
->tx_flush_interval
, tx_flush_interval
);
3831 VLOG_INFO("Flushing interval for tx queues set to %"PRIu32
" us",
3835 if (!nullable_string_is_equal(dp
->pmd_cmask
, cmask
)) {
3836 free(dp
->pmd_cmask
);
3837 dp
->pmd_cmask
= nullable_xstrdup(cmask
);
3838 dp_netdev_request_reconfigure(dp
);
3841 atomic_read_relaxed(&dp
->emc_insert_min
, &cur_min
);
3842 if (insert_prob
<= UINT32_MAX
) {
3843 insert_min
= insert_prob
== 0 ? 0 : UINT32_MAX
/ insert_prob
;
3845 insert_min
= DEFAULT_EM_FLOW_INSERT_MIN
;
3846 insert_prob
= DEFAULT_EM_FLOW_INSERT_INV_PROB
;
3849 if (insert_min
!= cur_min
) {
3850 atomic_store_relaxed(&dp
->emc_insert_min
, insert_min
);
3851 if (insert_min
== 0) {
3852 VLOG_INFO("EMC insertion probability changed to zero");
3854 VLOG_INFO("EMC insertion probability changed to 1/%llu (~%.2f%%)",
3855 insert_prob
, (100 / (float)insert_prob
));
3859 bool perf_enabled
= smap_get_bool(other_config
, "pmd-perf-metrics", false);
3860 bool cur_perf_enabled
;
3861 atomic_read_relaxed(&dp
->pmd_perf_metrics
, &cur_perf_enabled
);
3862 if (perf_enabled
!= cur_perf_enabled
) {
3863 atomic_store_relaxed(&dp
->pmd_perf_metrics
, perf_enabled
);
3865 VLOG_INFO("PMD performance metrics collection enabled");
3867 VLOG_INFO("PMD performance metrics collection disabled");
3871 bool smc_enable
= smap_get_bool(other_config
, "smc-enable", false);
3873 atomic_read_relaxed(&dp
->smc_enable_db
, &cur_smc
);
3874 if (smc_enable
!= cur_smc
) {
3875 atomic_store_relaxed(&dp
->smc_enable_db
, smc_enable
);
3877 VLOG_INFO("SMC cache is enabled");
3879 VLOG_INFO("SMC cache is disabled");
3883 bool pmd_rxq_assign_cyc
= !strcmp(pmd_rxq_assign
, "cycles");
3884 if (!pmd_rxq_assign_cyc
&& strcmp(pmd_rxq_assign
, "roundrobin")) {
3885 VLOG_WARN("Unsupported Rxq to PMD assignment mode in pmd-rxq-assign. "
3886 "Defaulting to 'cycles'.");
3887 pmd_rxq_assign_cyc
= true;
3888 pmd_rxq_assign
= "cycles";
3890 if (dp
->pmd_rxq_assign_cyc
!= pmd_rxq_assign_cyc
) {
3891 dp
->pmd_rxq_assign_cyc
= pmd_rxq_assign_cyc
;
3892 VLOG_INFO("Rxq to PMD assignment mode changed to: \'%s\'.",
3894 dp_netdev_request_reconfigure(dp
);
3897 struct pmd_auto_lb
*pmd_alb
= &dp
->pmd_alb
;
3898 pmd_alb
->auto_lb_requested
= smap_get_bool(other_config
, "pmd-auto-lb",
3901 rebalance_intvl
= smap_get_int(other_config
, "pmd-auto-lb-rebal-interval",
3902 ALB_PMD_REBALANCE_POLL_INTERVAL
);
3904 /* Input is in min, convert it to msec. */
3906 rebalance_intvl
? rebalance_intvl
* MIN_TO_MSEC
: MIN_TO_MSEC
;
3908 if (pmd_alb
->rebalance_intvl
!= rebalance_intvl
) {
3909 pmd_alb
->rebalance_intvl
= rebalance_intvl
;
3912 set_pmd_auto_lb(dp
);
3916 /* Parses affinity list and returns result in 'core_ids'. */
3918 parse_affinity_list(const char *affinity_list
, unsigned *core_ids
, int n_rxq
)
3921 char *list
, *copy
, *key
, *value
;
3924 for (i
= 0; i
< n_rxq
; i
++) {
3925 core_ids
[i
] = OVS_CORE_UNSPEC
;
3928 if (!affinity_list
) {
3932 list
= copy
= xstrdup(affinity_list
);
3934 while (ofputil_parse_key_value(&list
, &key
, &value
)) {
3935 int rxq_id
, core_id
;
3937 if (!str_to_int(key
, 0, &rxq_id
) || rxq_id
< 0
3938 || !str_to_int(value
, 0, &core_id
) || core_id
< 0) {
3943 if (rxq_id
< n_rxq
) {
3944 core_ids
[rxq_id
] = core_id
;
3952 /* Parses 'affinity_list' and applies configuration if it is valid. */
3954 dpif_netdev_port_set_rxq_affinity(struct dp_netdev_port
*port
,
3955 const char *affinity_list
)
3957 unsigned *core_ids
, i
;
3960 core_ids
= xmalloc(port
->n_rxq
* sizeof *core_ids
);
3961 if (parse_affinity_list(affinity_list
, core_ids
, port
->n_rxq
)) {
3966 for (i
= 0; i
< port
->n_rxq
; i
++) {
3967 port
->rxqs
[i
].core_id
= core_ids
[i
];
3975 /* Returns 'true' if one of the 'port's RX queues exists in 'poll_list'
3976 * of given PMD thread. */
3978 dpif_netdev_pmd_polls_port(struct dp_netdev_pmd_thread
*pmd
,
3979 struct dp_netdev_port
*port
)
3980 OVS_EXCLUDED(pmd
->port_mutex
)
3982 struct rxq_poll
*poll
;
3985 ovs_mutex_lock(&pmd
->port_mutex
);
3986 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
3987 if (port
== poll
->rxq
->port
) {
3992 ovs_mutex_unlock(&pmd
->port_mutex
);
3996 /* Updates port configuration from the database. The changes are actually
3997 * applied in dpif_netdev_run(). */
3999 dpif_netdev_port_set_config(struct dpif
*dpif
, odp_port_t port_no
,
4000 const struct smap
*cfg
)
4002 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4003 struct dp_netdev_port
*port
;
4005 const char *affinity_list
= smap_get(cfg
, "pmd-rxq-affinity");
4006 bool emc_enabled
= smap_get_bool(cfg
, "emc-enable", true);
4008 ovs_mutex_lock(&dp
->port_mutex
);
4009 error
= get_port_by_number(dp
, port_no
, &port
);
4014 if (emc_enabled
!= port
->emc_enabled
) {
4015 struct dp_netdev_pmd_thread
*pmd
;
4016 struct ds ds
= DS_EMPTY_INITIALIZER
;
4017 uint32_t cur_min
, insert_prob
;
4019 port
->emc_enabled
= emc_enabled
;
4020 /* Mark for reload all the threads that polls this port and request
4021 * for reconfiguration for the actual reloading of threads. */
4022 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4023 if (dpif_netdev_pmd_polls_port(pmd
, port
)) {
4024 pmd
->need_reload
= true;
4027 dp_netdev_request_reconfigure(dp
);
4029 ds_put_format(&ds
, "%s: EMC has been %s.",
4030 netdev_get_name(port
->netdev
),
4031 (emc_enabled
) ? "enabled" : "disabled");
4033 ds_put_cstr(&ds
, " Current insertion probability is ");
4034 atomic_read_relaxed(&dp
->emc_insert_min
, &cur_min
);
4036 ds_put_cstr(&ds
, "zero.");
4038 insert_prob
= UINT32_MAX
/ cur_min
;
4039 ds_put_format(&ds
, "1/%"PRIu32
" (~%.2f%%).",
4040 insert_prob
, 100 / (float) insert_prob
);
4043 VLOG_INFO("%s", ds_cstr(&ds
));
4047 /* Checking for RXq affinity changes. */
4048 if (!netdev_is_pmd(port
->netdev
)
4049 || nullable_string_is_equal(affinity_list
, port
->rxq_affinity_list
)) {
4053 error
= dpif_netdev_port_set_rxq_affinity(port
, affinity_list
);
4057 free(port
->rxq_affinity_list
);
4058 port
->rxq_affinity_list
= nullable_xstrdup(affinity_list
);
4060 dp_netdev_request_reconfigure(dp
);
4062 ovs_mutex_unlock(&dp
->port_mutex
);
4067 dpif_netdev_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
4068 uint32_t queue_id
, uint32_t *priority
)
4070 *priority
= queue_id
;
4075 /* Creates and returns a new 'struct dp_netdev_actions', whose actions are
4076 * a copy of the 'size' bytes of 'actions' input parameters. */
4077 struct dp_netdev_actions
*
4078 dp_netdev_actions_create(const struct nlattr
*actions
, size_t size
)
4080 struct dp_netdev_actions
*netdev_actions
;
4082 netdev_actions
= xmalloc(sizeof *netdev_actions
+ size
);
4083 memcpy(netdev_actions
->actions
, actions
, size
);
4084 netdev_actions
->size
= size
;
4086 return netdev_actions
;
4089 struct dp_netdev_actions
*
4090 dp_netdev_flow_get_actions(const struct dp_netdev_flow
*flow
)
4092 return ovsrcu_get(struct dp_netdev_actions
*, &flow
->actions
);
4096 dp_netdev_actions_free(struct dp_netdev_actions
*actions
)
4102 dp_netdev_rxq_set_cycles(struct dp_netdev_rxq
*rx
,
4103 enum rxq_cycles_counter_type type
,
4104 unsigned long long cycles
)
4106 atomic_store_relaxed(&rx
->cycles
[type
], cycles
);
4110 dp_netdev_rxq_add_cycles(struct dp_netdev_rxq
*rx
,
4111 enum rxq_cycles_counter_type type
,
4112 unsigned long long cycles
)
4114 non_atomic_ullong_add(&rx
->cycles
[type
], cycles
);
4118 dp_netdev_rxq_get_cycles(struct dp_netdev_rxq
*rx
,
4119 enum rxq_cycles_counter_type type
)
4121 unsigned long long processing_cycles
;
4122 atomic_read_relaxed(&rx
->cycles
[type
], &processing_cycles
);
4123 return processing_cycles
;
4127 dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq
*rx
,
4128 unsigned long long cycles
)
4130 unsigned int idx
= rx
->intrvl_idx
++ % PMD_RXQ_INTERVAL_MAX
;
4131 atomic_store_relaxed(&rx
->cycles_intrvl
[idx
], cycles
);
4135 dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq
*rx
, unsigned idx
)
4137 unsigned long long processing_cycles
;
4138 atomic_read_relaxed(&rx
->cycles_intrvl
[idx
], &processing_cycles
);
4139 return processing_cycles
;
4142 #if ATOMIC_ALWAYS_LOCK_FREE_8B
4144 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread
*pmd
)
4146 bool pmd_perf_enabled
;
4147 atomic_read_relaxed(&pmd
->dp
->pmd_perf_metrics
, &pmd_perf_enabled
);
4148 return pmd_perf_enabled
;
4151 /* If stores and reads of 64-bit integers are not atomic, the full PMD
4152 * performance metrics are not available as locked access to 64 bit
4153 * integers would be prohibitively expensive. */
4155 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread
*pmd OVS_UNUSED
)
4162 dp_netdev_pmd_flush_output_on_port(struct dp_netdev_pmd_thread
*pmd
,
4169 struct cycle_timer timer
;
4171 uint32_t tx_flush_interval
;
4173 cycle_timer_start(&pmd
->perf_stats
, &timer
);
4175 dynamic_txqs
= p
->port
->dynamic_txqs
;
4177 tx_qid
= dpif_netdev_xps_get_tx_qid(pmd
, p
);
4179 tx_qid
= pmd
->static_tx_qid
;
4182 output_cnt
= dp_packet_batch_size(&p
->output_pkts
);
4183 ovs_assert(output_cnt
> 0);
4185 netdev_send(p
->port
->netdev
, tx_qid
, &p
->output_pkts
, dynamic_txqs
);
4186 dp_packet_batch_init(&p
->output_pkts
);
4188 /* Update time of the next flush. */
4189 atomic_read_relaxed(&pmd
->dp
->tx_flush_interval
, &tx_flush_interval
);
4190 p
->flush_time
= pmd
->ctx
.now
+ tx_flush_interval
;
4192 ovs_assert(pmd
->n_output_batches
> 0);
4193 pmd
->n_output_batches
--;
4195 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_SENT_PKTS
, output_cnt
);
4196 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_SENT_BATCHES
, 1);
4198 /* Distribute send cycles evenly among transmitted packets and assign to
4199 * their respective rx queues. */
4200 cycles
= cycle_timer_stop(&pmd
->perf_stats
, &timer
) / output_cnt
;
4201 for (i
= 0; i
< output_cnt
; i
++) {
4202 if (p
->output_pkts_rxqs
[i
]) {
4203 dp_netdev_rxq_add_cycles(p
->output_pkts_rxqs
[i
],
4204 RXQ_CYCLES_PROC_CURR
, cycles
);
4212 dp_netdev_pmd_flush_output_packets(struct dp_netdev_pmd_thread
*pmd
,
4218 if (!pmd
->n_output_batches
) {
4222 HMAP_FOR_EACH (p
, node
, &pmd
->send_port_cache
) {
4223 if (!dp_packet_batch_is_empty(&p
->output_pkts
)
4224 && (force
|| pmd
->ctx
.now
>= p
->flush_time
)) {
4225 output_cnt
+= dp_netdev_pmd_flush_output_on_port(pmd
, p
);
4232 dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread
*pmd
,
4233 struct dp_netdev_rxq
*rxq
,
4236 struct pmd_perf_stats
*s
= &pmd
->perf_stats
;
4237 struct dp_packet_batch batch
;
4238 struct cycle_timer timer
;
4241 int rem_qlen
= 0, *qlen_p
= NULL
;
4244 /* Measure duration for polling and processing rx burst. */
4245 cycle_timer_start(&pmd
->perf_stats
, &timer
);
4247 pmd
->ctx
.last_rxq
= rxq
;
4248 dp_packet_batch_init(&batch
);
4250 /* Fetch the rx queue length only for vhostuser ports. */
4251 if (pmd_perf_metrics_enabled(pmd
) && rxq
->is_vhost
) {
4255 error
= netdev_rxq_recv(rxq
->rx
, &batch
, qlen_p
);
4257 /* At least one packet received. */
4258 *recirc_depth_get() = 0;
4259 pmd_thread_ctx_time_update(pmd
);
4260 batch_cnt
= batch
.count
;
4261 if (pmd_perf_metrics_enabled(pmd
)) {
4262 /* Update batch histogram. */
4263 s
->current
.batches
++;
4264 histogram_add_sample(&s
->pkts_per_batch
, batch_cnt
);
4265 /* Update the maximum vhost rx queue fill level. */
4266 if (rxq
->is_vhost
&& rem_qlen
>= 0) {
4267 uint32_t qfill
= batch_cnt
+ rem_qlen
;
4268 if (qfill
> s
->current
.max_vhost_qfill
) {
4269 s
->current
.max_vhost_qfill
= qfill
;
4273 /* Process packet batch. */
4274 dp_netdev_input(pmd
, &batch
, port_no
);
4276 /* Assign processing cycles to rx queue. */
4277 cycles
= cycle_timer_stop(&pmd
->perf_stats
, &timer
);
4278 dp_netdev_rxq_add_cycles(rxq
, RXQ_CYCLES_PROC_CURR
, cycles
);
4280 dp_netdev_pmd_flush_output_packets(pmd
, false);
4282 /* Discard cycles. */
4283 cycle_timer_stop(&pmd
->perf_stats
, &timer
);
4284 if (error
!= EAGAIN
&& error
!= EOPNOTSUPP
) {
4285 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4287 VLOG_ERR_RL(&rl
, "error receiving data from %s: %s",
4288 netdev_rxq_get_name(rxq
->rx
), ovs_strerror(error
));
4292 pmd
->ctx
.last_rxq
= NULL
;
4297 static struct tx_port
*
4298 tx_port_lookup(const struct hmap
*hmap
, odp_port_t port_no
)
4302 HMAP_FOR_EACH_IN_BUCKET (tx
, node
, hash_port_no(port_no
), hmap
) {
4303 if (tx
->port
->port_no
== port_no
) {
4312 port_reconfigure(struct dp_netdev_port
*port
)
4314 struct netdev
*netdev
= port
->netdev
;
4317 /* Closes the existing 'rxq's. */
4318 for (i
= 0; i
< port
->n_rxq
; i
++) {
4319 netdev_rxq_close(port
->rxqs
[i
].rx
);
4320 port
->rxqs
[i
].rx
= NULL
;
4322 unsigned last_nrxq
= port
->n_rxq
;
4325 /* Allows 'netdev' to apply the pending configuration changes. */
4326 if (netdev_is_reconf_required(netdev
) || port
->need_reconfigure
) {
4327 err
= netdev_reconfigure(netdev
);
4328 if (err
&& (err
!= EOPNOTSUPP
)) {
4329 VLOG_ERR("Failed to set interface %s new configuration",
4330 netdev_get_name(netdev
));
4334 /* If the netdev_reconfigure() above succeeds, reopens the 'rxq's. */
4335 port
->rxqs
= xrealloc(port
->rxqs
,
4336 sizeof *port
->rxqs
* netdev_n_rxq(netdev
));
4337 /* Realloc 'used' counters for tx queues. */
4338 free(port
->txq_used
);
4339 port
->txq_used
= xcalloc(netdev_n_txq(netdev
), sizeof *port
->txq_used
);
4341 for (i
= 0; i
< netdev_n_rxq(netdev
); i
++) {
4342 bool new_queue
= i
>= last_nrxq
;
4344 memset(&port
->rxqs
[i
], 0, sizeof port
->rxqs
[i
]);
4347 port
->rxqs
[i
].port
= port
;
4348 port
->rxqs
[i
].is_vhost
= !strncmp(port
->type
, "dpdkvhost", 9);
4350 err
= netdev_rxq_open(netdev
, &port
->rxqs
[i
].rx
, i
);
4357 /* Parse affinity list to apply configuration for new queues. */
4358 dpif_netdev_port_set_rxq_affinity(port
, port
->rxq_affinity_list
);
4360 /* If reconfiguration was successful mark it as such, so we can use it */
4361 port
->need_reconfigure
= false;
4366 struct rr_numa_list
{
4367 struct hmap numas
; /* Contains 'struct rr_numa' */
4371 struct hmap_node node
;
4375 /* Non isolated pmds on numa node 'numa_id' */
4376 struct dp_netdev_pmd_thread
**pmds
;
4383 static struct rr_numa
*
4384 rr_numa_list_lookup(struct rr_numa_list
*rr
, int numa_id
)
4386 struct rr_numa
*numa
;
4388 HMAP_FOR_EACH_WITH_HASH (numa
, node
, hash_int(numa_id
, 0), &rr
->numas
) {
4389 if (numa
->numa_id
== numa_id
) {
4397 /* Returns the next node in numa list following 'numa' in round-robin fashion.
4398 * Returns first node if 'numa' is a null pointer or the last node in 'rr'.
4399 * Returns NULL if 'rr' numa list is empty. */
4400 static struct rr_numa
*
4401 rr_numa_list_next(struct rr_numa_list
*rr
, const struct rr_numa
*numa
)
4403 struct hmap_node
*node
= NULL
;
4406 node
= hmap_next(&rr
->numas
, &numa
->node
);
4409 node
= hmap_first(&rr
->numas
);
4412 return (node
) ? CONTAINER_OF(node
, struct rr_numa
, node
) : NULL
;
4416 rr_numa_list_populate(struct dp_netdev
*dp
, struct rr_numa_list
*rr
)
4418 struct dp_netdev_pmd_thread
*pmd
;
4419 struct rr_numa
*numa
;
4421 hmap_init(&rr
->numas
);
4423 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4424 if (pmd
->core_id
== NON_PMD_CORE_ID
|| pmd
->isolated
) {
4428 numa
= rr_numa_list_lookup(rr
, pmd
->numa_id
);
4430 numa
= xzalloc(sizeof *numa
);
4431 numa
->numa_id
= pmd
->numa_id
;
4432 hmap_insert(&rr
->numas
, &numa
->node
, hash_int(pmd
->numa_id
, 0));
4435 numa
->pmds
= xrealloc(numa
->pmds
, numa
->n_pmds
* sizeof *numa
->pmds
);
4436 numa
->pmds
[numa
->n_pmds
- 1] = pmd
;
4437 /* At least one pmd so initialise curr_idx and idx_inc. */
4438 numa
->cur_index
= 0;
4439 numa
->idx_inc
= true;
4444 * Returns the next pmd from the numa node.
4446 * If 'updown' is 'true' it will alternate between selecting the next pmd in
4447 * either an up or down walk, switching between up/down when the first or last
4448 * core is reached. e.g. 1,2,3,3,2,1,1,2...
4450 * If 'updown' is 'false' it will select the next pmd wrapping around when last
4451 * core reached. e.g. 1,2,3,1,2,3,1,2...
4453 static struct dp_netdev_pmd_thread
*
4454 rr_numa_get_pmd(struct rr_numa
*numa
, bool updown
)
4456 int numa_idx
= numa
->cur_index
;
4458 if (numa
->idx_inc
== true) {
4459 /* Incrementing through list of pmds. */
4460 if (numa
->cur_index
== numa
->n_pmds
-1) {
4461 /* Reached the last pmd. */
4463 numa
->idx_inc
= false;
4465 numa
->cur_index
= 0;
4471 /* Decrementing through list of pmds. */
4472 if (numa
->cur_index
== 0) {
4473 /* Reached the first pmd. */
4474 numa
->idx_inc
= true;
4479 return numa
->pmds
[numa_idx
];
4483 rr_numa_list_destroy(struct rr_numa_list
*rr
)
4485 struct rr_numa
*numa
;
4487 HMAP_FOR_EACH_POP (numa
, node
, &rr
->numas
) {
4491 hmap_destroy(&rr
->numas
);
4494 /* Sort Rx Queues by the processing cycles they are consuming. */
4496 compare_rxq_cycles(const void *a
, const void *b
)
4498 struct dp_netdev_rxq
*qa
;
4499 struct dp_netdev_rxq
*qb
;
4500 uint64_t cycles_qa
, cycles_qb
;
4502 qa
= *(struct dp_netdev_rxq
**) a
;
4503 qb
= *(struct dp_netdev_rxq
**) b
;
4505 cycles_qa
= dp_netdev_rxq_get_cycles(qa
, RXQ_CYCLES_PROC_HIST
);
4506 cycles_qb
= dp_netdev_rxq_get_cycles(qb
, RXQ_CYCLES_PROC_HIST
);
4508 if (cycles_qa
!= cycles_qb
) {
4509 return (cycles_qa
< cycles_qb
) ? 1 : -1;
4511 /* Cycles are the same so tiebreak on port/queue id.
4512 * Tiebreaking (as opposed to return 0) ensures consistent
4513 * sort results across multiple OS's. */
4514 uint32_t port_qa
= odp_to_u32(qa
->port
->port_no
);
4515 uint32_t port_qb
= odp_to_u32(qb
->port
->port_no
);
4516 if (port_qa
!= port_qb
) {
4517 return port_qa
> port_qb
? 1 : -1;
4519 return netdev_rxq_get_queue_id(qa
->rx
)
4520 - netdev_rxq_get_queue_id(qb
->rx
);
4525 /* Assign pmds to queues. If 'pinned' is true, assign pmds to pinned
4526 * queues and marks the pmds as isolated. Otherwise, assign non isolated
4527 * pmds to unpinned queues.
4529 * The function doesn't touch the pmd threads, it just stores the assignment
4530 * in the 'pmd' member of each rxq. */
4532 rxq_scheduling(struct dp_netdev
*dp
, bool pinned
) OVS_REQUIRES(dp
->port_mutex
)
4534 struct dp_netdev_port
*port
;
4535 struct rr_numa_list rr
;
4536 struct rr_numa
*non_local_numa
= NULL
;
4537 struct dp_netdev_rxq
** rxqs
= NULL
;
4539 struct rr_numa
*numa
= NULL
;
4541 bool assign_cyc
= dp
->pmd_rxq_assign_cyc
;
4543 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4544 if (!netdev_is_pmd(port
->netdev
)) {
4548 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
4549 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
4551 if (pinned
&& q
->core_id
!= OVS_CORE_UNSPEC
) {
4552 struct dp_netdev_pmd_thread
*pmd
;
4554 pmd
= dp_netdev_get_pmd(dp
, q
->core_id
);
4556 VLOG_WARN("There is no PMD thread on core %d. Queue "
4557 "%d on port \'%s\' will not be polled.",
4558 q
->core_id
, qid
, netdev_get_name(port
->netdev
));
4561 pmd
->isolated
= true;
4562 dp_netdev_pmd_unref(pmd
);
4564 } else if (!pinned
&& q
->core_id
== OVS_CORE_UNSPEC
) {
4565 uint64_t cycle_hist
= 0;
4568 rxqs
= xmalloc(sizeof *rxqs
);
4570 rxqs
= xrealloc(rxqs
, sizeof *rxqs
* (n_rxqs
+ 1));
4574 /* Sum the queue intervals and store the cycle history. */
4575 for (unsigned i
= 0; i
< PMD_RXQ_INTERVAL_MAX
; i
++) {
4576 cycle_hist
+= dp_netdev_rxq_get_intrvl_cycles(q
, i
);
4578 dp_netdev_rxq_set_cycles(q
, RXQ_CYCLES_PROC_HIST
,
4581 /* Store the queue. */
4587 if (n_rxqs
> 1 && assign_cyc
) {
4588 /* Sort the queues in order of the processing cycles
4589 * they consumed during their last pmd interval. */
4590 qsort(rxqs
, n_rxqs
, sizeof *rxqs
, compare_rxq_cycles
);
4593 rr_numa_list_populate(dp
, &rr
);
4594 /* Assign the sorted queues to pmds in round robin. */
4595 for (int i
= 0; i
< n_rxqs
; i
++) {
4596 numa_id
= netdev_get_numa_id(rxqs
[i
]->port
->netdev
);
4597 numa
= rr_numa_list_lookup(&rr
, numa_id
);
4599 /* There are no pmds on the queue's local NUMA node.
4600 Round robin on the NUMA nodes that do have pmds. */
4601 non_local_numa
= rr_numa_list_next(&rr
, non_local_numa
);
4602 if (!non_local_numa
) {
4603 VLOG_ERR("There is no available (non-isolated) pmd "
4604 "thread for port \'%s\' queue %d. This queue "
4605 "will not be polled. Is pmd-cpu-mask set to "
4606 "zero? Or are all PMDs isolated to other "
4607 "queues?", netdev_rxq_get_name(rxqs
[i
]->rx
),
4608 netdev_rxq_get_queue_id(rxqs
[i
]->rx
));
4611 rxqs
[i
]->pmd
= rr_numa_get_pmd(non_local_numa
, assign_cyc
);
4612 VLOG_WARN("There's no available (non-isolated) pmd thread "
4613 "on numa node %d. Queue %d on port \'%s\' will "
4614 "be assigned to the pmd on core %d "
4615 "(numa node %d). Expect reduced performance.",
4616 numa_id
, netdev_rxq_get_queue_id(rxqs
[i
]->rx
),
4617 netdev_rxq_get_name(rxqs
[i
]->rx
),
4618 rxqs
[i
]->pmd
->core_id
, rxqs
[i
]->pmd
->numa_id
);
4620 rxqs
[i
]->pmd
= rr_numa_get_pmd(numa
, assign_cyc
);
4622 VLOG_INFO("Core %d on numa node %d assigned port \'%s\' "
4624 "(measured processing cycles %"PRIu64
").",
4625 rxqs
[i
]->pmd
->core_id
, numa_id
,
4626 netdev_rxq_get_name(rxqs
[i
]->rx
),
4627 netdev_rxq_get_queue_id(rxqs
[i
]->rx
),
4628 dp_netdev_rxq_get_cycles(rxqs
[i
],
4629 RXQ_CYCLES_PROC_HIST
));
4631 VLOG_INFO("Core %d on numa node %d assigned port \'%s\' "
4632 "rx queue %d.", rxqs
[i
]->pmd
->core_id
, numa_id
,
4633 netdev_rxq_get_name(rxqs
[i
]->rx
),
4634 netdev_rxq_get_queue_id(rxqs
[i
]->rx
));
4639 rr_numa_list_destroy(&rr
);
4644 reload_affected_pmds(struct dp_netdev
*dp
)
4646 struct dp_netdev_pmd_thread
*pmd
;
4648 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4649 if (pmd
->need_reload
) {
4650 flow_mark_flush(pmd
);
4651 dp_netdev_reload_pmd__(pmd
);
4655 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4656 if (pmd
->need_reload
) {
4657 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
4661 atomic_read_explicit(&pmd
->reload
, &reload
,
4662 memory_order_acquire
);
4665 pmd
->need_reload
= false;
4671 reconfigure_pmd_threads(struct dp_netdev
*dp
)
4672 OVS_REQUIRES(dp
->port_mutex
)
4674 struct dp_netdev_pmd_thread
*pmd
;
4675 struct ovs_numa_dump
*pmd_cores
;
4676 struct ovs_numa_info_core
*core
;
4677 struct hmapx to_delete
= HMAPX_INITIALIZER(&to_delete
);
4678 struct hmapx_node
*node
;
4679 bool changed
= false;
4680 bool need_to_adjust_static_tx_qids
= false;
4682 /* The pmd threads should be started only if there's a pmd port in the
4683 * datapath. If the user didn't provide any "pmd-cpu-mask", we start
4684 * NR_PMD_THREADS per numa node. */
4685 if (!has_pmd_port(dp
)) {
4686 pmd_cores
= ovs_numa_dump_n_cores_per_numa(0);
4687 } else if (dp
->pmd_cmask
&& dp
->pmd_cmask
[0]) {
4688 pmd_cores
= ovs_numa_dump_cores_with_cmask(dp
->pmd_cmask
);
4690 pmd_cores
= ovs_numa_dump_n_cores_per_numa(NR_PMD_THREADS
);
4693 /* We need to adjust 'static_tx_qid's only if we're reducing number of
4694 * PMD threads. Otherwise, new threads will allocate all the freed ids. */
4695 if (ovs_numa_dump_count(pmd_cores
) < cmap_count(&dp
->poll_threads
) - 1) {
4696 /* Adjustment is required to keep 'static_tx_qid's sequential and
4697 * avoid possible issues, for example, imbalanced tx queue usage
4698 * and unnecessary locking caused by remapping on netdev level. */
4699 need_to_adjust_static_tx_qids
= true;
4702 /* Check for unwanted pmd threads */
4703 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4704 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
4707 if (!ovs_numa_dump_contains_core(pmd_cores
, pmd
->numa_id
,
4709 hmapx_add(&to_delete
, pmd
);
4710 } else if (need_to_adjust_static_tx_qids
) {
4711 pmd
->need_reload
= true;
4715 HMAPX_FOR_EACH (node
, &to_delete
) {
4716 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
4717 VLOG_INFO("PMD thread on numa_id: %d, core id: %2d destroyed.",
4718 pmd
->numa_id
, pmd
->core_id
);
4719 dp_netdev_del_pmd(dp
, pmd
);
4721 changed
= !hmapx_is_empty(&to_delete
);
4722 hmapx_destroy(&to_delete
);
4724 if (need_to_adjust_static_tx_qids
) {
4725 /* 'static_tx_qid's are not sequential now.
4726 * Reload remaining threads to fix this. */
4727 reload_affected_pmds(dp
);
4730 /* Check for required new pmd threads */
4731 FOR_EACH_CORE_ON_DUMP(core
, pmd_cores
) {
4732 pmd
= dp_netdev_get_pmd(dp
, core
->core_id
);
4734 pmd
= xzalloc(sizeof *pmd
);
4735 dp_netdev_configure_pmd(pmd
, dp
, core
->core_id
, core
->numa_id
);
4736 pmd
->thread
= ovs_thread_create("pmd", pmd_thread_main
, pmd
);
4737 VLOG_INFO("PMD thread on numa_id: %d, core id: %2d created.",
4738 pmd
->numa_id
, pmd
->core_id
);
4741 dp_netdev_pmd_unref(pmd
);
4746 struct ovs_numa_info_numa
*numa
;
4748 /* Log the number of pmd threads per numa node. */
4749 FOR_EACH_NUMA_ON_DUMP (numa
, pmd_cores
) {
4750 VLOG_INFO("There are %"PRIuSIZE
" pmd threads on numa node %d",
4751 numa
->n_cores
, numa
->numa_id
);
4755 ovs_numa_dump_destroy(pmd_cores
);
4759 pmd_remove_stale_ports(struct dp_netdev
*dp
,
4760 struct dp_netdev_pmd_thread
*pmd
)
4761 OVS_EXCLUDED(pmd
->port_mutex
)
4762 OVS_REQUIRES(dp
->port_mutex
)
4764 struct rxq_poll
*poll
, *poll_next
;
4765 struct tx_port
*tx
, *tx_next
;
4767 ovs_mutex_lock(&pmd
->port_mutex
);
4768 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
4769 struct dp_netdev_port
*port
= poll
->rxq
->port
;
4771 if (port
->need_reconfigure
4772 || !hmap_contains(&dp
->ports
, &port
->node
)) {
4773 dp_netdev_del_rxq_from_pmd(pmd
, poll
);
4776 HMAP_FOR_EACH_SAFE (tx
, tx_next
, node
, &pmd
->tx_ports
) {
4777 struct dp_netdev_port
*port
= tx
->port
;
4779 if (port
->need_reconfigure
4780 || !hmap_contains(&dp
->ports
, &port
->node
)) {
4781 dp_netdev_del_port_tx_from_pmd(pmd
, tx
);
4784 ovs_mutex_unlock(&pmd
->port_mutex
);
4787 /* Must be called each time a port is added/removed or the cmask changes.
4788 * This creates and destroys pmd threads, reconfigures ports, opens their
4789 * rxqs and assigns all rxqs/txqs to pmd threads. */
4791 reconfigure_datapath(struct dp_netdev
*dp
)
4792 OVS_REQUIRES(dp
->port_mutex
)
4794 struct dp_netdev_pmd_thread
*pmd
;
4795 struct dp_netdev_port
*port
;
4798 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
4800 /* Step 1: Adjust the pmd threads based on the datapath ports, the cores
4801 * on the system and the user configuration. */
4802 reconfigure_pmd_threads(dp
);
4804 wanted_txqs
= cmap_count(&dp
->poll_threads
);
4806 /* The number of pmd threads might have changed, or a port can be new:
4807 * adjust the txqs. */
4808 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4809 netdev_set_tx_multiq(port
->netdev
, wanted_txqs
);
4812 /* Step 2: Remove from the pmd threads ports that have been removed or
4813 * need reconfiguration. */
4815 /* Check for all the ports that need reconfiguration. We cache this in
4816 * 'port->need_reconfigure', because netdev_is_reconf_required() can
4817 * change at any time. */
4818 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4819 if (netdev_is_reconf_required(port
->netdev
)) {
4820 port
->need_reconfigure
= true;
4824 /* Remove from the pmd threads all the ports that have been deleted or
4825 * need reconfiguration. */
4826 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4827 pmd_remove_stale_ports(dp
, pmd
);
4830 /* Reload affected pmd threads. We must wait for the pmd threads before
4831 * reconfiguring the ports, because a port cannot be reconfigured while
4832 * it's being used. */
4833 reload_affected_pmds(dp
);
4835 /* Step 3: Reconfigure ports. */
4837 /* We only reconfigure the ports that we determined above, because they're
4838 * not being used by any pmd thread at the moment. If a port fails to
4839 * reconfigure we remove it from the datapath. */
4840 struct dp_netdev_port
*next_port
;
4841 HMAP_FOR_EACH_SAFE (port
, next_port
, node
, &dp
->ports
) {
4844 if (!port
->need_reconfigure
) {
4848 err
= port_reconfigure(port
);
4850 hmap_remove(&dp
->ports
, &port
->node
);
4851 seq_change(dp
->port_seq
);
4854 port
->dynamic_txqs
= netdev_n_txq(port
->netdev
) < wanted_txqs
;
4858 /* Step 4: Compute new rxq scheduling. We don't touch the pmd threads
4859 * for now, we just update the 'pmd' pointer in each rxq to point to the
4860 * wanted thread according to the scheduling policy. */
4862 /* Reset all the pmd threads to non isolated. */
4863 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4864 pmd
->isolated
= false;
4867 /* Reset all the queues to unassigned */
4868 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4869 for (int i
= 0; i
< port
->n_rxq
; i
++) {
4870 port
->rxqs
[i
].pmd
= NULL
;
4874 /* Add pinned queues and mark pmd threads isolated. */
4875 rxq_scheduling(dp
, true);
4877 /* Add non-pinned queues. */
4878 rxq_scheduling(dp
, false);
4880 /* Step 5: Remove queues not compliant with new scheduling. */
4881 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4882 struct rxq_poll
*poll
, *poll_next
;
4884 ovs_mutex_lock(&pmd
->port_mutex
);
4885 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
4886 if (poll
->rxq
->pmd
!= pmd
) {
4887 dp_netdev_del_rxq_from_pmd(pmd
, poll
);
4890 ovs_mutex_unlock(&pmd
->port_mutex
);
4893 /* Reload affected pmd threads. We must wait for the pmd threads to remove
4894 * the old queues before readding them, otherwise a queue can be polled by
4895 * two threads at the same time. */
4896 reload_affected_pmds(dp
);
4898 /* Step 6: Add queues from scheduling, if they're not there already. */
4899 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4900 if (!netdev_is_pmd(port
->netdev
)) {
4904 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
4905 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
4908 ovs_mutex_lock(&q
->pmd
->port_mutex
);
4909 dp_netdev_add_rxq_to_pmd(q
->pmd
, q
);
4910 ovs_mutex_unlock(&q
->pmd
->port_mutex
);
4915 /* Add every port to the tx cache of every pmd thread, if it's not
4916 * there already and if this pmd has at least one rxq to poll. */
4917 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4918 ovs_mutex_lock(&pmd
->port_mutex
);
4919 if (hmap_count(&pmd
->poll_list
) || pmd
->core_id
== NON_PMD_CORE_ID
) {
4920 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4921 dp_netdev_add_port_tx_to_pmd(pmd
, port
);
4924 ovs_mutex_unlock(&pmd
->port_mutex
);
4927 /* Reload affected pmd threads. */
4928 reload_affected_pmds(dp
);
4930 /* Check if PMD Auto LB is to be enabled */
4931 set_pmd_auto_lb(dp
);
4934 /* Returns true if one of the netdevs in 'dp' requires a reconfiguration */
4936 ports_require_restart(const struct dp_netdev
*dp
)
4937 OVS_REQUIRES(dp
->port_mutex
)
4939 struct dp_netdev_port
*port
;
4941 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4942 if (netdev_is_reconf_required(port
->netdev
)) {
4950 /* Calculates variance in the values stored in array 'a'. 'n' is the number
4951 * of elements in array to be considered for calculating vairance.
4952 * Usage example: data array 'a' contains the processing load of each pmd and
4953 * 'n' is the number of PMDs. It returns the variance in processing load of
4956 variance(uint64_t a
[], int n
)
4958 /* Compute mean (average of elements). */
4961 uint64_t sqDiff
= 0;
4967 for (int i
= 0; i
< n
; i
++) {
4974 /* Compute sum squared differences with mean. */
4975 for (int i
= 0; i
< n
; i
++) {
4976 sqDiff
+= (a
[i
] - mean
)*(a
[i
] - mean
);
4979 return (sqDiff
? (sqDiff
/ n
) : 0);
4983 /* Returns the variance in the PMDs usage as part of dry run of rxqs
4984 * assignment to PMDs. */
4986 get_dry_run_variance(struct dp_netdev
*dp
, uint32_t *core_list
,
4987 uint32_t num_pmds
, uint64_t *predicted_variance
)
4988 OVS_REQUIRES(dp
->port_mutex
)
4990 struct dp_netdev_port
*port
;
4991 struct dp_netdev_pmd_thread
*pmd
;
4992 struct dp_netdev_rxq
**rxqs
= NULL
;
4993 struct rr_numa
*numa
= NULL
;
4994 struct rr_numa_list rr
;
4997 uint64_t *pmd_usage
;
4999 if (!predicted_variance
) {
5003 pmd_usage
= xcalloc(num_pmds
, sizeof(uint64_t));
5005 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
5006 if (!netdev_is_pmd(port
->netdev
)) {
5010 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
5011 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
5012 uint64_t cycle_hist
= 0;
5014 if (q
->pmd
->isolated
) {
5019 rxqs
= xmalloc(sizeof *rxqs
);
5021 rxqs
= xrealloc(rxqs
, sizeof *rxqs
* (n_rxqs
+ 1));
5024 /* Sum the queue intervals and store the cycle history. */
5025 for (unsigned i
= 0; i
< PMD_RXQ_INTERVAL_MAX
; i
++) {
5026 cycle_hist
+= dp_netdev_rxq_get_intrvl_cycles(q
, i
);
5028 dp_netdev_rxq_set_cycles(q
, RXQ_CYCLES_PROC_HIST
,
5030 /* Store the queue. */
5035 /* Sort the queues in order of the processing cycles
5036 * they consumed during their last pmd interval. */
5037 qsort(rxqs
, n_rxqs
, sizeof *rxqs
, compare_rxq_cycles
);
5039 rr_numa_list_populate(dp
, &rr
);
5041 for (int i
= 0; i
< n_rxqs
; i
++) {
5042 int numa_id
= netdev_get_numa_id(rxqs
[i
]->port
->netdev
);
5043 numa
= rr_numa_list_lookup(&rr
, numa_id
);
5045 /* Abort if cross NUMA polling. */
5046 VLOG_DBG("PMD auto lb dry run."
5047 " Aborting due to cross-numa polling.");
5051 pmd
= rr_numa_get_pmd(numa
, true);
5052 VLOG_DBG("PMD auto lb dry run. Predicted: Core %d on numa node %d "
5053 "to be assigned port \'%s\' rx queue %d "
5054 "(measured processing cycles %"PRIu64
").",
5055 pmd
->core_id
, numa_id
,
5056 netdev_rxq_get_name(rxqs
[i
]->rx
),
5057 netdev_rxq_get_queue_id(rxqs
[i
]->rx
),
5058 dp_netdev_rxq_get_cycles(rxqs
[i
], RXQ_CYCLES_PROC_HIST
));
5060 for (int id
= 0; id
< num_pmds
; id
++) {
5061 if (pmd
->core_id
== core_list
[id
]) {
5062 /* Add the processing cycles of rxq to pmd polling it. */
5063 pmd_usage
[id
] += dp_netdev_rxq_get_cycles(rxqs
[i
],
5064 RXQ_CYCLES_PROC_HIST
);
5069 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5070 uint64_t total_cycles
= 0;
5072 if ((pmd
->core_id
== NON_PMD_CORE_ID
) || pmd
->isolated
) {
5076 /* Get the total pmd cycles for an interval. */
5077 atomic_read_relaxed(&pmd
->intrvl_cycles
, &total_cycles
);
5078 /* Estimate the cycles to cover all intervals. */
5079 total_cycles
*= PMD_RXQ_INTERVAL_MAX
;
5080 for (int id
= 0; id
< num_pmds
; id
++) {
5081 if (pmd
->core_id
== core_list
[id
]) {
5082 if (pmd_usage
[id
]) {
5083 pmd_usage
[id
] = (pmd_usage
[id
] * 100) / total_cycles
;
5085 VLOG_DBG("PMD auto lb dry run. Predicted: Core %d, "
5086 "usage %"PRIu64
"", pmd
->core_id
, pmd_usage
[id
]);
5090 *predicted_variance
= variance(pmd_usage
, num_pmds
);
5094 rr_numa_list_destroy(&rr
);
5100 /* Does the dry run of Rxq assignment to PMDs and returns true if it gives
5101 * better distribution of load on PMDs. */
5103 pmd_rebalance_dry_run(struct dp_netdev
*dp
)
5104 OVS_REQUIRES(dp
->port_mutex
)
5106 struct dp_netdev_pmd_thread
*pmd
;
5107 uint64_t *curr_pmd_usage
;
5109 uint64_t curr_variance
;
5110 uint64_t new_variance
;
5111 uint64_t improvement
= 0;
5113 uint32_t *pmd_corelist
;
5114 struct rxq_poll
*poll
;
5117 num_pmds
= cmap_count(&dp
->poll_threads
);
5120 curr_pmd_usage
= xcalloc(num_pmds
, sizeof(uint64_t));
5121 pmd_corelist
= xcalloc(num_pmds
, sizeof(uint32_t));
5127 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5128 uint64_t total_cycles
= 0;
5129 uint64_t total_proc
= 0;
5131 if ((pmd
->core_id
== NON_PMD_CORE_ID
) || pmd
->isolated
) {
5135 /* Get the total pmd cycles for an interval. */
5136 atomic_read_relaxed(&pmd
->intrvl_cycles
, &total_cycles
);
5137 /* Estimate the cycles to cover all intervals. */
5138 total_cycles
*= PMD_RXQ_INTERVAL_MAX
;
5140 ovs_mutex_lock(&pmd
->port_mutex
);
5141 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
5142 for (unsigned i
= 0; i
< PMD_RXQ_INTERVAL_MAX
; i
++) {
5143 total_proc
+= dp_netdev_rxq_get_intrvl_cycles(poll
->rxq
, i
);
5146 ovs_mutex_unlock(&pmd
->port_mutex
);
5149 curr_pmd_usage
[num_pmds
] = (total_proc
* 100) / total_cycles
;
5152 VLOG_DBG("PMD auto lb dry run. Current: Core %d, usage %"PRIu64
"",
5153 pmd
->core_id
, curr_pmd_usage
[num_pmds
]);
5155 if (atomic_count_get(&pmd
->pmd_overloaded
)) {
5156 atomic_count_set(&pmd
->pmd_overloaded
, 0);
5159 pmd_corelist
[num_pmds
] = pmd
->core_id
;
5163 curr_variance
= variance(curr_pmd_usage
, num_pmds
);
5164 ret
= get_dry_run_variance(dp
, pmd_corelist
, num_pmds
, &new_variance
);
5167 VLOG_DBG("PMD auto lb dry run. Current PMD variance: %"PRIu64
","
5168 " Predicted PMD variance: %"PRIu64
"",
5169 curr_variance
, new_variance
);
5171 if (new_variance
< curr_variance
) {
5173 ((curr_variance
- new_variance
) * 100) / curr_variance
;
5175 if (improvement
< ALB_ACCEPTABLE_IMPROVEMENT
) {
5180 free(curr_pmd_usage
);
5186 /* Return true if needs to revalidate datapath flows. */
5188 dpif_netdev_run(struct dpif
*dpif
)
5190 struct dp_netdev_port
*port
;
5191 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5192 struct dp_netdev_pmd_thread
*non_pmd
;
5193 uint64_t new_tnl_seq
;
5194 bool need_to_flush
= true;
5195 bool pmd_rebalance
= false;
5196 long long int now
= time_msec();
5197 struct dp_netdev_pmd_thread
*pmd
;
5199 ovs_mutex_lock(&dp
->port_mutex
);
5200 non_pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
5202 ovs_mutex_lock(&dp
->non_pmd_mutex
);
5203 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
5204 if (!netdev_is_pmd(port
->netdev
)) {
5207 if (port
->emc_enabled
) {
5208 atomic_read_relaxed(&dp
->emc_insert_min
,
5209 &non_pmd
->ctx
.emc_insert_min
);
5211 non_pmd
->ctx
.emc_insert_min
= 0;
5214 for (i
= 0; i
< port
->n_rxq
; i
++) {
5216 if (!netdev_rxq_enabled(port
->rxqs
[i
].rx
)) {
5220 if (dp_netdev_process_rxq_port(non_pmd
,
5223 need_to_flush
= false;
5228 if (need_to_flush
) {
5229 /* We didn't receive anything in the process loop.
5230 * Check if we need to send something.
5231 * There was no time updates on current iteration. */
5232 pmd_thread_ctx_time_update(non_pmd
);
5233 dp_netdev_pmd_flush_output_packets(non_pmd
, false);
5236 dpif_netdev_xps_revalidate_pmd(non_pmd
, false);
5237 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
5239 dp_netdev_pmd_unref(non_pmd
);
5242 struct pmd_auto_lb
*pmd_alb
= &dp
->pmd_alb
;
5243 if (pmd_alb
->is_enabled
) {
5244 if (!pmd_alb
->rebalance_poll_timer
) {
5245 pmd_alb
->rebalance_poll_timer
= now
;
5246 } else if ((pmd_alb
->rebalance_poll_timer
+
5247 pmd_alb
->rebalance_intvl
) < now
) {
5248 pmd_alb
->rebalance_poll_timer
= now
;
5249 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5250 if (atomic_count_get(&pmd
->pmd_overloaded
) >=
5251 PMD_RXQ_INTERVAL_MAX
) {
5252 pmd_rebalance
= true;
5257 if (pmd_rebalance
&&
5258 !dp_netdev_is_reconf_required(dp
) &&
5259 !ports_require_restart(dp
) &&
5260 pmd_rebalance_dry_run(dp
)) {
5261 VLOG_INFO("PMD auto lb dry run."
5262 " requesting datapath reconfigure.");
5263 dp_netdev_request_reconfigure(dp
);
5268 if (dp_netdev_is_reconf_required(dp
) || ports_require_restart(dp
)) {
5269 reconfigure_datapath(dp
);
5271 ovs_mutex_unlock(&dp
->port_mutex
);
5273 tnl_neigh_cache_run();
5275 new_tnl_seq
= seq_read(tnl_conf_seq
);
5277 if (dp
->last_tnl_conf_seq
!= new_tnl_seq
) {
5278 dp
->last_tnl_conf_seq
= new_tnl_seq
;
5285 dpif_netdev_wait(struct dpif
*dpif
)
5287 struct dp_netdev_port
*port
;
5288 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5290 ovs_mutex_lock(&dp_netdev_mutex
);
5291 ovs_mutex_lock(&dp
->port_mutex
);
5292 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
5293 netdev_wait_reconf_required(port
->netdev
);
5294 if (!netdev_is_pmd(port
->netdev
)) {
5297 for (i
= 0; i
< port
->n_rxq
; i
++) {
5298 netdev_rxq_wait(port
->rxqs
[i
].rx
);
5302 ovs_mutex_unlock(&dp
->port_mutex
);
5303 ovs_mutex_unlock(&dp_netdev_mutex
);
5304 seq_wait(tnl_conf_seq
, dp
->last_tnl_conf_seq
);
5308 pmd_free_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
5310 struct tx_port
*tx_port_cached
;
5312 /* Flush all the queued packets. */
5313 dp_netdev_pmd_flush_output_packets(pmd
, true);
5314 /* Free all used tx queue ids. */
5315 dpif_netdev_xps_revalidate_pmd(pmd
, true);
5317 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->tnl_port_cache
) {
5318 free(tx_port_cached
);
5320 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->send_port_cache
) {
5321 free(tx_port_cached
);
5325 /* Copies ports from 'pmd->tx_ports' (shared with the main thread) to
5326 * thread-local copies. Copy to 'pmd->tnl_port_cache' if it is a tunnel
5327 * device, otherwise to 'pmd->send_port_cache' if the port has at least
5330 pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
5331 OVS_REQUIRES(pmd
->port_mutex
)
5333 struct tx_port
*tx_port
, *tx_port_cached
;
5335 pmd_free_cached_ports(pmd
);
5336 hmap_shrink(&pmd
->send_port_cache
);
5337 hmap_shrink(&pmd
->tnl_port_cache
);
5339 HMAP_FOR_EACH (tx_port
, node
, &pmd
->tx_ports
) {
5340 if (netdev_has_tunnel_push_pop(tx_port
->port
->netdev
)) {
5341 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
5342 hmap_insert(&pmd
->tnl_port_cache
, &tx_port_cached
->node
,
5343 hash_port_no(tx_port_cached
->port
->port_no
));
5346 if (netdev_n_txq(tx_port
->port
->netdev
)) {
5347 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
5348 hmap_insert(&pmd
->send_port_cache
, &tx_port_cached
->node
,
5349 hash_port_no(tx_port_cached
->port
->port_no
));
5355 pmd_alloc_static_tx_qid(struct dp_netdev_pmd_thread
*pmd
)
5357 ovs_mutex_lock(&pmd
->dp
->tx_qid_pool_mutex
);
5358 if (!id_pool_alloc_id(pmd
->dp
->tx_qid_pool
, &pmd
->static_tx_qid
)) {
5359 VLOG_ABORT("static_tx_qid allocation failed for PMD on core %2d"
5360 ", numa_id %d.", pmd
->core_id
, pmd
->numa_id
);
5362 ovs_mutex_unlock(&pmd
->dp
->tx_qid_pool_mutex
);
5364 VLOG_DBG("static_tx_qid = %d allocated for PMD thread on core %2d"
5365 ", numa_id %d.", pmd
->static_tx_qid
, pmd
->core_id
, pmd
->numa_id
);
5369 pmd_free_static_tx_qid(struct dp_netdev_pmd_thread
*pmd
)
5371 ovs_mutex_lock(&pmd
->dp
->tx_qid_pool_mutex
);
5372 id_pool_free_id(pmd
->dp
->tx_qid_pool
, pmd
->static_tx_qid
);
5373 ovs_mutex_unlock(&pmd
->dp
->tx_qid_pool_mutex
);
5377 pmd_load_queues_and_ports(struct dp_netdev_pmd_thread
*pmd
,
5378 struct polled_queue
**ppoll_list
)
5380 struct polled_queue
*poll_list
= *ppoll_list
;
5381 struct rxq_poll
*poll
;
5384 ovs_mutex_lock(&pmd
->port_mutex
);
5385 poll_list
= xrealloc(poll_list
, hmap_count(&pmd
->poll_list
)
5386 * sizeof *poll_list
);
5389 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
5390 poll_list
[i
].rxq
= poll
->rxq
;
5391 poll_list
[i
].port_no
= poll
->rxq
->port
->port_no
;
5392 poll_list
[i
].emc_enabled
= poll
->rxq
->port
->emc_enabled
;
5393 poll_list
[i
].rxq_enabled
= netdev_rxq_enabled(poll
->rxq
->rx
);
5394 poll_list
[i
].change_seq
=
5395 netdev_get_change_seq(poll
->rxq
->port
->netdev
);
5399 pmd_load_cached_ports(pmd
);
5401 ovs_mutex_unlock(&pmd
->port_mutex
);
5403 *ppoll_list
= poll_list
;
5408 pmd_thread_main(void *f_
)
5410 struct dp_netdev_pmd_thread
*pmd
= f_
;
5411 struct pmd_perf_stats
*s
= &pmd
->perf_stats
;
5412 unsigned int lc
= 0;
5413 struct polled_queue
*poll_list
;
5417 int process_packets
= 0;
5421 /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */
5422 ovsthread_setspecific(pmd
->dp
->per_pmd_key
, pmd
);
5423 ovs_numa_thread_setaffinity_core(pmd
->core_id
);
5424 dpdk_set_lcore_id(pmd
->core_id
);
5425 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
5426 dfc_cache_init(&pmd
->flow_cache
);
5428 pmd_alloc_static_tx_qid(pmd
);
5430 atomic_count_init(&pmd
->pmd_overloaded
, 0);
5432 /* List port/core affinity */
5433 for (i
= 0; i
< poll_cnt
; i
++) {
5434 VLOG_DBG("Core %d processing port \'%s\' with queue-id %d\n",
5435 pmd
->core_id
, netdev_rxq_get_name(poll_list
[i
].rxq
->rx
),
5436 netdev_rxq_get_queue_id(poll_list
[i
].rxq
->rx
));
5437 /* Reset the rxq current cycles counter. */
5438 dp_netdev_rxq_set_cycles(poll_list
[i
].rxq
, RXQ_CYCLES_PROC_CURR
, 0);
5442 while (seq_read(pmd
->reload_seq
) == pmd
->last_reload_seq
) {
5443 seq_wait(pmd
->reload_seq
, pmd
->last_reload_seq
);
5449 pmd
->intrvl_tsc_prev
= 0;
5450 atomic_store_relaxed(&pmd
->intrvl_cycles
, 0);
5451 cycles_counter_update(s
);
5452 /* Protect pmd stats from external clearing while polling. */
5453 ovs_mutex_lock(&pmd
->perf_stats
.stats_mutex
);
5455 uint64_t rx_packets
= 0, tx_packets
= 0;
5457 pmd_perf_start_iteration(s
);
5459 for (i
= 0; i
< poll_cnt
; i
++) {
5461 if (!poll_list
[i
].rxq_enabled
) {
5465 if (poll_list
[i
].emc_enabled
) {
5466 atomic_read_relaxed(&pmd
->dp
->emc_insert_min
,
5467 &pmd
->ctx
.emc_insert_min
);
5469 pmd
->ctx
.emc_insert_min
= 0;
5473 dp_netdev_process_rxq_port(pmd
, poll_list
[i
].rxq
,
5474 poll_list
[i
].port_no
);
5475 rx_packets
+= process_packets
;
5479 /* We didn't receive anything in the process loop.
5480 * Check if we need to send something.
5481 * There was no time updates on current iteration. */
5482 pmd_thread_ctx_time_update(pmd
);
5483 tx_packets
= dp_netdev_pmd_flush_output_packets(pmd
, false);
5491 coverage_try_clear();
5492 dp_netdev_pmd_try_optimize(pmd
, poll_list
, poll_cnt
);
5493 if (!ovsrcu_try_quiesce()) {
5494 emc_cache_slow_sweep(&((pmd
->flow_cache
).emc_cache
));
5497 atomic_read_explicit(&pmd
->reload
, &reload
, memory_order_acquire
);
5502 for (i
= 0; i
< poll_cnt
; i
++) {
5503 uint64_t current_seq
=
5504 netdev_get_change_seq(poll_list
[i
].rxq
->port
->netdev
);
5505 if (poll_list
[i
].change_seq
!= current_seq
) {
5506 poll_list
[i
].change_seq
= current_seq
;
5507 poll_list
[i
].rxq_enabled
=
5508 netdev_rxq_enabled(poll_list
[i
].rxq
->rx
);
5512 pmd_perf_end_iteration(s
, rx_packets
, tx_packets
,
5513 pmd_perf_metrics_enabled(pmd
));
5515 ovs_mutex_unlock(&pmd
->perf_stats
.stats_mutex
);
5517 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
5518 atomic_read_relaxed(&pmd
->exit
, &exiting
);
5519 /* Signal here to make sure the pmd finishes
5520 * reloading the updated configuration. */
5521 dp_netdev_pmd_reload_done(pmd
);
5523 pmd_free_static_tx_qid(pmd
);
5529 dfc_cache_uninit(&pmd
->flow_cache
);
5531 pmd_free_cached_ports(pmd
);
5536 dp_netdev_disable_upcall(struct dp_netdev
*dp
)
5537 OVS_ACQUIRES(dp
->upcall_rwlock
)
5539 fat_rwlock_wrlock(&dp
->upcall_rwlock
);
5545 dpif_netdev_meter_get_features(const struct dpif
* dpif OVS_UNUSED
,
5546 struct ofputil_meter_features
*features
)
5548 features
->max_meters
= MAX_METERS
;
5549 features
->band_types
= DP_SUPPORTED_METER_BAND_TYPES
;
5550 features
->capabilities
= DP_SUPPORTED_METER_FLAGS_MASK
;
5551 features
->max_bands
= MAX_BANDS
;
5552 features
->max_color
= 0;
5555 /* Applies the meter identified by 'meter_id' to 'packets_'. Packets
5556 * that exceed a band are dropped in-place. */
5558 dp_netdev_run_meter(struct dp_netdev
*dp
, struct dp_packet_batch
*packets_
,
5559 uint32_t meter_id
, long long int now
)
5561 struct dp_meter
*meter
;
5562 struct dp_meter_band
*band
;
5563 struct dp_packet
*packet
;
5564 long long int long_delta_t
; /* msec */
5565 uint32_t delta_t
; /* msec */
5566 const size_t cnt
= dp_packet_batch_size(packets_
);
5567 uint32_t bytes
, volume
;
5568 int exceeded_band
[NETDEV_MAX_BURST
];
5569 uint32_t exceeded_rate
[NETDEV_MAX_BURST
];
5570 int exceeded_pkt
= cnt
; /* First packet that exceeded a band rate. */
5572 if (meter_id
>= MAX_METERS
) {
5576 meter_lock(dp
, meter_id
);
5577 meter
= dp
->meters
[meter_id
];
5582 /* Initialize as negative values. */
5583 memset(exceeded_band
, 0xff, cnt
* sizeof *exceeded_band
);
5584 /* Initialize as zeroes. */
5585 memset(exceeded_rate
, 0, cnt
* sizeof *exceeded_rate
);
5587 /* All packets will hit the meter at the same time. */
5588 long_delta_t
= now
/ 1000 - meter
->used
/ 1000; /* msec */
5590 /* Make sure delta_t will not be too large, so that bucket will not
5591 * wrap around below. */
5592 delta_t
= (long_delta_t
> (long long int)meter
->max_delta_t
)
5593 ? meter
->max_delta_t
: (uint32_t)long_delta_t
;
5595 /* Update meter stats. */
5597 meter
->packet_count
+= cnt
;
5599 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
5600 bytes
+= dp_packet_size(packet
);
5602 meter
->byte_count
+= bytes
;
5604 /* Meters can operate in terms of packets per second or kilobits per
5606 if (meter
->flags
& OFPMF13_PKTPS
) {
5607 /* Rate in packets/second, bucket 1/1000 packets. */
5608 /* msec * packets/sec = 1/1000 packets. */
5609 volume
= cnt
* 1000; /* Take 'cnt' packets from the bucket. */
5611 /* Rate in kbps, bucket in bits. */
5612 /* msec * kbps = bits */
5616 /* Update all bands and find the one hit with the highest rate for each
5617 * packet (if any). */
5618 for (int m
= 0; m
< meter
->n_bands
; ++m
) {
5619 band
= &meter
->bands
[m
];
5621 /* Update band's bucket. */
5622 band
->bucket
+= delta_t
* band
->up
.rate
;
5623 if (band
->bucket
> band
->up
.burst_size
) {
5624 band
->bucket
= band
->up
.burst_size
;
5627 /* Drain the bucket for all the packets, if possible. */
5628 if (band
->bucket
>= volume
) {
5629 band
->bucket
-= volume
;
5631 int band_exceeded_pkt
;
5633 /* Band limit hit, must process packet-by-packet. */
5634 if (meter
->flags
& OFPMF13_PKTPS
) {
5635 band_exceeded_pkt
= band
->bucket
/ 1000;
5636 band
->bucket
%= 1000; /* Remainder stays in bucket. */
5638 /* Update the exceeding band for each exceeding packet.
5639 * (Only one band will be fired by a packet, and that
5640 * can be different for each packet.) */
5641 for (int i
= band_exceeded_pkt
; i
< cnt
; i
++) {
5642 if (band
->up
.rate
> exceeded_rate
[i
]) {
5643 exceeded_rate
[i
] = band
->up
.rate
;
5644 exceeded_band
[i
] = m
;
5648 /* Packet sizes differ, must process one-by-one. */
5649 band_exceeded_pkt
= cnt
;
5650 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
5651 uint32_t bits
= dp_packet_size(packet
) * 8;
5653 if (band
->bucket
>= bits
) {
5654 band
->bucket
-= bits
;
5656 if (i
< band_exceeded_pkt
) {
5657 band_exceeded_pkt
= i
;
5659 /* Update the exceeding band for the exceeding packet.
5660 * (Only one band will be fired by a packet, and that
5661 * can be different for each packet.) */
5662 if (band
->up
.rate
> exceeded_rate
[i
]) {
5663 exceeded_rate
[i
] = band
->up
.rate
;
5664 exceeded_band
[i
] = m
;
5669 /* Remember the first exceeding packet. */
5670 if (exceeded_pkt
> band_exceeded_pkt
) {
5671 exceeded_pkt
= band_exceeded_pkt
;
5676 /* Fire the highest rate band exceeded by each packet, and drop
5677 * packets if needed. */
5679 DP_PACKET_BATCH_REFILL_FOR_EACH (j
, cnt
, packet
, packets_
) {
5680 if (exceeded_band
[j
] >= 0) {
5681 /* Meter drop packet. */
5682 band
= &meter
->bands
[exceeded_band
[j
]];
5683 band
->packet_count
+= 1;
5684 band
->byte_count
+= dp_packet_size(packet
);
5686 dp_packet_delete(packet
);
5688 /* Meter accepts packet. */
5689 dp_packet_batch_refill(packets_
, packet
, j
);
5693 meter_unlock(dp
, meter_id
);
5696 /* Meter set/get/del processing is still single-threaded. */
5698 dpif_netdev_meter_set(struct dpif
*dpif
, ofproto_meter_id meter_id
,
5699 struct ofputil_meter_config
*config
)
5701 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5702 uint32_t mid
= meter_id
.uint32
;
5703 struct dp_meter
*meter
;
5706 if (mid
>= MAX_METERS
) {
5707 return EFBIG
; /* Meter_id out of range. */
5710 if (config
->flags
& ~DP_SUPPORTED_METER_FLAGS_MASK
) {
5711 return EBADF
; /* Unsupported flags set */
5714 if (config
->n_bands
> MAX_BANDS
) {
5718 for (i
= 0; i
< config
->n_bands
; ++i
) {
5719 switch (config
->bands
[i
].type
) {
5723 return ENODEV
; /* Unsupported band type */
5727 /* Allocate meter */
5728 meter
= xzalloc(sizeof *meter
5729 + config
->n_bands
* sizeof(struct dp_meter_band
));
5731 meter
->flags
= config
->flags
;
5732 meter
->n_bands
= config
->n_bands
;
5733 meter
->max_delta_t
= 0;
5734 meter
->used
= time_usec();
5737 for (i
= 0; i
< config
->n_bands
; ++i
) {
5738 uint32_t band_max_delta_t
;
5740 /* Set burst size to a workable value if none specified. */
5741 if (config
->bands
[i
].burst_size
== 0) {
5742 config
->bands
[i
].burst_size
= config
->bands
[i
].rate
;
5745 meter
->bands
[i
].up
= config
->bands
[i
];
5746 /* Convert burst size to the bucket units: */
5747 /* pkts => 1/1000 packets, kilobits => bits. */
5748 meter
->bands
[i
].up
.burst_size
*= 1000;
5749 /* Initialize bucket to empty. */
5750 meter
->bands
[i
].bucket
= 0;
5752 /* Figure out max delta_t that is enough to fill any bucket. */
5754 = meter
->bands
[i
].up
.burst_size
/ meter
->bands
[i
].up
.rate
;
5755 if (band_max_delta_t
> meter
->max_delta_t
) {
5756 meter
->max_delta_t
= band_max_delta_t
;
5760 meter_lock(dp
, mid
);
5761 dp_delete_meter(dp
, mid
); /* Free existing meter, if any */
5762 dp
->meters
[mid
] = meter
;
5763 meter_unlock(dp
, mid
);
5769 dpif_netdev_meter_get(const struct dpif
*dpif
,
5770 ofproto_meter_id meter_id_
,
5771 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
5773 const struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5774 uint32_t meter_id
= meter_id_
.uint32
;
5777 if (meter_id
>= MAX_METERS
) {
5781 meter_lock(dp
, meter_id
);
5782 const struct dp_meter
*meter
= dp
->meters
[meter_id
];
5790 stats
->packet_in_count
= meter
->packet_count
;
5791 stats
->byte_in_count
= meter
->byte_count
;
5793 for (i
= 0; i
< n_bands
&& i
< meter
->n_bands
; ++i
) {
5794 stats
->bands
[i
].packet_count
= meter
->bands
[i
].packet_count
;
5795 stats
->bands
[i
].byte_count
= meter
->bands
[i
].byte_count
;
5802 meter_unlock(dp
, meter_id
);
5807 dpif_netdev_meter_del(struct dpif
*dpif
,
5808 ofproto_meter_id meter_id_
,
5809 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
5811 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5814 error
= dpif_netdev_meter_get(dpif
, meter_id_
, stats
, n_bands
);
5816 uint32_t meter_id
= meter_id_
.uint32
;
5818 meter_lock(dp
, meter_id
);
5819 dp_delete_meter(dp
, meter_id
);
5820 meter_unlock(dp
, meter_id
);
5827 dpif_netdev_disable_upcall(struct dpif
*dpif
)
5828 OVS_NO_THREAD_SAFETY_ANALYSIS
5830 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5831 dp_netdev_disable_upcall(dp
);
5835 dp_netdev_enable_upcall(struct dp_netdev
*dp
)
5836 OVS_RELEASES(dp
->upcall_rwlock
)
5838 fat_rwlock_unlock(&dp
->upcall_rwlock
);
5842 dpif_netdev_enable_upcall(struct dpif
*dpif
)
5843 OVS_NO_THREAD_SAFETY_ANALYSIS
5845 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5846 dp_netdev_enable_upcall(dp
);
5850 dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
)
5852 pmd
->last_reload_seq
= seq_read(pmd
->reload_seq
);
5853 atomic_store_explicit(&pmd
->reload
, false, memory_order_release
);
5856 /* Finds and refs the dp_netdev_pmd_thread on core 'core_id'. Returns
5857 * the pointer if succeeds, otherwise, NULL (it can return NULL even if
5858 * 'core_id' is NON_PMD_CORE_ID).
5860 * Caller must unrefs the returned reference. */
5861 static struct dp_netdev_pmd_thread
*
5862 dp_netdev_get_pmd(struct dp_netdev
*dp
, unsigned core_id
)
5864 struct dp_netdev_pmd_thread
*pmd
;
5865 const struct cmap_node
*pnode
;
5867 pnode
= cmap_find(&dp
->poll_threads
, hash_int(core_id
, 0));
5871 pmd
= CONTAINER_OF(pnode
, struct dp_netdev_pmd_thread
, node
);
5873 return dp_netdev_pmd_try_ref(pmd
) ? pmd
: NULL
;
5876 /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */
5878 dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
5879 OVS_REQUIRES(dp
->port_mutex
)
5881 struct dp_netdev_pmd_thread
*non_pmd
;
5883 non_pmd
= xzalloc(sizeof *non_pmd
);
5884 dp_netdev_configure_pmd(non_pmd
, dp
, NON_PMD_CORE_ID
, OVS_NUMA_UNSPEC
);
5887 /* Caller must have valid pointer to 'pmd'. */
5889 dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
)
5891 return ovs_refcount_try_ref_rcu(&pmd
->ref_cnt
);
5895 dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
)
5897 if (pmd
&& ovs_refcount_unref(&pmd
->ref_cnt
) == 1) {
5898 ovsrcu_postpone(dp_netdev_destroy_pmd
, pmd
);
5902 /* Given cmap position 'pos', tries to ref the next node. If try_ref()
5903 * fails, keeps checking for next node until reaching the end of cmap.
5905 * Caller must unrefs the returned reference. */
5906 static struct dp_netdev_pmd_thread
*
5907 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
)
5909 struct dp_netdev_pmd_thread
*next
;
5912 struct cmap_node
*node
;
5914 node
= cmap_next_position(&dp
->poll_threads
, pos
);
5915 next
= node
? CONTAINER_OF(node
, struct dp_netdev_pmd_thread
, node
)
5917 } while (next
&& !dp_netdev_pmd_try_ref(next
));
5922 /* Configures the 'pmd' based on the input argument. */
5924 dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
, struct dp_netdev
*dp
,
5925 unsigned core_id
, int numa_id
)
5928 pmd
->core_id
= core_id
;
5929 pmd
->numa_id
= numa_id
;
5930 pmd
->need_reload
= false;
5931 pmd
->n_output_batches
= 0;
5933 ovs_refcount_init(&pmd
->ref_cnt
);
5934 atomic_init(&pmd
->exit
, false);
5935 pmd
->reload_seq
= seq_create();
5936 pmd
->last_reload_seq
= seq_read(pmd
->reload_seq
);
5937 atomic_init(&pmd
->reload
, false);
5938 ovs_mutex_init(&pmd
->flow_mutex
);
5939 ovs_mutex_init(&pmd
->port_mutex
);
5940 cmap_init(&pmd
->flow_table
);
5941 cmap_init(&pmd
->classifiers
);
5942 pmd
->ctx
.last_rxq
= NULL
;
5943 pmd_thread_ctx_time_update(pmd
);
5944 pmd
->next_optimization
= pmd
->ctx
.now
+ DPCLS_OPTIMIZATION_INTERVAL
;
5945 pmd
->rxq_next_cycle_store
= pmd
->ctx
.now
+ PMD_RXQ_INTERVAL_LEN
;
5946 hmap_init(&pmd
->poll_list
);
5947 hmap_init(&pmd
->tx_ports
);
5948 hmap_init(&pmd
->tnl_port_cache
);
5949 hmap_init(&pmd
->send_port_cache
);
5950 /* init the 'flow_cache' since there is no
5951 * actual thread created for NON_PMD_CORE_ID. */
5952 if (core_id
== NON_PMD_CORE_ID
) {
5953 dfc_cache_init(&pmd
->flow_cache
);
5954 pmd_alloc_static_tx_qid(pmd
);
5956 pmd_perf_stats_init(&pmd
->perf_stats
);
5957 cmap_insert(&dp
->poll_threads
, CONST_CAST(struct cmap_node
*, &pmd
->node
),
5958 hash_int(core_id
, 0));
5962 dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
)
5966 dp_netdev_pmd_flow_flush(pmd
);
5967 hmap_destroy(&pmd
->send_port_cache
);
5968 hmap_destroy(&pmd
->tnl_port_cache
);
5969 hmap_destroy(&pmd
->tx_ports
);
5970 hmap_destroy(&pmd
->poll_list
);
5971 /* All flows (including their dpcls_rules) have been deleted already */
5972 CMAP_FOR_EACH (cls
, node
, &pmd
->classifiers
) {
5974 ovsrcu_postpone(free
, cls
);
5976 cmap_destroy(&pmd
->classifiers
);
5977 cmap_destroy(&pmd
->flow_table
);
5978 ovs_mutex_destroy(&pmd
->flow_mutex
);
5979 seq_destroy(pmd
->reload_seq
);
5980 ovs_mutex_destroy(&pmd
->port_mutex
);
5984 /* Stops the pmd thread, removes it from the 'dp->poll_threads',
5985 * and unrefs the struct. */
5987 dp_netdev_del_pmd(struct dp_netdev
*dp
, struct dp_netdev_pmd_thread
*pmd
)
5989 /* NON_PMD_CORE_ID doesn't have a thread, so we don't have to synchronize,
5990 * but extra cleanup is necessary */
5991 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
5992 ovs_mutex_lock(&dp
->non_pmd_mutex
);
5993 dfc_cache_uninit(&pmd
->flow_cache
);
5994 pmd_free_cached_ports(pmd
);
5995 pmd_free_static_tx_qid(pmd
);
5996 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
5998 atomic_store_relaxed(&pmd
->exit
, true);
5999 dp_netdev_reload_pmd__(pmd
);
6000 xpthread_join(pmd
->thread
, NULL
);
6003 dp_netdev_pmd_clear_ports(pmd
);
6005 /* Purges the 'pmd''s flows after stopping the thread, but before
6006 * destroying the flows, so that the flow stats can be collected. */
6007 if (dp
->dp_purge_cb
) {
6008 dp
->dp_purge_cb(dp
->dp_purge_aux
, pmd
->core_id
);
6010 cmap_remove(&pmd
->dp
->poll_threads
, &pmd
->node
, hash_int(pmd
->core_id
, 0));
6011 dp_netdev_pmd_unref(pmd
);
6014 /* Destroys all pmd threads. If 'non_pmd' is true it also destroys the non pmd
6017 dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
, bool non_pmd
)
6019 struct dp_netdev_pmd_thread
*pmd
;
6020 struct dp_netdev_pmd_thread
**pmd_list
;
6021 size_t k
= 0, n_pmds
;
6023 n_pmds
= cmap_count(&dp
->poll_threads
);
6024 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
6026 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
6027 if (!non_pmd
&& pmd
->core_id
== NON_PMD_CORE_ID
) {
6030 /* We cannot call dp_netdev_del_pmd(), since it alters
6031 * 'dp->poll_threads' (while we're iterating it) and it
6033 ovs_assert(k
< n_pmds
);
6034 pmd_list
[k
++] = pmd
;
6037 for (size_t i
= 0; i
< k
; i
++) {
6038 dp_netdev_del_pmd(dp
, pmd_list
[i
]);
6043 /* Deletes all rx queues from pmd->poll_list and all the ports from
6046 dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
)
6048 struct rxq_poll
*poll
;
6049 struct tx_port
*port
;
6051 ovs_mutex_lock(&pmd
->port_mutex
);
6052 HMAP_FOR_EACH_POP (poll
, node
, &pmd
->poll_list
) {
6055 HMAP_FOR_EACH_POP (port
, node
, &pmd
->tx_ports
) {
6058 ovs_mutex_unlock(&pmd
->port_mutex
);
6061 /* Adds rx queue to poll_list of PMD thread, if it's not there already. */
6063 dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
6064 struct dp_netdev_rxq
*rxq
)
6065 OVS_REQUIRES(pmd
->port_mutex
)
6067 int qid
= netdev_rxq_get_queue_id(rxq
->rx
);
6068 uint32_t hash
= hash_2words(odp_to_u32(rxq
->port
->port_no
), qid
);
6069 struct rxq_poll
*poll
;
6071 HMAP_FOR_EACH_WITH_HASH (poll
, node
, hash
, &pmd
->poll_list
) {
6072 if (poll
->rxq
== rxq
) {
6073 /* 'rxq' is already polled by this thread. Do nothing. */
6078 poll
= xmalloc(sizeof *poll
);
6080 hmap_insert(&pmd
->poll_list
, &poll
->node
, hash
);
6082 pmd
->need_reload
= true;
6085 /* Delete 'poll' from poll_list of PMD thread. */
6087 dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
6088 struct rxq_poll
*poll
)
6089 OVS_REQUIRES(pmd
->port_mutex
)
6091 hmap_remove(&pmd
->poll_list
, &poll
->node
);
6094 pmd
->need_reload
= true;
6097 /* Add 'port' to the tx port cache of 'pmd', which must be reloaded for the
6098 * changes to take effect. */
6100 dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
6101 struct dp_netdev_port
*port
)
6102 OVS_REQUIRES(pmd
->port_mutex
)
6106 tx
= tx_port_lookup(&pmd
->tx_ports
, port
->port_no
);
6108 /* 'port' is already on this thread tx cache. Do nothing. */
6112 tx
= xzalloc(sizeof *tx
);
6116 tx
->flush_time
= 0LL;
6117 dp_packet_batch_init(&tx
->output_pkts
);
6119 hmap_insert(&pmd
->tx_ports
, &tx
->node
, hash_port_no(tx
->port
->port_no
));
6120 pmd
->need_reload
= true;
6123 /* Del 'tx' from the tx port cache of 'pmd', which must be reloaded for the
6124 * changes to take effect. */
6126 dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
6128 OVS_REQUIRES(pmd
->port_mutex
)
6130 hmap_remove(&pmd
->tx_ports
, &tx
->node
);
6132 pmd
->need_reload
= true;
6136 dpif_netdev_get_datapath_version(void)
6138 return xstrdup("<built-in>");
6142 dp_netdev_flow_used(struct dp_netdev_flow
*netdev_flow
, int cnt
, int size
,
6143 uint16_t tcp_flags
, long long now
)
6147 atomic_store_relaxed(&netdev_flow
->stats
.used
, now
);
6148 non_atomic_ullong_add(&netdev_flow
->stats
.packet_count
, cnt
);
6149 non_atomic_ullong_add(&netdev_flow
->stats
.byte_count
, size
);
6150 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
6152 atomic_store_relaxed(&netdev_flow
->stats
.tcp_flags
, flags
);
6156 dp_netdev_upcall(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet
*packet_
,
6157 struct flow
*flow
, struct flow_wildcards
*wc
, ovs_u128
*ufid
,
6158 enum dpif_upcall_type type
, const struct nlattr
*userdata
,
6159 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
6161 struct dp_netdev
*dp
= pmd
->dp
;
6163 if (OVS_UNLIKELY(!dp
->upcall_cb
)) {
6167 if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl
))) {
6168 struct ds ds
= DS_EMPTY_INITIALIZER
;
6171 struct odp_flow_key_parms odp_parms
= {
6173 .mask
= wc
? &wc
->masks
: NULL
,
6174 .support
= dp_netdev_support
,
6177 ofpbuf_init(&key
, 0);
6178 odp_flow_key_from_flow(&odp_parms
, &key
);
6179 packet_str
= ofp_dp_packet_to_string(packet_
);
6181 odp_flow_key_format(key
.data
, key
.size
, &ds
);
6183 VLOG_DBG("%s: %s upcall:\n%s\n%s", dp
->name
,
6184 dpif_upcall_type_to_string(type
), ds_cstr(&ds
), packet_str
);
6186 ofpbuf_uninit(&key
);
6192 return dp
->upcall_cb(packet_
, flow
, ufid
, pmd
->core_id
, type
, userdata
,
6193 actions
, wc
, put_actions
, dp
->upcall_aux
);
6196 static inline uint32_t
6197 dpif_netdev_packet_get_rss_hash_orig_pkt(struct dp_packet
*packet
,
6198 const struct miniflow
*mf
)
6202 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
6203 hash
= dp_packet_get_rss_hash(packet
);
6205 hash
= miniflow_hash_5tuple(mf
, 0);
6206 dp_packet_set_rss_hash(packet
, hash
);
6212 static inline uint32_t
6213 dpif_netdev_packet_get_rss_hash(struct dp_packet
*packet
,
6214 const struct miniflow
*mf
)
6216 uint32_t hash
, recirc_depth
;
6218 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
6219 hash
= dp_packet_get_rss_hash(packet
);
6221 hash
= miniflow_hash_5tuple(mf
, 0);
6222 dp_packet_set_rss_hash(packet
, hash
);
6225 /* The RSS hash must account for the recirculation depth to avoid
6226 * collisions in the exact match cache */
6227 recirc_depth
= *recirc_depth_get_unsafe();
6228 if (OVS_UNLIKELY(recirc_depth
)) {
6229 hash
= hash_finish(hash
, recirc_depth
);
6230 dp_packet_set_rss_hash(packet
, hash
);
6235 struct packet_batch_per_flow
{
6236 unsigned int byte_count
;
6238 struct dp_netdev_flow
*flow
;
6240 struct dp_packet_batch array
;
6244 packet_batch_per_flow_update(struct packet_batch_per_flow
*batch
,
6245 struct dp_packet
*packet
,
6248 batch
->byte_count
+= dp_packet_size(packet
);
6249 batch
->tcp_flags
|= tcp_flags
;
6250 batch
->array
.packets
[batch
->array
.count
++] = packet
;
6254 packet_batch_per_flow_init(struct packet_batch_per_flow
*batch
,
6255 struct dp_netdev_flow
*flow
)
6257 flow
->batch
= batch
;
6260 dp_packet_batch_init(&batch
->array
);
6261 batch
->byte_count
= 0;
6262 batch
->tcp_flags
= 0;
6266 packet_batch_per_flow_execute(struct packet_batch_per_flow
*batch
,
6267 struct dp_netdev_pmd_thread
*pmd
)
6269 struct dp_netdev_actions
*actions
;
6270 struct dp_netdev_flow
*flow
= batch
->flow
;
6272 dp_netdev_flow_used(flow
, batch
->array
.count
, batch
->byte_count
,
6273 batch
->tcp_flags
, pmd
->ctx
.now
/ 1000);
6275 actions
= dp_netdev_flow_get_actions(flow
);
6277 dp_netdev_execute_actions(pmd
, &batch
->array
, true, &flow
->flow
,
6278 actions
->actions
, actions
->size
);
6282 dp_netdev_queue_batches(struct dp_packet
*pkt
,
6283 struct dp_netdev_flow
*flow
, uint16_t tcp_flags
,
6284 struct packet_batch_per_flow
*batches
,
6287 struct packet_batch_per_flow
*batch
= flow
->batch
;
6289 if (OVS_UNLIKELY(!batch
)) {
6290 batch
= &batches
[(*n_batches
)++];
6291 packet_batch_per_flow_init(batch
, flow
);
6294 packet_batch_per_flow_update(batch
, pkt
, tcp_flags
);
6298 packet_enqueue_to_flow_map(struct dp_packet
*packet
,
6299 struct dp_netdev_flow
*flow
,
6301 struct dp_packet_flow_map
*flow_map
,
6304 struct dp_packet_flow_map
*map
= &flow_map
[index
];
6306 map
->packet
= packet
;
6307 map
->tcp_flags
= tcp_flags
;
6310 /* SMC lookup function for a batch of packets.
6311 * By doing batching SMC lookup, we can use prefetch
6312 * to hide memory access latency.
6315 smc_lookup_batch(struct dp_netdev_pmd_thread
*pmd
,
6316 struct netdev_flow_key
*keys
,
6317 struct netdev_flow_key
**missed_keys
,
6318 struct dp_packet_batch
*packets_
,
6320 struct dp_packet_flow_map
*flow_map
,
6324 struct dp_packet
*packet
;
6325 size_t n_smc_hit
= 0, n_missed
= 0;
6326 struct dfc_cache
*cache
= &pmd
->flow_cache
;
6327 struct smc_cache
*smc_cache
= &cache
->smc_cache
;
6328 const struct cmap_node
*flow_node
;
6332 /* Prefetch buckets for all packets */
6333 for (i
= 0; i
< cnt
; i
++) {
6334 OVS_PREFETCH(&smc_cache
->buckets
[keys
[i
].hash
& SMC_MASK
]);
6337 DP_PACKET_BATCH_REFILL_FOR_EACH (i
, cnt
, packet
, packets_
) {
6338 struct dp_netdev_flow
*flow
= NULL
;
6339 flow_node
= smc_entry_get(pmd
, keys
[i
].hash
);
6341 /* Get the original order of this packet in received batch. */
6342 recv_idx
= index_map
[i
];
6344 if (OVS_LIKELY(flow_node
!= NULL
)) {
6345 CMAP_NODE_FOR_EACH (flow
, node
, flow_node
) {
6346 /* Since we dont have per-port megaflow to check the port
6347 * number, we need to verify that the input ports match. */
6348 if (OVS_LIKELY(dpcls_rule_matches_key(&flow
->cr
, &keys
[i
]) &&
6349 flow
->flow
.in_port
.odp_port
== packet
->md
.in_port
.odp_port
)) {
6350 tcp_flags
= miniflow_get_tcp_flags(&keys
[i
].mf
);
6352 /* SMC hit and emc miss, we insert into EMC */
6354 netdev_flow_key_size(miniflow_n_values(&keys
[i
].mf
));
6355 emc_probabilistic_insert(pmd
, &keys
[i
], flow
);
6356 /* Add these packets into the flow map in the same order
6359 packet_enqueue_to_flow_map(packet
, flow
, tcp_flags
,
6360 flow_map
, recv_idx
);
6371 /* SMC missed. Group missed packets together at
6372 * the beginning of the 'packets' array. */
6373 dp_packet_batch_refill(packets_
, packet
, i
);
6375 /* Preserve the order of packet for flow batching. */
6376 index_map
[n_missed
] = recv_idx
;
6378 /* Put missed keys to the pointer arrays return to the caller */
6379 missed_keys
[n_missed
++] = &keys
[i
];
6382 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_SMC_HIT
, n_smc_hit
);
6385 /* Try to process all ('cnt') the 'packets' using only the datapath flow cache
6386 * 'pmd->flow_cache'. If a flow is not found for a packet 'packets[i]', the
6387 * miniflow is copied into 'keys' and the packet pointer is moved at the
6388 * beginning of the 'packets' array. The pointers of missed keys are put in the
6389 * missed_keys pointer array for future processing.
6391 * The function returns the number of packets that needs to be processed in the
6392 * 'packets' array (they have been moved to the beginning of the vector).
6394 * For performance reasons a caller may choose not to initialize the metadata
6395 * in 'packets_'. If 'md_is_valid' is false, the metadata in 'packets'
6396 * is not valid and must be initialized by this function using 'port_no'.
6397 * If 'md_is_valid' is true, the metadata is already valid and 'port_no'
6400 static inline size_t
6401 dfc_processing(struct dp_netdev_pmd_thread
*pmd
,
6402 struct dp_packet_batch
*packets_
,
6403 struct netdev_flow_key
*keys
,
6404 struct netdev_flow_key
**missed_keys
,
6405 struct packet_batch_per_flow batches
[], size_t *n_batches
,
6406 struct dp_packet_flow_map
*flow_map
,
6407 size_t *n_flows
, uint8_t *index_map
,
6408 bool md_is_valid
, odp_port_t port_no
)
6410 struct netdev_flow_key
*key
= &keys
[0];
6411 size_t n_missed
= 0, n_emc_hit
= 0;
6412 struct dfc_cache
*cache
= &pmd
->flow_cache
;
6413 struct dp_packet
*packet
;
6414 const size_t cnt
= dp_packet_batch_size(packets_
);
6415 uint32_t cur_min
= pmd
->ctx
.emc_insert_min
;
6420 bool batch_enable
= true;
6422 atomic_read_relaxed(&pmd
->dp
->smc_enable_db
, &smc_enable_db
);
6423 pmd_perf_update_counter(&pmd
->perf_stats
,
6424 md_is_valid
? PMD_STAT_RECIRC
: PMD_STAT_RECV
,
6427 DP_PACKET_BATCH_REFILL_FOR_EACH (i
, cnt
, packet
, packets_
) {
6428 struct dp_netdev_flow
*flow
;
6431 if (OVS_UNLIKELY(dp_packet_size(packet
) < ETH_HEADER_LEN
)) {
6432 dp_packet_delete(packet
);
6437 struct dp_packet
**packets
= packets_
->packets
;
6438 /* Prefetch next packet data and metadata. */
6439 OVS_PREFETCH(dp_packet_data(packets
[i
+1]));
6440 pkt_metadata_prefetch_init(&packets
[i
+1]->md
);
6444 pkt_metadata_init(&packet
->md
, port_no
);
6447 if ((*recirc_depth_get() == 0) &&
6448 dp_packet_has_flow_mark(packet
, &mark
)) {
6449 flow
= mark_to_flow_find(pmd
, mark
);
6450 if (OVS_LIKELY(flow
)) {
6451 tcp_flags
= parse_tcp_flags(packet
);
6452 if (OVS_LIKELY(batch_enable
)) {
6453 dp_netdev_queue_batches(packet
, flow
, tcp_flags
, batches
,
6456 /* Flow batching should be performed only after fast-path
6457 * processing is also completed for packets with emc miss
6458 * or else it will result in reordering of packets with
6459 * same datapath flows. */
6460 packet_enqueue_to_flow_map(packet
, flow
, tcp_flags
,
6461 flow_map
, map_cnt
++);
6467 miniflow_extract(packet
, &key
->mf
);
6468 key
->len
= 0; /* Not computed yet. */
6470 (md_is_valid
== false)
6471 ? dpif_netdev_packet_get_rss_hash_orig_pkt(packet
, &key
->mf
)
6472 : dpif_netdev_packet_get_rss_hash(packet
, &key
->mf
);
6474 /* If EMC is disabled skip emc_lookup */
6475 flow
= (cur_min
!= 0) ? emc_lookup(&cache
->emc_cache
, key
) : NULL
;
6476 if (OVS_LIKELY(flow
)) {
6477 tcp_flags
= miniflow_get_tcp_flags(&key
->mf
);
6479 if (OVS_LIKELY(batch_enable
)) {
6480 dp_netdev_queue_batches(packet
, flow
, tcp_flags
, batches
,
6483 /* Flow batching should be performed only after fast-path
6484 * processing is also completed for packets with emc miss
6485 * or else it will result in reordering of packets with
6486 * same datapath flows. */
6487 packet_enqueue_to_flow_map(packet
, flow
, tcp_flags
,
6488 flow_map
, map_cnt
++);
6491 /* Exact match cache missed. Group missed packets together at
6492 * the beginning of the 'packets' array. */
6493 dp_packet_batch_refill(packets_
, packet
, i
);
6495 /* Preserve the order of packet for flow batching. */
6496 index_map
[n_missed
] = map_cnt
;
6497 flow_map
[map_cnt
++].flow
= NULL
;
6499 /* 'key[n_missed]' contains the key of the current packet and it
6500 * will be passed to SMC lookup. The next key should be extracted
6501 * to 'keys[n_missed + 1]'.
6502 * We also maintain a pointer array to keys missed both SMC and EMC
6503 * which will be returned to the caller for future processing. */
6504 missed_keys
[n_missed
] = key
;
6505 key
= &keys
[++n_missed
];
6507 /* Skip batching for subsequent packets to avoid reordering. */
6508 batch_enable
= false;
6511 /* Count of packets which are not flow batched. */
6514 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_EXACT_HIT
, n_emc_hit
);
6516 if (!smc_enable_db
) {
6517 return dp_packet_batch_size(packets_
);
6520 /* Packets miss EMC will do a batch lookup in SMC if enabled */
6521 smc_lookup_batch(pmd
, keys
, missed_keys
, packets_
,
6522 n_missed
, flow_map
, index_map
);
6524 return dp_packet_batch_size(packets_
);
6528 handle_packet_upcall(struct dp_netdev_pmd_thread
*pmd
,
6529 struct dp_packet
*packet
,
6530 const struct netdev_flow_key
*key
,
6531 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
6533 struct ofpbuf
*add_actions
;
6534 struct dp_packet_batch b
;
6538 uint64_t cycles
= cycles_counter_update(&pmd
->perf_stats
);
6540 match
.tun_md
.valid
= false;
6541 miniflow_expand(&key
->mf
, &match
.flow
);
6543 ofpbuf_clear(actions
);
6544 ofpbuf_clear(put_actions
);
6546 dpif_flow_hash(pmd
->dp
->dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
6547 error
= dp_netdev_upcall(pmd
, packet
, &match
.flow
, &match
.wc
,
6548 &ufid
, DPIF_UC_MISS
, NULL
, actions
,
6550 if (OVS_UNLIKELY(error
&& error
!= ENOSPC
)) {
6551 dp_packet_delete(packet
);
6555 /* The Netlink encoding of datapath flow keys cannot express
6556 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
6557 * tag is interpreted as exact match on the fact that there is no
6558 * VLAN. Unless we refactor a lot of code that translates between
6559 * Netlink and struct flow representations, we have to do the same
6560 * here. This must be in sync with 'match' in dpif_netdev_flow_put(). */
6561 if (!match
.wc
.masks
.vlans
[0].tci
) {
6562 match
.wc
.masks
.vlans
[0].tci
= htons(0xffff);
6565 /* We can't allow the packet batching in the next loop to execute
6566 * the actions. Otherwise, if there are any slow path actions,
6567 * we'll send the packet up twice. */
6568 dp_packet_batch_init_packet(&b
, packet
);
6569 dp_netdev_execute_actions(pmd
, &b
, true, &match
.flow
,
6570 actions
->data
, actions
->size
);
6572 add_actions
= put_actions
->size
? put_actions
: actions
;
6573 if (OVS_LIKELY(error
!= ENOSPC
)) {
6574 struct dp_netdev_flow
*netdev_flow
;
6576 /* XXX: There's a race window where a flow covering this packet
6577 * could have already been installed since we last did the flow
6578 * lookup before upcall. This could be solved by moving the
6579 * mutex lock outside the loop, but that's an awful long time
6580 * to be locking revalidators out of making flow modifications. */
6581 ovs_mutex_lock(&pmd
->flow_mutex
);
6582 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
, NULL
);
6583 if (OVS_LIKELY(!netdev_flow
)) {
6584 netdev_flow
= dp_netdev_flow_add(pmd
, &match
, &ufid
,
6588 ovs_mutex_unlock(&pmd
->flow_mutex
);
6589 uint32_t hash
= dp_netdev_flow_hash(&netdev_flow
->ufid
);
6590 smc_insert(pmd
, key
, hash
);
6591 emc_probabilistic_insert(pmd
, key
, netdev_flow
);
6593 if (pmd_perf_metrics_enabled(pmd
)) {
6594 /* Update upcall stats. */
6595 cycles
= cycles_counter_update(&pmd
->perf_stats
) - cycles
;
6596 struct pmd_perf_stats
*s
= &pmd
->perf_stats
;
6597 s
->current
.upcalls
++;
6598 s
->current
.upcall_cycles
+= cycles
;
6599 histogram_add_sample(&s
->cycles_per_upcall
, cycles
);
6605 fast_path_processing(struct dp_netdev_pmd_thread
*pmd
,
6606 struct dp_packet_batch
*packets_
,
6607 struct netdev_flow_key
**keys
,
6608 struct dp_packet_flow_map
*flow_map
,
6612 const size_t cnt
= dp_packet_batch_size(packets_
);
6613 #if !defined(__CHECKER__) && !defined(_WIN32)
6614 const size_t PKT_ARRAY_SIZE
= cnt
;
6616 /* Sparse or MSVC doesn't like variable length array. */
6617 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
6619 struct dp_packet
*packet
;
6621 struct dpcls_rule
*rules
[PKT_ARRAY_SIZE
];
6622 struct dp_netdev
*dp
= pmd
->dp
;
6623 int upcall_ok_cnt
= 0, upcall_fail_cnt
= 0;
6624 int lookup_cnt
= 0, add_lookup_cnt
;
6627 for (size_t i
= 0; i
< cnt
; i
++) {
6628 /* Key length is needed in all the cases, hash computed on demand. */
6629 keys
[i
]->len
= netdev_flow_key_size(miniflow_n_values(&keys
[i
]->mf
));
6631 /* Get the classifier for the in_port */
6632 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
6633 if (OVS_LIKELY(cls
)) {
6634 any_miss
= !dpcls_lookup(cls
, (const struct netdev_flow_key
**)keys
,
6635 rules
, cnt
, &lookup_cnt
);
6638 memset(rules
, 0, sizeof(rules
));
6640 if (OVS_UNLIKELY(any_miss
) && !fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
6641 uint64_t actions_stub
[512 / 8], slow_stub
[512 / 8];
6642 struct ofpbuf actions
, put_actions
;
6644 ofpbuf_use_stub(&actions
, actions_stub
, sizeof actions_stub
);
6645 ofpbuf_use_stub(&put_actions
, slow_stub
, sizeof slow_stub
);
6647 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6648 struct dp_netdev_flow
*netdev_flow
;
6650 if (OVS_LIKELY(rules
[i
])) {
6654 /* It's possible that an earlier slow path execution installed
6655 * a rule covering this flow. In this case, it's a lot cheaper
6656 * to catch it here than execute a miss. */
6657 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, keys
[i
],
6660 lookup_cnt
+= add_lookup_cnt
;
6661 rules
[i
] = &netdev_flow
->cr
;
6665 int error
= handle_packet_upcall(pmd
, packet
, keys
[i
],
6666 &actions
, &put_actions
);
6668 if (OVS_UNLIKELY(error
)) {
6675 ofpbuf_uninit(&actions
);
6676 ofpbuf_uninit(&put_actions
);
6677 fat_rwlock_unlock(&dp
->upcall_rwlock
);
6678 } else if (OVS_UNLIKELY(any_miss
)) {
6679 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6680 if (OVS_UNLIKELY(!rules
[i
])) {
6681 dp_packet_delete(packet
);
6687 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6688 struct dp_netdev_flow
*flow
;
6689 /* Get the original order of this packet in received batch. */
6690 int recv_idx
= index_map
[i
];
6693 if (OVS_UNLIKELY(!rules
[i
])) {
6697 flow
= dp_netdev_flow_cast(rules
[i
]);
6698 uint32_t hash
= dp_netdev_flow_hash(&flow
->ufid
);
6699 smc_insert(pmd
, keys
[i
], hash
);
6701 emc_probabilistic_insert(pmd
, keys
[i
], flow
);
6702 /* Add these packets into the flow map in the same order
6705 tcp_flags
= miniflow_get_tcp_flags(&keys
[i
]->mf
);
6706 packet_enqueue_to_flow_map(packet
, flow
, tcp_flags
,
6707 flow_map
, recv_idx
);
6710 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MASKED_HIT
,
6711 cnt
- upcall_ok_cnt
- upcall_fail_cnt
);
6712 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MASKED_LOOKUP
,
6714 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MISS
,
6716 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_LOST
,
6720 /* Packets enter the datapath from a port (or from recirculation) here.
6722 * When 'md_is_valid' is true the metadata in 'packets' are already valid.
6723 * When false the metadata in 'packets' need to be initialized. */
6725 dp_netdev_input__(struct dp_netdev_pmd_thread
*pmd
,
6726 struct dp_packet_batch
*packets
,
6727 bool md_is_valid
, odp_port_t port_no
)
6729 #if !defined(__CHECKER__) && !defined(_WIN32)
6730 const size_t PKT_ARRAY_SIZE
= dp_packet_batch_size(packets
);
6732 /* Sparse or MSVC doesn't like variable length array. */
6733 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
6735 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
)
6736 struct netdev_flow_key keys
[PKT_ARRAY_SIZE
];
6737 struct netdev_flow_key
*missed_keys
[PKT_ARRAY_SIZE
];
6738 struct packet_batch_per_flow batches
[PKT_ARRAY_SIZE
];
6740 struct dp_packet_flow_map flow_map
[PKT_ARRAY_SIZE
];
6741 uint8_t index_map
[PKT_ARRAY_SIZE
];
6747 dfc_processing(pmd
, packets
, keys
, missed_keys
, batches
, &n_batches
,
6748 flow_map
, &n_flows
, index_map
, md_is_valid
, port_no
);
6750 if (!dp_packet_batch_is_empty(packets
)) {
6751 /* Get ingress port from first packet's metadata. */
6752 in_port
= packets
->packets
[0]->md
.in_port
.odp_port
;
6753 fast_path_processing(pmd
, packets
, missed_keys
,
6754 flow_map
, index_map
, in_port
);
6757 /* Batch rest of packets which are in flow map. */
6758 for (i
= 0; i
< n_flows
; i
++) {
6759 struct dp_packet_flow_map
*map
= &flow_map
[i
];
6761 if (OVS_UNLIKELY(!map
->flow
)) {
6764 dp_netdev_queue_batches(map
->packet
, map
->flow
, map
->tcp_flags
,
6765 batches
, &n_batches
);
6768 /* All the flow batches need to be reset before any call to
6769 * packet_batch_per_flow_execute() as it could potentially trigger
6770 * recirculation. When a packet matching flow ‘j’ happens to be
6771 * recirculated, the nested call to dp_netdev_input__() could potentially
6772 * classify the packet as matching another flow - say 'k'. It could happen
6773 * that in the previous call to dp_netdev_input__() that same flow 'k' had
6774 * already its own batches[k] still waiting to be served. So if its
6775 * ‘batch’ member is not reset, the recirculated packet would be wrongly
6776 * appended to batches[k] of the 1st call to dp_netdev_input__(). */
6777 for (i
= 0; i
< n_batches
; i
++) {
6778 batches
[i
].flow
->batch
= NULL
;
6781 for (i
= 0; i
< n_batches
; i
++) {
6782 packet_batch_per_flow_execute(&batches
[i
], pmd
);
6787 dp_netdev_input(struct dp_netdev_pmd_thread
*pmd
,
6788 struct dp_packet_batch
*packets
,
6791 dp_netdev_input__(pmd
, packets
, false, port_no
);
6795 dp_netdev_recirculate(struct dp_netdev_pmd_thread
*pmd
,
6796 struct dp_packet_batch
*packets
)
6798 dp_netdev_input__(pmd
, packets
, true, 0);
6801 struct dp_netdev_execute_aux
{
6802 struct dp_netdev_pmd_thread
*pmd
;
6803 const struct flow
*flow
;
6807 dpif_netdev_register_dp_purge_cb(struct dpif
*dpif
, dp_purge_callback
*cb
,
6810 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6811 dp
->dp_purge_aux
= aux
;
6812 dp
->dp_purge_cb
= cb
;
6816 dpif_netdev_register_upcall_cb(struct dpif
*dpif
, upcall_callback
*cb
,
6819 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6820 dp
->upcall_aux
= aux
;
6825 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
6829 struct dp_netdev_port
*port
;
6832 HMAP_FOR_EACH (tx
, node
, &pmd
->send_port_cache
) {
6833 if (!tx
->port
->dynamic_txqs
) {
6836 interval
= pmd
->ctx
.now
- tx
->last_used
;
6837 if (tx
->qid
>= 0 && (purge
|| interval
>= XPS_TIMEOUT
)) {
6839 ovs_mutex_lock(&port
->txq_used_mutex
);
6840 port
->txq_used
[tx
->qid
]--;
6841 ovs_mutex_unlock(&port
->txq_used_mutex
);
6848 dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
6851 struct dp_netdev_port
*port
;
6853 int i
, min_cnt
, min_qid
;
6855 interval
= pmd
->ctx
.now
- tx
->last_used
;
6856 tx
->last_used
= pmd
->ctx
.now
;
6858 if (OVS_LIKELY(tx
->qid
>= 0 && interval
< XPS_TIMEOUT
)) {
6864 ovs_mutex_lock(&port
->txq_used_mutex
);
6866 port
->txq_used
[tx
->qid
]--;
6872 for (i
= 0; i
< netdev_n_txq(port
->netdev
); i
++) {
6873 if (port
->txq_used
[i
] < min_cnt
|| min_cnt
== -1) {
6874 min_cnt
= port
->txq_used
[i
];
6879 port
->txq_used
[min_qid
]++;
6882 ovs_mutex_unlock(&port
->txq_used_mutex
);
6884 dpif_netdev_xps_revalidate_pmd(pmd
, false);
6886 VLOG_DBG("Core %d: New TX queue ID %d for port \'%s\'.",
6887 pmd
->core_id
, tx
->qid
, netdev_get_name(tx
->port
->netdev
));
6891 static struct tx_port
*
6892 pmd_tnl_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
6895 return tx_port_lookup(&pmd
->tnl_port_cache
, port_no
);
6898 static struct tx_port
*
6899 pmd_send_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
6902 return tx_port_lookup(&pmd
->send_port_cache
, port_no
);
6906 push_tnl_action(const struct dp_netdev_pmd_thread
*pmd
,
6907 const struct nlattr
*attr
,
6908 struct dp_packet_batch
*batch
)
6910 struct tx_port
*tun_port
;
6911 const struct ovs_action_push_tnl
*data
;
6914 data
= nl_attr_get(attr
);
6916 tun_port
= pmd_tnl_port_cache_lookup(pmd
, data
->tnl_port
);
6921 err
= netdev_push_header(tun_port
->port
->netdev
, batch
, data
);
6926 dp_packet_delete_batch(batch
, true);
6931 dp_execute_userspace_action(struct dp_netdev_pmd_thread
*pmd
,
6932 struct dp_packet
*packet
, bool should_steal
,
6933 struct flow
*flow
, ovs_u128
*ufid
,
6934 struct ofpbuf
*actions
,
6935 const struct nlattr
*userdata
)
6937 struct dp_packet_batch b
;
6940 ofpbuf_clear(actions
);
6942 error
= dp_netdev_upcall(pmd
, packet
, flow
, NULL
, ufid
,
6943 DPIF_UC_ACTION
, userdata
, actions
,
6945 if (!error
|| error
== ENOSPC
) {
6946 dp_packet_batch_init_packet(&b
, packet
);
6947 dp_netdev_execute_actions(pmd
, &b
, should_steal
, flow
,
6948 actions
->data
, actions
->size
);
6949 } else if (should_steal
) {
6950 dp_packet_delete(packet
);
6955 dp_execute_cb(void *aux_
, struct dp_packet_batch
*packets_
,
6956 const struct nlattr
*a
, bool should_steal
)
6957 OVS_NO_THREAD_SAFETY_ANALYSIS
6959 struct dp_netdev_execute_aux
*aux
= aux_
;
6960 uint32_t *depth
= recirc_depth_get();
6961 struct dp_netdev_pmd_thread
*pmd
= aux
->pmd
;
6962 struct dp_netdev
*dp
= pmd
->dp
;
6963 int type
= nl_attr_type(a
);
6966 switch ((enum ovs_action_attr
)type
) {
6967 case OVS_ACTION_ATTR_OUTPUT
:
6968 p
= pmd_send_port_cache_lookup(pmd
, nl_attr_get_odp_port(a
));
6969 if (OVS_LIKELY(p
)) {
6970 struct dp_packet
*packet
;
6971 struct dp_packet_batch out
;
6973 if (!should_steal
) {
6974 dp_packet_batch_clone(&out
, packets_
);
6975 dp_packet_batch_reset_cutlen(packets_
);
6978 dp_packet_batch_apply_cutlen(packets_
);
6981 if (OVS_UNLIKELY(!dp_packet_batch_is_empty(&p
->output_pkts
)
6982 && packets_
->packets
[0]->source
6983 != p
->output_pkts
.packets
[0]->source
)) {
6984 /* XXX: netdev-dpdk assumes that all packets in a single
6985 * output batch has the same source. Flush here to
6986 * avoid memory access issues. */
6987 dp_netdev_pmd_flush_output_on_port(pmd
, p
);
6990 if (dp_packet_batch_size(&p
->output_pkts
)
6991 + dp_packet_batch_size(packets_
) > NETDEV_MAX_BURST
) {
6992 /* Flush here to avoid overflow. */
6993 dp_netdev_pmd_flush_output_on_port(pmd
, p
);
6996 if (dp_packet_batch_is_empty(&p
->output_pkts
)) {
6997 pmd
->n_output_batches
++;
7000 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
7001 p
->output_pkts_rxqs
[dp_packet_batch_size(&p
->output_pkts
)] =
7003 dp_packet_batch_add(&p
->output_pkts
, packet
);
7009 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
7011 /* We're requested to push tunnel header, but also we need to take
7012 * the ownership of these packets. Thus, we can avoid performing
7013 * the action, because the caller will not use the result anyway.
7014 * Just break to free the batch. */
7017 dp_packet_batch_apply_cutlen(packets_
);
7018 push_tnl_action(pmd
, a
, packets_
);
7021 case OVS_ACTION_ATTR_TUNNEL_POP
:
7022 if (*depth
< MAX_RECIRC_DEPTH
) {
7023 struct dp_packet_batch
*orig_packets_
= packets_
;
7024 odp_port_t portno
= nl_attr_get_odp_port(a
);
7026 p
= pmd_tnl_port_cache_lookup(pmd
, portno
);
7028 struct dp_packet_batch tnl_pkt
;
7030 if (!should_steal
) {
7031 dp_packet_batch_clone(&tnl_pkt
, packets_
);
7032 packets_
= &tnl_pkt
;
7033 dp_packet_batch_reset_cutlen(orig_packets_
);
7036 dp_packet_batch_apply_cutlen(packets_
);
7038 netdev_pop_header(p
->port
->netdev
, packets_
);
7039 if (dp_packet_batch_is_empty(packets_
)) {
7043 struct dp_packet
*packet
;
7044 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
7045 packet
->md
.in_port
.odp_port
= portno
;
7049 dp_netdev_recirculate(pmd
, packets_
);
7056 case OVS_ACTION_ATTR_USERSPACE
:
7057 if (!fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
7058 struct dp_packet_batch
*orig_packets_
= packets_
;
7059 const struct nlattr
*userdata
;
7060 struct dp_packet_batch usr_pkt
;
7061 struct ofpbuf actions
;
7066 userdata
= nl_attr_find_nested(a
, OVS_USERSPACE_ATTR_USERDATA
);
7067 ofpbuf_init(&actions
, 0);
7069 if (packets_
->trunc
) {
7070 if (!should_steal
) {
7071 dp_packet_batch_clone(&usr_pkt
, packets_
);
7072 packets_
= &usr_pkt
;
7074 dp_packet_batch_reset_cutlen(orig_packets_
);
7077 dp_packet_batch_apply_cutlen(packets_
);
7080 struct dp_packet
*packet
;
7081 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
7082 flow_extract(packet
, &flow
);
7083 dpif_flow_hash(dp
->dpif
, &flow
, sizeof flow
, &ufid
);
7084 dp_execute_userspace_action(pmd
, packet
, should_steal
, &flow
,
7085 &ufid
, &actions
, userdata
);
7089 dp_packet_delete_batch(packets_
, true);
7092 ofpbuf_uninit(&actions
);
7093 fat_rwlock_unlock(&dp
->upcall_rwlock
);
7099 case OVS_ACTION_ATTR_RECIRC
:
7100 if (*depth
< MAX_RECIRC_DEPTH
) {
7101 struct dp_packet_batch recirc_pkts
;
7103 if (!should_steal
) {
7104 dp_packet_batch_clone(&recirc_pkts
, packets_
);
7105 packets_
= &recirc_pkts
;
7108 struct dp_packet
*packet
;
7109 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
7110 packet
->md
.recirc_id
= nl_attr_get_u32(a
);
7114 dp_netdev_recirculate(pmd
, packets_
);
7120 VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
7123 case OVS_ACTION_ATTR_CT
: {
7124 const struct nlattr
*b
;
7126 bool commit
= false;
7129 const char *helper
= NULL
;
7130 const uint32_t *setmark
= NULL
;
7131 const struct ovs_key_ct_labels
*setlabel
= NULL
;
7132 struct nat_action_info_t nat_action_info
;
7133 struct nat_action_info_t
*nat_action_info_ref
= NULL
;
7134 bool nat_config
= false;
7136 NL_ATTR_FOR_EACH_UNSAFE (b
, left
, nl_attr_get(a
),
7137 nl_attr_get_size(a
)) {
7138 enum ovs_ct_attr sub_type
= nl_attr_type(b
);
7141 case OVS_CT_ATTR_FORCE_COMMIT
:
7144 case OVS_CT_ATTR_COMMIT
:
7147 case OVS_CT_ATTR_ZONE
:
7148 zone
= nl_attr_get_u16(b
);
7150 case OVS_CT_ATTR_HELPER
:
7151 helper
= nl_attr_get_string(b
);
7153 case OVS_CT_ATTR_MARK
:
7154 setmark
= nl_attr_get(b
);
7156 case OVS_CT_ATTR_LABELS
:
7157 setlabel
= nl_attr_get(b
);
7159 case OVS_CT_ATTR_EVENTMASK
:
7160 /* Silently ignored, as userspace datapath does not generate
7161 * netlink events. */
7163 case OVS_CT_ATTR_NAT
: {
7164 const struct nlattr
*b_nest
;
7165 unsigned int left_nest
;
7166 bool ip_min_specified
= false;
7167 bool proto_num_min_specified
= false;
7168 bool ip_max_specified
= false;
7169 bool proto_num_max_specified
= false;
7170 memset(&nat_action_info
, 0, sizeof nat_action_info
);
7171 nat_action_info_ref
= &nat_action_info
;
7173 NL_NESTED_FOR_EACH_UNSAFE (b_nest
, left_nest
, b
) {
7174 enum ovs_nat_attr sub_type_nest
= nl_attr_type(b_nest
);
7176 switch (sub_type_nest
) {
7177 case OVS_NAT_ATTR_SRC
:
7178 case OVS_NAT_ATTR_DST
:
7180 nat_action_info
.nat_action
|=
7181 ((sub_type_nest
== OVS_NAT_ATTR_SRC
)
7182 ? NAT_ACTION_SRC
: NAT_ACTION_DST
);
7184 case OVS_NAT_ATTR_IP_MIN
:
7185 memcpy(&nat_action_info
.min_addr
,
7186 nl_attr_get(b_nest
),
7187 nl_attr_get_size(b_nest
));
7188 ip_min_specified
= true;
7190 case OVS_NAT_ATTR_IP_MAX
:
7191 memcpy(&nat_action_info
.max_addr
,
7192 nl_attr_get(b_nest
),
7193 nl_attr_get_size(b_nest
));
7194 ip_max_specified
= true;
7196 case OVS_NAT_ATTR_PROTO_MIN
:
7197 nat_action_info
.min_port
=
7198 nl_attr_get_u16(b_nest
);
7199 proto_num_min_specified
= true;
7201 case OVS_NAT_ATTR_PROTO_MAX
:
7202 nat_action_info
.max_port
=
7203 nl_attr_get_u16(b_nest
);
7204 proto_num_max_specified
= true;
7206 case OVS_NAT_ATTR_PERSISTENT
:
7207 case OVS_NAT_ATTR_PROTO_HASH
:
7208 case OVS_NAT_ATTR_PROTO_RANDOM
:
7210 case OVS_NAT_ATTR_UNSPEC
:
7211 case __OVS_NAT_ATTR_MAX
:
7216 if (ip_min_specified
&& !ip_max_specified
) {
7217 nat_action_info
.max_addr
= nat_action_info
.min_addr
;
7219 if (proto_num_min_specified
&& !proto_num_max_specified
) {
7220 nat_action_info
.max_port
= nat_action_info
.min_port
;
7222 if (proto_num_min_specified
|| proto_num_max_specified
) {
7223 if (nat_action_info
.nat_action
& NAT_ACTION_SRC
) {
7224 nat_action_info
.nat_action
|= NAT_ACTION_SRC_PORT
;
7225 } else if (nat_action_info
.nat_action
& NAT_ACTION_DST
) {
7226 nat_action_info
.nat_action
|= NAT_ACTION_DST_PORT
;
7231 case OVS_CT_ATTR_UNSPEC
:
7232 case __OVS_CT_ATTR_MAX
:
7237 /* We won't be able to function properly in this case, hence
7238 * complain loudly. */
7239 if (nat_config
&& !commit
) {
7240 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
7241 VLOG_WARN_RL(&rl
, "NAT specified without commit.");
7244 conntrack_execute(dp
->conntrack
, packets_
, aux
->flow
->dl_type
, force
,
7245 commit
, zone
, setmark
, setlabel
, aux
->flow
->tp_src
,
7246 aux
->flow
->tp_dst
, helper
, nat_action_info_ref
,
7247 pmd
->ctx
.now
/ 1000);
7251 case OVS_ACTION_ATTR_METER
:
7252 dp_netdev_run_meter(pmd
->dp
, packets_
, nl_attr_get_u32(a
),
7256 case OVS_ACTION_ATTR_PUSH_VLAN
:
7257 case OVS_ACTION_ATTR_POP_VLAN
:
7258 case OVS_ACTION_ATTR_PUSH_MPLS
:
7259 case OVS_ACTION_ATTR_POP_MPLS
:
7260 case OVS_ACTION_ATTR_SET
:
7261 case OVS_ACTION_ATTR_SET_MASKED
:
7262 case OVS_ACTION_ATTR_SAMPLE
:
7263 case OVS_ACTION_ATTR_HASH
:
7264 case OVS_ACTION_ATTR_UNSPEC
:
7265 case OVS_ACTION_ATTR_TRUNC
:
7266 case OVS_ACTION_ATTR_PUSH_ETH
:
7267 case OVS_ACTION_ATTR_POP_ETH
:
7268 case OVS_ACTION_ATTR_CLONE
:
7269 case OVS_ACTION_ATTR_PUSH_NSH
:
7270 case OVS_ACTION_ATTR_POP_NSH
:
7271 case OVS_ACTION_ATTR_CT_CLEAR
:
7272 case OVS_ACTION_ATTR_CHECK_PKT_LEN
:
7273 case __OVS_ACTION_ATTR_MAX
:
7277 dp_packet_delete_batch(packets_
, should_steal
);
7281 dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
7282 struct dp_packet_batch
*packets
,
7283 bool should_steal
, const struct flow
*flow
,
7284 const struct nlattr
*actions
, size_t actions_len
)
7286 struct dp_netdev_execute_aux aux
= { pmd
, flow
};
7288 odp_execute_actions(&aux
, packets
, should_steal
, actions
,
7289 actions_len
, dp_execute_cb
);
7292 struct dp_netdev_ct_dump
{
7293 struct ct_dpif_dump_state up
;
7294 struct conntrack_dump dump
;
7295 struct conntrack
*ct
;
7296 struct dp_netdev
*dp
;
7300 dpif_netdev_ct_dump_start(struct dpif
*dpif
, struct ct_dpif_dump_state
**dump_
,
7301 const uint16_t *pzone
, int *ptot_bkts
)
7303 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7304 struct dp_netdev_ct_dump
*dump
;
7306 dump
= xzalloc(sizeof *dump
);
7308 dump
->ct
= dp
->conntrack
;
7310 conntrack_dump_start(dp
->conntrack
, &dump
->dump
, pzone
, ptot_bkts
);
7318 dpif_netdev_ct_dump_next(struct dpif
*dpif OVS_UNUSED
,
7319 struct ct_dpif_dump_state
*dump_
,
7320 struct ct_dpif_entry
*entry
)
7322 struct dp_netdev_ct_dump
*dump
;
7324 INIT_CONTAINER(dump
, dump_
, up
);
7326 return conntrack_dump_next(&dump
->dump
, entry
);
7330 dpif_netdev_ct_dump_done(struct dpif
*dpif OVS_UNUSED
,
7331 struct ct_dpif_dump_state
*dump_
)
7333 struct dp_netdev_ct_dump
*dump
;
7336 INIT_CONTAINER(dump
, dump_
, up
);
7338 err
= conntrack_dump_done(&dump
->dump
);
7346 dpif_netdev_ct_flush(struct dpif
*dpif
, const uint16_t *zone
,
7347 const struct ct_dpif_tuple
*tuple
)
7349 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7352 return conntrack_flush_tuple(dp
->conntrack
, tuple
, zone
? *zone
: 0);
7354 return conntrack_flush(dp
->conntrack
, zone
);
7358 dpif_netdev_ct_set_maxconns(struct dpif
*dpif
, uint32_t maxconns
)
7360 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7362 return conntrack_set_maxconns(dp
->conntrack
, maxconns
);
7366 dpif_netdev_ct_get_maxconns(struct dpif
*dpif
, uint32_t *maxconns
)
7368 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7370 return conntrack_get_maxconns(dp
->conntrack
, maxconns
);
7374 dpif_netdev_ct_get_nconns(struct dpif
*dpif
, uint32_t *nconns
)
7376 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7378 return conntrack_get_nconns(dp
->conntrack
, nconns
);
7382 dpif_netdev_ipf_set_enabled(struct dpif
*dpif
, bool v6
, bool enable
)
7384 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7385 return ipf_set_enabled(conntrack_ipf_ctx(dp
->conntrack
), v6
, enable
);
7389 dpif_netdev_ipf_set_min_frag(struct dpif
*dpif
, bool v6
, uint32_t min_frag
)
7391 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7392 return ipf_set_min_frag(conntrack_ipf_ctx(dp
->conntrack
), v6
, min_frag
);
7396 dpif_netdev_ipf_set_max_nfrags(struct dpif
*dpif
, uint32_t max_frags
)
7398 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7399 return ipf_set_max_nfrags(conntrack_ipf_ctx(dp
->conntrack
), max_frags
);
7402 /* Adjust this function if 'dpif_ipf_status' and 'ipf_status' were to
7405 dpif_netdev_ipf_get_status(struct dpif
*dpif
,
7406 struct dpif_ipf_status
*dpif_ipf_status
)
7408 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7409 ipf_get_status(conntrack_ipf_ctx(dp
->conntrack
),
7410 (struct ipf_status
*) dpif_ipf_status
);
7415 dpif_netdev_ipf_dump_start(struct dpif
*dpif OVS_UNUSED
,
7416 struct ipf_dump_ctx
**ipf_dump_ctx
)
7418 return ipf_dump_start(ipf_dump_ctx
);
7422 dpif_netdev_ipf_dump_next(struct dpif
*dpif
, void *ipf_dump_ctx
, char **dump
)
7424 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7425 return ipf_dump_next(conntrack_ipf_ctx(dp
->conntrack
), ipf_dump_ctx
,
7430 dpif_netdev_ipf_dump_done(struct dpif
*dpif OVS_UNUSED
, void *ipf_dump_ctx
)
7432 return ipf_dump_done(ipf_dump_ctx
);
7436 const struct dpif_class dpif_netdev_class
= {
7438 true, /* cleanup_required */
7440 dpif_netdev_enumerate
,
7441 dpif_netdev_port_open_type
,
7444 dpif_netdev_destroy
,
7447 dpif_netdev_get_stats
,
7448 dpif_netdev_port_add
,
7449 dpif_netdev_port_del
,
7450 dpif_netdev_port_set_config
,
7451 dpif_netdev_port_query_by_number
,
7452 dpif_netdev_port_query_by_name
,
7453 NULL
, /* port_get_pid */
7454 dpif_netdev_port_dump_start
,
7455 dpif_netdev_port_dump_next
,
7456 dpif_netdev_port_dump_done
,
7457 dpif_netdev_port_poll
,
7458 dpif_netdev_port_poll_wait
,
7459 dpif_netdev_flow_flush
,
7460 dpif_netdev_flow_dump_create
,
7461 dpif_netdev_flow_dump_destroy
,
7462 dpif_netdev_flow_dump_thread_create
,
7463 dpif_netdev_flow_dump_thread_destroy
,
7464 dpif_netdev_flow_dump_next
,
7465 dpif_netdev_operate
,
7466 NULL
, /* recv_set */
7467 NULL
, /* handlers_set */
7468 dpif_netdev_set_config
,
7469 dpif_netdev_queue_to_priority
,
7471 NULL
, /* recv_wait */
7472 NULL
, /* recv_purge */
7473 dpif_netdev_register_dp_purge_cb
,
7474 dpif_netdev_register_upcall_cb
,
7475 dpif_netdev_enable_upcall
,
7476 dpif_netdev_disable_upcall
,
7477 dpif_netdev_get_datapath_version
,
7478 dpif_netdev_ct_dump_start
,
7479 dpif_netdev_ct_dump_next
,
7480 dpif_netdev_ct_dump_done
,
7481 dpif_netdev_ct_flush
,
7482 dpif_netdev_ct_set_maxconns
,
7483 dpif_netdev_ct_get_maxconns
,
7484 dpif_netdev_ct_get_nconns
,
7485 NULL
, /* ct_set_limits */
7486 NULL
, /* ct_get_limits */
7487 NULL
, /* ct_del_limits */
7488 dpif_netdev_ipf_set_enabled
,
7489 dpif_netdev_ipf_set_min_frag
,
7490 dpif_netdev_ipf_set_max_nfrags
,
7491 dpif_netdev_ipf_get_status
,
7492 dpif_netdev_ipf_dump_start
,
7493 dpif_netdev_ipf_dump_next
,
7494 dpif_netdev_ipf_dump_done
,
7495 dpif_netdev_meter_get_features
,
7496 dpif_netdev_meter_set
,
7497 dpif_netdev_meter_get
,
7498 dpif_netdev_meter_del
,
7502 dpif_dummy_change_port_number(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
7503 const char *argv
[], void *aux OVS_UNUSED
)
7505 struct dp_netdev_port
*port
;
7506 struct dp_netdev
*dp
;
7509 ovs_mutex_lock(&dp_netdev_mutex
);
7510 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
7511 if (!dp
|| !dpif_netdev_class_is_dummy(dp
->class)) {
7512 ovs_mutex_unlock(&dp_netdev_mutex
);
7513 unixctl_command_reply_error(conn
, "unknown datapath or not a dummy");
7516 ovs_refcount_ref(&dp
->ref_cnt
);
7517 ovs_mutex_unlock(&dp_netdev_mutex
);
7519 ovs_mutex_lock(&dp
->port_mutex
);
7520 if (get_port_by_name(dp
, argv
[2], &port
)) {
7521 unixctl_command_reply_error(conn
, "unknown port");
7525 port_no
= u32_to_odp(atoi(argv
[3]));
7526 if (!port_no
|| port_no
== ODPP_NONE
) {
7527 unixctl_command_reply_error(conn
, "bad port number");
7530 if (dp_netdev_lookup_port(dp
, port_no
)) {
7531 unixctl_command_reply_error(conn
, "port number already in use");
7536 hmap_remove(&dp
->ports
, &port
->node
);
7537 reconfigure_datapath(dp
);
7539 /* Reinsert with new port number. */
7540 port
->port_no
= port_no
;
7541 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
7542 reconfigure_datapath(dp
);
7544 seq_change(dp
->port_seq
);
7545 unixctl_command_reply(conn
, NULL
);
7548 ovs_mutex_unlock(&dp
->port_mutex
);
7549 dp_netdev_unref(dp
);
7553 dpif_dummy_register__(const char *type
)
7555 struct dpif_class
*class;
7557 class = xmalloc(sizeof *class);
7558 *class = dpif_netdev_class
;
7559 class->type
= xstrdup(type
);
7560 dp_register_provider(class);
7564 dpif_dummy_override(const char *type
)
7569 * Ignore EAFNOSUPPORT to allow --enable-dummy=system with
7570 * a userland-only build. It's useful for testsuite.
7572 error
= dp_unregister_provider(type
);
7573 if (error
== 0 || error
== EAFNOSUPPORT
) {
7574 dpif_dummy_register__(type
);
7579 dpif_dummy_register(enum dummy_level level
)
7581 if (level
== DUMMY_OVERRIDE_ALL
) {
7586 dp_enumerate_types(&types
);
7587 SSET_FOR_EACH (type
, &types
) {
7588 dpif_dummy_override(type
);
7590 sset_destroy(&types
);
7591 } else if (level
== DUMMY_OVERRIDE_SYSTEM
) {
7592 dpif_dummy_override("system");
7595 dpif_dummy_register__("dummy");
7597 unixctl_command_register("dpif-dummy/change-port-number",
7598 "dp port new-number",
7599 3, 3, dpif_dummy_change_port_number
, NULL
);
7602 /* Datapath Classifier. */
7604 /* A set of rules that all have the same fields wildcarded. */
7605 struct dpcls_subtable
{
7606 /* The fields are only used by writers. */
7607 struct cmap_node cmap_node OVS_GUARDED
; /* Within dpcls 'subtables_map'. */
7609 /* These fields are accessed by readers. */
7610 struct cmap rules
; /* Contains "struct dpcls_rule"s. */
7611 uint32_t hit_cnt
; /* Number of match hits in subtable in current
7612 optimization interval. */
7613 struct netdev_flow_key mask
; /* Wildcards for fields (const). */
7614 /* 'mask' must be the last field, additional space is allocated here. */
7618 dpcls_subtable_destroy_cb(struct dpcls_subtable
*subtable
)
7620 cmap_destroy(&subtable
->rules
);
7621 ovsrcu_postpone(free
, subtable
);
7624 /* Initializes 'cls' as a classifier that initially contains no classification
7627 dpcls_init(struct dpcls
*cls
)
7629 cmap_init(&cls
->subtables_map
);
7630 pvector_init(&cls
->subtables
);
7634 dpcls_destroy_subtable(struct dpcls
*cls
, struct dpcls_subtable
*subtable
)
7636 VLOG_DBG("Destroying subtable %p for in_port %d", subtable
, cls
->in_port
);
7637 pvector_remove(&cls
->subtables
, subtable
);
7638 cmap_remove(&cls
->subtables_map
, &subtable
->cmap_node
,
7639 subtable
->mask
.hash
);
7640 ovsrcu_postpone(dpcls_subtable_destroy_cb
, subtable
);
7643 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
7644 * caller's responsibility.
7645 * May only be called after all the readers have been terminated. */
7647 dpcls_destroy(struct dpcls
*cls
)
7650 struct dpcls_subtable
*subtable
;
7652 CMAP_FOR_EACH (subtable
, cmap_node
, &cls
->subtables_map
) {
7653 ovs_assert(cmap_count(&subtable
->rules
) == 0);
7654 dpcls_destroy_subtable(cls
, subtable
);
7656 cmap_destroy(&cls
->subtables_map
);
7657 pvector_destroy(&cls
->subtables
);
7661 static struct dpcls_subtable
*
7662 dpcls_create_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
7664 struct dpcls_subtable
*subtable
;
7666 /* Need to add one. */
7667 subtable
= xmalloc(sizeof *subtable
7668 - sizeof subtable
->mask
.mf
+ mask
->len
);
7669 cmap_init(&subtable
->rules
);
7670 subtable
->hit_cnt
= 0;
7671 netdev_flow_key_clone(&subtable
->mask
, mask
);
7672 cmap_insert(&cls
->subtables_map
, &subtable
->cmap_node
, mask
->hash
);
7673 /* Add the new subtable at the end of the pvector (with no hits yet) */
7674 pvector_insert(&cls
->subtables
, subtable
, 0);
7675 VLOG_DBG("Creating %"PRIuSIZE
". subtable %p for in_port %d",
7676 cmap_count(&cls
->subtables_map
), subtable
, cls
->in_port
);
7677 pvector_publish(&cls
->subtables
);
7682 static inline struct dpcls_subtable
*
7683 dpcls_find_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
7685 struct dpcls_subtable
*subtable
;
7687 CMAP_FOR_EACH_WITH_HASH (subtable
, cmap_node
, mask
->hash
,
7688 &cls
->subtables_map
) {
7689 if (netdev_flow_key_equal(&subtable
->mask
, mask
)) {
7693 return dpcls_create_subtable(cls
, mask
);
7697 /* Periodically sort the dpcls subtable vectors according to hit counts */
7699 dpcls_sort_subtable_vector(struct dpcls
*cls
)
7701 struct pvector
*pvec
= &cls
->subtables
;
7702 struct dpcls_subtable
*subtable
;
7704 PVECTOR_FOR_EACH (subtable
, pvec
) {
7705 pvector_change_priority(pvec
, subtable
, subtable
->hit_cnt
);
7706 subtable
->hit_cnt
= 0;
7708 pvector_publish(pvec
);
7712 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread
*pmd
,
7713 struct polled_queue
*poll_list
, int poll_cnt
)
7716 uint64_t tot_idle
= 0, tot_proc
= 0;
7717 unsigned int pmd_load
= 0;
7719 if (pmd
->ctx
.now
> pmd
->rxq_next_cycle_store
) {
7721 struct pmd_auto_lb
*pmd_alb
= &pmd
->dp
->pmd_alb
;
7722 if (pmd_alb
->is_enabled
&& !pmd
->isolated
7723 && (pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_IDLE
] >=
7724 pmd
->prev_stats
[PMD_CYCLES_ITER_IDLE
])
7725 && (pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_BUSY
] >=
7726 pmd
->prev_stats
[PMD_CYCLES_ITER_BUSY
]))
7728 tot_idle
= pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_IDLE
] -
7729 pmd
->prev_stats
[PMD_CYCLES_ITER_IDLE
];
7730 tot_proc
= pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_BUSY
] -
7731 pmd
->prev_stats
[PMD_CYCLES_ITER_BUSY
];
7734 pmd_load
= ((tot_proc
* 100) / (tot_idle
+ tot_proc
));
7737 if (pmd_load
>= ALB_PMD_LOAD_THRESHOLD
) {
7738 atomic_count_inc(&pmd
->pmd_overloaded
);
7740 atomic_count_set(&pmd
->pmd_overloaded
, 0);
7744 pmd
->prev_stats
[PMD_CYCLES_ITER_IDLE
] =
7745 pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_IDLE
];
7746 pmd
->prev_stats
[PMD_CYCLES_ITER_BUSY
] =
7747 pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_BUSY
];
7749 /* Get the cycles that were used to process each queue and store. */
7750 for (unsigned i
= 0; i
< poll_cnt
; i
++) {
7751 uint64_t rxq_cyc_curr
= dp_netdev_rxq_get_cycles(poll_list
[i
].rxq
,
7752 RXQ_CYCLES_PROC_CURR
);
7753 dp_netdev_rxq_set_intrvl_cycles(poll_list
[i
].rxq
, rxq_cyc_curr
);
7754 dp_netdev_rxq_set_cycles(poll_list
[i
].rxq
, RXQ_CYCLES_PROC_CURR
,
7757 curr_tsc
= cycles_counter_update(&pmd
->perf_stats
);
7758 if (pmd
->intrvl_tsc_prev
) {
7759 /* There is a prev timestamp, store a new intrvl cycle count. */
7760 atomic_store_relaxed(&pmd
->intrvl_cycles
,
7761 curr_tsc
- pmd
->intrvl_tsc_prev
);
7763 pmd
->intrvl_tsc_prev
= curr_tsc
;
7764 /* Start new measuring interval */
7765 pmd
->rxq_next_cycle_store
= pmd
->ctx
.now
+ PMD_RXQ_INTERVAL_LEN
;
7768 if (pmd
->ctx
.now
> pmd
->next_optimization
) {
7769 /* Try to obtain the flow lock to block out revalidator threads.
7770 * If not possible, just try next time. */
7771 if (!ovs_mutex_trylock(&pmd
->flow_mutex
)) {
7772 /* Optimize each classifier */
7773 CMAP_FOR_EACH (cls
, node
, &pmd
->classifiers
) {
7774 dpcls_sort_subtable_vector(cls
);
7776 ovs_mutex_unlock(&pmd
->flow_mutex
);
7777 /* Start new measuring interval */
7778 pmd
->next_optimization
= pmd
->ctx
.now
7779 + DPCLS_OPTIMIZATION_INTERVAL
;
7784 /* Insert 'rule' into 'cls'. */
7786 dpcls_insert(struct dpcls
*cls
, struct dpcls_rule
*rule
,
7787 const struct netdev_flow_key
*mask
)
7789 struct dpcls_subtable
*subtable
= dpcls_find_subtable(cls
, mask
);
7791 /* Refer to subtable's mask, also for later removal. */
7792 rule
->mask
= &subtable
->mask
;
7793 cmap_insert(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
);
7796 /* Removes 'rule' from 'cls', also destructing the 'rule'. */
7798 dpcls_remove(struct dpcls
*cls
, struct dpcls_rule
*rule
)
7800 struct dpcls_subtable
*subtable
;
7802 ovs_assert(rule
->mask
);
7804 /* Get subtable from reference in rule->mask. */
7805 INIT_CONTAINER(subtable
, rule
->mask
, mask
);
7806 if (cmap_remove(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
)
7808 /* Delete empty subtable. */
7809 dpcls_destroy_subtable(cls
, subtable
);
7810 pvector_publish(&cls
->subtables
);
7814 /* Returns true if 'target' satisfies 'key' in 'mask', that is, if each 1-bit
7815 * in 'mask' the values in 'key' and 'target' are the same. */
7817 dpcls_rule_matches_key(const struct dpcls_rule
*rule
,
7818 const struct netdev_flow_key
*target
)
7820 const uint64_t *keyp
= miniflow_get_values(&rule
->flow
.mf
);
7821 const uint64_t *maskp
= miniflow_get_values(&rule
->mask
->mf
);
7824 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, target
, rule
->flow
.mf
.map
) {
7825 if (OVS_UNLIKELY((value
& *maskp
++) != *keyp
++)) {
7832 /* For each miniflow in 'keys' performs a classifier lookup writing the result
7833 * into the corresponding slot in 'rules'. If a particular entry in 'keys' is
7834 * NULL it is skipped.
7836 * This function is optimized for use in the userspace datapath and therefore
7837 * does not implement a lot of features available in the standard
7838 * classifier_lookup() function. Specifically, it does not implement
7839 * priorities, instead returning any rule which matches the flow.
7841 * Returns true if all miniflows found a corresponding rule. */
7843 dpcls_lookup(struct dpcls
*cls
, const struct netdev_flow_key
*keys
[],
7844 struct dpcls_rule
**rules
, const size_t cnt
,
7847 /* The received 'cnt' miniflows are the search-keys that will be processed
7848 * to find a matching entry into the available subtables.
7849 * The number of bits in map_type is equal to NETDEV_MAX_BURST. */
7850 typedef uint32_t map_type
;
7851 #define MAP_BITS (sizeof(map_type) * CHAR_BIT)
7852 BUILD_ASSERT_DECL(MAP_BITS
>= NETDEV_MAX_BURST
);
7854 struct dpcls_subtable
*subtable
;
7856 map_type keys_map
= TYPE_MAXIMUM(map_type
); /* Set all bits. */
7858 uint32_t hashes
[MAP_BITS
];
7859 const struct cmap_node
*nodes
[MAP_BITS
];
7861 if (cnt
!= MAP_BITS
) {
7862 keys_map
>>= MAP_BITS
- cnt
; /* Clear extra bits. */
7864 memset(rules
, 0, cnt
* sizeof *rules
);
7866 int lookups_match
= 0, subtable_pos
= 1;
7868 /* The Datapath classifier - aka dpcls - is composed of subtables.
7869 * Subtables are dynamically created as needed when new rules are inserted.
7870 * Each subtable collects rules with matches on a specific subset of packet
7871 * fields as defined by the subtable's mask. We proceed to process every
7872 * search-key against each subtable, but when a match is found for a
7873 * search-key, the search for that key can stop because the rules are
7874 * non-overlapping. */
7875 PVECTOR_FOR_EACH (subtable
, &cls
->subtables
) {
7878 /* Compute hashes for the remaining keys. Each search-key is
7879 * masked with the subtable's mask to avoid hashing the wildcarded
7881 ULLONG_FOR_EACH_1(i
, keys_map
) {
7882 hashes
[i
] = netdev_flow_key_hash_in_mask(keys
[i
],
7886 found_map
= cmap_find_batch(&subtable
->rules
, keys_map
, hashes
, nodes
);
7887 /* Check results. When the i-th bit of found_map is set, it means
7888 * that a set of nodes with a matching hash value was found for the
7889 * i-th search-key. Due to possible hash collisions we need to check
7890 * which of the found rules, if any, really matches our masked
7892 ULLONG_FOR_EACH_1(i
, found_map
) {
7893 struct dpcls_rule
*rule
;
7895 CMAP_NODE_FOR_EACH (rule
, cmap_node
, nodes
[i
]) {
7896 if (OVS_LIKELY(dpcls_rule_matches_key(rule
, keys
[i
]))) {
7898 /* Even at 20 Mpps the 32-bit hit_cnt cannot wrap
7899 * within one second optimization interval. */
7900 subtable
->hit_cnt
++;
7901 lookups_match
+= subtable_pos
;
7905 /* None of the found rules was a match. Reset the i-th bit to
7906 * keep searching this key in the next subtable. */
7907 ULLONG_SET0(found_map
, i
); /* Did not match. */
7909 ; /* Keep Sparse happy. */
7911 keys_map
&= ~found_map
; /* Clear the found rules. */
7913 if (num_lookups_p
) {
7914 *num_lookups_p
= lookups_match
;
7916 return true; /* All found. */
7920 if (num_lookups_p
) {
7921 *num_lookups_p
= lookups_match
;
7923 return false; /* Some misses. */