2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2016, 2017 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "dpif-netdev.h"
25 #include <sys/types.h>
26 #include <netinet/in.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
37 #include "conntrack.h"
41 #include "dp-packet.h"
43 #include "dpif-netdev-perf.h"
44 #include "dpif-provider.h"
46 #include "fat-rwlock.h"
52 #include "netdev-provider.h"
53 #include "netdev-vport.h"
55 #include "odp-execute.h"
57 #include "openvswitch/dynamic-string.h"
58 #include "openvswitch/list.h"
59 #include "openvswitch/match.h"
60 #include "openvswitch/ofp-parse.h"
61 #include "openvswitch/ofp-print.h"
62 #include "openvswitch/ofpbuf.h"
63 #include "openvswitch/shash.h"
64 #include "openvswitch/vlog.h"
68 #include "openvswitch/poll-loop.h"
75 #include "tnl-neigh-cache.h"
76 #include "tnl-ports.h"
81 VLOG_DEFINE_THIS_MODULE(dpif_netdev
);
83 #define FLOW_DUMP_MAX_BATCH 50
84 /* Use per thread recirc_depth to prevent recirculation loop. */
85 #define MAX_RECIRC_DEPTH 6
86 DEFINE_STATIC_PER_THREAD_DATA(uint32_t, recirc_depth
, 0)
88 /* Use instant packet send by default. */
89 #define DEFAULT_TX_FLUSH_INTERVAL 0
91 /* Configuration parameters. */
92 enum { MAX_FLOWS
= 65536 }; /* Maximum number of flows in flow table. */
93 enum { MAX_METERS
= 65536 }; /* Maximum number of meters. */
94 enum { MAX_BANDS
= 8 }; /* Maximum number of bands / meter. */
95 enum { N_METER_LOCKS
= 64 }; /* Maximum number of meters. */
97 /* Protects against changes to 'dp_netdevs'. */
98 static struct ovs_mutex dp_netdev_mutex
= OVS_MUTEX_INITIALIZER
;
100 /* Contains all 'struct dp_netdev's. */
101 static struct shash dp_netdevs
OVS_GUARDED_BY(dp_netdev_mutex
)
102 = SHASH_INITIALIZER(&dp_netdevs
);
104 static struct vlog_rate_limit upcall_rl
= VLOG_RATE_LIMIT_INIT(600, 600);
106 #define DP_NETDEV_CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \
107 | CS_INVALID | CS_REPLY_DIR | CS_TRACKED \
108 | CS_SRC_NAT | CS_DST_NAT)
109 #define DP_NETDEV_CS_UNSUPPORTED_MASK (~(uint32_t)DP_NETDEV_CS_SUPPORTED_MASK)
111 static struct odp_support dp_netdev_support
= {
112 .max_vlan_headers
= SIZE_MAX
,
113 .max_mpls_depth
= SIZE_MAX
,
119 .ct_state_nat
= true,
120 .ct_orig_tuple
= true,
121 .ct_orig_tuple6
= true,
124 /* Stores a miniflow with inline values */
126 struct netdev_flow_key
{
127 uint32_t hash
; /* Hash function differs for different users. */
128 uint32_t len
; /* Length of the following miniflow (incl. map). */
130 uint64_t buf
[FLOW_MAX_PACKET_U64S
];
133 /* Exact match cache for frequently used flows
135 * The cache uses a 32-bit hash of the packet (which can be the RSS hash) to
136 * search its entries for a miniflow that matches exactly the miniflow of the
137 * packet. It stores the 'dpcls_rule' (rule) that matches the miniflow.
139 * A cache entry holds a reference to its 'dp_netdev_flow'.
141 * A miniflow with a given hash can be in one of EM_FLOW_HASH_SEGS different
142 * entries. The 32-bit hash is split into EM_FLOW_HASH_SEGS values (each of
143 * them is EM_FLOW_HASH_SHIFT bits wide and the remainder is thrown away). Each
144 * value is the index of a cache entry where the miniflow could be.
150 * Each pmd_thread has its own private exact match cache.
151 * If dp_netdev_input is not called from a pmd thread, a mutex is used.
154 #define EM_FLOW_HASH_SHIFT 13
155 #define EM_FLOW_HASH_ENTRIES (1u << EM_FLOW_HASH_SHIFT)
156 #define EM_FLOW_HASH_MASK (EM_FLOW_HASH_ENTRIES - 1)
157 #define EM_FLOW_HASH_SEGS 2
159 /* Default EMC insert probability is 1 / DEFAULT_EM_FLOW_INSERT_INV_PROB */
160 #define DEFAULT_EM_FLOW_INSERT_INV_PROB 100
161 #define DEFAULT_EM_FLOW_INSERT_MIN (UINT32_MAX / \
162 DEFAULT_EM_FLOW_INSERT_INV_PROB)
165 struct dp_netdev_flow
*flow
;
166 struct netdev_flow_key key
; /* key.hash used for emc hash value. */
170 struct emc_entry entries
[EM_FLOW_HASH_ENTRIES
];
171 int sweep_idx
; /* For emc_cache_slow_sweep(). */
174 /* Iterate in the exact match cache through every entry that might contain a
175 * miniflow with hash 'HASH'. */
176 #define EMC_FOR_EACH_POS_WITH_HASH(EMC, CURRENT_ENTRY, HASH) \
177 for (uint32_t i__ = 0, srch_hash__ = (HASH); \
178 (CURRENT_ENTRY) = &(EMC)->entries[srch_hash__ & EM_FLOW_HASH_MASK], \
179 i__ < EM_FLOW_HASH_SEGS; \
180 i__++, srch_hash__ >>= EM_FLOW_HASH_SHIFT)
182 /* Simple non-wildcarding single-priority classifier. */
184 /* Time in microseconds between successive optimizations of the dpcls
186 #define DPCLS_OPTIMIZATION_INTERVAL 1000000LL
188 /* Time in microseconds of the interval in which rxq processing cycles used
189 * in rxq to pmd assignments is measured and stored. */
190 #define PMD_RXQ_INTERVAL_LEN 10000000LL
192 /* Number of intervals for which cycles are stored
193 * and used during rxq to pmd assignment. */
194 #define PMD_RXQ_INTERVAL_MAX 6
197 struct cmap_node node
; /* Within dp_netdev_pmd_thread.classifiers */
199 struct cmap subtables_map
;
200 struct pvector subtables
;
203 /* A rule to be inserted to the classifier. */
205 struct cmap_node cmap_node
; /* Within struct dpcls_subtable 'rules'. */
206 struct netdev_flow_key
*mask
; /* Subtable's mask. */
207 struct netdev_flow_key flow
; /* Matching key. */
208 /* 'flow' must be the last field, additional space is allocated here. */
211 static void dpcls_init(struct dpcls
*);
212 static void dpcls_destroy(struct dpcls
*);
213 static void dpcls_sort_subtable_vector(struct dpcls
*);
214 static void dpcls_insert(struct dpcls
*, struct dpcls_rule
*,
215 const struct netdev_flow_key
*mask
);
216 static void dpcls_remove(struct dpcls
*, struct dpcls_rule
*);
217 static bool dpcls_lookup(struct dpcls
*cls
,
218 const struct netdev_flow_key keys
[],
219 struct dpcls_rule
**rules
, size_t cnt
,
222 /* Set of supported meter flags */
223 #define DP_SUPPORTED_METER_FLAGS_MASK \
224 (OFPMF13_STATS | OFPMF13_PKTPS | OFPMF13_KBPS | OFPMF13_BURST)
226 /* Set of supported meter band types */
227 #define DP_SUPPORTED_METER_BAND_TYPES \
228 ( 1 << OFPMBT13_DROP )
230 struct dp_meter_band
{
231 struct ofputil_meter_band up
; /* type, prec_level, pad, rate, burst_size */
232 uint32_t bucket
; /* In 1/1000 packets (for PKTPS), or in bits (for KBPS) */
233 uint64_t packet_count
;
240 uint32_t max_delta_t
;
242 uint64_t packet_count
;
244 struct dp_meter_band bands
[];
247 /* Datapath based on the network device interface from netdev.h.
253 * Some members, marked 'const', are immutable. Accessing other members
254 * requires synchronization, as noted in more detail below.
256 * Acquisition order is, from outermost to innermost:
258 * dp_netdev_mutex (global)
263 const struct dpif_class
*const class;
264 const char *const name
;
266 struct ovs_refcount ref_cnt
;
267 atomic_flag destroyed
;
271 * Any lookup into 'ports' or any access to the dp_netdev_ports found
272 * through 'ports' requires taking 'port_mutex'. */
273 struct ovs_mutex port_mutex
;
275 struct seq
*port_seq
; /* Incremented whenever a port changes. */
277 /* The time that a packet can wait in output batch for sending. */
278 atomic_uint32_t tx_flush_interval
;
281 struct ovs_mutex meter_locks
[N_METER_LOCKS
];
282 struct dp_meter
*meters
[MAX_METERS
]; /* Meter bands. */
284 /* Probability of EMC insertions is a factor of 'emc_insert_min'.*/
285 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
) atomic_uint32_t emc_insert_min
;
286 /* Enable collection of PMD performance metrics. */
287 atomic_bool pmd_perf_metrics
;
289 /* Protects access to ofproto-dpif-upcall interface during revalidator
290 * thread synchronization. */
291 struct fat_rwlock upcall_rwlock
;
292 upcall_callback
*upcall_cb
; /* Callback function for executing upcalls. */
295 /* Callback function for notifying the purging of dp flows (during
296 * reseting pmd deletion). */
297 dp_purge_callback
*dp_purge_cb
;
300 /* Stores all 'struct dp_netdev_pmd_thread's. */
301 struct cmap poll_threads
;
302 /* id pool for per thread static_tx_qid. */
303 struct id_pool
*tx_qid_pool
;
304 struct ovs_mutex tx_qid_pool_mutex
;
306 /* Protects the access of the 'struct dp_netdev_pmd_thread'
307 * instance for non-pmd thread. */
308 struct ovs_mutex non_pmd_mutex
;
310 /* Each pmd thread will store its pointer to
311 * 'struct dp_netdev_pmd_thread' in 'per_pmd_key'. */
312 ovsthread_key_t per_pmd_key
;
314 struct seq
*reconfigure_seq
;
315 uint64_t last_reconfigure_seq
;
317 /* Cpu mask for pin of pmd threads. */
320 uint64_t last_tnl_conf_seq
;
322 struct conntrack conntrack
;
325 static void meter_lock(const struct dp_netdev
*dp
, uint32_t meter_id
)
326 OVS_ACQUIRES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
328 ovs_mutex_lock(&dp
->meter_locks
[meter_id
% N_METER_LOCKS
]);
331 static void meter_unlock(const struct dp_netdev
*dp
, uint32_t meter_id
)
332 OVS_RELEASES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
334 ovs_mutex_unlock(&dp
->meter_locks
[meter_id
% N_METER_LOCKS
]);
338 static struct dp_netdev_port
*dp_netdev_lookup_port(const struct dp_netdev
*dp
,
340 OVS_REQUIRES(dp
->port_mutex
);
342 enum rxq_cycles_counter_type
{
343 RXQ_CYCLES_PROC_CURR
, /* Cycles spent successfully polling and
344 processing packets during the current
346 RXQ_CYCLES_PROC_HIST
, /* Total cycles of all intervals that are used
347 during rxq to pmd assignment. */
351 #define XPS_TIMEOUT 500000LL /* In microseconds. */
353 /* Contained by struct dp_netdev_port's 'rxqs' member. */
354 struct dp_netdev_rxq
{
355 struct dp_netdev_port
*port
;
356 struct netdev_rxq
*rx
;
357 unsigned core_id
; /* Core to which this queue should be
358 pinned. OVS_CORE_UNSPEC if the
359 queue doesn't need to be pinned to a
361 unsigned intrvl_idx
; /* Write index for 'cycles_intrvl'. */
362 struct dp_netdev_pmd_thread
*pmd
; /* pmd thread that polls this queue. */
363 bool is_vhost
; /* Is rxq of a vhost port. */
365 /* Counters of cycles spent successfully polling and processing pkts. */
366 atomic_ullong cycles
[RXQ_N_CYCLES
];
367 /* We store PMD_RXQ_INTERVAL_MAX intervals of data for an rxq and then
368 sum them to yield the cycles used for an rxq. */
369 atomic_ullong cycles_intrvl
[PMD_RXQ_INTERVAL_MAX
];
372 /* A port in a netdev-based datapath. */
373 struct dp_netdev_port
{
375 bool dynamic_txqs
; /* If true XPS will be used. */
376 bool need_reconfigure
; /* True if we should reconfigure netdev. */
377 struct netdev
*netdev
;
378 struct hmap_node node
; /* Node in dp_netdev's 'ports'. */
379 struct netdev_saved_flags
*sf
;
380 struct dp_netdev_rxq
*rxqs
;
381 unsigned n_rxq
; /* Number of elements in 'rxqs' */
382 unsigned *txq_used
; /* Number of threads that use each tx queue. */
383 struct ovs_mutex txq_used_mutex
;
384 char *type
; /* Port type as requested by user. */
385 char *rxq_affinity_list
; /* Requested affinity of rx queues. */
388 /* Contained by struct dp_netdev_flow's 'stats' member. */
389 struct dp_netdev_flow_stats
{
390 atomic_llong used
; /* Last used time, in monotonic msecs. */
391 atomic_ullong packet_count
; /* Number of packets matched. */
392 atomic_ullong byte_count
; /* Number of bytes matched. */
393 atomic_uint16_t tcp_flags
; /* Bitwise-OR of seen tcp_flags values. */
396 /* A flow in 'dp_netdev_pmd_thread's 'flow_table'.
402 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
403 * its pmd thread's classifier. The text below calls this classifier 'cls'.
408 * The thread safety rules described here for "struct dp_netdev_flow" are
409 * motivated by two goals:
411 * - Prevent threads that read members of "struct dp_netdev_flow" from
412 * reading bad data due to changes by some thread concurrently modifying
415 * - Prevent two threads making changes to members of a given "struct
416 * dp_netdev_flow" from interfering with each other.
422 * A flow 'flow' may be accessed without a risk of being freed during an RCU
423 * grace period. Code that needs to hold onto a flow for a while
424 * should try incrementing 'flow->ref_cnt' with dp_netdev_flow_ref().
426 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
427 * flow from being deleted from 'cls' and it doesn't protect members of 'flow'
430 * Some members, marked 'const', are immutable. Accessing other members
431 * requires synchronization, as noted in more detail below.
433 struct dp_netdev_flow
{
434 const struct flow flow
; /* Unmasked flow that created this entry. */
435 /* Hash table index by unmasked flow. */
436 const struct cmap_node node
; /* In owning dp_netdev_pmd_thread's */
438 const struct cmap_node mark_node
; /* In owning flow_mark's mark_to_flow */
439 const ovs_u128 ufid
; /* Unique flow identifier. */
440 const ovs_u128 mega_ufid
; /* Unique mega flow identifier. */
441 const unsigned pmd_id
; /* The 'core_id' of pmd thread owning this */
444 /* Number of references.
445 * The classifier owns one reference.
446 * Any thread trying to keep a rule from being freed should hold its own
448 struct ovs_refcount ref_cnt
;
451 uint32_t mark
; /* Unique flow mark assigned to a flow */
454 struct dp_netdev_flow_stats stats
;
457 OVSRCU_TYPE(struct dp_netdev_actions
*) actions
;
459 /* While processing a group of input packets, the datapath uses the next
460 * member to store a pointer to the output batch for the flow. It is
461 * reset after the batch has been sent out (See dp_netdev_queue_batches(),
462 * packet_batch_per_flow_init() and packet_batch_per_flow_execute()). */
463 struct packet_batch_per_flow
*batch
;
465 /* Packet classification. */
466 struct dpcls_rule cr
; /* In owning dp_netdev's 'cls'. */
467 /* 'cr' must be the last member. */
470 static void dp_netdev_flow_unref(struct dp_netdev_flow
*);
471 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*);
472 static int dpif_netdev_flow_from_nlattrs(const struct nlattr
*, uint32_t,
473 struct flow
*, bool);
475 /* A set of datapath actions within a "struct dp_netdev_flow".
481 * A struct dp_netdev_actions 'actions' is protected with RCU. */
482 struct dp_netdev_actions
{
483 /* These members are immutable: they do not change during the struct's
485 unsigned int size
; /* Size of 'actions', in bytes. */
486 struct nlattr actions
[]; /* Sequence of OVS_ACTION_ATTR_* attributes. */
489 struct dp_netdev_actions
*dp_netdev_actions_create(const struct nlattr
*,
491 struct dp_netdev_actions
*dp_netdev_flow_get_actions(
492 const struct dp_netdev_flow
*);
493 static void dp_netdev_actions_free(struct dp_netdev_actions
*);
495 struct polled_queue
{
496 struct dp_netdev_rxq
*rxq
;
500 /* Contained by struct dp_netdev_pmd_thread's 'poll_list' member. */
502 struct dp_netdev_rxq
*rxq
;
503 struct hmap_node node
;
506 /* Contained by struct dp_netdev_pmd_thread's 'send_port_cache',
507 * 'tnl_port_cache' or 'tx_ports'. */
509 struct dp_netdev_port
*port
;
512 struct hmap_node node
;
513 long long flush_time
;
514 struct dp_packet_batch output_pkts
;
515 struct dp_netdev_rxq
*output_pkts_rxqs
[NETDEV_MAX_BURST
];
518 /* A set of properties for the current processing loop that is not directly
519 * associated with the pmd thread itself, but with the packets being
520 * processed or the short-term system configuration (for example, time).
521 * Contained by struct dp_netdev_pmd_thread's 'ctx' member. */
522 struct dp_netdev_pmd_thread_ctx
{
523 /* Latest measured time. See 'pmd_thread_ctx_time_update()'. */
525 /* RX queue from which last packet was received. */
526 struct dp_netdev_rxq
*last_rxq
;
529 /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
530 * the performance overhead of interrupt processing. Therefore netdev can
531 * not implement rx-wait for these devices. dpif-netdev needs to poll
532 * these device to check for recv buffer. pmd-thread does polling for
533 * devices assigned to itself.
535 * DPDK used PMD for accessing NIC.
537 * Note, instance with cpu core id NON_PMD_CORE_ID will be reserved for
538 * I/O of all non-pmd threads. There will be no actual thread created
541 * Each struct has its own flow cache and classifier per managed ingress port.
542 * For packets received on ingress port, a look up is done on corresponding PMD
543 * thread's flow cache and in case of a miss, lookup is performed in the
544 * corresponding classifier of port. Packets are executed with the found
545 * actions in either case.
547 struct dp_netdev_pmd_thread
{
548 struct dp_netdev
*dp
;
549 struct ovs_refcount ref_cnt
; /* Every reference must be refcount'ed. */
550 struct cmap_node node
; /* In 'dp->poll_threads'. */
552 pthread_cond_t cond
; /* For synchronizing pmd thread reload. */
553 struct ovs_mutex cond_mutex
; /* Mutex for condition variable. */
555 /* Per thread exact-match cache. Note, the instance for cpu core
556 * NON_PMD_CORE_ID can be accessed by multiple threads, and thusly
557 * need to be protected by 'non_pmd_mutex'. Every other instance
558 * will only be accessed by its own pmd thread. */
559 struct emc_cache flow_cache
;
561 /* Flow-Table and classifiers
563 * Writers of 'flow_table' must take the 'flow_mutex'. Corresponding
564 * changes to 'classifiers' must be made while still holding the
567 struct ovs_mutex flow_mutex
;
568 struct cmap flow_table OVS_GUARDED
; /* Flow table. */
570 /* One classifier per in_port polled by the pmd */
571 struct cmap classifiers
;
572 /* Periodically sort subtable vectors according to hit frequencies */
573 long long int next_optimization
;
574 /* End of the next time interval for which processing cycles
575 are stored for each polled rxq. */
576 long long int rxq_next_cycle_store
;
578 /* Last interval timestamp. */
579 uint64_t intrvl_tsc_prev
;
580 /* Last interval cycles. */
581 atomic_ullong intrvl_cycles
;
583 /* Current context of the PMD thread. */
584 struct dp_netdev_pmd_thread_ctx ctx
;
586 struct latch exit_latch
; /* For terminating the pmd thread. */
587 struct seq
*reload_seq
;
588 uint64_t last_reload_seq
;
589 atomic_bool reload
; /* Do we need to reload ports? */
591 unsigned core_id
; /* CPU core id of this pmd thread. */
592 int numa_id
; /* numa node id of this pmd thread. */
595 /* Queue id used by this pmd thread to send packets on all netdevs if
596 * XPS disabled for this netdev. All static_tx_qid's are unique and less
597 * than 'cmap_count(dp->poll_threads)'. */
598 uint32_t static_tx_qid
;
600 /* Number of filled output batches. */
601 int n_output_batches
;
603 struct ovs_mutex port_mutex
; /* Mutex for 'poll_list' and 'tx_ports'. */
604 /* List of rx queues to poll. */
605 struct hmap poll_list OVS_GUARDED
;
606 /* Map of 'tx_port's used for transmission. Written by the main thread,
607 * read by the pmd thread. */
608 struct hmap tx_ports OVS_GUARDED
;
610 /* These are thread-local copies of 'tx_ports'. One contains only tunnel
611 * ports (that support push_tunnel/pop_tunnel), the other contains ports
612 * with at least one txq (that support send). A port can be in both.
614 * There are two separate maps to make sure that we don't try to execute
615 * OUTPUT on a device which has 0 txqs or PUSH/POP on a non-tunnel device.
617 * The instances for cpu core NON_PMD_CORE_ID can be accessed by multiple
618 * threads, and thusly need to be protected by 'non_pmd_mutex'. Every
619 * other instance will only be accessed by its own pmd thread. */
620 struct hmap tnl_port_cache
;
621 struct hmap send_port_cache
;
623 /* Keep track of detailed PMD performance statistics. */
624 struct pmd_perf_stats perf_stats
;
626 /* Set to true if the pmd thread needs to be reloaded. */
630 /* Interface to netdev-based datapath. */
633 struct dp_netdev
*dp
;
634 uint64_t last_port_seq
;
637 static int get_port_by_number(struct dp_netdev
*dp
, odp_port_t port_no
,
638 struct dp_netdev_port
**portp
)
639 OVS_REQUIRES(dp
->port_mutex
);
640 static int get_port_by_name(struct dp_netdev
*dp
, const char *devname
,
641 struct dp_netdev_port
**portp
)
642 OVS_REQUIRES(dp
->port_mutex
);
643 static void dp_netdev_free(struct dp_netdev
*)
644 OVS_REQUIRES(dp_netdev_mutex
);
645 static int do_add_port(struct dp_netdev
*dp
, const char *devname
,
646 const char *type
, odp_port_t port_no
)
647 OVS_REQUIRES(dp
->port_mutex
);
648 static void do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*)
649 OVS_REQUIRES(dp
->port_mutex
);
650 static int dpif_netdev_open(const struct dpif_class
*, const char *name
,
651 bool create
, struct dpif
**);
652 static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
653 struct dp_packet_batch
*,
655 const struct flow
*flow
,
656 const struct nlattr
*actions
,
658 static void dp_netdev_input(struct dp_netdev_pmd_thread
*,
659 struct dp_packet_batch
*, odp_port_t port_no
);
660 static void dp_netdev_recirculate(struct dp_netdev_pmd_thread
*,
661 struct dp_packet_batch
*);
663 static void dp_netdev_disable_upcall(struct dp_netdev
*);
664 static void dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
);
665 static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
,
666 struct dp_netdev
*dp
, unsigned core_id
,
668 static void dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
);
669 static void dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
670 OVS_REQUIRES(dp
->port_mutex
);
672 static void *pmd_thread_main(void *);
673 static struct dp_netdev_pmd_thread
*dp_netdev_get_pmd(struct dp_netdev
*dp
,
675 static struct dp_netdev_pmd_thread
*
676 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
);
677 static void dp_netdev_del_pmd(struct dp_netdev
*dp
,
678 struct dp_netdev_pmd_thread
*pmd
);
679 static void dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
, bool non_pmd
);
680 static void dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
);
681 static void dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
682 struct dp_netdev_port
*port
)
683 OVS_REQUIRES(pmd
->port_mutex
);
684 static void dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
686 OVS_REQUIRES(pmd
->port_mutex
);
687 static void dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
688 struct dp_netdev_rxq
*rxq
)
689 OVS_REQUIRES(pmd
->port_mutex
);
690 static void dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
691 struct rxq_poll
*poll
)
692 OVS_REQUIRES(pmd
->port_mutex
);
694 dp_netdev_pmd_flush_output_packets(struct dp_netdev_pmd_thread
*pmd
,
697 static void reconfigure_datapath(struct dp_netdev
*dp
)
698 OVS_REQUIRES(dp
->port_mutex
);
699 static bool dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
);
700 static void dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
);
701 static void dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
);
702 static void pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
703 OVS_REQUIRES(pmd
->port_mutex
);
705 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread
*pmd
,
706 struct polled_queue
*poll_list
, int poll_cnt
);
708 dp_netdev_rxq_set_cycles(struct dp_netdev_rxq
*rx
,
709 enum rxq_cycles_counter_type type
,
710 unsigned long long cycles
);
712 dp_netdev_rxq_get_cycles(struct dp_netdev_rxq
*rx
,
713 enum rxq_cycles_counter_type type
);
715 dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq
*rx
,
716 unsigned long long cycles
);
718 dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq
*rx
, unsigned idx
);
720 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
722 static int dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
725 static inline bool emc_entry_alive(struct emc_entry
*ce
);
726 static void emc_clear_entry(struct emc_entry
*ce
);
728 static void dp_netdev_request_reconfigure(struct dp_netdev
*dp
);
730 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread
*pmd
);
733 emc_cache_init(struct emc_cache
*flow_cache
)
737 flow_cache
->sweep_idx
= 0;
738 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
739 flow_cache
->entries
[i
].flow
= NULL
;
740 flow_cache
->entries
[i
].key
.hash
= 0;
741 flow_cache
->entries
[i
].key
.len
= sizeof(struct miniflow
);
742 flowmap_init(&flow_cache
->entries
[i
].key
.mf
.map
);
747 emc_cache_uninit(struct emc_cache
*flow_cache
)
751 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
752 emc_clear_entry(&flow_cache
->entries
[i
]);
756 /* Check and clear dead flow references slowly (one entry at each
759 emc_cache_slow_sweep(struct emc_cache
*flow_cache
)
761 struct emc_entry
*entry
= &flow_cache
->entries
[flow_cache
->sweep_idx
];
763 if (!emc_entry_alive(entry
)) {
764 emc_clear_entry(entry
);
766 flow_cache
->sweep_idx
= (flow_cache
->sweep_idx
+ 1) & EM_FLOW_HASH_MASK
;
769 /* Updates the time in PMD threads context and should be called in three cases:
771 * 1. PMD structure initialization:
772 * - dp_netdev_configure_pmd()
774 * 2. Before processing of the new packet batch:
775 * - dpif_netdev_execute()
776 * - dp_netdev_process_rxq_port()
778 * 3. At least once per polling iteration in main polling threads if no
779 * packets received on current iteration:
780 * - dpif_netdev_run()
781 * - pmd_thread_main()
783 * 'pmd->ctx.now' should be used without update in all other cases if possible.
786 pmd_thread_ctx_time_update(struct dp_netdev_pmd_thread
*pmd
)
788 pmd
->ctx
.now
= time_usec();
791 /* Returns true if 'dpif' is a netdev or dummy dpif, false otherwise. */
793 dpif_is_netdev(const struct dpif
*dpif
)
795 return dpif
->dpif_class
->open
== dpif_netdev_open
;
798 static struct dpif_netdev
*
799 dpif_netdev_cast(const struct dpif
*dpif
)
801 ovs_assert(dpif_is_netdev(dpif
));
802 return CONTAINER_OF(dpif
, struct dpif_netdev
, dpif
);
805 static struct dp_netdev
*
806 get_dp_netdev(const struct dpif
*dpif
)
808 return dpif_netdev_cast(dpif
)->dp
;
812 PMD_INFO_SHOW_STATS
, /* Show how cpu cycles are spent. */
813 PMD_INFO_CLEAR_STATS
, /* Set the cycles count to 0. */
814 PMD_INFO_SHOW_RXQ
, /* Show poll lists of pmd threads. */
815 PMD_INFO_PERF_SHOW
, /* Show pmd performance details. */
819 format_pmd_thread(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
821 ds_put_cstr(reply
, (pmd
->core_id
== NON_PMD_CORE_ID
)
822 ? "main thread" : "pmd thread");
823 if (pmd
->numa_id
!= OVS_NUMA_UNSPEC
) {
824 ds_put_format(reply
, " numa_id %d", pmd
->numa_id
);
826 if (pmd
->core_id
!= OVS_CORE_UNSPEC
&& pmd
->core_id
!= NON_PMD_CORE_ID
) {
827 ds_put_format(reply
, " core_id %u", pmd
->core_id
);
829 ds_put_cstr(reply
, ":\n");
833 pmd_info_show_stats(struct ds
*reply
,
834 struct dp_netdev_pmd_thread
*pmd
)
836 uint64_t stats
[PMD_N_STATS
];
837 uint64_t total_cycles
, total_packets
;
838 double passes_per_pkt
= 0;
839 double lookups_per_hit
= 0;
840 double packets_per_batch
= 0;
842 pmd_perf_read_counters(&pmd
->perf_stats
, stats
);
843 total_cycles
= stats
[PMD_CYCLES_ITER_IDLE
]
844 + stats
[PMD_CYCLES_ITER_BUSY
];
845 total_packets
= stats
[PMD_STAT_RECV
];
847 format_pmd_thread(reply
, pmd
);
849 if (total_packets
> 0) {
850 passes_per_pkt
= (total_packets
+ stats
[PMD_STAT_RECIRC
])
851 / (double) total_packets
;
853 if (stats
[PMD_STAT_MASKED_HIT
] > 0) {
854 lookups_per_hit
= stats
[PMD_STAT_MASKED_LOOKUP
]
855 / (double) stats
[PMD_STAT_MASKED_HIT
];
857 if (stats
[PMD_STAT_SENT_BATCHES
] > 0) {
858 packets_per_batch
= stats
[PMD_STAT_SENT_PKTS
]
859 / (double) stats
[PMD_STAT_SENT_BATCHES
];
863 " packets received: %"PRIu64
"\n"
864 " packet recirculations: %"PRIu64
"\n"
865 " avg. datapath passes per packet: %.02f\n"
866 " emc hits: %"PRIu64
"\n"
867 " megaflow hits: %"PRIu64
"\n"
868 " avg. subtable lookups per megaflow hit: %.02f\n"
869 " miss with success upcall: %"PRIu64
"\n"
870 " miss with failed upcall: %"PRIu64
"\n"
871 " avg. packets per output batch: %.02f\n",
872 total_packets
, stats
[PMD_STAT_RECIRC
],
873 passes_per_pkt
, stats
[PMD_STAT_EXACT_HIT
],
874 stats
[PMD_STAT_MASKED_HIT
], lookups_per_hit
,
875 stats
[PMD_STAT_MISS
], stats
[PMD_STAT_LOST
],
878 if (total_cycles
== 0) {
883 " idle cycles: %"PRIu64
" (%.02f%%)\n"
884 " processing cycles: %"PRIu64
" (%.02f%%)\n",
885 stats
[PMD_CYCLES_ITER_IDLE
],
886 stats
[PMD_CYCLES_ITER_IDLE
] / (double) total_cycles
* 100,
887 stats
[PMD_CYCLES_ITER_BUSY
],
888 stats
[PMD_CYCLES_ITER_BUSY
] / (double) total_cycles
* 100);
890 if (total_packets
== 0) {
895 " avg cycles per packet: %.02f (%"PRIu64
"/%"PRIu64
")\n",
896 total_cycles
/ (double) total_packets
,
897 total_cycles
, total_packets
);
900 " avg processing cycles per packet: "
901 "%.02f (%"PRIu64
"/%"PRIu64
")\n",
902 stats
[PMD_CYCLES_ITER_BUSY
] / (double) total_packets
,
903 stats
[PMD_CYCLES_ITER_BUSY
], total_packets
);
907 pmd_info_show_perf(struct ds
*reply
,
908 struct dp_netdev_pmd_thread
*pmd
,
909 struct pmd_perf_params
*par
)
911 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
913 xastrftime_msec("%H:%M:%S.###", time_wall_msec(), true);
914 long long now
= time_msec();
915 double duration
= (now
- pmd
->perf_stats
.start_ms
) / 1000.0;
917 ds_put_cstr(reply
, "\n");
918 ds_put_format(reply
, "Time: %s\n", time_str
);
919 ds_put_format(reply
, "Measurement duration: %.3f s\n", duration
);
920 ds_put_cstr(reply
, "\n");
921 format_pmd_thread(reply
, pmd
);
922 ds_put_cstr(reply
, "\n");
923 pmd_perf_format_overall_stats(reply
, &pmd
->perf_stats
, duration
);
924 if (pmd_perf_metrics_enabled(pmd
)) {
925 /* Prevent parallel clearing of perf metrics. */
926 ovs_mutex_lock(&pmd
->perf_stats
.clear_mutex
);
927 if (par
->histograms
) {
928 ds_put_cstr(reply
, "\n");
929 pmd_perf_format_histograms(reply
, &pmd
->perf_stats
);
931 if (par
->iter_hist_len
> 0) {
932 ds_put_cstr(reply
, "\n");
933 pmd_perf_format_iteration_history(reply
, &pmd
->perf_stats
,
936 if (par
->ms_hist_len
> 0) {
937 ds_put_cstr(reply
, "\n");
938 pmd_perf_format_ms_history(reply
, &pmd
->perf_stats
,
941 ovs_mutex_unlock(&pmd
->perf_stats
.clear_mutex
);
948 compare_poll_list(const void *a_
, const void *b_
)
950 const struct rxq_poll
*a
= a_
;
951 const struct rxq_poll
*b
= b_
;
953 const char *namea
= netdev_rxq_get_name(a
->rxq
->rx
);
954 const char *nameb
= netdev_rxq_get_name(b
->rxq
->rx
);
956 int cmp
= strcmp(namea
, nameb
);
958 return netdev_rxq_get_queue_id(a
->rxq
->rx
)
959 - netdev_rxq_get_queue_id(b
->rxq
->rx
);
966 sorted_poll_list(struct dp_netdev_pmd_thread
*pmd
, struct rxq_poll
**list
,
969 struct rxq_poll
*ret
, *poll
;
972 *n
= hmap_count(&pmd
->poll_list
);
976 ret
= xcalloc(*n
, sizeof *ret
);
978 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
983 qsort(ret
, *n
, sizeof *ret
, compare_poll_list
);
990 pmd_info_show_rxq(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
992 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
993 struct rxq_poll
*list
;
995 uint64_t total_cycles
= 0;
998 "pmd thread numa_id %d core_id %u:\n isolated : %s\n",
999 pmd
->numa_id
, pmd
->core_id
, (pmd
->isolated
)
1000 ? "true" : "false");
1002 ovs_mutex_lock(&pmd
->port_mutex
);
1003 sorted_poll_list(pmd
, &list
, &n_rxq
);
1005 /* Get the total pmd cycles for an interval. */
1006 atomic_read_relaxed(&pmd
->intrvl_cycles
, &total_cycles
);
1007 /* Estimate the cycles to cover all intervals. */
1008 total_cycles
*= PMD_RXQ_INTERVAL_MAX
;
1010 for (int i
= 0; i
< n_rxq
; i
++) {
1011 struct dp_netdev_rxq
*rxq
= list
[i
].rxq
;
1012 const char *name
= netdev_rxq_get_name(rxq
->rx
);
1013 uint64_t proc_cycles
= 0;
1015 for (int j
= 0; j
< PMD_RXQ_INTERVAL_MAX
; j
++) {
1016 proc_cycles
+= dp_netdev_rxq_get_intrvl_cycles(rxq
, j
);
1018 ds_put_format(reply
, " port: %-16s queue-id: %2d", name
,
1019 netdev_rxq_get_queue_id(list
[i
].rxq
->rx
));
1020 ds_put_format(reply
, " pmd usage: ");
1022 ds_put_format(reply
, "%2"PRIu64
"",
1023 proc_cycles
* 100 / total_cycles
);
1024 ds_put_cstr(reply
, " %");
1026 ds_put_format(reply
, "%s", "NOT AVAIL");
1028 ds_put_cstr(reply
, "\n");
1030 ovs_mutex_unlock(&pmd
->port_mutex
);
1036 compare_poll_thread_list(const void *a_
, const void *b_
)
1038 const struct dp_netdev_pmd_thread
*a
, *b
;
1040 a
= *(struct dp_netdev_pmd_thread
**)a_
;
1041 b
= *(struct dp_netdev_pmd_thread
**)b_
;
1043 if (a
->core_id
< b
->core_id
) {
1046 if (a
->core_id
> b
->core_id
) {
1052 /* Create a sorted list of pmd's from the dp->poll_threads cmap. We can use
1053 * this list, as long as we do not go to quiescent state. */
1055 sorted_poll_thread_list(struct dp_netdev
*dp
,
1056 struct dp_netdev_pmd_thread
***list
,
1059 struct dp_netdev_pmd_thread
*pmd
;
1060 struct dp_netdev_pmd_thread
**pmd_list
;
1061 size_t k
= 0, n_pmds
;
1063 n_pmds
= cmap_count(&dp
->poll_threads
);
1064 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
1066 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1070 pmd_list
[k
++] = pmd
;
1073 qsort(pmd_list
, k
, sizeof *pmd_list
, compare_poll_thread_list
);
1080 dpif_netdev_pmd_rebalance(struct unixctl_conn
*conn
, int argc
,
1081 const char *argv
[], void *aux OVS_UNUSED
)
1083 struct ds reply
= DS_EMPTY_INITIALIZER
;
1084 struct dp_netdev
*dp
= NULL
;
1086 ovs_mutex_lock(&dp_netdev_mutex
);
1089 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
1090 } else if (shash_count(&dp_netdevs
) == 1) {
1091 /* There's only one datapath */
1092 dp
= shash_first(&dp_netdevs
)->data
;
1096 ovs_mutex_unlock(&dp_netdev_mutex
);
1097 unixctl_command_reply_error(conn
,
1098 "please specify an existing datapath");
1102 dp_netdev_request_reconfigure(dp
);
1103 ovs_mutex_unlock(&dp_netdev_mutex
);
1104 ds_put_cstr(&reply
, "pmd rxq rebalance requested.\n");
1105 unixctl_command_reply(conn
, ds_cstr(&reply
));
1110 dpif_netdev_pmd_info(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
1113 struct ds reply
= DS_EMPTY_INITIALIZER
;
1114 struct dp_netdev_pmd_thread
**pmd_list
;
1115 struct dp_netdev
*dp
= NULL
;
1116 enum pmd_info_type type
= *(enum pmd_info_type
*) aux
;
1117 unsigned int core_id
;
1118 bool filter_on_pmd
= false;
1121 ovs_mutex_lock(&dp_netdev_mutex
);
1124 if (!strcmp(argv
[1], "-pmd") && argc
> 2) {
1125 if (str_to_uint(argv
[2], 10, &core_id
)) {
1126 filter_on_pmd
= true;
1131 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
1138 if (shash_count(&dp_netdevs
) == 1) {
1139 /* There's only one datapath */
1140 dp
= shash_first(&dp_netdevs
)->data
;
1142 ovs_mutex_unlock(&dp_netdev_mutex
);
1143 unixctl_command_reply_error(conn
,
1144 "please specify an existing datapath");
1149 sorted_poll_thread_list(dp
, &pmd_list
, &n
);
1150 for (size_t i
= 0; i
< n
; i
++) {
1151 struct dp_netdev_pmd_thread
*pmd
= pmd_list
[i
];
1155 if (filter_on_pmd
&& pmd
->core_id
!= core_id
) {
1158 if (type
== PMD_INFO_SHOW_RXQ
) {
1159 pmd_info_show_rxq(&reply
, pmd
);
1160 } else if (type
== PMD_INFO_CLEAR_STATS
) {
1161 pmd_perf_stats_clear(&pmd
->perf_stats
);
1162 } else if (type
== PMD_INFO_SHOW_STATS
) {
1163 pmd_info_show_stats(&reply
, pmd
);
1164 } else if (type
== PMD_INFO_PERF_SHOW
) {
1165 pmd_info_show_perf(&reply
, pmd
, (struct pmd_perf_params
*)aux
);
1170 ovs_mutex_unlock(&dp_netdev_mutex
);
1172 unixctl_command_reply(conn
, ds_cstr(&reply
));
1177 pmd_perf_show_cmd(struct unixctl_conn
*conn
, int argc
,
1179 void *aux OVS_UNUSED
)
1181 struct pmd_perf_params par
;
1182 long int it_hist
= 0, ms_hist
= 0;
1183 par
.histograms
= true;
1186 if (!strcmp(argv
[1], "-nh")) {
1187 par
.histograms
= false;
1190 } else if (!strcmp(argv
[1], "-it") && argc
> 2) {
1191 it_hist
= strtol(argv
[2], NULL
, 10);
1194 } else if (it_hist
> HISTORY_LEN
) {
1195 it_hist
= HISTORY_LEN
;
1199 } else if (!strcmp(argv
[1], "-ms") && argc
> 2) {
1200 ms_hist
= strtol(argv
[2], NULL
, 10);
1203 } else if (ms_hist
> HISTORY_LEN
) {
1204 ms_hist
= HISTORY_LEN
;
1212 par
.iter_hist_len
= it_hist
;
1213 par
.ms_hist_len
= ms_hist
;
1214 par
.command_type
= PMD_INFO_PERF_SHOW
;
1215 dpif_netdev_pmd_info(conn
, argc
, argv
, &par
);
1219 dpif_netdev_init(void)
1221 static enum pmd_info_type show_aux
= PMD_INFO_SHOW_STATS
,
1222 clear_aux
= PMD_INFO_CLEAR_STATS
,
1223 poll_aux
= PMD_INFO_SHOW_RXQ
;
1225 unixctl_command_register("dpif-netdev/pmd-stats-show", "[-pmd core] [dp]",
1226 0, 3, dpif_netdev_pmd_info
,
1228 unixctl_command_register("dpif-netdev/pmd-stats-clear", "[-pmd core] [dp]",
1229 0, 3, dpif_netdev_pmd_info
,
1230 (void *)&clear_aux
);
1231 unixctl_command_register("dpif-netdev/pmd-rxq-show", "[-pmd core] [dp]",
1232 0, 3, dpif_netdev_pmd_info
,
1234 unixctl_command_register("dpif-netdev/pmd-perf-show",
1235 "[-nh] [-it iter-history-len]"
1236 " [-ms ms-history-len]"
1237 " [-pmd core] [dp]",
1238 0, 8, pmd_perf_show_cmd
,
1240 unixctl_command_register("dpif-netdev/pmd-rxq-rebalance", "[dp]",
1241 0, 1, dpif_netdev_pmd_rebalance
,
1243 unixctl_command_register("dpif-netdev/pmd-perf-log-set",
1244 "on|off [-b before] [-a after] [-e|-ne] "
1245 "[-us usec] [-q qlen]",
1246 0, 10, pmd_perf_log_set_cmd
,
1252 dpif_netdev_enumerate(struct sset
*all_dps
,
1253 const struct dpif_class
*dpif_class
)
1255 struct shash_node
*node
;
1257 ovs_mutex_lock(&dp_netdev_mutex
);
1258 SHASH_FOR_EACH(node
, &dp_netdevs
) {
1259 struct dp_netdev
*dp
= node
->data
;
1260 if (dpif_class
!= dp
->class) {
1261 /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs.
1262 * If the class doesn't match, skip this dpif. */
1265 sset_add(all_dps
, node
->name
);
1267 ovs_mutex_unlock(&dp_netdev_mutex
);
1273 dpif_netdev_class_is_dummy(const struct dpif_class
*class)
1275 return class != &dpif_netdev_class
;
1279 dpif_netdev_port_open_type(const struct dpif_class
*class, const char *type
)
1281 return strcmp(type
, "internal") ? type
1282 : dpif_netdev_class_is_dummy(class) ? "dummy-internal"
1286 static struct dpif
*
1287 create_dpif_netdev(struct dp_netdev
*dp
)
1289 uint16_t netflow_id
= hash_string(dp
->name
, 0);
1290 struct dpif_netdev
*dpif
;
1292 ovs_refcount_ref(&dp
->ref_cnt
);
1294 dpif
= xmalloc(sizeof *dpif
);
1295 dpif_init(&dpif
->dpif
, dp
->class, dp
->name
, netflow_id
>> 8, netflow_id
);
1297 dpif
->last_port_seq
= seq_read(dp
->port_seq
);
1302 /* Choose an unused, non-zero port number and return it on success.
1303 * Return ODPP_NONE on failure. */
1305 choose_port(struct dp_netdev
*dp
, const char *name
)
1306 OVS_REQUIRES(dp
->port_mutex
)
1310 if (dp
->class != &dpif_netdev_class
) {
1314 /* If the port name begins with "br", start the number search at
1315 * 100 to make writing tests easier. */
1316 if (!strncmp(name
, "br", 2)) {
1320 /* If the port name contains a number, try to assign that port number.
1321 * This can make writing unit tests easier because port numbers are
1323 for (p
= name
; *p
!= '\0'; p
++) {
1324 if (isdigit((unsigned char) *p
)) {
1325 port_no
= start_no
+ strtol(p
, NULL
, 10);
1326 if (port_no
> 0 && port_no
!= odp_to_u32(ODPP_NONE
)
1327 && !dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
1328 return u32_to_odp(port_no
);
1335 for (port_no
= 1; port_no
<= UINT16_MAX
; port_no
++) {
1336 if (!dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
1337 return u32_to_odp(port_no
);
1345 create_dp_netdev(const char *name
, const struct dpif_class
*class,
1346 struct dp_netdev
**dpp
)
1347 OVS_REQUIRES(dp_netdev_mutex
)
1349 struct dp_netdev
*dp
;
1352 dp
= xzalloc(sizeof *dp
);
1353 shash_add(&dp_netdevs
, name
, dp
);
1355 *CONST_CAST(const struct dpif_class
**, &dp
->class) = class;
1356 *CONST_CAST(const char **, &dp
->name
) = xstrdup(name
);
1357 ovs_refcount_init(&dp
->ref_cnt
);
1358 atomic_flag_clear(&dp
->destroyed
);
1360 ovs_mutex_init(&dp
->port_mutex
);
1361 hmap_init(&dp
->ports
);
1362 dp
->port_seq
= seq_create();
1363 fat_rwlock_init(&dp
->upcall_rwlock
);
1365 dp
->reconfigure_seq
= seq_create();
1366 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
1368 for (int i
= 0; i
< N_METER_LOCKS
; ++i
) {
1369 ovs_mutex_init_adaptive(&dp
->meter_locks
[i
]);
1372 /* Disable upcalls by default. */
1373 dp_netdev_disable_upcall(dp
);
1374 dp
->upcall_aux
= NULL
;
1375 dp
->upcall_cb
= NULL
;
1377 conntrack_init(&dp
->conntrack
);
1379 atomic_init(&dp
->emc_insert_min
, DEFAULT_EM_FLOW_INSERT_MIN
);
1380 atomic_init(&dp
->tx_flush_interval
, DEFAULT_TX_FLUSH_INTERVAL
);
1382 cmap_init(&dp
->poll_threads
);
1384 ovs_mutex_init(&dp
->tx_qid_pool_mutex
);
1385 /* We need 1 Tx queue for each possible core + 1 for non-PMD threads. */
1386 dp
->tx_qid_pool
= id_pool_create(0, ovs_numa_get_n_cores() + 1);
1388 ovs_mutex_init_recursive(&dp
->non_pmd_mutex
);
1389 ovsthread_key_create(&dp
->per_pmd_key
, NULL
);
1391 ovs_mutex_lock(&dp
->port_mutex
);
1392 /* non-PMD will be created before all other threads and will
1393 * allocate static_tx_qid = 0. */
1394 dp_netdev_set_nonpmd(dp
);
1396 error
= do_add_port(dp
, name
, dpif_netdev_port_open_type(dp
->class,
1399 ovs_mutex_unlock(&dp
->port_mutex
);
1405 dp
->last_tnl_conf_seq
= seq_read(tnl_conf_seq
);
1411 dp_netdev_request_reconfigure(struct dp_netdev
*dp
)
1413 seq_change(dp
->reconfigure_seq
);
1417 dp_netdev_is_reconf_required(struct dp_netdev
*dp
)
1419 return seq_read(dp
->reconfigure_seq
) != dp
->last_reconfigure_seq
;
1423 dpif_netdev_open(const struct dpif_class
*class, const char *name
,
1424 bool create
, struct dpif
**dpifp
)
1426 struct dp_netdev
*dp
;
1429 ovs_mutex_lock(&dp_netdev_mutex
);
1430 dp
= shash_find_data(&dp_netdevs
, name
);
1432 error
= create
? create_dp_netdev(name
, class, &dp
) : ENODEV
;
1434 error
= (dp
->class != class ? EINVAL
1439 *dpifp
= create_dpif_netdev(dp
);
1442 ovs_mutex_unlock(&dp_netdev_mutex
);
1448 dp_netdev_destroy_upcall_lock(struct dp_netdev
*dp
)
1449 OVS_NO_THREAD_SAFETY_ANALYSIS
1451 /* Check that upcalls are disabled, i.e. that the rwlock is taken */
1452 ovs_assert(fat_rwlock_tryrdlock(&dp
->upcall_rwlock
));
1454 /* Before freeing a lock we should release it */
1455 fat_rwlock_unlock(&dp
->upcall_rwlock
);
1456 fat_rwlock_destroy(&dp
->upcall_rwlock
);
1460 dp_delete_meter(struct dp_netdev
*dp
, uint32_t meter_id
)
1461 OVS_REQUIRES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
1463 if (dp
->meters
[meter_id
]) {
1464 free(dp
->meters
[meter_id
]);
1465 dp
->meters
[meter_id
] = NULL
;
1469 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
1470 * through the 'dp_netdevs' shash while freeing 'dp'. */
1472 dp_netdev_free(struct dp_netdev
*dp
)
1473 OVS_REQUIRES(dp_netdev_mutex
)
1475 struct dp_netdev_port
*port
, *next
;
1477 shash_find_and_delete(&dp_netdevs
, dp
->name
);
1479 ovs_mutex_lock(&dp
->port_mutex
);
1480 HMAP_FOR_EACH_SAFE (port
, next
, node
, &dp
->ports
) {
1481 do_del_port(dp
, port
);
1483 ovs_mutex_unlock(&dp
->port_mutex
);
1485 dp_netdev_destroy_all_pmds(dp
, true);
1486 cmap_destroy(&dp
->poll_threads
);
1488 ovs_mutex_destroy(&dp
->tx_qid_pool_mutex
);
1489 id_pool_destroy(dp
->tx_qid_pool
);
1491 ovs_mutex_destroy(&dp
->non_pmd_mutex
);
1492 ovsthread_key_delete(dp
->per_pmd_key
);
1494 conntrack_destroy(&dp
->conntrack
);
1497 seq_destroy(dp
->reconfigure_seq
);
1499 seq_destroy(dp
->port_seq
);
1500 hmap_destroy(&dp
->ports
);
1501 ovs_mutex_destroy(&dp
->port_mutex
);
1503 /* Upcalls must be disabled at this point */
1504 dp_netdev_destroy_upcall_lock(dp
);
1508 for (i
= 0; i
< MAX_METERS
; ++i
) {
1510 dp_delete_meter(dp
, i
);
1511 meter_unlock(dp
, i
);
1513 for (i
= 0; i
< N_METER_LOCKS
; ++i
) {
1514 ovs_mutex_destroy(&dp
->meter_locks
[i
]);
1517 free(dp
->pmd_cmask
);
1518 free(CONST_CAST(char *, dp
->name
));
1523 dp_netdev_unref(struct dp_netdev
*dp
)
1526 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
1527 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
1528 ovs_mutex_lock(&dp_netdev_mutex
);
1529 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1532 ovs_mutex_unlock(&dp_netdev_mutex
);
1537 dpif_netdev_close(struct dpif
*dpif
)
1539 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1541 dp_netdev_unref(dp
);
1546 dpif_netdev_destroy(struct dpif
*dpif
)
1548 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1550 if (!atomic_flag_test_and_set(&dp
->destroyed
)) {
1551 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1552 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
1560 /* Add 'n' to the atomic variable 'var' non-atomically and using relaxed
1561 * load/store semantics. While the increment is not atomic, the load and
1562 * store operations are, making it impossible to read inconsistent values.
1564 * This is used to update thread local stats counters. */
1566 non_atomic_ullong_add(atomic_ullong
*var
, unsigned long long n
)
1568 unsigned long long tmp
;
1570 atomic_read_relaxed(var
, &tmp
);
1572 atomic_store_relaxed(var
, tmp
);
1576 dpif_netdev_get_stats(const struct dpif
*dpif
, struct dpif_dp_stats
*stats
)
1578 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1579 struct dp_netdev_pmd_thread
*pmd
;
1580 uint64_t pmd_stats
[PMD_N_STATS
];
1582 stats
->n_flows
= stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
1583 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1584 stats
->n_flows
+= cmap_count(&pmd
->flow_table
);
1585 pmd_perf_read_counters(&pmd
->perf_stats
, pmd_stats
);
1586 stats
->n_hit
+= pmd_stats
[PMD_STAT_EXACT_HIT
];
1587 stats
->n_hit
+= pmd_stats
[PMD_STAT_MASKED_HIT
];
1588 stats
->n_missed
+= pmd_stats
[PMD_STAT_MISS
];
1589 stats
->n_lost
+= pmd_stats
[PMD_STAT_LOST
];
1591 stats
->n_masks
= UINT32_MAX
;
1592 stats
->n_mask_hit
= UINT64_MAX
;
1598 dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread
*pmd
)
1600 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
1601 ovs_mutex_lock(&pmd
->dp
->non_pmd_mutex
);
1602 ovs_mutex_lock(&pmd
->port_mutex
);
1603 pmd_load_cached_ports(pmd
);
1604 ovs_mutex_unlock(&pmd
->port_mutex
);
1605 ovs_mutex_unlock(&pmd
->dp
->non_pmd_mutex
);
1609 ovs_mutex_lock(&pmd
->cond_mutex
);
1610 seq_change(pmd
->reload_seq
);
1611 atomic_store_relaxed(&pmd
->reload
, true);
1612 ovs_mutex_cond_wait(&pmd
->cond
, &pmd
->cond_mutex
);
1613 ovs_mutex_unlock(&pmd
->cond_mutex
);
1617 hash_port_no(odp_port_t port_no
)
1619 return hash_int(odp_to_u32(port_no
), 0);
1623 port_create(const char *devname
, const char *type
,
1624 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1626 struct netdev_saved_flags
*sf
;
1627 struct dp_netdev_port
*port
;
1628 enum netdev_flags flags
;
1629 struct netdev
*netdev
;
1634 /* Open and validate network device. */
1635 error
= netdev_open(devname
, type
, &netdev
);
1639 /* XXX reject non-Ethernet devices */
1641 netdev_get_flags(netdev
, &flags
);
1642 if (flags
& NETDEV_LOOPBACK
) {
1643 VLOG_ERR("%s: cannot add a loopback device", devname
);
1648 error
= netdev_turn_flags_on(netdev
, NETDEV_PROMISC
, &sf
);
1650 VLOG_ERR("%s: cannot set promisc flag", devname
);
1654 port
= xzalloc(sizeof *port
);
1655 port
->port_no
= port_no
;
1656 port
->netdev
= netdev
;
1657 port
->type
= xstrdup(type
);
1659 port
->need_reconfigure
= true;
1660 ovs_mutex_init(&port
->txq_used_mutex
);
1667 netdev_close(netdev
);
1672 do_add_port(struct dp_netdev
*dp
, const char *devname
, const char *type
,
1674 OVS_REQUIRES(dp
->port_mutex
)
1676 struct dp_netdev_port
*port
;
1679 /* Reject devices already in 'dp'. */
1680 if (!get_port_by_name(dp
, devname
, &port
)) {
1684 error
= port_create(devname
, type
, port_no
, &port
);
1689 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
1690 seq_change(dp
->port_seq
);
1692 reconfigure_datapath(dp
);
1698 dpif_netdev_port_add(struct dpif
*dpif
, struct netdev
*netdev
,
1699 odp_port_t
*port_nop
)
1701 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1702 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1703 const char *dpif_port
;
1707 ovs_mutex_lock(&dp
->port_mutex
);
1708 dpif_port
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1709 if (*port_nop
!= ODPP_NONE
) {
1710 port_no
= *port_nop
;
1711 error
= dp_netdev_lookup_port(dp
, *port_nop
) ? EBUSY
: 0;
1713 port_no
= choose_port(dp
, dpif_port
);
1714 error
= port_no
== ODPP_NONE
? EFBIG
: 0;
1717 *port_nop
= port_no
;
1718 error
= do_add_port(dp
, dpif_port
, netdev_get_type(netdev
), port_no
);
1720 ovs_mutex_unlock(&dp
->port_mutex
);
1726 dpif_netdev_port_del(struct dpif
*dpif
, odp_port_t port_no
)
1728 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1731 ovs_mutex_lock(&dp
->port_mutex
);
1732 if (port_no
== ODPP_LOCAL
) {
1735 struct dp_netdev_port
*port
;
1737 error
= get_port_by_number(dp
, port_no
, &port
);
1739 do_del_port(dp
, port
);
1742 ovs_mutex_unlock(&dp
->port_mutex
);
1748 is_valid_port_number(odp_port_t port_no
)
1750 return port_no
!= ODPP_NONE
;
1753 static struct dp_netdev_port
*
1754 dp_netdev_lookup_port(const struct dp_netdev
*dp
, odp_port_t port_no
)
1755 OVS_REQUIRES(dp
->port_mutex
)
1757 struct dp_netdev_port
*port
;
1759 HMAP_FOR_EACH_WITH_HASH (port
, node
, hash_port_no(port_no
), &dp
->ports
) {
1760 if (port
->port_no
== port_no
) {
1768 get_port_by_number(struct dp_netdev
*dp
,
1769 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1770 OVS_REQUIRES(dp
->port_mutex
)
1772 if (!is_valid_port_number(port_no
)) {
1776 *portp
= dp_netdev_lookup_port(dp
, port_no
);
1777 return *portp
? 0 : ENODEV
;
1782 port_destroy(struct dp_netdev_port
*port
)
1788 netdev_close(port
->netdev
);
1789 netdev_restore_flags(port
->sf
);
1791 for (unsigned i
= 0; i
< port
->n_rxq
; i
++) {
1792 netdev_rxq_close(port
->rxqs
[i
].rx
);
1794 ovs_mutex_destroy(&port
->txq_used_mutex
);
1795 free(port
->rxq_affinity_list
);
1796 free(port
->txq_used
);
1803 get_port_by_name(struct dp_netdev
*dp
,
1804 const char *devname
, struct dp_netdev_port
**portp
)
1805 OVS_REQUIRES(dp
->port_mutex
)
1807 struct dp_netdev_port
*port
;
1809 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1810 if (!strcmp(netdev_get_name(port
->netdev
), devname
)) {
1816 /* Callers of dpif_netdev_port_query_by_name() expect ENODEV for a non
1821 /* Returns 'true' if there is a port with pmd netdev. */
1823 has_pmd_port(struct dp_netdev
*dp
)
1824 OVS_REQUIRES(dp
->port_mutex
)
1826 struct dp_netdev_port
*port
;
1828 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1829 if (netdev_is_pmd(port
->netdev
)) {
1838 do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*port
)
1839 OVS_REQUIRES(dp
->port_mutex
)
1841 hmap_remove(&dp
->ports
, &port
->node
);
1842 seq_change(dp
->port_seq
);
1844 reconfigure_datapath(dp
);
1850 answer_port_query(const struct dp_netdev_port
*port
,
1851 struct dpif_port
*dpif_port
)
1853 dpif_port
->name
= xstrdup(netdev_get_name(port
->netdev
));
1854 dpif_port
->type
= xstrdup(port
->type
);
1855 dpif_port
->port_no
= port
->port_no
;
1859 dpif_netdev_port_query_by_number(const struct dpif
*dpif
, odp_port_t port_no
,
1860 struct dpif_port
*dpif_port
)
1862 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1863 struct dp_netdev_port
*port
;
1866 ovs_mutex_lock(&dp
->port_mutex
);
1867 error
= get_port_by_number(dp
, port_no
, &port
);
1868 if (!error
&& dpif_port
) {
1869 answer_port_query(port
, dpif_port
);
1871 ovs_mutex_unlock(&dp
->port_mutex
);
1877 dpif_netdev_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
1878 struct dpif_port
*dpif_port
)
1880 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1881 struct dp_netdev_port
*port
;
1884 ovs_mutex_lock(&dp
->port_mutex
);
1885 error
= get_port_by_name(dp
, devname
, &port
);
1886 if (!error
&& dpif_port
) {
1887 answer_port_query(port
, dpif_port
);
1889 ovs_mutex_unlock(&dp
->port_mutex
);
1895 dp_netdev_flow_free(struct dp_netdev_flow
*flow
)
1897 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow
));
1901 static void dp_netdev_flow_unref(struct dp_netdev_flow
*flow
)
1903 if (ovs_refcount_unref_relaxed(&flow
->ref_cnt
) == 1) {
1904 ovsrcu_postpone(dp_netdev_flow_free
, flow
);
1909 dp_netdev_flow_hash(const ovs_u128
*ufid
)
1911 return ufid
->u32
[0];
1914 static inline struct dpcls
*
1915 dp_netdev_pmd_lookup_dpcls(struct dp_netdev_pmd_thread
*pmd
,
1919 uint32_t hash
= hash_port_no(in_port
);
1920 CMAP_FOR_EACH_WITH_HASH (cls
, node
, hash
, &pmd
->classifiers
) {
1921 if (cls
->in_port
== in_port
) {
1922 /* Port classifier exists already */
1929 static inline struct dpcls
*
1930 dp_netdev_pmd_find_dpcls(struct dp_netdev_pmd_thread
*pmd
,
1932 OVS_REQUIRES(pmd
->flow_mutex
)
1934 struct dpcls
*cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
1935 uint32_t hash
= hash_port_no(in_port
);
1938 /* Create new classifier for in_port */
1939 cls
= xmalloc(sizeof(*cls
));
1941 cls
->in_port
= in_port
;
1942 cmap_insert(&pmd
->classifiers
, &cls
->node
, hash
);
1943 VLOG_DBG("Creating dpcls %p for in_port %d", cls
, in_port
);
1948 #define MAX_FLOW_MARK (UINT32_MAX - 1)
1949 #define INVALID_FLOW_MARK (UINT32_MAX)
1951 struct megaflow_to_mark_data
{
1952 const struct cmap_node node
;
1958 struct cmap megaflow_to_mark
;
1959 struct cmap mark_to_flow
;
1960 struct id_pool
*pool
;
1961 struct ovs_mutex mutex
;
1964 static struct flow_mark flow_mark
= {
1965 .megaflow_to_mark
= CMAP_INITIALIZER
,
1966 .mark_to_flow
= CMAP_INITIALIZER
,
1967 .mutex
= OVS_MUTEX_INITIALIZER
,
1971 flow_mark_alloc(void)
1975 if (!flow_mark
.pool
) {
1976 /* Haven't initiated yet, do it here */
1977 flow_mark
.pool
= id_pool_create(0, MAX_FLOW_MARK
);
1980 if (id_pool_alloc_id(flow_mark
.pool
, &mark
)) {
1984 return INVALID_FLOW_MARK
;
1988 flow_mark_free(uint32_t mark
)
1990 id_pool_free_id(flow_mark
.pool
, mark
);
1993 /* associate megaflow with a mark, which is a 1:1 mapping */
1995 megaflow_to_mark_associate(const ovs_u128
*mega_ufid
, uint32_t mark
)
1997 size_t hash
= dp_netdev_flow_hash(mega_ufid
);
1998 struct megaflow_to_mark_data
*data
= xzalloc(sizeof(*data
));
2000 data
->mega_ufid
= *mega_ufid
;
2003 cmap_insert(&flow_mark
.megaflow_to_mark
,
2004 CONST_CAST(struct cmap_node
*, &data
->node
), hash
);
2007 /* disassociate meagaflow with a mark */
2009 megaflow_to_mark_disassociate(const ovs_u128
*mega_ufid
)
2011 size_t hash
= dp_netdev_flow_hash(mega_ufid
);
2012 struct megaflow_to_mark_data
*data
;
2014 CMAP_FOR_EACH_WITH_HASH (data
, node
, hash
, &flow_mark
.megaflow_to_mark
) {
2015 if (ovs_u128_equals(*mega_ufid
, data
->mega_ufid
)) {
2016 cmap_remove(&flow_mark
.megaflow_to_mark
,
2017 CONST_CAST(struct cmap_node
*, &data
->node
), hash
);
2023 VLOG_WARN("Masked ufid "UUID_FMT
" is not associated with a mark?\n",
2024 UUID_ARGS((struct uuid
*)mega_ufid
));
2027 static inline uint32_t
2028 megaflow_to_mark_find(const ovs_u128
*mega_ufid
)
2030 size_t hash
= dp_netdev_flow_hash(mega_ufid
);
2031 struct megaflow_to_mark_data
*data
;
2033 CMAP_FOR_EACH_WITH_HASH (data
, node
, hash
, &flow_mark
.megaflow_to_mark
) {
2034 if (ovs_u128_equals(*mega_ufid
, data
->mega_ufid
)) {
2039 VLOG_WARN("Mark id for ufid "UUID_FMT
" was not found\n",
2040 UUID_ARGS((struct uuid
*)mega_ufid
));
2041 return INVALID_FLOW_MARK
;
2044 /* associate mark with a flow, which is 1:N mapping */
2046 mark_to_flow_associate(const uint32_t mark
, struct dp_netdev_flow
*flow
)
2048 dp_netdev_flow_ref(flow
);
2050 cmap_insert(&flow_mark
.mark_to_flow
,
2051 CONST_CAST(struct cmap_node
*, &flow
->mark_node
),
2055 VLOG_DBG("Associated dp_netdev flow %p with mark %u\n", flow
, mark
);
2059 flow_mark_has_no_ref(uint32_t mark
)
2061 struct dp_netdev_flow
*flow
;
2063 CMAP_FOR_EACH_WITH_HASH (flow
, mark_node
, hash_int(mark
, 0),
2064 &flow_mark
.mark_to_flow
) {
2065 if (flow
->mark
== mark
) {
2074 mark_to_flow_disassociate(struct dp_netdev_pmd_thread
*pmd
,
2075 struct dp_netdev_flow
*flow
)
2078 uint32_t mark
= flow
->mark
;
2079 struct cmap_node
*mark_node
= CONST_CAST(struct cmap_node
*,
2082 cmap_remove(&flow_mark
.mark_to_flow
, mark_node
, hash_int(mark
, 0));
2083 flow
->mark
= INVALID_FLOW_MARK
;
2086 * no flow is referencing the mark any more? If so, let's
2087 * remove the flow from hardware and free the mark.
2089 if (flow_mark_has_no_ref(mark
)) {
2090 struct dp_netdev_port
*port
;
2091 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
2093 ovs_mutex_lock(&pmd
->dp
->port_mutex
);
2094 port
= dp_netdev_lookup_port(pmd
->dp
, in_port
);
2096 ret
= netdev_flow_del(port
->netdev
, &flow
->mega_ufid
, NULL
);
2098 ovs_mutex_unlock(&pmd
->dp
->port_mutex
);
2100 flow_mark_free(mark
);
2101 VLOG_DBG("Freed flow mark %u\n", mark
);
2103 megaflow_to_mark_disassociate(&flow
->mega_ufid
);
2105 dp_netdev_flow_unref(flow
);
2111 flow_mark_flush(struct dp_netdev_pmd_thread
*pmd
)
2113 struct dp_netdev_flow
*flow
;
2115 CMAP_FOR_EACH (flow
, mark_node
, &flow_mark
.mark_to_flow
) {
2116 if (flow
->pmd_id
== pmd
->core_id
) {
2117 mark_to_flow_disassociate(pmd
, flow
);
2123 dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread
*pmd
,
2124 struct dp_netdev_flow
*flow
)
2125 OVS_REQUIRES(pmd
->flow_mutex
)
2127 struct cmap_node
*node
= CONST_CAST(struct cmap_node
*, &flow
->node
);
2129 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
2131 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
2132 ovs_assert(cls
!= NULL
);
2133 dpcls_remove(cls
, &flow
->cr
);
2134 cmap_remove(&pmd
->flow_table
, node
, dp_netdev_flow_hash(&flow
->ufid
));
2135 if (flow
->mark
!= INVALID_FLOW_MARK
) {
2136 mark_to_flow_disassociate(pmd
, flow
);
2140 dp_netdev_flow_unref(flow
);
2144 dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
)
2146 struct dp_netdev_flow
*netdev_flow
;
2148 ovs_mutex_lock(&pmd
->flow_mutex
);
2149 CMAP_FOR_EACH (netdev_flow
, node
, &pmd
->flow_table
) {
2150 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
2152 ovs_mutex_unlock(&pmd
->flow_mutex
);
2156 dpif_netdev_flow_flush(struct dpif
*dpif
)
2158 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2159 struct dp_netdev_pmd_thread
*pmd
;
2161 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2162 dp_netdev_pmd_flow_flush(pmd
);
2168 struct dp_netdev_port_state
{
2169 struct hmap_position position
;
2174 dpif_netdev_port_dump_start(const struct dpif
*dpif OVS_UNUSED
, void **statep
)
2176 *statep
= xzalloc(sizeof(struct dp_netdev_port_state
));
2181 dpif_netdev_port_dump_next(const struct dpif
*dpif
, void *state_
,
2182 struct dpif_port
*dpif_port
)
2184 struct dp_netdev_port_state
*state
= state_
;
2185 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2186 struct hmap_node
*node
;
2189 ovs_mutex_lock(&dp
->port_mutex
);
2190 node
= hmap_at_position(&dp
->ports
, &state
->position
);
2192 struct dp_netdev_port
*port
;
2194 port
= CONTAINER_OF(node
, struct dp_netdev_port
, node
);
2197 state
->name
= xstrdup(netdev_get_name(port
->netdev
));
2198 dpif_port
->name
= state
->name
;
2199 dpif_port
->type
= port
->type
;
2200 dpif_port
->port_no
= port
->port_no
;
2206 ovs_mutex_unlock(&dp
->port_mutex
);
2212 dpif_netdev_port_dump_done(const struct dpif
*dpif OVS_UNUSED
, void *state_
)
2214 struct dp_netdev_port_state
*state
= state_
;
2221 dpif_netdev_port_poll(const struct dpif
*dpif_
, char **devnamep OVS_UNUSED
)
2223 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
2224 uint64_t new_port_seq
;
2227 new_port_seq
= seq_read(dpif
->dp
->port_seq
);
2228 if (dpif
->last_port_seq
!= new_port_seq
) {
2229 dpif
->last_port_seq
= new_port_seq
;
2239 dpif_netdev_port_poll_wait(const struct dpif
*dpif_
)
2241 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
2243 seq_wait(dpif
->dp
->port_seq
, dpif
->last_port_seq
);
2246 static struct dp_netdev_flow
*
2247 dp_netdev_flow_cast(const struct dpcls_rule
*cr
)
2249 return cr
? CONTAINER_OF(cr
, struct dp_netdev_flow
, cr
) : NULL
;
2252 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*flow
)
2254 return ovs_refcount_try_ref_rcu(&flow
->ref_cnt
);
2257 /* netdev_flow_key utilities.
2259 * netdev_flow_key is basically a miniflow. We use these functions
2260 * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow
2261 * functions (miniflow_clone_inline, miniflow_equal, ...), because:
2263 * - Since we are dealing exclusively with miniflows created by
2264 * miniflow_extract(), if the map is different the miniflow is different.
2265 * Therefore we can be faster by comparing the map and the miniflow in a
2267 * - These functions can be inlined by the compiler. */
2269 /* Given the number of bits set in miniflow's maps, returns the size of the
2270 * 'netdev_flow_key.mf' */
2271 static inline size_t
2272 netdev_flow_key_size(size_t flow_u64s
)
2274 return sizeof(struct miniflow
) + MINIFLOW_VALUES_SIZE(flow_u64s
);
2278 netdev_flow_key_equal(const struct netdev_flow_key
*a
,
2279 const struct netdev_flow_key
*b
)
2281 /* 'b->len' may be not set yet. */
2282 return a
->hash
== b
->hash
&& !memcmp(&a
->mf
, &b
->mf
, a
->len
);
2285 /* Used to compare 'netdev_flow_key' in the exact match cache to a miniflow.
2286 * The maps are compared bitwise, so both 'key->mf' and 'mf' must have been
2287 * generated by miniflow_extract. */
2289 netdev_flow_key_equal_mf(const struct netdev_flow_key
*key
,
2290 const struct miniflow
*mf
)
2292 return !memcmp(&key
->mf
, mf
, key
->len
);
2296 netdev_flow_key_clone(struct netdev_flow_key
*dst
,
2297 const struct netdev_flow_key
*src
)
2300 offsetof(struct netdev_flow_key
, mf
) + src
->len
);
2303 /* Initialize a netdev_flow_key 'mask' from 'match'. */
2305 netdev_flow_mask_init(struct netdev_flow_key
*mask
,
2306 const struct match
*match
)
2308 uint64_t *dst
= miniflow_values(&mask
->mf
);
2309 struct flowmap fmap
;
2313 /* Only check masks that make sense for the flow. */
2314 flow_wc_map(&match
->flow
, &fmap
);
2315 flowmap_init(&mask
->mf
.map
);
2317 FLOWMAP_FOR_EACH_INDEX(idx
, fmap
) {
2318 uint64_t mask_u64
= flow_u64_value(&match
->wc
.masks
, idx
);
2321 flowmap_set(&mask
->mf
.map
, idx
, 1);
2323 hash
= hash_add64(hash
, mask_u64
);
2329 FLOWMAP_FOR_EACH_MAP (map
, mask
->mf
.map
) {
2330 hash
= hash_add64(hash
, map
);
2333 size_t n
= dst
- miniflow_get_values(&mask
->mf
);
2335 mask
->hash
= hash_finish(hash
, n
* 8);
2336 mask
->len
= netdev_flow_key_size(n
);
2339 /* Initializes 'dst' as a copy of 'flow' masked with 'mask'. */
2341 netdev_flow_key_init_masked(struct netdev_flow_key
*dst
,
2342 const struct flow
*flow
,
2343 const struct netdev_flow_key
*mask
)
2345 uint64_t *dst_u64
= miniflow_values(&dst
->mf
);
2346 const uint64_t *mask_u64
= miniflow_get_values(&mask
->mf
);
2350 dst
->len
= mask
->len
;
2351 dst
->mf
= mask
->mf
; /* Copy maps. */
2353 FLOW_FOR_EACH_IN_MAPS(value
, flow
, mask
->mf
.map
) {
2354 *dst_u64
= value
& *mask_u64
++;
2355 hash
= hash_add64(hash
, *dst_u64
++);
2357 dst
->hash
= hash_finish(hash
,
2358 (dst_u64
- miniflow_get_values(&dst
->mf
)) * 8);
2361 /* Iterate through netdev_flow_key TNL u64 values specified by 'FLOWMAP'. */
2362 #define NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(VALUE, KEY, FLOWMAP) \
2363 MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, &(KEY)->mf, FLOWMAP)
2365 /* Returns a hash value for the bits of 'key' where there are 1-bits in
2367 static inline uint32_t
2368 netdev_flow_key_hash_in_mask(const struct netdev_flow_key
*key
,
2369 const struct netdev_flow_key
*mask
)
2371 const uint64_t *p
= miniflow_get_values(&mask
->mf
);
2375 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, key
, mask
->mf
.map
) {
2376 hash
= hash_add64(hash
, value
& *p
++);
2379 return hash_finish(hash
, (p
- miniflow_get_values(&mask
->mf
)) * 8);
2383 emc_entry_alive(struct emc_entry
*ce
)
2385 return ce
->flow
&& !ce
->flow
->dead
;
2389 emc_clear_entry(struct emc_entry
*ce
)
2392 dp_netdev_flow_unref(ce
->flow
);
2398 emc_change_entry(struct emc_entry
*ce
, struct dp_netdev_flow
*flow
,
2399 const struct netdev_flow_key
*key
)
2401 if (ce
->flow
!= flow
) {
2403 dp_netdev_flow_unref(ce
->flow
);
2406 if (dp_netdev_flow_ref(flow
)) {
2413 netdev_flow_key_clone(&ce
->key
, key
);
2418 emc_insert(struct emc_cache
*cache
, const struct netdev_flow_key
*key
,
2419 struct dp_netdev_flow
*flow
)
2421 struct emc_entry
*to_be_replaced
= NULL
;
2422 struct emc_entry
*current_entry
;
2424 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
2425 if (netdev_flow_key_equal(¤t_entry
->key
, key
)) {
2426 /* We found the entry with the 'mf' miniflow */
2427 emc_change_entry(current_entry
, flow
, NULL
);
2431 /* Replacement policy: put the flow in an empty (not alive) entry, or
2432 * in the first entry where it can be */
2434 || (emc_entry_alive(to_be_replaced
)
2435 && !emc_entry_alive(current_entry
))
2436 || current_entry
->key
.hash
< to_be_replaced
->key
.hash
) {
2437 to_be_replaced
= current_entry
;
2440 /* We didn't find the miniflow in the cache.
2441 * The 'to_be_replaced' entry is where the new flow will be stored */
2443 emc_change_entry(to_be_replaced
, flow
, key
);
2447 emc_probabilistic_insert(struct dp_netdev_pmd_thread
*pmd
,
2448 const struct netdev_flow_key
*key
,
2449 struct dp_netdev_flow
*flow
)
2451 /* Insert an entry into the EMC based on probability value 'min'. By
2452 * default the value is UINT32_MAX / 100 which yields an insertion
2453 * probability of 1/100 ie. 1% */
2456 atomic_read_relaxed(&pmd
->dp
->emc_insert_min
, &min
);
2458 if (min
&& random_uint32() <= min
) {
2459 emc_insert(&pmd
->flow_cache
, key
, flow
);
2463 static inline struct dp_netdev_flow
*
2464 emc_lookup(struct emc_cache
*cache
, const struct netdev_flow_key
*key
)
2466 struct emc_entry
*current_entry
;
2468 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
2469 if (current_entry
->key
.hash
== key
->hash
2470 && emc_entry_alive(current_entry
)
2471 && netdev_flow_key_equal_mf(¤t_entry
->key
, &key
->mf
)) {
2473 /* We found the entry with the 'key->mf' miniflow */
2474 return current_entry
->flow
;
2481 static struct dp_netdev_flow
*
2482 dp_netdev_pmd_lookup_flow(struct dp_netdev_pmd_thread
*pmd
,
2483 const struct netdev_flow_key
*key
,
2487 struct dpcls_rule
*rule
;
2488 odp_port_t in_port
= u32_to_odp(MINIFLOW_GET_U32(&key
->mf
,
2490 struct dp_netdev_flow
*netdev_flow
= NULL
;
2492 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
2493 if (OVS_LIKELY(cls
)) {
2494 dpcls_lookup(cls
, key
, &rule
, 1, lookup_num_p
);
2495 netdev_flow
= dp_netdev_flow_cast(rule
);
2500 static struct dp_netdev_flow
*
2501 dp_netdev_pmd_find_flow(const struct dp_netdev_pmd_thread
*pmd
,
2502 const ovs_u128
*ufidp
, const struct nlattr
*key
,
2505 struct dp_netdev_flow
*netdev_flow
;
2509 /* If a UFID is not provided, determine one based on the key. */
2510 if (!ufidp
&& key
&& key_len
2511 && !dpif_netdev_flow_from_nlattrs(key
, key_len
, &flow
, false)) {
2512 dpif_flow_hash(pmd
->dp
->dpif
, &flow
, sizeof flow
, &ufid
);
2517 CMAP_FOR_EACH_WITH_HASH (netdev_flow
, node
, dp_netdev_flow_hash(ufidp
),
2519 if (ovs_u128_equals(netdev_flow
->ufid
, *ufidp
)) {
2529 get_dpif_flow_stats(const struct dp_netdev_flow
*netdev_flow_
,
2530 struct dpif_flow_stats
*stats
)
2532 struct dp_netdev_flow
*netdev_flow
;
2533 unsigned long long n
;
2537 netdev_flow
= CONST_CAST(struct dp_netdev_flow
*, netdev_flow_
);
2539 atomic_read_relaxed(&netdev_flow
->stats
.packet_count
, &n
);
2540 stats
->n_packets
= n
;
2541 atomic_read_relaxed(&netdev_flow
->stats
.byte_count
, &n
);
2543 atomic_read_relaxed(&netdev_flow
->stats
.used
, &used
);
2545 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
2546 stats
->tcp_flags
= flags
;
2549 /* Converts to the dpif_flow format, using 'key_buf' and 'mask_buf' for
2550 * storing the netlink-formatted key/mask. 'key_buf' may be the same as
2551 * 'mask_buf'. Actions will be returned without copying, by relying on RCU to
2554 dp_netdev_flow_to_dpif_flow(const struct dp_netdev_flow
*netdev_flow
,
2555 struct ofpbuf
*key_buf
, struct ofpbuf
*mask_buf
,
2556 struct dpif_flow
*flow
, bool terse
)
2559 memset(flow
, 0, sizeof *flow
);
2561 struct flow_wildcards wc
;
2562 struct dp_netdev_actions
*actions
;
2564 struct odp_flow_key_parms odp_parms
= {
2565 .flow
= &netdev_flow
->flow
,
2567 .support
= dp_netdev_support
,
2570 miniflow_expand(&netdev_flow
->cr
.mask
->mf
, &wc
.masks
);
2571 /* in_port is exact matched, but we have left it out from the mask for
2572 * optimnization reasons. Add in_port back to the mask. */
2573 wc
.masks
.in_port
.odp_port
= ODPP_NONE
;
2576 offset
= key_buf
->size
;
2577 flow
->key
= ofpbuf_tail(key_buf
);
2578 odp_flow_key_from_flow(&odp_parms
, key_buf
);
2579 flow
->key_len
= key_buf
->size
- offset
;
2582 offset
= mask_buf
->size
;
2583 flow
->mask
= ofpbuf_tail(mask_buf
);
2584 odp_parms
.key_buf
= key_buf
;
2585 odp_flow_key_from_mask(&odp_parms
, mask_buf
);
2586 flow
->mask_len
= mask_buf
->size
- offset
;
2589 actions
= dp_netdev_flow_get_actions(netdev_flow
);
2590 flow
->actions
= actions
->actions
;
2591 flow
->actions_len
= actions
->size
;
2594 flow
->ufid
= netdev_flow
->ufid
;
2595 flow
->ufid_present
= true;
2596 flow
->pmd_id
= netdev_flow
->pmd_id
;
2597 get_dpif_flow_stats(netdev_flow
, &flow
->stats
);
2601 dpif_netdev_mask_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
2602 const struct nlattr
*mask_key
,
2603 uint32_t mask_key_len
, const struct flow
*flow
,
2604 struct flow_wildcards
*wc
, bool probe
)
2606 enum odp_key_fitness fitness
;
2608 fitness
= odp_flow_key_to_mask(mask_key
, mask_key_len
, wc
, flow
);
2611 /* This should not happen: it indicates that
2612 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
2613 * disagree on the acceptable form of a mask. Log the problem
2614 * as an error, with enough details to enable debugging. */
2615 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2617 if (!VLOG_DROP_ERR(&rl
)) {
2621 odp_flow_format(key
, key_len
, mask_key
, mask_key_len
, NULL
, &s
,
2623 VLOG_ERR("internal error parsing flow mask %s (%s)",
2624 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
2636 dpif_netdev_flow_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
2637 struct flow
*flow
, bool probe
)
2639 if (odp_flow_key_to_flow(key
, key_len
, flow
)) {
2641 /* This should not happen: it indicates that
2642 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
2643 * the acceptable form of a flow. Log the problem as an error,
2644 * with enough details to enable debugging. */
2645 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2647 if (!VLOG_DROP_ERR(&rl
)) {
2651 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
2652 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
2660 if (flow
->ct_state
& DP_NETDEV_CS_UNSUPPORTED_MASK
) {
2668 dpif_netdev_flow_get(const struct dpif
*dpif
, const struct dpif_flow_get
*get
)
2670 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2671 struct dp_netdev_flow
*netdev_flow
;
2672 struct dp_netdev_pmd_thread
*pmd
;
2673 struct hmapx to_find
= HMAPX_INITIALIZER(&to_find
);
2674 struct hmapx_node
*node
;
2677 if (get
->pmd_id
== PMD_ID_NULL
) {
2678 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2679 if (dp_netdev_pmd_try_ref(pmd
) && !hmapx_add(&to_find
, pmd
)) {
2680 dp_netdev_pmd_unref(pmd
);
2684 pmd
= dp_netdev_get_pmd(dp
, get
->pmd_id
);
2688 hmapx_add(&to_find
, pmd
);
2691 if (!hmapx_count(&to_find
)) {
2695 HMAPX_FOR_EACH (node
, &to_find
) {
2696 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
2697 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, get
->ufid
, get
->key
,
2700 dp_netdev_flow_to_dpif_flow(netdev_flow
, get
->buffer
, get
->buffer
,
2709 HMAPX_FOR_EACH (node
, &to_find
) {
2710 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
2711 dp_netdev_pmd_unref(pmd
);
2714 hmapx_destroy(&to_find
);
2719 * There are two flow offload operations here: addition and modification.
2721 * For flow addition, this function does:
2722 * - allocate a new flow mark id
2723 * - perform hardware flow offload
2724 * - associate the flow mark with flow and mega flow
2726 * For flow modification, both flow mark and the associations are still
2727 * valid, thus only item 2 needed.
2730 try_netdev_flow_put(struct dp_netdev_pmd_thread
*pmd
, odp_port_t in_port
,
2731 struct dp_netdev_flow
*flow
, struct match
*match
,
2732 const struct nlattr
*actions
, size_t actions_len
)
2734 struct offload_info info
;
2735 struct dp_netdev_port
*port
;
2736 bool modification
= flow
->mark
!= INVALID_FLOW_MARK
;
2737 const char *op
= modification
? "modify" : "add";
2741 ovs_mutex_lock(&flow_mark
.mutex
);
2746 if (!netdev_is_flow_api_enabled()) {
2751 * If a mega flow has already been offloaded (from other PMD
2752 * instances), do not offload it again.
2754 mark
= megaflow_to_mark_find(&flow
->mega_ufid
);
2755 if (mark
!= INVALID_FLOW_MARK
) {
2756 VLOG_DBG("Flow has already been offloaded with mark %u\n", mark
);
2757 mark_to_flow_associate(mark
, flow
);
2761 mark
= flow_mark_alloc();
2762 if (mark
== INVALID_FLOW_MARK
) {
2763 VLOG_ERR("Failed to allocate flow mark!\n");
2767 info
.flow_mark
= mark
;
2769 ovs_mutex_lock(&pmd
->dp
->port_mutex
);
2770 port
= dp_netdev_lookup_port(pmd
->dp
, in_port
);
2772 ovs_mutex_unlock(&pmd
->dp
->port_mutex
);
2775 ret
= netdev_flow_put(port
->netdev
, match
,
2776 CONST_CAST(struct nlattr
*, actions
),
2777 actions_len
, &flow
->mega_ufid
, &info
, NULL
);
2778 ovs_mutex_unlock(&pmd
->dp
->port_mutex
);
2781 VLOG_ERR("Failed to %s netdev flow with mark %u\n", op
, mark
);
2782 if (!modification
) {
2783 flow_mark_free(mark
);
2785 mark_to_flow_disassociate(pmd
, flow
);
2790 if (!modification
) {
2791 megaflow_to_mark_associate(&flow
->mega_ufid
, mark
);
2792 mark_to_flow_associate(mark
, flow
);
2794 VLOG_DBG("Succeed to %s netdev flow with mark %u\n", op
, mark
);
2797 ovs_mutex_unlock(&flow_mark
.mutex
);
2801 dp_netdev_get_mega_ufid(const struct match
*match
, ovs_u128
*mega_ufid
)
2803 struct flow masked_flow
;
2806 for (i
= 0; i
< sizeof(struct flow
); i
++) {
2807 ((uint8_t *)&masked_flow
)[i
] = ((uint8_t *)&match
->flow
)[i
] &
2808 ((uint8_t *)&match
->wc
)[i
];
2810 dpif_flow_hash(NULL
, &masked_flow
, sizeof(struct flow
), mega_ufid
);
2813 static struct dp_netdev_flow
*
2814 dp_netdev_flow_add(struct dp_netdev_pmd_thread
*pmd
,
2815 struct match
*match
, const ovs_u128
*ufid
,
2816 const struct nlattr
*actions
, size_t actions_len
)
2817 OVS_REQUIRES(pmd
->flow_mutex
)
2819 struct dp_netdev_flow
*flow
;
2820 struct netdev_flow_key mask
;
2823 /* Make sure in_port is exact matched before we read it. */
2824 ovs_assert(match
->wc
.masks
.in_port
.odp_port
== ODPP_NONE
);
2825 odp_port_t in_port
= match
->flow
.in_port
.odp_port
;
2827 /* As we select the dpcls based on the port number, each netdev flow
2828 * belonging to the same dpcls will have the same odp_port value.
2829 * For performance reasons we wildcard odp_port here in the mask. In the
2830 * typical case dp_hash is also wildcarded, and the resulting 8-byte
2831 * chunk {dp_hash, in_port} will be ignored by netdev_flow_mask_init() and
2832 * will not be part of the subtable mask.
2833 * This will speed up the hash computation during dpcls_lookup() because
2834 * there is one less call to hash_add64() in this case. */
2835 match
->wc
.masks
.in_port
.odp_port
= 0;
2836 netdev_flow_mask_init(&mask
, match
);
2837 match
->wc
.masks
.in_port
.odp_port
= ODPP_NONE
;
2839 /* Make sure wc does not have metadata. */
2840 ovs_assert(!FLOWMAP_HAS_FIELD(&mask
.mf
.map
, metadata
)
2841 && !FLOWMAP_HAS_FIELD(&mask
.mf
.map
, regs
));
2843 /* Do not allocate extra space. */
2844 flow
= xmalloc(sizeof *flow
- sizeof flow
->cr
.flow
.mf
+ mask
.len
);
2845 memset(&flow
->stats
, 0, sizeof flow
->stats
);
2848 flow
->mark
= INVALID_FLOW_MARK
;
2849 *CONST_CAST(unsigned *, &flow
->pmd_id
) = pmd
->core_id
;
2850 *CONST_CAST(struct flow
*, &flow
->flow
) = match
->flow
;
2851 *CONST_CAST(ovs_u128
*, &flow
->ufid
) = *ufid
;
2852 ovs_refcount_init(&flow
->ref_cnt
);
2853 ovsrcu_set(&flow
->actions
, dp_netdev_actions_create(actions
, actions_len
));
2855 dp_netdev_get_mega_ufid(match
, CONST_CAST(ovs_u128
*, &flow
->mega_ufid
));
2856 netdev_flow_key_init_masked(&flow
->cr
.flow
, &match
->flow
, &mask
);
2858 /* Select dpcls for in_port. Relies on in_port to be exact match. */
2859 cls
= dp_netdev_pmd_find_dpcls(pmd
, in_port
);
2860 dpcls_insert(cls
, &flow
->cr
, &mask
);
2862 cmap_insert(&pmd
->flow_table
, CONST_CAST(struct cmap_node
*, &flow
->node
),
2863 dp_netdev_flow_hash(&flow
->ufid
));
2865 try_netdev_flow_put(pmd
, in_port
, flow
, match
, actions
, actions_len
);
2867 if (OVS_UNLIKELY(!VLOG_DROP_DBG((&upcall_rl
)))) {
2868 struct ds ds
= DS_EMPTY_INITIALIZER
;
2869 struct ofpbuf key_buf
, mask_buf
;
2870 struct odp_flow_key_parms odp_parms
= {
2871 .flow
= &match
->flow
,
2872 .mask
= &match
->wc
.masks
,
2873 .support
= dp_netdev_support
,
2876 ofpbuf_init(&key_buf
, 0);
2877 ofpbuf_init(&mask_buf
, 0);
2879 odp_flow_key_from_flow(&odp_parms
, &key_buf
);
2880 odp_parms
.key_buf
= &key_buf
;
2881 odp_flow_key_from_mask(&odp_parms
, &mask_buf
);
2883 ds_put_cstr(&ds
, "flow_add: ");
2884 odp_format_ufid(ufid
, &ds
);
2885 ds_put_cstr(&ds
, " ");
2886 odp_flow_format(key_buf
.data
, key_buf
.size
,
2887 mask_buf
.data
, mask_buf
.size
,
2889 ds_put_cstr(&ds
, ", actions:");
2890 format_odp_actions(&ds
, actions
, actions_len
, NULL
);
2892 VLOG_DBG("%s", ds_cstr(&ds
));
2894 ofpbuf_uninit(&key_buf
);
2895 ofpbuf_uninit(&mask_buf
);
2897 /* Add a printout of the actual match installed. */
2900 ds_put_cstr(&ds
, "flow match: ");
2901 miniflow_expand(&flow
->cr
.flow
.mf
, &m
.flow
);
2902 miniflow_expand(&flow
->cr
.mask
->mf
, &m
.wc
.masks
);
2903 memset(&m
.tun_md
, 0, sizeof m
.tun_md
);
2904 match_format(&m
, NULL
, &ds
, OFP_DEFAULT_PRIORITY
);
2906 VLOG_DBG("%s", ds_cstr(&ds
));
2915 flow_put_on_pmd(struct dp_netdev_pmd_thread
*pmd
,
2916 struct netdev_flow_key
*key
,
2917 struct match
*match
,
2919 const struct dpif_flow_put
*put
,
2920 struct dpif_flow_stats
*stats
)
2922 struct dp_netdev_flow
*netdev_flow
;
2926 memset(stats
, 0, sizeof *stats
);
2929 ovs_mutex_lock(&pmd
->flow_mutex
);
2930 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
, NULL
);
2932 if (put
->flags
& DPIF_FP_CREATE
) {
2933 if (cmap_count(&pmd
->flow_table
) < MAX_FLOWS
) {
2934 dp_netdev_flow_add(pmd
, match
, ufid
, put
->actions
,
2944 if (put
->flags
& DPIF_FP_MODIFY
) {
2945 struct dp_netdev_actions
*new_actions
;
2946 struct dp_netdev_actions
*old_actions
;
2947 odp_port_t in_port
= netdev_flow
->flow
.in_port
.odp_port
;
2949 new_actions
= dp_netdev_actions_create(put
->actions
,
2952 old_actions
= dp_netdev_flow_get_actions(netdev_flow
);
2953 ovsrcu_set(&netdev_flow
->actions
, new_actions
);
2955 try_netdev_flow_put(pmd
, in_port
, netdev_flow
, match
,
2956 put
->actions
, put
->actions_len
);
2959 get_dpif_flow_stats(netdev_flow
, stats
);
2961 if (put
->flags
& DPIF_FP_ZERO_STATS
) {
2962 /* XXX: The userspace datapath uses thread local statistics
2963 * (for flows), which should be updated only by the owning
2964 * thread. Since we cannot write on stats memory here,
2965 * we choose not to support this flag. Please note:
2966 * - This feature is currently used only by dpctl commands with
2968 * - Should the need arise, this operation can be implemented
2969 * by keeping a base value (to be update here) for each
2970 * counter, and subtracting it before outputting the stats */
2974 ovsrcu_postpone(dp_netdev_actions_free
, old_actions
);
2975 } else if (put
->flags
& DPIF_FP_CREATE
) {
2978 /* Overlapping flow. */
2982 ovs_mutex_unlock(&pmd
->flow_mutex
);
2987 dpif_netdev_flow_put(struct dpif
*dpif
, const struct dpif_flow_put
*put
)
2989 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2990 struct netdev_flow_key key
, mask
;
2991 struct dp_netdev_pmd_thread
*pmd
;
2995 bool probe
= put
->flags
& DPIF_FP_PROBE
;
2998 memset(put
->stats
, 0, sizeof *put
->stats
);
3000 error
= dpif_netdev_flow_from_nlattrs(put
->key
, put
->key_len
, &match
.flow
,
3005 error
= dpif_netdev_mask_from_nlattrs(put
->key
, put
->key_len
,
3006 put
->mask
, put
->mask_len
,
3007 &match
.flow
, &match
.wc
, probe
);
3015 dpif_flow_hash(dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
3018 /* Must produce a netdev_flow_key for lookup.
3019 * Use the same method as employed to create the key when adding
3020 * the flow to the dplcs to make sure they match. */
3021 netdev_flow_mask_init(&mask
, &match
);
3022 netdev_flow_key_init_masked(&key
, &match
.flow
, &mask
);
3024 if (put
->pmd_id
== PMD_ID_NULL
) {
3025 if (cmap_count(&dp
->poll_threads
) == 0) {
3028 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3029 struct dpif_flow_stats pmd_stats
;
3032 pmd_error
= flow_put_on_pmd(pmd
, &key
, &match
, &ufid
, put
,
3036 } else if (put
->stats
) {
3037 put
->stats
->n_packets
+= pmd_stats
.n_packets
;
3038 put
->stats
->n_bytes
+= pmd_stats
.n_bytes
;
3039 put
->stats
->used
= MAX(put
->stats
->used
, pmd_stats
.used
);
3040 put
->stats
->tcp_flags
|= pmd_stats
.tcp_flags
;
3044 pmd
= dp_netdev_get_pmd(dp
, put
->pmd_id
);
3048 error
= flow_put_on_pmd(pmd
, &key
, &match
, &ufid
, put
, put
->stats
);
3049 dp_netdev_pmd_unref(pmd
);
3056 flow_del_on_pmd(struct dp_netdev_pmd_thread
*pmd
,
3057 struct dpif_flow_stats
*stats
,
3058 const struct dpif_flow_del
*del
)
3060 struct dp_netdev_flow
*netdev_flow
;
3063 ovs_mutex_lock(&pmd
->flow_mutex
);
3064 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, del
->ufid
, del
->key
,
3068 get_dpif_flow_stats(netdev_flow
, stats
);
3070 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
3074 ovs_mutex_unlock(&pmd
->flow_mutex
);
3080 dpif_netdev_flow_del(struct dpif
*dpif
, const struct dpif_flow_del
*del
)
3082 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3083 struct dp_netdev_pmd_thread
*pmd
;
3087 memset(del
->stats
, 0, sizeof *del
->stats
);
3090 if (del
->pmd_id
== PMD_ID_NULL
) {
3091 if (cmap_count(&dp
->poll_threads
) == 0) {
3094 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3095 struct dpif_flow_stats pmd_stats
;
3098 pmd_error
= flow_del_on_pmd(pmd
, &pmd_stats
, del
);
3101 } else if (del
->stats
) {
3102 del
->stats
->n_packets
+= pmd_stats
.n_packets
;
3103 del
->stats
->n_bytes
+= pmd_stats
.n_bytes
;
3104 del
->stats
->used
= MAX(del
->stats
->used
, pmd_stats
.used
);
3105 del
->stats
->tcp_flags
|= pmd_stats
.tcp_flags
;
3109 pmd
= dp_netdev_get_pmd(dp
, del
->pmd_id
);
3113 error
= flow_del_on_pmd(pmd
, del
->stats
, del
);
3114 dp_netdev_pmd_unref(pmd
);
3121 struct dpif_netdev_flow_dump
{
3122 struct dpif_flow_dump up
;
3123 struct cmap_position poll_thread_pos
;
3124 struct cmap_position flow_pos
;
3125 struct dp_netdev_pmd_thread
*cur_pmd
;
3127 struct ovs_mutex mutex
;
3130 static struct dpif_netdev_flow_dump
*
3131 dpif_netdev_flow_dump_cast(struct dpif_flow_dump
*dump
)
3133 return CONTAINER_OF(dump
, struct dpif_netdev_flow_dump
, up
);
3136 static struct dpif_flow_dump
*
3137 dpif_netdev_flow_dump_create(const struct dpif
*dpif_
, bool terse
,
3138 char *type OVS_UNUSED
)
3140 struct dpif_netdev_flow_dump
*dump
;
3142 dump
= xzalloc(sizeof *dump
);
3143 dpif_flow_dump_init(&dump
->up
, dpif_
);
3144 dump
->up
.terse
= terse
;
3145 ovs_mutex_init(&dump
->mutex
);
3151 dpif_netdev_flow_dump_destroy(struct dpif_flow_dump
*dump_
)
3153 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
3155 ovs_mutex_destroy(&dump
->mutex
);
3160 struct dpif_netdev_flow_dump_thread
{
3161 struct dpif_flow_dump_thread up
;
3162 struct dpif_netdev_flow_dump
*dump
;
3163 struct odputil_keybuf keybuf
[FLOW_DUMP_MAX_BATCH
];
3164 struct odputil_keybuf maskbuf
[FLOW_DUMP_MAX_BATCH
];
3167 static struct dpif_netdev_flow_dump_thread
*
3168 dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread
*thread
)
3170 return CONTAINER_OF(thread
, struct dpif_netdev_flow_dump_thread
, up
);
3173 static struct dpif_flow_dump_thread
*
3174 dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump
*dump_
)
3176 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
3177 struct dpif_netdev_flow_dump_thread
*thread
;
3179 thread
= xmalloc(sizeof *thread
);
3180 dpif_flow_dump_thread_init(&thread
->up
, &dump
->up
);
3181 thread
->dump
= dump
;
3186 dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread
*thread_
)
3188 struct dpif_netdev_flow_dump_thread
*thread
3189 = dpif_netdev_flow_dump_thread_cast(thread_
);
3195 dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread
*thread_
,
3196 struct dpif_flow
*flows
, int max_flows
)
3198 struct dpif_netdev_flow_dump_thread
*thread
3199 = dpif_netdev_flow_dump_thread_cast(thread_
);
3200 struct dpif_netdev_flow_dump
*dump
= thread
->dump
;
3201 struct dp_netdev_flow
*netdev_flows
[FLOW_DUMP_MAX_BATCH
];
3205 ovs_mutex_lock(&dump
->mutex
);
3206 if (!dump
->status
) {
3207 struct dpif_netdev
*dpif
= dpif_netdev_cast(thread
->up
.dpif
);
3208 struct dp_netdev
*dp
= get_dp_netdev(&dpif
->dpif
);
3209 struct dp_netdev_pmd_thread
*pmd
= dump
->cur_pmd
;
3210 int flow_limit
= MIN(max_flows
, FLOW_DUMP_MAX_BATCH
);
3212 /* First call to dump_next(), extracts the first pmd thread.
3213 * If there is no pmd thread, returns immediately. */
3215 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
3217 ovs_mutex_unlock(&dump
->mutex
);
3224 for (n_flows
= 0; n_flows
< flow_limit
; n_flows
++) {
3225 struct cmap_node
*node
;
3227 node
= cmap_next_position(&pmd
->flow_table
, &dump
->flow_pos
);
3231 netdev_flows
[n_flows
] = CONTAINER_OF(node
,
3232 struct dp_netdev_flow
,
3235 /* When finishing dumping the current pmd thread, moves to
3237 if (n_flows
< flow_limit
) {
3238 memset(&dump
->flow_pos
, 0, sizeof dump
->flow_pos
);
3239 dp_netdev_pmd_unref(pmd
);
3240 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
3246 /* Keeps the reference to next caller. */
3247 dump
->cur_pmd
= pmd
;
3249 /* If the current dump is empty, do not exit the loop, since the
3250 * remaining pmds could have flows to be dumped. Just dumps again
3251 * on the new 'pmd'. */
3254 ovs_mutex_unlock(&dump
->mutex
);
3256 for (i
= 0; i
< n_flows
; i
++) {
3257 struct odputil_keybuf
*maskbuf
= &thread
->maskbuf
[i
];
3258 struct odputil_keybuf
*keybuf
= &thread
->keybuf
[i
];
3259 struct dp_netdev_flow
*netdev_flow
= netdev_flows
[i
];
3260 struct dpif_flow
*f
= &flows
[i
];
3261 struct ofpbuf key
, mask
;
3263 ofpbuf_use_stack(&key
, keybuf
, sizeof *keybuf
);
3264 ofpbuf_use_stack(&mask
, maskbuf
, sizeof *maskbuf
);
3265 dp_netdev_flow_to_dpif_flow(netdev_flow
, &key
, &mask
, f
,
3273 dpif_netdev_execute(struct dpif
*dpif
, struct dpif_execute
*execute
)
3274 OVS_NO_THREAD_SAFETY_ANALYSIS
3276 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3277 struct dp_netdev_pmd_thread
*pmd
;
3278 struct dp_packet_batch pp
;
3280 if (dp_packet_size(execute
->packet
) < ETH_HEADER_LEN
||
3281 dp_packet_size(execute
->packet
) > UINT16_MAX
) {
3285 /* Tries finding the 'pmd'. If NULL is returned, that means
3286 * the current thread is a non-pmd thread and should use
3287 * dp_netdev_get_pmd(dp, NON_PMD_CORE_ID). */
3288 pmd
= ovsthread_getspecific(dp
->per_pmd_key
);
3290 pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
3296 if (execute
->probe
) {
3297 /* If this is part of a probe, Drop the packet, since executing
3298 * the action may actually cause spurious packets be sent into
3300 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3301 dp_netdev_pmd_unref(pmd
);
3306 /* If the current thread is non-pmd thread, acquires
3307 * the 'non_pmd_mutex'. */
3308 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3309 ovs_mutex_lock(&dp
->non_pmd_mutex
);
3312 /* Update current time in PMD context. */
3313 pmd_thread_ctx_time_update(pmd
);
3315 /* The action processing expects the RSS hash to be valid, because
3316 * it's always initialized at the beginning of datapath processing.
3317 * In this case, though, 'execute->packet' may not have gone through
3318 * the datapath at all, it may have been generated by the upper layer
3319 * (OpenFlow packet-out, BFD frame, ...). */
3320 if (!dp_packet_rss_valid(execute
->packet
)) {
3321 dp_packet_set_rss_hash(execute
->packet
,
3322 flow_hash_5tuple(execute
->flow
, 0));
3325 dp_packet_batch_init_packet(&pp
, execute
->packet
);
3326 dp_netdev_execute_actions(pmd
, &pp
, false, execute
->flow
,
3327 execute
->actions
, execute
->actions_len
);
3328 dp_netdev_pmd_flush_output_packets(pmd
, true);
3330 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3331 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
3332 dp_netdev_pmd_unref(pmd
);
3339 dpif_netdev_operate(struct dpif
*dpif
, struct dpif_op
**ops
, size_t n_ops
)
3343 for (i
= 0; i
< n_ops
; i
++) {
3344 struct dpif_op
*op
= ops
[i
];
3347 case DPIF_OP_FLOW_PUT
:
3348 op
->error
= dpif_netdev_flow_put(dpif
, &op
->flow_put
);
3351 case DPIF_OP_FLOW_DEL
:
3352 op
->error
= dpif_netdev_flow_del(dpif
, &op
->flow_del
);
3355 case DPIF_OP_EXECUTE
:
3356 op
->error
= dpif_netdev_execute(dpif
, &op
->execute
);
3359 case DPIF_OP_FLOW_GET
:
3360 op
->error
= dpif_netdev_flow_get(dpif
, &op
->flow_get
);
3366 /* Applies datapath configuration from the database. Some of the changes are
3367 * actually applied in dpif_netdev_run(). */
3369 dpif_netdev_set_config(struct dpif
*dpif
, const struct smap
*other_config
)
3371 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3372 const char *cmask
= smap_get(other_config
, "pmd-cpu-mask");
3373 unsigned long long insert_prob
=
3374 smap_get_ullong(other_config
, "emc-insert-inv-prob",
3375 DEFAULT_EM_FLOW_INSERT_INV_PROB
);
3376 uint32_t insert_min
, cur_min
;
3377 uint32_t tx_flush_interval
, cur_tx_flush_interval
;
3379 tx_flush_interval
= smap_get_int(other_config
, "tx-flush-interval",
3380 DEFAULT_TX_FLUSH_INTERVAL
);
3381 atomic_read_relaxed(&dp
->tx_flush_interval
, &cur_tx_flush_interval
);
3382 if (tx_flush_interval
!= cur_tx_flush_interval
) {
3383 atomic_store_relaxed(&dp
->tx_flush_interval
, tx_flush_interval
);
3384 VLOG_INFO("Flushing interval for tx queues set to %"PRIu32
" us",
3388 if (!nullable_string_is_equal(dp
->pmd_cmask
, cmask
)) {
3389 free(dp
->pmd_cmask
);
3390 dp
->pmd_cmask
= nullable_xstrdup(cmask
);
3391 dp_netdev_request_reconfigure(dp
);
3394 atomic_read_relaxed(&dp
->emc_insert_min
, &cur_min
);
3395 if (insert_prob
<= UINT32_MAX
) {
3396 insert_min
= insert_prob
== 0 ? 0 : UINT32_MAX
/ insert_prob
;
3398 insert_min
= DEFAULT_EM_FLOW_INSERT_MIN
;
3399 insert_prob
= DEFAULT_EM_FLOW_INSERT_INV_PROB
;
3402 if (insert_min
!= cur_min
) {
3403 atomic_store_relaxed(&dp
->emc_insert_min
, insert_min
);
3404 if (insert_min
== 0) {
3405 VLOG_INFO("EMC has been disabled");
3407 VLOG_INFO("EMC insertion probability changed to 1/%llu (~%.2f%%)",
3408 insert_prob
, (100 / (float)insert_prob
));
3412 bool perf_enabled
= smap_get_bool(other_config
, "pmd-perf-metrics", false);
3413 bool cur_perf_enabled
;
3414 atomic_read_relaxed(&dp
->pmd_perf_metrics
, &cur_perf_enabled
);
3415 if (perf_enabled
!= cur_perf_enabled
) {
3416 atomic_store_relaxed(&dp
->pmd_perf_metrics
, perf_enabled
);
3418 VLOG_INFO("PMD performance metrics collection enabled");
3420 VLOG_INFO("PMD performance metrics collection disabled");
3427 /* Parses affinity list and returns result in 'core_ids'. */
3429 parse_affinity_list(const char *affinity_list
, unsigned *core_ids
, int n_rxq
)
3432 char *list
, *copy
, *key
, *value
;
3435 for (i
= 0; i
< n_rxq
; i
++) {
3436 core_ids
[i
] = OVS_CORE_UNSPEC
;
3439 if (!affinity_list
) {
3443 list
= copy
= xstrdup(affinity_list
);
3445 while (ofputil_parse_key_value(&list
, &key
, &value
)) {
3446 int rxq_id
, core_id
;
3448 if (!str_to_int(key
, 0, &rxq_id
) || rxq_id
< 0
3449 || !str_to_int(value
, 0, &core_id
) || core_id
< 0) {
3454 if (rxq_id
< n_rxq
) {
3455 core_ids
[rxq_id
] = core_id
;
3463 /* Parses 'affinity_list' and applies configuration if it is valid. */
3465 dpif_netdev_port_set_rxq_affinity(struct dp_netdev_port
*port
,
3466 const char *affinity_list
)
3468 unsigned *core_ids
, i
;
3471 core_ids
= xmalloc(port
->n_rxq
* sizeof *core_ids
);
3472 if (parse_affinity_list(affinity_list
, core_ids
, port
->n_rxq
)) {
3477 for (i
= 0; i
< port
->n_rxq
; i
++) {
3478 port
->rxqs
[i
].core_id
= core_ids
[i
];
3486 /* Changes the affinity of port's rx queues. The changes are actually applied
3487 * in dpif_netdev_run(). */
3489 dpif_netdev_port_set_config(struct dpif
*dpif
, odp_port_t port_no
,
3490 const struct smap
*cfg
)
3492 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3493 struct dp_netdev_port
*port
;
3495 const char *affinity_list
= smap_get(cfg
, "pmd-rxq-affinity");
3497 ovs_mutex_lock(&dp
->port_mutex
);
3498 error
= get_port_by_number(dp
, port_no
, &port
);
3499 if (error
|| !netdev_is_pmd(port
->netdev
)
3500 || nullable_string_is_equal(affinity_list
, port
->rxq_affinity_list
)) {
3504 error
= dpif_netdev_port_set_rxq_affinity(port
, affinity_list
);
3508 free(port
->rxq_affinity_list
);
3509 port
->rxq_affinity_list
= nullable_xstrdup(affinity_list
);
3511 dp_netdev_request_reconfigure(dp
);
3513 ovs_mutex_unlock(&dp
->port_mutex
);
3518 dpif_netdev_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
3519 uint32_t queue_id
, uint32_t *priority
)
3521 *priority
= queue_id
;
3526 /* Creates and returns a new 'struct dp_netdev_actions', whose actions are
3527 * a copy of the 'size' bytes of 'actions' input parameters. */
3528 struct dp_netdev_actions
*
3529 dp_netdev_actions_create(const struct nlattr
*actions
, size_t size
)
3531 struct dp_netdev_actions
*netdev_actions
;
3533 netdev_actions
= xmalloc(sizeof *netdev_actions
+ size
);
3534 memcpy(netdev_actions
->actions
, actions
, size
);
3535 netdev_actions
->size
= size
;
3537 return netdev_actions
;
3540 struct dp_netdev_actions
*
3541 dp_netdev_flow_get_actions(const struct dp_netdev_flow
*flow
)
3543 return ovsrcu_get(struct dp_netdev_actions
*, &flow
->actions
);
3547 dp_netdev_actions_free(struct dp_netdev_actions
*actions
)
3553 dp_netdev_rxq_set_cycles(struct dp_netdev_rxq
*rx
,
3554 enum rxq_cycles_counter_type type
,
3555 unsigned long long cycles
)
3557 atomic_store_relaxed(&rx
->cycles
[type
], cycles
);
3561 dp_netdev_rxq_add_cycles(struct dp_netdev_rxq
*rx
,
3562 enum rxq_cycles_counter_type type
,
3563 unsigned long long cycles
)
3565 non_atomic_ullong_add(&rx
->cycles
[type
], cycles
);
3569 dp_netdev_rxq_get_cycles(struct dp_netdev_rxq
*rx
,
3570 enum rxq_cycles_counter_type type
)
3572 unsigned long long processing_cycles
;
3573 atomic_read_relaxed(&rx
->cycles
[type
], &processing_cycles
);
3574 return processing_cycles
;
3578 dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq
*rx
,
3579 unsigned long long cycles
)
3581 unsigned int idx
= rx
->intrvl_idx
++ % PMD_RXQ_INTERVAL_MAX
;
3582 atomic_store_relaxed(&rx
->cycles_intrvl
[idx
], cycles
);
3586 dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq
*rx
, unsigned idx
)
3588 unsigned long long processing_cycles
;
3589 atomic_read_relaxed(&rx
->cycles_intrvl
[idx
], &processing_cycles
);
3590 return processing_cycles
;
3593 #if ATOMIC_ALWAYS_LOCK_FREE_8B
3595 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread
*pmd
)
3597 bool pmd_perf_enabled
;
3598 atomic_read_relaxed(&pmd
->dp
->pmd_perf_metrics
, &pmd_perf_enabled
);
3599 return pmd_perf_enabled
;
3602 /* If stores and reads of 64-bit integers are not atomic, the full PMD
3603 * performance metrics are not available as locked access to 64 bit
3604 * integers would be prohibitively expensive. */
3606 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread
*pmd OVS_UNUSED
)
3613 dp_netdev_pmd_flush_output_on_port(struct dp_netdev_pmd_thread
*pmd
,
3620 struct cycle_timer timer
;
3622 uint32_t tx_flush_interval
;
3624 cycle_timer_start(&pmd
->perf_stats
, &timer
);
3626 dynamic_txqs
= p
->port
->dynamic_txqs
;
3628 tx_qid
= dpif_netdev_xps_get_tx_qid(pmd
, p
);
3630 tx_qid
= pmd
->static_tx_qid
;
3633 output_cnt
= dp_packet_batch_size(&p
->output_pkts
);
3634 ovs_assert(output_cnt
> 0);
3636 netdev_send(p
->port
->netdev
, tx_qid
, &p
->output_pkts
, dynamic_txqs
);
3637 dp_packet_batch_init(&p
->output_pkts
);
3639 /* Update time of the next flush. */
3640 atomic_read_relaxed(&pmd
->dp
->tx_flush_interval
, &tx_flush_interval
);
3641 p
->flush_time
= pmd
->ctx
.now
+ tx_flush_interval
;
3643 ovs_assert(pmd
->n_output_batches
> 0);
3644 pmd
->n_output_batches
--;
3646 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_SENT_PKTS
, output_cnt
);
3647 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_SENT_BATCHES
, 1);
3649 /* Distribute send cycles evenly among transmitted packets and assign to
3650 * their respective rx queues. */
3651 cycles
= cycle_timer_stop(&pmd
->perf_stats
, &timer
) / output_cnt
;
3652 for (i
= 0; i
< output_cnt
; i
++) {
3653 if (p
->output_pkts_rxqs
[i
]) {
3654 dp_netdev_rxq_add_cycles(p
->output_pkts_rxqs
[i
],
3655 RXQ_CYCLES_PROC_CURR
, cycles
);
3663 dp_netdev_pmd_flush_output_packets(struct dp_netdev_pmd_thread
*pmd
,
3669 if (!pmd
->n_output_batches
) {
3673 HMAP_FOR_EACH (p
, node
, &pmd
->send_port_cache
) {
3674 if (!dp_packet_batch_is_empty(&p
->output_pkts
)
3675 && (force
|| pmd
->ctx
.now
>= p
->flush_time
)) {
3676 output_cnt
+= dp_netdev_pmd_flush_output_on_port(pmd
, p
);
3683 dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread
*pmd
,
3684 struct dp_netdev_rxq
*rxq
,
3687 struct pmd_perf_stats
*s
= &pmd
->perf_stats
;
3688 struct dp_packet_batch batch
;
3689 struct cycle_timer timer
;
3692 int rem_qlen
= 0, *qlen_p
= NULL
;
3695 /* Measure duration for polling and processing rx burst. */
3696 cycle_timer_start(&pmd
->perf_stats
, &timer
);
3698 pmd
->ctx
.last_rxq
= rxq
;
3699 dp_packet_batch_init(&batch
);
3701 /* Fetch the rx queue length only for vhostuser ports. */
3702 if (pmd_perf_metrics_enabled(pmd
) && rxq
->is_vhost
) {
3706 error
= netdev_rxq_recv(rxq
->rx
, &batch
, qlen_p
);
3708 /* At least one packet received. */
3709 *recirc_depth_get() = 0;
3710 pmd_thread_ctx_time_update(pmd
);
3711 batch_cnt
= batch
.count
;
3712 if (pmd_perf_metrics_enabled(pmd
)) {
3713 /* Update batch histogram. */
3714 s
->current
.batches
++;
3715 histogram_add_sample(&s
->pkts_per_batch
, batch_cnt
);
3716 /* Update the maximum vhost rx queue fill level. */
3717 if (rxq
->is_vhost
&& rem_qlen
>= 0) {
3718 uint32_t qfill
= batch_cnt
+ rem_qlen
;
3719 if (qfill
> s
->current
.max_vhost_qfill
) {
3720 s
->current
.max_vhost_qfill
= qfill
;
3724 /* Process packet batch. */
3725 dp_netdev_input(pmd
, &batch
, port_no
);
3727 /* Assign processing cycles to rx queue. */
3728 cycles
= cycle_timer_stop(&pmd
->perf_stats
, &timer
);
3729 dp_netdev_rxq_add_cycles(rxq
, RXQ_CYCLES_PROC_CURR
, cycles
);
3731 dp_netdev_pmd_flush_output_packets(pmd
, false);
3733 /* Discard cycles. */
3734 cycle_timer_stop(&pmd
->perf_stats
, &timer
);
3735 if (error
!= EAGAIN
&& error
!= EOPNOTSUPP
) {
3736 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3738 VLOG_ERR_RL(&rl
, "error receiving data from %s: %s",
3739 netdev_rxq_get_name(rxq
->rx
), ovs_strerror(error
));
3743 pmd
->ctx
.last_rxq
= NULL
;
3748 static struct tx_port
*
3749 tx_port_lookup(const struct hmap
*hmap
, odp_port_t port_no
)
3753 HMAP_FOR_EACH_IN_BUCKET (tx
, node
, hash_port_no(port_no
), hmap
) {
3754 if (tx
->port
->port_no
== port_no
) {
3763 port_reconfigure(struct dp_netdev_port
*port
)
3765 struct netdev
*netdev
= port
->netdev
;
3768 /* Closes the existing 'rxq's. */
3769 for (i
= 0; i
< port
->n_rxq
; i
++) {
3770 netdev_rxq_close(port
->rxqs
[i
].rx
);
3771 port
->rxqs
[i
].rx
= NULL
;
3773 unsigned last_nrxq
= port
->n_rxq
;
3776 /* Allows 'netdev' to apply the pending configuration changes. */
3777 if (netdev_is_reconf_required(netdev
) || port
->need_reconfigure
) {
3778 err
= netdev_reconfigure(netdev
);
3779 if (err
&& (err
!= EOPNOTSUPP
)) {
3780 VLOG_ERR("Failed to set interface %s new configuration",
3781 netdev_get_name(netdev
));
3785 /* If the netdev_reconfigure() above succeeds, reopens the 'rxq's. */
3786 port
->rxqs
= xrealloc(port
->rxqs
,
3787 sizeof *port
->rxqs
* netdev_n_rxq(netdev
));
3788 /* Realloc 'used' counters for tx queues. */
3789 free(port
->txq_used
);
3790 port
->txq_used
= xcalloc(netdev_n_txq(netdev
), sizeof *port
->txq_used
);
3792 for (i
= 0; i
< netdev_n_rxq(netdev
); i
++) {
3793 bool new_queue
= i
>= last_nrxq
;
3795 memset(&port
->rxqs
[i
], 0, sizeof port
->rxqs
[i
]);
3798 port
->rxqs
[i
].port
= port
;
3799 port
->rxqs
[i
].is_vhost
= !strncmp(port
->type
, "dpdkvhost", 9);
3801 err
= netdev_rxq_open(netdev
, &port
->rxqs
[i
].rx
, i
);
3808 /* Parse affinity list to apply configuration for new queues. */
3809 dpif_netdev_port_set_rxq_affinity(port
, port
->rxq_affinity_list
);
3811 /* If reconfiguration was successful mark it as such, so we can use it */
3812 port
->need_reconfigure
= false;
3817 struct rr_numa_list
{
3818 struct hmap numas
; /* Contains 'struct rr_numa' */
3822 struct hmap_node node
;
3826 /* Non isolated pmds on numa node 'numa_id' */
3827 struct dp_netdev_pmd_thread
**pmds
;
3834 static struct rr_numa
*
3835 rr_numa_list_lookup(struct rr_numa_list
*rr
, int numa_id
)
3837 struct rr_numa
*numa
;
3839 HMAP_FOR_EACH_WITH_HASH (numa
, node
, hash_int(numa_id
, 0), &rr
->numas
) {
3840 if (numa
->numa_id
== numa_id
) {
3848 /* Returns the next node in numa list following 'numa' in round-robin fashion.
3849 * Returns first node if 'numa' is a null pointer or the last node in 'rr'.
3850 * Returns NULL if 'rr' numa list is empty. */
3851 static struct rr_numa
*
3852 rr_numa_list_next(struct rr_numa_list
*rr
, const struct rr_numa
*numa
)
3854 struct hmap_node
*node
= NULL
;
3857 node
= hmap_next(&rr
->numas
, &numa
->node
);
3860 node
= hmap_first(&rr
->numas
);
3863 return (node
) ? CONTAINER_OF(node
, struct rr_numa
, node
) : NULL
;
3867 rr_numa_list_populate(struct dp_netdev
*dp
, struct rr_numa_list
*rr
)
3869 struct dp_netdev_pmd_thread
*pmd
;
3870 struct rr_numa
*numa
;
3872 hmap_init(&rr
->numas
);
3874 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3875 if (pmd
->core_id
== NON_PMD_CORE_ID
|| pmd
->isolated
) {
3879 numa
= rr_numa_list_lookup(rr
, pmd
->numa_id
);
3881 numa
= xzalloc(sizeof *numa
);
3882 numa
->numa_id
= pmd
->numa_id
;
3883 hmap_insert(&rr
->numas
, &numa
->node
, hash_int(pmd
->numa_id
, 0));
3886 numa
->pmds
= xrealloc(numa
->pmds
, numa
->n_pmds
* sizeof *numa
->pmds
);
3887 numa
->pmds
[numa
->n_pmds
- 1] = pmd
;
3888 /* At least one pmd so initialise curr_idx and idx_inc. */
3889 numa
->cur_index
= 0;
3890 numa
->idx_inc
= true;
3894 /* Returns the next pmd from the numa node in
3895 * incrementing or decrementing order. */
3896 static struct dp_netdev_pmd_thread
*
3897 rr_numa_get_pmd(struct rr_numa
*numa
)
3899 int numa_idx
= numa
->cur_index
;
3901 if (numa
->idx_inc
== true) {
3902 /* Incrementing through list of pmds. */
3903 if (numa
->cur_index
== numa
->n_pmds
-1) {
3904 /* Reached the last pmd. */
3905 numa
->idx_inc
= false;
3910 /* Decrementing through list of pmds. */
3911 if (numa
->cur_index
== 0) {
3912 /* Reached the first pmd. */
3913 numa
->idx_inc
= true;
3918 return numa
->pmds
[numa_idx
];
3922 rr_numa_list_destroy(struct rr_numa_list
*rr
)
3924 struct rr_numa
*numa
;
3926 HMAP_FOR_EACH_POP (numa
, node
, &rr
->numas
) {
3930 hmap_destroy(&rr
->numas
);
3933 /* Sort Rx Queues by the processing cycles they are consuming. */
3935 compare_rxq_cycles(const void *a
, const void *b
)
3937 struct dp_netdev_rxq
*qa
;
3938 struct dp_netdev_rxq
*qb
;
3939 uint64_t cycles_qa
, cycles_qb
;
3941 qa
= *(struct dp_netdev_rxq
**) a
;
3942 qb
= *(struct dp_netdev_rxq
**) b
;
3944 cycles_qa
= dp_netdev_rxq_get_cycles(qa
, RXQ_CYCLES_PROC_HIST
);
3945 cycles_qb
= dp_netdev_rxq_get_cycles(qb
, RXQ_CYCLES_PROC_HIST
);
3947 if (cycles_qa
!= cycles_qb
) {
3948 return (cycles_qa
< cycles_qb
) ? 1 : -1;
3950 /* Cycles are the same so tiebreak on port/queue id.
3951 * Tiebreaking (as opposed to return 0) ensures consistent
3952 * sort results across multiple OS's. */
3953 uint32_t port_qa
= odp_to_u32(qa
->port
->port_no
);
3954 uint32_t port_qb
= odp_to_u32(qb
->port
->port_no
);
3955 if (port_qa
!= port_qb
) {
3956 return port_qa
> port_qb
? 1 : -1;
3958 return netdev_rxq_get_queue_id(qa
->rx
)
3959 - netdev_rxq_get_queue_id(qb
->rx
);
3964 /* Assign pmds to queues. If 'pinned' is true, assign pmds to pinned
3965 * queues and marks the pmds as isolated. Otherwise, assign non isolated
3966 * pmds to unpinned queues.
3968 * If 'pinned' is false queues will be sorted by processing cycles they are
3969 * consuming and then assigned to pmds in round robin order.
3971 * The function doesn't touch the pmd threads, it just stores the assignment
3972 * in the 'pmd' member of each rxq. */
3974 rxq_scheduling(struct dp_netdev
*dp
, bool pinned
) OVS_REQUIRES(dp
->port_mutex
)
3976 struct dp_netdev_port
*port
;
3977 struct rr_numa_list rr
;
3978 struct rr_numa
*non_local_numa
= NULL
;
3979 struct dp_netdev_rxq
** rxqs
= NULL
;
3981 struct rr_numa
*numa
= NULL
;
3984 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3985 if (!netdev_is_pmd(port
->netdev
)) {
3989 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
3990 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
3992 if (pinned
&& q
->core_id
!= OVS_CORE_UNSPEC
) {
3993 struct dp_netdev_pmd_thread
*pmd
;
3995 pmd
= dp_netdev_get_pmd(dp
, q
->core_id
);
3997 VLOG_WARN("There is no PMD thread on core %d. Queue "
3998 "%d on port \'%s\' will not be polled.",
3999 q
->core_id
, qid
, netdev_get_name(port
->netdev
));
4002 pmd
->isolated
= true;
4003 dp_netdev_pmd_unref(pmd
);
4005 } else if (!pinned
&& q
->core_id
== OVS_CORE_UNSPEC
) {
4006 uint64_t cycle_hist
= 0;
4009 rxqs
= xmalloc(sizeof *rxqs
);
4011 rxqs
= xrealloc(rxqs
, sizeof *rxqs
* (n_rxqs
+ 1));
4013 /* Sum the queue intervals and store the cycle history. */
4014 for (unsigned i
= 0; i
< PMD_RXQ_INTERVAL_MAX
; i
++) {
4015 cycle_hist
+= dp_netdev_rxq_get_intrvl_cycles(q
, i
);
4017 dp_netdev_rxq_set_cycles(q
, RXQ_CYCLES_PROC_HIST
, cycle_hist
);
4019 /* Store the queue. */
4026 /* Sort the queues in order of the processing cycles
4027 * they consumed during their last pmd interval. */
4028 qsort(rxqs
, n_rxqs
, sizeof *rxqs
, compare_rxq_cycles
);
4031 rr_numa_list_populate(dp
, &rr
);
4032 /* Assign the sorted queues to pmds in round robin. */
4033 for (int i
= 0; i
< n_rxqs
; i
++) {
4034 numa_id
= netdev_get_numa_id(rxqs
[i
]->port
->netdev
);
4035 numa
= rr_numa_list_lookup(&rr
, numa_id
);
4037 /* There are no pmds on the queue's local NUMA node.
4038 Round robin on the NUMA nodes that do have pmds. */
4039 non_local_numa
= rr_numa_list_next(&rr
, non_local_numa
);
4040 if (!non_local_numa
) {
4041 VLOG_ERR("There is no available (non-isolated) pmd "
4042 "thread for port \'%s\' queue %d. This queue "
4043 "will not be polled. Is pmd-cpu-mask set to "
4044 "zero? Or are all PMDs isolated to other "
4045 "queues?", netdev_rxq_get_name(rxqs
[i
]->rx
),
4046 netdev_rxq_get_queue_id(rxqs
[i
]->rx
));
4049 rxqs
[i
]->pmd
= rr_numa_get_pmd(non_local_numa
);
4050 VLOG_WARN("There's no available (non-isolated) pmd thread "
4051 "on numa node %d. Queue %d on port \'%s\' will "
4052 "be assigned to the pmd on core %d "
4053 "(numa node %d). Expect reduced performance.",
4054 numa_id
, netdev_rxq_get_queue_id(rxqs
[i
]->rx
),
4055 netdev_rxq_get_name(rxqs
[i
]->rx
),
4056 rxqs
[i
]->pmd
->core_id
, rxqs
[i
]->pmd
->numa_id
);
4058 rxqs
[i
]->pmd
= rr_numa_get_pmd(numa
);
4059 VLOG_INFO("Core %d on numa node %d assigned port \'%s\' "
4060 "rx queue %d (measured processing cycles %"PRIu64
").",
4061 rxqs
[i
]->pmd
->core_id
, numa_id
,
4062 netdev_rxq_get_name(rxqs
[i
]->rx
),
4063 netdev_rxq_get_queue_id(rxqs
[i
]->rx
),
4064 dp_netdev_rxq_get_cycles(rxqs
[i
], RXQ_CYCLES_PROC_HIST
));
4068 rr_numa_list_destroy(&rr
);
4073 reload_affected_pmds(struct dp_netdev
*dp
)
4075 struct dp_netdev_pmd_thread
*pmd
;
4077 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4078 if (pmd
->need_reload
) {
4079 flow_mark_flush(pmd
);
4080 dp_netdev_reload_pmd__(pmd
);
4081 pmd
->need_reload
= false;
4087 reconfigure_pmd_threads(struct dp_netdev
*dp
)
4088 OVS_REQUIRES(dp
->port_mutex
)
4090 struct dp_netdev_pmd_thread
*pmd
;
4091 struct ovs_numa_dump
*pmd_cores
;
4092 struct ovs_numa_info_core
*core
;
4093 struct hmapx to_delete
= HMAPX_INITIALIZER(&to_delete
);
4094 struct hmapx_node
*node
;
4095 bool changed
= false;
4096 bool need_to_adjust_static_tx_qids
= false;
4098 /* The pmd threads should be started only if there's a pmd port in the
4099 * datapath. If the user didn't provide any "pmd-cpu-mask", we start
4100 * NR_PMD_THREADS per numa node. */
4101 if (!has_pmd_port(dp
)) {
4102 pmd_cores
= ovs_numa_dump_n_cores_per_numa(0);
4103 } else if (dp
->pmd_cmask
&& dp
->pmd_cmask
[0]) {
4104 pmd_cores
= ovs_numa_dump_cores_with_cmask(dp
->pmd_cmask
);
4106 pmd_cores
= ovs_numa_dump_n_cores_per_numa(NR_PMD_THREADS
);
4109 /* We need to adjust 'static_tx_qid's only if we're reducing number of
4110 * PMD threads. Otherwise, new threads will allocate all the freed ids. */
4111 if (ovs_numa_dump_count(pmd_cores
) < cmap_count(&dp
->poll_threads
) - 1) {
4112 /* Adjustment is required to keep 'static_tx_qid's sequential and
4113 * avoid possible issues, for example, imbalanced tx queue usage
4114 * and unnecessary locking caused by remapping on netdev level. */
4115 need_to_adjust_static_tx_qids
= true;
4118 /* Check for unwanted pmd threads */
4119 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4120 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
4123 if (!ovs_numa_dump_contains_core(pmd_cores
, pmd
->numa_id
,
4125 hmapx_add(&to_delete
, pmd
);
4126 } else if (need_to_adjust_static_tx_qids
) {
4127 pmd
->need_reload
= true;
4131 HMAPX_FOR_EACH (node
, &to_delete
) {
4132 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
4133 VLOG_INFO("PMD thread on numa_id: %d, core id: %2d destroyed.",
4134 pmd
->numa_id
, pmd
->core_id
);
4135 dp_netdev_del_pmd(dp
, pmd
);
4137 changed
= !hmapx_is_empty(&to_delete
);
4138 hmapx_destroy(&to_delete
);
4140 if (need_to_adjust_static_tx_qids
) {
4141 /* 'static_tx_qid's are not sequential now.
4142 * Reload remaining threads to fix this. */
4143 reload_affected_pmds(dp
);
4146 /* Check for required new pmd threads */
4147 FOR_EACH_CORE_ON_DUMP(core
, pmd_cores
) {
4148 pmd
= dp_netdev_get_pmd(dp
, core
->core_id
);
4150 pmd
= xzalloc(sizeof *pmd
);
4151 dp_netdev_configure_pmd(pmd
, dp
, core
->core_id
, core
->numa_id
);
4152 pmd
->thread
= ovs_thread_create("pmd", pmd_thread_main
, pmd
);
4153 VLOG_INFO("PMD thread on numa_id: %d, core id: %2d created.",
4154 pmd
->numa_id
, pmd
->core_id
);
4157 dp_netdev_pmd_unref(pmd
);
4162 struct ovs_numa_info_numa
*numa
;
4164 /* Log the number of pmd threads per numa node. */
4165 FOR_EACH_NUMA_ON_DUMP (numa
, pmd_cores
) {
4166 VLOG_INFO("There are %"PRIuSIZE
" pmd threads on numa node %d",
4167 numa
->n_cores
, numa
->numa_id
);
4171 ovs_numa_dump_destroy(pmd_cores
);
4175 pmd_remove_stale_ports(struct dp_netdev
*dp
,
4176 struct dp_netdev_pmd_thread
*pmd
)
4177 OVS_EXCLUDED(pmd
->port_mutex
)
4178 OVS_REQUIRES(dp
->port_mutex
)
4180 struct rxq_poll
*poll
, *poll_next
;
4181 struct tx_port
*tx
, *tx_next
;
4183 ovs_mutex_lock(&pmd
->port_mutex
);
4184 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
4185 struct dp_netdev_port
*port
= poll
->rxq
->port
;
4187 if (port
->need_reconfigure
4188 || !hmap_contains(&dp
->ports
, &port
->node
)) {
4189 dp_netdev_del_rxq_from_pmd(pmd
, poll
);
4192 HMAP_FOR_EACH_SAFE (tx
, tx_next
, node
, &pmd
->tx_ports
) {
4193 struct dp_netdev_port
*port
= tx
->port
;
4195 if (port
->need_reconfigure
4196 || !hmap_contains(&dp
->ports
, &port
->node
)) {
4197 dp_netdev_del_port_tx_from_pmd(pmd
, tx
);
4200 ovs_mutex_unlock(&pmd
->port_mutex
);
4203 /* Must be called each time a port is added/removed or the cmask changes.
4204 * This creates and destroys pmd threads, reconfigures ports, opens their
4205 * rxqs and assigns all rxqs/txqs to pmd threads. */
4207 reconfigure_datapath(struct dp_netdev
*dp
)
4208 OVS_REQUIRES(dp
->port_mutex
)
4210 struct dp_netdev_pmd_thread
*pmd
;
4211 struct dp_netdev_port
*port
;
4214 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
4216 /* Step 1: Adjust the pmd threads based on the datapath ports, the cores
4217 * on the system and the user configuration. */
4218 reconfigure_pmd_threads(dp
);
4220 wanted_txqs
= cmap_count(&dp
->poll_threads
);
4222 /* The number of pmd threads might have changed, or a port can be new:
4223 * adjust the txqs. */
4224 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4225 netdev_set_tx_multiq(port
->netdev
, wanted_txqs
);
4228 /* Step 2: Remove from the pmd threads ports that have been removed or
4229 * need reconfiguration. */
4231 /* Check for all the ports that need reconfiguration. We cache this in
4232 * 'port->need_reconfigure', because netdev_is_reconf_required() can
4233 * change at any time. */
4234 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4235 if (netdev_is_reconf_required(port
->netdev
)) {
4236 port
->need_reconfigure
= true;
4240 /* Remove from the pmd threads all the ports that have been deleted or
4241 * need reconfiguration. */
4242 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4243 pmd_remove_stale_ports(dp
, pmd
);
4246 /* Reload affected pmd threads. We must wait for the pmd threads before
4247 * reconfiguring the ports, because a port cannot be reconfigured while
4248 * it's being used. */
4249 reload_affected_pmds(dp
);
4251 /* Step 3: Reconfigure ports. */
4253 /* We only reconfigure the ports that we determined above, because they're
4254 * not being used by any pmd thread at the moment. If a port fails to
4255 * reconfigure we remove it from the datapath. */
4256 struct dp_netdev_port
*next_port
;
4257 HMAP_FOR_EACH_SAFE (port
, next_port
, node
, &dp
->ports
) {
4260 if (!port
->need_reconfigure
) {
4264 err
= port_reconfigure(port
);
4266 hmap_remove(&dp
->ports
, &port
->node
);
4267 seq_change(dp
->port_seq
);
4270 port
->dynamic_txqs
= netdev_n_txq(port
->netdev
) < wanted_txqs
;
4274 /* Step 4: Compute new rxq scheduling. We don't touch the pmd threads
4275 * for now, we just update the 'pmd' pointer in each rxq to point to the
4276 * wanted thread according to the scheduling policy. */
4278 /* Reset all the pmd threads to non isolated. */
4279 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4280 pmd
->isolated
= false;
4283 /* Reset all the queues to unassigned */
4284 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4285 for (int i
= 0; i
< port
->n_rxq
; i
++) {
4286 port
->rxqs
[i
].pmd
= NULL
;
4290 /* Add pinned queues and mark pmd threads isolated. */
4291 rxq_scheduling(dp
, true);
4293 /* Add non-pinned queues. */
4294 rxq_scheduling(dp
, false);
4296 /* Step 5: Remove queues not compliant with new scheduling. */
4297 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4298 struct rxq_poll
*poll
, *poll_next
;
4300 ovs_mutex_lock(&pmd
->port_mutex
);
4301 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
4302 if (poll
->rxq
->pmd
!= pmd
) {
4303 dp_netdev_del_rxq_from_pmd(pmd
, poll
);
4306 ovs_mutex_unlock(&pmd
->port_mutex
);
4309 /* Reload affected pmd threads. We must wait for the pmd threads to remove
4310 * the old queues before readding them, otherwise a queue can be polled by
4311 * two threads at the same time. */
4312 reload_affected_pmds(dp
);
4314 /* Step 6: Add queues from scheduling, if they're not there already. */
4315 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4316 if (!netdev_is_pmd(port
->netdev
)) {
4320 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
4321 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
4324 ovs_mutex_lock(&q
->pmd
->port_mutex
);
4325 dp_netdev_add_rxq_to_pmd(q
->pmd
, q
);
4326 ovs_mutex_unlock(&q
->pmd
->port_mutex
);
4331 /* Add every port to the tx cache of every pmd thread, if it's not
4332 * there already and if this pmd has at least one rxq to poll. */
4333 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4334 ovs_mutex_lock(&pmd
->port_mutex
);
4335 if (hmap_count(&pmd
->poll_list
) || pmd
->core_id
== NON_PMD_CORE_ID
) {
4336 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4337 dp_netdev_add_port_tx_to_pmd(pmd
, port
);
4340 ovs_mutex_unlock(&pmd
->port_mutex
);
4343 /* Reload affected pmd threads. */
4344 reload_affected_pmds(dp
);
4347 /* Returns true if one of the netdevs in 'dp' requires a reconfiguration */
4349 ports_require_restart(const struct dp_netdev
*dp
)
4350 OVS_REQUIRES(dp
->port_mutex
)
4352 struct dp_netdev_port
*port
;
4354 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4355 if (netdev_is_reconf_required(port
->netdev
)) {
4363 /* Return true if needs to revalidate datapath flows. */
4365 dpif_netdev_run(struct dpif
*dpif
)
4367 struct dp_netdev_port
*port
;
4368 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4369 struct dp_netdev_pmd_thread
*non_pmd
;
4370 uint64_t new_tnl_seq
;
4371 bool need_to_flush
= true;
4373 ovs_mutex_lock(&dp
->port_mutex
);
4374 non_pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
4376 ovs_mutex_lock(&dp
->non_pmd_mutex
);
4377 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4378 if (!netdev_is_pmd(port
->netdev
)) {
4381 for (i
= 0; i
< port
->n_rxq
; i
++) {
4382 if (dp_netdev_process_rxq_port(non_pmd
,
4385 need_to_flush
= false;
4390 if (need_to_flush
) {
4391 /* We didn't receive anything in the process loop.
4392 * Check if we need to send something.
4393 * There was no time updates on current iteration. */
4394 pmd_thread_ctx_time_update(non_pmd
);
4395 dp_netdev_pmd_flush_output_packets(non_pmd
, false);
4398 dpif_netdev_xps_revalidate_pmd(non_pmd
, false);
4399 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
4401 dp_netdev_pmd_unref(non_pmd
);
4404 if (dp_netdev_is_reconf_required(dp
) || ports_require_restart(dp
)) {
4405 reconfigure_datapath(dp
);
4407 ovs_mutex_unlock(&dp
->port_mutex
);
4409 tnl_neigh_cache_run();
4411 new_tnl_seq
= seq_read(tnl_conf_seq
);
4413 if (dp
->last_tnl_conf_seq
!= new_tnl_seq
) {
4414 dp
->last_tnl_conf_seq
= new_tnl_seq
;
4421 dpif_netdev_wait(struct dpif
*dpif
)
4423 struct dp_netdev_port
*port
;
4424 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4426 ovs_mutex_lock(&dp_netdev_mutex
);
4427 ovs_mutex_lock(&dp
->port_mutex
);
4428 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
4429 netdev_wait_reconf_required(port
->netdev
);
4430 if (!netdev_is_pmd(port
->netdev
)) {
4433 for (i
= 0; i
< port
->n_rxq
; i
++) {
4434 netdev_rxq_wait(port
->rxqs
[i
].rx
);
4438 ovs_mutex_unlock(&dp
->port_mutex
);
4439 ovs_mutex_unlock(&dp_netdev_mutex
);
4440 seq_wait(tnl_conf_seq
, dp
->last_tnl_conf_seq
);
4444 pmd_free_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
4446 struct tx_port
*tx_port_cached
;
4448 /* Flush all the queued packets. */
4449 dp_netdev_pmd_flush_output_packets(pmd
, true);
4450 /* Free all used tx queue ids. */
4451 dpif_netdev_xps_revalidate_pmd(pmd
, true);
4453 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->tnl_port_cache
) {
4454 free(tx_port_cached
);
4456 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->send_port_cache
) {
4457 free(tx_port_cached
);
4461 /* Copies ports from 'pmd->tx_ports' (shared with the main thread) to
4462 * thread-local copies. Copy to 'pmd->tnl_port_cache' if it is a tunnel
4463 * device, otherwise to 'pmd->send_port_cache' if the port has at least
4466 pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
4467 OVS_REQUIRES(pmd
->port_mutex
)
4469 struct tx_port
*tx_port
, *tx_port_cached
;
4471 pmd_free_cached_ports(pmd
);
4472 hmap_shrink(&pmd
->send_port_cache
);
4473 hmap_shrink(&pmd
->tnl_port_cache
);
4475 HMAP_FOR_EACH (tx_port
, node
, &pmd
->tx_ports
) {
4476 if (netdev_has_tunnel_push_pop(tx_port
->port
->netdev
)) {
4477 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
4478 hmap_insert(&pmd
->tnl_port_cache
, &tx_port_cached
->node
,
4479 hash_port_no(tx_port_cached
->port
->port_no
));
4482 if (netdev_n_txq(tx_port
->port
->netdev
)) {
4483 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
4484 hmap_insert(&pmd
->send_port_cache
, &tx_port_cached
->node
,
4485 hash_port_no(tx_port_cached
->port
->port_no
));
4491 pmd_alloc_static_tx_qid(struct dp_netdev_pmd_thread
*pmd
)
4493 ovs_mutex_lock(&pmd
->dp
->tx_qid_pool_mutex
);
4494 if (!id_pool_alloc_id(pmd
->dp
->tx_qid_pool
, &pmd
->static_tx_qid
)) {
4495 VLOG_ABORT("static_tx_qid allocation failed for PMD on core %2d"
4496 ", numa_id %d.", pmd
->core_id
, pmd
->numa_id
);
4498 ovs_mutex_unlock(&pmd
->dp
->tx_qid_pool_mutex
);
4500 VLOG_DBG("static_tx_qid = %d allocated for PMD thread on core %2d"
4501 ", numa_id %d.", pmd
->static_tx_qid
, pmd
->core_id
, pmd
->numa_id
);
4505 pmd_free_static_tx_qid(struct dp_netdev_pmd_thread
*pmd
)
4507 ovs_mutex_lock(&pmd
->dp
->tx_qid_pool_mutex
);
4508 id_pool_free_id(pmd
->dp
->tx_qid_pool
, pmd
->static_tx_qid
);
4509 ovs_mutex_unlock(&pmd
->dp
->tx_qid_pool_mutex
);
4513 pmd_load_queues_and_ports(struct dp_netdev_pmd_thread
*pmd
,
4514 struct polled_queue
**ppoll_list
)
4516 struct polled_queue
*poll_list
= *ppoll_list
;
4517 struct rxq_poll
*poll
;
4520 ovs_mutex_lock(&pmd
->port_mutex
);
4521 poll_list
= xrealloc(poll_list
, hmap_count(&pmd
->poll_list
)
4522 * sizeof *poll_list
);
4525 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
4526 poll_list
[i
].rxq
= poll
->rxq
;
4527 poll_list
[i
].port_no
= poll
->rxq
->port
->port_no
;
4531 pmd_load_cached_ports(pmd
);
4533 ovs_mutex_unlock(&pmd
->port_mutex
);
4535 *ppoll_list
= poll_list
;
4540 pmd_thread_main(void *f_
)
4542 struct dp_netdev_pmd_thread
*pmd
= f_
;
4543 struct pmd_perf_stats
*s
= &pmd
->perf_stats
;
4544 unsigned int lc
= 0;
4545 struct polled_queue
*poll_list
;
4549 int process_packets
= 0;
4553 /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */
4554 ovsthread_setspecific(pmd
->dp
->per_pmd_key
, pmd
);
4555 ovs_numa_thread_setaffinity_core(pmd
->core_id
);
4556 dpdk_set_lcore_id(pmd
->core_id
);
4557 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
4558 emc_cache_init(&pmd
->flow_cache
);
4560 pmd_alloc_static_tx_qid(pmd
);
4562 /* List port/core affinity */
4563 for (i
= 0; i
< poll_cnt
; i
++) {
4564 VLOG_DBG("Core %d processing port \'%s\' with queue-id %d\n",
4565 pmd
->core_id
, netdev_rxq_get_name(poll_list
[i
].rxq
->rx
),
4566 netdev_rxq_get_queue_id(poll_list
[i
].rxq
->rx
));
4567 /* Reset the rxq current cycles counter. */
4568 dp_netdev_rxq_set_cycles(poll_list
[i
].rxq
, RXQ_CYCLES_PROC_CURR
, 0);
4572 while (seq_read(pmd
->reload_seq
) == pmd
->last_reload_seq
) {
4573 seq_wait(pmd
->reload_seq
, pmd
->last_reload_seq
);
4579 pmd
->intrvl_tsc_prev
= 0;
4580 atomic_store_relaxed(&pmd
->intrvl_cycles
, 0);
4581 cycles_counter_update(s
);
4582 /* Protect pmd stats from external clearing while polling. */
4583 ovs_mutex_lock(&pmd
->perf_stats
.stats_mutex
);
4585 uint64_t rx_packets
= 0, tx_packets
= 0;
4587 pmd_perf_start_iteration(s
);
4589 for (i
= 0; i
< poll_cnt
; i
++) {
4591 dp_netdev_process_rxq_port(pmd
, poll_list
[i
].rxq
,
4592 poll_list
[i
].port_no
);
4593 rx_packets
+= process_packets
;
4597 /* We didn't receive anything in the process loop.
4598 * Check if we need to send something.
4599 * There was no time updates on current iteration. */
4600 pmd_thread_ctx_time_update(pmd
);
4601 tx_packets
= dp_netdev_pmd_flush_output_packets(pmd
, false);
4609 coverage_try_clear();
4610 dp_netdev_pmd_try_optimize(pmd
, poll_list
, poll_cnt
);
4611 if (!ovsrcu_try_quiesce()) {
4612 emc_cache_slow_sweep(&pmd
->flow_cache
);
4615 atomic_read_relaxed(&pmd
->reload
, &reload
);
4620 pmd_perf_end_iteration(s
, rx_packets
, tx_packets
,
4621 pmd_perf_metrics_enabled(pmd
));
4623 ovs_mutex_unlock(&pmd
->perf_stats
.stats_mutex
);
4625 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
4626 exiting
= latch_is_set(&pmd
->exit_latch
);
4627 /* Signal here to make sure the pmd finishes
4628 * reloading the updated configuration. */
4629 dp_netdev_pmd_reload_done(pmd
);
4631 pmd_free_static_tx_qid(pmd
);
4637 emc_cache_uninit(&pmd
->flow_cache
);
4639 pmd_free_cached_ports(pmd
);
4644 dp_netdev_disable_upcall(struct dp_netdev
*dp
)
4645 OVS_ACQUIRES(dp
->upcall_rwlock
)
4647 fat_rwlock_wrlock(&dp
->upcall_rwlock
);
4653 dpif_netdev_meter_get_features(const struct dpif
* dpif OVS_UNUSED
,
4654 struct ofputil_meter_features
*features
)
4656 features
->max_meters
= MAX_METERS
;
4657 features
->band_types
= DP_SUPPORTED_METER_BAND_TYPES
;
4658 features
->capabilities
= DP_SUPPORTED_METER_FLAGS_MASK
;
4659 features
->max_bands
= MAX_BANDS
;
4660 features
->max_color
= 0;
4663 /* Returns false when packet needs to be dropped. */
4665 dp_netdev_run_meter(struct dp_netdev
*dp
, struct dp_packet_batch
*packets_
,
4666 uint32_t meter_id
, long long int now
)
4668 struct dp_meter
*meter
;
4669 struct dp_meter_band
*band
;
4670 struct dp_packet
*packet
;
4671 long long int long_delta_t
; /* msec */
4672 uint32_t delta_t
; /* msec */
4673 const size_t cnt
= dp_packet_batch_size(packets_
);
4674 uint32_t bytes
, volume
;
4675 int exceeded_band
[NETDEV_MAX_BURST
];
4676 uint32_t exceeded_rate
[NETDEV_MAX_BURST
];
4677 int exceeded_pkt
= cnt
; /* First packet that exceeded a band rate. */
4679 if (meter_id
>= MAX_METERS
) {
4683 meter_lock(dp
, meter_id
);
4684 meter
= dp
->meters
[meter_id
];
4689 /* Initialize as negative values. */
4690 memset(exceeded_band
, 0xff, cnt
* sizeof *exceeded_band
);
4691 /* Initialize as zeroes. */
4692 memset(exceeded_rate
, 0, cnt
* sizeof *exceeded_rate
);
4694 /* All packets will hit the meter at the same time. */
4695 long_delta_t
= (now
- meter
->used
) / 1000; /* msec */
4697 /* Make sure delta_t will not be too large, so that bucket will not
4698 * wrap around below. */
4699 delta_t
= (long_delta_t
> (long long int)meter
->max_delta_t
)
4700 ? meter
->max_delta_t
: (uint32_t)long_delta_t
;
4702 /* Update meter stats. */
4704 meter
->packet_count
+= cnt
;
4706 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
4707 bytes
+= dp_packet_size(packet
);
4709 meter
->byte_count
+= bytes
;
4711 /* Meters can operate in terms of packets per second or kilobits per
4713 if (meter
->flags
& OFPMF13_PKTPS
) {
4714 /* Rate in packets/second, bucket 1/1000 packets. */
4715 /* msec * packets/sec = 1/1000 packets. */
4716 volume
= cnt
* 1000; /* Take 'cnt' packets from the bucket. */
4718 /* Rate in kbps, bucket in bits. */
4719 /* msec * kbps = bits */
4723 /* Update all bands and find the one hit with the highest rate for each
4724 * packet (if any). */
4725 for (int m
= 0; m
< meter
->n_bands
; ++m
) {
4726 band
= &meter
->bands
[m
];
4728 /* Update band's bucket. */
4729 band
->bucket
+= delta_t
* band
->up
.rate
;
4730 if (band
->bucket
> band
->up
.burst_size
) {
4731 band
->bucket
= band
->up
.burst_size
;
4734 /* Drain the bucket for all the packets, if possible. */
4735 if (band
->bucket
>= volume
) {
4736 band
->bucket
-= volume
;
4738 int band_exceeded_pkt
;
4740 /* Band limit hit, must process packet-by-packet. */
4741 if (meter
->flags
& OFPMF13_PKTPS
) {
4742 band_exceeded_pkt
= band
->bucket
/ 1000;
4743 band
->bucket
%= 1000; /* Remainder stays in bucket. */
4745 /* Update the exceeding band for each exceeding packet.
4746 * (Only one band will be fired by a packet, and that
4747 * can be different for each packet.) */
4748 for (int i
= band_exceeded_pkt
; i
< cnt
; i
++) {
4749 if (band
->up
.rate
> exceeded_rate
[i
]) {
4750 exceeded_rate
[i
] = band
->up
.rate
;
4751 exceeded_band
[i
] = m
;
4755 /* Packet sizes differ, must process one-by-one. */
4756 band_exceeded_pkt
= cnt
;
4757 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
4758 uint32_t bits
= dp_packet_size(packet
) * 8;
4760 if (band
->bucket
>= bits
) {
4761 band
->bucket
-= bits
;
4763 if (i
< band_exceeded_pkt
) {
4764 band_exceeded_pkt
= i
;
4766 /* Update the exceeding band for the exceeding packet.
4767 * (Only one band will be fired by a packet, and that
4768 * can be different for each packet.) */
4769 if (band
->up
.rate
> exceeded_rate
[i
]) {
4770 exceeded_rate
[i
] = band
->up
.rate
;
4771 exceeded_band
[i
] = m
;
4776 /* Remember the first exceeding packet. */
4777 if (exceeded_pkt
> band_exceeded_pkt
) {
4778 exceeded_pkt
= band_exceeded_pkt
;
4783 /* Fire the highest rate band exceeded by each packet.
4784 * Drop packets if needed, by swapping packet to the end that will be
4787 DP_PACKET_BATCH_REFILL_FOR_EACH (j
, cnt
, packet
, packets_
) {
4788 if (exceeded_band
[j
] >= 0) {
4789 /* Meter drop packet. */
4790 band
= &meter
->bands
[exceeded_band
[j
]];
4791 band
->packet_count
+= 1;
4792 band
->byte_count
+= dp_packet_size(packet
);
4794 dp_packet_delete(packet
);
4796 /* Meter accepts packet. */
4797 dp_packet_batch_refill(packets_
, packet
, j
);
4801 meter_unlock(dp
, meter_id
);
4804 /* Meter set/get/del processing is still single-threaded. */
4806 dpif_netdev_meter_set(struct dpif
*dpif
, ofproto_meter_id
*meter_id
,
4807 struct ofputil_meter_config
*config
)
4809 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4810 uint32_t mid
= meter_id
->uint32
;
4811 struct dp_meter
*meter
;
4814 if (mid
>= MAX_METERS
) {
4815 return EFBIG
; /* Meter_id out of range. */
4818 if (config
->flags
& ~DP_SUPPORTED_METER_FLAGS_MASK
||
4819 !(config
->flags
& (OFPMF13_KBPS
| OFPMF13_PKTPS
))) {
4820 return EBADF
; /* Unsupported flags set */
4823 /* Validate bands */
4824 if (config
->n_bands
== 0 || config
->n_bands
> MAX_BANDS
) {
4825 return EINVAL
; /* Too many bands */
4828 /* Validate rates */
4829 for (i
= 0; i
< config
->n_bands
; i
++) {
4830 if (config
->bands
[i
].rate
== 0) {
4831 return EDOM
; /* rate must be non-zero */
4835 for (i
= 0; i
< config
->n_bands
; ++i
) {
4836 switch (config
->bands
[i
].type
) {
4840 return ENODEV
; /* Unsupported band type */
4844 /* Allocate meter */
4845 meter
= xzalloc(sizeof *meter
4846 + config
->n_bands
* sizeof(struct dp_meter_band
));
4848 meter
->flags
= config
->flags
;
4849 meter
->n_bands
= config
->n_bands
;
4850 meter
->max_delta_t
= 0;
4851 meter
->used
= time_usec();
4854 for (i
= 0; i
< config
->n_bands
; ++i
) {
4855 uint32_t band_max_delta_t
;
4857 /* Set burst size to a workable value if none specified. */
4858 if (config
->bands
[i
].burst_size
== 0) {
4859 config
->bands
[i
].burst_size
= config
->bands
[i
].rate
;
4862 meter
->bands
[i
].up
= config
->bands
[i
];
4863 /* Convert burst size to the bucket units: */
4864 /* pkts => 1/1000 packets, kilobits => bits. */
4865 meter
->bands
[i
].up
.burst_size
*= 1000;
4866 /* Initialize bucket to empty. */
4867 meter
->bands
[i
].bucket
= 0;
4869 /* Figure out max delta_t that is enough to fill any bucket. */
4871 = meter
->bands
[i
].up
.burst_size
/ meter
->bands
[i
].up
.rate
;
4872 if (band_max_delta_t
> meter
->max_delta_t
) {
4873 meter
->max_delta_t
= band_max_delta_t
;
4877 meter_lock(dp
, mid
);
4878 dp_delete_meter(dp
, mid
); /* Free existing meter, if any */
4879 dp
->meters
[mid
] = meter
;
4880 meter_unlock(dp
, mid
);
4888 dpif_netdev_meter_get(const struct dpif
*dpif
,
4889 ofproto_meter_id meter_id_
,
4890 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
4892 const struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4893 const struct dp_meter
*meter
;
4894 uint32_t meter_id
= meter_id_
.uint32
;
4896 if (meter_id
>= MAX_METERS
) {
4899 meter
= dp
->meters
[meter_id
];
4906 meter_lock(dp
, meter_id
);
4907 stats
->packet_in_count
= meter
->packet_count
;
4908 stats
->byte_in_count
= meter
->byte_count
;
4910 for (i
= 0; i
< n_bands
&& i
< meter
->n_bands
; ++i
) {
4911 stats
->bands
[i
].packet_count
= meter
->bands
[i
].packet_count
;
4912 stats
->bands
[i
].byte_count
= meter
->bands
[i
].byte_count
;
4914 meter_unlock(dp
, meter_id
);
4922 dpif_netdev_meter_del(struct dpif
*dpif
,
4923 ofproto_meter_id meter_id_
,
4924 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
4926 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4929 error
= dpif_netdev_meter_get(dpif
, meter_id_
, stats
, n_bands
);
4931 uint32_t meter_id
= meter_id_
.uint32
;
4933 meter_lock(dp
, meter_id
);
4934 dp_delete_meter(dp
, meter_id
);
4935 meter_unlock(dp
, meter_id
);
4942 dpif_netdev_disable_upcall(struct dpif
*dpif
)
4943 OVS_NO_THREAD_SAFETY_ANALYSIS
4945 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4946 dp_netdev_disable_upcall(dp
);
4950 dp_netdev_enable_upcall(struct dp_netdev
*dp
)
4951 OVS_RELEASES(dp
->upcall_rwlock
)
4953 fat_rwlock_unlock(&dp
->upcall_rwlock
);
4957 dpif_netdev_enable_upcall(struct dpif
*dpif
)
4958 OVS_NO_THREAD_SAFETY_ANALYSIS
4960 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4961 dp_netdev_enable_upcall(dp
);
4965 dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
)
4967 ovs_mutex_lock(&pmd
->cond_mutex
);
4968 atomic_store_relaxed(&pmd
->reload
, false);
4969 pmd
->last_reload_seq
= seq_read(pmd
->reload_seq
);
4970 xpthread_cond_signal(&pmd
->cond
);
4971 ovs_mutex_unlock(&pmd
->cond_mutex
);
4974 /* Finds and refs the dp_netdev_pmd_thread on core 'core_id'. Returns
4975 * the pointer if succeeds, otherwise, NULL (it can return NULL even if
4976 * 'core_id' is NON_PMD_CORE_ID).
4978 * Caller must unrefs the returned reference. */
4979 static struct dp_netdev_pmd_thread
*
4980 dp_netdev_get_pmd(struct dp_netdev
*dp
, unsigned core_id
)
4982 struct dp_netdev_pmd_thread
*pmd
;
4983 const struct cmap_node
*pnode
;
4985 pnode
= cmap_find(&dp
->poll_threads
, hash_int(core_id
, 0));
4989 pmd
= CONTAINER_OF(pnode
, struct dp_netdev_pmd_thread
, node
);
4991 return dp_netdev_pmd_try_ref(pmd
) ? pmd
: NULL
;
4994 /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */
4996 dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
4997 OVS_REQUIRES(dp
->port_mutex
)
4999 struct dp_netdev_pmd_thread
*non_pmd
;
5001 non_pmd
= xzalloc(sizeof *non_pmd
);
5002 dp_netdev_configure_pmd(non_pmd
, dp
, NON_PMD_CORE_ID
, OVS_NUMA_UNSPEC
);
5005 /* Caller must have valid pointer to 'pmd'. */
5007 dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
)
5009 return ovs_refcount_try_ref_rcu(&pmd
->ref_cnt
);
5013 dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
)
5015 if (pmd
&& ovs_refcount_unref(&pmd
->ref_cnt
) == 1) {
5016 ovsrcu_postpone(dp_netdev_destroy_pmd
, pmd
);
5020 /* Given cmap position 'pos', tries to ref the next node. If try_ref()
5021 * fails, keeps checking for next node until reaching the end of cmap.
5023 * Caller must unrefs the returned reference. */
5024 static struct dp_netdev_pmd_thread
*
5025 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
)
5027 struct dp_netdev_pmd_thread
*next
;
5030 struct cmap_node
*node
;
5032 node
= cmap_next_position(&dp
->poll_threads
, pos
);
5033 next
= node
? CONTAINER_OF(node
, struct dp_netdev_pmd_thread
, node
)
5035 } while (next
&& !dp_netdev_pmd_try_ref(next
));
5040 /* Configures the 'pmd' based on the input argument. */
5042 dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
, struct dp_netdev
*dp
,
5043 unsigned core_id
, int numa_id
)
5046 pmd
->core_id
= core_id
;
5047 pmd
->numa_id
= numa_id
;
5048 pmd
->need_reload
= false;
5049 pmd
->n_output_batches
= 0;
5051 ovs_refcount_init(&pmd
->ref_cnt
);
5052 latch_init(&pmd
->exit_latch
);
5053 pmd
->reload_seq
= seq_create();
5054 pmd
->last_reload_seq
= seq_read(pmd
->reload_seq
);
5055 atomic_init(&pmd
->reload
, false);
5056 xpthread_cond_init(&pmd
->cond
, NULL
);
5057 ovs_mutex_init(&pmd
->cond_mutex
);
5058 ovs_mutex_init(&pmd
->flow_mutex
);
5059 ovs_mutex_init(&pmd
->port_mutex
);
5060 cmap_init(&pmd
->flow_table
);
5061 cmap_init(&pmd
->classifiers
);
5062 pmd
->ctx
.last_rxq
= NULL
;
5063 pmd_thread_ctx_time_update(pmd
);
5064 pmd
->next_optimization
= pmd
->ctx
.now
+ DPCLS_OPTIMIZATION_INTERVAL
;
5065 pmd
->rxq_next_cycle_store
= pmd
->ctx
.now
+ PMD_RXQ_INTERVAL_LEN
;
5066 hmap_init(&pmd
->poll_list
);
5067 hmap_init(&pmd
->tx_ports
);
5068 hmap_init(&pmd
->tnl_port_cache
);
5069 hmap_init(&pmd
->send_port_cache
);
5070 /* init the 'flow_cache' since there is no
5071 * actual thread created for NON_PMD_CORE_ID. */
5072 if (core_id
== NON_PMD_CORE_ID
) {
5073 emc_cache_init(&pmd
->flow_cache
);
5074 pmd_alloc_static_tx_qid(pmd
);
5076 pmd_perf_stats_init(&pmd
->perf_stats
);
5077 cmap_insert(&dp
->poll_threads
, CONST_CAST(struct cmap_node
*, &pmd
->node
),
5078 hash_int(core_id
, 0));
5082 dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
)
5086 dp_netdev_pmd_flow_flush(pmd
);
5087 hmap_destroy(&pmd
->send_port_cache
);
5088 hmap_destroy(&pmd
->tnl_port_cache
);
5089 hmap_destroy(&pmd
->tx_ports
);
5090 hmap_destroy(&pmd
->poll_list
);
5091 /* All flows (including their dpcls_rules) have been deleted already */
5092 CMAP_FOR_EACH (cls
, node
, &pmd
->classifiers
) {
5094 ovsrcu_postpone(free
, cls
);
5096 cmap_destroy(&pmd
->classifiers
);
5097 cmap_destroy(&pmd
->flow_table
);
5098 ovs_mutex_destroy(&pmd
->flow_mutex
);
5099 latch_destroy(&pmd
->exit_latch
);
5100 seq_destroy(pmd
->reload_seq
);
5101 xpthread_cond_destroy(&pmd
->cond
);
5102 ovs_mutex_destroy(&pmd
->cond_mutex
);
5103 ovs_mutex_destroy(&pmd
->port_mutex
);
5107 /* Stops the pmd thread, removes it from the 'dp->poll_threads',
5108 * and unrefs the struct. */
5110 dp_netdev_del_pmd(struct dp_netdev
*dp
, struct dp_netdev_pmd_thread
*pmd
)
5112 /* NON_PMD_CORE_ID doesn't have a thread, so we don't have to synchronize,
5113 * but extra cleanup is necessary */
5114 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
5115 ovs_mutex_lock(&dp
->non_pmd_mutex
);
5116 emc_cache_uninit(&pmd
->flow_cache
);
5117 pmd_free_cached_ports(pmd
);
5118 pmd_free_static_tx_qid(pmd
);
5119 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
5121 latch_set(&pmd
->exit_latch
);
5122 dp_netdev_reload_pmd__(pmd
);
5123 xpthread_join(pmd
->thread
, NULL
);
5126 dp_netdev_pmd_clear_ports(pmd
);
5128 /* Purges the 'pmd''s flows after stopping the thread, but before
5129 * destroying the flows, so that the flow stats can be collected. */
5130 if (dp
->dp_purge_cb
) {
5131 dp
->dp_purge_cb(dp
->dp_purge_aux
, pmd
->core_id
);
5133 cmap_remove(&pmd
->dp
->poll_threads
, &pmd
->node
, hash_int(pmd
->core_id
, 0));
5134 dp_netdev_pmd_unref(pmd
);
5137 /* Destroys all pmd threads. If 'non_pmd' is true it also destroys the non pmd
5140 dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
, bool non_pmd
)
5142 struct dp_netdev_pmd_thread
*pmd
;
5143 struct dp_netdev_pmd_thread
**pmd_list
;
5144 size_t k
= 0, n_pmds
;
5146 n_pmds
= cmap_count(&dp
->poll_threads
);
5147 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
5149 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
5150 if (!non_pmd
&& pmd
->core_id
== NON_PMD_CORE_ID
) {
5153 /* We cannot call dp_netdev_del_pmd(), since it alters
5154 * 'dp->poll_threads' (while we're iterating it) and it
5156 ovs_assert(k
< n_pmds
);
5157 pmd_list
[k
++] = pmd
;
5160 for (size_t i
= 0; i
< k
; i
++) {
5161 dp_netdev_del_pmd(dp
, pmd_list
[i
]);
5166 /* Deletes all rx queues from pmd->poll_list and all the ports from
5169 dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
)
5171 struct rxq_poll
*poll
;
5172 struct tx_port
*port
;
5174 ovs_mutex_lock(&pmd
->port_mutex
);
5175 HMAP_FOR_EACH_POP (poll
, node
, &pmd
->poll_list
) {
5178 HMAP_FOR_EACH_POP (port
, node
, &pmd
->tx_ports
) {
5181 ovs_mutex_unlock(&pmd
->port_mutex
);
5184 /* Adds rx queue to poll_list of PMD thread, if it's not there already. */
5186 dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
5187 struct dp_netdev_rxq
*rxq
)
5188 OVS_REQUIRES(pmd
->port_mutex
)
5190 int qid
= netdev_rxq_get_queue_id(rxq
->rx
);
5191 uint32_t hash
= hash_2words(odp_to_u32(rxq
->port
->port_no
), qid
);
5192 struct rxq_poll
*poll
;
5194 HMAP_FOR_EACH_WITH_HASH (poll
, node
, hash
, &pmd
->poll_list
) {
5195 if (poll
->rxq
== rxq
) {
5196 /* 'rxq' is already polled by this thread. Do nothing. */
5201 poll
= xmalloc(sizeof *poll
);
5203 hmap_insert(&pmd
->poll_list
, &poll
->node
, hash
);
5205 pmd
->need_reload
= true;
5208 /* Delete 'poll' from poll_list of PMD thread. */
5210 dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
5211 struct rxq_poll
*poll
)
5212 OVS_REQUIRES(pmd
->port_mutex
)
5214 hmap_remove(&pmd
->poll_list
, &poll
->node
);
5217 pmd
->need_reload
= true;
5220 /* Add 'port' to the tx port cache of 'pmd', which must be reloaded for the
5221 * changes to take effect. */
5223 dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
5224 struct dp_netdev_port
*port
)
5225 OVS_REQUIRES(pmd
->port_mutex
)
5229 tx
= tx_port_lookup(&pmd
->tx_ports
, port
->port_no
);
5231 /* 'port' is already on this thread tx cache. Do nothing. */
5235 tx
= xzalloc(sizeof *tx
);
5239 tx
->flush_time
= 0LL;
5240 dp_packet_batch_init(&tx
->output_pkts
);
5242 hmap_insert(&pmd
->tx_ports
, &tx
->node
, hash_port_no(tx
->port
->port_no
));
5243 pmd
->need_reload
= true;
5246 /* Del 'tx' from the tx port cache of 'pmd', which must be reloaded for the
5247 * changes to take effect. */
5249 dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
5251 OVS_REQUIRES(pmd
->port_mutex
)
5253 hmap_remove(&pmd
->tx_ports
, &tx
->node
);
5255 pmd
->need_reload
= true;
5259 dpif_netdev_get_datapath_version(void)
5261 return xstrdup("<built-in>");
5265 dp_netdev_flow_used(struct dp_netdev_flow
*netdev_flow
, int cnt
, int size
,
5266 uint16_t tcp_flags
, long long now
)
5270 atomic_store_relaxed(&netdev_flow
->stats
.used
, now
);
5271 non_atomic_ullong_add(&netdev_flow
->stats
.packet_count
, cnt
);
5272 non_atomic_ullong_add(&netdev_flow
->stats
.byte_count
, size
);
5273 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
5275 atomic_store_relaxed(&netdev_flow
->stats
.tcp_flags
, flags
);
5279 dp_netdev_upcall(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet
*packet_
,
5280 struct flow
*flow
, struct flow_wildcards
*wc
, ovs_u128
*ufid
,
5281 enum dpif_upcall_type type
, const struct nlattr
*userdata
,
5282 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
5284 struct dp_netdev
*dp
= pmd
->dp
;
5286 if (OVS_UNLIKELY(!dp
->upcall_cb
)) {
5290 if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl
))) {
5291 struct ds ds
= DS_EMPTY_INITIALIZER
;
5294 struct odp_flow_key_parms odp_parms
= {
5296 .mask
= wc
? &wc
->masks
: NULL
,
5297 .support
= dp_netdev_support
,
5300 ofpbuf_init(&key
, 0);
5301 odp_flow_key_from_flow(&odp_parms
, &key
);
5302 packet_str
= ofp_dp_packet_to_string(packet_
);
5304 odp_flow_key_format(key
.data
, key
.size
, &ds
);
5306 VLOG_DBG("%s: %s upcall:\n%s\n%s", dp
->name
,
5307 dpif_upcall_type_to_string(type
), ds_cstr(&ds
), packet_str
);
5309 ofpbuf_uninit(&key
);
5315 return dp
->upcall_cb(packet_
, flow
, ufid
, pmd
->core_id
, type
, userdata
,
5316 actions
, wc
, put_actions
, dp
->upcall_aux
);
5319 static inline uint32_t
5320 dpif_netdev_packet_get_rss_hash_orig_pkt(struct dp_packet
*packet
,
5321 const struct miniflow
*mf
)
5325 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
5326 hash
= dp_packet_get_rss_hash(packet
);
5328 hash
= miniflow_hash_5tuple(mf
, 0);
5329 dp_packet_set_rss_hash(packet
, hash
);
5335 static inline uint32_t
5336 dpif_netdev_packet_get_rss_hash(struct dp_packet
*packet
,
5337 const struct miniflow
*mf
)
5339 uint32_t hash
, recirc_depth
;
5341 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
5342 hash
= dp_packet_get_rss_hash(packet
);
5344 hash
= miniflow_hash_5tuple(mf
, 0);
5345 dp_packet_set_rss_hash(packet
, hash
);
5348 /* The RSS hash must account for the recirculation depth to avoid
5349 * collisions in the exact match cache */
5350 recirc_depth
= *recirc_depth_get_unsafe();
5351 if (OVS_UNLIKELY(recirc_depth
)) {
5352 hash
= hash_finish(hash
, recirc_depth
);
5353 dp_packet_set_rss_hash(packet
, hash
);
5358 struct packet_batch_per_flow
{
5359 unsigned int byte_count
;
5361 struct dp_netdev_flow
*flow
;
5363 struct dp_packet_batch array
;
5367 packet_batch_per_flow_update(struct packet_batch_per_flow
*batch
,
5368 struct dp_packet
*packet
,
5369 const struct miniflow
*mf
)
5371 batch
->byte_count
+= dp_packet_size(packet
);
5372 batch
->tcp_flags
|= miniflow_get_tcp_flags(mf
);
5373 batch
->array
.packets
[batch
->array
.count
++] = packet
;
5377 packet_batch_per_flow_init(struct packet_batch_per_flow
*batch
,
5378 struct dp_netdev_flow
*flow
)
5380 flow
->batch
= batch
;
5383 dp_packet_batch_init(&batch
->array
);
5384 batch
->byte_count
= 0;
5385 batch
->tcp_flags
= 0;
5389 packet_batch_per_flow_execute(struct packet_batch_per_flow
*batch
,
5390 struct dp_netdev_pmd_thread
*pmd
)
5392 struct dp_netdev_actions
*actions
;
5393 struct dp_netdev_flow
*flow
= batch
->flow
;
5395 dp_netdev_flow_used(flow
, batch
->array
.count
, batch
->byte_count
,
5396 batch
->tcp_flags
, pmd
->ctx
.now
/ 1000);
5398 actions
= dp_netdev_flow_get_actions(flow
);
5400 dp_netdev_execute_actions(pmd
, &batch
->array
, true, &flow
->flow
,
5401 actions
->actions
, actions
->size
);
5405 dp_netdev_queue_batches(struct dp_packet
*pkt
,
5406 struct dp_netdev_flow
*flow
, const struct miniflow
*mf
,
5407 struct packet_batch_per_flow
*batches
,
5410 struct packet_batch_per_flow
*batch
= flow
->batch
;
5412 if (OVS_UNLIKELY(!batch
)) {
5413 batch
= &batches
[(*n_batches
)++];
5414 packet_batch_per_flow_init(batch
, flow
);
5417 packet_batch_per_flow_update(batch
, pkt
, mf
);
5420 /* Try to process all ('cnt') the 'packets' using only the exact match cache
5421 * 'pmd->flow_cache'. If a flow is not found for a packet 'packets[i]', the
5422 * miniflow is copied into 'keys' and the packet pointer is moved at the
5423 * beginning of the 'packets' array.
5425 * The function returns the number of packets that needs to be processed in the
5426 * 'packets' array (they have been moved to the beginning of the vector).
5428 * For performance reasons a caller may choose not to initialize the metadata
5429 * in 'packets_'. If 'md_is_valid' is false, the metadata in 'packets'
5430 * is not valid and must be initialized by this function using 'port_no'.
5431 * If 'md_is_valid' is true, the metadata is already valid and 'port_no'
5434 static inline size_t
5435 emc_processing(struct dp_netdev_pmd_thread
*pmd
,
5436 struct dp_packet_batch
*packets_
,
5437 struct netdev_flow_key
*keys
,
5438 struct packet_batch_per_flow batches
[], size_t *n_batches
,
5439 bool md_is_valid
, odp_port_t port_no
)
5441 struct emc_cache
*flow_cache
= &pmd
->flow_cache
;
5442 struct netdev_flow_key
*key
= &keys
[0];
5443 size_t n_missed
= 0, n_dropped
= 0;
5444 struct dp_packet
*packet
;
5445 const size_t cnt
= dp_packet_batch_size(packets_
);
5449 atomic_read_relaxed(&pmd
->dp
->emc_insert_min
, &cur_min
);
5450 pmd_perf_update_counter(&pmd
->perf_stats
,
5451 md_is_valid
? PMD_STAT_RECIRC
: PMD_STAT_RECV
,
5454 DP_PACKET_BATCH_REFILL_FOR_EACH (i
, cnt
, packet
, packets_
) {
5455 struct dp_netdev_flow
*flow
;
5457 if (OVS_UNLIKELY(dp_packet_size(packet
) < ETH_HEADER_LEN
)) {
5458 dp_packet_delete(packet
);
5464 struct dp_packet
**packets
= packets_
->packets
;
5465 /* Prefetch next packet data and metadata. */
5466 OVS_PREFETCH(dp_packet_data(packets
[i
+1]));
5467 pkt_metadata_prefetch_init(&packets
[i
+1]->md
);
5471 pkt_metadata_init(&packet
->md
, port_no
);
5473 miniflow_extract(packet
, &key
->mf
);
5474 key
->len
= 0; /* Not computed yet. */
5475 /* If EMC is disabled skip hash computation and emc_lookup */
5478 key
->hash
= dpif_netdev_packet_get_rss_hash_orig_pkt(packet
,
5481 key
->hash
= dpif_netdev_packet_get_rss_hash(packet
, &key
->mf
);
5483 flow
= emc_lookup(flow_cache
, key
);
5487 if (OVS_LIKELY(flow
)) {
5488 dp_netdev_queue_batches(packet
, flow
, &key
->mf
, batches
,
5491 /* Exact match cache missed. Group missed packets together at
5492 * the beginning of the 'packets' array. */
5493 dp_packet_batch_refill(packets_
, packet
, i
);
5494 /* 'key[n_missed]' contains the key of the current packet and it
5495 * must be returned to the caller. The next key should be extracted
5496 * to 'keys[n_missed + 1]'. */
5497 key
= &keys
[++n_missed
];
5501 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_EXACT_HIT
,
5502 cnt
- n_dropped
- n_missed
);
5504 return dp_packet_batch_size(packets_
);
5508 handle_packet_upcall(struct dp_netdev_pmd_thread
*pmd
,
5509 struct dp_packet
*packet
,
5510 const struct netdev_flow_key
*key
,
5511 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
5513 struct ofpbuf
*add_actions
;
5514 struct dp_packet_batch b
;
5518 uint64_t cycles
= cycles_counter_update(&pmd
->perf_stats
);
5520 match
.tun_md
.valid
= false;
5521 miniflow_expand(&key
->mf
, &match
.flow
);
5523 ofpbuf_clear(actions
);
5524 ofpbuf_clear(put_actions
);
5526 dpif_flow_hash(pmd
->dp
->dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
5527 error
= dp_netdev_upcall(pmd
, packet
, &match
.flow
, &match
.wc
,
5528 &ufid
, DPIF_UC_MISS
, NULL
, actions
,
5530 if (OVS_UNLIKELY(error
&& error
!= ENOSPC
)) {
5531 dp_packet_delete(packet
);
5535 /* The Netlink encoding of datapath flow keys cannot express
5536 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
5537 * tag is interpreted as exact match on the fact that there is no
5538 * VLAN. Unless we refactor a lot of code that translates between
5539 * Netlink and struct flow representations, we have to do the same
5541 if (!match
.wc
.masks
.vlans
[0].tci
) {
5542 match
.wc
.masks
.vlans
[0].tci
= htons(0xffff);
5545 /* We can't allow the packet batching in the next loop to execute
5546 * the actions. Otherwise, if there are any slow path actions,
5547 * we'll send the packet up twice. */
5548 dp_packet_batch_init_packet(&b
, packet
);
5549 dp_netdev_execute_actions(pmd
, &b
, true, &match
.flow
,
5550 actions
->data
, actions
->size
);
5552 add_actions
= put_actions
->size
? put_actions
: actions
;
5553 if (OVS_LIKELY(error
!= ENOSPC
)) {
5554 struct dp_netdev_flow
*netdev_flow
;
5556 /* XXX: There's a race window where a flow covering this packet
5557 * could have already been installed since we last did the flow
5558 * lookup before upcall. This could be solved by moving the
5559 * mutex lock outside the loop, but that's an awful long time
5560 * to be locking everyone out of making flow installs. If we
5561 * move to a per-core classifier, it would be reasonable. */
5562 ovs_mutex_lock(&pmd
->flow_mutex
);
5563 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
, NULL
);
5564 if (OVS_LIKELY(!netdev_flow
)) {
5565 netdev_flow
= dp_netdev_flow_add(pmd
, &match
, &ufid
,
5569 ovs_mutex_unlock(&pmd
->flow_mutex
);
5570 emc_probabilistic_insert(pmd
, key
, netdev_flow
);
5572 if (pmd_perf_metrics_enabled(pmd
)) {
5573 /* Update upcall stats. */
5574 cycles
= cycles_counter_update(&pmd
->perf_stats
) - cycles
;
5575 struct pmd_perf_stats
*s
= &pmd
->perf_stats
;
5576 s
->current
.upcalls
++;
5577 s
->current
.upcall_cycles
+= cycles
;
5578 histogram_add_sample(&s
->cycles_per_upcall
, cycles
);
5584 fast_path_processing(struct dp_netdev_pmd_thread
*pmd
,
5585 struct dp_packet_batch
*packets_
,
5586 struct netdev_flow_key
*keys
,
5587 struct packet_batch_per_flow batches
[],
5591 const size_t cnt
= dp_packet_batch_size(packets_
);
5592 #if !defined(__CHECKER__) && !defined(_WIN32)
5593 const size_t PKT_ARRAY_SIZE
= cnt
;
5595 /* Sparse or MSVC doesn't like variable length array. */
5596 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
5598 struct dp_packet
*packet
;
5600 struct dpcls_rule
*rules
[PKT_ARRAY_SIZE
];
5601 struct dp_netdev
*dp
= pmd
->dp
;
5602 int upcall_ok_cnt
= 0, upcall_fail_cnt
= 0;
5603 int lookup_cnt
= 0, add_lookup_cnt
;
5606 for (size_t i
= 0; i
< cnt
; i
++) {
5607 /* Key length is needed in all the cases, hash computed on demand. */
5608 keys
[i
].len
= netdev_flow_key_size(miniflow_n_values(&keys
[i
].mf
));
5610 /* Get the classifier for the in_port */
5611 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
5612 if (OVS_LIKELY(cls
)) {
5613 any_miss
= !dpcls_lookup(cls
, keys
, rules
, cnt
, &lookup_cnt
);
5616 memset(rules
, 0, sizeof(rules
));
5618 if (OVS_UNLIKELY(any_miss
) && !fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
5619 uint64_t actions_stub
[512 / 8], slow_stub
[512 / 8];
5620 struct ofpbuf actions
, put_actions
;
5622 ofpbuf_use_stub(&actions
, actions_stub
, sizeof actions_stub
);
5623 ofpbuf_use_stub(&put_actions
, slow_stub
, sizeof slow_stub
);
5625 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
5626 struct dp_netdev_flow
*netdev_flow
;
5628 if (OVS_LIKELY(rules
[i
])) {
5632 /* It's possible that an earlier slow path execution installed
5633 * a rule covering this flow. In this case, it's a lot cheaper
5634 * to catch it here than execute a miss. */
5635 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, &keys
[i
],
5638 lookup_cnt
+= add_lookup_cnt
;
5639 rules
[i
] = &netdev_flow
->cr
;
5643 int error
= handle_packet_upcall(pmd
, packet
, &keys
[i
],
5644 &actions
, &put_actions
);
5646 if (OVS_UNLIKELY(error
)) {
5653 ofpbuf_uninit(&actions
);
5654 ofpbuf_uninit(&put_actions
);
5655 fat_rwlock_unlock(&dp
->upcall_rwlock
);
5656 } else if (OVS_UNLIKELY(any_miss
)) {
5657 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
5658 if (OVS_UNLIKELY(!rules
[i
])) {
5659 dp_packet_delete(packet
);
5665 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
5666 struct dp_netdev_flow
*flow
;
5668 if (OVS_UNLIKELY(!rules
[i
])) {
5672 flow
= dp_netdev_flow_cast(rules
[i
]);
5674 emc_probabilistic_insert(pmd
, &keys
[i
], flow
);
5675 dp_netdev_queue_batches(packet
, flow
, &keys
[i
].mf
, batches
, n_batches
);
5678 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MASKED_HIT
,
5679 cnt
- upcall_ok_cnt
- upcall_fail_cnt
);
5680 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MASKED_LOOKUP
,
5682 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MISS
,
5684 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_LOST
,
5688 /* Packets enter the datapath from a port (or from recirculation) here.
5690 * When 'md_is_valid' is true the metadata in 'packets' are already valid.
5691 * When false the metadata in 'packets' need to be initialized. */
5693 dp_netdev_input__(struct dp_netdev_pmd_thread
*pmd
,
5694 struct dp_packet_batch
*packets
,
5695 bool md_is_valid
, odp_port_t port_no
)
5697 #if !defined(__CHECKER__) && !defined(_WIN32)
5698 const size_t PKT_ARRAY_SIZE
= dp_packet_batch_size(packets
);
5700 /* Sparse or MSVC doesn't like variable length array. */
5701 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
5703 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
)
5704 struct netdev_flow_key keys
[PKT_ARRAY_SIZE
];
5705 struct packet_batch_per_flow batches
[PKT_ARRAY_SIZE
];
5710 emc_processing(pmd
, packets
, keys
, batches
, &n_batches
,
5711 md_is_valid
, port_no
);
5712 if (!dp_packet_batch_is_empty(packets
)) {
5713 /* Get ingress port from first packet's metadata. */
5714 in_port
= packets
->packets
[0]->md
.in_port
.odp_port
;
5715 fast_path_processing(pmd
, packets
, keys
,
5716 batches
, &n_batches
, in_port
);
5719 /* All the flow batches need to be reset before any call to
5720 * packet_batch_per_flow_execute() as it could potentially trigger
5721 * recirculation. When a packet matching flow ‘j’ happens to be
5722 * recirculated, the nested call to dp_netdev_input__() could potentially
5723 * classify the packet as matching another flow - say 'k'. It could happen
5724 * that in the previous call to dp_netdev_input__() that same flow 'k' had
5725 * already its own batches[k] still waiting to be served. So if its
5726 * ‘batch’ member is not reset, the recirculated packet would be wrongly
5727 * appended to batches[k] of the 1st call to dp_netdev_input__(). */
5729 for (i
= 0; i
< n_batches
; i
++) {
5730 batches
[i
].flow
->batch
= NULL
;
5733 for (i
= 0; i
< n_batches
; i
++) {
5734 packet_batch_per_flow_execute(&batches
[i
], pmd
);
5739 dp_netdev_input(struct dp_netdev_pmd_thread
*pmd
,
5740 struct dp_packet_batch
*packets
,
5743 dp_netdev_input__(pmd
, packets
, false, port_no
);
5747 dp_netdev_recirculate(struct dp_netdev_pmd_thread
*pmd
,
5748 struct dp_packet_batch
*packets
)
5750 dp_netdev_input__(pmd
, packets
, true, 0);
5753 struct dp_netdev_execute_aux
{
5754 struct dp_netdev_pmd_thread
*pmd
;
5755 const struct flow
*flow
;
5759 dpif_netdev_register_dp_purge_cb(struct dpif
*dpif
, dp_purge_callback
*cb
,
5762 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5763 dp
->dp_purge_aux
= aux
;
5764 dp
->dp_purge_cb
= cb
;
5768 dpif_netdev_register_upcall_cb(struct dpif
*dpif
, upcall_callback
*cb
,
5771 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5772 dp
->upcall_aux
= aux
;
5777 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
5781 struct dp_netdev_port
*port
;
5784 HMAP_FOR_EACH (tx
, node
, &pmd
->send_port_cache
) {
5785 if (!tx
->port
->dynamic_txqs
) {
5788 interval
= pmd
->ctx
.now
- tx
->last_used
;
5789 if (tx
->qid
>= 0 && (purge
|| interval
>= XPS_TIMEOUT
)) {
5791 ovs_mutex_lock(&port
->txq_used_mutex
);
5792 port
->txq_used
[tx
->qid
]--;
5793 ovs_mutex_unlock(&port
->txq_used_mutex
);
5800 dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
5803 struct dp_netdev_port
*port
;
5805 int i
, min_cnt
, min_qid
;
5807 interval
= pmd
->ctx
.now
- tx
->last_used
;
5808 tx
->last_used
= pmd
->ctx
.now
;
5810 if (OVS_LIKELY(tx
->qid
>= 0 && interval
< XPS_TIMEOUT
)) {
5816 ovs_mutex_lock(&port
->txq_used_mutex
);
5818 port
->txq_used
[tx
->qid
]--;
5824 for (i
= 0; i
< netdev_n_txq(port
->netdev
); i
++) {
5825 if (port
->txq_used
[i
] < min_cnt
|| min_cnt
== -1) {
5826 min_cnt
= port
->txq_used
[i
];
5831 port
->txq_used
[min_qid
]++;
5834 ovs_mutex_unlock(&port
->txq_used_mutex
);
5836 dpif_netdev_xps_revalidate_pmd(pmd
, false);
5838 VLOG_DBG("Core %d: New TX queue ID %d for port \'%s\'.",
5839 pmd
->core_id
, tx
->qid
, netdev_get_name(tx
->port
->netdev
));
5843 static struct tx_port
*
5844 pmd_tnl_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
5847 return tx_port_lookup(&pmd
->tnl_port_cache
, port_no
);
5850 static struct tx_port
*
5851 pmd_send_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
5854 return tx_port_lookup(&pmd
->send_port_cache
, port_no
);
5858 push_tnl_action(const struct dp_netdev_pmd_thread
*pmd
,
5859 const struct nlattr
*attr
,
5860 struct dp_packet_batch
*batch
)
5862 struct tx_port
*tun_port
;
5863 const struct ovs_action_push_tnl
*data
;
5866 data
= nl_attr_get(attr
);
5868 tun_port
= pmd_tnl_port_cache_lookup(pmd
, data
->tnl_port
);
5873 err
= netdev_push_header(tun_port
->port
->netdev
, batch
, data
);
5878 dp_packet_delete_batch(batch
, true);
5883 dp_execute_userspace_action(struct dp_netdev_pmd_thread
*pmd
,
5884 struct dp_packet
*packet
, bool should_steal
,
5885 struct flow
*flow
, ovs_u128
*ufid
,
5886 struct ofpbuf
*actions
,
5887 const struct nlattr
*userdata
)
5889 struct dp_packet_batch b
;
5892 ofpbuf_clear(actions
);
5894 error
= dp_netdev_upcall(pmd
, packet
, flow
, NULL
, ufid
,
5895 DPIF_UC_ACTION
, userdata
, actions
,
5897 if (!error
|| error
== ENOSPC
) {
5898 dp_packet_batch_init_packet(&b
, packet
);
5899 dp_netdev_execute_actions(pmd
, &b
, should_steal
, flow
,
5900 actions
->data
, actions
->size
);
5901 } else if (should_steal
) {
5902 dp_packet_delete(packet
);
5907 dp_execute_cb(void *aux_
, struct dp_packet_batch
*packets_
,
5908 const struct nlattr
*a
, bool should_steal
)
5909 OVS_NO_THREAD_SAFETY_ANALYSIS
5911 struct dp_netdev_execute_aux
*aux
= aux_
;
5912 uint32_t *depth
= recirc_depth_get();
5913 struct dp_netdev_pmd_thread
*pmd
= aux
->pmd
;
5914 struct dp_netdev
*dp
= pmd
->dp
;
5915 int type
= nl_attr_type(a
);
5918 switch ((enum ovs_action_attr
)type
) {
5919 case OVS_ACTION_ATTR_OUTPUT
:
5920 p
= pmd_send_port_cache_lookup(pmd
, nl_attr_get_odp_port(a
));
5921 if (OVS_LIKELY(p
)) {
5922 struct dp_packet
*packet
;
5923 struct dp_packet_batch out
;
5925 if (!should_steal
) {
5926 dp_packet_batch_clone(&out
, packets_
);
5927 dp_packet_batch_reset_cutlen(packets_
);
5930 dp_packet_batch_apply_cutlen(packets_
);
5933 if (OVS_UNLIKELY(!dp_packet_batch_is_empty(&p
->output_pkts
)
5934 && packets_
->packets
[0]->source
5935 != p
->output_pkts
.packets
[0]->source
)) {
5936 /* XXX: netdev-dpdk assumes that all packets in a single
5937 * output batch has the same source. Flush here to
5938 * avoid memory access issues. */
5939 dp_netdev_pmd_flush_output_on_port(pmd
, p
);
5942 if (dp_packet_batch_size(&p
->output_pkts
)
5943 + dp_packet_batch_size(packets_
) > NETDEV_MAX_BURST
) {
5944 /* Flush here to avoid overflow. */
5945 dp_netdev_pmd_flush_output_on_port(pmd
, p
);
5948 if (dp_packet_batch_is_empty(&p
->output_pkts
)) {
5949 pmd
->n_output_batches
++;
5952 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
5953 p
->output_pkts_rxqs
[dp_packet_batch_size(&p
->output_pkts
)] =
5955 dp_packet_batch_add(&p
->output_pkts
, packet
);
5961 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
5963 /* We're requested to push tunnel header, but also we need to take
5964 * the ownership of these packets. Thus, we can avoid performing
5965 * the action, because the caller will not use the result anyway.
5966 * Just break to free the batch. */
5969 dp_packet_batch_apply_cutlen(packets_
);
5970 push_tnl_action(pmd
, a
, packets_
);
5973 case OVS_ACTION_ATTR_TUNNEL_POP
:
5974 if (*depth
< MAX_RECIRC_DEPTH
) {
5975 struct dp_packet_batch
*orig_packets_
= packets_
;
5976 odp_port_t portno
= nl_attr_get_odp_port(a
);
5978 p
= pmd_tnl_port_cache_lookup(pmd
, portno
);
5980 struct dp_packet_batch tnl_pkt
;
5982 if (!should_steal
) {
5983 dp_packet_batch_clone(&tnl_pkt
, packets_
);
5984 packets_
= &tnl_pkt
;
5985 dp_packet_batch_reset_cutlen(orig_packets_
);
5988 dp_packet_batch_apply_cutlen(packets_
);
5990 netdev_pop_header(p
->port
->netdev
, packets_
);
5991 if (dp_packet_batch_is_empty(packets_
)) {
5995 struct dp_packet
*packet
;
5996 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
5997 packet
->md
.in_port
.odp_port
= portno
;
6001 dp_netdev_recirculate(pmd
, packets_
);
6008 case OVS_ACTION_ATTR_USERSPACE
:
6009 if (!fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
6010 struct dp_packet_batch
*orig_packets_
= packets_
;
6011 const struct nlattr
*userdata
;
6012 struct dp_packet_batch usr_pkt
;
6013 struct ofpbuf actions
;
6018 userdata
= nl_attr_find_nested(a
, OVS_USERSPACE_ATTR_USERDATA
);
6019 ofpbuf_init(&actions
, 0);
6021 if (packets_
->trunc
) {
6022 if (!should_steal
) {
6023 dp_packet_batch_clone(&usr_pkt
, packets_
);
6024 packets_
= &usr_pkt
;
6026 dp_packet_batch_reset_cutlen(orig_packets_
);
6029 dp_packet_batch_apply_cutlen(packets_
);
6032 struct dp_packet
*packet
;
6033 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6034 flow_extract(packet
, &flow
);
6035 dpif_flow_hash(dp
->dpif
, &flow
, sizeof flow
, &ufid
);
6036 dp_execute_userspace_action(pmd
, packet
, should_steal
, &flow
,
6037 &ufid
, &actions
, userdata
);
6041 dp_packet_delete_batch(packets_
, true);
6044 ofpbuf_uninit(&actions
);
6045 fat_rwlock_unlock(&dp
->upcall_rwlock
);
6051 case OVS_ACTION_ATTR_RECIRC
:
6052 if (*depth
< MAX_RECIRC_DEPTH
) {
6053 struct dp_packet_batch recirc_pkts
;
6055 if (!should_steal
) {
6056 dp_packet_batch_clone(&recirc_pkts
, packets_
);
6057 packets_
= &recirc_pkts
;
6060 struct dp_packet
*packet
;
6061 DP_PACKET_BATCH_FOR_EACH (i
, packet
, packets_
) {
6062 packet
->md
.recirc_id
= nl_attr_get_u32(a
);
6066 dp_netdev_recirculate(pmd
, packets_
);
6072 VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
6075 case OVS_ACTION_ATTR_CT
: {
6076 const struct nlattr
*b
;
6078 bool commit
= false;
6081 const char *helper
= NULL
;
6082 const uint32_t *setmark
= NULL
;
6083 const struct ovs_key_ct_labels
*setlabel
= NULL
;
6084 struct nat_action_info_t nat_action_info
;
6085 struct nat_action_info_t
*nat_action_info_ref
= NULL
;
6086 bool nat_config
= false;
6088 NL_ATTR_FOR_EACH_UNSAFE (b
, left
, nl_attr_get(a
),
6089 nl_attr_get_size(a
)) {
6090 enum ovs_ct_attr sub_type
= nl_attr_type(b
);
6093 case OVS_CT_ATTR_FORCE_COMMIT
:
6096 case OVS_CT_ATTR_COMMIT
:
6099 case OVS_CT_ATTR_ZONE
:
6100 zone
= nl_attr_get_u16(b
);
6102 case OVS_CT_ATTR_HELPER
:
6103 helper
= nl_attr_get_string(b
);
6105 case OVS_CT_ATTR_MARK
:
6106 setmark
= nl_attr_get(b
);
6108 case OVS_CT_ATTR_LABELS
:
6109 setlabel
= nl_attr_get(b
);
6111 case OVS_CT_ATTR_EVENTMASK
:
6112 /* Silently ignored, as userspace datapath does not generate
6113 * netlink events. */
6115 case OVS_CT_ATTR_NAT
: {
6116 const struct nlattr
*b_nest
;
6117 unsigned int left_nest
;
6118 bool ip_min_specified
= false;
6119 bool proto_num_min_specified
= false;
6120 bool ip_max_specified
= false;
6121 bool proto_num_max_specified
= false;
6122 memset(&nat_action_info
, 0, sizeof nat_action_info
);
6123 nat_action_info_ref
= &nat_action_info
;
6125 NL_NESTED_FOR_EACH_UNSAFE (b_nest
, left_nest
, b
) {
6126 enum ovs_nat_attr sub_type_nest
= nl_attr_type(b_nest
);
6128 switch (sub_type_nest
) {
6129 case OVS_NAT_ATTR_SRC
:
6130 case OVS_NAT_ATTR_DST
:
6132 nat_action_info
.nat_action
|=
6133 ((sub_type_nest
== OVS_NAT_ATTR_SRC
)
6134 ? NAT_ACTION_SRC
: NAT_ACTION_DST
);
6136 case OVS_NAT_ATTR_IP_MIN
:
6137 memcpy(&nat_action_info
.min_addr
,
6138 nl_attr_get(b_nest
),
6139 nl_attr_get_size(b_nest
));
6140 ip_min_specified
= true;
6142 case OVS_NAT_ATTR_IP_MAX
:
6143 memcpy(&nat_action_info
.max_addr
,
6144 nl_attr_get(b_nest
),
6145 nl_attr_get_size(b_nest
));
6146 ip_max_specified
= true;
6148 case OVS_NAT_ATTR_PROTO_MIN
:
6149 nat_action_info
.min_port
=
6150 nl_attr_get_u16(b_nest
);
6151 proto_num_min_specified
= true;
6153 case OVS_NAT_ATTR_PROTO_MAX
:
6154 nat_action_info
.max_port
=
6155 nl_attr_get_u16(b_nest
);
6156 proto_num_max_specified
= true;
6158 case OVS_NAT_ATTR_PERSISTENT
:
6159 case OVS_NAT_ATTR_PROTO_HASH
:
6160 case OVS_NAT_ATTR_PROTO_RANDOM
:
6162 case OVS_NAT_ATTR_UNSPEC
:
6163 case __OVS_NAT_ATTR_MAX
:
6168 if (ip_min_specified
&& !ip_max_specified
) {
6169 nat_action_info
.max_addr
= nat_action_info
.min_addr
;
6171 if (proto_num_min_specified
&& !proto_num_max_specified
) {
6172 nat_action_info
.max_port
= nat_action_info
.min_port
;
6174 if (proto_num_min_specified
|| proto_num_max_specified
) {
6175 if (nat_action_info
.nat_action
& NAT_ACTION_SRC
) {
6176 nat_action_info
.nat_action
|= NAT_ACTION_SRC_PORT
;
6177 } else if (nat_action_info
.nat_action
& NAT_ACTION_DST
) {
6178 nat_action_info
.nat_action
|= NAT_ACTION_DST_PORT
;
6183 case OVS_CT_ATTR_UNSPEC
:
6184 case __OVS_CT_ATTR_MAX
:
6189 /* We won't be able to function properly in this case, hence
6190 * complain loudly. */
6191 if (nat_config
&& !commit
) {
6192 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
6193 VLOG_WARN_RL(&rl
, "NAT specified without commit.");
6196 conntrack_execute(&dp
->conntrack
, packets_
, aux
->flow
->dl_type
, force
,
6197 commit
, zone
, setmark
, setlabel
, aux
->flow
->tp_src
,
6198 aux
->flow
->tp_dst
, helper
, nat_action_info_ref
,
6199 pmd
->ctx
.now
/ 1000);
6203 case OVS_ACTION_ATTR_METER
:
6204 dp_netdev_run_meter(pmd
->dp
, packets_
, nl_attr_get_u32(a
),
6208 case OVS_ACTION_ATTR_PUSH_VLAN
:
6209 case OVS_ACTION_ATTR_POP_VLAN
:
6210 case OVS_ACTION_ATTR_PUSH_MPLS
:
6211 case OVS_ACTION_ATTR_POP_MPLS
:
6212 case OVS_ACTION_ATTR_SET
:
6213 case OVS_ACTION_ATTR_SET_MASKED
:
6214 case OVS_ACTION_ATTR_SAMPLE
:
6215 case OVS_ACTION_ATTR_HASH
:
6216 case OVS_ACTION_ATTR_UNSPEC
:
6217 case OVS_ACTION_ATTR_TRUNC
:
6218 case OVS_ACTION_ATTR_PUSH_ETH
:
6219 case OVS_ACTION_ATTR_POP_ETH
:
6220 case OVS_ACTION_ATTR_CLONE
:
6221 case OVS_ACTION_ATTR_PUSH_NSH
:
6222 case OVS_ACTION_ATTR_POP_NSH
:
6223 case OVS_ACTION_ATTR_CT_CLEAR
:
6224 case __OVS_ACTION_ATTR_MAX
:
6228 dp_packet_delete_batch(packets_
, should_steal
);
6232 dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
6233 struct dp_packet_batch
*packets
,
6234 bool should_steal
, const struct flow
*flow
,
6235 const struct nlattr
*actions
, size_t actions_len
)
6237 struct dp_netdev_execute_aux aux
= { pmd
, flow
};
6239 odp_execute_actions(&aux
, packets
, should_steal
, actions
,
6240 actions_len
, dp_execute_cb
);
6243 struct dp_netdev_ct_dump
{
6244 struct ct_dpif_dump_state up
;
6245 struct conntrack_dump dump
;
6246 struct conntrack
*ct
;
6247 struct dp_netdev
*dp
;
6251 dpif_netdev_ct_dump_start(struct dpif
*dpif
, struct ct_dpif_dump_state
**dump_
,
6252 const uint16_t *pzone
, int *ptot_bkts
)
6254 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6255 struct dp_netdev_ct_dump
*dump
;
6257 dump
= xzalloc(sizeof *dump
);
6259 dump
->ct
= &dp
->conntrack
;
6261 conntrack_dump_start(&dp
->conntrack
, &dump
->dump
, pzone
, ptot_bkts
);
6269 dpif_netdev_ct_dump_next(struct dpif
*dpif OVS_UNUSED
,
6270 struct ct_dpif_dump_state
*dump_
,
6271 struct ct_dpif_entry
*entry
)
6273 struct dp_netdev_ct_dump
*dump
;
6275 INIT_CONTAINER(dump
, dump_
, up
);
6277 return conntrack_dump_next(&dump
->dump
, entry
);
6281 dpif_netdev_ct_dump_done(struct dpif
*dpif OVS_UNUSED
,
6282 struct ct_dpif_dump_state
*dump_
)
6284 struct dp_netdev_ct_dump
*dump
;
6287 INIT_CONTAINER(dump
, dump_
, up
);
6289 err
= conntrack_dump_done(&dump
->dump
);
6297 dpif_netdev_ct_flush(struct dpif
*dpif
, const uint16_t *zone
,
6298 const struct ct_dpif_tuple
*tuple
)
6300 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6303 return conntrack_flush_tuple(&dp
->conntrack
, tuple
, zone
? *zone
: 0);
6305 return conntrack_flush(&dp
->conntrack
, zone
);
6309 dpif_netdev_ct_set_maxconns(struct dpif
*dpif
, uint32_t maxconns
)
6311 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6313 return conntrack_set_maxconns(&dp
->conntrack
, maxconns
);
6317 dpif_netdev_ct_get_maxconns(struct dpif
*dpif
, uint32_t *maxconns
)
6319 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6321 return conntrack_get_maxconns(&dp
->conntrack
, maxconns
);
6325 dpif_netdev_ct_get_nconns(struct dpif
*dpif
, uint32_t *nconns
)
6327 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
6329 return conntrack_get_nconns(&dp
->conntrack
, nconns
);
6332 const struct dpif_class dpif_netdev_class
= {
6335 dpif_netdev_enumerate
,
6336 dpif_netdev_port_open_type
,
6339 dpif_netdev_destroy
,
6342 dpif_netdev_get_stats
,
6343 dpif_netdev_port_add
,
6344 dpif_netdev_port_del
,
6345 dpif_netdev_port_set_config
,
6346 dpif_netdev_port_query_by_number
,
6347 dpif_netdev_port_query_by_name
,
6348 NULL
, /* port_get_pid */
6349 dpif_netdev_port_dump_start
,
6350 dpif_netdev_port_dump_next
,
6351 dpif_netdev_port_dump_done
,
6352 dpif_netdev_port_poll
,
6353 dpif_netdev_port_poll_wait
,
6354 dpif_netdev_flow_flush
,
6355 dpif_netdev_flow_dump_create
,
6356 dpif_netdev_flow_dump_destroy
,
6357 dpif_netdev_flow_dump_thread_create
,
6358 dpif_netdev_flow_dump_thread_destroy
,
6359 dpif_netdev_flow_dump_next
,
6360 dpif_netdev_operate
,
6361 NULL
, /* recv_set */
6362 NULL
, /* handlers_set */
6363 dpif_netdev_set_config
,
6364 dpif_netdev_queue_to_priority
,
6366 NULL
, /* recv_wait */
6367 NULL
, /* recv_purge */
6368 dpif_netdev_register_dp_purge_cb
,
6369 dpif_netdev_register_upcall_cb
,
6370 dpif_netdev_enable_upcall
,
6371 dpif_netdev_disable_upcall
,
6372 dpif_netdev_get_datapath_version
,
6373 dpif_netdev_ct_dump_start
,
6374 dpif_netdev_ct_dump_next
,
6375 dpif_netdev_ct_dump_done
,
6376 dpif_netdev_ct_flush
,
6377 dpif_netdev_ct_set_maxconns
,
6378 dpif_netdev_ct_get_maxconns
,
6379 dpif_netdev_ct_get_nconns
,
6380 dpif_netdev_meter_get_features
,
6381 dpif_netdev_meter_set
,
6382 dpif_netdev_meter_get
,
6383 dpif_netdev_meter_del
,
6387 dpif_dummy_change_port_number(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
6388 const char *argv
[], void *aux OVS_UNUSED
)
6390 struct dp_netdev_port
*port
;
6391 struct dp_netdev
*dp
;
6394 ovs_mutex_lock(&dp_netdev_mutex
);
6395 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
6396 if (!dp
|| !dpif_netdev_class_is_dummy(dp
->class)) {
6397 ovs_mutex_unlock(&dp_netdev_mutex
);
6398 unixctl_command_reply_error(conn
, "unknown datapath or not a dummy");
6401 ovs_refcount_ref(&dp
->ref_cnt
);
6402 ovs_mutex_unlock(&dp_netdev_mutex
);
6404 ovs_mutex_lock(&dp
->port_mutex
);
6405 if (get_port_by_name(dp
, argv
[2], &port
)) {
6406 unixctl_command_reply_error(conn
, "unknown port");
6410 port_no
= u32_to_odp(atoi(argv
[3]));
6411 if (!port_no
|| port_no
== ODPP_NONE
) {
6412 unixctl_command_reply_error(conn
, "bad port number");
6415 if (dp_netdev_lookup_port(dp
, port_no
)) {
6416 unixctl_command_reply_error(conn
, "port number already in use");
6421 hmap_remove(&dp
->ports
, &port
->node
);
6422 reconfigure_datapath(dp
);
6424 /* Reinsert with new port number. */
6425 port
->port_no
= port_no
;
6426 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
6427 reconfigure_datapath(dp
);
6429 seq_change(dp
->port_seq
);
6430 unixctl_command_reply(conn
, NULL
);
6433 ovs_mutex_unlock(&dp
->port_mutex
);
6434 dp_netdev_unref(dp
);
6438 dpif_dummy_register__(const char *type
)
6440 struct dpif_class
*class;
6442 class = xmalloc(sizeof *class);
6443 *class = dpif_netdev_class
;
6444 class->type
= xstrdup(type
);
6445 dp_register_provider(class);
6449 dpif_dummy_override(const char *type
)
6454 * Ignore EAFNOSUPPORT to allow --enable-dummy=system with
6455 * a userland-only build. It's useful for testsuite.
6457 error
= dp_unregister_provider(type
);
6458 if (error
== 0 || error
== EAFNOSUPPORT
) {
6459 dpif_dummy_register__(type
);
6464 dpif_dummy_register(enum dummy_level level
)
6466 if (level
== DUMMY_OVERRIDE_ALL
) {
6471 dp_enumerate_types(&types
);
6472 SSET_FOR_EACH (type
, &types
) {
6473 dpif_dummy_override(type
);
6475 sset_destroy(&types
);
6476 } else if (level
== DUMMY_OVERRIDE_SYSTEM
) {
6477 dpif_dummy_override("system");
6480 dpif_dummy_register__("dummy");
6482 unixctl_command_register("dpif-dummy/change-port-number",
6483 "dp port new-number",
6484 3, 3, dpif_dummy_change_port_number
, NULL
);
6487 /* Datapath Classifier. */
6489 /* A set of rules that all have the same fields wildcarded. */
6490 struct dpcls_subtable
{
6491 /* The fields are only used by writers. */
6492 struct cmap_node cmap_node OVS_GUARDED
; /* Within dpcls 'subtables_map'. */
6494 /* These fields are accessed by readers. */
6495 struct cmap rules
; /* Contains "struct dpcls_rule"s. */
6496 uint32_t hit_cnt
; /* Number of match hits in subtable in current
6497 optimization interval. */
6498 struct netdev_flow_key mask
; /* Wildcards for fields (const). */
6499 /* 'mask' must be the last field, additional space is allocated here. */
6502 /* Initializes 'cls' as a classifier that initially contains no classification
6505 dpcls_init(struct dpcls
*cls
)
6507 cmap_init(&cls
->subtables_map
);
6508 pvector_init(&cls
->subtables
);
6512 dpcls_destroy_subtable(struct dpcls
*cls
, struct dpcls_subtable
*subtable
)
6514 VLOG_DBG("Destroying subtable %p for in_port %d", subtable
, cls
->in_port
);
6515 pvector_remove(&cls
->subtables
, subtable
);
6516 cmap_remove(&cls
->subtables_map
, &subtable
->cmap_node
,
6517 subtable
->mask
.hash
);
6518 cmap_destroy(&subtable
->rules
);
6519 ovsrcu_postpone(free
, subtable
);
6522 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
6523 * caller's responsibility.
6524 * May only be called after all the readers have been terminated. */
6526 dpcls_destroy(struct dpcls
*cls
)
6529 struct dpcls_subtable
*subtable
;
6531 CMAP_FOR_EACH (subtable
, cmap_node
, &cls
->subtables_map
) {
6532 ovs_assert(cmap_count(&subtable
->rules
) == 0);
6533 dpcls_destroy_subtable(cls
, subtable
);
6535 cmap_destroy(&cls
->subtables_map
);
6536 pvector_destroy(&cls
->subtables
);
6540 static struct dpcls_subtable
*
6541 dpcls_create_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
6543 struct dpcls_subtable
*subtable
;
6545 /* Need to add one. */
6546 subtable
= xmalloc(sizeof *subtable
6547 - sizeof subtable
->mask
.mf
+ mask
->len
);
6548 cmap_init(&subtable
->rules
);
6549 subtable
->hit_cnt
= 0;
6550 netdev_flow_key_clone(&subtable
->mask
, mask
);
6551 cmap_insert(&cls
->subtables_map
, &subtable
->cmap_node
, mask
->hash
);
6552 /* Add the new subtable at the end of the pvector (with no hits yet) */
6553 pvector_insert(&cls
->subtables
, subtable
, 0);
6554 VLOG_DBG("Creating %"PRIuSIZE
". subtable %p for in_port %d",
6555 cmap_count(&cls
->subtables_map
), subtable
, cls
->in_port
);
6556 pvector_publish(&cls
->subtables
);
6561 static inline struct dpcls_subtable
*
6562 dpcls_find_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
6564 struct dpcls_subtable
*subtable
;
6566 CMAP_FOR_EACH_WITH_HASH (subtable
, cmap_node
, mask
->hash
,
6567 &cls
->subtables_map
) {
6568 if (netdev_flow_key_equal(&subtable
->mask
, mask
)) {
6572 return dpcls_create_subtable(cls
, mask
);
6576 /* Periodically sort the dpcls subtable vectors according to hit counts */
6578 dpcls_sort_subtable_vector(struct dpcls
*cls
)
6580 struct pvector
*pvec
= &cls
->subtables
;
6581 struct dpcls_subtable
*subtable
;
6583 PVECTOR_FOR_EACH (subtable
, pvec
) {
6584 pvector_change_priority(pvec
, subtable
, subtable
->hit_cnt
);
6585 subtable
->hit_cnt
= 0;
6587 pvector_publish(pvec
);
6591 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread
*pmd
,
6592 struct polled_queue
*poll_list
, int poll_cnt
)
6596 if (pmd
->ctx
.now
> pmd
->rxq_next_cycle_store
) {
6598 /* Get the cycles that were used to process each queue and store. */
6599 for (unsigned i
= 0; i
< poll_cnt
; i
++) {
6600 uint64_t rxq_cyc_curr
= dp_netdev_rxq_get_cycles(poll_list
[i
].rxq
,
6601 RXQ_CYCLES_PROC_CURR
);
6602 dp_netdev_rxq_set_intrvl_cycles(poll_list
[i
].rxq
, rxq_cyc_curr
);
6603 dp_netdev_rxq_set_cycles(poll_list
[i
].rxq
, RXQ_CYCLES_PROC_CURR
,
6606 curr_tsc
= cycles_counter_update(&pmd
->perf_stats
);
6607 if (pmd
->intrvl_tsc_prev
) {
6608 /* There is a prev timestamp, store a new intrvl cycle count. */
6609 atomic_store_relaxed(&pmd
->intrvl_cycles
,
6610 curr_tsc
- pmd
->intrvl_tsc_prev
);
6612 pmd
->intrvl_tsc_prev
= curr_tsc
;
6613 /* Start new measuring interval */
6614 pmd
->rxq_next_cycle_store
= pmd
->ctx
.now
+ PMD_RXQ_INTERVAL_LEN
;
6617 if (pmd
->ctx
.now
> pmd
->next_optimization
) {
6618 /* Try to obtain the flow lock to block out revalidator threads.
6619 * If not possible, just try next time. */
6620 if (!ovs_mutex_trylock(&pmd
->flow_mutex
)) {
6621 /* Optimize each classifier */
6622 CMAP_FOR_EACH (cls
, node
, &pmd
->classifiers
) {
6623 dpcls_sort_subtable_vector(cls
);
6625 ovs_mutex_unlock(&pmd
->flow_mutex
);
6626 /* Start new measuring interval */
6627 pmd
->next_optimization
= pmd
->ctx
.now
6628 + DPCLS_OPTIMIZATION_INTERVAL
;
6633 /* Insert 'rule' into 'cls'. */
6635 dpcls_insert(struct dpcls
*cls
, struct dpcls_rule
*rule
,
6636 const struct netdev_flow_key
*mask
)
6638 struct dpcls_subtable
*subtable
= dpcls_find_subtable(cls
, mask
);
6640 /* Refer to subtable's mask, also for later removal. */
6641 rule
->mask
= &subtable
->mask
;
6642 cmap_insert(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
);
6645 /* Removes 'rule' from 'cls', also destructing the 'rule'. */
6647 dpcls_remove(struct dpcls
*cls
, struct dpcls_rule
*rule
)
6649 struct dpcls_subtable
*subtable
;
6651 ovs_assert(rule
->mask
);
6653 /* Get subtable from reference in rule->mask. */
6654 INIT_CONTAINER(subtable
, rule
->mask
, mask
);
6655 if (cmap_remove(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
)
6657 /* Delete empty subtable. */
6658 dpcls_destroy_subtable(cls
, subtable
);
6659 pvector_publish(&cls
->subtables
);
6663 /* Returns true if 'target' satisfies 'key' in 'mask', that is, if each 1-bit
6664 * in 'mask' the values in 'key' and 'target' are the same. */
6666 dpcls_rule_matches_key(const struct dpcls_rule
*rule
,
6667 const struct netdev_flow_key
*target
)
6669 const uint64_t *keyp
= miniflow_get_values(&rule
->flow
.mf
);
6670 const uint64_t *maskp
= miniflow_get_values(&rule
->mask
->mf
);
6673 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, target
, rule
->flow
.mf
.map
) {
6674 if (OVS_UNLIKELY((value
& *maskp
++) != *keyp
++)) {
6681 /* For each miniflow in 'keys' performs a classifier lookup writing the result
6682 * into the corresponding slot in 'rules'. If a particular entry in 'keys' is
6683 * NULL it is skipped.
6685 * This function is optimized for use in the userspace datapath and therefore
6686 * does not implement a lot of features available in the standard
6687 * classifier_lookup() function. Specifically, it does not implement
6688 * priorities, instead returning any rule which matches the flow.
6690 * Returns true if all miniflows found a corresponding rule. */
6692 dpcls_lookup(struct dpcls
*cls
, const struct netdev_flow_key keys
[],
6693 struct dpcls_rule
**rules
, const size_t cnt
,
6696 /* The received 'cnt' miniflows are the search-keys that will be processed
6697 * to find a matching entry into the available subtables.
6698 * The number of bits in map_type is equal to NETDEV_MAX_BURST. */
6699 typedef uint32_t map_type
;
6700 #define MAP_BITS (sizeof(map_type) * CHAR_BIT)
6701 BUILD_ASSERT_DECL(MAP_BITS
>= NETDEV_MAX_BURST
);
6703 struct dpcls_subtable
*subtable
;
6705 map_type keys_map
= TYPE_MAXIMUM(map_type
); /* Set all bits. */
6707 uint32_t hashes
[MAP_BITS
];
6708 const struct cmap_node
*nodes
[MAP_BITS
];
6710 if (cnt
!= MAP_BITS
) {
6711 keys_map
>>= MAP_BITS
- cnt
; /* Clear extra bits. */
6713 memset(rules
, 0, cnt
* sizeof *rules
);
6715 int lookups_match
= 0, subtable_pos
= 1;
6717 /* The Datapath classifier - aka dpcls - is composed of subtables.
6718 * Subtables are dynamically created as needed when new rules are inserted.
6719 * Each subtable collects rules with matches on a specific subset of packet
6720 * fields as defined by the subtable's mask. We proceed to process every
6721 * search-key against each subtable, but when a match is found for a
6722 * search-key, the search for that key can stop because the rules are
6723 * non-overlapping. */
6724 PVECTOR_FOR_EACH (subtable
, &cls
->subtables
) {
6727 /* Compute hashes for the remaining keys. Each search-key is
6728 * masked with the subtable's mask to avoid hashing the wildcarded
6730 ULLONG_FOR_EACH_1(i
, keys_map
) {
6731 hashes
[i
] = netdev_flow_key_hash_in_mask(&keys
[i
],
6735 found_map
= cmap_find_batch(&subtable
->rules
, keys_map
, hashes
, nodes
);
6736 /* Check results. When the i-th bit of found_map is set, it means
6737 * that a set of nodes with a matching hash value was found for the
6738 * i-th search-key. Due to possible hash collisions we need to check
6739 * which of the found rules, if any, really matches our masked
6741 ULLONG_FOR_EACH_1(i
, found_map
) {
6742 struct dpcls_rule
*rule
;
6744 CMAP_NODE_FOR_EACH (rule
, cmap_node
, nodes
[i
]) {
6745 if (OVS_LIKELY(dpcls_rule_matches_key(rule
, &keys
[i
]))) {
6747 /* Even at 20 Mpps the 32-bit hit_cnt cannot wrap
6748 * within one second optimization interval. */
6749 subtable
->hit_cnt
++;
6750 lookups_match
+= subtable_pos
;
6754 /* None of the found rules was a match. Reset the i-th bit to
6755 * keep searching this key in the next subtable. */
6756 ULLONG_SET0(found_map
, i
); /* Did not match. */
6758 ; /* Keep Sparse happy. */
6760 keys_map
&= ~found_map
; /* Clear the found rules. */
6762 if (num_lookups_p
) {
6763 *num_lookups_p
= lookups_match
;
6765 return true; /* All found. */
6769 if (num_lookups_p
) {
6770 *num_lookups_p
= lookups_match
;
6772 return false; /* Some misses. */