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-vport.h"
54 #include "odp-execute.h"
56 #include "openvswitch/dynamic-string.h"
57 #include "openvswitch/list.h"
58 #include "openvswitch/match.h"
59 #include "openvswitch/ofp-print.h"
60 #include "openvswitch/ofp-util.h"
61 #include "openvswitch/ofpbuf.h"
62 #include "openvswitch/shash.h"
63 #include "openvswitch/vlog.h"
67 #include "openvswitch/poll-loop.h"
74 #include "tnl-neigh-cache.h"
75 #include "tnl-ports.h"
79 VLOG_DEFINE_THIS_MODULE(dpif_netdev
);
81 #define FLOW_DUMP_MAX_BATCH 50
82 /* Use per thread recirc_depth to prevent recirculation loop. */
83 #define MAX_RECIRC_DEPTH 6
84 DEFINE_STATIC_PER_THREAD_DATA(uint32_t, recirc_depth
, 0)
86 /* Use instant packet send by default. */
87 #define DEFAULT_TX_FLUSH_INTERVAL 0
89 /* Configuration parameters. */
90 enum { MAX_FLOWS
= 65536 }; /* Maximum number of flows in flow table. */
91 enum { MAX_METERS
= 65536 }; /* Maximum number of meters. */
92 enum { MAX_BANDS
= 8 }; /* Maximum number of bands / meter. */
93 enum { N_METER_LOCKS
= 64 }; /* Maximum number of meters. */
95 /* Protects against changes to 'dp_netdevs'. */
96 static struct ovs_mutex dp_netdev_mutex
= OVS_MUTEX_INITIALIZER
;
98 /* Contains all 'struct dp_netdev's. */
99 static struct shash dp_netdevs
OVS_GUARDED_BY(dp_netdev_mutex
)
100 = SHASH_INITIALIZER(&dp_netdevs
);
102 static struct vlog_rate_limit upcall_rl
= VLOG_RATE_LIMIT_INIT(600, 600);
104 #define DP_NETDEV_CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \
105 | CS_INVALID | CS_REPLY_DIR | CS_TRACKED \
106 | CS_SRC_NAT | CS_DST_NAT)
107 #define DP_NETDEV_CS_UNSUPPORTED_MASK (~(uint32_t)DP_NETDEV_CS_SUPPORTED_MASK)
109 static struct odp_support dp_netdev_support
= {
110 .max_vlan_headers
= SIZE_MAX
,
111 .max_mpls_depth
= SIZE_MAX
,
117 .ct_state_nat
= true,
118 .ct_orig_tuple
= true,
119 .ct_orig_tuple6
= true,
122 /* Stores a miniflow with inline values */
124 struct netdev_flow_key
{
125 uint32_t hash
; /* Hash function differs for different users. */
126 uint32_t len
; /* Length of the following miniflow (incl. map). */
128 uint64_t buf
[FLOW_MAX_PACKET_U64S
];
131 /* Exact match cache for frequently used flows
133 * The cache uses a 32-bit hash of the packet (which can be the RSS hash) to
134 * search its entries for a miniflow that matches exactly the miniflow of the
135 * packet. It stores the 'dpcls_rule' (rule) that matches the miniflow.
137 * A cache entry holds a reference to its 'dp_netdev_flow'.
139 * A miniflow with a given hash can be in one of EM_FLOW_HASH_SEGS different
140 * entries. The 32-bit hash is split into EM_FLOW_HASH_SEGS values (each of
141 * them is EM_FLOW_HASH_SHIFT bits wide and the remainder is thrown away). Each
142 * value is the index of a cache entry where the miniflow could be.
148 * Each pmd_thread has its own private exact match cache.
149 * If dp_netdev_input is not called from a pmd thread, a mutex is used.
152 #define EM_FLOW_HASH_SHIFT 13
153 #define EM_FLOW_HASH_ENTRIES (1u << EM_FLOW_HASH_SHIFT)
154 #define EM_FLOW_HASH_MASK (EM_FLOW_HASH_ENTRIES - 1)
155 #define EM_FLOW_HASH_SEGS 2
157 /* Default EMC insert probability is 1 / DEFAULT_EM_FLOW_INSERT_INV_PROB */
158 #define DEFAULT_EM_FLOW_INSERT_INV_PROB 100
159 #define DEFAULT_EM_FLOW_INSERT_MIN (UINT32_MAX / \
160 DEFAULT_EM_FLOW_INSERT_INV_PROB)
163 struct dp_netdev_flow
*flow
;
164 struct netdev_flow_key key
; /* key.hash used for emc hash value. */
168 struct emc_entry entries
[EM_FLOW_HASH_ENTRIES
];
169 int sweep_idx
; /* For emc_cache_slow_sweep(). */
172 /* Iterate in the exact match cache through every entry that might contain a
173 * miniflow with hash 'HASH'. */
174 #define EMC_FOR_EACH_POS_WITH_HASH(EMC, CURRENT_ENTRY, HASH) \
175 for (uint32_t i__ = 0, srch_hash__ = (HASH); \
176 (CURRENT_ENTRY) = &(EMC)->entries[srch_hash__ & EM_FLOW_HASH_MASK], \
177 i__ < EM_FLOW_HASH_SEGS; \
178 i__++, srch_hash__ >>= EM_FLOW_HASH_SHIFT)
180 /* Simple non-wildcarding single-priority classifier. */
182 /* Time in microseconds between successive optimizations of the dpcls
184 #define DPCLS_OPTIMIZATION_INTERVAL 1000000LL
186 /* Time in microseconds of the interval in which rxq processing cycles used
187 * in rxq to pmd assignments is measured and stored. */
188 #define PMD_RXQ_INTERVAL_LEN 10000000LL
190 /* Number of intervals for which cycles are stored
191 * and used during rxq to pmd assignment. */
192 #define PMD_RXQ_INTERVAL_MAX 6
195 struct cmap_node node
; /* Within dp_netdev_pmd_thread.classifiers */
197 struct cmap subtables_map
;
198 struct pvector subtables
;
201 /* A rule to be inserted to the classifier. */
203 struct cmap_node cmap_node
; /* Within struct dpcls_subtable 'rules'. */
204 struct netdev_flow_key
*mask
; /* Subtable's mask. */
205 struct netdev_flow_key flow
; /* Matching key. */
206 /* 'flow' must be the last field, additional space is allocated here. */
209 static void dpcls_init(struct dpcls
*);
210 static void dpcls_destroy(struct dpcls
*);
211 static void dpcls_sort_subtable_vector(struct dpcls
*);
212 static void dpcls_insert(struct dpcls
*, struct dpcls_rule
*,
213 const struct netdev_flow_key
*mask
);
214 static void dpcls_remove(struct dpcls
*, struct dpcls_rule
*);
215 static bool dpcls_lookup(struct dpcls
*cls
,
216 const struct netdev_flow_key keys
[],
217 struct dpcls_rule
**rules
, size_t cnt
,
220 /* Set of supported meter flags */
221 #define DP_SUPPORTED_METER_FLAGS_MASK \
222 (OFPMF13_STATS | OFPMF13_PKTPS | OFPMF13_KBPS | OFPMF13_BURST)
224 /* Set of supported meter band types */
225 #define DP_SUPPORTED_METER_BAND_TYPES \
226 ( 1 << OFPMBT13_DROP )
228 struct dp_meter_band
{
229 struct ofputil_meter_band up
; /* type, prec_level, pad, rate, burst_size */
230 uint32_t bucket
; /* In 1/1000 packets (for PKTPS), or in bits (for KBPS) */
231 uint64_t packet_count
;
238 uint32_t max_delta_t
;
240 uint64_t packet_count
;
242 struct dp_meter_band bands
[];
245 /* Datapath based on the network device interface from netdev.h.
251 * Some members, marked 'const', are immutable. Accessing other members
252 * requires synchronization, as noted in more detail below.
254 * Acquisition order is, from outermost to innermost:
256 * dp_netdev_mutex (global)
261 const struct dpif_class
*const class;
262 const char *const name
;
264 struct ovs_refcount ref_cnt
;
265 atomic_flag destroyed
;
269 * Any lookup into 'ports' or any access to the dp_netdev_ports found
270 * through 'ports' requires taking 'port_mutex'. */
271 struct ovs_mutex port_mutex
;
273 struct seq
*port_seq
; /* Incremented whenever a port changes. */
275 /* The time that a packet can wait in output batch for sending. */
276 atomic_uint32_t tx_flush_interval
;
279 struct ovs_mutex meter_locks
[N_METER_LOCKS
];
280 struct dp_meter
*meters
[MAX_METERS
]; /* Meter bands. */
282 /* Probability of EMC insertions is a factor of 'emc_insert_min'.*/
283 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
) atomic_uint32_t emc_insert_min
;
285 /* Protects access to ofproto-dpif-upcall interface during revalidator
286 * thread synchronization. */
287 struct fat_rwlock upcall_rwlock
;
288 upcall_callback
*upcall_cb
; /* Callback function for executing upcalls. */
291 /* Callback function for notifying the purging of dp flows (during
292 * reseting pmd deletion). */
293 dp_purge_callback
*dp_purge_cb
;
296 /* Stores all 'struct dp_netdev_pmd_thread's. */
297 struct cmap poll_threads
;
298 /* id pool for per thread static_tx_qid. */
299 struct id_pool
*tx_qid_pool
;
300 struct ovs_mutex tx_qid_pool_mutex
;
302 /* Protects the access of the 'struct dp_netdev_pmd_thread'
303 * instance for non-pmd thread. */
304 struct ovs_mutex non_pmd_mutex
;
306 /* Each pmd thread will store its pointer to
307 * 'struct dp_netdev_pmd_thread' in 'per_pmd_key'. */
308 ovsthread_key_t per_pmd_key
;
310 struct seq
*reconfigure_seq
;
311 uint64_t last_reconfigure_seq
;
313 /* Cpu mask for pin of pmd threads. */
316 uint64_t last_tnl_conf_seq
;
318 struct conntrack conntrack
;
321 static void meter_lock(const struct dp_netdev
*dp
, uint32_t meter_id
)
322 OVS_ACQUIRES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
324 ovs_mutex_lock(&dp
->meter_locks
[meter_id
% N_METER_LOCKS
]);
327 static void meter_unlock(const struct dp_netdev
*dp
, uint32_t meter_id
)
328 OVS_RELEASES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
330 ovs_mutex_unlock(&dp
->meter_locks
[meter_id
% N_METER_LOCKS
]);
334 static struct dp_netdev_port
*dp_netdev_lookup_port(const struct dp_netdev
*dp
,
336 OVS_REQUIRES(dp
->port_mutex
);
338 enum rxq_cycles_counter_type
{
339 RXQ_CYCLES_PROC_CURR
, /* Cycles spent successfully polling and
340 processing packets during the current
342 RXQ_CYCLES_PROC_HIST
, /* Total cycles of all intervals that are used
343 during rxq to pmd assignment. */
347 #define XPS_TIMEOUT 500000LL /* In microseconds. */
349 /* Contained by struct dp_netdev_port's 'rxqs' member. */
350 struct dp_netdev_rxq
{
351 struct dp_netdev_port
*port
;
352 struct netdev_rxq
*rx
;
353 unsigned core_id
; /* Core to which this queue should be
354 pinned. OVS_CORE_UNSPEC if the
355 queue doesn't need to be pinned to a
357 unsigned intrvl_idx
; /* Write index for 'cycles_intrvl'. */
358 struct dp_netdev_pmd_thread
*pmd
; /* pmd thread that polls this queue. */
360 /* Counters of cycles spent successfully polling and processing pkts. */
361 atomic_ullong cycles
[RXQ_N_CYCLES
];
362 /* We store PMD_RXQ_INTERVAL_MAX intervals of data for an rxq and then
363 sum them to yield the cycles used for an rxq. */
364 atomic_ullong cycles_intrvl
[PMD_RXQ_INTERVAL_MAX
];
367 /* A port in a netdev-based datapath. */
368 struct dp_netdev_port
{
370 bool dynamic_txqs
; /* If true XPS will be used. */
371 bool need_reconfigure
; /* True if we should reconfigure netdev. */
372 struct netdev
*netdev
;
373 struct hmap_node node
; /* Node in dp_netdev's 'ports'. */
374 struct netdev_saved_flags
*sf
;
375 struct dp_netdev_rxq
*rxqs
;
376 unsigned n_rxq
; /* Number of elements in 'rxqs' */
377 unsigned *txq_used
; /* Number of threads that use each tx queue. */
378 struct ovs_mutex txq_used_mutex
;
379 char *type
; /* Port type as requested by user. */
380 char *rxq_affinity_list
; /* Requested affinity of rx queues. */
383 /* Contained by struct dp_netdev_flow's 'stats' member. */
384 struct dp_netdev_flow_stats
{
385 atomic_llong used
; /* Last used time, in monotonic msecs. */
386 atomic_ullong packet_count
; /* Number of packets matched. */
387 atomic_ullong byte_count
; /* Number of bytes matched. */
388 atomic_uint16_t tcp_flags
; /* Bitwise-OR of seen tcp_flags values. */
391 /* A flow in 'dp_netdev_pmd_thread's 'flow_table'.
397 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
398 * its pmd thread's classifier. The text below calls this classifier 'cls'.
403 * The thread safety rules described here for "struct dp_netdev_flow" are
404 * motivated by two goals:
406 * - Prevent threads that read members of "struct dp_netdev_flow" from
407 * reading bad data due to changes by some thread concurrently modifying
410 * - Prevent two threads making changes to members of a given "struct
411 * dp_netdev_flow" from interfering with each other.
417 * A flow 'flow' may be accessed without a risk of being freed during an RCU
418 * grace period. Code that needs to hold onto a flow for a while
419 * should try incrementing 'flow->ref_cnt' with dp_netdev_flow_ref().
421 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
422 * flow from being deleted from 'cls' and it doesn't protect members of 'flow'
425 * Some members, marked 'const', are immutable. Accessing other members
426 * requires synchronization, as noted in more detail below.
428 struct dp_netdev_flow
{
429 const struct flow flow
; /* Unmasked flow that created this entry. */
430 /* Hash table index by unmasked flow. */
431 const struct cmap_node node
; /* In owning dp_netdev_pmd_thread's */
433 const ovs_u128 ufid
; /* Unique flow identifier. */
434 const unsigned pmd_id
; /* The 'core_id' of pmd thread owning this */
437 /* Number of references.
438 * The classifier owns one reference.
439 * Any thread trying to keep a rule from being freed should hold its own
441 struct ovs_refcount ref_cnt
;
446 struct dp_netdev_flow_stats stats
;
449 OVSRCU_TYPE(struct dp_netdev_actions
*) actions
;
451 /* While processing a group of input packets, the datapath uses the next
452 * member to store a pointer to the output batch for the flow. It is
453 * reset after the batch has been sent out (See dp_netdev_queue_batches(),
454 * packet_batch_per_flow_init() and packet_batch_per_flow_execute()). */
455 struct packet_batch_per_flow
*batch
;
457 /* Packet classification. */
458 struct dpcls_rule cr
; /* In owning dp_netdev's 'cls'. */
459 /* 'cr' must be the last member. */
462 static void dp_netdev_flow_unref(struct dp_netdev_flow
*);
463 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*);
464 static int dpif_netdev_flow_from_nlattrs(const struct nlattr
*, uint32_t,
465 struct flow
*, bool);
467 /* A set of datapath actions within a "struct dp_netdev_flow".
473 * A struct dp_netdev_actions 'actions' is protected with RCU. */
474 struct dp_netdev_actions
{
475 /* These members are immutable: they do not change during the struct's
477 unsigned int size
; /* Size of 'actions', in bytes. */
478 struct nlattr actions
[]; /* Sequence of OVS_ACTION_ATTR_* attributes. */
481 struct dp_netdev_actions
*dp_netdev_actions_create(const struct nlattr
*,
483 struct dp_netdev_actions
*dp_netdev_flow_get_actions(
484 const struct dp_netdev_flow
*);
485 static void dp_netdev_actions_free(struct dp_netdev_actions
*);
487 struct polled_queue
{
488 struct dp_netdev_rxq
*rxq
;
492 /* Contained by struct dp_netdev_pmd_thread's 'poll_list' member. */
494 struct dp_netdev_rxq
*rxq
;
495 struct hmap_node node
;
498 /* Contained by struct dp_netdev_pmd_thread's 'send_port_cache',
499 * 'tnl_port_cache' or 'tx_ports'. */
501 struct dp_netdev_port
*port
;
504 struct hmap_node node
;
505 long long flush_time
;
506 struct dp_packet_batch output_pkts
;
507 struct dp_netdev_rxq
*output_pkts_rxqs
[NETDEV_MAX_BURST
];
510 /* A set of properties for the current processing loop that is not directly
511 * associated with the pmd thread itself, but with the packets being
512 * processed or the short-term system configuration (for example, time).
513 * Contained by struct dp_netdev_pmd_thread's 'ctx' member. */
514 struct dp_netdev_pmd_thread_ctx
{
515 /* Latest measured time. See 'pmd_thread_ctx_time_update()'. */
517 /* RX queue from which last packet was received. */
518 struct dp_netdev_rxq
*last_rxq
;
521 /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
522 * the performance overhead of interrupt processing. Therefore netdev can
523 * not implement rx-wait for these devices. dpif-netdev needs to poll
524 * these device to check for recv buffer. pmd-thread does polling for
525 * devices assigned to itself.
527 * DPDK used PMD for accessing NIC.
529 * Note, instance with cpu core id NON_PMD_CORE_ID will be reserved for
530 * I/O of all non-pmd threads. There will be no actual thread created
533 * Each struct has its own flow cache and classifier per managed ingress port.
534 * For packets received on ingress port, a look up is done on corresponding PMD
535 * thread's flow cache and in case of a miss, lookup is performed in the
536 * corresponding classifier of port. Packets are executed with the found
537 * actions in either case.
539 struct dp_netdev_pmd_thread
{
540 struct dp_netdev
*dp
;
541 struct ovs_refcount ref_cnt
; /* Every reference must be refcount'ed. */
542 struct cmap_node node
; /* In 'dp->poll_threads'. */
544 pthread_cond_t cond
; /* For synchronizing pmd thread reload. */
545 struct ovs_mutex cond_mutex
; /* Mutex for condition variable. */
547 /* Per thread exact-match cache. Note, the instance for cpu core
548 * NON_PMD_CORE_ID can be accessed by multiple threads, and thusly
549 * need to be protected by 'non_pmd_mutex'. Every other instance
550 * will only be accessed by its own pmd thread. */
551 struct emc_cache flow_cache
;
553 /* Flow-Table and classifiers
555 * Writers of 'flow_table' must take the 'flow_mutex'. Corresponding
556 * changes to 'classifiers' must be made while still holding the
559 struct ovs_mutex flow_mutex
;
560 struct cmap flow_table OVS_GUARDED
; /* Flow table. */
562 /* One classifier per in_port polled by the pmd */
563 struct cmap classifiers
;
564 /* Periodically sort subtable vectors according to hit frequencies */
565 long long int next_optimization
;
566 /* End of the next time interval for which processing cycles
567 are stored for each polled rxq. */
568 long long int rxq_next_cycle_store
;
570 /* Current context of the PMD thread. */
571 struct dp_netdev_pmd_thread_ctx ctx
;
573 struct latch exit_latch
; /* For terminating the pmd thread. */
574 struct seq
*reload_seq
;
575 uint64_t last_reload_seq
;
576 atomic_bool reload
; /* Do we need to reload ports? */
578 unsigned core_id
; /* CPU core id of this pmd thread. */
579 int numa_id
; /* numa node id of this pmd thread. */
582 /* Queue id used by this pmd thread to send packets on all netdevs if
583 * XPS disabled for this netdev. All static_tx_qid's are unique and less
584 * than 'cmap_count(dp->poll_threads)'. */
585 uint32_t static_tx_qid
;
587 /* Number of filled output batches. */
588 int n_output_batches
;
590 struct ovs_mutex port_mutex
; /* Mutex for 'poll_list' and 'tx_ports'. */
591 /* List of rx queues to poll. */
592 struct hmap poll_list OVS_GUARDED
;
593 /* Map of 'tx_port's used for transmission. Written by the main thread,
594 * read by the pmd thread. */
595 struct hmap tx_ports OVS_GUARDED
;
597 /* These are thread-local copies of 'tx_ports'. One contains only tunnel
598 * ports (that support push_tunnel/pop_tunnel), the other contains ports
599 * with at least one txq (that support send). A port can be in both.
601 * There are two separate maps to make sure that we don't try to execute
602 * OUTPUT on a device which has 0 txqs or PUSH/POP on a non-tunnel device.
604 * The instances for cpu core NON_PMD_CORE_ID can be accessed by multiple
605 * threads, and thusly need to be protected by 'non_pmd_mutex'. Every
606 * other instance will only be accessed by its own pmd thread. */
607 struct hmap tnl_port_cache
;
608 struct hmap send_port_cache
;
610 /* Keep track of detailed PMD performance statistics. */
611 struct pmd_perf_stats perf_stats
;
613 /* Set to true if the pmd thread needs to be reloaded. */
617 /* Interface to netdev-based datapath. */
620 struct dp_netdev
*dp
;
621 uint64_t last_port_seq
;
624 static int get_port_by_number(struct dp_netdev
*dp
, odp_port_t port_no
,
625 struct dp_netdev_port
**portp
)
626 OVS_REQUIRES(dp
->port_mutex
);
627 static int get_port_by_name(struct dp_netdev
*dp
, const char *devname
,
628 struct dp_netdev_port
**portp
)
629 OVS_REQUIRES(dp
->port_mutex
);
630 static void dp_netdev_free(struct dp_netdev
*)
631 OVS_REQUIRES(dp_netdev_mutex
);
632 static int do_add_port(struct dp_netdev
*dp
, const char *devname
,
633 const char *type
, odp_port_t port_no
)
634 OVS_REQUIRES(dp
->port_mutex
);
635 static void do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*)
636 OVS_REQUIRES(dp
->port_mutex
);
637 static int dpif_netdev_open(const struct dpif_class
*, const char *name
,
638 bool create
, struct dpif
**);
639 static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
640 struct dp_packet_batch
*,
641 bool may_steal
, const struct flow
*flow
,
642 const struct nlattr
*actions
,
644 static void dp_netdev_input(struct dp_netdev_pmd_thread
*,
645 struct dp_packet_batch
*, odp_port_t port_no
);
646 static void dp_netdev_recirculate(struct dp_netdev_pmd_thread
*,
647 struct dp_packet_batch
*);
649 static void dp_netdev_disable_upcall(struct dp_netdev
*);
650 static void dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
);
651 static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
,
652 struct dp_netdev
*dp
, unsigned core_id
,
654 static void dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
);
655 static void dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
656 OVS_REQUIRES(dp
->port_mutex
);
658 static void *pmd_thread_main(void *);
659 static struct dp_netdev_pmd_thread
*dp_netdev_get_pmd(struct dp_netdev
*dp
,
661 static struct dp_netdev_pmd_thread
*
662 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
);
663 static void dp_netdev_del_pmd(struct dp_netdev
*dp
,
664 struct dp_netdev_pmd_thread
*pmd
);
665 static void dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
, bool non_pmd
);
666 static void dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
);
667 static void dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
668 struct dp_netdev_port
*port
)
669 OVS_REQUIRES(pmd
->port_mutex
);
670 static void dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
672 OVS_REQUIRES(pmd
->port_mutex
);
673 static void dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
674 struct dp_netdev_rxq
*rxq
)
675 OVS_REQUIRES(pmd
->port_mutex
);
676 static void dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
677 struct rxq_poll
*poll
)
678 OVS_REQUIRES(pmd
->port_mutex
);
680 dp_netdev_pmd_flush_output_packets(struct dp_netdev_pmd_thread
*pmd
,
683 static void reconfigure_datapath(struct dp_netdev
*dp
)
684 OVS_REQUIRES(dp
->port_mutex
);
685 static bool dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
);
686 static void dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
);
687 static void dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
);
688 static void pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
689 OVS_REQUIRES(pmd
->port_mutex
);
691 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread
*pmd
,
692 struct polled_queue
*poll_list
, int poll_cnt
);
694 dp_netdev_rxq_set_cycles(struct dp_netdev_rxq
*rx
,
695 enum rxq_cycles_counter_type type
,
696 unsigned long long cycles
);
698 dp_netdev_rxq_get_cycles(struct dp_netdev_rxq
*rx
,
699 enum rxq_cycles_counter_type type
);
701 dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq
*rx
,
702 unsigned long long cycles
);
704 dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq
*rx
, unsigned idx
);
706 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
708 static int dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
711 static inline bool emc_entry_alive(struct emc_entry
*ce
);
712 static void emc_clear_entry(struct emc_entry
*ce
);
714 static void dp_netdev_request_reconfigure(struct dp_netdev
*dp
);
717 emc_cache_init(struct emc_cache
*flow_cache
)
721 flow_cache
->sweep_idx
= 0;
722 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
723 flow_cache
->entries
[i
].flow
= NULL
;
724 flow_cache
->entries
[i
].key
.hash
= 0;
725 flow_cache
->entries
[i
].key
.len
= sizeof(struct miniflow
);
726 flowmap_init(&flow_cache
->entries
[i
].key
.mf
.map
);
731 emc_cache_uninit(struct emc_cache
*flow_cache
)
735 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
736 emc_clear_entry(&flow_cache
->entries
[i
]);
740 /* Check and clear dead flow references slowly (one entry at each
743 emc_cache_slow_sweep(struct emc_cache
*flow_cache
)
745 struct emc_entry
*entry
= &flow_cache
->entries
[flow_cache
->sweep_idx
];
747 if (!emc_entry_alive(entry
)) {
748 emc_clear_entry(entry
);
750 flow_cache
->sweep_idx
= (flow_cache
->sweep_idx
+ 1) & EM_FLOW_HASH_MASK
;
753 /* Updates the time in PMD threads context and should be called in three cases:
755 * 1. PMD structure initialization:
756 * - dp_netdev_configure_pmd()
758 * 2. Before processing of the new packet batch:
759 * - dpif_netdev_execute()
760 * - dp_netdev_process_rxq_port()
762 * 3. At least once per polling iteration in main polling threads if no
763 * packets received on current iteration:
764 * - dpif_netdev_run()
765 * - pmd_thread_main()
767 * 'pmd->ctx.now' should be used without update in all other cases if possible.
770 pmd_thread_ctx_time_update(struct dp_netdev_pmd_thread
*pmd
)
772 pmd
->ctx
.now
= time_usec();
775 /* Returns true if 'dpif' is a netdev or dummy dpif, false otherwise. */
777 dpif_is_netdev(const struct dpif
*dpif
)
779 return dpif
->dpif_class
->open
== dpif_netdev_open
;
782 static struct dpif_netdev
*
783 dpif_netdev_cast(const struct dpif
*dpif
)
785 ovs_assert(dpif_is_netdev(dpif
));
786 return CONTAINER_OF(dpif
, struct dpif_netdev
, dpif
);
789 static struct dp_netdev
*
790 get_dp_netdev(const struct dpif
*dpif
)
792 return dpif_netdev_cast(dpif
)->dp
;
796 PMD_INFO_SHOW_STATS
, /* Show how cpu cycles are spent. */
797 PMD_INFO_CLEAR_STATS
, /* Set the cycles count to 0. */
798 PMD_INFO_SHOW_RXQ
/* Show poll-lists of pmd threads. */
802 format_pmd_thread(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
804 ds_put_cstr(reply
, (pmd
->core_id
== NON_PMD_CORE_ID
)
805 ? "main thread" : "pmd thread");
806 if (pmd
->numa_id
!= OVS_NUMA_UNSPEC
) {
807 ds_put_format(reply
, " numa_id %d", pmd
->numa_id
);
809 if (pmd
->core_id
!= OVS_CORE_UNSPEC
&& pmd
->core_id
!= NON_PMD_CORE_ID
) {
810 ds_put_format(reply
, " core_id %u", pmd
->core_id
);
812 ds_put_cstr(reply
, ":\n");
816 pmd_info_show_stats(struct ds
*reply
,
817 struct dp_netdev_pmd_thread
*pmd
)
819 uint64_t stats
[PMD_N_STATS
];
820 uint64_t total_cycles
, total_packets
;
821 double passes_per_pkt
= 0;
822 double lookups_per_hit
= 0;
823 double packets_per_batch
= 0;
825 pmd_perf_read_counters(&pmd
->perf_stats
, stats
);
826 total_cycles
= stats
[PMD_CYCLES_ITER_IDLE
]
827 + stats
[PMD_CYCLES_ITER_BUSY
];
828 total_packets
= stats
[PMD_STAT_RECV
];
830 format_pmd_thread(reply
, pmd
);
832 if (total_packets
> 0) {
833 passes_per_pkt
= (total_packets
+ stats
[PMD_STAT_RECIRC
])
834 / (double) total_packets
;
836 if (stats
[PMD_STAT_MASKED_HIT
] > 0) {
837 lookups_per_hit
= stats
[PMD_STAT_MASKED_LOOKUP
]
838 / (double) stats
[PMD_STAT_MASKED_HIT
];
840 if (stats
[PMD_STAT_SENT_BATCHES
] > 0) {
841 packets_per_batch
= stats
[PMD_STAT_SENT_PKTS
]
842 / (double) stats
[PMD_STAT_SENT_BATCHES
];
846 "\tpackets received: %"PRIu64
"\n"
847 "\tpacket recirculations: %"PRIu64
"\n"
848 "\tavg. datapath passes per packet: %.02f\n"
849 "\temc hits: %"PRIu64
"\n"
850 "\tmegaflow hits: %"PRIu64
"\n"
851 "\tavg. subtable lookups per megaflow hit: %.02f\n"
852 "\tmiss with success upcall: %"PRIu64
"\n"
853 "\tmiss with failed upcall: %"PRIu64
"\n"
854 "\tavg. packets per output batch: %.02f\n",
855 total_packets
, stats
[PMD_STAT_RECIRC
],
856 passes_per_pkt
, stats
[PMD_STAT_EXACT_HIT
],
857 stats
[PMD_STAT_MASKED_HIT
], lookups_per_hit
,
858 stats
[PMD_STAT_MISS
], stats
[PMD_STAT_LOST
],
861 if (total_cycles
== 0) {
866 "\tidle cycles: %"PRIu64
" (%.02f%%)\n"
867 "\tprocessing cycles: %"PRIu64
" (%.02f%%)\n",
868 stats
[PMD_CYCLES_ITER_IDLE
],
869 stats
[PMD_CYCLES_ITER_IDLE
] / (double) total_cycles
* 100,
870 stats
[PMD_CYCLES_ITER_BUSY
],
871 stats
[PMD_CYCLES_ITER_BUSY
] / (double) total_cycles
* 100);
873 if (total_packets
== 0) {
878 "\tavg cycles per packet: %.02f (%"PRIu64
"/%"PRIu64
")\n",
879 total_cycles
/ (double) total_packets
,
880 total_cycles
, total_packets
);
883 "\tavg processing cycles per packet: "
884 "%.02f (%"PRIu64
"/%"PRIu64
")\n",
885 stats
[PMD_CYCLES_ITER_BUSY
] / (double) total_packets
,
886 stats
[PMD_CYCLES_ITER_BUSY
], total_packets
);
890 compare_poll_list(const void *a_
, const void *b_
)
892 const struct rxq_poll
*a
= a_
;
893 const struct rxq_poll
*b
= b_
;
895 const char *namea
= netdev_rxq_get_name(a
->rxq
->rx
);
896 const char *nameb
= netdev_rxq_get_name(b
->rxq
->rx
);
898 int cmp
= strcmp(namea
, nameb
);
900 return netdev_rxq_get_queue_id(a
->rxq
->rx
)
901 - netdev_rxq_get_queue_id(b
->rxq
->rx
);
908 sorted_poll_list(struct dp_netdev_pmd_thread
*pmd
, struct rxq_poll
**list
,
911 struct rxq_poll
*ret
, *poll
;
914 *n
= hmap_count(&pmd
->poll_list
);
918 ret
= xcalloc(*n
, sizeof *ret
);
920 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
925 qsort(ret
, *n
, sizeof *ret
, compare_poll_list
);
932 pmd_info_show_rxq(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
934 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
935 const char *prev_name
= NULL
;
936 struct rxq_poll
*list
;
940 "pmd thread numa_id %d core_id %u:\n\tisolated : %s\n",
941 pmd
->numa_id
, pmd
->core_id
, (pmd
->isolated
)
944 ovs_mutex_lock(&pmd
->port_mutex
);
945 sorted_poll_list(pmd
, &list
, &n
);
946 for (i
= 0; i
< n
; i
++) {
947 const char *name
= netdev_rxq_get_name(list
[i
].rxq
->rx
);
949 if (!prev_name
|| strcmp(name
, prev_name
)) {
951 ds_put_cstr(reply
, "\n");
953 ds_put_format(reply
, "\tport: %s\tqueue-id:", name
);
955 ds_put_format(reply
, " %d",
956 netdev_rxq_get_queue_id(list
[i
].rxq
->rx
));
959 ovs_mutex_unlock(&pmd
->port_mutex
);
960 ds_put_cstr(reply
, "\n");
966 compare_poll_thread_list(const void *a_
, const void *b_
)
968 const struct dp_netdev_pmd_thread
*a
, *b
;
970 a
= *(struct dp_netdev_pmd_thread
**)a_
;
971 b
= *(struct dp_netdev_pmd_thread
**)b_
;
973 if (a
->core_id
< b
->core_id
) {
976 if (a
->core_id
> b
->core_id
) {
982 /* Create a sorted list of pmd's from the dp->poll_threads cmap. We can use
983 * this list, as long as we do not go to quiescent state. */
985 sorted_poll_thread_list(struct dp_netdev
*dp
,
986 struct dp_netdev_pmd_thread
***list
,
989 struct dp_netdev_pmd_thread
*pmd
;
990 struct dp_netdev_pmd_thread
**pmd_list
;
991 size_t k
= 0, n_pmds
;
993 n_pmds
= cmap_count(&dp
->poll_threads
);
994 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
996 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1000 pmd_list
[k
++] = pmd
;
1003 qsort(pmd_list
, k
, sizeof *pmd_list
, compare_poll_thread_list
);
1010 dpif_netdev_pmd_rebalance(struct unixctl_conn
*conn
, int argc
,
1011 const char *argv
[], void *aux OVS_UNUSED
)
1013 struct ds reply
= DS_EMPTY_INITIALIZER
;
1014 struct dp_netdev
*dp
= NULL
;
1016 ovs_mutex_lock(&dp_netdev_mutex
);
1019 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
1020 } else if (shash_count(&dp_netdevs
) == 1) {
1021 /* There's only one datapath */
1022 dp
= shash_first(&dp_netdevs
)->data
;
1026 ovs_mutex_unlock(&dp_netdev_mutex
);
1027 unixctl_command_reply_error(conn
,
1028 "please specify an existing datapath");
1032 dp_netdev_request_reconfigure(dp
);
1033 ovs_mutex_unlock(&dp_netdev_mutex
);
1034 ds_put_cstr(&reply
, "pmd rxq rebalance requested.\n");
1035 unixctl_command_reply(conn
, ds_cstr(&reply
));
1040 dpif_netdev_pmd_info(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
1043 struct ds reply
= DS_EMPTY_INITIALIZER
;
1044 struct dp_netdev_pmd_thread
**pmd_list
;
1045 struct dp_netdev
*dp
= NULL
;
1046 enum pmd_info_type type
= *(enum pmd_info_type
*) aux
;
1047 unsigned int core_id
;
1048 bool filter_on_pmd
= false;
1051 ovs_mutex_lock(&dp_netdev_mutex
);
1054 if (!strcmp(argv
[1], "-pmd") && argc
>= 3) {
1055 if (str_to_uint(argv
[2], 10, &core_id
)) {
1056 filter_on_pmd
= true;
1061 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
1068 if (shash_count(&dp_netdevs
) == 1) {
1069 /* There's only one datapath */
1070 dp
= shash_first(&dp_netdevs
)->data
;
1072 ovs_mutex_unlock(&dp_netdev_mutex
);
1073 unixctl_command_reply_error(conn
,
1074 "please specify an existing datapath");
1079 sorted_poll_thread_list(dp
, &pmd_list
, &n
);
1080 for (size_t i
= 0; i
< n
; i
++) {
1081 struct dp_netdev_pmd_thread
*pmd
= pmd_list
[i
];
1085 if (filter_on_pmd
&& pmd
->core_id
!= core_id
) {
1088 if (type
== PMD_INFO_SHOW_RXQ
) {
1089 pmd_info_show_rxq(&reply
, pmd
);
1090 } else if (type
== PMD_INFO_CLEAR_STATS
) {
1091 pmd_perf_stats_clear(&pmd
->perf_stats
);
1092 } else if (type
== PMD_INFO_SHOW_STATS
) {
1093 pmd_info_show_stats(&reply
, pmd
);
1098 ovs_mutex_unlock(&dp_netdev_mutex
);
1100 unixctl_command_reply(conn
, ds_cstr(&reply
));
1105 dpif_netdev_init(void)
1107 static enum pmd_info_type show_aux
= PMD_INFO_SHOW_STATS
,
1108 clear_aux
= PMD_INFO_CLEAR_STATS
,
1109 poll_aux
= PMD_INFO_SHOW_RXQ
;
1111 unixctl_command_register("dpif-netdev/pmd-stats-show", "[-pmd core] [dp]",
1112 0, 3, dpif_netdev_pmd_info
,
1114 unixctl_command_register("dpif-netdev/pmd-stats-clear", "[-pmd core] [dp]",
1115 0, 3, dpif_netdev_pmd_info
,
1116 (void *)&clear_aux
);
1117 unixctl_command_register("dpif-netdev/pmd-rxq-show", "[-pmd core] [dp]",
1118 0, 3, dpif_netdev_pmd_info
,
1120 unixctl_command_register("dpif-netdev/pmd-rxq-rebalance", "[dp]",
1121 0, 1, dpif_netdev_pmd_rebalance
,
1127 dpif_netdev_enumerate(struct sset
*all_dps
,
1128 const struct dpif_class
*dpif_class
)
1130 struct shash_node
*node
;
1132 ovs_mutex_lock(&dp_netdev_mutex
);
1133 SHASH_FOR_EACH(node
, &dp_netdevs
) {
1134 struct dp_netdev
*dp
= node
->data
;
1135 if (dpif_class
!= dp
->class) {
1136 /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs.
1137 * If the class doesn't match, skip this dpif. */
1140 sset_add(all_dps
, node
->name
);
1142 ovs_mutex_unlock(&dp_netdev_mutex
);
1148 dpif_netdev_class_is_dummy(const struct dpif_class
*class)
1150 return class != &dpif_netdev_class
;
1154 dpif_netdev_port_open_type(const struct dpif_class
*class, const char *type
)
1156 return strcmp(type
, "internal") ? type
1157 : dpif_netdev_class_is_dummy(class) ? "dummy-internal"
1161 static struct dpif
*
1162 create_dpif_netdev(struct dp_netdev
*dp
)
1164 uint16_t netflow_id
= hash_string(dp
->name
, 0);
1165 struct dpif_netdev
*dpif
;
1167 ovs_refcount_ref(&dp
->ref_cnt
);
1169 dpif
= xmalloc(sizeof *dpif
);
1170 dpif_init(&dpif
->dpif
, dp
->class, dp
->name
, netflow_id
>> 8, netflow_id
);
1172 dpif
->last_port_seq
= seq_read(dp
->port_seq
);
1177 /* Choose an unused, non-zero port number and return it on success.
1178 * Return ODPP_NONE on failure. */
1180 choose_port(struct dp_netdev
*dp
, const char *name
)
1181 OVS_REQUIRES(dp
->port_mutex
)
1185 if (dp
->class != &dpif_netdev_class
) {
1189 /* If the port name begins with "br", start the number search at
1190 * 100 to make writing tests easier. */
1191 if (!strncmp(name
, "br", 2)) {
1195 /* If the port name contains a number, try to assign that port number.
1196 * This can make writing unit tests easier because port numbers are
1198 for (p
= name
; *p
!= '\0'; p
++) {
1199 if (isdigit((unsigned char) *p
)) {
1200 port_no
= start_no
+ strtol(p
, NULL
, 10);
1201 if (port_no
> 0 && port_no
!= odp_to_u32(ODPP_NONE
)
1202 && !dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
1203 return u32_to_odp(port_no
);
1210 for (port_no
= 1; port_no
<= UINT16_MAX
; port_no
++) {
1211 if (!dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
1212 return u32_to_odp(port_no
);
1220 create_dp_netdev(const char *name
, const struct dpif_class
*class,
1221 struct dp_netdev
**dpp
)
1222 OVS_REQUIRES(dp_netdev_mutex
)
1224 struct dp_netdev
*dp
;
1227 dp
= xzalloc(sizeof *dp
);
1228 shash_add(&dp_netdevs
, name
, dp
);
1230 *CONST_CAST(const struct dpif_class
**, &dp
->class) = class;
1231 *CONST_CAST(const char **, &dp
->name
) = xstrdup(name
);
1232 ovs_refcount_init(&dp
->ref_cnt
);
1233 atomic_flag_clear(&dp
->destroyed
);
1235 ovs_mutex_init(&dp
->port_mutex
);
1236 hmap_init(&dp
->ports
);
1237 dp
->port_seq
= seq_create();
1238 fat_rwlock_init(&dp
->upcall_rwlock
);
1240 dp
->reconfigure_seq
= seq_create();
1241 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
1243 for (int i
= 0; i
< N_METER_LOCKS
; ++i
) {
1244 ovs_mutex_init_adaptive(&dp
->meter_locks
[i
]);
1247 /* Disable upcalls by default. */
1248 dp_netdev_disable_upcall(dp
);
1249 dp
->upcall_aux
= NULL
;
1250 dp
->upcall_cb
= NULL
;
1252 conntrack_init(&dp
->conntrack
);
1254 atomic_init(&dp
->emc_insert_min
, DEFAULT_EM_FLOW_INSERT_MIN
);
1255 atomic_init(&dp
->tx_flush_interval
, DEFAULT_TX_FLUSH_INTERVAL
);
1257 cmap_init(&dp
->poll_threads
);
1259 ovs_mutex_init(&dp
->tx_qid_pool_mutex
);
1260 /* We need 1 Tx queue for each possible core + 1 for non-PMD threads. */
1261 dp
->tx_qid_pool
= id_pool_create(0, ovs_numa_get_n_cores() + 1);
1263 ovs_mutex_init_recursive(&dp
->non_pmd_mutex
);
1264 ovsthread_key_create(&dp
->per_pmd_key
, NULL
);
1266 ovs_mutex_lock(&dp
->port_mutex
);
1267 /* non-PMD will be created before all other threads and will
1268 * allocate static_tx_qid = 0. */
1269 dp_netdev_set_nonpmd(dp
);
1271 error
= do_add_port(dp
, name
, dpif_netdev_port_open_type(dp
->class,
1274 ovs_mutex_unlock(&dp
->port_mutex
);
1280 dp
->last_tnl_conf_seq
= seq_read(tnl_conf_seq
);
1286 dp_netdev_request_reconfigure(struct dp_netdev
*dp
)
1288 seq_change(dp
->reconfigure_seq
);
1292 dp_netdev_is_reconf_required(struct dp_netdev
*dp
)
1294 return seq_read(dp
->reconfigure_seq
) != dp
->last_reconfigure_seq
;
1298 dpif_netdev_open(const struct dpif_class
*class, const char *name
,
1299 bool create
, struct dpif
**dpifp
)
1301 struct dp_netdev
*dp
;
1304 ovs_mutex_lock(&dp_netdev_mutex
);
1305 dp
= shash_find_data(&dp_netdevs
, name
);
1307 error
= create
? create_dp_netdev(name
, class, &dp
) : ENODEV
;
1309 error
= (dp
->class != class ? EINVAL
1314 *dpifp
= create_dpif_netdev(dp
);
1317 ovs_mutex_unlock(&dp_netdev_mutex
);
1323 dp_netdev_destroy_upcall_lock(struct dp_netdev
*dp
)
1324 OVS_NO_THREAD_SAFETY_ANALYSIS
1326 /* Check that upcalls are disabled, i.e. that the rwlock is taken */
1327 ovs_assert(fat_rwlock_tryrdlock(&dp
->upcall_rwlock
));
1329 /* Before freeing a lock we should release it */
1330 fat_rwlock_unlock(&dp
->upcall_rwlock
);
1331 fat_rwlock_destroy(&dp
->upcall_rwlock
);
1335 dp_delete_meter(struct dp_netdev
*dp
, uint32_t meter_id
)
1336 OVS_REQUIRES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
1338 if (dp
->meters
[meter_id
]) {
1339 free(dp
->meters
[meter_id
]);
1340 dp
->meters
[meter_id
] = NULL
;
1344 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
1345 * through the 'dp_netdevs' shash while freeing 'dp'. */
1347 dp_netdev_free(struct dp_netdev
*dp
)
1348 OVS_REQUIRES(dp_netdev_mutex
)
1350 struct dp_netdev_port
*port
, *next
;
1352 shash_find_and_delete(&dp_netdevs
, dp
->name
);
1354 ovs_mutex_lock(&dp
->port_mutex
);
1355 HMAP_FOR_EACH_SAFE (port
, next
, node
, &dp
->ports
) {
1356 do_del_port(dp
, port
);
1358 ovs_mutex_unlock(&dp
->port_mutex
);
1360 dp_netdev_destroy_all_pmds(dp
, true);
1361 cmap_destroy(&dp
->poll_threads
);
1363 ovs_mutex_destroy(&dp
->tx_qid_pool_mutex
);
1364 id_pool_destroy(dp
->tx_qid_pool
);
1366 ovs_mutex_destroy(&dp
->non_pmd_mutex
);
1367 ovsthread_key_delete(dp
->per_pmd_key
);
1369 conntrack_destroy(&dp
->conntrack
);
1372 seq_destroy(dp
->reconfigure_seq
);
1374 seq_destroy(dp
->port_seq
);
1375 hmap_destroy(&dp
->ports
);
1376 ovs_mutex_destroy(&dp
->port_mutex
);
1378 /* Upcalls must be disabled at this point */
1379 dp_netdev_destroy_upcall_lock(dp
);
1383 for (i
= 0; i
< MAX_METERS
; ++i
) {
1385 dp_delete_meter(dp
, i
);
1386 meter_unlock(dp
, i
);
1388 for (i
= 0; i
< N_METER_LOCKS
; ++i
) {
1389 ovs_mutex_destroy(&dp
->meter_locks
[i
]);
1392 free(dp
->pmd_cmask
);
1393 free(CONST_CAST(char *, dp
->name
));
1398 dp_netdev_unref(struct dp_netdev
*dp
)
1401 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
1402 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
1403 ovs_mutex_lock(&dp_netdev_mutex
);
1404 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1407 ovs_mutex_unlock(&dp_netdev_mutex
);
1412 dpif_netdev_close(struct dpif
*dpif
)
1414 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1416 dp_netdev_unref(dp
);
1421 dpif_netdev_destroy(struct dpif
*dpif
)
1423 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1425 if (!atomic_flag_test_and_set(&dp
->destroyed
)) {
1426 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1427 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
1435 /* Add 'n' to the atomic variable 'var' non-atomically and using relaxed
1436 * load/store semantics. While the increment is not atomic, the load and
1437 * store operations are, making it impossible to read inconsistent values.
1439 * This is used to update thread local stats counters. */
1441 non_atomic_ullong_add(atomic_ullong
*var
, unsigned long long n
)
1443 unsigned long long tmp
;
1445 atomic_read_relaxed(var
, &tmp
);
1447 atomic_store_relaxed(var
, tmp
);
1451 dpif_netdev_get_stats(const struct dpif
*dpif
, struct dpif_dp_stats
*stats
)
1453 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1454 struct dp_netdev_pmd_thread
*pmd
;
1455 uint64_t pmd_stats
[PMD_N_STATS
];
1457 stats
->n_flows
= stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
1458 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1459 stats
->n_flows
+= cmap_count(&pmd
->flow_table
);
1460 pmd_perf_read_counters(&pmd
->perf_stats
, pmd_stats
);
1461 stats
->n_hit
+= pmd_stats
[PMD_STAT_EXACT_HIT
];
1462 stats
->n_hit
+= pmd_stats
[PMD_STAT_MASKED_HIT
];
1463 stats
->n_missed
+= pmd_stats
[PMD_STAT_MISS
];
1464 stats
->n_lost
+= pmd_stats
[PMD_STAT_LOST
];
1466 stats
->n_masks
= UINT32_MAX
;
1467 stats
->n_mask_hit
= UINT64_MAX
;
1473 dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread
*pmd
)
1475 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
1476 ovs_mutex_lock(&pmd
->dp
->non_pmd_mutex
);
1477 ovs_mutex_lock(&pmd
->port_mutex
);
1478 pmd_load_cached_ports(pmd
);
1479 ovs_mutex_unlock(&pmd
->port_mutex
);
1480 ovs_mutex_unlock(&pmd
->dp
->non_pmd_mutex
);
1484 ovs_mutex_lock(&pmd
->cond_mutex
);
1485 seq_change(pmd
->reload_seq
);
1486 atomic_store_relaxed(&pmd
->reload
, true);
1487 ovs_mutex_cond_wait(&pmd
->cond
, &pmd
->cond_mutex
);
1488 ovs_mutex_unlock(&pmd
->cond_mutex
);
1492 hash_port_no(odp_port_t port_no
)
1494 return hash_int(odp_to_u32(port_no
), 0);
1498 port_create(const char *devname
, const char *type
,
1499 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1501 struct netdev_saved_flags
*sf
;
1502 struct dp_netdev_port
*port
;
1503 enum netdev_flags flags
;
1504 struct netdev
*netdev
;
1509 /* Open and validate network device. */
1510 error
= netdev_open(devname
, type
, &netdev
);
1514 /* XXX reject non-Ethernet devices */
1516 netdev_get_flags(netdev
, &flags
);
1517 if (flags
& NETDEV_LOOPBACK
) {
1518 VLOG_ERR("%s: cannot add a loopback device", devname
);
1523 error
= netdev_turn_flags_on(netdev
, NETDEV_PROMISC
, &sf
);
1525 VLOG_ERR("%s: cannot set promisc flag", devname
);
1529 port
= xzalloc(sizeof *port
);
1530 port
->port_no
= port_no
;
1531 port
->netdev
= netdev
;
1532 port
->type
= xstrdup(type
);
1534 port
->need_reconfigure
= true;
1535 ovs_mutex_init(&port
->txq_used_mutex
);
1542 netdev_close(netdev
);
1547 do_add_port(struct dp_netdev
*dp
, const char *devname
, const char *type
,
1549 OVS_REQUIRES(dp
->port_mutex
)
1551 struct dp_netdev_port
*port
;
1554 /* Reject devices already in 'dp'. */
1555 if (!get_port_by_name(dp
, devname
, &port
)) {
1559 error
= port_create(devname
, type
, port_no
, &port
);
1564 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
1565 seq_change(dp
->port_seq
);
1567 reconfigure_datapath(dp
);
1573 dpif_netdev_port_add(struct dpif
*dpif
, struct netdev
*netdev
,
1574 odp_port_t
*port_nop
)
1576 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1577 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1578 const char *dpif_port
;
1582 ovs_mutex_lock(&dp
->port_mutex
);
1583 dpif_port
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1584 if (*port_nop
!= ODPP_NONE
) {
1585 port_no
= *port_nop
;
1586 error
= dp_netdev_lookup_port(dp
, *port_nop
) ? EBUSY
: 0;
1588 port_no
= choose_port(dp
, dpif_port
);
1589 error
= port_no
== ODPP_NONE
? EFBIG
: 0;
1592 *port_nop
= port_no
;
1593 error
= do_add_port(dp
, dpif_port
, netdev_get_type(netdev
), port_no
);
1595 ovs_mutex_unlock(&dp
->port_mutex
);
1601 dpif_netdev_port_del(struct dpif
*dpif
, odp_port_t port_no
)
1603 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1606 ovs_mutex_lock(&dp
->port_mutex
);
1607 if (port_no
== ODPP_LOCAL
) {
1610 struct dp_netdev_port
*port
;
1612 error
= get_port_by_number(dp
, port_no
, &port
);
1614 do_del_port(dp
, port
);
1617 ovs_mutex_unlock(&dp
->port_mutex
);
1623 is_valid_port_number(odp_port_t port_no
)
1625 return port_no
!= ODPP_NONE
;
1628 static struct dp_netdev_port
*
1629 dp_netdev_lookup_port(const struct dp_netdev
*dp
, odp_port_t port_no
)
1630 OVS_REQUIRES(dp
->port_mutex
)
1632 struct dp_netdev_port
*port
;
1634 HMAP_FOR_EACH_WITH_HASH (port
, node
, hash_port_no(port_no
), &dp
->ports
) {
1635 if (port
->port_no
== port_no
) {
1643 get_port_by_number(struct dp_netdev
*dp
,
1644 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1645 OVS_REQUIRES(dp
->port_mutex
)
1647 if (!is_valid_port_number(port_no
)) {
1651 *portp
= dp_netdev_lookup_port(dp
, port_no
);
1652 return *portp
? 0 : ENODEV
;
1657 port_destroy(struct dp_netdev_port
*port
)
1663 netdev_close(port
->netdev
);
1664 netdev_restore_flags(port
->sf
);
1666 for (unsigned i
= 0; i
< port
->n_rxq
; i
++) {
1667 netdev_rxq_close(port
->rxqs
[i
].rx
);
1669 ovs_mutex_destroy(&port
->txq_used_mutex
);
1670 free(port
->rxq_affinity_list
);
1671 free(port
->txq_used
);
1678 get_port_by_name(struct dp_netdev
*dp
,
1679 const char *devname
, struct dp_netdev_port
**portp
)
1680 OVS_REQUIRES(dp
->port_mutex
)
1682 struct dp_netdev_port
*port
;
1684 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1685 if (!strcmp(netdev_get_name(port
->netdev
), devname
)) {
1691 /* Callers of dpif_netdev_port_query_by_name() expect ENODEV for a non
1696 /* Returns 'true' if there is a port with pmd netdev. */
1698 has_pmd_port(struct dp_netdev
*dp
)
1699 OVS_REQUIRES(dp
->port_mutex
)
1701 struct dp_netdev_port
*port
;
1703 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1704 if (netdev_is_pmd(port
->netdev
)) {
1713 do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*port
)
1714 OVS_REQUIRES(dp
->port_mutex
)
1716 hmap_remove(&dp
->ports
, &port
->node
);
1717 seq_change(dp
->port_seq
);
1719 reconfigure_datapath(dp
);
1725 answer_port_query(const struct dp_netdev_port
*port
,
1726 struct dpif_port
*dpif_port
)
1728 dpif_port
->name
= xstrdup(netdev_get_name(port
->netdev
));
1729 dpif_port
->type
= xstrdup(port
->type
);
1730 dpif_port
->port_no
= port
->port_no
;
1734 dpif_netdev_port_query_by_number(const struct dpif
*dpif
, odp_port_t port_no
,
1735 struct dpif_port
*dpif_port
)
1737 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1738 struct dp_netdev_port
*port
;
1741 ovs_mutex_lock(&dp
->port_mutex
);
1742 error
= get_port_by_number(dp
, port_no
, &port
);
1743 if (!error
&& dpif_port
) {
1744 answer_port_query(port
, dpif_port
);
1746 ovs_mutex_unlock(&dp
->port_mutex
);
1752 dpif_netdev_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
1753 struct dpif_port
*dpif_port
)
1755 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1756 struct dp_netdev_port
*port
;
1759 ovs_mutex_lock(&dp
->port_mutex
);
1760 error
= get_port_by_name(dp
, devname
, &port
);
1761 if (!error
&& dpif_port
) {
1762 answer_port_query(port
, dpif_port
);
1764 ovs_mutex_unlock(&dp
->port_mutex
);
1770 dp_netdev_flow_free(struct dp_netdev_flow
*flow
)
1772 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow
));
1776 static void dp_netdev_flow_unref(struct dp_netdev_flow
*flow
)
1778 if (ovs_refcount_unref_relaxed(&flow
->ref_cnt
) == 1) {
1779 ovsrcu_postpone(dp_netdev_flow_free
, flow
);
1784 dp_netdev_flow_hash(const ovs_u128
*ufid
)
1786 return ufid
->u32
[0];
1789 static inline struct dpcls
*
1790 dp_netdev_pmd_lookup_dpcls(struct dp_netdev_pmd_thread
*pmd
,
1794 uint32_t hash
= hash_port_no(in_port
);
1795 CMAP_FOR_EACH_WITH_HASH (cls
, node
, hash
, &pmd
->classifiers
) {
1796 if (cls
->in_port
== in_port
) {
1797 /* Port classifier exists already */
1804 static inline struct dpcls
*
1805 dp_netdev_pmd_find_dpcls(struct dp_netdev_pmd_thread
*pmd
,
1807 OVS_REQUIRES(pmd
->flow_mutex
)
1809 struct dpcls
*cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
1810 uint32_t hash
= hash_port_no(in_port
);
1813 /* Create new classifier for in_port */
1814 cls
= xmalloc(sizeof(*cls
));
1816 cls
->in_port
= in_port
;
1817 cmap_insert(&pmd
->classifiers
, &cls
->node
, hash
);
1818 VLOG_DBG("Creating dpcls %p for in_port %d", cls
, in_port
);
1824 dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread
*pmd
,
1825 struct dp_netdev_flow
*flow
)
1826 OVS_REQUIRES(pmd
->flow_mutex
)
1828 struct cmap_node
*node
= CONST_CAST(struct cmap_node
*, &flow
->node
);
1830 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
1832 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
1833 ovs_assert(cls
!= NULL
);
1834 dpcls_remove(cls
, &flow
->cr
);
1835 cmap_remove(&pmd
->flow_table
, node
, dp_netdev_flow_hash(&flow
->ufid
));
1838 dp_netdev_flow_unref(flow
);
1842 dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
)
1844 struct dp_netdev_flow
*netdev_flow
;
1846 ovs_mutex_lock(&pmd
->flow_mutex
);
1847 CMAP_FOR_EACH (netdev_flow
, node
, &pmd
->flow_table
) {
1848 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
1850 ovs_mutex_unlock(&pmd
->flow_mutex
);
1854 dpif_netdev_flow_flush(struct dpif
*dpif
)
1856 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1857 struct dp_netdev_pmd_thread
*pmd
;
1859 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1860 dp_netdev_pmd_flow_flush(pmd
);
1866 struct dp_netdev_port_state
{
1867 struct hmap_position position
;
1872 dpif_netdev_port_dump_start(const struct dpif
*dpif OVS_UNUSED
, void **statep
)
1874 *statep
= xzalloc(sizeof(struct dp_netdev_port_state
));
1879 dpif_netdev_port_dump_next(const struct dpif
*dpif
, void *state_
,
1880 struct dpif_port
*dpif_port
)
1882 struct dp_netdev_port_state
*state
= state_
;
1883 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1884 struct hmap_node
*node
;
1887 ovs_mutex_lock(&dp
->port_mutex
);
1888 node
= hmap_at_position(&dp
->ports
, &state
->position
);
1890 struct dp_netdev_port
*port
;
1892 port
= CONTAINER_OF(node
, struct dp_netdev_port
, node
);
1895 state
->name
= xstrdup(netdev_get_name(port
->netdev
));
1896 dpif_port
->name
= state
->name
;
1897 dpif_port
->type
= port
->type
;
1898 dpif_port
->port_no
= port
->port_no
;
1904 ovs_mutex_unlock(&dp
->port_mutex
);
1910 dpif_netdev_port_dump_done(const struct dpif
*dpif OVS_UNUSED
, void *state_
)
1912 struct dp_netdev_port_state
*state
= state_
;
1919 dpif_netdev_port_poll(const struct dpif
*dpif_
, char **devnamep OVS_UNUSED
)
1921 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
1922 uint64_t new_port_seq
;
1925 new_port_seq
= seq_read(dpif
->dp
->port_seq
);
1926 if (dpif
->last_port_seq
!= new_port_seq
) {
1927 dpif
->last_port_seq
= new_port_seq
;
1937 dpif_netdev_port_poll_wait(const struct dpif
*dpif_
)
1939 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
1941 seq_wait(dpif
->dp
->port_seq
, dpif
->last_port_seq
);
1944 static struct dp_netdev_flow
*
1945 dp_netdev_flow_cast(const struct dpcls_rule
*cr
)
1947 return cr
? CONTAINER_OF(cr
, struct dp_netdev_flow
, cr
) : NULL
;
1950 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*flow
)
1952 return ovs_refcount_try_ref_rcu(&flow
->ref_cnt
);
1955 /* netdev_flow_key utilities.
1957 * netdev_flow_key is basically a miniflow. We use these functions
1958 * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow
1959 * functions (miniflow_clone_inline, miniflow_equal, ...), because:
1961 * - Since we are dealing exclusively with miniflows created by
1962 * miniflow_extract(), if the map is different the miniflow is different.
1963 * Therefore we can be faster by comparing the map and the miniflow in a
1965 * - These functions can be inlined by the compiler. */
1967 /* Given the number of bits set in miniflow's maps, returns the size of the
1968 * 'netdev_flow_key.mf' */
1969 static inline size_t
1970 netdev_flow_key_size(size_t flow_u64s
)
1972 return sizeof(struct miniflow
) + MINIFLOW_VALUES_SIZE(flow_u64s
);
1976 netdev_flow_key_equal(const struct netdev_flow_key
*a
,
1977 const struct netdev_flow_key
*b
)
1979 /* 'b->len' may be not set yet. */
1980 return a
->hash
== b
->hash
&& !memcmp(&a
->mf
, &b
->mf
, a
->len
);
1983 /* Used to compare 'netdev_flow_key' in the exact match cache to a miniflow.
1984 * The maps are compared bitwise, so both 'key->mf' and 'mf' must have been
1985 * generated by miniflow_extract. */
1987 netdev_flow_key_equal_mf(const struct netdev_flow_key
*key
,
1988 const struct miniflow
*mf
)
1990 return !memcmp(&key
->mf
, mf
, key
->len
);
1994 netdev_flow_key_clone(struct netdev_flow_key
*dst
,
1995 const struct netdev_flow_key
*src
)
1998 offsetof(struct netdev_flow_key
, mf
) + src
->len
);
2001 /* Initialize a netdev_flow_key 'mask' from 'match'. */
2003 netdev_flow_mask_init(struct netdev_flow_key
*mask
,
2004 const struct match
*match
)
2006 uint64_t *dst
= miniflow_values(&mask
->mf
);
2007 struct flowmap fmap
;
2011 /* Only check masks that make sense for the flow. */
2012 flow_wc_map(&match
->flow
, &fmap
);
2013 flowmap_init(&mask
->mf
.map
);
2015 FLOWMAP_FOR_EACH_INDEX(idx
, fmap
) {
2016 uint64_t mask_u64
= flow_u64_value(&match
->wc
.masks
, idx
);
2019 flowmap_set(&mask
->mf
.map
, idx
, 1);
2021 hash
= hash_add64(hash
, mask_u64
);
2027 FLOWMAP_FOR_EACH_MAP (map
, mask
->mf
.map
) {
2028 hash
= hash_add64(hash
, map
);
2031 size_t n
= dst
- miniflow_get_values(&mask
->mf
);
2033 mask
->hash
= hash_finish(hash
, n
* 8);
2034 mask
->len
= netdev_flow_key_size(n
);
2037 /* Initializes 'dst' as a copy of 'flow' masked with 'mask'. */
2039 netdev_flow_key_init_masked(struct netdev_flow_key
*dst
,
2040 const struct flow
*flow
,
2041 const struct netdev_flow_key
*mask
)
2043 uint64_t *dst_u64
= miniflow_values(&dst
->mf
);
2044 const uint64_t *mask_u64
= miniflow_get_values(&mask
->mf
);
2048 dst
->len
= mask
->len
;
2049 dst
->mf
= mask
->mf
; /* Copy maps. */
2051 FLOW_FOR_EACH_IN_MAPS(value
, flow
, mask
->mf
.map
) {
2052 *dst_u64
= value
& *mask_u64
++;
2053 hash
= hash_add64(hash
, *dst_u64
++);
2055 dst
->hash
= hash_finish(hash
,
2056 (dst_u64
- miniflow_get_values(&dst
->mf
)) * 8);
2059 /* Iterate through netdev_flow_key TNL u64 values specified by 'FLOWMAP'. */
2060 #define NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(VALUE, KEY, FLOWMAP) \
2061 MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, &(KEY)->mf, FLOWMAP)
2063 /* Returns a hash value for the bits of 'key' where there are 1-bits in
2065 static inline uint32_t
2066 netdev_flow_key_hash_in_mask(const struct netdev_flow_key
*key
,
2067 const struct netdev_flow_key
*mask
)
2069 const uint64_t *p
= miniflow_get_values(&mask
->mf
);
2073 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, key
, mask
->mf
.map
) {
2074 hash
= hash_add64(hash
, value
& *p
++);
2077 return hash_finish(hash
, (p
- miniflow_get_values(&mask
->mf
)) * 8);
2081 emc_entry_alive(struct emc_entry
*ce
)
2083 return ce
->flow
&& !ce
->flow
->dead
;
2087 emc_clear_entry(struct emc_entry
*ce
)
2090 dp_netdev_flow_unref(ce
->flow
);
2096 emc_change_entry(struct emc_entry
*ce
, struct dp_netdev_flow
*flow
,
2097 const struct netdev_flow_key
*key
)
2099 if (ce
->flow
!= flow
) {
2101 dp_netdev_flow_unref(ce
->flow
);
2104 if (dp_netdev_flow_ref(flow
)) {
2111 netdev_flow_key_clone(&ce
->key
, key
);
2116 emc_insert(struct emc_cache
*cache
, const struct netdev_flow_key
*key
,
2117 struct dp_netdev_flow
*flow
)
2119 struct emc_entry
*to_be_replaced
= NULL
;
2120 struct emc_entry
*current_entry
;
2122 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
2123 if (netdev_flow_key_equal(¤t_entry
->key
, key
)) {
2124 /* We found the entry with the 'mf' miniflow */
2125 emc_change_entry(current_entry
, flow
, NULL
);
2129 /* Replacement policy: put the flow in an empty (not alive) entry, or
2130 * in the first entry where it can be */
2132 || (emc_entry_alive(to_be_replaced
)
2133 && !emc_entry_alive(current_entry
))
2134 || current_entry
->key
.hash
< to_be_replaced
->key
.hash
) {
2135 to_be_replaced
= current_entry
;
2138 /* We didn't find the miniflow in the cache.
2139 * The 'to_be_replaced' entry is where the new flow will be stored */
2141 emc_change_entry(to_be_replaced
, flow
, key
);
2145 emc_probabilistic_insert(struct dp_netdev_pmd_thread
*pmd
,
2146 const struct netdev_flow_key
*key
,
2147 struct dp_netdev_flow
*flow
)
2149 /* Insert an entry into the EMC based on probability value 'min'. By
2150 * default the value is UINT32_MAX / 100 which yields an insertion
2151 * probability of 1/100 ie. 1% */
2154 atomic_read_relaxed(&pmd
->dp
->emc_insert_min
, &min
);
2156 if (min
&& random_uint32() <= min
) {
2157 emc_insert(&pmd
->flow_cache
, key
, flow
);
2161 static inline struct dp_netdev_flow
*
2162 emc_lookup(struct emc_cache
*cache
, const struct netdev_flow_key
*key
)
2164 struct emc_entry
*current_entry
;
2166 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
2167 if (current_entry
->key
.hash
== key
->hash
2168 && emc_entry_alive(current_entry
)
2169 && netdev_flow_key_equal_mf(¤t_entry
->key
, &key
->mf
)) {
2171 /* We found the entry with the 'key->mf' miniflow */
2172 return current_entry
->flow
;
2179 static struct dp_netdev_flow
*
2180 dp_netdev_pmd_lookup_flow(struct dp_netdev_pmd_thread
*pmd
,
2181 const struct netdev_flow_key
*key
,
2185 struct dpcls_rule
*rule
;
2186 odp_port_t in_port
= u32_to_odp(MINIFLOW_GET_U32(&key
->mf
, in_port
));
2187 struct dp_netdev_flow
*netdev_flow
= NULL
;
2189 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
2190 if (OVS_LIKELY(cls
)) {
2191 dpcls_lookup(cls
, key
, &rule
, 1, lookup_num_p
);
2192 netdev_flow
= dp_netdev_flow_cast(rule
);
2197 static struct dp_netdev_flow
*
2198 dp_netdev_pmd_find_flow(const struct dp_netdev_pmd_thread
*pmd
,
2199 const ovs_u128
*ufidp
, const struct nlattr
*key
,
2202 struct dp_netdev_flow
*netdev_flow
;
2206 /* If a UFID is not provided, determine one based on the key. */
2207 if (!ufidp
&& key
&& key_len
2208 && !dpif_netdev_flow_from_nlattrs(key
, key_len
, &flow
, false)) {
2209 dpif_flow_hash(pmd
->dp
->dpif
, &flow
, sizeof flow
, &ufid
);
2214 CMAP_FOR_EACH_WITH_HASH (netdev_flow
, node
, dp_netdev_flow_hash(ufidp
),
2216 if (ovs_u128_equals(netdev_flow
->ufid
, *ufidp
)) {
2226 get_dpif_flow_stats(const struct dp_netdev_flow
*netdev_flow_
,
2227 struct dpif_flow_stats
*stats
)
2229 struct dp_netdev_flow
*netdev_flow
;
2230 unsigned long long n
;
2234 netdev_flow
= CONST_CAST(struct dp_netdev_flow
*, netdev_flow_
);
2236 atomic_read_relaxed(&netdev_flow
->stats
.packet_count
, &n
);
2237 stats
->n_packets
= n
;
2238 atomic_read_relaxed(&netdev_flow
->stats
.byte_count
, &n
);
2240 atomic_read_relaxed(&netdev_flow
->stats
.used
, &used
);
2242 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
2243 stats
->tcp_flags
= flags
;
2246 /* Converts to the dpif_flow format, using 'key_buf' and 'mask_buf' for
2247 * storing the netlink-formatted key/mask. 'key_buf' may be the same as
2248 * 'mask_buf'. Actions will be returned without copying, by relying on RCU to
2251 dp_netdev_flow_to_dpif_flow(const struct dp_netdev_flow
*netdev_flow
,
2252 struct ofpbuf
*key_buf
, struct ofpbuf
*mask_buf
,
2253 struct dpif_flow
*flow
, bool terse
)
2256 memset(flow
, 0, sizeof *flow
);
2258 struct flow_wildcards wc
;
2259 struct dp_netdev_actions
*actions
;
2261 struct odp_flow_key_parms odp_parms
= {
2262 .flow
= &netdev_flow
->flow
,
2264 .support
= dp_netdev_support
,
2267 miniflow_expand(&netdev_flow
->cr
.mask
->mf
, &wc
.masks
);
2268 /* in_port is exact matched, but we have left it out from the mask for
2269 * optimnization reasons. Add in_port back to the mask. */
2270 wc
.masks
.in_port
.odp_port
= ODPP_NONE
;
2273 offset
= key_buf
->size
;
2274 flow
->key
= ofpbuf_tail(key_buf
);
2275 odp_flow_key_from_flow(&odp_parms
, key_buf
);
2276 flow
->key_len
= key_buf
->size
- offset
;
2279 offset
= mask_buf
->size
;
2280 flow
->mask
= ofpbuf_tail(mask_buf
);
2281 odp_parms
.key_buf
= key_buf
;
2282 odp_flow_key_from_mask(&odp_parms
, mask_buf
);
2283 flow
->mask_len
= mask_buf
->size
- offset
;
2286 actions
= dp_netdev_flow_get_actions(netdev_flow
);
2287 flow
->actions
= actions
->actions
;
2288 flow
->actions_len
= actions
->size
;
2291 flow
->ufid
= netdev_flow
->ufid
;
2292 flow
->ufid_present
= true;
2293 flow
->pmd_id
= netdev_flow
->pmd_id
;
2294 get_dpif_flow_stats(netdev_flow
, &flow
->stats
);
2298 dpif_netdev_mask_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
2299 const struct nlattr
*mask_key
,
2300 uint32_t mask_key_len
, const struct flow
*flow
,
2301 struct flow_wildcards
*wc
, bool probe
)
2303 enum odp_key_fitness fitness
;
2305 fitness
= odp_flow_key_to_mask(mask_key
, mask_key_len
, wc
, flow
);
2308 /* This should not happen: it indicates that
2309 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
2310 * disagree on the acceptable form of a mask. Log the problem
2311 * as an error, with enough details to enable debugging. */
2312 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2314 if (!VLOG_DROP_ERR(&rl
)) {
2318 odp_flow_format(key
, key_len
, mask_key
, mask_key_len
, NULL
, &s
,
2320 VLOG_ERR("internal error parsing flow mask %s (%s)",
2321 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
2333 dpif_netdev_flow_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
2334 struct flow
*flow
, bool probe
)
2336 if (odp_flow_key_to_flow(key
, key_len
, flow
)) {
2338 /* This should not happen: it indicates that
2339 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
2340 * the acceptable form of a flow. Log the problem as an error,
2341 * with enough details to enable debugging. */
2342 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2344 if (!VLOG_DROP_ERR(&rl
)) {
2348 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
2349 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
2357 if (flow
->ct_state
& DP_NETDEV_CS_UNSUPPORTED_MASK
) {
2365 dpif_netdev_flow_get(const struct dpif
*dpif
, const struct dpif_flow_get
*get
)
2367 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2368 struct dp_netdev_flow
*netdev_flow
;
2369 struct dp_netdev_pmd_thread
*pmd
;
2370 struct hmapx to_find
= HMAPX_INITIALIZER(&to_find
);
2371 struct hmapx_node
*node
;
2374 if (get
->pmd_id
== PMD_ID_NULL
) {
2375 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2376 if (dp_netdev_pmd_try_ref(pmd
) && !hmapx_add(&to_find
, pmd
)) {
2377 dp_netdev_pmd_unref(pmd
);
2381 pmd
= dp_netdev_get_pmd(dp
, get
->pmd_id
);
2385 hmapx_add(&to_find
, pmd
);
2388 if (!hmapx_count(&to_find
)) {
2392 HMAPX_FOR_EACH (node
, &to_find
) {
2393 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
2394 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, get
->ufid
, get
->key
,
2397 dp_netdev_flow_to_dpif_flow(netdev_flow
, get
->buffer
, get
->buffer
,
2406 HMAPX_FOR_EACH (node
, &to_find
) {
2407 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
2408 dp_netdev_pmd_unref(pmd
);
2411 hmapx_destroy(&to_find
);
2415 static struct dp_netdev_flow
*
2416 dp_netdev_flow_add(struct dp_netdev_pmd_thread
*pmd
,
2417 struct match
*match
, const ovs_u128
*ufid
,
2418 const struct nlattr
*actions
, size_t actions_len
)
2419 OVS_REQUIRES(pmd
->flow_mutex
)
2421 struct dp_netdev_flow
*flow
;
2422 struct netdev_flow_key mask
;
2425 /* Make sure in_port is exact matched before we read it. */
2426 ovs_assert(match
->wc
.masks
.in_port
.odp_port
== ODPP_NONE
);
2427 odp_port_t in_port
= match
->flow
.in_port
.odp_port
;
2429 /* As we select the dpcls based on the port number, each netdev flow
2430 * belonging to the same dpcls will have the same odp_port value.
2431 * For performance reasons we wildcard odp_port here in the mask. In the
2432 * typical case dp_hash is also wildcarded, and the resulting 8-byte
2433 * chunk {dp_hash, in_port} will be ignored by netdev_flow_mask_init() and
2434 * will not be part of the subtable mask.
2435 * This will speed up the hash computation during dpcls_lookup() because
2436 * there is one less call to hash_add64() in this case. */
2437 match
->wc
.masks
.in_port
.odp_port
= 0;
2438 netdev_flow_mask_init(&mask
, match
);
2439 match
->wc
.masks
.in_port
.odp_port
= ODPP_NONE
;
2441 /* Make sure wc does not have metadata. */
2442 ovs_assert(!FLOWMAP_HAS_FIELD(&mask
.mf
.map
, metadata
)
2443 && !FLOWMAP_HAS_FIELD(&mask
.mf
.map
, regs
));
2445 /* Do not allocate extra space. */
2446 flow
= xmalloc(sizeof *flow
- sizeof flow
->cr
.flow
.mf
+ mask
.len
);
2447 memset(&flow
->stats
, 0, sizeof flow
->stats
);
2450 *CONST_CAST(unsigned *, &flow
->pmd_id
) = pmd
->core_id
;
2451 *CONST_CAST(struct flow
*, &flow
->flow
) = match
->flow
;
2452 *CONST_CAST(ovs_u128
*, &flow
->ufid
) = *ufid
;
2453 ovs_refcount_init(&flow
->ref_cnt
);
2454 ovsrcu_set(&flow
->actions
, dp_netdev_actions_create(actions
, actions_len
));
2456 netdev_flow_key_init_masked(&flow
->cr
.flow
, &match
->flow
, &mask
);
2458 /* Select dpcls for in_port. Relies on in_port to be exact match. */
2459 cls
= dp_netdev_pmd_find_dpcls(pmd
, in_port
);
2460 dpcls_insert(cls
, &flow
->cr
, &mask
);
2462 cmap_insert(&pmd
->flow_table
, CONST_CAST(struct cmap_node
*, &flow
->node
),
2463 dp_netdev_flow_hash(&flow
->ufid
));
2465 if (OVS_UNLIKELY(!VLOG_DROP_DBG((&upcall_rl
)))) {
2466 struct ds ds
= DS_EMPTY_INITIALIZER
;
2467 struct ofpbuf key_buf
, mask_buf
;
2468 struct odp_flow_key_parms odp_parms
= {
2469 .flow
= &match
->flow
,
2470 .mask
= &match
->wc
.masks
,
2471 .support
= dp_netdev_support
,
2474 ofpbuf_init(&key_buf
, 0);
2475 ofpbuf_init(&mask_buf
, 0);
2477 odp_flow_key_from_flow(&odp_parms
, &key_buf
);
2478 odp_parms
.key_buf
= &key_buf
;
2479 odp_flow_key_from_mask(&odp_parms
, &mask_buf
);
2481 ds_put_cstr(&ds
, "flow_add: ");
2482 odp_format_ufid(ufid
, &ds
);
2483 ds_put_cstr(&ds
, " ");
2484 odp_flow_format(key_buf
.data
, key_buf
.size
,
2485 mask_buf
.data
, mask_buf
.size
,
2487 ds_put_cstr(&ds
, ", actions:");
2488 format_odp_actions(&ds
, actions
, actions_len
, NULL
);
2490 VLOG_DBG("%s", ds_cstr(&ds
));
2492 ofpbuf_uninit(&key_buf
);
2493 ofpbuf_uninit(&mask_buf
);
2495 /* Add a printout of the actual match installed. */
2498 ds_put_cstr(&ds
, "flow match: ");
2499 miniflow_expand(&flow
->cr
.flow
.mf
, &m
.flow
);
2500 miniflow_expand(&flow
->cr
.mask
->mf
, &m
.wc
.masks
);
2501 memset(&m
.tun_md
, 0, sizeof m
.tun_md
);
2502 match_format(&m
, NULL
, &ds
, OFP_DEFAULT_PRIORITY
);
2504 VLOG_DBG("%s", ds_cstr(&ds
));
2513 flow_put_on_pmd(struct dp_netdev_pmd_thread
*pmd
,
2514 struct netdev_flow_key
*key
,
2515 struct match
*match
,
2517 const struct dpif_flow_put
*put
,
2518 struct dpif_flow_stats
*stats
)
2520 struct dp_netdev_flow
*netdev_flow
;
2524 memset(stats
, 0, sizeof *stats
);
2527 ovs_mutex_lock(&pmd
->flow_mutex
);
2528 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
, NULL
);
2530 if (put
->flags
& DPIF_FP_CREATE
) {
2531 if (cmap_count(&pmd
->flow_table
) < MAX_FLOWS
) {
2532 dp_netdev_flow_add(pmd
, match
, ufid
, put
->actions
,
2542 if (put
->flags
& DPIF_FP_MODIFY
) {
2543 struct dp_netdev_actions
*new_actions
;
2544 struct dp_netdev_actions
*old_actions
;
2546 new_actions
= dp_netdev_actions_create(put
->actions
,
2549 old_actions
= dp_netdev_flow_get_actions(netdev_flow
);
2550 ovsrcu_set(&netdev_flow
->actions
, new_actions
);
2553 get_dpif_flow_stats(netdev_flow
, stats
);
2555 if (put
->flags
& DPIF_FP_ZERO_STATS
) {
2556 /* XXX: The userspace datapath uses thread local statistics
2557 * (for flows), which should be updated only by the owning
2558 * thread. Since we cannot write on stats memory here,
2559 * we choose not to support this flag. Please note:
2560 * - This feature is currently used only by dpctl commands with
2562 * - Should the need arise, this operation can be implemented
2563 * by keeping a base value (to be update here) for each
2564 * counter, and subtracting it before outputting the stats */
2568 ovsrcu_postpone(dp_netdev_actions_free
, old_actions
);
2569 } else if (put
->flags
& DPIF_FP_CREATE
) {
2572 /* Overlapping flow. */
2576 ovs_mutex_unlock(&pmd
->flow_mutex
);
2581 dpif_netdev_flow_put(struct dpif
*dpif
, const struct dpif_flow_put
*put
)
2583 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2584 struct netdev_flow_key key
, mask
;
2585 struct dp_netdev_pmd_thread
*pmd
;
2589 bool probe
= put
->flags
& DPIF_FP_PROBE
;
2592 memset(put
->stats
, 0, sizeof *put
->stats
);
2594 error
= dpif_netdev_flow_from_nlattrs(put
->key
, put
->key_len
, &match
.flow
,
2599 error
= dpif_netdev_mask_from_nlattrs(put
->key
, put
->key_len
,
2600 put
->mask
, put
->mask_len
,
2601 &match
.flow
, &match
.wc
, probe
);
2609 dpif_flow_hash(dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
2612 /* Must produce a netdev_flow_key for lookup.
2613 * Use the same method as employed to create the key when adding
2614 * the flow to the dplcs to make sure they match. */
2615 netdev_flow_mask_init(&mask
, &match
);
2616 netdev_flow_key_init_masked(&key
, &match
.flow
, &mask
);
2618 if (put
->pmd_id
== PMD_ID_NULL
) {
2619 if (cmap_count(&dp
->poll_threads
) == 0) {
2622 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2623 struct dpif_flow_stats pmd_stats
;
2626 pmd_error
= flow_put_on_pmd(pmd
, &key
, &match
, &ufid
, put
,
2630 } else if (put
->stats
) {
2631 put
->stats
->n_packets
+= pmd_stats
.n_packets
;
2632 put
->stats
->n_bytes
+= pmd_stats
.n_bytes
;
2633 put
->stats
->used
= MAX(put
->stats
->used
, pmd_stats
.used
);
2634 put
->stats
->tcp_flags
|= pmd_stats
.tcp_flags
;
2638 pmd
= dp_netdev_get_pmd(dp
, put
->pmd_id
);
2642 error
= flow_put_on_pmd(pmd
, &key
, &match
, &ufid
, put
, put
->stats
);
2643 dp_netdev_pmd_unref(pmd
);
2650 flow_del_on_pmd(struct dp_netdev_pmd_thread
*pmd
,
2651 struct dpif_flow_stats
*stats
,
2652 const struct dpif_flow_del
*del
)
2654 struct dp_netdev_flow
*netdev_flow
;
2657 ovs_mutex_lock(&pmd
->flow_mutex
);
2658 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, del
->ufid
, del
->key
,
2662 get_dpif_flow_stats(netdev_flow
, stats
);
2664 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
2668 ovs_mutex_unlock(&pmd
->flow_mutex
);
2674 dpif_netdev_flow_del(struct dpif
*dpif
, const struct dpif_flow_del
*del
)
2676 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2677 struct dp_netdev_pmd_thread
*pmd
;
2681 memset(del
->stats
, 0, sizeof *del
->stats
);
2684 if (del
->pmd_id
== PMD_ID_NULL
) {
2685 if (cmap_count(&dp
->poll_threads
) == 0) {
2688 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2689 struct dpif_flow_stats pmd_stats
;
2692 pmd_error
= flow_del_on_pmd(pmd
, &pmd_stats
, del
);
2695 } else if (del
->stats
) {
2696 del
->stats
->n_packets
+= pmd_stats
.n_packets
;
2697 del
->stats
->n_bytes
+= pmd_stats
.n_bytes
;
2698 del
->stats
->used
= MAX(del
->stats
->used
, pmd_stats
.used
);
2699 del
->stats
->tcp_flags
|= pmd_stats
.tcp_flags
;
2703 pmd
= dp_netdev_get_pmd(dp
, del
->pmd_id
);
2707 error
= flow_del_on_pmd(pmd
, del
->stats
, del
);
2708 dp_netdev_pmd_unref(pmd
);
2715 struct dpif_netdev_flow_dump
{
2716 struct dpif_flow_dump up
;
2717 struct cmap_position poll_thread_pos
;
2718 struct cmap_position flow_pos
;
2719 struct dp_netdev_pmd_thread
*cur_pmd
;
2721 struct ovs_mutex mutex
;
2724 static struct dpif_netdev_flow_dump
*
2725 dpif_netdev_flow_dump_cast(struct dpif_flow_dump
*dump
)
2727 return CONTAINER_OF(dump
, struct dpif_netdev_flow_dump
, up
);
2730 static struct dpif_flow_dump
*
2731 dpif_netdev_flow_dump_create(const struct dpif
*dpif_
, bool terse
,
2732 char *type OVS_UNUSED
)
2734 struct dpif_netdev_flow_dump
*dump
;
2736 dump
= xzalloc(sizeof *dump
);
2737 dpif_flow_dump_init(&dump
->up
, dpif_
);
2738 dump
->up
.terse
= terse
;
2739 ovs_mutex_init(&dump
->mutex
);
2745 dpif_netdev_flow_dump_destroy(struct dpif_flow_dump
*dump_
)
2747 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
2749 ovs_mutex_destroy(&dump
->mutex
);
2754 struct dpif_netdev_flow_dump_thread
{
2755 struct dpif_flow_dump_thread up
;
2756 struct dpif_netdev_flow_dump
*dump
;
2757 struct odputil_keybuf keybuf
[FLOW_DUMP_MAX_BATCH
];
2758 struct odputil_keybuf maskbuf
[FLOW_DUMP_MAX_BATCH
];
2761 static struct dpif_netdev_flow_dump_thread
*
2762 dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread
*thread
)
2764 return CONTAINER_OF(thread
, struct dpif_netdev_flow_dump_thread
, up
);
2767 static struct dpif_flow_dump_thread
*
2768 dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump
*dump_
)
2770 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
2771 struct dpif_netdev_flow_dump_thread
*thread
;
2773 thread
= xmalloc(sizeof *thread
);
2774 dpif_flow_dump_thread_init(&thread
->up
, &dump
->up
);
2775 thread
->dump
= dump
;
2780 dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread
*thread_
)
2782 struct dpif_netdev_flow_dump_thread
*thread
2783 = dpif_netdev_flow_dump_thread_cast(thread_
);
2789 dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread
*thread_
,
2790 struct dpif_flow
*flows
, int max_flows
)
2792 struct dpif_netdev_flow_dump_thread
*thread
2793 = dpif_netdev_flow_dump_thread_cast(thread_
);
2794 struct dpif_netdev_flow_dump
*dump
= thread
->dump
;
2795 struct dp_netdev_flow
*netdev_flows
[FLOW_DUMP_MAX_BATCH
];
2799 ovs_mutex_lock(&dump
->mutex
);
2800 if (!dump
->status
) {
2801 struct dpif_netdev
*dpif
= dpif_netdev_cast(thread
->up
.dpif
);
2802 struct dp_netdev
*dp
= get_dp_netdev(&dpif
->dpif
);
2803 struct dp_netdev_pmd_thread
*pmd
= dump
->cur_pmd
;
2804 int flow_limit
= MIN(max_flows
, FLOW_DUMP_MAX_BATCH
);
2806 /* First call to dump_next(), extracts the first pmd thread.
2807 * If there is no pmd thread, returns immediately. */
2809 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
2811 ovs_mutex_unlock(&dump
->mutex
);
2818 for (n_flows
= 0; n_flows
< flow_limit
; n_flows
++) {
2819 struct cmap_node
*node
;
2821 node
= cmap_next_position(&pmd
->flow_table
, &dump
->flow_pos
);
2825 netdev_flows
[n_flows
] = CONTAINER_OF(node
,
2826 struct dp_netdev_flow
,
2829 /* When finishing dumping the current pmd thread, moves to
2831 if (n_flows
< flow_limit
) {
2832 memset(&dump
->flow_pos
, 0, sizeof dump
->flow_pos
);
2833 dp_netdev_pmd_unref(pmd
);
2834 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
2840 /* Keeps the reference to next caller. */
2841 dump
->cur_pmd
= pmd
;
2843 /* If the current dump is empty, do not exit the loop, since the
2844 * remaining pmds could have flows to be dumped. Just dumps again
2845 * on the new 'pmd'. */
2848 ovs_mutex_unlock(&dump
->mutex
);
2850 for (i
= 0; i
< n_flows
; i
++) {
2851 struct odputil_keybuf
*maskbuf
= &thread
->maskbuf
[i
];
2852 struct odputil_keybuf
*keybuf
= &thread
->keybuf
[i
];
2853 struct dp_netdev_flow
*netdev_flow
= netdev_flows
[i
];
2854 struct dpif_flow
*f
= &flows
[i
];
2855 struct ofpbuf key
, mask
;
2857 ofpbuf_use_stack(&key
, keybuf
, sizeof *keybuf
);
2858 ofpbuf_use_stack(&mask
, maskbuf
, sizeof *maskbuf
);
2859 dp_netdev_flow_to_dpif_flow(netdev_flow
, &key
, &mask
, f
,
2867 dpif_netdev_execute(struct dpif
*dpif
, struct dpif_execute
*execute
)
2868 OVS_NO_THREAD_SAFETY_ANALYSIS
2870 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2871 struct dp_netdev_pmd_thread
*pmd
;
2872 struct dp_packet_batch pp
;
2874 if (dp_packet_size(execute
->packet
) < ETH_HEADER_LEN
||
2875 dp_packet_size(execute
->packet
) > UINT16_MAX
) {
2879 /* Tries finding the 'pmd'. If NULL is returned, that means
2880 * the current thread is a non-pmd thread and should use
2881 * dp_netdev_get_pmd(dp, NON_PMD_CORE_ID). */
2882 pmd
= ovsthread_getspecific(dp
->per_pmd_key
);
2884 pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
2890 if (execute
->probe
) {
2891 /* If this is part of a probe, Drop the packet, since executing
2892 * the action may actually cause spurious packets be sent into
2894 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
2895 dp_netdev_pmd_unref(pmd
);
2900 /* If the current thread is non-pmd thread, acquires
2901 * the 'non_pmd_mutex'. */
2902 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
2903 ovs_mutex_lock(&dp
->non_pmd_mutex
);
2906 /* Update current time in PMD context. */
2907 pmd_thread_ctx_time_update(pmd
);
2909 /* The action processing expects the RSS hash to be valid, because
2910 * it's always initialized at the beginning of datapath processing.
2911 * In this case, though, 'execute->packet' may not have gone through
2912 * the datapath at all, it may have been generated by the upper layer
2913 * (OpenFlow packet-out, BFD frame, ...). */
2914 if (!dp_packet_rss_valid(execute
->packet
)) {
2915 dp_packet_set_rss_hash(execute
->packet
,
2916 flow_hash_5tuple(execute
->flow
, 0));
2919 dp_packet_batch_init_packet(&pp
, execute
->packet
);
2920 dp_netdev_execute_actions(pmd
, &pp
, false, execute
->flow
,
2921 execute
->actions
, execute
->actions_len
);
2922 dp_netdev_pmd_flush_output_packets(pmd
, true);
2924 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
2925 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
2926 dp_netdev_pmd_unref(pmd
);
2933 dpif_netdev_operate(struct dpif
*dpif
, struct dpif_op
**ops
, size_t n_ops
)
2937 for (i
= 0; i
< n_ops
; i
++) {
2938 struct dpif_op
*op
= ops
[i
];
2941 case DPIF_OP_FLOW_PUT
:
2942 op
->error
= dpif_netdev_flow_put(dpif
, &op
->u
.flow_put
);
2945 case DPIF_OP_FLOW_DEL
:
2946 op
->error
= dpif_netdev_flow_del(dpif
, &op
->u
.flow_del
);
2949 case DPIF_OP_EXECUTE
:
2950 op
->error
= dpif_netdev_execute(dpif
, &op
->u
.execute
);
2953 case DPIF_OP_FLOW_GET
:
2954 op
->error
= dpif_netdev_flow_get(dpif
, &op
->u
.flow_get
);
2960 /* Applies datapath configuration from the database. Some of the changes are
2961 * actually applied in dpif_netdev_run(). */
2963 dpif_netdev_set_config(struct dpif
*dpif
, const struct smap
*other_config
)
2965 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2966 const char *cmask
= smap_get(other_config
, "pmd-cpu-mask");
2967 unsigned long long insert_prob
=
2968 smap_get_ullong(other_config
, "emc-insert-inv-prob",
2969 DEFAULT_EM_FLOW_INSERT_INV_PROB
);
2970 uint32_t insert_min
, cur_min
;
2971 uint32_t tx_flush_interval
, cur_tx_flush_interval
;
2973 tx_flush_interval
= smap_get_int(other_config
, "tx-flush-interval",
2974 DEFAULT_TX_FLUSH_INTERVAL
);
2975 atomic_read_relaxed(&dp
->tx_flush_interval
, &cur_tx_flush_interval
);
2976 if (tx_flush_interval
!= cur_tx_flush_interval
) {
2977 atomic_store_relaxed(&dp
->tx_flush_interval
, tx_flush_interval
);
2978 VLOG_INFO("Flushing interval for tx queues set to %"PRIu32
" us",
2982 if (!nullable_string_is_equal(dp
->pmd_cmask
, cmask
)) {
2983 free(dp
->pmd_cmask
);
2984 dp
->pmd_cmask
= nullable_xstrdup(cmask
);
2985 dp_netdev_request_reconfigure(dp
);
2988 atomic_read_relaxed(&dp
->emc_insert_min
, &cur_min
);
2989 if (insert_prob
<= UINT32_MAX
) {
2990 insert_min
= insert_prob
== 0 ? 0 : UINT32_MAX
/ insert_prob
;
2992 insert_min
= DEFAULT_EM_FLOW_INSERT_MIN
;
2993 insert_prob
= DEFAULT_EM_FLOW_INSERT_INV_PROB
;
2996 if (insert_min
!= cur_min
) {
2997 atomic_store_relaxed(&dp
->emc_insert_min
, insert_min
);
2998 if (insert_min
== 0) {
2999 VLOG_INFO("EMC has been disabled");
3001 VLOG_INFO("EMC insertion probability changed to 1/%llu (~%.2f%%)",
3002 insert_prob
, (100 / (float)insert_prob
));
3009 /* Parses affinity list and returns result in 'core_ids'. */
3011 parse_affinity_list(const char *affinity_list
, unsigned *core_ids
, int n_rxq
)
3014 char *list
, *copy
, *key
, *value
;
3017 for (i
= 0; i
< n_rxq
; i
++) {
3018 core_ids
[i
] = OVS_CORE_UNSPEC
;
3021 if (!affinity_list
) {
3025 list
= copy
= xstrdup(affinity_list
);
3027 while (ofputil_parse_key_value(&list
, &key
, &value
)) {
3028 int rxq_id
, core_id
;
3030 if (!str_to_int(key
, 0, &rxq_id
) || rxq_id
< 0
3031 || !str_to_int(value
, 0, &core_id
) || core_id
< 0) {
3036 if (rxq_id
< n_rxq
) {
3037 core_ids
[rxq_id
] = core_id
;
3045 /* Parses 'affinity_list' and applies configuration if it is valid. */
3047 dpif_netdev_port_set_rxq_affinity(struct dp_netdev_port
*port
,
3048 const char *affinity_list
)
3050 unsigned *core_ids
, i
;
3053 core_ids
= xmalloc(port
->n_rxq
* sizeof *core_ids
);
3054 if (parse_affinity_list(affinity_list
, core_ids
, port
->n_rxq
)) {
3059 for (i
= 0; i
< port
->n_rxq
; i
++) {
3060 port
->rxqs
[i
].core_id
= core_ids
[i
];
3068 /* Changes the affinity of port's rx queues. The changes are actually applied
3069 * in dpif_netdev_run(). */
3071 dpif_netdev_port_set_config(struct dpif
*dpif
, odp_port_t port_no
,
3072 const struct smap
*cfg
)
3074 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3075 struct dp_netdev_port
*port
;
3077 const char *affinity_list
= smap_get(cfg
, "pmd-rxq-affinity");
3079 ovs_mutex_lock(&dp
->port_mutex
);
3080 error
= get_port_by_number(dp
, port_no
, &port
);
3081 if (error
|| !netdev_is_pmd(port
->netdev
)
3082 || nullable_string_is_equal(affinity_list
, port
->rxq_affinity_list
)) {
3086 error
= dpif_netdev_port_set_rxq_affinity(port
, affinity_list
);
3090 free(port
->rxq_affinity_list
);
3091 port
->rxq_affinity_list
= nullable_xstrdup(affinity_list
);
3093 dp_netdev_request_reconfigure(dp
);
3095 ovs_mutex_unlock(&dp
->port_mutex
);
3100 dpif_netdev_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
3101 uint32_t queue_id
, uint32_t *priority
)
3103 *priority
= queue_id
;
3108 /* Creates and returns a new 'struct dp_netdev_actions', whose actions are
3109 * a copy of the 'size' bytes of 'actions' input parameters. */
3110 struct dp_netdev_actions
*
3111 dp_netdev_actions_create(const struct nlattr
*actions
, size_t size
)
3113 struct dp_netdev_actions
*netdev_actions
;
3115 netdev_actions
= xmalloc(sizeof *netdev_actions
+ size
);
3116 memcpy(netdev_actions
->actions
, actions
, size
);
3117 netdev_actions
->size
= size
;
3119 return netdev_actions
;
3122 struct dp_netdev_actions
*
3123 dp_netdev_flow_get_actions(const struct dp_netdev_flow
*flow
)
3125 return ovsrcu_get(struct dp_netdev_actions
*, &flow
->actions
);
3129 dp_netdev_actions_free(struct dp_netdev_actions
*actions
)
3135 dp_netdev_rxq_set_cycles(struct dp_netdev_rxq
*rx
,
3136 enum rxq_cycles_counter_type type
,
3137 unsigned long long cycles
)
3139 atomic_store_relaxed(&rx
->cycles
[type
], cycles
);
3143 dp_netdev_rxq_add_cycles(struct dp_netdev_rxq
*rx
,
3144 enum rxq_cycles_counter_type type
,
3145 unsigned long long cycles
)
3147 non_atomic_ullong_add(&rx
->cycles
[type
], cycles
);
3151 dp_netdev_rxq_get_cycles(struct dp_netdev_rxq
*rx
,
3152 enum rxq_cycles_counter_type type
)
3154 unsigned long long processing_cycles
;
3155 atomic_read_relaxed(&rx
->cycles
[type
], &processing_cycles
);
3156 return processing_cycles
;
3160 dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq
*rx
,
3161 unsigned long long cycles
)
3163 unsigned int idx
= rx
->intrvl_idx
++ % PMD_RXQ_INTERVAL_MAX
;
3164 atomic_store_relaxed(&rx
->cycles_intrvl
[idx
], cycles
);
3168 dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq
*rx
, unsigned idx
)
3170 unsigned long long processing_cycles
;
3171 atomic_read_relaxed(&rx
->cycles_intrvl
[idx
], &processing_cycles
);
3172 return processing_cycles
;
3176 dp_netdev_pmd_flush_output_on_port(struct dp_netdev_pmd_thread
*pmd
,
3183 struct cycle_timer timer
;
3185 uint32_t tx_flush_interval
;
3187 cycle_timer_start(&pmd
->perf_stats
, &timer
);
3189 dynamic_txqs
= p
->port
->dynamic_txqs
;
3191 tx_qid
= dpif_netdev_xps_get_tx_qid(pmd
, p
);
3193 tx_qid
= pmd
->static_tx_qid
;
3196 output_cnt
= dp_packet_batch_size(&p
->output_pkts
);
3197 ovs_assert(output_cnt
> 0);
3199 netdev_send(p
->port
->netdev
, tx_qid
, &p
->output_pkts
, dynamic_txqs
);
3200 dp_packet_batch_init(&p
->output_pkts
);
3202 /* Update time of the next flush. */
3203 atomic_read_relaxed(&pmd
->dp
->tx_flush_interval
, &tx_flush_interval
);
3204 p
->flush_time
= pmd
->ctx
.now
+ tx_flush_interval
;
3206 ovs_assert(pmd
->n_output_batches
> 0);
3207 pmd
->n_output_batches
--;
3209 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_SENT_PKTS
, output_cnt
);
3210 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_SENT_BATCHES
, 1);
3212 /* Distribute send cycles evenly among transmitted packets and assign to
3213 * their respective rx queues. */
3214 cycles
= cycle_timer_stop(&pmd
->perf_stats
, &timer
) / output_cnt
;
3215 for (i
= 0; i
< output_cnt
; i
++) {
3216 if (p
->output_pkts_rxqs
[i
]) {
3217 dp_netdev_rxq_add_cycles(p
->output_pkts_rxqs
[i
],
3218 RXQ_CYCLES_PROC_CURR
, cycles
);
3226 dp_netdev_pmd_flush_output_packets(struct dp_netdev_pmd_thread
*pmd
,
3232 if (!pmd
->n_output_batches
) {
3236 HMAP_FOR_EACH (p
, node
, &pmd
->send_port_cache
) {
3237 if (!dp_packet_batch_is_empty(&p
->output_pkts
)
3238 && (force
|| pmd
->ctx
.now
>= p
->flush_time
)) {
3239 output_cnt
+= dp_netdev_pmd_flush_output_on_port(pmd
, p
);
3246 dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread
*pmd
,
3247 struct dp_netdev_rxq
*rxq
,
3250 struct dp_packet_batch batch
;
3251 struct cycle_timer timer
;
3253 int batch_cnt
= 0, output_cnt
= 0;
3256 /* Measure duration for polling and processing rx burst. */
3257 cycle_timer_start(&pmd
->perf_stats
, &timer
);
3259 pmd
->ctx
.last_rxq
= rxq
;
3260 dp_packet_batch_init(&batch
);
3262 error
= netdev_rxq_recv(rxq
->rx
, &batch
);
3264 /* At least one packet received. */
3265 *recirc_depth_get() = 0;
3266 pmd_thread_ctx_time_update(pmd
);
3268 batch_cnt
= batch
.count
;
3269 dp_netdev_input(pmd
, &batch
, port_no
);
3271 /* Assign processing cycles to rx queue. */
3272 cycles
= cycle_timer_stop(&pmd
->perf_stats
, &timer
);
3273 dp_netdev_rxq_add_cycles(rxq
, RXQ_CYCLES_PROC_CURR
, cycles
);
3275 output_cnt
= dp_netdev_pmd_flush_output_packets(pmd
, false);
3277 /* Discard cycles. */
3278 cycle_timer_stop(&pmd
->perf_stats
, &timer
);
3279 if (error
!= EAGAIN
&& error
!= EOPNOTSUPP
) {
3280 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3282 VLOG_ERR_RL(&rl
, "error receiving data from %s: %s",
3283 netdev_rxq_get_name(rxq
->rx
), ovs_strerror(error
));
3287 pmd
->ctx
.last_rxq
= NULL
;
3289 return batch_cnt
+ output_cnt
;
3292 static struct tx_port
*
3293 tx_port_lookup(const struct hmap
*hmap
, odp_port_t port_no
)
3297 HMAP_FOR_EACH_IN_BUCKET (tx
, node
, hash_port_no(port_no
), hmap
) {
3298 if (tx
->port
->port_no
== port_no
) {
3307 port_reconfigure(struct dp_netdev_port
*port
)
3309 struct netdev
*netdev
= port
->netdev
;
3312 port
->need_reconfigure
= false;
3314 /* Closes the existing 'rxq's. */
3315 for (i
= 0; i
< port
->n_rxq
; i
++) {
3316 netdev_rxq_close(port
->rxqs
[i
].rx
);
3317 port
->rxqs
[i
].rx
= NULL
;
3319 unsigned last_nrxq
= port
->n_rxq
;
3322 /* Allows 'netdev' to apply the pending configuration changes. */
3323 if (netdev_is_reconf_required(netdev
)) {
3324 err
= netdev_reconfigure(netdev
);
3325 if (err
&& (err
!= EOPNOTSUPP
)) {
3326 VLOG_ERR("Failed to set interface %s new configuration",
3327 netdev_get_name(netdev
));
3331 /* If the netdev_reconfigure() above succeeds, reopens the 'rxq's. */
3332 port
->rxqs
= xrealloc(port
->rxqs
,
3333 sizeof *port
->rxqs
* netdev_n_rxq(netdev
));
3334 /* Realloc 'used' counters for tx queues. */
3335 free(port
->txq_used
);
3336 port
->txq_used
= xcalloc(netdev_n_txq(netdev
), sizeof *port
->txq_used
);
3338 for (i
= 0; i
< netdev_n_rxq(netdev
); i
++) {
3339 bool new_queue
= i
>= last_nrxq
;
3341 memset(&port
->rxqs
[i
], 0, sizeof port
->rxqs
[i
]);
3344 port
->rxqs
[i
].port
= port
;
3346 err
= netdev_rxq_open(netdev
, &port
->rxqs
[i
].rx
, i
);
3353 /* Parse affinity list to apply configuration for new queues. */
3354 dpif_netdev_port_set_rxq_affinity(port
, port
->rxq_affinity_list
);
3359 struct rr_numa_list
{
3360 struct hmap numas
; /* Contains 'struct rr_numa' */
3364 struct hmap_node node
;
3368 /* Non isolated pmds on numa node 'numa_id' */
3369 struct dp_netdev_pmd_thread
**pmds
;
3376 static struct rr_numa
*
3377 rr_numa_list_lookup(struct rr_numa_list
*rr
, int numa_id
)
3379 struct rr_numa
*numa
;
3381 HMAP_FOR_EACH_WITH_HASH (numa
, node
, hash_int(numa_id
, 0), &rr
->numas
) {
3382 if (numa
->numa_id
== numa_id
) {
3390 /* Returns the next node in numa list following 'numa' in round-robin fashion.
3391 * Returns first node if 'numa' is a null pointer or the last node in 'rr'.
3392 * Returns NULL if 'rr' numa list is empty. */
3393 static struct rr_numa
*
3394 rr_numa_list_next(struct rr_numa_list
*rr
, const struct rr_numa
*numa
)
3396 struct hmap_node
*node
= NULL
;
3399 node
= hmap_next(&rr
->numas
, &numa
->node
);
3402 node
= hmap_first(&rr
->numas
);
3405 return (node
) ? CONTAINER_OF(node
, struct rr_numa
, node
) : NULL
;
3409 rr_numa_list_populate(struct dp_netdev
*dp
, struct rr_numa_list
*rr
)
3411 struct dp_netdev_pmd_thread
*pmd
;
3412 struct rr_numa
*numa
;
3414 hmap_init(&rr
->numas
);
3416 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3417 if (pmd
->core_id
== NON_PMD_CORE_ID
|| pmd
->isolated
) {
3421 numa
= rr_numa_list_lookup(rr
, pmd
->numa_id
);
3423 numa
= xzalloc(sizeof *numa
);
3424 numa
->numa_id
= pmd
->numa_id
;
3425 hmap_insert(&rr
->numas
, &numa
->node
, hash_int(pmd
->numa_id
, 0));
3428 numa
->pmds
= xrealloc(numa
->pmds
, numa
->n_pmds
* sizeof *numa
->pmds
);
3429 numa
->pmds
[numa
->n_pmds
- 1] = pmd
;
3430 /* At least one pmd so initialise curr_idx and idx_inc. */
3431 numa
->cur_index
= 0;
3432 numa
->idx_inc
= true;
3436 /* Returns the next pmd from the numa node in
3437 * incrementing or decrementing order. */
3438 static struct dp_netdev_pmd_thread
*
3439 rr_numa_get_pmd(struct rr_numa
*numa
)
3441 int numa_idx
= numa
->cur_index
;
3443 if (numa
->idx_inc
== true) {
3444 /* Incrementing through list of pmds. */
3445 if (numa
->cur_index
== numa
->n_pmds
-1) {
3446 /* Reached the last pmd. */
3447 numa
->idx_inc
= false;
3452 /* Decrementing through list of pmds. */
3453 if (numa
->cur_index
== 0) {
3454 /* Reached the first pmd. */
3455 numa
->idx_inc
= true;
3460 return numa
->pmds
[numa_idx
];
3464 rr_numa_list_destroy(struct rr_numa_list
*rr
)
3466 struct rr_numa
*numa
;
3468 HMAP_FOR_EACH_POP (numa
, node
, &rr
->numas
) {
3472 hmap_destroy(&rr
->numas
);
3475 /* Sort Rx Queues by the processing cycles they are consuming. */
3477 compare_rxq_cycles(const void *a
, const void *b
)
3479 struct dp_netdev_rxq
*qa
;
3480 struct dp_netdev_rxq
*qb
;
3481 uint64_t cycles_qa
, cycles_qb
;
3483 qa
= *(struct dp_netdev_rxq
**) a
;
3484 qb
= *(struct dp_netdev_rxq
**) b
;
3486 cycles_qa
= dp_netdev_rxq_get_cycles(qa
, RXQ_CYCLES_PROC_HIST
);
3487 cycles_qb
= dp_netdev_rxq_get_cycles(qb
, RXQ_CYCLES_PROC_HIST
);
3489 if (cycles_qa
!= cycles_qb
) {
3490 return (cycles_qa
< cycles_qb
) ? 1 : -1;
3492 /* Cycles are the same so tiebreak on port/queue id.
3493 * Tiebreaking (as opposed to return 0) ensures consistent
3494 * sort results across multiple OS's. */
3495 uint32_t port_qa
= odp_to_u32(qa
->port
->port_no
);
3496 uint32_t port_qb
= odp_to_u32(qb
->port
->port_no
);
3497 if (port_qa
!= port_qb
) {
3498 return port_qa
> port_qb
? 1 : -1;
3500 return netdev_rxq_get_queue_id(qa
->rx
)
3501 - netdev_rxq_get_queue_id(qb
->rx
);
3506 /* Assign pmds to queues. If 'pinned' is true, assign pmds to pinned
3507 * queues and marks the pmds as isolated. Otherwise, assign non isolated
3508 * pmds to unpinned queues.
3510 * If 'pinned' is false queues will be sorted by processing cycles they are
3511 * consuming and then assigned to pmds in round robin order.
3513 * The function doesn't touch the pmd threads, it just stores the assignment
3514 * in the 'pmd' member of each rxq. */
3516 rxq_scheduling(struct dp_netdev
*dp
, bool pinned
) OVS_REQUIRES(dp
->port_mutex
)
3518 struct dp_netdev_port
*port
;
3519 struct rr_numa_list rr
;
3520 struct rr_numa
*non_local_numa
= NULL
;
3521 struct dp_netdev_rxq
** rxqs
= NULL
;
3523 struct rr_numa
*numa
= NULL
;
3526 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3527 if (!netdev_is_pmd(port
->netdev
)) {
3531 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
3532 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
3534 if (pinned
&& q
->core_id
!= OVS_CORE_UNSPEC
) {
3535 struct dp_netdev_pmd_thread
*pmd
;
3537 pmd
= dp_netdev_get_pmd(dp
, q
->core_id
);
3539 VLOG_WARN("There is no PMD thread on core %d. Queue "
3540 "%d on port \'%s\' will not be polled.",
3541 q
->core_id
, qid
, netdev_get_name(port
->netdev
));
3544 pmd
->isolated
= true;
3545 dp_netdev_pmd_unref(pmd
);
3547 } else if (!pinned
&& q
->core_id
== OVS_CORE_UNSPEC
) {
3548 uint64_t cycle_hist
= 0;
3551 rxqs
= xmalloc(sizeof *rxqs
);
3553 rxqs
= xrealloc(rxqs
, sizeof *rxqs
* (n_rxqs
+ 1));
3555 /* Sum the queue intervals and store the cycle history. */
3556 for (unsigned i
= 0; i
< PMD_RXQ_INTERVAL_MAX
; i
++) {
3557 cycle_hist
+= dp_netdev_rxq_get_intrvl_cycles(q
, i
);
3559 dp_netdev_rxq_set_cycles(q
, RXQ_CYCLES_PROC_HIST
, cycle_hist
);
3561 /* Store the queue. */
3568 /* Sort the queues in order of the processing cycles
3569 * they consumed during their last pmd interval. */
3570 qsort(rxqs
, n_rxqs
, sizeof *rxqs
, compare_rxq_cycles
);
3573 rr_numa_list_populate(dp
, &rr
);
3574 /* Assign the sorted queues to pmds in round robin. */
3575 for (i
= 0; i
< n_rxqs
; i
++) {
3576 numa_id
= netdev_get_numa_id(rxqs
[i
]->port
->netdev
);
3577 numa
= rr_numa_list_lookup(&rr
, numa_id
);
3579 /* There are no pmds on the queue's local NUMA node.
3580 Round robin on the NUMA nodes that do have pmds. */
3581 non_local_numa
= rr_numa_list_next(&rr
, non_local_numa
);
3582 if (!non_local_numa
) {
3583 VLOG_ERR("There is no available (non-isolated) pmd "
3584 "thread for port \'%s\' queue %d. This queue "
3585 "will not be polled. Is pmd-cpu-mask set to "
3586 "zero? Or are all PMDs isolated to other "
3587 "queues?", netdev_rxq_get_name(rxqs
[i
]->rx
),
3588 netdev_rxq_get_queue_id(rxqs
[i
]->rx
));
3591 rxqs
[i
]->pmd
= rr_numa_get_pmd(non_local_numa
);
3592 VLOG_WARN("There's no available (non-isolated) pmd thread "
3593 "on numa node %d. Queue %d on port \'%s\' will "
3594 "be assigned to the pmd on core %d "
3595 "(numa node %d). Expect reduced performance.",
3596 numa_id
, netdev_rxq_get_queue_id(rxqs
[i
]->rx
),
3597 netdev_rxq_get_name(rxqs
[i
]->rx
),
3598 rxqs
[i
]->pmd
->core_id
, rxqs
[i
]->pmd
->numa_id
);
3600 rxqs
[i
]->pmd
= rr_numa_get_pmd(numa
);
3601 VLOG_INFO("Core %d on numa node %d assigned port \'%s\' "
3602 "rx queue %d (measured processing cycles %"PRIu64
").",
3603 rxqs
[i
]->pmd
->core_id
, numa_id
,
3604 netdev_rxq_get_name(rxqs
[i
]->rx
),
3605 netdev_rxq_get_queue_id(rxqs
[i
]->rx
),
3606 dp_netdev_rxq_get_cycles(rxqs
[i
], RXQ_CYCLES_PROC_HIST
));
3610 rr_numa_list_destroy(&rr
);
3615 reload_affected_pmds(struct dp_netdev
*dp
)
3617 struct dp_netdev_pmd_thread
*pmd
;
3619 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3620 if (pmd
->need_reload
) {
3621 dp_netdev_reload_pmd__(pmd
);
3622 pmd
->need_reload
= false;
3628 reconfigure_pmd_threads(struct dp_netdev
*dp
)
3629 OVS_REQUIRES(dp
->port_mutex
)
3631 struct dp_netdev_pmd_thread
*pmd
;
3632 struct ovs_numa_dump
*pmd_cores
;
3633 struct ovs_numa_info_core
*core
;
3634 struct hmapx to_delete
= HMAPX_INITIALIZER(&to_delete
);
3635 struct hmapx_node
*node
;
3636 bool changed
= false;
3637 bool need_to_adjust_static_tx_qids
= false;
3639 /* The pmd threads should be started only if there's a pmd port in the
3640 * datapath. If the user didn't provide any "pmd-cpu-mask", we start
3641 * NR_PMD_THREADS per numa node. */
3642 if (!has_pmd_port(dp
)) {
3643 pmd_cores
= ovs_numa_dump_n_cores_per_numa(0);
3644 } else if (dp
->pmd_cmask
&& dp
->pmd_cmask
[0]) {
3645 pmd_cores
= ovs_numa_dump_cores_with_cmask(dp
->pmd_cmask
);
3647 pmd_cores
= ovs_numa_dump_n_cores_per_numa(NR_PMD_THREADS
);
3650 /* We need to adjust 'static_tx_qid's only if we're reducing number of
3651 * PMD threads. Otherwise, new threads will allocate all the freed ids. */
3652 if (ovs_numa_dump_count(pmd_cores
) < cmap_count(&dp
->poll_threads
) - 1) {
3653 /* Adjustment is required to keep 'static_tx_qid's sequential and
3654 * avoid possible issues, for example, imbalanced tx queue usage
3655 * and unnecessary locking caused by remapping on netdev level. */
3656 need_to_adjust_static_tx_qids
= true;
3659 /* Check for unwanted pmd threads */
3660 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3661 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3664 if (!ovs_numa_dump_contains_core(pmd_cores
, pmd
->numa_id
,
3666 hmapx_add(&to_delete
, pmd
);
3667 } else if (need_to_adjust_static_tx_qids
) {
3668 pmd
->need_reload
= true;
3672 HMAPX_FOR_EACH (node
, &to_delete
) {
3673 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3674 VLOG_INFO("PMD thread on numa_id: %d, core id: %2d destroyed.",
3675 pmd
->numa_id
, pmd
->core_id
);
3676 dp_netdev_del_pmd(dp
, pmd
);
3678 changed
= !hmapx_is_empty(&to_delete
);
3679 hmapx_destroy(&to_delete
);
3681 if (need_to_adjust_static_tx_qids
) {
3682 /* 'static_tx_qid's are not sequential now.
3683 * Reload remaining threads to fix this. */
3684 reload_affected_pmds(dp
);
3687 /* Check for required new pmd threads */
3688 FOR_EACH_CORE_ON_DUMP(core
, pmd_cores
) {
3689 pmd
= dp_netdev_get_pmd(dp
, core
->core_id
);
3691 pmd
= xzalloc(sizeof *pmd
);
3692 dp_netdev_configure_pmd(pmd
, dp
, core
->core_id
, core
->numa_id
);
3693 pmd
->thread
= ovs_thread_create("pmd", pmd_thread_main
, pmd
);
3694 VLOG_INFO("PMD thread on numa_id: %d, core id: %2d created.",
3695 pmd
->numa_id
, pmd
->core_id
);
3698 dp_netdev_pmd_unref(pmd
);
3703 struct ovs_numa_info_numa
*numa
;
3705 /* Log the number of pmd threads per numa node. */
3706 FOR_EACH_NUMA_ON_DUMP (numa
, pmd_cores
) {
3707 VLOG_INFO("There are %"PRIuSIZE
" pmd threads on numa node %d",
3708 numa
->n_cores
, numa
->numa_id
);
3712 ovs_numa_dump_destroy(pmd_cores
);
3716 pmd_remove_stale_ports(struct dp_netdev
*dp
,
3717 struct dp_netdev_pmd_thread
*pmd
)
3718 OVS_EXCLUDED(pmd
->port_mutex
)
3719 OVS_REQUIRES(dp
->port_mutex
)
3721 struct rxq_poll
*poll
, *poll_next
;
3722 struct tx_port
*tx
, *tx_next
;
3724 ovs_mutex_lock(&pmd
->port_mutex
);
3725 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
3726 struct dp_netdev_port
*port
= poll
->rxq
->port
;
3728 if (port
->need_reconfigure
3729 || !hmap_contains(&dp
->ports
, &port
->node
)) {
3730 dp_netdev_del_rxq_from_pmd(pmd
, poll
);
3733 HMAP_FOR_EACH_SAFE (tx
, tx_next
, node
, &pmd
->tx_ports
) {
3734 struct dp_netdev_port
*port
= tx
->port
;
3736 if (port
->need_reconfigure
3737 || !hmap_contains(&dp
->ports
, &port
->node
)) {
3738 dp_netdev_del_port_tx_from_pmd(pmd
, tx
);
3741 ovs_mutex_unlock(&pmd
->port_mutex
);
3744 /* Must be called each time a port is added/removed or the cmask changes.
3745 * This creates and destroys pmd threads, reconfigures ports, opens their
3746 * rxqs and assigns all rxqs/txqs to pmd threads. */
3748 reconfigure_datapath(struct dp_netdev
*dp
)
3749 OVS_REQUIRES(dp
->port_mutex
)
3751 struct dp_netdev_pmd_thread
*pmd
;
3752 struct dp_netdev_port
*port
;
3755 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
3757 /* Step 1: Adjust the pmd threads based on the datapath ports, the cores
3758 * on the system and the user configuration. */
3759 reconfigure_pmd_threads(dp
);
3761 wanted_txqs
= cmap_count(&dp
->poll_threads
);
3763 /* The number of pmd threads might have changed, or a port can be new:
3764 * adjust the txqs. */
3765 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3766 netdev_set_tx_multiq(port
->netdev
, wanted_txqs
);
3769 /* Step 2: Remove from the pmd threads ports that have been removed or
3770 * need reconfiguration. */
3772 /* Check for all the ports that need reconfiguration. We cache this in
3773 * 'port->need_reconfigure', because netdev_is_reconf_required() can
3774 * change at any time. */
3775 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3776 if (netdev_is_reconf_required(port
->netdev
)) {
3777 port
->need_reconfigure
= true;
3781 /* Remove from the pmd threads all the ports that have been deleted or
3782 * need reconfiguration. */
3783 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3784 pmd_remove_stale_ports(dp
, pmd
);
3787 /* Reload affected pmd threads. We must wait for the pmd threads before
3788 * reconfiguring the ports, because a port cannot be reconfigured while
3789 * it's being used. */
3790 reload_affected_pmds(dp
);
3792 /* Step 3: Reconfigure ports. */
3794 /* We only reconfigure the ports that we determined above, because they're
3795 * not being used by any pmd thread at the moment. If a port fails to
3796 * reconfigure we remove it from the datapath. */
3797 struct dp_netdev_port
*next_port
;
3798 HMAP_FOR_EACH_SAFE (port
, next_port
, node
, &dp
->ports
) {
3801 if (!port
->need_reconfigure
) {
3805 err
= port_reconfigure(port
);
3807 hmap_remove(&dp
->ports
, &port
->node
);
3808 seq_change(dp
->port_seq
);
3811 port
->dynamic_txqs
= netdev_n_txq(port
->netdev
) < wanted_txqs
;
3815 /* Step 4: Compute new rxq scheduling. We don't touch the pmd threads
3816 * for now, we just update the 'pmd' pointer in each rxq to point to the
3817 * wanted thread according to the scheduling policy. */
3819 /* Reset all the pmd threads to non isolated. */
3820 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3821 pmd
->isolated
= false;
3824 /* Reset all the queues to unassigned */
3825 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3826 for (int i
= 0; i
< port
->n_rxq
; i
++) {
3827 port
->rxqs
[i
].pmd
= NULL
;
3831 /* Add pinned queues and mark pmd threads isolated. */
3832 rxq_scheduling(dp
, true);
3834 /* Add non-pinned queues. */
3835 rxq_scheduling(dp
, false);
3837 /* Step 5: Remove queues not compliant with new scheduling. */
3838 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3839 struct rxq_poll
*poll
, *poll_next
;
3841 ovs_mutex_lock(&pmd
->port_mutex
);
3842 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
3843 if (poll
->rxq
->pmd
!= pmd
) {
3844 dp_netdev_del_rxq_from_pmd(pmd
, poll
);
3847 ovs_mutex_unlock(&pmd
->port_mutex
);
3850 /* Reload affected pmd threads. We must wait for the pmd threads to remove
3851 * the old queues before readding them, otherwise a queue can be polled by
3852 * two threads at the same time. */
3853 reload_affected_pmds(dp
);
3855 /* Step 6: Add queues from scheduling, if they're not there already. */
3856 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3857 if (!netdev_is_pmd(port
->netdev
)) {
3861 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
3862 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
3865 ovs_mutex_lock(&q
->pmd
->port_mutex
);
3866 dp_netdev_add_rxq_to_pmd(q
->pmd
, q
);
3867 ovs_mutex_unlock(&q
->pmd
->port_mutex
);
3872 /* Add every port to the tx cache of every pmd thread, if it's not
3873 * there already and if this pmd has at least one rxq to poll. */
3874 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3875 ovs_mutex_lock(&pmd
->port_mutex
);
3876 if (hmap_count(&pmd
->poll_list
) || pmd
->core_id
== NON_PMD_CORE_ID
) {
3877 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3878 dp_netdev_add_port_tx_to_pmd(pmd
, port
);
3881 ovs_mutex_unlock(&pmd
->port_mutex
);
3884 /* Reload affected pmd threads. */
3885 reload_affected_pmds(dp
);
3888 /* Returns true if one of the netdevs in 'dp' requires a reconfiguration */
3890 ports_require_restart(const struct dp_netdev
*dp
)
3891 OVS_REQUIRES(dp
->port_mutex
)
3893 struct dp_netdev_port
*port
;
3895 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3896 if (netdev_is_reconf_required(port
->netdev
)) {
3904 /* Return true if needs to revalidate datapath flows. */
3906 dpif_netdev_run(struct dpif
*dpif
)
3908 struct dp_netdev_port
*port
;
3909 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3910 struct dp_netdev_pmd_thread
*non_pmd
;
3911 uint64_t new_tnl_seq
;
3912 bool need_to_flush
= true;
3914 ovs_mutex_lock(&dp
->port_mutex
);
3915 non_pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
3917 ovs_mutex_lock(&dp
->non_pmd_mutex
);
3918 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3919 if (!netdev_is_pmd(port
->netdev
)) {
3922 for (i
= 0; i
< port
->n_rxq
; i
++) {
3923 if (dp_netdev_process_rxq_port(non_pmd
,
3926 need_to_flush
= false;
3931 if (need_to_flush
) {
3932 /* We didn't receive anything in the process loop.
3933 * Check if we need to send something.
3934 * There was no time updates on current iteration. */
3935 pmd_thread_ctx_time_update(non_pmd
);
3936 dp_netdev_pmd_flush_output_packets(non_pmd
, false);
3939 dpif_netdev_xps_revalidate_pmd(non_pmd
, false);
3940 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
3942 dp_netdev_pmd_unref(non_pmd
);
3945 if (dp_netdev_is_reconf_required(dp
) || ports_require_restart(dp
)) {
3946 reconfigure_datapath(dp
);
3948 ovs_mutex_unlock(&dp
->port_mutex
);
3950 tnl_neigh_cache_run();
3952 new_tnl_seq
= seq_read(tnl_conf_seq
);
3954 if (dp
->last_tnl_conf_seq
!= new_tnl_seq
) {
3955 dp
->last_tnl_conf_seq
= new_tnl_seq
;
3962 dpif_netdev_wait(struct dpif
*dpif
)
3964 struct dp_netdev_port
*port
;
3965 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3967 ovs_mutex_lock(&dp_netdev_mutex
);
3968 ovs_mutex_lock(&dp
->port_mutex
);
3969 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3970 netdev_wait_reconf_required(port
->netdev
);
3971 if (!netdev_is_pmd(port
->netdev
)) {
3974 for (i
= 0; i
< port
->n_rxq
; i
++) {
3975 netdev_rxq_wait(port
->rxqs
[i
].rx
);
3979 ovs_mutex_unlock(&dp
->port_mutex
);
3980 ovs_mutex_unlock(&dp_netdev_mutex
);
3981 seq_wait(tnl_conf_seq
, dp
->last_tnl_conf_seq
);
3985 pmd_free_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
3987 struct tx_port
*tx_port_cached
;
3989 /* Flush all the queued packets. */
3990 dp_netdev_pmd_flush_output_packets(pmd
, true);
3991 /* Free all used tx queue ids. */
3992 dpif_netdev_xps_revalidate_pmd(pmd
, true);
3994 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->tnl_port_cache
) {
3995 free(tx_port_cached
);
3997 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->send_port_cache
) {
3998 free(tx_port_cached
);
4002 /* Copies ports from 'pmd->tx_ports' (shared with the main thread) to
4003 * thread-local copies. Copy to 'pmd->tnl_port_cache' if it is a tunnel
4004 * device, otherwise to 'pmd->send_port_cache' if the port has at least
4007 pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
4008 OVS_REQUIRES(pmd
->port_mutex
)
4010 struct tx_port
*tx_port
, *tx_port_cached
;
4012 pmd_free_cached_ports(pmd
);
4013 hmap_shrink(&pmd
->send_port_cache
);
4014 hmap_shrink(&pmd
->tnl_port_cache
);
4016 HMAP_FOR_EACH (tx_port
, node
, &pmd
->tx_ports
) {
4017 if (netdev_has_tunnel_push_pop(tx_port
->port
->netdev
)) {
4018 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
4019 hmap_insert(&pmd
->tnl_port_cache
, &tx_port_cached
->node
,
4020 hash_port_no(tx_port_cached
->port
->port_no
));
4023 if (netdev_n_txq(tx_port
->port
->netdev
)) {
4024 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
4025 hmap_insert(&pmd
->send_port_cache
, &tx_port_cached
->node
,
4026 hash_port_no(tx_port_cached
->port
->port_no
));
4032 pmd_alloc_static_tx_qid(struct dp_netdev_pmd_thread
*pmd
)
4034 ovs_mutex_lock(&pmd
->dp
->tx_qid_pool_mutex
);
4035 if (!id_pool_alloc_id(pmd
->dp
->tx_qid_pool
, &pmd
->static_tx_qid
)) {
4036 VLOG_ABORT("static_tx_qid allocation failed for PMD on core %2d"
4037 ", numa_id %d.", pmd
->core_id
, pmd
->numa_id
);
4039 ovs_mutex_unlock(&pmd
->dp
->tx_qid_pool_mutex
);
4041 VLOG_DBG("static_tx_qid = %d allocated for PMD thread on core %2d"
4042 ", numa_id %d.", pmd
->static_tx_qid
, pmd
->core_id
, pmd
->numa_id
);
4046 pmd_free_static_tx_qid(struct dp_netdev_pmd_thread
*pmd
)
4048 ovs_mutex_lock(&pmd
->dp
->tx_qid_pool_mutex
);
4049 id_pool_free_id(pmd
->dp
->tx_qid_pool
, pmd
->static_tx_qid
);
4050 ovs_mutex_unlock(&pmd
->dp
->tx_qid_pool_mutex
);
4054 pmd_load_queues_and_ports(struct dp_netdev_pmd_thread
*pmd
,
4055 struct polled_queue
**ppoll_list
)
4057 struct polled_queue
*poll_list
= *ppoll_list
;
4058 struct rxq_poll
*poll
;
4061 ovs_mutex_lock(&pmd
->port_mutex
);
4062 poll_list
= xrealloc(poll_list
, hmap_count(&pmd
->poll_list
)
4063 * sizeof *poll_list
);
4066 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
4067 poll_list
[i
].rxq
= poll
->rxq
;
4068 poll_list
[i
].port_no
= poll
->rxq
->port
->port_no
;
4072 pmd_load_cached_ports(pmd
);
4074 ovs_mutex_unlock(&pmd
->port_mutex
);
4076 *ppoll_list
= poll_list
;
4081 pmd_thread_main(void *f_
)
4083 struct dp_netdev_pmd_thread
*pmd
= f_
;
4084 struct pmd_perf_stats
*s
= &pmd
->perf_stats
;
4085 unsigned int lc
= 0;
4086 struct polled_queue
*poll_list
;
4090 int process_packets
= 0;
4094 /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */
4095 ovsthread_setspecific(pmd
->dp
->per_pmd_key
, pmd
);
4096 ovs_numa_thread_setaffinity_core(pmd
->core_id
);
4097 dpdk_set_lcore_id(pmd
->core_id
);
4098 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
4099 emc_cache_init(&pmd
->flow_cache
);
4101 pmd_alloc_static_tx_qid(pmd
);
4103 /* List port/core affinity */
4104 for (i
= 0; i
< poll_cnt
; i
++) {
4105 VLOG_DBG("Core %d processing port \'%s\' with queue-id %d\n",
4106 pmd
->core_id
, netdev_rxq_get_name(poll_list
[i
].rxq
->rx
),
4107 netdev_rxq_get_queue_id(poll_list
[i
].rxq
->rx
));
4108 /* Reset the rxq current cycles counter. */
4109 dp_netdev_rxq_set_cycles(poll_list
[i
].rxq
, RXQ_CYCLES_PROC_CURR
, 0);
4113 while (seq_read(pmd
->reload_seq
) == pmd
->last_reload_seq
) {
4114 seq_wait(pmd
->reload_seq
, pmd
->last_reload_seq
);
4120 cycles_counter_update(s
);
4122 uint64_t iter_packets
= 0;
4124 pmd_perf_start_iteration(s
);
4125 for (i
= 0; i
< poll_cnt
; i
++) {
4127 dp_netdev_process_rxq_port(pmd
, poll_list
[i
].rxq
,
4128 poll_list
[i
].port_no
);
4129 iter_packets
+= process_packets
;
4132 if (!iter_packets
) {
4133 /* We didn't receive anything in the process loop.
4134 * Check if we need to send something.
4135 * There was no time updates on current iteration. */
4136 pmd_thread_ctx_time_update(pmd
);
4137 iter_packets
+= dp_netdev_pmd_flush_output_packets(pmd
, false);
4145 coverage_try_clear();
4146 dp_netdev_pmd_try_optimize(pmd
, poll_list
, poll_cnt
);
4147 if (!ovsrcu_try_quiesce()) {
4148 emc_cache_slow_sweep(&pmd
->flow_cache
);
4151 atomic_read_relaxed(&pmd
->reload
, &reload
);
4156 pmd_perf_end_iteration(s
, iter_packets
);
4159 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
4160 exiting
= latch_is_set(&pmd
->exit_latch
);
4161 /* Signal here to make sure the pmd finishes
4162 * reloading the updated configuration. */
4163 dp_netdev_pmd_reload_done(pmd
);
4165 pmd_free_static_tx_qid(pmd
);
4171 emc_cache_uninit(&pmd
->flow_cache
);
4173 pmd_free_cached_ports(pmd
);
4178 dp_netdev_disable_upcall(struct dp_netdev
*dp
)
4179 OVS_ACQUIRES(dp
->upcall_rwlock
)
4181 fat_rwlock_wrlock(&dp
->upcall_rwlock
);
4187 dpif_netdev_meter_get_features(const struct dpif
* dpif OVS_UNUSED
,
4188 struct ofputil_meter_features
*features
)
4190 features
->max_meters
= MAX_METERS
;
4191 features
->band_types
= DP_SUPPORTED_METER_BAND_TYPES
;
4192 features
->capabilities
= DP_SUPPORTED_METER_FLAGS_MASK
;
4193 features
->max_bands
= MAX_BANDS
;
4194 features
->max_color
= 0;
4197 /* Returns false when packet needs to be dropped. */
4199 dp_netdev_run_meter(struct dp_netdev
*dp
, struct dp_packet_batch
*packets_
,
4200 uint32_t meter_id
, long long int now
)
4202 struct dp_meter
*meter
;
4203 struct dp_meter_band
*band
;
4204 struct dp_packet
*packet
;
4205 long long int long_delta_t
; /* msec */
4206 uint32_t delta_t
; /* msec */
4208 const size_t cnt
= dp_packet_batch_size(packets_
);
4209 uint32_t bytes
, volume
;
4210 int exceeded_band
[NETDEV_MAX_BURST
];
4211 uint32_t exceeded_rate
[NETDEV_MAX_BURST
];
4212 int exceeded_pkt
= cnt
; /* First packet that exceeded a band rate. */
4214 if (meter_id
>= MAX_METERS
) {
4218 meter_lock(dp
, meter_id
);
4219 meter
= dp
->meters
[meter_id
];
4224 /* Initialize as negative values. */
4225 memset(exceeded_band
, 0xff, cnt
* sizeof *exceeded_band
);
4226 /* Initialize as zeroes. */
4227 memset(exceeded_rate
, 0, cnt
* sizeof *exceeded_rate
);
4229 /* All packets will hit the meter at the same time. */
4230 long_delta_t
= (now
- meter
->used
) / 1000; /* msec */
4232 /* Make sure delta_t will not be too large, so that bucket will not
4233 * wrap around below. */
4234 delta_t
= (long_delta_t
> (long long int)meter
->max_delta_t
)
4235 ? meter
->max_delta_t
: (uint32_t)long_delta_t
;
4237 /* Update meter stats. */
4239 meter
->packet_count
+= cnt
;
4241 DP_PACKET_BATCH_FOR_EACH (packet
, packets_
) {
4242 bytes
+= dp_packet_size(packet
);
4244 meter
->byte_count
+= bytes
;
4246 /* Meters can operate in terms of packets per second or kilobits per
4248 if (meter
->flags
& OFPMF13_PKTPS
) {
4249 /* Rate in packets/second, bucket 1/1000 packets. */
4250 /* msec * packets/sec = 1/1000 packets. */
4251 volume
= cnt
* 1000; /* Take 'cnt' packets from the bucket. */
4253 /* Rate in kbps, bucket in bits. */
4254 /* msec * kbps = bits */
4258 /* Update all bands and find the one hit with the highest rate for each
4259 * packet (if any). */
4260 for (int m
= 0; m
< meter
->n_bands
; ++m
) {
4261 band
= &meter
->bands
[m
];
4263 /* Update band's bucket. */
4264 band
->bucket
+= delta_t
* band
->up
.rate
;
4265 if (band
->bucket
> band
->up
.burst_size
) {
4266 band
->bucket
= band
->up
.burst_size
;
4269 /* Drain the bucket for all the packets, if possible. */
4270 if (band
->bucket
>= volume
) {
4271 band
->bucket
-= volume
;
4273 int band_exceeded_pkt
;
4275 /* Band limit hit, must process packet-by-packet. */
4276 if (meter
->flags
& OFPMF13_PKTPS
) {
4277 band_exceeded_pkt
= band
->bucket
/ 1000;
4278 band
->bucket
%= 1000; /* Remainder stays in bucket. */
4280 /* Update the exceeding band for each exceeding packet.
4281 * (Only one band will be fired by a packet, and that
4282 * can be different for each packet.) */
4283 for (i
= band_exceeded_pkt
; i
< cnt
; i
++) {
4284 if (band
->up
.rate
> exceeded_rate
[i
]) {
4285 exceeded_rate
[i
] = band
->up
.rate
;
4286 exceeded_band
[i
] = m
;
4290 /* Packet sizes differ, must process one-by-one. */
4291 band_exceeded_pkt
= cnt
;
4292 DP_PACKET_BATCH_FOR_EACH (packet
, packets_
) {
4293 uint32_t bits
= dp_packet_size(packet
) * 8;
4295 if (band
->bucket
>= bits
) {
4296 band
->bucket
-= bits
;
4298 if (i
< band_exceeded_pkt
) {
4299 band_exceeded_pkt
= i
;
4301 /* Update the exceeding band for the exceeding packet.
4302 * (Only one band will be fired by a packet, and that
4303 * can be different for each packet.) */
4304 if (band
->up
.rate
> exceeded_rate
[i
]) {
4305 exceeded_rate
[i
] = band
->up
.rate
;
4306 exceeded_band
[i
] = m
;
4311 /* Remember the first exceeding packet. */
4312 if (exceeded_pkt
> band_exceeded_pkt
) {
4313 exceeded_pkt
= band_exceeded_pkt
;
4318 /* Fire the highest rate band exceeded by each packet.
4319 * Drop packets if needed, by swapping packet to the end that will be
4322 DP_PACKET_BATCH_REFILL_FOR_EACH (j
, cnt
, packet
, packets_
) {
4323 if (exceeded_band
[j
] >= 0) {
4324 /* Meter drop packet. */
4325 band
= &meter
->bands
[exceeded_band
[j
]];
4326 band
->packet_count
+= 1;
4327 band
->byte_count
+= dp_packet_size(packet
);
4329 dp_packet_delete(packet
);
4331 /* Meter accepts packet. */
4332 dp_packet_batch_refill(packets_
, packet
, j
);
4336 meter_unlock(dp
, meter_id
);
4339 /* Meter set/get/del processing is still single-threaded. */
4341 dpif_netdev_meter_set(struct dpif
*dpif
, ofproto_meter_id
*meter_id
,
4342 struct ofputil_meter_config
*config
)
4344 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4345 uint32_t mid
= meter_id
->uint32
;
4346 struct dp_meter
*meter
;
4349 if (mid
>= MAX_METERS
) {
4350 return EFBIG
; /* Meter_id out of range. */
4353 if (config
->flags
& ~DP_SUPPORTED_METER_FLAGS_MASK
||
4354 !(config
->flags
& (OFPMF13_KBPS
| OFPMF13_PKTPS
))) {
4355 return EBADF
; /* Unsupported flags set */
4358 /* Validate bands */
4359 if (config
->n_bands
== 0 || config
->n_bands
> MAX_BANDS
) {
4360 return EINVAL
; /* Too many bands */
4363 /* Validate rates */
4364 for (i
= 0; i
< config
->n_bands
; i
++) {
4365 if (config
->bands
[i
].rate
== 0) {
4366 return EDOM
; /* rate must be non-zero */
4370 for (i
= 0; i
< config
->n_bands
; ++i
) {
4371 switch (config
->bands
[i
].type
) {
4375 return ENODEV
; /* Unsupported band type */
4379 /* Allocate meter */
4380 meter
= xzalloc(sizeof *meter
4381 + config
->n_bands
* sizeof(struct dp_meter_band
));
4383 meter
->flags
= config
->flags
;
4384 meter
->n_bands
= config
->n_bands
;
4385 meter
->max_delta_t
= 0;
4386 meter
->used
= time_usec();
4389 for (i
= 0; i
< config
->n_bands
; ++i
) {
4390 uint32_t band_max_delta_t
;
4392 /* Set burst size to a workable value if none specified. */
4393 if (config
->bands
[i
].burst_size
== 0) {
4394 config
->bands
[i
].burst_size
= config
->bands
[i
].rate
;
4397 meter
->bands
[i
].up
= config
->bands
[i
];
4398 /* Convert burst size to the bucket units: */
4399 /* pkts => 1/1000 packets, kilobits => bits. */
4400 meter
->bands
[i
].up
.burst_size
*= 1000;
4401 /* Initialize bucket to empty. */
4402 meter
->bands
[i
].bucket
= 0;
4404 /* Figure out max delta_t that is enough to fill any bucket. */
4406 = meter
->bands
[i
].up
.burst_size
/ meter
->bands
[i
].up
.rate
;
4407 if (band_max_delta_t
> meter
->max_delta_t
) {
4408 meter
->max_delta_t
= band_max_delta_t
;
4412 meter_lock(dp
, mid
);
4413 dp_delete_meter(dp
, mid
); /* Free existing meter, if any */
4414 dp
->meters
[mid
] = meter
;
4415 meter_unlock(dp
, mid
);
4423 dpif_netdev_meter_get(const struct dpif
*dpif
,
4424 ofproto_meter_id meter_id_
,
4425 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
4427 const struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4428 const struct dp_meter
*meter
;
4429 uint32_t meter_id
= meter_id_
.uint32
;
4431 if (meter_id
>= MAX_METERS
) {
4434 meter
= dp
->meters
[meter_id
];
4441 meter_lock(dp
, meter_id
);
4442 stats
->packet_in_count
= meter
->packet_count
;
4443 stats
->byte_in_count
= meter
->byte_count
;
4445 for (i
= 0; i
< n_bands
&& i
< meter
->n_bands
; ++i
) {
4446 stats
->bands
[i
].packet_count
= meter
->bands
[i
].packet_count
;
4447 stats
->bands
[i
].byte_count
= meter
->bands
[i
].byte_count
;
4449 meter_unlock(dp
, meter_id
);
4457 dpif_netdev_meter_del(struct dpif
*dpif
,
4458 ofproto_meter_id meter_id_
,
4459 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
4461 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4464 error
= dpif_netdev_meter_get(dpif
, meter_id_
, stats
, n_bands
);
4466 uint32_t meter_id
= meter_id_
.uint32
;
4468 meter_lock(dp
, meter_id
);
4469 dp_delete_meter(dp
, meter_id
);
4470 meter_unlock(dp
, meter_id
);
4477 dpif_netdev_disable_upcall(struct dpif
*dpif
)
4478 OVS_NO_THREAD_SAFETY_ANALYSIS
4480 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4481 dp_netdev_disable_upcall(dp
);
4485 dp_netdev_enable_upcall(struct dp_netdev
*dp
)
4486 OVS_RELEASES(dp
->upcall_rwlock
)
4488 fat_rwlock_unlock(&dp
->upcall_rwlock
);
4492 dpif_netdev_enable_upcall(struct dpif
*dpif
)
4493 OVS_NO_THREAD_SAFETY_ANALYSIS
4495 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4496 dp_netdev_enable_upcall(dp
);
4500 dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
)
4502 ovs_mutex_lock(&pmd
->cond_mutex
);
4503 atomic_store_relaxed(&pmd
->reload
, false);
4504 pmd
->last_reload_seq
= seq_read(pmd
->reload_seq
);
4505 xpthread_cond_signal(&pmd
->cond
);
4506 ovs_mutex_unlock(&pmd
->cond_mutex
);
4509 /* Finds and refs the dp_netdev_pmd_thread on core 'core_id'. Returns
4510 * the pointer if succeeds, otherwise, NULL (it can return NULL even if
4511 * 'core_id' is NON_PMD_CORE_ID).
4513 * Caller must unrefs the returned reference. */
4514 static struct dp_netdev_pmd_thread
*
4515 dp_netdev_get_pmd(struct dp_netdev
*dp
, unsigned core_id
)
4517 struct dp_netdev_pmd_thread
*pmd
;
4518 const struct cmap_node
*pnode
;
4520 pnode
= cmap_find(&dp
->poll_threads
, hash_int(core_id
, 0));
4524 pmd
= CONTAINER_OF(pnode
, struct dp_netdev_pmd_thread
, node
);
4526 return dp_netdev_pmd_try_ref(pmd
) ? pmd
: NULL
;
4529 /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */
4531 dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
4532 OVS_REQUIRES(dp
->port_mutex
)
4534 struct dp_netdev_pmd_thread
*non_pmd
;
4536 non_pmd
= xzalloc(sizeof *non_pmd
);
4537 dp_netdev_configure_pmd(non_pmd
, dp
, NON_PMD_CORE_ID
, OVS_NUMA_UNSPEC
);
4540 /* Caller must have valid pointer to 'pmd'. */
4542 dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
)
4544 return ovs_refcount_try_ref_rcu(&pmd
->ref_cnt
);
4548 dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
)
4550 if (pmd
&& ovs_refcount_unref(&pmd
->ref_cnt
) == 1) {
4551 ovsrcu_postpone(dp_netdev_destroy_pmd
, pmd
);
4555 /* Given cmap position 'pos', tries to ref the next node. If try_ref()
4556 * fails, keeps checking for next node until reaching the end of cmap.
4558 * Caller must unrefs the returned reference. */
4559 static struct dp_netdev_pmd_thread
*
4560 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
)
4562 struct dp_netdev_pmd_thread
*next
;
4565 struct cmap_node
*node
;
4567 node
= cmap_next_position(&dp
->poll_threads
, pos
);
4568 next
= node
? CONTAINER_OF(node
, struct dp_netdev_pmd_thread
, node
)
4570 } while (next
&& !dp_netdev_pmd_try_ref(next
));
4575 /* Configures the 'pmd' based on the input argument. */
4577 dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
, struct dp_netdev
*dp
,
4578 unsigned core_id
, int numa_id
)
4581 pmd
->core_id
= core_id
;
4582 pmd
->numa_id
= numa_id
;
4583 pmd
->need_reload
= false;
4584 pmd
->n_output_batches
= 0;
4586 ovs_refcount_init(&pmd
->ref_cnt
);
4587 latch_init(&pmd
->exit_latch
);
4588 pmd
->reload_seq
= seq_create();
4589 pmd
->last_reload_seq
= seq_read(pmd
->reload_seq
);
4590 atomic_init(&pmd
->reload
, false);
4591 xpthread_cond_init(&pmd
->cond
, NULL
);
4592 ovs_mutex_init(&pmd
->cond_mutex
);
4593 ovs_mutex_init(&pmd
->flow_mutex
);
4594 ovs_mutex_init(&pmd
->port_mutex
);
4595 cmap_init(&pmd
->flow_table
);
4596 cmap_init(&pmd
->classifiers
);
4597 pmd
->ctx
.last_rxq
= NULL
;
4598 pmd_thread_ctx_time_update(pmd
);
4599 pmd
->next_optimization
= pmd
->ctx
.now
+ DPCLS_OPTIMIZATION_INTERVAL
;
4600 pmd
->rxq_next_cycle_store
= pmd
->ctx
.now
+ PMD_RXQ_INTERVAL_LEN
;
4601 hmap_init(&pmd
->poll_list
);
4602 hmap_init(&pmd
->tx_ports
);
4603 hmap_init(&pmd
->tnl_port_cache
);
4604 hmap_init(&pmd
->send_port_cache
);
4605 /* init the 'flow_cache' since there is no
4606 * actual thread created for NON_PMD_CORE_ID. */
4607 if (core_id
== NON_PMD_CORE_ID
) {
4608 emc_cache_init(&pmd
->flow_cache
);
4609 pmd_alloc_static_tx_qid(pmd
);
4611 pmd_perf_stats_init(&pmd
->perf_stats
);
4612 cmap_insert(&dp
->poll_threads
, CONST_CAST(struct cmap_node
*, &pmd
->node
),
4613 hash_int(core_id
, 0));
4617 dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
)
4621 dp_netdev_pmd_flow_flush(pmd
);
4622 hmap_destroy(&pmd
->send_port_cache
);
4623 hmap_destroy(&pmd
->tnl_port_cache
);
4624 hmap_destroy(&pmd
->tx_ports
);
4625 hmap_destroy(&pmd
->poll_list
);
4626 /* All flows (including their dpcls_rules) have been deleted already */
4627 CMAP_FOR_EACH (cls
, node
, &pmd
->classifiers
) {
4629 ovsrcu_postpone(free
, cls
);
4631 cmap_destroy(&pmd
->classifiers
);
4632 cmap_destroy(&pmd
->flow_table
);
4633 ovs_mutex_destroy(&pmd
->flow_mutex
);
4634 latch_destroy(&pmd
->exit_latch
);
4635 seq_destroy(pmd
->reload_seq
);
4636 xpthread_cond_destroy(&pmd
->cond
);
4637 ovs_mutex_destroy(&pmd
->cond_mutex
);
4638 ovs_mutex_destroy(&pmd
->port_mutex
);
4642 /* Stops the pmd thread, removes it from the 'dp->poll_threads',
4643 * and unrefs the struct. */
4645 dp_netdev_del_pmd(struct dp_netdev
*dp
, struct dp_netdev_pmd_thread
*pmd
)
4647 /* NON_PMD_CORE_ID doesn't have a thread, so we don't have to synchronize,
4648 * but extra cleanup is necessary */
4649 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
4650 ovs_mutex_lock(&dp
->non_pmd_mutex
);
4651 emc_cache_uninit(&pmd
->flow_cache
);
4652 pmd_free_cached_ports(pmd
);
4653 pmd_free_static_tx_qid(pmd
);
4654 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
4656 latch_set(&pmd
->exit_latch
);
4657 dp_netdev_reload_pmd__(pmd
);
4658 xpthread_join(pmd
->thread
, NULL
);
4661 dp_netdev_pmd_clear_ports(pmd
);
4663 /* Purges the 'pmd''s flows after stopping the thread, but before
4664 * destroying the flows, so that the flow stats can be collected. */
4665 if (dp
->dp_purge_cb
) {
4666 dp
->dp_purge_cb(dp
->dp_purge_aux
, pmd
->core_id
);
4668 cmap_remove(&pmd
->dp
->poll_threads
, &pmd
->node
, hash_int(pmd
->core_id
, 0));
4669 dp_netdev_pmd_unref(pmd
);
4672 /* Destroys all pmd threads. If 'non_pmd' is true it also destroys the non pmd
4675 dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
, bool non_pmd
)
4677 struct dp_netdev_pmd_thread
*pmd
;
4678 struct dp_netdev_pmd_thread
**pmd_list
;
4679 size_t k
= 0, n_pmds
;
4681 n_pmds
= cmap_count(&dp
->poll_threads
);
4682 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
4684 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4685 if (!non_pmd
&& pmd
->core_id
== NON_PMD_CORE_ID
) {
4688 /* We cannot call dp_netdev_del_pmd(), since it alters
4689 * 'dp->poll_threads' (while we're iterating it) and it
4691 ovs_assert(k
< n_pmds
);
4692 pmd_list
[k
++] = pmd
;
4695 for (size_t i
= 0; i
< k
; i
++) {
4696 dp_netdev_del_pmd(dp
, pmd_list
[i
]);
4701 /* Deletes all rx queues from pmd->poll_list and all the ports from
4704 dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
)
4706 struct rxq_poll
*poll
;
4707 struct tx_port
*port
;
4709 ovs_mutex_lock(&pmd
->port_mutex
);
4710 HMAP_FOR_EACH_POP (poll
, node
, &pmd
->poll_list
) {
4713 HMAP_FOR_EACH_POP (port
, node
, &pmd
->tx_ports
) {
4716 ovs_mutex_unlock(&pmd
->port_mutex
);
4719 /* Adds rx queue to poll_list of PMD thread, if it's not there already. */
4721 dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
4722 struct dp_netdev_rxq
*rxq
)
4723 OVS_REQUIRES(pmd
->port_mutex
)
4725 int qid
= netdev_rxq_get_queue_id(rxq
->rx
);
4726 uint32_t hash
= hash_2words(odp_to_u32(rxq
->port
->port_no
), qid
);
4727 struct rxq_poll
*poll
;
4729 HMAP_FOR_EACH_WITH_HASH (poll
, node
, hash
, &pmd
->poll_list
) {
4730 if (poll
->rxq
== rxq
) {
4731 /* 'rxq' is already polled by this thread. Do nothing. */
4736 poll
= xmalloc(sizeof *poll
);
4738 hmap_insert(&pmd
->poll_list
, &poll
->node
, hash
);
4740 pmd
->need_reload
= true;
4743 /* Delete 'poll' from poll_list of PMD thread. */
4745 dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
4746 struct rxq_poll
*poll
)
4747 OVS_REQUIRES(pmd
->port_mutex
)
4749 hmap_remove(&pmd
->poll_list
, &poll
->node
);
4752 pmd
->need_reload
= true;
4755 /* Add 'port' to the tx port cache of 'pmd', which must be reloaded for the
4756 * changes to take effect. */
4758 dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
4759 struct dp_netdev_port
*port
)
4760 OVS_REQUIRES(pmd
->port_mutex
)
4764 tx
= tx_port_lookup(&pmd
->tx_ports
, port
->port_no
);
4766 /* 'port' is already on this thread tx cache. Do nothing. */
4770 tx
= xzalloc(sizeof *tx
);
4774 tx
->flush_time
= 0LL;
4775 dp_packet_batch_init(&tx
->output_pkts
);
4777 hmap_insert(&pmd
->tx_ports
, &tx
->node
, hash_port_no(tx
->port
->port_no
));
4778 pmd
->need_reload
= true;
4781 /* Del 'tx' from the tx port cache of 'pmd', which must be reloaded for the
4782 * changes to take effect. */
4784 dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
4786 OVS_REQUIRES(pmd
->port_mutex
)
4788 hmap_remove(&pmd
->tx_ports
, &tx
->node
);
4790 pmd
->need_reload
= true;
4794 dpif_netdev_get_datapath_version(void)
4796 return xstrdup("<built-in>");
4800 dp_netdev_flow_used(struct dp_netdev_flow
*netdev_flow
, int cnt
, int size
,
4801 uint16_t tcp_flags
, long long now
)
4805 atomic_store_relaxed(&netdev_flow
->stats
.used
, now
);
4806 non_atomic_ullong_add(&netdev_flow
->stats
.packet_count
, cnt
);
4807 non_atomic_ullong_add(&netdev_flow
->stats
.byte_count
, size
);
4808 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
4810 atomic_store_relaxed(&netdev_flow
->stats
.tcp_flags
, flags
);
4814 dp_netdev_upcall(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet
*packet_
,
4815 struct flow
*flow
, struct flow_wildcards
*wc
, ovs_u128
*ufid
,
4816 enum dpif_upcall_type type
, const struct nlattr
*userdata
,
4817 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
4819 struct dp_netdev
*dp
= pmd
->dp
;
4821 if (OVS_UNLIKELY(!dp
->upcall_cb
)) {
4825 if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl
))) {
4826 struct ds ds
= DS_EMPTY_INITIALIZER
;
4829 struct odp_flow_key_parms odp_parms
= {
4831 .mask
= wc
? &wc
->masks
: NULL
,
4832 .support
= dp_netdev_support
,
4835 ofpbuf_init(&key
, 0);
4836 odp_flow_key_from_flow(&odp_parms
, &key
);
4837 packet_str
= ofp_dp_packet_to_string(packet_
);
4839 odp_flow_key_format(key
.data
, key
.size
, &ds
);
4841 VLOG_DBG("%s: %s upcall:\n%s\n%s", dp
->name
,
4842 dpif_upcall_type_to_string(type
), ds_cstr(&ds
), packet_str
);
4844 ofpbuf_uninit(&key
);
4850 return dp
->upcall_cb(packet_
, flow
, ufid
, pmd
->core_id
, type
, userdata
,
4851 actions
, wc
, put_actions
, dp
->upcall_aux
);
4854 static inline uint32_t
4855 dpif_netdev_packet_get_rss_hash_orig_pkt(struct dp_packet
*packet
,
4856 const struct miniflow
*mf
)
4860 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
4861 hash
= dp_packet_get_rss_hash(packet
);
4863 hash
= miniflow_hash_5tuple(mf
, 0);
4864 dp_packet_set_rss_hash(packet
, hash
);
4870 static inline uint32_t
4871 dpif_netdev_packet_get_rss_hash(struct dp_packet
*packet
,
4872 const struct miniflow
*mf
)
4874 uint32_t hash
, recirc_depth
;
4876 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
4877 hash
= dp_packet_get_rss_hash(packet
);
4879 hash
= miniflow_hash_5tuple(mf
, 0);
4880 dp_packet_set_rss_hash(packet
, hash
);
4883 /* The RSS hash must account for the recirculation depth to avoid
4884 * collisions in the exact match cache */
4885 recirc_depth
= *recirc_depth_get_unsafe();
4886 if (OVS_UNLIKELY(recirc_depth
)) {
4887 hash
= hash_finish(hash
, recirc_depth
);
4888 dp_packet_set_rss_hash(packet
, hash
);
4893 struct packet_batch_per_flow
{
4894 unsigned int byte_count
;
4896 struct dp_netdev_flow
*flow
;
4898 struct dp_packet_batch array
;
4902 packet_batch_per_flow_update(struct packet_batch_per_flow
*batch
,
4903 struct dp_packet
*packet
,
4904 const struct miniflow
*mf
)
4906 batch
->byte_count
+= dp_packet_size(packet
);
4907 batch
->tcp_flags
|= miniflow_get_tcp_flags(mf
);
4908 batch
->array
.packets
[batch
->array
.count
++] = packet
;
4912 packet_batch_per_flow_init(struct packet_batch_per_flow
*batch
,
4913 struct dp_netdev_flow
*flow
)
4915 flow
->batch
= batch
;
4918 dp_packet_batch_init(&batch
->array
);
4919 batch
->byte_count
= 0;
4920 batch
->tcp_flags
= 0;
4924 packet_batch_per_flow_execute(struct packet_batch_per_flow
*batch
,
4925 struct dp_netdev_pmd_thread
*pmd
)
4927 struct dp_netdev_actions
*actions
;
4928 struct dp_netdev_flow
*flow
= batch
->flow
;
4930 dp_netdev_flow_used(flow
, batch
->array
.count
, batch
->byte_count
,
4931 batch
->tcp_flags
, pmd
->ctx
.now
/ 1000);
4933 actions
= dp_netdev_flow_get_actions(flow
);
4935 dp_netdev_execute_actions(pmd
, &batch
->array
, true, &flow
->flow
,
4936 actions
->actions
, actions
->size
);
4940 dp_netdev_queue_batches(struct dp_packet
*pkt
,
4941 struct dp_netdev_flow
*flow
, const struct miniflow
*mf
,
4942 struct packet_batch_per_flow
*batches
,
4945 struct packet_batch_per_flow
*batch
= flow
->batch
;
4947 if (OVS_UNLIKELY(!batch
)) {
4948 batch
= &batches
[(*n_batches
)++];
4949 packet_batch_per_flow_init(batch
, flow
);
4952 packet_batch_per_flow_update(batch
, pkt
, mf
);
4955 /* Try to process all ('cnt') the 'packets' using only the exact match cache
4956 * 'pmd->flow_cache'. If a flow is not found for a packet 'packets[i]', the
4957 * miniflow is copied into 'keys' and the packet pointer is moved at the
4958 * beginning of the 'packets' array.
4960 * The function returns the number of packets that needs to be processed in the
4961 * 'packets' array (they have been moved to the beginning of the vector).
4963 * For performance reasons a caller may choose not to initialize the metadata
4964 * in 'packets_'. If 'md_is_valid' is false, the metadata in 'packets'
4965 * is not valid and must be initialized by this function using 'port_no'.
4966 * If 'md_is_valid' is true, the metadata is already valid and 'port_no'
4969 static inline size_t
4970 emc_processing(struct dp_netdev_pmd_thread
*pmd
,
4971 struct dp_packet_batch
*packets_
,
4972 struct netdev_flow_key
*keys
,
4973 struct packet_batch_per_flow batches
[], size_t *n_batches
,
4974 bool md_is_valid
, odp_port_t port_no
)
4976 struct emc_cache
*flow_cache
= &pmd
->flow_cache
;
4977 struct netdev_flow_key
*key
= &keys
[0];
4978 size_t n_missed
= 0, n_dropped
= 0;
4979 struct dp_packet
*packet
;
4980 const size_t cnt
= dp_packet_batch_size(packets_
);
4984 atomic_read_relaxed(&pmd
->dp
->emc_insert_min
, &cur_min
);
4985 pmd_perf_update_counter(&pmd
->perf_stats
,
4986 md_is_valid
? PMD_STAT_RECIRC
: PMD_STAT_RECV
,
4989 DP_PACKET_BATCH_REFILL_FOR_EACH (i
, cnt
, packet
, packets_
) {
4990 struct dp_netdev_flow
*flow
;
4992 if (OVS_UNLIKELY(dp_packet_size(packet
) < ETH_HEADER_LEN
)) {
4993 dp_packet_delete(packet
);
4999 struct dp_packet
**packets
= packets_
->packets
;
5000 /* Prefetch next packet data and metadata. */
5001 OVS_PREFETCH(dp_packet_data(packets
[i
+1]));
5002 pkt_metadata_prefetch_init(&packets
[i
+1]->md
);
5006 pkt_metadata_init(&packet
->md
, port_no
);
5008 miniflow_extract(packet
, &key
->mf
);
5009 key
->len
= 0; /* Not computed yet. */
5010 /* If EMC is disabled skip hash computation and emc_lookup */
5013 key
->hash
= dpif_netdev_packet_get_rss_hash_orig_pkt(packet
,
5016 key
->hash
= dpif_netdev_packet_get_rss_hash(packet
, &key
->mf
);
5018 flow
= emc_lookup(flow_cache
, key
);
5022 if (OVS_LIKELY(flow
)) {
5023 dp_netdev_queue_batches(packet
, flow
, &key
->mf
, batches
,
5026 /* Exact match cache missed. Group missed packets together at
5027 * the beginning of the 'packets' array. */
5028 dp_packet_batch_refill(packets_
, packet
, i
);
5029 /* 'key[n_missed]' contains the key of the current packet and it
5030 * must be returned to the caller. The next key should be extracted
5031 * to 'keys[n_missed + 1]'. */
5032 key
= &keys
[++n_missed
];
5036 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_EXACT_HIT
,
5037 cnt
- n_dropped
- n_missed
);
5039 return dp_packet_batch_size(packets_
);
5043 handle_packet_upcall(struct dp_netdev_pmd_thread
*pmd
,
5044 struct dp_packet
*packet
,
5045 const struct netdev_flow_key
*key
,
5046 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
5048 struct ofpbuf
*add_actions
;
5049 struct dp_packet_batch b
;
5054 match
.tun_md
.valid
= false;
5055 miniflow_expand(&key
->mf
, &match
.flow
);
5057 ofpbuf_clear(actions
);
5058 ofpbuf_clear(put_actions
);
5060 dpif_flow_hash(pmd
->dp
->dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
5061 error
= dp_netdev_upcall(pmd
, packet
, &match
.flow
, &match
.wc
,
5062 &ufid
, DPIF_UC_MISS
, NULL
, actions
,
5064 if (OVS_UNLIKELY(error
&& error
!= ENOSPC
)) {
5065 dp_packet_delete(packet
);
5069 /* The Netlink encoding of datapath flow keys cannot express
5070 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
5071 * tag is interpreted as exact match on the fact that there is no
5072 * VLAN. Unless we refactor a lot of code that translates between
5073 * Netlink and struct flow representations, we have to do the same
5075 if (!match
.wc
.masks
.vlans
[0].tci
) {
5076 match
.wc
.masks
.vlans
[0].tci
= htons(0xffff);
5079 /* We can't allow the packet batching in the next loop to execute
5080 * the actions. Otherwise, if there are any slow path actions,
5081 * we'll send the packet up twice. */
5082 dp_packet_batch_init_packet(&b
, packet
);
5083 dp_netdev_execute_actions(pmd
, &b
, true, &match
.flow
,
5084 actions
->data
, actions
->size
);
5086 add_actions
= put_actions
->size
? put_actions
: actions
;
5087 if (OVS_LIKELY(error
!= ENOSPC
)) {
5088 struct dp_netdev_flow
*netdev_flow
;
5090 /* XXX: There's a race window where a flow covering this packet
5091 * could have already been installed since we last did the flow
5092 * lookup before upcall. This could be solved by moving the
5093 * mutex lock outside the loop, but that's an awful long time
5094 * to be locking everyone out of making flow installs. If we
5095 * move to a per-core classifier, it would be reasonable. */
5096 ovs_mutex_lock(&pmd
->flow_mutex
);
5097 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
, NULL
);
5098 if (OVS_LIKELY(!netdev_flow
)) {
5099 netdev_flow
= dp_netdev_flow_add(pmd
, &match
, &ufid
,
5103 ovs_mutex_unlock(&pmd
->flow_mutex
);
5104 emc_probabilistic_insert(pmd
, key
, netdev_flow
);
5110 fast_path_processing(struct dp_netdev_pmd_thread
*pmd
,
5111 struct dp_packet_batch
*packets_
,
5112 struct netdev_flow_key
*keys
,
5113 struct packet_batch_per_flow batches
[],
5117 const size_t cnt
= dp_packet_batch_size(packets_
);
5118 #if !defined(__CHECKER__) && !defined(_WIN32)
5119 const size_t PKT_ARRAY_SIZE
= cnt
;
5121 /* Sparse or MSVC doesn't like variable length array. */
5122 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
5124 struct dp_packet
*packet
;
5126 struct dpcls_rule
*rules
[PKT_ARRAY_SIZE
];
5127 struct dp_netdev
*dp
= pmd
->dp
;
5128 int upcall_ok_cnt
= 0, upcall_fail_cnt
= 0;
5129 int lookup_cnt
= 0, add_lookup_cnt
;
5133 for (i
= 0; i
< cnt
; i
++) {
5134 /* Key length is needed in all the cases, hash computed on demand. */
5135 keys
[i
].len
= netdev_flow_key_size(miniflow_n_values(&keys
[i
].mf
));
5137 /* Get the classifier for the in_port */
5138 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
5139 if (OVS_LIKELY(cls
)) {
5140 any_miss
= !dpcls_lookup(cls
, keys
, rules
, cnt
, &lookup_cnt
);
5143 memset(rules
, 0, sizeof(rules
));
5145 if (OVS_UNLIKELY(any_miss
) && !fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
5146 uint64_t actions_stub
[512 / 8], slow_stub
[512 / 8];
5147 struct ofpbuf actions
, put_actions
;
5149 ofpbuf_use_stub(&actions
, actions_stub
, sizeof actions_stub
);
5150 ofpbuf_use_stub(&put_actions
, slow_stub
, sizeof slow_stub
);
5152 DP_PACKET_BATCH_FOR_EACH (packet
, packets_
) {
5153 struct dp_netdev_flow
*netdev_flow
;
5155 if (OVS_LIKELY(rules
[i
])) {
5159 /* It's possible that an earlier slow path execution installed
5160 * a rule covering this flow. In this case, it's a lot cheaper
5161 * to catch it here than execute a miss. */
5162 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, &keys
[i
],
5165 lookup_cnt
+= add_lookup_cnt
;
5166 rules
[i
] = &netdev_flow
->cr
;
5170 int error
= handle_packet_upcall(pmd
, packet
, &keys
[i
],
5171 &actions
, &put_actions
);
5173 if (OVS_UNLIKELY(error
)) {
5180 ofpbuf_uninit(&actions
);
5181 ofpbuf_uninit(&put_actions
);
5182 fat_rwlock_unlock(&dp
->upcall_rwlock
);
5183 } else if (OVS_UNLIKELY(any_miss
)) {
5184 DP_PACKET_BATCH_FOR_EACH (packet
, packets_
) {
5185 if (OVS_UNLIKELY(!rules
[i
])) {
5186 dp_packet_delete(packet
);
5192 DP_PACKET_BATCH_FOR_EACH (packet
, packets_
) {
5193 struct dp_netdev_flow
*flow
;
5195 if (OVS_UNLIKELY(!rules
[i
])) {
5199 flow
= dp_netdev_flow_cast(rules
[i
]);
5201 emc_probabilistic_insert(pmd
, &keys
[i
], flow
);
5202 dp_netdev_queue_batches(packet
, flow
, &keys
[i
].mf
, batches
, n_batches
);
5205 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MASKED_HIT
,
5206 cnt
- upcall_ok_cnt
- upcall_fail_cnt
);
5207 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MASKED_LOOKUP
,
5209 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_MISS
,
5211 pmd_perf_update_counter(&pmd
->perf_stats
, PMD_STAT_LOST
,
5215 /* Packets enter the datapath from a port (or from recirculation) here.
5217 * When 'md_is_valid' is true the metadata in 'packets' are already valid.
5218 * When false the metadata in 'packets' need to be initialized. */
5220 dp_netdev_input__(struct dp_netdev_pmd_thread
*pmd
,
5221 struct dp_packet_batch
*packets
,
5222 bool md_is_valid
, odp_port_t port_no
)
5224 #if !defined(__CHECKER__) && !defined(_WIN32)
5225 const size_t PKT_ARRAY_SIZE
= dp_packet_batch_size(packets
);
5227 /* Sparse or MSVC doesn't like variable length array. */
5228 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
5230 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
)
5231 struct netdev_flow_key keys
[PKT_ARRAY_SIZE
];
5232 struct packet_batch_per_flow batches
[PKT_ARRAY_SIZE
];
5237 emc_processing(pmd
, packets
, keys
, batches
, &n_batches
,
5238 md_is_valid
, port_no
);
5239 if (!dp_packet_batch_is_empty(packets
)) {
5240 /* Get ingress port from first packet's metadata. */
5241 in_port
= packets
->packets
[0]->md
.in_port
.odp_port
;
5242 fast_path_processing(pmd
, packets
, keys
,
5243 batches
, &n_batches
, in_port
);
5246 /* All the flow batches need to be reset before any call to
5247 * packet_batch_per_flow_execute() as it could potentially trigger
5248 * recirculation. When a packet matching flow ‘j’ happens to be
5249 * recirculated, the nested call to dp_netdev_input__() could potentially
5250 * classify the packet as matching another flow - say 'k'. It could happen
5251 * that in the previous call to dp_netdev_input__() that same flow 'k' had
5252 * already its own batches[k] still waiting to be served. So if its
5253 * ‘batch’ member is not reset, the recirculated packet would be wrongly
5254 * appended to batches[k] of the 1st call to dp_netdev_input__(). */
5256 for (i
= 0; i
< n_batches
; i
++) {
5257 batches
[i
].flow
->batch
= NULL
;
5260 for (i
= 0; i
< n_batches
; i
++) {
5261 packet_batch_per_flow_execute(&batches
[i
], pmd
);
5266 dp_netdev_input(struct dp_netdev_pmd_thread
*pmd
,
5267 struct dp_packet_batch
*packets
,
5270 dp_netdev_input__(pmd
, packets
, false, port_no
);
5274 dp_netdev_recirculate(struct dp_netdev_pmd_thread
*pmd
,
5275 struct dp_packet_batch
*packets
)
5277 dp_netdev_input__(pmd
, packets
, true, 0);
5280 struct dp_netdev_execute_aux
{
5281 struct dp_netdev_pmd_thread
*pmd
;
5282 const struct flow
*flow
;
5286 dpif_netdev_register_dp_purge_cb(struct dpif
*dpif
, dp_purge_callback
*cb
,
5289 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5290 dp
->dp_purge_aux
= aux
;
5291 dp
->dp_purge_cb
= cb
;
5295 dpif_netdev_register_upcall_cb(struct dpif
*dpif
, upcall_callback
*cb
,
5298 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5299 dp
->upcall_aux
= aux
;
5304 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
5308 struct dp_netdev_port
*port
;
5311 HMAP_FOR_EACH (tx
, node
, &pmd
->send_port_cache
) {
5312 if (!tx
->port
->dynamic_txqs
) {
5315 interval
= pmd
->ctx
.now
- tx
->last_used
;
5316 if (tx
->qid
>= 0 && (purge
|| interval
>= XPS_TIMEOUT
)) {
5318 ovs_mutex_lock(&port
->txq_used_mutex
);
5319 port
->txq_used
[tx
->qid
]--;
5320 ovs_mutex_unlock(&port
->txq_used_mutex
);
5327 dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
5330 struct dp_netdev_port
*port
;
5332 int i
, min_cnt
, min_qid
;
5334 interval
= pmd
->ctx
.now
- tx
->last_used
;
5335 tx
->last_used
= pmd
->ctx
.now
;
5337 if (OVS_LIKELY(tx
->qid
>= 0 && interval
< XPS_TIMEOUT
)) {
5343 ovs_mutex_lock(&port
->txq_used_mutex
);
5345 port
->txq_used
[tx
->qid
]--;
5351 for (i
= 0; i
< netdev_n_txq(port
->netdev
); i
++) {
5352 if (port
->txq_used
[i
] < min_cnt
|| min_cnt
== -1) {
5353 min_cnt
= port
->txq_used
[i
];
5358 port
->txq_used
[min_qid
]++;
5361 ovs_mutex_unlock(&port
->txq_used_mutex
);
5363 dpif_netdev_xps_revalidate_pmd(pmd
, false);
5365 VLOG_DBG("Core %d: New TX queue ID %d for port \'%s\'.",
5366 pmd
->core_id
, tx
->qid
, netdev_get_name(tx
->port
->netdev
));
5370 static struct tx_port
*
5371 pmd_tnl_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
5374 return tx_port_lookup(&pmd
->tnl_port_cache
, port_no
);
5377 static struct tx_port
*
5378 pmd_send_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
5381 return tx_port_lookup(&pmd
->send_port_cache
, port_no
);
5385 push_tnl_action(const struct dp_netdev_pmd_thread
*pmd
,
5386 const struct nlattr
*attr
,
5387 struct dp_packet_batch
*batch
)
5389 struct tx_port
*tun_port
;
5390 const struct ovs_action_push_tnl
*data
;
5393 data
= nl_attr_get(attr
);
5395 tun_port
= pmd_tnl_port_cache_lookup(pmd
, data
->tnl_port
);
5400 err
= netdev_push_header(tun_port
->port
->netdev
, batch
, data
);
5405 dp_packet_delete_batch(batch
, true);
5410 dp_execute_userspace_action(struct dp_netdev_pmd_thread
*pmd
,
5411 struct dp_packet
*packet
, bool may_steal
,
5412 struct flow
*flow
, ovs_u128
*ufid
,
5413 struct ofpbuf
*actions
,
5414 const struct nlattr
*userdata
)
5416 struct dp_packet_batch b
;
5419 ofpbuf_clear(actions
);
5421 error
= dp_netdev_upcall(pmd
, packet
, flow
, NULL
, ufid
,
5422 DPIF_UC_ACTION
, userdata
, actions
,
5424 if (!error
|| error
== ENOSPC
) {
5425 dp_packet_batch_init_packet(&b
, packet
);
5426 dp_netdev_execute_actions(pmd
, &b
, may_steal
, flow
,
5427 actions
->data
, actions
->size
);
5428 } else if (may_steal
) {
5429 dp_packet_delete(packet
);
5434 dp_execute_cb(void *aux_
, struct dp_packet_batch
*packets_
,
5435 const struct nlattr
*a
, bool may_steal
)
5436 OVS_NO_THREAD_SAFETY_ANALYSIS
5438 struct dp_netdev_execute_aux
*aux
= aux_
;
5439 uint32_t *depth
= recirc_depth_get();
5440 struct dp_netdev_pmd_thread
*pmd
= aux
->pmd
;
5441 struct dp_netdev
*dp
= pmd
->dp
;
5442 int type
= nl_attr_type(a
);
5445 switch ((enum ovs_action_attr
)type
) {
5446 case OVS_ACTION_ATTR_OUTPUT
:
5447 p
= pmd_send_port_cache_lookup(pmd
, nl_attr_get_odp_port(a
));
5448 if (OVS_LIKELY(p
)) {
5449 struct dp_packet
*packet
;
5450 struct dp_packet_batch out
;
5453 dp_packet_batch_clone(&out
, packets_
);
5454 dp_packet_batch_reset_cutlen(packets_
);
5457 dp_packet_batch_apply_cutlen(packets_
);
5460 if (OVS_UNLIKELY(!dp_packet_batch_is_empty(&p
->output_pkts
)
5461 && packets_
->packets
[0]->source
5462 != p
->output_pkts
.packets
[0]->source
)) {
5463 /* XXX: netdev-dpdk assumes that all packets in a single
5464 * output batch has the same source. Flush here to
5465 * avoid memory access issues. */
5466 dp_netdev_pmd_flush_output_on_port(pmd
, p
);
5469 if (dp_packet_batch_size(&p
->output_pkts
)
5470 + dp_packet_batch_size(packets_
) > NETDEV_MAX_BURST
) {
5471 /* Flush here to avoid overflow. */
5472 dp_netdev_pmd_flush_output_on_port(pmd
, p
);
5475 if (dp_packet_batch_is_empty(&p
->output_pkts
)) {
5476 pmd
->n_output_batches
++;
5479 DP_PACKET_BATCH_FOR_EACH (packet
, packets_
) {
5480 p
->output_pkts_rxqs
[dp_packet_batch_size(&p
->output_pkts
)] =
5482 dp_packet_batch_add(&p
->output_pkts
, packet
);
5488 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
5489 if (*depth
< MAX_RECIRC_DEPTH
) {
5490 dp_packet_batch_apply_cutlen(packets_
);
5491 push_tnl_action(pmd
, a
, packets_
);
5496 case OVS_ACTION_ATTR_TUNNEL_POP
:
5497 if (*depth
< MAX_RECIRC_DEPTH
) {
5498 struct dp_packet_batch
*orig_packets_
= packets_
;
5499 odp_port_t portno
= nl_attr_get_odp_port(a
);
5501 p
= pmd_tnl_port_cache_lookup(pmd
, portno
);
5503 struct dp_packet_batch tnl_pkt
;
5506 dp_packet_batch_clone(&tnl_pkt
, packets_
);
5507 packets_
= &tnl_pkt
;
5508 dp_packet_batch_reset_cutlen(orig_packets_
);
5511 dp_packet_batch_apply_cutlen(packets_
);
5513 netdev_pop_header(p
->port
->netdev
, packets_
);
5514 if (dp_packet_batch_is_empty(packets_
)) {
5518 struct dp_packet
*packet
;
5519 DP_PACKET_BATCH_FOR_EACH (packet
, packets_
) {
5520 packet
->md
.in_port
.odp_port
= portno
;
5524 dp_netdev_recirculate(pmd
, packets_
);
5531 case OVS_ACTION_ATTR_USERSPACE
:
5532 if (!fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
5533 struct dp_packet_batch
*orig_packets_
= packets_
;
5534 const struct nlattr
*userdata
;
5535 struct dp_packet_batch usr_pkt
;
5536 struct ofpbuf actions
;
5541 userdata
= nl_attr_find_nested(a
, OVS_USERSPACE_ATTR_USERDATA
);
5542 ofpbuf_init(&actions
, 0);
5544 if (packets_
->trunc
) {
5546 dp_packet_batch_clone(&usr_pkt
, packets_
);
5547 packets_
= &usr_pkt
;
5549 dp_packet_batch_reset_cutlen(orig_packets_
);
5552 dp_packet_batch_apply_cutlen(packets_
);
5555 struct dp_packet
*packet
;
5556 DP_PACKET_BATCH_FOR_EACH (packet
, packets_
) {
5557 flow_extract(packet
, &flow
);
5558 dpif_flow_hash(dp
->dpif
, &flow
, sizeof flow
, &ufid
);
5559 dp_execute_userspace_action(pmd
, packet
, may_steal
, &flow
,
5560 &ufid
, &actions
, userdata
);
5564 dp_packet_delete_batch(packets_
, true);
5567 ofpbuf_uninit(&actions
);
5568 fat_rwlock_unlock(&dp
->upcall_rwlock
);
5574 case OVS_ACTION_ATTR_RECIRC
:
5575 if (*depth
< MAX_RECIRC_DEPTH
) {
5576 struct dp_packet_batch recirc_pkts
;
5579 dp_packet_batch_clone(&recirc_pkts
, packets_
);
5580 packets_
= &recirc_pkts
;
5583 struct dp_packet
*packet
;
5584 DP_PACKET_BATCH_FOR_EACH (packet
, packets_
) {
5585 packet
->md
.recirc_id
= nl_attr_get_u32(a
);
5589 dp_netdev_recirculate(pmd
, packets_
);
5595 VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
5598 case OVS_ACTION_ATTR_CT
: {
5599 const struct nlattr
*b
;
5601 bool commit
= false;
5604 const char *helper
= NULL
;
5605 const uint32_t *setmark
= NULL
;
5606 const struct ovs_key_ct_labels
*setlabel
= NULL
;
5607 struct nat_action_info_t nat_action_info
;
5608 struct nat_action_info_t
*nat_action_info_ref
= NULL
;
5609 bool nat_config
= false;
5611 NL_ATTR_FOR_EACH_UNSAFE (b
, left
, nl_attr_get(a
),
5612 nl_attr_get_size(a
)) {
5613 enum ovs_ct_attr sub_type
= nl_attr_type(b
);
5616 case OVS_CT_ATTR_FORCE_COMMIT
:
5619 case OVS_CT_ATTR_COMMIT
:
5622 case OVS_CT_ATTR_ZONE
:
5623 zone
= nl_attr_get_u16(b
);
5625 case OVS_CT_ATTR_HELPER
:
5626 helper
= nl_attr_get_string(b
);
5628 case OVS_CT_ATTR_MARK
:
5629 setmark
= nl_attr_get(b
);
5631 case OVS_CT_ATTR_LABELS
:
5632 setlabel
= nl_attr_get(b
);
5634 case OVS_CT_ATTR_EVENTMASK
:
5635 /* Silently ignored, as userspace datapath does not generate
5636 * netlink events. */
5638 case OVS_CT_ATTR_NAT
: {
5639 const struct nlattr
*b_nest
;
5640 unsigned int left_nest
;
5641 bool ip_min_specified
= false;
5642 bool proto_num_min_specified
= false;
5643 bool ip_max_specified
= false;
5644 bool proto_num_max_specified
= false;
5645 memset(&nat_action_info
, 0, sizeof nat_action_info
);
5646 nat_action_info_ref
= &nat_action_info
;
5648 NL_NESTED_FOR_EACH_UNSAFE (b_nest
, left_nest
, b
) {
5649 enum ovs_nat_attr sub_type_nest
= nl_attr_type(b_nest
);
5651 switch (sub_type_nest
) {
5652 case OVS_NAT_ATTR_SRC
:
5653 case OVS_NAT_ATTR_DST
:
5655 nat_action_info
.nat_action
|=
5656 ((sub_type_nest
== OVS_NAT_ATTR_SRC
)
5657 ? NAT_ACTION_SRC
: NAT_ACTION_DST
);
5659 case OVS_NAT_ATTR_IP_MIN
:
5660 memcpy(&nat_action_info
.min_addr
,
5661 nl_attr_get(b_nest
),
5662 nl_attr_get_size(b_nest
));
5663 ip_min_specified
= true;
5665 case OVS_NAT_ATTR_IP_MAX
:
5666 memcpy(&nat_action_info
.max_addr
,
5667 nl_attr_get(b_nest
),
5668 nl_attr_get_size(b_nest
));
5669 ip_max_specified
= true;
5671 case OVS_NAT_ATTR_PROTO_MIN
:
5672 nat_action_info
.min_port
=
5673 nl_attr_get_u16(b_nest
);
5674 proto_num_min_specified
= true;
5676 case OVS_NAT_ATTR_PROTO_MAX
:
5677 nat_action_info
.max_port
=
5678 nl_attr_get_u16(b_nest
);
5679 proto_num_max_specified
= true;
5681 case OVS_NAT_ATTR_PERSISTENT
:
5682 case OVS_NAT_ATTR_PROTO_HASH
:
5683 case OVS_NAT_ATTR_PROTO_RANDOM
:
5685 case OVS_NAT_ATTR_UNSPEC
:
5686 case __OVS_NAT_ATTR_MAX
:
5691 if (ip_min_specified
&& !ip_max_specified
) {
5692 nat_action_info
.max_addr
= nat_action_info
.min_addr
;
5694 if (proto_num_min_specified
&& !proto_num_max_specified
) {
5695 nat_action_info
.max_port
= nat_action_info
.min_port
;
5697 if (proto_num_min_specified
|| proto_num_max_specified
) {
5698 if (nat_action_info
.nat_action
& NAT_ACTION_SRC
) {
5699 nat_action_info
.nat_action
|= NAT_ACTION_SRC_PORT
;
5700 } else if (nat_action_info
.nat_action
& NAT_ACTION_DST
) {
5701 nat_action_info
.nat_action
|= NAT_ACTION_DST_PORT
;
5706 case OVS_CT_ATTR_UNSPEC
:
5707 case __OVS_CT_ATTR_MAX
:
5712 /* We won't be able to function properly in this case, hence
5713 * complain loudly. */
5714 if (nat_config
&& !commit
) {
5715 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
5716 VLOG_WARN_RL(&rl
, "NAT specified without commit.");
5719 conntrack_execute(&dp
->conntrack
, packets_
, aux
->flow
->dl_type
, force
,
5720 commit
, zone
, setmark
, setlabel
, aux
->flow
->tp_src
,
5721 aux
->flow
->tp_dst
, helper
, nat_action_info_ref
,
5722 pmd
->ctx
.now
/ 1000);
5726 case OVS_ACTION_ATTR_METER
:
5727 dp_netdev_run_meter(pmd
->dp
, packets_
, nl_attr_get_u32(a
),
5731 case OVS_ACTION_ATTR_PUSH_VLAN
:
5732 case OVS_ACTION_ATTR_POP_VLAN
:
5733 case OVS_ACTION_ATTR_PUSH_MPLS
:
5734 case OVS_ACTION_ATTR_POP_MPLS
:
5735 case OVS_ACTION_ATTR_SET
:
5736 case OVS_ACTION_ATTR_SET_MASKED
:
5737 case OVS_ACTION_ATTR_SAMPLE
:
5738 case OVS_ACTION_ATTR_HASH
:
5739 case OVS_ACTION_ATTR_UNSPEC
:
5740 case OVS_ACTION_ATTR_TRUNC
:
5741 case OVS_ACTION_ATTR_PUSH_ETH
:
5742 case OVS_ACTION_ATTR_POP_ETH
:
5743 case OVS_ACTION_ATTR_CLONE
:
5744 case OVS_ACTION_ATTR_PUSH_NSH
:
5745 case OVS_ACTION_ATTR_POP_NSH
:
5746 case __OVS_ACTION_ATTR_MAX
:
5750 dp_packet_delete_batch(packets_
, may_steal
);
5754 dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
5755 struct dp_packet_batch
*packets
,
5756 bool may_steal
, const struct flow
*flow
,
5757 const struct nlattr
*actions
, size_t actions_len
)
5759 struct dp_netdev_execute_aux aux
= { pmd
, flow
};
5761 odp_execute_actions(&aux
, packets
, may_steal
, actions
,
5762 actions_len
, dp_execute_cb
);
5765 struct dp_netdev_ct_dump
{
5766 struct ct_dpif_dump_state up
;
5767 struct conntrack_dump dump
;
5768 struct conntrack
*ct
;
5769 struct dp_netdev
*dp
;
5773 dpif_netdev_ct_dump_start(struct dpif
*dpif
, struct ct_dpif_dump_state
**dump_
,
5774 const uint16_t *pzone
, int *ptot_bkts
)
5776 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5777 struct dp_netdev_ct_dump
*dump
;
5779 dump
= xzalloc(sizeof *dump
);
5781 dump
->ct
= &dp
->conntrack
;
5783 conntrack_dump_start(&dp
->conntrack
, &dump
->dump
, pzone
, ptot_bkts
);
5791 dpif_netdev_ct_dump_next(struct dpif
*dpif OVS_UNUSED
,
5792 struct ct_dpif_dump_state
*dump_
,
5793 struct ct_dpif_entry
*entry
)
5795 struct dp_netdev_ct_dump
*dump
;
5797 INIT_CONTAINER(dump
, dump_
, up
);
5799 return conntrack_dump_next(&dump
->dump
, entry
);
5803 dpif_netdev_ct_dump_done(struct dpif
*dpif OVS_UNUSED
,
5804 struct ct_dpif_dump_state
*dump_
)
5806 struct dp_netdev_ct_dump
*dump
;
5809 INIT_CONTAINER(dump
, dump_
, up
);
5811 err
= conntrack_dump_done(&dump
->dump
);
5819 dpif_netdev_ct_flush(struct dpif
*dpif
, const uint16_t *zone
,
5820 const struct ct_dpif_tuple
*tuple
)
5822 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5827 return conntrack_flush(&dp
->conntrack
, zone
);
5831 dpif_netdev_ct_set_maxconns(struct dpif
*dpif
, uint32_t maxconns
)
5833 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5835 return conntrack_set_maxconns(&dp
->conntrack
, maxconns
);
5839 dpif_netdev_ct_get_maxconns(struct dpif
*dpif
, uint32_t *maxconns
)
5841 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5843 return conntrack_get_maxconns(&dp
->conntrack
, maxconns
);
5847 dpif_netdev_ct_get_nconns(struct dpif
*dpif
, uint32_t *nconns
)
5849 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5851 return conntrack_get_nconns(&dp
->conntrack
, nconns
);
5854 const struct dpif_class dpif_netdev_class
= {
5857 dpif_netdev_enumerate
,
5858 dpif_netdev_port_open_type
,
5861 dpif_netdev_destroy
,
5864 dpif_netdev_get_stats
,
5865 dpif_netdev_port_add
,
5866 dpif_netdev_port_del
,
5867 dpif_netdev_port_set_config
,
5868 dpif_netdev_port_query_by_number
,
5869 dpif_netdev_port_query_by_name
,
5870 NULL
, /* port_get_pid */
5871 dpif_netdev_port_dump_start
,
5872 dpif_netdev_port_dump_next
,
5873 dpif_netdev_port_dump_done
,
5874 dpif_netdev_port_poll
,
5875 dpif_netdev_port_poll_wait
,
5876 dpif_netdev_flow_flush
,
5877 dpif_netdev_flow_dump_create
,
5878 dpif_netdev_flow_dump_destroy
,
5879 dpif_netdev_flow_dump_thread_create
,
5880 dpif_netdev_flow_dump_thread_destroy
,
5881 dpif_netdev_flow_dump_next
,
5882 dpif_netdev_operate
,
5883 NULL
, /* recv_set */
5884 NULL
, /* handlers_set */
5885 dpif_netdev_set_config
,
5886 dpif_netdev_queue_to_priority
,
5888 NULL
, /* recv_wait */
5889 NULL
, /* recv_purge */
5890 dpif_netdev_register_dp_purge_cb
,
5891 dpif_netdev_register_upcall_cb
,
5892 dpif_netdev_enable_upcall
,
5893 dpif_netdev_disable_upcall
,
5894 dpif_netdev_get_datapath_version
,
5895 dpif_netdev_ct_dump_start
,
5896 dpif_netdev_ct_dump_next
,
5897 dpif_netdev_ct_dump_done
,
5898 dpif_netdev_ct_flush
,
5899 dpif_netdev_ct_set_maxconns
,
5900 dpif_netdev_ct_get_maxconns
,
5901 dpif_netdev_ct_get_nconns
,
5902 dpif_netdev_meter_get_features
,
5903 dpif_netdev_meter_set
,
5904 dpif_netdev_meter_get
,
5905 dpif_netdev_meter_del
,
5909 dpif_dummy_change_port_number(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
5910 const char *argv
[], void *aux OVS_UNUSED
)
5912 struct dp_netdev_port
*port
;
5913 struct dp_netdev
*dp
;
5916 ovs_mutex_lock(&dp_netdev_mutex
);
5917 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
5918 if (!dp
|| !dpif_netdev_class_is_dummy(dp
->class)) {
5919 ovs_mutex_unlock(&dp_netdev_mutex
);
5920 unixctl_command_reply_error(conn
, "unknown datapath or not a dummy");
5923 ovs_refcount_ref(&dp
->ref_cnt
);
5924 ovs_mutex_unlock(&dp_netdev_mutex
);
5926 ovs_mutex_lock(&dp
->port_mutex
);
5927 if (get_port_by_name(dp
, argv
[2], &port
)) {
5928 unixctl_command_reply_error(conn
, "unknown port");
5932 port_no
= u32_to_odp(atoi(argv
[3]));
5933 if (!port_no
|| port_no
== ODPP_NONE
) {
5934 unixctl_command_reply_error(conn
, "bad port number");
5937 if (dp_netdev_lookup_port(dp
, port_no
)) {
5938 unixctl_command_reply_error(conn
, "port number already in use");
5943 hmap_remove(&dp
->ports
, &port
->node
);
5944 reconfigure_datapath(dp
);
5946 /* Reinsert with new port number. */
5947 port
->port_no
= port_no
;
5948 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
5949 reconfigure_datapath(dp
);
5951 seq_change(dp
->port_seq
);
5952 unixctl_command_reply(conn
, NULL
);
5955 ovs_mutex_unlock(&dp
->port_mutex
);
5956 dp_netdev_unref(dp
);
5960 dpif_dummy_register__(const char *type
)
5962 struct dpif_class
*class;
5964 class = xmalloc(sizeof *class);
5965 *class = dpif_netdev_class
;
5966 class->type
= xstrdup(type
);
5967 dp_register_provider(class);
5971 dpif_dummy_override(const char *type
)
5976 * Ignore EAFNOSUPPORT to allow --enable-dummy=system with
5977 * a userland-only build. It's useful for testsuite.
5979 error
= dp_unregister_provider(type
);
5980 if (error
== 0 || error
== EAFNOSUPPORT
) {
5981 dpif_dummy_register__(type
);
5986 dpif_dummy_register(enum dummy_level level
)
5988 if (level
== DUMMY_OVERRIDE_ALL
) {
5993 dp_enumerate_types(&types
);
5994 SSET_FOR_EACH (type
, &types
) {
5995 dpif_dummy_override(type
);
5997 sset_destroy(&types
);
5998 } else if (level
== DUMMY_OVERRIDE_SYSTEM
) {
5999 dpif_dummy_override("system");
6002 dpif_dummy_register__("dummy");
6004 unixctl_command_register("dpif-dummy/change-port-number",
6005 "dp port new-number",
6006 3, 3, dpif_dummy_change_port_number
, NULL
);
6009 /* Datapath Classifier. */
6011 /* A set of rules that all have the same fields wildcarded. */
6012 struct dpcls_subtable
{
6013 /* The fields are only used by writers. */
6014 struct cmap_node cmap_node OVS_GUARDED
; /* Within dpcls 'subtables_map'. */
6016 /* These fields are accessed by readers. */
6017 struct cmap rules
; /* Contains "struct dpcls_rule"s. */
6018 uint32_t hit_cnt
; /* Number of match hits in subtable in current
6019 optimization interval. */
6020 struct netdev_flow_key mask
; /* Wildcards for fields (const). */
6021 /* 'mask' must be the last field, additional space is allocated here. */
6024 /* Initializes 'cls' as a classifier that initially contains no classification
6027 dpcls_init(struct dpcls
*cls
)
6029 cmap_init(&cls
->subtables_map
);
6030 pvector_init(&cls
->subtables
);
6034 dpcls_destroy_subtable(struct dpcls
*cls
, struct dpcls_subtable
*subtable
)
6036 VLOG_DBG("Destroying subtable %p for in_port %d", subtable
, cls
->in_port
);
6037 pvector_remove(&cls
->subtables
, subtable
);
6038 cmap_remove(&cls
->subtables_map
, &subtable
->cmap_node
,
6039 subtable
->mask
.hash
);
6040 cmap_destroy(&subtable
->rules
);
6041 ovsrcu_postpone(free
, subtable
);
6044 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
6045 * caller's responsibility.
6046 * May only be called after all the readers have been terminated. */
6048 dpcls_destroy(struct dpcls
*cls
)
6051 struct dpcls_subtable
*subtable
;
6053 CMAP_FOR_EACH (subtable
, cmap_node
, &cls
->subtables_map
) {
6054 ovs_assert(cmap_count(&subtable
->rules
) == 0);
6055 dpcls_destroy_subtable(cls
, subtable
);
6057 cmap_destroy(&cls
->subtables_map
);
6058 pvector_destroy(&cls
->subtables
);
6062 static struct dpcls_subtable
*
6063 dpcls_create_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
6065 struct dpcls_subtable
*subtable
;
6067 /* Need to add one. */
6068 subtable
= xmalloc(sizeof *subtable
6069 - sizeof subtable
->mask
.mf
+ mask
->len
);
6070 cmap_init(&subtable
->rules
);
6071 subtable
->hit_cnt
= 0;
6072 netdev_flow_key_clone(&subtable
->mask
, mask
);
6073 cmap_insert(&cls
->subtables_map
, &subtable
->cmap_node
, mask
->hash
);
6074 /* Add the new subtable at the end of the pvector (with no hits yet) */
6075 pvector_insert(&cls
->subtables
, subtable
, 0);
6076 VLOG_DBG("Creating %"PRIuSIZE
". subtable %p for in_port %d",
6077 cmap_count(&cls
->subtables_map
), subtable
, cls
->in_port
);
6078 pvector_publish(&cls
->subtables
);
6083 static inline struct dpcls_subtable
*
6084 dpcls_find_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
6086 struct dpcls_subtable
*subtable
;
6088 CMAP_FOR_EACH_WITH_HASH (subtable
, cmap_node
, mask
->hash
,
6089 &cls
->subtables_map
) {
6090 if (netdev_flow_key_equal(&subtable
->mask
, mask
)) {
6094 return dpcls_create_subtable(cls
, mask
);
6098 /* Periodically sort the dpcls subtable vectors according to hit counts */
6100 dpcls_sort_subtable_vector(struct dpcls
*cls
)
6102 struct pvector
*pvec
= &cls
->subtables
;
6103 struct dpcls_subtable
*subtable
;
6105 PVECTOR_FOR_EACH (subtable
, pvec
) {
6106 pvector_change_priority(pvec
, subtable
, subtable
->hit_cnt
);
6107 subtable
->hit_cnt
= 0;
6109 pvector_publish(pvec
);
6113 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread
*pmd
,
6114 struct polled_queue
*poll_list
, int poll_cnt
)
6118 if (pmd
->ctx
.now
> pmd
->rxq_next_cycle_store
) {
6119 /* Get the cycles that were used to process each queue and store. */
6120 for (unsigned i
= 0; i
< poll_cnt
; i
++) {
6121 uint64_t rxq_cyc_curr
= dp_netdev_rxq_get_cycles(poll_list
[i
].rxq
,
6122 RXQ_CYCLES_PROC_CURR
);
6123 dp_netdev_rxq_set_intrvl_cycles(poll_list
[i
].rxq
, rxq_cyc_curr
);
6124 dp_netdev_rxq_set_cycles(poll_list
[i
].rxq
, RXQ_CYCLES_PROC_CURR
,
6127 /* Start new measuring interval */
6128 pmd
->rxq_next_cycle_store
= pmd
->ctx
.now
+ PMD_RXQ_INTERVAL_LEN
;
6131 if (pmd
->ctx
.now
> pmd
->next_optimization
) {
6132 /* Try to obtain the flow lock to block out revalidator threads.
6133 * If not possible, just try next time. */
6134 if (!ovs_mutex_trylock(&pmd
->flow_mutex
)) {
6135 /* Optimize each classifier */
6136 CMAP_FOR_EACH (cls
, node
, &pmd
->classifiers
) {
6137 dpcls_sort_subtable_vector(cls
);
6139 ovs_mutex_unlock(&pmd
->flow_mutex
);
6140 /* Start new measuring interval */
6141 pmd
->next_optimization
= pmd
->ctx
.now
6142 + DPCLS_OPTIMIZATION_INTERVAL
;
6147 /* Insert 'rule' into 'cls'. */
6149 dpcls_insert(struct dpcls
*cls
, struct dpcls_rule
*rule
,
6150 const struct netdev_flow_key
*mask
)
6152 struct dpcls_subtable
*subtable
= dpcls_find_subtable(cls
, mask
);
6154 /* Refer to subtable's mask, also for later removal. */
6155 rule
->mask
= &subtable
->mask
;
6156 cmap_insert(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
);
6159 /* Removes 'rule' from 'cls', also destructing the 'rule'. */
6161 dpcls_remove(struct dpcls
*cls
, struct dpcls_rule
*rule
)
6163 struct dpcls_subtable
*subtable
;
6165 ovs_assert(rule
->mask
);
6167 /* Get subtable from reference in rule->mask. */
6168 INIT_CONTAINER(subtable
, rule
->mask
, mask
);
6169 if (cmap_remove(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
)
6171 /* Delete empty subtable. */
6172 dpcls_destroy_subtable(cls
, subtable
);
6173 pvector_publish(&cls
->subtables
);
6177 /* Returns true if 'target' satisfies 'key' in 'mask', that is, if each 1-bit
6178 * in 'mask' the values in 'key' and 'target' are the same. */
6180 dpcls_rule_matches_key(const struct dpcls_rule
*rule
,
6181 const struct netdev_flow_key
*target
)
6183 const uint64_t *keyp
= miniflow_get_values(&rule
->flow
.mf
);
6184 const uint64_t *maskp
= miniflow_get_values(&rule
->mask
->mf
);
6187 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, target
, rule
->flow
.mf
.map
) {
6188 if (OVS_UNLIKELY((value
& *maskp
++) != *keyp
++)) {
6195 /* For each miniflow in 'keys' performs a classifier lookup writing the result
6196 * into the corresponding slot in 'rules'. If a particular entry in 'keys' is
6197 * NULL it is skipped.
6199 * This function is optimized for use in the userspace datapath and therefore
6200 * does not implement a lot of features available in the standard
6201 * classifier_lookup() function. Specifically, it does not implement
6202 * priorities, instead returning any rule which matches the flow.
6204 * Returns true if all miniflows found a corresponding rule. */
6206 dpcls_lookup(struct dpcls
*cls
, const struct netdev_flow_key keys
[],
6207 struct dpcls_rule
**rules
, const size_t cnt
,
6210 /* The received 'cnt' miniflows are the search-keys that will be processed
6211 * to find a matching entry into the available subtables.
6212 * The number of bits in map_type is equal to NETDEV_MAX_BURST. */
6213 typedef uint32_t map_type
;
6214 #define MAP_BITS (sizeof(map_type) * CHAR_BIT)
6215 BUILD_ASSERT_DECL(MAP_BITS
>= NETDEV_MAX_BURST
);
6217 struct dpcls_subtable
*subtable
;
6219 map_type keys_map
= TYPE_MAXIMUM(map_type
); /* Set all bits. */
6221 uint32_t hashes
[MAP_BITS
];
6222 const struct cmap_node
*nodes
[MAP_BITS
];
6224 if (cnt
!= MAP_BITS
) {
6225 keys_map
>>= MAP_BITS
- cnt
; /* Clear extra bits. */
6227 memset(rules
, 0, cnt
* sizeof *rules
);
6229 int lookups_match
= 0, subtable_pos
= 1;
6231 /* The Datapath classifier - aka dpcls - is composed of subtables.
6232 * Subtables are dynamically created as needed when new rules are inserted.
6233 * Each subtable collects rules with matches on a specific subset of packet
6234 * fields as defined by the subtable's mask. We proceed to process every
6235 * search-key against each subtable, but when a match is found for a
6236 * search-key, the search for that key can stop because the rules are
6237 * non-overlapping. */
6238 PVECTOR_FOR_EACH (subtable
, &cls
->subtables
) {
6241 /* Compute hashes for the remaining keys. Each search-key is
6242 * masked with the subtable's mask to avoid hashing the wildcarded
6244 ULLONG_FOR_EACH_1(i
, keys_map
) {
6245 hashes
[i
] = netdev_flow_key_hash_in_mask(&keys
[i
],
6249 found_map
= cmap_find_batch(&subtable
->rules
, keys_map
, hashes
, nodes
);
6250 /* Check results. When the i-th bit of found_map is set, it means
6251 * that a set of nodes with a matching hash value was found for the
6252 * i-th search-key. Due to possible hash collisions we need to check
6253 * which of the found rules, if any, really matches our masked
6255 ULLONG_FOR_EACH_1(i
, found_map
) {
6256 struct dpcls_rule
*rule
;
6258 CMAP_NODE_FOR_EACH (rule
, cmap_node
, nodes
[i
]) {
6259 if (OVS_LIKELY(dpcls_rule_matches_key(rule
, &keys
[i
]))) {
6261 /* Even at 20 Mpps the 32-bit hit_cnt cannot wrap
6262 * within one second optimization interval. */
6263 subtable
->hit_cnt
++;
6264 lookups_match
+= subtable_pos
;
6268 /* None of the found rules was a match. Reset the i-th bit to
6269 * keep searching this key in the next subtable. */
6270 ULLONG_SET0(found_map
, i
); /* Did not match. */
6272 ; /* Keep Sparse happy. */
6274 keys_map
&= ~found_map
; /* Clear the found rules. */
6276 if (num_lookups_p
) {
6277 *num_lookups_p
= lookups_match
;
6279 return true; /* All found. */
6283 if (num_lookups_p
) {
6284 *num_lookups_p
= lookups_match
;
6286 return false; /* Some misses. */