2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2016 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 <netinet/in.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
35 #include <rte_cycles.h>
40 #include "conntrack.h"
44 #include "dp-packet.h"
46 #include "dpif-provider.h"
48 #include "fat-rwlock.h"
53 #include "netdev-vport.h"
55 #include "odp-execute.h"
57 #include "openvswitch/dynamic-string.h"
58 #include "openvswitch/list.h"
59 #include "openvswitch/match.h"
60 #include "openvswitch/ofp-print.h"
61 #include "openvswitch/ofp-util.h"
62 #include "openvswitch/ofpbuf.h"
63 #include "openvswitch/shash.h"
64 #include "openvswitch/vlog.h"
68 #include "poll-loop.h"
75 #include "tnl-neigh-cache.h"
76 #include "tnl-ports.h"
80 VLOG_DEFINE_THIS_MODULE(dpif_netdev
);
82 #define FLOW_DUMP_MAX_BATCH 50
83 /* Use per thread recirc_depth to prevent recirculation loop. */
84 #define MAX_RECIRC_DEPTH 5
85 DEFINE_STATIC_PER_THREAD_DATA(uint32_t, recirc_depth
, 0)
87 /* Configuration parameters. */
88 enum { MAX_FLOWS
= 65536 }; /* Maximum number of flows in flow table. */
89 enum { MAX_METERS
= 65536 }; /* Maximum number of meters. */
90 enum { MAX_BANDS
= 8 }; /* Maximum number of bands / meter. */
91 enum { N_METER_LOCKS
= 64 }; /* Maximum number of meters. */
93 /* Protects against changes to 'dp_netdevs'. */
94 static struct ovs_mutex dp_netdev_mutex
= OVS_MUTEX_INITIALIZER
;
96 /* Contains all 'struct dp_netdev's. */
97 static struct shash dp_netdevs
OVS_GUARDED_BY(dp_netdev_mutex
)
98 = SHASH_INITIALIZER(&dp_netdevs
);
100 static struct vlog_rate_limit upcall_rl
= VLOG_RATE_LIMIT_INIT(600, 600);
102 #define DP_NETDEV_CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \
103 | CS_INVALID | CS_REPLY_DIR | CS_TRACKED)
104 #define DP_NETDEV_CS_UNSUPPORTED_MASK (~(uint32_t)DP_NETDEV_CS_SUPPORTED_MASK)
106 static struct odp_support dp_netdev_support
= {
107 .max_vlan_headers
= SIZE_MAX
,
108 .max_mpls_depth
= SIZE_MAX
,
116 /* Stores a miniflow with inline values */
118 struct netdev_flow_key
{
119 uint32_t hash
; /* Hash function differs for different users. */
120 uint32_t len
; /* Length of the following miniflow (incl. map). */
122 uint64_t buf
[FLOW_MAX_PACKET_U64S
];
125 /* Exact match cache for frequently used flows
127 * The cache uses a 32-bit hash of the packet (which can be the RSS hash) to
128 * search its entries for a miniflow that matches exactly the miniflow of the
129 * packet. It stores the 'dpcls_rule' (rule) that matches the miniflow.
131 * A cache entry holds a reference to its 'dp_netdev_flow'.
133 * A miniflow with a given hash can be in one of EM_FLOW_HASH_SEGS different
134 * entries. The 32-bit hash is split into EM_FLOW_HASH_SEGS values (each of
135 * them is EM_FLOW_HASH_SHIFT bits wide and the remainder is thrown away). Each
136 * value is the index of a cache entry where the miniflow could be.
142 * Each pmd_thread has its own private exact match cache.
143 * If dp_netdev_input is not called from a pmd thread, a mutex is used.
146 #define EM_FLOW_HASH_SHIFT 13
147 #define EM_FLOW_HASH_ENTRIES (1u << EM_FLOW_HASH_SHIFT)
148 #define EM_FLOW_HASH_MASK (EM_FLOW_HASH_ENTRIES - 1)
149 #define EM_FLOW_HASH_SEGS 2
151 /* Default EMC insert probability is 1 / DEFAULT_EM_FLOW_INSERT_INV_PROB */
152 #define DEFAULT_EM_FLOW_INSERT_INV_PROB 100
153 #define DEFAULT_EM_FLOW_INSERT_MIN (UINT32_MAX / \
154 DEFAULT_EM_FLOW_INSERT_INV_PROB)
157 struct dp_netdev_flow
*flow
;
158 struct netdev_flow_key key
; /* key.hash used for emc hash value. */
162 struct emc_entry entries
[EM_FLOW_HASH_ENTRIES
];
163 int sweep_idx
; /* For emc_cache_slow_sweep(). */
166 /* Iterate in the exact match cache through every entry that might contain a
167 * miniflow with hash 'HASH'. */
168 #define EMC_FOR_EACH_POS_WITH_HASH(EMC, CURRENT_ENTRY, HASH) \
169 for (uint32_t i__ = 0, srch_hash__ = (HASH); \
170 (CURRENT_ENTRY) = &(EMC)->entries[srch_hash__ & EM_FLOW_HASH_MASK], \
171 i__ < EM_FLOW_HASH_SEGS; \
172 i__++, srch_hash__ >>= EM_FLOW_HASH_SHIFT)
174 /* Simple non-wildcarding single-priority classifier. */
176 /* Time in ms between successive optimizations of the dpcls subtable vector */
177 #define DPCLS_OPTIMIZATION_INTERVAL 1000
180 struct cmap_node node
; /* Within dp_netdev_pmd_thread.classifiers */
182 struct cmap subtables_map
;
183 struct pvector subtables
;
186 /* A rule to be inserted to the classifier. */
188 struct cmap_node cmap_node
; /* Within struct dpcls_subtable 'rules'. */
189 struct netdev_flow_key
*mask
; /* Subtable's mask. */
190 struct netdev_flow_key flow
; /* Matching key. */
191 /* 'flow' must be the last field, additional space is allocated here. */
194 static void dpcls_init(struct dpcls
*);
195 static void dpcls_destroy(struct dpcls
*);
196 static void dpcls_sort_subtable_vector(struct dpcls
*);
197 static void dpcls_insert(struct dpcls
*, struct dpcls_rule
*,
198 const struct netdev_flow_key
*mask
);
199 static void dpcls_remove(struct dpcls
*, struct dpcls_rule
*);
200 static bool dpcls_lookup(struct dpcls
*cls
,
201 const struct netdev_flow_key keys
[],
202 struct dpcls_rule
**rules
, size_t cnt
,
205 /* Set of supported meter flags */
206 #define DP_SUPPORTED_METER_FLAGS_MASK \
207 (OFPMF13_STATS | OFPMF13_PKTPS | OFPMF13_KBPS | OFPMF13_BURST)
209 /* Set of supported meter band types */
210 #define DP_SUPPORTED_METER_BAND_TYPES \
211 ( 1 << OFPMBT13_DROP )
213 struct dp_meter_band
{
214 struct ofputil_meter_band up
; /* type, prec_level, pad, rate, burst_size */
215 uint32_t bucket
; /* In 1/1000 packets (for PKTPS), or in bits (for KBPS) */
216 uint64_t packet_count
;
223 uint32_t max_delta_t
;
225 uint64_t packet_count
;
227 struct dp_meter_band bands
[];
230 /* Datapath based on the network device interface from netdev.h.
236 * Some members, marked 'const', are immutable. Accessing other members
237 * requires synchronization, as noted in more detail below.
239 * Acquisition order is, from outermost to innermost:
241 * dp_netdev_mutex (global)
246 const struct dpif_class
*const class;
247 const char *const name
;
249 struct ovs_refcount ref_cnt
;
250 atomic_flag destroyed
;
254 * Any lookup into 'ports' or any access to the dp_netdev_ports found
255 * through 'ports' requires taking 'port_mutex'. */
256 struct ovs_mutex port_mutex
;
258 struct seq
*port_seq
; /* Incremented whenever a port changes. */
261 struct ovs_mutex meter_locks
[N_METER_LOCKS
];
262 struct dp_meter
*meters
[MAX_METERS
]; /* Meter bands. */
264 /* Protects access to ofproto-dpif-upcall interface during revalidator
265 * thread synchronization. */
266 struct fat_rwlock upcall_rwlock
;
267 upcall_callback
*upcall_cb
; /* Callback function for executing upcalls. */
270 /* Callback function for notifying the purging of dp flows (during
271 * reseting pmd deletion). */
272 dp_purge_callback
*dp_purge_cb
;
275 /* Stores all 'struct dp_netdev_pmd_thread's. */
276 struct cmap poll_threads
;
278 /* Protects the access of the 'struct dp_netdev_pmd_thread'
279 * instance for non-pmd thread. */
280 struct ovs_mutex non_pmd_mutex
;
282 /* Each pmd thread will store its pointer to
283 * 'struct dp_netdev_pmd_thread' in 'per_pmd_key'. */
284 ovsthread_key_t per_pmd_key
;
286 struct seq
*reconfigure_seq
;
287 uint64_t last_reconfigure_seq
;
289 /* Cpu mask for pin of pmd threads. */
292 uint64_t last_tnl_conf_seq
;
294 struct conntrack conntrack
;
296 /* Probability of EMC insertions is a factor of 'emc_insert_min'.*/
297 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
) atomic_uint32_t emc_insert_min
;
300 static void meter_lock(const struct dp_netdev
*dp
, uint32_t meter_id
)
301 OVS_ACQUIRES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
303 ovs_mutex_lock(&dp
->meter_locks
[meter_id
% N_METER_LOCKS
]);
306 static void meter_unlock(const struct dp_netdev
*dp
, uint32_t meter_id
)
307 OVS_RELEASES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
309 ovs_mutex_unlock(&dp
->meter_locks
[meter_id
% N_METER_LOCKS
]);
313 static struct dp_netdev_port
*dp_netdev_lookup_port(const struct dp_netdev
*dp
,
315 OVS_REQUIRES(dp
->port_mutex
);
318 DP_STAT_EXACT_HIT
, /* Packets that had an exact match (emc). */
319 DP_STAT_MASKED_HIT
, /* Packets that matched in the flow table. */
320 DP_STAT_MISS
, /* Packets that did not match. */
321 DP_STAT_LOST
, /* Packets not passed up to the client. */
322 DP_STAT_LOOKUP_HIT
, /* Number of subtable lookups for flow table
327 enum pmd_cycles_counter_type
{
328 PMD_CYCLES_POLLING
, /* Cycles spent polling NICs. */
329 PMD_CYCLES_PROCESSING
, /* Cycles spent processing packets */
333 #define XPS_TIMEOUT_MS 500LL
335 /* Contained by struct dp_netdev_port's 'rxqs' member. */
336 struct dp_netdev_rxq
{
337 struct dp_netdev_port
*port
;
338 struct netdev_rxq
*rx
;
339 unsigned core_id
; /* Core to which this queue should be
340 pinned. OVS_CORE_UNSPEC if the
341 queue doesn't need to be pinned to a
343 struct dp_netdev_pmd_thread
*pmd
; /* pmd thread that will poll this queue. */
346 /* A port in a netdev-based datapath. */
347 struct dp_netdev_port
{
349 struct netdev
*netdev
;
350 struct hmap_node node
; /* Node in dp_netdev's 'ports'. */
351 struct netdev_saved_flags
*sf
;
352 struct dp_netdev_rxq
*rxqs
;
353 unsigned n_rxq
; /* Number of elements in 'rxq' */
354 bool dynamic_txqs
; /* If true XPS will be used. */
355 unsigned *txq_used
; /* Number of threads that uses each tx queue. */
356 struct ovs_mutex txq_used_mutex
;
357 char *type
; /* Port type as requested by user. */
358 char *rxq_affinity_list
; /* Requested affinity of rx queues. */
359 bool need_reconfigure
; /* True if we should reconfigure netdev. */
362 /* Contained by struct dp_netdev_flow's 'stats' member. */
363 struct dp_netdev_flow_stats
{
364 atomic_llong used
; /* Last used time, in monotonic msecs. */
365 atomic_ullong packet_count
; /* Number of packets matched. */
366 atomic_ullong byte_count
; /* Number of bytes matched. */
367 atomic_uint16_t tcp_flags
; /* Bitwise-OR of seen tcp_flags values. */
370 /* A flow in 'dp_netdev_pmd_thread's 'flow_table'.
376 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
377 * its pmd thread's classifier. The text below calls this classifier 'cls'.
382 * The thread safety rules described here for "struct dp_netdev_flow" are
383 * motivated by two goals:
385 * - Prevent threads that read members of "struct dp_netdev_flow" from
386 * reading bad data due to changes by some thread concurrently modifying
389 * - Prevent two threads making changes to members of a given "struct
390 * dp_netdev_flow" from interfering with each other.
396 * A flow 'flow' may be accessed without a risk of being freed during an RCU
397 * grace period. Code that needs to hold onto a flow for a while
398 * should try incrementing 'flow->ref_cnt' with dp_netdev_flow_ref().
400 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
401 * flow from being deleted from 'cls' and it doesn't protect members of 'flow'
404 * Some members, marked 'const', are immutable. Accessing other members
405 * requires synchronization, as noted in more detail below.
407 struct dp_netdev_flow
{
408 const struct flow flow
; /* Unmasked flow that created this entry. */
409 /* Hash table index by unmasked flow. */
410 const struct cmap_node node
; /* In owning dp_netdev_pmd_thread's */
412 const ovs_u128 ufid
; /* Unique flow identifier. */
413 const unsigned pmd_id
; /* The 'core_id' of pmd thread owning this */
416 /* Number of references.
417 * The classifier owns one reference.
418 * Any thread trying to keep a rule from being freed should hold its own
420 struct ovs_refcount ref_cnt
;
425 struct dp_netdev_flow_stats stats
;
428 OVSRCU_TYPE(struct dp_netdev_actions
*) actions
;
430 /* While processing a group of input packets, the datapath uses the next
431 * member to store a pointer to the output batch for the flow. It is
432 * reset after the batch has been sent out (See dp_netdev_queue_batches(),
433 * packet_batch_per_flow_init() and packet_batch_per_flow_execute()). */
434 struct packet_batch_per_flow
*batch
;
436 /* Packet classification. */
437 struct dpcls_rule cr
; /* In owning dp_netdev's 'cls'. */
438 /* 'cr' must be the last member. */
441 static void dp_netdev_flow_unref(struct dp_netdev_flow
*);
442 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*);
443 static int dpif_netdev_flow_from_nlattrs(const struct nlattr
*, uint32_t,
444 struct flow
*, bool);
446 /* A set of datapath actions within a "struct dp_netdev_flow".
452 * A struct dp_netdev_actions 'actions' is protected with RCU. */
453 struct dp_netdev_actions
{
454 /* These members are immutable: they do not change during the struct's
456 unsigned int size
; /* Size of 'actions', in bytes. */
457 struct nlattr actions
[]; /* Sequence of OVS_ACTION_ATTR_* attributes. */
460 struct dp_netdev_actions
*dp_netdev_actions_create(const struct nlattr
*,
462 struct dp_netdev_actions
*dp_netdev_flow_get_actions(
463 const struct dp_netdev_flow
*);
464 static void dp_netdev_actions_free(struct dp_netdev_actions
*);
466 /* Contained by struct dp_netdev_pmd_thread's 'stats' member. */
467 struct dp_netdev_pmd_stats
{
468 /* Indexed by DP_STAT_*. */
469 atomic_ullong n
[DP_N_STATS
];
472 /* Contained by struct dp_netdev_pmd_thread's 'cycle' member. */
473 struct dp_netdev_pmd_cycles
{
474 /* Indexed by PMD_CYCLES_*. */
475 atomic_ullong n
[PMD_N_CYCLES
];
478 struct polled_queue
{
479 struct netdev_rxq
*rx
;
483 /* Contained by struct dp_netdev_pmd_thread's 'poll_list' member. */
485 struct dp_netdev_rxq
*rxq
;
486 struct hmap_node node
;
489 /* Contained by struct dp_netdev_pmd_thread's 'send_port_cache',
490 * 'tnl_port_cache' or 'tx_ports'. */
492 struct dp_netdev_port
*port
;
495 struct hmap_node node
;
498 /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
499 * the performance overhead of interrupt processing. Therefore netdev can
500 * not implement rx-wait for these devices. dpif-netdev needs to poll
501 * these device to check for recv buffer. pmd-thread does polling for
502 * devices assigned to itself.
504 * DPDK used PMD for accessing NIC.
506 * Note, instance with cpu core id NON_PMD_CORE_ID will be reserved for
507 * I/O of all non-pmd threads. There will be no actual thread created
510 * Each struct has its own flow table and classifier. Packets received
511 * from managed ports are looked up in the corresponding pmd thread's
512 * flow table, and are executed with the found actions.
514 struct dp_netdev_pmd_thread
{
515 struct dp_netdev
*dp
;
516 struct ovs_refcount ref_cnt
; /* Every reference must be refcount'ed. */
517 struct cmap_node node
; /* In 'dp->poll_threads'. */
519 pthread_cond_t cond
; /* For synchronizing pmd thread reload. */
520 struct ovs_mutex cond_mutex
; /* Mutex for condition variable. */
522 /* Per thread exact-match cache. Note, the instance for cpu core
523 * NON_PMD_CORE_ID can be accessed by multiple threads, and thusly
524 * need to be protected by 'non_pmd_mutex'. Every other instance
525 * will only be accessed by its own pmd thread. */
526 struct emc_cache flow_cache
;
528 /* Flow-Table and classifiers
530 * Writers of 'flow_table' must take the 'flow_mutex'. Corresponding
531 * changes to 'classifiers' must be made while still holding the
534 struct ovs_mutex flow_mutex
;
535 struct cmap flow_table OVS_GUARDED
; /* Flow table. */
537 /* One classifier per in_port polled by the pmd */
538 struct cmap classifiers
;
539 /* Periodically sort subtable vectors according to hit frequencies */
540 long long int next_optimization
;
543 struct dp_netdev_pmd_stats stats
;
545 /* Cycles counters */
546 struct dp_netdev_pmd_cycles cycles
;
548 /* Used to count cicles. See 'cycles_counter_end()' */
549 unsigned long long last_cycles
;
551 struct latch exit_latch
; /* For terminating the pmd thread. */
552 struct seq
*reload_seq
;
553 uint64_t last_reload_seq
;
554 atomic_bool reload
; /* Do we need to reload ports? */
556 unsigned core_id
; /* CPU core id of this pmd thread. */
557 int numa_id
; /* numa node id of this pmd thread. */
560 /* Queue id used by this pmd thread to send packets on all netdevs if
561 * XPS disabled for this netdev. All static_tx_qid's are unique and less
562 * than 'cmap_count(dp->poll_threads)'. */
563 const int static_tx_qid
;
565 struct ovs_mutex port_mutex
; /* Mutex for 'poll_list' and 'tx_ports'. */
566 /* List of rx queues to poll. */
567 struct hmap poll_list OVS_GUARDED
;
568 /* Map of 'tx_port's used for transmission. Written by the main thread,
569 * read by the pmd thread. */
570 struct hmap tx_ports OVS_GUARDED
;
572 /* These are thread-local copies of 'tx_ports'. One contains only tunnel
573 * ports (that support push_tunnel/pop_tunnel), the other contains ports
574 * with at least one txq (that support send). A port can be in both.
576 * There are two separate maps to make sure that we don't try to execute
577 * OUTPUT on a device which has 0 txqs or PUSH/POP on a non-tunnel device.
579 * The instances for cpu core NON_PMD_CORE_ID can be accessed by multiple
580 * threads, and thusly need to be protected by 'non_pmd_mutex'. Every
581 * other instance will only be accessed by its own pmd thread. */
582 struct hmap tnl_port_cache
;
583 struct hmap send_port_cache
;
585 /* Only a pmd thread can write on its own 'cycles' and 'stats'.
586 * The main thread keeps 'stats_zero' and 'cycles_zero' as base
587 * values and subtracts them from 'stats' and 'cycles' before
588 * reporting to the user */
589 unsigned long long stats_zero
[DP_N_STATS
];
590 uint64_t cycles_zero
[PMD_N_CYCLES
];
592 /* Set to true if the pmd thread needs to be reloaded. */
596 /* Interface to netdev-based datapath. */
599 struct dp_netdev
*dp
;
600 uint64_t last_port_seq
;
603 static int get_port_by_number(struct dp_netdev
*dp
, odp_port_t port_no
,
604 struct dp_netdev_port
**portp
)
605 OVS_REQUIRES(dp
->port_mutex
);
606 static int get_port_by_name(struct dp_netdev
*dp
, const char *devname
,
607 struct dp_netdev_port
**portp
)
608 OVS_REQUIRES(dp
->port_mutex
);
609 static void dp_netdev_free(struct dp_netdev
*)
610 OVS_REQUIRES(dp_netdev_mutex
);
611 static int do_add_port(struct dp_netdev
*dp
, const char *devname
,
612 const char *type
, odp_port_t port_no
)
613 OVS_REQUIRES(dp
->port_mutex
);
614 static void do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*)
615 OVS_REQUIRES(dp
->port_mutex
);
616 static int dpif_netdev_open(const struct dpif_class
*, const char *name
,
617 bool create
, struct dpif
**);
618 static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
619 struct dp_packet_batch
*,
620 bool may_steal
, const struct flow
*flow
,
621 const struct nlattr
*actions
,
624 static void dp_netdev_input(struct dp_netdev_pmd_thread
*,
625 struct dp_packet_batch
*, odp_port_t port_no
);
626 static void dp_netdev_recirculate(struct dp_netdev_pmd_thread
*,
627 struct dp_packet_batch
*);
629 static void dp_netdev_disable_upcall(struct dp_netdev
*);
630 static void dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
);
631 static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
,
632 struct dp_netdev
*dp
, unsigned core_id
,
634 static void dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
);
635 static void dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
636 OVS_REQUIRES(dp
->port_mutex
);
638 static void *pmd_thread_main(void *);
639 static struct dp_netdev_pmd_thread
*dp_netdev_get_pmd(struct dp_netdev
*dp
,
641 static struct dp_netdev_pmd_thread
*
642 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
);
643 static void dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
, bool non_pmd
);
644 static void dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
);
645 static void dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
646 struct dp_netdev_port
*port
)
647 OVS_REQUIRES(pmd
->port_mutex
);
648 static void dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
650 OVS_REQUIRES(pmd
->port_mutex
);
651 static void dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
652 struct dp_netdev_rxq
*rxq
)
653 OVS_REQUIRES(pmd
->port_mutex
);
654 static void dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
655 struct rxq_poll
*poll
)
656 OVS_REQUIRES(pmd
->port_mutex
);
657 static void reconfigure_datapath(struct dp_netdev
*dp
)
658 OVS_REQUIRES(dp
->port_mutex
);
659 static bool dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
);
660 static void dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
);
661 static void dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
);
662 static void pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
663 OVS_REQUIRES(pmd
->port_mutex
);
665 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread
*pmd
);
668 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
669 long long now
, bool purge
);
670 static int dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
671 struct tx_port
*tx
, long long now
);
673 static inline bool emc_entry_alive(struct emc_entry
*ce
);
674 static void emc_clear_entry(struct emc_entry
*ce
);
677 emc_cache_init(struct emc_cache
*flow_cache
)
681 flow_cache
->sweep_idx
= 0;
682 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
683 flow_cache
->entries
[i
].flow
= NULL
;
684 flow_cache
->entries
[i
].key
.hash
= 0;
685 flow_cache
->entries
[i
].key
.len
= sizeof(struct miniflow
);
686 flowmap_init(&flow_cache
->entries
[i
].key
.mf
.map
);
691 emc_cache_uninit(struct emc_cache
*flow_cache
)
695 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
696 emc_clear_entry(&flow_cache
->entries
[i
]);
700 /* Check and clear dead flow references slowly (one entry at each
703 emc_cache_slow_sweep(struct emc_cache
*flow_cache
)
705 struct emc_entry
*entry
= &flow_cache
->entries
[flow_cache
->sweep_idx
];
707 if (!emc_entry_alive(entry
)) {
708 emc_clear_entry(entry
);
710 flow_cache
->sweep_idx
= (flow_cache
->sweep_idx
+ 1) & EM_FLOW_HASH_MASK
;
713 /* Returns true if 'dpif' is a netdev or dummy dpif, false otherwise. */
715 dpif_is_netdev(const struct dpif
*dpif
)
717 return dpif
->dpif_class
->open
== dpif_netdev_open
;
720 static struct dpif_netdev
*
721 dpif_netdev_cast(const struct dpif
*dpif
)
723 ovs_assert(dpif_is_netdev(dpif
));
724 return CONTAINER_OF(dpif
, struct dpif_netdev
, dpif
);
727 static struct dp_netdev
*
728 get_dp_netdev(const struct dpif
*dpif
)
730 return dpif_netdev_cast(dpif
)->dp
;
734 PMD_INFO_SHOW_STATS
, /* Show how cpu cycles are spent. */
735 PMD_INFO_CLEAR_STATS
, /* Set the cycles count to 0. */
736 PMD_INFO_SHOW_RXQ
/* Show poll-lists of pmd threads. */
740 pmd_info_show_stats(struct ds
*reply
,
741 struct dp_netdev_pmd_thread
*pmd
,
742 unsigned long long stats
[DP_N_STATS
],
743 uint64_t cycles
[PMD_N_CYCLES
])
745 unsigned long long total_packets
= 0;
746 uint64_t total_cycles
= 0;
749 /* These loops subtracts reference values ('*_zero') from the counters.
750 * Since loads and stores are relaxed, it might be possible for a '*_zero'
751 * value to be more recent than the current value we're reading from the
752 * counter. This is not a big problem, since these numbers are not
753 * supposed to be too accurate, but we should at least make sure that
754 * the result is not negative. */
755 for (i
= 0; i
< DP_N_STATS
; i
++) {
756 if (stats
[i
] > pmd
->stats_zero
[i
]) {
757 stats
[i
] -= pmd
->stats_zero
[i
];
762 if (i
!= DP_STAT_LOST
) {
763 /* Lost packets are already included in DP_STAT_MISS */
764 total_packets
+= stats
[i
];
768 for (i
= 0; i
< PMD_N_CYCLES
; i
++) {
769 if (cycles
[i
] > pmd
->cycles_zero
[i
]) {
770 cycles
[i
] -= pmd
->cycles_zero
[i
];
775 total_cycles
+= cycles
[i
];
778 ds_put_cstr(reply
, (pmd
->core_id
== NON_PMD_CORE_ID
)
779 ? "main thread" : "pmd thread");
781 if (pmd
->numa_id
!= OVS_NUMA_UNSPEC
) {
782 ds_put_format(reply
, " numa_id %d", pmd
->numa_id
);
784 if (pmd
->core_id
!= OVS_CORE_UNSPEC
&& pmd
->core_id
!= NON_PMD_CORE_ID
) {
785 ds_put_format(reply
, " core_id %u", pmd
->core_id
);
787 ds_put_cstr(reply
, ":\n");
790 "\temc hits:%llu\n\tmegaflow hits:%llu\n"
791 "\tavg. subtable lookups per hit:%.2f\n"
792 "\tmiss:%llu\n\tlost:%llu\n",
793 stats
[DP_STAT_EXACT_HIT
], stats
[DP_STAT_MASKED_HIT
],
794 stats
[DP_STAT_MASKED_HIT
] > 0
795 ? (1.0*stats
[DP_STAT_LOOKUP_HIT
])/stats
[DP_STAT_MASKED_HIT
]
797 stats
[DP_STAT_MISS
], stats
[DP_STAT_LOST
]);
799 if (total_cycles
== 0) {
804 "\tpolling cycles:%"PRIu64
" (%.02f%%)\n"
805 "\tprocessing cycles:%"PRIu64
" (%.02f%%)\n",
806 cycles
[PMD_CYCLES_POLLING
],
807 cycles
[PMD_CYCLES_POLLING
] / (double)total_cycles
* 100,
808 cycles
[PMD_CYCLES_PROCESSING
],
809 cycles
[PMD_CYCLES_PROCESSING
] / (double)total_cycles
* 100);
811 if (total_packets
== 0) {
816 "\tavg cycles per packet: %.02f (%"PRIu64
"/%llu)\n",
817 total_cycles
/ (double)total_packets
,
818 total_cycles
, total_packets
);
821 "\tavg processing cycles per packet: "
822 "%.02f (%"PRIu64
"/%llu)\n",
823 cycles
[PMD_CYCLES_PROCESSING
] / (double)total_packets
,
824 cycles
[PMD_CYCLES_PROCESSING
], total_packets
);
828 pmd_info_clear_stats(struct ds
*reply OVS_UNUSED
,
829 struct dp_netdev_pmd_thread
*pmd
,
830 unsigned long long stats
[DP_N_STATS
],
831 uint64_t cycles
[PMD_N_CYCLES
])
835 /* We cannot write 'stats' and 'cycles' (because they're written by other
836 * threads) and we shouldn't change 'stats' (because they're used to count
837 * datapath stats, which must not be cleared here). Instead, we save the
838 * current values and subtract them from the values to be displayed in the
840 for (i
= 0; i
< DP_N_STATS
; i
++) {
841 pmd
->stats_zero
[i
] = stats
[i
];
843 for (i
= 0; i
< PMD_N_CYCLES
; i
++) {
844 pmd
->cycles_zero
[i
] = cycles
[i
];
849 compare_poll_list(const void *a_
, const void *b_
)
851 const struct rxq_poll
*a
= a_
;
852 const struct rxq_poll
*b
= b_
;
854 const char *namea
= netdev_rxq_get_name(a
->rxq
->rx
);
855 const char *nameb
= netdev_rxq_get_name(b
->rxq
->rx
);
857 int cmp
= strcmp(namea
, nameb
);
859 return netdev_rxq_get_queue_id(a
->rxq
->rx
)
860 - netdev_rxq_get_queue_id(b
->rxq
->rx
);
867 sorted_poll_list(struct dp_netdev_pmd_thread
*pmd
, struct rxq_poll
**list
,
870 struct rxq_poll
*ret
, *poll
;
873 *n
= hmap_count(&pmd
->poll_list
);
877 ret
= xcalloc(*n
, sizeof *ret
);
879 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
886 qsort(ret
, *n
, sizeof *ret
, compare_poll_list
);
892 pmd_info_show_rxq(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
894 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
895 const char *prev_name
= NULL
;
896 struct rxq_poll
*list
;
900 "pmd thread numa_id %d core_id %u:\n\tisolated : %s\n",
901 pmd
->numa_id
, pmd
->core_id
, (pmd
->isolated
)
904 ovs_mutex_lock(&pmd
->port_mutex
);
905 sorted_poll_list(pmd
, &list
, &n
);
906 for (i
= 0; i
< n
; i
++) {
907 const char *name
= netdev_rxq_get_name(list
[i
].rxq
->rx
);
909 if (!prev_name
|| strcmp(name
, prev_name
)) {
911 ds_put_cstr(reply
, "\n");
913 ds_put_format(reply
, "\tport: %s\tqueue-id:", name
);
915 ds_put_format(reply
, " %d",
916 netdev_rxq_get_queue_id(list
[i
].rxq
->rx
));
919 ovs_mutex_unlock(&pmd
->port_mutex
);
920 ds_put_cstr(reply
, "\n");
926 dpif_netdev_pmd_info(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
929 struct ds reply
= DS_EMPTY_INITIALIZER
;
930 struct dp_netdev_pmd_thread
*pmd
;
931 struct dp_netdev
*dp
= NULL
;
932 enum pmd_info_type type
= *(enum pmd_info_type
*) aux
;
934 ovs_mutex_lock(&dp_netdev_mutex
);
937 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
938 } else if (shash_count(&dp_netdevs
) == 1) {
939 /* There's only one datapath */
940 dp
= shash_first(&dp_netdevs
)->data
;
944 ovs_mutex_unlock(&dp_netdev_mutex
);
945 unixctl_command_reply_error(conn
,
946 "please specify an existing datapath");
950 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
951 if (type
== PMD_INFO_SHOW_RXQ
) {
952 pmd_info_show_rxq(&reply
, pmd
);
954 unsigned long long stats
[DP_N_STATS
];
955 uint64_t cycles
[PMD_N_CYCLES
];
958 /* Read current stats and cycle counters */
959 for (i
= 0; i
< ARRAY_SIZE(stats
); i
++) {
960 atomic_read_relaxed(&pmd
->stats
.n
[i
], &stats
[i
]);
962 for (i
= 0; i
< ARRAY_SIZE(cycles
); i
++) {
963 atomic_read_relaxed(&pmd
->cycles
.n
[i
], &cycles
[i
]);
966 if (type
== PMD_INFO_CLEAR_STATS
) {
967 pmd_info_clear_stats(&reply
, pmd
, stats
, cycles
);
968 } else if (type
== PMD_INFO_SHOW_STATS
) {
969 pmd_info_show_stats(&reply
, pmd
, stats
, cycles
);
974 ovs_mutex_unlock(&dp_netdev_mutex
);
976 unixctl_command_reply(conn
, ds_cstr(&reply
));
981 dpif_netdev_init(void)
983 static enum pmd_info_type show_aux
= PMD_INFO_SHOW_STATS
,
984 clear_aux
= PMD_INFO_CLEAR_STATS
,
985 poll_aux
= PMD_INFO_SHOW_RXQ
;
987 unixctl_command_register("dpif-netdev/pmd-stats-show", "[dp]",
988 0, 1, dpif_netdev_pmd_info
,
990 unixctl_command_register("dpif-netdev/pmd-stats-clear", "[dp]",
991 0, 1, dpif_netdev_pmd_info
,
993 unixctl_command_register("dpif-netdev/pmd-rxq-show", "[dp]",
994 0, 1, dpif_netdev_pmd_info
,
1000 dpif_netdev_enumerate(struct sset
*all_dps
,
1001 const struct dpif_class
*dpif_class
)
1003 struct shash_node
*node
;
1005 ovs_mutex_lock(&dp_netdev_mutex
);
1006 SHASH_FOR_EACH(node
, &dp_netdevs
) {
1007 struct dp_netdev
*dp
= node
->data
;
1008 if (dpif_class
!= dp
->class) {
1009 /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs.
1010 * If the class doesn't match, skip this dpif. */
1013 sset_add(all_dps
, node
->name
);
1015 ovs_mutex_unlock(&dp_netdev_mutex
);
1021 dpif_netdev_class_is_dummy(const struct dpif_class
*class)
1023 return class != &dpif_netdev_class
;
1027 dpif_netdev_port_open_type(const struct dpif_class
*class, const char *type
)
1029 return strcmp(type
, "internal") ? type
1030 : dpif_netdev_class_is_dummy(class) ? "dummy-internal"
1034 static struct dpif
*
1035 create_dpif_netdev(struct dp_netdev
*dp
)
1037 uint16_t netflow_id
= hash_string(dp
->name
, 0);
1038 struct dpif_netdev
*dpif
;
1040 ovs_refcount_ref(&dp
->ref_cnt
);
1042 dpif
= xmalloc(sizeof *dpif
);
1043 dpif_init(&dpif
->dpif
, dp
->class, dp
->name
, netflow_id
>> 8, netflow_id
);
1045 dpif
->last_port_seq
= seq_read(dp
->port_seq
);
1050 /* Choose an unused, non-zero port number and return it on success.
1051 * Return ODPP_NONE on failure. */
1053 choose_port(struct dp_netdev
*dp
, const char *name
)
1054 OVS_REQUIRES(dp
->port_mutex
)
1058 if (dp
->class != &dpif_netdev_class
) {
1062 /* If the port name begins with "br", start the number search at
1063 * 100 to make writing tests easier. */
1064 if (!strncmp(name
, "br", 2)) {
1068 /* If the port name contains a number, try to assign that port number.
1069 * This can make writing unit tests easier because port numbers are
1071 for (p
= name
; *p
!= '\0'; p
++) {
1072 if (isdigit((unsigned char) *p
)) {
1073 port_no
= start_no
+ strtol(p
, NULL
, 10);
1074 if (port_no
> 0 && port_no
!= odp_to_u32(ODPP_NONE
)
1075 && !dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
1076 return u32_to_odp(port_no
);
1083 for (port_no
= 1; port_no
<= UINT16_MAX
; port_no
++) {
1084 if (!dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
1085 return u32_to_odp(port_no
);
1093 create_dp_netdev(const char *name
, const struct dpif_class
*class,
1094 struct dp_netdev
**dpp
)
1095 OVS_REQUIRES(dp_netdev_mutex
)
1097 struct dp_netdev
*dp
;
1100 dp
= xzalloc(sizeof *dp
);
1101 shash_add(&dp_netdevs
, name
, dp
);
1103 *CONST_CAST(const struct dpif_class
**, &dp
->class) = class;
1104 *CONST_CAST(const char **, &dp
->name
) = xstrdup(name
);
1105 ovs_refcount_init(&dp
->ref_cnt
);
1106 atomic_flag_clear(&dp
->destroyed
);
1108 ovs_mutex_init(&dp
->port_mutex
);
1109 hmap_init(&dp
->ports
);
1110 dp
->port_seq
= seq_create();
1111 fat_rwlock_init(&dp
->upcall_rwlock
);
1113 dp
->reconfigure_seq
= seq_create();
1114 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
1116 for (int i
= 0; i
< N_METER_LOCKS
; ++i
) {
1117 ovs_mutex_init_adaptive(&dp
->meter_locks
[i
]);
1120 /* Disable upcalls by default. */
1121 dp_netdev_disable_upcall(dp
);
1122 dp
->upcall_aux
= NULL
;
1123 dp
->upcall_cb
= NULL
;
1125 conntrack_init(&dp
->conntrack
);
1127 atomic_init(&dp
->emc_insert_min
, DEFAULT_EM_FLOW_INSERT_MIN
);
1129 cmap_init(&dp
->poll_threads
);
1130 ovs_mutex_init_recursive(&dp
->non_pmd_mutex
);
1131 ovsthread_key_create(&dp
->per_pmd_key
, NULL
);
1133 ovs_mutex_lock(&dp
->port_mutex
);
1134 dp_netdev_set_nonpmd(dp
);
1136 error
= do_add_port(dp
, name
, dpif_netdev_port_open_type(dp
->class,
1139 ovs_mutex_unlock(&dp
->port_mutex
);
1145 dp
->last_tnl_conf_seq
= seq_read(tnl_conf_seq
);
1151 dp_netdev_request_reconfigure(struct dp_netdev
*dp
)
1153 seq_change(dp
->reconfigure_seq
);
1157 dp_netdev_is_reconf_required(struct dp_netdev
*dp
)
1159 return seq_read(dp
->reconfigure_seq
) != dp
->last_reconfigure_seq
;
1163 dpif_netdev_open(const struct dpif_class
*class, const char *name
,
1164 bool create
, struct dpif
**dpifp
)
1166 struct dp_netdev
*dp
;
1169 ovs_mutex_lock(&dp_netdev_mutex
);
1170 dp
= shash_find_data(&dp_netdevs
, name
);
1172 error
= create
? create_dp_netdev(name
, class, &dp
) : ENODEV
;
1174 error
= (dp
->class != class ? EINVAL
1179 *dpifp
= create_dpif_netdev(dp
);
1182 ovs_mutex_unlock(&dp_netdev_mutex
);
1188 dp_netdev_destroy_upcall_lock(struct dp_netdev
*dp
)
1189 OVS_NO_THREAD_SAFETY_ANALYSIS
1191 /* Check that upcalls are disabled, i.e. that the rwlock is taken */
1192 ovs_assert(fat_rwlock_tryrdlock(&dp
->upcall_rwlock
));
1194 /* Before freeing a lock we should release it */
1195 fat_rwlock_unlock(&dp
->upcall_rwlock
);
1196 fat_rwlock_destroy(&dp
->upcall_rwlock
);
1200 dp_delete_meter(struct dp_netdev
*dp
, uint32_t meter_id
)
1201 OVS_REQUIRES(dp
->meter_locks
[meter_id
% N_METER_LOCKS
])
1203 if (dp
->meters
[meter_id
]) {
1204 free(dp
->meters
[meter_id
]);
1205 dp
->meters
[meter_id
] = NULL
;
1209 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
1210 * through the 'dp_netdevs' shash while freeing 'dp'. */
1212 dp_netdev_free(struct dp_netdev
*dp
)
1213 OVS_REQUIRES(dp_netdev_mutex
)
1215 struct dp_netdev_port
*port
, *next
;
1217 shash_find_and_delete(&dp_netdevs
, dp
->name
);
1219 ovs_mutex_lock(&dp
->port_mutex
);
1220 HMAP_FOR_EACH_SAFE (port
, next
, node
, &dp
->ports
) {
1221 do_del_port(dp
, port
);
1223 ovs_mutex_unlock(&dp
->port_mutex
);
1225 dp_netdev_destroy_all_pmds(dp
, true);
1226 cmap_destroy(&dp
->poll_threads
);
1228 ovs_mutex_destroy(&dp
->non_pmd_mutex
);
1229 ovsthread_key_delete(dp
->per_pmd_key
);
1231 conntrack_destroy(&dp
->conntrack
);
1234 seq_destroy(dp
->reconfigure_seq
);
1236 seq_destroy(dp
->port_seq
);
1237 hmap_destroy(&dp
->ports
);
1238 ovs_mutex_destroy(&dp
->port_mutex
);
1240 /* Upcalls must be disabled at this point */
1241 dp_netdev_destroy_upcall_lock(dp
);
1245 for (i
= 0; i
< MAX_METERS
; ++i
) {
1247 dp_delete_meter(dp
, i
);
1248 meter_unlock(dp
, i
);
1250 for (i
= 0; i
< N_METER_LOCKS
; ++i
) {
1251 ovs_mutex_destroy(&dp
->meter_locks
[i
]);
1254 free(dp
->pmd_cmask
);
1255 free(CONST_CAST(char *, dp
->name
));
1260 dp_netdev_unref(struct dp_netdev
*dp
)
1263 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
1264 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
1265 ovs_mutex_lock(&dp_netdev_mutex
);
1266 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1269 ovs_mutex_unlock(&dp_netdev_mutex
);
1274 dpif_netdev_close(struct dpif
*dpif
)
1276 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1278 dp_netdev_unref(dp
);
1283 dpif_netdev_destroy(struct dpif
*dpif
)
1285 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1287 if (!atomic_flag_test_and_set(&dp
->destroyed
)) {
1288 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1289 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
1297 /* Add 'n' to the atomic variable 'var' non-atomically and using relaxed
1298 * load/store semantics. While the increment is not atomic, the load and
1299 * store operations are, making it impossible to read inconsistent values.
1301 * This is used to update thread local stats counters. */
1303 non_atomic_ullong_add(atomic_ullong
*var
, unsigned long long n
)
1305 unsigned long long tmp
;
1307 atomic_read_relaxed(var
, &tmp
);
1309 atomic_store_relaxed(var
, tmp
);
1313 dpif_netdev_get_stats(const struct dpif
*dpif
, struct dpif_dp_stats
*stats
)
1315 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1316 struct dp_netdev_pmd_thread
*pmd
;
1318 stats
->n_flows
= stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
1319 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1320 unsigned long long n
;
1321 stats
->n_flows
+= cmap_count(&pmd
->flow_table
);
1323 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_MASKED_HIT
], &n
);
1325 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_EXACT_HIT
], &n
);
1327 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_MISS
], &n
);
1328 stats
->n_missed
+= n
;
1329 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_LOST
], &n
);
1332 stats
->n_masks
= UINT32_MAX
;
1333 stats
->n_mask_hit
= UINT64_MAX
;
1339 dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread
*pmd
)
1341 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
1342 ovs_mutex_lock(&pmd
->dp
->non_pmd_mutex
);
1343 ovs_mutex_lock(&pmd
->port_mutex
);
1344 pmd_load_cached_ports(pmd
);
1345 ovs_mutex_unlock(&pmd
->port_mutex
);
1346 ovs_mutex_unlock(&pmd
->dp
->non_pmd_mutex
);
1350 ovs_mutex_lock(&pmd
->cond_mutex
);
1351 seq_change(pmd
->reload_seq
);
1352 atomic_store_relaxed(&pmd
->reload
, true);
1353 ovs_mutex_cond_wait(&pmd
->cond
, &pmd
->cond_mutex
);
1354 ovs_mutex_unlock(&pmd
->cond_mutex
);
1358 hash_port_no(odp_port_t port_no
)
1360 return hash_int(odp_to_u32(port_no
), 0);
1364 port_create(const char *devname
, const char *type
,
1365 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1367 struct netdev_saved_flags
*sf
;
1368 struct dp_netdev_port
*port
;
1369 enum netdev_flags flags
;
1370 struct netdev
*netdev
;
1375 /* Open and validate network device. */
1376 error
= netdev_open(devname
, type
, &netdev
);
1380 /* XXX reject non-Ethernet devices */
1382 netdev_get_flags(netdev
, &flags
);
1383 if (flags
& NETDEV_LOOPBACK
) {
1384 VLOG_ERR("%s: cannot add a loopback device", devname
);
1389 error
= netdev_turn_flags_on(netdev
, NETDEV_PROMISC
, &sf
);
1391 VLOG_ERR("%s: cannot set promisc flag", devname
);
1395 port
= xzalloc(sizeof *port
);
1396 port
->port_no
= port_no
;
1397 port
->netdev
= netdev
;
1398 port
->type
= xstrdup(type
);
1400 port
->need_reconfigure
= true;
1401 ovs_mutex_init(&port
->txq_used_mutex
);
1408 netdev_close(netdev
);
1413 do_add_port(struct dp_netdev
*dp
, const char *devname
, const char *type
,
1415 OVS_REQUIRES(dp
->port_mutex
)
1417 struct dp_netdev_port
*port
;
1420 /* Reject devices already in 'dp'. */
1421 if (!get_port_by_name(dp
, devname
, &port
)) {
1425 error
= port_create(devname
, type
, port_no
, &port
);
1430 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
1431 seq_change(dp
->port_seq
);
1433 reconfigure_datapath(dp
);
1439 dpif_netdev_port_add(struct dpif
*dpif
, struct netdev
*netdev
,
1440 odp_port_t
*port_nop
)
1442 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1443 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1444 const char *dpif_port
;
1448 ovs_mutex_lock(&dp
->port_mutex
);
1449 dpif_port
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1450 if (*port_nop
!= ODPP_NONE
) {
1451 port_no
= *port_nop
;
1452 error
= dp_netdev_lookup_port(dp
, *port_nop
) ? EBUSY
: 0;
1454 port_no
= choose_port(dp
, dpif_port
);
1455 error
= port_no
== ODPP_NONE
? EFBIG
: 0;
1458 *port_nop
= port_no
;
1459 error
= do_add_port(dp
, dpif_port
, netdev_get_type(netdev
), port_no
);
1461 ovs_mutex_unlock(&dp
->port_mutex
);
1467 dpif_netdev_port_del(struct dpif
*dpif
, odp_port_t port_no
)
1469 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1472 ovs_mutex_lock(&dp
->port_mutex
);
1473 if (port_no
== ODPP_LOCAL
) {
1476 struct dp_netdev_port
*port
;
1478 error
= get_port_by_number(dp
, port_no
, &port
);
1480 do_del_port(dp
, port
);
1483 ovs_mutex_unlock(&dp
->port_mutex
);
1489 is_valid_port_number(odp_port_t port_no
)
1491 return port_no
!= ODPP_NONE
;
1494 static struct dp_netdev_port
*
1495 dp_netdev_lookup_port(const struct dp_netdev
*dp
, odp_port_t port_no
)
1496 OVS_REQUIRES(dp
->port_mutex
)
1498 struct dp_netdev_port
*port
;
1500 HMAP_FOR_EACH_WITH_HASH (port
, node
, hash_port_no(port_no
), &dp
->ports
) {
1501 if (port
->port_no
== port_no
) {
1509 get_port_by_number(struct dp_netdev
*dp
,
1510 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1511 OVS_REQUIRES(dp
->port_mutex
)
1513 if (!is_valid_port_number(port_no
)) {
1517 *portp
= dp_netdev_lookup_port(dp
, port_no
);
1518 return *portp
? 0 : ENODEV
;
1523 port_destroy(struct dp_netdev_port
*port
)
1529 netdev_close(port
->netdev
);
1530 netdev_restore_flags(port
->sf
);
1532 for (unsigned i
= 0; i
< port
->n_rxq
; i
++) {
1533 netdev_rxq_close(port
->rxqs
[i
].rx
);
1535 ovs_mutex_destroy(&port
->txq_used_mutex
);
1536 free(port
->rxq_affinity_list
);
1537 free(port
->txq_used
);
1544 get_port_by_name(struct dp_netdev
*dp
,
1545 const char *devname
, struct dp_netdev_port
**portp
)
1546 OVS_REQUIRES(dp
->port_mutex
)
1548 struct dp_netdev_port
*port
;
1550 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1551 if (!strcmp(netdev_get_name(port
->netdev
), devname
)) {
1557 /* Callers of dpif_netdev_port_query_by_name() expect ENODEV for a non
1562 /* Returns 'true' if there is a port with pmd netdev. */
1564 has_pmd_port(struct dp_netdev
*dp
)
1565 OVS_REQUIRES(dp
->port_mutex
)
1567 struct dp_netdev_port
*port
;
1569 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1570 if (netdev_is_pmd(port
->netdev
)) {
1579 do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*port
)
1580 OVS_REQUIRES(dp
->port_mutex
)
1582 hmap_remove(&dp
->ports
, &port
->node
);
1583 seq_change(dp
->port_seq
);
1585 reconfigure_datapath(dp
);
1591 answer_port_query(const struct dp_netdev_port
*port
,
1592 struct dpif_port
*dpif_port
)
1594 dpif_port
->name
= xstrdup(netdev_get_name(port
->netdev
));
1595 dpif_port
->type
= xstrdup(port
->type
);
1596 dpif_port
->port_no
= port
->port_no
;
1600 dpif_netdev_port_query_by_number(const struct dpif
*dpif
, odp_port_t port_no
,
1601 struct dpif_port
*dpif_port
)
1603 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1604 struct dp_netdev_port
*port
;
1607 ovs_mutex_lock(&dp
->port_mutex
);
1608 error
= get_port_by_number(dp
, port_no
, &port
);
1609 if (!error
&& dpif_port
) {
1610 answer_port_query(port
, dpif_port
);
1612 ovs_mutex_unlock(&dp
->port_mutex
);
1618 dpif_netdev_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
1619 struct dpif_port
*dpif_port
)
1621 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1622 struct dp_netdev_port
*port
;
1625 ovs_mutex_lock(&dp
->port_mutex
);
1626 error
= get_port_by_name(dp
, devname
, &port
);
1627 if (!error
&& dpif_port
) {
1628 answer_port_query(port
, dpif_port
);
1630 ovs_mutex_unlock(&dp
->port_mutex
);
1636 dp_netdev_flow_free(struct dp_netdev_flow
*flow
)
1638 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow
));
1642 static void dp_netdev_flow_unref(struct dp_netdev_flow
*flow
)
1644 if (ovs_refcount_unref_relaxed(&flow
->ref_cnt
) == 1) {
1645 ovsrcu_postpone(dp_netdev_flow_free
, flow
);
1650 dp_netdev_flow_hash(const ovs_u128
*ufid
)
1652 return ufid
->u32
[0];
1655 static inline struct dpcls
*
1656 dp_netdev_pmd_lookup_dpcls(struct dp_netdev_pmd_thread
*pmd
,
1660 uint32_t hash
= hash_port_no(in_port
);
1661 CMAP_FOR_EACH_WITH_HASH (cls
, node
, hash
, &pmd
->classifiers
) {
1662 if (cls
->in_port
== in_port
) {
1663 /* Port classifier exists already */
1670 static inline struct dpcls
*
1671 dp_netdev_pmd_find_dpcls(struct dp_netdev_pmd_thread
*pmd
,
1673 OVS_REQUIRES(pmd
->flow_mutex
)
1675 struct dpcls
*cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
1676 uint32_t hash
= hash_port_no(in_port
);
1679 /* Create new classifier for in_port */
1680 cls
= xmalloc(sizeof(*cls
));
1682 cls
->in_port
= in_port
;
1683 cmap_insert(&pmd
->classifiers
, &cls
->node
, hash
);
1684 VLOG_DBG("Creating dpcls %p for in_port %d", cls
, in_port
);
1690 dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread
*pmd
,
1691 struct dp_netdev_flow
*flow
)
1692 OVS_REQUIRES(pmd
->flow_mutex
)
1694 struct cmap_node
*node
= CONST_CAST(struct cmap_node
*, &flow
->node
);
1696 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
1698 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
1699 ovs_assert(cls
!= NULL
);
1700 dpcls_remove(cls
, &flow
->cr
);
1701 cmap_remove(&pmd
->flow_table
, node
, dp_netdev_flow_hash(&flow
->ufid
));
1704 dp_netdev_flow_unref(flow
);
1708 dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
)
1710 struct dp_netdev_flow
*netdev_flow
;
1712 ovs_mutex_lock(&pmd
->flow_mutex
);
1713 CMAP_FOR_EACH (netdev_flow
, node
, &pmd
->flow_table
) {
1714 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
1716 ovs_mutex_unlock(&pmd
->flow_mutex
);
1720 dpif_netdev_flow_flush(struct dpif
*dpif
)
1722 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1723 struct dp_netdev_pmd_thread
*pmd
;
1725 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1726 dp_netdev_pmd_flow_flush(pmd
);
1732 struct dp_netdev_port_state
{
1733 struct hmap_position position
;
1738 dpif_netdev_port_dump_start(const struct dpif
*dpif OVS_UNUSED
, void **statep
)
1740 *statep
= xzalloc(sizeof(struct dp_netdev_port_state
));
1745 dpif_netdev_port_dump_next(const struct dpif
*dpif
, void *state_
,
1746 struct dpif_port
*dpif_port
)
1748 struct dp_netdev_port_state
*state
= state_
;
1749 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1750 struct hmap_node
*node
;
1753 ovs_mutex_lock(&dp
->port_mutex
);
1754 node
= hmap_at_position(&dp
->ports
, &state
->position
);
1756 struct dp_netdev_port
*port
;
1758 port
= CONTAINER_OF(node
, struct dp_netdev_port
, node
);
1761 state
->name
= xstrdup(netdev_get_name(port
->netdev
));
1762 dpif_port
->name
= state
->name
;
1763 dpif_port
->type
= port
->type
;
1764 dpif_port
->port_no
= port
->port_no
;
1770 ovs_mutex_unlock(&dp
->port_mutex
);
1776 dpif_netdev_port_dump_done(const struct dpif
*dpif OVS_UNUSED
, void *state_
)
1778 struct dp_netdev_port_state
*state
= state_
;
1785 dpif_netdev_port_poll(const struct dpif
*dpif_
, char **devnamep OVS_UNUSED
)
1787 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
1788 uint64_t new_port_seq
;
1791 new_port_seq
= seq_read(dpif
->dp
->port_seq
);
1792 if (dpif
->last_port_seq
!= new_port_seq
) {
1793 dpif
->last_port_seq
= new_port_seq
;
1803 dpif_netdev_port_poll_wait(const struct dpif
*dpif_
)
1805 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
1807 seq_wait(dpif
->dp
->port_seq
, dpif
->last_port_seq
);
1810 static struct dp_netdev_flow
*
1811 dp_netdev_flow_cast(const struct dpcls_rule
*cr
)
1813 return cr
? CONTAINER_OF(cr
, struct dp_netdev_flow
, cr
) : NULL
;
1816 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*flow
)
1818 return ovs_refcount_try_ref_rcu(&flow
->ref_cnt
);
1821 /* netdev_flow_key utilities.
1823 * netdev_flow_key is basically a miniflow. We use these functions
1824 * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow
1825 * functions (miniflow_clone_inline, miniflow_equal, ...), because:
1827 * - Since we are dealing exclusively with miniflows created by
1828 * miniflow_extract(), if the map is different the miniflow is different.
1829 * Therefore we can be faster by comparing the map and the miniflow in a
1831 * - These functions can be inlined by the compiler. */
1833 /* Given the number of bits set in miniflow's maps, returns the size of the
1834 * 'netdev_flow_key.mf' */
1835 static inline size_t
1836 netdev_flow_key_size(size_t flow_u64s
)
1838 return sizeof(struct miniflow
) + MINIFLOW_VALUES_SIZE(flow_u64s
);
1842 netdev_flow_key_equal(const struct netdev_flow_key
*a
,
1843 const struct netdev_flow_key
*b
)
1845 /* 'b->len' may be not set yet. */
1846 return a
->hash
== b
->hash
&& !memcmp(&a
->mf
, &b
->mf
, a
->len
);
1849 /* Used to compare 'netdev_flow_key' in the exact match cache to a miniflow.
1850 * The maps are compared bitwise, so both 'key->mf' and 'mf' must have been
1851 * generated by miniflow_extract. */
1853 netdev_flow_key_equal_mf(const struct netdev_flow_key
*key
,
1854 const struct miniflow
*mf
)
1856 return !memcmp(&key
->mf
, mf
, key
->len
);
1860 netdev_flow_key_clone(struct netdev_flow_key
*dst
,
1861 const struct netdev_flow_key
*src
)
1864 offsetof(struct netdev_flow_key
, mf
) + src
->len
);
1869 netdev_flow_key_from_flow(struct netdev_flow_key
*dst
,
1870 const struct flow
*src
)
1872 struct dp_packet packet
;
1873 uint64_t buf_stub
[512 / 8];
1875 dp_packet_use_stub(&packet
, buf_stub
, sizeof buf_stub
);
1876 pkt_metadata_from_flow(&packet
.md
, src
);
1877 flow_compose(&packet
, src
);
1878 miniflow_extract(&packet
, &dst
->mf
);
1879 dp_packet_uninit(&packet
);
1881 dst
->len
= netdev_flow_key_size(miniflow_n_values(&dst
->mf
));
1882 dst
->hash
= 0; /* Not computed yet. */
1885 /* Initialize a netdev_flow_key 'mask' from 'match'. */
1887 netdev_flow_mask_init(struct netdev_flow_key
*mask
,
1888 const struct match
*match
)
1890 uint64_t *dst
= miniflow_values(&mask
->mf
);
1891 struct flowmap fmap
;
1895 /* Only check masks that make sense for the flow. */
1896 flow_wc_map(&match
->flow
, &fmap
);
1897 flowmap_init(&mask
->mf
.map
);
1899 FLOWMAP_FOR_EACH_INDEX(idx
, fmap
) {
1900 uint64_t mask_u64
= flow_u64_value(&match
->wc
.masks
, idx
);
1903 flowmap_set(&mask
->mf
.map
, idx
, 1);
1905 hash
= hash_add64(hash
, mask_u64
);
1911 FLOWMAP_FOR_EACH_MAP (map
, mask
->mf
.map
) {
1912 hash
= hash_add64(hash
, map
);
1915 size_t n
= dst
- miniflow_get_values(&mask
->mf
);
1917 mask
->hash
= hash_finish(hash
, n
* 8);
1918 mask
->len
= netdev_flow_key_size(n
);
1921 /* Initializes 'dst' as a copy of 'flow' masked with 'mask'. */
1923 netdev_flow_key_init_masked(struct netdev_flow_key
*dst
,
1924 const struct flow
*flow
,
1925 const struct netdev_flow_key
*mask
)
1927 uint64_t *dst_u64
= miniflow_values(&dst
->mf
);
1928 const uint64_t *mask_u64
= miniflow_get_values(&mask
->mf
);
1932 dst
->len
= mask
->len
;
1933 dst
->mf
= mask
->mf
; /* Copy maps. */
1935 FLOW_FOR_EACH_IN_MAPS(value
, flow
, mask
->mf
.map
) {
1936 *dst_u64
= value
& *mask_u64
++;
1937 hash
= hash_add64(hash
, *dst_u64
++);
1939 dst
->hash
= hash_finish(hash
,
1940 (dst_u64
- miniflow_get_values(&dst
->mf
)) * 8);
1943 /* Iterate through netdev_flow_key TNL u64 values specified by 'FLOWMAP'. */
1944 #define NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(VALUE, KEY, FLOWMAP) \
1945 MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, &(KEY)->mf, FLOWMAP)
1947 /* Returns a hash value for the bits of 'key' where there are 1-bits in
1949 static inline uint32_t
1950 netdev_flow_key_hash_in_mask(const struct netdev_flow_key
*key
,
1951 const struct netdev_flow_key
*mask
)
1953 const uint64_t *p
= miniflow_get_values(&mask
->mf
);
1957 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, key
, mask
->mf
.map
) {
1958 hash
= hash_add64(hash
, value
& *p
++);
1961 return hash_finish(hash
, (p
- miniflow_get_values(&mask
->mf
)) * 8);
1965 emc_entry_alive(struct emc_entry
*ce
)
1967 return ce
->flow
&& !ce
->flow
->dead
;
1971 emc_clear_entry(struct emc_entry
*ce
)
1974 dp_netdev_flow_unref(ce
->flow
);
1980 emc_change_entry(struct emc_entry
*ce
, struct dp_netdev_flow
*flow
,
1981 const struct netdev_flow_key
*key
)
1983 if (ce
->flow
!= flow
) {
1985 dp_netdev_flow_unref(ce
->flow
);
1988 if (dp_netdev_flow_ref(flow
)) {
1995 netdev_flow_key_clone(&ce
->key
, key
);
2000 emc_insert(struct emc_cache
*cache
, const struct netdev_flow_key
*key
,
2001 struct dp_netdev_flow
*flow
)
2003 struct emc_entry
*to_be_replaced
= NULL
;
2004 struct emc_entry
*current_entry
;
2006 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
2007 if (netdev_flow_key_equal(¤t_entry
->key
, key
)) {
2008 /* We found the entry with the 'mf' miniflow */
2009 emc_change_entry(current_entry
, flow
, NULL
);
2013 /* Replacement policy: put the flow in an empty (not alive) entry, or
2014 * in the first entry where it can be */
2016 || (emc_entry_alive(to_be_replaced
)
2017 && !emc_entry_alive(current_entry
))
2018 || current_entry
->key
.hash
< to_be_replaced
->key
.hash
) {
2019 to_be_replaced
= current_entry
;
2022 /* We didn't find the miniflow in the cache.
2023 * The 'to_be_replaced' entry is where the new flow will be stored */
2025 emc_change_entry(to_be_replaced
, flow
, key
);
2029 emc_probabilistic_insert(struct dp_netdev_pmd_thread
*pmd
,
2030 const struct netdev_flow_key
*key
,
2031 struct dp_netdev_flow
*flow
)
2033 /* Insert an entry into the EMC based on probability value 'min'. By
2034 * default the value is UINT32_MAX / 100 which yields an insertion
2035 * probability of 1/100 ie. 1% */
2038 atomic_read_relaxed(&pmd
->dp
->emc_insert_min
, &min
);
2041 if (min
&& (key
->hash
^ (uint32_t) pmd
->last_cycles
) <= min
) {
2043 if (min
&& (key
->hash
^ random_uint32()) <= min
) {
2045 emc_insert(&pmd
->flow_cache
, key
, flow
);
2049 static inline struct dp_netdev_flow
*
2050 emc_lookup(struct emc_cache
*cache
, const struct netdev_flow_key
*key
)
2052 struct emc_entry
*current_entry
;
2054 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
2055 if (current_entry
->key
.hash
== key
->hash
2056 && emc_entry_alive(current_entry
)
2057 && netdev_flow_key_equal_mf(¤t_entry
->key
, &key
->mf
)) {
2059 /* We found the entry with the 'key->mf' miniflow */
2060 return current_entry
->flow
;
2067 static struct dp_netdev_flow
*
2068 dp_netdev_pmd_lookup_flow(struct dp_netdev_pmd_thread
*pmd
,
2069 const struct netdev_flow_key
*key
,
2073 struct dpcls_rule
*rule
;
2074 odp_port_t in_port
= u32_to_odp(MINIFLOW_GET_U32(&key
->mf
, in_port
));
2075 struct dp_netdev_flow
*netdev_flow
= NULL
;
2077 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
2078 if (OVS_LIKELY(cls
)) {
2079 dpcls_lookup(cls
, key
, &rule
, 1, lookup_num_p
);
2080 netdev_flow
= dp_netdev_flow_cast(rule
);
2085 static struct dp_netdev_flow
*
2086 dp_netdev_pmd_find_flow(const struct dp_netdev_pmd_thread
*pmd
,
2087 const ovs_u128
*ufidp
, const struct nlattr
*key
,
2090 struct dp_netdev_flow
*netdev_flow
;
2094 /* If a UFID is not provided, determine one based on the key. */
2095 if (!ufidp
&& key
&& key_len
2096 && !dpif_netdev_flow_from_nlattrs(key
, key_len
, &flow
, false)) {
2097 dpif_flow_hash(pmd
->dp
->dpif
, &flow
, sizeof flow
, &ufid
);
2102 CMAP_FOR_EACH_WITH_HASH (netdev_flow
, node
, dp_netdev_flow_hash(ufidp
),
2104 if (ovs_u128_equals(netdev_flow
->ufid
, *ufidp
)) {
2114 get_dpif_flow_stats(const struct dp_netdev_flow
*netdev_flow_
,
2115 struct dpif_flow_stats
*stats
)
2117 struct dp_netdev_flow
*netdev_flow
;
2118 unsigned long long n
;
2122 netdev_flow
= CONST_CAST(struct dp_netdev_flow
*, netdev_flow_
);
2124 atomic_read_relaxed(&netdev_flow
->stats
.packet_count
, &n
);
2125 stats
->n_packets
= n
;
2126 atomic_read_relaxed(&netdev_flow
->stats
.byte_count
, &n
);
2128 atomic_read_relaxed(&netdev_flow
->stats
.used
, &used
);
2130 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
2131 stats
->tcp_flags
= flags
;
2134 /* Converts to the dpif_flow format, using 'key_buf' and 'mask_buf' for
2135 * storing the netlink-formatted key/mask. 'key_buf' may be the same as
2136 * 'mask_buf'. Actions will be returned without copying, by relying on RCU to
2139 dp_netdev_flow_to_dpif_flow(const struct dp_netdev_flow
*netdev_flow
,
2140 struct ofpbuf
*key_buf
, struct ofpbuf
*mask_buf
,
2141 struct dpif_flow
*flow
, bool terse
)
2144 memset(flow
, 0, sizeof *flow
);
2146 struct flow_wildcards wc
;
2147 struct dp_netdev_actions
*actions
;
2149 struct odp_flow_key_parms odp_parms
= {
2150 .flow
= &netdev_flow
->flow
,
2152 .support
= dp_netdev_support
,
2155 miniflow_expand(&netdev_flow
->cr
.mask
->mf
, &wc
.masks
);
2156 /* in_port is exact matched, but we have left it out from the mask for
2157 * optimnization reasons. Add in_port back to the mask. */
2158 wc
.masks
.in_port
.odp_port
= ODPP_NONE
;
2161 offset
= key_buf
->size
;
2162 flow
->key
= ofpbuf_tail(key_buf
);
2163 odp_flow_key_from_flow(&odp_parms
, key_buf
);
2164 flow
->key_len
= key_buf
->size
- offset
;
2167 offset
= mask_buf
->size
;
2168 flow
->mask
= ofpbuf_tail(mask_buf
);
2169 odp_parms
.key_buf
= key_buf
;
2170 odp_flow_key_from_mask(&odp_parms
, mask_buf
);
2171 flow
->mask_len
= mask_buf
->size
- offset
;
2174 actions
= dp_netdev_flow_get_actions(netdev_flow
);
2175 flow
->actions
= actions
->actions
;
2176 flow
->actions_len
= actions
->size
;
2179 flow
->ufid
= netdev_flow
->ufid
;
2180 flow
->ufid_present
= true;
2181 flow
->pmd_id
= netdev_flow
->pmd_id
;
2182 get_dpif_flow_stats(netdev_flow
, &flow
->stats
);
2186 dpif_netdev_mask_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
2187 const struct nlattr
*mask_key
,
2188 uint32_t mask_key_len
, const struct flow
*flow
,
2189 struct flow_wildcards
*wc
, bool probe
)
2191 enum odp_key_fitness fitness
;
2193 fitness
= odp_flow_key_to_mask(mask_key
, mask_key_len
, wc
, flow
);
2196 /* This should not happen: it indicates that
2197 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
2198 * disagree on the acceptable form of a mask. Log the problem
2199 * as an error, with enough details to enable debugging. */
2200 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2202 if (!VLOG_DROP_ERR(&rl
)) {
2206 odp_flow_format(key
, key_len
, mask_key
, mask_key_len
, NULL
, &s
,
2208 VLOG_ERR("internal error parsing flow mask %s (%s)",
2209 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
2221 dpif_netdev_flow_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
2222 struct flow
*flow
, bool probe
)
2226 if (odp_flow_key_to_flow(key
, key_len
, flow
)) {
2228 /* This should not happen: it indicates that
2229 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
2230 * the acceptable form of a flow. Log the problem as an error,
2231 * with enough details to enable debugging. */
2232 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2234 if (!VLOG_DROP_ERR(&rl
)) {
2238 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
2239 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
2247 in_port
= flow
->in_port
.odp_port
;
2248 if (!is_valid_port_number(in_port
) && in_port
!= ODPP_NONE
) {
2252 if (flow
->ct_state
& DP_NETDEV_CS_UNSUPPORTED_MASK
) {
2260 dpif_netdev_flow_get(const struct dpif
*dpif
, const struct dpif_flow_get
*get
)
2262 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2263 struct dp_netdev_flow
*netdev_flow
;
2264 struct dp_netdev_pmd_thread
*pmd
;
2265 struct hmapx to_find
= HMAPX_INITIALIZER(&to_find
);
2266 struct hmapx_node
*node
;
2269 if (get
->pmd_id
== PMD_ID_NULL
) {
2270 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2271 if (dp_netdev_pmd_try_ref(pmd
) && !hmapx_add(&to_find
, pmd
)) {
2272 dp_netdev_pmd_unref(pmd
);
2276 pmd
= dp_netdev_get_pmd(dp
, get
->pmd_id
);
2280 hmapx_add(&to_find
, pmd
);
2283 if (!hmapx_count(&to_find
)) {
2287 HMAPX_FOR_EACH (node
, &to_find
) {
2288 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
2289 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, get
->ufid
, get
->key
,
2292 dp_netdev_flow_to_dpif_flow(netdev_flow
, get
->buffer
, get
->buffer
,
2301 HMAPX_FOR_EACH (node
, &to_find
) {
2302 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
2303 dp_netdev_pmd_unref(pmd
);
2306 hmapx_destroy(&to_find
);
2310 static struct dp_netdev_flow
*
2311 dp_netdev_flow_add(struct dp_netdev_pmd_thread
*pmd
,
2312 struct match
*match
, const ovs_u128
*ufid
,
2313 const struct nlattr
*actions
, size_t actions_len
)
2314 OVS_REQUIRES(pmd
->flow_mutex
)
2316 struct dp_netdev_flow
*flow
;
2317 struct netdev_flow_key mask
;
2320 /* Make sure in_port is exact matched before we read it. */
2321 ovs_assert(match
->wc
.masks
.in_port
.odp_port
== ODPP_NONE
);
2322 odp_port_t in_port
= match
->flow
.in_port
.odp_port
;
2324 /* As we select the dpcls based on the port number, each netdev flow
2325 * belonging to the same dpcls will have the same odp_port value.
2326 * For performance reasons we wildcard odp_port here in the mask. In the
2327 * typical case dp_hash is also wildcarded, and the resulting 8-byte
2328 * chunk {dp_hash, in_port} will be ignored by netdev_flow_mask_init() and
2329 * will not be part of the subtable mask.
2330 * This will speed up the hash computation during dpcls_lookup() because
2331 * there is one less call to hash_add64() in this case. */
2332 match
->wc
.masks
.in_port
.odp_port
= 0;
2333 netdev_flow_mask_init(&mask
, match
);
2334 match
->wc
.masks
.in_port
.odp_port
= ODPP_NONE
;
2336 /* Make sure wc does not have metadata. */
2337 ovs_assert(!FLOWMAP_HAS_FIELD(&mask
.mf
.map
, metadata
)
2338 && !FLOWMAP_HAS_FIELD(&mask
.mf
.map
, regs
));
2340 /* Do not allocate extra space. */
2341 flow
= xmalloc(sizeof *flow
- sizeof flow
->cr
.flow
.mf
+ mask
.len
);
2342 memset(&flow
->stats
, 0, sizeof flow
->stats
);
2345 *CONST_CAST(unsigned *, &flow
->pmd_id
) = pmd
->core_id
;
2346 *CONST_CAST(struct flow
*, &flow
->flow
) = match
->flow
;
2347 *CONST_CAST(ovs_u128
*, &flow
->ufid
) = *ufid
;
2348 ovs_refcount_init(&flow
->ref_cnt
);
2349 ovsrcu_set(&flow
->actions
, dp_netdev_actions_create(actions
, actions_len
));
2351 netdev_flow_key_init_masked(&flow
->cr
.flow
, &match
->flow
, &mask
);
2353 /* Select dpcls for in_port. Relies on in_port to be exact match. */
2354 cls
= dp_netdev_pmd_find_dpcls(pmd
, in_port
);
2355 dpcls_insert(cls
, &flow
->cr
, &mask
);
2357 cmap_insert(&pmd
->flow_table
, CONST_CAST(struct cmap_node
*, &flow
->node
),
2358 dp_netdev_flow_hash(&flow
->ufid
));
2360 if (OVS_UNLIKELY(VLOG_IS_DBG_ENABLED())) {
2361 struct ds ds
= DS_EMPTY_INITIALIZER
;
2362 struct ofpbuf key_buf
, mask_buf
;
2363 struct odp_flow_key_parms odp_parms
= {
2364 .flow
= &match
->flow
,
2365 .mask
= &match
->wc
.masks
,
2366 .support
= dp_netdev_support
,
2369 ofpbuf_init(&key_buf
, 0);
2370 ofpbuf_init(&mask_buf
, 0);
2372 odp_flow_key_from_flow(&odp_parms
, &key_buf
);
2373 odp_parms
.key_buf
= &key_buf
;
2374 odp_flow_key_from_mask(&odp_parms
, &mask_buf
);
2376 ds_put_cstr(&ds
, "flow_add: ");
2377 odp_format_ufid(ufid
, &ds
);
2378 ds_put_cstr(&ds
, " ");
2379 odp_flow_format(key_buf
.data
, key_buf
.size
,
2380 mask_buf
.data
, mask_buf
.size
,
2382 ds_put_cstr(&ds
, ", actions:");
2383 format_odp_actions(&ds
, actions
, actions_len
);
2385 VLOG_DBG_RL(&upcall_rl
, "%s", ds_cstr(&ds
));
2387 ofpbuf_uninit(&key_buf
);
2388 ofpbuf_uninit(&mask_buf
);
2396 flow_put_on_pmd(struct dp_netdev_pmd_thread
*pmd
,
2397 struct netdev_flow_key
*key
,
2398 struct match
*match
,
2400 const struct dpif_flow_put
*put
,
2401 struct dpif_flow_stats
*stats
)
2403 struct dp_netdev_flow
*netdev_flow
;
2407 memset(stats
, 0, sizeof *stats
);
2410 ovs_mutex_lock(&pmd
->flow_mutex
);
2411 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
, NULL
);
2413 if (put
->flags
& DPIF_FP_CREATE
) {
2414 if (cmap_count(&pmd
->flow_table
) < MAX_FLOWS
) {
2415 dp_netdev_flow_add(pmd
, match
, ufid
, put
->actions
,
2425 if (put
->flags
& DPIF_FP_MODIFY
2426 && flow_equal(&match
->flow
, &netdev_flow
->flow
)) {
2427 struct dp_netdev_actions
*new_actions
;
2428 struct dp_netdev_actions
*old_actions
;
2430 new_actions
= dp_netdev_actions_create(put
->actions
,
2433 old_actions
= dp_netdev_flow_get_actions(netdev_flow
);
2434 ovsrcu_set(&netdev_flow
->actions
, new_actions
);
2437 get_dpif_flow_stats(netdev_flow
, stats
);
2439 if (put
->flags
& DPIF_FP_ZERO_STATS
) {
2440 /* XXX: The userspace datapath uses thread local statistics
2441 * (for flows), which should be updated only by the owning
2442 * thread. Since we cannot write on stats memory here,
2443 * we choose not to support this flag. Please note:
2444 * - This feature is currently used only by dpctl commands with
2446 * - Should the need arise, this operation can be implemented
2447 * by keeping a base value (to be update here) for each
2448 * counter, and subtracting it before outputting the stats */
2452 ovsrcu_postpone(dp_netdev_actions_free
, old_actions
);
2453 } else if (put
->flags
& DPIF_FP_CREATE
) {
2456 /* Overlapping flow. */
2460 ovs_mutex_unlock(&pmd
->flow_mutex
);
2465 dpif_netdev_flow_put(struct dpif
*dpif
, const struct dpif_flow_put
*put
)
2467 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2468 struct netdev_flow_key key
;
2469 struct dp_netdev_pmd_thread
*pmd
;
2473 bool probe
= put
->flags
& DPIF_FP_PROBE
;
2476 memset(put
->stats
, 0, sizeof *put
->stats
);
2478 error
= dpif_netdev_flow_from_nlattrs(put
->key
, put
->key_len
, &match
.flow
,
2483 error
= dpif_netdev_mask_from_nlattrs(put
->key
, put
->key_len
,
2484 put
->mask
, put
->mask_len
,
2485 &match
.flow
, &match
.wc
, probe
);
2493 dpif_flow_hash(dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
2496 /* Must produce a netdev_flow_key for lookup.
2497 * This interface is no longer performance critical, since it is not used
2498 * for upcall processing any more. */
2499 netdev_flow_key_from_flow(&key
, &match
.flow
);
2501 if (put
->pmd_id
== PMD_ID_NULL
) {
2502 if (cmap_count(&dp
->poll_threads
) == 0) {
2505 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2506 struct dpif_flow_stats pmd_stats
;
2509 pmd_error
= flow_put_on_pmd(pmd
, &key
, &match
, &ufid
, put
,
2513 } else if (put
->stats
) {
2514 put
->stats
->n_packets
+= pmd_stats
.n_packets
;
2515 put
->stats
->n_bytes
+= pmd_stats
.n_bytes
;
2516 put
->stats
->used
= MAX(put
->stats
->used
, pmd_stats
.used
);
2517 put
->stats
->tcp_flags
|= pmd_stats
.tcp_flags
;
2521 pmd
= dp_netdev_get_pmd(dp
, put
->pmd_id
);
2525 error
= flow_put_on_pmd(pmd
, &key
, &match
, &ufid
, put
, put
->stats
);
2526 dp_netdev_pmd_unref(pmd
);
2533 flow_del_on_pmd(struct dp_netdev_pmd_thread
*pmd
,
2534 struct dpif_flow_stats
*stats
,
2535 const struct dpif_flow_del
*del
)
2537 struct dp_netdev_flow
*netdev_flow
;
2540 ovs_mutex_lock(&pmd
->flow_mutex
);
2541 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, del
->ufid
, del
->key
,
2545 get_dpif_flow_stats(netdev_flow
, stats
);
2547 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
2551 ovs_mutex_unlock(&pmd
->flow_mutex
);
2557 dpif_netdev_flow_del(struct dpif
*dpif
, const struct dpif_flow_del
*del
)
2559 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2560 struct dp_netdev_pmd_thread
*pmd
;
2564 memset(del
->stats
, 0, sizeof *del
->stats
);
2567 if (del
->pmd_id
== PMD_ID_NULL
) {
2568 if (cmap_count(&dp
->poll_threads
) == 0) {
2571 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2572 struct dpif_flow_stats pmd_stats
;
2575 pmd_error
= flow_del_on_pmd(pmd
, &pmd_stats
, del
);
2578 } else if (del
->stats
) {
2579 del
->stats
->n_packets
+= pmd_stats
.n_packets
;
2580 del
->stats
->n_bytes
+= pmd_stats
.n_bytes
;
2581 del
->stats
->used
= MAX(del
->stats
->used
, pmd_stats
.used
);
2582 del
->stats
->tcp_flags
|= pmd_stats
.tcp_flags
;
2586 pmd
= dp_netdev_get_pmd(dp
, del
->pmd_id
);
2590 error
= flow_del_on_pmd(pmd
, del
->stats
, del
);
2591 dp_netdev_pmd_unref(pmd
);
2598 struct dpif_netdev_flow_dump
{
2599 struct dpif_flow_dump up
;
2600 struct cmap_position poll_thread_pos
;
2601 struct cmap_position flow_pos
;
2602 struct dp_netdev_pmd_thread
*cur_pmd
;
2604 struct ovs_mutex mutex
;
2607 static struct dpif_netdev_flow_dump
*
2608 dpif_netdev_flow_dump_cast(struct dpif_flow_dump
*dump
)
2610 return CONTAINER_OF(dump
, struct dpif_netdev_flow_dump
, up
);
2613 static struct dpif_flow_dump
*
2614 dpif_netdev_flow_dump_create(const struct dpif
*dpif_
, bool terse
)
2616 struct dpif_netdev_flow_dump
*dump
;
2618 dump
= xzalloc(sizeof *dump
);
2619 dpif_flow_dump_init(&dump
->up
, dpif_
);
2620 dump
->up
.terse
= terse
;
2621 ovs_mutex_init(&dump
->mutex
);
2627 dpif_netdev_flow_dump_destroy(struct dpif_flow_dump
*dump_
)
2629 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
2631 ovs_mutex_destroy(&dump
->mutex
);
2636 struct dpif_netdev_flow_dump_thread
{
2637 struct dpif_flow_dump_thread up
;
2638 struct dpif_netdev_flow_dump
*dump
;
2639 struct odputil_keybuf keybuf
[FLOW_DUMP_MAX_BATCH
];
2640 struct odputil_keybuf maskbuf
[FLOW_DUMP_MAX_BATCH
];
2643 static struct dpif_netdev_flow_dump_thread
*
2644 dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread
*thread
)
2646 return CONTAINER_OF(thread
, struct dpif_netdev_flow_dump_thread
, up
);
2649 static struct dpif_flow_dump_thread
*
2650 dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump
*dump_
)
2652 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
2653 struct dpif_netdev_flow_dump_thread
*thread
;
2655 thread
= xmalloc(sizeof *thread
);
2656 dpif_flow_dump_thread_init(&thread
->up
, &dump
->up
);
2657 thread
->dump
= dump
;
2662 dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread
*thread_
)
2664 struct dpif_netdev_flow_dump_thread
*thread
2665 = dpif_netdev_flow_dump_thread_cast(thread_
);
2671 dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread
*thread_
,
2672 struct dpif_flow
*flows
, int max_flows
)
2674 struct dpif_netdev_flow_dump_thread
*thread
2675 = dpif_netdev_flow_dump_thread_cast(thread_
);
2676 struct dpif_netdev_flow_dump
*dump
= thread
->dump
;
2677 struct dp_netdev_flow
*netdev_flows
[FLOW_DUMP_MAX_BATCH
];
2681 ovs_mutex_lock(&dump
->mutex
);
2682 if (!dump
->status
) {
2683 struct dpif_netdev
*dpif
= dpif_netdev_cast(thread
->up
.dpif
);
2684 struct dp_netdev
*dp
= get_dp_netdev(&dpif
->dpif
);
2685 struct dp_netdev_pmd_thread
*pmd
= dump
->cur_pmd
;
2686 int flow_limit
= MIN(max_flows
, FLOW_DUMP_MAX_BATCH
);
2688 /* First call to dump_next(), extracts the first pmd thread.
2689 * If there is no pmd thread, returns immediately. */
2691 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
2693 ovs_mutex_unlock(&dump
->mutex
);
2700 for (n_flows
= 0; n_flows
< flow_limit
; n_flows
++) {
2701 struct cmap_node
*node
;
2703 node
= cmap_next_position(&pmd
->flow_table
, &dump
->flow_pos
);
2707 netdev_flows
[n_flows
] = CONTAINER_OF(node
,
2708 struct dp_netdev_flow
,
2711 /* When finishing dumping the current pmd thread, moves to
2713 if (n_flows
< flow_limit
) {
2714 memset(&dump
->flow_pos
, 0, sizeof dump
->flow_pos
);
2715 dp_netdev_pmd_unref(pmd
);
2716 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
2722 /* Keeps the reference to next caller. */
2723 dump
->cur_pmd
= pmd
;
2725 /* If the current dump is empty, do not exit the loop, since the
2726 * remaining pmds could have flows to be dumped. Just dumps again
2727 * on the new 'pmd'. */
2730 ovs_mutex_unlock(&dump
->mutex
);
2732 for (i
= 0; i
< n_flows
; i
++) {
2733 struct odputil_keybuf
*maskbuf
= &thread
->maskbuf
[i
];
2734 struct odputil_keybuf
*keybuf
= &thread
->keybuf
[i
];
2735 struct dp_netdev_flow
*netdev_flow
= netdev_flows
[i
];
2736 struct dpif_flow
*f
= &flows
[i
];
2737 struct ofpbuf key
, mask
;
2739 ofpbuf_use_stack(&key
, keybuf
, sizeof *keybuf
);
2740 ofpbuf_use_stack(&mask
, maskbuf
, sizeof *maskbuf
);
2741 dp_netdev_flow_to_dpif_flow(netdev_flow
, &key
, &mask
, f
,
2749 dpif_netdev_execute(struct dpif
*dpif
, struct dpif_execute
*execute
)
2750 OVS_NO_THREAD_SAFETY_ANALYSIS
2752 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2753 struct dp_netdev_pmd_thread
*pmd
;
2754 struct dp_packet_batch pp
;
2756 if (dp_packet_size(execute
->packet
) < ETH_HEADER_LEN
||
2757 dp_packet_size(execute
->packet
) > UINT16_MAX
) {
2761 /* Tries finding the 'pmd'. If NULL is returned, that means
2762 * the current thread is a non-pmd thread and should use
2763 * dp_netdev_get_pmd(dp, NON_PMD_CORE_ID). */
2764 pmd
= ovsthread_getspecific(dp
->per_pmd_key
);
2766 pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
2772 if (execute
->probe
) {
2773 /* If this is part of a probe, Drop the packet, since executing
2774 * the action may actually cause spurious packets be sent into
2779 /* If the current thread is non-pmd thread, acquires
2780 * the 'non_pmd_mutex'. */
2781 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
2782 ovs_mutex_lock(&dp
->non_pmd_mutex
);
2785 /* The action processing expects the RSS hash to be valid, because
2786 * it's always initialized at the beginning of datapath processing.
2787 * In this case, though, 'execute->packet' may not have gone through
2788 * the datapath at all, it may have been generated by the upper layer
2789 * (OpenFlow packet-out, BFD frame, ...). */
2790 if (!dp_packet_rss_valid(execute
->packet
)) {
2791 dp_packet_set_rss_hash(execute
->packet
,
2792 flow_hash_5tuple(execute
->flow
, 0));
2795 dp_packet_batch_init_packet(&pp
, execute
->packet
);
2796 dp_netdev_execute_actions(pmd
, &pp
, false, execute
->flow
,
2797 execute
->actions
, execute
->actions_len
,
2800 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
2801 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
2802 dp_netdev_pmd_unref(pmd
);
2809 dpif_netdev_operate(struct dpif
*dpif
, struct dpif_op
**ops
, size_t n_ops
)
2813 for (i
= 0; i
< n_ops
; i
++) {
2814 struct dpif_op
*op
= ops
[i
];
2817 case DPIF_OP_FLOW_PUT
:
2818 op
->error
= dpif_netdev_flow_put(dpif
, &op
->u
.flow_put
);
2821 case DPIF_OP_FLOW_DEL
:
2822 op
->error
= dpif_netdev_flow_del(dpif
, &op
->u
.flow_del
);
2825 case DPIF_OP_EXECUTE
:
2826 op
->error
= dpif_netdev_execute(dpif
, &op
->u
.execute
);
2829 case DPIF_OP_FLOW_GET
:
2830 op
->error
= dpif_netdev_flow_get(dpif
, &op
->u
.flow_get
);
2836 /* Applies datapath configuration from the database. Some of the changes are
2837 * actually applied in dpif_netdev_run(). */
2839 dpif_netdev_set_config(struct dpif
*dpif
, const struct smap
*other_config
)
2841 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2842 const char *cmask
= smap_get(other_config
, "pmd-cpu-mask");
2843 unsigned long long insert_prob
=
2844 smap_get_ullong(other_config
, "emc-insert-inv-prob",
2845 DEFAULT_EM_FLOW_INSERT_INV_PROB
);
2846 uint32_t insert_min
, cur_min
;
2848 if (!nullable_string_is_equal(dp
->pmd_cmask
, cmask
)) {
2849 free(dp
->pmd_cmask
);
2850 dp
->pmd_cmask
= nullable_xstrdup(cmask
);
2851 dp_netdev_request_reconfigure(dp
);
2854 atomic_read_relaxed(&dp
->emc_insert_min
, &cur_min
);
2855 if (insert_prob
<= UINT32_MAX
) {
2856 insert_min
= insert_prob
== 0 ? 0 : UINT32_MAX
/ insert_prob
;
2858 insert_min
= DEFAULT_EM_FLOW_INSERT_MIN
;
2859 insert_prob
= DEFAULT_EM_FLOW_INSERT_INV_PROB
;
2862 if (insert_min
!= cur_min
) {
2863 atomic_store_relaxed(&dp
->emc_insert_min
, insert_min
);
2864 if (insert_min
== 0) {
2865 VLOG_INFO("EMC has been disabled");
2867 VLOG_INFO("EMC insertion probability changed to 1/%llu (~%.2f%%)",
2868 insert_prob
, (100 / (float)insert_prob
));
2875 /* Parses affinity list and returns result in 'core_ids'. */
2877 parse_affinity_list(const char *affinity_list
, unsigned *core_ids
, int n_rxq
)
2880 char *list
, *copy
, *key
, *value
;
2883 for (i
= 0; i
< n_rxq
; i
++) {
2884 core_ids
[i
] = OVS_CORE_UNSPEC
;
2887 if (!affinity_list
) {
2891 list
= copy
= xstrdup(affinity_list
);
2893 while (ofputil_parse_key_value(&list
, &key
, &value
)) {
2894 int rxq_id
, core_id
;
2896 if (!str_to_int(key
, 0, &rxq_id
) || rxq_id
< 0
2897 || !str_to_int(value
, 0, &core_id
) || core_id
< 0) {
2902 if (rxq_id
< n_rxq
) {
2903 core_ids
[rxq_id
] = core_id
;
2911 /* Parses 'affinity_list' and applies configuration if it is valid. */
2913 dpif_netdev_port_set_rxq_affinity(struct dp_netdev_port
*port
,
2914 const char *affinity_list
)
2916 unsigned *core_ids
, i
;
2919 core_ids
= xmalloc(port
->n_rxq
* sizeof *core_ids
);
2920 if (parse_affinity_list(affinity_list
, core_ids
, port
->n_rxq
)) {
2925 for (i
= 0; i
< port
->n_rxq
; i
++) {
2926 port
->rxqs
[i
].core_id
= core_ids
[i
];
2934 /* Changes the affinity of port's rx queues. The changes are actually applied
2935 * in dpif_netdev_run(). */
2937 dpif_netdev_port_set_config(struct dpif
*dpif
, odp_port_t port_no
,
2938 const struct smap
*cfg
)
2940 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2941 struct dp_netdev_port
*port
;
2943 const char *affinity_list
= smap_get(cfg
, "pmd-rxq-affinity");
2945 ovs_mutex_lock(&dp
->port_mutex
);
2946 error
= get_port_by_number(dp
, port_no
, &port
);
2947 if (error
|| !netdev_is_pmd(port
->netdev
)
2948 || nullable_string_is_equal(affinity_list
, port
->rxq_affinity_list
)) {
2952 error
= dpif_netdev_port_set_rxq_affinity(port
, affinity_list
);
2956 free(port
->rxq_affinity_list
);
2957 port
->rxq_affinity_list
= nullable_xstrdup(affinity_list
);
2959 dp_netdev_request_reconfigure(dp
);
2961 ovs_mutex_unlock(&dp
->port_mutex
);
2966 dpif_netdev_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
2967 uint32_t queue_id
, uint32_t *priority
)
2969 *priority
= queue_id
;
2974 /* Creates and returns a new 'struct dp_netdev_actions', whose actions are
2975 * a copy of the 'ofpacts_len' bytes of 'ofpacts'. */
2976 struct dp_netdev_actions
*
2977 dp_netdev_actions_create(const struct nlattr
*actions
, size_t size
)
2979 struct dp_netdev_actions
*netdev_actions
;
2981 netdev_actions
= xmalloc(sizeof *netdev_actions
+ size
);
2982 memcpy(netdev_actions
->actions
, actions
, size
);
2983 netdev_actions
->size
= size
;
2985 return netdev_actions
;
2988 struct dp_netdev_actions
*
2989 dp_netdev_flow_get_actions(const struct dp_netdev_flow
*flow
)
2991 return ovsrcu_get(struct dp_netdev_actions
*, &flow
->actions
);
2995 dp_netdev_actions_free(struct dp_netdev_actions
*actions
)
3000 static inline unsigned long long
3001 cycles_counter(void)
3004 return rte_get_tsc_cycles();
3010 /* Fake mutex to make sure that the calls to cycles_count_* are balanced */
3011 extern struct ovs_mutex cycles_counter_fake_mutex
;
3013 /* Start counting cycles. Must be followed by 'cycles_count_end()' */
3015 cycles_count_start(struct dp_netdev_pmd_thread
*pmd
)
3016 OVS_ACQUIRES(&cycles_counter_fake_mutex
)
3017 OVS_NO_THREAD_SAFETY_ANALYSIS
3019 pmd
->last_cycles
= cycles_counter();
3022 /* Stop counting cycles and add them to the counter 'type' */
3024 cycles_count_end(struct dp_netdev_pmd_thread
*pmd
,
3025 enum pmd_cycles_counter_type type
)
3026 OVS_RELEASES(&cycles_counter_fake_mutex
)
3027 OVS_NO_THREAD_SAFETY_ANALYSIS
3029 unsigned long long interval
= cycles_counter() - pmd
->last_cycles
;
3031 non_atomic_ullong_add(&pmd
->cycles
.n
[type
], interval
);
3035 dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread
*pmd
,
3036 struct netdev_rxq
*rx
,
3039 struct dp_packet_batch batch
;
3042 dp_packet_batch_init(&batch
);
3043 cycles_count_start(pmd
);
3044 error
= netdev_rxq_recv(rx
, &batch
);
3045 cycles_count_end(pmd
, PMD_CYCLES_POLLING
);
3047 *recirc_depth_get() = 0;
3049 cycles_count_start(pmd
);
3050 dp_netdev_input(pmd
, &batch
, port_no
);
3051 cycles_count_end(pmd
, PMD_CYCLES_PROCESSING
);
3052 } else if (error
!= EAGAIN
&& error
!= EOPNOTSUPP
) {
3053 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3055 VLOG_ERR_RL(&rl
, "error receiving data from %s: %s",
3056 netdev_rxq_get_name(rx
), ovs_strerror(error
));
3060 static struct tx_port
*
3061 tx_port_lookup(const struct hmap
*hmap
, odp_port_t port_no
)
3065 HMAP_FOR_EACH_IN_BUCKET (tx
, node
, hash_port_no(port_no
), hmap
) {
3066 if (tx
->port
->port_no
== port_no
) {
3075 port_reconfigure(struct dp_netdev_port
*port
)
3077 struct netdev
*netdev
= port
->netdev
;
3080 port
->need_reconfigure
= false;
3082 /* Closes the existing 'rxq's. */
3083 for (i
= 0; i
< port
->n_rxq
; i
++) {
3084 netdev_rxq_close(port
->rxqs
[i
].rx
);
3085 port
->rxqs
[i
].rx
= NULL
;
3089 /* Allows 'netdev' to apply the pending configuration changes. */
3090 if (netdev_is_reconf_required(netdev
)) {
3091 err
= netdev_reconfigure(netdev
);
3092 if (err
&& (err
!= EOPNOTSUPP
)) {
3093 VLOG_ERR("Failed to set interface %s new configuration",
3094 netdev_get_name(netdev
));
3098 /* If the netdev_reconfigure() above succeeds, reopens the 'rxq's. */
3099 port
->rxqs
= xrealloc(port
->rxqs
,
3100 sizeof *port
->rxqs
* netdev_n_rxq(netdev
));
3101 /* Realloc 'used' counters for tx queues. */
3102 free(port
->txq_used
);
3103 port
->txq_used
= xcalloc(netdev_n_txq(netdev
), sizeof *port
->txq_used
);
3105 for (i
= 0; i
< netdev_n_rxq(netdev
); i
++) {
3106 port
->rxqs
[i
].port
= port
;
3107 err
= netdev_rxq_open(netdev
, &port
->rxqs
[i
].rx
, i
);
3114 /* Parse affinity list to apply configuration for new queues. */
3115 dpif_netdev_port_set_rxq_affinity(port
, port
->rxq_affinity_list
);
3120 struct rr_numa_list
{
3121 struct hmap numas
; /* Contains 'struct rr_numa' */
3125 struct hmap_node node
;
3129 /* Non isolated pmds on numa node 'numa_id' */
3130 struct dp_netdev_pmd_thread
**pmds
;
3136 static struct rr_numa
*
3137 rr_numa_list_lookup(struct rr_numa_list
*rr
, int numa_id
)
3139 struct rr_numa
*numa
;
3141 HMAP_FOR_EACH_WITH_HASH (numa
, node
, hash_int(numa_id
, 0), &rr
->numas
) {
3142 if (numa
->numa_id
== numa_id
) {
3151 rr_numa_list_populate(struct dp_netdev
*dp
, struct rr_numa_list
*rr
)
3153 struct dp_netdev_pmd_thread
*pmd
;
3154 struct rr_numa
*numa
;
3156 hmap_init(&rr
->numas
);
3158 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3159 if (pmd
->core_id
== NON_PMD_CORE_ID
|| pmd
->isolated
) {
3163 numa
= rr_numa_list_lookup(rr
, pmd
->numa_id
);
3165 numa
= xzalloc(sizeof *numa
);
3166 numa
->numa_id
= pmd
->numa_id
;
3167 hmap_insert(&rr
->numas
, &numa
->node
, hash_int(pmd
->numa_id
, 0));
3170 numa
->pmds
= xrealloc(numa
->pmds
, numa
->n_pmds
* sizeof *numa
->pmds
);
3171 numa
->pmds
[numa
->n_pmds
- 1] = pmd
;
3175 static struct dp_netdev_pmd_thread
*
3176 rr_numa_get_pmd(struct rr_numa
*numa
)
3178 return numa
->pmds
[numa
->cur_index
++ % numa
->n_pmds
];
3182 rr_numa_list_destroy(struct rr_numa_list
*rr
)
3184 struct rr_numa
*numa
;
3186 HMAP_FOR_EACH_POP (numa
, node
, &rr
->numas
) {
3190 hmap_destroy(&rr
->numas
);
3193 /* Assign pmds to queues. If 'pinned' is true, assign pmds to pinned
3194 * queues and marks the pmds as isolated. Otherwise, assign non isolated
3195 * pmds to unpinned queues.
3197 * The function doesn't touch the pmd threads, it just stores the assignment
3198 * in the 'pmd' member of each rxq. */
3200 rxq_scheduling(struct dp_netdev
*dp
, bool pinned
) OVS_REQUIRES(dp
->port_mutex
)
3202 struct dp_netdev_port
*port
;
3203 struct rr_numa_list rr
;
3205 rr_numa_list_populate(dp
, &rr
);
3207 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3208 struct rr_numa
*numa
;
3211 if (!netdev_is_pmd(port
->netdev
)) {
3215 numa_id
= netdev_get_numa_id(port
->netdev
);
3216 numa
= rr_numa_list_lookup(&rr
, numa_id
);
3218 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
3219 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
3221 if (pinned
&& q
->core_id
!= OVS_CORE_UNSPEC
) {
3222 struct dp_netdev_pmd_thread
*pmd
;
3224 pmd
= dp_netdev_get_pmd(dp
, q
->core_id
);
3226 VLOG_WARN("There is no PMD thread on core %d. Queue "
3227 "%d on port \'%s\' will not be polled.",
3228 q
->core_id
, qid
, netdev_get_name(port
->netdev
));
3231 pmd
->isolated
= true;
3232 dp_netdev_pmd_unref(pmd
);
3234 } else if (!pinned
&& q
->core_id
== OVS_CORE_UNSPEC
) {
3236 VLOG_WARN("There's no available (non isolated) pmd thread "
3237 "on numa node %d. Queue %d on port \'%s\' will "
3239 numa_id
, qid
, netdev_get_name(port
->netdev
));
3241 q
->pmd
= rr_numa_get_pmd(numa
);
3247 rr_numa_list_destroy(&rr
);
3251 reconfigure_pmd_threads(struct dp_netdev
*dp
)
3252 OVS_REQUIRES(dp
->port_mutex
)
3254 struct dp_netdev_pmd_thread
*pmd
;
3255 struct ovs_numa_dump
*pmd_cores
;
3256 bool changed
= false;
3258 /* The pmd threads should be started only if there's a pmd port in the
3259 * datapath. If the user didn't provide any "pmd-cpu-mask", we start
3260 * NR_PMD_THREADS per numa node. */
3261 if (!has_pmd_port(dp
)) {
3262 pmd_cores
= ovs_numa_dump_n_cores_per_numa(0);
3263 } else if (dp
->pmd_cmask
&& dp
->pmd_cmask
[0]) {
3264 pmd_cores
= ovs_numa_dump_cores_with_cmask(dp
->pmd_cmask
);
3266 pmd_cores
= ovs_numa_dump_n_cores_per_numa(NR_PMD_THREADS
);
3269 /* Check for changed configuration */
3270 if (ovs_numa_dump_count(pmd_cores
) != cmap_count(&dp
->poll_threads
) - 1) {
3273 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3274 if (pmd
->core_id
!= NON_PMD_CORE_ID
3275 && !ovs_numa_dump_contains_core(pmd_cores
,
3284 /* Destroy the old and recreate the new pmd threads. We don't perform an
3285 * incremental update because we would have to adjust 'static_tx_qid'. */
3287 struct ovs_numa_info_core
*core
;
3288 struct ovs_numa_info_numa
*numa
;
3290 /* Do not destroy the non pmd thread. */
3291 dp_netdev_destroy_all_pmds(dp
, false);
3292 FOR_EACH_CORE_ON_DUMP (core
, pmd_cores
) {
3293 struct dp_netdev_pmd_thread
*pmd
= xzalloc(sizeof *pmd
);
3295 dp_netdev_configure_pmd(pmd
, dp
, core
->core_id
, core
->numa_id
);
3297 pmd
->thread
= ovs_thread_create("pmd", pmd_thread_main
, pmd
);
3300 /* Log the number of pmd threads per numa node. */
3301 FOR_EACH_NUMA_ON_DUMP (numa
, pmd_cores
) {
3302 VLOG_INFO("Created %"PRIuSIZE
" pmd threads on numa node %d",
3303 numa
->n_cores
, numa
->numa_id
);
3307 ovs_numa_dump_destroy(pmd_cores
);
3311 reload_affected_pmds(struct dp_netdev
*dp
)
3313 struct dp_netdev_pmd_thread
*pmd
;
3315 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3316 if (pmd
->need_reload
) {
3317 dp_netdev_reload_pmd__(pmd
);
3318 pmd
->need_reload
= false;
3324 pmd_remove_stale_ports(struct dp_netdev
*dp
,
3325 struct dp_netdev_pmd_thread
*pmd
)
3326 OVS_EXCLUDED(pmd
->port_mutex
)
3327 OVS_REQUIRES(dp
->port_mutex
)
3329 struct rxq_poll
*poll
, *poll_next
;
3330 struct tx_port
*tx
, *tx_next
;
3332 ovs_mutex_lock(&pmd
->port_mutex
);
3333 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
3334 struct dp_netdev_port
*port
= poll
->rxq
->port
;
3336 if (port
->need_reconfigure
3337 || !hmap_contains(&dp
->ports
, &port
->node
)) {
3338 dp_netdev_del_rxq_from_pmd(pmd
, poll
);
3341 HMAP_FOR_EACH_SAFE (tx
, tx_next
, node
, &pmd
->tx_ports
) {
3342 struct dp_netdev_port
*port
= tx
->port
;
3344 if (port
->need_reconfigure
3345 || !hmap_contains(&dp
->ports
, &port
->node
)) {
3346 dp_netdev_del_port_tx_from_pmd(pmd
, tx
);
3349 ovs_mutex_unlock(&pmd
->port_mutex
);
3352 /* Must be called each time a port is added/removed or the cmask changes.
3353 * This creates and destroys pmd threads, reconfigures ports, opens their
3354 * rxqs and assigns all rxqs/txqs to pmd threads. */
3356 reconfigure_datapath(struct dp_netdev
*dp
)
3357 OVS_REQUIRES(dp
->port_mutex
)
3359 struct dp_netdev_pmd_thread
*pmd
;
3360 struct dp_netdev_port
*port
;
3363 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
3365 /* Step 1: Adjust the pmd threads based on the datapath ports, the cores
3366 * on the system and the user configuration. */
3367 reconfigure_pmd_threads(dp
);
3369 wanted_txqs
= cmap_count(&dp
->poll_threads
);
3371 /* The number of pmd threads might have changed, or a port can be new:
3372 * adjust the txqs. */
3373 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3374 netdev_set_tx_multiq(port
->netdev
, wanted_txqs
);
3377 /* Step 2: Remove from the pmd threads ports that have been removed or
3378 * need reconfiguration. */
3380 /* Check for all the ports that need reconfiguration. We cache this in
3381 * 'port->reconfigure', because netdev_is_reconf_required() can change at
3383 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3384 if (netdev_is_reconf_required(port
->netdev
)) {
3385 port
->need_reconfigure
= true;
3389 /* Remove from the pmd threads all the ports that have been deleted or
3390 * need reconfiguration. */
3391 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3392 pmd_remove_stale_ports(dp
, pmd
);
3395 /* Reload affected pmd threads. We must wait for the pmd threads before
3396 * reconfiguring the ports, because a port cannot be reconfigured while
3397 * it's being used. */
3398 reload_affected_pmds(dp
);
3400 /* Step 3: Reconfigure ports. */
3402 /* We only reconfigure the ports that we determined above, because they're
3403 * not being used by any pmd thread at the moment. If a port fails to
3404 * reconfigure we remove it from the datapath. */
3405 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3408 if (!port
->need_reconfigure
) {
3412 err
= port_reconfigure(port
);
3414 hmap_remove(&dp
->ports
, &port
->node
);
3415 seq_change(dp
->port_seq
);
3418 port
->dynamic_txqs
= netdev_n_txq(port
->netdev
) < wanted_txqs
;
3422 /* Step 4: Compute new rxq scheduling. We don't touch the pmd threads
3423 * for now, we just update the 'pmd' pointer in each rxq to point to the
3424 * wanted thread according to the scheduling policy. */
3426 /* Reset all the pmd threads to non isolated. */
3427 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3428 pmd
->isolated
= false;
3431 /* Reset all the queues to unassigned */
3432 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3433 for (int i
= 0; i
< port
->n_rxq
; i
++) {
3434 port
->rxqs
[i
].pmd
= NULL
;
3438 /* Add pinned queues and mark pmd threads isolated. */
3439 rxq_scheduling(dp
, true);
3441 /* Add non-pinned queues. */
3442 rxq_scheduling(dp
, false);
3444 /* Step 5: Remove queues not compliant with new scheduling. */
3445 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3446 struct rxq_poll
*poll
, *poll_next
;
3448 ovs_mutex_lock(&pmd
->port_mutex
);
3449 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
3450 if (poll
->rxq
->pmd
!= pmd
) {
3451 dp_netdev_del_rxq_from_pmd(pmd
, poll
);
3454 ovs_mutex_unlock(&pmd
->port_mutex
);
3457 /* Reload affected pmd threads. We must wait for the pmd threads to remove
3458 * the old queues before readding them, otherwise a queue can be polled by
3459 * two threads at the same time. */
3460 reload_affected_pmds(dp
);
3462 /* Step 6: Add queues from scheduling, if they're not there already. */
3463 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3464 if (!netdev_is_pmd(port
->netdev
)) {
3468 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
3469 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
3472 ovs_mutex_lock(&q
->pmd
->port_mutex
);
3473 dp_netdev_add_rxq_to_pmd(q
->pmd
, q
);
3474 ovs_mutex_unlock(&q
->pmd
->port_mutex
);
3479 /* Add every port to the tx cache of every pmd thread, if it's not
3480 * there already and if this pmd has at least one rxq to poll. */
3481 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3482 ovs_mutex_lock(&pmd
->port_mutex
);
3483 if (hmap_count(&pmd
->poll_list
) || pmd
->core_id
== NON_PMD_CORE_ID
) {
3484 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3485 dp_netdev_add_port_tx_to_pmd(pmd
, port
);
3488 ovs_mutex_unlock(&pmd
->port_mutex
);
3491 /* Reload affected pmd threads. */
3492 reload_affected_pmds(dp
);
3495 /* Returns true if one of the netdevs in 'dp' requires a reconfiguration */
3497 ports_require_restart(const struct dp_netdev
*dp
)
3498 OVS_REQUIRES(dp
->port_mutex
)
3500 struct dp_netdev_port
*port
;
3502 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3503 if (netdev_is_reconf_required(port
->netdev
)) {
3511 /* Return true if needs to revalidate datapath flows. */
3513 dpif_netdev_run(struct dpif
*dpif
)
3515 struct dp_netdev_port
*port
;
3516 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3517 struct dp_netdev_pmd_thread
*non_pmd
;
3518 uint64_t new_tnl_seq
;
3520 ovs_mutex_lock(&dp
->port_mutex
);
3521 non_pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
3523 ovs_mutex_lock(&dp
->non_pmd_mutex
);
3524 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3525 if (!netdev_is_pmd(port
->netdev
)) {
3528 for (i
= 0; i
< port
->n_rxq
; i
++) {
3529 dp_netdev_process_rxq_port(non_pmd
, port
->rxqs
[i
].rx
,
3534 dpif_netdev_xps_revalidate_pmd(non_pmd
, time_msec(), false);
3535 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
3537 dp_netdev_pmd_unref(non_pmd
);
3540 if (dp_netdev_is_reconf_required(dp
) || ports_require_restart(dp
)) {
3541 reconfigure_datapath(dp
);
3543 ovs_mutex_unlock(&dp
->port_mutex
);
3545 tnl_neigh_cache_run();
3547 new_tnl_seq
= seq_read(tnl_conf_seq
);
3549 if (dp
->last_tnl_conf_seq
!= new_tnl_seq
) {
3550 dp
->last_tnl_conf_seq
= new_tnl_seq
;
3557 dpif_netdev_wait(struct dpif
*dpif
)
3559 struct dp_netdev_port
*port
;
3560 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3562 ovs_mutex_lock(&dp_netdev_mutex
);
3563 ovs_mutex_lock(&dp
->port_mutex
);
3564 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3565 netdev_wait_reconf_required(port
->netdev
);
3566 if (!netdev_is_pmd(port
->netdev
)) {
3569 for (i
= 0; i
< port
->n_rxq
; i
++) {
3570 netdev_rxq_wait(port
->rxqs
[i
].rx
);
3574 ovs_mutex_unlock(&dp
->port_mutex
);
3575 ovs_mutex_unlock(&dp_netdev_mutex
);
3576 seq_wait(tnl_conf_seq
, dp
->last_tnl_conf_seq
);
3580 pmd_free_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
3582 struct tx_port
*tx_port_cached
;
3584 /* Free all used tx queue ids. */
3585 dpif_netdev_xps_revalidate_pmd(pmd
, 0, true);
3587 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->tnl_port_cache
) {
3588 free(tx_port_cached
);
3590 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->send_port_cache
) {
3591 free(tx_port_cached
);
3595 /* Copies ports from 'pmd->tx_ports' (shared with the main thread) to
3596 * 'pmd->port_cache' (thread local) */
3598 pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
3599 OVS_REQUIRES(pmd
->port_mutex
)
3601 struct tx_port
*tx_port
, *tx_port_cached
;
3603 pmd_free_cached_ports(pmd
);
3604 hmap_shrink(&pmd
->send_port_cache
);
3605 hmap_shrink(&pmd
->tnl_port_cache
);
3607 HMAP_FOR_EACH (tx_port
, node
, &pmd
->tx_ports
) {
3608 if (netdev_has_tunnel_push_pop(tx_port
->port
->netdev
)) {
3609 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
3610 hmap_insert(&pmd
->tnl_port_cache
, &tx_port_cached
->node
,
3611 hash_port_no(tx_port_cached
->port
->port_no
));
3614 if (netdev_n_txq(tx_port
->port
->netdev
)) {
3615 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
3616 hmap_insert(&pmd
->send_port_cache
, &tx_port_cached
->node
,
3617 hash_port_no(tx_port_cached
->port
->port_no
));
3623 pmd_load_queues_and_ports(struct dp_netdev_pmd_thread
*pmd
,
3624 struct polled_queue
**ppoll_list
)
3626 struct polled_queue
*poll_list
= *ppoll_list
;
3627 struct rxq_poll
*poll
;
3630 ovs_mutex_lock(&pmd
->port_mutex
);
3631 poll_list
= xrealloc(poll_list
, hmap_count(&pmd
->poll_list
)
3632 * sizeof *poll_list
);
3635 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
3636 poll_list
[i
].rx
= poll
->rxq
->rx
;
3637 poll_list
[i
].port_no
= poll
->rxq
->port
->port_no
;
3641 pmd_load_cached_ports(pmd
);
3643 ovs_mutex_unlock(&pmd
->port_mutex
);
3645 *ppoll_list
= poll_list
;
3650 pmd_thread_main(void *f_
)
3652 struct dp_netdev_pmd_thread
*pmd
= f_
;
3653 unsigned int lc
= 0;
3654 struct polled_queue
*poll_list
;
3661 /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */
3662 ovsthread_setspecific(pmd
->dp
->per_pmd_key
, pmd
);
3663 ovs_numa_thread_setaffinity_core(pmd
->core_id
);
3664 dpdk_set_lcore_id(pmd
->core_id
);
3665 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
3667 emc_cache_init(&pmd
->flow_cache
);
3669 /* List port/core affinity */
3670 for (i
= 0; i
< poll_cnt
; i
++) {
3671 VLOG_DBG("Core %d processing port \'%s\' with queue-id %d\n",
3672 pmd
->core_id
, netdev_rxq_get_name(poll_list
[i
].rx
),
3673 netdev_rxq_get_queue_id(poll_list
[i
].rx
));
3677 while (seq_read(pmd
->reload_seq
) == pmd
->last_reload_seq
) {
3678 seq_wait(pmd
->reload_seq
, pmd
->last_reload_seq
);
3685 for (i
= 0; i
< poll_cnt
; i
++) {
3686 dp_netdev_process_rxq_port(pmd
, poll_list
[i
].rx
,
3687 poll_list
[i
].port_no
);
3695 coverage_try_clear();
3696 dp_netdev_pmd_try_optimize(pmd
);
3697 if (!ovsrcu_try_quiesce()) {
3698 emc_cache_slow_sweep(&pmd
->flow_cache
);
3701 atomic_read_relaxed(&pmd
->reload
, &reload
);
3708 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
3709 exiting
= latch_is_set(&pmd
->exit_latch
);
3710 /* Signal here to make sure the pmd finishes
3711 * reloading the updated configuration. */
3712 dp_netdev_pmd_reload_done(pmd
);
3714 emc_cache_uninit(&pmd
->flow_cache
);
3721 pmd_free_cached_ports(pmd
);
3726 dp_netdev_disable_upcall(struct dp_netdev
*dp
)
3727 OVS_ACQUIRES(dp
->upcall_rwlock
)
3729 fat_rwlock_wrlock(&dp
->upcall_rwlock
);
3735 dpif_netdev_meter_get_features(const struct dpif
* dpif OVS_UNUSED
,
3736 struct ofputil_meter_features
*features
)
3738 features
->max_meters
= MAX_METERS
;
3739 features
->band_types
= DP_SUPPORTED_METER_BAND_TYPES
;
3740 features
->capabilities
= DP_SUPPORTED_METER_FLAGS_MASK
;
3741 features
->max_bands
= MAX_BANDS
;
3742 features
->max_color
= 0;
3745 /* Returns false when packet needs to be dropped. */
3747 dp_netdev_run_meter(struct dp_netdev
*dp
, struct dp_packet_batch
*packets_
,
3748 uint32_t meter_id
, long long int now
)
3750 struct dp_meter
*meter
;
3751 struct dp_meter_band
*band
;
3752 long long int long_delta_t
; /* msec */
3753 uint32_t delta_t
; /* msec */
3755 int cnt
= packets_
->count
;
3756 uint32_t bytes
, volume
;
3757 int exceeded_band
[NETDEV_MAX_BURST
];
3758 uint32_t exceeded_rate
[NETDEV_MAX_BURST
];
3759 int exceeded_pkt
= cnt
; /* First packet that exceeded a band rate. */
3761 if (meter_id
>= MAX_METERS
) {
3765 meter_lock(dp
, meter_id
);
3766 meter
= dp
->meters
[meter_id
];
3771 /* Initialize as negative values. */
3772 memset(exceeded_band
, 0xff, cnt
* sizeof *exceeded_band
);
3773 /* Initialize as zeroes. */
3774 memset(exceeded_rate
, 0, cnt
* sizeof *exceeded_rate
);
3776 /* All packets will hit the meter at the same time. */
3777 long_delta_t
= (now
- meter
->used
); /* msec */
3779 /* Make sure delta_t will not be too large, so that bucket will not
3780 * wrap around below. */
3781 delta_t
= (long_delta_t
> (long long int)meter
->max_delta_t
)
3782 ? meter
->max_delta_t
: (uint32_t)long_delta_t
;
3784 /* Update meter stats. */
3786 meter
->packet_count
+= cnt
;
3788 for (i
= 0; i
< cnt
; i
++) {
3789 bytes
+= dp_packet_size(packets_
->packets
[i
]);
3791 meter
->byte_count
+= bytes
;
3793 /* Meters can operate in terms of packets per second or kilobits per
3795 if (meter
->flags
& OFPMF13_PKTPS
) {
3796 /* Rate in packets/second, bucket 1/1000 packets. */
3797 /* msec * packets/sec = 1/1000 packets. */
3798 volume
= cnt
* 1000; /* Take 'cnt' packets from the bucket. */
3800 /* Rate in kbps, bucket in bits. */
3801 /* msec * kbps = bits */
3805 /* Update all bands and find the one hit with the highest rate for each
3806 * packet (if any). */
3807 for (int m
= 0; m
< meter
->n_bands
; ++m
) {
3808 band
= &meter
->bands
[m
];
3810 /* Update band's bucket. */
3811 band
->bucket
+= delta_t
* band
->up
.rate
;
3812 if (band
->bucket
> band
->up
.burst_size
) {
3813 band
->bucket
= band
->up
.burst_size
;
3816 /* Drain the bucket for all the packets, if possible. */
3817 if (band
->bucket
>= volume
) {
3818 band
->bucket
-= volume
;
3820 int band_exceeded_pkt
;
3822 /* Band limit hit, must process packet-by-packet. */
3823 if (meter
->flags
& OFPMF13_PKTPS
) {
3824 band_exceeded_pkt
= band
->bucket
/ 1000;
3825 band
->bucket
%= 1000; /* Remainder stays in bucket. */
3827 /* Update the exceeding band for each exceeding packet.
3828 * (Only one band will be fired by a packet, and that
3829 * can be different for each packet.) */
3830 for (i
= band_exceeded_pkt
; i
< cnt
; i
++) {
3831 if (band
->up
.rate
> exceeded_rate
[i
]) {
3832 exceeded_rate
[i
] = band
->up
.rate
;
3833 exceeded_band
[i
] = m
;
3837 /* Packet sizes differ, must process one-by-one. */
3838 band_exceeded_pkt
= cnt
;
3839 for (i
= 0; i
< cnt
; i
++) {
3840 uint32_t bits
= dp_packet_size(packets_
->packets
[i
]) * 8;
3842 if (band
->bucket
>= bits
) {
3843 band
->bucket
-= bits
;
3845 if (i
< band_exceeded_pkt
) {
3846 band_exceeded_pkt
= i
;
3848 /* Update the exceeding band for the exceeding packet.
3849 * (Only one band will be fired by a packet, and that
3850 * can be different for each packet.) */
3851 if (band
->up
.rate
> exceeded_rate
[i
]) {
3852 exceeded_rate
[i
] = band
->up
.rate
;
3853 exceeded_band
[i
] = m
;
3858 /* Remember the first exceeding packet. */
3859 if (exceeded_pkt
> band_exceeded_pkt
) {
3860 exceeded_pkt
= band_exceeded_pkt
;
3865 /* Fire the highest rate band exceeded by each packet.
3866 * Drop packets if needed, by swapping packet to the end that will be
3868 const size_t size
= dp_packet_batch_size(packets_
);
3869 struct dp_packet
*packet
;
3871 DP_PACKET_BATCH_REFILL_FOR_EACH (j
, size
, packet
, packets_
) {
3872 if (exceeded_band
[j
] >= 0) {
3873 /* Meter drop packet. */
3874 band
= &meter
->bands
[exceeded_band
[j
]];
3875 band
->packet_count
+= 1;
3876 band
->byte_count
+= dp_packet_size(packet
);
3878 dp_packet_delete(packet
);
3880 /* Meter accepts packet. */
3881 dp_packet_batch_refill(packets_
, packet
, j
);
3885 meter_unlock(dp
, meter_id
);
3888 /* Meter set/get/del processing is still single-threaded. */
3890 dpif_netdev_meter_set(struct dpif
*dpif
, ofproto_meter_id
*meter_id
,
3891 struct ofputil_meter_config
*config
)
3893 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3894 uint32_t mid
= meter_id
->uint32
;
3895 struct dp_meter
*meter
;
3898 if (mid
>= MAX_METERS
) {
3899 return EFBIG
; /* Meter_id out of range. */
3902 if (config
->flags
& ~DP_SUPPORTED_METER_FLAGS_MASK
||
3903 !(config
->flags
& (OFPMF13_KBPS
| OFPMF13_PKTPS
))) {
3904 return EBADF
; /* Unsupported flags set */
3906 /* Validate bands */
3907 if (config
->n_bands
== 0 || config
->n_bands
> MAX_BANDS
) {
3908 return EINVAL
; /* Too many bands */
3910 for (i
= 0; i
< config
->n_bands
; ++i
) {
3911 switch (config
->bands
[i
].type
) {
3915 return ENODEV
; /* Unsupported band type */
3919 /* Allocate meter */
3920 meter
= xzalloc(sizeof *meter
3921 + config
->n_bands
* sizeof(struct dp_meter_band
));
3923 meter
->flags
= config
->flags
;
3924 meter
->n_bands
= config
->n_bands
;
3925 meter
->max_delta_t
= 0;
3926 meter
->used
= time_msec();
3929 for (i
= 0; i
< config
->n_bands
; ++i
) {
3930 uint32_t band_max_delta_t
;
3932 /* Set burst size to a workable value if none specified. */
3933 if (config
->bands
[i
].burst_size
== 0) {
3934 config
->bands
[i
].burst_size
= config
->bands
[i
].rate
;
3937 meter
->bands
[i
].up
= config
->bands
[i
];
3938 /* Convert burst size to the bucket units: */
3939 /* pkts => 1/1000 packets, kilobits => bits. */
3940 meter
->bands
[i
].up
.burst_size
*= 1000;
3941 /* Initialize bucket to empty. */
3942 meter
->bands
[i
].bucket
= 0;
3944 /* Figure out max delta_t that is enough to fill any bucket. */
3946 = meter
->bands
[i
].up
.burst_size
/ meter
->bands
[i
].up
.rate
;
3947 if (band_max_delta_t
> meter
->max_delta_t
) {
3948 meter
->max_delta_t
= band_max_delta_t
;
3952 meter_lock(dp
, mid
);
3953 dp_delete_meter(dp
, mid
); /* Free existing meter, if any */
3954 dp
->meters
[mid
] = meter
;
3955 meter_unlock(dp
, mid
);
3963 dpif_netdev_meter_get(const struct dpif
*dpif
,
3964 ofproto_meter_id meter_id_
,
3965 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
3967 const struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3968 const struct dp_meter
*meter
;
3969 uint32_t meter_id
= meter_id_
.uint32
;
3971 if (meter_id
>= MAX_METERS
) {
3974 meter
= dp
->meters
[meter_id
];
3981 meter_lock(dp
, meter_id
);
3982 stats
->packet_in_count
= meter
->packet_count
;
3983 stats
->byte_in_count
= meter
->byte_count
;
3985 for (i
= 0; i
< n_bands
&& i
< meter
->n_bands
; ++i
) {
3986 stats
->bands
[i
].packet_count
= meter
->bands
[i
].packet_count
;
3987 stats
->bands
[i
].byte_count
= meter
->bands
[i
].byte_count
;
3989 meter_unlock(dp
, meter_id
);
3997 dpif_netdev_meter_del(struct dpif
*dpif
,
3998 ofproto_meter_id meter_id_
,
3999 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
4001 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4004 error
= dpif_netdev_meter_get(dpif
, meter_id_
, stats
, n_bands
);
4006 uint32_t meter_id
= meter_id_
.uint32
;
4008 meter_lock(dp
, meter_id
);
4009 dp_delete_meter(dp
, meter_id
);
4010 meter_unlock(dp
, meter_id
);
4017 dpif_netdev_disable_upcall(struct dpif
*dpif
)
4018 OVS_NO_THREAD_SAFETY_ANALYSIS
4020 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4021 dp_netdev_disable_upcall(dp
);
4025 dp_netdev_enable_upcall(struct dp_netdev
*dp
)
4026 OVS_RELEASES(dp
->upcall_rwlock
)
4028 fat_rwlock_unlock(&dp
->upcall_rwlock
);
4032 dpif_netdev_enable_upcall(struct dpif
*dpif
)
4033 OVS_NO_THREAD_SAFETY_ANALYSIS
4035 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4036 dp_netdev_enable_upcall(dp
);
4040 dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
)
4042 ovs_mutex_lock(&pmd
->cond_mutex
);
4043 atomic_store_relaxed(&pmd
->reload
, false);
4044 pmd
->last_reload_seq
= seq_read(pmd
->reload_seq
);
4045 xpthread_cond_signal(&pmd
->cond
);
4046 ovs_mutex_unlock(&pmd
->cond_mutex
);
4049 /* Finds and refs the dp_netdev_pmd_thread on core 'core_id'. Returns
4050 * the pointer if succeeds, otherwise, NULL (it can return NULL even if
4051 * 'core_id' is NON_PMD_CORE_ID).
4053 * Caller must unrefs the returned reference. */
4054 static struct dp_netdev_pmd_thread
*
4055 dp_netdev_get_pmd(struct dp_netdev
*dp
, unsigned core_id
)
4057 struct dp_netdev_pmd_thread
*pmd
;
4058 const struct cmap_node
*pnode
;
4060 pnode
= cmap_find(&dp
->poll_threads
, hash_int(core_id
, 0));
4064 pmd
= CONTAINER_OF(pnode
, struct dp_netdev_pmd_thread
, node
);
4066 return dp_netdev_pmd_try_ref(pmd
) ? pmd
: NULL
;
4069 /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */
4071 dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
4072 OVS_REQUIRES(dp
->port_mutex
)
4074 struct dp_netdev_pmd_thread
*non_pmd
;
4076 non_pmd
= xzalloc(sizeof *non_pmd
);
4077 dp_netdev_configure_pmd(non_pmd
, dp
, NON_PMD_CORE_ID
, OVS_NUMA_UNSPEC
);
4080 /* Caller must have valid pointer to 'pmd'. */
4082 dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
)
4084 return ovs_refcount_try_ref_rcu(&pmd
->ref_cnt
);
4088 dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
)
4090 if (pmd
&& ovs_refcount_unref(&pmd
->ref_cnt
) == 1) {
4091 ovsrcu_postpone(dp_netdev_destroy_pmd
, pmd
);
4095 /* Given cmap position 'pos', tries to ref the next node. If try_ref()
4096 * fails, keeps checking for next node until reaching the end of cmap.
4098 * Caller must unrefs the returned reference. */
4099 static struct dp_netdev_pmd_thread
*
4100 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
)
4102 struct dp_netdev_pmd_thread
*next
;
4105 struct cmap_node
*node
;
4107 node
= cmap_next_position(&dp
->poll_threads
, pos
);
4108 next
= node
? CONTAINER_OF(node
, struct dp_netdev_pmd_thread
, node
)
4110 } while (next
&& !dp_netdev_pmd_try_ref(next
));
4115 /* Configures the 'pmd' based on the input argument. */
4117 dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
, struct dp_netdev
*dp
,
4118 unsigned core_id
, int numa_id
)
4121 pmd
->core_id
= core_id
;
4122 pmd
->numa_id
= numa_id
;
4123 pmd
->need_reload
= false;
4125 *CONST_CAST(int *, &pmd
->static_tx_qid
) = cmap_count(&dp
->poll_threads
);
4127 ovs_refcount_init(&pmd
->ref_cnt
);
4128 latch_init(&pmd
->exit_latch
);
4129 pmd
->reload_seq
= seq_create();
4130 pmd
->last_reload_seq
= seq_read(pmd
->reload_seq
);
4131 atomic_init(&pmd
->reload
, false);
4132 xpthread_cond_init(&pmd
->cond
, NULL
);
4133 ovs_mutex_init(&pmd
->cond_mutex
);
4134 ovs_mutex_init(&pmd
->flow_mutex
);
4135 ovs_mutex_init(&pmd
->port_mutex
);
4136 cmap_init(&pmd
->flow_table
);
4137 cmap_init(&pmd
->classifiers
);
4138 pmd
->next_optimization
= time_msec() + DPCLS_OPTIMIZATION_INTERVAL
;
4139 hmap_init(&pmd
->poll_list
);
4140 hmap_init(&pmd
->tx_ports
);
4141 hmap_init(&pmd
->tnl_port_cache
);
4142 hmap_init(&pmd
->send_port_cache
);
4143 /* init the 'flow_cache' since there is no
4144 * actual thread created for NON_PMD_CORE_ID. */
4145 if (core_id
== NON_PMD_CORE_ID
) {
4146 emc_cache_init(&pmd
->flow_cache
);
4148 cmap_insert(&dp
->poll_threads
, CONST_CAST(struct cmap_node
*, &pmd
->node
),
4149 hash_int(core_id
, 0));
4153 dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
)
4157 dp_netdev_pmd_flow_flush(pmd
);
4158 hmap_destroy(&pmd
->send_port_cache
);
4159 hmap_destroy(&pmd
->tnl_port_cache
);
4160 hmap_destroy(&pmd
->tx_ports
);
4161 hmap_destroy(&pmd
->poll_list
);
4162 /* All flows (including their dpcls_rules) have been deleted already */
4163 CMAP_FOR_EACH (cls
, node
, &pmd
->classifiers
) {
4165 ovsrcu_postpone(free
, cls
);
4167 cmap_destroy(&pmd
->classifiers
);
4168 cmap_destroy(&pmd
->flow_table
);
4169 ovs_mutex_destroy(&pmd
->flow_mutex
);
4170 latch_destroy(&pmd
->exit_latch
);
4171 seq_destroy(pmd
->reload_seq
);
4172 xpthread_cond_destroy(&pmd
->cond
);
4173 ovs_mutex_destroy(&pmd
->cond_mutex
);
4174 ovs_mutex_destroy(&pmd
->port_mutex
);
4178 /* Stops the pmd thread, removes it from the 'dp->poll_threads',
4179 * and unrefs the struct. */
4181 dp_netdev_del_pmd(struct dp_netdev
*dp
, struct dp_netdev_pmd_thread
*pmd
)
4183 /* NON_PMD_CORE_ID doesn't have a thread, so we don't have to synchronize,
4184 * but extra cleanup is necessary */
4185 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
4186 ovs_mutex_lock(&dp
->non_pmd_mutex
);
4187 emc_cache_uninit(&pmd
->flow_cache
);
4188 pmd_free_cached_ports(pmd
);
4189 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
4191 latch_set(&pmd
->exit_latch
);
4192 dp_netdev_reload_pmd__(pmd
);
4193 xpthread_join(pmd
->thread
, NULL
);
4196 dp_netdev_pmd_clear_ports(pmd
);
4198 /* Purges the 'pmd''s flows after stopping the thread, but before
4199 * destroying the flows, so that the flow stats can be collected. */
4200 if (dp
->dp_purge_cb
) {
4201 dp
->dp_purge_cb(dp
->dp_purge_aux
, pmd
->core_id
);
4203 cmap_remove(&pmd
->dp
->poll_threads
, &pmd
->node
, hash_int(pmd
->core_id
, 0));
4204 dp_netdev_pmd_unref(pmd
);
4207 /* Destroys all pmd threads. If 'non_pmd' is true it also destroys the non pmd
4210 dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
, bool non_pmd
)
4212 struct dp_netdev_pmd_thread
*pmd
;
4213 struct dp_netdev_pmd_thread
**pmd_list
;
4214 size_t k
= 0, n_pmds
;
4216 n_pmds
= cmap_count(&dp
->poll_threads
);
4217 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
4219 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
4220 if (!non_pmd
&& pmd
->core_id
== NON_PMD_CORE_ID
) {
4223 /* We cannot call dp_netdev_del_pmd(), since it alters
4224 * 'dp->poll_threads' (while we're iterating it) and it
4226 ovs_assert(k
< n_pmds
);
4227 pmd_list
[k
++] = pmd
;
4230 for (size_t i
= 0; i
< k
; i
++) {
4231 dp_netdev_del_pmd(dp
, pmd_list
[i
]);
4236 /* Deletes all rx queues from pmd->poll_list and all the ports from
4239 dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
)
4241 struct rxq_poll
*poll
;
4242 struct tx_port
*port
;
4244 ovs_mutex_lock(&pmd
->port_mutex
);
4245 HMAP_FOR_EACH_POP (poll
, node
, &pmd
->poll_list
) {
4248 HMAP_FOR_EACH_POP (port
, node
, &pmd
->tx_ports
) {
4251 ovs_mutex_unlock(&pmd
->port_mutex
);
4254 /* Adds rx queue to poll_list of PMD thread, if it's not there already. */
4256 dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
4257 struct dp_netdev_rxq
*rxq
)
4258 OVS_REQUIRES(pmd
->port_mutex
)
4260 int qid
= netdev_rxq_get_queue_id(rxq
->rx
);
4261 uint32_t hash
= hash_2words(odp_to_u32(rxq
->port
->port_no
), qid
);
4262 struct rxq_poll
*poll
;
4264 HMAP_FOR_EACH_WITH_HASH (poll
, node
, hash
, &pmd
->poll_list
) {
4265 if (poll
->rxq
== rxq
) {
4266 /* 'rxq' is already polled by this thread. Do nothing. */
4271 poll
= xmalloc(sizeof *poll
);
4273 hmap_insert(&pmd
->poll_list
, &poll
->node
, hash
);
4275 pmd
->need_reload
= true;
4278 /* Delete 'poll' from poll_list of PMD thread. */
4280 dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
4281 struct rxq_poll
*poll
)
4282 OVS_REQUIRES(pmd
->port_mutex
)
4284 hmap_remove(&pmd
->poll_list
, &poll
->node
);
4287 pmd
->need_reload
= true;
4290 /* Add 'port' to the tx port cache of 'pmd', which must be reloaded for the
4291 * changes to take effect. */
4293 dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
4294 struct dp_netdev_port
*port
)
4295 OVS_REQUIRES(pmd
->port_mutex
)
4299 tx
= tx_port_lookup(&pmd
->tx_ports
, port
->port_no
);
4301 /* 'port' is already on this thread tx cache. Do nothing. */
4305 tx
= xzalloc(sizeof *tx
);
4310 hmap_insert(&pmd
->tx_ports
, &tx
->node
, hash_port_no(tx
->port
->port_no
));
4311 pmd
->need_reload
= true;
4314 /* Del 'tx' from the tx port cache of 'pmd', which must be reloaded for the
4315 * changes to take effect. */
4317 dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
4319 OVS_REQUIRES(pmd
->port_mutex
)
4321 hmap_remove(&pmd
->tx_ports
, &tx
->node
);
4323 pmd
->need_reload
= true;
4327 dpif_netdev_get_datapath_version(void)
4329 return xstrdup("<built-in>");
4333 dp_netdev_flow_used(struct dp_netdev_flow
*netdev_flow
, int cnt
, int size
,
4334 uint16_t tcp_flags
, long long now
)
4338 atomic_store_relaxed(&netdev_flow
->stats
.used
, now
);
4339 non_atomic_ullong_add(&netdev_flow
->stats
.packet_count
, cnt
);
4340 non_atomic_ullong_add(&netdev_flow
->stats
.byte_count
, size
);
4341 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
4343 atomic_store_relaxed(&netdev_flow
->stats
.tcp_flags
, flags
);
4347 dp_netdev_count_packet(struct dp_netdev_pmd_thread
*pmd
,
4348 enum dp_stat_type type
, int cnt
)
4350 non_atomic_ullong_add(&pmd
->stats
.n
[type
], cnt
);
4354 dp_netdev_upcall(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet
*packet_
,
4355 struct flow
*flow
, struct flow_wildcards
*wc
, ovs_u128
*ufid
,
4356 enum dpif_upcall_type type
, const struct nlattr
*userdata
,
4357 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
4359 struct dp_netdev
*dp
= pmd
->dp
;
4361 if (OVS_UNLIKELY(!dp
->upcall_cb
)) {
4365 if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl
))) {
4366 struct ds ds
= DS_EMPTY_INITIALIZER
;
4369 struct odp_flow_key_parms odp_parms
= {
4371 .mask
= wc
? &wc
->masks
: NULL
,
4372 .support
= dp_netdev_support
,
4375 ofpbuf_init(&key
, 0);
4376 odp_flow_key_from_flow(&odp_parms
, &key
);
4377 packet_str
= ofp_dp_packet_to_string(packet_
);
4379 odp_flow_key_format(key
.data
, key
.size
, &ds
);
4381 VLOG_DBG("%s: %s upcall:\n%s\n%s", dp
->name
,
4382 dpif_upcall_type_to_string(type
), ds_cstr(&ds
), packet_str
);
4384 ofpbuf_uninit(&key
);
4390 return dp
->upcall_cb(packet_
, flow
, ufid
, pmd
->core_id
, type
, userdata
,
4391 actions
, wc
, put_actions
, dp
->upcall_aux
);
4394 static inline uint32_t
4395 dpif_netdev_packet_get_rss_hash(struct dp_packet
*packet
,
4396 const struct miniflow
*mf
)
4398 uint32_t hash
, recirc_depth
;
4400 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
4401 hash
= dp_packet_get_rss_hash(packet
);
4403 hash
= miniflow_hash_5tuple(mf
, 0);
4404 dp_packet_set_rss_hash(packet
, hash
);
4407 /* The RSS hash must account for the recirculation depth to avoid
4408 * collisions in the exact match cache */
4409 recirc_depth
= *recirc_depth_get_unsafe();
4410 if (OVS_UNLIKELY(recirc_depth
)) {
4411 hash
= hash_finish(hash
, recirc_depth
);
4412 dp_packet_set_rss_hash(packet
, hash
);
4417 struct packet_batch_per_flow
{
4418 unsigned int byte_count
;
4420 struct dp_netdev_flow
*flow
;
4422 struct dp_packet_batch array
;
4426 packet_batch_per_flow_update(struct packet_batch_per_flow
*batch
,
4427 struct dp_packet
*packet
,
4428 const struct miniflow
*mf
)
4430 batch
->byte_count
+= dp_packet_size(packet
);
4431 batch
->tcp_flags
|= miniflow_get_tcp_flags(mf
);
4432 batch
->array
.packets
[batch
->array
.count
++] = packet
;
4436 packet_batch_per_flow_init(struct packet_batch_per_flow
*batch
,
4437 struct dp_netdev_flow
*flow
)
4439 flow
->batch
= batch
;
4442 dp_packet_batch_init(&batch
->array
);
4443 batch
->byte_count
= 0;
4444 batch
->tcp_flags
= 0;
4448 packet_batch_per_flow_execute(struct packet_batch_per_flow
*batch
,
4449 struct dp_netdev_pmd_thread
*pmd
,
4452 struct dp_netdev_actions
*actions
;
4453 struct dp_netdev_flow
*flow
= batch
->flow
;
4455 dp_netdev_flow_used(flow
, batch
->array
.count
, batch
->byte_count
,
4456 batch
->tcp_flags
, now
);
4458 actions
= dp_netdev_flow_get_actions(flow
);
4460 dp_netdev_execute_actions(pmd
, &batch
->array
, true, &flow
->flow
,
4461 actions
->actions
, actions
->size
, now
);
4465 dp_netdev_queue_batches(struct dp_packet
*pkt
,
4466 struct dp_netdev_flow
*flow
, const struct miniflow
*mf
,
4467 struct packet_batch_per_flow
*batches
, size_t *n_batches
)
4469 struct packet_batch_per_flow
*batch
= flow
->batch
;
4471 if (OVS_UNLIKELY(!batch
)) {
4472 batch
= &batches
[(*n_batches
)++];
4473 packet_batch_per_flow_init(batch
, flow
);
4476 packet_batch_per_flow_update(batch
, pkt
, mf
);
4479 /* Try to process all ('cnt') the 'packets' using only the exact match cache
4480 * 'pmd->flow_cache'. If a flow is not found for a packet 'packets[i]', the
4481 * miniflow is copied into 'keys' and the packet pointer is moved at the
4482 * beginning of the 'packets' array.
4484 * The function returns the number of packets that needs to be processed in the
4485 * 'packets' array (they have been moved to the beginning of the vector).
4487 * If 'md_is_valid' is false, the metadata in 'packets' is not valid and must be
4488 * initialized by this function using 'port_no'.
4490 static inline size_t
4491 emc_processing(struct dp_netdev_pmd_thread
*pmd
,
4492 struct dp_packet_batch
*packets_
,
4493 struct netdev_flow_key
*keys
,
4494 struct packet_batch_per_flow batches
[], size_t *n_batches
,
4495 bool md_is_valid
, odp_port_t port_no
)
4497 struct emc_cache
*flow_cache
= &pmd
->flow_cache
;
4498 struct netdev_flow_key
*key
= &keys
[0];
4499 size_t n_missed
= 0, n_dropped
= 0;
4500 struct dp_packet
*packet
;
4501 const size_t size
= dp_packet_batch_size(packets_
);
4504 DP_PACKET_BATCH_REFILL_FOR_EACH (i
, size
, packet
, packets_
) {
4505 struct dp_netdev_flow
*flow
;
4507 if (OVS_UNLIKELY(dp_packet_size(packet
) < ETH_HEADER_LEN
)) {
4508 dp_packet_delete(packet
);
4513 if (i
!= size
- 1) {
4514 struct dp_packet
**packets
= packets_
->packets
;
4515 /* Prefetch next packet data and metadata. */
4516 OVS_PREFETCH(dp_packet_data(packets
[i
+1]));
4517 pkt_metadata_prefetch_init(&packets
[i
+1]->md
);
4521 pkt_metadata_init(&packet
->md
, port_no
);
4523 miniflow_extract(packet
, &key
->mf
);
4524 key
->len
= 0; /* Not computed yet. */
4525 key
->hash
= dpif_netdev_packet_get_rss_hash(packet
, &key
->mf
);
4527 flow
= emc_lookup(flow_cache
, key
);
4528 if (OVS_LIKELY(flow
)) {
4529 dp_netdev_queue_batches(packet
, flow
, &key
->mf
, batches
,
4532 /* Exact match cache missed. Group missed packets together at
4533 * the beginning of the 'packets' array. */
4534 dp_packet_batch_refill(packets_
, packet
, i
);
4535 /* 'key[n_missed]' contains the key of the current packet and it
4536 * must be returned to the caller. The next key should be extracted
4537 * to 'keys[n_missed + 1]'. */
4538 key
= &keys
[++n_missed
];
4542 dp_netdev_count_packet(pmd
, DP_STAT_EXACT_HIT
, size
- n_dropped
- n_missed
);
4544 return dp_packet_batch_size(packets_
);
4548 handle_packet_upcall(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet
*packet
,
4549 const struct netdev_flow_key
*key
,
4550 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
,
4551 int *lost_cnt
, long long now
)
4553 struct ofpbuf
*add_actions
;
4554 struct dp_packet_batch b
;
4559 match
.tun_md
.valid
= false;
4560 miniflow_expand(&key
->mf
, &match
.flow
);
4562 ofpbuf_clear(actions
);
4563 ofpbuf_clear(put_actions
);
4565 dpif_flow_hash(pmd
->dp
->dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
4566 error
= dp_netdev_upcall(pmd
, packet
, &match
.flow
, &match
.wc
,
4567 &ufid
, DPIF_UC_MISS
, NULL
, actions
,
4569 if (OVS_UNLIKELY(error
&& error
!= ENOSPC
)) {
4570 dp_packet_delete(packet
);
4575 /* The Netlink encoding of datapath flow keys cannot express
4576 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
4577 * tag is interpreted as exact match on the fact that there is no
4578 * VLAN. Unless we refactor a lot of code that translates between
4579 * Netlink and struct flow representations, we have to do the same
4581 if (!match
.wc
.masks
.vlans
[0].tci
) {
4582 match
.wc
.masks
.vlans
[0].tci
= htons(0xffff);
4585 /* We can't allow the packet batching in the next loop to execute
4586 * the actions. Otherwise, if there are any slow path actions,
4587 * we'll send the packet up twice. */
4588 dp_packet_batch_init_packet(&b
, packet
);
4589 dp_netdev_execute_actions(pmd
, &b
, true, &match
.flow
,
4590 actions
->data
, actions
->size
, now
);
4592 add_actions
= put_actions
->size
? put_actions
: actions
;
4593 if (OVS_LIKELY(error
!= ENOSPC
)) {
4594 struct dp_netdev_flow
*netdev_flow
;
4596 /* XXX: There's a race window where a flow covering this packet
4597 * could have already been installed since we last did the flow
4598 * lookup before upcall. This could be solved by moving the
4599 * mutex lock outside the loop, but that's an awful long time
4600 * to be locking everyone out of making flow installs. If we
4601 * move to a per-core classifier, it would be reasonable. */
4602 ovs_mutex_lock(&pmd
->flow_mutex
);
4603 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
, NULL
);
4604 if (OVS_LIKELY(!netdev_flow
)) {
4605 netdev_flow
= dp_netdev_flow_add(pmd
, &match
, &ufid
,
4609 ovs_mutex_unlock(&pmd
->flow_mutex
);
4610 emc_probabilistic_insert(pmd
, key
, netdev_flow
);
4615 fast_path_processing(struct dp_netdev_pmd_thread
*pmd
,
4616 struct dp_packet_batch
*packets_
,
4617 struct netdev_flow_key
*keys
,
4618 struct packet_batch_per_flow batches
[], size_t *n_batches
,
4622 int cnt
= packets_
->count
;
4623 #if !defined(__CHECKER__) && !defined(_WIN32)
4624 const size_t PKT_ARRAY_SIZE
= cnt
;
4626 /* Sparse or MSVC doesn't like variable length array. */
4627 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
4629 struct dp_packet
**packets
= packets_
->packets
;
4631 struct dpcls_rule
*rules
[PKT_ARRAY_SIZE
];
4632 struct dp_netdev
*dp
= pmd
->dp
;
4633 int miss_cnt
= 0, lost_cnt
= 0;
4634 int lookup_cnt
= 0, add_lookup_cnt
;
4638 for (i
= 0; i
< cnt
; i
++) {
4639 /* Key length is needed in all the cases, hash computed on demand. */
4640 keys
[i
].len
= netdev_flow_key_size(miniflow_n_values(&keys
[i
].mf
));
4642 /* Get the classifier for the in_port */
4643 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
4644 if (OVS_LIKELY(cls
)) {
4645 any_miss
= !dpcls_lookup(cls
, keys
, rules
, cnt
, &lookup_cnt
);
4648 memset(rules
, 0, sizeof(rules
));
4650 if (OVS_UNLIKELY(any_miss
) && !fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
4651 uint64_t actions_stub
[512 / 8], slow_stub
[512 / 8];
4652 struct ofpbuf actions
, put_actions
;
4654 ofpbuf_use_stub(&actions
, actions_stub
, sizeof actions_stub
);
4655 ofpbuf_use_stub(&put_actions
, slow_stub
, sizeof slow_stub
);
4657 for (i
= 0; i
< cnt
; i
++) {
4658 struct dp_netdev_flow
*netdev_flow
;
4660 if (OVS_LIKELY(rules
[i
])) {
4664 /* It's possible that an earlier slow path execution installed
4665 * a rule covering this flow. In this case, it's a lot cheaper
4666 * to catch it here than execute a miss. */
4667 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, &keys
[i
],
4670 lookup_cnt
+= add_lookup_cnt
;
4671 rules
[i
] = &netdev_flow
->cr
;
4676 handle_packet_upcall(pmd
, packets
[i
], &keys
[i
], &actions
,
4677 &put_actions
, &lost_cnt
, now
);
4680 ofpbuf_uninit(&actions
);
4681 ofpbuf_uninit(&put_actions
);
4682 fat_rwlock_unlock(&dp
->upcall_rwlock
);
4683 } else if (OVS_UNLIKELY(any_miss
)) {
4684 for (i
= 0; i
< cnt
; i
++) {
4685 if (OVS_UNLIKELY(!rules
[i
])) {
4686 dp_packet_delete(packets
[i
]);
4693 for (i
= 0; i
< cnt
; i
++) {
4694 struct dp_packet
*packet
= packets
[i
];
4695 struct dp_netdev_flow
*flow
;
4697 if (OVS_UNLIKELY(!rules
[i
])) {
4701 flow
= dp_netdev_flow_cast(rules
[i
]);
4703 emc_probabilistic_insert(pmd
, &keys
[i
], flow
);
4704 dp_netdev_queue_batches(packet
, flow
, &keys
[i
].mf
, batches
, n_batches
);
4707 dp_netdev_count_packet(pmd
, DP_STAT_MASKED_HIT
, cnt
- miss_cnt
);
4708 dp_netdev_count_packet(pmd
, DP_STAT_LOOKUP_HIT
, lookup_cnt
);
4709 dp_netdev_count_packet(pmd
, DP_STAT_MISS
, miss_cnt
);
4710 dp_netdev_count_packet(pmd
, DP_STAT_LOST
, lost_cnt
);
4713 /* Packets enter the datapath from a port (or from recirculation) here.
4715 * For performance reasons a caller may choose not to initialize the metadata
4716 * in 'packets': in this case 'mdinit' is false and this function needs to
4717 * initialize it using 'port_no'. If the metadata in 'packets' is already
4718 * valid, 'md_is_valid' must be true and 'port_no' will be ignored. */
4720 dp_netdev_input__(struct dp_netdev_pmd_thread
*pmd
,
4721 struct dp_packet_batch
*packets
,
4722 bool md_is_valid
, odp_port_t port_no
)
4724 int cnt
= packets
->count
;
4725 #if !defined(__CHECKER__) && !defined(_WIN32)
4726 const size_t PKT_ARRAY_SIZE
= cnt
;
4728 /* Sparse or MSVC doesn't like variable length array. */
4729 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
4731 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
) struct netdev_flow_key keys
[PKT_ARRAY_SIZE
];
4732 struct packet_batch_per_flow batches
[PKT_ARRAY_SIZE
];
4733 long long now
= time_msec();
4738 emc_processing(pmd
, packets
, keys
, batches
, &n_batches
,
4739 md_is_valid
, port_no
);
4740 if (!dp_packet_batch_is_empty(packets
)) {
4741 /* Get ingress port from first packet's metadata. */
4742 in_port
= packets
->packets
[0]->md
.in_port
.odp_port
;
4743 fast_path_processing(pmd
, packets
, keys
, batches
, &n_batches
, in_port
, now
);
4746 /* All the flow batches need to be reset before any call to
4747 * packet_batch_per_flow_execute() as it could potentially trigger
4748 * recirculation. When a packet matching flow ‘j’ happens to be
4749 * recirculated, the nested call to dp_netdev_input__() could potentially
4750 * classify the packet as matching another flow - say 'k'. It could happen
4751 * that in the previous call to dp_netdev_input__() that same flow 'k' had
4752 * already its own batches[k] still waiting to be served. So if its
4753 * ‘batch’ member is not reset, the recirculated packet would be wrongly
4754 * appended to batches[k] of the 1st call to dp_netdev_input__(). */
4756 for (i
= 0; i
< n_batches
; i
++) {
4757 batches
[i
].flow
->batch
= NULL
;
4760 for (i
= 0; i
< n_batches
; i
++) {
4761 packet_batch_per_flow_execute(&batches
[i
], pmd
, now
);
4766 dp_netdev_input(struct dp_netdev_pmd_thread
*pmd
,
4767 struct dp_packet_batch
*packets
,
4770 dp_netdev_input__(pmd
, packets
, false, port_no
);
4774 dp_netdev_recirculate(struct dp_netdev_pmd_thread
*pmd
,
4775 struct dp_packet_batch
*packets
)
4777 dp_netdev_input__(pmd
, packets
, true, 0);
4780 struct dp_netdev_execute_aux
{
4781 struct dp_netdev_pmd_thread
*pmd
;
4783 const struct flow
*flow
;
4787 dpif_netdev_register_dp_purge_cb(struct dpif
*dpif
, dp_purge_callback
*cb
,
4790 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4791 dp
->dp_purge_aux
= aux
;
4792 dp
->dp_purge_cb
= cb
;
4796 dpif_netdev_register_upcall_cb(struct dpif
*dpif
, upcall_callback
*cb
,
4799 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4800 dp
->upcall_aux
= aux
;
4805 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
4806 long long now
, bool purge
)
4809 struct dp_netdev_port
*port
;
4812 HMAP_FOR_EACH (tx
, node
, &pmd
->send_port_cache
) {
4813 if (!tx
->port
->dynamic_txqs
) {
4816 interval
= now
- tx
->last_used
;
4817 if (tx
->qid
>= 0 && (purge
|| interval
>= XPS_TIMEOUT_MS
)) {
4819 ovs_mutex_lock(&port
->txq_used_mutex
);
4820 port
->txq_used
[tx
->qid
]--;
4821 ovs_mutex_unlock(&port
->txq_used_mutex
);
4828 dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
4829 struct tx_port
*tx
, long long now
)
4831 struct dp_netdev_port
*port
;
4833 int i
, min_cnt
, min_qid
;
4835 if (OVS_UNLIKELY(!now
)) {
4839 interval
= now
- tx
->last_used
;
4840 tx
->last_used
= now
;
4842 if (OVS_LIKELY(tx
->qid
>= 0 && interval
< XPS_TIMEOUT_MS
)) {
4848 ovs_mutex_lock(&port
->txq_used_mutex
);
4850 port
->txq_used
[tx
->qid
]--;
4856 for (i
= 0; i
< netdev_n_txq(port
->netdev
); i
++) {
4857 if (port
->txq_used
[i
] < min_cnt
|| min_cnt
== -1) {
4858 min_cnt
= port
->txq_used
[i
];
4863 port
->txq_used
[min_qid
]++;
4866 ovs_mutex_unlock(&port
->txq_used_mutex
);
4868 dpif_netdev_xps_revalidate_pmd(pmd
, now
, false);
4870 VLOG_DBG("Core %d: New TX queue ID %d for port \'%s\'.",
4871 pmd
->core_id
, tx
->qid
, netdev_get_name(tx
->port
->netdev
));
4875 static struct tx_port
*
4876 pmd_tnl_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
4879 return tx_port_lookup(&pmd
->tnl_port_cache
, port_no
);
4882 static struct tx_port
*
4883 pmd_send_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
4886 return tx_port_lookup(&pmd
->send_port_cache
, port_no
);
4890 push_tnl_action(const struct dp_netdev_pmd_thread
*pmd
,
4891 const struct nlattr
*attr
,
4892 struct dp_packet_batch
*batch
)
4894 struct tx_port
*tun_port
;
4895 const struct ovs_action_push_tnl
*data
;
4898 data
= nl_attr_get(attr
);
4900 tun_port
= pmd_tnl_port_cache_lookup(pmd
, u32_to_odp(data
->tnl_port
));
4905 err
= netdev_push_header(tun_port
->port
->netdev
, batch
, data
);
4910 dp_packet_delete_batch(batch
, true);
4915 dp_execute_userspace_action(struct dp_netdev_pmd_thread
*pmd
,
4916 struct dp_packet
*packet
, bool may_steal
,
4917 struct flow
*flow
, ovs_u128
*ufid
,
4918 struct ofpbuf
*actions
,
4919 const struct nlattr
*userdata
, long long now
)
4921 struct dp_packet_batch b
;
4924 ofpbuf_clear(actions
);
4926 error
= dp_netdev_upcall(pmd
, packet
, flow
, NULL
, ufid
,
4927 DPIF_UC_ACTION
, userdata
, actions
,
4929 if (!error
|| error
== ENOSPC
) {
4930 dp_packet_batch_init_packet(&b
, packet
);
4931 dp_netdev_execute_actions(pmd
, &b
, may_steal
, flow
,
4932 actions
->data
, actions
->size
, now
);
4933 } else if (may_steal
) {
4934 dp_packet_delete(packet
);
4939 dp_execute_cb(void *aux_
, struct dp_packet_batch
*packets_
,
4940 const struct nlattr
*a
, bool may_steal
)
4941 OVS_NO_THREAD_SAFETY_ANALYSIS
4943 struct dp_netdev_execute_aux
*aux
= aux_
;
4944 uint32_t *depth
= recirc_depth_get();
4945 struct dp_netdev_pmd_thread
*pmd
= aux
->pmd
;
4946 struct dp_netdev
*dp
= pmd
->dp
;
4947 int type
= nl_attr_type(a
);
4948 long long now
= aux
->now
;
4951 switch ((enum ovs_action_attr
)type
) {
4952 case OVS_ACTION_ATTR_OUTPUT
:
4953 p
= pmd_send_port_cache_lookup(pmd
, nl_attr_get_odp_port(a
));
4954 if (OVS_LIKELY(p
)) {
4958 dynamic_txqs
= p
->port
->dynamic_txqs
;
4960 tx_qid
= dpif_netdev_xps_get_tx_qid(pmd
, p
, now
);
4962 tx_qid
= pmd
->static_tx_qid
;
4965 netdev_send(p
->port
->netdev
, tx_qid
, packets_
, may_steal
,
4971 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
4972 if (*depth
< MAX_RECIRC_DEPTH
) {
4973 dp_packet_batch_apply_cutlen(packets_
);
4974 push_tnl_action(pmd
, a
, packets_
);
4979 case OVS_ACTION_ATTR_TUNNEL_POP
:
4980 if (*depth
< MAX_RECIRC_DEPTH
) {
4981 struct dp_packet_batch
*orig_packets_
= packets_
;
4982 odp_port_t portno
= nl_attr_get_odp_port(a
);
4984 p
= pmd_tnl_port_cache_lookup(pmd
, portno
);
4986 struct dp_packet_batch tnl_pkt
;
4989 dp_packet_batch_clone(&tnl_pkt
, packets_
);
4990 packets_
= &tnl_pkt
;
4991 dp_packet_batch_reset_cutlen(orig_packets_
);
4994 dp_packet_batch_apply_cutlen(packets_
);
4996 netdev_pop_header(p
->port
->netdev
, packets_
);
4997 if (dp_packet_batch_is_empty(packets_
)) {
5001 struct dp_packet
*packet
;
5002 DP_PACKET_BATCH_FOR_EACH (packet
, packets_
) {
5003 packet
->md
.in_port
.odp_port
= portno
;
5007 dp_netdev_recirculate(pmd
, packets_
);
5014 case OVS_ACTION_ATTR_USERSPACE
:
5015 if (!fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
5016 struct dp_packet_batch
*orig_packets_
= packets_
;
5017 const struct nlattr
*userdata
;
5018 struct dp_packet_batch usr_pkt
;
5019 struct ofpbuf actions
;
5024 userdata
= nl_attr_find_nested(a
, OVS_USERSPACE_ATTR_USERDATA
);
5025 ofpbuf_init(&actions
, 0);
5027 if (packets_
->trunc
) {
5029 dp_packet_batch_clone(&usr_pkt
, packets_
);
5030 packets_
= &usr_pkt
;
5032 dp_packet_batch_reset_cutlen(orig_packets_
);
5035 dp_packet_batch_apply_cutlen(packets_
);
5038 struct dp_packet
*packet
;
5039 DP_PACKET_BATCH_FOR_EACH (packet
, packets_
) {
5040 flow_extract(packet
, &flow
);
5041 dpif_flow_hash(dp
->dpif
, &flow
, sizeof flow
, &ufid
);
5042 dp_execute_userspace_action(pmd
, packet
, may_steal
, &flow
,
5043 &ufid
, &actions
, userdata
, now
);
5047 dp_packet_delete_batch(packets_
, true);
5050 ofpbuf_uninit(&actions
);
5051 fat_rwlock_unlock(&dp
->upcall_rwlock
);
5057 case OVS_ACTION_ATTR_RECIRC
:
5058 if (*depth
< MAX_RECIRC_DEPTH
) {
5059 struct dp_packet_batch recirc_pkts
;
5062 dp_packet_batch_clone(&recirc_pkts
, packets_
);
5063 packets_
= &recirc_pkts
;
5066 struct dp_packet
*packet
;
5067 DP_PACKET_BATCH_FOR_EACH (packet
, packets_
) {
5068 packet
->md
.recirc_id
= nl_attr_get_u32(a
);
5072 dp_netdev_recirculate(pmd
, packets_
);
5078 VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
5081 case OVS_ACTION_ATTR_CT
: {
5082 const struct nlattr
*b
;
5084 bool commit
= false;
5087 const char *helper
= NULL
;
5088 const uint32_t *setmark
= NULL
;
5089 const struct ovs_key_ct_labels
*setlabel
= NULL
;
5091 NL_ATTR_FOR_EACH_UNSAFE (b
, left
, nl_attr_get(a
),
5092 nl_attr_get_size(a
)) {
5093 enum ovs_ct_attr sub_type
= nl_attr_type(b
);
5096 case OVS_CT_ATTR_FORCE_COMMIT
:
5099 case OVS_CT_ATTR_COMMIT
:
5102 case OVS_CT_ATTR_ZONE
:
5103 zone
= nl_attr_get_u16(b
);
5105 case OVS_CT_ATTR_HELPER
:
5106 helper
= nl_attr_get_string(b
);
5108 case OVS_CT_ATTR_MARK
:
5109 setmark
= nl_attr_get(b
);
5111 case OVS_CT_ATTR_LABELS
:
5112 setlabel
= nl_attr_get(b
);
5114 case OVS_CT_ATTR_EVENTMASK
:
5115 /* Silently ignored, as userspace datapath does not generate
5116 * netlink events. */
5118 case OVS_CT_ATTR_NAT
:
5119 case OVS_CT_ATTR_UNSPEC
:
5120 case __OVS_CT_ATTR_MAX
:
5125 conntrack_execute(&dp
->conntrack
, packets_
, aux
->flow
->dl_type
, force
,
5126 commit
, zone
, setmark
, setlabel
, helper
);
5130 case OVS_ACTION_ATTR_METER
:
5131 dp_netdev_run_meter(pmd
->dp
, packets_
, nl_attr_get_u32(a
),
5135 case OVS_ACTION_ATTR_PUSH_VLAN
:
5136 case OVS_ACTION_ATTR_POP_VLAN
:
5137 case OVS_ACTION_ATTR_PUSH_MPLS
:
5138 case OVS_ACTION_ATTR_POP_MPLS
:
5139 case OVS_ACTION_ATTR_SET
:
5140 case OVS_ACTION_ATTR_SET_MASKED
:
5141 case OVS_ACTION_ATTR_SAMPLE
:
5142 case OVS_ACTION_ATTR_HASH
:
5143 case OVS_ACTION_ATTR_UNSPEC
:
5144 case OVS_ACTION_ATTR_TRUNC
:
5145 case OVS_ACTION_ATTR_PUSH_ETH
:
5146 case OVS_ACTION_ATTR_POP_ETH
:
5147 case OVS_ACTION_ATTR_CLONE
:
5148 case __OVS_ACTION_ATTR_MAX
:
5152 dp_packet_delete_batch(packets_
, may_steal
);
5156 dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
5157 struct dp_packet_batch
*packets
,
5158 bool may_steal
, const struct flow
*flow
,
5159 const struct nlattr
*actions
, size_t actions_len
,
5162 struct dp_netdev_execute_aux aux
= { pmd
, now
, flow
};
5164 odp_execute_actions(&aux
, packets
, may_steal
, actions
,
5165 actions_len
, dp_execute_cb
);
5168 struct dp_netdev_ct_dump
{
5169 struct ct_dpif_dump_state up
;
5170 struct conntrack_dump dump
;
5171 struct conntrack
*ct
;
5172 struct dp_netdev
*dp
;
5176 dpif_netdev_ct_dump_start(struct dpif
*dpif
, struct ct_dpif_dump_state
**dump_
,
5177 const uint16_t *pzone
)
5179 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5180 struct dp_netdev_ct_dump
*dump
;
5182 dump
= xzalloc(sizeof *dump
);
5184 dump
->ct
= &dp
->conntrack
;
5186 conntrack_dump_start(&dp
->conntrack
, &dump
->dump
, pzone
);
5194 dpif_netdev_ct_dump_next(struct dpif
*dpif OVS_UNUSED
,
5195 struct ct_dpif_dump_state
*dump_
,
5196 struct ct_dpif_entry
*entry
)
5198 struct dp_netdev_ct_dump
*dump
;
5200 INIT_CONTAINER(dump
, dump_
, up
);
5202 return conntrack_dump_next(&dump
->dump
, entry
);
5206 dpif_netdev_ct_dump_done(struct dpif
*dpif OVS_UNUSED
,
5207 struct ct_dpif_dump_state
*dump_
)
5209 struct dp_netdev_ct_dump
*dump
;
5212 INIT_CONTAINER(dump
, dump_
, up
);
5214 err
= conntrack_dump_done(&dump
->dump
);
5222 dpif_netdev_ct_flush(struct dpif
*dpif
, const uint16_t *zone
)
5224 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
5226 return conntrack_flush(&dp
->conntrack
, zone
);
5229 const struct dpif_class dpif_netdev_class
= {
5232 dpif_netdev_enumerate
,
5233 dpif_netdev_port_open_type
,
5236 dpif_netdev_destroy
,
5239 dpif_netdev_get_stats
,
5240 dpif_netdev_port_add
,
5241 dpif_netdev_port_del
,
5242 dpif_netdev_port_set_config
,
5243 dpif_netdev_port_query_by_number
,
5244 dpif_netdev_port_query_by_name
,
5245 NULL
, /* port_get_pid */
5246 dpif_netdev_port_dump_start
,
5247 dpif_netdev_port_dump_next
,
5248 dpif_netdev_port_dump_done
,
5249 dpif_netdev_port_poll
,
5250 dpif_netdev_port_poll_wait
,
5251 dpif_netdev_flow_flush
,
5252 dpif_netdev_flow_dump_create
,
5253 dpif_netdev_flow_dump_destroy
,
5254 dpif_netdev_flow_dump_thread_create
,
5255 dpif_netdev_flow_dump_thread_destroy
,
5256 dpif_netdev_flow_dump_next
,
5257 dpif_netdev_operate
,
5258 NULL
, /* recv_set */
5259 NULL
, /* handlers_set */
5260 dpif_netdev_set_config
,
5261 dpif_netdev_queue_to_priority
,
5263 NULL
, /* recv_wait */
5264 NULL
, /* recv_purge */
5265 dpif_netdev_register_dp_purge_cb
,
5266 dpif_netdev_register_upcall_cb
,
5267 dpif_netdev_enable_upcall
,
5268 dpif_netdev_disable_upcall
,
5269 dpif_netdev_get_datapath_version
,
5270 dpif_netdev_ct_dump_start
,
5271 dpif_netdev_ct_dump_next
,
5272 dpif_netdev_ct_dump_done
,
5273 dpif_netdev_ct_flush
,
5274 dpif_netdev_meter_get_features
,
5275 dpif_netdev_meter_set
,
5276 dpif_netdev_meter_get
,
5277 dpif_netdev_meter_del
,
5281 dpif_dummy_change_port_number(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
5282 const char *argv
[], void *aux OVS_UNUSED
)
5284 struct dp_netdev_port
*port
;
5285 struct dp_netdev
*dp
;
5288 ovs_mutex_lock(&dp_netdev_mutex
);
5289 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
5290 if (!dp
|| !dpif_netdev_class_is_dummy(dp
->class)) {
5291 ovs_mutex_unlock(&dp_netdev_mutex
);
5292 unixctl_command_reply_error(conn
, "unknown datapath or not a dummy");
5295 ovs_refcount_ref(&dp
->ref_cnt
);
5296 ovs_mutex_unlock(&dp_netdev_mutex
);
5298 ovs_mutex_lock(&dp
->port_mutex
);
5299 if (get_port_by_name(dp
, argv
[2], &port
)) {
5300 unixctl_command_reply_error(conn
, "unknown port");
5304 port_no
= u32_to_odp(atoi(argv
[3]));
5305 if (!port_no
|| port_no
== ODPP_NONE
) {
5306 unixctl_command_reply_error(conn
, "bad port number");
5309 if (dp_netdev_lookup_port(dp
, port_no
)) {
5310 unixctl_command_reply_error(conn
, "port number already in use");
5315 hmap_remove(&dp
->ports
, &port
->node
);
5316 reconfigure_datapath(dp
);
5318 /* Reinsert with new port number. */
5319 port
->port_no
= port_no
;
5320 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
5321 reconfigure_datapath(dp
);
5323 seq_change(dp
->port_seq
);
5324 unixctl_command_reply(conn
, NULL
);
5327 ovs_mutex_unlock(&dp
->port_mutex
);
5328 dp_netdev_unref(dp
);
5332 dpif_dummy_register__(const char *type
)
5334 struct dpif_class
*class;
5336 class = xmalloc(sizeof *class);
5337 *class = dpif_netdev_class
;
5338 class->type
= xstrdup(type
);
5339 dp_register_provider(class);
5343 dpif_dummy_override(const char *type
)
5348 * Ignore EAFNOSUPPORT to allow --enable-dummy=system with
5349 * a userland-only build. It's useful for testsuite.
5351 error
= dp_unregister_provider(type
);
5352 if (error
== 0 || error
== EAFNOSUPPORT
) {
5353 dpif_dummy_register__(type
);
5358 dpif_dummy_register(enum dummy_level level
)
5360 if (level
== DUMMY_OVERRIDE_ALL
) {
5365 dp_enumerate_types(&types
);
5366 SSET_FOR_EACH (type
, &types
) {
5367 dpif_dummy_override(type
);
5369 sset_destroy(&types
);
5370 } else if (level
== DUMMY_OVERRIDE_SYSTEM
) {
5371 dpif_dummy_override("system");
5374 dpif_dummy_register__("dummy");
5376 unixctl_command_register("dpif-dummy/change-port-number",
5377 "dp port new-number",
5378 3, 3, dpif_dummy_change_port_number
, NULL
);
5381 /* Datapath Classifier. */
5383 /* A set of rules that all have the same fields wildcarded. */
5384 struct dpcls_subtable
{
5385 /* The fields are only used by writers. */
5386 struct cmap_node cmap_node OVS_GUARDED
; /* Within dpcls 'subtables_map'. */
5388 /* These fields are accessed by readers. */
5389 struct cmap rules
; /* Contains "struct dpcls_rule"s. */
5390 uint32_t hit_cnt
; /* Number of match hits in subtable in current
5391 optimization interval. */
5392 struct netdev_flow_key mask
; /* Wildcards for fields (const). */
5393 /* 'mask' must be the last field, additional space is allocated here. */
5396 /* Initializes 'cls' as a classifier that initially contains no classification
5399 dpcls_init(struct dpcls
*cls
)
5401 cmap_init(&cls
->subtables_map
);
5402 pvector_init(&cls
->subtables
);
5406 dpcls_destroy_subtable(struct dpcls
*cls
, struct dpcls_subtable
*subtable
)
5408 VLOG_DBG("Destroying subtable %p for in_port %d", subtable
, cls
->in_port
);
5409 pvector_remove(&cls
->subtables
, subtable
);
5410 cmap_remove(&cls
->subtables_map
, &subtable
->cmap_node
,
5411 subtable
->mask
.hash
);
5412 cmap_destroy(&subtable
->rules
);
5413 ovsrcu_postpone(free
, subtable
);
5416 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
5417 * caller's responsibility.
5418 * May only be called after all the readers have been terminated. */
5420 dpcls_destroy(struct dpcls
*cls
)
5423 struct dpcls_subtable
*subtable
;
5425 CMAP_FOR_EACH (subtable
, cmap_node
, &cls
->subtables_map
) {
5426 ovs_assert(cmap_count(&subtable
->rules
) == 0);
5427 dpcls_destroy_subtable(cls
, subtable
);
5429 cmap_destroy(&cls
->subtables_map
);
5430 pvector_destroy(&cls
->subtables
);
5434 static struct dpcls_subtable
*
5435 dpcls_create_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
5437 struct dpcls_subtable
*subtable
;
5439 /* Need to add one. */
5440 subtable
= xmalloc(sizeof *subtable
5441 - sizeof subtable
->mask
.mf
+ mask
->len
);
5442 cmap_init(&subtable
->rules
);
5443 subtable
->hit_cnt
= 0;
5444 netdev_flow_key_clone(&subtable
->mask
, mask
);
5445 cmap_insert(&cls
->subtables_map
, &subtable
->cmap_node
, mask
->hash
);
5446 /* Add the new subtable at the end of the pvector (with no hits yet) */
5447 pvector_insert(&cls
->subtables
, subtable
, 0);
5448 VLOG_DBG("Creating %"PRIuSIZE
". subtable %p for in_port %d",
5449 cmap_count(&cls
->subtables_map
), subtable
, cls
->in_port
);
5450 pvector_publish(&cls
->subtables
);
5455 static inline struct dpcls_subtable
*
5456 dpcls_find_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
5458 struct dpcls_subtable
*subtable
;
5460 CMAP_FOR_EACH_WITH_HASH (subtable
, cmap_node
, mask
->hash
,
5461 &cls
->subtables_map
) {
5462 if (netdev_flow_key_equal(&subtable
->mask
, mask
)) {
5466 return dpcls_create_subtable(cls
, mask
);
5470 /* Periodically sort the dpcls subtable vectors according to hit counts */
5472 dpcls_sort_subtable_vector(struct dpcls
*cls
)
5474 struct pvector
*pvec
= &cls
->subtables
;
5475 struct dpcls_subtable
*subtable
;
5477 PVECTOR_FOR_EACH (subtable
, pvec
) {
5478 pvector_change_priority(pvec
, subtable
, subtable
->hit_cnt
);
5479 subtable
->hit_cnt
= 0;
5481 pvector_publish(pvec
);
5485 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread
*pmd
)
5488 long long int now
= time_msec();
5490 if (now
> pmd
->next_optimization
) {
5491 /* Try to obtain the flow lock to block out revalidator threads.
5492 * If not possible, just try next time. */
5493 if (!ovs_mutex_trylock(&pmd
->flow_mutex
)) {
5494 /* Optimize each classifier */
5495 CMAP_FOR_EACH (cls
, node
, &pmd
->classifiers
) {
5496 dpcls_sort_subtable_vector(cls
);
5498 ovs_mutex_unlock(&pmd
->flow_mutex
);
5499 /* Start new measuring interval */
5500 pmd
->next_optimization
= now
+ DPCLS_OPTIMIZATION_INTERVAL
;
5505 /* Insert 'rule' into 'cls'. */
5507 dpcls_insert(struct dpcls
*cls
, struct dpcls_rule
*rule
,
5508 const struct netdev_flow_key
*mask
)
5510 struct dpcls_subtable
*subtable
= dpcls_find_subtable(cls
, mask
);
5512 /* Refer to subtable's mask, also for later removal. */
5513 rule
->mask
= &subtable
->mask
;
5514 cmap_insert(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
);
5517 /* Removes 'rule' from 'cls', also destructing the 'rule'. */
5519 dpcls_remove(struct dpcls
*cls
, struct dpcls_rule
*rule
)
5521 struct dpcls_subtable
*subtable
;
5523 ovs_assert(rule
->mask
);
5525 /* Get subtable from reference in rule->mask. */
5526 INIT_CONTAINER(subtable
, rule
->mask
, mask
);
5527 if (cmap_remove(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
)
5529 /* Delete empty subtable. */
5530 dpcls_destroy_subtable(cls
, subtable
);
5531 pvector_publish(&cls
->subtables
);
5535 /* Returns true if 'target' satisfies 'key' in 'mask', that is, if each 1-bit
5536 * in 'mask' the values in 'key' and 'target' are the same. */
5538 dpcls_rule_matches_key(const struct dpcls_rule
*rule
,
5539 const struct netdev_flow_key
*target
)
5541 const uint64_t *keyp
= miniflow_get_values(&rule
->flow
.mf
);
5542 const uint64_t *maskp
= miniflow_get_values(&rule
->mask
->mf
);
5545 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, target
, rule
->flow
.mf
.map
) {
5546 if (OVS_UNLIKELY((value
& *maskp
++) != *keyp
++)) {
5553 /* For each miniflow in 'keys' performs a classifier lookup writing the result
5554 * into the corresponding slot in 'rules'. If a particular entry in 'keys' is
5555 * NULL it is skipped.
5557 * This function is optimized for use in the userspace datapath and therefore
5558 * does not implement a lot of features available in the standard
5559 * classifier_lookup() function. Specifically, it does not implement
5560 * priorities, instead returning any rule which matches the flow.
5562 * Returns true if all miniflows found a corresponding rule. */
5564 dpcls_lookup(struct dpcls
*cls
, const struct netdev_flow_key keys
[],
5565 struct dpcls_rule
**rules
, const size_t cnt
,
5568 /* The received 'cnt' miniflows are the search-keys that will be processed
5569 * to find a matching entry into the available subtables.
5570 * The number of bits in map_type is equal to NETDEV_MAX_BURST. */
5571 typedef uint32_t map_type
;
5572 #define MAP_BITS (sizeof(map_type) * CHAR_BIT)
5573 BUILD_ASSERT_DECL(MAP_BITS
>= NETDEV_MAX_BURST
);
5575 struct dpcls_subtable
*subtable
;
5577 map_type keys_map
= TYPE_MAXIMUM(map_type
); /* Set all bits. */
5579 uint32_t hashes
[MAP_BITS
];
5580 const struct cmap_node
*nodes
[MAP_BITS
];
5582 if (cnt
!= MAP_BITS
) {
5583 keys_map
>>= MAP_BITS
- cnt
; /* Clear extra bits. */
5585 memset(rules
, 0, cnt
* sizeof *rules
);
5587 int lookups_match
= 0, subtable_pos
= 1;
5589 /* The Datapath classifier - aka dpcls - is composed of subtables.
5590 * Subtables are dynamically created as needed when new rules are inserted.
5591 * Each subtable collects rules with matches on a specific subset of packet
5592 * fields as defined by the subtable's mask. We proceed to process every
5593 * search-key against each subtable, but when a match is found for a
5594 * search-key, the search for that key can stop because the rules are
5595 * non-overlapping. */
5596 PVECTOR_FOR_EACH (subtable
, &cls
->subtables
) {
5599 /* Compute hashes for the remaining keys. Each search-key is
5600 * masked with the subtable's mask to avoid hashing the wildcarded
5602 ULLONG_FOR_EACH_1(i
, keys_map
) {
5603 hashes
[i
] = netdev_flow_key_hash_in_mask(&keys
[i
],
5607 found_map
= cmap_find_batch(&subtable
->rules
, keys_map
, hashes
, nodes
);
5608 /* Check results. When the i-th bit of found_map is set, it means
5609 * that a set of nodes with a matching hash value was found for the
5610 * i-th search-key. Due to possible hash collisions we need to check
5611 * which of the found rules, if any, really matches our masked
5613 ULLONG_FOR_EACH_1(i
, found_map
) {
5614 struct dpcls_rule
*rule
;
5616 CMAP_NODE_FOR_EACH (rule
, cmap_node
, nodes
[i
]) {
5617 if (OVS_LIKELY(dpcls_rule_matches_key(rule
, &keys
[i
]))) {
5619 /* Even at 20 Mpps the 32-bit hit_cnt cannot wrap
5620 * within one second optimization interval. */
5621 subtable
->hit_cnt
++;
5622 lookups_match
+= subtable_pos
;
5626 /* None of the found rules was a match. Reset the i-th bit to
5627 * keep searching this key in the next subtable. */
5628 ULLONG_SET0(found_map
, i
); /* Did not match. */
5630 ; /* Keep Sparse happy. */
5632 keys_map
&= ~found_map
; /* Clear the found rules. */
5634 if (num_lookups_p
) {
5635 *num_lookups_p
= lookups_match
;
5637 return true; /* All found. */
5641 if (num_lookups_p
) {
5642 *num_lookups_p
= lookups_match
;
5644 return false; /* Some misses. */