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"
19 #include "dpif-netdev-private.h"
26 #include <sys/types.h>
27 #include <netinet/in.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
38 #include "conntrack.h"
39 #include "conntrack-tp.h"
43 #include "dp-packet.h"
45 #include "dpif-netdev-lookup.h"
46 #include "dpif-netdev-perf.h"
47 #include "dpif-provider.h"
49 #include "fat-rwlock.h"
55 #include "netdev-offload.h"
56 #include "netdev-provider.h"
57 #include "netdev-vport.h"
59 #include "odp-execute.h"
61 #include "openvswitch/dynamic-string.h"
62 #include "openvswitch/list.h"
63 #include "openvswitch/match.h"
64 #include "openvswitch/ofp-parse.h"
65 #include "openvswitch/ofp-print.h"
66 #include "openvswitch/ofpbuf.h"
67 #include "openvswitch/shash.h"
68 #include "openvswitch/vlog.h"
72 #include "openvswitch/poll-loop.h"
79 #include "tnl-neigh-cache.h"
80 #include "tnl-ports.h"
85 VLOG_DEFINE_THIS_MODULE(dpif_netdev
);
87 /* Auto Load Balancing Defaults */
88 #define ALB_ACCEPTABLE_IMPROVEMENT 25
89 #define ALB_PMD_LOAD_THRESHOLD 95
90 #define ALB_PMD_REBALANCE_POLL_INTERVAL 1 /* 1 Min */
91 #define MIN_TO_MSEC 60000
93 #define FLOW_DUMP_MAX_BATCH 50
94 /* Use per thread recirc_depth to prevent recirculation loop. */
95 #define MAX_RECIRC_DEPTH 6
96 DEFINE_STATIC_PER_THREAD_DATA(uint32_t, recirc_depth
, 0)
98 /* Use instant packet send by default. */
99 #define DEFAULT_TX_FLUSH_INTERVAL 0
101 /* Configuration parameters. */
102 enum { MAX_METERS
= 65536 }; /* Maximum number of meters. */
103 enum { MAX_BANDS
= 8 }; /* Maximum number of bands / meter. */
104 enum { N_METER_LOCKS
= 64 }; /* Maximum number of meters. */
106 COVERAGE_DEFINE(datapath_drop_meter
);
107 COVERAGE_DEFINE(datapath_drop_upcall_error
);
108 COVERAGE_DEFINE(datapath_drop_lock_error
);
109 COVERAGE_DEFINE(datapath_drop_userspace_action_error
);
110 COVERAGE_DEFINE(datapath_drop_tunnel_push_error
);
111 COVERAGE_DEFINE(datapath_drop_tunnel_pop_error
);
112 COVERAGE_DEFINE(datapath_drop_recirc_error
);
113 COVERAGE_DEFINE(datapath_drop_invalid_port
);
114 COVERAGE_DEFINE(datapath_drop_invalid_bond
);
115 COVERAGE_DEFINE(datapath_drop_invalid_tnl_port
);
116 COVERAGE_DEFINE(datapath_drop_rx_invalid_packet
);
118 /* Protects against changes to 'dp_netdevs'. */
119 static struct ovs_mutex dp_netdev_mutex
= OVS_MUTEX_INITIALIZER
;
121 /* Contains all 'struct dp_netdev's. */
122 static struct shash dp_netdevs
OVS_GUARDED_BY(dp_netdev_mutex
)
123 = SHASH_INITIALIZER(&dp_netdevs
);
125 static struct vlog_rate_limit upcall_rl
= VLOG_RATE_LIMIT_INIT(600, 600);
127 #define DP_NETDEV_CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \
128 | CS_INVALID | CS_REPLY_DIR | CS_TRACKED \
129 | CS_SRC_NAT | CS_DST_NAT)
130 #define DP_NETDEV_CS_UNSUPPORTED_MASK (~(uint32_t)DP_NETDEV_CS_SUPPORTED_MASK)
132 static struct odp_support dp_netdev_support
= {
133 .max_vlan_headers
= SIZE_MAX
,
134 .max_mpls_depth
= SIZE_MAX
,
140 .ct_state_nat
= true,
141 .ct_orig_tuple
= true,
142 .ct_orig_tuple6
= true,
145 /* EMC cache and SMC cache compose the datapath flow cache (DFC)
147 * Exact match cache for frequently used flows
149 * The cache uses a 32-bit hash of the packet (which can be the RSS hash) to
150 * search its entries for a miniflow that matches exactly the miniflow of the
151 * packet. It stores the 'dpcls_rule' (rule) that matches the miniflow.
153 * A cache entry holds a reference to its 'dp_netdev_flow'.
155 * A miniflow with a given hash can be in one of EM_FLOW_HASH_SEGS different
156 * entries. The 32-bit hash is split into EM_FLOW_HASH_SEGS values (each of
157 * them is EM_FLOW_HASH_SHIFT bits wide and the remainder is thrown away). Each
158 * value is the index of a cache entry where the miniflow could be.
161 * Signature match cache (SMC)
163 * This cache stores a 16-bit signature for each flow without storing keys, and
164 * stores the corresponding 16-bit flow_table index to the 'dp_netdev_flow'.
165 * Each flow thus occupies 32bit which is much more memory efficient than EMC.
166 * SMC uses a set-associative design that each bucket contains
167 * SMC_ENTRY_PER_BUCKET number of entries.
168 * Since 16-bit flow_table index is used, if there are more than 2^16
169 * dp_netdev_flow, SMC will miss them that cannot be indexed by a 16-bit value.
175 * Each pmd_thread has its own private exact match cache.
176 * If dp_netdev_input is not called from a pmd thread, a mutex is used.
179 #define EM_FLOW_HASH_SHIFT 13
180 #define EM_FLOW_HASH_ENTRIES (1u << EM_FLOW_HASH_SHIFT)
181 #define EM_FLOW_HASH_MASK (EM_FLOW_HASH_ENTRIES - 1)
182 #define EM_FLOW_HASH_SEGS 2
184 /* SMC uses a set-associative design. A bucket contains a set of entries that
185 * a flow item can occupy. For now, it uses one hash function rather than two
186 * as for the EMC design. */
187 #define SMC_ENTRY_PER_BUCKET 4
188 #define SMC_ENTRIES (1u << 20)
189 #define SMC_BUCKET_CNT (SMC_ENTRIES / SMC_ENTRY_PER_BUCKET)
190 #define SMC_MASK (SMC_BUCKET_CNT - 1)
192 /* Default EMC insert probability is 1 / DEFAULT_EM_FLOW_INSERT_INV_PROB */
193 #define DEFAULT_EM_FLOW_INSERT_INV_PROB 100
194 #define DEFAULT_EM_FLOW_INSERT_MIN (UINT32_MAX / \
195 DEFAULT_EM_FLOW_INSERT_INV_PROB)
198 struct dp_netdev_flow
*flow
;
199 struct netdev_flow_key key
; /* key.hash used for emc hash value. */
203 struct emc_entry entries
[EM_FLOW_HASH_ENTRIES
];
204 int sweep_idx
; /* For emc_cache_slow_sweep(). */
208 uint16_t sig
[SMC_ENTRY_PER_BUCKET
];
209 uint16_t flow_idx
[SMC_ENTRY_PER_BUCKET
];
212 /* Signature match cache, differentiate from EMC cache */
214 struct smc_bucket buckets
[SMC_BUCKET_CNT
];
218 struct emc_cache emc_cache
;
219 struct smc_cache smc_cache
;
222 /* Iterate in the exact match cache through every entry that might contain a
223 * miniflow with hash 'HASH'. */
224 #define EMC_FOR_EACH_POS_WITH_HASH(EMC, CURRENT_ENTRY, HASH) \
225 for (uint32_t i__ = 0, srch_hash__ = (HASH); \
226 (CURRENT_ENTRY) = &(EMC)->entries[srch_hash__ & EM_FLOW_HASH_MASK], \
227 i__ < EM_FLOW_HASH_SEGS; \
228 i__++, srch_hash__ >>= EM_FLOW_HASH_SHIFT)
230 /* Simple non-wildcarding single-priority classifier. */
232 /* Time in microseconds between successive optimizations of the dpcls
234 #define DPCLS_OPTIMIZATION_INTERVAL 1000000LL
236 /* Time in microseconds of the interval in which rxq processing cycles used
237 * in rxq to pmd assignments is measured and stored. */
238 #define PMD_RXQ_INTERVAL_LEN 10000000LL
240 /* Number of intervals for which cycles are stored
241 * and used during rxq to pmd assignment. */
242 #define PMD_RXQ_INTERVAL_MAX 6
244 /* Time in microseconds to try RCU quiescing. */
245 #define PMD_RCU_QUIESCE_INTERVAL 10000LL
248 struct cmap_node node
; /* Within dp_netdev_pmd_thread.classifiers */
250 struct cmap subtables_map
;
251 struct pvector subtables
;
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 uint32_t dpcls_subtable_lookup_reprobe(struct dpcls
*cls
);
265 static void dpcls_insert(struct dpcls
*, struct dpcls_rule
*,
266 const struct netdev_flow_key
*mask
);
267 static void dpcls_remove(struct dpcls
*, struct dpcls_rule
*);
268 static bool dpcls_lookup(struct dpcls
*cls
,
269 const struct netdev_flow_key
*keys
[],
270 struct dpcls_rule
**rules
, size_t cnt
,
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)
322 const struct dpif_class
*const class;
323 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 struct ovs_mutex bond_mutex
; /* Protects updates of 'tx_bonds'. */
389 struct cmap tx_bonds
; /* Contains 'struct tx_bond'. */
392 static void meter_lock(const struct dp_netdev
*dp
, uint32_t meter_id
)
393 OVS_ACQUIRES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
395 ovs_mutex_lock(&dp
->meter_locks
[meter_id
% N_METER_LOCKS
]);
398 static void meter_unlock(const struct dp_netdev
*dp
, uint32_t meter_id
)
399 OVS_RELEASES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
401 ovs_mutex_unlock(&dp
->meter_locks
[meter_id
% N_METER_LOCKS
]);
405 static struct dp_netdev_port
*dp_netdev_lookup_port(const struct dp_netdev
*dp
,
407 OVS_REQUIRES(dp
->port_mutex
);
409 enum rxq_cycles_counter_type
{
410 RXQ_CYCLES_PROC_CURR
, /* Cycles spent successfully polling and
411 processing packets during the current
413 RXQ_CYCLES_PROC_HIST
, /* Total cycles of all intervals that are used
414 during rxq to pmd assignment. */
419 DP_NETDEV_FLOW_OFFLOAD_OP_ADD
,
420 DP_NETDEV_FLOW_OFFLOAD_OP_MOD
,
421 DP_NETDEV_FLOW_OFFLOAD_OP_DEL
,
424 struct dp_flow_offload_item
{
425 struct dp_netdev_pmd_thread
*pmd
;
426 struct dp_netdev_flow
*flow
;
429 struct nlattr
*actions
;
432 struct ovs_list node
;
435 struct dp_flow_offload
{
436 struct ovs_mutex mutex
;
437 struct ovs_list list
;
441 static struct dp_flow_offload dp_flow_offload
= {
442 .mutex
= OVS_MUTEX_INITIALIZER
,
443 .list
= OVS_LIST_INITIALIZER(&dp_flow_offload
.list
),
446 static struct ovsthread_once offload_thread_once
447 = OVSTHREAD_ONCE_INITIALIZER
;
449 #define XPS_TIMEOUT 500000LL /* In microseconds. */
451 /* Contained by struct dp_netdev_port's 'rxqs' member. */
452 struct dp_netdev_rxq
{
453 struct dp_netdev_port
*port
;
454 struct netdev_rxq
*rx
;
455 unsigned core_id
; /* Core to which this queue should be
456 pinned. OVS_CORE_UNSPEC if the
457 queue doesn't need to be pinned to a
459 unsigned intrvl_idx
; /* Write index for 'cycles_intrvl'. */
460 struct dp_netdev_pmd_thread
*pmd
; /* pmd thread that polls this queue. */
461 bool is_vhost
; /* Is rxq of a vhost port. */
463 /* Counters of cycles spent successfully polling and processing pkts. */
464 atomic_ullong cycles
[RXQ_N_CYCLES
];
465 /* We store PMD_RXQ_INTERVAL_MAX intervals of data for an rxq and then
466 sum them to yield the cycles used for an rxq. */
467 atomic_ullong cycles_intrvl
[PMD_RXQ_INTERVAL_MAX
];
470 /* A port in a netdev-based datapath. */
471 struct dp_netdev_port
{
473 bool dynamic_txqs
; /* If true XPS will be used. */
474 bool need_reconfigure
; /* True if we should reconfigure netdev. */
475 struct netdev
*netdev
;
476 struct hmap_node node
; /* Node in dp_netdev's 'ports'. */
477 struct netdev_saved_flags
*sf
;
478 struct dp_netdev_rxq
*rxqs
;
479 unsigned n_rxq
; /* Number of elements in 'rxqs' */
480 unsigned *txq_used
; /* Number of threads that use each tx queue. */
481 struct ovs_mutex txq_used_mutex
;
482 bool emc_enabled
; /* If true EMC will be used. */
483 char *type
; /* Port type as requested by user. */
484 char *rxq_affinity_list
; /* Requested affinity of rx queues. */
487 /* Contained by struct dp_netdev_flow's 'stats' member. */
488 struct dp_netdev_flow_stats
{
489 atomic_llong used
; /* Last used time, in monotonic msecs. */
490 atomic_ullong packet_count
; /* Number of packets matched. */
491 atomic_ullong byte_count
; /* Number of bytes matched. */
492 atomic_uint16_t tcp_flags
; /* Bitwise-OR of seen tcp_flags values. */
495 /* Contained by struct dp_netdev_flow's 'last_attrs' member. */
496 struct dp_netdev_flow_attrs
{
497 atomic_bool offloaded
; /* True if flow is offloaded to HW. */
498 ATOMIC(const char *) dp_layer
; /* DP layer the flow is handled in. */
501 /* A flow in 'dp_netdev_pmd_thread's 'flow_table'.
507 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
508 * its pmd thread's classifier. The text below calls this classifier 'cls'.
513 * The thread safety rules described here for "struct dp_netdev_flow" are
514 * motivated by two goals:
516 * - Prevent threads that read members of "struct dp_netdev_flow" from
517 * reading bad data due to changes by some thread concurrently modifying
520 * - Prevent two threads making changes to members of a given "struct
521 * dp_netdev_flow" from interfering with each other.
527 * A flow 'flow' may be accessed without a risk of being freed during an RCU
528 * grace period. Code that needs to hold onto a flow for a while
529 * should try incrementing 'flow->ref_cnt' with dp_netdev_flow_ref().
531 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
532 * flow from being deleted from 'cls' and it doesn't protect members of 'flow'
535 * Some members, marked 'const', are immutable. Accessing other members
536 * requires synchronization, as noted in more detail below.
538 struct dp_netdev_flow
{
539 const struct flow flow
; /* Unmasked flow that created this entry. */
540 /* Hash table index by unmasked flow. */
541 const struct cmap_node node
; /* In owning dp_netdev_pmd_thread's */
543 const struct cmap_node mark_node
; /* In owning flow_mark's mark_to_flow */
544 const ovs_u128 ufid
; /* Unique flow identifier. */
545 const ovs_u128 mega_ufid
; /* Unique mega flow identifier. */
546 const unsigned pmd_id
; /* The 'core_id' of pmd thread owning this */
549 /* Number of references.
550 * The classifier owns one reference.
551 * Any thread trying to keep a rule from being freed should hold its own
553 struct ovs_refcount ref_cnt
;
556 uint32_t mark
; /* Unique flow mark assigned to a flow */
559 struct dp_netdev_flow_stats stats
;
561 /* Statistics and attributes received from the netdev offload provider. */
562 atomic_int netdev_flow_get_result
;
563 struct dp_netdev_flow_stats last_stats
;
564 struct dp_netdev_flow_attrs last_attrs
;
567 OVSRCU_TYPE(struct dp_netdev_actions
*) actions
;
569 /* While processing a group of input packets, the datapath uses the next
570 * member to store a pointer to the output batch for the flow. It is
571 * reset after the batch has been sent out (See dp_netdev_queue_batches(),
572 * packet_batch_per_flow_init() and packet_batch_per_flow_execute()). */
573 struct packet_batch_per_flow
*batch
;
575 /* Packet classification. */
576 char *dp_extra_info
; /* String to return in a flow dump/get. */
577 struct dpcls_rule cr
; /* In owning dp_netdev's 'cls'. */
578 /* 'cr' must be the last member. */
581 static void dp_netdev_flow_unref(struct dp_netdev_flow
*);
582 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*);
583 static int dpif_netdev_flow_from_nlattrs(const struct nlattr
*, uint32_t,
584 struct flow
*, bool);
586 /* A set of datapath actions within a "struct dp_netdev_flow".
592 * A struct dp_netdev_actions 'actions' is protected with RCU. */
593 struct dp_netdev_actions
{
594 /* These members are immutable: they do not change during the struct's
596 unsigned int size
; /* Size of 'actions', in bytes. */
597 struct nlattr actions
[]; /* Sequence of OVS_ACTION_ATTR_* attributes. */
600 struct dp_netdev_actions
*dp_netdev_actions_create(const struct nlattr
*,
602 struct dp_netdev_actions
*dp_netdev_flow_get_actions(
603 const struct dp_netdev_flow
*);
604 static void dp_netdev_actions_free(struct dp_netdev_actions
*);
606 struct polled_queue
{
607 struct dp_netdev_rxq
*rxq
;
614 /* Contained by struct dp_netdev_pmd_thread's 'poll_list' member. */
616 struct dp_netdev_rxq
*rxq
;
617 struct hmap_node node
;
620 /* Contained by struct dp_netdev_pmd_thread's 'send_port_cache',
621 * 'tnl_port_cache' or 'tx_ports'. */
623 struct dp_netdev_port
*port
;
626 struct hmap_node node
;
627 long long flush_time
;
628 struct dp_packet_batch output_pkts
;
629 struct dp_netdev_rxq
*output_pkts_rxqs
[NETDEV_MAX_BURST
];
632 /* Contained by struct tx_bond 'slave_buckets'. */
635 atomic_ullong n_packets
;
636 atomic_ullong n_bytes
;
639 /* Contained by struct dp_netdev_pmd_thread's 'tx_bonds'. */
641 struct cmap_node node
;
643 struct slave_entry slave_buckets
[BOND_BUCKETS
];
646 /* A set of properties for the current processing loop that is not directly
647 * associated with the pmd thread itself, but with the packets being
648 * processed or the short-term system configuration (for example, time).
649 * Contained by struct dp_netdev_pmd_thread's 'ctx' member. */
650 struct dp_netdev_pmd_thread_ctx
{
651 /* Latest measured time. See 'pmd_thread_ctx_time_update()'. */
653 /* RX queue from which last packet was received. */
654 struct dp_netdev_rxq
*last_rxq
;
655 /* EMC insertion probability context for the current processing cycle. */
656 uint32_t emc_insert_min
;
659 /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
660 * the performance overhead of interrupt processing. Therefore netdev can
661 * not implement rx-wait for these devices. dpif-netdev needs to poll
662 * these device to check for recv buffer. pmd-thread does polling for
663 * devices assigned to itself.
665 * DPDK used PMD for accessing NIC.
667 * Note, instance with cpu core id NON_PMD_CORE_ID will be reserved for
668 * I/O of all non-pmd threads. There will be no actual thread created
671 * Each struct has its own flow cache and classifier per managed ingress port.
672 * For packets received on ingress port, a look up is done on corresponding PMD
673 * thread's flow cache and in case of a miss, lookup is performed in the
674 * corresponding classifier of port. Packets are executed with the found
675 * actions in either case.
677 struct dp_netdev_pmd_thread
{
678 struct dp_netdev
*dp
;
679 struct ovs_refcount ref_cnt
; /* Every reference must be refcount'ed. */
680 struct cmap_node node
; /* In 'dp->poll_threads'. */
682 /* Per thread exact-match cache. Note, the instance for cpu core
683 * NON_PMD_CORE_ID can be accessed by multiple threads, and thusly
684 * need to be protected by 'non_pmd_mutex'. Every other instance
685 * will only be accessed by its own pmd thread. */
686 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
) struct dfc_cache flow_cache
;
688 /* Flow-Table and classifiers
690 * Writers of 'flow_table' must take the 'flow_mutex'. Corresponding
691 * changes to 'classifiers' must be made while still holding the
694 struct ovs_mutex flow_mutex
;
695 struct cmap flow_table OVS_GUARDED
; /* Flow table. */
697 /* One classifier per in_port polled by the pmd */
698 struct cmap classifiers
;
699 /* Periodically sort subtable vectors according to hit frequencies */
700 long long int next_optimization
;
701 /* End of the next time interval for which processing cycles
702 are stored for each polled rxq. */
703 long long int rxq_next_cycle_store
;
705 /* Last interval timestamp. */
706 uint64_t intrvl_tsc_prev
;
707 /* Last interval cycles. */
708 atomic_ullong intrvl_cycles
;
710 /* Current context of the PMD thread. */
711 struct dp_netdev_pmd_thread_ctx ctx
;
713 struct seq
*reload_seq
;
714 uint64_t last_reload_seq
;
716 /* These are atomic variables used as a synchronization and configuration
717 * points for thread reload/exit.
719 * 'reload' atomic is the main one and it's used as a memory
720 * synchronization point for all other knobs and data.
722 * For a thread that requests PMD reload:
724 * * All changes that should be visible to the PMD thread must be made
725 * before setting the 'reload'. These changes could use any memory
726 * ordering model including 'relaxed'.
727 * * Setting the 'reload' atomic should occur in the same thread where
728 * all other PMD configuration options updated.
729 * * Setting the 'reload' atomic should be done with 'release' memory
730 * ordering model or stricter. This will guarantee that all previous
731 * changes (including non-atomic and 'relaxed') will be visible to
733 * * To check that reload is done, thread should poll the 'reload' atomic
734 * to become 'false'. Polling should be done with 'acquire' memory
735 * ordering model or stricter. This ensures that PMD thread completed
736 * the reload process.
738 * For the PMD thread:
740 * * PMD thread should read 'reload' atomic with 'acquire' memory
741 * ordering model or stricter. This will guarantee that all changes
742 * made before setting the 'reload' in the requesting thread will be
743 * visible to the PMD thread.
744 * * All other configuration data could be read with any memory
745 * ordering model (including non-atomic and 'relaxed') but *only after*
746 * reading the 'reload' atomic set to 'true'.
747 * * When the PMD reload done, PMD should (optionally) set all the below
748 * knobs except the 'reload' to their default ('false') values and
749 * (mandatory), as the last step, set the 'reload' to 'false' using
750 * 'release' memory ordering model or stricter. This will inform the
751 * requesting thread that PMD has completed a reload cycle.
753 atomic_bool reload
; /* Do we need to reload ports? */
754 atomic_bool wait_for_reload
; /* Can we busy wait for the next reload? */
755 atomic_bool reload_tx_qid
; /* Do we need to reload static_tx_qid? */
756 atomic_bool exit
; /* For terminating the pmd thread. */
759 unsigned core_id
; /* CPU core id of this pmd thread. */
760 int numa_id
; /* numa node id of this pmd thread. */
763 /* Queue id used by this pmd thread to send packets on all netdevs if
764 * XPS disabled for this netdev. All static_tx_qid's are unique and less
765 * than 'cmap_count(dp->poll_threads)'. */
766 uint32_t static_tx_qid
;
768 /* Number of filled output batches. */
769 int n_output_batches
;
771 struct ovs_mutex port_mutex
; /* Mutex for 'poll_list' and 'tx_ports'. */
772 /* List of rx queues to poll. */
773 struct hmap poll_list OVS_GUARDED
;
774 /* Map of 'tx_port's used for transmission. Written by the main thread,
775 * read by the pmd thread. */
776 struct hmap tx_ports OVS_GUARDED
;
778 struct ovs_mutex bond_mutex
; /* Protects updates of 'tx_bonds'. */
779 /* Map of 'tx_bond's used for transmission. Written by the main thread
780 * and read by the pmd thread. */
781 struct cmap tx_bonds
;
783 /* These are thread-local copies of 'tx_ports'. One contains only tunnel
784 * ports (that support push_tunnel/pop_tunnel), the other contains ports
785 * with at least one txq (that support send). A port can be in both.
787 * There are two separate maps to make sure that we don't try to execute
788 * OUTPUT on a device which has 0 txqs or PUSH/POP on a non-tunnel device.
790 * The instances for cpu core NON_PMD_CORE_ID can be accessed by multiple
791 * threads, and thusly need to be protected by 'non_pmd_mutex'. Every
792 * other instance will only be accessed by its own pmd thread. */
793 struct hmap tnl_port_cache
;
794 struct hmap send_port_cache
;
796 /* Keep track of detailed PMD performance statistics. */
797 struct pmd_perf_stats perf_stats
;
799 /* Stats from previous iteration used by automatic pmd
800 * load balance logic. */
801 uint64_t prev_stats
[PMD_N_STATS
];
802 atomic_count pmd_overloaded
;
804 /* Set to true if the pmd thread needs to be reloaded. */
807 /* Next time when PMD should try RCU quiescing. */
808 long long next_rcu_quiesce
;
811 /* Interface to netdev-based datapath. */
814 struct dp_netdev
*dp
;
815 uint64_t last_port_seq
;
818 static int get_port_by_number(struct dp_netdev
*dp
, odp_port_t port_no
,
819 struct dp_netdev_port
**portp
)
820 OVS_REQUIRES(dp
->port_mutex
);
821 static int get_port_by_name(struct dp_netdev
*dp
, const char *devname
,
822 struct dp_netdev_port
**portp
)
823 OVS_REQUIRES(dp
->port_mutex
);
824 static void dp_netdev_free(struct dp_netdev
*)
825 OVS_REQUIRES(dp_netdev_mutex
);
826 static int do_add_port(struct dp_netdev
*dp
, const char *devname
,
827 const char *type
, odp_port_t port_no
)
828 OVS_REQUIRES(dp
->port_mutex
);
829 static void do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*)
830 OVS_REQUIRES(dp
->port_mutex
);
831 static int dpif_netdev_open(const struct dpif_class
*, const char *name
,
832 bool create
, struct dpif
**);
833 static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
834 struct dp_packet_batch
*,
836 const struct flow
*flow
,
837 const struct nlattr
*actions
,
839 static void dp_netdev_input(struct dp_netdev_pmd_thread
*,
840 struct dp_packet_batch
*, odp_port_t port_no
);
841 static void dp_netdev_recirculate(struct dp_netdev_pmd_thread
*,
842 struct dp_packet_batch
*);
844 static void dp_netdev_disable_upcall(struct dp_netdev
*);
845 static void dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
);
846 static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
,
847 struct dp_netdev
*dp
, unsigned core_id
,
849 static void dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
);
850 static void dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
851 OVS_REQUIRES(dp
->port_mutex
);
853 static void *pmd_thread_main(void *);
854 static struct dp_netdev_pmd_thread
*dp_netdev_get_pmd(struct dp_netdev
*dp
,
856 static struct dp_netdev_pmd_thread
*
857 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
);
858 static void dp_netdev_del_pmd(struct dp_netdev
*dp
,
859 struct dp_netdev_pmd_thread
*pmd
);
860 static void dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
, bool non_pmd
);
861 static void dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
);
862 static void dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
863 struct dp_netdev_port
*port
)
864 OVS_REQUIRES(pmd
->port_mutex
);
865 static void dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
867 OVS_REQUIRES(pmd
->port_mutex
);
868 static void dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
869 struct dp_netdev_rxq
*rxq
)
870 OVS_REQUIRES(pmd
->port_mutex
);
871 static void dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
872 struct rxq_poll
*poll
)
873 OVS_REQUIRES(pmd
->port_mutex
);
875 dp_netdev_pmd_flush_output_packets(struct dp_netdev_pmd_thread
*pmd
,
877 static void dp_netdev_add_bond_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
878 struct tx_bond
*bond
, bool update
)
879 OVS_EXCLUDED(pmd
->bond_mutex
);
880 static void dp_netdev_del_bond_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
882 OVS_EXCLUDED(pmd
->bond_mutex
);
884 static void reconfigure_datapath(struct dp_netdev
*dp
)
885 OVS_REQUIRES(dp
->port_mutex
);
886 static bool dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
);
887 static void dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
);
888 static void dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
);
889 static void pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
890 OVS_REQUIRES(pmd
->port_mutex
);
892 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread
*pmd
,
893 struct polled_queue
*poll_list
, int poll_cnt
);
895 dp_netdev_rxq_set_cycles(struct dp_netdev_rxq
*rx
,
896 enum rxq_cycles_counter_type type
,
897 unsigned long long cycles
);
899 dp_netdev_rxq_get_cycles(struct dp_netdev_rxq
*rx
,
900 enum rxq_cycles_counter_type type
);
902 dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq
*rx
,
903 unsigned long long cycles
);
905 dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq
*rx
, unsigned idx
);
907 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
909 static int dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
911 static inline struct dpcls
*
912 dp_netdev_pmd_lookup_dpcls(struct dp_netdev_pmd_thread
*pmd
,
915 static inline bool emc_entry_alive(struct emc_entry
*ce
);
916 static void emc_clear_entry(struct emc_entry
*ce
);
917 static void smc_clear_entry(struct smc_bucket
*b
, int idx
);
919 static void dp_netdev_request_reconfigure(struct dp_netdev
*dp
);
921 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread
*pmd
);
922 static void queue_netdev_flow_del(struct dp_netdev_pmd_thread
*pmd
,
923 struct dp_netdev_flow
*flow
);
926 emc_cache_init(struct emc_cache
*flow_cache
)
930 flow_cache
->sweep_idx
= 0;
931 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
932 flow_cache
->entries
[i
].flow
= NULL
;
933 flow_cache
->entries
[i
].key
.hash
= 0;
934 flow_cache
->entries
[i
].key
.len
= sizeof(struct miniflow
);
935 flowmap_init(&flow_cache
->entries
[i
].key
.mf
.map
);
940 smc_cache_init(struct smc_cache
*smc_cache
)
943 for (i
= 0; i
< SMC_BUCKET_CNT
; i
++) {
944 for (j
= 0; j
< SMC_ENTRY_PER_BUCKET
; j
++) {
945 smc_cache
->buckets
[i
].flow_idx
[j
] = UINT16_MAX
;
951 dfc_cache_init(struct dfc_cache
*flow_cache
)
953 emc_cache_init(&flow_cache
->emc_cache
);
954 smc_cache_init(&flow_cache
->smc_cache
);
958 emc_cache_uninit(struct emc_cache
*flow_cache
)
962 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
963 emc_clear_entry(&flow_cache
->entries
[i
]);
968 smc_cache_uninit(struct smc_cache
*smc
)
972 for (i
= 0; i
< SMC_BUCKET_CNT
; i
++) {
973 for (j
= 0; j
< SMC_ENTRY_PER_BUCKET
; j
++) {
974 smc_clear_entry(&(smc
->buckets
[i
]), j
);
980 dfc_cache_uninit(struct dfc_cache
*flow_cache
)
982 smc_cache_uninit(&flow_cache
->smc_cache
);
983 emc_cache_uninit(&flow_cache
->emc_cache
);
986 /* Check and clear dead flow references slowly (one entry at each
989 emc_cache_slow_sweep(struct emc_cache
*flow_cache
)
991 struct emc_entry
*entry
= &flow_cache
->entries
[flow_cache
->sweep_idx
];
993 if (!emc_entry_alive(entry
)) {
994 emc_clear_entry(entry
);
996 flow_cache
->sweep_idx
= (flow_cache
->sweep_idx
+ 1) & EM_FLOW_HASH_MASK
;
999 /* Updates the time in PMD threads context and should be called in three cases:
1001 * 1. PMD structure initialization:
1002 * - dp_netdev_configure_pmd()
1004 * 2. Before processing of the new packet batch:
1005 * - dpif_netdev_execute()
1006 * - dp_netdev_process_rxq_port()
1008 * 3. At least once per polling iteration in main polling threads if no
1009 * packets received on current iteration:
1010 * - dpif_netdev_run()
1011 * - pmd_thread_main()
1013 * 'pmd->ctx.now' should be used without update in all other cases if possible.
1016 pmd_thread_ctx_time_update(struct dp_netdev_pmd_thread
*pmd
)
1018 pmd
->ctx
.now
= time_usec();
1021 /* Returns true if 'dpif' is a netdev or dummy dpif, false otherwise. */
1023 dpif_is_netdev(const struct dpif
*dpif
)
1025 return dpif
->dpif_class
->open
== dpif_netdev_open
;
1028 static struct dpif_netdev
*
1029 dpif_netdev_cast(const struct dpif
*dpif
)
1031 ovs_assert(dpif_is_netdev(dpif
));
1032 return CONTAINER_OF(dpif
, struct dpif_netdev
, dpif
);
1035 static struct dp_netdev
*
1036 get_dp_netdev(const struct dpif
*dpif
)
1038 return dpif_netdev_cast(dpif
)->dp
;
1041 enum pmd_info_type
{
1042 PMD_INFO_SHOW_STATS
, /* Show how cpu cycles are spent. */
1043 PMD_INFO_CLEAR_STATS
, /* Set the cycles count to 0. */
1044 PMD_INFO_SHOW_RXQ
, /* Show poll lists of pmd threads. */
1045 PMD_INFO_PERF_SHOW
, /* Show pmd performance details. */
1049 format_pmd_thread(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
1051 ds_put_cstr(reply
, (pmd
->core_id
== NON_PMD_CORE_ID
)
1052 ? "main thread" : "pmd thread");
1053 if (pmd
->numa_id
!= OVS_NUMA_UNSPEC
) {
1054 ds_put_format(reply
, " numa_id %d", pmd
->numa_id
);
1056 if (pmd
->core_id
!= OVS_CORE_UNSPEC
&& pmd
->core_id
!= NON_PMD_CORE_ID
) {
1057 ds_put_format(reply
, " core_id %u", pmd
->core_id
);
1059 ds_put_cstr(reply
, ":\n");
1063 pmd_info_show_stats(struct ds
*reply
,
1064 struct dp_netdev_pmd_thread
*pmd
)
1066 uint64_t stats
[PMD_N_STATS
];
1067 uint64_t total_cycles
, total_packets
;
1068 double passes_per_pkt
= 0;
1069 double lookups_per_hit
= 0;
1070 double packets_per_batch
= 0;
1072 pmd_perf_read_counters(&pmd
->perf_stats
, stats
);
1073 total_cycles
= stats
[PMD_CYCLES_ITER_IDLE
]
1074 + stats
[PMD_CYCLES_ITER_BUSY
];
1075 total_packets
= stats
[PMD_STAT_RECV
];
1077 format_pmd_thread(reply
, pmd
);
1079 if (total_packets
> 0) {
1080 passes_per_pkt
= (total_packets
+ stats
[PMD_STAT_RECIRC
])
1081 / (double) total_packets
;
1083 if (stats
[PMD_STAT_MASKED_HIT
] > 0) {
1084 lookups_per_hit
= stats
[PMD_STAT_MASKED_LOOKUP
]
1085 / (double) stats
[PMD_STAT_MASKED_HIT
];
1087 if (stats
[PMD_STAT_SENT_BATCHES
] > 0) {
1088 packets_per_batch
= stats
[PMD_STAT_SENT_PKTS
]
1089 / (double) stats
[PMD_STAT_SENT_BATCHES
];
1092 ds_put_format(reply
,
1093 " packets received: %"PRIu64
"\n"
1094 " packet recirculations: %"PRIu64
"\n"
1095 " avg. datapath passes per packet: %.02f\n"
1096 " emc hits: %"PRIu64
"\n"
1097 " smc hits: %"PRIu64
"\n"
1098 " megaflow hits: %"PRIu64
"\n"
1099 " avg. subtable lookups per megaflow hit: %.02f\n"
1100 " miss with success upcall: %"PRIu64
"\n"
1101 " miss with failed upcall: %"PRIu64
"\n"
1102 " avg. packets per output batch: %.02f\n",
1103 total_packets
, stats
[PMD_STAT_RECIRC
],
1104 passes_per_pkt
, stats
[PMD_STAT_EXACT_HIT
],
1105 stats
[PMD_STAT_SMC_HIT
],
1106 stats
[PMD_STAT_MASKED_HIT
], lookups_per_hit
,
1107 stats
[PMD_STAT_MISS
], stats
[PMD_STAT_LOST
],
1110 if (total_cycles
== 0) {
1114 ds_put_format(reply
,
1115 " idle cycles: %"PRIu64
" (%.02f%%)\n"
1116 " processing cycles: %"PRIu64
" (%.02f%%)\n",
1117 stats
[PMD_CYCLES_ITER_IDLE
],
1118 stats
[PMD_CYCLES_ITER_IDLE
] / (double) total_cycles
* 100,
1119 stats
[PMD_CYCLES_ITER_BUSY
],
1120 stats
[PMD_CYCLES_ITER_BUSY
] / (double) total_cycles
* 100);
1122 if (total_packets
== 0) {
1126 ds_put_format(reply
,
1127 " avg cycles per packet: %.02f (%"PRIu64
"/%"PRIu64
")\n",
1128 total_cycles
/ (double) total_packets
,
1129 total_cycles
, total_packets
);
1131 ds_put_format(reply
,
1132 " avg processing cycles per packet: "
1133 "%.02f (%"PRIu64
"/%"PRIu64
")\n",
1134 stats
[PMD_CYCLES_ITER_BUSY
] / (double) total_packets
,
1135 stats
[PMD_CYCLES_ITER_BUSY
], total_packets
);
1139 pmd_info_show_perf(struct ds
*reply
,
1140 struct dp_netdev_pmd_thread
*pmd
,
1141 struct pmd_perf_params
*par
)
1143 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
1145 xastrftime_msec("%H:%M:%S.###", time_wall_msec(), true);
1146 long long now
= time_msec();
1147 double duration
= (now
- pmd
->perf_stats
.start_ms
) / 1000.0;
1149 ds_put_cstr(reply
, "\n");
1150 ds_put_format(reply
, "Time: %s\n", time_str
);
1151 ds_put_format(reply
, "Measurement duration: %.3f s\n", duration
);
1152 ds_put_cstr(reply
, "\n");
1153 format_pmd_thread(reply
, pmd
);
1154 ds_put_cstr(reply
, "\n");
1155 pmd_perf_format_overall_stats(reply
, &pmd
->perf_stats
, duration
);
1156 if (pmd_perf_metrics_enabled(pmd
)) {
1157 /* Prevent parallel clearing of perf metrics. */
1158 ovs_mutex_lock(&pmd
->perf_stats
.clear_mutex
);
1159 if (par
->histograms
) {
1160 ds_put_cstr(reply
, "\n");
1161 pmd_perf_format_histograms(reply
, &pmd
->perf_stats
);
1163 if (par
->iter_hist_len
> 0) {
1164 ds_put_cstr(reply
, "\n");
1165 pmd_perf_format_iteration_history(reply
, &pmd
->perf_stats
,
1166 par
->iter_hist_len
);
1168 if (par
->ms_hist_len
> 0) {
1169 ds_put_cstr(reply
, "\n");
1170 pmd_perf_format_ms_history(reply
, &pmd
->perf_stats
,
1173 ovs_mutex_unlock(&pmd
->perf_stats
.clear_mutex
);
1180 compare_poll_list(const void *a_
, const void *b_
)
1182 const struct rxq_poll
*a
= a_
;
1183 const struct rxq_poll
*b
= b_
;
1185 const char *namea
= netdev_rxq_get_name(a
->rxq
->rx
);
1186 const char *nameb
= netdev_rxq_get_name(b
->rxq
->rx
);
1188 int cmp
= strcmp(namea
, nameb
);
1190 return netdev_rxq_get_queue_id(a
->rxq
->rx
)
1191 - netdev_rxq_get_queue_id(b
->rxq
->rx
);
1198 sorted_poll_list(struct dp_netdev_pmd_thread
*pmd
, struct rxq_poll
**list
,
1200 OVS_REQUIRES(pmd
->port_mutex
)
1202 struct rxq_poll
*ret
, *poll
;
1205 *n
= hmap_count(&pmd
->poll_list
);
1209 ret
= xcalloc(*n
, sizeof *ret
);
1211 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
1215 ovs_assert(i
== *n
);
1216 qsort(ret
, *n
, sizeof *ret
, compare_poll_list
);
1223 pmd_info_show_rxq(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
1225 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
1226 struct rxq_poll
*list
;
1228 uint64_t total_cycles
= 0;
1230 ds_put_format(reply
,
1231 "pmd thread numa_id %d core_id %u:\n isolated : %s\n",
1232 pmd
->numa_id
, pmd
->core_id
, (pmd
->isolated
)
1233 ? "true" : "false");
1235 ovs_mutex_lock(&pmd
->port_mutex
);
1236 sorted_poll_list(pmd
, &list
, &n_rxq
);
1238 /* Get the total pmd cycles for an interval. */
1239 atomic_read_relaxed(&pmd
->intrvl_cycles
, &total_cycles
);
1240 /* Estimate the cycles to cover all intervals. */
1241 total_cycles
*= PMD_RXQ_INTERVAL_MAX
;
1243 for (int i
= 0; i
< n_rxq
; i
++) {
1244 struct dp_netdev_rxq
*rxq
= list
[i
].rxq
;
1245 const char *name
= netdev_rxq_get_name(rxq
->rx
);
1246 uint64_t proc_cycles
= 0;
1248 for (int j
= 0; j
< PMD_RXQ_INTERVAL_MAX
; j
++) {
1249 proc_cycles
+= dp_netdev_rxq_get_intrvl_cycles(rxq
, j
);
1251 ds_put_format(reply
, " port: %-16s queue-id: %2d", name
,
1252 netdev_rxq_get_queue_id(list
[i
].rxq
->rx
));
1253 ds_put_format(reply
, " %s", netdev_rxq_enabled(list
[i
].rxq
->rx
)
1254 ? "(enabled) " : "(disabled)");
1255 ds_put_format(reply
, " pmd usage: ");
1257 ds_put_format(reply
, "%2"PRIu64
"",
1258 proc_cycles
* 100 / total_cycles
);
1259 ds_put_cstr(reply
, " %");
1261 ds_put_format(reply
, "%s", "NOT AVAIL");
1263 ds_put_cstr(reply
, "\n");
1265 ovs_mutex_unlock(&pmd
->port_mutex
);
1271 compare_poll_thread_list(const void *a_
, const void *b_
)
1273 const struct dp_netdev_pmd_thread
*a
, *b
;
1275 a
= *(struct dp_netdev_pmd_thread
**)a_
;
1276 b
= *(struct dp_netdev_pmd_thread
**)b_
;
1278 if (a
->core_id
< b
->core_id
) {
1281 if (a
->core_id
> b
->core_id
) {
1287 /* Create a sorted list of pmd's from the dp->poll_threads cmap. We can use
1288 * this list, as long as we do not go to quiescent state. */
1290 sorted_poll_thread_list(struct dp_netdev
*dp
,
1291 struct dp_netdev_pmd_thread
***list
,
1294 struct dp_netdev_pmd_thread
*pmd
;
1295 struct dp_netdev_pmd_thread
**pmd_list
;
1296 size_t k
= 0, n_pmds
;
1298 n_pmds
= cmap_count(&dp
->poll_threads
);
1299 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
1301 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1305 pmd_list
[k
++] = pmd
;
1308 qsort(pmd_list
, k
, sizeof *pmd_list
, compare_poll_thread_list
);
1315 dpif_netdev_subtable_lookup_get(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1316 const char *argv
[] OVS_UNUSED
,
1317 void *aux OVS_UNUSED
)
1319 /* Get a list of all lookup functions. */
1320 struct dpcls_subtable_lookup_info_t
*lookup_funcs
= NULL
;
1321 int32_t count
= dpcls_subtable_lookup_info_get(&lookup_funcs
);
1323 unixctl_command_reply_error(conn
, "error getting lookup names");
1327 /* Add all lookup functions to reply string. */
1328 struct ds reply
= DS_EMPTY_INITIALIZER
;
1329 ds_put_cstr(&reply
, "Available lookup functions (priority : name)\n");
1330 for (int i
= 0; i
< count
; i
++) {
1331 ds_put_format(&reply
, " %d : %s\n", lookup_funcs
[i
].prio
,
1332 lookup_funcs
[i
].name
);
1334 unixctl_command_reply(conn
, ds_cstr(&reply
));
1339 dpif_netdev_subtable_lookup_set(struct unixctl_conn
*conn
, int argc
,
1340 const char *argv
[], void *aux OVS_UNUSED
)
1342 /* This function requires 2 parameters (argv[1] and argv[2]) to execute.
1343 * argv[1] is subtable name
1344 * argv[2] is priority
1345 * argv[3] is the datapath name (optional if only 1 datapath exists)
1347 const char *func_name
= argv
[1];
1351 uint32_t new_prio
= strtoul(argv
[2], &err_char
, 10);
1352 if (errno
!= 0 || new_prio
> UINT8_MAX
) {
1353 unixctl_command_reply_error(conn
,
1354 "error converting priority, use integer in range 0-255\n");
1358 int32_t err
= dpcls_subtable_set_prio(func_name
, new_prio
);
1360 unixctl_command_reply_error(conn
,
1361 "error, subtable lookup function not found\n");
1365 /* argv[3] is optional datapath instance. If no datapath name is provided
1366 * and only one datapath exists, the one existing datapath is reprobed.
1368 ovs_mutex_lock(&dp_netdev_mutex
);
1369 struct dp_netdev
*dp
= NULL
;
1372 dp
= shash_find_data(&dp_netdevs
, argv
[3]);
1373 } else if (shash_count(&dp_netdevs
) == 1) {
1374 dp
= shash_first(&dp_netdevs
)->data
;
1378 ovs_mutex_unlock(&dp_netdev_mutex
);
1379 unixctl_command_reply_error(conn
,
1380 "please specify an existing datapath");
1384 /* Get PMD threads list, required to get DPCLS instances. */
1386 uint32_t lookup_dpcls_changed
= 0;
1387 uint32_t lookup_subtable_changed
= 0;
1388 struct dp_netdev_pmd_thread
**pmd_list
;
1389 sorted_poll_thread_list(dp
, &pmd_list
, &n
);
1391 /* take port mutex as HMAP iters over them. */
1392 ovs_mutex_lock(&dp
->port_mutex
);
1394 for (size_t i
= 0; i
< n
; i
++) {
1395 struct dp_netdev_pmd_thread
*pmd
= pmd_list
[i
];
1396 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
1400 struct dp_netdev_port
*port
= NULL
;
1401 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1402 odp_port_t in_port
= port
->port_no
;
1403 struct dpcls
*cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
1407 uint32_t subtbl_changes
= dpcls_subtable_lookup_reprobe(cls
);
1408 if (subtbl_changes
) {
1409 lookup_dpcls_changed
++;
1410 lookup_subtable_changed
+= subtbl_changes
;
1415 /* release port mutex before netdev mutex. */
1416 ovs_mutex_unlock(&dp
->port_mutex
);
1417 ovs_mutex_unlock(&dp_netdev_mutex
);
1419 struct ds reply
= DS_EMPTY_INITIALIZER
;
1420 ds_put_format(&reply
,
1421 "Lookup priority change affected %d dpcls ports and %d subtables.\n",
1422 lookup_dpcls_changed
, lookup_subtable_changed
);
1423 const char *reply_str
= ds_cstr(&reply
);
1424 unixctl_command_reply(conn
, reply_str
);
1425 VLOG_INFO("%s", reply_str
);
1430 dpif_netdev_pmd_rebalance(struct unixctl_conn
*conn
, int argc
,
1431 const char *argv
[], void *aux OVS_UNUSED
)
1433 struct ds reply
= DS_EMPTY_INITIALIZER
;
1434 struct dp_netdev
*dp
= NULL
;
1436 ovs_mutex_lock(&dp_netdev_mutex
);
1439 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
1440 } else if (shash_count(&dp_netdevs
) == 1) {
1441 /* There's only one datapath */
1442 dp
= shash_first(&dp_netdevs
)->data
;
1446 ovs_mutex_unlock(&dp_netdev_mutex
);
1447 unixctl_command_reply_error(conn
,
1448 "please specify an existing datapath");
1452 dp_netdev_request_reconfigure(dp
);
1453 ovs_mutex_unlock(&dp_netdev_mutex
);
1454 ds_put_cstr(&reply
, "pmd rxq rebalance requested.\n");
1455 unixctl_command_reply(conn
, ds_cstr(&reply
));
1460 dpif_netdev_pmd_info(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
1463 struct ds reply
= DS_EMPTY_INITIALIZER
;
1464 struct dp_netdev_pmd_thread
**pmd_list
;
1465 struct dp_netdev
*dp
= NULL
;
1466 enum pmd_info_type type
= *(enum pmd_info_type
*) aux
;
1467 unsigned int core_id
;
1468 bool filter_on_pmd
= false;
1471 ovs_mutex_lock(&dp_netdev_mutex
);
1474 if (!strcmp(argv
[1], "-pmd") && argc
> 2) {
1475 if (str_to_uint(argv
[2], 10, &core_id
)) {
1476 filter_on_pmd
= true;
1481 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
1488 if (shash_count(&dp_netdevs
) == 1) {
1489 /* There's only one datapath */
1490 dp
= shash_first(&dp_netdevs
)->data
;
1492 ovs_mutex_unlock(&dp_netdev_mutex
);
1493 unixctl_command_reply_error(conn
,
1494 "please specify an existing datapath");
1499 sorted_poll_thread_list(dp
, &pmd_list
, &n
);
1500 for (size_t i
= 0; i
< n
; i
++) {
1501 struct dp_netdev_pmd_thread
*pmd
= pmd_list
[i
];
1505 if (filter_on_pmd
&& pmd
->core_id
!= core_id
) {
1508 if (type
== PMD_INFO_SHOW_RXQ
) {
1509 pmd_info_show_rxq(&reply
, pmd
);
1510 } else if (type
== PMD_INFO_CLEAR_STATS
) {
1511 pmd_perf_stats_clear(&pmd
->perf_stats
);
1512 } else if (type
== PMD_INFO_SHOW_STATS
) {
1513 pmd_info_show_stats(&reply
, pmd
);
1514 } else if (type
== PMD_INFO_PERF_SHOW
) {
1515 pmd_info_show_perf(&reply
, pmd
, (struct pmd_perf_params
*)aux
);
1520 ovs_mutex_unlock(&dp_netdev_mutex
);
1522 unixctl_command_reply(conn
, ds_cstr(&reply
));
1527 pmd_perf_show_cmd(struct unixctl_conn
*conn
, int argc
,
1529 void *aux OVS_UNUSED
)
1531 struct pmd_perf_params par
;
1532 long int it_hist
= 0, ms_hist
= 0;
1533 par
.histograms
= true;
1536 if (!strcmp(argv
[1], "-nh")) {
1537 par
.histograms
= false;
1540 } else if (!strcmp(argv
[1], "-it") && argc
> 2) {
1541 it_hist
= strtol(argv
[2], NULL
, 10);
1544 } else if (it_hist
> HISTORY_LEN
) {
1545 it_hist
= HISTORY_LEN
;
1549 } else if (!strcmp(argv
[1], "-ms") && argc
> 2) {
1550 ms_hist
= strtol(argv
[2], NULL
, 10);
1553 } else if (ms_hist
> HISTORY_LEN
) {
1554 ms_hist
= HISTORY_LEN
;
1562 par
.iter_hist_len
= it_hist
;
1563 par
.ms_hist_len
= ms_hist
;
1564 par
.command_type
= PMD_INFO_PERF_SHOW
;
1565 dpif_netdev_pmd_info(conn
, argc
, argv
, &par
);
1569 dpif_netdev_bond_show(struct unixctl_conn
*conn
, int argc
,
1570 const char *argv
[], void *aux OVS_UNUSED
)
1572 struct ds reply
= DS_EMPTY_INITIALIZER
;
1573 struct dp_netdev
*dp
= NULL
;
1575 ovs_mutex_lock(&dp_netdev_mutex
);
1577 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
1578 } else if (shash_count(&dp_netdevs
) == 1) {
1579 /* There's only one datapath. */
1580 dp
= shash_first(&dp_netdevs
)->data
;
1583 ovs_mutex_unlock(&dp_netdev_mutex
);
1584 unixctl_command_reply_error(conn
,
1585 "please specify an existing datapath");
1589 if (cmap_count(&dp
->tx_bonds
) > 0) {
1590 struct tx_bond
*dp_bond_entry
;
1593 ds_put_cstr(&reply
, "Bonds:\n");
1594 CMAP_FOR_EACH (dp_bond_entry
, node
, &dp
->tx_bonds
) {
1595 ds_put_format(&reply
, " bond-id %"PRIu32
":\n",
1596 dp_bond_entry
->bond_id
);
1597 for (int bucket
= 0; bucket
< BOND_BUCKETS
; bucket
++) {
1599 odp_to_u32(dp_bond_entry
->slave_buckets
[bucket
].slave_id
);
1600 ds_put_format(&reply
, " bucket %d - slave %"PRIu32
"\n",
1605 ovs_mutex_unlock(&dp_netdev_mutex
);
1606 unixctl_command_reply(conn
, ds_cstr(&reply
));
1612 dpif_netdev_init(void)
1614 static enum pmd_info_type show_aux
= PMD_INFO_SHOW_STATS
,
1615 clear_aux
= PMD_INFO_CLEAR_STATS
,
1616 poll_aux
= PMD_INFO_SHOW_RXQ
;
1618 unixctl_command_register("dpif-netdev/pmd-stats-show", "[-pmd core] [dp]",
1619 0, 3, dpif_netdev_pmd_info
,
1621 unixctl_command_register("dpif-netdev/pmd-stats-clear", "[-pmd core] [dp]",
1622 0, 3, dpif_netdev_pmd_info
,
1623 (void *)&clear_aux
);
1624 unixctl_command_register("dpif-netdev/pmd-rxq-show", "[-pmd core] [dp]",
1625 0, 3, dpif_netdev_pmd_info
,
1627 unixctl_command_register("dpif-netdev/pmd-perf-show",
1628 "[-nh] [-it iter-history-len]"
1629 " [-ms ms-history-len]"
1630 " [-pmd core] [dp]",
1631 0, 8, pmd_perf_show_cmd
,
1633 unixctl_command_register("dpif-netdev/pmd-rxq-rebalance", "[dp]",
1634 0, 1, dpif_netdev_pmd_rebalance
,
1636 unixctl_command_register("dpif-netdev/pmd-perf-log-set",
1637 "on|off [-b before] [-a after] [-e|-ne] "
1638 "[-us usec] [-q qlen]",
1639 0, 10, pmd_perf_log_set_cmd
,
1641 unixctl_command_register("dpif-netdev/bond-show", "[dp]",
1642 0, 1, dpif_netdev_bond_show
,
1644 unixctl_command_register("dpif-netdev/subtable-lookup-prio-set",
1645 "[lookup_func] [prio] [dp]",
1646 2, 3, dpif_netdev_subtable_lookup_set
,
1648 unixctl_command_register("dpif-netdev/subtable-lookup-prio-get", "",
1649 0, 0, dpif_netdev_subtable_lookup_get
,
1655 dpif_netdev_enumerate(struct sset
*all_dps
,
1656 const struct dpif_class
*dpif_class
)
1658 struct shash_node
*node
;
1660 ovs_mutex_lock(&dp_netdev_mutex
);
1661 SHASH_FOR_EACH(node
, &dp_netdevs
) {
1662 struct dp_netdev
*dp
= node
->data
;
1663 if (dpif_class
!= dp
->class) {
1664 /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs.
1665 * If the class doesn't match, skip this dpif. */
1668 sset_add(all_dps
, node
->name
);
1670 ovs_mutex_unlock(&dp_netdev_mutex
);
1676 dpif_netdev_class_is_dummy(const struct dpif_class
*class)
1678 return class != &dpif_netdev_class
;
1682 dpif_netdev_port_open_type(const struct dpif_class
*class, const char *type
)
1684 return strcmp(type
, "internal") ? type
1685 : dpif_netdev_class_is_dummy(class) ? "dummy-internal"
1689 static struct dpif
*
1690 create_dpif_netdev(struct dp_netdev
*dp
)
1692 uint16_t netflow_id
= hash_string(dp
->name
, 0);
1693 struct dpif_netdev
*dpif
;
1695 ovs_refcount_ref(&dp
->ref_cnt
);
1697 dpif
= xmalloc(sizeof *dpif
);
1698 dpif_init(&dpif
->dpif
, dp
->class, dp
->name
, netflow_id
>> 8, netflow_id
);
1700 dpif
->last_port_seq
= seq_read(dp
->port_seq
);
1705 /* Choose an unused, non-zero port number and return it on success.
1706 * Return ODPP_NONE on failure. */
1708 choose_port(struct dp_netdev
*dp
, const char *name
)
1709 OVS_REQUIRES(dp
->port_mutex
)
1713 if (dp
->class != &dpif_netdev_class
) {
1717 /* If the port name begins with "br", start the number search at
1718 * 100 to make writing tests easier. */
1719 if (!strncmp(name
, "br", 2)) {
1723 /* If the port name contains a number, try to assign that port number.
1724 * This can make writing unit tests easier because port numbers are
1726 for (p
= name
; *p
!= '\0'; p
++) {
1727 if (isdigit((unsigned char) *p
)) {
1728 port_no
= start_no
+ strtol(p
, NULL
, 10);
1729 if (port_no
> 0 && port_no
!= odp_to_u32(ODPP_NONE
)
1730 && !dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
1731 return u32_to_odp(port_no
);
1738 for (port_no
= 1; port_no
<= UINT16_MAX
; port_no
++) {
1739 if (!dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
1740 return u32_to_odp(port_no
);
1748 create_dp_netdev(const char *name
, const struct dpif_class
*class,
1749 struct dp_netdev
**dpp
)
1750 OVS_REQUIRES(dp_netdev_mutex
)
1752 static struct ovsthread_once tsc_freq_check
= OVSTHREAD_ONCE_INITIALIZER
;
1753 struct dp_netdev
*dp
;
1756 /* Avoid estimating TSC frequency for dummy datapath to not slow down
1758 if (!dpif_netdev_class_is_dummy(class)
1759 && ovsthread_once_start(&tsc_freq_check
)) {
1760 pmd_perf_estimate_tsc_frequency();
1761 ovsthread_once_done(&tsc_freq_check
);
1764 dp
= xzalloc(sizeof *dp
);
1765 shash_add(&dp_netdevs
, name
, dp
);
1767 *CONST_CAST(const struct dpif_class
**, &dp
->class) = class;
1768 *CONST_CAST(const char **, &dp
->name
) = xstrdup(name
);
1769 ovs_refcount_init(&dp
->ref_cnt
);
1770 atomic_flag_clear(&dp
->destroyed
);
1772 ovs_mutex_init_recursive(&dp
->port_mutex
);
1773 hmap_init(&dp
->ports
);
1774 dp
->port_seq
= seq_create();
1775 ovs_mutex_init(&dp
->bond_mutex
);
1776 cmap_init(&dp
->tx_bonds
);
1778 fat_rwlock_init(&dp
->upcall_rwlock
);
1780 dp
->reconfigure_seq
= seq_create();
1781 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
1783 for (int i
= 0; i
< N_METER_LOCKS
; ++i
) {
1784 ovs_mutex_init_adaptive(&dp
->meter_locks
[i
]);
1787 /* Disable upcalls by default. */
1788 dp_netdev_disable_upcall(dp
);
1789 dp
->upcall_aux
= NULL
;
1790 dp
->upcall_cb
= NULL
;
1792 dp
->conntrack
= conntrack_init();
1794 atomic_init(&dp
->emc_insert_min
, DEFAULT_EM_FLOW_INSERT_MIN
);
1795 atomic_init(&dp
->tx_flush_interval
, DEFAULT_TX_FLUSH_INTERVAL
);
1797 cmap_init(&dp
->poll_threads
);
1798 dp
->pmd_rxq_assign_cyc
= true;
1800 ovs_mutex_init(&dp
->tx_qid_pool_mutex
);
1801 /* We need 1 Tx queue for each possible core + 1 for non-PMD threads. */
1802 dp
->tx_qid_pool
= id_pool_create(0, ovs_numa_get_n_cores() + 1);
1804 ovs_mutex_init_recursive(&dp
->non_pmd_mutex
);
1805 ovsthread_key_create(&dp
->per_pmd_key
, NULL
);
1807 ovs_mutex_lock(&dp
->port_mutex
);
1808 /* non-PMD will be created before all other threads and will
1809 * allocate static_tx_qid = 0. */
1810 dp_netdev_set_nonpmd(dp
);
1812 error
= do_add_port(dp
, name
, dpif_netdev_port_open_type(dp
->class,
1815 ovs_mutex_unlock(&dp
->port_mutex
);
1821 dp
->last_tnl_conf_seq
= seq_read(tnl_conf_seq
);
1827 dp_netdev_request_reconfigure(struct dp_netdev
*dp
)
1829 seq_change(dp
->reconfigure_seq
);
1833 dp_netdev_is_reconf_required(struct dp_netdev
*dp
)
1835 return seq_read(dp
->reconfigure_seq
) != dp
->last_reconfigure_seq
;
1839 dpif_netdev_open(const struct dpif_class
*class, const char *name
,
1840 bool create
, struct dpif
**dpifp
)
1842 struct dp_netdev
*dp
;
1845 ovs_mutex_lock(&dp_netdev_mutex
);
1846 dp
= shash_find_data(&dp_netdevs
, name
);
1848 error
= create
? create_dp_netdev(name
, class, &dp
) : ENODEV
;
1850 error
= (dp
->class != class ? EINVAL
1855 *dpifp
= create_dpif_netdev(dp
);
1857 ovs_mutex_unlock(&dp_netdev_mutex
);
1863 dp_netdev_destroy_upcall_lock(struct dp_netdev
*dp
)
1864 OVS_NO_THREAD_SAFETY_ANALYSIS
1866 /* Check that upcalls are disabled, i.e. that the rwlock is taken */
1867 ovs_assert(fat_rwlock_tryrdlock(&dp
->upcall_rwlock
));
1869 /* Before freeing a lock we should release it */
1870 fat_rwlock_unlock(&dp
->upcall_rwlock
);
1871 fat_rwlock_destroy(&dp
->upcall_rwlock
);
1875 dp_delete_meter(struct dp_netdev
*dp
, uint32_t meter_id
)
1876 OVS_REQUIRES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
1878 if (dp
->meters
[meter_id
]) {
1879 free(dp
->meters
[meter_id
]);
1880 dp
->meters
[meter_id
] = NULL
;
1885 hash_bond_id(uint32_t bond_id
)
1887 return hash_int(bond_id
, 0);
1890 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
1891 * through the 'dp_netdevs' shash while freeing 'dp'. */
1893 dp_netdev_free(struct dp_netdev
*dp
)
1894 OVS_REQUIRES(dp_netdev_mutex
)
1896 struct dp_netdev_port
*port
, *next
;
1897 struct tx_bond
*bond
;
1899 shash_find_and_delete(&dp_netdevs
, dp
->name
);
1901 ovs_mutex_lock(&dp
->port_mutex
);
1902 HMAP_FOR_EACH_SAFE (port
, next
, node
, &dp
->ports
) {
1903 do_del_port(dp
, port
);
1905 ovs_mutex_unlock(&dp
->port_mutex
);
1907 ovs_mutex_lock(&dp
->bond_mutex
);
1908 CMAP_FOR_EACH (bond
, node
, &dp
->tx_bonds
) {
1909 cmap_remove(&dp
->tx_bonds
, &bond
->node
, hash_bond_id(bond
->bond_id
));
1910 ovsrcu_postpone(free
, bond
);
1912 ovs_mutex_unlock(&dp
->bond_mutex
);
1914 dp_netdev_destroy_all_pmds(dp
, true);
1915 cmap_destroy(&dp
->poll_threads
);
1917 ovs_mutex_destroy(&dp
->tx_qid_pool_mutex
);
1918 id_pool_destroy(dp
->tx_qid_pool
);
1920 ovs_mutex_destroy(&dp
->non_pmd_mutex
);
1921 ovsthread_key_delete(dp
->per_pmd_key
);
1923 conntrack_destroy(dp
->conntrack
);
1926 seq_destroy(dp
->reconfigure_seq
);
1928 seq_destroy(dp
->port_seq
);
1929 hmap_destroy(&dp
->ports
);
1930 ovs_mutex_destroy(&dp
->port_mutex
);
1932 cmap_destroy(&dp
->tx_bonds
);
1933 ovs_mutex_destroy(&dp
->bond_mutex
);
1935 /* Upcalls must be disabled at this point */
1936 dp_netdev_destroy_upcall_lock(dp
);
1940 for (i
= 0; i
< MAX_METERS
; ++i
) {
1942 dp_delete_meter(dp
, i
);
1943 meter_unlock(dp
, i
);
1945 for (i
= 0; i
< N_METER_LOCKS
; ++i
) {
1946 ovs_mutex_destroy(&dp
->meter_locks
[i
]);
1949 free(dp
->pmd_cmask
);
1950 free(CONST_CAST(char *, dp
->name
));
1955 dp_netdev_unref(struct dp_netdev
*dp
)
1958 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
1959 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
1960 ovs_mutex_lock(&dp_netdev_mutex
);
1961 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1964 ovs_mutex_unlock(&dp_netdev_mutex
);
1969 dpif_netdev_close(struct dpif
*dpif
)
1971 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1973 dp_netdev_unref(dp
);
1978 dpif_netdev_destroy(struct dpif
*dpif
)
1980 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1982 if (!atomic_flag_test_and_set(&dp
->destroyed
)) {
1983 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1984 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
1992 /* Add 'n' to the atomic variable 'var' non-atomically and using relaxed
1993 * load/store semantics. While the increment is not atomic, the load and
1994 * store operations are, making it impossible to read inconsistent values.
1996 * This is used to update thread local stats counters. */
1998 non_atomic_ullong_add(atomic_ullong
*var
, unsigned long long n
)
2000 unsigned long long tmp
;
2002 atomic_read_relaxed(var
, &tmp
);
2004 atomic_store_relaxed(var
, tmp
);
2008 dpif_netdev_get_stats(const struct dpif
*dpif
, struct dpif_dp_stats
*stats
)
2010 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2011 struct dp_netdev_pmd_thread
*pmd
;
2012 uint64_t pmd_stats
[PMD_N_STATS
];
2014 stats
->n_flows
= stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
2015 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2016 stats
->n_flows
+= cmap_count(&pmd
->flow_table
);
2017 pmd_perf_read_counters(&pmd
->perf_stats
, pmd_stats
);
2018 stats
->n_hit
+= pmd_stats
[PMD_STAT_EXACT_HIT
];
2019 stats
->n_hit
+= pmd_stats
[PMD_STAT_SMC_HIT
];
2020 stats
->n_hit
+= pmd_stats
[PMD_STAT_MASKED_HIT
];
2021 stats
->n_missed
+= pmd_stats
[PMD_STAT_MISS
];
2022 stats
->n_lost
+= pmd_stats
[PMD_STAT_LOST
];
2024 stats
->n_masks
= UINT32_MAX
;
2025 stats
->n_mask_hit
= UINT64_MAX
;
2031 dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread
*pmd
)
2033 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
2034 ovs_mutex_lock(&pmd
->dp
->non_pmd_mutex
);
2035 ovs_mutex_lock(&pmd
->port_mutex
);
2036 pmd_load_cached_ports(pmd
);
2037 ovs_mutex_unlock(&pmd
->port_mutex
);
2038 ovs_mutex_unlock(&pmd
->dp
->non_pmd_mutex
);
2042 seq_change(pmd
->reload_seq
);
2043 atomic_store_explicit(&pmd
->reload
, true, memory_order_release
);
2047 hash_port_no(odp_port_t port_no
)
2049 return hash_int(odp_to_u32(port_no
), 0);
2053 port_create(const char *devname
, const char *type
,
2054 odp_port_t port_no
, struct dp_netdev_port
**portp
)
2056 struct dp_netdev_port
*port
;
2057 enum netdev_flags flags
;
2058 struct netdev
*netdev
;
2063 /* Open and validate network device. */
2064 error
= netdev_open(devname
, type
, &netdev
);
2068 /* XXX reject non-Ethernet devices */
2070 netdev_get_flags(netdev
, &flags
);
2071 if (flags
& NETDEV_LOOPBACK
) {
2072 VLOG_ERR("%s: cannot add a loopback device", devname
);
2077 port
= xzalloc(sizeof *port
);
2078 port
->port_no
= port_no
;
2079 port
->netdev
= netdev
;
2080 port
->type
= xstrdup(type
);
2082 port
->emc_enabled
= true;
2083 port
->need_reconfigure
= true;
2084 ovs_mutex_init(&port
->txq_used_mutex
);
2091 netdev_close(netdev
);
2096 do_add_port(struct dp_netdev
*dp
, const char *devname
, const char *type
,
2098 OVS_REQUIRES(dp
->port_mutex
)
2100 struct netdev_saved_flags
*sf
;
2101 struct dp_netdev_port
*port
;
2104 /* Reject devices already in 'dp'. */
2105 if (!get_port_by_name(dp
, devname
, &port
)) {
2109 error
= port_create(devname
, type
, port_no
, &port
);
2114 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
2115 seq_change(dp
->port_seq
);
2117 reconfigure_datapath(dp
);
2119 /* Check that port was successfully configured. */
2120 if (!dp_netdev_lookup_port(dp
, port_no
)) {
2124 /* Updating device flags triggers an if_notifier, which triggers a bridge
2125 * reconfiguration and another attempt to add this port, leading to an
2126 * infinite loop if the device is configured incorrectly and cannot be
2127 * added. Setting the promisc mode after a successful reconfiguration,
2128 * since we already know that the device is somehow properly configured. */
2129 error
= netdev_turn_flags_on(port
->netdev
, NETDEV_PROMISC
, &sf
);
2131 VLOG_ERR("%s: cannot set promisc flag", devname
);
2132 do_del_port(dp
, port
);
2141 dpif_netdev_port_add(struct dpif
*dpif
, struct netdev
*netdev
,
2142 odp_port_t
*port_nop
)
2144 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2145 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
2146 const char *dpif_port
;
2150 ovs_mutex_lock(&dp
->port_mutex
);
2151 dpif_port
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
2152 if (*port_nop
!= ODPP_NONE
) {
2153 port_no
= *port_nop
;
2154 error
= dp_netdev_lookup_port(dp
, *port_nop
) ? EBUSY
: 0;
2156 port_no
= choose_port(dp
, dpif_port
);
2157 error
= port_no
== ODPP_NONE
? EFBIG
: 0;
2160 *port_nop
= port_no
;
2161 error
= do_add_port(dp
, dpif_port
, netdev_get_type(netdev
), port_no
);
2163 ovs_mutex_unlock(&dp
->port_mutex
);
2169 dpif_netdev_port_del(struct dpif
*dpif
, odp_port_t port_no
)
2171 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2174 ovs_mutex_lock(&dp
->port_mutex
);
2175 if (port_no
== ODPP_LOCAL
) {
2178 struct dp_netdev_port
*port
;
2180 error
= get_port_by_number(dp
, port_no
, &port
);
2182 do_del_port(dp
, port
);
2185 ovs_mutex_unlock(&dp
->port_mutex
);
2191 is_valid_port_number(odp_port_t port_no
)
2193 return port_no
!= ODPP_NONE
;
2196 static struct dp_netdev_port
*
2197 dp_netdev_lookup_port(const struct dp_netdev
*dp
, odp_port_t port_no
)
2198 OVS_REQUIRES(dp
->port_mutex
)
2200 struct dp_netdev_port
*port
;
2202 HMAP_FOR_EACH_WITH_HASH (port
, node
, hash_port_no(port_no
), &dp
->ports
) {
2203 if (port
->port_no
== port_no
) {
2211 get_port_by_number(struct dp_netdev
*dp
,
2212 odp_port_t port_no
, struct dp_netdev_port
**portp
)
2213 OVS_REQUIRES(dp
->port_mutex
)
2215 if (!is_valid_port_number(port_no
)) {
2219 *portp
= dp_netdev_lookup_port(dp
, port_no
);
2220 return *portp
? 0 : ENODEV
;
2225 port_destroy(struct dp_netdev_port
*port
)
2231 netdev_close(port
->netdev
);
2232 netdev_restore_flags(port
->sf
);
2234 for (unsigned i
= 0; i
< port
->n_rxq
; i
++) {
2235 netdev_rxq_close(port
->rxqs
[i
].rx
);
2237 ovs_mutex_destroy(&port
->txq_used_mutex
);
2238 free(port
->rxq_affinity_list
);
2239 free(port
->txq_used
);
2246 get_port_by_name(struct dp_netdev
*dp
,
2247 const char *devname
, struct dp_netdev_port
**portp
)
2248 OVS_REQUIRES(dp
->port_mutex
)
2250 struct dp_netdev_port
*port
;
2252 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
2253 if (!strcmp(netdev_get_name(port
->netdev
), devname
)) {
2259 /* Callers of dpif_netdev_port_query_by_name() expect ENODEV for a non
2264 /* Returns 'true' if there is a port with pmd netdev. */
2266 has_pmd_port(struct dp_netdev
*dp
)
2267 OVS_REQUIRES(dp
->port_mutex
)
2269 struct dp_netdev_port
*port
;
2271 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
2272 if (netdev_is_pmd(port
->netdev
)) {
2281 do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*port
)
2282 OVS_REQUIRES(dp
->port_mutex
)
2284 hmap_remove(&dp
->ports
, &port
->node
);
2285 seq_change(dp
->port_seq
);
2287 reconfigure_datapath(dp
);
2293 answer_port_query(const struct dp_netdev_port
*port
,
2294 struct dpif_port
*dpif_port
)
2296 dpif_port
->name
= xstrdup(netdev_get_name(port
->netdev
));
2297 dpif_port
->type
= xstrdup(port
->type
);
2298 dpif_port
->port_no
= port
->port_no
;
2302 dpif_netdev_port_query_by_number(const struct dpif
*dpif
, odp_port_t port_no
,
2303 struct dpif_port
*dpif_port
)
2305 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2306 struct dp_netdev_port
*port
;
2309 ovs_mutex_lock(&dp
->port_mutex
);
2310 error
= get_port_by_number(dp
, port_no
, &port
);
2311 if (!error
&& dpif_port
) {
2312 answer_port_query(port
, dpif_port
);
2314 ovs_mutex_unlock(&dp
->port_mutex
);
2320 dpif_netdev_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
2321 struct dpif_port
*dpif_port
)
2323 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2324 struct dp_netdev_port
*port
;
2327 ovs_mutex_lock(&dp
->port_mutex
);
2328 error
= get_port_by_name(dp
, devname
, &port
);
2329 if (!error
&& dpif_port
) {
2330 answer_port_query(port
, dpif_port
);
2332 ovs_mutex_unlock(&dp
->port_mutex
);
2338 dp_netdev_flow_free(struct dp_netdev_flow
*flow
)
2340 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow
));
2341 free(flow
->dp_extra_info
);
2345 static void dp_netdev_flow_unref(struct dp_netdev_flow
*flow
)
2347 if (ovs_refcount_unref_relaxed(&flow
->ref_cnt
) == 1) {
2348 ovsrcu_postpone(dp_netdev_flow_free
, flow
);
2353 dp_netdev_flow_hash(const ovs_u128
*ufid
)
2355 return ufid
->u32
[0];
2358 static inline struct dpcls
*
2359 dp_netdev_pmd_lookup_dpcls(struct dp_netdev_pmd_thread
*pmd
,
2363 uint32_t hash
= hash_port_no(in_port
);
2364 CMAP_FOR_EACH_WITH_HASH (cls
, node
, hash
, &pmd
->classifiers
) {
2365 if (cls
->in_port
== in_port
) {
2366 /* Port classifier exists already */
2373 static inline struct dpcls
*
2374 dp_netdev_pmd_find_dpcls(struct dp_netdev_pmd_thread
*pmd
,
2376 OVS_REQUIRES(pmd
->flow_mutex
)
2378 struct dpcls
*cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
2379 uint32_t hash
= hash_port_no(in_port
);
2382 /* Create new classifier for in_port */
2383 cls
= xmalloc(sizeof(*cls
));
2385 cls
->in_port
= in_port
;
2386 cmap_insert(&pmd
->classifiers
, &cls
->node
, hash
);
2387 VLOG_DBG("Creating dpcls %p for in_port %d", cls
, in_port
);
2392 #define MAX_FLOW_MARK (UINT32_MAX - 1)
2393 #define INVALID_FLOW_MARK 0
2394 /* Zero flow mark is used to indicate the HW to remove the mark. A packet
2395 * marked with zero mark is received in SW without a mark at all, so it
2396 * cannot be used as a valid mark.
2399 struct megaflow_to_mark_data
{
2400 const struct cmap_node node
;
2406 struct cmap megaflow_to_mark
;
2407 struct cmap mark_to_flow
;
2408 struct id_pool
*pool
;
2411 static struct flow_mark flow_mark
= {
2412 .megaflow_to_mark
= CMAP_INITIALIZER
,
2413 .mark_to_flow
= CMAP_INITIALIZER
,
2417 flow_mark_alloc(void)
2421 if (!flow_mark
.pool
) {
2422 /* Haven't initiated yet, do it here */
2423 flow_mark
.pool
= id_pool_create(1, MAX_FLOW_MARK
);
2426 if (id_pool_alloc_id(flow_mark
.pool
, &mark
)) {
2430 return INVALID_FLOW_MARK
;
2434 flow_mark_free(uint32_t mark
)
2436 id_pool_free_id(flow_mark
.pool
, mark
);
2439 /* associate megaflow with a mark, which is a 1:1 mapping */
2441 megaflow_to_mark_associate(const ovs_u128
*mega_ufid
, uint32_t mark
)
2443 size_t hash
= dp_netdev_flow_hash(mega_ufid
);
2444 struct megaflow_to_mark_data
*data
= xzalloc(sizeof(*data
));
2446 data
->mega_ufid
= *mega_ufid
;
2449 cmap_insert(&flow_mark
.megaflow_to_mark
,
2450 CONST_CAST(struct cmap_node
*, &data
->node
), hash
);
2453 /* disassociate meagaflow with a mark */
2455 megaflow_to_mark_disassociate(const ovs_u128
*mega_ufid
)
2457 size_t hash
= dp_netdev_flow_hash(mega_ufid
);
2458 struct megaflow_to_mark_data
*data
;
2460 CMAP_FOR_EACH_WITH_HASH (data
, node
, hash
, &flow_mark
.megaflow_to_mark
) {
2461 if (ovs_u128_equals(*mega_ufid
, data
->mega_ufid
)) {
2462 cmap_remove(&flow_mark
.megaflow_to_mark
,
2463 CONST_CAST(struct cmap_node
*, &data
->node
), hash
);
2464 ovsrcu_postpone(free
, data
);
2469 VLOG_WARN("Masked ufid "UUID_FMT
" is not associated with a mark?\n",
2470 UUID_ARGS((struct uuid
*)mega_ufid
));
2473 static inline uint32_t
2474 megaflow_to_mark_find(const ovs_u128
*mega_ufid
)
2476 size_t hash
= dp_netdev_flow_hash(mega_ufid
);
2477 struct megaflow_to_mark_data
*data
;
2479 CMAP_FOR_EACH_WITH_HASH (data
, node
, hash
, &flow_mark
.megaflow_to_mark
) {
2480 if (ovs_u128_equals(*mega_ufid
, data
->mega_ufid
)) {
2485 VLOG_DBG("Mark id for ufid "UUID_FMT
" was not found\n",
2486 UUID_ARGS((struct uuid
*)mega_ufid
));
2487 return INVALID_FLOW_MARK
;
2490 /* associate mark with a flow, which is 1:N mapping */
2492 mark_to_flow_associate(const uint32_t mark
, struct dp_netdev_flow
*flow
)
2494 dp_netdev_flow_ref(flow
);
2496 cmap_insert(&flow_mark
.mark_to_flow
,
2497 CONST_CAST(struct cmap_node
*, &flow
->mark_node
),
2501 VLOG_DBG("Associated dp_netdev flow %p with mark %u mega_ufid "UUID_FMT
,
2502 flow
, mark
, UUID_ARGS((struct uuid
*) &flow
->mega_ufid
));
2506 flow_mark_has_no_ref(uint32_t mark
)
2508 struct dp_netdev_flow
*flow
;
2510 CMAP_FOR_EACH_WITH_HASH (flow
, mark_node
, hash_int(mark
, 0),
2511 &flow_mark
.mark_to_flow
) {
2512 if (flow
->mark
== mark
) {
2521 mark_to_flow_disassociate(struct dp_netdev_pmd_thread
*pmd
,
2522 struct dp_netdev_flow
*flow
)
2524 const char *dpif_type_str
= dpif_normalize_type(pmd
->dp
->class->type
);
2525 struct cmap_node
*mark_node
= CONST_CAST(struct cmap_node
*,
2527 uint32_t mark
= flow
->mark
;
2530 /* INVALID_FLOW_MARK may mean that the flow has been disassociated or
2531 * never associated. */
2532 if (OVS_UNLIKELY(mark
== INVALID_FLOW_MARK
)) {
2536 cmap_remove(&flow_mark
.mark_to_flow
, mark_node
, hash_int(mark
, 0));
2537 flow
->mark
= INVALID_FLOW_MARK
;
2540 * no flow is referencing the mark any more? If so, let's
2541 * remove the flow from hardware and free the mark.
2543 if (flow_mark_has_no_ref(mark
)) {
2544 struct netdev
*port
;
2545 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
2547 port
= netdev_ports_get(in_port
, dpif_type_str
);
2549 /* Taking a global 'port_mutex' to fulfill thread safety
2550 * restrictions for the netdev-offload-dpdk module. */
2551 ovs_mutex_lock(&pmd
->dp
->port_mutex
);
2552 ret
= netdev_flow_del(port
, &flow
->mega_ufid
, NULL
);
2553 ovs_mutex_unlock(&pmd
->dp
->port_mutex
);
2557 flow_mark_free(mark
);
2558 VLOG_DBG("Freed flow mark %u mega_ufid "UUID_FMT
, mark
,
2559 UUID_ARGS((struct uuid
*) &flow
->mega_ufid
));
2561 megaflow_to_mark_disassociate(&flow
->mega_ufid
);
2563 dp_netdev_flow_unref(flow
);
2569 flow_mark_flush(struct dp_netdev_pmd_thread
*pmd
)
2571 struct dp_netdev_flow
*flow
;
2573 CMAP_FOR_EACH (flow
, mark_node
, &flow_mark
.mark_to_flow
) {
2574 if (flow
->pmd_id
== pmd
->core_id
) {
2575 queue_netdev_flow_del(pmd
, flow
);
2580 static struct dp_netdev_flow
*
2581 mark_to_flow_find(const struct dp_netdev_pmd_thread
*pmd
,
2582 const uint32_t mark
)
2584 struct dp_netdev_flow
*flow
;
2586 CMAP_FOR_EACH_WITH_HASH (flow
, mark_node
, hash_int(mark
, 0),
2587 &flow_mark
.mark_to_flow
) {
2588 if (flow
->mark
== mark
&& flow
->pmd_id
== pmd
->core_id
&&
2589 flow
->dead
== false) {
2597 static struct dp_flow_offload_item
*
2598 dp_netdev_alloc_flow_offload(struct dp_netdev_pmd_thread
*pmd
,
2599 struct dp_netdev_flow
*flow
,
2602 struct dp_flow_offload_item
*offload
;
2604 offload
= xzalloc(sizeof(*offload
));
2606 offload
->flow
= flow
;
2609 dp_netdev_flow_ref(flow
);
2610 dp_netdev_pmd_try_ref(pmd
);
2616 dp_netdev_free_flow_offload(struct dp_flow_offload_item
*offload
)
2618 dp_netdev_pmd_unref(offload
->pmd
);
2619 dp_netdev_flow_unref(offload
->flow
);
2621 free(offload
->actions
);
2626 dp_netdev_append_flow_offload(struct dp_flow_offload_item
*offload
)
2628 ovs_mutex_lock(&dp_flow_offload
.mutex
);
2629 ovs_list_push_back(&dp_flow_offload
.list
, &offload
->node
);
2630 xpthread_cond_signal(&dp_flow_offload
.cond
);
2631 ovs_mutex_unlock(&dp_flow_offload
.mutex
);
2635 dp_netdev_flow_offload_del(struct dp_flow_offload_item
*offload
)
2637 return mark_to_flow_disassociate(offload
->pmd
, offload
->flow
);
2641 * There are two flow offload operations here: addition and modification.
2643 * For flow addition, this function does:
2644 * - allocate a new flow mark id
2645 * - perform hardware flow offload
2646 * - associate the flow mark with flow and mega flow
2648 * For flow modification, both flow mark and the associations are still
2649 * valid, thus only item 2 needed.
2652 dp_netdev_flow_offload_put(struct dp_flow_offload_item
*offload
)
2654 struct dp_netdev_pmd_thread
*pmd
= offload
->pmd
;
2655 struct dp_netdev_flow
*flow
= offload
->flow
;
2656 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
2657 const char *dpif_type_str
= dpif_normalize_type(pmd
->dp
->class->type
);
2658 bool modification
= offload
->op
== DP_NETDEV_FLOW_OFFLOAD_OP_MOD
;
2659 struct offload_info info
;
2660 struct netdev
*port
;
2670 ovs_assert(mark
!= INVALID_FLOW_MARK
);
2673 * If a mega flow has already been offloaded (from other PMD
2674 * instances), do not offload it again.
2676 mark
= megaflow_to_mark_find(&flow
->mega_ufid
);
2677 if (mark
!= INVALID_FLOW_MARK
) {
2678 VLOG_DBG("Flow has already been offloaded with mark %u\n", mark
);
2679 if (flow
->mark
!= INVALID_FLOW_MARK
) {
2680 ovs_assert(flow
->mark
== mark
);
2682 mark_to_flow_associate(mark
, flow
);
2687 mark
= flow_mark_alloc();
2688 if (mark
== INVALID_FLOW_MARK
) {
2689 VLOG_ERR("Failed to allocate flow mark!\n");
2693 info
.flow_mark
= mark
;
2695 port
= netdev_ports_get(in_port
, dpif_type_str
);
2696 if (!port
|| netdev_vport_is_vport_class(port
->netdev_class
)) {
2700 /* Taking a global 'port_mutex' to fulfill thread safety restrictions for
2701 * the netdev-offload-dpdk module. */
2702 ovs_mutex_lock(&pmd
->dp
->port_mutex
);
2703 ret
= netdev_flow_put(port
, &offload
->match
,
2704 CONST_CAST(struct nlattr
*, offload
->actions
),
2705 offload
->actions_len
, &flow
->mega_ufid
, &info
,
2707 ovs_mutex_unlock(&pmd
->dp
->port_mutex
);
2714 if (!modification
) {
2715 megaflow_to_mark_associate(&flow
->mega_ufid
, mark
);
2716 mark_to_flow_associate(mark
, flow
);
2721 if (!modification
) {
2722 flow_mark_free(mark
);
2724 mark_to_flow_disassociate(pmd
, flow
);
2730 dp_netdev_flow_offload_main(void *data OVS_UNUSED
)
2732 struct dp_flow_offload_item
*offload
;
2733 struct ovs_list
*list
;
2738 ovs_mutex_lock(&dp_flow_offload
.mutex
);
2739 if (ovs_list_is_empty(&dp_flow_offload
.list
)) {
2740 ovsrcu_quiesce_start();
2741 ovs_mutex_cond_wait(&dp_flow_offload
.cond
,
2742 &dp_flow_offload
.mutex
);
2743 ovsrcu_quiesce_end();
2745 list
= ovs_list_pop_front(&dp_flow_offload
.list
);
2746 offload
= CONTAINER_OF(list
, struct dp_flow_offload_item
, node
);
2747 ovs_mutex_unlock(&dp_flow_offload
.mutex
);
2749 switch (offload
->op
) {
2750 case DP_NETDEV_FLOW_OFFLOAD_OP_ADD
:
2752 ret
= dp_netdev_flow_offload_put(offload
);
2754 case DP_NETDEV_FLOW_OFFLOAD_OP_MOD
:
2756 ret
= dp_netdev_flow_offload_put(offload
);
2758 case DP_NETDEV_FLOW_OFFLOAD_OP_DEL
:
2760 ret
= dp_netdev_flow_offload_del(offload
);
2766 VLOG_DBG("%s to %s netdev flow "UUID_FMT
,
2767 ret
== 0 ? "succeed" : "failed", op
,
2768 UUID_ARGS((struct uuid
*) &offload
->flow
->mega_ufid
));
2769 dp_netdev_free_flow_offload(offload
);
2777 queue_netdev_flow_del(struct dp_netdev_pmd_thread
*pmd
,
2778 struct dp_netdev_flow
*flow
)
2780 struct dp_flow_offload_item
*offload
;
2782 if (ovsthread_once_start(&offload_thread_once
)) {
2783 xpthread_cond_init(&dp_flow_offload
.cond
, NULL
);
2784 ovs_thread_create("dp_netdev_flow_offload",
2785 dp_netdev_flow_offload_main
, NULL
);
2786 ovsthread_once_done(&offload_thread_once
);
2789 offload
= dp_netdev_alloc_flow_offload(pmd
, flow
,
2790 DP_NETDEV_FLOW_OFFLOAD_OP_DEL
);
2791 dp_netdev_append_flow_offload(offload
);
2795 queue_netdev_flow_put(struct dp_netdev_pmd_thread
*pmd
,
2796 struct dp_netdev_flow
*flow
, struct match
*match
,
2797 const struct nlattr
*actions
, size_t actions_len
)
2799 struct dp_flow_offload_item
*offload
;
2802 if (!netdev_is_flow_api_enabled()) {
2806 if (ovsthread_once_start(&offload_thread_once
)) {
2807 xpthread_cond_init(&dp_flow_offload
.cond
, NULL
);
2808 ovs_thread_create("dp_netdev_flow_offload",
2809 dp_netdev_flow_offload_main
, NULL
);
2810 ovsthread_once_done(&offload_thread_once
);
2813 if (flow
->mark
!= INVALID_FLOW_MARK
) {
2814 op
= DP_NETDEV_FLOW_OFFLOAD_OP_MOD
;
2816 op
= DP_NETDEV_FLOW_OFFLOAD_OP_ADD
;
2818 offload
= dp_netdev_alloc_flow_offload(pmd
, flow
, op
);
2819 offload
->match
= *match
;
2820 offload
->actions
= xmalloc(actions_len
);
2821 memcpy(offload
->actions
, actions
, actions_len
);
2822 offload
->actions_len
= actions_len
;
2824 dp_netdev_append_flow_offload(offload
);
2828 dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread
*pmd
,
2829 struct dp_netdev_flow
*flow
)
2830 OVS_REQUIRES(pmd
->flow_mutex
)
2832 struct cmap_node
*node
= CONST_CAST(struct cmap_node
*, &flow
->node
);
2834 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
2836 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
2837 ovs_assert(cls
!= NULL
);
2838 dpcls_remove(cls
, &flow
->cr
);
2839 cmap_remove(&pmd
->flow_table
, node
, dp_netdev_flow_hash(&flow
->ufid
));
2840 if (flow
->mark
!= INVALID_FLOW_MARK
) {
2841 queue_netdev_flow_del(pmd
, flow
);
2845 dp_netdev_flow_unref(flow
);
2849 dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
)
2851 struct dp_netdev_flow
*netdev_flow
;
2853 ovs_mutex_lock(&pmd
->flow_mutex
);
2854 CMAP_FOR_EACH (netdev_flow
, node
, &pmd
->flow_table
) {
2855 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
2857 ovs_mutex_unlock(&pmd
->flow_mutex
);
2861 dpif_netdev_flow_flush(struct dpif
*dpif
)
2863 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2864 struct dp_netdev_pmd_thread
*pmd
;
2866 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2867 dp_netdev_pmd_flow_flush(pmd
);
2873 struct dp_netdev_port_state
{
2874 struct hmap_position position
;
2879 dpif_netdev_port_dump_start(const struct dpif
*dpif OVS_UNUSED
, void **statep
)
2881 *statep
= xzalloc(sizeof(struct dp_netdev_port_state
));
2886 dpif_netdev_port_dump_next(const struct dpif
*dpif
, void *state_
,
2887 struct dpif_port
*dpif_port
)
2889 struct dp_netdev_port_state
*state
= state_
;
2890 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2891 struct hmap_node
*node
;
2894 ovs_mutex_lock(&dp
->port_mutex
);
2895 node
= hmap_at_position(&dp
->ports
, &state
->position
);
2897 struct dp_netdev_port
*port
;
2899 port
= CONTAINER_OF(node
, struct dp_netdev_port
, node
);
2902 state
->name
= xstrdup(netdev_get_name(port
->netdev
));
2903 dpif_port
->name
= state
->name
;
2904 dpif_port
->type
= port
->type
;
2905 dpif_port
->port_no
= port
->port_no
;
2911 ovs_mutex_unlock(&dp
->port_mutex
);
2917 dpif_netdev_port_dump_done(const struct dpif
*dpif OVS_UNUSED
, void *state_
)
2919 struct dp_netdev_port_state
*state
= state_
;
2926 dpif_netdev_port_poll(const struct dpif
*dpif_
, char **devnamep OVS_UNUSED
)
2928 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
2929 uint64_t new_port_seq
;
2932 new_port_seq
= seq_read(dpif
->dp
->port_seq
);
2933 if (dpif
->last_port_seq
!= new_port_seq
) {
2934 dpif
->last_port_seq
= new_port_seq
;
2944 dpif_netdev_port_poll_wait(const struct dpif
*dpif_
)
2946 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
2948 seq_wait(dpif
->dp
->port_seq
, dpif
->last_port_seq
);
2951 static struct dp_netdev_flow
*
2952 dp_netdev_flow_cast(const struct dpcls_rule
*cr
)
2954 return cr
? CONTAINER_OF(cr
, struct dp_netdev_flow
, cr
) : NULL
;
2957 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*flow
)
2959 return ovs_refcount_try_ref_rcu(&flow
->ref_cnt
);
2962 /* netdev_flow_key utilities.
2964 * netdev_flow_key is basically a miniflow. We use these functions
2965 * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow
2966 * functions (miniflow_clone_inline, miniflow_equal, ...), because:
2968 * - Since we are dealing exclusively with miniflows created by
2969 * miniflow_extract(), if the map is different the miniflow is different.
2970 * Therefore we can be faster by comparing the map and the miniflow in a
2972 * - These functions can be inlined by the compiler. */
2974 /* Given the number of bits set in miniflow's maps, returns the size of the
2975 * 'netdev_flow_key.mf' */
2976 static inline size_t
2977 netdev_flow_key_size(size_t flow_u64s
)
2979 return sizeof(struct miniflow
) + MINIFLOW_VALUES_SIZE(flow_u64s
);
2983 netdev_flow_key_equal(const struct netdev_flow_key
*a
,
2984 const struct netdev_flow_key
*b
)
2986 /* 'b->len' may be not set yet. */
2987 return a
->hash
== b
->hash
&& !memcmp(&a
->mf
, &b
->mf
, a
->len
);
2990 /* Used to compare 'netdev_flow_key' in the exact match cache to a miniflow.
2991 * The maps are compared bitwise, so both 'key->mf' and 'mf' must have been
2992 * generated by miniflow_extract. */
2994 netdev_flow_key_equal_mf(const struct netdev_flow_key
*key
,
2995 const struct miniflow
*mf
)
2997 return !memcmp(&key
->mf
, mf
, key
->len
);
3001 netdev_flow_key_clone(struct netdev_flow_key
*dst
,
3002 const struct netdev_flow_key
*src
)
3005 offsetof(struct netdev_flow_key
, mf
) + src
->len
);
3008 /* Initialize a netdev_flow_key 'mask' from 'match'. */
3010 netdev_flow_mask_init(struct netdev_flow_key
*mask
,
3011 const struct match
*match
)
3013 uint64_t *dst
= miniflow_values(&mask
->mf
);
3014 struct flowmap fmap
;
3018 /* Only check masks that make sense for the flow. */
3019 flow_wc_map(&match
->flow
, &fmap
);
3020 flowmap_init(&mask
->mf
.map
);
3022 FLOWMAP_FOR_EACH_INDEX(idx
, fmap
) {
3023 uint64_t mask_u64
= flow_u64_value(&match
->wc
.masks
, idx
);
3026 flowmap_set(&mask
->mf
.map
, idx
, 1);
3028 hash
= hash_add64(hash
, mask_u64
);
3034 FLOWMAP_FOR_EACH_MAP (map
, mask
->mf
.map
) {
3035 hash
= hash_add64(hash
, map
);
3038 size_t n
= dst
- miniflow_get_values(&mask
->mf
);
3040 mask
->hash
= hash_finish(hash
, n
* 8);
3041 mask
->len
= netdev_flow_key_size(n
);
3044 /* Initializes 'dst' as a copy of 'flow' masked with 'mask'. */
3046 netdev_flow_key_init_masked(struct netdev_flow_key
*dst
,
3047 const struct flow
*flow
,
3048 const struct netdev_flow_key
*mask
)
3050 uint64_t *dst_u64
= miniflow_values(&dst
->mf
);
3051 const uint64_t *mask_u64
= miniflow_get_values(&mask
->mf
);
3055 dst
->len
= mask
->len
;
3056 dst
->mf
= mask
->mf
; /* Copy maps. */
3058 FLOW_FOR_EACH_IN_MAPS(value
, flow
, mask
->mf
.map
) {
3059 *dst_u64
= value
& *mask_u64
++;
3060 hash
= hash_add64(hash
, *dst_u64
++);
3062 dst
->hash
= hash_finish(hash
,
3063 (dst_u64
- miniflow_get_values(&dst
->mf
)) * 8);
3067 emc_entry_alive(struct emc_entry
*ce
)
3069 return ce
->flow
&& !ce
->flow
->dead
;
3073 emc_clear_entry(struct emc_entry
*ce
)
3076 dp_netdev_flow_unref(ce
->flow
);
3082 emc_change_entry(struct emc_entry
*ce
, struct dp_netdev_flow
*flow
,
3083 const struct netdev_flow_key
*key
)
3085 if (ce
->flow
!= flow
) {
3087 dp_netdev_flow_unref(ce
->flow
);
3090 if (dp_netdev_flow_ref(flow
)) {
3097 netdev_flow_key_clone(&ce
->key
, key
);
3102 emc_insert(struct emc_cache
*cache
, const struct netdev_flow_key
*key
,
3103 struct dp_netdev_flow
*flow
)
3105 struct emc_entry
*to_be_replaced
= NULL
;
3106 struct emc_entry
*current_entry
;
3108 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
3109 if (netdev_flow_key_equal(¤t_entry
->key
, key
)) {
3110 /* We found the entry with the 'mf' miniflow */
3111 emc_change_entry(current_entry
, flow
, NULL
);
3115 /* Replacement policy: put the flow in an empty (not alive) entry, or
3116 * in the first entry where it can be */
3118 || (emc_entry_alive(to_be_replaced
)
3119 && !emc_entry_alive(current_entry
))
3120 || current_entry
->key
.hash
< to_be_replaced
->key
.hash
) {
3121 to_be_replaced
= current_entry
;
3124 /* We didn't find the miniflow in the cache.
3125 * The 'to_be_replaced' entry is where the new flow will be stored */
3127 emc_change_entry(to_be_replaced
, flow
, key
);
3131 emc_probabilistic_insert(struct dp_netdev_pmd_thread
*pmd
,
3132 const struct netdev_flow_key
*key
,
3133 struct dp_netdev_flow
*flow
)
3135 /* Insert an entry into the EMC based on probability value 'min'. By
3136 * default the value is UINT32_MAX / 100 which yields an insertion
3137 * probability of 1/100 ie. 1% */
3139 uint32_t min
= pmd
->ctx
.emc_insert_min
;
3141 if (min
&& random_uint32() <= min
) {
3142 emc_insert(&(pmd
->flow_cache
).emc_cache
, key
, flow
);
3146 static inline struct dp_netdev_flow
*
3147 emc_lookup(struct emc_cache
*cache
, const struct netdev_flow_key
*key
)
3149 struct emc_entry
*current_entry
;
3151 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
3152 if (current_entry
->key
.hash
== key
->hash
3153 && emc_entry_alive(current_entry
)
3154 && netdev_flow_key_equal_mf(¤t_entry
->key
, &key
->mf
)) {
3156 /* We found the entry with the 'key->mf' miniflow */
3157 return current_entry
->flow
;
3164 static inline const struct cmap_node
*
3165 smc_entry_get(struct dp_netdev_pmd_thread
*pmd
, const uint32_t hash
)
3167 struct smc_cache
*cache
= &(pmd
->flow_cache
).smc_cache
;
3168 struct smc_bucket
*bucket
= &cache
->buckets
[hash
& SMC_MASK
];
3169 uint16_t sig
= hash
>> 16;
3170 uint16_t index
= UINT16_MAX
;
3172 for (int i
= 0; i
< SMC_ENTRY_PER_BUCKET
; i
++) {
3173 if (bucket
->sig
[i
] == sig
) {
3174 index
= bucket
->flow_idx
[i
];
3178 if (index
!= UINT16_MAX
) {
3179 return cmap_find_by_index(&pmd
->flow_table
, index
);
3185 smc_clear_entry(struct smc_bucket
*b
, int idx
)
3187 b
->flow_idx
[idx
] = UINT16_MAX
;
3190 /* Insert the flow_table index into SMC. Insertion may fail when 1) SMC is
3191 * turned off, 2) the flow_table index is larger than uint16_t can handle.
3192 * If there is already an SMC entry having same signature, the index will be
3193 * updated. If there is no existing entry, but an empty entry is available,
3194 * the empty entry will be taken. If no empty entry or existing same signature,
3195 * a random entry from the hashed bucket will be picked. */
3197 smc_insert(struct dp_netdev_pmd_thread
*pmd
,
3198 const struct netdev_flow_key
*key
,
3201 struct smc_cache
*smc_cache
= &(pmd
->flow_cache
).smc_cache
;
3202 struct smc_bucket
*bucket
= &smc_cache
->buckets
[key
->hash
& SMC_MASK
];
3204 uint32_t cmap_index
;
3208 atomic_read_relaxed(&pmd
->dp
->smc_enable_db
, &smc_enable_db
);
3209 if (!smc_enable_db
) {
3213 cmap_index
= cmap_find_index(&pmd
->flow_table
, hash
);
3214 index
= (cmap_index
>= UINT16_MAX
) ? UINT16_MAX
: (uint16_t)cmap_index
;
3216 /* If the index is larger than SMC can handle (uint16_t), we don't
3218 if (index
== UINT16_MAX
) {
3222 /* If an entry with same signature already exists, update the index */
3223 uint16_t sig
= key
->hash
>> 16;
3224 for (i
= 0; i
< SMC_ENTRY_PER_BUCKET
; i
++) {
3225 if (bucket
->sig
[i
] == sig
) {
3226 bucket
->flow_idx
[i
] = index
;
3230 /* If there is an empty entry, occupy it. */
3231 for (i
= 0; i
< SMC_ENTRY_PER_BUCKET
; i
++) {
3232 if (bucket
->flow_idx
[i
] == UINT16_MAX
) {
3233 bucket
->sig
[i
] = sig
;
3234 bucket
->flow_idx
[i
] = index
;
3238 /* Otherwise, pick a random entry. */
3239 i
= random_uint32() % SMC_ENTRY_PER_BUCKET
;
3240 bucket
->sig
[i
] = sig
;
3241 bucket
->flow_idx
[i
] = index
;
3244 static struct dp_netdev_flow
*
3245 dp_netdev_pmd_lookup_flow(struct dp_netdev_pmd_thread
*pmd
,
3246 const struct netdev_flow_key
*key
,
3250 struct dpcls_rule
*rule
;
3251 odp_port_t in_port
= u32_to_odp(MINIFLOW_GET_U32(&key
->mf
,
3253 struct dp_netdev_flow
*netdev_flow
= NULL
;
3255 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
3256 if (OVS_LIKELY(cls
)) {
3257 dpcls_lookup(cls
, &key
, &rule
, 1, lookup_num_p
);
3258 netdev_flow
= dp_netdev_flow_cast(rule
);
3263 static struct dp_netdev_flow
*
3264 dp_netdev_pmd_find_flow(const struct dp_netdev_pmd_thread
*pmd
,
3265 const ovs_u128
*ufidp
, const struct nlattr
*key
,
3268 struct dp_netdev_flow
*netdev_flow
;
3272 /* If a UFID is not provided, determine one based on the key. */
3273 if (!ufidp
&& key
&& key_len
3274 && !dpif_netdev_flow_from_nlattrs(key
, key_len
, &flow
, false)) {
3275 odp_flow_key_hash(&flow
, sizeof flow
, &ufid
);
3280 CMAP_FOR_EACH_WITH_HASH (netdev_flow
, node
, dp_netdev_flow_hash(ufidp
),
3282 if (ovs_u128_equals(netdev_flow
->ufid
, *ufidp
)) {
3292 dp_netdev_flow_set_last_stats_attrs(struct dp_netdev_flow
*netdev_flow
,
3293 const struct dpif_flow_stats
*stats
,
3294 const struct dpif_flow_attrs
*attrs
,
3297 struct dp_netdev_flow_stats
*last_stats
= &netdev_flow
->last_stats
;
3298 struct dp_netdev_flow_attrs
*last_attrs
= &netdev_flow
->last_attrs
;
3300 atomic_store_relaxed(&netdev_flow
->netdev_flow_get_result
, result
);
3305 atomic_store_relaxed(&last_stats
->used
, stats
->used
);
3306 atomic_store_relaxed(&last_stats
->packet_count
, stats
->n_packets
);
3307 atomic_store_relaxed(&last_stats
->byte_count
, stats
->n_bytes
);
3308 atomic_store_relaxed(&last_stats
->tcp_flags
, stats
->tcp_flags
);
3310 atomic_store_relaxed(&last_attrs
->offloaded
, attrs
->offloaded
);
3311 atomic_store_relaxed(&last_attrs
->dp_layer
, attrs
->dp_layer
);
3316 dp_netdev_flow_get_last_stats_attrs(struct dp_netdev_flow
*netdev_flow
,
3317 struct dpif_flow_stats
*stats
,
3318 struct dpif_flow_attrs
*attrs
,
3321 struct dp_netdev_flow_stats
*last_stats
= &netdev_flow
->last_stats
;
3322 struct dp_netdev_flow_attrs
*last_attrs
= &netdev_flow
->last_attrs
;
3324 atomic_read_relaxed(&netdev_flow
->netdev_flow_get_result
, result
);
3329 atomic_read_relaxed(&last_stats
->used
, &stats
->used
);
3330 atomic_read_relaxed(&last_stats
->packet_count
, &stats
->n_packets
);
3331 atomic_read_relaxed(&last_stats
->byte_count
, &stats
->n_bytes
);
3332 atomic_read_relaxed(&last_stats
->tcp_flags
, &stats
->tcp_flags
);
3334 atomic_read_relaxed(&last_attrs
->offloaded
, &attrs
->offloaded
);
3335 atomic_read_relaxed(&last_attrs
->dp_layer
, &attrs
->dp_layer
);
3339 dpif_netdev_get_flow_offload_status(const struct dp_netdev
*dp
,
3340 struct dp_netdev_flow
*netdev_flow
,
3341 struct dpif_flow_stats
*stats
,
3342 struct dpif_flow_attrs
*attrs
)
3344 uint64_t act_buf
[1024 / 8];
3345 struct nlattr
*actions
;
3346 struct netdev
*netdev
;
3352 if (!netdev_is_flow_api_enabled()) {
3356 netdev
= netdev_ports_get(netdev_flow
->flow
.in_port
.odp_port
,
3357 dpif_normalize_type(dp
->class->type
));
3361 ofpbuf_use_stack(&buf
, &act_buf
, sizeof act_buf
);
3362 /* Taking a global 'port_mutex' to fulfill thread safety
3363 * restrictions for the netdev-offload-dpdk module.
3365 * XXX: Main thread will try to pause/stop all revalidators during datapath
3366 * reconfiguration via datapath purge callback (dp_purge_cb) while
3367 * holding 'dp->port_mutex'. So we're not waiting for mutex here.
3368 * Otherwise, deadlock is possible, bcause revalidators might sleep
3369 * waiting for the main thread to release the lock and main thread
3370 * will wait for them to stop processing.
3371 * This workaround might make statistics less accurate. Especially
3372 * for flow deletion case, since there will be no other attempt. */
3373 if (!ovs_mutex_trylock(&dp
->port_mutex
)) {
3374 ret
= netdev_flow_get(netdev
, &match
, &actions
,
3375 &netdev_flow
->mega_ufid
, stats
, attrs
, &buf
);
3376 /* Storing statistics and attributes from the last request for
3377 * later use on mutex contention. */
3378 dp_netdev_flow_set_last_stats_attrs(netdev_flow
, stats
, attrs
, ret
);
3379 ovs_mutex_unlock(&dp
->port_mutex
);
3381 dp_netdev_flow_get_last_stats_attrs(netdev_flow
, stats
, attrs
, &ret
);
3382 if (!ret
&& !attrs
->dp_layer
) {
3383 /* Flow was never reported as 'offloaded' so it's harmless
3384 * to continue to think so. */
3388 netdev_close(netdev
);
3397 get_dpif_flow_status(const struct dp_netdev
*dp
,
3398 const struct dp_netdev_flow
*netdev_flow_
,
3399 struct dpif_flow_stats
*stats
,
3400 struct dpif_flow_attrs
*attrs
)
3402 struct dpif_flow_stats offload_stats
;
3403 struct dpif_flow_attrs offload_attrs
;
3404 struct dp_netdev_flow
*netdev_flow
;
3405 unsigned long long n
;
3409 netdev_flow
= CONST_CAST(struct dp_netdev_flow
*, netdev_flow_
);
3411 atomic_read_relaxed(&netdev_flow
->stats
.packet_count
, &n
);
3412 stats
->n_packets
= n
;
3413 atomic_read_relaxed(&netdev_flow
->stats
.byte_count
, &n
);
3415 atomic_read_relaxed(&netdev_flow
->stats
.used
, &used
);
3417 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
3418 stats
->tcp_flags
= flags
;
3420 if (dpif_netdev_get_flow_offload_status(dp
, netdev_flow
,
3421 &offload_stats
, &offload_attrs
)) {
3422 stats
->n_packets
+= offload_stats
.n_packets
;
3423 stats
->n_bytes
+= offload_stats
.n_bytes
;
3424 stats
->used
= MAX(stats
->used
, offload_stats
.used
);
3425 stats
->tcp_flags
|= offload_stats
.tcp_flags
;
3427 attrs
->offloaded
= offload_attrs
.offloaded
;
3428 attrs
->dp_layer
= offload_attrs
.dp_layer
;
3431 attrs
->offloaded
= false;
3432 attrs
->dp_layer
= "ovs";
3436 /* Converts to the dpif_flow format, using 'key_buf' and 'mask_buf' for
3437 * storing the netlink-formatted key/mask. 'key_buf' may be the same as
3438 * 'mask_buf'. Actions will be returned without copying, by relying on RCU to
3441 dp_netdev_flow_to_dpif_flow(const struct dp_netdev
*dp
,
3442 const struct dp_netdev_flow
*netdev_flow
,
3443 struct ofpbuf
*key_buf
, struct ofpbuf
*mask_buf
,
3444 struct dpif_flow
*flow
, bool terse
)
3447 memset(flow
, 0, sizeof *flow
);
3449 struct flow_wildcards wc
;
3450 struct dp_netdev_actions
*actions
;
3452 struct odp_flow_key_parms odp_parms
= {
3453 .flow
= &netdev_flow
->flow
,
3455 .support
= dp_netdev_support
,
3458 miniflow_expand(&netdev_flow
->cr
.mask
->mf
, &wc
.masks
);
3459 /* in_port is exact matched, but we have left it out from the mask for
3460 * optimnization reasons. Add in_port back to the mask. */
3461 wc
.masks
.in_port
.odp_port
= ODPP_NONE
;
3464 offset
= key_buf
->size
;
3465 flow
->key
= ofpbuf_tail(key_buf
);
3466 odp_flow_key_from_flow(&odp_parms
, key_buf
);
3467 flow
->key_len
= key_buf
->size
- offset
;
3470 offset
= mask_buf
->size
;
3471 flow
->mask
= ofpbuf_tail(mask_buf
);
3472 odp_parms
.key_buf
= key_buf
;
3473 odp_flow_key_from_mask(&odp_parms
, mask_buf
);
3474 flow
->mask_len
= mask_buf
->size
- offset
;
3477 actions
= dp_netdev_flow_get_actions(netdev_flow
);
3478 flow
->actions
= actions
->actions
;
3479 flow
->actions_len
= actions
->size
;
3482 flow
->ufid
= netdev_flow
->ufid
;
3483 flow
->ufid_present
= true;
3484 flow
->pmd_id
= netdev_flow
->pmd_id
;
3486 get_dpif_flow_status(dp
, netdev_flow
, &flow
->stats
, &flow
->attrs
);
3487 flow
->attrs
.dp_extra_info
= netdev_flow
->dp_extra_info
;
3491 dpif_netdev_mask_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
3492 const struct nlattr
*mask_key
,
3493 uint32_t mask_key_len
, const struct flow
*flow
,
3494 struct flow_wildcards
*wc
, bool probe
)
3496 enum odp_key_fitness fitness
;
3498 fitness
= odp_flow_key_to_mask(mask_key
, mask_key_len
, wc
, flow
, NULL
);
3501 /* This should not happen: it indicates that
3502 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
3503 * disagree on the acceptable form of a mask. Log the problem
3504 * as an error, with enough details to enable debugging. */
3505 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3507 if (!VLOG_DROP_ERR(&rl
)) {
3511 odp_flow_format(key
, key_len
, mask_key
, mask_key_len
, NULL
, &s
,
3513 VLOG_ERR("internal error parsing flow mask %s (%s)",
3514 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
3526 dpif_netdev_flow_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
3527 struct flow
*flow
, bool probe
)
3529 if (odp_flow_key_to_flow(key
, key_len
, flow
, NULL
)) {
3531 /* This should not happen: it indicates that
3532 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
3533 * the acceptable form of a flow. Log the problem as an error,
3534 * with enough details to enable debugging. */
3535 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3537 if (!VLOG_DROP_ERR(&rl
)) {
3541 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
3542 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
3550 if (flow
->ct_state
& DP_NETDEV_CS_UNSUPPORTED_MASK
) {
3558 dpif_netdev_flow_get(const struct dpif
*dpif
, const struct dpif_flow_get
*get
)
3560 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3561 struct dp_netdev_flow
*netdev_flow
;
3562 struct dp_netdev_pmd_thread
*pmd
;
3563 struct hmapx to_find
= HMAPX_INITIALIZER(&to_find
);
3564 struct hmapx_node
*node
;
3567 if (get
->pmd_id
== PMD_ID_NULL
) {
3568 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3569 if (dp_netdev_pmd_try_ref(pmd
) && !hmapx_add(&to_find
, pmd
)) {
3570 dp_netdev_pmd_unref(pmd
);
3574 pmd
= dp_netdev_get_pmd(dp
, get
->pmd_id
);
3578 hmapx_add(&to_find
, pmd
);
3581 if (!hmapx_count(&to_find
)) {
3585 HMAPX_FOR_EACH (node
, &to_find
) {
3586 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3587 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, get
->ufid
, get
->key
,
3590 dp_netdev_flow_to_dpif_flow(dp
, netdev_flow
, get
->buffer
,
3591 get
->buffer
, get
->flow
, false);
3599 HMAPX_FOR_EACH (node
, &to_find
) {
3600 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3601 dp_netdev_pmd_unref(pmd
);
3604 hmapx_destroy(&to_find
);
3609 dp_netdev_get_mega_ufid(const struct match
*match
, ovs_u128
*mega_ufid
)
3611 struct flow masked_flow
;
3614 for (i
= 0; i
< sizeof(struct flow
); i
++) {
3615 ((uint8_t *)&masked_flow
)[i
] = ((uint8_t *)&match
->flow
)[i
] &
3616 ((uint8_t *)&match
->wc
)[i
];
3618 odp_flow_key_hash(&masked_flow
, sizeof masked_flow
, mega_ufid
);
3621 static struct dp_netdev_flow
*
3622 dp_netdev_flow_add(struct dp_netdev_pmd_thread
*pmd
,
3623 struct match
*match
, const ovs_u128
*ufid
,
3624 const struct nlattr
*actions
, size_t actions_len
)
3625 OVS_REQUIRES(pmd
->flow_mutex
)
3627 struct ds extra_info
= DS_EMPTY_INITIALIZER
;
3628 struct dp_netdev_flow
*flow
;
3629 struct netdev_flow_key mask
;
3633 /* Make sure in_port is exact matched before we read it. */
3634 ovs_assert(match
->wc
.masks
.in_port
.odp_port
== ODPP_NONE
);
3635 odp_port_t in_port
= match
->flow
.in_port
.odp_port
;
3637 /* As we select the dpcls based on the port number, each netdev flow
3638 * belonging to the same dpcls will have the same odp_port value.
3639 * For performance reasons we wildcard odp_port here in the mask. In the
3640 * typical case dp_hash is also wildcarded, and the resulting 8-byte
3641 * chunk {dp_hash, in_port} will be ignored by netdev_flow_mask_init() and
3642 * will not be part of the subtable mask.
3643 * This will speed up the hash computation during dpcls_lookup() because
3644 * there is one less call to hash_add64() in this case. */
3645 match
->wc
.masks
.in_port
.odp_port
= 0;
3646 netdev_flow_mask_init(&mask
, match
);
3647 match
->wc
.masks
.in_port
.odp_port
= ODPP_NONE
;
3649 /* Make sure wc does not have metadata. */
3650 ovs_assert(!FLOWMAP_HAS_FIELD(&mask
.mf
.map
, metadata
)
3651 && !FLOWMAP_HAS_FIELD(&mask
.mf
.map
, regs
));
3653 /* Do not allocate extra space. */
3654 flow
= xmalloc(sizeof *flow
- sizeof flow
->cr
.flow
.mf
+ mask
.len
);
3655 memset(&flow
->stats
, 0, sizeof flow
->stats
);
3656 atomic_init(&flow
->netdev_flow_get_result
, 0);
3657 memset(&flow
->last_stats
, 0, sizeof flow
->last_stats
);
3658 memset(&flow
->last_attrs
, 0, sizeof flow
->last_attrs
);
3661 flow
->mark
= INVALID_FLOW_MARK
;
3662 *CONST_CAST(unsigned *, &flow
->pmd_id
) = pmd
->core_id
;
3663 *CONST_CAST(struct flow
*, &flow
->flow
) = match
->flow
;
3664 *CONST_CAST(ovs_u128
*, &flow
->ufid
) = *ufid
;
3665 ovs_refcount_init(&flow
->ref_cnt
);
3666 ovsrcu_set(&flow
->actions
, dp_netdev_actions_create(actions
, actions_len
));
3668 dp_netdev_get_mega_ufid(match
, CONST_CAST(ovs_u128
*, &flow
->mega_ufid
));
3669 netdev_flow_key_init_masked(&flow
->cr
.flow
, &match
->flow
, &mask
);
3671 /* Select dpcls for in_port. Relies on in_port to be exact match. */
3672 cls
= dp_netdev_pmd_find_dpcls(pmd
, in_port
);
3673 dpcls_insert(cls
, &flow
->cr
, &mask
);
3675 ds_put_cstr(&extra_info
, "miniflow_bits(");
3676 FLOWMAP_FOR_EACH_UNIT (unit
) {
3678 ds_put_char(&extra_info
, ',');
3680 ds_put_format(&extra_info
, "%d",
3681 count_1bits(flow
->cr
.mask
->mf
.map
.bits
[unit
]));
3683 ds_put_char(&extra_info
, ')');
3684 flow
->dp_extra_info
= ds_steal_cstr(&extra_info
);
3685 ds_destroy(&extra_info
);
3687 cmap_insert(&pmd
->flow_table
, CONST_CAST(struct cmap_node
*, &flow
->node
),
3688 dp_netdev_flow_hash(&flow
->ufid
));
3690 queue_netdev_flow_put(pmd
, flow
, match
, actions
, actions_len
);
3692 if (OVS_UNLIKELY(!VLOG_DROP_DBG((&upcall_rl
)))) {
3693 struct ds ds
= DS_EMPTY_INITIALIZER
;
3694 struct ofpbuf key_buf
, mask_buf
;
3695 struct odp_flow_key_parms odp_parms
= {
3696 .flow
= &match
->flow
,
3697 .mask
= &match
->wc
.masks
,
3698 .support
= dp_netdev_support
,
3701 ofpbuf_init(&key_buf
, 0);
3702 ofpbuf_init(&mask_buf
, 0);
3704 odp_flow_key_from_flow(&odp_parms
, &key_buf
);
3705 odp_parms
.key_buf
= &key_buf
;
3706 odp_flow_key_from_mask(&odp_parms
, &mask_buf
);
3708 ds_put_cstr(&ds
, "flow_add: ");
3709 odp_format_ufid(ufid
, &ds
);
3710 ds_put_cstr(&ds
, " mega_");
3711 odp_format_ufid(&flow
->mega_ufid
, &ds
);
3712 ds_put_cstr(&ds
, " ");
3713 odp_flow_format(key_buf
.data
, key_buf
.size
,
3714 mask_buf
.data
, mask_buf
.size
,
3716 ds_put_cstr(&ds
, ", actions:");
3717 format_odp_actions(&ds
, actions
, actions_len
, NULL
);
3719 VLOG_DBG("%s", ds_cstr(&ds
));
3721 ofpbuf_uninit(&key_buf
);
3722 ofpbuf_uninit(&mask_buf
);
3724 /* Add a printout of the actual match installed. */
3727 ds_put_cstr(&ds
, "flow match: ");
3728 miniflow_expand(&flow
->cr
.flow
.mf
, &m
.flow
);
3729 miniflow_expand(&flow
->cr
.mask
->mf
, &m
.wc
.masks
);
3730 memset(&m
.tun_md
, 0, sizeof m
.tun_md
);
3731 match_format(&m
, NULL
, &ds
, OFP_DEFAULT_PRIORITY
);
3733 VLOG_DBG("%s", ds_cstr(&ds
));
3742 flow_put_on_pmd(struct dp_netdev_pmd_thread
*pmd
,
3743 struct netdev_flow_key
*key
,
3744 struct match
*match
,
3746 const struct dpif_flow_put
*put
,
3747 struct dpif_flow_stats
*stats
)
3749 struct dp_netdev_flow
*netdev_flow
;
3753 memset(stats
, 0, sizeof *stats
);
3756 ovs_mutex_lock(&pmd
->flow_mutex
);
3757 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
, NULL
);
3759 if (put
->flags
& DPIF_FP_CREATE
) {
3760 dp_netdev_flow_add(pmd
, match
, ufid
, put
->actions
,
3766 if (put
->flags
& DPIF_FP_MODIFY
) {
3767 struct dp_netdev_actions
*new_actions
;
3768 struct dp_netdev_actions
*old_actions
;
3770 new_actions
= dp_netdev_actions_create(put
->actions
,
3773 old_actions
= dp_netdev_flow_get_actions(netdev_flow
);
3774 ovsrcu_set(&netdev_flow
->actions
, new_actions
);
3776 queue_netdev_flow_put(pmd
, netdev_flow
, match
,
3777 put
->actions
, put
->actions_len
);
3780 get_dpif_flow_status(pmd
->dp
, netdev_flow
, stats
, NULL
);
3782 if (put
->flags
& DPIF_FP_ZERO_STATS
) {
3783 /* XXX: The userspace datapath uses thread local statistics
3784 * (for flows), which should be updated only by the owning
3785 * thread. Since we cannot write on stats memory here,
3786 * we choose not to support this flag. Please note:
3787 * - This feature is currently used only by dpctl commands with
3789 * - Should the need arise, this operation can be implemented
3790 * by keeping a base value (to be update here) for each
3791 * counter, and subtracting it before outputting the stats */
3795 ovsrcu_postpone(dp_netdev_actions_free
, old_actions
);
3796 } else if (put
->flags
& DPIF_FP_CREATE
) {
3799 /* Overlapping flow. */
3803 ovs_mutex_unlock(&pmd
->flow_mutex
);
3808 dpif_netdev_flow_put(struct dpif
*dpif
, const struct dpif_flow_put
*put
)
3810 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3811 struct netdev_flow_key key
, mask
;
3812 struct dp_netdev_pmd_thread
*pmd
;
3816 bool probe
= put
->flags
& DPIF_FP_PROBE
;
3819 memset(put
->stats
, 0, sizeof *put
->stats
);
3821 error
= dpif_netdev_flow_from_nlattrs(put
->key
, put
->key_len
, &match
.flow
,
3826 error
= dpif_netdev_mask_from_nlattrs(put
->key
, put
->key_len
,
3827 put
->mask
, put
->mask_len
,
3828 &match
.flow
, &match
.wc
, probe
);
3836 odp_flow_key_hash(&match
.flow
, sizeof match
.flow
, &ufid
);
3839 /* The Netlink encoding of datapath flow keys cannot express
3840 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
3841 * tag is interpreted as exact match on the fact that there is no
3842 * VLAN. Unless we refactor a lot of code that translates between
3843 * Netlink and struct flow representations, we have to do the same
3844 * here. This must be in sync with 'match' in handle_packet_upcall(). */
3845 if (!match
.wc
.masks
.vlans
[0].tci
) {
3846 match
.wc
.masks
.vlans
[0].tci
= htons(0xffff);
3849 /* Must produce a netdev_flow_key for lookup.
3850 * Use the same method as employed to create the key when adding
3851 * the flow to the dplcs to make sure they match. */
3852 netdev_flow_mask_init(&mask
, &match
);
3853 netdev_flow_key_init_masked(&key
, &match
.flow
, &mask
);
3855 if (put
->pmd_id
== PMD_ID_NULL
) {
3856 if (cmap_count(&dp
->poll_threads
) == 0) {
3859 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3860 struct dpif_flow_stats pmd_stats
;
3863 pmd_error
= flow_put_on_pmd(pmd
, &key
, &match
, &ufid
, put
,
3867 } else if (put
->stats
) {
3868 put
->stats
->n_packets
+= pmd_stats
.n_packets
;
3869 put
->stats
->n_bytes
+= pmd_stats
.n_bytes
;
3870 put
->stats
->used
= MAX(put
->stats
->used
, pmd_stats
.used
);
3871 put
->stats
->tcp_flags
|= pmd_stats
.tcp_flags
;
3875 pmd
= dp_netdev_get_pmd(dp
, put
->pmd_id
);
3879 error
= flow_put_on_pmd(pmd
, &key
, &match
, &ufid
, put
, put
->stats
);
3880 dp_netdev_pmd_unref(pmd
);
3887 flow_del_on_pmd(struct dp_netdev_pmd_thread
*pmd
,
3888 struct dpif_flow_stats
*stats
,
3889 const struct dpif_flow_del
*del
)
3891 struct dp_netdev_flow
*netdev_flow
;
3894 ovs_mutex_lock(&pmd
->flow_mutex
);
3895 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, del
->ufid
, del
->key
,
3899 get_dpif_flow_status(pmd
->dp
, netdev_flow
, stats
, NULL
);
3901 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
3905 ovs_mutex_unlock(&pmd
->flow_mutex
);
3911 dpif_netdev_flow_del(struct dpif
*dpif
, const struct dpif_flow_del
*del
)
3913 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3914 struct dp_netdev_pmd_thread
*pmd
;
3918 memset(del
->stats
, 0, sizeof *del
->stats
);
3921 if (del
->pmd_id
== PMD_ID_NULL
) {
3922 if (cmap_count(&dp
->poll_threads
) == 0) {
3925 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3926 struct dpif_flow_stats pmd_stats
;
3929 pmd_error
= flow_del_on_pmd(pmd
, &pmd_stats
, del
);
3932 } else if (del
->stats
) {
3933 del
->stats
->n_packets
+= pmd_stats
.n_packets
;
3934 del
->stats
->n_bytes
+= pmd_stats
.n_bytes
;
3935 del
->stats
->used
= MAX(del
->stats
->used
, pmd_stats
.used
);
3936 del
->stats
->tcp_flags
|= pmd_stats
.tcp_flags
;
3940 pmd
= dp_netdev_get_pmd(dp
, del
->pmd_id
);
3944 error
= flow_del_on_pmd(pmd
, del
->stats
, del
);
3945 dp_netdev_pmd_unref(pmd
);
3952 struct dpif_netdev_flow_dump
{
3953 struct dpif_flow_dump up
;
3954 struct cmap_position poll_thread_pos
;
3955 struct cmap_position flow_pos
;
3956 struct dp_netdev_pmd_thread
*cur_pmd
;
3958 struct ovs_mutex mutex
;
3961 static struct dpif_netdev_flow_dump
*
3962 dpif_netdev_flow_dump_cast(struct dpif_flow_dump
*dump
)
3964 return CONTAINER_OF(dump
, struct dpif_netdev_flow_dump
, up
);
3967 static struct dpif_flow_dump
*
3968 dpif_netdev_flow_dump_create(const struct dpif
*dpif_
, bool terse
,
3969 struct dpif_flow_dump_types
*types OVS_UNUSED
)
3971 struct dpif_netdev_flow_dump
*dump
;
3973 dump
= xzalloc(sizeof *dump
);
3974 dpif_flow_dump_init(&dump
->up
, dpif_
);
3975 dump
->up
.terse
= terse
;
3976 ovs_mutex_init(&dump
->mutex
);
3982 dpif_netdev_flow_dump_destroy(struct dpif_flow_dump
*dump_
)
3984 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
3986 ovs_mutex_destroy(&dump
->mutex
);
3991 struct dpif_netdev_flow_dump_thread
{
3992 struct dpif_flow_dump_thread up
;
3993 struct dpif_netdev_flow_dump
*dump
;
3994 struct odputil_keybuf keybuf
[FLOW_DUMP_MAX_BATCH
];
3995 struct odputil_keybuf maskbuf
[FLOW_DUMP_MAX_BATCH
];
3998 static struct dpif_netdev_flow_dump_thread
*
3999 dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread
*thread
)
4001 return CONTAINER_OF(thread
, struct dpif_netdev_flow_dump_thread
, up
);
4004 static struct dpif_flow_dump_thread
*
4005 dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump
*dump_
)
4007 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
4008 struct dpif_netdev_flow_dump_thread
*thread
;
4010 thread
= xmalloc(sizeof *thread
);
4011 dpif_flow_dump_thread_init(&thread
->up
, &dump
->up
);
4012 thread
->dump
= dump
;
4017 dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread
*thread_
)
4019 struct dpif_netdev_flow_dump_thread
*thread
4020 = dpif_netdev_flow_dump_thread_cast(thread_
);
4026 dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread
*thread_
,
4027 struct dpif_flow
*flows
, int max_flows
)
4029 struct dpif_netdev_flow_dump_thread
*thread
4030 = dpif_netdev_flow_dump_thread_cast(thread_
);
4031 struct dpif_netdev_flow_dump
*dump
= thread
->dump
;
4032 struct dp_netdev_flow
*netdev_flows
[FLOW_DUMP_MAX_BATCH
];
4033 struct dpif_netdev
*dpif
= dpif_netdev_cast(thread
->up
.dpif
);
4034 struct dp_netdev
*dp
= get_dp_netdev(&dpif
->dpif
);
4038 ovs_mutex_lock(&dump
->mutex
);
4039 if (!dump
->status
) {
4040 struct dp_netdev_pmd_thread
*pmd
= dump
->cur_pmd
;
4041 int flow_limit
= MIN(max_flows
, FLOW_DUMP_MAX_BATCH
);
4043 /* First call to dump_next(), extracts the first pmd thread.
4044 * If there is no pmd thread, returns immediately. */
4046 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
4048 ovs_mutex_unlock(&dump
->mutex
);
4055 for (n_flows
= 0; n_flows
< flow_limit
; n_flows
++) {
4056 struct cmap_node
*node
;
4058 node
= cmap_next_position(&pmd
->flow_table
, &dump
->flow_pos
);
4062 netdev_flows
[n_flows
] = CONTAINER_OF(node
,
4063 struct dp_netdev_flow
,
4066 /* When finishing dumping the current pmd thread, moves to
4068 if (n_flows
< flow_limit
) {
4069 memset(&dump
->flow_pos
, 0, sizeof dump
->flow_pos
);
4070 dp_netdev_pmd_unref(pmd
);
4071 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
4077 /* Keeps the reference to next caller. */
4078 dump
->cur_pmd
= pmd
;
4080 /* If the current dump is empty, do not exit the loop, since the
4081 * remaining pmds could have flows to be dumped. Just dumps again
4082 * on the new 'pmd'. */
4085 ovs_mutex_unlock(&dump
->mutex
);
4087 for (i
= 0; i
< n_flows
; i
++) {
4088 struct odputil_keybuf
*maskbuf
= &thread
->maskbuf
[i
];
4089 struct odputil_keybuf
*keybuf
= &thread
->keybuf
[i
];
4090 struct dp_netdev_flow
*netdev_flow
= netdev_flows
[i
];
4091 struct dpif_flow
*f
= &flows
[i
];
4092 struct ofpbuf key
, mask
;
4094 ofpbuf_use_stack(&key
, keybuf
, sizeof *keybuf
);
4095 ofpbuf_use_stack(&mask
, maskbuf
, sizeof *maskbuf
);
4096 dp_netdev_flow_to_dpif_flow(dp
, netdev_flow
, &key
, &mask
, f
,
4104 dpif_netdev_execute(struct dpif
*dpif
, struct dpif_execute
*execute
)
4105 OVS_NO_THREAD_SAFETY_ANALYSIS
4107 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4108 struct dp_netdev_pmd_thread
*pmd
;
4109 struct dp_packet_batch pp
;
4111 if (dp_packet_size(execute
->packet
) < ETH_HEADER_LEN
||
4112 dp_packet_size(execute
->packet
) > UINT16_MAX
) {
4116 /* Tries finding the 'pmd'. If NULL is returned, that means
4117 * the current thread is a non-pmd thread and should use
4118 * dp_netdev_get_pmd(dp, NON_PMD_CORE_ID). */
4119 pmd
= ovsthread_getspecific(dp
->per_pmd_key
);
4121 pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
4127 if (execute
->probe
) {
4128 /* If this is part of a probe, Drop the packet, since executing
4129 * the action may actually cause spurious packets be sent into
4131 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
4132 dp_netdev_pmd_unref(pmd
);
4137 /* If the current thread is non-pmd thread, acquires
4138 * the 'non_pmd_mutex'. */
4139 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
4140 ovs_mutex_lock(&dp
->non_pmd_mutex
);
4143 /* Update current time in PMD context. We don't care about EMC insertion
4144 * probability, because we are on a slow path. */
4145 pmd_thread_ctx_time_update(pmd
);
4147 /* The action processing expects the RSS hash to be valid, because
4148 * it's always initialized at the beginning of datapath processing.
4149 * In this case, though, 'execute->packet' may not have gone through
4150 * the datapath at all, it may have been generated by the upper layer
4151 * (OpenFlow packet-out, BFD frame, ...). */
4152 if (!dp_packet_rss_valid(execute
->packet
)) {
4153 dp_packet_set_rss_hash(execute
->packet
,
4154 flow_hash_5tuple(execute
->flow
, 0));
4157 dp_packet_batch_init_packet(&pp
, execute
->packet
);
4158 pp
.do_not_steal
= true;
4159 dp_netdev_execute_actions(pmd
, &pp
, false, execute
->flow
,
4160 execute
->actions
, execute
->actions_len
);
4161 dp_netdev_pmd_flush_output_packets(pmd
, true);
4163 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
4164 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
4165 dp_netdev_pmd_unref(pmd
);
4172 dpif_netdev_operate(struct dpif
*dpif
, struct dpif_op
**ops
, size_t n_ops
,
4173 enum dpif_offload_type offload_type OVS_UNUSED
)
4177 for (i
= 0; i
< n_ops
; i
++) {
4178 struct dpif_op
*op
= ops
[i
];
4181 case DPIF_OP_FLOW_PUT
:
4182 op
->error
= dpif_netdev_flow_put(dpif
, &op
->flow_put
);
4185 case DPIF_OP_FLOW_DEL
:
4186 op
->error
= dpif_netdev_flow_del(dpif
, &op
->flow_del
);
4189 case DPIF_OP_EXECUTE
:
4190 op
->error
= dpif_netdev_execute(dpif
, &op
->execute
);
4193 case DPIF_OP_FLOW_GET
:
4194 op
->error
= dpif_netdev_flow_get(dpif
, &op
->flow_get
);
4200 /* Enable or Disable PMD auto load balancing. */
4202 set_pmd_auto_lb(struct dp_netdev
*dp
)
4204 unsigned int cnt
= 0;
4205 struct dp_netdev_pmd_thread
*pmd
;
4206 struct pmd_auto_lb
*pmd_alb
= &dp
->pmd_alb
;
4208 bool enable_alb
= false;
4209 bool multi_rxq
= false;
4210 bool pmd_rxq_assign_cyc
= dp
->pmd_rxq_assign_cyc
;
4212 /* Ensure that there is at least 2 non-isolated PMDs and
4213 * one of them is polling more than one rxq. */
4214 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4215 if (pmd
->core_id
== NON_PMD_CORE_ID
|| pmd
->isolated
) {
4219 if (hmap_count(&pmd
->poll_list
) > 1) {
4222 if (cnt
&& multi_rxq
) {
4229 /* Enable auto LB if it is requested and cycle based assignment is true. */
4230 enable_alb
= enable_alb
&& pmd_rxq_assign_cyc
&&
4231 pmd_alb
->auto_lb_requested
;
4233 if (pmd_alb
->is_enabled
!= enable_alb
) {
4234 pmd_alb
->is_enabled
= enable_alb
;
4235 if (pmd_alb
->is_enabled
) {
4236 VLOG_INFO("PMD auto load balance is enabled "
4237 "(with rebalance interval:%"PRIu64
" msec)",
4238 pmd_alb
->rebalance_intvl
);
4240 pmd_alb
->rebalance_poll_timer
= 0;
4241 VLOG_INFO("PMD auto load balance is disabled");
4247 /* Applies datapath configuration from the database. Some of the changes are
4248 * actually applied in dpif_netdev_run(). */
4250 dpif_netdev_set_config(struct dpif
*dpif
, const struct smap
*other_config
)
4252 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4253 const char *cmask
= smap_get(other_config
, "pmd-cpu-mask");
4254 const char *pmd_rxq_assign
= smap_get_def(other_config
, "pmd-rxq-assign",
4256 unsigned long long insert_prob
=
4257 smap_get_ullong(other_config
, "emc-insert-inv-prob",
4258 DEFAULT_EM_FLOW_INSERT_INV_PROB
);
4259 uint32_t insert_min
, cur_min
;
4260 uint32_t tx_flush_interval
, cur_tx_flush_interval
;
4261 uint64_t rebalance_intvl
;
4263 tx_flush_interval
= smap_get_int(other_config
, "tx-flush-interval",
4264 DEFAULT_TX_FLUSH_INTERVAL
);
4265 atomic_read_relaxed(&dp
->tx_flush_interval
, &cur_tx_flush_interval
);
4266 if (tx_flush_interval
!= cur_tx_flush_interval
) {
4267 atomic_store_relaxed(&dp
->tx_flush_interval
, tx_flush_interval
);
4268 VLOG_INFO("Flushing interval for tx queues set to %"PRIu32
" us",
4272 if (!nullable_string_is_equal(dp
->pmd_cmask
, cmask
)) {
4273 free(dp
->pmd_cmask
);
4274 dp
->pmd_cmask
= nullable_xstrdup(cmask
);
4275 dp_netdev_request_reconfigure(dp
);
4278 atomic_read_relaxed(&dp
->emc_insert_min
, &cur_min
);
4279 if (insert_prob
<= UINT32_MAX
) {
4280 insert_min
= insert_prob
== 0 ? 0 : UINT32_MAX
/ insert_prob
;
4282 insert_min
= DEFAULT_EM_FLOW_INSERT_MIN
;
4283 insert_prob
= DEFAULT_EM_FLOW_INSERT_INV_PROB
;
4286 if (insert_min
!= cur_min
) {
4287 atomic_store_relaxed(&dp
->emc_insert_min
, insert_min
);
4288 if (insert_min
== 0) {
4289 VLOG_INFO("EMC insertion probability changed to zero");
4291 VLOG_INFO("EMC insertion probability changed to 1/%llu (~%.2f%%)",
4292 insert_prob
, (100 / (float)insert_prob
));
4296 bool perf_enabled
= smap_get_bool(other_config
, "pmd-perf-metrics", false);
4297 bool cur_perf_enabled
;
4298 atomic_read_relaxed(&dp
->pmd_perf_metrics
, &cur_perf_enabled
);
4299 if (perf_enabled
!= cur_perf_enabled
) {
4300 atomic_store_relaxed(&dp
->pmd_perf_metrics
, perf_enabled
);
4302 VLOG_INFO("PMD performance metrics collection enabled");
4304 VLOG_INFO("PMD performance metrics collection disabled");
4308 bool smc_enable
= smap_get_bool(other_config
, "smc-enable", false);
4310 atomic_read_relaxed(&dp
->smc_enable_db
, &cur_smc
);
4311 if (smc_enable
!= cur_smc
) {
4312 atomic_store_relaxed(&dp
->smc_enable_db
, smc_enable
);
4314 VLOG_INFO("SMC cache is enabled");
4316 VLOG_INFO("SMC cache is disabled");
4320 bool pmd_rxq_assign_cyc
= !strcmp(pmd_rxq_assign
, "cycles");
4321 if (!pmd_rxq_assign_cyc
&& strcmp(pmd_rxq_assign
, "roundrobin")) {
4322 VLOG_WARN("Unsupported Rxq to PMD assignment mode in pmd-rxq-assign. "
4323 "Defaulting to 'cycles'.");
4324 pmd_rxq_assign_cyc
= true;
4325 pmd_rxq_assign
= "cycles";
4327 if (dp
->pmd_rxq_assign_cyc
!= pmd_rxq_assign_cyc
) {
4328 dp
->pmd_rxq_assign_cyc
= pmd_rxq_assign_cyc
;
4329 VLOG_INFO("Rxq to PMD assignment mode changed to: \'%s\'.",
4331 dp_netdev_request_reconfigure(dp
);
4334 struct pmd_auto_lb
*pmd_alb
= &dp
->pmd_alb
;
4335 pmd_alb
->auto_lb_requested
= smap_get_bool(other_config
, "pmd-auto-lb",
4338 rebalance_intvl
= smap_get_int(other_config
, "pmd-auto-lb-rebal-interval",
4339 ALB_PMD_REBALANCE_POLL_INTERVAL
);
4341 /* Input is in min, convert it to msec. */
4343 rebalance_intvl
? rebalance_intvl
* MIN_TO_MSEC
: MIN_TO_MSEC
;
4345 if (pmd_alb
->rebalance_intvl
!= rebalance_intvl
) {
4346 pmd_alb
->rebalance_intvl
= rebalance_intvl
;
4349 set_pmd_auto_lb(dp
);
4353 /* Parses affinity list and returns result in 'core_ids'. */
4355 parse_affinity_list(const char *affinity_list
, unsigned *core_ids
, int n_rxq
)
4358 char *list
, *copy
, *key
, *value
;
4361 for (i
= 0; i
< n_rxq
; i
++) {
4362 core_ids
[i
] = OVS_CORE_UNSPEC
;
4365 if (!affinity_list
) {
4369 list
= copy
= xstrdup(affinity_list
);
4371 while (ofputil_parse_key_value(&list
, &key
, &value
)) {
4372 int rxq_id
, core_id
;
4374 if (!str_to_int(key
, 0, &rxq_id
) || rxq_id
< 0
4375 || !str_to_int(value
, 0, &core_id
) || core_id
< 0) {
4380 if (rxq_id
< n_rxq
) {
4381 core_ids
[rxq_id
] = core_id
;
4389 /* Parses 'affinity_list' and applies configuration if it is valid. */
4391 dpif_netdev_port_set_rxq_affinity(struct dp_netdev_port
*port
,
4392 const char *affinity_list
)
4394 unsigned *core_ids
, i
;
4397 core_ids
= xmalloc(port
->n_rxq
* sizeof *core_ids
);
4398 if (parse_affinity_list(affinity_list
, core_ids
, port
->n_rxq
)) {
4403 for (i
= 0; i
< port
->n_rxq
; i
++) {
4404 port
->rxqs
[i
].core_id
= core_ids
[i
];
4412 /* Returns 'true' if one of the 'port's RX queues exists in 'poll_list'
4413 * of given PMD thread. */
4415 dpif_netdev_pmd_polls_port(struct dp_netdev_pmd_thread
*pmd
,
4416 struct dp_netdev_port
*port
)
4417 OVS_EXCLUDED(pmd
->port_mutex
)
4419 struct rxq_poll
*poll
;
4422 ovs_mutex_lock(&pmd
->port_mutex
);
4423 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
4424 if (port
== poll
->rxq
->port
) {
4429 ovs_mutex_unlock(&pmd
->port_mutex
);
4433 /* Updates port configuration from the database. The changes are actually
4434 * applied in dpif_netdev_run(). */
4436 dpif_netdev_port_set_config(struct dpif
*dpif
, odp_port_t port_no
,
4437 const struct smap
*cfg
)
4439 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4440 struct dp_netdev_port
*port
;
4442 const char *affinity_list
= smap_get(cfg
, "pmd-rxq-affinity");
4443 bool emc_enabled
= smap_get_bool(cfg
, "emc-enable", true);
4445 ovs_mutex_lock(&dp
->port_mutex
);
4446 error
= get_port_by_number(dp
, port_no
, &port
);
4451 if (emc_enabled
!= port
->emc_enabled
) {
4452 struct dp_netdev_pmd_thread
*pmd
;
4453 struct ds ds
= DS_EMPTY_INITIALIZER
;
4454 uint32_t cur_min
, insert_prob
;
4456 port
->emc_enabled
= emc_enabled
;
4457 /* Mark for reload all the threads that polls this port and request
4458 * for reconfiguration for the actual reloading of threads. */
4459 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4460 if (dpif_netdev_pmd_polls_port(pmd
, port
)) {
4461 pmd
->need_reload
= true;
4464 dp_netdev_request_reconfigure(dp
);
4466 ds_put_format(&ds
, "%s: EMC has been %s.",
4467 netdev_get_name(port
->netdev
),
4468 (emc_enabled
) ? "enabled" : "disabled");
4470 ds_put_cstr(&ds
, " Current insertion probability is ");
4471 atomic_read_relaxed(&dp
->emc_insert_min
, &cur_min
);
4473 ds_put_cstr(&ds
, "zero.");
4475 insert_prob
= UINT32_MAX
/ cur_min
;
4476 ds_put_format(&ds
, "1/%"PRIu32
" (~%.2f%%).",
4477 insert_prob
, 100 / (float) insert_prob
);
4480 VLOG_INFO("%s", ds_cstr(&ds
));
4484 /* Checking for RXq affinity changes. */
4485 if (!netdev_is_pmd(port
->netdev
)
4486 || nullable_string_is_equal(affinity_list
, port
->rxq_affinity_list
)) {
4490 error
= dpif_netdev_port_set_rxq_affinity(port
, affinity_list
);
4494 free(port
->rxq_affinity_list
);
4495 port
->rxq_affinity_list
= nullable_xstrdup(affinity_list
);
4497 dp_netdev_request_reconfigure(dp
);
4499 ovs_mutex_unlock(&dp
->port_mutex
);
4504 dpif_netdev_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
4505 uint32_t queue_id
, uint32_t *priority
)
4507 *priority
= queue_id
;
4512 /* Creates and returns a new 'struct dp_netdev_actions', whose actions are
4513 * a copy of the 'size' bytes of 'actions' input parameters. */
4514 struct dp_netdev_actions
*
4515 dp_netdev_actions_create(const struct nlattr
*actions
, size_t size
)
4517 struct dp_netdev_actions
*netdev_actions
;
4519 netdev_actions
= xmalloc(sizeof *netdev_actions
+ size
);
4520 memcpy(netdev_actions
->actions
, actions
, size
);
4521 netdev_actions
->size
= size
;
4523 return netdev_actions
;
4526 struct dp_netdev_actions
*
4527 dp_netdev_flow_get_actions(const struct dp_netdev_flow
*flow
)
4529 return ovsrcu_get(struct dp_netdev_actions
*, &flow
->actions
);
4533 dp_netdev_actions_free(struct dp_netdev_actions
*actions
)
4539 dp_netdev_rxq_set_cycles(struct dp_netdev_rxq
*rx
,
4540 enum rxq_cycles_counter_type type
,
4541 unsigned long long cycles
)
4543 atomic_store_relaxed(&rx
->cycles
[type
], cycles
);
4547 dp_netdev_rxq_add_cycles(struct dp_netdev_rxq
*rx
,
4548 enum rxq_cycles_counter_type type
,
4549 unsigned long long cycles
)
4551 non_atomic_ullong_add(&rx
->cycles
[type
], cycles
);
4555 dp_netdev_rxq_get_cycles(struct dp_netdev_rxq
*rx
,
4556 enum rxq_cycles_counter_type type
)
4558 unsigned long long processing_cycles
;
4559 atomic_read_relaxed(&rx
->cycles
[type
], &processing_cycles
);
4560 return processing_cycles
;
4564 dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq
*rx
,
4565 unsigned long long cycles
)
4567 unsigned int idx
= rx
->intrvl_idx
++ % PMD_RXQ_INTERVAL_MAX
;
4568 atomic_store_relaxed(&rx
->cycles_intrvl
[idx
], cycles
);
4572 dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq
*rx
, unsigned idx
)
4574 unsigned long long processing_cycles
;
4575 atomic_read_relaxed(&rx
->cycles_intrvl
[idx
], &processing_cycles
);
4576 return processing_cycles
;
4579 #if ATOMIC_ALWAYS_LOCK_FREE_8B
4581 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread
*pmd
)
4583 bool pmd_perf_enabled
;
4584 atomic_read_relaxed(&pmd
->dp
->pmd_perf_metrics
, &pmd_perf_enabled
);
4585 return pmd_perf_enabled
;
4588 /* If stores and reads of 64-bit integers are not atomic, the full PMD
4589 * performance metrics are not available as locked access to 64 bit
4590 * integers would be prohibitively expensive. */
4592 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread
*pmd OVS_UNUSED
)
4599 dp_netdev_pmd_flush_output_on_port(struct dp_netdev_pmd_thread
*pmd
,
4606 struct cycle_timer timer
;
4608 uint32_t tx_flush_interval
;
4610 cycle_timer_start(&pmd
->perf_stats
, &timer
);
4612 dynamic_txqs
= p
->port
->dynamic_txqs
;
4614 tx_qid
= dpif_netdev_xps_get_tx_qid(pmd
, p
);
4616 tx_qid
= pmd
->static_tx_qid
;
4619 output_cnt
= dp_packet_batch_size(&p
->output_pkts
);
4620 ovs_assert(output_cnt
> 0);
4622 netdev_send(p
->port
->netdev
, tx_qid
, &p
->output_pkts
, dynamic_txqs
);
4623 dp_packet_batch_init(&p
->output_pkts
);
4625 /* Update time of the next flush. */
4626 atomic_read_relaxed(&pmd
->dp
->tx_flush_interval
, &tx_flush_interval
);
4627 p
->flush_time
= pmd
->ctx
.now
+ tx_flush_interval
;
4629 ovs_assert(pmd
->n_output_batches
> 0);
4630 pmd
->n_output_batches
--;
4632 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_SENT_PKTS
, output_cnt
);
4633 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_SENT_BATCHES
, 1);
4635 /* Distribute send cycles evenly among transmitted packets and assign to
4636 * their respective rx queues. */
4637 cycles
= cycle_timer_stop(&pmd
->perf_stats
, &timer
) / output_cnt
;
4638 for (i
= 0; i
< output_cnt
; i
++) {
4639 if (p
->output_pkts_rxqs
[i
]) {
4640 dp_netdev_rxq_add_cycles(p
->output_pkts_rxqs
[i
],
4641 RXQ_CYCLES_PROC_CURR
, cycles
);
4649 dp_netdev_pmd_flush_output_packets(struct dp_netdev_pmd_thread
*pmd
,
4655 if (!pmd
->n_output_batches
) {
4659 HMAP_FOR_EACH (p
, node
, &pmd
->send_port_cache
) {
4660 if (!dp_packet_batch_is_empty(&p
->output_pkts
)
4661 && (force
|| pmd
->ctx
.now
>= p
->flush_time
)) {
4662 output_cnt
+= dp_netdev_pmd_flush_output_on_port(pmd
, p
);
4669 dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread
*pmd
,
4670 struct dp_netdev_rxq
*rxq
,
4673 struct pmd_perf_stats
*s
= &pmd
->perf_stats
;
4674 struct dp_packet_batch batch
;
4675 struct cycle_timer timer
;
4678 int rem_qlen
= 0, *qlen_p
= NULL
;
4681 /* Measure duration for polling and processing rx burst. */
4682 cycle_timer_start(&pmd
->perf_stats
, &timer
);
4684 pmd
->ctx
.last_rxq
= rxq
;
4685 dp_packet_batch_init(&batch
);
4687 /* Fetch the rx queue length only for vhostuser ports. */
4688 if (pmd_perf_metrics_enabled(pmd
) && rxq
->is_vhost
) {
4692 error
= netdev_rxq_recv(rxq
->rx
, &batch
, qlen_p
);
4694 /* At least one packet received. */
4695 *recirc_depth_get() = 0;
4696 pmd_thread_ctx_time_update(pmd
);
4697 batch_cnt
= dp_packet_batch_size(&batch
);
4698 if (pmd_perf_metrics_enabled(pmd
)) {
4699 /* Update batch histogram. */
4700 s
->current
.batches
++;
4701 histogram_add_sample(&s
->pkts_per_batch
, batch_cnt
);
4702 /* Update the maximum vhost rx queue fill level. */
4703 if (rxq
->is_vhost
&& rem_qlen
>= 0) {
4704 uint32_t qfill
= batch_cnt
+ rem_qlen
;
4705 if (qfill
> s
->current
.max_vhost_qfill
) {
4706 s
->current
.max_vhost_qfill
= qfill
;
4710 /* Process packet batch. */
4711 dp_netdev_input(pmd
, &batch
, port_no
);
4713 /* Assign processing cycles to rx queue. */
4714 cycles
= cycle_timer_stop(&pmd
->perf_stats
, &timer
);
4715 dp_netdev_rxq_add_cycles(rxq
, RXQ_CYCLES_PROC_CURR
, cycles
);
4717 dp_netdev_pmd_flush_output_packets(pmd
, false);
4719 /* Discard cycles. */
4720 cycle_timer_stop(&pmd
->perf_stats
, &timer
);
4721 if (error
!= EAGAIN
&& error
!= EOPNOTSUPP
) {
4722 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4724 VLOG_ERR_RL(&rl
, "error receiving data from %s: %s",
4725 netdev_rxq_get_name(rxq
->rx
), ovs_strerror(error
));
4729 pmd
->ctx
.last_rxq
= NULL
;
4734 static struct tx_port
*
4735 tx_port_lookup(const struct hmap
*hmap
, odp_port_t port_no
)
4739 HMAP_FOR_EACH_IN_BUCKET (tx
, node
, hash_port_no(port_no
), hmap
) {
4740 if (tx
->port
->port_no
== port_no
) {
4748 static struct tx_bond
*
4749 tx_bond_lookup(const struct cmap
*tx_bonds
, uint32_t bond_id
)
4751 uint32_t hash
= hash_bond_id(bond_id
);
4754 CMAP_FOR_EACH_WITH_HASH (tx
, node
, hash
, tx_bonds
) {
4755 if (tx
->bond_id
== bond_id
) {
4763 port_reconfigure(struct dp_netdev_port
*port
)
4765 struct netdev
*netdev
= port
->netdev
;
4768 /* Closes the existing 'rxq's. */
4769 for (i
= 0; i
< port
->n_rxq
; i
++) {
4770 netdev_rxq_close(port
->rxqs
[i
].rx
);
4771 port
->rxqs
[i
].rx
= NULL
;
4773 unsigned last_nrxq
= port
->n_rxq
;
4776 /* Allows 'netdev' to apply the pending configuration changes. */
4777 if (netdev_is_reconf_required(netdev
) || port
->need_reconfigure
) {
4778 err
= netdev_reconfigure(netdev
);
4779 if (err
&& (err
!= EOPNOTSUPP
)) {
4780 VLOG_ERR("Failed to set interface %s new configuration",
4781 netdev_get_name(netdev
));
4785 /* If the netdev_reconfigure() above succeeds, reopens the 'rxq's. */
4786 port
->rxqs
= xrealloc(port
->rxqs
,
4787 sizeof *port
->rxqs
* netdev_n_rxq(netdev
));
4788 /* Realloc 'used' counters for tx queues. */
4789 free(port
->txq_used
);
4790 port
->txq_used
= xcalloc(netdev_n_txq(netdev
), sizeof *port
->txq_used
);
4792 for (i
= 0; i
< netdev_n_rxq(netdev
); i
++) {
4793 bool new_queue
= i
>= last_nrxq
;
4795 memset(&port
->rxqs
[i
], 0, sizeof port
->rxqs
[i
]);
4798 port
->rxqs
[i
].port
= port
;
4799 port
->rxqs
[i
].is_vhost
= !strncmp(port
->type
, "dpdkvhost", 9);
4801 err
= netdev_rxq_open(netdev
, &port
->rxqs
[i
].rx
, i
);
4808 /* Parse affinity list to apply configuration for new queues. */
4809 dpif_netdev_port_set_rxq_affinity(port
, port
->rxq_affinity_list
);
4811 /* If reconfiguration was successful mark it as such, so we can use it */
4812 port
->need_reconfigure
= false;
4817 struct rr_numa_list
{
4818 struct hmap numas
; /* Contains 'struct rr_numa' */
4822 struct hmap_node node
;
4826 /* Non isolated pmds on numa node 'numa_id' */
4827 struct dp_netdev_pmd_thread
**pmds
;
4834 static struct rr_numa
*
4835 rr_numa_list_lookup(struct rr_numa_list
*rr
, int numa_id
)
4837 struct rr_numa
*numa
;
4839 HMAP_FOR_EACH_WITH_HASH (numa
, node
, hash_int(numa_id
, 0), &rr
->numas
) {
4840 if (numa
->numa_id
== numa_id
) {
4848 /* Returns the next node in numa list following 'numa' in round-robin fashion.
4849 * Returns first node if 'numa' is a null pointer or the last node in 'rr'.
4850 * Returns NULL if 'rr' numa list is empty. */
4851 static struct rr_numa
*
4852 rr_numa_list_next(struct rr_numa_list
*rr
, const struct rr_numa
*numa
)
4854 struct hmap_node
*node
= NULL
;
4857 node
= hmap_next(&rr
->numas
, &numa
->node
);
4860 node
= hmap_first(&rr
->numas
);
4863 return (node
) ? CONTAINER_OF(node
, struct rr_numa
, node
) : NULL
;
4867 rr_numa_list_populate(struct dp_netdev
*dp
, struct rr_numa_list
*rr
)
4869 struct dp_netdev_pmd_thread
*pmd
;
4870 struct rr_numa
*numa
;
4872 hmap_init(&rr
->numas
);
4874 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4875 if (pmd
->core_id
== NON_PMD_CORE_ID
|| pmd
->isolated
) {
4879 numa
= rr_numa_list_lookup(rr
, pmd
->numa_id
);
4881 numa
= xzalloc(sizeof *numa
);
4882 numa
->numa_id
= pmd
->numa_id
;
4883 hmap_insert(&rr
->numas
, &numa
->node
, hash_int(pmd
->numa_id
, 0));
4886 numa
->pmds
= xrealloc(numa
->pmds
, numa
->n_pmds
* sizeof *numa
->pmds
);
4887 numa
->pmds
[numa
->n_pmds
- 1] = pmd
;
4888 /* At least one pmd so initialise curr_idx and idx_inc. */
4889 numa
->cur_index
= 0;
4890 numa
->idx_inc
= true;
4895 * Returns the next pmd from the numa node.
4897 * If 'updown' is 'true' it will alternate between selecting the next pmd in
4898 * either an up or down walk, switching between up/down when the first or last
4899 * core is reached. e.g. 1,2,3,3,2,1,1,2...
4901 * If 'updown' is 'false' it will select the next pmd wrapping around when last
4902 * core reached. e.g. 1,2,3,1,2,3,1,2...
4904 static struct dp_netdev_pmd_thread
*
4905 rr_numa_get_pmd(struct rr_numa
*numa
, bool updown
)
4907 int numa_idx
= numa
->cur_index
;
4909 if (numa
->idx_inc
== true) {
4910 /* Incrementing through list of pmds. */
4911 if (numa
->cur_index
== numa
->n_pmds
-1) {
4912 /* Reached the last pmd. */
4914 numa
->idx_inc
= false;
4916 numa
->cur_index
= 0;
4922 /* Decrementing through list of pmds. */
4923 if (numa
->cur_index
== 0) {
4924 /* Reached the first pmd. */
4925 numa
->idx_inc
= true;
4930 return numa
->pmds
[numa_idx
];
4934 rr_numa_list_destroy(struct rr_numa_list
*rr
)
4936 struct rr_numa
*numa
;
4938 HMAP_FOR_EACH_POP (numa
, node
, &rr
->numas
) {
4942 hmap_destroy(&rr
->numas
);
4945 /* Sort Rx Queues by the processing cycles they are consuming. */
4947 compare_rxq_cycles(const void *a
, const void *b
)
4949 struct dp_netdev_rxq
*qa
;
4950 struct dp_netdev_rxq
*qb
;
4951 uint64_t cycles_qa
, cycles_qb
;
4953 qa
= *(struct dp_netdev_rxq
**) a
;
4954 qb
= *(struct dp_netdev_rxq
**) b
;
4956 cycles_qa
= dp_netdev_rxq_get_cycles(qa
, RXQ_CYCLES_PROC_HIST
);
4957 cycles_qb
= dp_netdev_rxq_get_cycles(qb
, RXQ_CYCLES_PROC_HIST
);
4959 if (cycles_qa
!= cycles_qb
) {
4960 return (cycles_qa
< cycles_qb
) ? 1 : -1;
4962 /* Cycles are the same so tiebreak on port/queue id.
4963 * Tiebreaking (as opposed to return 0) ensures consistent
4964 * sort results across multiple OS's. */
4965 uint32_t port_qa
= odp_to_u32(qa
->port
->port_no
);
4966 uint32_t port_qb
= odp_to_u32(qb
->port
->port_no
);
4967 if (port_qa
!= port_qb
) {
4968 return port_qa
> port_qb
? 1 : -1;
4970 return netdev_rxq_get_queue_id(qa
->rx
)
4971 - netdev_rxq_get_queue_id(qb
->rx
);
4976 /* Assign pmds to queues. If 'pinned' is true, assign pmds to pinned
4977 * queues and marks the pmds as isolated. Otherwise, assign non isolated
4978 * pmds to unpinned queues.
4980 * The function doesn't touch the pmd threads, it just stores the assignment
4981 * in the 'pmd' member of each rxq. */
4983 rxq_scheduling(struct dp_netdev
*dp
, bool pinned
) OVS_REQUIRES(dp
->port_mutex
)
4985 struct dp_netdev_port
*port
;
4986 struct rr_numa_list rr
;
4987 struct rr_numa
*non_local_numa
= NULL
;
4988 struct dp_netdev_rxq
** rxqs
= NULL
;
4990 struct rr_numa
*numa
= NULL
;
4992 bool assign_cyc
= dp
->pmd_rxq_assign_cyc
;
4994 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4995 if (!netdev_is_pmd(port
->netdev
)) {
4999 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
5000 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
5002 if (pinned
&& q
->core_id
!= OVS_CORE_UNSPEC
) {
5003 struct dp_netdev_pmd_thread
*pmd
;
5005 pmd
= dp_netdev_get_pmd(dp
, q
->core_id
);
5007 VLOG_WARN("There is no PMD thread on core %d. Queue "
5008 "%d on port \'%s\' will not be polled.",
5009 q
->core_id
, qid
, netdev_get_name(port
->netdev
));
5012 pmd
->isolated
= true;
5013 VLOG_INFO("Core %d on numa node %d assigned port \'%s\' "
5014 "rx queue %d.", pmd
->core_id
, pmd
->numa_id
,
5015 netdev_rxq_get_name(q
->rx
),
5016 netdev_rxq_get_queue_id(q
->rx
));
5017 dp_netdev_pmd_unref(pmd
);
5019 } else if (!pinned
&& q
->core_id
== OVS_CORE_UNSPEC
) {
5020 uint64_t cycle_hist
= 0;
5023 rxqs
= xmalloc(sizeof *rxqs
);
5025 rxqs
= xrealloc(rxqs
, sizeof *rxqs
* (n_rxqs
+ 1));
5029 /* Sum the queue intervals and store the cycle history. */
5030 for (unsigned i
= 0; i
< PMD_RXQ_INTERVAL_MAX
; i
++) {
5031 cycle_hist
+= dp_netdev_rxq_get_intrvl_cycles(q
, i
);
5033 dp_netdev_rxq_set_cycles(q
, RXQ_CYCLES_PROC_HIST
,
5036 /* Store the queue. */
5042 if (n_rxqs
> 1 && assign_cyc
) {
5043 /* Sort the queues in order of the processing cycles
5044 * they consumed during their last pmd interval. */
5045 qsort(rxqs
, n_rxqs
, sizeof *rxqs
, compare_rxq_cycles
);
5048 rr_numa_list_populate(dp
, &rr
);
5049 /* Assign the sorted queues to pmds in round robin. */
5050 for (int i
= 0; i
< n_rxqs
; i
++) {
5051 numa_id
= netdev_get_numa_id(rxqs
[i
]->port
->netdev
);
5052 numa
= rr_numa_list_lookup(&rr
, numa_id
);
5054 /* There are no pmds on the queue's local NUMA node.
5055 Round robin on the NUMA nodes that do have pmds. */
5056 non_local_numa
= rr_numa_list_next(&rr
, non_local_numa
);
5057 if (!non_local_numa
) {
5058 VLOG_ERR("There is no available (non-isolated) pmd "
5059 "thread for port \'%s\' queue %d. This queue "
5060 "will not be polled. Is pmd-cpu-mask set to "
5061 "zero? Or are all PMDs isolated to other "
5062 "queues?", netdev_rxq_get_name(rxqs
[i
]->rx
),
5063 netdev_rxq_get_queue_id(rxqs
[i
]->rx
));
5066 rxqs
[i
]->pmd
= rr_numa_get_pmd(non_local_numa
, assign_cyc
);
5067 VLOG_WARN("There's no available (non-isolated) pmd thread "
5068 "on numa node %d. Queue %d on port \'%s\' will "
5069 "be assigned to the pmd on core %d "
5070 "(numa node %d). Expect reduced performance.",
5071 numa_id
, netdev_rxq_get_queue_id(rxqs
[i
]->rx
),
5072 netdev_rxq_get_name(rxqs
[i
]->rx
),
5073 rxqs
[i
]->pmd
->core_id
, rxqs
[i
]->pmd
->numa_id
);
5075 rxqs
[i
]->pmd
= rr_numa_get_pmd(numa
, assign_cyc
);
5077 VLOG_INFO("Core %d on numa node %d assigned port \'%s\' "
5079 "(measured processing cycles %"PRIu64
").",
5080 rxqs
[i
]->pmd
->core_id
, numa_id
,
5081 netdev_rxq_get_name(rxqs
[i
]->rx
),
5082 netdev_rxq_get_queue_id(rxqs
[i
]->rx
),
5083 dp_netdev_rxq_get_cycles(rxqs
[i
],
5084 RXQ_CYCLES_PROC_HIST
));
5086 VLOG_INFO("Core %d on numa node %d assigned port \'%s\' "
5087 "rx queue %d.", rxqs
[i
]->pmd
->core_id
, numa_id
,
5088 netdev_rxq_get_name(rxqs
[i
]->rx
),
5089 netdev_rxq_get_queue_id(rxqs
[i
]->rx
));
5094 rr_numa_list_destroy(&rr
);
5099 reload_affected_pmds(struct dp_netdev
*dp
)
5101 struct dp_netdev_pmd_thread
*pmd
;
5103 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5104 if (pmd
->need_reload
) {
5105 flow_mark_flush(pmd
);
5106 dp_netdev_reload_pmd__(pmd
);
5110 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5111 if (pmd
->need_reload
) {
5112 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
5116 atomic_read_explicit(&pmd
->reload
, &reload
,
5117 memory_order_acquire
);
5120 pmd
->need_reload
= false;
5126 reconfigure_pmd_threads(struct dp_netdev
*dp
)
5127 OVS_REQUIRES(dp
->port_mutex
)
5129 struct dp_netdev_pmd_thread
*pmd
;
5130 struct ovs_numa_dump
*pmd_cores
;
5131 struct ovs_numa_info_core
*core
;
5132 struct hmapx to_delete
= HMAPX_INITIALIZER(&to_delete
);
5133 struct hmapx_node
*node
;
5134 bool changed
= false;
5135 bool need_to_adjust_static_tx_qids
= false;
5137 /* The pmd threads should be started only if there's a pmd port in the
5138 * datapath. If the user didn't provide any "pmd-cpu-mask", we start
5139 * NR_PMD_THREADS per numa node. */
5140 if (!has_pmd_port(dp
)) {
5141 pmd_cores
= ovs_numa_dump_n_cores_per_numa(0);
5142 } else if (dp
->pmd_cmask
&& dp
->pmd_cmask
[0]) {
5143 pmd_cores
= ovs_numa_dump_cores_with_cmask(dp
->pmd_cmask
);
5145 pmd_cores
= ovs_numa_dump_n_cores_per_numa(NR_PMD_THREADS
);
5148 /* We need to adjust 'static_tx_qid's only if we're reducing number of
5149 * PMD threads. Otherwise, new threads will allocate all the freed ids. */
5150 if (ovs_numa_dump_count(pmd_cores
) < cmap_count(&dp
->poll_threads
) - 1) {
5151 /* Adjustment is required to keep 'static_tx_qid's sequential and
5152 * avoid possible issues, for example, imbalanced tx queue usage
5153 * and unnecessary locking caused by remapping on netdev level. */
5154 need_to_adjust_static_tx_qids
= true;
5157 /* Check for unwanted pmd threads */
5158 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5159 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
5162 if (!ovs_numa_dump_contains_core(pmd_cores
, pmd
->numa_id
,
5164 hmapx_add(&to_delete
, pmd
);
5165 } else if (need_to_adjust_static_tx_qids
) {
5166 atomic_store_relaxed(&pmd
->reload_tx_qid
, true);
5167 pmd
->need_reload
= true;
5171 HMAPX_FOR_EACH (node
, &to_delete
) {
5172 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
5173 VLOG_INFO("PMD thread on numa_id: %d, core id: %2d destroyed.",
5174 pmd
->numa_id
, pmd
->core_id
);
5175 dp_netdev_del_pmd(dp
, pmd
);
5177 changed
= !hmapx_is_empty(&to_delete
);
5178 hmapx_destroy(&to_delete
);
5180 if (need_to_adjust_static_tx_qids
) {
5181 /* 'static_tx_qid's are not sequential now.
5182 * Reload remaining threads to fix this. */
5183 reload_affected_pmds(dp
);
5186 /* Check for required new pmd threads */
5187 FOR_EACH_CORE_ON_DUMP(core
, pmd_cores
) {
5188 pmd
= dp_netdev_get_pmd(dp
, core
->core_id
);
5190 struct ds name
= DS_EMPTY_INITIALIZER
;
5192 pmd
= xzalloc(sizeof *pmd
);
5193 dp_netdev_configure_pmd(pmd
, dp
, core
->core_id
, core
->numa_id
);
5195 ds_put_format(&name
, "pmd-c%02d/id:", core
->core_id
);
5196 pmd
->thread
= ovs_thread_create(ds_cstr(&name
),
5197 pmd_thread_main
, pmd
);
5200 VLOG_INFO("PMD thread on numa_id: %d, core id: %2d created.",
5201 pmd
->numa_id
, pmd
->core_id
);
5204 dp_netdev_pmd_unref(pmd
);
5209 struct ovs_numa_info_numa
*numa
;
5211 /* Log the number of pmd threads per numa node. */
5212 FOR_EACH_NUMA_ON_DUMP (numa
, pmd_cores
) {
5213 VLOG_INFO("There are %"PRIuSIZE
" pmd threads on numa node %d",
5214 numa
->n_cores
, numa
->numa_id
);
5218 ovs_numa_dump_destroy(pmd_cores
);
5222 pmd_remove_stale_ports(struct dp_netdev
*dp
,
5223 struct dp_netdev_pmd_thread
*pmd
)
5224 OVS_EXCLUDED(pmd
->port_mutex
)
5225 OVS_REQUIRES(dp
->port_mutex
)
5227 struct rxq_poll
*poll
, *poll_next
;
5228 struct tx_port
*tx
, *tx_next
;
5230 ovs_mutex_lock(&pmd
->port_mutex
);
5231 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
5232 struct dp_netdev_port
*port
= poll
->rxq
->port
;
5234 if (port
->need_reconfigure
5235 || !hmap_contains(&dp
->ports
, &port
->node
)) {
5236 dp_netdev_del_rxq_from_pmd(pmd
, poll
);
5239 HMAP_FOR_EACH_SAFE (tx
, tx_next
, node
, &pmd
->tx_ports
) {
5240 struct dp_netdev_port
*port
= tx
->port
;
5242 if (port
->need_reconfigure
5243 || !hmap_contains(&dp
->ports
, &port
->node
)) {
5244 dp_netdev_del_port_tx_from_pmd(pmd
, tx
);
5247 ovs_mutex_unlock(&pmd
->port_mutex
);
5250 /* Must be called each time a port is added/removed or the cmask changes.
5251 * This creates and destroys pmd threads, reconfigures ports, opens their
5252 * rxqs and assigns all rxqs/txqs to pmd threads. */
5254 reconfigure_datapath(struct dp_netdev
*dp
)
5255 OVS_REQUIRES(dp
->port_mutex
)
5257 struct hmapx busy_threads
= HMAPX_INITIALIZER(&busy_threads
);
5258 struct dp_netdev_pmd_thread
*pmd
;
5259 struct dp_netdev_port
*port
;
5262 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
5264 /* Step 1: Adjust the pmd threads based on the datapath ports, the cores
5265 * on the system and the user configuration. */
5266 reconfigure_pmd_threads(dp
);
5268 wanted_txqs
= cmap_count(&dp
->poll_threads
);
5270 /* The number of pmd threads might have changed, or a port can be new:
5271 * adjust the txqs. */
5272 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
5273 netdev_set_tx_multiq(port
->netdev
, wanted_txqs
);
5276 /* Step 2: Remove from the pmd threads ports that have been removed or
5277 * need reconfiguration. */
5279 /* Check for all the ports that need reconfiguration. We cache this in
5280 * 'port->need_reconfigure', because netdev_is_reconf_required() can
5281 * change at any time.
5282 * Also mark for reconfiguration all ports which will likely change their
5283 * 'dynamic_txqs' parameter. It's required to stop using them before
5284 * changing this setting and it's simpler to mark ports here and allow
5285 * 'pmd_remove_stale_ports' to remove them from threads. There will be
5286 * no actual reconfiguration in 'port_reconfigure' because it's
5288 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
5289 if (netdev_is_reconf_required(port
->netdev
)
5290 || (port
->dynamic_txqs
5291 != (netdev_n_txq(port
->netdev
) < wanted_txqs
))) {
5292 port
->need_reconfigure
= true;
5296 /* Remove from the pmd threads all the ports that have been deleted or
5297 * need reconfiguration. */
5298 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5299 pmd_remove_stale_ports(dp
, pmd
);
5302 /* Reload affected pmd threads. We must wait for the pmd threads before
5303 * reconfiguring the ports, because a port cannot be reconfigured while
5304 * it's being used. */
5305 reload_affected_pmds(dp
);
5307 /* Step 3: Reconfigure ports. */
5309 /* We only reconfigure the ports that we determined above, because they're
5310 * not being used by any pmd thread at the moment. If a port fails to
5311 * reconfigure we remove it from the datapath. */
5312 struct dp_netdev_port
*next_port
;
5313 HMAP_FOR_EACH_SAFE (port
, next_port
, node
, &dp
->ports
) {
5316 if (!port
->need_reconfigure
) {
5320 err
= port_reconfigure(port
);
5322 hmap_remove(&dp
->ports
, &port
->node
);
5323 seq_change(dp
->port_seq
);
5326 port
->dynamic_txqs
= netdev_n_txq(port
->netdev
) < wanted_txqs
;
5330 /* Step 4: Compute new rxq scheduling. We don't touch the pmd threads
5331 * for now, we just update the 'pmd' pointer in each rxq to point to the
5332 * wanted thread according to the scheduling policy. */
5334 /* Reset all the pmd threads to non isolated. */
5335 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5336 pmd
->isolated
= false;
5339 /* Reset all the queues to unassigned */
5340 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
5341 for (int i
= 0; i
< port
->n_rxq
; i
++) {
5342 port
->rxqs
[i
].pmd
= NULL
;
5346 /* Add pinned queues and mark pmd threads isolated. */
5347 rxq_scheduling(dp
, true);
5349 /* Add non-pinned queues. */
5350 rxq_scheduling(dp
, false);
5352 /* Step 5: Remove queues not compliant with new scheduling. */
5354 /* Count all the threads that will have at least one queue to poll. */
5355 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
5356 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
5357 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
5360 hmapx_add(&busy_threads
, q
->pmd
);
5365 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5366 struct rxq_poll
*poll
, *poll_next
;
5368 ovs_mutex_lock(&pmd
->port_mutex
);
5369 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
5370 if (poll
->rxq
->pmd
!= pmd
) {
5371 dp_netdev_del_rxq_from_pmd(pmd
, poll
);
5373 /* This pmd might sleep after this step if it has no rxq
5374 * remaining. Tell it to busy wait for new assignment if it
5375 * has at least one scheduled queue. */
5376 if (hmap_count(&pmd
->poll_list
) == 0 &&
5377 hmapx_contains(&busy_threads
, pmd
)) {
5378 atomic_store_relaxed(&pmd
->wait_for_reload
, true);
5382 ovs_mutex_unlock(&pmd
->port_mutex
);
5385 hmapx_destroy(&busy_threads
);
5387 /* Reload affected pmd threads. We must wait for the pmd threads to remove
5388 * the old queues before readding them, otherwise a queue can be polled by
5389 * two threads at the same time. */
5390 reload_affected_pmds(dp
);
5392 /* Step 6: Add queues from scheduling, if they're not there already. */
5393 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
5394 if (!netdev_is_pmd(port
->netdev
)) {
5398 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
5399 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
5402 ovs_mutex_lock(&q
->pmd
->port_mutex
);
5403 dp_netdev_add_rxq_to_pmd(q
->pmd
, q
);
5404 ovs_mutex_unlock(&q
->pmd
->port_mutex
);
5409 /* Add every port and bond to the tx port and bond caches of
5410 * every pmd thread, if it's not there already and if this pmd
5411 * has at least one rxq to poll.
5413 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5414 ovs_mutex_lock(&pmd
->port_mutex
);
5415 if (hmap_count(&pmd
->poll_list
) || pmd
->core_id
== NON_PMD_CORE_ID
) {
5416 struct tx_bond
*bond
;
5418 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
5419 dp_netdev_add_port_tx_to_pmd(pmd
, port
);
5422 CMAP_FOR_EACH (bond
, node
, &dp
->tx_bonds
) {
5423 dp_netdev_add_bond_tx_to_pmd(pmd
, bond
, false);
5426 ovs_mutex_unlock(&pmd
->port_mutex
);
5429 /* Reload affected pmd threads. */
5430 reload_affected_pmds(dp
);
5432 /* Check if PMD Auto LB is to be enabled */
5433 set_pmd_auto_lb(dp
);
5436 /* Returns true if one of the netdevs in 'dp' requires a reconfiguration */
5438 ports_require_restart(const struct dp_netdev
*dp
)
5439 OVS_REQUIRES(dp
->port_mutex
)
5441 struct dp_netdev_port
*port
;
5443 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
5444 if (netdev_is_reconf_required(port
->netdev
)) {
5452 /* Calculates variance in the values stored in array 'a'. 'n' is the number
5453 * of elements in array to be considered for calculating vairance.
5454 * Usage example: data array 'a' contains the processing load of each pmd and
5455 * 'n' is the number of PMDs. It returns the variance in processing load of
5458 variance(uint64_t a
[], int n
)
5460 /* Compute mean (average of elements). */
5463 uint64_t sqDiff
= 0;
5469 for (int i
= 0; i
< n
; i
++) {
5476 /* Compute sum squared differences with mean. */
5477 for (int i
= 0; i
< n
; i
++) {
5478 sqDiff
+= (a
[i
] - mean
)*(a
[i
] - mean
);
5481 return (sqDiff
? (sqDiff
/ n
) : 0);
5485 /* Returns the variance in the PMDs usage as part of dry run of rxqs
5486 * assignment to PMDs. */
5488 get_dry_run_variance(struct dp_netdev
*dp
, uint32_t *core_list
,
5489 uint32_t num_pmds
, uint64_t *predicted_variance
)
5490 OVS_REQUIRES(dp
->port_mutex
)
5492 struct dp_netdev_port
*port
;
5493 struct dp_netdev_pmd_thread
*pmd
;
5494 struct dp_netdev_rxq
**rxqs
= NULL
;
5495 struct rr_numa
*numa
= NULL
;
5496 struct rr_numa_list rr
;
5499 uint64_t *pmd_usage
;
5501 if (!predicted_variance
) {
5505 pmd_usage
= xcalloc(num_pmds
, sizeof(uint64_t));
5507 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
5508 if (!netdev_is_pmd(port
->netdev
)) {
5512 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
5513 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
5514 uint64_t cycle_hist
= 0;
5516 if (q
->pmd
->isolated
) {
5521 rxqs
= xmalloc(sizeof *rxqs
);
5523 rxqs
= xrealloc(rxqs
, sizeof *rxqs
* (n_rxqs
+ 1));
5526 /* Sum the queue intervals and store the cycle history. */
5527 for (unsigned i
= 0; i
< PMD_RXQ_INTERVAL_MAX
; i
++) {
5528 cycle_hist
+= dp_netdev_rxq_get_intrvl_cycles(q
, i
);
5530 dp_netdev_rxq_set_cycles(q
, RXQ_CYCLES_PROC_HIST
,
5532 /* Store the queue. */
5537 /* Sort the queues in order of the processing cycles
5538 * they consumed during their last pmd interval. */
5539 qsort(rxqs
, n_rxqs
, sizeof *rxqs
, compare_rxq_cycles
);
5541 rr_numa_list_populate(dp
, &rr
);
5543 for (int i
= 0; i
< n_rxqs
; i
++) {
5544 int numa_id
= netdev_get_numa_id(rxqs
[i
]->port
->netdev
);
5545 numa
= rr_numa_list_lookup(&rr
, numa_id
);
5547 /* Abort if cross NUMA polling. */
5548 VLOG_DBG("PMD auto lb dry run."
5549 " Aborting due to cross-numa polling.");
5553 pmd
= rr_numa_get_pmd(numa
, true);
5554 VLOG_DBG("PMD auto lb dry run. Predicted: Core %d on numa node %d "
5555 "to be assigned port \'%s\' rx queue %d "
5556 "(measured processing cycles %"PRIu64
").",
5557 pmd
->core_id
, numa_id
,
5558 netdev_rxq_get_name(rxqs
[i
]->rx
),
5559 netdev_rxq_get_queue_id(rxqs
[i
]->rx
),
5560 dp_netdev_rxq_get_cycles(rxqs
[i
], RXQ_CYCLES_PROC_HIST
));
5562 for (int id
= 0; id
< num_pmds
; id
++) {
5563 if (pmd
->core_id
== core_list
[id
]) {
5564 /* Add the processing cycles of rxq to pmd polling it. */
5565 pmd_usage
[id
] += dp_netdev_rxq_get_cycles(rxqs
[i
],
5566 RXQ_CYCLES_PROC_HIST
);
5571 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5572 uint64_t total_cycles
= 0;
5574 if ((pmd
->core_id
== NON_PMD_CORE_ID
) || pmd
->isolated
) {
5578 /* Get the total pmd cycles for an interval. */
5579 atomic_read_relaxed(&pmd
->intrvl_cycles
, &total_cycles
);
5580 /* Estimate the cycles to cover all intervals. */
5581 total_cycles
*= PMD_RXQ_INTERVAL_MAX
;
5582 for (int id
= 0; id
< num_pmds
; id
++) {
5583 if (pmd
->core_id
== core_list
[id
]) {
5584 if (pmd_usage
[id
]) {
5585 pmd_usage
[id
] = (pmd_usage
[id
] * 100) / total_cycles
;
5587 VLOG_DBG("PMD auto lb dry run. Predicted: Core %d, "
5588 "usage %"PRIu64
"", pmd
->core_id
, pmd_usage
[id
]);
5592 *predicted_variance
= variance(pmd_usage
, num_pmds
);
5596 rr_numa_list_destroy(&rr
);
5602 /* Does the dry run of Rxq assignment to PMDs and returns true if it gives
5603 * better distribution of load on PMDs. */
5605 pmd_rebalance_dry_run(struct dp_netdev
*dp
)
5606 OVS_REQUIRES(dp
->port_mutex
)
5608 struct dp_netdev_pmd_thread
*pmd
;
5609 uint64_t *curr_pmd_usage
;
5611 uint64_t curr_variance
;
5612 uint64_t new_variance
;
5613 uint64_t improvement
= 0;
5615 uint32_t *pmd_corelist
;
5616 struct rxq_poll
*poll
;
5619 num_pmds
= cmap_count(&dp
->poll_threads
);
5622 curr_pmd_usage
= xcalloc(num_pmds
, sizeof(uint64_t));
5623 pmd_corelist
= xcalloc(num_pmds
, sizeof(uint32_t));
5629 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5630 uint64_t total_cycles
= 0;
5631 uint64_t total_proc
= 0;
5633 if ((pmd
->core_id
== NON_PMD_CORE_ID
) || pmd
->isolated
) {
5637 /* Get the total pmd cycles for an interval. */
5638 atomic_read_relaxed(&pmd
->intrvl_cycles
, &total_cycles
);
5639 /* Estimate the cycles to cover all intervals. */
5640 total_cycles
*= PMD_RXQ_INTERVAL_MAX
;
5642 ovs_mutex_lock(&pmd
->port_mutex
);
5643 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
5644 for (unsigned i
= 0; i
< PMD_RXQ_INTERVAL_MAX
; i
++) {
5645 total_proc
+= dp_netdev_rxq_get_intrvl_cycles(poll
->rxq
, i
);
5648 ovs_mutex_unlock(&pmd
->port_mutex
);
5651 curr_pmd_usage
[num_pmds
] = (total_proc
* 100) / total_cycles
;
5654 VLOG_DBG("PMD auto lb dry run. Current: Core %d, usage %"PRIu64
"",
5655 pmd
->core_id
, curr_pmd_usage
[num_pmds
]);
5657 if (atomic_count_get(&pmd
->pmd_overloaded
)) {
5658 atomic_count_set(&pmd
->pmd_overloaded
, 0);
5661 pmd_corelist
[num_pmds
] = pmd
->core_id
;
5665 curr_variance
= variance(curr_pmd_usage
, num_pmds
);
5666 ret
= get_dry_run_variance(dp
, pmd_corelist
, num_pmds
, &new_variance
);
5669 VLOG_DBG("PMD auto lb dry run. Current PMD variance: %"PRIu64
","
5670 " Predicted PMD variance: %"PRIu64
"",
5671 curr_variance
, new_variance
);
5673 if (new_variance
< curr_variance
) {
5675 ((curr_variance
- new_variance
) * 100) / curr_variance
;
5677 if (improvement
< ALB_ACCEPTABLE_IMPROVEMENT
) {
5682 free(curr_pmd_usage
);
5688 /* Return true if needs to revalidate datapath flows. */
5690 dpif_netdev_run(struct dpif
*dpif
)
5692 struct dp_netdev_port
*port
;
5693 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5694 struct dp_netdev_pmd_thread
*non_pmd
;
5695 uint64_t new_tnl_seq
;
5696 bool need_to_flush
= true;
5697 bool pmd_rebalance
= false;
5698 long long int now
= time_msec();
5699 struct dp_netdev_pmd_thread
*pmd
;
5701 ovs_mutex_lock(&dp
->port_mutex
);
5702 non_pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
5704 ovs_mutex_lock(&dp
->non_pmd_mutex
);
5705 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
5706 if (!netdev_is_pmd(port
->netdev
)) {
5709 if (port
->emc_enabled
) {
5710 atomic_read_relaxed(&dp
->emc_insert_min
,
5711 &non_pmd
->ctx
.emc_insert_min
);
5713 non_pmd
->ctx
.emc_insert_min
= 0;
5716 for (i
= 0; i
< port
->n_rxq
; i
++) {
5718 if (!netdev_rxq_enabled(port
->rxqs
[i
].rx
)) {
5722 if (dp_netdev_process_rxq_port(non_pmd
,
5725 need_to_flush
= false;
5730 if (need_to_flush
) {
5731 /* We didn't receive anything in the process loop.
5732 * Check if we need to send something.
5733 * There was no time updates on current iteration. */
5734 pmd_thread_ctx_time_update(non_pmd
);
5735 dp_netdev_pmd_flush_output_packets(non_pmd
, false);
5738 dpif_netdev_xps_revalidate_pmd(non_pmd
, false);
5739 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
5741 dp_netdev_pmd_unref(non_pmd
);
5744 struct pmd_auto_lb
*pmd_alb
= &dp
->pmd_alb
;
5745 if (pmd_alb
->is_enabled
) {
5746 if (!pmd_alb
->rebalance_poll_timer
) {
5747 pmd_alb
->rebalance_poll_timer
= now
;
5748 } else if ((pmd_alb
->rebalance_poll_timer
+
5749 pmd_alb
->rebalance_intvl
) < now
) {
5750 pmd_alb
->rebalance_poll_timer
= now
;
5751 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5752 if (atomic_count_get(&pmd
->pmd_overloaded
) >=
5753 PMD_RXQ_INTERVAL_MAX
) {
5754 pmd_rebalance
= true;
5759 if (pmd_rebalance
&&
5760 !dp_netdev_is_reconf_required(dp
) &&
5761 !ports_require_restart(dp
) &&
5762 pmd_rebalance_dry_run(dp
)) {
5763 VLOG_INFO("PMD auto lb dry run."
5764 " requesting datapath reconfigure.");
5765 dp_netdev_request_reconfigure(dp
);
5770 if (dp_netdev_is_reconf_required(dp
) || ports_require_restart(dp
)) {
5771 reconfigure_datapath(dp
);
5773 ovs_mutex_unlock(&dp
->port_mutex
);
5775 tnl_neigh_cache_run();
5777 new_tnl_seq
= seq_read(tnl_conf_seq
);
5779 if (dp
->last_tnl_conf_seq
!= new_tnl_seq
) {
5780 dp
->last_tnl_conf_seq
= new_tnl_seq
;
5787 dpif_netdev_wait(struct dpif
*dpif
)
5789 struct dp_netdev_port
*port
;
5790 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5792 ovs_mutex_lock(&dp_netdev_mutex
);
5793 ovs_mutex_lock(&dp
->port_mutex
);
5794 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
5795 netdev_wait_reconf_required(port
->netdev
);
5796 if (!netdev_is_pmd(port
->netdev
)) {
5799 for (i
= 0; i
< port
->n_rxq
; i
++) {
5800 netdev_rxq_wait(port
->rxqs
[i
].rx
);
5804 ovs_mutex_unlock(&dp
->port_mutex
);
5805 ovs_mutex_unlock(&dp_netdev_mutex
);
5806 seq_wait(tnl_conf_seq
, dp
->last_tnl_conf_seq
);
5810 pmd_free_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
5812 struct tx_port
*tx_port_cached
;
5814 /* Flush all the queued packets. */
5815 dp_netdev_pmd_flush_output_packets(pmd
, true);
5816 /* Free all used tx queue ids. */
5817 dpif_netdev_xps_revalidate_pmd(pmd
, true);
5819 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->tnl_port_cache
) {
5820 free(tx_port_cached
);
5822 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->send_port_cache
) {
5823 free(tx_port_cached
);
5827 /* Copies ports from 'pmd->tx_ports' (shared with the main thread) to
5828 * thread-local copies. Copy to 'pmd->tnl_port_cache' if it is a tunnel
5829 * device, otherwise to 'pmd->send_port_cache' if the port has at least
5832 pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
5833 OVS_REQUIRES(pmd
->port_mutex
)
5835 struct tx_port
*tx_port
, *tx_port_cached
;
5837 pmd_free_cached_ports(pmd
);
5838 hmap_shrink(&pmd
->send_port_cache
);
5839 hmap_shrink(&pmd
->tnl_port_cache
);
5841 HMAP_FOR_EACH (tx_port
, node
, &pmd
->tx_ports
) {
5842 if (netdev_has_tunnel_push_pop(tx_port
->port
->netdev
)) {
5843 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
5844 hmap_insert(&pmd
->tnl_port_cache
, &tx_port_cached
->node
,
5845 hash_port_no(tx_port_cached
->port
->port_no
));
5848 if (netdev_n_txq(tx_port
->port
->netdev
)) {
5849 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
5850 hmap_insert(&pmd
->send_port_cache
, &tx_port_cached
->node
,
5851 hash_port_no(tx_port_cached
->port
->port_no
));
5857 pmd_alloc_static_tx_qid(struct dp_netdev_pmd_thread
*pmd
)
5859 ovs_mutex_lock(&pmd
->dp
->tx_qid_pool_mutex
);
5860 if (!id_pool_alloc_id(pmd
->dp
->tx_qid_pool
, &pmd
->static_tx_qid
)) {
5861 VLOG_ABORT("static_tx_qid allocation failed for PMD on core %2d"
5862 ", numa_id %d.", pmd
->core_id
, pmd
->numa_id
);
5864 ovs_mutex_unlock(&pmd
->dp
->tx_qid_pool_mutex
);
5866 VLOG_DBG("static_tx_qid = %d allocated for PMD thread on core %2d"
5867 ", numa_id %d.", pmd
->static_tx_qid
, pmd
->core_id
, pmd
->numa_id
);
5871 pmd_free_static_tx_qid(struct dp_netdev_pmd_thread
*pmd
)
5873 ovs_mutex_lock(&pmd
->dp
->tx_qid_pool_mutex
);
5874 id_pool_free_id(pmd
->dp
->tx_qid_pool
, pmd
->static_tx_qid
);
5875 ovs_mutex_unlock(&pmd
->dp
->tx_qid_pool_mutex
);
5879 pmd_load_queues_and_ports(struct dp_netdev_pmd_thread
*pmd
,
5880 struct polled_queue
**ppoll_list
)
5882 struct polled_queue
*poll_list
= *ppoll_list
;
5883 struct rxq_poll
*poll
;
5886 ovs_mutex_lock(&pmd
->port_mutex
);
5887 poll_list
= xrealloc(poll_list
, hmap_count(&pmd
->poll_list
)
5888 * sizeof *poll_list
);
5891 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
5892 poll_list
[i
].rxq
= poll
->rxq
;
5893 poll_list
[i
].port_no
= poll
->rxq
->port
->port_no
;
5894 poll_list
[i
].emc_enabled
= poll
->rxq
->port
->emc_enabled
;
5895 poll_list
[i
].rxq_enabled
= netdev_rxq_enabled(poll
->rxq
->rx
);
5896 poll_list
[i
].change_seq
=
5897 netdev_get_change_seq(poll
->rxq
->port
->netdev
);
5901 pmd_load_cached_ports(pmd
);
5903 ovs_mutex_unlock(&pmd
->port_mutex
);
5905 *ppoll_list
= poll_list
;
5910 pmd_thread_main(void *f_
)
5912 struct dp_netdev_pmd_thread
*pmd
= f_
;
5913 struct pmd_perf_stats
*s
= &pmd
->perf_stats
;
5914 unsigned int lc
= 0;
5915 struct polled_queue
*poll_list
;
5916 bool wait_for_reload
= false;
5922 int process_packets
= 0;
5926 /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */
5927 ovsthread_setspecific(pmd
->dp
->per_pmd_key
, pmd
);
5928 ovs_numa_thread_setaffinity_core(pmd
->core_id
);
5929 dpdk_set_lcore_id(pmd
->core_id
);
5930 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
5931 dfc_cache_init(&pmd
->flow_cache
);
5932 pmd_alloc_static_tx_qid(pmd
);
5935 atomic_count_init(&pmd
->pmd_overloaded
, 0);
5937 /* List port/core affinity */
5938 for (i
= 0; i
< poll_cnt
; i
++) {
5939 VLOG_DBG("Core %d processing port \'%s\' with queue-id %d\n",
5940 pmd
->core_id
, netdev_rxq_get_name(poll_list
[i
].rxq
->rx
),
5941 netdev_rxq_get_queue_id(poll_list
[i
].rxq
->rx
));
5942 /* Reset the rxq current cycles counter. */
5943 dp_netdev_rxq_set_cycles(poll_list
[i
].rxq
, RXQ_CYCLES_PROC_CURR
, 0);
5947 if (wait_for_reload
) {
5948 /* Don't sleep, control thread will ask for a reload shortly. */
5950 atomic_read_explicit(&pmd
->reload
, &reload
,
5951 memory_order_acquire
);
5954 while (seq_read(pmd
->reload_seq
) == pmd
->last_reload_seq
) {
5955 seq_wait(pmd
->reload_seq
, pmd
->last_reload_seq
);
5961 pmd
->intrvl_tsc_prev
= 0;
5962 atomic_store_relaxed(&pmd
->intrvl_cycles
, 0);
5963 cycles_counter_update(s
);
5965 pmd
->next_rcu_quiesce
= pmd
->ctx
.now
+ PMD_RCU_QUIESCE_INTERVAL
;
5967 /* Protect pmd stats from external clearing while polling. */
5968 ovs_mutex_lock(&pmd
->perf_stats
.stats_mutex
);
5970 uint64_t rx_packets
= 0, tx_packets
= 0;
5972 pmd_perf_start_iteration(s
);
5974 for (i
= 0; i
< poll_cnt
; i
++) {
5976 if (!poll_list
[i
].rxq_enabled
) {
5980 if (poll_list
[i
].emc_enabled
) {
5981 atomic_read_relaxed(&pmd
->dp
->emc_insert_min
,
5982 &pmd
->ctx
.emc_insert_min
);
5984 pmd
->ctx
.emc_insert_min
= 0;
5988 dp_netdev_process_rxq_port(pmd
, poll_list
[i
].rxq
,
5989 poll_list
[i
].port_no
);
5990 rx_packets
+= process_packets
;
5994 /* We didn't receive anything in the process loop.
5995 * Check if we need to send something.
5996 * There was no time updates on current iteration. */
5997 pmd_thread_ctx_time_update(pmd
);
5998 tx_packets
= dp_netdev_pmd_flush_output_packets(pmd
, false);
6001 /* Do RCU synchronization at fixed interval. This ensures that
6002 * synchronization would not be delayed long even at high load of
6003 * packet processing. */
6004 if (pmd
->ctx
.now
> pmd
->next_rcu_quiesce
) {
6005 if (!ovsrcu_try_quiesce()) {
6006 pmd
->next_rcu_quiesce
=
6007 pmd
->ctx
.now
+ PMD_RCU_QUIESCE_INTERVAL
;
6014 coverage_try_clear();
6015 dp_netdev_pmd_try_optimize(pmd
, poll_list
, poll_cnt
);
6016 if (!ovsrcu_try_quiesce()) {
6017 emc_cache_slow_sweep(&((pmd
->flow_cache
).emc_cache
));
6018 pmd
->next_rcu_quiesce
=
6019 pmd
->ctx
.now
+ PMD_RCU_QUIESCE_INTERVAL
;
6022 for (i
= 0; i
< poll_cnt
; i
++) {
6023 uint64_t current_seq
=
6024 netdev_get_change_seq(poll_list
[i
].rxq
->port
->netdev
);
6025 if (poll_list
[i
].change_seq
!= current_seq
) {
6026 poll_list
[i
].change_seq
= current_seq
;
6027 poll_list
[i
].rxq_enabled
=
6028 netdev_rxq_enabled(poll_list
[i
].rxq
->rx
);
6033 atomic_read_explicit(&pmd
->reload
, &reload
, memory_order_acquire
);
6034 if (OVS_UNLIKELY(reload
)) {
6038 pmd_perf_end_iteration(s
, rx_packets
, tx_packets
,
6039 pmd_perf_metrics_enabled(pmd
));
6041 ovs_mutex_unlock(&pmd
->perf_stats
.stats_mutex
);
6043 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
6044 atomic_read_relaxed(&pmd
->wait_for_reload
, &wait_for_reload
);
6045 atomic_read_relaxed(&pmd
->reload_tx_qid
, &reload_tx_qid
);
6046 atomic_read_relaxed(&pmd
->exit
, &exiting
);
6047 /* Signal here to make sure the pmd finishes
6048 * reloading the updated configuration. */
6049 dp_netdev_pmd_reload_done(pmd
);
6051 if (reload_tx_qid
) {
6052 pmd_free_static_tx_qid(pmd
);
6053 pmd_alloc_static_tx_qid(pmd
);
6060 pmd_free_static_tx_qid(pmd
);
6061 dfc_cache_uninit(&pmd
->flow_cache
);
6063 pmd_free_cached_ports(pmd
);
6068 dp_netdev_disable_upcall(struct dp_netdev
*dp
)
6069 OVS_ACQUIRES(dp
->upcall_rwlock
)
6071 fat_rwlock_wrlock(&dp
->upcall_rwlock
);
6077 dpif_netdev_meter_get_features(const struct dpif
* dpif OVS_UNUSED
,
6078 struct ofputil_meter_features
*features
)
6080 features
->max_meters
= MAX_METERS
;
6081 features
->band_types
= DP_SUPPORTED_METER_BAND_TYPES
;
6082 features
->capabilities
= DP_SUPPORTED_METER_FLAGS_MASK
;
6083 features
->max_bands
= MAX_BANDS
;
6084 features
->max_color
= 0;
6087 /* Applies the meter identified by 'meter_id' to 'packets_'. Packets
6088 * that exceed a band are dropped in-place. */
6090 dp_netdev_run_meter(struct dp_netdev
*dp
, struct dp_packet_batch
*packets_
,
6091 uint32_t meter_id
, long long int now
)
6093 struct dp_meter
*meter
;
6094 struct dp_meter_band
*band
;
6095 struct dp_packet
*packet
;
6096 long long int long_delta_t
; /* msec */
6097 uint32_t delta_t
; /* msec */
6098 const size_t cnt
= dp_packet_batch_size(packets_
);
6099 uint32_t bytes
, volume
;
6100 int exceeded_band
[NETDEV_MAX_BURST
];
6101 uint32_t exceeded_rate
[NETDEV_MAX_BURST
];
6102 int exceeded_pkt
= cnt
; /* First packet that exceeded a band rate. */
6104 if (meter_id
>= MAX_METERS
) {
6108 meter_lock(dp
, meter_id
);
6109 meter
= dp
->meters
[meter_id
];
6114 /* Initialize as negative values. */
6115 memset(exceeded_band
, 0xff, cnt
* sizeof *exceeded_band
);
6116 /* Initialize as zeroes. */
6117 memset(exceeded_rate
, 0, cnt
* sizeof *exceeded_rate
);
6119 /* All packets will hit the meter at the same time. */
6120 long_delta_t
= now
/ 1000 - meter
->used
/ 1000; /* msec */
6122 if (long_delta_t
< 0) {
6123 /* This condition means that we have several threads fighting for a
6124 meter lock, and the one who received the packets a bit later wins.
6125 Assuming that all racing threads received packets at the same time
6126 to avoid overflow. */
6130 /* Make sure delta_t will not be too large, so that bucket will not
6131 * wrap around below. */
6132 delta_t
= (long_delta_t
> (long long int)meter
->max_delta_t
)
6133 ? meter
->max_delta_t
: (uint32_t)long_delta_t
;
6135 /* Update meter stats. */
6137 meter
->packet_count
+= cnt
;
6139 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6140 bytes
+= dp_packet_size(packet
);
6142 meter
->byte_count
+= bytes
;
6144 /* Meters can operate in terms of packets per second or kilobits per
6146 if (meter
->flags
& OFPMF13_PKTPS
) {
6147 /* Rate in packets/second, bucket 1/1000 packets. */
6148 /* msec * packets/sec = 1/1000 packets. */
6149 volume
= cnt
* 1000; /* Take 'cnt' packets from the bucket. */
6151 /* Rate in kbps, bucket in bits. */
6152 /* msec * kbps = bits */
6156 /* Update all bands and find the one hit with the highest rate for each
6157 * packet (if any). */
6158 for (int m
= 0; m
< meter
->n_bands
; ++m
) {
6159 band
= &meter
->bands
[m
];
6161 /* Update band's bucket. */
6162 band
->bucket
+= delta_t
* band
->up
.rate
;
6163 if (band
->bucket
> band
->up
.burst_size
) {
6164 band
->bucket
= band
->up
.burst_size
;
6167 /* Drain the bucket for all the packets, if possible. */
6168 if (band
->bucket
>= volume
) {
6169 band
->bucket
-= volume
;
6171 int band_exceeded_pkt
;
6173 /* Band limit hit, must process packet-by-packet. */
6174 if (meter
->flags
& OFPMF13_PKTPS
) {
6175 band_exceeded_pkt
= band
->bucket
/ 1000;
6176 band
->bucket
%= 1000; /* Remainder stays in bucket. */
6178 /* Update the exceeding band for each exceeding packet.
6179 * (Only one band will be fired by a packet, and that
6180 * can be different for each packet.) */
6181 for (int i
= band_exceeded_pkt
; i
< cnt
; i
++) {
6182 if (band
->up
.rate
> exceeded_rate
[i
]) {
6183 exceeded_rate
[i
] = band
->up
.rate
;
6184 exceeded_band
[i
] = m
;
6188 /* Packet sizes differ, must process one-by-one. */
6189 band_exceeded_pkt
= cnt
;
6190 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6191 uint32_t bits
= dp_packet_size(packet
) * 8;
6193 if (band
->bucket
>= bits
) {
6194 band
->bucket
-= bits
;
6196 if (i
< band_exceeded_pkt
) {
6197 band_exceeded_pkt
= i
;
6199 /* Update the exceeding band for the exceeding packet.
6200 * (Only one band will be fired by a packet, and that
6201 * can be different for each packet.) */
6202 if (band
->up
.rate
> exceeded_rate
[i
]) {
6203 exceeded_rate
[i
] = band
->up
.rate
;
6204 exceeded_band
[i
] = m
;
6209 /* Remember the first exceeding packet. */
6210 if (exceeded_pkt
> band_exceeded_pkt
) {
6211 exceeded_pkt
= band_exceeded_pkt
;
6216 /* Fire the highest rate band exceeded by each packet, and drop
6217 * packets if needed. */
6219 DP_PACKET_BATCH_REFILL_FOR_EACH (j
, cnt
, packet
, packets_
) {
6220 if (exceeded_band
[j
] >= 0) {
6221 /* Meter drop packet. */
6222 band
= &meter
->bands
[exceeded_band
[j
]];
6223 band
->packet_count
+= 1;
6224 band
->byte_count
+= dp_packet_size(packet
);
6225 COVERAGE_INC(datapath_drop_meter
);
6226 dp_packet_delete(packet
);
6228 /* Meter accepts packet. */
6229 dp_packet_batch_refill(packets_
, packet
, j
);
6233 meter_unlock(dp
, meter_id
);
6236 /* Meter set/get/del processing is still single-threaded. */
6238 dpif_netdev_meter_set(struct dpif
*dpif
, ofproto_meter_id meter_id
,
6239 struct ofputil_meter_config
*config
)
6241 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6242 uint32_t mid
= meter_id
.uint32
;
6243 struct dp_meter
*meter
;
6246 if (mid
>= MAX_METERS
) {
6247 return EFBIG
; /* Meter_id out of range. */
6250 if (config
->flags
& ~DP_SUPPORTED_METER_FLAGS_MASK
) {
6251 return EBADF
; /* Unsupported flags set */
6254 if (config
->n_bands
> MAX_BANDS
) {
6258 for (i
= 0; i
< config
->n_bands
; ++i
) {
6259 switch (config
->bands
[i
].type
) {
6263 return ENODEV
; /* Unsupported band type */
6267 /* Allocate meter */
6268 meter
= xzalloc(sizeof *meter
6269 + config
->n_bands
* sizeof(struct dp_meter_band
));
6271 meter
->flags
= config
->flags
;
6272 meter
->n_bands
= config
->n_bands
;
6273 meter
->max_delta_t
= 0;
6274 meter
->used
= time_usec();
6277 for (i
= 0; i
< config
->n_bands
; ++i
) {
6278 uint32_t band_max_delta_t
;
6280 /* Set burst size to a workable value if none specified. */
6281 if (config
->bands
[i
].burst_size
== 0) {
6282 config
->bands
[i
].burst_size
= config
->bands
[i
].rate
;
6285 meter
->bands
[i
].up
= config
->bands
[i
];
6286 /* Convert burst size to the bucket units: */
6287 /* pkts => 1/1000 packets, kilobits => bits. */
6288 meter
->bands
[i
].up
.burst_size
*= 1000;
6289 /* Initialize bucket to empty. */
6290 meter
->bands
[i
].bucket
= 0;
6292 /* Figure out max delta_t that is enough to fill any bucket. */
6294 = meter
->bands
[i
].up
.burst_size
/ meter
->bands
[i
].up
.rate
;
6295 if (band_max_delta_t
> meter
->max_delta_t
) {
6296 meter
->max_delta_t
= band_max_delta_t
;
6300 meter_lock(dp
, mid
);
6301 dp_delete_meter(dp
, mid
); /* Free existing meter, if any */
6302 dp
->meters
[mid
] = meter
;
6303 meter_unlock(dp
, mid
);
6309 dpif_netdev_meter_get(const struct dpif
*dpif
,
6310 ofproto_meter_id meter_id_
,
6311 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
6313 const struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6314 uint32_t meter_id
= meter_id_
.uint32
;
6317 if (meter_id
>= MAX_METERS
) {
6321 meter_lock(dp
, meter_id
);
6322 const struct dp_meter
*meter
= dp
->meters
[meter_id
];
6330 stats
->packet_in_count
= meter
->packet_count
;
6331 stats
->byte_in_count
= meter
->byte_count
;
6333 for (i
= 0; i
< n_bands
&& i
< meter
->n_bands
; ++i
) {
6334 stats
->bands
[i
].packet_count
= meter
->bands
[i
].packet_count
;
6335 stats
->bands
[i
].byte_count
= meter
->bands
[i
].byte_count
;
6342 meter_unlock(dp
, meter_id
);
6347 dpif_netdev_meter_del(struct dpif
*dpif
,
6348 ofproto_meter_id meter_id_
,
6349 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
6351 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6354 error
= dpif_netdev_meter_get(dpif
, meter_id_
, stats
, n_bands
);
6356 uint32_t meter_id
= meter_id_
.uint32
;
6358 meter_lock(dp
, meter_id
);
6359 dp_delete_meter(dp
, meter_id
);
6360 meter_unlock(dp
, meter_id
);
6367 dpif_netdev_disable_upcall(struct dpif
*dpif
)
6368 OVS_NO_THREAD_SAFETY_ANALYSIS
6370 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6371 dp_netdev_disable_upcall(dp
);
6375 dp_netdev_enable_upcall(struct dp_netdev
*dp
)
6376 OVS_RELEASES(dp
->upcall_rwlock
)
6378 fat_rwlock_unlock(&dp
->upcall_rwlock
);
6382 dpif_netdev_enable_upcall(struct dpif
*dpif
)
6383 OVS_NO_THREAD_SAFETY_ANALYSIS
6385 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6386 dp_netdev_enable_upcall(dp
);
6390 dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
)
6392 atomic_store_relaxed(&pmd
->wait_for_reload
, false);
6393 atomic_store_relaxed(&pmd
->reload_tx_qid
, false);
6394 pmd
->last_reload_seq
= seq_read(pmd
->reload_seq
);
6395 atomic_store_explicit(&pmd
->reload
, false, memory_order_release
);
6398 /* Finds and refs the dp_netdev_pmd_thread on core 'core_id'. Returns
6399 * the pointer if succeeds, otherwise, NULL (it can return NULL even if
6400 * 'core_id' is NON_PMD_CORE_ID).
6402 * Caller must unrefs the returned reference. */
6403 static struct dp_netdev_pmd_thread
*
6404 dp_netdev_get_pmd(struct dp_netdev
*dp
, unsigned core_id
)
6406 struct dp_netdev_pmd_thread
*pmd
;
6407 const struct cmap_node
*pnode
;
6409 pnode
= cmap_find(&dp
->poll_threads
, hash_int(core_id
, 0));
6413 pmd
= CONTAINER_OF(pnode
, struct dp_netdev_pmd_thread
, node
);
6415 return dp_netdev_pmd_try_ref(pmd
) ? pmd
: NULL
;
6418 /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */
6420 dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
6421 OVS_REQUIRES(dp
->port_mutex
)
6423 struct dp_netdev_pmd_thread
*non_pmd
;
6425 non_pmd
= xzalloc(sizeof *non_pmd
);
6426 dp_netdev_configure_pmd(non_pmd
, dp
, NON_PMD_CORE_ID
, OVS_NUMA_UNSPEC
);
6429 /* Caller must have valid pointer to 'pmd'. */
6431 dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
)
6433 return ovs_refcount_try_ref_rcu(&pmd
->ref_cnt
);
6437 dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
)
6439 if (pmd
&& ovs_refcount_unref(&pmd
->ref_cnt
) == 1) {
6440 ovsrcu_postpone(dp_netdev_destroy_pmd
, pmd
);
6444 /* Given cmap position 'pos', tries to ref the next node. If try_ref()
6445 * fails, keeps checking for next node until reaching the end of cmap.
6447 * Caller must unrefs the returned reference. */
6448 static struct dp_netdev_pmd_thread
*
6449 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
)
6451 struct dp_netdev_pmd_thread
*next
;
6454 struct cmap_node
*node
;
6456 node
= cmap_next_position(&dp
->poll_threads
, pos
);
6457 next
= node
? CONTAINER_OF(node
, struct dp_netdev_pmd_thread
, node
)
6459 } while (next
&& !dp_netdev_pmd_try_ref(next
));
6464 /* Configures the 'pmd' based on the input argument. */
6466 dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
, struct dp_netdev
*dp
,
6467 unsigned core_id
, int numa_id
)
6470 pmd
->core_id
= core_id
;
6471 pmd
->numa_id
= numa_id
;
6472 pmd
->need_reload
= false;
6473 pmd
->n_output_batches
= 0;
6475 ovs_refcount_init(&pmd
->ref_cnt
);
6476 atomic_init(&pmd
->exit
, false);
6477 pmd
->reload_seq
= seq_create();
6478 pmd
->last_reload_seq
= seq_read(pmd
->reload_seq
);
6479 atomic_init(&pmd
->reload
, false);
6480 ovs_mutex_init(&pmd
->flow_mutex
);
6481 ovs_mutex_init(&pmd
->port_mutex
);
6482 ovs_mutex_init(&pmd
->bond_mutex
);
6483 cmap_init(&pmd
->flow_table
);
6484 cmap_init(&pmd
->classifiers
);
6485 pmd
->ctx
.last_rxq
= NULL
;
6486 pmd_thread_ctx_time_update(pmd
);
6487 pmd
->next_optimization
= pmd
->ctx
.now
+ DPCLS_OPTIMIZATION_INTERVAL
;
6488 pmd
->next_rcu_quiesce
= pmd
->ctx
.now
+ PMD_RCU_QUIESCE_INTERVAL
;
6489 pmd
->rxq_next_cycle_store
= pmd
->ctx
.now
+ PMD_RXQ_INTERVAL_LEN
;
6490 hmap_init(&pmd
->poll_list
);
6491 hmap_init(&pmd
->tx_ports
);
6492 hmap_init(&pmd
->tnl_port_cache
);
6493 hmap_init(&pmd
->send_port_cache
);
6494 cmap_init(&pmd
->tx_bonds
);
6495 /* init the 'flow_cache' since there is no
6496 * actual thread created for NON_PMD_CORE_ID. */
6497 if (core_id
== NON_PMD_CORE_ID
) {
6498 dfc_cache_init(&pmd
->flow_cache
);
6499 pmd_alloc_static_tx_qid(pmd
);
6501 pmd_perf_stats_init(&pmd
->perf_stats
);
6502 cmap_insert(&dp
->poll_threads
, CONST_CAST(struct cmap_node
*, &pmd
->node
),
6503 hash_int(core_id
, 0));
6507 dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
)
6511 dp_netdev_pmd_flow_flush(pmd
);
6512 hmap_destroy(&pmd
->send_port_cache
);
6513 hmap_destroy(&pmd
->tnl_port_cache
);
6514 hmap_destroy(&pmd
->tx_ports
);
6515 cmap_destroy(&pmd
->tx_bonds
);
6516 hmap_destroy(&pmd
->poll_list
);
6517 /* All flows (including their dpcls_rules) have been deleted already */
6518 CMAP_FOR_EACH (cls
, node
, &pmd
->classifiers
) {
6520 ovsrcu_postpone(free
, cls
);
6522 cmap_destroy(&pmd
->classifiers
);
6523 cmap_destroy(&pmd
->flow_table
);
6524 ovs_mutex_destroy(&pmd
->flow_mutex
);
6525 seq_destroy(pmd
->reload_seq
);
6526 ovs_mutex_destroy(&pmd
->port_mutex
);
6527 ovs_mutex_destroy(&pmd
->bond_mutex
);
6531 /* Stops the pmd thread, removes it from the 'dp->poll_threads',
6532 * and unrefs the struct. */
6534 dp_netdev_del_pmd(struct dp_netdev
*dp
, struct dp_netdev_pmd_thread
*pmd
)
6536 /* NON_PMD_CORE_ID doesn't have a thread, so we don't have to synchronize,
6537 * but extra cleanup is necessary */
6538 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
6539 ovs_mutex_lock(&dp
->non_pmd_mutex
);
6540 dfc_cache_uninit(&pmd
->flow_cache
);
6541 pmd_free_cached_ports(pmd
);
6542 pmd_free_static_tx_qid(pmd
);
6543 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
6545 atomic_store_relaxed(&pmd
->exit
, true);
6546 dp_netdev_reload_pmd__(pmd
);
6547 xpthread_join(pmd
->thread
, NULL
);
6550 dp_netdev_pmd_clear_ports(pmd
);
6552 /* Purges the 'pmd''s flows after stopping the thread, but before
6553 * destroying the flows, so that the flow stats can be collected. */
6554 if (dp
->dp_purge_cb
) {
6555 dp
->dp_purge_cb(dp
->dp_purge_aux
, pmd
->core_id
);
6557 cmap_remove(&pmd
->dp
->poll_threads
, &pmd
->node
, hash_int(pmd
->core_id
, 0));
6558 dp_netdev_pmd_unref(pmd
);
6561 /* Destroys all pmd threads. If 'non_pmd' is true it also destroys the non pmd
6564 dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
, bool non_pmd
)
6566 struct dp_netdev_pmd_thread
*pmd
;
6567 struct dp_netdev_pmd_thread
**pmd_list
;
6568 size_t k
= 0, n_pmds
;
6570 n_pmds
= cmap_count(&dp
->poll_threads
);
6571 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
6573 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
6574 if (!non_pmd
&& pmd
->core_id
== NON_PMD_CORE_ID
) {
6577 /* We cannot call dp_netdev_del_pmd(), since it alters
6578 * 'dp->poll_threads' (while we're iterating it) and it
6580 ovs_assert(k
< n_pmds
);
6581 pmd_list
[k
++] = pmd
;
6584 for (size_t i
= 0; i
< k
; i
++) {
6585 dp_netdev_del_pmd(dp
, pmd_list
[i
]);
6590 /* Deletes all rx queues from pmd->poll_list and all the ports from
6593 dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
)
6595 struct rxq_poll
*poll
;
6596 struct tx_port
*port
;
6599 ovs_mutex_lock(&pmd
->port_mutex
);
6600 HMAP_FOR_EACH_POP (poll
, node
, &pmd
->poll_list
) {
6603 HMAP_FOR_EACH_POP (port
, node
, &pmd
->tx_ports
) {
6606 ovs_mutex_unlock(&pmd
->port_mutex
);
6608 ovs_mutex_lock(&pmd
->bond_mutex
);
6609 CMAP_FOR_EACH (tx
, node
, &pmd
->tx_bonds
) {
6610 cmap_remove(&pmd
->tx_bonds
, &tx
->node
, hash_bond_id(tx
->bond_id
));
6611 ovsrcu_postpone(free
, tx
);
6613 ovs_mutex_unlock(&pmd
->bond_mutex
);
6616 /* Adds rx queue to poll_list of PMD thread, if it's not there already. */
6618 dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
6619 struct dp_netdev_rxq
*rxq
)
6620 OVS_REQUIRES(pmd
->port_mutex
)
6622 int qid
= netdev_rxq_get_queue_id(rxq
->rx
);
6623 uint32_t hash
= hash_2words(odp_to_u32(rxq
->port
->port_no
), qid
);
6624 struct rxq_poll
*poll
;
6626 HMAP_FOR_EACH_WITH_HASH (poll
, node
, hash
, &pmd
->poll_list
) {
6627 if (poll
->rxq
== rxq
) {
6628 /* 'rxq' is already polled by this thread. Do nothing. */
6633 poll
= xmalloc(sizeof *poll
);
6635 hmap_insert(&pmd
->poll_list
, &poll
->node
, hash
);
6637 pmd
->need_reload
= true;
6640 /* Delete 'poll' from poll_list of PMD thread. */
6642 dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
6643 struct rxq_poll
*poll
)
6644 OVS_REQUIRES(pmd
->port_mutex
)
6646 hmap_remove(&pmd
->poll_list
, &poll
->node
);
6649 pmd
->need_reload
= true;
6652 /* Add 'port' to the tx port cache of 'pmd', which must be reloaded for the
6653 * changes to take effect. */
6655 dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
6656 struct dp_netdev_port
*port
)
6657 OVS_REQUIRES(pmd
->port_mutex
)
6661 tx
= tx_port_lookup(&pmd
->tx_ports
, port
->port_no
);
6663 /* 'port' is already on this thread tx cache. Do nothing. */
6667 tx
= xzalloc(sizeof *tx
);
6671 tx
->flush_time
= 0LL;
6672 dp_packet_batch_init(&tx
->output_pkts
);
6674 hmap_insert(&pmd
->tx_ports
, &tx
->node
, hash_port_no(tx
->port
->port_no
));
6675 pmd
->need_reload
= true;
6678 /* Del 'tx' from the tx port cache of 'pmd', which must be reloaded for the
6679 * changes to take effect. */
6681 dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
6683 OVS_REQUIRES(pmd
->port_mutex
)
6685 hmap_remove(&pmd
->tx_ports
, &tx
->node
);
6687 pmd
->need_reload
= true;
6690 /* Add bond to the tx bond cmap of 'pmd'. */
6692 dp_netdev_add_bond_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
6693 struct tx_bond
*bond
, bool update
)
6694 OVS_EXCLUDED(pmd
->bond_mutex
)
6698 ovs_mutex_lock(&pmd
->bond_mutex
);
6699 tx
= tx_bond_lookup(&pmd
->tx_bonds
, bond
->bond_id
);
6701 if (tx
&& !update
) {
6702 /* It's not an update and the entry already exists. Do nothing. */
6707 struct tx_bond
*new_tx
= xmemdup(bond
, sizeof *bond
);
6709 /* Copy the stats for each bucket. */
6710 for (int i
= 0; i
< BOND_BUCKETS
; i
++) {
6711 uint64_t n_packets
, n_bytes
;
6713 atomic_read_relaxed(&tx
->slave_buckets
[i
].n_packets
, &n_packets
);
6714 atomic_read_relaxed(&tx
->slave_buckets
[i
].n_bytes
, &n_bytes
);
6715 atomic_init(&new_tx
->slave_buckets
[i
].n_packets
, n_packets
);
6716 atomic_init(&new_tx
->slave_buckets
[i
].n_bytes
, n_bytes
);
6718 cmap_replace(&pmd
->tx_bonds
, &tx
->node
, &new_tx
->node
,
6719 hash_bond_id(bond
->bond_id
));
6720 ovsrcu_postpone(free
, tx
);
6722 tx
= xmemdup(bond
, sizeof *bond
);
6723 cmap_insert(&pmd
->tx_bonds
, &tx
->node
, hash_bond_id(bond
->bond_id
));
6726 ovs_mutex_unlock(&pmd
->bond_mutex
);
6729 /* Delete bond from the tx bond cmap of 'pmd'. */
6731 dp_netdev_del_bond_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
6733 OVS_EXCLUDED(pmd
->bond_mutex
)
6737 ovs_mutex_lock(&pmd
->bond_mutex
);
6738 tx
= tx_bond_lookup(&pmd
->tx_bonds
, bond_id
);
6740 cmap_remove(&pmd
->tx_bonds
, &tx
->node
, hash_bond_id(tx
->bond_id
));
6741 ovsrcu_postpone(free
, tx
);
6743 ovs_mutex_unlock(&pmd
->bond_mutex
);
6747 dpif_netdev_get_datapath_version(void)
6749 return xstrdup("<built-in>");
6753 dp_netdev_flow_used(struct dp_netdev_flow
*netdev_flow
, int cnt
, int size
,
6754 uint16_t tcp_flags
, long long now
)
6758 atomic_store_relaxed(&netdev_flow
->stats
.used
, now
);
6759 non_atomic_ullong_add(&netdev_flow
->stats
.packet_count
, cnt
);
6760 non_atomic_ullong_add(&netdev_flow
->stats
.byte_count
, size
);
6761 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
6763 atomic_store_relaxed(&netdev_flow
->stats
.tcp_flags
, flags
);
6767 dp_netdev_upcall(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet
*packet_
,
6768 struct flow
*flow
, struct flow_wildcards
*wc
, ovs_u128
*ufid
,
6769 enum dpif_upcall_type type
, const struct nlattr
*userdata
,
6770 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
6772 struct dp_netdev
*dp
= pmd
->dp
;
6774 if (OVS_UNLIKELY(!dp
->upcall_cb
)) {
6778 if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl
))) {
6779 struct ds ds
= DS_EMPTY_INITIALIZER
;
6782 struct odp_flow_key_parms odp_parms
= {
6784 .mask
= wc
? &wc
->masks
: NULL
,
6785 .support
= dp_netdev_support
,
6788 ofpbuf_init(&key
, 0);
6789 odp_flow_key_from_flow(&odp_parms
, &key
);
6790 packet_str
= ofp_dp_packet_to_string(packet_
);
6792 odp_flow_key_format(key
.data
, key
.size
, &ds
);
6794 VLOG_DBG("%s: %s upcall:\n%s\n%s", dp
->name
,
6795 dpif_upcall_type_to_string(type
), ds_cstr(&ds
), packet_str
);
6797 ofpbuf_uninit(&key
);
6803 return dp
->upcall_cb(packet_
, flow
, ufid
, pmd
->core_id
, type
, userdata
,
6804 actions
, wc
, put_actions
, dp
->upcall_aux
);
6807 static inline uint32_t
6808 dpif_netdev_packet_get_rss_hash_orig_pkt(struct dp_packet
*packet
,
6809 const struct miniflow
*mf
)
6813 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
6814 hash
= dp_packet_get_rss_hash(packet
);
6816 hash
= miniflow_hash_5tuple(mf
, 0);
6817 dp_packet_set_rss_hash(packet
, hash
);
6823 static inline uint32_t
6824 dpif_netdev_packet_get_rss_hash(struct dp_packet
*packet
,
6825 const struct miniflow
*mf
)
6827 uint32_t hash
, recirc_depth
;
6829 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
6830 hash
= dp_packet_get_rss_hash(packet
);
6832 hash
= miniflow_hash_5tuple(mf
, 0);
6833 dp_packet_set_rss_hash(packet
, hash
);
6836 /* The RSS hash must account for the recirculation depth to avoid
6837 * collisions in the exact match cache */
6838 recirc_depth
= *recirc_depth_get_unsafe();
6839 if (OVS_UNLIKELY(recirc_depth
)) {
6840 hash
= hash_finish(hash
, recirc_depth
);
6845 struct packet_batch_per_flow
{
6846 unsigned int byte_count
;
6848 struct dp_netdev_flow
*flow
;
6850 struct dp_packet_batch array
;
6854 packet_batch_per_flow_update(struct packet_batch_per_flow
*batch
,
6855 struct dp_packet
*packet
,
6858 batch
->byte_count
+= dp_packet_size(packet
);
6859 batch
->tcp_flags
|= tcp_flags
;
6860 dp_packet_batch_add(&batch
->array
, packet
);
6864 packet_batch_per_flow_init(struct packet_batch_per_flow
*batch
,
6865 struct dp_netdev_flow
*flow
)
6867 flow
->batch
= batch
;
6870 dp_packet_batch_init(&batch
->array
);
6871 batch
->byte_count
= 0;
6872 batch
->tcp_flags
= 0;
6876 packet_batch_per_flow_execute(struct packet_batch_per_flow
*batch
,
6877 struct dp_netdev_pmd_thread
*pmd
)
6879 struct dp_netdev_actions
*actions
;
6880 struct dp_netdev_flow
*flow
= batch
->flow
;
6882 dp_netdev_flow_used(flow
, dp_packet_batch_size(&batch
->array
),
6884 batch
->tcp_flags
, pmd
->ctx
.now
/ 1000);
6886 actions
= dp_netdev_flow_get_actions(flow
);
6888 dp_netdev_execute_actions(pmd
, &batch
->array
, true, &flow
->flow
,
6889 actions
->actions
, actions
->size
);
6893 dp_netdev_queue_batches(struct dp_packet
*pkt
,
6894 struct dp_netdev_flow
*flow
, uint16_t tcp_flags
,
6895 struct packet_batch_per_flow
*batches
,
6898 struct packet_batch_per_flow
*batch
= flow
->batch
;
6900 if (OVS_UNLIKELY(!batch
)) {
6901 batch
= &batches
[(*n_batches
)++];
6902 packet_batch_per_flow_init(batch
, flow
);
6905 packet_batch_per_flow_update(batch
, pkt
, tcp_flags
);
6909 packet_enqueue_to_flow_map(struct dp_packet
*packet
,
6910 struct dp_netdev_flow
*flow
,
6912 struct dp_packet_flow_map
*flow_map
,
6915 struct dp_packet_flow_map
*map
= &flow_map
[index
];
6917 map
->packet
= packet
;
6918 map
->tcp_flags
= tcp_flags
;
6921 /* SMC lookup function for a batch of packets.
6922 * By doing batching SMC lookup, we can use prefetch
6923 * to hide memory access latency.
6926 smc_lookup_batch(struct dp_netdev_pmd_thread
*pmd
,
6927 struct netdev_flow_key
*keys
,
6928 struct netdev_flow_key
**missed_keys
,
6929 struct dp_packet_batch
*packets_
,
6931 struct dp_packet_flow_map
*flow_map
,
6935 struct dp_packet
*packet
;
6936 size_t n_smc_hit
= 0, n_missed
= 0;
6937 struct dfc_cache
*cache
= &pmd
->flow_cache
;
6938 struct smc_cache
*smc_cache
= &cache
->smc_cache
;
6939 const struct cmap_node
*flow_node
;
6943 /* Prefetch buckets for all packets */
6944 for (i
= 0; i
< cnt
; i
++) {
6945 OVS_PREFETCH(&smc_cache
->buckets
[keys
[i
].hash
& SMC_MASK
]);
6948 DP_PACKET_BATCH_REFILL_FOR_EACH (i
, cnt
, packet
, packets_
) {
6949 struct dp_netdev_flow
*flow
= NULL
;
6950 flow_node
= smc_entry_get(pmd
, keys
[i
].hash
);
6952 /* Get the original order of this packet in received batch. */
6953 recv_idx
= index_map
[i
];
6955 if (OVS_LIKELY(flow_node
!= NULL
)) {
6956 CMAP_NODE_FOR_EACH (flow
, node
, flow_node
) {
6957 /* Since we dont have per-port megaflow to check the port
6958 * number, we need to verify that the input ports match. */
6959 if (OVS_LIKELY(dpcls_rule_matches_key(&flow
->cr
, &keys
[i
]) &&
6960 flow
->flow
.in_port
.odp_port
== packet
->md
.in_port
.odp_port
)) {
6961 tcp_flags
= miniflow_get_tcp_flags(&keys
[i
].mf
);
6963 /* SMC hit and emc miss, we insert into EMC */
6965 netdev_flow_key_size(miniflow_n_values(&keys
[i
].mf
));
6966 emc_probabilistic_insert(pmd
, &keys
[i
], flow
);
6967 /* Add these packets into the flow map in the same order
6970 packet_enqueue_to_flow_map(packet
, flow
, tcp_flags
,
6971 flow_map
, recv_idx
);
6982 /* SMC missed. Group missed packets together at
6983 * the beginning of the 'packets' array. */
6984 dp_packet_batch_refill(packets_
, packet
, i
);
6986 /* Preserve the order of packet for flow batching. */
6987 index_map
[n_missed
] = recv_idx
;
6989 /* Put missed keys to the pointer arrays return to the caller */
6990 missed_keys
[n_missed
++] = &keys
[i
];
6993 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_SMC_HIT
, n_smc_hit
);
6996 /* Try to process all ('cnt') the 'packets' using only the datapath flow cache
6997 * 'pmd->flow_cache'. If a flow is not found for a packet 'packets[i]', the
6998 * miniflow is copied into 'keys' and the packet pointer is moved at the
6999 * beginning of the 'packets' array. The pointers of missed keys are put in the
7000 * missed_keys pointer array for future processing.
7002 * The function returns the number of packets that needs to be processed in the
7003 * 'packets' array (they have been moved to the beginning of the vector).
7005 * For performance reasons a caller may choose not to initialize the metadata
7006 * in 'packets_'. If 'md_is_valid' is false, the metadata in 'packets'
7007 * is not valid and must be initialized by this function using 'port_no'.
7008 * If 'md_is_valid' is true, the metadata is already valid and 'port_no'
7011 static inline size_t
7012 dfc_processing(struct dp_netdev_pmd_thread
*pmd
,
7013 struct dp_packet_batch
*packets_
,
7014 struct netdev_flow_key
*keys
,
7015 struct netdev_flow_key
**missed_keys
,
7016 struct packet_batch_per_flow batches
[], size_t *n_batches
,
7017 struct dp_packet_flow_map
*flow_map
,
7018 size_t *n_flows
, uint8_t *index_map
,
7019 bool md_is_valid
, odp_port_t port_no
)
7021 struct netdev_flow_key
*key
= &keys
[0];
7022 size_t n_missed
= 0, n_emc_hit
= 0;
7023 struct dfc_cache
*cache
= &pmd
->flow_cache
;
7024 struct dp_packet
*packet
;
7025 const size_t cnt
= dp_packet_batch_size(packets_
);
7026 uint32_t cur_min
= pmd
->ctx
.emc_insert_min
;
7031 bool batch_enable
= true;
7033 atomic_read_relaxed(&pmd
->dp
->smc_enable_db
, &smc_enable_db
);
7034 pmd_perf_update_counter(&pmd
->perf_stats
,
7035 md_is_valid
? PMD_STAT_RECIRC
: PMD_STAT_RECV
,
7038 DP_PACKET_BATCH_REFILL_FOR_EACH (i
, cnt
, packet
, packets_
) {
7039 struct dp_netdev_flow
*flow
;
7042 if (OVS_UNLIKELY(dp_packet_size(packet
) < ETH_HEADER_LEN
)) {
7043 dp_packet_delete(packet
);
7044 COVERAGE_INC(datapath_drop_rx_invalid_packet
);
7049 struct dp_packet
**packets
= packets_
->packets
;
7050 /* Prefetch next packet data and metadata. */
7051 OVS_PREFETCH(dp_packet_data(packets
[i
+1]));
7052 pkt_metadata_prefetch_init(&packets
[i
+1]->md
);
7056 pkt_metadata_init(&packet
->md
, port_no
);
7059 if ((*recirc_depth_get() == 0) &&
7060 dp_packet_has_flow_mark(packet
, &mark
)) {
7061 flow
= mark_to_flow_find(pmd
, mark
);
7062 if (OVS_LIKELY(flow
)) {
7063 tcp_flags
= parse_tcp_flags(packet
);
7064 if (OVS_LIKELY(batch_enable
)) {
7065 dp_netdev_queue_batches(packet
, flow
, tcp_flags
, batches
,
7068 /* Flow batching should be performed only after fast-path
7069 * processing is also completed for packets with emc miss
7070 * or else it will result in reordering of packets with
7071 * same datapath flows. */
7072 packet_enqueue_to_flow_map(packet
, flow
, tcp_flags
,
7073 flow_map
, map_cnt
++);
7079 miniflow_extract(packet
, &key
->mf
);
7080 key
->len
= 0; /* Not computed yet. */
7082 (md_is_valid
== false)
7083 ? dpif_netdev_packet_get_rss_hash_orig_pkt(packet
, &key
->mf
)
7084 : dpif_netdev_packet_get_rss_hash(packet
, &key
->mf
);
7086 /* If EMC is disabled skip emc_lookup */
7087 flow
= (cur_min
!= 0) ? emc_lookup(&cache
->emc_cache
, key
) : NULL
;
7088 if (OVS_LIKELY(flow
)) {
7089 tcp_flags
= miniflow_get_tcp_flags(&key
->mf
);
7091 if (OVS_LIKELY(batch_enable
)) {
7092 dp_netdev_queue_batches(packet
, flow
, tcp_flags
, batches
,
7095 /* Flow batching should be performed only after fast-path
7096 * processing is also completed for packets with emc miss
7097 * or else it will result in reordering of packets with
7098 * same datapath flows. */
7099 packet_enqueue_to_flow_map(packet
, flow
, tcp_flags
,
7100 flow_map
, map_cnt
++);
7103 /* Exact match cache missed. Group missed packets together at
7104 * the beginning of the 'packets' array. */
7105 dp_packet_batch_refill(packets_
, packet
, i
);
7107 /* Preserve the order of packet for flow batching. */
7108 index_map
[n_missed
] = map_cnt
;
7109 flow_map
[map_cnt
++].flow
= NULL
;
7111 /* 'key[n_missed]' contains the key of the current packet and it
7112 * will be passed to SMC lookup. The next key should be extracted
7113 * to 'keys[n_missed + 1]'.
7114 * We also maintain a pointer array to keys missed both SMC and EMC
7115 * which will be returned to the caller for future processing. */
7116 missed_keys
[n_missed
] = key
;
7117 key
= &keys
[++n_missed
];
7119 /* Skip batching for subsequent packets to avoid reordering. */
7120 batch_enable
= false;
7123 /* Count of packets which are not flow batched. */
7126 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_EXACT_HIT
, n_emc_hit
);
7128 if (!smc_enable_db
) {
7129 return dp_packet_batch_size(packets_
);
7132 /* Packets miss EMC will do a batch lookup in SMC if enabled */
7133 smc_lookup_batch(pmd
, keys
, missed_keys
, packets_
,
7134 n_missed
, flow_map
, index_map
);
7136 return dp_packet_batch_size(packets_
);
7140 handle_packet_upcall(struct dp_netdev_pmd_thread
*pmd
,
7141 struct dp_packet
*packet
,
7142 const struct netdev_flow_key
*key
,
7143 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
7145 struct ofpbuf
*add_actions
;
7146 struct dp_packet_batch b
;
7150 uint64_t cycles
= cycles_counter_update(&pmd
->perf_stats
);
7152 match
.tun_md
.valid
= false;
7153 miniflow_expand(&key
->mf
, &match
.flow
);
7154 memset(&match
.wc
, 0, sizeof match
.wc
);
7156 ofpbuf_clear(actions
);
7157 ofpbuf_clear(put_actions
);
7159 odp_flow_key_hash(&match
.flow
, sizeof match
.flow
, &ufid
);
7160 error
= dp_netdev_upcall(pmd
, packet
, &match
.flow
, &match
.wc
,
7161 &ufid
, DPIF_UC_MISS
, NULL
, actions
,
7163 if (OVS_UNLIKELY(error
&& error
!= ENOSPC
)) {
7164 dp_packet_delete(packet
);
7165 COVERAGE_INC(datapath_drop_upcall_error
);
7169 /* The Netlink encoding of datapath flow keys cannot express
7170 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
7171 * tag is interpreted as exact match on the fact that there is no
7172 * VLAN. Unless we refactor a lot of code that translates between
7173 * Netlink and struct flow representations, we have to do the same
7174 * here. This must be in sync with 'match' in dpif_netdev_flow_put(). */
7175 if (!match
.wc
.masks
.vlans
[0].tci
) {
7176 match
.wc
.masks
.vlans
[0].tci
= htons(0xffff);
7179 /* We can't allow the packet batching in the next loop to execute
7180 * the actions. Otherwise, if there are any slow path actions,
7181 * we'll send the packet up twice. */
7182 dp_packet_batch_init_packet(&b
, packet
);
7183 dp_netdev_execute_actions(pmd
, &b
, true, &match
.flow
,
7184 actions
->data
, actions
->size
);
7186 add_actions
= put_actions
->size
? put_actions
: actions
;
7187 if (OVS_LIKELY(error
!= ENOSPC
)) {
7188 struct dp_netdev_flow
*netdev_flow
;
7190 /* XXX: There's a race window where a flow covering this packet
7191 * could have already been installed since we last did the flow
7192 * lookup before upcall. This could be solved by moving the
7193 * mutex lock outside the loop, but that's an awful long time
7194 * to be locking revalidators out of making flow modifications. */
7195 ovs_mutex_lock(&pmd
->flow_mutex
);
7196 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
, NULL
);
7197 if (OVS_LIKELY(!netdev_flow
)) {
7198 netdev_flow
= dp_netdev_flow_add(pmd
, &match
, &ufid
,
7202 ovs_mutex_unlock(&pmd
->flow_mutex
);
7203 uint32_t hash
= dp_netdev_flow_hash(&netdev_flow
->ufid
);
7204 smc_insert(pmd
, key
, hash
);
7205 emc_probabilistic_insert(pmd
, key
, netdev_flow
);
7207 if (pmd_perf_metrics_enabled(pmd
)) {
7208 /* Update upcall stats. */
7209 cycles
= cycles_counter_update(&pmd
->perf_stats
) - cycles
;
7210 struct pmd_perf_stats
*s
= &pmd
->perf_stats
;
7211 s
->current
.upcalls
++;
7212 s
->current
.upcall_cycles
+= cycles
;
7213 histogram_add_sample(&s
->cycles_per_upcall
, cycles
);
7219 fast_path_processing(struct dp_netdev_pmd_thread
*pmd
,
7220 struct dp_packet_batch
*packets_
,
7221 struct netdev_flow_key
**keys
,
7222 struct dp_packet_flow_map
*flow_map
,
7226 const size_t cnt
= dp_packet_batch_size(packets_
);
7227 #if !defined(__CHECKER__) && !defined(_WIN32)
7228 const size_t PKT_ARRAY_SIZE
= cnt
;
7230 /* Sparse or MSVC doesn't like variable length array. */
7231 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
7233 struct dp_packet
*packet
;
7235 struct dpcls_rule
*rules
[PKT_ARRAY_SIZE
];
7236 struct dp_netdev
*dp
= pmd
->dp
;
7237 int upcall_ok_cnt
= 0, upcall_fail_cnt
= 0;
7238 int lookup_cnt
= 0, add_lookup_cnt
;
7241 for (size_t i
= 0; i
< cnt
; i
++) {
7242 /* Key length is needed in all the cases, hash computed on demand. */
7243 keys
[i
]->len
= netdev_flow_key_size(miniflow_n_values(&keys
[i
]->mf
));
7245 /* Get the classifier for the in_port */
7246 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
7247 if (OVS_LIKELY(cls
)) {
7248 any_miss
= !dpcls_lookup(cls
, (const struct netdev_flow_key
**)keys
,
7249 rules
, cnt
, &lookup_cnt
);
7252 memset(rules
, 0, sizeof(rules
));
7254 if (OVS_UNLIKELY(any_miss
) && !fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
7255 uint64_t actions_stub
[512 / 8], slow_stub
[512 / 8];
7256 struct ofpbuf actions
, put_actions
;
7258 ofpbuf_use_stub(&actions
, actions_stub
, sizeof actions_stub
);
7259 ofpbuf_use_stub(&put_actions
, slow_stub
, sizeof slow_stub
);
7261 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
7262 struct dp_netdev_flow
*netdev_flow
;
7264 if (OVS_LIKELY(rules
[i
])) {
7268 /* It's possible that an earlier slow path execution installed
7269 * a rule covering this flow. In this case, it's a lot cheaper
7270 * to catch it here than execute a miss. */
7271 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, keys
[i
],
7274 lookup_cnt
+= add_lookup_cnt
;
7275 rules
[i
] = &netdev_flow
->cr
;
7279 int error
= handle_packet_upcall(pmd
, packet
, keys
[i
],
7280 &actions
, &put_actions
);
7282 if (OVS_UNLIKELY(error
)) {
7289 ofpbuf_uninit(&actions
);
7290 ofpbuf_uninit(&put_actions
);
7291 fat_rwlock_unlock(&dp
->upcall_rwlock
);
7292 } else if (OVS_UNLIKELY(any_miss
)) {
7293 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
7294 if (OVS_UNLIKELY(!rules
[i
])) {
7295 dp_packet_delete(packet
);
7296 COVERAGE_INC(datapath_drop_lock_error
);
7302 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
7303 struct dp_netdev_flow
*flow
;
7304 /* Get the original order of this packet in received batch. */
7305 int recv_idx
= index_map
[i
];
7308 if (OVS_UNLIKELY(!rules
[i
])) {
7312 flow
= dp_netdev_flow_cast(rules
[i
]);
7313 uint32_t hash
= dp_netdev_flow_hash(&flow
->ufid
);
7314 smc_insert(pmd
, keys
[i
], hash
);
7316 emc_probabilistic_insert(pmd
, keys
[i
], flow
);
7317 /* Add these packets into the flow map in the same order
7320 tcp_flags
= miniflow_get_tcp_flags(&keys
[i
]->mf
);
7321 packet_enqueue_to_flow_map(packet
, flow
, tcp_flags
,
7322 flow_map
, recv_idx
);
7325 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MASKED_HIT
,
7326 cnt
- upcall_ok_cnt
- upcall_fail_cnt
);
7327 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MASKED_LOOKUP
,
7329 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MISS
,
7331 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_LOST
,
7335 /* Packets enter the datapath from a port (or from recirculation) here.
7337 * When 'md_is_valid' is true the metadata in 'packets' are already valid.
7338 * When false the metadata in 'packets' need to be initialized. */
7340 dp_netdev_input__(struct dp_netdev_pmd_thread
*pmd
,
7341 struct dp_packet_batch
*packets
,
7342 bool md_is_valid
, odp_port_t port_no
)
7344 #if !defined(__CHECKER__) && !defined(_WIN32)
7345 const size_t PKT_ARRAY_SIZE
= dp_packet_batch_size(packets
);
7347 /* Sparse or MSVC doesn't like variable length array. */
7348 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
7350 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
)
7351 struct netdev_flow_key keys
[PKT_ARRAY_SIZE
];
7352 struct netdev_flow_key
*missed_keys
[PKT_ARRAY_SIZE
];
7353 struct packet_batch_per_flow batches
[PKT_ARRAY_SIZE
];
7355 struct dp_packet_flow_map flow_map
[PKT_ARRAY_SIZE
];
7356 uint8_t index_map
[PKT_ARRAY_SIZE
];
7362 dfc_processing(pmd
, packets
, keys
, missed_keys
, batches
, &n_batches
,
7363 flow_map
, &n_flows
, index_map
, md_is_valid
, port_no
);
7365 if (!dp_packet_batch_is_empty(packets
)) {
7366 /* Get ingress port from first packet's metadata. */
7367 in_port
= packets
->packets
[0]->md
.in_port
.odp_port
;
7368 fast_path_processing(pmd
, packets
, missed_keys
,
7369 flow_map
, index_map
, in_port
);
7372 /* Batch rest of packets which are in flow map. */
7373 for (i
= 0; i
< n_flows
; i
++) {
7374 struct dp_packet_flow_map
*map
= &flow_map
[i
];
7376 if (OVS_UNLIKELY(!map
->flow
)) {
7379 dp_netdev_queue_batches(map
->packet
, map
->flow
, map
->tcp_flags
,
7380 batches
, &n_batches
);
7383 /* All the flow batches need to be reset before any call to
7384 * packet_batch_per_flow_execute() as it could potentially trigger
7385 * recirculation. When a packet matching flow ‘j’ happens to be
7386 * recirculated, the nested call to dp_netdev_input__() could potentially
7387 * classify the packet as matching another flow - say 'k'. It could happen
7388 * that in the previous call to dp_netdev_input__() that same flow 'k' had
7389 * already its own batches[k] still waiting to be served. So if its
7390 * ‘batch’ member is not reset, the recirculated packet would be wrongly
7391 * appended to batches[k] of the 1st call to dp_netdev_input__(). */
7392 for (i
= 0; i
< n_batches
; i
++) {
7393 batches
[i
].flow
->batch
= NULL
;
7396 for (i
= 0; i
< n_batches
; i
++) {
7397 packet_batch_per_flow_execute(&batches
[i
], pmd
);
7402 dp_netdev_input(struct dp_netdev_pmd_thread
*pmd
,
7403 struct dp_packet_batch
*packets
,
7406 dp_netdev_input__(pmd
, packets
, false, port_no
);
7410 dp_netdev_recirculate(struct dp_netdev_pmd_thread
*pmd
,
7411 struct dp_packet_batch
*packets
)
7413 dp_netdev_input__(pmd
, packets
, true, 0);
7416 struct dp_netdev_execute_aux
{
7417 struct dp_netdev_pmd_thread
*pmd
;
7418 const struct flow
*flow
;
7422 dpif_netdev_register_dp_purge_cb(struct dpif
*dpif
, dp_purge_callback
*cb
,
7425 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7426 dp
->dp_purge_aux
= aux
;
7427 dp
->dp_purge_cb
= cb
;
7431 dpif_netdev_register_upcall_cb(struct dpif
*dpif
, upcall_callback
*cb
,
7434 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7435 dp
->upcall_aux
= aux
;
7440 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
7444 struct dp_netdev_port
*port
;
7447 HMAP_FOR_EACH (tx
, node
, &pmd
->send_port_cache
) {
7448 if (!tx
->port
->dynamic_txqs
) {
7451 interval
= pmd
->ctx
.now
- tx
->last_used
;
7452 if (tx
->qid
>= 0 && (purge
|| interval
>= XPS_TIMEOUT
)) {
7454 ovs_mutex_lock(&port
->txq_used_mutex
);
7455 port
->txq_used
[tx
->qid
]--;
7456 ovs_mutex_unlock(&port
->txq_used_mutex
);
7463 dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
7466 struct dp_netdev_port
*port
;
7468 int i
, min_cnt
, min_qid
;
7470 interval
= pmd
->ctx
.now
- tx
->last_used
;
7471 tx
->last_used
= pmd
->ctx
.now
;
7473 if (OVS_LIKELY(tx
->qid
>= 0 && interval
< XPS_TIMEOUT
)) {
7479 ovs_mutex_lock(&port
->txq_used_mutex
);
7481 port
->txq_used
[tx
->qid
]--;
7487 for (i
= 0; i
< netdev_n_txq(port
->netdev
); i
++) {
7488 if (port
->txq_used
[i
] < min_cnt
|| min_cnt
== -1) {
7489 min_cnt
= port
->txq_used
[i
];
7494 port
->txq_used
[min_qid
]++;
7497 ovs_mutex_unlock(&port
->txq_used_mutex
);
7499 dpif_netdev_xps_revalidate_pmd(pmd
, false);
7501 VLOG_DBG("Core %d: New TX queue ID %d for port \'%s\'.",
7502 pmd
->core_id
, tx
->qid
, netdev_get_name(tx
->port
->netdev
));
7506 static struct tx_port
*
7507 pmd_tnl_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
7510 return tx_port_lookup(&pmd
->tnl_port_cache
, port_no
);
7513 static struct tx_port
*
7514 pmd_send_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
7517 return tx_port_lookup(&pmd
->send_port_cache
, port_no
);
7521 push_tnl_action(const struct dp_netdev_pmd_thread
*pmd
,
7522 const struct nlattr
*attr
,
7523 struct dp_packet_batch
*batch
)
7525 struct tx_port
*tun_port
;
7526 const struct ovs_action_push_tnl
*data
;
7529 data
= nl_attr_get(attr
);
7531 tun_port
= pmd_tnl_port_cache_lookup(pmd
, data
->tnl_port
);
7536 err
= netdev_push_header(tun_port
->port
->netdev
, batch
, data
);
7541 dp_packet_delete_batch(batch
, true);
7546 dp_execute_userspace_action(struct dp_netdev_pmd_thread
*pmd
,
7547 struct dp_packet
*packet
, bool should_steal
,
7548 struct flow
*flow
, ovs_u128
*ufid
,
7549 struct ofpbuf
*actions
,
7550 const struct nlattr
*userdata
)
7552 struct dp_packet_batch b
;
7555 ofpbuf_clear(actions
);
7557 error
= dp_netdev_upcall(pmd
, packet
, flow
, NULL
, ufid
,
7558 DPIF_UC_ACTION
, userdata
, actions
,
7560 if (!error
|| error
== ENOSPC
) {
7561 dp_packet_batch_init_packet(&b
, packet
);
7562 dp_netdev_execute_actions(pmd
, &b
, should_steal
, flow
,
7563 actions
->data
, actions
->size
);
7564 } else if (should_steal
) {
7565 dp_packet_delete(packet
);
7566 COVERAGE_INC(datapath_drop_userspace_action_error
);
7571 dp_execute_output_action(struct dp_netdev_pmd_thread
*pmd
,
7572 struct dp_packet_batch
*packets_
,
7573 bool should_steal
, odp_port_t port_no
)
7575 struct tx_port
*p
= pmd_send_port_cache_lookup(pmd
, port_no
);
7576 struct dp_packet_batch out
;
7578 if (!OVS_LIKELY(p
)) {
7579 COVERAGE_ADD(datapath_drop_invalid_port
,
7580 dp_packet_batch_size(packets_
));
7581 dp_packet_delete_batch(packets_
, should_steal
);
7584 if (!should_steal
) {
7585 dp_packet_batch_clone(&out
, packets_
);
7586 dp_packet_batch_reset_cutlen(packets_
);
7589 dp_packet_batch_apply_cutlen(packets_
);
7591 if (OVS_UNLIKELY(!dp_packet_batch_is_empty(&p
->output_pkts
)
7592 && packets_
->packets
[0]->source
7593 != p
->output_pkts
.packets
[0]->source
)) {
7594 /* XXX: netdev-dpdk assumes that all packets in a single
7595 * output batch has the same source. Flush here to
7596 * avoid memory access issues. */
7597 dp_netdev_pmd_flush_output_on_port(pmd
, p
);
7600 if (dp_packet_batch_size(&p
->output_pkts
)
7601 + dp_packet_batch_size(packets_
) > NETDEV_MAX_BURST
) {
7602 /* Flush here to avoid overflow. */
7603 dp_netdev_pmd_flush_output_on_port(pmd
, p
);
7605 if (dp_packet_batch_is_empty(&p
->output_pkts
)) {
7606 pmd
->n_output_batches
++;
7609 struct dp_packet
*packet
;
7610 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
7611 p
->output_pkts_rxqs
[dp_packet_batch_size(&p
->output_pkts
)] =
7613 dp_packet_batch_add(&p
->output_pkts
, packet
);
7619 dp_execute_lb_output_action(struct dp_netdev_pmd_thread
*pmd
,
7620 struct dp_packet_batch
*packets_
,
7621 bool should_steal
, uint32_t bond
)
7623 struct tx_bond
*p_bond
= tx_bond_lookup(&pmd
->tx_bonds
, bond
);
7624 struct dp_packet_batch out
;
7625 struct dp_packet
*packet
;
7628 COVERAGE_ADD(datapath_drop_invalid_bond
,
7629 dp_packet_batch_size(packets_
));
7630 dp_packet_delete_batch(packets_
, should_steal
);
7633 if (!should_steal
) {
7634 dp_packet_batch_clone(&out
, packets_
);
7635 dp_packet_batch_reset_cutlen(packets_
);
7638 dp_packet_batch_apply_cutlen(packets_
);
7640 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
7642 * Lookup the bond-hash table using hash to get the slave.
7644 uint32_t hash
= dp_packet_get_rss_hash(packet
);
7645 struct slave_entry
*s_entry
= &p_bond
->slave_buckets
[hash
& BOND_MASK
];
7646 odp_port_t bond_member
= s_entry
->slave_id
;
7647 uint32_t size
= dp_packet_size(packet
);
7648 struct dp_packet_batch output_pkt
;
7650 dp_packet_batch_init_packet(&output_pkt
, packet
);
7651 if (OVS_LIKELY(dp_execute_output_action(pmd
, &output_pkt
, true,
7653 /* Update slave stats. */
7654 non_atomic_ullong_add(&s_entry
->n_packets
, 1);
7655 non_atomic_ullong_add(&s_entry
->n_bytes
, size
);
7661 dp_execute_cb(void *aux_
, struct dp_packet_batch
*packets_
,
7662 const struct nlattr
*a
, bool should_steal
)
7663 OVS_NO_THREAD_SAFETY_ANALYSIS
7665 struct dp_netdev_execute_aux
*aux
= aux_
;
7666 uint32_t *depth
= recirc_depth_get();
7667 struct dp_netdev_pmd_thread
*pmd
= aux
->pmd
;
7668 struct dp_netdev
*dp
= pmd
->dp
;
7669 int type
= nl_attr_type(a
);
7671 uint32_t packet_count
, packets_dropped
;
7673 switch ((enum ovs_action_attr
)type
) {
7674 case OVS_ACTION_ATTR_OUTPUT
:
7675 dp_execute_output_action(pmd
, packets_
, should_steal
,
7676 nl_attr_get_odp_port(a
));
7679 case OVS_ACTION_ATTR_LB_OUTPUT
:
7680 dp_execute_lb_output_action(pmd
, packets_
, should_steal
,
7681 nl_attr_get_u32(a
));
7684 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
7686 /* We're requested to push tunnel header, but also we need to take
7687 * the ownership of these packets. Thus, we can avoid performing
7688 * the action, because the caller will not use the result anyway.
7689 * Just break to free the batch. */
7692 dp_packet_batch_apply_cutlen(packets_
);
7693 packet_count
= dp_packet_batch_size(packets_
);
7694 if (push_tnl_action(pmd
, a
, packets_
)) {
7695 COVERAGE_ADD(datapath_drop_tunnel_push_error
,
7700 case OVS_ACTION_ATTR_TUNNEL_POP
:
7701 if (*depth
< MAX_RECIRC_DEPTH
) {
7702 struct dp_packet_batch
*orig_packets_
= packets_
;
7703 odp_port_t portno
= nl_attr_get_odp_port(a
);
7705 p
= pmd_tnl_port_cache_lookup(pmd
, portno
);
7707 struct dp_packet_batch tnl_pkt
;
7709 if (!should_steal
) {
7710 dp_packet_batch_clone(&tnl_pkt
, packets_
);
7711 packets_
= &tnl_pkt
;
7712 dp_packet_batch_reset_cutlen(orig_packets_
);
7715 dp_packet_batch_apply_cutlen(packets_
);
7717 packet_count
= dp_packet_batch_size(packets_
);
7718 netdev_pop_header(p
->port
->netdev
, packets_
);
7720 packet_count
- dp_packet_batch_size(packets_
);
7721 if (packets_dropped
) {
7722 COVERAGE_ADD(datapath_drop_tunnel_pop_error
,
7725 if (dp_packet_batch_is_empty(packets_
)) {
7729 struct dp_packet
*packet
;
7730 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
7731 packet
->md
.in_port
.odp_port
= portno
;
7735 dp_netdev_recirculate(pmd
, packets_
);
7739 COVERAGE_ADD(datapath_drop_invalid_tnl_port
,
7740 dp_packet_batch_size(packets_
));
7742 COVERAGE_ADD(datapath_drop_recirc_error
,
7743 dp_packet_batch_size(packets_
));
7747 case OVS_ACTION_ATTR_USERSPACE
:
7748 if (!fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
7749 struct dp_packet_batch
*orig_packets_
= packets_
;
7750 const struct nlattr
*userdata
;
7751 struct dp_packet_batch usr_pkt
;
7752 struct ofpbuf actions
;
7757 userdata
= nl_attr_find_nested(a
, OVS_USERSPACE_ATTR_USERDATA
);
7758 ofpbuf_init(&actions
, 0);
7760 if (packets_
->trunc
) {
7761 if (!should_steal
) {
7762 dp_packet_batch_clone(&usr_pkt
, packets_
);
7763 packets_
= &usr_pkt
;
7765 dp_packet_batch_reset_cutlen(orig_packets_
);
7768 dp_packet_batch_apply_cutlen(packets_
);
7771 struct dp_packet
*packet
;
7772 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
7773 flow_extract(packet
, &flow
);
7774 odp_flow_key_hash(&flow
, sizeof flow
, &ufid
);
7775 dp_execute_userspace_action(pmd
, packet
, should_steal
, &flow
,
7776 &ufid
, &actions
, userdata
);
7780 dp_packet_delete_batch(packets_
, true);
7783 ofpbuf_uninit(&actions
);
7784 fat_rwlock_unlock(&dp
->upcall_rwlock
);
7788 COVERAGE_ADD(datapath_drop_lock_error
,
7789 dp_packet_batch_size(packets_
));
7792 case OVS_ACTION_ATTR_RECIRC
:
7793 if (*depth
< MAX_RECIRC_DEPTH
) {
7794 struct dp_packet_batch recirc_pkts
;
7796 if (!should_steal
) {
7797 dp_packet_batch_clone(&recirc_pkts
, packets_
);
7798 packets_
= &recirc_pkts
;
7801 struct dp_packet
*packet
;
7802 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
7803 packet
->md
.recirc_id
= nl_attr_get_u32(a
);
7807 dp_netdev_recirculate(pmd
, packets_
);
7813 COVERAGE_ADD(datapath_drop_recirc_error
,
7814 dp_packet_batch_size(packets_
));
7815 VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
7818 case OVS_ACTION_ATTR_CT
: {
7819 const struct nlattr
*b
;
7821 bool commit
= false;
7825 const char *helper
= NULL
;
7826 const uint32_t *setmark
= NULL
;
7827 const struct ovs_key_ct_labels
*setlabel
= NULL
;
7828 struct nat_action_info_t nat_action_info
;
7829 struct nat_action_info_t
*nat_action_info_ref
= NULL
;
7830 bool nat_config
= false;
7832 NL_ATTR_FOR_EACH_UNSAFE (b
, left
, nl_attr_get(a
),
7833 nl_attr_get_size(a
)) {
7834 enum ovs_ct_attr sub_type
= nl_attr_type(b
);
7837 case OVS_CT_ATTR_FORCE_COMMIT
:
7840 case OVS_CT_ATTR_COMMIT
:
7843 case OVS_CT_ATTR_ZONE
:
7844 zone
= nl_attr_get_u16(b
);
7846 case OVS_CT_ATTR_HELPER
:
7847 helper
= nl_attr_get_string(b
);
7849 case OVS_CT_ATTR_MARK
:
7850 setmark
= nl_attr_get(b
);
7852 case OVS_CT_ATTR_LABELS
:
7853 setlabel
= nl_attr_get(b
);
7855 case OVS_CT_ATTR_EVENTMASK
:
7856 /* Silently ignored, as userspace datapath does not generate
7857 * netlink events. */
7859 case OVS_CT_ATTR_TIMEOUT
:
7860 if (!str_to_uint(nl_attr_get_string(b
), 10, &tp_id
)) {
7861 VLOG_WARN("Invalid Timeout Policy ID: %s.",
7862 nl_attr_get_string(b
));
7863 tp_id
= DEFAULT_TP_ID
;
7866 case OVS_CT_ATTR_NAT
: {
7867 const struct nlattr
*b_nest
;
7868 unsigned int left_nest
;
7869 bool ip_min_specified
= false;
7870 bool proto_num_min_specified
= false;
7871 bool ip_max_specified
= false;
7872 bool proto_num_max_specified
= false;
7873 memset(&nat_action_info
, 0, sizeof nat_action_info
);
7874 nat_action_info_ref
= &nat_action_info
;
7876 NL_NESTED_FOR_EACH_UNSAFE (b_nest
, left_nest
, b
) {
7877 enum ovs_nat_attr sub_type_nest
= nl_attr_type(b_nest
);
7879 switch (sub_type_nest
) {
7880 case OVS_NAT_ATTR_SRC
:
7881 case OVS_NAT_ATTR_DST
:
7883 nat_action_info
.nat_action
|=
7884 ((sub_type_nest
== OVS_NAT_ATTR_SRC
)
7885 ? NAT_ACTION_SRC
: NAT_ACTION_DST
);
7887 case OVS_NAT_ATTR_IP_MIN
:
7888 memcpy(&nat_action_info
.min_addr
,
7889 nl_attr_get(b_nest
),
7890 nl_attr_get_size(b_nest
));
7891 ip_min_specified
= true;
7893 case OVS_NAT_ATTR_IP_MAX
:
7894 memcpy(&nat_action_info
.max_addr
,
7895 nl_attr_get(b_nest
),
7896 nl_attr_get_size(b_nest
));
7897 ip_max_specified
= true;
7899 case OVS_NAT_ATTR_PROTO_MIN
:
7900 nat_action_info
.min_port
=
7901 nl_attr_get_u16(b_nest
);
7902 proto_num_min_specified
= true;
7904 case OVS_NAT_ATTR_PROTO_MAX
:
7905 nat_action_info
.max_port
=
7906 nl_attr_get_u16(b_nest
);
7907 proto_num_max_specified
= true;
7909 case OVS_NAT_ATTR_PERSISTENT
:
7910 case OVS_NAT_ATTR_PROTO_HASH
:
7911 case OVS_NAT_ATTR_PROTO_RANDOM
:
7913 case OVS_NAT_ATTR_UNSPEC
:
7914 case __OVS_NAT_ATTR_MAX
:
7919 if (ip_min_specified
&& !ip_max_specified
) {
7920 nat_action_info
.max_addr
= nat_action_info
.min_addr
;
7922 if (proto_num_min_specified
&& !proto_num_max_specified
) {
7923 nat_action_info
.max_port
= nat_action_info
.min_port
;
7925 if (proto_num_min_specified
|| proto_num_max_specified
) {
7926 if (nat_action_info
.nat_action
& NAT_ACTION_SRC
) {
7927 nat_action_info
.nat_action
|= NAT_ACTION_SRC_PORT
;
7928 } else if (nat_action_info
.nat_action
& NAT_ACTION_DST
) {
7929 nat_action_info
.nat_action
|= NAT_ACTION_DST_PORT
;
7934 case OVS_CT_ATTR_UNSPEC
:
7935 case __OVS_CT_ATTR_MAX
:
7940 /* We won't be able to function properly in this case, hence
7941 * complain loudly. */
7942 if (nat_config
&& !commit
) {
7943 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
7944 VLOG_WARN_RL(&rl
, "NAT specified without commit.");
7947 conntrack_execute(dp
->conntrack
, packets_
, aux
->flow
->dl_type
, force
,
7948 commit
, zone
, setmark
, setlabel
, aux
->flow
->tp_src
,
7949 aux
->flow
->tp_dst
, helper
, nat_action_info_ref
,
7950 pmd
->ctx
.now
/ 1000, tp_id
);
7954 case OVS_ACTION_ATTR_METER
:
7955 dp_netdev_run_meter(pmd
->dp
, packets_
, nl_attr_get_u32(a
),
7959 case OVS_ACTION_ATTR_PUSH_VLAN
:
7960 case OVS_ACTION_ATTR_POP_VLAN
:
7961 case OVS_ACTION_ATTR_PUSH_MPLS
:
7962 case OVS_ACTION_ATTR_POP_MPLS
:
7963 case OVS_ACTION_ATTR_SET
:
7964 case OVS_ACTION_ATTR_SET_MASKED
:
7965 case OVS_ACTION_ATTR_SAMPLE
:
7966 case OVS_ACTION_ATTR_HASH
:
7967 case OVS_ACTION_ATTR_UNSPEC
:
7968 case OVS_ACTION_ATTR_TRUNC
:
7969 case OVS_ACTION_ATTR_PUSH_ETH
:
7970 case OVS_ACTION_ATTR_POP_ETH
:
7971 case OVS_ACTION_ATTR_CLONE
:
7972 case OVS_ACTION_ATTR_PUSH_NSH
:
7973 case OVS_ACTION_ATTR_POP_NSH
:
7974 case OVS_ACTION_ATTR_CT_CLEAR
:
7975 case OVS_ACTION_ATTR_CHECK_PKT_LEN
:
7976 case OVS_ACTION_ATTR_DROP
:
7977 case __OVS_ACTION_ATTR_MAX
:
7981 dp_packet_delete_batch(packets_
, should_steal
);
7985 dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
7986 struct dp_packet_batch
*packets
,
7987 bool should_steal
, const struct flow
*flow
,
7988 const struct nlattr
*actions
, size_t actions_len
)
7990 struct dp_netdev_execute_aux aux
= { pmd
, flow
};
7992 odp_execute_actions(&aux
, packets
, should_steal
, actions
,
7993 actions_len
, dp_execute_cb
);
7996 struct dp_netdev_ct_dump
{
7997 struct ct_dpif_dump_state up
;
7998 struct conntrack_dump dump
;
7999 struct conntrack
*ct
;
8000 struct dp_netdev
*dp
;
8004 dpif_netdev_ct_dump_start(struct dpif
*dpif
, struct ct_dpif_dump_state
**dump_
,
8005 const uint16_t *pzone
, int *ptot_bkts
)
8007 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8008 struct dp_netdev_ct_dump
*dump
;
8010 dump
= xzalloc(sizeof *dump
);
8012 dump
->ct
= dp
->conntrack
;
8014 conntrack_dump_start(dp
->conntrack
, &dump
->dump
, pzone
, ptot_bkts
);
8022 dpif_netdev_ct_dump_next(struct dpif
*dpif OVS_UNUSED
,
8023 struct ct_dpif_dump_state
*dump_
,
8024 struct ct_dpif_entry
*entry
)
8026 struct dp_netdev_ct_dump
*dump
;
8028 INIT_CONTAINER(dump
, dump_
, up
);
8030 return conntrack_dump_next(&dump
->dump
, entry
);
8034 dpif_netdev_ct_dump_done(struct dpif
*dpif OVS_UNUSED
,
8035 struct ct_dpif_dump_state
*dump_
)
8037 struct dp_netdev_ct_dump
*dump
;
8040 INIT_CONTAINER(dump
, dump_
, up
);
8042 err
= conntrack_dump_done(&dump
->dump
);
8050 dpif_netdev_ct_flush(struct dpif
*dpif
, const uint16_t *zone
,
8051 const struct ct_dpif_tuple
*tuple
)
8053 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8056 return conntrack_flush_tuple(dp
->conntrack
, tuple
, zone
? *zone
: 0);
8058 return conntrack_flush(dp
->conntrack
, zone
);
8062 dpif_netdev_ct_set_maxconns(struct dpif
*dpif
, uint32_t maxconns
)
8064 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8066 return conntrack_set_maxconns(dp
->conntrack
, maxconns
);
8070 dpif_netdev_ct_get_maxconns(struct dpif
*dpif
, uint32_t *maxconns
)
8072 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8074 return conntrack_get_maxconns(dp
->conntrack
, maxconns
);
8078 dpif_netdev_ct_get_nconns(struct dpif
*dpif
, uint32_t *nconns
)
8080 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8082 return conntrack_get_nconns(dp
->conntrack
, nconns
);
8086 dpif_netdev_ct_set_tcp_seq_chk(struct dpif
*dpif
, bool enabled
)
8088 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8090 return conntrack_set_tcp_seq_chk(dp
->conntrack
, enabled
);
8094 dpif_netdev_ct_get_tcp_seq_chk(struct dpif
*dpif
, bool *enabled
)
8096 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8097 *enabled
= conntrack_get_tcp_seq_chk(dp
->conntrack
);
8102 dpif_netdev_ct_set_limits(struct dpif
*dpif OVS_UNUSED
,
8103 const uint32_t *default_limits
,
8104 const struct ovs_list
*zone_limits
)
8107 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8108 if (default_limits
) {
8109 err
= zone_limit_update(dp
->conntrack
, DEFAULT_ZONE
, *default_limits
);
8115 struct ct_dpif_zone_limit
*zone_limit
;
8116 LIST_FOR_EACH (zone_limit
, node
, zone_limits
) {
8117 err
= zone_limit_update(dp
->conntrack
, zone_limit
->zone
,
8127 dpif_netdev_ct_get_limits(struct dpif
*dpif OVS_UNUSED
,
8128 uint32_t *default_limit
,
8129 const struct ovs_list
*zone_limits_request
,
8130 struct ovs_list
*zone_limits_reply
)
8132 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8133 struct conntrack_zone_limit czl
;
8135 czl
= zone_limit_get(dp
->conntrack
, DEFAULT_ZONE
);
8136 if (czl
.zone
== DEFAULT_ZONE
) {
8137 *default_limit
= czl
.limit
;
8142 if (!ovs_list_is_empty(zone_limits_request
)) {
8143 struct ct_dpif_zone_limit
*zone_limit
;
8144 LIST_FOR_EACH (zone_limit
, node
, zone_limits_request
) {
8145 czl
= zone_limit_get(dp
->conntrack
, zone_limit
->zone
);
8146 if (czl
.zone
== zone_limit
->zone
|| czl
.zone
== DEFAULT_ZONE
) {
8147 ct_dpif_push_zone_limit(zone_limits_reply
, zone_limit
->zone
,
8148 czl
.limit
, czl
.count
);
8154 for (int z
= MIN_ZONE
; z
<= MAX_ZONE
; z
++) {
8155 czl
= zone_limit_get(dp
->conntrack
, z
);
8156 if (czl
.zone
== z
) {
8157 ct_dpif_push_zone_limit(zone_limits_reply
, z
, czl
.limit
,
8167 dpif_netdev_ct_del_limits(struct dpif
*dpif OVS_UNUSED
,
8168 const struct ovs_list
*zone_limits
)
8171 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8172 struct ct_dpif_zone_limit
*zone_limit
;
8173 LIST_FOR_EACH (zone_limit
, node
, zone_limits
) {
8174 err
= zone_limit_delete(dp
->conntrack
, zone_limit
->zone
);
8184 dpif_netdev_ct_set_timeout_policy(struct dpif
*dpif
,
8185 const struct ct_dpif_timeout_policy
*dpif_tp
)
8187 struct timeout_policy tp
;
8188 struct dp_netdev
*dp
;
8190 dp
= get_dp_netdev(dpif
);
8191 memcpy(&tp
.policy
, dpif_tp
, sizeof tp
.policy
);
8192 return timeout_policy_update(dp
->conntrack
, &tp
);
8196 dpif_netdev_ct_get_timeout_policy(struct dpif
*dpif
, uint32_t tp_id
,
8197 struct ct_dpif_timeout_policy
*dpif_tp
)
8199 struct timeout_policy
*tp
;
8200 struct dp_netdev
*dp
;
8203 dp
= get_dp_netdev(dpif
);
8204 tp
= timeout_policy_get(dp
->conntrack
, tp_id
);
8208 memcpy(dpif_tp
, &tp
->policy
, sizeof tp
->policy
);
8213 dpif_netdev_ct_del_timeout_policy(struct dpif
*dpif
,
8216 struct dp_netdev
*dp
;
8219 dp
= get_dp_netdev(dpif
);
8220 err
= timeout_policy_delete(dp
->conntrack
, tp_id
);
8225 dpif_netdev_ct_get_timeout_policy_name(struct dpif
*dpif OVS_UNUSED
,
8227 uint16_t dl_type OVS_UNUSED
,
8228 uint8_t nw_proto OVS_UNUSED
,
8229 char **tp_name
, bool *is_generic
)
8231 struct ds ds
= DS_EMPTY_INITIALIZER
;
8233 ds_put_format(&ds
, "%"PRIu32
, tp_id
);
8234 *tp_name
= ds_steal_cstr(&ds
);
8240 dpif_netdev_ipf_set_enabled(struct dpif
*dpif
, bool v6
, bool enable
)
8242 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8243 return ipf_set_enabled(conntrack_ipf_ctx(dp
->conntrack
), v6
, enable
);
8247 dpif_netdev_ipf_set_min_frag(struct dpif
*dpif
, bool v6
, uint32_t min_frag
)
8249 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8250 return ipf_set_min_frag(conntrack_ipf_ctx(dp
->conntrack
), v6
, min_frag
);
8254 dpif_netdev_ipf_set_max_nfrags(struct dpif
*dpif
, uint32_t max_frags
)
8256 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8257 return ipf_set_max_nfrags(conntrack_ipf_ctx(dp
->conntrack
), max_frags
);
8260 /* Adjust this function if 'dpif_ipf_status' and 'ipf_status' were to
8263 dpif_netdev_ipf_get_status(struct dpif
*dpif
,
8264 struct dpif_ipf_status
*dpif_ipf_status
)
8266 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8267 ipf_get_status(conntrack_ipf_ctx(dp
->conntrack
),
8268 (struct ipf_status
*) dpif_ipf_status
);
8273 dpif_netdev_ipf_dump_start(struct dpif
*dpif OVS_UNUSED
,
8274 struct ipf_dump_ctx
**ipf_dump_ctx
)
8276 return ipf_dump_start(ipf_dump_ctx
);
8280 dpif_netdev_ipf_dump_next(struct dpif
*dpif
, void *ipf_dump_ctx
, char **dump
)
8282 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8283 return ipf_dump_next(conntrack_ipf_ctx(dp
->conntrack
), ipf_dump_ctx
,
8288 dpif_netdev_ipf_dump_done(struct dpif
*dpif OVS_UNUSED
, void *ipf_dump_ctx
)
8290 return ipf_dump_done(ipf_dump_ctx
);
8295 dpif_netdev_bond_add(struct dpif
*dpif
, uint32_t bond_id
,
8296 odp_port_t
*slave_map
)
8298 struct tx_bond
*new_tx
= xzalloc(sizeof *new_tx
);
8299 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8300 struct dp_netdev_pmd_thread
*pmd
;
8302 /* Prepare new bond mapping. */
8303 new_tx
->bond_id
= bond_id
;
8304 for (int bucket
= 0; bucket
< BOND_BUCKETS
; bucket
++) {
8305 new_tx
->slave_buckets
[bucket
].slave_id
= slave_map
[bucket
];
8308 ovs_mutex_lock(&dp
->bond_mutex
);
8309 /* Check if bond already existed. */
8310 struct tx_bond
*old_tx
= tx_bond_lookup(&dp
->tx_bonds
, bond_id
);
8312 cmap_replace(&dp
->tx_bonds
, &old_tx
->node
, &new_tx
->node
,
8313 hash_bond_id(bond_id
));
8314 ovsrcu_postpone(free
, old_tx
);
8316 cmap_insert(&dp
->tx_bonds
, &new_tx
->node
, hash_bond_id(bond_id
));
8318 ovs_mutex_unlock(&dp
->bond_mutex
);
8320 /* Update all PMDs with new bond mapping. */
8321 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
8322 dp_netdev_add_bond_tx_to_pmd(pmd
, new_tx
, true);
8328 dpif_netdev_bond_del(struct dpif
*dpif
, uint32_t bond_id
)
8330 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8331 struct dp_netdev_pmd_thread
*pmd
;
8334 ovs_mutex_lock(&dp
->bond_mutex
);
8335 /* Check if bond existed. */
8336 tx
= tx_bond_lookup(&dp
->tx_bonds
, bond_id
);
8338 cmap_remove(&dp
->tx_bonds
, &tx
->node
, hash_bond_id(bond_id
));
8339 ovsrcu_postpone(free
, tx
);
8341 /* Bond is not present. */
8342 ovs_mutex_unlock(&dp
->bond_mutex
);
8345 ovs_mutex_unlock(&dp
->bond_mutex
);
8347 /* Remove the bond map in all pmds. */
8348 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
8349 dp_netdev_del_bond_tx_from_pmd(pmd
, bond_id
);
8355 dpif_netdev_bond_stats_get(struct dpif
*dpif
, uint32_t bond_id
,
8358 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
8359 struct dp_netdev_pmd_thread
*pmd
;
8361 if (!tx_bond_lookup(&dp
->tx_bonds
, bond_id
)) {
8365 /* Search the bond in all PMDs. */
8366 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
8367 struct tx_bond
*pmd_bond_entry
8368 = tx_bond_lookup(&pmd
->tx_bonds
, bond_id
);
8370 if (!pmd_bond_entry
) {
8374 /* Read bond stats. */
8375 for (int i
= 0; i
< BOND_BUCKETS
; i
++) {
8376 uint64_t pmd_n_bytes
;
8378 atomic_read_relaxed(&pmd_bond_entry
->slave_buckets
[i
].n_bytes
,
8380 n_bytes
[i
] += pmd_n_bytes
;
8386 const struct dpif_class dpif_netdev_class
= {
8388 true, /* cleanup_required */
8390 dpif_netdev_enumerate
,
8391 dpif_netdev_port_open_type
,
8394 dpif_netdev_destroy
,
8397 dpif_netdev_get_stats
,
8398 NULL
, /* set_features */
8399 dpif_netdev_port_add
,
8400 dpif_netdev_port_del
,
8401 dpif_netdev_port_set_config
,
8402 dpif_netdev_port_query_by_number
,
8403 dpif_netdev_port_query_by_name
,
8404 NULL
, /* port_get_pid */
8405 dpif_netdev_port_dump_start
,
8406 dpif_netdev_port_dump_next
,
8407 dpif_netdev_port_dump_done
,
8408 dpif_netdev_port_poll
,
8409 dpif_netdev_port_poll_wait
,
8410 dpif_netdev_flow_flush
,
8411 dpif_netdev_flow_dump_create
,
8412 dpif_netdev_flow_dump_destroy
,
8413 dpif_netdev_flow_dump_thread_create
,
8414 dpif_netdev_flow_dump_thread_destroy
,
8415 dpif_netdev_flow_dump_next
,
8416 dpif_netdev_operate
,
8417 NULL
, /* recv_set */
8418 NULL
, /* handlers_set */
8419 dpif_netdev_set_config
,
8420 dpif_netdev_queue_to_priority
,
8422 NULL
, /* recv_wait */
8423 NULL
, /* recv_purge */
8424 dpif_netdev_register_dp_purge_cb
,
8425 dpif_netdev_register_upcall_cb
,
8426 dpif_netdev_enable_upcall
,
8427 dpif_netdev_disable_upcall
,
8428 dpif_netdev_get_datapath_version
,
8429 dpif_netdev_ct_dump_start
,
8430 dpif_netdev_ct_dump_next
,
8431 dpif_netdev_ct_dump_done
,
8432 dpif_netdev_ct_flush
,
8433 dpif_netdev_ct_set_maxconns
,
8434 dpif_netdev_ct_get_maxconns
,
8435 dpif_netdev_ct_get_nconns
,
8436 dpif_netdev_ct_set_tcp_seq_chk
,
8437 dpif_netdev_ct_get_tcp_seq_chk
,
8438 dpif_netdev_ct_set_limits
,
8439 dpif_netdev_ct_get_limits
,
8440 dpif_netdev_ct_del_limits
,
8441 dpif_netdev_ct_set_timeout_policy
,
8442 dpif_netdev_ct_get_timeout_policy
,
8443 dpif_netdev_ct_del_timeout_policy
,
8444 NULL
, /* ct_timeout_policy_dump_start */
8445 NULL
, /* ct_timeout_policy_dump_next */
8446 NULL
, /* ct_timeout_policy_dump_done */
8447 dpif_netdev_ct_get_timeout_policy_name
,
8448 dpif_netdev_ipf_set_enabled
,
8449 dpif_netdev_ipf_set_min_frag
,
8450 dpif_netdev_ipf_set_max_nfrags
,
8451 dpif_netdev_ipf_get_status
,
8452 dpif_netdev_ipf_dump_start
,
8453 dpif_netdev_ipf_dump_next
,
8454 dpif_netdev_ipf_dump_done
,
8455 dpif_netdev_meter_get_features
,
8456 dpif_netdev_meter_set
,
8457 dpif_netdev_meter_get
,
8458 dpif_netdev_meter_del
,
8459 dpif_netdev_bond_add
,
8460 dpif_netdev_bond_del
,
8461 dpif_netdev_bond_stats_get
,
8465 dpif_dummy_change_port_number(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
8466 const char *argv
[], void *aux OVS_UNUSED
)
8468 struct dp_netdev_port
*port
;
8469 struct dp_netdev
*dp
;
8472 ovs_mutex_lock(&dp_netdev_mutex
);
8473 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
8474 if (!dp
|| !dpif_netdev_class_is_dummy(dp
->class)) {
8475 ovs_mutex_unlock(&dp_netdev_mutex
);
8476 unixctl_command_reply_error(conn
, "unknown datapath or not a dummy");
8479 ovs_refcount_ref(&dp
->ref_cnt
);
8480 ovs_mutex_unlock(&dp_netdev_mutex
);
8482 ovs_mutex_lock(&dp
->port_mutex
);
8483 if (get_port_by_name(dp
, argv
[2], &port
)) {
8484 unixctl_command_reply_error(conn
, "unknown port");
8488 port_no
= u32_to_odp(atoi(argv
[3]));
8489 if (!port_no
|| port_no
== ODPP_NONE
) {
8490 unixctl_command_reply_error(conn
, "bad port number");
8493 if (dp_netdev_lookup_port(dp
, port_no
)) {
8494 unixctl_command_reply_error(conn
, "port number already in use");
8499 hmap_remove(&dp
->ports
, &port
->node
);
8500 reconfigure_datapath(dp
);
8502 /* Reinsert with new port number. */
8503 port
->port_no
= port_no
;
8504 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
8505 reconfigure_datapath(dp
);
8507 seq_change(dp
->port_seq
);
8508 unixctl_command_reply(conn
, NULL
);
8511 ovs_mutex_unlock(&dp
->port_mutex
);
8512 dp_netdev_unref(dp
);
8516 dpif_dummy_register__(const char *type
)
8518 struct dpif_class
*class;
8520 class = xmalloc(sizeof *class);
8521 *class = dpif_netdev_class
;
8522 class->type
= xstrdup(type
);
8523 dp_register_provider(class);
8527 dpif_dummy_override(const char *type
)
8532 * Ignore EAFNOSUPPORT to allow --enable-dummy=system with
8533 * a userland-only build. It's useful for testsuite.
8535 error
= dp_unregister_provider(type
);
8536 if (error
== 0 || error
== EAFNOSUPPORT
) {
8537 dpif_dummy_register__(type
);
8542 dpif_dummy_register(enum dummy_level level
)
8544 if (level
== DUMMY_OVERRIDE_ALL
) {
8549 dp_enumerate_types(&types
);
8550 SSET_FOR_EACH (type
, &types
) {
8551 dpif_dummy_override(type
);
8553 sset_destroy(&types
);
8554 } else if (level
== DUMMY_OVERRIDE_SYSTEM
) {
8555 dpif_dummy_override("system");
8558 dpif_dummy_register__("dummy");
8560 unixctl_command_register("dpif-dummy/change-port-number",
8561 "dp port new-number",
8562 3, 3, dpif_dummy_change_port_number
, NULL
);
8565 /* Datapath Classifier. */
8568 dpcls_subtable_destroy_cb(struct dpcls_subtable
*subtable
)
8570 cmap_destroy(&subtable
->rules
);
8571 ovsrcu_postpone(free
, subtable
->mf_masks
);
8572 ovsrcu_postpone(free
, subtable
);
8575 /* Initializes 'cls' as a classifier that initially contains no classification
8578 dpcls_init(struct dpcls
*cls
)
8580 cmap_init(&cls
->subtables_map
);
8581 pvector_init(&cls
->subtables
);
8585 dpcls_destroy_subtable(struct dpcls
*cls
, struct dpcls_subtable
*subtable
)
8587 VLOG_DBG("Destroying subtable %p for in_port %d", subtable
, cls
->in_port
);
8588 pvector_remove(&cls
->subtables
, subtable
);
8589 cmap_remove(&cls
->subtables_map
, &subtable
->cmap_node
,
8590 subtable
->mask
.hash
);
8591 ovsrcu_postpone(dpcls_subtable_destroy_cb
, subtable
);
8594 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
8595 * caller's responsibility.
8596 * May only be called after all the readers have been terminated. */
8598 dpcls_destroy(struct dpcls
*cls
)
8601 struct dpcls_subtable
*subtable
;
8603 CMAP_FOR_EACH (subtable
, cmap_node
, &cls
->subtables_map
) {
8604 ovs_assert(cmap_count(&subtable
->rules
) == 0);
8605 dpcls_destroy_subtable(cls
, subtable
);
8607 cmap_destroy(&cls
->subtables_map
);
8608 pvector_destroy(&cls
->subtables
);
8612 static struct dpcls_subtable
*
8613 dpcls_create_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
8615 struct dpcls_subtable
*subtable
;
8617 /* Need to add one. */
8618 subtable
= xmalloc(sizeof *subtable
8619 - sizeof subtable
->mask
.mf
+ mask
->len
);
8620 cmap_init(&subtable
->rules
);
8621 subtable
->hit_cnt
= 0;
8622 netdev_flow_key_clone(&subtable
->mask
, mask
);
8624 /* The count of bits in the mask defines the space required for masks.
8625 * Then call gen_masks() to create the appropriate masks, avoiding the cost
8626 * of doing runtime calculations. */
8627 uint32_t unit0
= count_1bits(mask
->mf
.map
.bits
[0]);
8628 uint32_t unit1
= count_1bits(mask
->mf
.map
.bits
[1]);
8629 subtable
->mf_bits_set_unit0
= unit0
;
8630 subtable
->mf_bits_set_unit1
= unit1
;
8631 subtable
->mf_masks
= xmalloc(sizeof(uint64_t) * (unit0
+ unit1
));
8632 netdev_flow_key_gen_masks(mask
, subtable
->mf_masks
, unit0
, unit1
);
8634 /* Get the preferred subtable search function for this (u0,u1) subtable.
8635 * The function is guaranteed to always return a valid implementation, and
8636 * possibly an ISA optimized, and/or specialized implementation.
8638 subtable
->lookup_func
= dpcls_subtable_get_best_impl(unit0
, unit1
);
8640 cmap_insert(&cls
->subtables_map
, &subtable
->cmap_node
, mask
->hash
);
8641 /* Add the new subtable at the end of the pvector (with no hits yet) */
8642 pvector_insert(&cls
->subtables
, subtable
, 0);
8643 VLOG_DBG("Creating %"PRIuSIZE
". subtable %p for in_port %d",
8644 cmap_count(&cls
->subtables_map
), subtable
, cls
->in_port
);
8645 pvector_publish(&cls
->subtables
);
8650 static inline struct dpcls_subtable
*
8651 dpcls_find_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
8653 struct dpcls_subtable
*subtable
;
8655 CMAP_FOR_EACH_WITH_HASH (subtable
, cmap_node
, mask
->hash
,
8656 &cls
->subtables_map
) {
8657 if (netdev_flow_key_equal(&subtable
->mask
, mask
)) {
8661 return dpcls_create_subtable(cls
, mask
);
8664 /* Checks for the best available implementation for each subtable lookup
8665 * function, and assigns it as the lookup function pointer for each subtable.
8666 * Returns the number of subtables that have changed lookup implementation.
8669 dpcls_subtable_lookup_reprobe(struct dpcls
*cls
)
8671 struct pvector
*pvec
= &cls
->subtables
;
8672 uint32_t subtables_changed
= 0;
8673 struct dpcls_subtable
*subtable
= NULL
;
8675 PVECTOR_FOR_EACH (subtable
, pvec
) {
8676 uint32_t u0_bits
= subtable
->mf_bits_set_unit0
;
8677 uint32_t u1_bits
= subtable
->mf_bits_set_unit1
;
8678 void *old_func
= subtable
->lookup_func
;
8679 subtable
->lookup_func
= dpcls_subtable_get_best_impl(u0_bits
, u1_bits
);
8680 subtables_changed
+= (old_func
!= subtable
->lookup_func
);
8682 pvector_publish(pvec
);
8684 return subtables_changed
;
8687 /* Periodically sort the dpcls subtable vectors according to hit counts */
8689 dpcls_sort_subtable_vector(struct dpcls
*cls
)
8691 struct pvector
*pvec
= &cls
->subtables
;
8692 struct dpcls_subtable
*subtable
;
8694 PVECTOR_FOR_EACH (subtable
, pvec
) {
8695 pvector_change_priority(pvec
, subtable
, subtable
->hit_cnt
);
8696 subtable
->hit_cnt
= 0;
8698 pvector_publish(pvec
);
8702 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread
*pmd
,
8703 struct polled_queue
*poll_list
, int poll_cnt
)
8706 uint64_t tot_idle
= 0, tot_proc
= 0;
8707 unsigned int pmd_load
= 0;
8709 if (pmd
->ctx
.now
> pmd
->rxq_next_cycle_store
) {
8711 struct pmd_auto_lb
*pmd_alb
= &pmd
->dp
->pmd_alb
;
8712 if (pmd_alb
->is_enabled
&& !pmd
->isolated
8713 && (pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_IDLE
] >=
8714 pmd
->prev_stats
[PMD_CYCLES_ITER_IDLE
])
8715 && (pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_BUSY
] >=
8716 pmd
->prev_stats
[PMD_CYCLES_ITER_BUSY
]))
8718 tot_idle
= pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_IDLE
] -
8719 pmd
->prev_stats
[PMD_CYCLES_ITER_IDLE
];
8720 tot_proc
= pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_BUSY
] -
8721 pmd
->prev_stats
[PMD_CYCLES_ITER_BUSY
];
8724 pmd_load
= ((tot_proc
* 100) / (tot_idle
+ tot_proc
));
8727 if (pmd_load
>= ALB_PMD_LOAD_THRESHOLD
) {
8728 atomic_count_inc(&pmd
->pmd_overloaded
);
8730 atomic_count_set(&pmd
->pmd_overloaded
, 0);
8734 pmd
->prev_stats
[PMD_CYCLES_ITER_IDLE
] =
8735 pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_IDLE
];
8736 pmd
->prev_stats
[PMD_CYCLES_ITER_BUSY
] =
8737 pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_BUSY
];
8739 /* Get the cycles that were used to process each queue and store. */
8740 for (unsigned i
= 0; i
< poll_cnt
; i
++) {
8741 uint64_t rxq_cyc_curr
= dp_netdev_rxq_get_cycles(poll_list
[i
].rxq
,
8742 RXQ_CYCLES_PROC_CURR
);
8743 dp_netdev_rxq_set_intrvl_cycles(poll_list
[i
].rxq
, rxq_cyc_curr
);
8744 dp_netdev_rxq_set_cycles(poll_list
[i
].rxq
, RXQ_CYCLES_PROC_CURR
,
8747 curr_tsc
= cycles_counter_update(&pmd
->perf_stats
);
8748 if (pmd
->intrvl_tsc_prev
) {
8749 /* There is a prev timestamp, store a new intrvl cycle count. */
8750 atomic_store_relaxed(&pmd
->intrvl_cycles
,
8751 curr_tsc
- pmd
->intrvl_tsc_prev
);
8753 pmd
->intrvl_tsc_prev
= curr_tsc
;
8754 /* Start new measuring interval */
8755 pmd
->rxq_next_cycle_store
= pmd
->ctx
.now
+ PMD_RXQ_INTERVAL_LEN
;
8758 if (pmd
->ctx
.now
> pmd
->next_optimization
) {
8759 /* Try to obtain the flow lock to block out revalidator threads.
8760 * If not possible, just try next time. */
8761 if (!ovs_mutex_trylock(&pmd
->flow_mutex
)) {
8762 /* Optimize each classifier */
8763 CMAP_FOR_EACH (cls
, node
, &pmd
->classifiers
) {
8764 dpcls_sort_subtable_vector(cls
);
8766 ovs_mutex_unlock(&pmd
->flow_mutex
);
8767 /* Start new measuring interval */
8768 pmd
->next_optimization
= pmd
->ctx
.now
8769 + DPCLS_OPTIMIZATION_INTERVAL
;
8774 /* Insert 'rule' into 'cls'. */
8776 dpcls_insert(struct dpcls
*cls
, struct dpcls_rule
*rule
,
8777 const struct netdev_flow_key
*mask
)
8779 struct dpcls_subtable
*subtable
= dpcls_find_subtable(cls
, mask
);
8781 /* Refer to subtable's mask, also for later removal. */
8782 rule
->mask
= &subtable
->mask
;
8783 cmap_insert(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
);
8786 /* Removes 'rule' from 'cls', also destructing the 'rule'. */
8788 dpcls_remove(struct dpcls
*cls
, struct dpcls_rule
*rule
)
8790 struct dpcls_subtable
*subtable
;
8792 ovs_assert(rule
->mask
);
8794 /* Get subtable from reference in rule->mask. */
8795 INIT_CONTAINER(subtable
, rule
->mask
, mask
);
8796 if (cmap_remove(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
)
8798 /* Delete empty subtable. */
8799 dpcls_destroy_subtable(cls
, subtable
);
8800 pvector_publish(&cls
->subtables
);
8804 /* Inner loop for mask generation of a unit, see netdev_flow_key_gen_masks. */
8806 netdev_flow_key_gen_mask_unit(uint64_t iter
,
8807 const uint64_t count
,
8811 for (i
= 0; i
< count
; i
++) {
8812 uint64_t lowest_bit
= (iter
& -iter
);
8813 iter
&= ~lowest_bit
;
8814 mf_masks
[i
] = (lowest_bit
- 1);
8816 /* Checks that count has covered all bits in the iter bitmap. */
8817 ovs_assert(iter
== 0);
8820 /* Generate a mask for each block in the miniflow, based on the bits set. This
8821 * allows easily masking packets with the generated array here, without
8822 * calculations. This replaces runtime-calculating the masks.
8823 * @param key The table to generate the mf_masks for
8824 * @param mf_masks Pointer to a u64 array of at least *mf_bits* in size
8825 * @param mf_bits_total Number of bits set in the whole miniflow (both units)
8826 * @param mf_bits_unit0 Number of bits set in unit0 of the miniflow
8829 netdev_flow_key_gen_masks(const struct netdev_flow_key
*tbl
,
8831 const uint32_t mf_bits_u0
,
8832 const uint32_t mf_bits_u1
)
8834 uint64_t iter_u0
= tbl
->mf
.map
.bits
[0];
8835 uint64_t iter_u1
= tbl
->mf
.map
.bits
[1];
8837 netdev_flow_key_gen_mask_unit(iter_u0
, mf_bits_u0
, &mf_masks
[0]);
8838 netdev_flow_key_gen_mask_unit(iter_u1
, mf_bits_u1
, &mf_masks
[mf_bits_u0
]);
8841 /* Returns true if 'target' satisfies 'key' in 'mask', that is, if each 1-bit
8842 * in 'mask' the values in 'key' and 'target' are the same. */
8844 dpcls_rule_matches_key(const struct dpcls_rule
*rule
,
8845 const struct netdev_flow_key
*target
)
8847 const uint64_t *keyp
= miniflow_get_values(&rule
->flow
.mf
);
8848 const uint64_t *maskp
= miniflow_get_values(&rule
->mask
->mf
);
8851 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, target
, rule
->flow
.mf
.map
) {
8852 if (OVS_UNLIKELY((value
& *maskp
++) != *keyp
++)) {
8859 /* For each miniflow in 'keys' performs a classifier lookup writing the result
8860 * into the corresponding slot in 'rules'. If a particular entry in 'keys' is
8861 * NULL it is skipped.
8863 * This function is optimized for use in the userspace datapath and therefore
8864 * does not implement a lot of features available in the standard
8865 * classifier_lookup() function. Specifically, it does not implement
8866 * priorities, instead returning any rule which matches the flow.
8868 * Returns true if all miniflows found a corresponding rule. */
8870 dpcls_lookup(struct dpcls
*cls
, const struct netdev_flow_key
*keys
[],
8871 struct dpcls_rule
**rules
, const size_t cnt
,
8874 /* The received 'cnt' miniflows are the search-keys that will be processed
8875 * to find a matching entry into the available subtables.
8876 * The number of bits in map_type is equal to NETDEV_MAX_BURST. */
8877 #define MAP_BITS (sizeof(uint32_t) * CHAR_BIT)
8878 BUILD_ASSERT_DECL(MAP_BITS
>= NETDEV_MAX_BURST
);
8880 struct dpcls_subtable
*subtable
;
8881 uint32_t keys_map
= TYPE_MAXIMUM(uint32_t); /* Set all bits. */
8883 if (cnt
!= MAP_BITS
) {
8884 keys_map
>>= MAP_BITS
- cnt
; /* Clear extra bits. */
8886 memset(rules
, 0, cnt
* sizeof *rules
);
8888 int lookups_match
= 0, subtable_pos
= 1;
8891 /* The Datapath classifier - aka dpcls - is composed of subtables.
8892 * Subtables are dynamically created as needed when new rules are inserted.
8893 * Each subtable collects rules with matches on a specific subset of packet
8894 * fields as defined by the subtable's mask. We proceed to process every
8895 * search-key against each subtable, but when a match is found for a
8896 * search-key, the search for that key can stop because the rules are
8897 * non-overlapping. */
8898 PVECTOR_FOR_EACH (subtable
, &cls
->subtables
) {
8899 /* Call the subtable specific lookup function. */
8900 found_map
= subtable
->lookup_func(subtable
, keys_map
, keys
, rules
);
8902 /* Count the number of subtables searched for this packet match. This
8903 * estimates the "spread" of subtables looked at per matched packet. */
8904 uint32_t pkts_matched
= count_1bits(found_map
);
8905 lookups_match
+= pkts_matched
* subtable_pos
;
8907 /* Clear the found rules, and return early if all packets are found. */
8908 keys_map
&= ~found_map
;
8910 if (num_lookups_p
) {
8911 *num_lookups_p
= lookups_match
;
8918 if (num_lookups_p
) {
8919 *num_lookups_p
= lookups_match
;