2 * Copyright (c) 2009-2014, 2016-2018 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "dpif-netdev.h"
25 #include <sys/types.h>
26 #include <netinet/in.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
37 #include "conntrack.h"
41 #include "dp-packet.h"
43 #include "dpif-netdev-perf.h"
44 #include "dpif-provider.h"
46 #include "fat-rwlock.h"
53 #include "netdev-provider.h"
54 #include "netdev-vport.h"
56 #include "odp-execute.h"
58 #include "openvswitch/dynamic-string.h"
59 #include "openvswitch/list.h"
60 #include "openvswitch/match.h"
61 #include "openvswitch/ofp-parse.h"
62 #include "openvswitch/ofp-print.h"
63 #include "openvswitch/ofpbuf.h"
64 #include "openvswitch/shash.h"
65 #include "openvswitch/vlog.h"
69 #include "openvswitch/poll-loop.h"
76 #include "tnl-neigh-cache.h"
77 #include "tnl-ports.h"
82 VLOG_DEFINE_THIS_MODULE(dpif_netdev
);
84 /* Auto Load Balancing Defaults */
85 #define ALB_ACCEPTABLE_IMPROVEMENT 25
86 #define ALB_PMD_LOAD_THRESHOLD 95
87 #define ALB_PMD_REBALANCE_POLL_INTERVAL 1 /* 1 Min */
88 #define MIN_TO_MSEC 60000
90 #define FLOW_DUMP_MAX_BATCH 50
91 /* Use per thread recirc_depth to prevent recirculation loop. */
92 #define MAX_RECIRC_DEPTH 6
93 DEFINE_STATIC_PER_THREAD_DATA(uint32_t, recirc_depth
, 0)
95 /* Use instant packet send by default. */
96 #define DEFAULT_TX_FLUSH_INTERVAL 0
98 /* Configuration parameters. */
99 enum { MAX_FLOWS
= 65536 }; /* Maximum number of flows in flow table. */
100 enum { MAX_METERS
= 65536 }; /* Maximum number of meters. */
101 enum { MAX_BANDS
= 8 }; /* Maximum number of bands / meter. */
102 enum { N_METER_LOCKS
= 64 }; /* Maximum number of meters. */
104 /* Protects against changes to 'dp_netdevs'. */
105 static struct ovs_mutex dp_netdev_mutex
= OVS_MUTEX_INITIALIZER
;
107 /* Contains all 'struct dp_netdev's. */
108 static struct shash dp_netdevs
OVS_GUARDED_BY(dp_netdev_mutex
)
109 = SHASH_INITIALIZER(&dp_netdevs
);
111 static struct vlog_rate_limit upcall_rl
= VLOG_RATE_LIMIT_INIT(600, 600);
113 #define DP_NETDEV_CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \
114 | CS_INVALID | CS_REPLY_DIR | CS_TRACKED \
115 | CS_SRC_NAT | CS_DST_NAT)
116 #define DP_NETDEV_CS_UNSUPPORTED_MASK (~(uint32_t)DP_NETDEV_CS_SUPPORTED_MASK)
118 static struct odp_support dp_netdev_support
= {
119 .max_vlan_headers
= SIZE_MAX
,
120 .max_mpls_depth
= SIZE_MAX
,
126 .ct_state_nat
= true,
127 .ct_orig_tuple
= true,
128 .ct_orig_tuple6
= true,
131 /* Stores a miniflow with inline values */
133 struct netdev_flow_key
{
134 uint32_t hash
; /* Hash function differs for different users. */
135 uint32_t len
; /* Length of the following miniflow (incl. map). */
137 uint64_t buf
[FLOW_MAX_PACKET_U64S
];
140 /* EMC cache and SMC cache compose the datapath flow cache (DFC)
142 * Exact match cache for frequently used flows
144 * The cache uses a 32-bit hash of the packet (which can be the RSS hash) to
145 * search its entries for a miniflow that matches exactly the miniflow of the
146 * packet. It stores the 'dpcls_rule' (rule) that matches the miniflow.
148 * A cache entry holds a reference to its 'dp_netdev_flow'.
150 * A miniflow with a given hash can be in one of EM_FLOW_HASH_SEGS different
151 * entries. The 32-bit hash is split into EM_FLOW_HASH_SEGS values (each of
152 * them is EM_FLOW_HASH_SHIFT bits wide and the remainder is thrown away). Each
153 * value is the index of a cache entry where the miniflow could be.
156 * Signature match cache (SMC)
158 * This cache stores a 16-bit signature for each flow without storing keys, and
159 * stores the corresponding 16-bit flow_table index to the 'dp_netdev_flow'.
160 * Each flow thus occupies 32bit which is much more memory efficient than EMC.
161 * SMC uses a set-associative design that each bucket contains
162 * SMC_ENTRY_PER_BUCKET number of entries.
163 * Since 16-bit flow_table index is used, if there are more than 2^16
164 * dp_netdev_flow, SMC will miss them that cannot be indexed by a 16-bit value.
170 * Each pmd_thread has its own private exact match cache.
171 * If dp_netdev_input is not called from a pmd thread, a mutex is used.
174 #define EM_FLOW_HASH_SHIFT 13
175 #define EM_FLOW_HASH_ENTRIES (1u << EM_FLOW_HASH_SHIFT)
176 #define EM_FLOW_HASH_MASK (EM_FLOW_HASH_ENTRIES - 1)
177 #define EM_FLOW_HASH_SEGS 2
179 /* SMC uses a set-associative design. A bucket contains a set of entries that
180 * a flow item can occupy. For now, it uses one hash function rather than two
181 * as for the EMC design. */
182 #define SMC_ENTRY_PER_BUCKET 4
183 #define SMC_ENTRIES (1u << 20)
184 #define SMC_BUCKET_CNT (SMC_ENTRIES / SMC_ENTRY_PER_BUCKET)
185 #define SMC_MASK (SMC_BUCKET_CNT - 1)
187 /* Default EMC insert probability is 1 / DEFAULT_EM_FLOW_INSERT_INV_PROB */
188 #define DEFAULT_EM_FLOW_INSERT_INV_PROB 100
189 #define DEFAULT_EM_FLOW_INSERT_MIN (UINT32_MAX / \
190 DEFAULT_EM_FLOW_INSERT_INV_PROB)
193 struct dp_netdev_flow
*flow
;
194 struct netdev_flow_key key
; /* key.hash used for emc hash value. */
198 struct emc_entry entries
[EM_FLOW_HASH_ENTRIES
];
199 int sweep_idx
; /* For emc_cache_slow_sweep(). */
203 uint16_t sig
[SMC_ENTRY_PER_BUCKET
];
204 uint16_t flow_idx
[SMC_ENTRY_PER_BUCKET
];
207 /* Signature match cache, differentiate from EMC cache */
209 struct smc_bucket buckets
[SMC_BUCKET_CNT
];
213 struct emc_cache emc_cache
;
214 struct smc_cache smc_cache
;
217 /* Iterate in the exact match cache through every entry that might contain a
218 * miniflow with hash 'HASH'. */
219 #define EMC_FOR_EACH_POS_WITH_HASH(EMC, CURRENT_ENTRY, HASH) \
220 for (uint32_t i__ = 0, srch_hash__ = (HASH); \
221 (CURRENT_ENTRY) = &(EMC)->entries[srch_hash__ & EM_FLOW_HASH_MASK], \
222 i__ < EM_FLOW_HASH_SEGS; \
223 i__++, srch_hash__ >>= EM_FLOW_HASH_SHIFT)
225 /* Simple non-wildcarding single-priority classifier. */
227 /* Time in microseconds between successive optimizations of the dpcls
229 #define DPCLS_OPTIMIZATION_INTERVAL 1000000LL
231 /* Time in microseconds of the interval in which rxq processing cycles used
232 * in rxq to pmd assignments is measured and stored. */
233 #define PMD_RXQ_INTERVAL_LEN 10000000LL
235 /* Number of intervals for which cycles are stored
236 * and used during rxq to pmd assignment. */
237 #define PMD_RXQ_INTERVAL_MAX 6
240 struct cmap_node node
; /* Within dp_netdev_pmd_thread.classifiers */
242 struct cmap subtables_map
;
243 struct pvector subtables
;
246 /* A rule to be inserted to the classifier. */
248 struct cmap_node cmap_node
; /* Within struct dpcls_subtable 'rules'. */
249 struct netdev_flow_key
*mask
; /* Subtable's mask. */
250 struct netdev_flow_key flow
; /* Matching key. */
251 /* 'flow' must be the last field, additional space is allocated here. */
254 /* Data structure to keep packet order till fastpath processing. */
255 struct dp_packet_flow_map
{
256 struct dp_packet
*packet
;
257 struct dp_netdev_flow
*flow
;
261 static void dpcls_init(struct dpcls
*);
262 static void dpcls_destroy(struct dpcls
*);
263 static void dpcls_sort_subtable_vector(struct dpcls
*);
264 static void dpcls_insert(struct dpcls
*, struct dpcls_rule
*,
265 const struct netdev_flow_key
*mask
);
266 static void dpcls_remove(struct dpcls
*, struct dpcls_rule
*);
267 static bool dpcls_lookup(struct dpcls
*cls
,
268 const struct netdev_flow_key
*keys
[],
269 struct dpcls_rule
**rules
, size_t cnt
,
271 static bool dpcls_rule_matches_key(const struct dpcls_rule
*rule
,
272 const struct netdev_flow_key
*target
);
273 /* Set of supported meter flags */
274 #define DP_SUPPORTED_METER_FLAGS_MASK \
275 (OFPMF13_STATS | OFPMF13_PKTPS | OFPMF13_KBPS | OFPMF13_BURST)
277 /* Set of supported meter band types */
278 #define DP_SUPPORTED_METER_BAND_TYPES \
279 ( 1 << OFPMBT13_DROP )
281 struct dp_meter_band
{
282 struct ofputil_meter_band up
; /* type, prec_level, pad, rate, burst_size */
283 uint32_t bucket
; /* In 1/1000 packets (for PKTPS), or in bits (for KBPS) */
284 uint64_t packet_count
;
291 uint32_t max_delta_t
;
293 uint64_t packet_count
;
295 struct dp_meter_band bands
[];
299 bool auto_lb_requested
; /* Auto load balancing requested by user. */
300 bool is_enabled
; /* Current status of Auto load balancing. */
301 uint64_t rebalance_intvl
;
302 uint64_t rebalance_poll_timer
;
305 /* Datapath based on the network device interface from netdev.h.
311 * Some members, marked 'const', are immutable. Accessing other members
312 * requires synchronization, as noted in more detail below.
314 * Acquisition order is, from outermost to innermost:
316 * dp_netdev_mutex (global)
321 const struct dpif_class
*const class;
322 const char *const name
;
324 struct ovs_refcount ref_cnt
;
325 atomic_flag destroyed
;
329 * Any lookup into 'ports' or any access to the dp_netdev_ports found
330 * through 'ports' requires taking 'port_mutex'. */
331 struct ovs_mutex port_mutex
;
333 struct seq
*port_seq
; /* Incremented whenever a port changes. */
335 /* The time that a packet can wait in output batch for sending. */
336 atomic_uint32_t tx_flush_interval
;
339 struct ovs_mutex meter_locks
[N_METER_LOCKS
];
340 struct dp_meter
*meters
[MAX_METERS
]; /* Meter bands. */
342 /* Probability of EMC insertions is a factor of 'emc_insert_min'.*/
343 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
) atomic_uint32_t emc_insert_min
;
344 /* Enable collection of PMD performance metrics. */
345 atomic_bool pmd_perf_metrics
;
346 /* Enable the SMC cache from ovsdb config */
347 atomic_bool smc_enable_db
;
349 /* Protects access to ofproto-dpif-upcall interface during revalidator
350 * thread synchronization. */
351 struct fat_rwlock upcall_rwlock
;
352 upcall_callback
*upcall_cb
; /* Callback function for executing upcalls. */
355 /* Callback function for notifying the purging of dp flows (during
356 * reseting pmd deletion). */
357 dp_purge_callback
*dp_purge_cb
;
360 /* Stores all 'struct dp_netdev_pmd_thread's. */
361 struct cmap poll_threads
;
362 /* id pool for per thread static_tx_qid. */
363 struct id_pool
*tx_qid_pool
;
364 struct ovs_mutex tx_qid_pool_mutex
;
365 /* Use measured cycles for rxq to pmd assignment. */
366 bool pmd_rxq_assign_cyc
;
368 /* Protects the access of the 'struct dp_netdev_pmd_thread'
369 * instance for non-pmd thread. */
370 struct ovs_mutex non_pmd_mutex
;
372 /* Each pmd thread will store its pointer to
373 * 'struct dp_netdev_pmd_thread' in 'per_pmd_key'. */
374 ovsthread_key_t per_pmd_key
;
376 struct seq
*reconfigure_seq
;
377 uint64_t last_reconfigure_seq
;
379 /* Cpu mask for pin of pmd threads. */
382 uint64_t last_tnl_conf_seq
;
384 struct conntrack conntrack
;
385 struct pmd_auto_lb pmd_alb
;
388 static void meter_lock(const struct dp_netdev
*dp
, uint32_t meter_id
)
389 OVS_ACQUIRES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
391 ovs_mutex_lock(&dp
->meter_locks
[meter_id
% N_METER_LOCKS
]);
394 static void meter_unlock(const struct dp_netdev
*dp
, uint32_t meter_id
)
395 OVS_RELEASES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
397 ovs_mutex_unlock(&dp
->meter_locks
[meter_id
% N_METER_LOCKS
]);
401 static struct dp_netdev_port
*dp_netdev_lookup_port(const struct dp_netdev
*dp
,
403 OVS_REQUIRES(dp
->port_mutex
);
405 enum rxq_cycles_counter_type
{
406 RXQ_CYCLES_PROC_CURR
, /* Cycles spent successfully polling and
407 processing packets during the current
409 RXQ_CYCLES_PROC_HIST
, /* Total cycles of all intervals that are used
410 during rxq to pmd assignment. */
415 DP_NETDEV_FLOW_OFFLOAD_OP_ADD
,
416 DP_NETDEV_FLOW_OFFLOAD_OP_MOD
,
417 DP_NETDEV_FLOW_OFFLOAD_OP_DEL
,
420 struct dp_flow_offload_item
{
421 struct dp_netdev_pmd_thread
*pmd
;
422 struct dp_netdev_flow
*flow
;
425 struct nlattr
*actions
;
428 struct ovs_list node
;
431 struct dp_flow_offload
{
432 struct ovs_mutex mutex
;
433 struct ovs_list list
;
437 static struct dp_flow_offload dp_flow_offload
= {
438 .mutex
= OVS_MUTEX_INITIALIZER
,
439 .list
= OVS_LIST_INITIALIZER(&dp_flow_offload
.list
),
442 static struct ovsthread_once offload_thread_once
443 = OVSTHREAD_ONCE_INITIALIZER
;
445 #define XPS_TIMEOUT 500000LL /* In microseconds. */
447 /* Contained by struct dp_netdev_port's 'rxqs' member. */
448 struct dp_netdev_rxq
{
449 struct dp_netdev_port
*port
;
450 struct netdev_rxq
*rx
;
451 unsigned core_id
; /* Core to which this queue should be
452 pinned. OVS_CORE_UNSPEC if the
453 queue doesn't need to be pinned to a
455 unsigned intrvl_idx
; /* Write index for 'cycles_intrvl'. */
456 struct dp_netdev_pmd_thread
*pmd
; /* pmd thread that polls this queue. */
457 bool is_vhost
; /* Is rxq of a vhost port. */
459 /* Counters of cycles spent successfully polling and processing pkts. */
460 atomic_ullong cycles
[RXQ_N_CYCLES
];
461 /* We store PMD_RXQ_INTERVAL_MAX intervals of data for an rxq and then
462 sum them to yield the cycles used for an rxq. */
463 atomic_ullong cycles_intrvl
[PMD_RXQ_INTERVAL_MAX
];
466 /* A port in a netdev-based datapath. */
467 struct dp_netdev_port
{
469 bool dynamic_txqs
; /* If true XPS will be used. */
470 bool need_reconfigure
; /* True if we should reconfigure netdev. */
471 struct netdev
*netdev
;
472 struct hmap_node node
; /* Node in dp_netdev's 'ports'. */
473 struct netdev_saved_flags
*sf
;
474 struct dp_netdev_rxq
*rxqs
;
475 unsigned n_rxq
; /* Number of elements in 'rxqs' */
476 unsigned *txq_used
; /* Number of threads that use each tx queue. */
477 struct ovs_mutex txq_used_mutex
;
478 bool emc_enabled
; /* If true EMC will be used. */
479 char *type
; /* Port type as requested by user. */
480 char *rxq_affinity_list
; /* Requested affinity of rx queues. */
483 /* Contained by struct dp_netdev_flow's 'stats' member. */
484 struct dp_netdev_flow_stats
{
485 atomic_llong used
; /* Last used time, in monotonic msecs. */
486 atomic_ullong packet_count
; /* Number of packets matched. */
487 atomic_ullong byte_count
; /* Number of bytes matched. */
488 atomic_uint16_t tcp_flags
; /* Bitwise-OR of seen tcp_flags values. */
491 /* A flow in 'dp_netdev_pmd_thread's 'flow_table'.
497 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
498 * its pmd thread's classifier. The text below calls this classifier 'cls'.
503 * The thread safety rules described here for "struct dp_netdev_flow" are
504 * motivated by two goals:
506 * - Prevent threads that read members of "struct dp_netdev_flow" from
507 * reading bad data due to changes by some thread concurrently modifying
510 * - Prevent two threads making changes to members of a given "struct
511 * dp_netdev_flow" from interfering with each other.
517 * A flow 'flow' may be accessed without a risk of being freed during an RCU
518 * grace period. Code that needs to hold onto a flow for a while
519 * should try incrementing 'flow->ref_cnt' with dp_netdev_flow_ref().
521 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
522 * flow from being deleted from 'cls' and it doesn't protect members of 'flow'
525 * Some members, marked 'const', are immutable. Accessing other members
526 * requires synchronization, as noted in more detail below.
528 struct dp_netdev_flow
{
529 const struct flow flow
; /* Unmasked flow that created this entry. */
530 /* Hash table index by unmasked flow. */
531 const struct cmap_node node
; /* In owning dp_netdev_pmd_thread's */
533 const struct cmap_node mark_node
; /* In owning flow_mark's mark_to_flow */
534 const ovs_u128 ufid
; /* Unique flow identifier. */
535 const ovs_u128 mega_ufid
; /* Unique mega flow identifier. */
536 const unsigned pmd_id
; /* The 'core_id' of pmd thread owning this */
539 /* Number of references.
540 * The classifier owns one reference.
541 * Any thread trying to keep a rule from being freed should hold its own
543 struct ovs_refcount ref_cnt
;
546 uint32_t mark
; /* Unique flow mark assigned to a flow */
549 struct dp_netdev_flow_stats stats
;
552 OVSRCU_TYPE(struct dp_netdev_actions
*) actions
;
554 /* While processing a group of input packets, the datapath uses the next
555 * member to store a pointer to the output batch for the flow. It is
556 * reset after the batch has been sent out (See dp_netdev_queue_batches(),
557 * packet_batch_per_flow_init() and packet_batch_per_flow_execute()). */
558 struct packet_batch_per_flow
*batch
;
560 /* Packet classification. */
561 struct dpcls_rule cr
; /* In owning dp_netdev's 'cls'. */
562 /* 'cr' must be the last member. */
565 static void dp_netdev_flow_unref(struct dp_netdev_flow
*);
566 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*);
567 static int dpif_netdev_flow_from_nlattrs(const struct nlattr
*, uint32_t,
568 struct flow
*, bool);
570 /* A set of datapath actions within a "struct dp_netdev_flow".
576 * A struct dp_netdev_actions 'actions' is protected with RCU. */
577 struct dp_netdev_actions
{
578 /* These members are immutable: they do not change during the struct's
580 unsigned int size
; /* Size of 'actions', in bytes. */
581 struct nlattr actions
[]; /* Sequence of OVS_ACTION_ATTR_* attributes. */
584 struct dp_netdev_actions
*dp_netdev_actions_create(const struct nlattr
*,
586 struct dp_netdev_actions
*dp_netdev_flow_get_actions(
587 const struct dp_netdev_flow
*);
588 static void dp_netdev_actions_free(struct dp_netdev_actions
*);
590 struct polled_queue
{
591 struct dp_netdev_rxq
*rxq
;
596 /* Contained by struct dp_netdev_pmd_thread's 'poll_list' member. */
598 struct dp_netdev_rxq
*rxq
;
599 struct hmap_node node
;
602 /* Contained by struct dp_netdev_pmd_thread's 'send_port_cache',
603 * 'tnl_port_cache' or 'tx_ports'. */
605 struct dp_netdev_port
*port
;
608 struct hmap_node node
;
609 long long flush_time
;
610 struct dp_packet_batch output_pkts
;
611 struct dp_netdev_rxq
*output_pkts_rxqs
[NETDEV_MAX_BURST
];
614 /* A set of properties for the current processing loop that is not directly
615 * associated with the pmd thread itself, but with the packets being
616 * processed or the short-term system configuration (for example, time).
617 * Contained by struct dp_netdev_pmd_thread's 'ctx' member. */
618 struct dp_netdev_pmd_thread_ctx
{
619 /* Latest measured time. See 'pmd_thread_ctx_time_update()'. */
621 /* RX queue from which last packet was received. */
622 struct dp_netdev_rxq
*last_rxq
;
623 /* EMC insertion probability context for the current processing cycle. */
624 uint32_t emc_insert_min
;
627 /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
628 * the performance overhead of interrupt processing. Therefore netdev can
629 * not implement rx-wait for these devices. dpif-netdev needs to poll
630 * these device to check for recv buffer. pmd-thread does polling for
631 * devices assigned to itself.
633 * DPDK used PMD for accessing NIC.
635 * Note, instance with cpu core id NON_PMD_CORE_ID will be reserved for
636 * I/O of all non-pmd threads. There will be no actual thread created
639 * Each struct has its own flow cache and classifier per managed ingress port.
640 * For packets received on ingress port, a look up is done on corresponding PMD
641 * thread's flow cache and in case of a miss, lookup is performed in the
642 * corresponding classifier of port. Packets are executed with the found
643 * actions in either case.
645 struct dp_netdev_pmd_thread
{
646 struct dp_netdev
*dp
;
647 struct ovs_refcount ref_cnt
; /* Every reference must be refcount'ed. */
648 struct cmap_node node
; /* In 'dp->poll_threads'. */
650 pthread_cond_t cond
; /* For synchronizing pmd thread reload. */
651 struct ovs_mutex cond_mutex
; /* Mutex for condition variable. */
653 /* Per thread exact-match cache. Note, the instance for cpu core
654 * NON_PMD_CORE_ID can be accessed by multiple threads, and thusly
655 * need to be protected by 'non_pmd_mutex'. Every other instance
656 * will only be accessed by its own pmd thread. */
657 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
) struct dfc_cache flow_cache
;
659 /* Flow-Table and classifiers
661 * Writers of 'flow_table' must take the 'flow_mutex'. Corresponding
662 * changes to 'classifiers' must be made while still holding the
665 struct ovs_mutex flow_mutex
;
666 struct cmap flow_table OVS_GUARDED
; /* Flow table. */
668 /* One classifier per in_port polled by the pmd */
669 struct cmap classifiers
;
670 /* Periodically sort subtable vectors according to hit frequencies */
671 long long int next_optimization
;
672 /* End of the next time interval for which processing cycles
673 are stored for each polled rxq. */
674 long long int rxq_next_cycle_store
;
676 /* Last interval timestamp. */
677 uint64_t intrvl_tsc_prev
;
678 /* Last interval cycles. */
679 atomic_ullong intrvl_cycles
;
681 /* Current context of the PMD thread. */
682 struct dp_netdev_pmd_thread_ctx ctx
;
684 struct latch exit_latch
; /* For terminating the pmd thread. */
685 struct seq
*reload_seq
;
686 uint64_t last_reload_seq
;
687 atomic_bool reload
; /* Do we need to reload ports? */
689 unsigned core_id
; /* CPU core id of this pmd thread. */
690 int numa_id
; /* numa node id of this pmd thread. */
693 /* Queue id used by this pmd thread to send packets on all netdevs if
694 * XPS disabled for this netdev. All static_tx_qid's are unique and less
695 * than 'cmap_count(dp->poll_threads)'. */
696 uint32_t static_tx_qid
;
698 /* Number of filled output batches. */
699 int n_output_batches
;
701 struct ovs_mutex port_mutex
; /* Mutex for 'poll_list' and 'tx_ports'. */
702 /* List of rx queues to poll. */
703 struct hmap poll_list OVS_GUARDED
;
704 /* Map of 'tx_port's used for transmission. Written by the main thread,
705 * read by the pmd thread. */
706 struct hmap tx_ports OVS_GUARDED
;
708 /* These are thread-local copies of 'tx_ports'. One contains only tunnel
709 * ports (that support push_tunnel/pop_tunnel), the other contains ports
710 * with at least one txq (that support send). A port can be in both.
712 * There are two separate maps to make sure that we don't try to execute
713 * OUTPUT on a device which has 0 txqs or PUSH/POP on a non-tunnel device.
715 * The instances for cpu core NON_PMD_CORE_ID can be accessed by multiple
716 * threads, and thusly need to be protected by 'non_pmd_mutex'. Every
717 * other instance will only be accessed by its own pmd thread. */
718 struct hmap tnl_port_cache
;
719 struct hmap send_port_cache
;
721 /* Keep track of detailed PMD performance statistics. */
722 struct pmd_perf_stats perf_stats
;
724 /* Stats from previous iteration used by automatic pmd
725 * load balance logic. */
726 uint64_t prev_stats
[PMD_N_STATS
];
727 atomic_count pmd_overloaded
;
729 /* Set to true if the pmd thread needs to be reloaded. */
733 /* Interface to netdev-based datapath. */
736 struct dp_netdev
*dp
;
737 uint64_t last_port_seq
;
740 static int get_port_by_number(struct dp_netdev
*dp
, odp_port_t port_no
,
741 struct dp_netdev_port
**portp
)
742 OVS_REQUIRES(dp
->port_mutex
);
743 static int get_port_by_name(struct dp_netdev
*dp
, const char *devname
,
744 struct dp_netdev_port
**portp
)
745 OVS_REQUIRES(dp
->port_mutex
);
746 static void dp_netdev_free(struct dp_netdev
*)
747 OVS_REQUIRES(dp_netdev_mutex
);
748 static int do_add_port(struct dp_netdev
*dp
, const char *devname
,
749 const char *type
, odp_port_t port_no
)
750 OVS_REQUIRES(dp
->port_mutex
);
751 static void do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*)
752 OVS_REQUIRES(dp
->port_mutex
);
753 static int dpif_netdev_open(const struct dpif_class
*, const char *name
,
754 bool create
, struct dpif
**);
755 static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
756 struct dp_packet_batch
*,
758 const struct flow
*flow
,
759 const struct nlattr
*actions
,
761 static void dp_netdev_input(struct dp_netdev_pmd_thread
*,
762 struct dp_packet_batch
*, odp_port_t port_no
);
763 static void dp_netdev_recirculate(struct dp_netdev_pmd_thread
*,
764 struct dp_packet_batch
*);
766 static void dp_netdev_disable_upcall(struct dp_netdev
*);
767 static void dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
);
768 static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
,
769 struct dp_netdev
*dp
, unsigned core_id
,
771 static void dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
);
772 static void dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
773 OVS_REQUIRES(dp
->port_mutex
);
775 static void *pmd_thread_main(void *);
776 static struct dp_netdev_pmd_thread
*dp_netdev_get_pmd(struct dp_netdev
*dp
,
778 static struct dp_netdev_pmd_thread
*
779 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
);
780 static void dp_netdev_del_pmd(struct dp_netdev
*dp
,
781 struct dp_netdev_pmd_thread
*pmd
);
782 static void dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
, bool non_pmd
);
783 static void dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
);
784 static void dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
785 struct dp_netdev_port
*port
)
786 OVS_REQUIRES(pmd
->port_mutex
);
787 static void dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
789 OVS_REQUIRES(pmd
->port_mutex
);
790 static void dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
791 struct dp_netdev_rxq
*rxq
)
792 OVS_REQUIRES(pmd
->port_mutex
);
793 static void dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
794 struct rxq_poll
*poll
)
795 OVS_REQUIRES(pmd
->port_mutex
);
797 dp_netdev_pmd_flush_output_packets(struct dp_netdev_pmd_thread
*pmd
,
800 static void reconfigure_datapath(struct dp_netdev
*dp
)
801 OVS_REQUIRES(dp
->port_mutex
);
802 static bool dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
);
803 static void dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
);
804 static void dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
);
805 static void pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
806 OVS_REQUIRES(pmd
->port_mutex
);
808 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread
*pmd
,
809 struct polled_queue
*poll_list
, int poll_cnt
);
811 dp_netdev_rxq_set_cycles(struct dp_netdev_rxq
*rx
,
812 enum rxq_cycles_counter_type type
,
813 unsigned long long cycles
);
815 dp_netdev_rxq_get_cycles(struct dp_netdev_rxq
*rx
,
816 enum rxq_cycles_counter_type type
);
818 dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq
*rx
,
819 unsigned long long cycles
);
821 dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq
*rx
, unsigned idx
);
823 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
825 static int dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
828 static inline bool emc_entry_alive(struct emc_entry
*ce
);
829 static void emc_clear_entry(struct emc_entry
*ce
);
830 static void smc_clear_entry(struct smc_bucket
*b
, int idx
);
832 static void dp_netdev_request_reconfigure(struct dp_netdev
*dp
);
834 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread
*pmd
);
835 static void queue_netdev_flow_del(struct dp_netdev_pmd_thread
*pmd
,
836 struct dp_netdev_flow
*flow
);
839 emc_cache_init(struct emc_cache
*flow_cache
)
843 flow_cache
->sweep_idx
= 0;
844 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
845 flow_cache
->entries
[i
].flow
= NULL
;
846 flow_cache
->entries
[i
].key
.hash
= 0;
847 flow_cache
->entries
[i
].key
.len
= sizeof(struct miniflow
);
848 flowmap_init(&flow_cache
->entries
[i
].key
.mf
.map
);
853 smc_cache_init(struct smc_cache
*smc_cache
)
856 for (i
= 0; i
< SMC_BUCKET_CNT
; i
++) {
857 for (j
= 0; j
< SMC_ENTRY_PER_BUCKET
; j
++) {
858 smc_cache
->buckets
[i
].flow_idx
[j
] = UINT16_MAX
;
864 dfc_cache_init(struct dfc_cache
*flow_cache
)
866 emc_cache_init(&flow_cache
->emc_cache
);
867 smc_cache_init(&flow_cache
->smc_cache
);
871 emc_cache_uninit(struct emc_cache
*flow_cache
)
875 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
876 emc_clear_entry(&flow_cache
->entries
[i
]);
881 smc_cache_uninit(struct smc_cache
*smc
)
885 for (i
= 0; i
< SMC_BUCKET_CNT
; i
++) {
886 for (j
= 0; j
< SMC_ENTRY_PER_BUCKET
; j
++) {
887 smc_clear_entry(&(smc
->buckets
[i
]), j
);
893 dfc_cache_uninit(struct dfc_cache
*flow_cache
)
895 smc_cache_uninit(&flow_cache
->smc_cache
);
896 emc_cache_uninit(&flow_cache
->emc_cache
);
899 /* Check and clear dead flow references slowly (one entry at each
902 emc_cache_slow_sweep(struct emc_cache
*flow_cache
)
904 struct emc_entry
*entry
= &flow_cache
->entries
[flow_cache
->sweep_idx
];
906 if (!emc_entry_alive(entry
)) {
907 emc_clear_entry(entry
);
909 flow_cache
->sweep_idx
= (flow_cache
->sweep_idx
+ 1) & EM_FLOW_HASH_MASK
;
912 /* Updates the time in PMD threads context and should be called in three cases:
914 * 1. PMD structure initialization:
915 * - dp_netdev_configure_pmd()
917 * 2. Before processing of the new packet batch:
918 * - dpif_netdev_execute()
919 * - dp_netdev_process_rxq_port()
921 * 3. At least once per polling iteration in main polling threads if no
922 * packets received on current iteration:
923 * - dpif_netdev_run()
924 * - pmd_thread_main()
926 * 'pmd->ctx.now' should be used without update in all other cases if possible.
929 pmd_thread_ctx_time_update(struct dp_netdev_pmd_thread
*pmd
)
931 pmd
->ctx
.now
= time_usec();
934 /* Returns true if 'dpif' is a netdev or dummy dpif, false otherwise. */
936 dpif_is_netdev(const struct dpif
*dpif
)
938 return dpif
->dpif_class
->open
== dpif_netdev_open
;
941 static struct dpif_netdev
*
942 dpif_netdev_cast(const struct dpif
*dpif
)
944 ovs_assert(dpif_is_netdev(dpif
));
945 return CONTAINER_OF(dpif
, struct dpif_netdev
, dpif
);
948 static struct dp_netdev
*
949 get_dp_netdev(const struct dpif
*dpif
)
951 return dpif_netdev_cast(dpif
)->dp
;
955 PMD_INFO_SHOW_STATS
, /* Show how cpu cycles are spent. */
956 PMD_INFO_CLEAR_STATS
, /* Set the cycles count to 0. */
957 PMD_INFO_SHOW_RXQ
, /* Show poll lists of pmd threads. */
958 PMD_INFO_PERF_SHOW
, /* Show pmd performance details. */
962 format_pmd_thread(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
964 ds_put_cstr(reply
, (pmd
->core_id
== NON_PMD_CORE_ID
)
965 ? "main thread" : "pmd thread");
966 if (pmd
->numa_id
!= OVS_NUMA_UNSPEC
) {
967 ds_put_format(reply
, " numa_id %d", pmd
->numa_id
);
969 if (pmd
->core_id
!= OVS_CORE_UNSPEC
&& pmd
->core_id
!= NON_PMD_CORE_ID
) {
970 ds_put_format(reply
, " core_id %u", pmd
->core_id
);
972 ds_put_cstr(reply
, ":\n");
976 pmd_info_show_stats(struct ds
*reply
,
977 struct dp_netdev_pmd_thread
*pmd
)
979 uint64_t stats
[PMD_N_STATS
];
980 uint64_t total_cycles
, total_packets
;
981 double passes_per_pkt
= 0;
982 double lookups_per_hit
= 0;
983 double packets_per_batch
= 0;
985 pmd_perf_read_counters(&pmd
->perf_stats
, stats
);
986 total_cycles
= stats
[PMD_CYCLES_ITER_IDLE
]
987 + stats
[PMD_CYCLES_ITER_BUSY
];
988 total_packets
= stats
[PMD_STAT_RECV
];
990 format_pmd_thread(reply
, pmd
);
992 if (total_packets
> 0) {
993 passes_per_pkt
= (total_packets
+ stats
[PMD_STAT_RECIRC
])
994 / (double) total_packets
;
996 if (stats
[PMD_STAT_MASKED_HIT
] > 0) {
997 lookups_per_hit
= stats
[PMD_STAT_MASKED_LOOKUP
]
998 / (double) stats
[PMD_STAT_MASKED_HIT
];
1000 if (stats
[PMD_STAT_SENT_BATCHES
] > 0) {
1001 packets_per_batch
= stats
[PMD_STAT_SENT_PKTS
]
1002 / (double) stats
[PMD_STAT_SENT_BATCHES
];
1005 ds_put_format(reply
,
1006 " packets received: %"PRIu64
"\n"
1007 " packet recirculations: %"PRIu64
"\n"
1008 " avg. datapath passes per packet: %.02f\n"
1009 " emc hits: %"PRIu64
"\n"
1010 " smc hits: %"PRIu64
"\n"
1011 " megaflow hits: %"PRIu64
"\n"
1012 " avg. subtable lookups per megaflow hit: %.02f\n"
1013 " miss with success upcall: %"PRIu64
"\n"
1014 " miss with failed upcall: %"PRIu64
"\n"
1015 " avg. packets per output batch: %.02f\n",
1016 total_packets
, stats
[PMD_STAT_RECIRC
],
1017 passes_per_pkt
, stats
[PMD_STAT_EXACT_HIT
],
1018 stats
[PMD_STAT_SMC_HIT
],
1019 stats
[PMD_STAT_MASKED_HIT
], lookups_per_hit
,
1020 stats
[PMD_STAT_MISS
], stats
[PMD_STAT_LOST
],
1023 if (total_cycles
== 0) {
1027 ds_put_format(reply
,
1028 " idle cycles: %"PRIu64
" (%.02f%%)\n"
1029 " processing cycles: %"PRIu64
" (%.02f%%)\n",
1030 stats
[PMD_CYCLES_ITER_IDLE
],
1031 stats
[PMD_CYCLES_ITER_IDLE
] / (double) total_cycles
* 100,
1032 stats
[PMD_CYCLES_ITER_BUSY
],
1033 stats
[PMD_CYCLES_ITER_BUSY
] / (double) total_cycles
* 100);
1035 if (total_packets
== 0) {
1039 ds_put_format(reply
,
1040 " avg cycles per packet: %.02f (%"PRIu64
"/%"PRIu64
")\n",
1041 total_cycles
/ (double) total_packets
,
1042 total_cycles
, total_packets
);
1044 ds_put_format(reply
,
1045 " avg processing cycles per packet: "
1046 "%.02f (%"PRIu64
"/%"PRIu64
")\n",
1047 stats
[PMD_CYCLES_ITER_BUSY
] / (double) total_packets
,
1048 stats
[PMD_CYCLES_ITER_BUSY
], total_packets
);
1052 pmd_info_show_perf(struct ds
*reply
,
1053 struct dp_netdev_pmd_thread
*pmd
,
1054 struct pmd_perf_params
*par
)
1056 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
1058 xastrftime_msec("%H:%M:%S.###", time_wall_msec(), true);
1059 long long now
= time_msec();
1060 double duration
= (now
- pmd
->perf_stats
.start_ms
) / 1000.0;
1062 ds_put_cstr(reply
, "\n");
1063 ds_put_format(reply
, "Time: %s\n", time_str
);
1064 ds_put_format(reply
, "Measurement duration: %.3f s\n", duration
);
1065 ds_put_cstr(reply
, "\n");
1066 format_pmd_thread(reply
, pmd
);
1067 ds_put_cstr(reply
, "\n");
1068 pmd_perf_format_overall_stats(reply
, &pmd
->perf_stats
, duration
);
1069 if (pmd_perf_metrics_enabled(pmd
)) {
1070 /* Prevent parallel clearing of perf metrics. */
1071 ovs_mutex_lock(&pmd
->perf_stats
.clear_mutex
);
1072 if (par
->histograms
) {
1073 ds_put_cstr(reply
, "\n");
1074 pmd_perf_format_histograms(reply
, &pmd
->perf_stats
);
1076 if (par
->iter_hist_len
> 0) {
1077 ds_put_cstr(reply
, "\n");
1078 pmd_perf_format_iteration_history(reply
, &pmd
->perf_stats
,
1079 par
->iter_hist_len
);
1081 if (par
->ms_hist_len
> 0) {
1082 ds_put_cstr(reply
, "\n");
1083 pmd_perf_format_ms_history(reply
, &pmd
->perf_stats
,
1086 ovs_mutex_unlock(&pmd
->perf_stats
.clear_mutex
);
1093 compare_poll_list(const void *a_
, const void *b_
)
1095 const struct rxq_poll
*a
= a_
;
1096 const struct rxq_poll
*b
= b_
;
1098 const char *namea
= netdev_rxq_get_name(a
->rxq
->rx
);
1099 const char *nameb
= netdev_rxq_get_name(b
->rxq
->rx
);
1101 int cmp
= strcmp(namea
, nameb
);
1103 return netdev_rxq_get_queue_id(a
->rxq
->rx
)
1104 - netdev_rxq_get_queue_id(b
->rxq
->rx
);
1111 sorted_poll_list(struct dp_netdev_pmd_thread
*pmd
, struct rxq_poll
**list
,
1113 OVS_REQUIRES(pmd
->port_mutex
)
1115 struct rxq_poll
*ret
, *poll
;
1118 *n
= hmap_count(&pmd
->poll_list
);
1122 ret
= xcalloc(*n
, sizeof *ret
);
1124 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
1128 ovs_assert(i
== *n
);
1129 qsort(ret
, *n
, sizeof *ret
, compare_poll_list
);
1136 pmd_info_show_rxq(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
1138 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
1139 struct rxq_poll
*list
;
1141 uint64_t total_cycles
= 0;
1143 ds_put_format(reply
,
1144 "pmd thread numa_id %d core_id %u:\n isolated : %s\n",
1145 pmd
->numa_id
, pmd
->core_id
, (pmd
->isolated
)
1146 ? "true" : "false");
1148 ovs_mutex_lock(&pmd
->port_mutex
);
1149 sorted_poll_list(pmd
, &list
, &n_rxq
);
1151 /* Get the total pmd cycles for an interval. */
1152 atomic_read_relaxed(&pmd
->intrvl_cycles
, &total_cycles
);
1153 /* Estimate the cycles to cover all intervals. */
1154 total_cycles
*= PMD_RXQ_INTERVAL_MAX
;
1156 for (int i
= 0; i
< n_rxq
; i
++) {
1157 struct dp_netdev_rxq
*rxq
= list
[i
].rxq
;
1158 const char *name
= netdev_rxq_get_name(rxq
->rx
);
1159 uint64_t proc_cycles
= 0;
1161 for (int j
= 0; j
< PMD_RXQ_INTERVAL_MAX
; j
++) {
1162 proc_cycles
+= dp_netdev_rxq_get_intrvl_cycles(rxq
, j
);
1164 ds_put_format(reply
, " port: %-16s queue-id: %2d", name
,
1165 netdev_rxq_get_queue_id(list
[i
].rxq
->rx
));
1166 ds_put_format(reply
, " pmd usage: ");
1168 ds_put_format(reply
, "%2"PRIu64
"",
1169 proc_cycles
* 100 / total_cycles
);
1170 ds_put_cstr(reply
, " %");
1172 ds_put_format(reply
, "%s", "NOT AVAIL");
1174 ds_put_cstr(reply
, "\n");
1176 ovs_mutex_unlock(&pmd
->port_mutex
);
1182 compare_poll_thread_list(const void *a_
, const void *b_
)
1184 const struct dp_netdev_pmd_thread
*a
, *b
;
1186 a
= *(struct dp_netdev_pmd_thread
**)a_
;
1187 b
= *(struct dp_netdev_pmd_thread
**)b_
;
1189 if (a
->core_id
< b
->core_id
) {
1192 if (a
->core_id
> b
->core_id
) {
1198 /* Create a sorted list of pmd's from the dp->poll_threads cmap. We can use
1199 * this list, as long as we do not go to quiescent state. */
1201 sorted_poll_thread_list(struct dp_netdev
*dp
,
1202 struct dp_netdev_pmd_thread
***list
,
1205 struct dp_netdev_pmd_thread
*pmd
;
1206 struct dp_netdev_pmd_thread
**pmd_list
;
1207 size_t k
= 0, n_pmds
;
1209 n_pmds
= cmap_count(&dp
->poll_threads
);
1210 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
1212 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1216 pmd_list
[k
++] = pmd
;
1219 qsort(pmd_list
, k
, sizeof *pmd_list
, compare_poll_thread_list
);
1226 dpif_netdev_pmd_rebalance(struct unixctl_conn
*conn
, int argc
,
1227 const char *argv
[], void *aux OVS_UNUSED
)
1229 struct ds reply
= DS_EMPTY_INITIALIZER
;
1230 struct dp_netdev
*dp
= NULL
;
1232 ovs_mutex_lock(&dp_netdev_mutex
);
1235 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
1236 } else if (shash_count(&dp_netdevs
) == 1) {
1237 /* There's only one datapath */
1238 dp
= shash_first(&dp_netdevs
)->data
;
1242 ovs_mutex_unlock(&dp_netdev_mutex
);
1243 unixctl_command_reply_error(conn
,
1244 "please specify an existing datapath");
1248 dp_netdev_request_reconfigure(dp
);
1249 ovs_mutex_unlock(&dp_netdev_mutex
);
1250 ds_put_cstr(&reply
, "pmd rxq rebalance requested.\n");
1251 unixctl_command_reply(conn
, ds_cstr(&reply
));
1256 dpif_netdev_pmd_info(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
1259 struct ds reply
= DS_EMPTY_INITIALIZER
;
1260 struct dp_netdev_pmd_thread
**pmd_list
;
1261 struct dp_netdev
*dp
= NULL
;
1262 enum pmd_info_type type
= *(enum pmd_info_type
*) aux
;
1263 unsigned int core_id
;
1264 bool filter_on_pmd
= false;
1267 ovs_mutex_lock(&dp_netdev_mutex
);
1270 if (!strcmp(argv
[1], "-pmd") && argc
> 2) {
1271 if (str_to_uint(argv
[2], 10, &core_id
)) {
1272 filter_on_pmd
= true;
1277 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
1284 if (shash_count(&dp_netdevs
) == 1) {
1285 /* There's only one datapath */
1286 dp
= shash_first(&dp_netdevs
)->data
;
1288 ovs_mutex_unlock(&dp_netdev_mutex
);
1289 unixctl_command_reply_error(conn
,
1290 "please specify an existing datapath");
1295 sorted_poll_thread_list(dp
, &pmd_list
, &n
);
1296 for (size_t i
= 0; i
< n
; i
++) {
1297 struct dp_netdev_pmd_thread
*pmd
= pmd_list
[i
];
1301 if (filter_on_pmd
&& pmd
->core_id
!= core_id
) {
1304 if (type
== PMD_INFO_SHOW_RXQ
) {
1305 pmd_info_show_rxq(&reply
, pmd
);
1306 } else if (type
== PMD_INFO_CLEAR_STATS
) {
1307 pmd_perf_stats_clear(&pmd
->perf_stats
);
1308 } else if (type
== PMD_INFO_SHOW_STATS
) {
1309 pmd_info_show_stats(&reply
, pmd
);
1310 } else if (type
== PMD_INFO_PERF_SHOW
) {
1311 pmd_info_show_perf(&reply
, pmd
, (struct pmd_perf_params
*)aux
);
1316 ovs_mutex_unlock(&dp_netdev_mutex
);
1318 unixctl_command_reply(conn
, ds_cstr(&reply
));
1323 pmd_perf_show_cmd(struct unixctl_conn
*conn
, int argc
,
1325 void *aux OVS_UNUSED
)
1327 struct pmd_perf_params par
;
1328 long int it_hist
= 0, ms_hist
= 0;
1329 par
.histograms
= true;
1332 if (!strcmp(argv
[1], "-nh")) {
1333 par
.histograms
= false;
1336 } else if (!strcmp(argv
[1], "-it") && argc
> 2) {
1337 it_hist
= strtol(argv
[2], NULL
, 10);
1340 } else if (it_hist
> HISTORY_LEN
) {
1341 it_hist
= HISTORY_LEN
;
1345 } else if (!strcmp(argv
[1], "-ms") && argc
> 2) {
1346 ms_hist
= strtol(argv
[2], NULL
, 10);
1349 } else if (ms_hist
> HISTORY_LEN
) {
1350 ms_hist
= HISTORY_LEN
;
1358 par
.iter_hist_len
= it_hist
;
1359 par
.ms_hist_len
= ms_hist
;
1360 par
.command_type
= PMD_INFO_PERF_SHOW
;
1361 dpif_netdev_pmd_info(conn
, argc
, argv
, &par
);
1365 dpif_netdev_init(void)
1367 static enum pmd_info_type show_aux
= PMD_INFO_SHOW_STATS
,
1368 clear_aux
= PMD_INFO_CLEAR_STATS
,
1369 poll_aux
= PMD_INFO_SHOW_RXQ
;
1371 unixctl_command_register("dpif-netdev/pmd-stats-show", "[-pmd core] [dp]",
1372 0, 3, dpif_netdev_pmd_info
,
1374 unixctl_command_register("dpif-netdev/pmd-stats-clear", "[-pmd core] [dp]",
1375 0, 3, dpif_netdev_pmd_info
,
1376 (void *)&clear_aux
);
1377 unixctl_command_register("dpif-netdev/pmd-rxq-show", "[-pmd core] [dp]",
1378 0, 3, dpif_netdev_pmd_info
,
1380 unixctl_command_register("dpif-netdev/pmd-perf-show",
1381 "[-nh] [-it iter-history-len]"
1382 " [-ms ms-history-len]"
1383 " [-pmd core] [dp]",
1384 0, 8, pmd_perf_show_cmd
,
1386 unixctl_command_register("dpif-netdev/pmd-rxq-rebalance", "[dp]",
1387 0, 1, dpif_netdev_pmd_rebalance
,
1389 unixctl_command_register("dpif-netdev/pmd-perf-log-set",
1390 "on|off [-b before] [-a after] [-e|-ne] "
1391 "[-us usec] [-q qlen]",
1392 0, 10, pmd_perf_log_set_cmd
,
1398 dpif_netdev_enumerate(struct sset
*all_dps
,
1399 const struct dpif_class
*dpif_class
)
1401 struct shash_node
*node
;
1403 ovs_mutex_lock(&dp_netdev_mutex
);
1404 SHASH_FOR_EACH(node
, &dp_netdevs
) {
1405 struct dp_netdev
*dp
= node
->data
;
1406 if (dpif_class
!= dp
->class) {
1407 /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs.
1408 * If the class doesn't match, skip this dpif. */
1411 sset_add(all_dps
, node
->name
);
1413 ovs_mutex_unlock(&dp_netdev_mutex
);
1419 dpif_netdev_class_is_dummy(const struct dpif_class
*class)
1421 return class != &dpif_netdev_class
;
1425 dpif_netdev_port_open_type(const struct dpif_class
*class, const char *type
)
1427 return strcmp(type
, "internal") ? type
1428 : dpif_netdev_class_is_dummy(class) ? "dummy-internal"
1432 static struct dpif
*
1433 create_dpif_netdev(struct dp_netdev
*dp
)
1435 uint16_t netflow_id
= hash_string(dp
->name
, 0);
1436 struct dpif_netdev
*dpif
;
1438 ovs_refcount_ref(&dp
->ref_cnt
);
1440 dpif
= xmalloc(sizeof *dpif
);
1441 dpif_init(&dpif
->dpif
, dp
->class, dp
->name
, netflow_id
>> 8, netflow_id
);
1443 dpif
->last_port_seq
= seq_read(dp
->port_seq
);
1448 /* Choose an unused, non-zero port number and return it on success.
1449 * Return ODPP_NONE on failure. */
1451 choose_port(struct dp_netdev
*dp
, const char *name
)
1452 OVS_REQUIRES(dp
->port_mutex
)
1456 if (dp
->class != &dpif_netdev_class
) {
1460 /* If the port name begins with "br", start the number search at
1461 * 100 to make writing tests easier. */
1462 if (!strncmp(name
, "br", 2)) {
1466 /* If the port name contains a number, try to assign that port number.
1467 * This can make writing unit tests easier because port numbers are
1469 for (p
= name
; *p
!= '\0'; p
++) {
1470 if (isdigit((unsigned char) *p
)) {
1471 port_no
= start_no
+ strtol(p
, NULL
, 10);
1472 if (port_no
> 0 && port_no
!= odp_to_u32(ODPP_NONE
)
1473 && !dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
1474 return u32_to_odp(port_no
);
1481 for (port_no
= 1; port_no
<= UINT16_MAX
; port_no
++) {
1482 if (!dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
1483 return u32_to_odp(port_no
);
1491 create_dp_netdev(const char *name
, const struct dpif_class
*class,
1492 struct dp_netdev
**dpp
)
1493 OVS_REQUIRES(dp_netdev_mutex
)
1495 struct dp_netdev
*dp
;
1498 dp
= xzalloc(sizeof *dp
);
1499 shash_add(&dp_netdevs
, name
, dp
);
1501 *CONST_CAST(const struct dpif_class
**, &dp
->class) = class;
1502 *CONST_CAST(const char **, &dp
->name
) = xstrdup(name
);
1503 ovs_refcount_init(&dp
->ref_cnt
);
1504 atomic_flag_clear(&dp
->destroyed
);
1506 ovs_mutex_init(&dp
->port_mutex
);
1507 hmap_init(&dp
->ports
);
1508 dp
->port_seq
= seq_create();
1509 fat_rwlock_init(&dp
->upcall_rwlock
);
1511 dp
->reconfigure_seq
= seq_create();
1512 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
1514 for (int i
= 0; i
< N_METER_LOCKS
; ++i
) {
1515 ovs_mutex_init_adaptive(&dp
->meter_locks
[i
]);
1518 /* Disable upcalls by default. */
1519 dp_netdev_disable_upcall(dp
);
1520 dp
->upcall_aux
= NULL
;
1521 dp
->upcall_cb
= NULL
;
1523 conntrack_init(&dp
->conntrack
);
1525 atomic_init(&dp
->emc_insert_min
, DEFAULT_EM_FLOW_INSERT_MIN
);
1526 atomic_init(&dp
->tx_flush_interval
, DEFAULT_TX_FLUSH_INTERVAL
);
1528 cmap_init(&dp
->poll_threads
);
1529 dp
->pmd_rxq_assign_cyc
= true;
1531 ovs_mutex_init(&dp
->tx_qid_pool_mutex
);
1532 /* We need 1 Tx queue for each possible core + 1 for non-PMD threads. */
1533 dp
->tx_qid_pool
= id_pool_create(0, ovs_numa_get_n_cores() + 1);
1535 ovs_mutex_init_recursive(&dp
->non_pmd_mutex
);
1536 ovsthread_key_create(&dp
->per_pmd_key
, NULL
);
1538 ovs_mutex_lock(&dp
->port_mutex
);
1539 /* non-PMD will be created before all other threads and will
1540 * allocate static_tx_qid = 0. */
1541 dp_netdev_set_nonpmd(dp
);
1543 error
= do_add_port(dp
, name
, dpif_netdev_port_open_type(dp
->class,
1546 ovs_mutex_unlock(&dp
->port_mutex
);
1552 dp
->last_tnl_conf_seq
= seq_read(tnl_conf_seq
);
1558 dp_netdev_request_reconfigure(struct dp_netdev
*dp
)
1560 seq_change(dp
->reconfigure_seq
);
1564 dp_netdev_is_reconf_required(struct dp_netdev
*dp
)
1566 return seq_read(dp
->reconfigure_seq
) != dp
->last_reconfigure_seq
;
1570 dpif_netdev_open(const struct dpif_class
*class, const char *name
,
1571 bool create
, struct dpif
**dpifp
)
1573 struct dp_netdev
*dp
;
1576 ovs_mutex_lock(&dp_netdev_mutex
);
1577 dp
= shash_find_data(&dp_netdevs
, name
);
1579 error
= create
? create_dp_netdev(name
, class, &dp
) : ENODEV
;
1581 error
= (dp
->class != class ? EINVAL
1586 *dpifp
= create_dpif_netdev(dp
);
1589 ovs_mutex_unlock(&dp_netdev_mutex
);
1595 dp_netdev_destroy_upcall_lock(struct dp_netdev
*dp
)
1596 OVS_NO_THREAD_SAFETY_ANALYSIS
1598 /* Check that upcalls are disabled, i.e. that the rwlock is taken */
1599 ovs_assert(fat_rwlock_tryrdlock(&dp
->upcall_rwlock
));
1601 /* Before freeing a lock we should release it */
1602 fat_rwlock_unlock(&dp
->upcall_rwlock
);
1603 fat_rwlock_destroy(&dp
->upcall_rwlock
);
1607 dp_delete_meter(struct dp_netdev
*dp
, uint32_t meter_id
)
1608 OVS_REQUIRES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
1610 if (dp
->meters
[meter_id
]) {
1611 free(dp
->meters
[meter_id
]);
1612 dp
->meters
[meter_id
] = NULL
;
1616 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
1617 * through the 'dp_netdevs' shash while freeing 'dp'. */
1619 dp_netdev_free(struct dp_netdev
*dp
)
1620 OVS_REQUIRES(dp_netdev_mutex
)
1622 struct dp_netdev_port
*port
, *next
;
1624 shash_find_and_delete(&dp_netdevs
, dp
->name
);
1626 ovs_mutex_lock(&dp
->port_mutex
);
1627 HMAP_FOR_EACH_SAFE (port
, next
, node
, &dp
->ports
) {
1628 do_del_port(dp
, port
);
1630 ovs_mutex_unlock(&dp
->port_mutex
);
1632 dp_netdev_destroy_all_pmds(dp
, true);
1633 cmap_destroy(&dp
->poll_threads
);
1635 ovs_mutex_destroy(&dp
->tx_qid_pool_mutex
);
1636 id_pool_destroy(dp
->tx_qid_pool
);
1638 ovs_mutex_destroy(&dp
->non_pmd_mutex
);
1639 ovsthread_key_delete(dp
->per_pmd_key
);
1641 conntrack_destroy(&dp
->conntrack
);
1644 seq_destroy(dp
->reconfigure_seq
);
1646 seq_destroy(dp
->port_seq
);
1647 hmap_destroy(&dp
->ports
);
1648 ovs_mutex_destroy(&dp
->port_mutex
);
1650 /* Upcalls must be disabled at this point */
1651 dp_netdev_destroy_upcall_lock(dp
);
1655 for (i
= 0; i
< MAX_METERS
; ++i
) {
1657 dp_delete_meter(dp
, i
);
1658 meter_unlock(dp
, i
);
1660 for (i
= 0; i
< N_METER_LOCKS
; ++i
) {
1661 ovs_mutex_destroy(&dp
->meter_locks
[i
]);
1664 free(dp
->pmd_cmask
);
1665 free(CONST_CAST(char *, dp
->name
));
1670 dp_netdev_unref(struct dp_netdev
*dp
)
1673 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
1674 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
1675 ovs_mutex_lock(&dp_netdev_mutex
);
1676 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1679 ovs_mutex_unlock(&dp_netdev_mutex
);
1684 dpif_netdev_close(struct dpif
*dpif
)
1686 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1688 dp_netdev_unref(dp
);
1693 dpif_netdev_destroy(struct dpif
*dpif
)
1695 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1697 if (!atomic_flag_test_and_set(&dp
->destroyed
)) {
1698 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1699 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
1707 /* Add 'n' to the atomic variable 'var' non-atomically and using relaxed
1708 * load/store semantics. While the increment is not atomic, the load and
1709 * store operations are, making it impossible to read inconsistent values.
1711 * This is used to update thread local stats counters. */
1713 non_atomic_ullong_add(atomic_ullong
*var
, unsigned long long n
)
1715 unsigned long long tmp
;
1717 atomic_read_relaxed(var
, &tmp
);
1719 atomic_store_relaxed(var
, tmp
);
1723 dpif_netdev_get_stats(const struct dpif
*dpif
, struct dpif_dp_stats
*stats
)
1725 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1726 struct dp_netdev_pmd_thread
*pmd
;
1727 uint64_t pmd_stats
[PMD_N_STATS
];
1729 stats
->n_flows
= stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
1730 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1731 stats
->n_flows
+= cmap_count(&pmd
->flow_table
);
1732 pmd_perf_read_counters(&pmd
->perf_stats
, pmd_stats
);
1733 stats
->n_hit
+= pmd_stats
[PMD_STAT_EXACT_HIT
];
1734 stats
->n_hit
+= pmd_stats
[PMD_STAT_SMC_HIT
];
1735 stats
->n_hit
+= pmd_stats
[PMD_STAT_MASKED_HIT
];
1736 stats
->n_missed
+= pmd_stats
[PMD_STAT_MISS
];
1737 stats
->n_lost
+= pmd_stats
[PMD_STAT_LOST
];
1739 stats
->n_masks
= UINT32_MAX
;
1740 stats
->n_mask_hit
= UINT64_MAX
;
1746 dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread
*pmd
)
1748 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
1749 ovs_mutex_lock(&pmd
->dp
->non_pmd_mutex
);
1750 ovs_mutex_lock(&pmd
->port_mutex
);
1751 pmd_load_cached_ports(pmd
);
1752 ovs_mutex_unlock(&pmd
->port_mutex
);
1753 ovs_mutex_unlock(&pmd
->dp
->non_pmd_mutex
);
1757 ovs_mutex_lock(&pmd
->cond_mutex
);
1758 seq_change(pmd
->reload_seq
);
1759 atomic_store_relaxed(&pmd
->reload
, true);
1760 ovs_mutex_cond_wait(&pmd
->cond
, &pmd
->cond_mutex
);
1761 ovs_mutex_unlock(&pmd
->cond_mutex
);
1765 hash_port_no(odp_port_t port_no
)
1767 return hash_int(odp_to_u32(port_no
), 0);
1771 port_create(const char *devname
, const char *type
,
1772 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1774 struct netdev_saved_flags
*sf
;
1775 struct dp_netdev_port
*port
;
1776 enum netdev_flags flags
;
1777 struct netdev
*netdev
;
1782 /* Open and validate network device. */
1783 error
= netdev_open(devname
, type
, &netdev
);
1787 /* XXX reject non-Ethernet devices */
1789 netdev_get_flags(netdev
, &flags
);
1790 if (flags
& NETDEV_LOOPBACK
) {
1791 VLOG_ERR("%s: cannot add a loopback device", devname
);
1796 error
= netdev_turn_flags_on(netdev
, NETDEV_PROMISC
, &sf
);
1798 VLOG_ERR("%s: cannot set promisc flag", devname
);
1802 port
= xzalloc(sizeof *port
);
1803 port
->port_no
= port_no
;
1804 port
->netdev
= netdev
;
1805 port
->type
= xstrdup(type
);
1807 port
->emc_enabled
= true;
1808 port
->need_reconfigure
= true;
1809 ovs_mutex_init(&port
->txq_used_mutex
);
1816 netdev_close(netdev
);
1821 do_add_port(struct dp_netdev
*dp
, const char *devname
, const char *type
,
1823 OVS_REQUIRES(dp
->port_mutex
)
1825 struct dp_netdev_port
*port
;
1828 /* Reject devices already in 'dp'. */
1829 if (!get_port_by_name(dp
, devname
, &port
)) {
1833 error
= port_create(devname
, type
, port_no
, &port
);
1838 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
1839 seq_change(dp
->port_seq
);
1841 reconfigure_datapath(dp
);
1847 dpif_netdev_port_add(struct dpif
*dpif
, struct netdev
*netdev
,
1848 odp_port_t
*port_nop
)
1850 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1851 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1852 const char *dpif_port
;
1856 ovs_mutex_lock(&dp
->port_mutex
);
1857 dpif_port
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1858 if (*port_nop
!= ODPP_NONE
) {
1859 port_no
= *port_nop
;
1860 error
= dp_netdev_lookup_port(dp
, *port_nop
) ? EBUSY
: 0;
1862 port_no
= choose_port(dp
, dpif_port
);
1863 error
= port_no
== ODPP_NONE
? EFBIG
: 0;
1866 *port_nop
= port_no
;
1867 error
= do_add_port(dp
, dpif_port
, netdev_get_type(netdev
), port_no
);
1869 ovs_mutex_unlock(&dp
->port_mutex
);
1875 dpif_netdev_port_del(struct dpif
*dpif
, odp_port_t port_no
)
1877 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1880 ovs_mutex_lock(&dp
->port_mutex
);
1881 if (port_no
== ODPP_LOCAL
) {
1884 struct dp_netdev_port
*port
;
1886 error
= get_port_by_number(dp
, port_no
, &port
);
1888 do_del_port(dp
, port
);
1891 ovs_mutex_unlock(&dp
->port_mutex
);
1897 is_valid_port_number(odp_port_t port_no
)
1899 return port_no
!= ODPP_NONE
;
1902 static struct dp_netdev_port
*
1903 dp_netdev_lookup_port(const struct dp_netdev
*dp
, odp_port_t port_no
)
1904 OVS_REQUIRES(dp
->port_mutex
)
1906 struct dp_netdev_port
*port
;
1908 HMAP_FOR_EACH_WITH_HASH (port
, node
, hash_port_no(port_no
), &dp
->ports
) {
1909 if (port
->port_no
== port_no
) {
1917 get_port_by_number(struct dp_netdev
*dp
,
1918 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1919 OVS_REQUIRES(dp
->port_mutex
)
1921 if (!is_valid_port_number(port_no
)) {
1925 *portp
= dp_netdev_lookup_port(dp
, port_no
);
1926 return *portp
? 0 : ENODEV
;
1931 port_destroy(struct dp_netdev_port
*port
)
1937 netdev_close(port
->netdev
);
1938 netdev_restore_flags(port
->sf
);
1940 for (unsigned i
= 0; i
< port
->n_rxq
; i
++) {
1941 netdev_rxq_close(port
->rxqs
[i
].rx
);
1943 ovs_mutex_destroy(&port
->txq_used_mutex
);
1944 free(port
->rxq_affinity_list
);
1945 free(port
->txq_used
);
1952 get_port_by_name(struct dp_netdev
*dp
,
1953 const char *devname
, struct dp_netdev_port
**portp
)
1954 OVS_REQUIRES(dp
->port_mutex
)
1956 struct dp_netdev_port
*port
;
1958 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1959 if (!strcmp(netdev_get_name(port
->netdev
), devname
)) {
1965 /* Callers of dpif_netdev_port_query_by_name() expect ENODEV for a non
1970 /* Returns 'true' if there is a port with pmd netdev. */
1972 has_pmd_port(struct dp_netdev
*dp
)
1973 OVS_REQUIRES(dp
->port_mutex
)
1975 struct dp_netdev_port
*port
;
1977 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1978 if (netdev_is_pmd(port
->netdev
)) {
1987 do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*port
)
1988 OVS_REQUIRES(dp
->port_mutex
)
1990 hmap_remove(&dp
->ports
, &port
->node
);
1991 seq_change(dp
->port_seq
);
1993 reconfigure_datapath(dp
);
1999 answer_port_query(const struct dp_netdev_port
*port
,
2000 struct dpif_port
*dpif_port
)
2002 dpif_port
->name
= xstrdup(netdev_get_name(port
->netdev
));
2003 dpif_port
->type
= xstrdup(port
->type
);
2004 dpif_port
->port_no
= port
->port_no
;
2008 dpif_netdev_port_query_by_number(const struct dpif
*dpif
, odp_port_t port_no
,
2009 struct dpif_port
*dpif_port
)
2011 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2012 struct dp_netdev_port
*port
;
2015 ovs_mutex_lock(&dp
->port_mutex
);
2016 error
= get_port_by_number(dp
, port_no
, &port
);
2017 if (!error
&& dpif_port
) {
2018 answer_port_query(port
, dpif_port
);
2020 ovs_mutex_unlock(&dp
->port_mutex
);
2026 dpif_netdev_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
2027 struct dpif_port
*dpif_port
)
2029 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2030 struct dp_netdev_port
*port
;
2033 ovs_mutex_lock(&dp
->port_mutex
);
2034 error
= get_port_by_name(dp
, devname
, &port
);
2035 if (!error
&& dpif_port
) {
2036 answer_port_query(port
, dpif_port
);
2038 ovs_mutex_unlock(&dp
->port_mutex
);
2044 dp_netdev_flow_free(struct dp_netdev_flow
*flow
)
2046 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow
));
2050 static void dp_netdev_flow_unref(struct dp_netdev_flow
*flow
)
2052 if (ovs_refcount_unref_relaxed(&flow
->ref_cnt
) == 1) {
2053 ovsrcu_postpone(dp_netdev_flow_free
, flow
);
2058 dp_netdev_flow_hash(const ovs_u128
*ufid
)
2060 return ufid
->u32
[0];
2063 static inline struct dpcls
*
2064 dp_netdev_pmd_lookup_dpcls(struct dp_netdev_pmd_thread
*pmd
,
2068 uint32_t hash
= hash_port_no(in_port
);
2069 CMAP_FOR_EACH_WITH_HASH (cls
, node
, hash
, &pmd
->classifiers
) {
2070 if (cls
->in_port
== in_port
) {
2071 /* Port classifier exists already */
2078 static inline struct dpcls
*
2079 dp_netdev_pmd_find_dpcls(struct dp_netdev_pmd_thread
*pmd
,
2081 OVS_REQUIRES(pmd
->flow_mutex
)
2083 struct dpcls
*cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
2084 uint32_t hash
= hash_port_no(in_port
);
2087 /* Create new classifier for in_port */
2088 cls
= xmalloc(sizeof(*cls
));
2090 cls
->in_port
= in_port
;
2091 cmap_insert(&pmd
->classifiers
, &cls
->node
, hash
);
2092 VLOG_DBG("Creating dpcls %p for in_port %d", cls
, in_port
);
2097 #define MAX_FLOW_MARK (UINT32_MAX - 1)
2098 #define INVALID_FLOW_MARK (UINT32_MAX)
2100 struct megaflow_to_mark_data
{
2101 const struct cmap_node node
;
2107 struct cmap megaflow_to_mark
;
2108 struct cmap mark_to_flow
;
2109 struct id_pool
*pool
;
2112 static struct flow_mark flow_mark
= {
2113 .megaflow_to_mark
= CMAP_INITIALIZER
,
2114 .mark_to_flow
= CMAP_INITIALIZER
,
2118 flow_mark_alloc(void)
2122 if (!flow_mark
.pool
) {
2123 /* Haven't initiated yet, do it here */
2124 flow_mark
.pool
= id_pool_create(0, MAX_FLOW_MARK
);
2127 if (id_pool_alloc_id(flow_mark
.pool
, &mark
)) {
2131 return INVALID_FLOW_MARK
;
2135 flow_mark_free(uint32_t mark
)
2137 id_pool_free_id(flow_mark
.pool
, mark
);
2140 /* associate megaflow with a mark, which is a 1:1 mapping */
2142 megaflow_to_mark_associate(const ovs_u128
*mega_ufid
, uint32_t mark
)
2144 size_t hash
= dp_netdev_flow_hash(mega_ufid
);
2145 struct megaflow_to_mark_data
*data
= xzalloc(sizeof(*data
));
2147 data
->mega_ufid
= *mega_ufid
;
2150 cmap_insert(&flow_mark
.megaflow_to_mark
,
2151 CONST_CAST(struct cmap_node
*, &data
->node
), hash
);
2154 /* disassociate meagaflow with a mark */
2156 megaflow_to_mark_disassociate(const ovs_u128
*mega_ufid
)
2158 size_t hash
= dp_netdev_flow_hash(mega_ufid
);
2159 struct megaflow_to_mark_data
*data
;
2161 CMAP_FOR_EACH_WITH_HASH (data
, node
, hash
, &flow_mark
.megaflow_to_mark
) {
2162 if (ovs_u128_equals(*mega_ufid
, data
->mega_ufid
)) {
2163 cmap_remove(&flow_mark
.megaflow_to_mark
,
2164 CONST_CAST(struct cmap_node
*, &data
->node
), hash
);
2165 ovsrcu_postpone(free
, data
);
2170 VLOG_WARN("Masked ufid "UUID_FMT
" is not associated with a mark?\n",
2171 UUID_ARGS((struct uuid
*)mega_ufid
));
2174 static inline uint32_t
2175 megaflow_to_mark_find(const ovs_u128
*mega_ufid
)
2177 size_t hash
= dp_netdev_flow_hash(mega_ufid
);
2178 struct megaflow_to_mark_data
*data
;
2180 CMAP_FOR_EACH_WITH_HASH (data
, node
, hash
, &flow_mark
.megaflow_to_mark
) {
2181 if (ovs_u128_equals(*mega_ufid
, data
->mega_ufid
)) {
2186 VLOG_DBG("Mark id for ufid "UUID_FMT
" was not found\n",
2187 UUID_ARGS((struct uuid
*)mega_ufid
));
2188 return INVALID_FLOW_MARK
;
2191 /* associate mark with a flow, which is 1:N mapping */
2193 mark_to_flow_associate(const uint32_t mark
, struct dp_netdev_flow
*flow
)
2195 dp_netdev_flow_ref(flow
);
2197 cmap_insert(&flow_mark
.mark_to_flow
,
2198 CONST_CAST(struct cmap_node
*, &flow
->mark_node
),
2202 VLOG_DBG("Associated dp_netdev flow %p with mark %u\n", flow
, mark
);
2206 flow_mark_has_no_ref(uint32_t mark
)
2208 struct dp_netdev_flow
*flow
;
2210 CMAP_FOR_EACH_WITH_HASH (flow
, mark_node
, hash_int(mark
, 0),
2211 &flow_mark
.mark_to_flow
) {
2212 if (flow
->mark
== mark
) {
2221 mark_to_flow_disassociate(struct dp_netdev_pmd_thread
*pmd
,
2222 struct dp_netdev_flow
*flow
)
2225 uint32_t mark
= flow
->mark
;
2226 struct cmap_node
*mark_node
= CONST_CAST(struct cmap_node
*,
2229 cmap_remove(&flow_mark
.mark_to_flow
, mark_node
, hash_int(mark
, 0));
2230 flow
->mark
= INVALID_FLOW_MARK
;
2233 * no flow is referencing the mark any more? If so, let's
2234 * remove the flow from hardware and free the mark.
2236 if (flow_mark_has_no_ref(mark
)) {
2237 struct dp_netdev_port
*port
;
2238 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
2240 ovs_mutex_lock(&pmd
->dp
->port_mutex
);
2241 port
= dp_netdev_lookup_port(pmd
->dp
, in_port
);
2243 ret
= netdev_flow_del(port
->netdev
, &flow
->mega_ufid
, NULL
);
2245 ovs_mutex_unlock(&pmd
->dp
->port_mutex
);
2247 flow_mark_free(mark
);
2248 VLOG_DBG("Freed flow mark %u\n", mark
);
2250 megaflow_to_mark_disassociate(&flow
->mega_ufid
);
2252 dp_netdev_flow_unref(flow
);
2258 flow_mark_flush(struct dp_netdev_pmd_thread
*pmd
)
2260 struct dp_netdev_flow
*flow
;
2262 CMAP_FOR_EACH (flow
, mark_node
, &flow_mark
.mark_to_flow
) {
2263 if (flow
->pmd_id
== pmd
->core_id
) {
2264 queue_netdev_flow_del(pmd
, flow
);
2269 static struct dp_netdev_flow
*
2270 mark_to_flow_find(const struct dp_netdev_pmd_thread
*pmd
,
2271 const uint32_t mark
)
2273 struct dp_netdev_flow
*flow
;
2275 CMAP_FOR_EACH_WITH_HASH (flow
, mark_node
, hash_int(mark
, 0),
2276 &flow_mark
.mark_to_flow
) {
2277 if (flow
->mark
== mark
&& flow
->pmd_id
== pmd
->core_id
&&
2278 flow
->dead
== false) {
2286 static struct dp_flow_offload_item
*
2287 dp_netdev_alloc_flow_offload(struct dp_netdev_pmd_thread
*pmd
,
2288 struct dp_netdev_flow
*flow
,
2291 struct dp_flow_offload_item
*offload
;
2293 offload
= xzalloc(sizeof(*offload
));
2295 offload
->flow
= flow
;
2298 dp_netdev_flow_ref(flow
);
2299 dp_netdev_pmd_try_ref(pmd
);
2305 dp_netdev_free_flow_offload(struct dp_flow_offload_item
*offload
)
2307 dp_netdev_pmd_unref(offload
->pmd
);
2308 dp_netdev_flow_unref(offload
->flow
);
2310 free(offload
->actions
);
2315 dp_netdev_append_flow_offload(struct dp_flow_offload_item
*offload
)
2317 ovs_mutex_lock(&dp_flow_offload
.mutex
);
2318 ovs_list_push_back(&dp_flow_offload
.list
, &offload
->node
);
2319 xpthread_cond_signal(&dp_flow_offload
.cond
);
2320 ovs_mutex_unlock(&dp_flow_offload
.mutex
);
2324 dp_netdev_flow_offload_del(struct dp_flow_offload_item
*offload
)
2326 return mark_to_flow_disassociate(offload
->pmd
, offload
->flow
);
2330 * There are two flow offload operations here: addition and modification.
2332 * For flow addition, this function does:
2333 * - allocate a new flow mark id
2334 * - perform hardware flow offload
2335 * - associate the flow mark with flow and mega flow
2337 * For flow modification, both flow mark and the associations are still
2338 * valid, thus only item 2 needed.
2341 dp_netdev_flow_offload_put(struct dp_flow_offload_item
*offload
)
2343 struct dp_netdev_port
*port
;
2344 struct dp_netdev_pmd_thread
*pmd
= offload
->pmd
;
2345 struct dp_netdev_flow
*flow
= offload
->flow
;
2346 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
2347 bool modification
= offload
->op
== DP_NETDEV_FLOW_OFFLOAD_OP_MOD
;
2348 struct offload_info info
;
2358 ovs_assert(mark
!= INVALID_FLOW_MARK
);
2361 * If a mega flow has already been offloaded (from other PMD
2362 * instances), do not offload it again.
2364 mark
= megaflow_to_mark_find(&flow
->mega_ufid
);
2365 if (mark
!= INVALID_FLOW_MARK
) {
2366 VLOG_DBG("Flow has already been offloaded with mark %u\n", mark
);
2367 if (flow
->mark
!= INVALID_FLOW_MARK
) {
2368 ovs_assert(flow
->mark
== mark
);
2370 mark_to_flow_associate(mark
, flow
);
2375 mark
= flow_mark_alloc();
2376 if (mark
== INVALID_FLOW_MARK
) {
2377 VLOG_ERR("Failed to allocate flow mark!\n");
2380 info
.flow_mark
= mark
;
2382 ovs_mutex_lock(&pmd
->dp
->port_mutex
);
2383 port
= dp_netdev_lookup_port(pmd
->dp
, in_port
);
2385 ovs_mutex_unlock(&pmd
->dp
->port_mutex
);
2388 ret
= netdev_flow_put(port
->netdev
, &offload
->match
,
2389 CONST_CAST(struct nlattr
*, offload
->actions
),
2390 offload
->actions_len
, &flow
->mega_ufid
, &info
,
2392 ovs_mutex_unlock(&pmd
->dp
->port_mutex
);
2395 if (!modification
) {
2396 flow_mark_free(mark
);
2398 mark_to_flow_disassociate(pmd
, flow
);
2403 if (!modification
) {
2404 megaflow_to_mark_associate(&flow
->mega_ufid
, mark
);
2405 mark_to_flow_associate(mark
, flow
);
2412 dp_netdev_flow_offload_main(void *data OVS_UNUSED
)
2414 struct dp_flow_offload_item
*offload
;
2415 struct ovs_list
*list
;
2420 ovs_mutex_lock(&dp_flow_offload
.mutex
);
2421 if (ovs_list_is_empty(&dp_flow_offload
.list
)) {
2422 ovsrcu_quiesce_start();
2423 ovs_mutex_cond_wait(&dp_flow_offload
.cond
,
2424 &dp_flow_offload
.mutex
);
2425 ovsrcu_quiesce_end();
2427 list
= ovs_list_pop_front(&dp_flow_offload
.list
);
2428 offload
= CONTAINER_OF(list
, struct dp_flow_offload_item
, node
);
2429 ovs_mutex_unlock(&dp_flow_offload
.mutex
);
2431 switch (offload
->op
) {
2432 case DP_NETDEV_FLOW_OFFLOAD_OP_ADD
:
2434 ret
= dp_netdev_flow_offload_put(offload
);
2436 case DP_NETDEV_FLOW_OFFLOAD_OP_MOD
:
2438 ret
= dp_netdev_flow_offload_put(offload
);
2440 case DP_NETDEV_FLOW_OFFLOAD_OP_DEL
:
2442 ret
= dp_netdev_flow_offload_del(offload
);
2448 VLOG_DBG("%s to %s netdev flow\n",
2449 ret
== 0 ? "succeed" : "failed", op
);
2450 dp_netdev_free_flow_offload(offload
);
2457 queue_netdev_flow_del(struct dp_netdev_pmd_thread
*pmd
,
2458 struct dp_netdev_flow
*flow
)
2460 struct dp_flow_offload_item
*offload
;
2462 if (ovsthread_once_start(&offload_thread_once
)) {
2463 xpthread_cond_init(&dp_flow_offload
.cond
, NULL
);
2464 ovs_thread_create("dp_netdev_flow_offload",
2465 dp_netdev_flow_offload_main
, NULL
);
2466 ovsthread_once_done(&offload_thread_once
);
2469 offload
= dp_netdev_alloc_flow_offload(pmd
, flow
,
2470 DP_NETDEV_FLOW_OFFLOAD_OP_DEL
);
2471 dp_netdev_append_flow_offload(offload
);
2475 queue_netdev_flow_put(struct dp_netdev_pmd_thread
*pmd
,
2476 struct dp_netdev_flow
*flow
, struct match
*match
,
2477 const struct nlattr
*actions
, size_t actions_len
)
2479 struct dp_flow_offload_item
*offload
;
2482 if (!netdev_is_flow_api_enabled()) {
2486 if (ovsthread_once_start(&offload_thread_once
)) {
2487 xpthread_cond_init(&dp_flow_offload
.cond
, NULL
);
2488 ovs_thread_create("dp_netdev_flow_offload",
2489 dp_netdev_flow_offload_main
, NULL
);
2490 ovsthread_once_done(&offload_thread_once
);
2493 if (flow
->mark
!= INVALID_FLOW_MARK
) {
2494 op
= DP_NETDEV_FLOW_OFFLOAD_OP_MOD
;
2496 op
= DP_NETDEV_FLOW_OFFLOAD_OP_ADD
;
2498 offload
= dp_netdev_alloc_flow_offload(pmd
, flow
, op
);
2499 offload
->match
= *match
;
2500 offload
->actions
= xmalloc(actions_len
);
2501 memcpy(offload
->actions
, actions
, actions_len
);
2502 offload
->actions_len
= actions_len
;
2504 dp_netdev_append_flow_offload(offload
);
2508 dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread
*pmd
,
2509 struct dp_netdev_flow
*flow
)
2510 OVS_REQUIRES(pmd
->flow_mutex
)
2512 struct cmap_node
*node
= CONST_CAST(struct cmap_node
*, &flow
->node
);
2514 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
2516 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
2517 ovs_assert(cls
!= NULL
);
2518 dpcls_remove(cls
, &flow
->cr
);
2519 cmap_remove(&pmd
->flow_table
, node
, dp_netdev_flow_hash(&flow
->ufid
));
2520 if (flow
->mark
!= INVALID_FLOW_MARK
) {
2521 queue_netdev_flow_del(pmd
, flow
);
2525 dp_netdev_flow_unref(flow
);
2529 dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
)
2531 struct dp_netdev_flow
*netdev_flow
;
2533 ovs_mutex_lock(&pmd
->flow_mutex
);
2534 CMAP_FOR_EACH (netdev_flow
, node
, &pmd
->flow_table
) {
2535 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
2537 ovs_mutex_unlock(&pmd
->flow_mutex
);
2541 dpif_netdev_flow_flush(struct dpif
*dpif
)
2543 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2544 struct dp_netdev_pmd_thread
*pmd
;
2546 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2547 dp_netdev_pmd_flow_flush(pmd
);
2553 struct dp_netdev_port_state
{
2554 struct hmap_position position
;
2559 dpif_netdev_port_dump_start(const struct dpif
*dpif OVS_UNUSED
, void **statep
)
2561 *statep
= xzalloc(sizeof(struct dp_netdev_port_state
));
2566 dpif_netdev_port_dump_next(const struct dpif
*dpif
, void *state_
,
2567 struct dpif_port
*dpif_port
)
2569 struct dp_netdev_port_state
*state
= state_
;
2570 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2571 struct hmap_node
*node
;
2574 ovs_mutex_lock(&dp
->port_mutex
);
2575 node
= hmap_at_position(&dp
->ports
, &state
->position
);
2577 struct dp_netdev_port
*port
;
2579 port
= CONTAINER_OF(node
, struct dp_netdev_port
, node
);
2582 state
->name
= xstrdup(netdev_get_name(port
->netdev
));
2583 dpif_port
->name
= state
->name
;
2584 dpif_port
->type
= port
->type
;
2585 dpif_port
->port_no
= port
->port_no
;
2591 ovs_mutex_unlock(&dp
->port_mutex
);
2597 dpif_netdev_port_dump_done(const struct dpif
*dpif OVS_UNUSED
, void *state_
)
2599 struct dp_netdev_port_state
*state
= state_
;
2606 dpif_netdev_port_poll(const struct dpif
*dpif_
, char **devnamep OVS_UNUSED
)
2608 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
2609 uint64_t new_port_seq
;
2612 new_port_seq
= seq_read(dpif
->dp
->port_seq
);
2613 if (dpif
->last_port_seq
!= new_port_seq
) {
2614 dpif
->last_port_seq
= new_port_seq
;
2624 dpif_netdev_port_poll_wait(const struct dpif
*dpif_
)
2626 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
2628 seq_wait(dpif
->dp
->port_seq
, dpif
->last_port_seq
);
2631 static struct dp_netdev_flow
*
2632 dp_netdev_flow_cast(const struct dpcls_rule
*cr
)
2634 return cr
? CONTAINER_OF(cr
, struct dp_netdev_flow
, cr
) : NULL
;
2637 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*flow
)
2639 return ovs_refcount_try_ref_rcu(&flow
->ref_cnt
);
2642 /* netdev_flow_key utilities.
2644 * netdev_flow_key is basically a miniflow. We use these functions
2645 * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow
2646 * functions (miniflow_clone_inline, miniflow_equal, ...), because:
2648 * - Since we are dealing exclusively with miniflows created by
2649 * miniflow_extract(), if the map is different the miniflow is different.
2650 * Therefore we can be faster by comparing the map and the miniflow in a
2652 * - These functions can be inlined by the compiler. */
2654 /* Given the number of bits set in miniflow's maps, returns the size of the
2655 * 'netdev_flow_key.mf' */
2656 static inline size_t
2657 netdev_flow_key_size(size_t flow_u64s
)
2659 return sizeof(struct miniflow
) + MINIFLOW_VALUES_SIZE(flow_u64s
);
2663 netdev_flow_key_equal(const struct netdev_flow_key
*a
,
2664 const struct netdev_flow_key
*b
)
2666 /* 'b->len' may be not set yet. */
2667 return a
->hash
== b
->hash
&& !memcmp(&a
->mf
, &b
->mf
, a
->len
);
2670 /* Used to compare 'netdev_flow_key' in the exact match cache to a miniflow.
2671 * The maps are compared bitwise, so both 'key->mf' and 'mf' must have been
2672 * generated by miniflow_extract. */
2674 netdev_flow_key_equal_mf(const struct netdev_flow_key
*key
,
2675 const struct miniflow
*mf
)
2677 return !memcmp(&key
->mf
, mf
, key
->len
);
2681 netdev_flow_key_clone(struct netdev_flow_key
*dst
,
2682 const struct netdev_flow_key
*src
)
2685 offsetof(struct netdev_flow_key
, mf
) + src
->len
);
2688 /* Initialize a netdev_flow_key 'mask' from 'match'. */
2690 netdev_flow_mask_init(struct netdev_flow_key
*mask
,
2691 const struct match
*match
)
2693 uint64_t *dst
= miniflow_values(&mask
->mf
);
2694 struct flowmap fmap
;
2698 /* Only check masks that make sense for the flow. */
2699 flow_wc_map(&match
->flow
, &fmap
);
2700 flowmap_init(&mask
->mf
.map
);
2702 FLOWMAP_FOR_EACH_INDEX(idx
, fmap
) {
2703 uint64_t mask_u64
= flow_u64_value(&match
->wc
.masks
, idx
);
2706 flowmap_set(&mask
->mf
.map
, idx
, 1);
2708 hash
= hash_add64(hash
, mask_u64
);
2714 FLOWMAP_FOR_EACH_MAP (map
, mask
->mf
.map
) {
2715 hash
= hash_add64(hash
, map
);
2718 size_t n
= dst
- miniflow_get_values(&mask
->mf
);
2720 mask
->hash
= hash_finish(hash
, n
* 8);
2721 mask
->len
= netdev_flow_key_size(n
);
2724 /* Initializes 'dst' as a copy of 'flow' masked with 'mask'. */
2726 netdev_flow_key_init_masked(struct netdev_flow_key
*dst
,
2727 const struct flow
*flow
,
2728 const struct netdev_flow_key
*mask
)
2730 uint64_t *dst_u64
= miniflow_values(&dst
->mf
);
2731 const uint64_t *mask_u64
= miniflow_get_values(&mask
->mf
);
2735 dst
->len
= mask
->len
;
2736 dst
->mf
= mask
->mf
; /* Copy maps. */
2738 FLOW_FOR_EACH_IN_MAPS(value
, flow
, mask
->mf
.map
) {
2739 *dst_u64
= value
& *mask_u64
++;
2740 hash
= hash_add64(hash
, *dst_u64
++);
2742 dst
->hash
= hash_finish(hash
,
2743 (dst_u64
- miniflow_get_values(&dst
->mf
)) * 8);
2746 /* Iterate through netdev_flow_key TNL u64 values specified by 'FLOWMAP'. */
2747 #define NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(VALUE, KEY, FLOWMAP) \
2748 MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, &(KEY)->mf, FLOWMAP)
2750 /* Returns a hash value for the bits of 'key' where there are 1-bits in
2752 static inline uint32_t
2753 netdev_flow_key_hash_in_mask(const struct netdev_flow_key
*key
,
2754 const struct netdev_flow_key
*mask
)
2756 const uint64_t *p
= miniflow_get_values(&mask
->mf
);
2760 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, key
, mask
->mf
.map
) {
2761 hash
= hash_add64(hash
, value
& *p
++);
2764 return hash_finish(hash
, (p
- miniflow_get_values(&mask
->mf
)) * 8);
2768 emc_entry_alive(struct emc_entry
*ce
)
2770 return ce
->flow
&& !ce
->flow
->dead
;
2774 emc_clear_entry(struct emc_entry
*ce
)
2777 dp_netdev_flow_unref(ce
->flow
);
2783 emc_change_entry(struct emc_entry
*ce
, struct dp_netdev_flow
*flow
,
2784 const struct netdev_flow_key
*key
)
2786 if (ce
->flow
!= flow
) {
2788 dp_netdev_flow_unref(ce
->flow
);
2791 if (dp_netdev_flow_ref(flow
)) {
2798 netdev_flow_key_clone(&ce
->key
, key
);
2803 emc_insert(struct emc_cache
*cache
, const struct netdev_flow_key
*key
,
2804 struct dp_netdev_flow
*flow
)
2806 struct emc_entry
*to_be_replaced
= NULL
;
2807 struct emc_entry
*current_entry
;
2809 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
2810 if (netdev_flow_key_equal(¤t_entry
->key
, key
)) {
2811 /* We found the entry with the 'mf' miniflow */
2812 emc_change_entry(current_entry
, flow
, NULL
);
2816 /* Replacement policy: put the flow in an empty (not alive) entry, or
2817 * in the first entry where it can be */
2819 || (emc_entry_alive(to_be_replaced
)
2820 && !emc_entry_alive(current_entry
))
2821 || current_entry
->key
.hash
< to_be_replaced
->key
.hash
) {
2822 to_be_replaced
= current_entry
;
2825 /* We didn't find the miniflow in the cache.
2826 * The 'to_be_replaced' entry is where the new flow will be stored */
2828 emc_change_entry(to_be_replaced
, flow
, key
);
2832 emc_probabilistic_insert(struct dp_netdev_pmd_thread
*pmd
,
2833 const struct netdev_flow_key
*key
,
2834 struct dp_netdev_flow
*flow
)
2836 /* Insert an entry into the EMC based on probability value 'min'. By
2837 * default the value is UINT32_MAX / 100 which yields an insertion
2838 * probability of 1/100 ie. 1% */
2840 uint32_t min
= pmd
->ctx
.emc_insert_min
;
2842 if (min
&& random_uint32() <= min
) {
2843 emc_insert(&(pmd
->flow_cache
).emc_cache
, key
, flow
);
2847 static inline struct dp_netdev_flow
*
2848 emc_lookup(struct emc_cache
*cache
, const struct netdev_flow_key
*key
)
2850 struct emc_entry
*current_entry
;
2852 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
2853 if (current_entry
->key
.hash
== key
->hash
2854 && emc_entry_alive(current_entry
)
2855 && netdev_flow_key_equal_mf(¤t_entry
->key
, &key
->mf
)) {
2857 /* We found the entry with the 'key->mf' miniflow */
2858 return current_entry
->flow
;
2865 static inline const struct cmap_node
*
2866 smc_entry_get(struct dp_netdev_pmd_thread
*pmd
, const uint32_t hash
)
2868 struct smc_cache
*cache
= &(pmd
->flow_cache
).smc_cache
;
2869 struct smc_bucket
*bucket
= &cache
->buckets
[hash
& SMC_MASK
];
2870 uint16_t sig
= hash
>> 16;
2871 uint16_t index
= UINT16_MAX
;
2873 for (int i
= 0; i
< SMC_ENTRY_PER_BUCKET
; i
++) {
2874 if (bucket
->sig
[i
] == sig
) {
2875 index
= bucket
->flow_idx
[i
];
2879 if (index
!= UINT16_MAX
) {
2880 return cmap_find_by_index(&pmd
->flow_table
, index
);
2886 smc_clear_entry(struct smc_bucket
*b
, int idx
)
2888 b
->flow_idx
[idx
] = UINT16_MAX
;
2891 /* Insert the flow_table index into SMC. Insertion may fail when 1) SMC is
2892 * turned off, 2) the flow_table index is larger than uint16_t can handle.
2893 * If there is already an SMC entry having same signature, the index will be
2894 * updated. If there is no existing entry, but an empty entry is available,
2895 * the empty entry will be taken. If no empty entry or existing same signature,
2896 * a random entry from the hashed bucket will be picked. */
2898 smc_insert(struct dp_netdev_pmd_thread
*pmd
,
2899 const struct netdev_flow_key
*key
,
2902 struct smc_cache
*smc_cache
= &(pmd
->flow_cache
).smc_cache
;
2903 struct smc_bucket
*bucket
= &smc_cache
->buckets
[key
->hash
& SMC_MASK
];
2905 uint32_t cmap_index
;
2909 atomic_read_relaxed(&pmd
->dp
->smc_enable_db
, &smc_enable_db
);
2910 if (!smc_enable_db
) {
2914 cmap_index
= cmap_find_index(&pmd
->flow_table
, hash
);
2915 index
= (cmap_index
>= UINT16_MAX
) ? UINT16_MAX
: (uint16_t)cmap_index
;
2917 /* If the index is larger than SMC can handle (uint16_t), we don't
2919 if (index
== UINT16_MAX
) {
2923 /* If an entry with same signature already exists, update the index */
2924 uint16_t sig
= key
->hash
>> 16;
2925 for (i
= 0; i
< SMC_ENTRY_PER_BUCKET
; i
++) {
2926 if (bucket
->sig
[i
] == sig
) {
2927 bucket
->flow_idx
[i
] = index
;
2931 /* If there is an empty entry, occupy it. */
2932 for (i
= 0; i
< SMC_ENTRY_PER_BUCKET
; i
++) {
2933 if (bucket
->flow_idx
[i
] == UINT16_MAX
) {
2934 bucket
->sig
[i
] = sig
;
2935 bucket
->flow_idx
[i
] = index
;
2939 /* Otherwise, pick a random entry. */
2940 i
= random_uint32() % SMC_ENTRY_PER_BUCKET
;
2941 bucket
->sig
[i
] = sig
;
2942 bucket
->flow_idx
[i
] = index
;
2945 static struct dp_netdev_flow
*
2946 dp_netdev_pmd_lookup_flow(struct dp_netdev_pmd_thread
*pmd
,
2947 const struct netdev_flow_key
*key
,
2951 struct dpcls_rule
*rule
;
2952 odp_port_t in_port
= u32_to_odp(MINIFLOW_GET_U32(&key
->mf
,
2954 struct dp_netdev_flow
*netdev_flow
= NULL
;
2956 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
2957 if (OVS_LIKELY(cls
)) {
2958 dpcls_lookup(cls
, &key
, &rule
, 1, lookup_num_p
);
2959 netdev_flow
= dp_netdev_flow_cast(rule
);
2964 static struct dp_netdev_flow
*
2965 dp_netdev_pmd_find_flow(const struct dp_netdev_pmd_thread
*pmd
,
2966 const ovs_u128
*ufidp
, const struct nlattr
*key
,
2969 struct dp_netdev_flow
*netdev_flow
;
2973 /* If a UFID is not provided, determine one based on the key. */
2974 if (!ufidp
&& key
&& key_len
2975 && !dpif_netdev_flow_from_nlattrs(key
, key_len
, &flow
, false)) {
2976 dpif_flow_hash(pmd
->dp
->dpif
, &flow
, sizeof flow
, &ufid
);
2981 CMAP_FOR_EACH_WITH_HASH (netdev_flow
, node
, dp_netdev_flow_hash(ufidp
),
2983 if (ovs_u128_equals(netdev_flow
->ufid
, *ufidp
)) {
2993 get_dpif_flow_stats(const struct dp_netdev_flow
*netdev_flow_
,
2994 struct dpif_flow_stats
*stats
)
2996 struct dp_netdev_flow
*netdev_flow
;
2997 unsigned long long n
;
3001 netdev_flow
= CONST_CAST(struct dp_netdev_flow
*, netdev_flow_
);
3003 atomic_read_relaxed(&netdev_flow
->stats
.packet_count
, &n
);
3004 stats
->n_packets
= n
;
3005 atomic_read_relaxed(&netdev_flow
->stats
.byte_count
, &n
);
3007 atomic_read_relaxed(&netdev_flow
->stats
.used
, &used
);
3009 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
3010 stats
->tcp_flags
= flags
;
3013 /* Converts to the dpif_flow format, using 'key_buf' and 'mask_buf' for
3014 * storing the netlink-formatted key/mask. 'key_buf' may be the same as
3015 * 'mask_buf'. Actions will be returned without copying, by relying on RCU to
3018 dp_netdev_flow_to_dpif_flow(const struct dp_netdev_flow
*netdev_flow
,
3019 struct ofpbuf
*key_buf
, struct ofpbuf
*mask_buf
,
3020 struct dpif_flow
*flow
, bool terse
)
3023 memset(flow
, 0, sizeof *flow
);
3025 struct flow_wildcards wc
;
3026 struct dp_netdev_actions
*actions
;
3028 struct odp_flow_key_parms odp_parms
= {
3029 .flow
= &netdev_flow
->flow
,
3031 .support
= dp_netdev_support
,
3034 miniflow_expand(&netdev_flow
->cr
.mask
->mf
, &wc
.masks
);
3035 /* in_port is exact matched, but we have left it out from the mask for
3036 * optimnization reasons. Add in_port back to the mask. */
3037 wc
.masks
.in_port
.odp_port
= ODPP_NONE
;
3040 offset
= key_buf
->size
;
3041 flow
->key
= ofpbuf_tail(key_buf
);
3042 odp_flow_key_from_flow(&odp_parms
, key_buf
);
3043 flow
->key_len
= key_buf
->size
- offset
;
3046 offset
= mask_buf
->size
;
3047 flow
->mask
= ofpbuf_tail(mask_buf
);
3048 odp_parms
.key_buf
= key_buf
;
3049 odp_flow_key_from_mask(&odp_parms
, mask_buf
);
3050 flow
->mask_len
= mask_buf
->size
- offset
;
3053 actions
= dp_netdev_flow_get_actions(netdev_flow
);
3054 flow
->actions
= actions
->actions
;
3055 flow
->actions_len
= actions
->size
;
3058 flow
->ufid
= netdev_flow
->ufid
;
3059 flow
->ufid_present
= true;
3060 flow
->pmd_id
= netdev_flow
->pmd_id
;
3061 get_dpif_flow_stats(netdev_flow
, &flow
->stats
);
3063 flow
->attrs
.offloaded
= false;
3064 flow
->attrs
.dp_layer
= "ovs";
3068 dpif_netdev_mask_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
3069 const struct nlattr
*mask_key
,
3070 uint32_t mask_key_len
, const struct flow
*flow
,
3071 struct flow_wildcards
*wc
, bool probe
)
3073 enum odp_key_fitness fitness
;
3075 fitness
= odp_flow_key_to_mask(mask_key
, mask_key_len
, wc
, flow
, NULL
);
3078 /* This should not happen: it indicates that
3079 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
3080 * disagree on the acceptable form of a mask. Log the problem
3081 * as an error, with enough details to enable debugging. */
3082 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3084 if (!VLOG_DROP_ERR(&rl
)) {
3088 odp_flow_format(key
, key_len
, mask_key
, mask_key_len
, NULL
, &s
,
3090 VLOG_ERR("internal error parsing flow mask %s (%s)",
3091 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
3103 dpif_netdev_flow_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
3104 struct flow
*flow
, bool probe
)
3106 if (odp_flow_key_to_flow(key
, key_len
, flow
, NULL
)) {
3108 /* This should not happen: it indicates that
3109 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
3110 * the acceptable form of a flow. Log the problem as an error,
3111 * with enough details to enable debugging. */
3112 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3114 if (!VLOG_DROP_ERR(&rl
)) {
3118 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
3119 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
3127 if (flow
->ct_state
& DP_NETDEV_CS_UNSUPPORTED_MASK
) {
3135 dpif_netdev_flow_get(const struct dpif
*dpif
, const struct dpif_flow_get
*get
)
3137 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3138 struct dp_netdev_flow
*netdev_flow
;
3139 struct dp_netdev_pmd_thread
*pmd
;
3140 struct hmapx to_find
= HMAPX_INITIALIZER(&to_find
);
3141 struct hmapx_node
*node
;
3144 if (get
->pmd_id
== PMD_ID_NULL
) {
3145 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3146 if (dp_netdev_pmd_try_ref(pmd
) && !hmapx_add(&to_find
, pmd
)) {
3147 dp_netdev_pmd_unref(pmd
);
3151 pmd
= dp_netdev_get_pmd(dp
, get
->pmd_id
);
3155 hmapx_add(&to_find
, pmd
);
3158 if (!hmapx_count(&to_find
)) {
3162 HMAPX_FOR_EACH (node
, &to_find
) {
3163 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3164 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, get
->ufid
, get
->key
,
3167 dp_netdev_flow_to_dpif_flow(netdev_flow
, get
->buffer
, get
->buffer
,
3176 HMAPX_FOR_EACH (node
, &to_find
) {
3177 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3178 dp_netdev_pmd_unref(pmd
);
3181 hmapx_destroy(&to_find
);
3186 dp_netdev_get_mega_ufid(const struct match
*match
, ovs_u128
*mega_ufid
)
3188 struct flow masked_flow
;
3191 for (i
= 0; i
< sizeof(struct flow
); i
++) {
3192 ((uint8_t *)&masked_flow
)[i
] = ((uint8_t *)&match
->flow
)[i
] &
3193 ((uint8_t *)&match
->wc
)[i
];
3195 dpif_flow_hash(NULL
, &masked_flow
, sizeof(struct flow
), mega_ufid
);
3198 static struct dp_netdev_flow
*
3199 dp_netdev_flow_add(struct dp_netdev_pmd_thread
*pmd
,
3200 struct match
*match
, const ovs_u128
*ufid
,
3201 const struct nlattr
*actions
, size_t actions_len
)
3202 OVS_REQUIRES(pmd
->flow_mutex
)
3204 struct dp_netdev_flow
*flow
;
3205 struct netdev_flow_key mask
;
3208 /* Make sure in_port is exact matched before we read it. */
3209 ovs_assert(match
->wc
.masks
.in_port
.odp_port
== ODPP_NONE
);
3210 odp_port_t in_port
= match
->flow
.in_port
.odp_port
;
3212 /* As we select the dpcls based on the port number, each netdev flow
3213 * belonging to the same dpcls will have the same odp_port value.
3214 * For performance reasons we wildcard odp_port here in the mask. In the
3215 * typical case dp_hash is also wildcarded, and the resulting 8-byte
3216 * chunk {dp_hash, in_port} will be ignored by netdev_flow_mask_init() and
3217 * will not be part of the subtable mask.
3218 * This will speed up the hash computation during dpcls_lookup() because
3219 * there is one less call to hash_add64() in this case. */
3220 match
->wc
.masks
.in_port
.odp_port
= 0;
3221 netdev_flow_mask_init(&mask
, match
);
3222 match
->wc
.masks
.in_port
.odp_port
= ODPP_NONE
;
3224 /* Make sure wc does not have metadata. */
3225 ovs_assert(!FLOWMAP_HAS_FIELD(&mask
.mf
.map
, metadata
)
3226 && !FLOWMAP_HAS_FIELD(&mask
.mf
.map
, regs
));
3228 /* Do not allocate extra space. */
3229 flow
= xmalloc(sizeof *flow
- sizeof flow
->cr
.flow
.mf
+ mask
.len
);
3230 memset(&flow
->stats
, 0, sizeof flow
->stats
);
3233 flow
->mark
= INVALID_FLOW_MARK
;
3234 *CONST_CAST(unsigned *, &flow
->pmd_id
) = pmd
->core_id
;
3235 *CONST_CAST(struct flow
*, &flow
->flow
) = match
->flow
;
3236 *CONST_CAST(ovs_u128
*, &flow
->ufid
) = *ufid
;
3237 ovs_refcount_init(&flow
->ref_cnt
);
3238 ovsrcu_set(&flow
->actions
, dp_netdev_actions_create(actions
, actions_len
));
3240 dp_netdev_get_mega_ufid(match
, CONST_CAST(ovs_u128
*, &flow
->mega_ufid
));
3241 netdev_flow_key_init_masked(&flow
->cr
.flow
, &match
->flow
, &mask
);
3243 /* Select dpcls for in_port. Relies on in_port to be exact match. */
3244 cls
= dp_netdev_pmd_find_dpcls(pmd
, in_port
);
3245 dpcls_insert(cls
, &flow
->cr
, &mask
);
3247 cmap_insert(&pmd
->flow_table
, CONST_CAST(struct cmap_node
*, &flow
->node
),
3248 dp_netdev_flow_hash(&flow
->ufid
));
3250 queue_netdev_flow_put(pmd
, flow
, match
, actions
, actions_len
);
3252 if (OVS_UNLIKELY(!VLOG_DROP_DBG((&upcall_rl
)))) {
3253 struct ds ds
= DS_EMPTY_INITIALIZER
;
3254 struct ofpbuf key_buf
, mask_buf
;
3255 struct odp_flow_key_parms odp_parms
= {
3256 .flow
= &match
->flow
,
3257 .mask
= &match
->wc
.masks
,
3258 .support
= dp_netdev_support
,
3261 ofpbuf_init(&key_buf
, 0);
3262 ofpbuf_init(&mask_buf
, 0);
3264 odp_flow_key_from_flow(&odp_parms
, &key_buf
);
3265 odp_parms
.key_buf
= &key_buf
;
3266 odp_flow_key_from_mask(&odp_parms
, &mask_buf
);
3268 ds_put_cstr(&ds
, "flow_add: ");
3269 odp_format_ufid(ufid
, &ds
);
3270 ds_put_cstr(&ds
, " ");
3271 odp_flow_format(key_buf
.data
, key_buf
.size
,
3272 mask_buf
.data
, mask_buf
.size
,
3274 ds_put_cstr(&ds
, ", actions:");
3275 format_odp_actions(&ds
, actions
, actions_len
, NULL
);
3277 VLOG_DBG("%s", ds_cstr(&ds
));
3279 ofpbuf_uninit(&key_buf
);
3280 ofpbuf_uninit(&mask_buf
);
3282 /* Add a printout of the actual match installed. */
3285 ds_put_cstr(&ds
, "flow match: ");
3286 miniflow_expand(&flow
->cr
.flow
.mf
, &m
.flow
);
3287 miniflow_expand(&flow
->cr
.mask
->mf
, &m
.wc
.masks
);
3288 memset(&m
.tun_md
, 0, sizeof m
.tun_md
);
3289 match_format(&m
, NULL
, &ds
, OFP_DEFAULT_PRIORITY
);
3291 VLOG_DBG("%s", ds_cstr(&ds
));
3300 flow_put_on_pmd(struct dp_netdev_pmd_thread
*pmd
,
3301 struct netdev_flow_key
*key
,
3302 struct match
*match
,
3304 const struct dpif_flow_put
*put
,
3305 struct dpif_flow_stats
*stats
)
3307 struct dp_netdev_flow
*netdev_flow
;
3311 memset(stats
, 0, sizeof *stats
);
3314 ovs_mutex_lock(&pmd
->flow_mutex
);
3315 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
, NULL
);
3317 if (put
->flags
& DPIF_FP_CREATE
) {
3318 if (cmap_count(&pmd
->flow_table
) < MAX_FLOWS
) {
3319 dp_netdev_flow_add(pmd
, match
, ufid
, put
->actions
,
3329 if (put
->flags
& DPIF_FP_MODIFY
) {
3330 struct dp_netdev_actions
*new_actions
;
3331 struct dp_netdev_actions
*old_actions
;
3333 new_actions
= dp_netdev_actions_create(put
->actions
,
3336 old_actions
= dp_netdev_flow_get_actions(netdev_flow
);
3337 ovsrcu_set(&netdev_flow
->actions
, new_actions
);
3339 queue_netdev_flow_put(pmd
, netdev_flow
, match
,
3340 put
->actions
, put
->actions_len
);
3343 get_dpif_flow_stats(netdev_flow
, stats
);
3345 if (put
->flags
& DPIF_FP_ZERO_STATS
) {
3346 /* XXX: The userspace datapath uses thread local statistics
3347 * (for flows), which should be updated only by the owning
3348 * thread. Since we cannot write on stats memory here,
3349 * we choose not to support this flag. Please note:
3350 * - This feature is currently used only by dpctl commands with
3352 * - Should the need arise, this operation can be implemented
3353 * by keeping a base value (to be update here) for each
3354 * counter, and subtracting it before outputting the stats */
3358 ovsrcu_postpone(dp_netdev_actions_free
, old_actions
);
3359 } else if (put
->flags
& DPIF_FP_CREATE
) {
3362 /* Overlapping flow. */
3366 ovs_mutex_unlock(&pmd
->flow_mutex
);
3371 dpif_netdev_flow_put(struct dpif
*dpif
, const struct dpif_flow_put
*put
)
3373 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3374 struct netdev_flow_key key
, mask
;
3375 struct dp_netdev_pmd_thread
*pmd
;
3379 bool probe
= put
->flags
& DPIF_FP_PROBE
;
3382 memset(put
->stats
, 0, sizeof *put
->stats
);
3384 error
= dpif_netdev_flow_from_nlattrs(put
->key
, put
->key_len
, &match
.flow
,
3389 error
= dpif_netdev_mask_from_nlattrs(put
->key
, put
->key_len
,
3390 put
->mask
, put
->mask_len
,
3391 &match
.flow
, &match
.wc
, probe
);
3399 dpif_flow_hash(dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
3402 /* The Netlink encoding of datapath flow keys cannot express
3403 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
3404 * tag is interpreted as exact match on the fact that there is no
3405 * VLAN. Unless we refactor a lot of code that translates between
3406 * Netlink and struct flow representations, we have to do the same
3407 * here. This must be in sync with 'match' in handle_packet_upcall(). */
3408 if (!match
.wc
.masks
.vlans
[0].tci
) {
3409 match
.wc
.masks
.vlans
[0].tci
= htons(0xffff);
3412 /* Must produce a netdev_flow_key for lookup.
3413 * Use the same method as employed to create the key when adding
3414 * the flow to the dplcs to make sure they match. */
3415 netdev_flow_mask_init(&mask
, &match
);
3416 netdev_flow_key_init_masked(&key
, &match
.flow
, &mask
);
3418 if (put
->pmd_id
== PMD_ID_NULL
) {
3419 if (cmap_count(&dp
->poll_threads
) == 0) {
3422 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3423 struct dpif_flow_stats pmd_stats
;
3426 pmd_error
= flow_put_on_pmd(pmd
, &key
, &match
, &ufid
, put
,
3430 } else if (put
->stats
) {
3431 put
->stats
->n_packets
+= pmd_stats
.n_packets
;
3432 put
->stats
->n_bytes
+= pmd_stats
.n_bytes
;
3433 put
->stats
->used
= MAX(put
->stats
->used
, pmd_stats
.used
);
3434 put
->stats
->tcp_flags
|= pmd_stats
.tcp_flags
;
3438 pmd
= dp_netdev_get_pmd(dp
, put
->pmd_id
);
3442 error
= flow_put_on_pmd(pmd
, &key
, &match
, &ufid
, put
, put
->stats
);
3443 dp_netdev_pmd_unref(pmd
);
3450 flow_del_on_pmd(struct dp_netdev_pmd_thread
*pmd
,
3451 struct dpif_flow_stats
*stats
,
3452 const struct dpif_flow_del
*del
)
3454 struct dp_netdev_flow
*netdev_flow
;
3457 ovs_mutex_lock(&pmd
->flow_mutex
);
3458 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, del
->ufid
, del
->key
,
3462 get_dpif_flow_stats(netdev_flow
, stats
);
3464 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
3468 ovs_mutex_unlock(&pmd
->flow_mutex
);
3474 dpif_netdev_flow_del(struct dpif
*dpif
, const struct dpif_flow_del
*del
)
3476 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3477 struct dp_netdev_pmd_thread
*pmd
;
3481 memset(del
->stats
, 0, sizeof *del
->stats
);
3484 if (del
->pmd_id
== PMD_ID_NULL
) {
3485 if (cmap_count(&dp
->poll_threads
) == 0) {
3488 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3489 struct dpif_flow_stats pmd_stats
;
3492 pmd_error
= flow_del_on_pmd(pmd
, &pmd_stats
, del
);
3495 } else if (del
->stats
) {
3496 del
->stats
->n_packets
+= pmd_stats
.n_packets
;
3497 del
->stats
->n_bytes
+= pmd_stats
.n_bytes
;
3498 del
->stats
->used
= MAX(del
->stats
->used
, pmd_stats
.used
);
3499 del
->stats
->tcp_flags
|= pmd_stats
.tcp_flags
;
3503 pmd
= dp_netdev_get_pmd(dp
, del
->pmd_id
);
3507 error
= flow_del_on_pmd(pmd
, del
->stats
, del
);
3508 dp_netdev_pmd_unref(pmd
);
3515 struct dpif_netdev_flow_dump
{
3516 struct dpif_flow_dump up
;
3517 struct cmap_position poll_thread_pos
;
3518 struct cmap_position flow_pos
;
3519 struct dp_netdev_pmd_thread
*cur_pmd
;
3521 struct ovs_mutex mutex
;
3524 static struct dpif_netdev_flow_dump
*
3525 dpif_netdev_flow_dump_cast(struct dpif_flow_dump
*dump
)
3527 return CONTAINER_OF(dump
, struct dpif_netdev_flow_dump
, up
);
3530 static struct dpif_flow_dump
*
3531 dpif_netdev_flow_dump_create(const struct dpif
*dpif_
, bool terse
,
3532 struct dpif_flow_dump_types
*types OVS_UNUSED
)
3534 struct dpif_netdev_flow_dump
*dump
;
3536 dump
= xzalloc(sizeof *dump
);
3537 dpif_flow_dump_init(&dump
->up
, dpif_
);
3538 dump
->up
.terse
= terse
;
3539 ovs_mutex_init(&dump
->mutex
);
3545 dpif_netdev_flow_dump_destroy(struct dpif_flow_dump
*dump_
)
3547 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
3549 ovs_mutex_destroy(&dump
->mutex
);
3554 struct dpif_netdev_flow_dump_thread
{
3555 struct dpif_flow_dump_thread up
;
3556 struct dpif_netdev_flow_dump
*dump
;
3557 struct odputil_keybuf keybuf
[FLOW_DUMP_MAX_BATCH
];
3558 struct odputil_keybuf maskbuf
[FLOW_DUMP_MAX_BATCH
];
3561 static struct dpif_netdev_flow_dump_thread
*
3562 dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread
*thread
)
3564 return CONTAINER_OF(thread
, struct dpif_netdev_flow_dump_thread
, up
);
3567 static struct dpif_flow_dump_thread
*
3568 dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump
*dump_
)
3570 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
3571 struct dpif_netdev_flow_dump_thread
*thread
;
3573 thread
= xmalloc(sizeof *thread
);
3574 dpif_flow_dump_thread_init(&thread
->up
, &dump
->up
);
3575 thread
->dump
= dump
;
3580 dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread
*thread_
)
3582 struct dpif_netdev_flow_dump_thread
*thread
3583 = dpif_netdev_flow_dump_thread_cast(thread_
);
3589 dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread
*thread_
,
3590 struct dpif_flow
*flows
, int max_flows
)
3592 struct dpif_netdev_flow_dump_thread
*thread
3593 = dpif_netdev_flow_dump_thread_cast(thread_
);
3594 struct dpif_netdev_flow_dump
*dump
= thread
->dump
;
3595 struct dp_netdev_flow
*netdev_flows
[FLOW_DUMP_MAX_BATCH
];
3599 ovs_mutex_lock(&dump
->mutex
);
3600 if (!dump
->status
) {
3601 struct dpif_netdev
*dpif
= dpif_netdev_cast(thread
->up
.dpif
);
3602 struct dp_netdev
*dp
= get_dp_netdev(&dpif
->dpif
);
3603 struct dp_netdev_pmd_thread
*pmd
= dump
->cur_pmd
;
3604 int flow_limit
= MIN(max_flows
, FLOW_DUMP_MAX_BATCH
);
3606 /* First call to dump_next(), extracts the first pmd thread.
3607 * If there is no pmd thread, returns immediately. */
3609 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
3611 ovs_mutex_unlock(&dump
->mutex
);
3618 for (n_flows
= 0; n_flows
< flow_limit
; n_flows
++) {
3619 struct cmap_node
*node
;
3621 node
= cmap_next_position(&pmd
->flow_table
, &dump
->flow_pos
);
3625 netdev_flows
[n_flows
] = CONTAINER_OF(node
,
3626 struct dp_netdev_flow
,
3629 /* When finishing dumping the current pmd thread, moves to
3631 if (n_flows
< flow_limit
) {
3632 memset(&dump
->flow_pos
, 0, sizeof dump
->flow_pos
);
3633 dp_netdev_pmd_unref(pmd
);
3634 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
3640 /* Keeps the reference to next caller. */
3641 dump
->cur_pmd
= pmd
;
3643 /* If the current dump is empty, do not exit the loop, since the
3644 * remaining pmds could have flows to be dumped. Just dumps again
3645 * on the new 'pmd'. */
3648 ovs_mutex_unlock(&dump
->mutex
);
3650 for (i
= 0; i
< n_flows
; i
++) {
3651 struct odputil_keybuf
*maskbuf
= &thread
->maskbuf
[i
];
3652 struct odputil_keybuf
*keybuf
= &thread
->keybuf
[i
];
3653 struct dp_netdev_flow
*netdev_flow
= netdev_flows
[i
];
3654 struct dpif_flow
*f
= &flows
[i
];
3655 struct ofpbuf key
, mask
;
3657 ofpbuf_use_stack(&key
, keybuf
, sizeof *keybuf
);
3658 ofpbuf_use_stack(&mask
, maskbuf
, sizeof *maskbuf
);
3659 dp_netdev_flow_to_dpif_flow(netdev_flow
, &key
, &mask
, f
,
3667 dpif_netdev_execute(struct dpif
*dpif
, struct dpif_execute
*execute
)
3668 OVS_NO_THREAD_SAFETY_ANALYSIS
3670 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3671 struct dp_netdev_pmd_thread
*pmd
;
3672 struct dp_packet_batch pp
;
3674 if (dp_packet_size(execute
->packet
) < ETH_HEADER_LEN
||
3675 dp_packet_size(execute
->packet
) > UINT16_MAX
) {
3679 /* Tries finding the 'pmd'. If NULL is returned, that means
3680 * the current thread is a non-pmd thread and should use
3681 * dp_netdev_get_pmd(dp, NON_PMD_CORE_ID). */
3682 pmd
= ovsthread_getspecific(dp
->per_pmd_key
);
3684 pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
3690 if (execute
->probe
) {
3691 /* If this is part of a probe, Drop the packet, since executing
3692 * the action may actually cause spurious packets be sent into
3694 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3695 dp_netdev_pmd_unref(pmd
);
3700 /* If the current thread is non-pmd thread, acquires
3701 * the 'non_pmd_mutex'. */
3702 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3703 ovs_mutex_lock(&dp
->non_pmd_mutex
);
3706 /* Update current time in PMD context. We don't care about EMC insertion
3707 * probability, because we are on a slow path. */
3708 pmd_thread_ctx_time_update(pmd
);
3710 /* The action processing expects the RSS hash to be valid, because
3711 * it's always initialized at the beginning of datapath processing.
3712 * In this case, though, 'execute->packet' may not have gone through
3713 * the datapath at all, it may have been generated by the upper layer
3714 * (OpenFlow packet-out, BFD frame, ...). */
3715 if (!dp_packet_rss_valid(execute
->packet
)) {
3716 dp_packet_set_rss_hash(execute
->packet
,
3717 flow_hash_5tuple(execute
->flow
, 0));
3720 dp_packet_batch_init_packet(&pp
, execute
->packet
);
3721 pp
.do_not_steal
= true;
3722 dp_netdev_execute_actions(pmd
, &pp
, false, execute
->flow
,
3723 execute
->actions
, execute
->actions_len
);
3724 dp_netdev_pmd_flush_output_packets(pmd
, true);
3726 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3727 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
3728 dp_netdev_pmd_unref(pmd
);
3735 dpif_netdev_operate(struct dpif
*dpif
, struct dpif_op
**ops
, size_t n_ops
,
3736 enum dpif_offload_type offload_type OVS_UNUSED
)
3740 for (i
= 0; i
< n_ops
; i
++) {
3741 struct dpif_op
*op
= ops
[i
];
3744 case DPIF_OP_FLOW_PUT
:
3745 op
->error
= dpif_netdev_flow_put(dpif
, &op
->flow_put
);
3748 case DPIF_OP_FLOW_DEL
:
3749 op
->error
= dpif_netdev_flow_del(dpif
, &op
->flow_del
);
3752 case DPIF_OP_EXECUTE
:
3753 op
->error
= dpif_netdev_execute(dpif
, &op
->execute
);
3756 case DPIF_OP_FLOW_GET
:
3757 op
->error
= dpif_netdev_flow_get(dpif
, &op
->flow_get
);
3763 /* Enable or Disable PMD auto load balancing. */
3765 set_pmd_auto_lb(struct dp_netdev
*dp
)
3767 unsigned int cnt
= 0;
3768 struct dp_netdev_pmd_thread
*pmd
;
3769 struct pmd_auto_lb
*pmd_alb
= &dp
->pmd_alb
;
3771 bool enable_alb
= false;
3772 bool multi_rxq
= false;
3773 bool pmd_rxq_assign_cyc
= dp
->pmd_rxq_assign_cyc
;
3775 /* Ensure that there is at least 2 non-isolated PMDs and
3776 * one of them is polling more than one rxq. */
3777 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3778 if (pmd
->core_id
== NON_PMD_CORE_ID
|| pmd
->isolated
) {
3782 if (hmap_count(&pmd
->poll_list
) > 1) {
3785 if (cnt
&& multi_rxq
) {
3792 /* Enable auto LB if it is requested and cycle based assignment is true. */
3793 enable_alb
= enable_alb
&& pmd_rxq_assign_cyc
&&
3794 pmd_alb
->auto_lb_requested
;
3796 if (pmd_alb
->is_enabled
!= enable_alb
) {
3797 pmd_alb
->is_enabled
= enable_alb
;
3798 if (pmd_alb
->is_enabled
) {
3799 VLOG_INFO("PMD auto load balance is enabled "
3800 "(with rebalance interval:%"PRIu64
" msec)",
3801 pmd_alb
->rebalance_intvl
);
3803 pmd_alb
->rebalance_poll_timer
= 0;
3804 VLOG_INFO("PMD auto load balance is disabled");
3810 /* Applies datapath configuration from the database. Some of the changes are
3811 * actually applied in dpif_netdev_run(). */
3813 dpif_netdev_set_config(struct dpif
*dpif
, const struct smap
*other_config
)
3815 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3816 const char *cmask
= smap_get(other_config
, "pmd-cpu-mask");
3817 const char *pmd_rxq_assign
= smap_get_def(other_config
, "pmd-rxq-assign",
3819 unsigned long long insert_prob
=
3820 smap_get_ullong(other_config
, "emc-insert-inv-prob",
3821 DEFAULT_EM_FLOW_INSERT_INV_PROB
);
3822 uint32_t insert_min
, cur_min
;
3823 uint32_t tx_flush_interval
, cur_tx_flush_interval
;
3824 uint64_t rebalance_intvl
;
3826 tx_flush_interval
= smap_get_int(other_config
, "tx-flush-interval",
3827 DEFAULT_TX_FLUSH_INTERVAL
);
3828 atomic_read_relaxed(&dp
->tx_flush_interval
, &cur_tx_flush_interval
);
3829 if (tx_flush_interval
!= cur_tx_flush_interval
) {
3830 atomic_store_relaxed(&dp
->tx_flush_interval
, tx_flush_interval
);
3831 VLOG_INFO("Flushing interval for tx queues set to %"PRIu32
" us",
3835 if (!nullable_string_is_equal(dp
->pmd_cmask
, cmask
)) {
3836 free(dp
->pmd_cmask
);
3837 dp
->pmd_cmask
= nullable_xstrdup(cmask
);
3838 dp_netdev_request_reconfigure(dp
);
3841 atomic_read_relaxed(&dp
->emc_insert_min
, &cur_min
);
3842 if (insert_prob
<= UINT32_MAX
) {
3843 insert_min
= insert_prob
== 0 ? 0 : UINT32_MAX
/ insert_prob
;
3845 insert_min
= DEFAULT_EM_FLOW_INSERT_MIN
;
3846 insert_prob
= DEFAULT_EM_FLOW_INSERT_INV_PROB
;
3849 if (insert_min
!= cur_min
) {
3850 atomic_store_relaxed(&dp
->emc_insert_min
, insert_min
);
3851 if (insert_min
== 0) {
3852 VLOG_INFO("EMC insertion probability changed to zero");
3854 VLOG_INFO("EMC insertion probability changed to 1/%llu (~%.2f%%)",
3855 insert_prob
, (100 / (float)insert_prob
));
3859 bool perf_enabled
= smap_get_bool(other_config
, "pmd-perf-metrics", false);
3860 bool cur_perf_enabled
;
3861 atomic_read_relaxed(&dp
->pmd_perf_metrics
, &cur_perf_enabled
);
3862 if (perf_enabled
!= cur_perf_enabled
) {
3863 atomic_store_relaxed(&dp
->pmd_perf_metrics
, perf_enabled
);
3865 VLOG_INFO("PMD performance metrics collection enabled");
3867 VLOG_INFO("PMD performance metrics collection disabled");
3871 bool smc_enable
= smap_get_bool(other_config
, "smc-enable", false);
3873 atomic_read_relaxed(&dp
->smc_enable_db
, &cur_smc
);
3874 if (smc_enable
!= cur_smc
) {
3875 atomic_store_relaxed(&dp
->smc_enable_db
, smc_enable
);
3877 VLOG_INFO("SMC cache is enabled");
3879 VLOG_INFO("SMC cache is disabled");
3883 bool pmd_rxq_assign_cyc
= !strcmp(pmd_rxq_assign
, "cycles");
3884 if (!pmd_rxq_assign_cyc
&& strcmp(pmd_rxq_assign
, "roundrobin")) {
3885 VLOG_WARN("Unsupported Rxq to PMD assignment mode in pmd-rxq-assign. "
3886 "Defaulting to 'cycles'.");
3887 pmd_rxq_assign_cyc
= true;
3888 pmd_rxq_assign
= "cycles";
3890 if (dp
->pmd_rxq_assign_cyc
!= pmd_rxq_assign_cyc
) {
3891 dp
->pmd_rxq_assign_cyc
= pmd_rxq_assign_cyc
;
3892 VLOG_INFO("Rxq to PMD assignment mode changed to: \'%s\'.",
3894 dp_netdev_request_reconfigure(dp
);
3897 struct pmd_auto_lb
*pmd_alb
= &dp
->pmd_alb
;
3898 pmd_alb
->auto_lb_requested
= smap_get_bool(other_config
, "pmd-auto-lb",
3901 rebalance_intvl
= smap_get_int(other_config
, "pmd-auto-lb-rebal-interval",
3902 ALB_PMD_REBALANCE_POLL_INTERVAL
);
3904 /* Input is in min, convert it to msec. */
3906 rebalance_intvl
? rebalance_intvl
* MIN_TO_MSEC
: MIN_TO_MSEC
;
3908 if (pmd_alb
->rebalance_intvl
!= rebalance_intvl
) {
3909 pmd_alb
->rebalance_intvl
= rebalance_intvl
;
3912 set_pmd_auto_lb(dp
);
3916 /* Parses affinity list and returns result in 'core_ids'. */
3918 parse_affinity_list(const char *affinity_list
, unsigned *core_ids
, int n_rxq
)
3921 char *list
, *copy
, *key
, *value
;
3924 for (i
= 0; i
< n_rxq
; i
++) {
3925 core_ids
[i
] = OVS_CORE_UNSPEC
;
3928 if (!affinity_list
) {
3932 list
= copy
= xstrdup(affinity_list
);
3934 while (ofputil_parse_key_value(&list
, &key
, &value
)) {
3935 int rxq_id
, core_id
;
3937 if (!str_to_int(key
, 0, &rxq_id
) || rxq_id
< 0
3938 || !str_to_int(value
, 0, &core_id
) || core_id
< 0) {
3943 if (rxq_id
< n_rxq
) {
3944 core_ids
[rxq_id
] = core_id
;
3952 /* Parses 'affinity_list' and applies configuration if it is valid. */
3954 dpif_netdev_port_set_rxq_affinity(struct dp_netdev_port
*port
,
3955 const char *affinity_list
)
3957 unsigned *core_ids
, i
;
3960 core_ids
= xmalloc(port
->n_rxq
* sizeof *core_ids
);
3961 if (parse_affinity_list(affinity_list
, core_ids
, port
->n_rxq
)) {
3966 for (i
= 0; i
< port
->n_rxq
; i
++) {
3967 port
->rxqs
[i
].core_id
= core_ids
[i
];
3975 /* Returns 'true' if one of the 'port's RX queues exists in 'poll_list'
3976 * of given PMD thread. */
3978 dpif_netdev_pmd_polls_port(struct dp_netdev_pmd_thread
*pmd
,
3979 struct dp_netdev_port
*port
)
3980 OVS_EXCLUDED(pmd
->port_mutex
)
3982 struct rxq_poll
*poll
;
3985 ovs_mutex_lock(&pmd
->port_mutex
);
3986 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
3987 if (port
== poll
->rxq
->port
) {
3992 ovs_mutex_unlock(&pmd
->port_mutex
);
3996 /* Updates port configuration from the database. The changes are actually
3997 * applied in dpif_netdev_run(). */
3999 dpif_netdev_port_set_config(struct dpif
*dpif
, odp_port_t port_no
,
4000 const struct smap
*cfg
)
4002 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4003 struct dp_netdev_port
*port
;
4005 const char *affinity_list
= smap_get(cfg
, "pmd-rxq-affinity");
4006 bool emc_enabled
= smap_get_bool(cfg
, "emc-enable", true);
4008 ovs_mutex_lock(&dp
->port_mutex
);
4009 error
= get_port_by_number(dp
, port_no
, &port
);
4014 if (emc_enabled
!= port
->emc_enabled
) {
4015 struct dp_netdev_pmd_thread
*pmd
;
4016 struct ds ds
= DS_EMPTY_INITIALIZER
;
4017 uint32_t cur_min
, insert_prob
;
4019 port
->emc_enabled
= emc_enabled
;
4020 /* Mark for reload all the threads that polls this port and request
4021 * for reconfiguration for the actual reloading of threads. */
4022 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4023 if (dpif_netdev_pmd_polls_port(pmd
, port
)) {
4024 pmd
->need_reload
= true;
4027 dp_netdev_request_reconfigure(dp
);
4029 ds_put_format(&ds
, "%s: EMC has been %s.",
4030 netdev_get_name(port
->netdev
),
4031 (emc_enabled
) ? "enabled" : "disabled");
4033 ds_put_cstr(&ds
, " Current insertion probability is ");
4034 atomic_read_relaxed(&dp
->emc_insert_min
, &cur_min
);
4036 ds_put_cstr(&ds
, "zero.");
4038 insert_prob
= UINT32_MAX
/ cur_min
;
4039 ds_put_format(&ds
, "1/%"PRIu32
" (~%.2f%%).",
4040 insert_prob
, 100 / (float) insert_prob
);
4043 VLOG_INFO("%s", ds_cstr(&ds
));
4047 /* Checking for RXq affinity changes. */
4048 if (!netdev_is_pmd(port
->netdev
)
4049 || nullable_string_is_equal(affinity_list
, port
->rxq_affinity_list
)) {
4053 error
= dpif_netdev_port_set_rxq_affinity(port
, affinity_list
);
4057 free(port
->rxq_affinity_list
);
4058 port
->rxq_affinity_list
= nullable_xstrdup(affinity_list
);
4060 dp_netdev_request_reconfigure(dp
);
4062 ovs_mutex_unlock(&dp
->port_mutex
);
4067 dpif_netdev_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
4068 uint32_t queue_id
, uint32_t *priority
)
4070 *priority
= queue_id
;
4075 /* Creates and returns a new 'struct dp_netdev_actions', whose actions are
4076 * a copy of the 'size' bytes of 'actions' input parameters. */
4077 struct dp_netdev_actions
*
4078 dp_netdev_actions_create(const struct nlattr
*actions
, size_t size
)
4080 struct dp_netdev_actions
*netdev_actions
;
4082 netdev_actions
= xmalloc(sizeof *netdev_actions
+ size
);
4083 memcpy(netdev_actions
->actions
, actions
, size
);
4084 netdev_actions
->size
= size
;
4086 return netdev_actions
;
4089 struct dp_netdev_actions
*
4090 dp_netdev_flow_get_actions(const struct dp_netdev_flow
*flow
)
4092 return ovsrcu_get(struct dp_netdev_actions
*, &flow
->actions
);
4096 dp_netdev_actions_free(struct dp_netdev_actions
*actions
)
4102 dp_netdev_rxq_set_cycles(struct dp_netdev_rxq
*rx
,
4103 enum rxq_cycles_counter_type type
,
4104 unsigned long long cycles
)
4106 atomic_store_relaxed(&rx
->cycles
[type
], cycles
);
4110 dp_netdev_rxq_add_cycles(struct dp_netdev_rxq
*rx
,
4111 enum rxq_cycles_counter_type type
,
4112 unsigned long long cycles
)
4114 non_atomic_ullong_add(&rx
->cycles
[type
], cycles
);
4118 dp_netdev_rxq_get_cycles(struct dp_netdev_rxq
*rx
,
4119 enum rxq_cycles_counter_type type
)
4121 unsigned long long processing_cycles
;
4122 atomic_read_relaxed(&rx
->cycles
[type
], &processing_cycles
);
4123 return processing_cycles
;
4127 dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq
*rx
,
4128 unsigned long long cycles
)
4130 unsigned int idx
= rx
->intrvl_idx
++ % PMD_RXQ_INTERVAL_MAX
;
4131 atomic_store_relaxed(&rx
->cycles_intrvl
[idx
], cycles
);
4135 dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq
*rx
, unsigned idx
)
4137 unsigned long long processing_cycles
;
4138 atomic_read_relaxed(&rx
->cycles_intrvl
[idx
], &processing_cycles
);
4139 return processing_cycles
;
4142 #if ATOMIC_ALWAYS_LOCK_FREE_8B
4144 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread
*pmd
)
4146 bool pmd_perf_enabled
;
4147 atomic_read_relaxed(&pmd
->dp
->pmd_perf_metrics
, &pmd_perf_enabled
);
4148 return pmd_perf_enabled
;
4151 /* If stores and reads of 64-bit integers are not atomic, the full PMD
4152 * performance metrics are not available as locked access to 64 bit
4153 * integers would be prohibitively expensive. */
4155 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread
*pmd OVS_UNUSED
)
4162 dp_netdev_pmd_flush_output_on_port(struct dp_netdev_pmd_thread
*pmd
,
4169 struct cycle_timer timer
;
4171 uint32_t tx_flush_interval
;
4173 cycle_timer_start(&pmd
->perf_stats
, &timer
);
4175 dynamic_txqs
= p
->port
->dynamic_txqs
;
4177 tx_qid
= dpif_netdev_xps_get_tx_qid(pmd
, p
);
4179 tx_qid
= pmd
->static_tx_qid
;
4182 output_cnt
= dp_packet_batch_size(&p
->output_pkts
);
4183 ovs_assert(output_cnt
> 0);
4185 netdev_send(p
->port
->netdev
, tx_qid
, &p
->output_pkts
, dynamic_txqs
);
4186 dp_packet_batch_init(&p
->output_pkts
);
4188 /* Update time of the next flush. */
4189 atomic_read_relaxed(&pmd
->dp
->tx_flush_interval
, &tx_flush_interval
);
4190 p
->flush_time
= pmd
->ctx
.now
+ tx_flush_interval
;
4192 ovs_assert(pmd
->n_output_batches
> 0);
4193 pmd
->n_output_batches
--;
4195 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_SENT_PKTS
, output_cnt
);
4196 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_SENT_BATCHES
, 1);
4198 /* Distribute send cycles evenly among transmitted packets and assign to
4199 * their respective rx queues. */
4200 cycles
= cycle_timer_stop(&pmd
->perf_stats
, &timer
) / output_cnt
;
4201 for (i
= 0; i
< output_cnt
; i
++) {
4202 if (p
->output_pkts_rxqs
[i
]) {
4203 dp_netdev_rxq_add_cycles(p
->output_pkts_rxqs
[i
],
4204 RXQ_CYCLES_PROC_CURR
, cycles
);
4212 dp_netdev_pmd_flush_output_packets(struct dp_netdev_pmd_thread
*pmd
,
4218 if (!pmd
->n_output_batches
) {
4222 HMAP_FOR_EACH (p
, node
, &pmd
->send_port_cache
) {
4223 if (!dp_packet_batch_is_empty(&p
->output_pkts
)
4224 && (force
|| pmd
->ctx
.now
>= p
->flush_time
)) {
4225 output_cnt
+= dp_netdev_pmd_flush_output_on_port(pmd
, p
);
4232 dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread
*pmd
,
4233 struct dp_netdev_rxq
*rxq
,
4236 struct pmd_perf_stats
*s
= &pmd
->perf_stats
;
4237 struct dp_packet_batch batch
;
4238 struct cycle_timer timer
;
4241 int rem_qlen
= 0, *qlen_p
= NULL
;
4244 /* Measure duration for polling and processing rx burst. */
4245 cycle_timer_start(&pmd
->perf_stats
, &timer
);
4247 pmd
->ctx
.last_rxq
= rxq
;
4248 dp_packet_batch_init(&batch
);
4250 /* Fetch the rx queue length only for vhostuser ports. */
4251 if (pmd_perf_metrics_enabled(pmd
) && rxq
->is_vhost
) {
4255 error
= netdev_rxq_recv(rxq
->rx
, &batch
, qlen_p
);
4257 /* At least one packet received. */
4258 *recirc_depth_get() = 0;
4259 pmd_thread_ctx_time_update(pmd
);
4260 batch_cnt
= batch
.count
;
4261 if (pmd_perf_metrics_enabled(pmd
)) {
4262 /* Update batch histogram. */
4263 s
->current
.batches
++;
4264 histogram_add_sample(&s
->pkts_per_batch
, batch_cnt
);
4265 /* Update the maximum vhost rx queue fill level. */
4266 if (rxq
->is_vhost
&& rem_qlen
>= 0) {
4267 uint32_t qfill
= batch_cnt
+ rem_qlen
;
4268 if (qfill
> s
->current
.max_vhost_qfill
) {
4269 s
->current
.max_vhost_qfill
= qfill
;
4273 /* Process packet batch. */
4274 dp_netdev_input(pmd
, &batch
, port_no
);
4276 /* Assign processing cycles to rx queue. */
4277 cycles
= cycle_timer_stop(&pmd
->perf_stats
, &timer
);
4278 dp_netdev_rxq_add_cycles(rxq
, RXQ_CYCLES_PROC_CURR
, cycles
);
4280 dp_netdev_pmd_flush_output_packets(pmd
, false);
4282 /* Discard cycles. */
4283 cycle_timer_stop(&pmd
->perf_stats
, &timer
);
4284 if (error
!= EAGAIN
&& error
!= EOPNOTSUPP
) {
4285 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4287 VLOG_ERR_RL(&rl
, "error receiving data from %s: %s",
4288 netdev_rxq_get_name(rxq
->rx
), ovs_strerror(error
));
4292 pmd
->ctx
.last_rxq
= NULL
;
4297 static struct tx_port
*
4298 tx_port_lookup(const struct hmap
*hmap
, odp_port_t port_no
)
4302 HMAP_FOR_EACH_IN_BUCKET (tx
, node
, hash_port_no(port_no
), hmap
) {
4303 if (tx
->port
->port_no
== port_no
) {
4312 port_reconfigure(struct dp_netdev_port
*port
)
4314 struct netdev
*netdev
= port
->netdev
;
4317 /* Closes the existing 'rxq's. */
4318 for (i
= 0; i
< port
->n_rxq
; i
++) {
4319 netdev_rxq_close(port
->rxqs
[i
].rx
);
4320 port
->rxqs
[i
].rx
= NULL
;
4322 unsigned last_nrxq
= port
->n_rxq
;
4325 /* Allows 'netdev' to apply the pending configuration changes. */
4326 if (netdev_is_reconf_required(netdev
) || port
->need_reconfigure
) {
4327 err
= netdev_reconfigure(netdev
);
4328 if (err
&& (err
!= EOPNOTSUPP
)) {
4329 VLOG_ERR("Failed to set interface %s new configuration",
4330 netdev_get_name(netdev
));
4334 /* If the netdev_reconfigure() above succeeds, reopens the 'rxq's. */
4335 port
->rxqs
= xrealloc(port
->rxqs
,
4336 sizeof *port
->rxqs
* netdev_n_rxq(netdev
));
4337 /* Realloc 'used' counters for tx queues. */
4338 free(port
->txq_used
);
4339 port
->txq_used
= xcalloc(netdev_n_txq(netdev
), sizeof *port
->txq_used
);
4341 for (i
= 0; i
< netdev_n_rxq(netdev
); i
++) {
4342 bool new_queue
= i
>= last_nrxq
;
4344 memset(&port
->rxqs
[i
], 0, sizeof port
->rxqs
[i
]);
4347 port
->rxqs
[i
].port
= port
;
4348 port
->rxqs
[i
].is_vhost
= !strncmp(port
->type
, "dpdkvhost", 9);
4350 err
= netdev_rxq_open(netdev
, &port
->rxqs
[i
].rx
, i
);
4357 /* Parse affinity list to apply configuration for new queues. */
4358 dpif_netdev_port_set_rxq_affinity(port
, port
->rxq_affinity_list
);
4360 /* If reconfiguration was successful mark it as such, so we can use it */
4361 port
->need_reconfigure
= false;
4366 struct rr_numa_list
{
4367 struct hmap numas
; /* Contains 'struct rr_numa' */
4371 struct hmap_node node
;
4375 /* Non isolated pmds on numa node 'numa_id' */
4376 struct dp_netdev_pmd_thread
**pmds
;
4383 static struct rr_numa
*
4384 rr_numa_list_lookup(struct rr_numa_list
*rr
, int numa_id
)
4386 struct rr_numa
*numa
;
4388 HMAP_FOR_EACH_WITH_HASH (numa
, node
, hash_int(numa_id
, 0), &rr
->numas
) {
4389 if (numa
->numa_id
== numa_id
) {
4397 /* Returns the next node in numa list following 'numa' in round-robin fashion.
4398 * Returns first node if 'numa' is a null pointer or the last node in 'rr'.
4399 * Returns NULL if 'rr' numa list is empty. */
4400 static struct rr_numa
*
4401 rr_numa_list_next(struct rr_numa_list
*rr
, const struct rr_numa
*numa
)
4403 struct hmap_node
*node
= NULL
;
4406 node
= hmap_next(&rr
->numas
, &numa
->node
);
4409 node
= hmap_first(&rr
->numas
);
4412 return (node
) ? CONTAINER_OF(node
, struct rr_numa
, node
) : NULL
;
4416 rr_numa_list_populate(struct dp_netdev
*dp
, struct rr_numa_list
*rr
)
4418 struct dp_netdev_pmd_thread
*pmd
;
4419 struct rr_numa
*numa
;
4421 hmap_init(&rr
->numas
);
4423 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4424 if (pmd
->core_id
== NON_PMD_CORE_ID
|| pmd
->isolated
) {
4428 numa
= rr_numa_list_lookup(rr
, pmd
->numa_id
);
4430 numa
= xzalloc(sizeof *numa
);
4431 numa
->numa_id
= pmd
->numa_id
;
4432 hmap_insert(&rr
->numas
, &numa
->node
, hash_int(pmd
->numa_id
, 0));
4435 numa
->pmds
= xrealloc(numa
->pmds
, numa
->n_pmds
* sizeof *numa
->pmds
);
4436 numa
->pmds
[numa
->n_pmds
- 1] = pmd
;
4437 /* At least one pmd so initialise curr_idx and idx_inc. */
4438 numa
->cur_index
= 0;
4439 numa
->idx_inc
= true;
4444 * Returns the next pmd from the numa node.
4446 * If 'updown' is 'true' it will alternate between selecting the next pmd in
4447 * either an up or down walk, switching between up/down when the first or last
4448 * core is reached. e.g. 1,2,3,3,2,1,1,2...
4450 * If 'updown' is 'false' it will select the next pmd wrapping around when last
4451 * core reached. e.g. 1,2,3,1,2,3,1,2...
4453 static struct dp_netdev_pmd_thread
*
4454 rr_numa_get_pmd(struct rr_numa
*numa
, bool updown
)
4456 int numa_idx
= numa
->cur_index
;
4458 if (numa
->idx_inc
== true) {
4459 /* Incrementing through list of pmds. */
4460 if (numa
->cur_index
== numa
->n_pmds
-1) {
4461 /* Reached the last pmd. */
4463 numa
->idx_inc
= false;
4465 numa
->cur_index
= 0;
4471 /* Decrementing through list of pmds. */
4472 if (numa
->cur_index
== 0) {
4473 /* Reached the first pmd. */
4474 numa
->idx_inc
= true;
4479 return numa
->pmds
[numa_idx
];
4483 rr_numa_list_destroy(struct rr_numa_list
*rr
)
4485 struct rr_numa
*numa
;
4487 HMAP_FOR_EACH_POP (numa
, node
, &rr
->numas
) {
4491 hmap_destroy(&rr
->numas
);
4494 /* Sort Rx Queues by the processing cycles they are consuming. */
4496 compare_rxq_cycles(const void *a
, const void *b
)
4498 struct dp_netdev_rxq
*qa
;
4499 struct dp_netdev_rxq
*qb
;
4500 uint64_t cycles_qa
, cycles_qb
;
4502 qa
= *(struct dp_netdev_rxq
**) a
;
4503 qb
= *(struct dp_netdev_rxq
**) b
;
4505 cycles_qa
= dp_netdev_rxq_get_cycles(qa
, RXQ_CYCLES_PROC_HIST
);
4506 cycles_qb
= dp_netdev_rxq_get_cycles(qb
, RXQ_CYCLES_PROC_HIST
);
4508 if (cycles_qa
!= cycles_qb
) {
4509 return (cycles_qa
< cycles_qb
) ? 1 : -1;
4511 /* Cycles are the same so tiebreak on port/queue id.
4512 * Tiebreaking (as opposed to return 0) ensures consistent
4513 * sort results across multiple OS's. */
4514 uint32_t port_qa
= odp_to_u32(qa
->port
->port_no
);
4515 uint32_t port_qb
= odp_to_u32(qb
->port
->port_no
);
4516 if (port_qa
!= port_qb
) {
4517 return port_qa
> port_qb
? 1 : -1;
4519 return netdev_rxq_get_queue_id(qa
->rx
)
4520 - netdev_rxq_get_queue_id(qb
->rx
);
4525 /* Assign pmds to queues. If 'pinned' is true, assign pmds to pinned
4526 * queues and marks the pmds as isolated. Otherwise, assign non isolated
4527 * pmds to unpinned queues.
4529 * The function doesn't touch the pmd threads, it just stores the assignment
4530 * in the 'pmd' member of each rxq. */
4532 rxq_scheduling(struct dp_netdev
*dp
, bool pinned
) OVS_REQUIRES(dp
->port_mutex
)
4534 struct dp_netdev_port
*port
;
4535 struct rr_numa_list rr
;
4536 struct rr_numa
*non_local_numa
= NULL
;
4537 struct dp_netdev_rxq
** rxqs
= NULL
;
4539 struct rr_numa
*numa
= NULL
;
4541 bool assign_cyc
= dp
->pmd_rxq_assign_cyc
;
4543 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4544 if (!netdev_is_pmd(port
->netdev
)) {
4548 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
4549 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
4551 if (pinned
&& q
->core_id
!= OVS_CORE_UNSPEC
) {
4552 struct dp_netdev_pmd_thread
*pmd
;
4554 pmd
= dp_netdev_get_pmd(dp
, q
->core_id
);
4556 VLOG_WARN("There is no PMD thread on core %d. Queue "
4557 "%d on port \'%s\' will not be polled.",
4558 q
->core_id
, qid
, netdev_get_name(port
->netdev
));
4561 pmd
->isolated
= true;
4562 dp_netdev_pmd_unref(pmd
);
4564 } else if (!pinned
&& q
->core_id
== OVS_CORE_UNSPEC
) {
4565 uint64_t cycle_hist
= 0;
4568 rxqs
= xmalloc(sizeof *rxqs
);
4570 rxqs
= xrealloc(rxqs
, sizeof *rxqs
* (n_rxqs
+ 1));
4574 /* Sum the queue intervals and store the cycle history. */
4575 for (unsigned i
= 0; i
< PMD_RXQ_INTERVAL_MAX
; i
++) {
4576 cycle_hist
+= dp_netdev_rxq_get_intrvl_cycles(q
, i
);
4578 dp_netdev_rxq_set_cycles(q
, RXQ_CYCLES_PROC_HIST
,
4581 /* Store the queue. */
4587 if (n_rxqs
> 1 && assign_cyc
) {
4588 /* Sort the queues in order of the processing cycles
4589 * they consumed during their last pmd interval. */
4590 qsort(rxqs
, n_rxqs
, sizeof *rxqs
, compare_rxq_cycles
);
4593 rr_numa_list_populate(dp
, &rr
);
4594 /* Assign the sorted queues to pmds in round robin. */
4595 for (int i
= 0; i
< n_rxqs
; i
++) {
4596 numa_id
= netdev_get_numa_id(rxqs
[i
]->port
->netdev
);
4597 numa
= rr_numa_list_lookup(&rr
, numa_id
);
4599 /* There are no pmds on the queue's local NUMA node.
4600 Round robin on the NUMA nodes that do have pmds. */
4601 non_local_numa
= rr_numa_list_next(&rr
, non_local_numa
);
4602 if (!non_local_numa
) {
4603 VLOG_ERR("There is no available (non-isolated) pmd "
4604 "thread for port \'%s\' queue %d. This queue "
4605 "will not be polled. Is pmd-cpu-mask set to "
4606 "zero? Or are all PMDs isolated to other "
4607 "queues?", netdev_rxq_get_name(rxqs
[i
]->rx
),
4608 netdev_rxq_get_queue_id(rxqs
[i
]->rx
));
4611 rxqs
[i
]->pmd
= rr_numa_get_pmd(non_local_numa
, assign_cyc
);
4612 VLOG_WARN("There's no available (non-isolated) pmd thread "
4613 "on numa node %d. Queue %d on port \'%s\' will "
4614 "be assigned to the pmd on core %d "
4615 "(numa node %d). Expect reduced performance.",
4616 numa_id
, netdev_rxq_get_queue_id(rxqs
[i
]->rx
),
4617 netdev_rxq_get_name(rxqs
[i
]->rx
),
4618 rxqs
[i
]->pmd
->core_id
, rxqs
[i
]->pmd
->numa_id
);
4620 rxqs
[i
]->pmd
= rr_numa_get_pmd(numa
, assign_cyc
);
4622 VLOG_INFO("Core %d on numa node %d assigned port \'%s\' "
4624 "(measured processing cycles %"PRIu64
").",
4625 rxqs
[i
]->pmd
->core_id
, numa_id
,
4626 netdev_rxq_get_name(rxqs
[i
]->rx
),
4627 netdev_rxq_get_queue_id(rxqs
[i
]->rx
),
4628 dp_netdev_rxq_get_cycles(rxqs
[i
],
4629 RXQ_CYCLES_PROC_HIST
));
4631 VLOG_INFO("Core %d on numa node %d assigned port \'%s\' "
4632 "rx queue %d.", rxqs
[i
]->pmd
->core_id
, numa_id
,
4633 netdev_rxq_get_name(rxqs
[i
]->rx
),
4634 netdev_rxq_get_queue_id(rxqs
[i
]->rx
));
4639 rr_numa_list_destroy(&rr
);
4644 reload_affected_pmds(struct dp_netdev
*dp
)
4646 struct dp_netdev_pmd_thread
*pmd
;
4648 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4649 if (pmd
->need_reload
) {
4650 flow_mark_flush(pmd
);
4651 dp_netdev_reload_pmd__(pmd
);
4652 pmd
->need_reload
= false;
4658 reconfigure_pmd_threads(struct dp_netdev
*dp
)
4659 OVS_REQUIRES(dp
->port_mutex
)
4661 struct dp_netdev_pmd_thread
*pmd
;
4662 struct ovs_numa_dump
*pmd_cores
;
4663 struct ovs_numa_info_core
*core
;
4664 struct hmapx to_delete
= HMAPX_INITIALIZER(&to_delete
);
4665 struct hmapx_node
*node
;
4666 bool changed
= false;
4667 bool need_to_adjust_static_tx_qids
= false;
4669 /* The pmd threads should be started only if there's a pmd port in the
4670 * datapath. If the user didn't provide any "pmd-cpu-mask", we start
4671 * NR_PMD_THREADS per numa node. */
4672 if (!has_pmd_port(dp
)) {
4673 pmd_cores
= ovs_numa_dump_n_cores_per_numa(0);
4674 } else if (dp
->pmd_cmask
&& dp
->pmd_cmask
[0]) {
4675 pmd_cores
= ovs_numa_dump_cores_with_cmask(dp
->pmd_cmask
);
4677 pmd_cores
= ovs_numa_dump_n_cores_per_numa(NR_PMD_THREADS
);
4680 /* We need to adjust 'static_tx_qid's only if we're reducing number of
4681 * PMD threads. Otherwise, new threads will allocate all the freed ids. */
4682 if (ovs_numa_dump_count(pmd_cores
) < cmap_count(&dp
->poll_threads
) - 1) {
4683 /* Adjustment is required to keep 'static_tx_qid's sequential and
4684 * avoid possible issues, for example, imbalanced tx queue usage
4685 * and unnecessary locking caused by remapping on netdev level. */
4686 need_to_adjust_static_tx_qids
= true;
4689 /* Check for unwanted pmd threads */
4690 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4691 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
4694 if (!ovs_numa_dump_contains_core(pmd_cores
, pmd
->numa_id
,
4696 hmapx_add(&to_delete
, pmd
);
4697 } else if (need_to_adjust_static_tx_qids
) {
4698 pmd
->need_reload
= true;
4702 HMAPX_FOR_EACH (node
, &to_delete
) {
4703 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
4704 VLOG_INFO("PMD thread on numa_id: %d, core id: %2d destroyed.",
4705 pmd
->numa_id
, pmd
->core_id
);
4706 dp_netdev_del_pmd(dp
, pmd
);
4708 changed
= !hmapx_is_empty(&to_delete
);
4709 hmapx_destroy(&to_delete
);
4711 if (need_to_adjust_static_tx_qids
) {
4712 /* 'static_tx_qid's are not sequential now.
4713 * Reload remaining threads to fix this. */
4714 reload_affected_pmds(dp
);
4717 /* Check for required new pmd threads */
4718 FOR_EACH_CORE_ON_DUMP(core
, pmd_cores
) {
4719 pmd
= dp_netdev_get_pmd(dp
, core
->core_id
);
4721 pmd
= xzalloc(sizeof *pmd
);
4722 dp_netdev_configure_pmd(pmd
, dp
, core
->core_id
, core
->numa_id
);
4723 pmd
->thread
= ovs_thread_create("pmd", pmd_thread_main
, pmd
);
4724 VLOG_INFO("PMD thread on numa_id: %d, core id: %2d created.",
4725 pmd
->numa_id
, pmd
->core_id
);
4728 dp_netdev_pmd_unref(pmd
);
4733 struct ovs_numa_info_numa
*numa
;
4735 /* Log the number of pmd threads per numa node. */
4736 FOR_EACH_NUMA_ON_DUMP (numa
, pmd_cores
) {
4737 VLOG_INFO("There are %"PRIuSIZE
" pmd threads on numa node %d",
4738 numa
->n_cores
, numa
->numa_id
);
4742 ovs_numa_dump_destroy(pmd_cores
);
4746 pmd_remove_stale_ports(struct dp_netdev
*dp
,
4747 struct dp_netdev_pmd_thread
*pmd
)
4748 OVS_EXCLUDED(pmd
->port_mutex
)
4749 OVS_REQUIRES(dp
->port_mutex
)
4751 struct rxq_poll
*poll
, *poll_next
;
4752 struct tx_port
*tx
, *tx_next
;
4754 ovs_mutex_lock(&pmd
->port_mutex
);
4755 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
4756 struct dp_netdev_port
*port
= poll
->rxq
->port
;
4758 if (port
->need_reconfigure
4759 || !hmap_contains(&dp
->ports
, &port
->node
)) {
4760 dp_netdev_del_rxq_from_pmd(pmd
, poll
);
4763 HMAP_FOR_EACH_SAFE (tx
, tx_next
, node
, &pmd
->tx_ports
) {
4764 struct dp_netdev_port
*port
= tx
->port
;
4766 if (port
->need_reconfigure
4767 || !hmap_contains(&dp
->ports
, &port
->node
)) {
4768 dp_netdev_del_port_tx_from_pmd(pmd
, tx
);
4771 ovs_mutex_unlock(&pmd
->port_mutex
);
4774 /* Must be called each time a port is added/removed or the cmask changes.
4775 * This creates and destroys pmd threads, reconfigures ports, opens their
4776 * rxqs and assigns all rxqs/txqs to pmd threads. */
4778 reconfigure_datapath(struct dp_netdev
*dp
)
4779 OVS_REQUIRES(dp
->port_mutex
)
4781 struct dp_netdev_pmd_thread
*pmd
;
4782 struct dp_netdev_port
*port
;
4785 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
4787 /* Step 1: Adjust the pmd threads based on the datapath ports, the cores
4788 * on the system and the user configuration. */
4789 reconfigure_pmd_threads(dp
);
4791 wanted_txqs
= cmap_count(&dp
->poll_threads
);
4793 /* The number of pmd threads might have changed, or a port can be new:
4794 * adjust the txqs. */
4795 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4796 netdev_set_tx_multiq(port
->netdev
, wanted_txqs
);
4799 /* Step 2: Remove from the pmd threads ports that have been removed or
4800 * need reconfiguration. */
4802 /* Check for all the ports that need reconfiguration. We cache this in
4803 * 'port->need_reconfigure', because netdev_is_reconf_required() can
4804 * change at any time. */
4805 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4806 if (netdev_is_reconf_required(port
->netdev
)) {
4807 port
->need_reconfigure
= true;
4811 /* Remove from the pmd threads all the ports that have been deleted or
4812 * need reconfiguration. */
4813 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4814 pmd_remove_stale_ports(dp
, pmd
);
4817 /* Reload affected pmd threads. We must wait for the pmd threads before
4818 * reconfiguring the ports, because a port cannot be reconfigured while
4819 * it's being used. */
4820 reload_affected_pmds(dp
);
4822 /* Step 3: Reconfigure ports. */
4824 /* We only reconfigure the ports that we determined above, because they're
4825 * not being used by any pmd thread at the moment. If a port fails to
4826 * reconfigure we remove it from the datapath. */
4827 struct dp_netdev_port
*next_port
;
4828 HMAP_FOR_EACH_SAFE (port
, next_port
, node
, &dp
->ports
) {
4831 if (!port
->need_reconfigure
) {
4835 err
= port_reconfigure(port
);
4837 hmap_remove(&dp
->ports
, &port
->node
);
4838 seq_change(dp
->port_seq
);
4841 port
->dynamic_txqs
= netdev_n_txq(port
->netdev
) < wanted_txqs
;
4845 /* Step 4: Compute new rxq scheduling. We don't touch the pmd threads
4846 * for now, we just update the 'pmd' pointer in each rxq to point to the
4847 * wanted thread according to the scheduling policy. */
4849 /* Reset all the pmd threads to non isolated. */
4850 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4851 pmd
->isolated
= false;
4854 /* Reset all the queues to unassigned */
4855 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4856 for (int i
= 0; i
< port
->n_rxq
; i
++) {
4857 port
->rxqs
[i
].pmd
= NULL
;
4861 /* Add pinned queues and mark pmd threads isolated. */
4862 rxq_scheduling(dp
, true);
4864 /* Add non-pinned queues. */
4865 rxq_scheduling(dp
, false);
4867 /* Step 5: Remove queues not compliant with new scheduling. */
4868 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4869 struct rxq_poll
*poll
, *poll_next
;
4871 ovs_mutex_lock(&pmd
->port_mutex
);
4872 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
4873 if (poll
->rxq
->pmd
!= pmd
) {
4874 dp_netdev_del_rxq_from_pmd(pmd
, poll
);
4877 ovs_mutex_unlock(&pmd
->port_mutex
);
4880 /* Reload affected pmd threads. We must wait for the pmd threads to remove
4881 * the old queues before readding them, otherwise a queue can be polled by
4882 * two threads at the same time. */
4883 reload_affected_pmds(dp
);
4885 /* Step 6: Add queues from scheduling, if they're not there already. */
4886 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4887 if (!netdev_is_pmd(port
->netdev
)) {
4891 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
4892 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
4895 ovs_mutex_lock(&q
->pmd
->port_mutex
);
4896 dp_netdev_add_rxq_to_pmd(q
->pmd
, q
);
4897 ovs_mutex_unlock(&q
->pmd
->port_mutex
);
4902 /* Add every port to the tx cache of every pmd thread, if it's not
4903 * there already and if this pmd has at least one rxq to poll. */
4904 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4905 ovs_mutex_lock(&pmd
->port_mutex
);
4906 if (hmap_count(&pmd
->poll_list
) || pmd
->core_id
== NON_PMD_CORE_ID
) {
4907 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4908 dp_netdev_add_port_tx_to_pmd(pmd
, port
);
4911 ovs_mutex_unlock(&pmd
->port_mutex
);
4914 /* Reload affected pmd threads. */
4915 reload_affected_pmds(dp
);
4917 /* Check if PMD Auto LB is to be enabled */
4918 set_pmd_auto_lb(dp
);
4921 /* Returns true if one of the netdevs in 'dp' requires a reconfiguration */
4923 ports_require_restart(const struct dp_netdev
*dp
)
4924 OVS_REQUIRES(dp
->port_mutex
)
4926 struct dp_netdev_port
*port
;
4928 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4929 if (netdev_is_reconf_required(port
->netdev
)) {
4937 /* Calculates variance in the values stored in array 'a'. 'n' is the number
4938 * of elements in array to be considered for calculating vairance.
4939 * Usage example: data array 'a' contains the processing load of each pmd and
4940 * 'n' is the number of PMDs. It returns the variance in processing load of
4943 variance(uint64_t a
[], int n
)
4945 /* Compute mean (average of elements). */
4948 uint64_t sqDiff
= 0;
4954 for (int i
= 0; i
< n
; i
++) {
4961 /* Compute sum squared differences with mean. */
4962 for (int i
= 0; i
< n
; i
++) {
4963 sqDiff
+= (a
[i
] - mean
)*(a
[i
] - mean
);
4966 return (sqDiff
? (sqDiff
/ n
) : 0);
4970 /* Returns the variance in the PMDs usage as part of dry run of rxqs
4971 * assignment to PMDs. */
4973 get_dry_run_variance(struct dp_netdev
*dp
, uint32_t *core_list
,
4974 uint32_t num_pmds
, uint64_t *predicted_variance
)
4975 OVS_REQUIRES(dp
->port_mutex
)
4977 struct dp_netdev_port
*port
;
4978 struct dp_netdev_pmd_thread
*pmd
;
4979 struct dp_netdev_rxq
**rxqs
= NULL
;
4980 struct rr_numa
*numa
= NULL
;
4981 struct rr_numa_list rr
;
4984 uint64_t *pmd_usage
;
4986 if (!predicted_variance
) {
4990 pmd_usage
= xcalloc(num_pmds
, sizeof(uint64_t));
4992 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4993 if (!netdev_is_pmd(port
->netdev
)) {
4997 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
4998 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
4999 uint64_t cycle_hist
= 0;
5001 if (q
->pmd
->isolated
) {
5006 rxqs
= xmalloc(sizeof *rxqs
);
5008 rxqs
= xrealloc(rxqs
, sizeof *rxqs
* (n_rxqs
+ 1));
5011 /* Sum the queue intervals and store the cycle history. */
5012 for (unsigned i
= 0; i
< PMD_RXQ_INTERVAL_MAX
; i
++) {
5013 cycle_hist
+= dp_netdev_rxq_get_intrvl_cycles(q
, i
);
5015 dp_netdev_rxq_set_cycles(q
, RXQ_CYCLES_PROC_HIST
,
5017 /* Store the queue. */
5022 /* Sort the queues in order of the processing cycles
5023 * they consumed during their last pmd interval. */
5024 qsort(rxqs
, n_rxqs
, sizeof *rxqs
, compare_rxq_cycles
);
5026 rr_numa_list_populate(dp
, &rr
);
5028 for (int i
= 0; i
< n_rxqs
; i
++) {
5029 int numa_id
= netdev_get_numa_id(rxqs
[i
]->port
->netdev
);
5030 numa
= rr_numa_list_lookup(&rr
, numa_id
);
5032 /* Abort if cross NUMA polling. */
5033 VLOG_DBG("PMD auto lb dry run."
5034 " Aborting due to cross-numa polling.");
5038 pmd
= rr_numa_get_pmd(numa
, true);
5039 VLOG_DBG("PMD auto lb dry run. Predicted: Core %d on numa node %d "
5040 "to be assigned port \'%s\' rx queue %d "
5041 "(measured processing cycles %"PRIu64
").",
5042 pmd
->core_id
, numa_id
,
5043 netdev_rxq_get_name(rxqs
[i
]->rx
),
5044 netdev_rxq_get_queue_id(rxqs
[i
]->rx
),
5045 dp_netdev_rxq_get_cycles(rxqs
[i
], RXQ_CYCLES_PROC_HIST
));
5047 for (int id
= 0; id
< num_pmds
; id
++) {
5048 if (pmd
->core_id
== core_list
[id
]) {
5049 /* Add the processing cycles of rxq to pmd polling it. */
5050 pmd_usage
[id
] += dp_netdev_rxq_get_cycles(rxqs
[i
],
5051 RXQ_CYCLES_PROC_HIST
);
5056 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5057 uint64_t total_cycles
= 0;
5059 if ((pmd
->core_id
== NON_PMD_CORE_ID
) || pmd
->isolated
) {
5063 /* Get the total pmd cycles for an interval. */
5064 atomic_read_relaxed(&pmd
->intrvl_cycles
, &total_cycles
);
5065 /* Estimate the cycles to cover all intervals. */
5066 total_cycles
*= PMD_RXQ_INTERVAL_MAX
;
5067 for (int id
= 0; id
< num_pmds
; id
++) {
5068 if (pmd
->core_id
== core_list
[id
]) {
5069 if (pmd_usage
[id
]) {
5070 pmd_usage
[id
] = (pmd_usage
[id
] * 100) / total_cycles
;
5072 VLOG_DBG("PMD auto lb dry run. Predicted: Core %d, "
5073 "usage %"PRIu64
"", pmd
->core_id
, pmd_usage
[id
]);
5077 *predicted_variance
= variance(pmd_usage
, num_pmds
);
5081 rr_numa_list_destroy(&rr
);
5087 /* Does the dry run of Rxq assignment to PMDs and returns true if it gives
5088 * better distribution of load on PMDs. */
5090 pmd_rebalance_dry_run(struct dp_netdev
*dp
)
5091 OVS_REQUIRES(dp
->port_mutex
)
5093 struct dp_netdev_pmd_thread
*pmd
;
5094 uint64_t *curr_pmd_usage
;
5096 uint64_t curr_variance
;
5097 uint64_t new_variance
;
5098 uint64_t improvement
= 0;
5100 uint32_t *pmd_corelist
;
5101 struct rxq_poll
*poll
, *poll_next
;
5104 num_pmds
= cmap_count(&dp
->poll_threads
);
5107 curr_pmd_usage
= xcalloc(num_pmds
, sizeof(uint64_t));
5108 pmd_corelist
= xcalloc(num_pmds
, sizeof(uint32_t));
5114 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5115 uint64_t total_cycles
= 0;
5116 uint64_t total_proc
= 0;
5118 if ((pmd
->core_id
== NON_PMD_CORE_ID
) || pmd
->isolated
) {
5122 /* Get the total pmd cycles for an interval. */
5123 atomic_read_relaxed(&pmd
->intrvl_cycles
, &total_cycles
);
5124 /* Estimate the cycles to cover all intervals. */
5125 total_cycles
*= PMD_RXQ_INTERVAL_MAX
;
5127 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
5128 uint64_t proc_cycles
= 0;
5129 for (unsigned i
= 0; i
< PMD_RXQ_INTERVAL_MAX
; i
++) {
5130 proc_cycles
+= dp_netdev_rxq_get_intrvl_cycles(poll
->rxq
, i
);
5132 total_proc
+= proc_cycles
;
5135 curr_pmd_usage
[num_pmds
] = (total_proc
* 100) / total_cycles
;
5138 VLOG_DBG("PMD auto lb dry run. Current: Core %d, usage %"PRIu64
"",
5139 pmd
->core_id
, curr_pmd_usage
[num_pmds
]);
5141 if (atomic_count_get(&pmd
->pmd_overloaded
)) {
5142 atomic_count_set(&pmd
->pmd_overloaded
, 0);
5145 pmd_corelist
[num_pmds
] = pmd
->core_id
;
5149 curr_variance
= variance(curr_pmd_usage
, num_pmds
);
5150 ret
= get_dry_run_variance(dp
, pmd_corelist
, num_pmds
, &new_variance
);
5153 VLOG_DBG("PMD auto lb dry run. Current PMD variance: %"PRIu64
","
5154 " Predicted PMD variance: %"PRIu64
"",
5155 curr_variance
, new_variance
);
5157 if (new_variance
< curr_variance
) {
5159 ((curr_variance
- new_variance
) * 100) / curr_variance
;
5161 if (improvement
< ALB_ACCEPTABLE_IMPROVEMENT
) {
5166 free(curr_pmd_usage
);
5172 /* Return true if needs to revalidate datapath flows. */
5174 dpif_netdev_run(struct dpif
*dpif
)
5176 struct dp_netdev_port
*port
;
5177 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5178 struct dp_netdev_pmd_thread
*non_pmd
;
5179 uint64_t new_tnl_seq
;
5180 bool need_to_flush
= true;
5181 bool pmd_rebalance
= false;
5182 long long int now
= time_msec();
5183 struct dp_netdev_pmd_thread
*pmd
;
5185 ovs_mutex_lock(&dp
->port_mutex
);
5186 non_pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
5188 ovs_mutex_lock(&dp
->non_pmd_mutex
);
5189 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
5190 if (!netdev_is_pmd(port
->netdev
)) {
5193 if (port
->emc_enabled
) {
5194 atomic_read_relaxed(&dp
->emc_insert_min
,
5195 &non_pmd
->ctx
.emc_insert_min
);
5197 non_pmd
->ctx
.emc_insert_min
= 0;
5200 for (i
= 0; i
< port
->n_rxq
; i
++) {
5201 if (dp_netdev_process_rxq_port(non_pmd
,
5204 need_to_flush
= false;
5209 if (need_to_flush
) {
5210 /* We didn't receive anything in the process loop.
5211 * Check if we need to send something.
5212 * There was no time updates on current iteration. */
5213 pmd_thread_ctx_time_update(non_pmd
);
5214 dp_netdev_pmd_flush_output_packets(non_pmd
, false);
5217 dpif_netdev_xps_revalidate_pmd(non_pmd
, false);
5218 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
5220 dp_netdev_pmd_unref(non_pmd
);
5223 struct pmd_auto_lb
*pmd_alb
= &dp
->pmd_alb
;
5224 if (pmd_alb
->is_enabled
) {
5225 if (!pmd_alb
->rebalance_poll_timer
) {
5226 pmd_alb
->rebalance_poll_timer
= now
;
5227 } else if ((pmd_alb
->rebalance_poll_timer
+
5228 pmd_alb
->rebalance_intvl
) < now
) {
5229 pmd_alb
->rebalance_poll_timer
= now
;
5230 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5231 if (atomic_count_get(&pmd
->pmd_overloaded
) >=
5232 PMD_RXQ_INTERVAL_MAX
) {
5233 pmd_rebalance
= true;
5238 if (pmd_rebalance
&&
5239 !dp_netdev_is_reconf_required(dp
) &&
5240 !ports_require_restart(dp
) &&
5241 pmd_rebalance_dry_run(dp
)) {
5242 VLOG_INFO("PMD auto lb dry run."
5243 " requesting datapath reconfigure.");
5244 dp_netdev_request_reconfigure(dp
);
5249 if (dp_netdev_is_reconf_required(dp
) || ports_require_restart(dp
)) {
5250 reconfigure_datapath(dp
);
5252 ovs_mutex_unlock(&dp
->port_mutex
);
5254 tnl_neigh_cache_run();
5256 new_tnl_seq
= seq_read(tnl_conf_seq
);
5258 if (dp
->last_tnl_conf_seq
!= new_tnl_seq
) {
5259 dp
->last_tnl_conf_seq
= new_tnl_seq
;
5266 dpif_netdev_wait(struct dpif
*dpif
)
5268 struct dp_netdev_port
*port
;
5269 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5271 ovs_mutex_lock(&dp_netdev_mutex
);
5272 ovs_mutex_lock(&dp
->port_mutex
);
5273 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
5274 netdev_wait_reconf_required(port
->netdev
);
5275 if (!netdev_is_pmd(port
->netdev
)) {
5278 for (i
= 0; i
< port
->n_rxq
; i
++) {
5279 netdev_rxq_wait(port
->rxqs
[i
].rx
);
5283 ovs_mutex_unlock(&dp
->port_mutex
);
5284 ovs_mutex_unlock(&dp_netdev_mutex
);
5285 seq_wait(tnl_conf_seq
, dp
->last_tnl_conf_seq
);
5289 pmd_free_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
5291 struct tx_port
*tx_port_cached
;
5293 /* Flush all the queued packets. */
5294 dp_netdev_pmd_flush_output_packets(pmd
, true);
5295 /* Free all used tx queue ids. */
5296 dpif_netdev_xps_revalidate_pmd(pmd
, true);
5298 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->tnl_port_cache
) {
5299 free(tx_port_cached
);
5301 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->send_port_cache
) {
5302 free(tx_port_cached
);
5306 /* Copies ports from 'pmd->tx_ports' (shared with the main thread) to
5307 * thread-local copies. Copy to 'pmd->tnl_port_cache' if it is a tunnel
5308 * device, otherwise to 'pmd->send_port_cache' if the port has at least
5311 pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
5312 OVS_REQUIRES(pmd
->port_mutex
)
5314 struct tx_port
*tx_port
, *tx_port_cached
;
5316 pmd_free_cached_ports(pmd
);
5317 hmap_shrink(&pmd
->send_port_cache
);
5318 hmap_shrink(&pmd
->tnl_port_cache
);
5320 HMAP_FOR_EACH (tx_port
, node
, &pmd
->tx_ports
) {
5321 if (netdev_has_tunnel_push_pop(tx_port
->port
->netdev
)) {
5322 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
5323 hmap_insert(&pmd
->tnl_port_cache
, &tx_port_cached
->node
,
5324 hash_port_no(tx_port_cached
->port
->port_no
));
5327 if (netdev_n_txq(tx_port
->port
->netdev
)) {
5328 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
5329 hmap_insert(&pmd
->send_port_cache
, &tx_port_cached
->node
,
5330 hash_port_no(tx_port_cached
->port
->port_no
));
5336 pmd_alloc_static_tx_qid(struct dp_netdev_pmd_thread
*pmd
)
5338 ovs_mutex_lock(&pmd
->dp
->tx_qid_pool_mutex
);
5339 if (!id_pool_alloc_id(pmd
->dp
->tx_qid_pool
, &pmd
->static_tx_qid
)) {
5340 VLOG_ABORT("static_tx_qid allocation failed for PMD on core %2d"
5341 ", numa_id %d.", pmd
->core_id
, pmd
->numa_id
);
5343 ovs_mutex_unlock(&pmd
->dp
->tx_qid_pool_mutex
);
5345 VLOG_DBG("static_tx_qid = %d allocated for PMD thread on core %2d"
5346 ", numa_id %d.", pmd
->static_tx_qid
, pmd
->core_id
, pmd
->numa_id
);
5350 pmd_free_static_tx_qid(struct dp_netdev_pmd_thread
*pmd
)
5352 ovs_mutex_lock(&pmd
->dp
->tx_qid_pool_mutex
);
5353 id_pool_free_id(pmd
->dp
->tx_qid_pool
, pmd
->static_tx_qid
);
5354 ovs_mutex_unlock(&pmd
->dp
->tx_qid_pool_mutex
);
5358 pmd_load_queues_and_ports(struct dp_netdev_pmd_thread
*pmd
,
5359 struct polled_queue
**ppoll_list
)
5361 struct polled_queue
*poll_list
= *ppoll_list
;
5362 struct rxq_poll
*poll
;
5365 ovs_mutex_lock(&pmd
->port_mutex
);
5366 poll_list
= xrealloc(poll_list
, hmap_count(&pmd
->poll_list
)
5367 * sizeof *poll_list
);
5370 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
5371 poll_list
[i
].rxq
= poll
->rxq
;
5372 poll_list
[i
].port_no
= poll
->rxq
->port
->port_no
;
5373 poll_list
[i
].emc_enabled
= poll
->rxq
->port
->emc_enabled
;
5377 pmd_load_cached_ports(pmd
);
5379 ovs_mutex_unlock(&pmd
->port_mutex
);
5381 *ppoll_list
= poll_list
;
5386 pmd_thread_main(void *f_
)
5388 struct dp_netdev_pmd_thread
*pmd
= f_
;
5389 struct pmd_perf_stats
*s
= &pmd
->perf_stats
;
5390 unsigned int lc
= 0;
5391 struct polled_queue
*poll_list
;
5395 int process_packets
= 0;
5399 /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */
5400 ovsthread_setspecific(pmd
->dp
->per_pmd_key
, pmd
);
5401 ovs_numa_thread_setaffinity_core(pmd
->core_id
);
5402 dpdk_set_lcore_id(pmd
->core_id
);
5403 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
5404 dfc_cache_init(&pmd
->flow_cache
);
5406 pmd_alloc_static_tx_qid(pmd
);
5408 atomic_count_init(&pmd
->pmd_overloaded
, 0);
5410 /* List port/core affinity */
5411 for (i
= 0; i
< poll_cnt
; i
++) {
5412 VLOG_DBG("Core %d processing port \'%s\' with queue-id %d\n",
5413 pmd
->core_id
, netdev_rxq_get_name(poll_list
[i
].rxq
->rx
),
5414 netdev_rxq_get_queue_id(poll_list
[i
].rxq
->rx
));
5415 /* Reset the rxq current cycles counter. */
5416 dp_netdev_rxq_set_cycles(poll_list
[i
].rxq
, RXQ_CYCLES_PROC_CURR
, 0);
5420 while (seq_read(pmd
->reload_seq
) == pmd
->last_reload_seq
) {
5421 seq_wait(pmd
->reload_seq
, pmd
->last_reload_seq
);
5427 pmd
->intrvl_tsc_prev
= 0;
5428 atomic_store_relaxed(&pmd
->intrvl_cycles
, 0);
5429 cycles_counter_update(s
);
5430 /* Protect pmd stats from external clearing while polling. */
5431 ovs_mutex_lock(&pmd
->perf_stats
.stats_mutex
);
5433 uint64_t rx_packets
= 0, tx_packets
= 0;
5435 pmd_perf_start_iteration(s
);
5437 for (i
= 0; i
< poll_cnt
; i
++) {
5439 if (poll_list
[i
].emc_enabled
) {
5440 atomic_read_relaxed(&pmd
->dp
->emc_insert_min
,
5441 &pmd
->ctx
.emc_insert_min
);
5443 pmd
->ctx
.emc_insert_min
= 0;
5447 dp_netdev_process_rxq_port(pmd
, poll_list
[i
].rxq
,
5448 poll_list
[i
].port_no
);
5449 rx_packets
+= process_packets
;
5453 /* We didn't receive anything in the process loop.
5454 * Check if we need to send something.
5455 * There was no time updates on current iteration. */
5456 pmd_thread_ctx_time_update(pmd
);
5457 tx_packets
= dp_netdev_pmd_flush_output_packets(pmd
, false);
5465 coverage_try_clear();
5466 dp_netdev_pmd_try_optimize(pmd
, poll_list
, poll_cnt
);
5467 if (!ovsrcu_try_quiesce()) {
5468 emc_cache_slow_sweep(&((pmd
->flow_cache
).emc_cache
));
5471 atomic_read_relaxed(&pmd
->reload
, &reload
);
5476 pmd_perf_end_iteration(s
, rx_packets
, tx_packets
,
5477 pmd_perf_metrics_enabled(pmd
));
5479 ovs_mutex_unlock(&pmd
->perf_stats
.stats_mutex
);
5481 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
5482 exiting
= latch_is_set(&pmd
->exit_latch
);
5483 /* Signal here to make sure the pmd finishes
5484 * reloading the updated configuration. */
5485 dp_netdev_pmd_reload_done(pmd
);
5487 pmd_free_static_tx_qid(pmd
);
5493 dfc_cache_uninit(&pmd
->flow_cache
);
5495 pmd_free_cached_ports(pmd
);
5500 dp_netdev_disable_upcall(struct dp_netdev
*dp
)
5501 OVS_ACQUIRES(dp
->upcall_rwlock
)
5503 fat_rwlock_wrlock(&dp
->upcall_rwlock
);
5509 dpif_netdev_meter_get_features(const struct dpif
* dpif OVS_UNUSED
,
5510 struct ofputil_meter_features
*features
)
5512 features
->max_meters
= MAX_METERS
;
5513 features
->band_types
= DP_SUPPORTED_METER_BAND_TYPES
;
5514 features
->capabilities
= DP_SUPPORTED_METER_FLAGS_MASK
;
5515 features
->max_bands
= MAX_BANDS
;
5516 features
->max_color
= 0;
5519 /* Applies the meter identified by 'meter_id' to 'packets_'. Packets
5520 * that exceed a band are dropped in-place. */
5522 dp_netdev_run_meter(struct dp_netdev
*dp
, struct dp_packet_batch
*packets_
,
5523 uint32_t meter_id
, long long int now
)
5525 struct dp_meter
*meter
;
5526 struct dp_meter_band
*band
;
5527 struct dp_packet
*packet
;
5528 long long int long_delta_t
; /* msec */
5529 uint32_t delta_t
; /* msec */
5530 const size_t cnt
= dp_packet_batch_size(packets_
);
5531 uint32_t bytes
, volume
;
5532 int exceeded_band
[NETDEV_MAX_BURST
];
5533 uint32_t exceeded_rate
[NETDEV_MAX_BURST
];
5534 int exceeded_pkt
= cnt
; /* First packet that exceeded a band rate. */
5536 if (meter_id
>= MAX_METERS
) {
5540 meter_lock(dp
, meter_id
);
5541 meter
= dp
->meters
[meter_id
];
5546 /* Initialize as negative values. */
5547 memset(exceeded_band
, 0xff, cnt
* sizeof *exceeded_band
);
5548 /* Initialize as zeroes. */
5549 memset(exceeded_rate
, 0, cnt
* sizeof *exceeded_rate
);
5551 /* All packets will hit the meter at the same time. */
5552 long_delta_t
= (now
- meter
->used
) / 1000; /* msec */
5554 /* Make sure delta_t will not be too large, so that bucket will not
5555 * wrap around below. */
5556 delta_t
= (long_delta_t
> (long long int)meter
->max_delta_t
)
5557 ? meter
->max_delta_t
: (uint32_t)long_delta_t
;
5559 /* Update meter stats. */
5561 meter
->packet_count
+= cnt
;
5563 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
5564 bytes
+= dp_packet_size(packet
);
5566 meter
->byte_count
+= bytes
;
5568 /* Meters can operate in terms of packets per second or kilobits per
5570 if (meter
->flags
& OFPMF13_PKTPS
) {
5571 /* Rate in packets/second, bucket 1/1000 packets. */
5572 /* msec * packets/sec = 1/1000 packets. */
5573 volume
= cnt
* 1000; /* Take 'cnt' packets from the bucket. */
5575 /* Rate in kbps, bucket in bits. */
5576 /* msec * kbps = bits */
5580 /* Update all bands and find the one hit with the highest rate for each
5581 * packet (if any). */
5582 for (int m
= 0; m
< meter
->n_bands
; ++m
) {
5583 band
= &meter
->bands
[m
];
5585 /* Update band's bucket. */
5586 band
->bucket
+= delta_t
* band
->up
.rate
;
5587 if (band
->bucket
> band
->up
.burst_size
) {
5588 band
->bucket
= band
->up
.burst_size
;
5591 /* Drain the bucket for all the packets, if possible. */
5592 if (band
->bucket
>= volume
) {
5593 band
->bucket
-= volume
;
5595 int band_exceeded_pkt
;
5597 /* Band limit hit, must process packet-by-packet. */
5598 if (meter
->flags
& OFPMF13_PKTPS
) {
5599 band_exceeded_pkt
= band
->bucket
/ 1000;
5600 band
->bucket
%= 1000; /* Remainder stays in bucket. */
5602 /* Update the exceeding band for each exceeding packet.
5603 * (Only one band will be fired by a packet, and that
5604 * can be different for each packet.) */
5605 for (int i
= band_exceeded_pkt
; i
< cnt
; i
++) {
5606 if (band
->up
.rate
> exceeded_rate
[i
]) {
5607 exceeded_rate
[i
] = band
->up
.rate
;
5608 exceeded_band
[i
] = m
;
5612 /* Packet sizes differ, must process one-by-one. */
5613 band_exceeded_pkt
= cnt
;
5614 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
5615 uint32_t bits
= dp_packet_size(packet
) * 8;
5617 if (band
->bucket
>= bits
) {
5618 band
->bucket
-= bits
;
5620 if (i
< band_exceeded_pkt
) {
5621 band_exceeded_pkt
= i
;
5623 /* Update the exceeding band for the exceeding packet.
5624 * (Only one band will be fired by a packet, and that
5625 * can be different for each packet.) */
5626 if (band
->up
.rate
> exceeded_rate
[i
]) {
5627 exceeded_rate
[i
] = band
->up
.rate
;
5628 exceeded_band
[i
] = m
;
5633 /* Remember the first exceeding packet. */
5634 if (exceeded_pkt
> band_exceeded_pkt
) {
5635 exceeded_pkt
= band_exceeded_pkt
;
5640 /* Fire the highest rate band exceeded by each packet, and drop
5641 * packets if needed. */
5643 DP_PACKET_BATCH_REFILL_FOR_EACH (j
, cnt
, packet
, packets_
) {
5644 if (exceeded_band
[j
] >= 0) {
5645 /* Meter drop packet. */
5646 band
= &meter
->bands
[exceeded_band
[j
]];
5647 band
->packet_count
+= 1;
5648 band
->byte_count
+= dp_packet_size(packet
);
5650 dp_packet_delete(packet
);
5652 /* Meter accepts packet. */
5653 dp_packet_batch_refill(packets_
, packet
, j
);
5657 meter_unlock(dp
, meter_id
);
5660 /* Meter set/get/del processing is still single-threaded. */
5662 dpif_netdev_meter_set(struct dpif
*dpif
, ofproto_meter_id meter_id
,
5663 struct ofputil_meter_config
*config
)
5665 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5666 uint32_t mid
= meter_id
.uint32
;
5667 struct dp_meter
*meter
;
5670 if (mid
>= MAX_METERS
) {
5671 return EFBIG
; /* Meter_id out of range. */
5674 if (config
->flags
& ~DP_SUPPORTED_METER_FLAGS_MASK
) {
5675 return EBADF
; /* Unsupported flags set */
5678 if (config
->n_bands
> MAX_BANDS
) {
5682 for (i
= 0; i
< config
->n_bands
; ++i
) {
5683 switch (config
->bands
[i
].type
) {
5687 return ENODEV
; /* Unsupported band type */
5691 /* Allocate meter */
5692 meter
= xzalloc(sizeof *meter
5693 + config
->n_bands
* sizeof(struct dp_meter_band
));
5695 meter
->flags
= config
->flags
;
5696 meter
->n_bands
= config
->n_bands
;
5697 meter
->max_delta_t
= 0;
5698 meter
->used
= time_usec();
5701 for (i
= 0; i
< config
->n_bands
; ++i
) {
5702 uint32_t band_max_delta_t
;
5704 /* Set burst size to a workable value if none specified. */
5705 if (config
->bands
[i
].burst_size
== 0) {
5706 config
->bands
[i
].burst_size
= config
->bands
[i
].rate
;
5709 meter
->bands
[i
].up
= config
->bands
[i
];
5710 /* Convert burst size to the bucket units: */
5711 /* pkts => 1/1000 packets, kilobits => bits. */
5712 meter
->bands
[i
].up
.burst_size
*= 1000;
5713 /* Initialize bucket to empty. */
5714 meter
->bands
[i
].bucket
= 0;
5716 /* Figure out max delta_t that is enough to fill any bucket. */
5718 = meter
->bands
[i
].up
.burst_size
/ meter
->bands
[i
].up
.rate
;
5719 if (band_max_delta_t
> meter
->max_delta_t
) {
5720 meter
->max_delta_t
= band_max_delta_t
;
5724 meter_lock(dp
, mid
);
5725 dp_delete_meter(dp
, mid
); /* Free existing meter, if any */
5726 dp
->meters
[mid
] = meter
;
5727 meter_unlock(dp
, mid
);
5733 dpif_netdev_meter_get(const struct dpif
*dpif
,
5734 ofproto_meter_id meter_id_
,
5735 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
5737 const struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5738 uint32_t meter_id
= meter_id_
.uint32
;
5741 if (meter_id
>= MAX_METERS
) {
5745 meter_lock(dp
, meter_id
);
5746 const struct dp_meter
*meter
= dp
->meters
[meter_id
];
5754 stats
->packet_in_count
= meter
->packet_count
;
5755 stats
->byte_in_count
= meter
->byte_count
;
5757 for (i
= 0; i
< n_bands
&& i
< meter
->n_bands
; ++i
) {
5758 stats
->bands
[i
].packet_count
= meter
->bands
[i
].packet_count
;
5759 stats
->bands
[i
].byte_count
= meter
->bands
[i
].byte_count
;
5766 meter_unlock(dp
, meter_id
);
5771 dpif_netdev_meter_del(struct dpif
*dpif
,
5772 ofproto_meter_id meter_id_
,
5773 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
5775 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5778 error
= dpif_netdev_meter_get(dpif
, meter_id_
, stats
, n_bands
);
5780 uint32_t meter_id
= meter_id_
.uint32
;
5782 meter_lock(dp
, meter_id
);
5783 dp_delete_meter(dp
, meter_id
);
5784 meter_unlock(dp
, meter_id
);
5791 dpif_netdev_disable_upcall(struct dpif
*dpif
)
5792 OVS_NO_THREAD_SAFETY_ANALYSIS
5794 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5795 dp_netdev_disable_upcall(dp
);
5799 dp_netdev_enable_upcall(struct dp_netdev
*dp
)
5800 OVS_RELEASES(dp
->upcall_rwlock
)
5802 fat_rwlock_unlock(&dp
->upcall_rwlock
);
5806 dpif_netdev_enable_upcall(struct dpif
*dpif
)
5807 OVS_NO_THREAD_SAFETY_ANALYSIS
5809 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5810 dp_netdev_enable_upcall(dp
);
5814 dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
)
5816 ovs_mutex_lock(&pmd
->cond_mutex
);
5817 atomic_store_relaxed(&pmd
->reload
, false);
5818 pmd
->last_reload_seq
= seq_read(pmd
->reload_seq
);
5819 xpthread_cond_signal(&pmd
->cond
);
5820 ovs_mutex_unlock(&pmd
->cond_mutex
);
5823 /* Finds and refs the dp_netdev_pmd_thread on core 'core_id'. Returns
5824 * the pointer if succeeds, otherwise, NULL (it can return NULL even if
5825 * 'core_id' is NON_PMD_CORE_ID).
5827 * Caller must unrefs the returned reference. */
5828 static struct dp_netdev_pmd_thread
*
5829 dp_netdev_get_pmd(struct dp_netdev
*dp
, unsigned core_id
)
5831 struct dp_netdev_pmd_thread
*pmd
;
5832 const struct cmap_node
*pnode
;
5834 pnode
= cmap_find(&dp
->poll_threads
, hash_int(core_id
, 0));
5838 pmd
= CONTAINER_OF(pnode
, struct dp_netdev_pmd_thread
, node
);
5840 return dp_netdev_pmd_try_ref(pmd
) ? pmd
: NULL
;
5843 /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */
5845 dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
5846 OVS_REQUIRES(dp
->port_mutex
)
5848 struct dp_netdev_pmd_thread
*non_pmd
;
5850 non_pmd
= xzalloc(sizeof *non_pmd
);
5851 dp_netdev_configure_pmd(non_pmd
, dp
, NON_PMD_CORE_ID
, OVS_NUMA_UNSPEC
);
5854 /* Caller must have valid pointer to 'pmd'. */
5856 dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
)
5858 return ovs_refcount_try_ref_rcu(&pmd
->ref_cnt
);
5862 dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
)
5864 if (pmd
&& ovs_refcount_unref(&pmd
->ref_cnt
) == 1) {
5865 ovsrcu_postpone(dp_netdev_destroy_pmd
, pmd
);
5869 /* Given cmap position 'pos', tries to ref the next node. If try_ref()
5870 * fails, keeps checking for next node until reaching the end of cmap.
5872 * Caller must unrefs the returned reference. */
5873 static struct dp_netdev_pmd_thread
*
5874 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
)
5876 struct dp_netdev_pmd_thread
*next
;
5879 struct cmap_node
*node
;
5881 node
= cmap_next_position(&dp
->poll_threads
, pos
);
5882 next
= node
? CONTAINER_OF(node
, struct dp_netdev_pmd_thread
, node
)
5884 } while (next
&& !dp_netdev_pmd_try_ref(next
));
5889 /* Configures the 'pmd' based on the input argument. */
5891 dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
, struct dp_netdev
*dp
,
5892 unsigned core_id
, int numa_id
)
5895 pmd
->core_id
= core_id
;
5896 pmd
->numa_id
= numa_id
;
5897 pmd
->need_reload
= false;
5898 pmd
->n_output_batches
= 0;
5900 ovs_refcount_init(&pmd
->ref_cnt
);
5901 latch_init(&pmd
->exit_latch
);
5902 pmd
->reload_seq
= seq_create();
5903 pmd
->last_reload_seq
= seq_read(pmd
->reload_seq
);
5904 atomic_init(&pmd
->reload
, false);
5905 xpthread_cond_init(&pmd
->cond
, NULL
);
5906 ovs_mutex_init(&pmd
->cond_mutex
);
5907 ovs_mutex_init(&pmd
->flow_mutex
);
5908 ovs_mutex_init(&pmd
->port_mutex
);
5909 cmap_init(&pmd
->flow_table
);
5910 cmap_init(&pmd
->classifiers
);
5911 pmd
->ctx
.last_rxq
= NULL
;
5912 pmd_thread_ctx_time_update(pmd
);
5913 pmd
->next_optimization
= pmd
->ctx
.now
+ DPCLS_OPTIMIZATION_INTERVAL
;
5914 pmd
->rxq_next_cycle_store
= pmd
->ctx
.now
+ PMD_RXQ_INTERVAL_LEN
;
5915 hmap_init(&pmd
->poll_list
);
5916 hmap_init(&pmd
->tx_ports
);
5917 hmap_init(&pmd
->tnl_port_cache
);
5918 hmap_init(&pmd
->send_port_cache
);
5919 /* init the 'flow_cache' since there is no
5920 * actual thread created for NON_PMD_CORE_ID. */
5921 if (core_id
== NON_PMD_CORE_ID
) {
5922 dfc_cache_init(&pmd
->flow_cache
);
5923 pmd_alloc_static_tx_qid(pmd
);
5925 pmd_perf_stats_init(&pmd
->perf_stats
);
5926 cmap_insert(&dp
->poll_threads
, CONST_CAST(struct cmap_node
*, &pmd
->node
),
5927 hash_int(core_id
, 0));
5931 dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
)
5935 dp_netdev_pmd_flow_flush(pmd
);
5936 hmap_destroy(&pmd
->send_port_cache
);
5937 hmap_destroy(&pmd
->tnl_port_cache
);
5938 hmap_destroy(&pmd
->tx_ports
);
5939 hmap_destroy(&pmd
->poll_list
);
5940 /* All flows (including their dpcls_rules) have been deleted already */
5941 CMAP_FOR_EACH (cls
, node
, &pmd
->classifiers
) {
5943 ovsrcu_postpone(free
, cls
);
5945 cmap_destroy(&pmd
->classifiers
);
5946 cmap_destroy(&pmd
->flow_table
);
5947 ovs_mutex_destroy(&pmd
->flow_mutex
);
5948 latch_destroy(&pmd
->exit_latch
);
5949 seq_destroy(pmd
->reload_seq
);
5950 xpthread_cond_destroy(&pmd
->cond
);
5951 ovs_mutex_destroy(&pmd
->cond_mutex
);
5952 ovs_mutex_destroy(&pmd
->port_mutex
);
5956 /* Stops the pmd thread, removes it from the 'dp->poll_threads',
5957 * and unrefs the struct. */
5959 dp_netdev_del_pmd(struct dp_netdev
*dp
, struct dp_netdev_pmd_thread
*pmd
)
5961 /* NON_PMD_CORE_ID doesn't have a thread, so we don't have to synchronize,
5962 * but extra cleanup is necessary */
5963 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
5964 ovs_mutex_lock(&dp
->non_pmd_mutex
);
5965 dfc_cache_uninit(&pmd
->flow_cache
);
5966 pmd_free_cached_ports(pmd
);
5967 pmd_free_static_tx_qid(pmd
);
5968 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
5970 latch_set(&pmd
->exit_latch
);
5971 dp_netdev_reload_pmd__(pmd
);
5972 xpthread_join(pmd
->thread
, NULL
);
5975 dp_netdev_pmd_clear_ports(pmd
);
5977 /* Purges the 'pmd''s flows after stopping the thread, but before
5978 * destroying the flows, so that the flow stats can be collected. */
5979 if (dp
->dp_purge_cb
) {
5980 dp
->dp_purge_cb(dp
->dp_purge_aux
, pmd
->core_id
);
5982 cmap_remove(&pmd
->dp
->poll_threads
, &pmd
->node
, hash_int(pmd
->core_id
, 0));
5983 dp_netdev_pmd_unref(pmd
);
5986 /* Destroys all pmd threads. If 'non_pmd' is true it also destroys the non pmd
5989 dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
, bool non_pmd
)
5991 struct dp_netdev_pmd_thread
*pmd
;
5992 struct dp_netdev_pmd_thread
**pmd_list
;
5993 size_t k
= 0, n_pmds
;
5995 n_pmds
= cmap_count(&dp
->poll_threads
);
5996 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
5998 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5999 if (!non_pmd
&& pmd
->core_id
== NON_PMD_CORE_ID
) {
6002 /* We cannot call dp_netdev_del_pmd(), since it alters
6003 * 'dp->poll_threads' (while we're iterating it) and it
6005 ovs_assert(k
< n_pmds
);
6006 pmd_list
[k
++] = pmd
;
6009 for (size_t i
= 0; i
< k
; i
++) {
6010 dp_netdev_del_pmd(dp
, pmd_list
[i
]);
6015 /* Deletes all rx queues from pmd->poll_list and all the ports from
6018 dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
)
6020 struct rxq_poll
*poll
;
6021 struct tx_port
*port
;
6023 ovs_mutex_lock(&pmd
->port_mutex
);
6024 HMAP_FOR_EACH_POP (poll
, node
, &pmd
->poll_list
) {
6027 HMAP_FOR_EACH_POP (port
, node
, &pmd
->tx_ports
) {
6030 ovs_mutex_unlock(&pmd
->port_mutex
);
6033 /* Adds rx queue to poll_list of PMD thread, if it's not there already. */
6035 dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
6036 struct dp_netdev_rxq
*rxq
)
6037 OVS_REQUIRES(pmd
->port_mutex
)
6039 int qid
= netdev_rxq_get_queue_id(rxq
->rx
);
6040 uint32_t hash
= hash_2words(odp_to_u32(rxq
->port
->port_no
), qid
);
6041 struct rxq_poll
*poll
;
6043 HMAP_FOR_EACH_WITH_HASH (poll
, node
, hash
, &pmd
->poll_list
) {
6044 if (poll
->rxq
== rxq
) {
6045 /* 'rxq' is already polled by this thread. Do nothing. */
6050 poll
= xmalloc(sizeof *poll
);
6052 hmap_insert(&pmd
->poll_list
, &poll
->node
, hash
);
6054 pmd
->need_reload
= true;
6057 /* Delete 'poll' from poll_list of PMD thread. */
6059 dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
6060 struct rxq_poll
*poll
)
6061 OVS_REQUIRES(pmd
->port_mutex
)
6063 hmap_remove(&pmd
->poll_list
, &poll
->node
);
6066 pmd
->need_reload
= true;
6069 /* Add 'port' to the tx port cache of 'pmd', which must be reloaded for the
6070 * changes to take effect. */
6072 dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
6073 struct dp_netdev_port
*port
)
6074 OVS_REQUIRES(pmd
->port_mutex
)
6078 tx
= tx_port_lookup(&pmd
->tx_ports
, port
->port_no
);
6080 /* 'port' is already on this thread tx cache. Do nothing. */
6084 tx
= xzalloc(sizeof *tx
);
6088 tx
->flush_time
= 0LL;
6089 dp_packet_batch_init(&tx
->output_pkts
);
6091 hmap_insert(&pmd
->tx_ports
, &tx
->node
, hash_port_no(tx
->port
->port_no
));
6092 pmd
->need_reload
= true;
6095 /* Del 'tx' from the tx port cache of 'pmd', which must be reloaded for the
6096 * changes to take effect. */
6098 dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
6100 OVS_REQUIRES(pmd
->port_mutex
)
6102 hmap_remove(&pmd
->tx_ports
, &tx
->node
);
6104 pmd
->need_reload
= true;
6108 dpif_netdev_get_datapath_version(void)
6110 return xstrdup("<built-in>");
6114 dp_netdev_flow_used(struct dp_netdev_flow
*netdev_flow
, int cnt
, int size
,
6115 uint16_t tcp_flags
, long long now
)
6119 atomic_store_relaxed(&netdev_flow
->stats
.used
, now
);
6120 non_atomic_ullong_add(&netdev_flow
->stats
.packet_count
, cnt
);
6121 non_atomic_ullong_add(&netdev_flow
->stats
.byte_count
, size
);
6122 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
6124 atomic_store_relaxed(&netdev_flow
->stats
.tcp_flags
, flags
);
6128 dp_netdev_upcall(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet
*packet_
,
6129 struct flow
*flow
, struct flow_wildcards
*wc
, ovs_u128
*ufid
,
6130 enum dpif_upcall_type type
, const struct nlattr
*userdata
,
6131 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
6133 struct dp_netdev
*dp
= pmd
->dp
;
6135 if (OVS_UNLIKELY(!dp
->upcall_cb
)) {
6139 if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl
))) {
6140 struct ds ds
= DS_EMPTY_INITIALIZER
;
6143 struct odp_flow_key_parms odp_parms
= {
6145 .mask
= wc
? &wc
->masks
: NULL
,
6146 .support
= dp_netdev_support
,
6149 ofpbuf_init(&key
, 0);
6150 odp_flow_key_from_flow(&odp_parms
, &key
);
6151 packet_str
= ofp_dp_packet_to_string(packet_
);
6153 odp_flow_key_format(key
.data
, key
.size
, &ds
);
6155 VLOG_DBG("%s: %s upcall:\n%s\n%s", dp
->name
,
6156 dpif_upcall_type_to_string(type
), ds_cstr(&ds
), packet_str
);
6158 ofpbuf_uninit(&key
);
6164 return dp
->upcall_cb(packet_
, flow
, ufid
, pmd
->core_id
, type
, userdata
,
6165 actions
, wc
, put_actions
, dp
->upcall_aux
);
6168 static inline uint32_t
6169 dpif_netdev_packet_get_rss_hash_orig_pkt(struct dp_packet
*packet
,
6170 const struct miniflow
*mf
)
6174 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
6175 hash
= dp_packet_get_rss_hash(packet
);
6177 hash
= miniflow_hash_5tuple(mf
, 0);
6178 dp_packet_set_rss_hash(packet
, hash
);
6184 static inline uint32_t
6185 dpif_netdev_packet_get_rss_hash(struct dp_packet
*packet
,
6186 const struct miniflow
*mf
)
6188 uint32_t hash
, recirc_depth
;
6190 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
6191 hash
= dp_packet_get_rss_hash(packet
);
6193 hash
= miniflow_hash_5tuple(mf
, 0);
6194 dp_packet_set_rss_hash(packet
, hash
);
6197 /* The RSS hash must account for the recirculation depth to avoid
6198 * collisions in the exact match cache */
6199 recirc_depth
= *recirc_depth_get_unsafe();
6200 if (OVS_UNLIKELY(recirc_depth
)) {
6201 hash
= hash_finish(hash
, recirc_depth
);
6202 dp_packet_set_rss_hash(packet
, hash
);
6207 struct packet_batch_per_flow
{
6208 unsigned int byte_count
;
6210 struct dp_netdev_flow
*flow
;
6212 struct dp_packet_batch array
;
6216 packet_batch_per_flow_update(struct packet_batch_per_flow
*batch
,
6217 struct dp_packet
*packet
,
6220 batch
->byte_count
+= dp_packet_size(packet
);
6221 batch
->tcp_flags
|= tcp_flags
;
6222 batch
->array
.packets
[batch
->array
.count
++] = packet
;
6226 packet_batch_per_flow_init(struct packet_batch_per_flow
*batch
,
6227 struct dp_netdev_flow
*flow
)
6229 flow
->batch
= batch
;
6232 dp_packet_batch_init(&batch
->array
);
6233 batch
->byte_count
= 0;
6234 batch
->tcp_flags
= 0;
6238 packet_batch_per_flow_execute(struct packet_batch_per_flow
*batch
,
6239 struct dp_netdev_pmd_thread
*pmd
)
6241 struct dp_netdev_actions
*actions
;
6242 struct dp_netdev_flow
*flow
= batch
->flow
;
6244 dp_netdev_flow_used(flow
, batch
->array
.count
, batch
->byte_count
,
6245 batch
->tcp_flags
, pmd
->ctx
.now
/ 1000);
6247 actions
= dp_netdev_flow_get_actions(flow
);
6249 dp_netdev_execute_actions(pmd
, &batch
->array
, true, &flow
->flow
,
6250 actions
->actions
, actions
->size
);
6254 dp_netdev_queue_batches(struct dp_packet
*pkt
,
6255 struct dp_netdev_flow
*flow
, uint16_t tcp_flags
,
6256 struct packet_batch_per_flow
*batches
,
6259 struct packet_batch_per_flow
*batch
= flow
->batch
;
6261 if (OVS_UNLIKELY(!batch
)) {
6262 batch
= &batches
[(*n_batches
)++];
6263 packet_batch_per_flow_init(batch
, flow
);
6266 packet_batch_per_flow_update(batch
, pkt
, tcp_flags
);
6270 packet_enqueue_to_flow_map(struct dp_packet
*packet
,
6271 struct dp_netdev_flow
*flow
,
6273 struct dp_packet_flow_map
*flow_map
,
6276 struct dp_packet_flow_map
*map
= &flow_map
[index
];
6278 map
->packet
= packet
;
6279 map
->tcp_flags
= tcp_flags
;
6282 /* SMC lookup function for a batch of packets.
6283 * By doing batching SMC lookup, we can use prefetch
6284 * to hide memory access latency.
6287 smc_lookup_batch(struct dp_netdev_pmd_thread
*pmd
,
6288 struct netdev_flow_key
*keys
,
6289 struct netdev_flow_key
**missed_keys
,
6290 struct dp_packet_batch
*packets_
,
6292 struct dp_packet_flow_map
*flow_map
,
6296 struct dp_packet
*packet
;
6297 size_t n_smc_hit
= 0, n_missed
= 0;
6298 struct dfc_cache
*cache
= &pmd
->flow_cache
;
6299 struct smc_cache
*smc_cache
= &cache
->smc_cache
;
6300 const struct cmap_node
*flow_node
;
6304 /* Prefetch buckets for all packets */
6305 for (i
= 0; i
< cnt
; i
++) {
6306 OVS_PREFETCH(&smc_cache
->buckets
[keys
[i
].hash
& SMC_MASK
]);
6309 DP_PACKET_BATCH_REFILL_FOR_EACH (i
, cnt
, packet
, packets_
) {
6310 struct dp_netdev_flow
*flow
= NULL
;
6311 flow_node
= smc_entry_get(pmd
, keys
[i
].hash
);
6313 /* Get the original order of this packet in received batch. */
6314 recv_idx
= index_map
[i
];
6316 if (OVS_LIKELY(flow_node
!= NULL
)) {
6317 CMAP_NODE_FOR_EACH (flow
, node
, flow_node
) {
6318 /* Since we dont have per-port megaflow to check the port
6319 * number, we need to verify that the input ports match. */
6320 if (OVS_LIKELY(dpcls_rule_matches_key(&flow
->cr
, &keys
[i
]) &&
6321 flow
->flow
.in_port
.odp_port
== packet
->md
.in_port
.odp_port
)) {
6322 tcp_flags
= miniflow_get_tcp_flags(&keys
[i
].mf
);
6324 /* SMC hit and emc miss, we insert into EMC */
6326 netdev_flow_key_size(miniflow_n_values(&keys
[i
].mf
));
6327 emc_probabilistic_insert(pmd
, &keys
[i
], flow
);
6328 /* Add these packets into the flow map in the same order
6331 packet_enqueue_to_flow_map(packet
, flow
, tcp_flags
,
6332 flow_map
, recv_idx
);
6343 /* SMC missed. Group missed packets together at
6344 * the beginning of the 'packets' array. */
6345 dp_packet_batch_refill(packets_
, packet
, i
);
6347 /* Preserve the order of packet for flow batching. */
6348 index_map
[n_missed
] = recv_idx
;
6350 /* Put missed keys to the pointer arrays return to the caller */
6351 missed_keys
[n_missed
++] = &keys
[i
];
6354 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_SMC_HIT
, n_smc_hit
);
6357 /* Try to process all ('cnt') the 'packets' using only the datapath flow cache
6358 * 'pmd->flow_cache'. If a flow is not found for a packet 'packets[i]', the
6359 * miniflow is copied into 'keys' and the packet pointer is moved at the
6360 * beginning of the 'packets' array. The pointers of missed keys are put in the
6361 * missed_keys pointer array for future processing.
6363 * The function returns the number of packets that needs to be processed in the
6364 * 'packets' array (they have been moved to the beginning of the vector).
6366 * For performance reasons a caller may choose not to initialize the metadata
6367 * in 'packets_'. If 'md_is_valid' is false, the metadata in 'packets'
6368 * is not valid and must be initialized by this function using 'port_no'.
6369 * If 'md_is_valid' is true, the metadata is already valid and 'port_no'
6372 static inline size_t
6373 dfc_processing(struct dp_netdev_pmd_thread
*pmd
,
6374 struct dp_packet_batch
*packets_
,
6375 struct netdev_flow_key
*keys
,
6376 struct netdev_flow_key
**missed_keys
,
6377 struct packet_batch_per_flow batches
[], size_t *n_batches
,
6378 struct dp_packet_flow_map
*flow_map
,
6379 size_t *n_flows
, uint8_t *index_map
,
6380 bool md_is_valid
, odp_port_t port_no
)
6382 struct netdev_flow_key
*key
= &keys
[0];
6383 size_t n_missed
= 0, n_emc_hit
= 0;
6384 struct dfc_cache
*cache
= &pmd
->flow_cache
;
6385 struct dp_packet
*packet
;
6386 const size_t cnt
= dp_packet_batch_size(packets_
);
6387 uint32_t cur_min
= pmd
->ctx
.emc_insert_min
;
6392 bool batch_enable
= true;
6394 atomic_read_relaxed(&pmd
->dp
->smc_enable_db
, &smc_enable_db
);
6395 pmd_perf_update_counter(&pmd
->perf_stats
,
6396 md_is_valid
? PMD_STAT_RECIRC
: PMD_STAT_RECV
,
6399 DP_PACKET_BATCH_REFILL_FOR_EACH (i
, cnt
, packet
, packets_
) {
6400 struct dp_netdev_flow
*flow
;
6403 if (OVS_UNLIKELY(dp_packet_size(packet
) < ETH_HEADER_LEN
)) {
6404 dp_packet_delete(packet
);
6409 struct dp_packet
**packets
= packets_
->packets
;
6410 /* Prefetch next packet data and metadata. */
6411 OVS_PREFETCH(dp_packet_data(packets
[i
+1]));
6412 pkt_metadata_prefetch_init(&packets
[i
+1]->md
);
6416 pkt_metadata_init(&packet
->md
, port_no
);
6419 if ((*recirc_depth_get() == 0) &&
6420 dp_packet_has_flow_mark(packet
, &mark
)) {
6421 flow
= mark_to_flow_find(pmd
, mark
);
6422 if (OVS_LIKELY(flow
)) {
6423 tcp_flags
= parse_tcp_flags(packet
);
6424 if (OVS_LIKELY(batch_enable
)) {
6425 dp_netdev_queue_batches(packet
, flow
, tcp_flags
, batches
,
6428 /* Flow batching should be performed only after fast-path
6429 * processing is also completed for packets with emc miss
6430 * or else it will result in reordering of packets with
6431 * same datapath flows. */
6432 packet_enqueue_to_flow_map(packet
, flow
, tcp_flags
,
6433 flow_map
, map_cnt
++);
6439 miniflow_extract(packet
, &key
->mf
);
6440 key
->len
= 0; /* Not computed yet. */
6442 (md_is_valid
== false)
6443 ? dpif_netdev_packet_get_rss_hash_orig_pkt(packet
, &key
->mf
)
6444 : dpif_netdev_packet_get_rss_hash(packet
, &key
->mf
);
6446 /* If EMC is disabled skip emc_lookup */
6447 flow
= (cur_min
!= 0) ? emc_lookup(&cache
->emc_cache
, key
) : NULL
;
6448 if (OVS_LIKELY(flow
)) {
6449 tcp_flags
= miniflow_get_tcp_flags(&key
->mf
);
6451 if (OVS_LIKELY(batch_enable
)) {
6452 dp_netdev_queue_batches(packet
, flow
, tcp_flags
, batches
,
6455 /* Flow batching should be performed only after fast-path
6456 * processing is also completed for packets with emc miss
6457 * or else it will result in reordering of packets with
6458 * same datapath flows. */
6459 packet_enqueue_to_flow_map(packet
, flow
, tcp_flags
,
6460 flow_map
, map_cnt
++);
6463 /* Exact match cache missed. Group missed packets together at
6464 * the beginning of the 'packets' array. */
6465 dp_packet_batch_refill(packets_
, packet
, i
);
6467 /* Preserve the order of packet for flow batching. */
6468 index_map
[n_missed
] = map_cnt
;
6469 flow_map
[map_cnt
++].flow
= NULL
;
6471 /* 'key[n_missed]' contains the key of the current packet and it
6472 * will be passed to SMC lookup. The next key should be extracted
6473 * to 'keys[n_missed + 1]'.
6474 * We also maintain a pointer array to keys missed both SMC and EMC
6475 * which will be returned to the caller for future processing. */
6476 missed_keys
[n_missed
] = key
;
6477 key
= &keys
[++n_missed
];
6479 /* Skip batching for subsequent packets to avoid reordering. */
6480 batch_enable
= false;
6483 /* Count of packets which are not flow batched. */
6486 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_EXACT_HIT
, n_emc_hit
);
6488 if (!smc_enable_db
) {
6489 return dp_packet_batch_size(packets_
);
6492 /* Packets miss EMC will do a batch lookup in SMC if enabled */
6493 smc_lookup_batch(pmd
, keys
, missed_keys
, packets_
,
6494 n_missed
, flow_map
, index_map
);
6496 return dp_packet_batch_size(packets_
);
6500 handle_packet_upcall(struct dp_netdev_pmd_thread
*pmd
,
6501 struct dp_packet
*packet
,
6502 const struct netdev_flow_key
*key
,
6503 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
6505 struct ofpbuf
*add_actions
;
6506 struct dp_packet_batch b
;
6510 uint64_t cycles
= cycles_counter_update(&pmd
->perf_stats
);
6512 match
.tun_md
.valid
= false;
6513 miniflow_expand(&key
->mf
, &match
.flow
);
6515 ofpbuf_clear(actions
);
6516 ofpbuf_clear(put_actions
);
6518 dpif_flow_hash(pmd
->dp
->dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
6519 error
= dp_netdev_upcall(pmd
, packet
, &match
.flow
, &match
.wc
,
6520 &ufid
, DPIF_UC_MISS
, NULL
, actions
,
6522 if (OVS_UNLIKELY(error
&& error
!= ENOSPC
)) {
6523 dp_packet_delete(packet
);
6527 /* The Netlink encoding of datapath flow keys cannot express
6528 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
6529 * tag is interpreted as exact match on the fact that there is no
6530 * VLAN. Unless we refactor a lot of code that translates between
6531 * Netlink and struct flow representations, we have to do the same
6532 * here. This must be in sync with 'match' in dpif_netdev_flow_put(). */
6533 if (!match
.wc
.masks
.vlans
[0].tci
) {
6534 match
.wc
.masks
.vlans
[0].tci
= htons(0xffff);
6537 /* We can't allow the packet batching in the next loop to execute
6538 * the actions. Otherwise, if there are any slow path actions,
6539 * we'll send the packet up twice. */
6540 dp_packet_batch_init_packet(&b
, packet
);
6541 dp_netdev_execute_actions(pmd
, &b
, true, &match
.flow
,
6542 actions
->data
, actions
->size
);
6544 add_actions
= put_actions
->size
? put_actions
: actions
;
6545 if (OVS_LIKELY(error
!= ENOSPC
)) {
6546 struct dp_netdev_flow
*netdev_flow
;
6548 /* XXX: There's a race window where a flow covering this packet
6549 * could have already been installed since we last did the flow
6550 * lookup before upcall. This could be solved by moving the
6551 * mutex lock outside the loop, but that's an awful long time
6552 * to be locking everyone out of making flow installs. If we
6553 * move to a per-core classifier, it would be reasonable. */
6554 ovs_mutex_lock(&pmd
->flow_mutex
);
6555 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
, NULL
);
6556 if (OVS_LIKELY(!netdev_flow
)) {
6557 netdev_flow
= dp_netdev_flow_add(pmd
, &match
, &ufid
,
6561 ovs_mutex_unlock(&pmd
->flow_mutex
);
6562 uint32_t hash
= dp_netdev_flow_hash(&netdev_flow
->ufid
);
6563 smc_insert(pmd
, key
, hash
);
6564 emc_probabilistic_insert(pmd
, key
, netdev_flow
);
6566 if (pmd_perf_metrics_enabled(pmd
)) {
6567 /* Update upcall stats. */
6568 cycles
= cycles_counter_update(&pmd
->perf_stats
) - cycles
;
6569 struct pmd_perf_stats
*s
= &pmd
->perf_stats
;
6570 s
->current
.upcalls
++;
6571 s
->current
.upcall_cycles
+= cycles
;
6572 histogram_add_sample(&s
->cycles_per_upcall
, cycles
);
6578 fast_path_processing(struct dp_netdev_pmd_thread
*pmd
,
6579 struct dp_packet_batch
*packets_
,
6580 struct netdev_flow_key
**keys
,
6581 struct dp_packet_flow_map
*flow_map
,
6585 const size_t cnt
= dp_packet_batch_size(packets_
);
6586 #if !defined(__CHECKER__) && !defined(_WIN32)
6587 const size_t PKT_ARRAY_SIZE
= cnt
;
6589 /* Sparse or MSVC doesn't like variable length array. */
6590 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
6592 struct dp_packet
*packet
;
6594 struct dpcls_rule
*rules
[PKT_ARRAY_SIZE
];
6595 struct dp_netdev
*dp
= pmd
->dp
;
6596 int upcall_ok_cnt
= 0, upcall_fail_cnt
= 0;
6597 int lookup_cnt
= 0, add_lookup_cnt
;
6600 for (size_t i
= 0; i
< cnt
; i
++) {
6601 /* Key length is needed in all the cases, hash computed on demand. */
6602 keys
[i
]->len
= netdev_flow_key_size(miniflow_n_values(&keys
[i
]->mf
));
6604 /* Get the classifier for the in_port */
6605 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
6606 if (OVS_LIKELY(cls
)) {
6607 any_miss
= !dpcls_lookup(cls
, (const struct netdev_flow_key
**)keys
,
6608 rules
, cnt
, &lookup_cnt
);
6611 memset(rules
, 0, sizeof(rules
));
6613 if (OVS_UNLIKELY(any_miss
) && !fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
6614 uint64_t actions_stub
[512 / 8], slow_stub
[512 / 8];
6615 struct ofpbuf actions
, put_actions
;
6617 ofpbuf_use_stub(&actions
, actions_stub
, sizeof actions_stub
);
6618 ofpbuf_use_stub(&put_actions
, slow_stub
, sizeof slow_stub
);
6620 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6621 struct dp_netdev_flow
*netdev_flow
;
6623 if (OVS_LIKELY(rules
[i
])) {
6627 /* It's possible that an earlier slow path execution installed
6628 * a rule covering this flow. In this case, it's a lot cheaper
6629 * to catch it here than execute a miss. */
6630 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, keys
[i
],
6633 lookup_cnt
+= add_lookup_cnt
;
6634 rules
[i
] = &netdev_flow
->cr
;
6638 int error
= handle_packet_upcall(pmd
, packet
, keys
[i
],
6639 &actions
, &put_actions
);
6641 if (OVS_UNLIKELY(error
)) {
6648 ofpbuf_uninit(&actions
);
6649 ofpbuf_uninit(&put_actions
);
6650 fat_rwlock_unlock(&dp
->upcall_rwlock
);
6651 } else if (OVS_UNLIKELY(any_miss
)) {
6652 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6653 if (OVS_UNLIKELY(!rules
[i
])) {
6654 dp_packet_delete(packet
);
6660 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6661 struct dp_netdev_flow
*flow
;
6662 /* Get the original order of this packet in received batch. */
6663 int recv_idx
= index_map
[i
];
6666 if (OVS_UNLIKELY(!rules
[i
])) {
6670 flow
= dp_netdev_flow_cast(rules
[i
]);
6671 uint32_t hash
= dp_netdev_flow_hash(&flow
->ufid
);
6672 smc_insert(pmd
, keys
[i
], hash
);
6674 emc_probabilistic_insert(pmd
, keys
[i
], flow
);
6675 /* Add these packets into the flow map in the same order
6678 tcp_flags
= miniflow_get_tcp_flags(&keys
[i
]->mf
);
6679 packet_enqueue_to_flow_map(packet
, flow
, tcp_flags
,
6680 flow_map
, recv_idx
);
6683 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MASKED_HIT
,
6684 cnt
- upcall_ok_cnt
- upcall_fail_cnt
);
6685 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MASKED_LOOKUP
,
6687 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MISS
,
6689 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_LOST
,
6693 /* Packets enter the datapath from a port (or from recirculation) here.
6695 * When 'md_is_valid' is true the metadata in 'packets' are already valid.
6696 * When false the metadata in 'packets' need to be initialized. */
6698 dp_netdev_input__(struct dp_netdev_pmd_thread
*pmd
,
6699 struct dp_packet_batch
*packets
,
6700 bool md_is_valid
, odp_port_t port_no
)
6702 #if !defined(__CHECKER__) && !defined(_WIN32)
6703 const size_t PKT_ARRAY_SIZE
= dp_packet_batch_size(packets
);
6705 /* Sparse or MSVC doesn't like variable length array. */
6706 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
6708 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
)
6709 struct netdev_flow_key keys
[PKT_ARRAY_SIZE
];
6710 struct netdev_flow_key
*missed_keys
[PKT_ARRAY_SIZE
];
6711 struct packet_batch_per_flow batches
[PKT_ARRAY_SIZE
];
6713 struct dp_packet_flow_map flow_map
[PKT_ARRAY_SIZE
];
6714 uint8_t index_map
[PKT_ARRAY_SIZE
];
6720 dfc_processing(pmd
, packets
, keys
, missed_keys
, batches
, &n_batches
,
6721 flow_map
, &n_flows
, index_map
, md_is_valid
, port_no
);
6723 if (!dp_packet_batch_is_empty(packets
)) {
6724 /* Get ingress port from first packet's metadata. */
6725 in_port
= packets
->packets
[0]->md
.in_port
.odp_port
;
6726 fast_path_processing(pmd
, packets
, missed_keys
,
6727 flow_map
, index_map
, in_port
);
6730 /* Batch rest of packets which are in flow map. */
6731 for (i
= 0; i
< n_flows
; i
++) {
6732 struct dp_packet_flow_map
*map
= &flow_map
[i
];
6734 if (OVS_UNLIKELY(!map
->flow
)) {
6737 dp_netdev_queue_batches(map
->packet
, map
->flow
, map
->tcp_flags
,
6738 batches
, &n_batches
);
6741 /* All the flow batches need to be reset before any call to
6742 * packet_batch_per_flow_execute() as it could potentially trigger
6743 * recirculation. When a packet matching flow ‘j’ happens to be
6744 * recirculated, the nested call to dp_netdev_input__() could potentially
6745 * classify the packet as matching another flow - say 'k'. It could happen
6746 * that in the previous call to dp_netdev_input__() that same flow 'k' had
6747 * already its own batches[k] still waiting to be served. So if its
6748 * ‘batch’ member is not reset, the recirculated packet would be wrongly
6749 * appended to batches[k] of the 1st call to dp_netdev_input__(). */
6750 for (i
= 0; i
< n_batches
; i
++) {
6751 batches
[i
].flow
->batch
= NULL
;
6754 for (i
= 0; i
< n_batches
; i
++) {
6755 packet_batch_per_flow_execute(&batches
[i
], pmd
);
6760 dp_netdev_input(struct dp_netdev_pmd_thread
*pmd
,
6761 struct dp_packet_batch
*packets
,
6764 dp_netdev_input__(pmd
, packets
, false, port_no
);
6768 dp_netdev_recirculate(struct dp_netdev_pmd_thread
*pmd
,
6769 struct dp_packet_batch
*packets
)
6771 dp_netdev_input__(pmd
, packets
, true, 0);
6774 struct dp_netdev_execute_aux
{
6775 struct dp_netdev_pmd_thread
*pmd
;
6776 const struct flow
*flow
;
6780 dpif_netdev_register_dp_purge_cb(struct dpif
*dpif
, dp_purge_callback
*cb
,
6783 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6784 dp
->dp_purge_aux
= aux
;
6785 dp
->dp_purge_cb
= cb
;
6789 dpif_netdev_register_upcall_cb(struct dpif
*dpif
, upcall_callback
*cb
,
6792 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6793 dp
->upcall_aux
= aux
;
6798 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
6802 struct dp_netdev_port
*port
;
6805 HMAP_FOR_EACH (tx
, node
, &pmd
->send_port_cache
) {
6806 if (!tx
->port
->dynamic_txqs
) {
6809 interval
= pmd
->ctx
.now
- tx
->last_used
;
6810 if (tx
->qid
>= 0 && (purge
|| interval
>= XPS_TIMEOUT
)) {
6812 ovs_mutex_lock(&port
->txq_used_mutex
);
6813 port
->txq_used
[tx
->qid
]--;
6814 ovs_mutex_unlock(&port
->txq_used_mutex
);
6821 dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
6824 struct dp_netdev_port
*port
;
6826 int i
, min_cnt
, min_qid
;
6828 interval
= pmd
->ctx
.now
- tx
->last_used
;
6829 tx
->last_used
= pmd
->ctx
.now
;
6831 if (OVS_LIKELY(tx
->qid
>= 0 && interval
< XPS_TIMEOUT
)) {
6837 ovs_mutex_lock(&port
->txq_used_mutex
);
6839 port
->txq_used
[tx
->qid
]--;
6845 for (i
= 0; i
< netdev_n_txq(port
->netdev
); i
++) {
6846 if (port
->txq_used
[i
] < min_cnt
|| min_cnt
== -1) {
6847 min_cnt
= port
->txq_used
[i
];
6852 port
->txq_used
[min_qid
]++;
6855 ovs_mutex_unlock(&port
->txq_used_mutex
);
6857 dpif_netdev_xps_revalidate_pmd(pmd
, false);
6859 VLOG_DBG("Core %d: New TX queue ID %d for port \'%s\'.",
6860 pmd
->core_id
, tx
->qid
, netdev_get_name(tx
->port
->netdev
));
6864 static struct tx_port
*
6865 pmd_tnl_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
6868 return tx_port_lookup(&pmd
->tnl_port_cache
, port_no
);
6871 static struct tx_port
*
6872 pmd_send_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
6875 return tx_port_lookup(&pmd
->send_port_cache
, port_no
);
6879 push_tnl_action(const struct dp_netdev_pmd_thread
*pmd
,
6880 const struct nlattr
*attr
,
6881 struct dp_packet_batch
*batch
)
6883 struct tx_port
*tun_port
;
6884 const struct ovs_action_push_tnl
*data
;
6887 data
= nl_attr_get(attr
);
6889 tun_port
= pmd_tnl_port_cache_lookup(pmd
, data
->tnl_port
);
6894 err
= netdev_push_header(tun_port
->port
->netdev
, batch
, data
);
6899 dp_packet_delete_batch(batch
, true);
6904 dp_execute_userspace_action(struct dp_netdev_pmd_thread
*pmd
,
6905 struct dp_packet
*packet
, bool should_steal
,
6906 struct flow
*flow
, ovs_u128
*ufid
,
6907 struct ofpbuf
*actions
,
6908 const struct nlattr
*userdata
)
6910 struct dp_packet_batch b
;
6913 ofpbuf_clear(actions
);
6915 error
= dp_netdev_upcall(pmd
, packet
, flow
, NULL
, ufid
,
6916 DPIF_UC_ACTION
, userdata
, actions
,
6918 if (!error
|| error
== ENOSPC
) {
6919 dp_packet_batch_init_packet(&b
, packet
);
6920 dp_netdev_execute_actions(pmd
, &b
, should_steal
, flow
,
6921 actions
->data
, actions
->size
);
6922 } else if (should_steal
) {
6923 dp_packet_delete(packet
);
6928 dp_execute_cb(void *aux_
, struct dp_packet_batch
*packets_
,
6929 const struct nlattr
*a
, bool should_steal
)
6930 OVS_NO_THREAD_SAFETY_ANALYSIS
6932 struct dp_netdev_execute_aux
*aux
= aux_
;
6933 uint32_t *depth
= recirc_depth_get();
6934 struct dp_netdev_pmd_thread
*pmd
= aux
->pmd
;
6935 struct dp_netdev
*dp
= pmd
->dp
;
6936 int type
= nl_attr_type(a
);
6939 switch ((enum ovs_action_attr
)type
) {
6940 case OVS_ACTION_ATTR_OUTPUT
:
6941 p
= pmd_send_port_cache_lookup(pmd
, nl_attr_get_odp_port(a
));
6942 if (OVS_LIKELY(p
)) {
6943 struct dp_packet
*packet
;
6944 struct dp_packet_batch out
;
6946 if (!should_steal
) {
6947 dp_packet_batch_clone(&out
, packets_
);
6948 dp_packet_batch_reset_cutlen(packets_
);
6951 dp_packet_batch_apply_cutlen(packets_
);
6954 if (OVS_UNLIKELY(!dp_packet_batch_is_empty(&p
->output_pkts
)
6955 && packets_
->packets
[0]->source
6956 != p
->output_pkts
.packets
[0]->source
)) {
6957 /* XXX: netdev-dpdk assumes that all packets in a single
6958 * output batch has the same source. Flush here to
6959 * avoid memory access issues. */
6960 dp_netdev_pmd_flush_output_on_port(pmd
, p
);
6963 if (dp_packet_batch_size(&p
->output_pkts
)
6964 + dp_packet_batch_size(packets_
) > NETDEV_MAX_BURST
) {
6965 /* Flush here to avoid overflow. */
6966 dp_netdev_pmd_flush_output_on_port(pmd
, p
);
6969 if (dp_packet_batch_is_empty(&p
->output_pkts
)) {
6970 pmd
->n_output_batches
++;
6973 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6974 p
->output_pkts_rxqs
[dp_packet_batch_size(&p
->output_pkts
)] =
6976 dp_packet_batch_add(&p
->output_pkts
, packet
);
6982 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
6984 /* We're requested to push tunnel header, but also we need to take
6985 * the ownership of these packets. Thus, we can avoid performing
6986 * the action, because the caller will not use the result anyway.
6987 * Just break to free the batch. */
6990 dp_packet_batch_apply_cutlen(packets_
);
6991 push_tnl_action(pmd
, a
, packets_
);
6994 case OVS_ACTION_ATTR_TUNNEL_POP
:
6995 if (*depth
< MAX_RECIRC_DEPTH
) {
6996 struct dp_packet_batch
*orig_packets_
= packets_
;
6997 odp_port_t portno
= nl_attr_get_odp_port(a
);
6999 p
= pmd_tnl_port_cache_lookup(pmd
, portno
);
7001 struct dp_packet_batch tnl_pkt
;
7003 if (!should_steal
) {
7004 dp_packet_batch_clone(&tnl_pkt
, packets_
);
7005 packets_
= &tnl_pkt
;
7006 dp_packet_batch_reset_cutlen(orig_packets_
);
7009 dp_packet_batch_apply_cutlen(packets_
);
7011 netdev_pop_header(p
->port
->netdev
, packets_
);
7012 if (dp_packet_batch_is_empty(packets_
)) {
7016 struct dp_packet
*packet
;
7017 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
7018 packet
->md
.in_port
.odp_port
= portno
;
7022 dp_netdev_recirculate(pmd
, packets_
);
7029 case OVS_ACTION_ATTR_USERSPACE
:
7030 if (!fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
7031 struct dp_packet_batch
*orig_packets_
= packets_
;
7032 const struct nlattr
*userdata
;
7033 struct dp_packet_batch usr_pkt
;
7034 struct ofpbuf actions
;
7039 userdata
= nl_attr_find_nested(a
, OVS_USERSPACE_ATTR_USERDATA
);
7040 ofpbuf_init(&actions
, 0);
7042 if (packets_
->trunc
) {
7043 if (!should_steal
) {
7044 dp_packet_batch_clone(&usr_pkt
, packets_
);
7045 packets_
= &usr_pkt
;
7047 dp_packet_batch_reset_cutlen(orig_packets_
);
7050 dp_packet_batch_apply_cutlen(packets_
);
7053 struct dp_packet
*packet
;
7054 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
7055 flow_extract(packet
, &flow
);
7056 dpif_flow_hash(dp
->dpif
, &flow
, sizeof flow
, &ufid
);
7057 dp_execute_userspace_action(pmd
, packet
, should_steal
, &flow
,
7058 &ufid
, &actions
, userdata
);
7062 dp_packet_delete_batch(packets_
, true);
7065 ofpbuf_uninit(&actions
);
7066 fat_rwlock_unlock(&dp
->upcall_rwlock
);
7072 case OVS_ACTION_ATTR_RECIRC
:
7073 if (*depth
< MAX_RECIRC_DEPTH
) {
7074 struct dp_packet_batch recirc_pkts
;
7076 if (!should_steal
) {
7077 dp_packet_batch_clone(&recirc_pkts
, packets_
);
7078 packets_
= &recirc_pkts
;
7081 struct dp_packet
*packet
;
7082 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
7083 packet
->md
.recirc_id
= nl_attr_get_u32(a
);
7087 dp_netdev_recirculate(pmd
, packets_
);
7093 VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
7096 case OVS_ACTION_ATTR_CT
: {
7097 const struct nlattr
*b
;
7099 bool commit
= false;
7102 const char *helper
= NULL
;
7103 const uint32_t *setmark
= NULL
;
7104 const struct ovs_key_ct_labels
*setlabel
= NULL
;
7105 struct nat_action_info_t nat_action_info
;
7106 struct nat_action_info_t
*nat_action_info_ref
= NULL
;
7107 bool nat_config
= false;
7109 NL_ATTR_FOR_EACH_UNSAFE (b
, left
, nl_attr_get(a
),
7110 nl_attr_get_size(a
)) {
7111 enum ovs_ct_attr sub_type
= nl_attr_type(b
);
7114 case OVS_CT_ATTR_FORCE_COMMIT
:
7117 case OVS_CT_ATTR_COMMIT
:
7120 case OVS_CT_ATTR_ZONE
:
7121 zone
= nl_attr_get_u16(b
);
7123 case OVS_CT_ATTR_HELPER
:
7124 helper
= nl_attr_get_string(b
);
7126 case OVS_CT_ATTR_MARK
:
7127 setmark
= nl_attr_get(b
);
7129 case OVS_CT_ATTR_LABELS
:
7130 setlabel
= nl_attr_get(b
);
7132 case OVS_CT_ATTR_EVENTMASK
:
7133 /* Silently ignored, as userspace datapath does not generate
7134 * netlink events. */
7136 case OVS_CT_ATTR_NAT
: {
7137 const struct nlattr
*b_nest
;
7138 unsigned int left_nest
;
7139 bool ip_min_specified
= false;
7140 bool proto_num_min_specified
= false;
7141 bool ip_max_specified
= false;
7142 bool proto_num_max_specified
= false;
7143 memset(&nat_action_info
, 0, sizeof nat_action_info
);
7144 nat_action_info_ref
= &nat_action_info
;
7146 NL_NESTED_FOR_EACH_UNSAFE (b_nest
, left_nest
, b
) {
7147 enum ovs_nat_attr sub_type_nest
= nl_attr_type(b_nest
);
7149 switch (sub_type_nest
) {
7150 case OVS_NAT_ATTR_SRC
:
7151 case OVS_NAT_ATTR_DST
:
7153 nat_action_info
.nat_action
|=
7154 ((sub_type_nest
== OVS_NAT_ATTR_SRC
)
7155 ? NAT_ACTION_SRC
: NAT_ACTION_DST
);
7157 case OVS_NAT_ATTR_IP_MIN
:
7158 memcpy(&nat_action_info
.min_addr
,
7159 nl_attr_get(b_nest
),
7160 nl_attr_get_size(b_nest
));
7161 ip_min_specified
= true;
7163 case OVS_NAT_ATTR_IP_MAX
:
7164 memcpy(&nat_action_info
.max_addr
,
7165 nl_attr_get(b_nest
),
7166 nl_attr_get_size(b_nest
));
7167 ip_max_specified
= true;
7169 case OVS_NAT_ATTR_PROTO_MIN
:
7170 nat_action_info
.min_port
=
7171 nl_attr_get_u16(b_nest
);
7172 proto_num_min_specified
= true;
7174 case OVS_NAT_ATTR_PROTO_MAX
:
7175 nat_action_info
.max_port
=
7176 nl_attr_get_u16(b_nest
);
7177 proto_num_max_specified
= true;
7179 case OVS_NAT_ATTR_PERSISTENT
:
7180 case OVS_NAT_ATTR_PROTO_HASH
:
7181 case OVS_NAT_ATTR_PROTO_RANDOM
:
7183 case OVS_NAT_ATTR_UNSPEC
:
7184 case __OVS_NAT_ATTR_MAX
:
7189 if (ip_min_specified
&& !ip_max_specified
) {
7190 nat_action_info
.max_addr
= nat_action_info
.min_addr
;
7192 if (proto_num_min_specified
&& !proto_num_max_specified
) {
7193 nat_action_info
.max_port
= nat_action_info
.min_port
;
7195 if (proto_num_min_specified
|| proto_num_max_specified
) {
7196 if (nat_action_info
.nat_action
& NAT_ACTION_SRC
) {
7197 nat_action_info
.nat_action
|= NAT_ACTION_SRC_PORT
;
7198 } else if (nat_action_info
.nat_action
& NAT_ACTION_DST
) {
7199 nat_action_info
.nat_action
|= NAT_ACTION_DST_PORT
;
7204 case OVS_CT_ATTR_UNSPEC
:
7205 case __OVS_CT_ATTR_MAX
:
7210 /* We won't be able to function properly in this case, hence
7211 * complain loudly. */
7212 if (nat_config
&& !commit
) {
7213 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
7214 VLOG_WARN_RL(&rl
, "NAT specified without commit.");
7217 conntrack_execute(&dp
->conntrack
, packets_
, aux
->flow
->dl_type
, force
,
7218 commit
, zone
, setmark
, setlabel
, aux
->flow
->tp_src
,
7219 aux
->flow
->tp_dst
, helper
, nat_action_info_ref
,
7220 pmd
->ctx
.now
/ 1000);
7224 case OVS_ACTION_ATTR_METER
:
7225 dp_netdev_run_meter(pmd
->dp
, packets_
, nl_attr_get_u32(a
),
7229 case OVS_ACTION_ATTR_PUSH_VLAN
:
7230 case OVS_ACTION_ATTR_POP_VLAN
:
7231 case OVS_ACTION_ATTR_PUSH_MPLS
:
7232 case OVS_ACTION_ATTR_POP_MPLS
:
7233 case OVS_ACTION_ATTR_SET
:
7234 case OVS_ACTION_ATTR_SET_MASKED
:
7235 case OVS_ACTION_ATTR_SAMPLE
:
7236 case OVS_ACTION_ATTR_HASH
:
7237 case OVS_ACTION_ATTR_UNSPEC
:
7238 case OVS_ACTION_ATTR_TRUNC
:
7239 case OVS_ACTION_ATTR_PUSH_ETH
:
7240 case OVS_ACTION_ATTR_POP_ETH
:
7241 case OVS_ACTION_ATTR_CLONE
:
7242 case OVS_ACTION_ATTR_PUSH_NSH
:
7243 case OVS_ACTION_ATTR_POP_NSH
:
7244 case OVS_ACTION_ATTR_CT_CLEAR
:
7245 case __OVS_ACTION_ATTR_MAX
:
7249 dp_packet_delete_batch(packets_
, should_steal
);
7253 dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
7254 struct dp_packet_batch
*packets
,
7255 bool should_steal
, const struct flow
*flow
,
7256 const struct nlattr
*actions
, size_t actions_len
)
7258 struct dp_netdev_execute_aux aux
= { pmd
, flow
};
7260 odp_execute_actions(&aux
, packets
, should_steal
, actions
,
7261 actions_len
, dp_execute_cb
);
7264 struct dp_netdev_ct_dump
{
7265 struct ct_dpif_dump_state up
;
7266 struct conntrack_dump dump
;
7267 struct conntrack
*ct
;
7268 struct dp_netdev
*dp
;
7272 dpif_netdev_ct_dump_start(struct dpif
*dpif
, struct ct_dpif_dump_state
**dump_
,
7273 const uint16_t *pzone
, int *ptot_bkts
)
7275 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7276 struct dp_netdev_ct_dump
*dump
;
7278 dump
= xzalloc(sizeof *dump
);
7280 dump
->ct
= &dp
->conntrack
;
7282 conntrack_dump_start(&dp
->conntrack
, &dump
->dump
, pzone
, ptot_bkts
);
7290 dpif_netdev_ct_dump_next(struct dpif
*dpif OVS_UNUSED
,
7291 struct ct_dpif_dump_state
*dump_
,
7292 struct ct_dpif_entry
*entry
)
7294 struct dp_netdev_ct_dump
*dump
;
7296 INIT_CONTAINER(dump
, dump_
, up
);
7298 return conntrack_dump_next(&dump
->dump
, entry
);
7302 dpif_netdev_ct_dump_done(struct dpif
*dpif OVS_UNUSED
,
7303 struct ct_dpif_dump_state
*dump_
)
7305 struct dp_netdev_ct_dump
*dump
;
7308 INIT_CONTAINER(dump
, dump_
, up
);
7310 err
= conntrack_dump_done(&dump
->dump
);
7318 dpif_netdev_ct_flush(struct dpif
*dpif
, const uint16_t *zone
,
7319 const struct ct_dpif_tuple
*tuple
)
7321 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7324 return conntrack_flush_tuple(&dp
->conntrack
, tuple
, zone
? *zone
: 0);
7326 return conntrack_flush(&dp
->conntrack
, zone
);
7330 dpif_netdev_ct_set_maxconns(struct dpif
*dpif
, uint32_t maxconns
)
7332 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7334 return conntrack_set_maxconns(&dp
->conntrack
, maxconns
);
7338 dpif_netdev_ct_get_maxconns(struct dpif
*dpif
, uint32_t *maxconns
)
7340 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7342 return conntrack_get_maxconns(&dp
->conntrack
, maxconns
);
7346 dpif_netdev_ct_get_nconns(struct dpif
*dpif
, uint32_t *nconns
)
7348 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7350 return conntrack_get_nconns(&dp
->conntrack
, nconns
);
7354 dpif_netdev_ipf_set_enabled(struct dpif
*dpif
, bool v6
, bool enable
)
7356 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7357 return ipf_set_enabled(conntrack_ipf_ctx(&dp
->conntrack
), v6
, enable
);
7361 dpif_netdev_ipf_set_min_frag(struct dpif
*dpif
, bool v6
, uint32_t min_frag
)
7363 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7364 return ipf_set_min_frag(conntrack_ipf_ctx(&dp
->conntrack
), v6
, min_frag
);
7368 dpif_netdev_ipf_set_max_nfrags(struct dpif
*dpif
, uint32_t max_frags
)
7370 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7371 return ipf_set_max_nfrags(conntrack_ipf_ctx(&dp
->conntrack
), max_frags
);
7374 /* Adjust this function if 'dpif_ipf_status' and 'ipf_status' were to
7377 dpif_netdev_ipf_get_status(struct dpif
*dpif
,
7378 struct dpif_ipf_status
*dpif_ipf_status
)
7380 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7381 ipf_get_status(conntrack_ipf_ctx(&dp
->conntrack
),
7382 (struct ipf_status
*) dpif_ipf_status
);
7387 dpif_netdev_ipf_dump_start(struct dpif
*dpif OVS_UNUSED
,
7388 struct ipf_dump_ctx
**ipf_dump_ctx
)
7390 return ipf_dump_start(ipf_dump_ctx
);
7394 dpif_netdev_ipf_dump_next(struct dpif
*dpif
, void *ipf_dump_ctx
, char **dump
)
7396 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
7397 return ipf_dump_next(conntrack_ipf_ctx(&dp
->conntrack
), ipf_dump_ctx
,
7402 dpif_netdev_ipf_dump_done(struct dpif
*dpif OVS_UNUSED
, void *ipf_dump_ctx
)
7404 return ipf_dump_done(ipf_dump_ctx
);
7408 const struct dpif_class dpif_netdev_class
= {
7411 dpif_netdev_enumerate
,
7412 dpif_netdev_port_open_type
,
7415 dpif_netdev_destroy
,
7418 dpif_netdev_get_stats
,
7419 dpif_netdev_port_add
,
7420 dpif_netdev_port_del
,
7421 dpif_netdev_port_set_config
,
7422 dpif_netdev_port_query_by_number
,
7423 dpif_netdev_port_query_by_name
,
7424 NULL
, /* port_get_pid */
7425 dpif_netdev_port_dump_start
,
7426 dpif_netdev_port_dump_next
,
7427 dpif_netdev_port_dump_done
,
7428 dpif_netdev_port_poll
,
7429 dpif_netdev_port_poll_wait
,
7430 dpif_netdev_flow_flush
,
7431 dpif_netdev_flow_dump_create
,
7432 dpif_netdev_flow_dump_destroy
,
7433 dpif_netdev_flow_dump_thread_create
,
7434 dpif_netdev_flow_dump_thread_destroy
,
7435 dpif_netdev_flow_dump_next
,
7436 dpif_netdev_operate
,
7437 NULL
, /* recv_set */
7438 NULL
, /* handlers_set */
7439 dpif_netdev_set_config
,
7440 dpif_netdev_queue_to_priority
,
7442 NULL
, /* recv_wait */
7443 NULL
, /* recv_purge */
7444 dpif_netdev_register_dp_purge_cb
,
7445 dpif_netdev_register_upcall_cb
,
7446 dpif_netdev_enable_upcall
,
7447 dpif_netdev_disable_upcall
,
7448 dpif_netdev_get_datapath_version
,
7449 dpif_netdev_ct_dump_start
,
7450 dpif_netdev_ct_dump_next
,
7451 dpif_netdev_ct_dump_done
,
7452 dpif_netdev_ct_flush
,
7453 dpif_netdev_ct_set_maxconns
,
7454 dpif_netdev_ct_get_maxconns
,
7455 dpif_netdev_ct_get_nconns
,
7456 NULL
, /* ct_set_limits */
7457 NULL
, /* ct_get_limits */
7458 NULL
, /* ct_del_limits */
7459 dpif_netdev_ipf_set_enabled
,
7460 dpif_netdev_ipf_set_min_frag
,
7461 dpif_netdev_ipf_set_max_nfrags
,
7462 dpif_netdev_ipf_get_status
,
7463 dpif_netdev_ipf_dump_start
,
7464 dpif_netdev_ipf_dump_next
,
7465 dpif_netdev_ipf_dump_done
,
7466 dpif_netdev_meter_get_features
,
7467 dpif_netdev_meter_set
,
7468 dpif_netdev_meter_get
,
7469 dpif_netdev_meter_del
,
7473 dpif_dummy_change_port_number(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
7474 const char *argv
[], void *aux OVS_UNUSED
)
7476 struct dp_netdev_port
*port
;
7477 struct dp_netdev
*dp
;
7480 ovs_mutex_lock(&dp_netdev_mutex
);
7481 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
7482 if (!dp
|| !dpif_netdev_class_is_dummy(dp
->class)) {
7483 ovs_mutex_unlock(&dp_netdev_mutex
);
7484 unixctl_command_reply_error(conn
, "unknown datapath or not a dummy");
7487 ovs_refcount_ref(&dp
->ref_cnt
);
7488 ovs_mutex_unlock(&dp_netdev_mutex
);
7490 ovs_mutex_lock(&dp
->port_mutex
);
7491 if (get_port_by_name(dp
, argv
[2], &port
)) {
7492 unixctl_command_reply_error(conn
, "unknown port");
7496 port_no
= u32_to_odp(atoi(argv
[3]));
7497 if (!port_no
|| port_no
== ODPP_NONE
) {
7498 unixctl_command_reply_error(conn
, "bad port number");
7501 if (dp_netdev_lookup_port(dp
, port_no
)) {
7502 unixctl_command_reply_error(conn
, "port number already in use");
7507 hmap_remove(&dp
->ports
, &port
->node
);
7508 reconfigure_datapath(dp
);
7510 /* Reinsert with new port number. */
7511 port
->port_no
= port_no
;
7512 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
7513 reconfigure_datapath(dp
);
7515 seq_change(dp
->port_seq
);
7516 unixctl_command_reply(conn
, NULL
);
7519 ovs_mutex_unlock(&dp
->port_mutex
);
7520 dp_netdev_unref(dp
);
7524 dpif_dummy_register__(const char *type
)
7526 struct dpif_class
*class;
7528 class = xmalloc(sizeof *class);
7529 *class = dpif_netdev_class
;
7530 class->type
= xstrdup(type
);
7531 dp_register_provider(class);
7535 dpif_dummy_override(const char *type
)
7540 * Ignore EAFNOSUPPORT to allow --enable-dummy=system with
7541 * a userland-only build. It's useful for testsuite.
7543 error
= dp_unregister_provider(type
);
7544 if (error
== 0 || error
== EAFNOSUPPORT
) {
7545 dpif_dummy_register__(type
);
7550 dpif_dummy_register(enum dummy_level level
)
7552 if (level
== DUMMY_OVERRIDE_ALL
) {
7557 dp_enumerate_types(&types
);
7558 SSET_FOR_EACH (type
, &types
) {
7559 dpif_dummy_override(type
);
7561 sset_destroy(&types
);
7562 } else if (level
== DUMMY_OVERRIDE_SYSTEM
) {
7563 dpif_dummy_override("system");
7566 dpif_dummy_register__("dummy");
7568 unixctl_command_register("dpif-dummy/change-port-number",
7569 "dp port new-number",
7570 3, 3, dpif_dummy_change_port_number
, NULL
);
7573 /* Datapath Classifier. */
7575 /* A set of rules that all have the same fields wildcarded. */
7576 struct dpcls_subtable
{
7577 /* The fields are only used by writers. */
7578 struct cmap_node cmap_node OVS_GUARDED
; /* Within dpcls 'subtables_map'. */
7580 /* These fields are accessed by readers. */
7581 struct cmap rules
; /* Contains "struct dpcls_rule"s. */
7582 uint32_t hit_cnt
; /* Number of match hits in subtable in current
7583 optimization interval. */
7584 struct netdev_flow_key mask
; /* Wildcards for fields (const). */
7585 /* 'mask' must be the last field, additional space is allocated here. */
7588 /* Initializes 'cls' as a classifier that initially contains no classification
7591 dpcls_init(struct dpcls
*cls
)
7593 cmap_init(&cls
->subtables_map
);
7594 pvector_init(&cls
->subtables
);
7598 dpcls_destroy_subtable(struct dpcls
*cls
, struct dpcls_subtable
*subtable
)
7600 VLOG_DBG("Destroying subtable %p for in_port %d", subtable
, cls
->in_port
);
7601 pvector_remove(&cls
->subtables
, subtable
);
7602 cmap_remove(&cls
->subtables_map
, &subtable
->cmap_node
,
7603 subtable
->mask
.hash
);
7604 cmap_destroy(&subtable
->rules
);
7605 ovsrcu_postpone(free
, subtable
);
7608 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
7609 * caller's responsibility.
7610 * May only be called after all the readers have been terminated. */
7612 dpcls_destroy(struct dpcls
*cls
)
7615 struct dpcls_subtable
*subtable
;
7617 CMAP_FOR_EACH (subtable
, cmap_node
, &cls
->subtables_map
) {
7618 ovs_assert(cmap_count(&subtable
->rules
) == 0);
7619 dpcls_destroy_subtable(cls
, subtable
);
7621 cmap_destroy(&cls
->subtables_map
);
7622 pvector_destroy(&cls
->subtables
);
7626 static struct dpcls_subtable
*
7627 dpcls_create_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
7629 struct dpcls_subtable
*subtable
;
7631 /* Need to add one. */
7632 subtable
= xmalloc(sizeof *subtable
7633 - sizeof subtable
->mask
.mf
+ mask
->len
);
7634 cmap_init(&subtable
->rules
);
7635 subtable
->hit_cnt
= 0;
7636 netdev_flow_key_clone(&subtable
->mask
, mask
);
7637 cmap_insert(&cls
->subtables_map
, &subtable
->cmap_node
, mask
->hash
);
7638 /* Add the new subtable at the end of the pvector (with no hits yet) */
7639 pvector_insert(&cls
->subtables
, subtable
, 0);
7640 VLOG_DBG("Creating %"PRIuSIZE
". subtable %p for in_port %d",
7641 cmap_count(&cls
->subtables_map
), subtable
, cls
->in_port
);
7642 pvector_publish(&cls
->subtables
);
7647 static inline struct dpcls_subtable
*
7648 dpcls_find_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
7650 struct dpcls_subtable
*subtable
;
7652 CMAP_FOR_EACH_WITH_HASH (subtable
, cmap_node
, mask
->hash
,
7653 &cls
->subtables_map
) {
7654 if (netdev_flow_key_equal(&subtable
->mask
, mask
)) {
7658 return dpcls_create_subtable(cls
, mask
);
7662 /* Periodically sort the dpcls subtable vectors according to hit counts */
7664 dpcls_sort_subtable_vector(struct dpcls
*cls
)
7666 struct pvector
*pvec
= &cls
->subtables
;
7667 struct dpcls_subtable
*subtable
;
7669 PVECTOR_FOR_EACH (subtable
, pvec
) {
7670 pvector_change_priority(pvec
, subtable
, subtable
->hit_cnt
);
7671 subtable
->hit_cnt
= 0;
7673 pvector_publish(pvec
);
7677 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread
*pmd
,
7678 struct polled_queue
*poll_list
, int poll_cnt
)
7681 uint64_t tot_idle
= 0, tot_proc
= 0;
7682 unsigned int pmd_load
= 0;
7684 if (pmd
->ctx
.now
> pmd
->rxq_next_cycle_store
) {
7686 struct pmd_auto_lb
*pmd_alb
= &pmd
->dp
->pmd_alb
;
7687 if (pmd_alb
->is_enabled
&& !pmd
->isolated
7688 && (pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_IDLE
] >=
7689 pmd
->prev_stats
[PMD_CYCLES_ITER_IDLE
])
7690 && (pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_BUSY
] >=
7691 pmd
->prev_stats
[PMD_CYCLES_ITER_BUSY
]))
7693 tot_idle
= pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_IDLE
] -
7694 pmd
->prev_stats
[PMD_CYCLES_ITER_IDLE
];
7695 tot_proc
= pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_BUSY
] -
7696 pmd
->prev_stats
[PMD_CYCLES_ITER_BUSY
];
7699 pmd_load
= ((tot_proc
* 100) / (tot_idle
+ tot_proc
));
7702 if (pmd_load
>= ALB_PMD_LOAD_THRESHOLD
) {
7703 atomic_count_inc(&pmd
->pmd_overloaded
);
7705 atomic_count_set(&pmd
->pmd_overloaded
, 0);
7709 pmd
->prev_stats
[PMD_CYCLES_ITER_IDLE
] =
7710 pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_IDLE
];
7711 pmd
->prev_stats
[PMD_CYCLES_ITER_BUSY
] =
7712 pmd
->perf_stats
.counters
.n
[PMD_CYCLES_ITER_BUSY
];
7714 /* Get the cycles that were used to process each queue and store. */
7715 for (unsigned i
= 0; i
< poll_cnt
; i
++) {
7716 uint64_t rxq_cyc_curr
= dp_netdev_rxq_get_cycles(poll_list
[i
].rxq
,
7717 RXQ_CYCLES_PROC_CURR
);
7718 dp_netdev_rxq_set_intrvl_cycles(poll_list
[i
].rxq
, rxq_cyc_curr
);
7719 dp_netdev_rxq_set_cycles(poll_list
[i
].rxq
, RXQ_CYCLES_PROC_CURR
,
7722 curr_tsc
= cycles_counter_update(&pmd
->perf_stats
);
7723 if (pmd
->intrvl_tsc_prev
) {
7724 /* There is a prev timestamp, store a new intrvl cycle count. */
7725 atomic_store_relaxed(&pmd
->intrvl_cycles
,
7726 curr_tsc
- pmd
->intrvl_tsc_prev
);
7728 pmd
->intrvl_tsc_prev
= curr_tsc
;
7729 /* Start new measuring interval */
7730 pmd
->rxq_next_cycle_store
= pmd
->ctx
.now
+ PMD_RXQ_INTERVAL_LEN
;
7733 if (pmd
->ctx
.now
> pmd
->next_optimization
) {
7734 /* Try to obtain the flow lock to block out revalidator threads.
7735 * If not possible, just try next time. */
7736 if (!ovs_mutex_trylock(&pmd
->flow_mutex
)) {
7737 /* Optimize each classifier */
7738 CMAP_FOR_EACH (cls
, node
, &pmd
->classifiers
) {
7739 dpcls_sort_subtable_vector(cls
);
7741 ovs_mutex_unlock(&pmd
->flow_mutex
);
7742 /* Start new measuring interval */
7743 pmd
->next_optimization
= pmd
->ctx
.now
7744 + DPCLS_OPTIMIZATION_INTERVAL
;
7749 /* Insert 'rule' into 'cls'. */
7751 dpcls_insert(struct dpcls
*cls
, struct dpcls_rule
*rule
,
7752 const struct netdev_flow_key
*mask
)
7754 struct dpcls_subtable
*subtable
= dpcls_find_subtable(cls
, mask
);
7756 /* Refer to subtable's mask, also for later removal. */
7757 rule
->mask
= &subtable
->mask
;
7758 cmap_insert(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
);
7761 /* Removes 'rule' from 'cls', also destructing the 'rule'. */
7763 dpcls_remove(struct dpcls
*cls
, struct dpcls_rule
*rule
)
7765 struct dpcls_subtable
*subtable
;
7767 ovs_assert(rule
->mask
);
7769 /* Get subtable from reference in rule->mask. */
7770 INIT_CONTAINER(subtable
, rule
->mask
, mask
);
7771 if (cmap_remove(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
)
7773 /* Delete empty subtable. */
7774 dpcls_destroy_subtable(cls
, subtable
);
7775 pvector_publish(&cls
->subtables
);
7779 /* Returns true if 'target' satisfies 'key' in 'mask', that is, if each 1-bit
7780 * in 'mask' the values in 'key' and 'target' are the same. */
7782 dpcls_rule_matches_key(const struct dpcls_rule
*rule
,
7783 const struct netdev_flow_key
*target
)
7785 const uint64_t *keyp
= miniflow_get_values(&rule
->flow
.mf
);
7786 const uint64_t *maskp
= miniflow_get_values(&rule
->mask
->mf
);
7789 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, target
, rule
->flow
.mf
.map
) {
7790 if (OVS_UNLIKELY((value
& *maskp
++) != *keyp
++)) {
7797 /* For each miniflow in 'keys' performs a classifier lookup writing the result
7798 * into the corresponding slot in 'rules'. If a particular entry in 'keys' is
7799 * NULL it is skipped.
7801 * This function is optimized for use in the userspace datapath and therefore
7802 * does not implement a lot of features available in the standard
7803 * classifier_lookup() function. Specifically, it does not implement
7804 * priorities, instead returning any rule which matches the flow.
7806 * Returns true if all miniflows found a corresponding rule. */
7808 dpcls_lookup(struct dpcls
*cls
, const struct netdev_flow_key
*keys
[],
7809 struct dpcls_rule
**rules
, const size_t cnt
,
7812 /* The received 'cnt' miniflows are the search-keys that will be processed
7813 * to find a matching entry into the available subtables.
7814 * The number of bits in map_type is equal to NETDEV_MAX_BURST. */
7815 typedef uint32_t map_type
;
7816 #define MAP_BITS (sizeof(map_type) * CHAR_BIT)
7817 BUILD_ASSERT_DECL(MAP_BITS
>= NETDEV_MAX_BURST
);
7819 struct dpcls_subtable
*subtable
;
7821 map_type keys_map
= TYPE_MAXIMUM(map_type
); /* Set all bits. */
7823 uint32_t hashes
[MAP_BITS
];
7824 const struct cmap_node
*nodes
[MAP_BITS
];
7826 if (cnt
!= MAP_BITS
) {
7827 keys_map
>>= MAP_BITS
- cnt
; /* Clear extra bits. */
7829 memset(rules
, 0, cnt
* sizeof *rules
);
7831 int lookups_match
= 0, subtable_pos
= 1;
7833 /* The Datapath classifier - aka dpcls - is composed of subtables.
7834 * Subtables are dynamically created as needed when new rules are inserted.
7835 * Each subtable collects rules with matches on a specific subset of packet
7836 * fields as defined by the subtable's mask. We proceed to process every
7837 * search-key against each subtable, but when a match is found for a
7838 * search-key, the search for that key can stop because the rules are
7839 * non-overlapping. */
7840 PVECTOR_FOR_EACH (subtable
, &cls
->subtables
) {
7843 /* Compute hashes for the remaining keys. Each search-key is
7844 * masked with the subtable's mask to avoid hashing the wildcarded
7846 ULLONG_FOR_EACH_1(i
, keys_map
) {
7847 hashes
[i
] = netdev_flow_key_hash_in_mask(keys
[i
],
7851 found_map
= cmap_find_batch(&subtable
->rules
, keys_map
, hashes
, nodes
);
7852 /* Check results. When the i-th bit of found_map is set, it means
7853 * that a set of nodes with a matching hash value was found for the
7854 * i-th search-key. Due to possible hash collisions we need to check
7855 * which of the found rules, if any, really matches our masked
7857 ULLONG_FOR_EACH_1(i
, found_map
) {
7858 struct dpcls_rule
*rule
;
7860 CMAP_NODE_FOR_EACH (rule
, cmap_node
, nodes
[i
]) {
7861 if (OVS_LIKELY(dpcls_rule_matches_key(rule
, keys
[i
]))) {
7863 /* Even at 20 Mpps the 32-bit hit_cnt cannot wrap
7864 * within one second optimization interval. */
7865 subtable
->hit_cnt
++;
7866 lookups_match
+= subtable_pos
;
7870 /* None of the found rules was a match. Reset the i-th bit to
7871 * keep searching this key in the next subtable. */
7872 ULLONG_SET0(found_map
, i
); /* Did not match. */
7874 ; /* Keep Sparse happy. */
7876 keys_map
&= ~found_map
; /* Clear the found rules. */
7878 if (num_lookups_p
) {
7879 *num_lookups_p
= lookups_match
;
7881 return true; /* All found. */
7885 if (num_lookups_p
) {
7886 *num_lookups_p
= lookups_match
;
7888 return false; /* Some misses. */