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. */
90 /* Protects against changes to 'dp_netdevs'. */
91 static struct ovs_mutex dp_netdev_mutex
= OVS_MUTEX_INITIALIZER
;
93 /* Contains all 'struct dp_netdev's. */
94 static struct shash dp_netdevs
OVS_GUARDED_BY(dp_netdev_mutex
)
95 = SHASH_INITIALIZER(&dp_netdevs
);
97 static struct vlog_rate_limit upcall_rl
= VLOG_RATE_LIMIT_INIT(600, 600);
99 #define DP_NETDEV_CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \
100 | CS_INVALID | CS_REPLY_DIR | CS_TRACKED)
101 #define DP_NETDEV_CS_UNSUPPORTED_MASK (~(uint32_t)DP_NETDEV_CS_SUPPORTED_MASK)
103 static struct odp_support dp_netdev_support
= {
104 .max_mpls_depth
= SIZE_MAX
,
112 /* Stores a miniflow with inline values */
114 struct netdev_flow_key
{
115 uint32_t hash
; /* Hash function differs for different users. */
116 uint32_t len
; /* Length of the following miniflow (incl. map). */
118 uint64_t buf
[FLOW_MAX_PACKET_U64S
];
121 /* Exact match cache for frequently used flows
123 * The cache uses a 32-bit hash of the packet (which can be the RSS hash) to
124 * search its entries for a miniflow that matches exactly the miniflow of the
125 * packet. It stores the 'dpcls_rule' (rule) that matches the miniflow.
127 * A cache entry holds a reference to its 'dp_netdev_flow'.
129 * A miniflow with a given hash can be in one of EM_FLOW_HASH_SEGS different
130 * entries. The 32-bit hash is split into EM_FLOW_HASH_SEGS values (each of
131 * them is EM_FLOW_HASH_SHIFT bits wide and the remainder is thrown away). Each
132 * value is the index of a cache entry where the miniflow could be.
138 * Each pmd_thread has its own private exact match cache.
139 * If dp_netdev_input is not called from a pmd thread, a mutex is used.
142 #define EM_FLOW_HASH_SHIFT 13
143 #define EM_FLOW_HASH_ENTRIES (1u << EM_FLOW_HASH_SHIFT)
144 #define EM_FLOW_HASH_MASK (EM_FLOW_HASH_ENTRIES - 1)
145 #define EM_FLOW_HASH_SEGS 2
148 struct dp_netdev_flow
*flow
;
149 struct netdev_flow_key key
; /* key.hash used for emc hash value. */
153 struct emc_entry entries
[EM_FLOW_HASH_ENTRIES
];
154 int sweep_idx
; /* For emc_cache_slow_sweep(). */
157 /* Iterate in the exact match cache through every entry that might contain a
158 * miniflow with hash 'HASH'. */
159 #define EMC_FOR_EACH_POS_WITH_HASH(EMC, CURRENT_ENTRY, HASH) \
160 for (uint32_t i__ = 0, srch_hash__ = (HASH); \
161 (CURRENT_ENTRY) = &(EMC)->entries[srch_hash__ & EM_FLOW_HASH_MASK], \
162 i__ < EM_FLOW_HASH_SEGS; \
163 i__++, srch_hash__ >>= EM_FLOW_HASH_SHIFT)
165 /* Simple non-wildcarding single-priority classifier. */
167 /* Time in ms between successive optimizations of the dpcls subtable vector */
168 #define DPCLS_OPTIMIZATION_INTERVAL 1000
171 struct cmap_node node
; /* Within dp_netdev_pmd_thread.classifiers */
173 struct cmap subtables_map
;
174 struct pvector subtables
;
177 /* A rule to be inserted to the classifier. */
179 struct cmap_node cmap_node
; /* Within struct dpcls_subtable 'rules'. */
180 struct netdev_flow_key
*mask
; /* Subtable's mask. */
181 struct netdev_flow_key flow
; /* Matching key. */
182 /* 'flow' must be the last field, additional space is allocated here. */
185 static void dpcls_init(struct dpcls
*);
186 static void dpcls_destroy(struct dpcls
*);
187 static void dpcls_sort_subtable_vector(struct dpcls
*);
188 static void dpcls_insert(struct dpcls
*, struct dpcls_rule
*,
189 const struct netdev_flow_key
*mask
);
190 static void dpcls_remove(struct dpcls
*, struct dpcls_rule
*);
191 static bool dpcls_lookup(struct dpcls
*cls
,
192 const struct netdev_flow_key keys
[],
193 struct dpcls_rule
**rules
, size_t cnt
,
196 /* Datapath based on the network device interface from netdev.h.
202 * Some members, marked 'const', are immutable. Accessing other members
203 * requires synchronization, as noted in more detail below.
205 * Acquisition order is, from outermost to innermost:
207 * dp_netdev_mutex (global)
212 const struct dpif_class
*const class;
213 const char *const name
;
215 struct ovs_refcount ref_cnt
;
216 atomic_flag destroyed
;
220 * Any lookup into 'ports' or any access to the dp_netdev_ports found
221 * through 'ports' requires taking 'port_mutex'. */
222 struct ovs_mutex port_mutex
;
224 struct seq
*port_seq
; /* Incremented whenever a port changes. */
226 /* Protects access to ofproto-dpif-upcall interface during revalidator
227 * thread synchronization. */
228 struct fat_rwlock upcall_rwlock
;
229 upcall_callback
*upcall_cb
; /* Callback function for executing upcalls. */
232 /* Callback function for notifying the purging of dp flows (during
233 * reseting pmd deletion). */
234 dp_purge_callback
*dp_purge_cb
;
237 /* Stores all 'struct dp_netdev_pmd_thread's. */
238 struct cmap poll_threads
;
240 /* Protects the access of the 'struct dp_netdev_pmd_thread'
241 * instance for non-pmd thread. */
242 struct ovs_mutex non_pmd_mutex
;
244 /* Each pmd thread will store its pointer to
245 * 'struct dp_netdev_pmd_thread' in 'per_pmd_key'. */
246 ovsthread_key_t per_pmd_key
;
248 struct seq
*reconfigure_seq
;
249 uint64_t last_reconfigure_seq
;
251 /* Cpu mask for pin of pmd threads. */
254 uint64_t last_tnl_conf_seq
;
256 struct conntrack conntrack
;
259 static struct dp_netdev_port
*dp_netdev_lookup_port(const struct dp_netdev
*dp
,
261 OVS_REQUIRES(dp
->port_mutex
);
264 DP_STAT_EXACT_HIT
, /* Packets that had an exact match (emc). */
265 DP_STAT_MASKED_HIT
, /* Packets that matched in the flow table. */
266 DP_STAT_MISS
, /* Packets that did not match. */
267 DP_STAT_LOST
, /* Packets not passed up to the client. */
268 DP_STAT_LOOKUP_HIT
, /* Number of subtable lookups for flow table
273 enum pmd_cycles_counter_type
{
274 PMD_CYCLES_POLLING
, /* Cycles spent polling NICs. */
275 PMD_CYCLES_PROCESSING
, /* Cycles spent processing packets */
279 #define XPS_TIMEOUT_MS 500LL
281 /* Contained by struct dp_netdev_port's 'rxqs' member. */
282 struct dp_netdev_rxq
{
283 struct dp_netdev_port
*port
;
284 struct netdev_rxq
*rx
;
285 unsigned core_id
; /* Core to which this queue should be
286 pinned. OVS_CORE_UNSPEC if the
287 queue doesn't need to be pinned to a
289 struct dp_netdev_pmd_thread
*pmd
; /* pmd thread that will poll this queue. */
292 /* A port in a netdev-based datapath. */
293 struct dp_netdev_port
{
295 struct netdev
*netdev
;
296 struct hmap_node node
; /* Node in dp_netdev's 'ports'. */
297 struct netdev_saved_flags
*sf
;
298 struct dp_netdev_rxq
*rxqs
;
299 unsigned n_rxq
; /* Number of elements in 'rxq' */
300 bool dynamic_txqs
; /* If true XPS will be used. */
301 unsigned *txq_used
; /* Number of threads that uses each tx queue. */
302 struct ovs_mutex txq_used_mutex
;
303 char *type
; /* Port type as requested by user. */
304 char *rxq_affinity_list
; /* Requested affinity of rx queues. */
305 bool need_reconfigure
; /* True if we should reconfigure netdev. */
308 /* Contained by struct dp_netdev_flow's 'stats' member. */
309 struct dp_netdev_flow_stats
{
310 atomic_llong used
; /* Last used time, in monotonic msecs. */
311 atomic_ullong packet_count
; /* Number of packets matched. */
312 atomic_ullong byte_count
; /* Number of bytes matched. */
313 atomic_uint16_t tcp_flags
; /* Bitwise-OR of seen tcp_flags values. */
316 /* A flow in 'dp_netdev_pmd_thread's 'flow_table'.
322 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
323 * its pmd thread's classifier. The text below calls this classifier 'cls'.
328 * The thread safety rules described here for "struct dp_netdev_flow" are
329 * motivated by two goals:
331 * - Prevent threads that read members of "struct dp_netdev_flow" from
332 * reading bad data due to changes by some thread concurrently modifying
335 * - Prevent two threads making changes to members of a given "struct
336 * dp_netdev_flow" from interfering with each other.
342 * A flow 'flow' may be accessed without a risk of being freed during an RCU
343 * grace period. Code that needs to hold onto a flow for a while
344 * should try incrementing 'flow->ref_cnt' with dp_netdev_flow_ref().
346 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
347 * flow from being deleted from 'cls' and it doesn't protect members of 'flow'
350 * Some members, marked 'const', are immutable. Accessing other members
351 * requires synchronization, as noted in more detail below.
353 struct dp_netdev_flow
{
354 const struct flow flow
; /* Unmasked flow that created this entry. */
355 /* Hash table index by unmasked flow. */
356 const struct cmap_node node
; /* In owning dp_netdev_pmd_thread's */
358 const ovs_u128 ufid
; /* Unique flow identifier. */
359 const unsigned pmd_id
; /* The 'core_id' of pmd thread owning this */
362 /* Number of references.
363 * The classifier owns one reference.
364 * Any thread trying to keep a rule from being freed should hold its own
366 struct ovs_refcount ref_cnt
;
371 struct dp_netdev_flow_stats stats
;
374 OVSRCU_TYPE(struct dp_netdev_actions
*) actions
;
376 /* While processing a group of input packets, the datapath uses the next
377 * member to store a pointer to the output batch for the flow. It is
378 * reset after the batch has been sent out (See dp_netdev_queue_batches(),
379 * packet_batch_per_flow_init() and packet_batch_per_flow_execute()). */
380 struct packet_batch_per_flow
*batch
;
382 /* Packet classification. */
383 struct dpcls_rule cr
; /* In owning dp_netdev's 'cls'. */
384 /* 'cr' must be the last member. */
387 static void dp_netdev_flow_unref(struct dp_netdev_flow
*);
388 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*);
389 static int dpif_netdev_flow_from_nlattrs(const struct nlattr
*, uint32_t,
392 /* A set of datapath actions within a "struct dp_netdev_flow".
398 * A struct dp_netdev_actions 'actions' is protected with RCU. */
399 struct dp_netdev_actions
{
400 /* These members are immutable: they do not change during the struct's
402 unsigned int size
; /* Size of 'actions', in bytes. */
403 struct nlattr actions
[]; /* Sequence of OVS_ACTION_ATTR_* attributes. */
406 struct dp_netdev_actions
*dp_netdev_actions_create(const struct nlattr
*,
408 struct dp_netdev_actions
*dp_netdev_flow_get_actions(
409 const struct dp_netdev_flow
*);
410 static void dp_netdev_actions_free(struct dp_netdev_actions
*);
412 /* Contained by struct dp_netdev_pmd_thread's 'stats' member. */
413 struct dp_netdev_pmd_stats
{
414 /* Indexed by DP_STAT_*. */
415 atomic_ullong n
[DP_N_STATS
];
418 /* Contained by struct dp_netdev_pmd_thread's 'cycle' member. */
419 struct dp_netdev_pmd_cycles
{
420 /* Indexed by PMD_CYCLES_*. */
421 atomic_ullong n
[PMD_N_CYCLES
];
424 struct polled_queue
{
425 struct netdev_rxq
*rx
;
429 /* Contained by struct dp_netdev_pmd_thread's 'poll_list' member. */
431 struct dp_netdev_rxq
*rxq
;
432 struct hmap_node node
;
435 /* Contained by struct dp_netdev_pmd_thread's 'send_port_cache',
436 * 'tnl_port_cache' or 'tx_ports'. */
438 struct dp_netdev_port
*port
;
441 struct hmap_node node
;
444 /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
445 * the performance overhead of interrupt processing. Therefore netdev can
446 * not implement rx-wait for these devices. dpif-netdev needs to poll
447 * these device to check for recv buffer. pmd-thread does polling for
448 * devices assigned to itself.
450 * DPDK used PMD for accessing NIC.
452 * Note, instance with cpu core id NON_PMD_CORE_ID will be reserved for
453 * I/O of all non-pmd threads. There will be no actual thread created
456 * Each struct has its own flow table and classifier. Packets received
457 * from managed ports are looked up in the corresponding pmd thread's
458 * flow table, and are executed with the found actions.
460 struct dp_netdev_pmd_thread
{
461 struct dp_netdev
*dp
;
462 struct ovs_refcount ref_cnt
; /* Every reference must be refcount'ed. */
463 struct cmap_node node
; /* In 'dp->poll_threads'. */
465 pthread_cond_t cond
; /* For synchronizing pmd thread reload. */
466 struct ovs_mutex cond_mutex
; /* Mutex for condition variable. */
468 /* Per thread exact-match cache. Note, the instance for cpu core
469 * NON_PMD_CORE_ID can be accessed by multiple threads, and thusly
470 * need to be protected by 'non_pmd_mutex'. Every other instance
471 * will only be accessed by its own pmd thread. */
472 struct emc_cache flow_cache
;
474 /* Flow-Table and classifiers
476 * Writers of 'flow_table' must take the 'flow_mutex'. Corresponding
477 * changes to 'classifiers' must be made while still holding the
480 struct ovs_mutex flow_mutex
;
481 struct cmap flow_table OVS_GUARDED
; /* Flow table. */
483 /* One classifier per in_port polled by the pmd */
484 struct cmap classifiers
;
485 /* Periodically sort subtable vectors according to hit frequencies */
486 long long int next_optimization
;
489 struct dp_netdev_pmd_stats stats
;
491 /* Cycles counters */
492 struct dp_netdev_pmd_cycles cycles
;
494 /* Used to count cicles. See 'cycles_counter_end()' */
495 unsigned long long last_cycles
;
497 struct latch exit_latch
; /* For terminating the pmd thread. */
498 struct seq
*reload_seq
;
499 uint64_t last_reload_seq
;
500 atomic_bool reload
; /* Do we need to reload ports? */
502 unsigned core_id
; /* CPU core id of this pmd thread. */
503 int numa_id
; /* numa node id of this pmd thread. */
506 /* Queue id used by this pmd thread to send packets on all netdevs if
507 * XPS disabled for this netdev. All static_tx_qid's are unique and less
508 * than 'cmap_count(dp->poll_threads)'. */
509 const int static_tx_qid
;
511 struct ovs_mutex port_mutex
; /* Mutex for 'poll_list' and 'tx_ports'. */
512 /* List of rx queues to poll. */
513 struct hmap poll_list OVS_GUARDED
;
514 /* Map of 'tx_port's used for transmission. Written by the main thread,
515 * read by the pmd thread. */
516 struct hmap tx_ports OVS_GUARDED
;
518 /* These are thread-local copies of 'tx_ports'. One contains only tunnel
519 * ports (that support push_tunnel/pop_tunnel), the other contains ports
520 * with at least one txq (that support send). A port can be in both.
522 * There are two separate maps to make sure that we don't try to execute
523 * OUTPUT on a device which has 0 txqs or PUSH/POP on a non-tunnel device.
525 * The instances for cpu core NON_PMD_CORE_ID can be accessed by multiple
526 * threads, and thusly need to be protected by 'non_pmd_mutex'. Every
527 * other instance will only be accessed by its own pmd thread. */
528 struct hmap tnl_port_cache
;
529 struct hmap send_port_cache
;
531 /* Only a pmd thread can write on its own 'cycles' and 'stats'.
532 * The main thread keeps 'stats_zero' and 'cycles_zero' as base
533 * values and subtracts them from 'stats' and 'cycles' before
534 * reporting to the user */
535 unsigned long long stats_zero
[DP_N_STATS
];
536 uint64_t cycles_zero
[PMD_N_CYCLES
];
538 /* Set to true if the pmd thread needs to be reloaded. */
542 /* Interface to netdev-based datapath. */
545 struct dp_netdev
*dp
;
546 uint64_t last_port_seq
;
549 static int get_port_by_number(struct dp_netdev
*dp
, odp_port_t port_no
,
550 struct dp_netdev_port
**portp
)
551 OVS_REQUIRES(dp
->port_mutex
);
552 static int get_port_by_name(struct dp_netdev
*dp
, const char *devname
,
553 struct dp_netdev_port
**portp
)
554 OVS_REQUIRES(dp
->port_mutex
);
555 static void dp_netdev_free(struct dp_netdev
*)
556 OVS_REQUIRES(dp_netdev_mutex
);
557 static int do_add_port(struct dp_netdev
*dp
, const char *devname
,
558 const char *type
, odp_port_t port_no
)
559 OVS_REQUIRES(dp
->port_mutex
);
560 static void do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*)
561 OVS_REQUIRES(dp
->port_mutex
);
562 static int dpif_netdev_open(const struct dpif_class
*, const char *name
,
563 bool create
, struct dpif
**);
564 static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
565 struct dp_packet_batch
*,
566 bool may_steal
, const struct flow
*flow
,
567 const struct nlattr
*actions
,
570 static void dp_netdev_input(struct dp_netdev_pmd_thread
*,
571 struct dp_packet_batch
*, odp_port_t port_no
);
572 static void dp_netdev_recirculate(struct dp_netdev_pmd_thread
*,
573 struct dp_packet_batch
*);
575 static void dp_netdev_disable_upcall(struct dp_netdev
*);
576 static void dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
);
577 static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
,
578 struct dp_netdev
*dp
, unsigned core_id
,
580 static void dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
);
581 static void dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
582 OVS_REQUIRES(dp
->port_mutex
);
584 static void *pmd_thread_main(void *);
585 static struct dp_netdev_pmd_thread
*dp_netdev_get_pmd(struct dp_netdev
*dp
,
587 static struct dp_netdev_pmd_thread
*
588 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
);
589 static void dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
, bool non_pmd
);
590 static void dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
);
591 static void dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
592 struct dp_netdev_port
*port
)
593 OVS_REQUIRES(pmd
->port_mutex
);
594 static void dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
596 OVS_REQUIRES(pmd
->port_mutex
);
597 static void dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
598 struct dp_netdev_rxq
*rxq
)
599 OVS_REQUIRES(pmd
->port_mutex
);
600 static void dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
601 struct rxq_poll
*poll
)
602 OVS_REQUIRES(pmd
->port_mutex
);
603 static void reconfigure_datapath(struct dp_netdev
*dp
)
604 OVS_REQUIRES(dp
->port_mutex
);
605 static bool dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
);
606 static void dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
);
607 static void dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
);
608 static void pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
609 OVS_REQUIRES(pmd
->port_mutex
);
611 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread
*pmd
);
614 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
615 long long now
, bool purge
);
616 static int dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
617 struct tx_port
*tx
, long long now
);
619 static inline bool emc_entry_alive(struct emc_entry
*ce
);
620 static void emc_clear_entry(struct emc_entry
*ce
);
623 emc_cache_init(struct emc_cache
*flow_cache
)
627 flow_cache
->sweep_idx
= 0;
628 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
629 flow_cache
->entries
[i
].flow
= NULL
;
630 flow_cache
->entries
[i
].key
.hash
= 0;
631 flow_cache
->entries
[i
].key
.len
= sizeof(struct miniflow
);
632 flowmap_init(&flow_cache
->entries
[i
].key
.mf
.map
);
637 emc_cache_uninit(struct emc_cache
*flow_cache
)
641 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
642 emc_clear_entry(&flow_cache
->entries
[i
]);
646 /* Check and clear dead flow references slowly (one entry at each
649 emc_cache_slow_sweep(struct emc_cache
*flow_cache
)
651 struct emc_entry
*entry
= &flow_cache
->entries
[flow_cache
->sweep_idx
];
653 if (!emc_entry_alive(entry
)) {
654 emc_clear_entry(entry
);
656 flow_cache
->sweep_idx
= (flow_cache
->sweep_idx
+ 1) & EM_FLOW_HASH_MASK
;
659 /* Returns true if 'dpif' is a netdev or dummy dpif, false otherwise. */
661 dpif_is_netdev(const struct dpif
*dpif
)
663 return dpif
->dpif_class
->open
== dpif_netdev_open
;
666 static struct dpif_netdev
*
667 dpif_netdev_cast(const struct dpif
*dpif
)
669 ovs_assert(dpif_is_netdev(dpif
));
670 return CONTAINER_OF(dpif
, struct dpif_netdev
, dpif
);
673 static struct dp_netdev
*
674 get_dp_netdev(const struct dpif
*dpif
)
676 return dpif_netdev_cast(dpif
)->dp
;
680 PMD_INFO_SHOW_STATS
, /* Show how cpu cycles are spent. */
681 PMD_INFO_CLEAR_STATS
, /* Set the cycles count to 0. */
682 PMD_INFO_SHOW_RXQ
/* Show poll-lists of pmd threads. */
686 pmd_info_show_stats(struct ds
*reply
,
687 struct dp_netdev_pmd_thread
*pmd
,
688 unsigned long long stats
[DP_N_STATS
],
689 uint64_t cycles
[PMD_N_CYCLES
])
691 unsigned long long total_packets
= 0;
692 uint64_t total_cycles
= 0;
695 /* These loops subtracts reference values ('*_zero') from the counters.
696 * Since loads and stores are relaxed, it might be possible for a '*_zero'
697 * value to be more recent than the current value we're reading from the
698 * counter. This is not a big problem, since these numbers are not
699 * supposed to be too accurate, but we should at least make sure that
700 * the result is not negative. */
701 for (i
= 0; i
< DP_N_STATS
; i
++) {
702 if (stats
[i
] > pmd
->stats_zero
[i
]) {
703 stats
[i
] -= pmd
->stats_zero
[i
];
708 if (i
!= DP_STAT_LOST
) {
709 /* Lost packets are already included in DP_STAT_MISS */
710 total_packets
+= stats
[i
];
714 for (i
= 0; i
< PMD_N_CYCLES
; i
++) {
715 if (cycles
[i
] > pmd
->cycles_zero
[i
]) {
716 cycles
[i
] -= pmd
->cycles_zero
[i
];
721 total_cycles
+= cycles
[i
];
724 ds_put_cstr(reply
, (pmd
->core_id
== NON_PMD_CORE_ID
)
725 ? "main thread" : "pmd thread");
727 if (pmd
->numa_id
!= OVS_NUMA_UNSPEC
) {
728 ds_put_format(reply
, " numa_id %d", pmd
->numa_id
);
730 if (pmd
->core_id
!= OVS_CORE_UNSPEC
&& pmd
->core_id
!= NON_PMD_CORE_ID
) {
731 ds_put_format(reply
, " core_id %u", pmd
->core_id
);
733 ds_put_cstr(reply
, ":\n");
736 "\temc hits:%llu\n\tmegaflow hits:%llu\n"
737 "\tavg. subtable lookups per hit:%.2f\n"
738 "\tmiss:%llu\n\tlost:%llu\n",
739 stats
[DP_STAT_EXACT_HIT
], stats
[DP_STAT_MASKED_HIT
],
740 stats
[DP_STAT_MASKED_HIT
] > 0
741 ? (1.0*stats
[DP_STAT_LOOKUP_HIT
])/stats
[DP_STAT_MASKED_HIT
]
743 stats
[DP_STAT_MISS
], stats
[DP_STAT_LOST
]);
745 if (total_cycles
== 0) {
750 "\tpolling cycles:%"PRIu64
" (%.02f%%)\n"
751 "\tprocessing cycles:%"PRIu64
" (%.02f%%)\n",
752 cycles
[PMD_CYCLES_POLLING
],
753 cycles
[PMD_CYCLES_POLLING
] / (double)total_cycles
* 100,
754 cycles
[PMD_CYCLES_PROCESSING
],
755 cycles
[PMD_CYCLES_PROCESSING
] / (double)total_cycles
* 100);
757 if (total_packets
== 0) {
762 "\tavg cycles per packet: %.02f (%"PRIu64
"/%llu)\n",
763 total_cycles
/ (double)total_packets
,
764 total_cycles
, total_packets
);
767 "\tavg processing cycles per packet: "
768 "%.02f (%"PRIu64
"/%llu)\n",
769 cycles
[PMD_CYCLES_PROCESSING
] / (double)total_packets
,
770 cycles
[PMD_CYCLES_PROCESSING
], total_packets
);
774 pmd_info_clear_stats(struct ds
*reply OVS_UNUSED
,
775 struct dp_netdev_pmd_thread
*pmd
,
776 unsigned long long stats
[DP_N_STATS
],
777 uint64_t cycles
[PMD_N_CYCLES
])
781 /* We cannot write 'stats' and 'cycles' (because they're written by other
782 * threads) and we shouldn't change 'stats' (because they're used to count
783 * datapath stats, which must not be cleared here). Instead, we save the
784 * current values and subtract them from the values to be displayed in the
786 for (i
= 0; i
< DP_N_STATS
; i
++) {
787 pmd
->stats_zero
[i
] = stats
[i
];
789 for (i
= 0; i
< PMD_N_CYCLES
; i
++) {
790 pmd
->cycles_zero
[i
] = cycles
[i
];
795 compare_poll_list(const void *a_
, const void *b_
)
797 const struct rxq_poll
*a
= a_
;
798 const struct rxq_poll
*b
= b_
;
800 const char *namea
= netdev_rxq_get_name(a
->rxq
->rx
);
801 const char *nameb
= netdev_rxq_get_name(b
->rxq
->rx
);
803 int cmp
= strcmp(namea
, nameb
);
805 return netdev_rxq_get_queue_id(a
->rxq
->rx
)
806 - netdev_rxq_get_queue_id(b
->rxq
->rx
);
813 sorted_poll_list(struct dp_netdev_pmd_thread
*pmd
, struct rxq_poll
**list
,
816 struct rxq_poll
*ret
, *poll
;
819 *n
= hmap_count(&pmd
->poll_list
);
823 ret
= xcalloc(*n
, sizeof *ret
);
825 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
832 qsort(ret
, *n
, sizeof *ret
, compare_poll_list
);
838 pmd_info_show_rxq(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
840 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
841 const char *prev_name
= NULL
;
842 struct rxq_poll
*list
;
846 "pmd thread numa_id %d core_id %u:\n\tisolated : %s\n",
847 pmd
->numa_id
, pmd
->core_id
, (pmd
->isolated
)
850 ovs_mutex_lock(&pmd
->port_mutex
);
851 sorted_poll_list(pmd
, &list
, &n
);
852 for (i
= 0; i
< n
; i
++) {
853 const char *name
= netdev_rxq_get_name(list
[i
].rxq
->rx
);
855 if (!prev_name
|| strcmp(name
, prev_name
)) {
857 ds_put_cstr(reply
, "\n");
859 ds_put_format(reply
, "\tport: %s\tqueue-id:", name
);
861 ds_put_format(reply
, " %d",
862 netdev_rxq_get_queue_id(list
[i
].rxq
->rx
));
865 ovs_mutex_unlock(&pmd
->port_mutex
);
866 ds_put_cstr(reply
, "\n");
872 dpif_netdev_pmd_info(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
875 struct ds reply
= DS_EMPTY_INITIALIZER
;
876 struct dp_netdev_pmd_thread
*pmd
;
877 struct dp_netdev
*dp
= NULL
;
878 enum pmd_info_type type
= *(enum pmd_info_type
*) aux
;
880 ovs_mutex_lock(&dp_netdev_mutex
);
883 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
884 } else if (shash_count(&dp_netdevs
) == 1) {
885 /* There's only one datapath */
886 dp
= shash_first(&dp_netdevs
)->data
;
890 ovs_mutex_unlock(&dp_netdev_mutex
);
891 unixctl_command_reply_error(conn
,
892 "please specify an existing datapath");
896 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
897 if (type
== PMD_INFO_SHOW_RXQ
) {
898 pmd_info_show_rxq(&reply
, pmd
);
900 unsigned long long stats
[DP_N_STATS
];
901 uint64_t cycles
[PMD_N_CYCLES
];
904 /* Read current stats and cycle counters */
905 for (i
= 0; i
< ARRAY_SIZE(stats
); i
++) {
906 atomic_read_relaxed(&pmd
->stats
.n
[i
], &stats
[i
]);
908 for (i
= 0; i
< ARRAY_SIZE(cycles
); i
++) {
909 atomic_read_relaxed(&pmd
->cycles
.n
[i
], &cycles
[i
]);
912 if (type
== PMD_INFO_CLEAR_STATS
) {
913 pmd_info_clear_stats(&reply
, pmd
, stats
, cycles
);
914 } else if (type
== PMD_INFO_SHOW_STATS
) {
915 pmd_info_show_stats(&reply
, pmd
, stats
, cycles
);
920 ovs_mutex_unlock(&dp_netdev_mutex
);
922 unixctl_command_reply(conn
, ds_cstr(&reply
));
927 dpif_netdev_init(void)
929 static enum pmd_info_type show_aux
= PMD_INFO_SHOW_STATS
,
930 clear_aux
= PMD_INFO_CLEAR_STATS
,
931 poll_aux
= PMD_INFO_SHOW_RXQ
;
933 unixctl_command_register("dpif-netdev/pmd-stats-show", "[dp]",
934 0, 1, dpif_netdev_pmd_info
,
936 unixctl_command_register("dpif-netdev/pmd-stats-clear", "[dp]",
937 0, 1, dpif_netdev_pmd_info
,
939 unixctl_command_register("dpif-netdev/pmd-rxq-show", "[dp]",
940 0, 1, dpif_netdev_pmd_info
,
946 dpif_netdev_enumerate(struct sset
*all_dps
,
947 const struct dpif_class
*dpif_class
)
949 struct shash_node
*node
;
951 ovs_mutex_lock(&dp_netdev_mutex
);
952 SHASH_FOR_EACH(node
, &dp_netdevs
) {
953 struct dp_netdev
*dp
= node
->data
;
954 if (dpif_class
!= dp
->class) {
955 /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs.
956 * If the class doesn't match, skip this dpif. */
959 sset_add(all_dps
, node
->name
);
961 ovs_mutex_unlock(&dp_netdev_mutex
);
967 dpif_netdev_class_is_dummy(const struct dpif_class
*class)
969 return class != &dpif_netdev_class
;
973 dpif_netdev_port_open_type(const struct dpif_class
*class, const char *type
)
975 return strcmp(type
, "internal") ? type
976 : dpif_netdev_class_is_dummy(class) ? "dummy-internal"
981 create_dpif_netdev(struct dp_netdev
*dp
)
983 uint16_t netflow_id
= hash_string(dp
->name
, 0);
984 struct dpif_netdev
*dpif
;
986 ovs_refcount_ref(&dp
->ref_cnt
);
988 dpif
= xmalloc(sizeof *dpif
);
989 dpif_init(&dpif
->dpif
, dp
->class, dp
->name
, netflow_id
>> 8, netflow_id
);
991 dpif
->last_port_seq
= seq_read(dp
->port_seq
);
996 /* Choose an unused, non-zero port number and return it on success.
997 * Return ODPP_NONE on failure. */
999 choose_port(struct dp_netdev
*dp
, const char *name
)
1000 OVS_REQUIRES(dp
->port_mutex
)
1004 if (dp
->class != &dpif_netdev_class
) {
1008 /* If the port name begins with "br", start the number search at
1009 * 100 to make writing tests easier. */
1010 if (!strncmp(name
, "br", 2)) {
1014 /* If the port name contains a number, try to assign that port number.
1015 * This can make writing unit tests easier because port numbers are
1017 for (p
= name
; *p
!= '\0'; p
++) {
1018 if (isdigit((unsigned char) *p
)) {
1019 port_no
= start_no
+ strtol(p
, NULL
, 10);
1020 if (port_no
> 0 && port_no
!= odp_to_u32(ODPP_NONE
)
1021 && !dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
1022 return u32_to_odp(port_no
);
1029 for (port_no
= 1; port_no
<= UINT16_MAX
; port_no
++) {
1030 if (!dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
1031 return u32_to_odp(port_no
);
1039 create_dp_netdev(const char *name
, const struct dpif_class
*class,
1040 struct dp_netdev
**dpp
)
1041 OVS_REQUIRES(dp_netdev_mutex
)
1043 struct dp_netdev
*dp
;
1046 dp
= xzalloc(sizeof *dp
);
1047 shash_add(&dp_netdevs
, name
, dp
);
1049 *CONST_CAST(const struct dpif_class
**, &dp
->class) = class;
1050 *CONST_CAST(const char **, &dp
->name
) = xstrdup(name
);
1051 ovs_refcount_init(&dp
->ref_cnt
);
1052 atomic_flag_clear(&dp
->destroyed
);
1054 ovs_mutex_init(&dp
->port_mutex
);
1055 hmap_init(&dp
->ports
);
1056 dp
->port_seq
= seq_create();
1057 fat_rwlock_init(&dp
->upcall_rwlock
);
1059 dp
->reconfigure_seq
= seq_create();
1060 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
1062 /* Disable upcalls by default. */
1063 dp_netdev_disable_upcall(dp
);
1064 dp
->upcall_aux
= NULL
;
1065 dp
->upcall_cb
= NULL
;
1067 conntrack_init(&dp
->conntrack
);
1069 cmap_init(&dp
->poll_threads
);
1070 ovs_mutex_init_recursive(&dp
->non_pmd_mutex
);
1071 ovsthread_key_create(&dp
->per_pmd_key
, NULL
);
1073 ovs_mutex_lock(&dp
->port_mutex
);
1074 dp_netdev_set_nonpmd(dp
);
1076 error
= do_add_port(dp
, name
, dpif_netdev_port_open_type(dp
->class,
1079 ovs_mutex_unlock(&dp
->port_mutex
);
1085 dp
->last_tnl_conf_seq
= seq_read(tnl_conf_seq
);
1091 dp_netdev_request_reconfigure(struct dp_netdev
*dp
)
1093 seq_change(dp
->reconfigure_seq
);
1097 dp_netdev_is_reconf_required(struct dp_netdev
*dp
)
1099 return seq_read(dp
->reconfigure_seq
) != dp
->last_reconfigure_seq
;
1103 dpif_netdev_open(const struct dpif_class
*class, const char *name
,
1104 bool create
, struct dpif
**dpifp
)
1106 struct dp_netdev
*dp
;
1109 ovs_mutex_lock(&dp_netdev_mutex
);
1110 dp
= shash_find_data(&dp_netdevs
, name
);
1112 error
= create
? create_dp_netdev(name
, class, &dp
) : ENODEV
;
1114 error
= (dp
->class != class ? EINVAL
1119 *dpifp
= create_dpif_netdev(dp
);
1122 ovs_mutex_unlock(&dp_netdev_mutex
);
1128 dp_netdev_destroy_upcall_lock(struct dp_netdev
*dp
)
1129 OVS_NO_THREAD_SAFETY_ANALYSIS
1131 /* Check that upcalls are disabled, i.e. that the rwlock is taken */
1132 ovs_assert(fat_rwlock_tryrdlock(&dp
->upcall_rwlock
));
1134 /* Before freeing a lock we should release it */
1135 fat_rwlock_unlock(&dp
->upcall_rwlock
);
1136 fat_rwlock_destroy(&dp
->upcall_rwlock
);
1139 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
1140 * through the 'dp_netdevs' shash while freeing 'dp'. */
1142 dp_netdev_free(struct dp_netdev
*dp
)
1143 OVS_REQUIRES(dp_netdev_mutex
)
1145 struct dp_netdev_port
*port
, *next
;
1147 shash_find_and_delete(&dp_netdevs
, dp
->name
);
1149 ovs_mutex_lock(&dp
->port_mutex
);
1150 HMAP_FOR_EACH_SAFE (port
, next
, node
, &dp
->ports
) {
1151 do_del_port(dp
, port
);
1153 ovs_mutex_unlock(&dp
->port_mutex
);
1154 dp_netdev_destroy_all_pmds(dp
, true);
1155 cmap_destroy(&dp
->poll_threads
);
1157 ovs_mutex_destroy(&dp
->non_pmd_mutex
);
1158 ovsthread_key_delete(dp
->per_pmd_key
);
1160 conntrack_destroy(&dp
->conntrack
);
1163 seq_destroy(dp
->reconfigure_seq
);
1165 seq_destroy(dp
->port_seq
);
1166 hmap_destroy(&dp
->ports
);
1167 ovs_mutex_destroy(&dp
->port_mutex
);
1169 /* Upcalls must be disabled at this point */
1170 dp_netdev_destroy_upcall_lock(dp
);
1172 free(dp
->pmd_cmask
);
1173 free(CONST_CAST(char *, dp
->name
));
1178 dp_netdev_unref(struct dp_netdev
*dp
)
1181 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
1182 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
1183 ovs_mutex_lock(&dp_netdev_mutex
);
1184 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1187 ovs_mutex_unlock(&dp_netdev_mutex
);
1192 dpif_netdev_close(struct dpif
*dpif
)
1194 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1196 dp_netdev_unref(dp
);
1201 dpif_netdev_destroy(struct dpif
*dpif
)
1203 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1205 if (!atomic_flag_test_and_set(&dp
->destroyed
)) {
1206 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1207 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
1215 /* Add 'n' to the atomic variable 'var' non-atomically and using relaxed
1216 * load/store semantics. While the increment is not atomic, the load and
1217 * store operations are, making it impossible to read inconsistent values.
1219 * This is used to update thread local stats counters. */
1221 non_atomic_ullong_add(atomic_ullong
*var
, unsigned long long n
)
1223 unsigned long long tmp
;
1225 atomic_read_relaxed(var
, &tmp
);
1227 atomic_store_relaxed(var
, tmp
);
1231 dpif_netdev_get_stats(const struct dpif
*dpif
, struct dpif_dp_stats
*stats
)
1233 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1234 struct dp_netdev_pmd_thread
*pmd
;
1236 stats
->n_flows
= stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
1237 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1238 unsigned long long n
;
1239 stats
->n_flows
+= cmap_count(&pmd
->flow_table
);
1241 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_MASKED_HIT
], &n
);
1243 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_EXACT_HIT
], &n
);
1245 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_MISS
], &n
);
1246 stats
->n_missed
+= n
;
1247 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_LOST
], &n
);
1250 stats
->n_masks
= UINT32_MAX
;
1251 stats
->n_mask_hit
= UINT64_MAX
;
1257 dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread
*pmd
)
1259 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
1260 ovs_mutex_lock(&pmd
->dp
->non_pmd_mutex
);
1261 ovs_mutex_lock(&pmd
->port_mutex
);
1262 pmd_load_cached_ports(pmd
);
1263 ovs_mutex_unlock(&pmd
->port_mutex
);
1264 ovs_mutex_unlock(&pmd
->dp
->non_pmd_mutex
);
1268 ovs_mutex_lock(&pmd
->cond_mutex
);
1269 seq_change(pmd
->reload_seq
);
1270 atomic_store_relaxed(&pmd
->reload
, true);
1271 ovs_mutex_cond_wait(&pmd
->cond
, &pmd
->cond_mutex
);
1272 ovs_mutex_unlock(&pmd
->cond_mutex
);
1276 hash_port_no(odp_port_t port_no
)
1278 return hash_int(odp_to_u32(port_no
), 0);
1282 port_create(const char *devname
, const char *type
,
1283 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1285 struct netdev_saved_flags
*sf
;
1286 struct dp_netdev_port
*port
;
1287 enum netdev_flags flags
;
1288 struct netdev
*netdev
;
1293 /* Open and validate network device. */
1294 error
= netdev_open(devname
, type
, &netdev
);
1298 /* XXX reject non-Ethernet devices */
1300 netdev_get_flags(netdev
, &flags
);
1301 if (flags
& NETDEV_LOOPBACK
) {
1302 VLOG_ERR("%s: cannot add a loopback device", devname
);
1307 error
= netdev_turn_flags_on(netdev
, NETDEV_PROMISC
, &sf
);
1309 VLOG_ERR("%s: cannot set promisc flag", devname
);
1313 port
= xzalloc(sizeof *port
);
1314 port
->port_no
= port_no
;
1315 port
->netdev
= netdev
;
1316 port
->type
= xstrdup(type
);
1318 port
->need_reconfigure
= true;
1319 ovs_mutex_init(&port
->txq_used_mutex
);
1326 netdev_close(netdev
);
1331 do_add_port(struct dp_netdev
*dp
, const char *devname
, const char *type
,
1333 OVS_REQUIRES(dp
->port_mutex
)
1335 struct dp_netdev_port
*port
;
1338 /* Reject devices already in 'dp'. */
1339 if (!get_port_by_name(dp
, devname
, &port
)) {
1343 error
= port_create(devname
, type
, port_no
, &port
);
1348 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
1349 seq_change(dp
->port_seq
);
1351 reconfigure_datapath(dp
);
1357 dpif_netdev_port_add(struct dpif
*dpif
, struct netdev
*netdev
,
1358 odp_port_t
*port_nop
)
1360 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1361 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1362 const char *dpif_port
;
1366 ovs_mutex_lock(&dp
->port_mutex
);
1367 dpif_port
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1368 if (*port_nop
!= ODPP_NONE
) {
1369 port_no
= *port_nop
;
1370 error
= dp_netdev_lookup_port(dp
, *port_nop
) ? EBUSY
: 0;
1372 port_no
= choose_port(dp
, dpif_port
);
1373 error
= port_no
== ODPP_NONE
? EFBIG
: 0;
1376 *port_nop
= port_no
;
1377 error
= do_add_port(dp
, dpif_port
, netdev_get_type(netdev
), port_no
);
1379 ovs_mutex_unlock(&dp
->port_mutex
);
1385 dpif_netdev_port_del(struct dpif
*dpif
, odp_port_t port_no
)
1387 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1390 ovs_mutex_lock(&dp
->port_mutex
);
1391 if (port_no
== ODPP_LOCAL
) {
1394 struct dp_netdev_port
*port
;
1396 error
= get_port_by_number(dp
, port_no
, &port
);
1398 do_del_port(dp
, port
);
1401 ovs_mutex_unlock(&dp
->port_mutex
);
1407 is_valid_port_number(odp_port_t port_no
)
1409 return port_no
!= ODPP_NONE
;
1412 static struct dp_netdev_port
*
1413 dp_netdev_lookup_port(const struct dp_netdev
*dp
, odp_port_t port_no
)
1414 OVS_REQUIRES(dp
->port_mutex
)
1416 struct dp_netdev_port
*port
;
1418 HMAP_FOR_EACH_WITH_HASH (port
, node
, hash_port_no(port_no
), &dp
->ports
) {
1419 if (port
->port_no
== port_no
) {
1427 get_port_by_number(struct dp_netdev
*dp
,
1428 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1429 OVS_REQUIRES(dp
->port_mutex
)
1431 if (!is_valid_port_number(port_no
)) {
1435 *portp
= dp_netdev_lookup_port(dp
, port_no
);
1436 return *portp
? 0 : ENODEV
;
1441 port_destroy(struct dp_netdev_port
*port
)
1447 netdev_close(port
->netdev
);
1448 netdev_restore_flags(port
->sf
);
1450 for (unsigned i
= 0; i
< port
->n_rxq
; i
++) {
1451 netdev_rxq_close(port
->rxqs
[i
].rx
);
1453 ovs_mutex_destroy(&port
->txq_used_mutex
);
1454 free(port
->rxq_affinity_list
);
1455 free(port
->txq_used
);
1462 get_port_by_name(struct dp_netdev
*dp
,
1463 const char *devname
, struct dp_netdev_port
**portp
)
1464 OVS_REQUIRES(dp
->port_mutex
)
1466 struct dp_netdev_port
*port
;
1468 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1469 if (!strcmp(netdev_get_name(port
->netdev
), devname
)) {
1475 /* Callers of dpif_netdev_port_query_by_name() expect ENODEV for a non
1480 /* Returns 'true' if there is a port with pmd netdev. */
1482 has_pmd_port(struct dp_netdev
*dp
)
1483 OVS_REQUIRES(dp
->port_mutex
)
1485 struct dp_netdev_port
*port
;
1487 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1488 if (netdev_is_pmd(port
->netdev
)) {
1497 do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*port
)
1498 OVS_REQUIRES(dp
->port_mutex
)
1500 hmap_remove(&dp
->ports
, &port
->node
);
1501 seq_change(dp
->port_seq
);
1503 reconfigure_datapath(dp
);
1509 answer_port_query(const struct dp_netdev_port
*port
,
1510 struct dpif_port
*dpif_port
)
1512 dpif_port
->name
= xstrdup(netdev_get_name(port
->netdev
));
1513 dpif_port
->type
= xstrdup(port
->type
);
1514 dpif_port
->port_no
= port
->port_no
;
1518 dpif_netdev_port_query_by_number(const struct dpif
*dpif
, odp_port_t port_no
,
1519 struct dpif_port
*dpif_port
)
1521 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1522 struct dp_netdev_port
*port
;
1525 ovs_mutex_lock(&dp
->port_mutex
);
1526 error
= get_port_by_number(dp
, port_no
, &port
);
1527 if (!error
&& dpif_port
) {
1528 answer_port_query(port
, dpif_port
);
1530 ovs_mutex_unlock(&dp
->port_mutex
);
1536 dpif_netdev_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
1537 struct dpif_port
*dpif_port
)
1539 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1540 struct dp_netdev_port
*port
;
1543 ovs_mutex_lock(&dp
->port_mutex
);
1544 error
= get_port_by_name(dp
, devname
, &port
);
1545 if (!error
&& dpif_port
) {
1546 answer_port_query(port
, dpif_port
);
1548 ovs_mutex_unlock(&dp
->port_mutex
);
1554 dp_netdev_flow_free(struct dp_netdev_flow
*flow
)
1556 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow
));
1560 static void dp_netdev_flow_unref(struct dp_netdev_flow
*flow
)
1562 if (ovs_refcount_unref_relaxed(&flow
->ref_cnt
) == 1) {
1563 ovsrcu_postpone(dp_netdev_flow_free
, flow
);
1568 dp_netdev_flow_hash(const ovs_u128
*ufid
)
1570 return ufid
->u32
[0];
1573 static inline struct dpcls
*
1574 dp_netdev_pmd_lookup_dpcls(struct dp_netdev_pmd_thread
*pmd
,
1578 uint32_t hash
= hash_port_no(in_port
);
1579 CMAP_FOR_EACH_WITH_HASH (cls
, node
, hash
, &pmd
->classifiers
) {
1580 if (cls
->in_port
== in_port
) {
1581 /* Port classifier exists already */
1588 static inline struct dpcls
*
1589 dp_netdev_pmd_find_dpcls(struct dp_netdev_pmd_thread
*pmd
,
1591 OVS_REQUIRES(pmd
->flow_mutex
)
1593 struct dpcls
*cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
1594 uint32_t hash
= hash_port_no(in_port
);
1597 /* Create new classifier for in_port */
1598 cls
= xmalloc(sizeof(*cls
));
1600 cls
->in_port
= in_port
;
1601 cmap_insert(&pmd
->classifiers
, &cls
->node
, hash
);
1602 VLOG_DBG("Creating dpcls %p for in_port %d", cls
, in_port
);
1608 dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread
*pmd
,
1609 struct dp_netdev_flow
*flow
)
1610 OVS_REQUIRES(pmd
->flow_mutex
)
1612 struct cmap_node
*node
= CONST_CAST(struct cmap_node
*, &flow
->node
);
1614 odp_port_t in_port
= flow
->flow
.in_port
.odp_port
;
1616 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
1617 ovs_assert(cls
!= NULL
);
1618 dpcls_remove(cls
, &flow
->cr
);
1619 cmap_remove(&pmd
->flow_table
, node
, dp_netdev_flow_hash(&flow
->ufid
));
1622 dp_netdev_flow_unref(flow
);
1626 dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
)
1628 struct dp_netdev_flow
*netdev_flow
;
1630 ovs_mutex_lock(&pmd
->flow_mutex
);
1631 CMAP_FOR_EACH (netdev_flow
, node
, &pmd
->flow_table
) {
1632 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
1634 ovs_mutex_unlock(&pmd
->flow_mutex
);
1638 dpif_netdev_flow_flush(struct dpif
*dpif
)
1640 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1641 struct dp_netdev_pmd_thread
*pmd
;
1643 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1644 dp_netdev_pmd_flow_flush(pmd
);
1650 struct dp_netdev_port_state
{
1651 struct hmap_position position
;
1656 dpif_netdev_port_dump_start(const struct dpif
*dpif OVS_UNUSED
, void **statep
)
1658 *statep
= xzalloc(sizeof(struct dp_netdev_port_state
));
1663 dpif_netdev_port_dump_next(const struct dpif
*dpif
, void *state_
,
1664 struct dpif_port
*dpif_port
)
1666 struct dp_netdev_port_state
*state
= state_
;
1667 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1668 struct hmap_node
*node
;
1671 ovs_mutex_lock(&dp
->port_mutex
);
1672 node
= hmap_at_position(&dp
->ports
, &state
->position
);
1674 struct dp_netdev_port
*port
;
1676 port
= CONTAINER_OF(node
, struct dp_netdev_port
, node
);
1679 state
->name
= xstrdup(netdev_get_name(port
->netdev
));
1680 dpif_port
->name
= state
->name
;
1681 dpif_port
->type
= port
->type
;
1682 dpif_port
->port_no
= port
->port_no
;
1688 ovs_mutex_unlock(&dp
->port_mutex
);
1694 dpif_netdev_port_dump_done(const struct dpif
*dpif OVS_UNUSED
, void *state_
)
1696 struct dp_netdev_port_state
*state
= state_
;
1703 dpif_netdev_port_poll(const struct dpif
*dpif_
, char **devnamep OVS_UNUSED
)
1705 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
1706 uint64_t new_port_seq
;
1709 new_port_seq
= seq_read(dpif
->dp
->port_seq
);
1710 if (dpif
->last_port_seq
!= new_port_seq
) {
1711 dpif
->last_port_seq
= new_port_seq
;
1721 dpif_netdev_port_poll_wait(const struct dpif
*dpif_
)
1723 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
1725 seq_wait(dpif
->dp
->port_seq
, dpif
->last_port_seq
);
1728 static struct dp_netdev_flow
*
1729 dp_netdev_flow_cast(const struct dpcls_rule
*cr
)
1731 return cr
? CONTAINER_OF(cr
, struct dp_netdev_flow
, cr
) : NULL
;
1734 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*flow
)
1736 return ovs_refcount_try_ref_rcu(&flow
->ref_cnt
);
1739 /* netdev_flow_key utilities.
1741 * netdev_flow_key is basically a miniflow. We use these functions
1742 * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow
1743 * functions (miniflow_clone_inline, miniflow_equal, ...), because:
1745 * - Since we are dealing exclusively with miniflows created by
1746 * miniflow_extract(), if the map is different the miniflow is different.
1747 * Therefore we can be faster by comparing the map and the miniflow in a
1749 * - These functions can be inlined by the compiler. */
1751 /* Given the number of bits set in miniflow's maps, returns the size of the
1752 * 'netdev_flow_key.mf' */
1753 static inline size_t
1754 netdev_flow_key_size(size_t flow_u64s
)
1756 return sizeof(struct miniflow
) + MINIFLOW_VALUES_SIZE(flow_u64s
);
1760 netdev_flow_key_equal(const struct netdev_flow_key
*a
,
1761 const struct netdev_flow_key
*b
)
1763 /* 'b->len' may be not set yet. */
1764 return a
->hash
== b
->hash
&& !memcmp(&a
->mf
, &b
->mf
, a
->len
);
1767 /* Used to compare 'netdev_flow_key' in the exact match cache to a miniflow.
1768 * The maps are compared bitwise, so both 'key->mf' and 'mf' must have been
1769 * generated by miniflow_extract. */
1771 netdev_flow_key_equal_mf(const struct netdev_flow_key
*key
,
1772 const struct miniflow
*mf
)
1774 return !memcmp(&key
->mf
, mf
, key
->len
);
1778 netdev_flow_key_clone(struct netdev_flow_key
*dst
,
1779 const struct netdev_flow_key
*src
)
1782 offsetof(struct netdev_flow_key
, mf
) + src
->len
);
1787 netdev_flow_key_from_flow(struct netdev_flow_key
*dst
,
1788 const struct flow
*src
)
1790 struct dp_packet packet
;
1791 uint64_t buf_stub
[512 / 8];
1793 dp_packet_use_stub(&packet
, buf_stub
, sizeof buf_stub
);
1794 pkt_metadata_from_flow(&packet
.md
, src
);
1795 flow_compose(&packet
, src
);
1796 miniflow_extract(&packet
, &dst
->mf
);
1797 dp_packet_uninit(&packet
);
1799 dst
->len
= netdev_flow_key_size(miniflow_n_values(&dst
->mf
));
1800 dst
->hash
= 0; /* Not computed yet. */
1803 /* Initialize a netdev_flow_key 'mask' from 'match'. */
1805 netdev_flow_mask_init(struct netdev_flow_key
*mask
,
1806 const struct match
*match
)
1808 uint64_t *dst
= miniflow_values(&mask
->mf
);
1809 struct flowmap fmap
;
1813 /* Only check masks that make sense for the flow. */
1814 flow_wc_map(&match
->flow
, &fmap
);
1815 flowmap_init(&mask
->mf
.map
);
1817 FLOWMAP_FOR_EACH_INDEX(idx
, fmap
) {
1818 uint64_t mask_u64
= flow_u64_value(&match
->wc
.masks
, idx
);
1821 flowmap_set(&mask
->mf
.map
, idx
, 1);
1823 hash
= hash_add64(hash
, mask_u64
);
1829 FLOWMAP_FOR_EACH_MAP (map
, mask
->mf
.map
) {
1830 hash
= hash_add64(hash
, map
);
1833 size_t n
= dst
- miniflow_get_values(&mask
->mf
);
1835 mask
->hash
= hash_finish(hash
, n
* 8);
1836 mask
->len
= netdev_flow_key_size(n
);
1839 /* Initializes 'dst' as a copy of 'flow' masked with 'mask'. */
1841 netdev_flow_key_init_masked(struct netdev_flow_key
*dst
,
1842 const struct flow
*flow
,
1843 const struct netdev_flow_key
*mask
)
1845 uint64_t *dst_u64
= miniflow_values(&dst
->mf
);
1846 const uint64_t *mask_u64
= miniflow_get_values(&mask
->mf
);
1850 dst
->len
= mask
->len
;
1851 dst
->mf
= mask
->mf
; /* Copy maps. */
1853 FLOW_FOR_EACH_IN_MAPS(value
, flow
, mask
->mf
.map
) {
1854 *dst_u64
= value
& *mask_u64
++;
1855 hash
= hash_add64(hash
, *dst_u64
++);
1857 dst
->hash
= hash_finish(hash
,
1858 (dst_u64
- miniflow_get_values(&dst
->mf
)) * 8);
1861 /* Iterate through netdev_flow_key TNL u64 values specified by 'FLOWMAP'. */
1862 #define NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(VALUE, KEY, FLOWMAP) \
1863 MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, &(KEY)->mf, FLOWMAP)
1865 /* Returns a hash value for the bits of 'key' where there are 1-bits in
1867 static inline uint32_t
1868 netdev_flow_key_hash_in_mask(const struct netdev_flow_key
*key
,
1869 const struct netdev_flow_key
*mask
)
1871 const uint64_t *p
= miniflow_get_values(&mask
->mf
);
1875 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, key
, mask
->mf
.map
) {
1876 hash
= hash_add64(hash
, value
& *p
++);
1879 return hash_finish(hash
, (p
- miniflow_get_values(&mask
->mf
)) * 8);
1883 emc_entry_alive(struct emc_entry
*ce
)
1885 return ce
->flow
&& !ce
->flow
->dead
;
1889 emc_clear_entry(struct emc_entry
*ce
)
1892 dp_netdev_flow_unref(ce
->flow
);
1898 emc_change_entry(struct emc_entry
*ce
, struct dp_netdev_flow
*flow
,
1899 const struct netdev_flow_key
*key
)
1901 if (ce
->flow
!= flow
) {
1903 dp_netdev_flow_unref(ce
->flow
);
1906 if (dp_netdev_flow_ref(flow
)) {
1913 netdev_flow_key_clone(&ce
->key
, key
);
1918 emc_insert(struct emc_cache
*cache
, const struct netdev_flow_key
*key
,
1919 struct dp_netdev_flow
*flow
)
1921 struct emc_entry
*to_be_replaced
= NULL
;
1922 struct emc_entry
*current_entry
;
1924 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
1925 if (netdev_flow_key_equal(¤t_entry
->key
, key
)) {
1926 /* We found the entry with the 'mf' miniflow */
1927 emc_change_entry(current_entry
, flow
, NULL
);
1931 /* Replacement policy: put the flow in an empty (not alive) entry, or
1932 * in the first entry where it can be */
1934 || (emc_entry_alive(to_be_replaced
)
1935 && !emc_entry_alive(current_entry
))
1936 || current_entry
->key
.hash
< to_be_replaced
->key
.hash
) {
1937 to_be_replaced
= current_entry
;
1940 /* We didn't find the miniflow in the cache.
1941 * The 'to_be_replaced' entry is where the new flow will be stored */
1943 emc_change_entry(to_be_replaced
, flow
, key
);
1946 static inline struct dp_netdev_flow
*
1947 emc_lookup(struct emc_cache
*cache
, const struct netdev_flow_key
*key
)
1949 struct emc_entry
*current_entry
;
1951 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
1952 if (current_entry
->key
.hash
== key
->hash
1953 && emc_entry_alive(current_entry
)
1954 && netdev_flow_key_equal_mf(¤t_entry
->key
, &key
->mf
)) {
1956 /* We found the entry with the 'key->mf' miniflow */
1957 return current_entry
->flow
;
1964 static struct dp_netdev_flow
*
1965 dp_netdev_pmd_lookup_flow(struct dp_netdev_pmd_thread
*pmd
,
1966 const struct netdev_flow_key
*key
,
1970 struct dpcls_rule
*rule
;
1971 odp_port_t in_port
= u32_to_odp(MINIFLOW_GET_U32(&key
->mf
, in_port
));
1972 struct dp_netdev_flow
*netdev_flow
= NULL
;
1974 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
1975 if (OVS_LIKELY(cls
)) {
1976 dpcls_lookup(cls
, key
, &rule
, 1, lookup_num_p
);
1977 netdev_flow
= dp_netdev_flow_cast(rule
);
1982 static struct dp_netdev_flow
*
1983 dp_netdev_pmd_find_flow(const struct dp_netdev_pmd_thread
*pmd
,
1984 const ovs_u128
*ufidp
, const struct nlattr
*key
,
1987 struct dp_netdev_flow
*netdev_flow
;
1991 /* If a UFID is not provided, determine one based on the key. */
1992 if (!ufidp
&& key
&& key_len
1993 && !dpif_netdev_flow_from_nlattrs(key
, key_len
, &flow
)) {
1994 dpif_flow_hash(pmd
->dp
->dpif
, &flow
, sizeof flow
, &ufid
);
1999 CMAP_FOR_EACH_WITH_HASH (netdev_flow
, node
, dp_netdev_flow_hash(ufidp
),
2001 if (ovs_u128_equals(netdev_flow
->ufid
, *ufidp
)) {
2011 get_dpif_flow_stats(const struct dp_netdev_flow
*netdev_flow_
,
2012 struct dpif_flow_stats
*stats
)
2014 struct dp_netdev_flow
*netdev_flow
;
2015 unsigned long long n
;
2019 netdev_flow
= CONST_CAST(struct dp_netdev_flow
*, netdev_flow_
);
2021 atomic_read_relaxed(&netdev_flow
->stats
.packet_count
, &n
);
2022 stats
->n_packets
= n
;
2023 atomic_read_relaxed(&netdev_flow
->stats
.byte_count
, &n
);
2025 atomic_read_relaxed(&netdev_flow
->stats
.used
, &used
);
2027 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
2028 stats
->tcp_flags
= flags
;
2031 /* Converts to the dpif_flow format, using 'key_buf' and 'mask_buf' for
2032 * storing the netlink-formatted key/mask. 'key_buf' may be the same as
2033 * 'mask_buf'. Actions will be returned without copying, by relying on RCU to
2036 dp_netdev_flow_to_dpif_flow(const struct dp_netdev_flow
*netdev_flow
,
2037 struct ofpbuf
*key_buf
, struct ofpbuf
*mask_buf
,
2038 struct dpif_flow
*flow
, bool terse
)
2041 memset(flow
, 0, sizeof *flow
);
2043 struct flow_wildcards wc
;
2044 struct dp_netdev_actions
*actions
;
2046 struct odp_flow_key_parms odp_parms
= {
2047 .flow
= &netdev_flow
->flow
,
2049 .support
= dp_netdev_support
,
2052 miniflow_expand(&netdev_flow
->cr
.mask
->mf
, &wc
.masks
);
2053 /* in_port is exact matched, but we have left it out from the mask for
2054 * optimnization reasons. Add in_port back to the mask. */
2055 wc
.masks
.in_port
.odp_port
= ODPP_NONE
;
2058 offset
= key_buf
->size
;
2059 flow
->key
= ofpbuf_tail(key_buf
);
2060 odp_flow_key_from_flow(&odp_parms
, key_buf
);
2061 flow
->key_len
= key_buf
->size
- offset
;
2064 offset
= mask_buf
->size
;
2065 flow
->mask
= ofpbuf_tail(mask_buf
);
2066 odp_parms
.key_buf
= key_buf
;
2067 odp_flow_key_from_mask(&odp_parms
, mask_buf
);
2068 flow
->mask_len
= mask_buf
->size
- offset
;
2071 actions
= dp_netdev_flow_get_actions(netdev_flow
);
2072 flow
->actions
= actions
->actions
;
2073 flow
->actions_len
= actions
->size
;
2076 flow
->ufid
= netdev_flow
->ufid
;
2077 flow
->ufid_present
= true;
2078 flow
->pmd_id
= netdev_flow
->pmd_id
;
2079 get_dpif_flow_stats(netdev_flow
, &flow
->stats
);
2083 dpif_netdev_mask_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
2084 const struct nlattr
*mask_key
,
2085 uint32_t mask_key_len
, const struct flow
*flow
,
2086 struct flow_wildcards
*wc
)
2088 enum odp_key_fitness fitness
;
2090 fitness
= odp_flow_key_to_mask(mask_key
, mask_key_len
, wc
, flow
);
2092 /* This should not happen: it indicates that
2093 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
2094 * disagree on the acceptable form of a mask. Log the problem
2095 * as an error, with enough details to enable debugging. */
2096 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2098 if (!VLOG_DROP_ERR(&rl
)) {
2102 odp_flow_format(key
, key_len
, mask_key
, mask_key_len
, NULL
, &s
,
2104 VLOG_ERR("internal error parsing flow mask %s (%s)",
2105 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
2116 dpif_netdev_flow_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
2121 if (odp_flow_key_to_flow(key
, key_len
, flow
)) {
2122 /* This should not happen: it indicates that odp_flow_key_from_flow()
2123 * and odp_flow_key_to_flow() disagree on the acceptable form of a
2124 * flow. Log the problem as an error, with enough details to enable
2126 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2128 if (!VLOG_DROP_ERR(&rl
)) {
2132 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
2133 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
2140 in_port
= flow
->in_port
.odp_port
;
2141 if (!is_valid_port_number(in_port
) && in_port
!= ODPP_NONE
) {
2145 if (flow
->ct_state
& DP_NETDEV_CS_UNSUPPORTED_MASK
) {
2153 dpif_netdev_flow_get(const struct dpif
*dpif
, const struct dpif_flow_get
*get
)
2155 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2156 struct dp_netdev_flow
*netdev_flow
;
2157 struct dp_netdev_pmd_thread
*pmd
;
2158 struct hmapx to_find
= HMAPX_INITIALIZER(&to_find
);
2159 struct hmapx_node
*node
;
2162 if (get
->pmd_id
== PMD_ID_NULL
) {
2163 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2164 if (dp_netdev_pmd_try_ref(pmd
) && !hmapx_add(&to_find
, pmd
)) {
2165 dp_netdev_pmd_unref(pmd
);
2169 pmd
= dp_netdev_get_pmd(dp
, get
->pmd_id
);
2173 hmapx_add(&to_find
, pmd
);
2176 if (!hmapx_count(&to_find
)) {
2180 HMAPX_FOR_EACH (node
, &to_find
) {
2181 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
2182 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, get
->ufid
, get
->key
,
2185 dp_netdev_flow_to_dpif_flow(netdev_flow
, get
->buffer
, get
->buffer
,
2194 HMAPX_FOR_EACH (node
, &to_find
) {
2195 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
2196 dp_netdev_pmd_unref(pmd
);
2199 hmapx_destroy(&to_find
);
2203 static struct dp_netdev_flow
*
2204 dp_netdev_flow_add(struct dp_netdev_pmd_thread
*pmd
,
2205 struct match
*match
, const ovs_u128
*ufid
,
2206 const struct nlattr
*actions
, size_t actions_len
)
2207 OVS_REQUIRES(pmd
->flow_mutex
)
2209 struct dp_netdev_flow
*flow
;
2210 struct netdev_flow_key mask
;
2213 /* Make sure in_port is exact matched before we read it. */
2214 ovs_assert(match
->wc
.masks
.in_port
.odp_port
== ODPP_NONE
);
2215 odp_port_t in_port
= match
->flow
.in_port
.odp_port
;
2217 /* As we select the dpcls based on the port number, each netdev flow
2218 * belonging to the same dpcls will have the same odp_port value.
2219 * For performance reasons we wildcard odp_port here in the mask. In the
2220 * typical case dp_hash is also wildcarded, and the resulting 8-byte
2221 * chunk {dp_hash, in_port} will be ignored by netdev_flow_mask_init() and
2222 * will not be part of the subtable mask.
2223 * This will speed up the hash computation during dpcls_lookup() because
2224 * there is one less call to hash_add64() in this case. */
2225 match
->wc
.masks
.in_port
.odp_port
= 0;
2226 netdev_flow_mask_init(&mask
, match
);
2227 match
->wc
.masks
.in_port
.odp_port
= ODPP_NONE
;
2229 /* Make sure wc does not have metadata. */
2230 ovs_assert(!FLOWMAP_HAS_FIELD(&mask
.mf
.map
, metadata
)
2231 && !FLOWMAP_HAS_FIELD(&mask
.mf
.map
, regs
));
2233 /* Do not allocate extra space. */
2234 flow
= xmalloc(sizeof *flow
- sizeof flow
->cr
.flow
.mf
+ mask
.len
);
2235 memset(&flow
->stats
, 0, sizeof flow
->stats
);
2238 *CONST_CAST(unsigned *, &flow
->pmd_id
) = pmd
->core_id
;
2239 *CONST_CAST(struct flow
*, &flow
->flow
) = match
->flow
;
2240 *CONST_CAST(ovs_u128
*, &flow
->ufid
) = *ufid
;
2241 ovs_refcount_init(&flow
->ref_cnt
);
2242 ovsrcu_set(&flow
->actions
, dp_netdev_actions_create(actions
, actions_len
));
2244 netdev_flow_key_init_masked(&flow
->cr
.flow
, &match
->flow
, &mask
);
2246 /* Select dpcls for in_port. Relies on in_port to be exact match. */
2247 cls
= dp_netdev_pmd_find_dpcls(pmd
, in_port
);
2248 dpcls_insert(cls
, &flow
->cr
, &mask
);
2250 cmap_insert(&pmd
->flow_table
, CONST_CAST(struct cmap_node
*, &flow
->node
),
2251 dp_netdev_flow_hash(&flow
->ufid
));
2253 if (OVS_UNLIKELY(VLOG_IS_DBG_ENABLED())) {
2254 struct ds ds
= DS_EMPTY_INITIALIZER
;
2255 struct ofpbuf key_buf
, mask_buf
;
2256 struct odp_flow_key_parms odp_parms
= {
2257 .flow
= &match
->flow
,
2258 .mask
= &match
->wc
.masks
,
2259 .support
= dp_netdev_support
,
2262 ofpbuf_init(&key_buf
, 0);
2263 ofpbuf_init(&mask_buf
, 0);
2265 odp_flow_key_from_flow(&odp_parms
, &key_buf
);
2266 odp_parms
.key_buf
= &key_buf
;
2267 odp_flow_key_from_mask(&odp_parms
, &mask_buf
);
2269 ds_put_cstr(&ds
, "flow_add: ");
2270 odp_format_ufid(ufid
, &ds
);
2271 ds_put_cstr(&ds
, " ");
2272 odp_flow_format(key_buf
.data
, key_buf
.size
,
2273 mask_buf
.data
, mask_buf
.size
,
2275 ds_put_cstr(&ds
, ", actions:");
2276 format_odp_actions(&ds
, actions
, actions_len
);
2278 VLOG_DBG_RL(&upcall_rl
, "%s", ds_cstr(&ds
));
2280 ofpbuf_uninit(&key_buf
);
2281 ofpbuf_uninit(&mask_buf
);
2289 flow_put_on_pmd(struct dp_netdev_pmd_thread
*pmd
,
2290 struct netdev_flow_key
*key
,
2291 struct match
*match
,
2293 const struct dpif_flow_put
*put
,
2294 struct dpif_flow_stats
*stats
)
2296 struct dp_netdev_flow
*netdev_flow
;
2300 memset(stats
, 0, sizeof *stats
);
2303 ovs_mutex_lock(&pmd
->flow_mutex
);
2304 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
, NULL
);
2306 if (put
->flags
& DPIF_FP_CREATE
) {
2307 if (cmap_count(&pmd
->flow_table
) < MAX_FLOWS
) {
2308 dp_netdev_flow_add(pmd
, match
, ufid
, put
->actions
,
2318 if (put
->flags
& DPIF_FP_MODIFY
2319 && flow_equal(&match
->flow
, &netdev_flow
->flow
)) {
2320 struct dp_netdev_actions
*new_actions
;
2321 struct dp_netdev_actions
*old_actions
;
2323 new_actions
= dp_netdev_actions_create(put
->actions
,
2326 old_actions
= dp_netdev_flow_get_actions(netdev_flow
);
2327 ovsrcu_set(&netdev_flow
->actions
, new_actions
);
2330 get_dpif_flow_stats(netdev_flow
, stats
);
2332 if (put
->flags
& DPIF_FP_ZERO_STATS
) {
2333 /* XXX: The userspace datapath uses thread local statistics
2334 * (for flows), which should be updated only by the owning
2335 * thread. Since we cannot write on stats memory here,
2336 * we choose not to support this flag. Please note:
2337 * - This feature is currently used only by dpctl commands with
2339 * - Should the need arise, this operation can be implemented
2340 * by keeping a base value (to be update here) for each
2341 * counter, and subtracting it before outputting the stats */
2345 ovsrcu_postpone(dp_netdev_actions_free
, old_actions
);
2346 } else if (put
->flags
& DPIF_FP_CREATE
) {
2349 /* Overlapping flow. */
2353 ovs_mutex_unlock(&pmd
->flow_mutex
);
2358 dpif_netdev_flow_put(struct dpif
*dpif
, const struct dpif_flow_put
*put
)
2360 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2361 struct netdev_flow_key key
;
2362 struct dp_netdev_pmd_thread
*pmd
;
2368 memset(put
->stats
, 0, sizeof *put
->stats
);
2370 error
= dpif_netdev_flow_from_nlattrs(put
->key
, put
->key_len
, &match
.flow
);
2374 error
= dpif_netdev_mask_from_nlattrs(put
->key
, put
->key_len
,
2375 put
->mask
, put
->mask_len
,
2376 &match
.flow
, &match
.wc
);
2384 dpif_flow_hash(dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
2387 /* Must produce a netdev_flow_key for lookup.
2388 * This interface is no longer performance critical, since it is not used
2389 * for upcall processing any more. */
2390 netdev_flow_key_from_flow(&key
, &match
.flow
);
2392 if (put
->pmd_id
== PMD_ID_NULL
) {
2393 if (cmap_count(&dp
->poll_threads
) == 0) {
2396 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2397 struct dpif_flow_stats pmd_stats
;
2400 pmd_error
= flow_put_on_pmd(pmd
, &key
, &match
, &ufid
, put
,
2404 } else if (put
->stats
) {
2405 put
->stats
->n_packets
+= pmd_stats
.n_packets
;
2406 put
->stats
->n_bytes
+= pmd_stats
.n_bytes
;
2407 put
->stats
->used
= MAX(put
->stats
->used
, pmd_stats
.used
);
2408 put
->stats
->tcp_flags
|= pmd_stats
.tcp_flags
;
2412 pmd
= dp_netdev_get_pmd(dp
, put
->pmd_id
);
2416 error
= flow_put_on_pmd(pmd
, &key
, &match
, &ufid
, put
, put
->stats
);
2417 dp_netdev_pmd_unref(pmd
);
2424 flow_del_on_pmd(struct dp_netdev_pmd_thread
*pmd
,
2425 struct dpif_flow_stats
*stats
,
2426 const struct dpif_flow_del
*del
)
2428 struct dp_netdev_flow
*netdev_flow
;
2431 ovs_mutex_lock(&pmd
->flow_mutex
);
2432 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, del
->ufid
, del
->key
,
2436 get_dpif_flow_stats(netdev_flow
, stats
);
2438 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
2442 ovs_mutex_unlock(&pmd
->flow_mutex
);
2448 dpif_netdev_flow_del(struct dpif
*dpif
, const struct dpif_flow_del
*del
)
2450 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2451 struct dp_netdev_pmd_thread
*pmd
;
2455 memset(del
->stats
, 0, sizeof *del
->stats
);
2458 if (del
->pmd_id
== PMD_ID_NULL
) {
2459 if (cmap_count(&dp
->poll_threads
) == 0) {
2462 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2463 struct dpif_flow_stats pmd_stats
;
2466 pmd_error
= flow_del_on_pmd(pmd
, &pmd_stats
, del
);
2469 } else if (del
->stats
) {
2470 del
->stats
->n_packets
+= pmd_stats
.n_packets
;
2471 del
->stats
->n_bytes
+= pmd_stats
.n_bytes
;
2472 del
->stats
->used
= MAX(del
->stats
->used
, pmd_stats
.used
);
2473 del
->stats
->tcp_flags
|= pmd_stats
.tcp_flags
;
2477 pmd
= dp_netdev_get_pmd(dp
, del
->pmd_id
);
2481 error
= flow_del_on_pmd(pmd
, del
->stats
, del
);
2482 dp_netdev_pmd_unref(pmd
);
2489 struct dpif_netdev_flow_dump
{
2490 struct dpif_flow_dump up
;
2491 struct cmap_position poll_thread_pos
;
2492 struct cmap_position flow_pos
;
2493 struct dp_netdev_pmd_thread
*cur_pmd
;
2495 struct ovs_mutex mutex
;
2498 static struct dpif_netdev_flow_dump
*
2499 dpif_netdev_flow_dump_cast(struct dpif_flow_dump
*dump
)
2501 return CONTAINER_OF(dump
, struct dpif_netdev_flow_dump
, up
);
2504 static struct dpif_flow_dump
*
2505 dpif_netdev_flow_dump_create(const struct dpif
*dpif_
, bool terse
)
2507 struct dpif_netdev_flow_dump
*dump
;
2509 dump
= xzalloc(sizeof *dump
);
2510 dpif_flow_dump_init(&dump
->up
, dpif_
);
2511 dump
->up
.terse
= terse
;
2512 ovs_mutex_init(&dump
->mutex
);
2518 dpif_netdev_flow_dump_destroy(struct dpif_flow_dump
*dump_
)
2520 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
2522 ovs_mutex_destroy(&dump
->mutex
);
2527 struct dpif_netdev_flow_dump_thread
{
2528 struct dpif_flow_dump_thread up
;
2529 struct dpif_netdev_flow_dump
*dump
;
2530 struct odputil_keybuf keybuf
[FLOW_DUMP_MAX_BATCH
];
2531 struct odputil_keybuf maskbuf
[FLOW_DUMP_MAX_BATCH
];
2534 static struct dpif_netdev_flow_dump_thread
*
2535 dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread
*thread
)
2537 return CONTAINER_OF(thread
, struct dpif_netdev_flow_dump_thread
, up
);
2540 static struct dpif_flow_dump_thread
*
2541 dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump
*dump_
)
2543 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
2544 struct dpif_netdev_flow_dump_thread
*thread
;
2546 thread
= xmalloc(sizeof *thread
);
2547 dpif_flow_dump_thread_init(&thread
->up
, &dump
->up
);
2548 thread
->dump
= dump
;
2553 dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread
*thread_
)
2555 struct dpif_netdev_flow_dump_thread
*thread
2556 = dpif_netdev_flow_dump_thread_cast(thread_
);
2562 dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread
*thread_
,
2563 struct dpif_flow
*flows
, int max_flows
)
2565 struct dpif_netdev_flow_dump_thread
*thread
2566 = dpif_netdev_flow_dump_thread_cast(thread_
);
2567 struct dpif_netdev_flow_dump
*dump
= thread
->dump
;
2568 struct dp_netdev_flow
*netdev_flows
[FLOW_DUMP_MAX_BATCH
];
2572 ovs_mutex_lock(&dump
->mutex
);
2573 if (!dump
->status
) {
2574 struct dpif_netdev
*dpif
= dpif_netdev_cast(thread
->up
.dpif
);
2575 struct dp_netdev
*dp
= get_dp_netdev(&dpif
->dpif
);
2576 struct dp_netdev_pmd_thread
*pmd
= dump
->cur_pmd
;
2577 int flow_limit
= MIN(max_flows
, FLOW_DUMP_MAX_BATCH
);
2579 /* First call to dump_next(), extracts the first pmd thread.
2580 * If there is no pmd thread, returns immediately. */
2582 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
2584 ovs_mutex_unlock(&dump
->mutex
);
2591 for (n_flows
= 0; n_flows
< flow_limit
; n_flows
++) {
2592 struct cmap_node
*node
;
2594 node
= cmap_next_position(&pmd
->flow_table
, &dump
->flow_pos
);
2598 netdev_flows
[n_flows
] = CONTAINER_OF(node
,
2599 struct dp_netdev_flow
,
2602 /* When finishing dumping the current pmd thread, moves to
2604 if (n_flows
< flow_limit
) {
2605 memset(&dump
->flow_pos
, 0, sizeof dump
->flow_pos
);
2606 dp_netdev_pmd_unref(pmd
);
2607 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
2613 /* Keeps the reference to next caller. */
2614 dump
->cur_pmd
= pmd
;
2616 /* If the current dump is empty, do not exit the loop, since the
2617 * remaining pmds could have flows to be dumped. Just dumps again
2618 * on the new 'pmd'. */
2621 ovs_mutex_unlock(&dump
->mutex
);
2623 for (i
= 0; i
< n_flows
; i
++) {
2624 struct odputil_keybuf
*maskbuf
= &thread
->maskbuf
[i
];
2625 struct odputil_keybuf
*keybuf
= &thread
->keybuf
[i
];
2626 struct dp_netdev_flow
*netdev_flow
= netdev_flows
[i
];
2627 struct dpif_flow
*f
= &flows
[i
];
2628 struct ofpbuf key
, mask
;
2630 ofpbuf_use_stack(&key
, keybuf
, sizeof *keybuf
);
2631 ofpbuf_use_stack(&mask
, maskbuf
, sizeof *maskbuf
);
2632 dp_netdev_flow_to_dpif_flow(netdev_flow
, &key
, &mask
, f
,
2640 dpif_netdev_execute(struct dpif
*dpif
, struct dpif_execute
*execute
)
2641 OVS_NO_THREAD_SAFETY_ANALYSIS
2643 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2644 struct dp_netdev_pmd_thread
*pmd
;
2645 struct dp_packet_batch pp
;
2647 if (dp_packet_size(execute
->packet
) < ETH_HEADER_LEN
||
2648 dp_packet_size(execute
->packet
) > UINT16_MAX
) {
2652 /* Tries finding the 'pmd'. If NULL is returned, that means
2653 * the current thread is a non-pmd thread and should use
2654 * dp_netdev_get_pmd(dp, NON_PMD_CORE_ID). */
2655 pmd
= ovsthread_getspecific(dp
->per_pmd_key
);
2657 pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
2663 if (execute
->probe
) {
2664 /* If this is part of a probe, Drop the packet, since executing
2665 * the action may actually cause spurious packets be sent into
2670 /* If the current thread is non-pmd thread, acquires
2671 * the 'non_pmd_mutex'. */
2672 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
2673 ovs_mutex_lock(&dp
->non_pmd_mutex
);
2676 /* The action processing expects the RSS hash to be valid, because
2677 * it's always initialized at the beginning of datapath processing.
2678 * In this case, though, 'execute->packet' may not have gone through
2679 * the datapath at all, it may have been generated by the upper layer
2680 * (OpenFlow packet-out, BFD frame, ...). */
2681 if (!dp_packet_rss_valid(execute
->packet
)) {
2682 dp_packet_set_rss_hash(execute
->packet
,
2683 flow_hash_5tuple(execute
->flow
, 0));
2686 dp_packet_batch_init_packet(&pp
, execute
->packet
);
2687 dp_netdev_execute_actions(pmd
, &pp
, false, execute
->flow
,
2688 execute
->actions
, execute
->actions_len
,
2691 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
2692 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
2693 dp_netdev_pmd_unref(pmd
);
2700 dpif_netdev_operate(struct dpif
*dpif
, struct dpif_op
**ops
, size_t n_ops
)
2704 for (i
= 0; i
< n_ops
; i
++) {
2705 struct dpif_op
*op
= ops
[i
];
2708 case DPIF_OP_FLOW_PUT
:
2709 op
->error
= dpif_netdev_flow_put(dpif
, &op
->u
.flow_put
);
2712 case DPIF_OP_FLOW_DEL
:
2713 op
->error
= dpif_netdev_flow_del(dpif
, &op
->u
.flow_del
);
2716 case DPIF_OP_EXECUTE
:
2717 op
->error
= dpif_netdev_execute(dpif
, &op
->u
.execute
);
2720 case DPIF_OP_FLOW_GET
:
2721 op
->error
= dpif_netdev_flow_get(dpif
, &op
->u
.flow_get
);
2727 /* Applies datapath configuration from the database. Some of the changes are
2728 * actually applied in dpif_netdev_run(). */
2730 dpif_netdev_set_config(struct dpif
*dpif
, const struct smap
*other_config
)
2732 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2733 const char *cmask
= smap_get(other_config
, "pmd-cpu-mask");
2735 if (!nullable_string_is_equal(dp
->pmd_cmask
, cmask
)) {
2736 free(dp
->pmd_cmask
);
2737 dp
->pmd_cmask
= nullable_xstrdup(cmask
);
2738 dp_netdev_request_reconfigure(dp
);
2744 /* Parses affinity list and returns result in 'core_ids'. */
2746 parse_affinity_list(const char *affinity_list
, unsigned *core_ids
, int n_rxq
)
2749 char *list
, *copy
, *key
, *value
;
2752 for (i
= 0; i
< n_rxq
; i
++) {
2753 core_ids
[i
] = OVS_CORE_UNSPEC
;
2756 if (!affinity_list
) {
2760 list
= copy
= xstrdup(affinity_list
);
2762 while (ofputil_parse_key_value(&list
, &key
, &value
)) {
2763 int rxq_id
, core_id
;
2765 if (!str_to_int(key
, 0, &rxq_id
) || rxq_id
< 0
2766 || !str_to_int(value
, 0, &core_id
) || core_id
< 0) {
2771 if (rxq_id
< n_rxq
) {
2772 core_ids
[rxq_id
] = core_id
;
2780 /* Parses 'affinity_list' and applies configuration if it is valid. */
2782 dpif_netdev_port_set_rxq_affinity(struct dp_netdev_port
*port
,
2783 const char *affinity_list
)
2785 unsigned *core_ids
, i
;
2788 core_ids
= xmalloc(port
->n_rxq
* sizeof *core_ids
);
2789 if (parse_affinity_list(affinity_list
, core_ids
, port
->n_rxq
)) {
2794 for (i
= 0; i
< port
->n_rxq
; i
++) {
2795 port
->rxqs
[i
].core_id
= core_ids
[i
];
2803 /* Changes the affinity of port's rx queues. The changes are actually applied
2804 * in dpif_netdev_run(). */
2806 dpif_netdev_port_set_config(struct dpif
*dpif
, odp_port_t port_no
,
2807 const struct smap
*cfg
)
2809 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2810 struct dp_netdev_port
*port
;
2812 const char *affinity_list
= smap_get(cfg
, "pmd-rxq-affinity");
2814 ovs_mutex_lock(&dp
->port_mutex
);
2815 error
= get_port_by_number(dp
, port_no
, &port
);
2816 if (error
|| !netdev_is_pmd(port
->netdev
)
2817 || nullable_string_is_equal(affinity_list
, port
->rxq_affinity_list
)) {
2821 error
= dpif_netdev_port_set_rxq_affinity(port
, affinity_list
);
2825 free(port
->rxq_affinity_list
);
2826 port
->rxq_affinity_list
= nullable_xstrdup(affinity_list
);
2828 dp_netdev_request_reconfigure(dp
);
2830 ovs_mutex_unlock(&dp
->port_mutex
);
2835 dpif_netdev_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
2836 uint32_t queue_id
, uint32_t *priority
)
2838 *priority
= queue_id
;
2843 /* Creates and returns a new 'struct dp_netdev_actions', whose actions are
2844 * a copy of the 'ofpacts_len' bytes of 'ofpacts'. */
2845 struct dp_netdev_actions
*
2846 dp_netdev_actions_create(const struct nlattr
*actions
, size_t size
)
2848 struct dp_netdev_actions
*netdev_actions
;
2850 netdev_actions
= xmalloc(sizeof *netdev_actions
+ size
);
2851 memcpy(netdev_actions
->actions
, actions
, size
);
2852 netdev_actions
->size
= size
;
2854 return netdev_actions
;
2857 struct dp_netdev_actions
*
2858 dp_netdev_flow_get_actions(const struct dp_netdev_flow
*flow
)
2860 return ovsrcu_get(struct dp_netdev_actions
*, &flow
->actions
);
2864 dp_netdev_actions_free(struct dp_netdev_actions
*actions
)
2869 static inline unsigned long long
2870 cycles_counter(void)
2873 return rte_get_tsc_cycles();
2879 /* Fake mutex to make sure that the calls to cycles_count_* are balanced */
2880 extern struct ovs_mutex cycles_counter_fake_mutex
;
2882 /* Start counting cycles. Must be followed by 'cycles_count_end()' */
2884 cycles_count_start(struct dp_netdev_pmd_thread
*pmd
)
2885 OVS_ACQUIRES(&cycles_counter_fake_mutex
)
2886 OVS_NO_THREAD_SAFETY_ANALYSIS
2888 pmd
->last_cycles
= cycles_counter();
2891 /* Stop counting cycles and add them to the counter 'type' */
2893 cycles_count_end(struct dp_netdev_pmd_thread
*pmd
,
2894 enum pmd_cycles_counter_type type
)
2895 OVS_RELEASES(&cycles_counter_fake_mutex
)
2896 OVS_NO_THREAD_SAFETY_ANALYSIS
2898 unsigned long long interval
= cycles_counter() - pmd
->last_cycles
;
2900 non_atomic_ullong_add(&pmd
->cycles
.n
[type
], interval
);
2904 dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread
*pmd
,
2905 struct netdev_rxq
*rx
,
2908 struct dp_packet_batch batch
;
2911 dp_packet_batch_init(&batch
);
2912 cycles_count_start(pmd
);
2913 error
= netdev_rxq_recv(rx
, &batch
);
2914 cycles_count_end(pmd
, PMD_CYCLES_POLLING
);
2916 *recirc_depth_get() = 0;
2918 cycles_count_start(pmd
);
2919 dp_netdev_input(pmd
, &batch
, port_no
);
2920 cycles_count_end(pmd
, PMD_CYCLES_PROCESSING
);
2921 } else if (error
!= EAGAIN
&& error
!= EOPNOTSUPP
) {
2922 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2924 VLOG_ERR_RL(&rl
, "error receiving data from %s: %s",
2925 netdev_rxq_get_name(rx
), ovs_strerror(error
));
2929 static struct tx_port
*
2930 tx_port_lookup(const struct hmap
*hmap
, odp_port_t port_no
)
2934 HMAP_FOR_EACH_IN_BUCKET (tx
, node
, hash_port_no(port_no
), hmap
) {
2935 if (tx
->port
->port_no
== port_no
) {
2944 port_reconfigure(struct dp_netdev_port
*port
)
2946 struct netdev
*netdev
= port
->netdev
;
2949 port
->need_reconfigure
= false;
2951 /* Closes the existing 'rxq's. */
2952 for (i
= 0; i
< port
->n_rxq
; i
++) {
2953 netdev_rxq_close(port
->rxqs
[i
].rx
);
2954 port
->rxqs
[i
].rx
= NULL
;
2958 /* Allows 'netdev' to apply the pending configuration changes. */
2959 if (netdev_is_reconf_required(netdev
)) {
2960 err
= netdev_reconfigure(netdev
);
2961 if (err
&& (err
!= EOPNOTSUPP
)) {
2962 VLOG_ERR("Failed to set interface %s new configuration",
2963 netdev_get_name(netdev
));
2967 /* If the netdev_reconfigure() above succeeds, reopens the 'rxq's. */
2968 port
->rxqs
= xrealloc(port
->rxqs
,
2969 sizeof *port
->rxqs
* netdev_n_rxq(netdev
));
2970 /* Realloc 'used' counters for tx queues. */
2971 free(port
->txq_used
);
2972 port
->txq_used
= xcalloc(netdev_n_txq(netdev
), sizeof *port
->txq_used
);
2974 for (i
= 0; i
< netdev_n_rxq(netdev
); i
++) {
2975 port
->rxqs
[i
].port
= port
;
2976 err
= netdev_rxq_open(netdev
, &port
->rxqs
[i
].rx
, i
);
2983 /* Parse affinity list to apply configuration for new queues. */
2984 dpif_netdev_port_set_rxq_affinity(port
, port
->rxq_affinity_list
);
2989 struct rr_numa_list
{
2990 struct hmap numas
; /* Contains 'struct rr_numa' */
2994 struct hmap_node node
;
2998 /* Non isolated pmds on numa node 'numa_id' */
2999 struct dp_netdev_pmd_thread
**pmds
;
3005 static struct rr_numa
*
3006 rr_numa_list_lookup(struct rr_numa_list
*rr
, int numa_id
)
3008 struct rr_numa
*numa
;
3010 HMAP_FOR_EACH_WITH_HASH (numa
, node
, hash_int(numa_id
, 0), &rr
->numas
) {
3011 if (numa
->numa_id
== numa_id
) {
3020 rr_numa_list_populate(struct dp_netdev
*dp
, struct rr_numa_list
*rr
)
3022 struct dp_netdev_pmd_thread
*pmd
;
3023 struct rr_numa
*numa
;
3025 hmap_init(&rr
->numas
);
3027 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3028 if (pmd
->core_id
== NON_PMD_CORE_ID
|| pmd
->isolated
) {
3032 numa
= rr_numa_list_lookup(rr
, pmd
->numa_id
);
3034 numa
= xzalloc(sizeof *numa
);
3035 numa
->numa_id
= pmd
->numa_id
;
3036 hmap_insert(&rr
->numas
, &numa
->node
, hash_int(pmd
->numa_id
, 0));
3039 numa
->pmds
= xrealloc(numa
->pmds
, numa
->n_pmds
* sizeof *numa
->pmds
);
3040 numa
->pmds
[numa
->n_pmds
- 1] = pmd
;
3044 static struct dp_netdev_pmd_thread
*
3045 rr_numa_get_pmd(struct rr_numa
*numa
)
3047 return numa
->pmds
[numa
->cur_index
++ % numa
->n_pmds
];
3051 rr_numa_list_destroy(struct rr_numa_list
*rr
)
3053 struct rr_numa
*numa
;
3055 HMAP_FOR_EACH_POP (numa
, node
, &rr
->numas
) {
3059 hmap_destroy(&rr
->numas
);
3062 /* Assign pmds to queues. If 'pinned' is true, assign pmds to pinned
3063 * queues and marks the pmds as isolated. Otherwise, assign non isolated
3064 * pmds to unpinned queues.
3066 * The function doesn't touch the pmd threads, it just stores the assignment
3067 * in the 'pmd' member of each rxq. */
3069 rxq_scheduling(struct dp_netdev
*dp
, bool pinned
) OVS_REQUIRES(dp
->port_mutex
)
3071 struct dp_netdev_port
*port
;
3072 struct rr_numa_list rr
;
3074 rr_numa_list_populate(dp
, &rr
);
3076 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3077 struct rr_numa
*numa
;
3080 if (!netdev_is_pmd(port
->netdev
)) {
3084 numa_id
= netdev_get_numa_id(port
->netdev
);
3085 numa
= rr_numa_list_lookup(&rr
, numa_id
);
3087 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
3088 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
3090 if (pinned
&& q
->core_id
!= OVS_CORE_UNSPEC
) {
3091 struct dp_netdev_pmd_thread
*pmd
;
3093 pmd
= dp_netdev_get_pmd(dp
, q
->core_id
);
3095 VLOG_WARN("There is no PMD thread on core %d. Queue "
3096 "%d on port \'%s\' will not be polled.",
3097 q
->core_id
, qid
, netdev_get_name(port
->netdev
));
3100 pmd
->isolated
= true;
3101 dp_netdev_pmd_unref(pmd
);
3103 } else if (!pinned
&& q
->core_id
== OVS_CORE_UNSPEC
) {
3105 VLOG_WARN("There's no available (non isolated) pmd thread "
3106 "on numa node %d. Queue %d on port \'%s\' will "
3108 numa_id
, qid
, netdev_get_name(port
->netdev
));
3110 q
->pmd
= rr_numa_get_pmd(numa
);
3116 rr_numa_list_destroy(&rr
);
3120 reconfigure_pmd_threads(struct dp_netdev
*dp
)
3121 OVS_REQUIRES(dp
->port_mutex
)
3123 struct dp_netdev_pmd_thread
*pmd
;
3124 struct ovs_numa_dump
*pmd_cores
;
3125 bool changed
= false;
3127 /* The pmd threads should be started only if there's a pmd port in the
3128 * datapath. If the user didn't provide any "pmd-cpu-mask", we start
3129 * NR_PMD_THREADS per numa node. */
3130 if (!has_pmd_port(dp
)) {
3131 pmd_cores
= ovs_numa_dump_n_cores_per_numa(0);
3132 } else if (dp
->pmd_cmask
&& dp
->pmd_cmask
[0]) {
3133 pmd_cores
= ovs_numa_dump_cores_with_cmask(dp
->pmd_cmask
);
3135 pmd_cores
= ovs_numa_dump_n_cores_per_numa(NR_PMD_THREADS
);
3138 /* Check for changed configuration */
3139 if (ovs_numa_dump_count(pmd_cores
) != cmap_count(&dp
->poll_threads
) - 1) {
3142 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3143 if (pmd
->core_id
!= NON_PMD_CORE_ID
3144 && !ovs_numa_dump_contains_core(pmd_cores
,
3153 /* Destroy the old and recreate the new pmd threads. We don't perform an
3154 * incremental update because we would have to adjust 'static_tx_qid'. */
3156 struct ovs_numa_info_core
*core
;
3157 struct ovs_numa_info_numa
*numa
;
3159 /* Do not destroy the non pmd thread. */
3160 dp_netdev_destroy_all_pmds(dp
, false);
3161 FOR_EACH_CORE_ON_DUMP (core
, pmd_cores
) {
3162 struct dp_netdev_pmd_thread
*pmd
= xzalloc(sizeof *pmd
);
3164 dp_netdev_configure_pmd(pmd
, dp
, core
->core_id
, core
->numa_id
);
3166 pmd
->thread
= ovs_thread_create("pmd", pmd_thread_main
, pmd
);
3169 /* Log the number of pmd threads per numa node. */
3170 FOR_EACH_NUMA_ON_DUMP (numa
, pmd_cores
) {
3171 VLOG_INFO("Created %"PRIuSIZE
" pmd threads on numa node %d",
3172 numa
->n_cores
, numa
->numa_id
);
3176 ovs_numa_dump_destroy(pmd_cores
);
3180 reload_affected_pmds(struct dp_netdev
*dp
)
3182 struct dp_netdev_pmd_thread
*pmd
;
3184 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3185 if (pmd
->need_reload
) {
3186 dp_netdev_reload_pmd__(pmd
);
3187 pmd
->need_reload
= false;
3193 pmd_remove_stale_ports(struct dp_netdev
*dp
,
3194 struct dp_netdev_pmd_thread
*pmd
)
3195 OVS_EXCLUDED(pmd
->port_mutex
)
3196 OVS_REQUIRES(dp
->port_mutex
)
3198 struct rxq_poll
*poll
, *poll_next
;
3199 struct tx_port
*tx
, *tx_next
;
3201 ovs_mutex_lock(&pmd
->port_mutex
);
3202 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
3203 struct dp_netdev_port
*port
= poll
->rxq
->port
;
3205 if (port
->need_reconfigure
3206 || !hmap_contains(&dp
->ports
, &port
->node
)) {
3207 dp_netdev_del_rxq_from_pmd(pmd
, poll
);
3210 HMAP_FOR_EACH_SAFE (tx
, tx_next
, node
, &pmd
->tx_ports
) {
3211 struct dp_netdev_port
*port
= tx
->port
;
3213 if (port
->need_reconfigure
3214 || !hmap_contains(&dp
->ports
, &port
->node
)) {
3215 dp_netdev_del_port_tx_from_pmd(pmd
, tx
);
3218 ovs_mutex_unlock(&pmd
->port_mutex
);
3221 /* Must be called each time a port is added/removed or the cmask changes.
3222 * This creates and destroys pmd threads, reconfigures ports, opens their
3223 * rxqs and assigns all rxqs/txqs to pmd threads. */
3225 reconfigure_datapath(struct dp_netdev
*dp
)
3226 OVS_REQUIRES(dp
->port_mutex
)
3228 struct dp_netdev_pmd_thread
*pmd
;
3229 struct dp_netdev_port
*port
;
3232 dp
->last_reconfigure_seq
= seq_read(dp
->reconfigure_seq
);
3234 /* Step 1: Adjust the pmd threads based on the datapath ports, the cores
3235 * on the system and the user configuration. */
3236 reconfigure_pmd_threads(dp
);
3238 wanted_txqs
= cmap_count(&dp
->poll_threads
);
3240 /* The number of pmd threads might have changed, or a port can be new:
3241 * adjust the txqs. */
3242 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3243 netdev_set_tx_multiq(port
->netdev
, wanted_txqs
);
3246 /* Step 2: Remove from the pmd threads ports that have been removed or
3247 * need reconfiguration. */
3249 /* Check for all the ports that need reconfiguration. We cache this in
3250 * 'port->reconfigure', because netdev_is_reconf_required() can change at
3252 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3253 if (netdev_is_reconf_required(port
->netdev
)) {
3254 port
->need_reconfigure
= true;
3258 /* Remove from the pmd threads all the ports that have been deleted or
3259 * need reconfiguration. */
3260 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3261 pmd_remove_stale_ports(dp
, pmd
);
3264 /* Reload affected pmd threads. We must wait for the pmd threads before
3265 * reconfiguring the ports, because a port cannot be reconfigured while
3266 * it's being used. */
3267 reload_affected_pmds(dp
);
3269 /* Step 3: Reconfigure ports. */
3271 /* We only reconfigure the ports that we determined above, because they're
3272 * not being used by any pmd thread at the moment. If a port fails to
3273 * reconfigure we remove it from the datapath. */
3274 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3277 if (!port
->need_reconfigure
) {
3281 err
= port_reconfigure(port
);
3283 hmap_remove(&dp
->ports
, &port
->node
);
3284 seq_change(dp
->port_seq
);
3287 port
->dynamic_txqs
= netdev_n_txq(port
->netdev
) < wanted_txqs
;
3291 /* Step 4: Compute new rxq scheduling. We don't touch the pmd threads
3292 * for now, we just update the 'pmd' pointer in each rxq to point to the
3293 * wanted thread according to the scheduling policy. */
3295 /* Reset all the pmd threads to non isolated. */
3296 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3297 pmd
->isolated
= false;
3300 /* Reset all the queues to unassigned */
3301 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3302 for (int i
= 0; i
< port
->n_rxq
; i
++) {
3303 port
->rxqs
[i
].pmd
= NULL
;
3307 /* Add pinned queues and mark pmd threads isolated. */
3308 rxq_scheduling(dp
, true);
3310 /* Add non-pinned queues. */
3311 rxq_scheduling(dp
, false);
3313 /* Step 5: Remove queues not compliant with new scheduling. */
3314 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3315 struct rxq_poll
*poll
, *poll_next
;
3317 ovs_mutex_lock(&pmd
->port_mutex
);
3318 HMAP_FOR_EACH_SAFE (poll
, poll_next
, node
, &pmd
->poll_list
) {
3319 if (poll
->rxq
->pmd
!= pmd
) {
3320 dp_netdev_del_rxq_from_pmd(pmd
, poll
);
3323 ovs_mutex_unlock(&pmd
->port_mutex
);
3326 /* Reload affected pmd threads. We must wait for the pmd threads to remove
3327 * the old queues before readding them, otherwise a queue can be polled by
3328 * two threads at the same time. */
3329 reload_affected_pmds(dp
);
3331 /* Step 6: Add queues from scheduling, if they're not there already. */
3332 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3333 if (!netdev_is_pmd(port
->netdev
)) {
3337 for (int qid
= 0; qid
< port
->n_rxq
; qid
++) {
3338 struct dp_netdev_rxq
*q
= &port
->rxqs
[qid
];
3341 ovs_mutex_lock(&q
->pmd
->port_mutex
);
3342 dp_netdev_add_rxq_to_pmd(q
->pmd
, q
);
3343 ovs_mutex_unlock(&q
->pmd
->port_mutex
);
3348 /* Add every port to the tx cache of every pmd thread, if it's not
3349 * there already and if this pmd has at least one rxq to poll. */
3350 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3351 ovs_mutex_lock(&pmd
->port_mutex
);
3352 if (hmap_count(&pmd
->poll_list
) || pmd
->core_id
== NON_PMD_CORE_ID
) {
3353 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3354 dp_netdev_add_port_tx_to_pmd(pmd
, port
);
3357 ovs_mutex_unlock(&pmd
->port_mutex
);
3360 /* Reload affected pmd threads. */
3361 reload_affected_pmds(dp
);
3364 /* Returns true if one of the netdevs in 'dp' requires a reconfiguration */
3366 ports_require_restart(const struct dp_netdev
*dp
)
3367 OVS_REQUIRES(dp
->port_mutex
)
3369 struct dp_netdev_port
*port
;
3371 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3372 if (netdev_is_reconf_required(port
->netdev
)) {
3380 /* Return true if needs to revalidate datapath flows. */
3382 dpif_netdev_run(struct dpif
*dpif
)
3384 struct dp_netdev_port
*port
;
3385 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3386 struct dp_netdev_pmd_thread
*non_pmd
;
3387 uint64_t new_tnl_seq
;
3389 ovs_mutex_lock(&dp
->port_mutex
);
3390 non_pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
3392 ovs_mutex_lock(&dp
->non_pmd_mutex
);
3393 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3394 if (!netdev_is_pmd(port
->netdev
)) {
3397 for (i
= 0; i
< port
->n_rxq
; i
++) {
3398 dp_netdev_process_rxq_port(non_pmd
, port
->rxqs
[i
].rx
,
3403 dpif_netdev_xps_revalidate_pmd(non_pmd
, time_msec(), false);
3404 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
3406 dp_netdev_pmd_unref(non_pmd
);
3409 if (dp_netdev_is_reconf_required(dp
) || ports_require_restart(dp
)) {
3410 reconfigure_datapath(dp
);
3412 ovs_mutex_unlock(&dp
->port_mutex
);
3414 tnl_neigh_cache_run();
3416 new_tnl_seq
= seq_read(tnl_conf_seq
);
3418 if (dp
->last_tnl_conf_seq
!= new_tnl_seq
) {
3419 dp
->last_tnl_conf_seq
= new_tnl_seq
;
3426 dpif_netdev_wait(struct dpif
*dpif
)
3428 struct dp_netdev_port
*port
;
3429 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3431 ovs_mutex_lock(&dp_netdev_mutex
);
3432 ovs_mutex_lock(&dp
->port_mutex
);
3433 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3434 netdev_wait_reconf_required(port
->netdev
);
3435 if (!netdev_is_pmd(port
->netdev
)) {
3438 for (i
= 0; i
< port
->n_rxq
; i
++) {
3439 netdev_rxq_wait(port
->rxqs
[i
].rx
);
3443 ovs_mutex_unlock(&dp
->port_mutex
);
3444 ovs_mutex_unlock(&dp_netdev_mutex
);
3445 seq_wait(tnl_conf_seq
, dp
->last_tnl_conf_seq
);
3449 pmd_free_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
3451 struct tx_port
*tx_port_cached
;
3453 /* Free all used tx queue ids. */
3454 dpif_netdev_xps_revalidate_pmd(pmd
, 0, true);
3456 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->tnl_port_cache
) {
3457 free(tx_port_cached
);
3459 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->send_port_cache
) {
3460 free(tx_port_cached
);
3464 /* Copies ports from 'pmd->tx_ports' (shared with the main thread) to
3465 * 'pmd->port_cache' (thread local) */
3467 pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
3468 OVS_REQUIRES(pmd
->port_mutex
)
3470 struct tx_port
*tx_port
, *tx_port_cached
;
3472 pmd_free_cached_ports(pmd
);
3473 hmap_shrink(&pmd
->send_port_cache
);
3474 hmap_shrink(&pmd
->tnl_port_cache
);
3476 HMAP_FOR_EACH (tx_port
, node
, &pmd
->tx_ports
) {
3477 if (netdev_has_tunnel_push_pop(tx_port
->port
->netdev
)) {
3478 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
3479 hmap_insert(&pmd
->tnl_port_cache
, &tx_port_cached
->node
,
3480 hash_port_no(tx_port_cached
->port
->port_no
));
3483 if (netdev_n_txq(tx_port
->port
->netdev
)) {
3484 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
3485 hmap_insert(&pmd
->send_port_cache
, &tx_port_cached
->node
,
3486 hash_port_no(tx_port_cached
->port
->port_no
));
3492 pmd_load_queues_and_ports(struct dp_netdev_pmd_thread
*pmd
,
3493 struct polled_queue
**ppoll_list
)
3495 struct polled_queue
*poll_list
= *ppoll_list
;
3496 struct rxq_poll
*poll
;
3499 ovs_mutex_lock(&pmd
->port_mutex
);
3500 poll_list
= xrealloc(poll_list
, hmap_count(&pmd
->poll_list
)
3501 * sizeof *poll_list
);
3504 HMAP_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
3505 poll_list
[i
].rx
= poll
->rxq
->rx
;
3506 poll_list
[i
].port_no
= poll
->rxq
->port
->port_no
;
3510 pmd_load_cached_ports(pmd
);
3512 ovs_mutex_unlock(&pmd
->port_mutex
);
3514 *ppoll_list
= poll_list
;
3519 pmd_thread_main(void *f_
)
3521 struct dp_netdev_pmd_thread
*pmd
= f_
;
3522 unsigned int lc
= 0;
3523 struct polled_queue
*poll_list
;
3530 /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */
3531 ovsthread_setspecific(pmd
->dp
->per_pmd_key
, pmd
);
3532 ovs_numa_thread_setaffinity_core(pmd
->core_id
);
3533 dpdk_set_lcore_id(pmd
->core_id
);
3534 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
3536 emc_cache_init(&pmd
->flow_cache
);
3538 /* List port/core affinity */
3539 for (i
= 0; i
< poll_cnt
; i
++) {
3540 VLOG_DBG("Core %d processing port \'%s\' with queue-id %d\n",
3541 pmd
->core_id
, netdev_rxq_get_name(poll_list
[i
].rx
),
3542 netdev_rxq_get_queue_id(poll_list
[i
].rx
));
3546 while (seq_read(pmd
->reload_seq
) == pmd
->last_reload_seq
) {
3547 seq_wait(pmd
->reload_seq
, pmd
->last_reload_seq
);
3554 for (i
= 0; i
< poll_cnt
; i
++) {
3555 dp_netdev_process_rxq_port(pmd
, poll_list
[i
].rx
,
3556 poll_list
[i
].port_no
);
3564 coverage_try_clear();
3565 dp_netdev_pmd_try_optimize(pmd
);
3566 if (!ovsrcu_try_quiesce()) {
3567 emc_cache_slow_sweep(&pmd
->flow_cache
);
3570 atomic_read_relaxed(&pmd
->reload
, &reload
);
3577 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
3578 exiting
= latch_is_set(&pmd
->exit_latch
);
3579 /* Signal here to make sure the pmd finishes
3580 * reloading the updated configuration. */
3581 dp_netdev_pmd_reload_done(pmd
);
3583 emc_cache_uninit(&pmd
->flow_cache
);
3590 pmd_free_cached_ports(pmd
);
3595 dp_netdev_disable_upcall(struct dp_netdev
*dp
)
3596 OVS_ACQUIRES(dp
->upcall_rwlock
)
3598 fat_rwlock_wrlock(&dp
->upcall_rwlock
);
3602 dpif_netdev_disable_upcall(struct dpif
*dpif
)
3603 OVS_NO_THREAD_SAFETY_ANALYSIS
3605 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3606 dp_netdev_disable_upcall(dp
);
3610 dp_netdev_enable_upcall(struct dp_netdev
*dp
)
3611 OVS_RELEASES(dp
->upcall_rwlock
)
3613 fat_rwlock_unlock(&dp
->upcall_rwlock
);
3617 dpif_netdev_enable_upcall(struct dpif
*dpif
)
3618 OVS_NO_THREAD_SAFETY_ANALYSIS
3620 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
3621 dp_netdev_enable_upcall(dp
);
3625 dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
)
3627 ovs_mutex_lock(&pmd
->cond_mutex
);
3628 atomic_store_relaxed(&pmd
->reload
, false);
3629 pmd
->last_reload_seq
= seq_read(pmd
->reload_seq
);
3630 xpthread_cond_signal(&pmd
->cond
);
3631 ovs_mutex_unlock(&pmd
->cond_mutex
);
3634 /* Finds and refs the dp_netdev_pmd_thread on core 'core_id'. Returns
3635 * the pointer if succeeds, otherwise, NULL (it can return NULL even if
3636 * 'core_id' is NON_PMD_CORE_ID).
3638 * Caller must unrefs the returned reference. */
3639 static struct dp_netdev_pmd_thread
*
3640 dp_netdev_get_pmd(struct dp_netdev
*dp
, unsigned core_id
)
3642 struct dp_netdev_pmd_thread
*pmd
;
3643 const struct cmap_node
*pnode
;
3645 pnode
= cmap_find(&dp
->poll_threads
, hash_int(core_id
, 0));
3649 pmd
= CONTAINER_OF(pnode
, struct dp_netdev_pmd_thread
, node
);
3651 return dp_netdev_pmd_try_ref(pmd
) ? pmd
: NULL
;
3654 /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */
3656 dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
3657 OVS_REQUIRES(dp
->port_mutex
)
3659 struct dp_netdev_pmd_thread
*non_pmd
;
3661 non_pmd
= xzalloc(sizeof *non_pmd
);
3662 dp_netdev_configure_pmd(non_pmd
, dp
, NON_PMD_CORE_ID
, OVS_NUMA_UNSPEC
);
3665 /* Caller must have valid pointer to 'pmd'. */
3667 dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
)
3669 return ovs_refcount_try_ref_rcu(&pmd
->ref_cnt
);
3673 dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
)
3675 if (pmd
&& ovs_refcount_unref(&pmd
->ref_cnt
) == 1) {
3676 ovsrcu_postpone(dp_netdev_destroy_pmd
, pmd
);
3680 /* Given cmap position 'pos', tries to ref the next node. If try_ref()
3681 * fails, keeps checking for next node until reaching the end of cmap.
3683 * Caller must unrefs the returned reference. */
3684 static struct dp_netdev_pmd_thread
*
3685 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
)
3687 struct dp_netdev_pmd_thread
*next
;
3690 struct cmap_node
*node
;
3692 node
= cmap_next_position(&dp
->poll_threads
, pos
);
3693 next
= node
? CONTAINER_OF(node
, struct dp_netdev_pmd_thread
, node
)
3695 } while (next
&& !dp_netdev_pmd_try_ref(next
));
3700 /* Configures the 'pmd' based on the input argument. */
3702 dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
, struct dp_netdev
*dp
,
3703 unsigned core_id
, int numa_id
)
3706 pmd
->core_id
= core_id
;
3707 pmd
->numa_id
= numa_id
;
3708 pmd
->need_reload
= false;
3710 *CONST_CAST(int *, &pmd
->static_tx_qid
) = cmap_count(&dp
->poll_threads
);
3712 ovs_refcount_init(&pmd
->ref_cnt
);
3713 latch_init(&pmd
->exit_latch
);
3714 pmd
->reload_seq
= seq_create();
3715 pmd
->last_reload_seq
= seq_read(pmd
->reload_seq
);
3716 atomic_init(&pmd
->reload
, false);
3717 xpthread_cond_init(&pmd
->cond
, NULL
);
3718 ovs_mutex_init(&pmd
->cond_mutex
);
3719 ovs_mutex_init(&pmd
->flow_mutex
);
3720 ovs_mutex_init(&pmd
->port_mutex
);
3721 cmap_init(&pmd
->flow_table
);
3722 cmap_init(&pmd
->classifiers
);
3723 pmd
->next_optimization
= time_msec() + DPCLS_OPTIMIZATION_INTERVAL
;
3724 hmap_init(&pmd
->poll_list
);
3725 hmap_init(&pmd
->tx_ports
);
3726 hmap_init(&pmd
->tnl_port_cache
);
3727 hmap_init(&pmd
->send_port_cache
);
3728 /* init the 'flow_cache' since there is no
3729 * actual thread created for NON_PMD_CORE_ID. */
3730 if (core_id
== NON_PMD_CORE_ID
) {
3731 emc_cache_init(&pmd
->flow_cache
);
3733 cmap_insert(&dp
->poll_threads
, CONST_CAST(struct cmap_node
*, &pmd
->node
),
3734 hash_int(core_id
, 0));
3738 dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
)
3742 dp_netdev_pmd_flow_flush(pmd
);
3743 hmap_destroy(&pmd
->send_port_cache
);
3744 hmap_destroy(&pmd
->tnl_port_cache
);
3745 hmap_destroy(&pmd
->tx_ports
);
3746 hmap_destroy(&pmd
->poll_list
);
3747 /* All flows (including their dpcls_rules) have been deleted already */
3748 CMAP_FOR_EACH (cls
, node
, &pmd
->classifiers
) {
3750 ovsrcu_postpone(free
, cls
);
3752 cmap_destroy(&pmd
->classifiers
);
3753 cmap_destroy(&pmd
->flow_table
);
3754 ovs_mutex_destroy(&pmd
->flow_mutex
);
3755 latch_destroy(&pmd
->exit_latch
);
3756 seq_destroy(pmd
->reload_seq
);
3757 xpthread_cond_destroy(&pmd
->cond
);
3758 ovs_mutex_destroy(&pmd
->cond_mutex
);
3759 ovs_mutex_destroy(&pmd
->port_mutex
);
3763 /* Stops the pmd thread, removes it from the 'dp->poll_threads',
3764 * and unrefs the struct. */
3766 dp_netdev_del_pmd(struct dp_netdev
*dp
, struct dp_netdev_pmd_thread
*pmd
)
3768 /* NON_PMD_CORE_ID doesn't have a thread, so we don't have to synchronize,
3769 * but extra cleanup is necessary */
3770 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3771 ovs_mutex_lock(&dp
->non_pmd_mutex
);
3772 emc_cache_uninit(&pmd
->flow_cache
);
3773 pmd_free_cached_ports(pmd
);
3774 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
3776 latch_set(&pmd
->exit_latch
);
3777 dp_netdev_reload_pmd__(pmd
);
3778 xpthread_join(pmd
->thread
, NULL
);
3781 dp_netdev_pmd_clear_ports(pmd
);
3783 /* Purges the 'pmd''s flows after stopping the thread, but before
3784 * destroying the flows, so that the flow stats can be collected. */
3785 if (dp
->dp_purge_cb
) {
3786 dp
->dp_purge_cb(dp
->dp_purge_aux
, pmd
->core_id
);
3788 cmap_remove(&pmd
->dp
->poll_threads
, &pmd
->node
, hash_int(pmd
->core_id
, 0));
3789 dp_netdev_pmd_unref(pmd
);
3792 /* Destroys all pmd threads. If 'non_pmd' is true it also destroys the non pmd
3795 dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
, bool non_pmd
)
3797 struct dp_netdev_pmd_thread
*pmd
;
3798 struct dp_netdev_pmd_thread
**pmd_list
;
3799 size_t k
= 0, n_pmds
;
3801 n_pmds
= cmap_count(&dp
->poll_threads
);
3802 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
3804 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3805 if (!non_pmd
&& pmd
->core_id
== NON_PMD_CORE_ID
) {
3808 /* We cannot call dp_netdev_del_pmd(), since it alters
3809 * 'dp->poll_threads' (while we're iterating it) and it
3811 ovs_assert(k
< n_pmds
);
3812 pmd_list
[k
++] = pmd
;
3815 for (size_t i
= 0; i
< k
; i
++) {
3816 dp_netdev_del_pmd(dp
, pmd_list
[i
]);
3821 /* Deletes all rx queues from pmd->poll_list and all the ports from
3824 dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
)
3826 struct rxq_poll
*poll
;
3827 struct tx_port
*port
;
3829 ovs_mutex_lock(&pmd
->port_mutex
);
3830 HMAP_FOR_EACH_POP (poll
, node
, &pmd
->poll_list
) {
3833 HMAP_FOR_EACH_POP (port
, node
, &pmd
->tx_ports
) {
3836 ovs_mutex_unlock(&pmd
->port_mutex
);
3839 /* Adds rx queue to poll_list of PMD thread, if it's not there already. */
3841 dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
3842 struct dp_netdev_rxq
*rxq
)
3843 OVS_REQUIRES(pmd
->port_mutex
)
3845 int qid
= netdev_rxq_get_queue_id(rxq
->rx
);
3846 uint32_t hash
= hash_2words(odp_to_u32(rxq
->port
->port_no
), qid
);
3847 struct rxq_poll
*poll
;
3849 HMAP_FOR_EACH_WITH_HASH (poll
, node
, hash
, &pmd
->poll_list
) {
3850 if (poll
->rxq
== rxq
) {
3851 /* 'rxq' is already polled by this thread. Do nothing. */
3856 poll
= xmalloc(sizeof *poll
);
3858 hmap_insert(&pmd
->poll_list
, &poll
->node
, hash
);
3860 pmd
->need_reload
= true;
3863 /* Delete 'poll' from poll_list of PMD thread. */
3865 dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
3866 struct rxq_poll
*poll
)
3867 OVS_REQUIRES(pmd
->port_mutex
)
3869 hmap_remove(&pmd
->poll_list
, &poll
->node
);
3872 pmd
->need_reload
= true;
3875 /* Add 'port' to the tx port cache of 'pmd', which must be reloaded for the
3876 * changes to take effect. */
3878 dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
3879 struct dp_netdev_port
*port
)
3880 OVS_REQUIRES(pmd
->port_mutex
)
3884 tx
= tx_port_lookup(&pmd
->tx_ports
, port
->port_no
);
3886 /* 'port' is already on this thread tx cache. Do nothing. */
3890 tx
= xzalloc(sizeof *tx
);
3895 hmap_insert(&pmd
->tx_ports
, &tx
->node
, hash_port_no(tx
->port
->port_no
));
3896 pmd
->need_reload
= true;
3899 /* Del 'tx' from the tx port cache of 'pmd', which must be reloaded for the
3900 * changes to take effect. */
3902 dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread
*pmd
,
3904 OVS_REQUIRES(pmd
->port_mutex
)
3906 hmap_remove(&pmd
->tx_ports
, &tx
->node
);
3908 pmd
->need_reload
= true;
3912 dpif_netdev_get_datapath_version(void)
3914 return xstrdup("<built-in>");
3918 dp_netdev_flow_used(struct dp_netdev_flow
*netdev_flow
, int cnt
, int size
,
3919 uint16_t tcp_flags
, long long now
)
3923 atomic_store_relaxed(&netdev_flow
->stats
.used
, now
);
3924 non_atomic_ullong_add(&netdev_flow
->stats
.packet_count
, cnt
);
3925 non_atomic_ullong_add(&netdev_flow
->stats
.byte_count
, size
);
3926 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
3928 atomic_store_relaxed(&netdev_flow
->stats
.tcp_flags
, flags
);
3932 dp_netdev_count_packet(struct dp_netdev_pmd_thread
*pmd
,
3933 enum dp_stat_type type
, int cnt
)
3935 non_atomic_ullong_add(&pmd
->stats
.n
[type
], cnt
);
3939 dp_netdev_upcall(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet
*packet_
,
3940 struct flow
*flow
, struct flow_wildcards
*wc
, ovs_u128
*ufid
,
3941 enum dpif_upcall_type type
, const struct nlattr
*userdata
,
3942 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
3944 struct dp_netdev
*dp
= pmd
->dp
;
3946 if (OVS_UNLIKELY(!dp
->upcall_cb
)) {
3950 if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl
))) {
3951 struct ds ds
= DS_EMPTY_INITIALIZER
;
3954 struct odp_flow_key_parms odp_parms
= {
3956 .mask
= wc
? &wc
->masks
: NULL
,
3957 .support
= dp_netdev_support
,
3960 ofpbuf_init(&key
, 0);
3961 odp_flow_key_from_flow(&odp_parms
, &key
);
3962 packet_str
= ofp_packet_to_string(dp_packet_data(packet_
),
3963 dp_packet_size(packet_
));
3965 odp_flow_key_format(key
.data
, key
.size
, &ds
);
3967 VLOG_DBG("%s: %s upcall:\n%s\n%s", dp
->name
,
3968 dpif_upcall_type_to_string(type
), ds_cstr(&ds
), packet_str
);
3970 ofpbuf_uninit(&key
);
3976 return dp
->upcall_cb(packet_
, flow
, ufid
, pmd
->core_id
, type
, userdata
,
3977 actions
, wc
, put_actions
, dp
->upcall_aux
);
3980 static inline uint32_t
3981 dpif_netdev_packet_get_rss_hash(struct dp_packet
*packet
,
3982 const struct miniflow
*mf
)
3984 uint32_t hash
, recirc_depth
;
3986 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
3987 hash
= dp_packet_get_rss_hash(packet
);
3989 hash
= miniflow_hash_5tuple(mf
, 0);
3990 dp_packet_set_rss_hash(packet
, hash
);
3993 /* The RSS hash must account for the recirculation depth to avoid
3994 * collisions in the exact match cache */
3995 recirc_depth
= *recirc_depth_get_unsafe();
3996 if (OVS_UNLIKELY(recirc_depth
)) {
3997 hash
= hash_finish(hash
, recirc_depth
);
3998 dp_packet_set_rss_hash(packet
, hash
);
4003 struct packet_batch_per_flow
{
4004 unsigned int byte_count
;
4006 struct dp_netdev_flow
*flow
;
4008 struct dp_packet_batch array
;
4012 packet_batch_per_flow_update(struct packet_batch_per_flow
*batch
,
4013 struct dp_packet
*packet
,
4014 const struct miniflow
*mf
)
4016 batch
->byte_count
+= dp_packet_size(packet
);
4017 batch
->tcp_flags
|= miniflow_get_tcp_flags(mf
);
4018 batch
->array
.packets
[batch
->array
.count
++] = packet
;
4022 packet_batch_per_flow_init(struct packet_batch_per_flow
*batch
,
4023 struct dp_netdev_flow
*flow
)
4025 flow
->batch
= batch
;
4028 dp_packet_batch_init(&batch
->array
);
4029 batch
->byte_count
= 0;
4030 batch
->tcp_flags
= 0;
4034 packet_batch_per_flow_execute(struct packet_batch_per_flow
*batch
,
4035 struct dp_netdev_pmd_thread
*pmd
,
4038 struct dp_netdev_actions
*actions
;
4039 struct dp_netdev_flow
*flow
= batch
->flow
;
4041 dp_netdev_flow_used(flow
, batch
->array
.count
, batch
->byte_count
,
4042 batch
->tcp_flags
, now
);
4044 actions
= dp_netdev_flow_get_actions(flow
);
4046 dp_netdev_execute_actions(pmd
, &batch
->array
, true, &flow
->flow
,
4047 actions
->actions
, actions
->size
, now
);
4051 dp_netdev_queue_batches(struct dp_packet
*pkt
,
4052 struct dp_netdev_flow
*flow
, const struct miniflow
*mf
,
4053 struct packet_batch_per_flow
*batches
, size_t *n_batches
)
4055 struct packet_batch_per_flow
*batch
= flow
->batch
;
4057 if (OVS_UNLIKELY(!batch
)) {
4058 batch
= &batches
[(*n_batches
)++];
4059 packet_batch_per_flow_init(batch
, flow
);
4062 packet_batch_per_flow_update(batch
, pkt
, mf
);
4065 /* Try to process all ('cnt') the 'packets' using only the exact match cache
4066 * 'pmd->flow_cache'. If a flow is not found for a packet 'packets[i]', the
4067 * miniflow is copied into 'keys' and the packet pointer is moved at the
4068 * beginning of the 'packets' array.
4070 * The function returns the number of packets that needs to be processed in the
4071 * 'packets' array (they have been moved to the beginning of the vector).
4073 * If 'md_is_valid' is false, the metadata in 'packets' is not valid and must be
4074 * initialized by this function using 'port_no'.
4076 static inline size_t
4077 emc_processing(struct dp_netdev_pmd_thread
*pmd
,
4078 struct dp_packet_batch
*packets_
,
4079 struct netdev_flow_key
*keys
,
4080 struct packet_batch_per_flow batches
[], size_t *n_batches
,
4081 bool md_is_valid
, odp_port_t port_no
)
4083 struct emc_cache
*flow_cache
= &pmd
->flow_cache
;
4084 struct netdev_flow_key
*key
= &keys
[0];
4085 size_t n_missed
= 0, n_dropped
= 0;
4086 struct dp_packet
*packet
;
4087 const size_t size
= dp_packet_batch_size(packets_
);
4090 DP_PACKET_BATCH_REFILL_FOR_EACH (i
, size
, packet
, packets_
) {
4091 struct dp_netdev_flow
*flow
;
4093 if (OVS_UNLIKELY(dp_packet_size(packet
) < ETH_HEADER_LEN
)) {
4094 dp_packet_delete(packet
);
4099 if (i
!= size
- 1) {
4100 struct dp_packet
**packets
= packets_
->packets
;
4101 /* Prefetch next packet data and metadata. */
4102 OVS_PREFETCH(dp_packet_data(packets
[i
+1]));
4103 pkt_metadata_prefetch_init(&packets
[i
+1]->md
);
4107 pkt_metadata_init(&packet
->md
, port_no
);
4109 miniflow_extract(packet
, &key
->mf
);
4110 key
->len
= 0; /* Not computed yet. */
4111 key
->hash
= dpif_netdev_packet_get_rss_hash(packet
, &key
->mf
);
4113 flow
= emc_lookup(flow_cache
, key
);
4114 if (OVS_LIKELY(flow
)) {
4115 dp_netdev_queue_batches(packet
, flow
, &key
->mf
, batches
,
4118 /* Exact match cache missed. Group missed packets together at
4119 * the beginning of the 'packets' array. */
4120 dp_packet_batch_refill(packets_
, packet
, i
);
4121 /* 'key[n_missed]' contains the key of the current packet and it
4122 * must be returned to the caller. The next key should be extracted
4123 * to 'keys[n_missed + 1]'. */
4124 key
= &keys
[++n_missed
];
4128 dp_netdev_count_packet(pmd
, DP_STAT_EXACT_HIT
, size
- n_dropped
- n_missed
);
4130 return dp_packet_batch_size(packets_
);
4134 handle_packet_upcall(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet
*packet
,
4135 const struct netdev_flow_key
*key
,
4136 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
,
4137 int *lost_cnt
, long long now
)
4139 struct ofpbuf
*add_actions
;
4140 struct dp_packet_batch b
;
4145 match
.tun_md
.valid
= false;
4146 miniflow_expand(&key
->mf
, &match
.flow
);
4148 ofpbuf_clear(actions
);
4149 ofpbuf_clear(put_actions
);
4151 dpif_flow_hash(pmd
->dp
->dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
4152 error
= dp_netdev_upcall(pmd
, packet
, &match
.flow
, &match
.wc
,
4153 &ufid
, DPIF_UC_MISS
, NULL
, actions
,
4155 if (OVS_UNLIKELY(error
&& error
!= ENOSPC
)) {
4156 dp_packet_delete(packet
);
4161 /* The Netlink encoding of datapath flow keys cannot express
4162 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
4163 * tag is interpreted as exact match on the fact that there is no
4164 * VLAN. Unless we refactor a lot of code that translates between
4165 * Netlink and struct flow representations, we have to do the same
4167 if (!match
.wc
.masks
.vlan_tci
) {
4168 match
.wc
.masks
.vlan_tci
= htons(0xffff);
4171 /* We can't allow the packet batching in the next loop to execute
4172 * the actions. Otherwise, if there are any slow path actions,
4173 * we'll send the packet up twice. */
4174 dp_packet_batch_init_packet(&b
, packet
);
4175 dp_netdev_execute_actions(pmd
, &b
, true, &match
.flow
,
4176 actions
->data
, actions
->size
, now
);
4178 add_actions
= put_actions
->size
? put_actions
: actions
;
4179 if (OVS_LIKELY(error
!= ENOSPC
)) {
4180 struct dp_netdev_flow
*netdev_flow
;
4182 /* XXX: There's a race window where a flow covering this packet
4183 * could have already been installed since we last did the flow
4184 * lookup before upcall. This could be solved by moving the
4185 * mutex lock outside the loop, but that's an awful long time
4186 * to be locking everyone out of making flow installs. If we
4187 * move to a per-core classifier, it would be reasonable. */
4188 ovs_mutex_lock(&pmd
->flow_mutex
);
4189 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
, NULL
);
4190 if (OVS_LIKELY(!netdev_flow
)) {
4191 netdev_flow
= dp_netdev_flow_add(pmd
, &match
, &ufid
,
4195 ovs_mutex_unlock(&pmd
->flow_mutex
);
4197 emc_insert(&pmd
->flow_cache
, key
, netdev_flow
);
4202 fast_path_processing(struct dp_netdev_pmd_thread
*pmd
,
4203 struct dp_packet_batch
*packets_
,
4204 struct netdev_flow_key
*keys
,
4205 struct packet_batch_per_flow batches
[], size_t *n_batches
,
4209 int cnt
= packets_
->count
;
4210 #if !defined(__CHECKER__) && !defined(_WIN32)
4211 const size_t PKT_ARRAY_SIZE
= cnt
;
4213 /* Sparse or MSVC doesn't like variable length array. */
4214 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
4216 struct dp_packet
**packets
= packets_
->packets
;
4218 struct dpcls_rule
*rules
[PKT_ARRAY_SIZE
];
4219 struct dp_netdev
*dp
= pmd
->dp
;
4220 struct emc_cache
*flow_cache
= &pmd
->flow_cache
;
4221 int miss_cnt
= 0, lost_cnt
= 0;
4222 int lookup_cnt
= 0, add_lookup_cnt
;
4226 for (i
= 0; i
< cnt
; i
++) {
4227 /* Key length is needed in all the cases, hash computed on demand. */
4228 keys
[i
].len
= netdev_flow_key_size(miniflow_n_values(&keys
[i
].mf
));
4230 /* Get the classifier for the in_port */
4231 cls
= dp_netdev_pmd_lookup_dpcls(pmd
, in_port
);
4232 if (OVS_LIKELY(cls
)) {
4233 any_miss
= !dpcls_lookup(cls
, keys
, rules
, cnt
, &lookup_cnt
);
4236 memset(rules
, 0, sizeof(rules
));
4238 if (OVS_UNLIKELY(any_miss
) && !fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
4239 uint64_t actions_stub
[512 / 8], slow_stub
[512 / 8];
4240 struct ofpbuf actions
, put_actions
;
4242 ofpbuf_use_stub(&actions
, actions_stub
, sizeof actions_stub
);
4243 ofpbuf_use_stub(&put_actions
, slow_stub
, sizeof slow_stub
);
4245 for (i
= 0; i
< cnt
; i
++) {
4246 struct dp_netdev_flow
*netdev_flow
;
4248 if (OVS_LIKELY(rules
[i
])) {
4252 /* It's possible that an earlier slow path execution installed
4253 * a rule covering this flow. In this case, it's a lot cheaper
4254 * to catch it here than execute a miss. */
4255 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, &keys
[i
],
4258 lookup_cnt
+= add_lookup_cnt
;
4259 rules
[i
] = &netdev_flow
->cr
;
4264 handle_packet_upcall(pmd
, packets
[i
], &keys
[i
], &actions
,
4265 &put_actions
, &lost_cnt
, now
);
4268 ofpbuf_uninit(&actions
);
4269 ofpbuf_uninit(&put_actions
);
4270 fat_rwlock_unlock(&dp
->upcall_rwlock
);
4271 } else if (OVS_UNLIKELY(any_miss
)) {
4272 for (i
= 0; i
< cnt
; i
++) {
4273 if (OVS_UNLIKELY(!rules
[i
])) {
4274 dp_packet_delete(packets
[i
]);
4281 for (i
= 0; i
< cnt
; i
++) {
4282 struct dp_packet
*packet
= packets
[i
];
4283 struct dp_netdev_flow
*flow
;
4285 if (OVS_UNLIKELY(!rules
[i
])) {
4289 flow
= dp_netdev_flow_cast(rules
[i
]);
4291 emc_insert(flow_cache
, &keys
[i
], flow
);
4292 dp_netdev_queue_batches(packet
, flow
, &keys
[i
].mf
, batches
, n_batches
);
4295 dp_netdev_count_packet(pmd
, DP_STAT_MASKED_HIT
, cnt
- miss_cnt
);
4296 dp_netdev_count_packet(pmd
, DP_STAT_LOOKUP_HIT
, lookup_cnt
);
4297 dp_netdev_count_packet(pmd
, DP_STAT_MISS
, miss_cnt
);
4298 dp_netdev_count_packet(pmd
, DP_STAT_LOST
, lost_cnt
);
4301 /* Packets enter the datapath from a port (or from recirculation) here.
4303 * For performance reasons a caller may choose not to initialize the metadata
4304 * in 'packets': in this case 'mdinit' is false and this function needs to
4305 * initialize it using 'port_no'. If the metadata in 'packets' is already
4306 * valid, 'md_is_valid' must be true and 'port_no' will be ignored. */
4308 dp_netdev_input__(struct dp_netdev_pmd_thread
*pmd
,
4309 struct dp_packet_batch
*packets
,
4310 bool md_is_valid
, odp_port_t port_no
)
4312 int cnt
= packets
->count
;
4313 #if !defined(__CHECKER__) && !defined(_WIN32)
4314 const size_t PKT_ARRAY_SIZE
= cnt
;
4316 /* Sparse or MSVC doesn't like variable length array. */
4317 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
4319 OVS_ALIGNED_VAR(CACHE_LINE_SIZE
) struct netdev_flow_key keys
[PKT_ARRAY_SIZE
];
4320 struct packet_batch_per_flow batches
[PKT_ARRAY_SIZE
];
4321 long long now
= time_msec();
4326 emc_processing(pmd
, packets
, keys
, batches
, &n_batches
,
4327 md_is_valid
, port_no
);
4328 if (!dp_packet_batch_is_empty(packets
)) {
4329 /* Get ingress port from first packet's metadata. */
4330 in_port
= packets
->packets
[0]->md
.in_port
.odp_port
;
4331 fast_path_processing(pmd
, packets
, keys
, batches
, &n_batches
, in_port
, now
);
4334 /* All the flow batches need to be reset before any call to
4335 * packet_batch_per_flow_execute() as it could potentially trigger
4336 * recirculation. When a packet matching flow ‘j’ happens to be
4337 * recirculated, the nested call to dp_netdev_input__() could potentially
4338 * classify the packet as matching another flow - say 'k'. It could happen
4339 * that in the previous call to dp_netdev_input__() that same flow 'k' had
4340 * already its own batches[k] still waiting to be served. So if its
4341 * ‘batch’ member is not reset, the recirculated packet would be wrongly
4342 * appended to batches[k] of the 1st call to dp_netdev_input__(). */
4344 for (i
= 0; i
< n_batches
; i
++) {
4345 batches
[i
].flow
->batch
= NULL
;
4348 for (i
= 0; i
< n_batches
; i
++) {
4349 packet_batch_per_flow_execute(&batches
[i
], pmd
, now
);
4354 dp_netdev_input(struct dp_netdev_pmd_thread
*pmd
,
4355 struct dp_packet_batch
*packets
,
4358 dp_netdev_input__(pmd
, packets
, false, port_no
);
4362 dp_netdev_recirculate(struct dp_netdev_pmd_thread
*pmd
,
4363 struct dp_packet_batch
*packets
)
4365 dp_netdev_input__(pmd
, packets
, true, 0);
4368 struct dp_netdev_execute_aux
{
4369 struct dp_netdev_pmd_thread
*pmd
;
4371 const struct flow
*flow
;
4375 dpif_netdev_register_dp_purge_cb(struct dpif
*dpif
, dp_purge_callback
*cb
,
4378 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4379 dp
->dp_purge_aux
= aux
;
4380 dp
->dp_purge_cb
= cb
;
4384 dpif_netdev_register_upcall_cb(struct dpif
*dpif
, upcall_callback
*cb
,
4387 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4388 dp
->upcall_aux
= aux
;
4393 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
4394 long long now
, bool purge
)
4397 struct dp_netdev_port
*port
;
4400 HMAP_FOR_EACH (tx
, node
, &pmd
->send_port_cache
) {
4401 if (!tx
->port
->dynamic_txqs
) {
4404 interval
= now
- tx
->last_used
;
4405 if (tx
->qid
>= 0 && (purge
|| interval
>= XPS_TIMEOUT_MS
)) {
4407 ovs_mutex_lock(&port
->txq_used_mutex
);
4408 port
->txq_used
[tx
->qid
]--;
4409 ovs_mutex_unlock(&port
->txq_used_mutex
);
4416 dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
4417 struct tx_port
*tx
, long long now
)
4419 struct dp_netdev_port
*port
;
4421 int i
, min_cnt
, min_qid
;
4423 if (OVS_UNLIKELY(!now
)) {
4427 interval
= now
- tx
->last_used
;
4428 tx
->last_used
= now
;
4430 if (OVS_LIKELY(tx
->qid
>= 0 && interval
< XPS_TIMEOUT_MS
)) {
4436 ovs_mutex_lock(&port
->txq_used_mutex
);
4438 port
->txq_used
[tx
->qid
]--;
4444 for (i
= 0; i
< netdev_n_txq(port
->netdev
); i
++) {
4445 if (port
->txq_used
[i
] < min_cnt
|| min_cnt
== -1) {
4446 min_cnt
= port
->txq_used
[i
];
4451 port
->txq_used
[min_qid
]++;
4454 ovs_mutex_unlock(&port
->txq_used_mutex
);
4456 dpif_netdev_xps_revalidate_pmd(pmd
, now
, false);
4458 VLOG_DBG("Core %d: New TX queue ID %d for port \'%s\'.",
4459 pmd
->core_id
, tx
->qid
, netdev_get_name(tx
->port
->netdev
));
4463 static struct tx_port
*
4464 pmd_tnl_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
4467 return tx_port_lookup(&pmd
->tnl_port_cache
, port_no
);
4470 static struct tx_port
*
4471 pmd_send_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
4474 return tx_port_lookup(&pmd
->send_port_cache
, port_no
);
4478 push_tnl_action(const struct dp_netdev_pmd_thread
*pmd
,
4479 const struct nlattr
*attr
,
4480 struct dp_packet_batch
*batch
)
4482 struct tx_port
*tun_port
;
4483 const struct ovs_action_push_tnl
*data
;
4486 data
= nl_attr_get(attr
);
4488 tun_port
= pmd_tnl_port_cache_lookup(pmd
, u32_to_odp(data
->tnl_port
));
4493 err
= netdev_push_header(tun_port
->port
->netdev
, batch
, data
);
4498 dp_packet_delete_batch(batch
, true);
4503 dp_execute_userspace_action(struct dp_netdev_pmd_thread
*pmd
,
4504 struct dp_packet
*packet
, bool may_steal
,
4505 struct flow
*flow
, ovs_u128
*ufid
,
4506 struct ofpbuf
*actions
,
4507 const struct nlattr
*userdata
, long long now
)
4509 struct dp_packet_batch b
;
4512 ofpbuf_clear(actions
);
4514 error
= dp_netdev_upcall(pmd
, packet
, flow
, NULL
, ufid
,
4515 DPIF_UC_ACTION
, userdata
, actions
,
4517 if (!error
|| error
== ENOSPC
) {
4518 dp_packet_batch_init_packet(&b
, packet
);
4519 dp_netdev_execute_actions(pmd
, &b
, may_steal
, flow
,
4520 actions
->data
, actions
->size
, now
);
4521 } else if (may_steal
) {
4522 dp_packet_delete(packet
);
4527 dp_execute_cb(void *aux_
, struct dp_packet_batch
*packets_
,
4528 const struct nlattr
*a
, bool may_steal
)
4530 struct dp_netdev_execute_aux
*aux
= aux_
;
4531 uint32_t *depth
= recirc_depth_get();
4532 struct dp_netdev_pmd_thread
*pmd
= aux
->pmd
;
4533 struct dp_netdev
*dp
= pmd
->dp
;
4534 int type
= nl_attr_type(a
);
4535 long long now
= aux
->now
;
4538 switch ((enum ovs_action_attr
)type
) {
4539 case OVS_ACTION_ATTR_OUTPUT
:
4540 p
= pmd_send_port_cache_lookup(pmd
, nl_attr_get_odp_port(a
));
4541 if (OVS_LIKELY(p
)) {
4545 dynamic_txqs
= p
->port
->dynamic_txqs
;
4547 tx_qid
= dpif_netdev_xps_get_tx_qid(pmd
, p
, now
);
4549 tx_qid
= pmd
->static_tx_qid
;
4552 netdev_send(p
->port
->netdev
, tx_qid
, packets_
, may_steal
,
4558 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
4559 if (*depth
< MAX_RECIRC_DEPTH
) {
4560 struct dp_packet_batch tnl_pkt
;
4561 struct dp_packet_batch
*orig_packets_
= packets_
;
4565 dp_packet_batch_clone(&tnl_pkt
, packets_
);
4566 packets_
= &tnl_pkt
;
4567 dp_packet_batch_reset_cutlen(orig_packets_
);
4570 dp_packet_batch_apply_cutlen(packets_
);
4572 err
= push_tnl_action(pmd
, a
, packets_
);
4575 dp_netdev_recirculate(pmd
, packets_
);
4582 case OVS_ACTION_ATTR_TUNNEL_POP
:
4583 if (*depth
< MAX_RECIRC_DEPTH
) {
4584 struct dp_packet_batch
*orig_packets_
= packets_
;
4585 odp_port_t portno
= nl_attr_get_odp_port(a
);
4587 p
= pmd_tnl_port_cache_lookup(pmd
, portno
);
4589 struct dp_packet_batch tnl_pkt
;
4592 dp_packet_batch_clone(&tnl_pkt
, packets_
);
4593 packets_
= &tnl_pkt
;
4594 dp_packet_batch_reset_cutlen(orig_packets_
);
4597 dp_packet_batch_apply_cutlen(packets_
);
4599 netdev_pop_header(p
->port
->netdev
, packets_
);
4600 if (dp_packet_batch_is_empty(packets_
)) {
4604 struct dp_packet
*packet
;
4605 DP_PACKET_BATCH_FOR_EACH (packet
, packets_
) {
4606 packet
->md
.in_port
.odp_port
= portno
;
4610 dp_netdev_recirculate(pmd
, packets_
);
4617 case OVS_ACTION_ATTR_USERSPACE
:
4618 if (!fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
4619 struct dp_packet_batch
*orig_packets_
= packets_
;
4620 const struct nlattr
*userdata
;
4621 struct dp_packet_batch usr_pkt
;
4622 struct ofpbuf actions
;
4627 userdata
= nl_attr_find_nested(a
, OVS_USERSPACE_ATTR_USERDATA
);
4628 ofpbuf_init(&actions
, 0);
4630 if (packets_
->trunc
) {
4632 dp_packet_batch_clone(&usr_pkt
, packets_
);
4633 packets_
= &usr_pkt
;
4635 dp_packet_batch_reset_cutlen(orig_packets_
);
4638 dp_packet_batch_apply_cutlen(packets_
);
4641 struct dp_packet
*packet
;
4642 DP_PACKET_BATCH_FOR_EACH (packet
, packets_
) {
4643 flow_extract(packet
, &flow
);
4644 dpif_flow_hash(dp
->dpif
, &flow
, sizeof flow
, &ufid
);
4645 dp_execute_userspace_action(pmd
, packet
, may_steal
, &flow
,
4646 &ufid
, &actions
, userdata
, now
);
4650 dp_packet_delete_batch(packets_
, true);
4653 ofpbuf_uninit(&actions
);
4654 fat_rwlock_unlock(&dp
->upcall_rwlock
);
4660 case OVS_ACTION_ATTR_RECIRC
:
4661 if (*depth
< MAX_RECIRC_DEPTH
) {
4662 struct dp_packet_batch recirc_pkts
;
4665 dp_packet_batch_clone(&recirc_pkts
, packets_
);
4666 packets_
= &recirc_pkts
;
4669 struct dp_packet
*packet
;
4670 DP_PACKET_BATCH_FOR_EACH (packet
, packets_
) {
4671 packet
->md
.recirc_id
= nl_attr_get_u32(a
);
4675 dp_netdev_recirculate(pmd
, packets_
);
4681 VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
4684 case OVS_ACTION_ATTR_CT
: {
4685 const struct nlattr
*b
;
4686 bool commit
= false;
4689 const char *helper
= NULL
;
4690 const uint32_t *setmark
= NULL
;
4691 const struct ovs_key_ct_labels
*setlabel
= NULL
;
4693 NL_ATTR_FOR_EACH_UNSAFE (b
, left
, nl_attr_get(a
),
4694 nl_attr_get_size(a
)) {
4695 enum ovs_ct_attr sub_type
= nl_attr_type(b
);
4698 case OVS_CT_ATTR_COMMIT
:
4701 case OVS_CT_ATTR_ZONE
:
4702 zone
= nl_attr_get_u16(b
);
4704 case OVS_CT_ATTR_HELPER
:
4705 helper
= nl_attr_get_string(b
);
4707 case OVS_CT_ATTR_MARK
:
4708 setmark
= nl_attr_get(b
);
4710 case OVS_CT_ATTR_LABELS
:
4711 setlabel
= nl_attr_get(b
);
4713 case OVS_CT_ATTR_NAT
:
4714 case OVS_CT_ATTR_UNSPEC
:
4715 case __OVS_CT_ATTR_MAX
:
4720 conntrack_execute(&dp
->conntrack
, packets_
, aux
->flow
->dl_type
, commit
,
4721 zone
, setmark
, setlabel
, helper
);
4725 case OVS_ACTION_ATTR_PUSH_VLAN
:
4726 case OVS_ACTION_ATTR_POP_VLAN
:
4727 case OVS_ACTION_ATTR_PUSH_MPLS
:
4728 case OVS_ACTION_ATTR_POP_MPLS
:
4729 case OVS_ACTION_ATTR_SET
:
4730 case OVS_ACTION_ATTR_SET_MASKED
:
4731 case OVS_ACTION_ATTR_SAMPLE
:
4732 case OVS_ACTION_ATTR_HASH
:
4733 case OVS_ACTION_ATTR_UNSPEC
:
4734 case OVS_ACTION_ATTR_TRUNC
:
4735 case OVS_ACTION_ATTR_CLONE
:
4736 case __OVS_ACTION_ATTR_MAX
:
4740 dp_packet_delete_batch(packets_
, may_steal
);
4744 dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
4745 struct dp_packet_batch
*packets
,
4746 bool may_steal
, const struct flow
*flow
,
4747 const struct nlattr
*actions
, size_t actions_len
,
4750 struct dp_netdev_execute_aux aux
= { pmd
, now
, flow
};
4752 odp_execute_actions(&aux
, packets
, may_steal
, actions
,
4753 actions_len
, dp_execute_cb
);
4756 struct dp_netdev_ct_dump
{
4757 struct ct_dpif_dump_state up
;
4758 struct conntrack_dump dump
;
4759 struct conntrack
*ct
;
4760 struct dp_netdev
*dp
;
4764 dpif_netdev_ct_dump_start(struct dpif
*dpif
, struct ct_dpif_dump_state
**dump_
,
4765 const uint16_t *pzone
)
4767 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4768 struct dp_netdev_ct_dump
*dump
;
4770 dump
= xzalloc(sizeof *dump
);
4772 dump
->ct
= &dp
->conntrack
;
4774 conntrack_dump_start(&dp
->conntrack
, &dump
->dump
, pzone
);
4782 dpif_netdev_ct_dump_next(struct dpif
*dpif OVS_UNUSED
,
4783 struct ct_dpif_dump_state
*dump_
,
4784 struct ct_dpif_entry
*entry
)
4786 struct dp_netdev_ct_dump
*dump
;
4788 INIT_CONTAINER(dump
, dump_
, up
);
4790 return conntrack_dump_next(&dump
->dump
, entry
);
4794 dpif_netdev_ct_dump_done(struct dpif
*dpif OVS_UNUSED
,
4795 struct ct_dpif_dump_state
*dump_
)
4797 struct dp_netdev_ct_dump
*dump
;
4800 INIT_CONTAINER(dump
, dump_
, up
);
4802 err
= conntrack_dump_done(&dump
->dump
);
4810 dpif_netdev_ct_flush(struct dpif
*dpif
, const uint16_t *zone
)
4812 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4814 return conntrack_flush(&dp
->conntrack
, zone
);
4817 const struct dpif_class dpif_netdev_class
= {
4820 dpif_netdev_enumerate
,
4821 dpif_netdev_port_open_type
,
4824 dpif_netdev_destroy
,
4827 dpif_netdev_get_stats
,
4828 dpif_netdev_port_add
,
4829 dpif_netdev_port_del
,
4830 dpif_netdev_port_set_config
,
4831 dpif_netdev_port_query_by_number
,
4832 dpif_netdev_port_query_by_name
,
4833 NULL
, /* port_get_pid */
4834 dpif_netdev_port_dump_start
,
4835 dpif_netdev_port_dump_next
,
4836 dpif_netdev_port_dump_done
,
4837 dpif_netdev_port_poll
,
4838 dpif_netdev_port_poll_wait
,
4839 dpif_netdev_flow_flush
,
4840 dpif_netdev_flow_dump_create
,
4841 dpif_netdev_flow_dump_destroy
,
4842 dpif_netdev_flow_dump_thread_create
,
4843 dpif_netdev_flow_dump_thread_destroy
,
4844 dpif_netdev_flow_dump_next
,
4845 dpif_netdev_operate
,
4846 NULL
, /* recv_set */
4847 NULL
, /* handlers_set */
4848 dpif_netdev_set_config
,
4849 dpif_netdev_queue_to_priority
,
4851 NULL
, /* recv_wait */
4852 NULL
, /* recv_purge */
4853 dpif_netdev_register_dp_purge_cb
,
4854 dpif_netdev_register_upcall_cb
,
4855 dpif_netdev_enable_upcall
,
4856 dpif_netdev_disable_upcall
,
4857 dpif_netdev_get_datapath_version
,
4858 dpif_netdev_ct_dump_start
,
4859 dpif_netdev_ct_dump_next
,
4860 dpif_netdev_ct_dump_done
,
4861 dpif_netdev_ct_flush
,
4865 dpif_dummy_change_port_number(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
4866 const char *argv
[], void *aux OVS_UNUSED
)
4868 struct dp_netdev_port
*port
;
4869 struct dp_netdev
*dp
;
4872 ovs_mutex_lock(&dp_netdev_mutex
);
4873 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
4874 if (!dp
|| !dpif_netdev_class_is_dummy(dp
->class)) {
4875 ovs_mutex_unlock(&dp_netdev_mutex
);
4876 unixctl_command_reply_error(conn
, "unknown datapath or not a dummy");
4879 ovs_refcount_ref(&dp
->ref_cnt
);
4880 ovs_mutex_unlock(&dp_netdev_mutex
);
4882 ovs_mutex_lock(&dp
->port_mutex
);
4883 if (get_port_by_name(dp
, argv
[2], &port
)) {
4884 unixctl_command_reply_error(conn
, "unknown port");
4888 port_no
= u32_to_odp(atoi(argv
[3]));
4889 if (!port_no
|| port_no
== ODPP_NONE
) {
4890 unixctl_command_reply_error(conn
, "bad port number");
4893 if (dp_netdev_lookup_port(dp
, port_no
)) {
4894 unixctl_command_reply_error(conn
, "port number already in use");
4899 hmap_remove(&dp
->ports
, &port
->node
);
4900 reconfigure_datapath(dp
);
4902 /* Reinsert with new port number. */
4903 port
->port_no
= port_no
;
4904 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
4905 reconfigure_datapath(dp
);
4907 seq_change(dp
->port_seq
);
4908 unixctl_command_reply(conn
, NULL
);
4911 ovs_mutex_unlock(&dp
->port_mutex
);
4912 dp_netdev_unref(dp
);
4916 dpif_dummy_register__(const char *type
)
4918 struct dpif_class
*class;
4920 class = xmalloc(sizeof *class);
4921 *class = dpif_netdev_class
;
4922 class->type
= xstrdup(type
);
4923 dp_register_provider(class);
4927 dpif_dummy_override(const char *type
)
4932 * Ignore EAFNOSUPPORT to allow --enable-dummy=system with
4933 * a userland-only build. It's useful for testsuite.
4935 error
= dp_unregister_provider(type
);
4936 if (error
== 0 || error
== EAFNOSUPPORT
) {
4937 dpif_dummy_register__(type
);
4942 dpif_dummy_register(enum dummy_level level
)
4944 if (level
== DUMMY_OVERRIDE_ALL
) {
4949 dp_enumerate_types(&types
);
4950 SSET_FOR_EACH (type
, &types
) {
4951 dpif_dummy_override(type
);
4953 sset_destroy(&types
);
4954 } else if (level
== DUMMY_OVERRIDE_SYSTEM
) {
4955 dpif_dummy_override("system");
4958 dpif_dummy_register__("dummy");
4960 unixctl_command_register("dpif-dummy/change-port-number",
4961 "dp port new-number",
4962 3, 3, dpif_dummy_change_port_number
, NULL
);
4965 /* Datapath Classifier. */
4967 /* A set of rules that all have the same fields wildcarded. */
4968 struct dpcls_subtable
{
4969 /* The fields are only used by writers. */
4970 struct cmap_node cmap_node OVS_GUARDED
; /* Within dpcls 'subtables_map'. */
4972 /* These fields are accessed by readers. */
4973 struct cmap rules
; /* Contains "struct dpcls_rule"s. */
4974 uint32_t hit_cnt
; /* Number of match hits in subtable in current
4975 optimization interval. */
4976 struct netdev_flow_key mask
; /* Wildcards for fields (const). */
4977 /* 'mask' must be the last field, additional space is allocated here. */
4980 /* Initializes 'cls' as a classifier that initially contains no classification
4983 dpcls_init(struct dpcls
*cls
)
4985 cmap_init(&cls
->subtables_map
);
4986 pvector_init(&cls
->subtables
);
4990 dpcls_destroy_subtable(struct dpcls
*cls
, struct dpcls_subtable
*subtable
)
4992 VLOG_DBG("Destroying subtable %p for in_port %d", subtable
, cls
->in_port
);
4993 pvector_remove(&cls
->subtables
, subtable
);
4994 cmap_remove(&cls
->subtables_map
, &subtable
->cmap_node
,
4995 subtable
->mask
.hash
);
4996 cmap_destroy(&subtable
->rules
);
4997 ovsrcu_postpone(free
, subtable
);
5000 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
5001 * caller's responsibility.
5002 * May only be called after all the readers have been terminated. */
5004 dpcls_destroy(struct dpcls
*cls
)
5007 struct dpcls_subtable
*subtable
;
5009 CMAP_FOR_EACH (subtable
, cmap_node
, &cls
->subtables_map
) {
5010 ovs_assert(cmap_count(&subtable
->rules
) == 0);
5011 dpcls_destroy_subtable(cls
, subtable
);
5013 cmap_destroy(&cls
->subtables_map
);
5014 pvector_destroy(&cls
->subtables
);
5018 static struct dpcls_subtable
*
5019 dpcls_create_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
5021 struct dpcls_subtable
*subtable
;
5023 /* Need to add one. */
5024 subtable
= xmalloc(sizeof *subtable
5025 - sizeof subtable
->mask
.mf
+ mask
->len
);
5026 cmap_init(&subtable
->rules
);
5027 subtable
->hit_cnt
= 0;
5028 netdev_flow_key_clone(&subtable
->mask
, mask
);
5029 cmap_insert(&cls
->subtables_map
, &subtable
->cmap_node
, mask
->hash
);
5030 /* Add the new subtable at the end of the pvector (with no hits yet) */
5031 pvector_insert(&cls
->subtables
, subtable
, 0);
5032 VLOG_DBG("Creating %"PRIuSIZE
". subtable %p for in_port %d",
5033 cmap_count(&cls
->subtables_map
), subtable
, cls
->in_port
);
5034 pvector_publish(&cls
->subtables
);
5039 static inline struct dpcls_subtable
*
5040 dpcls_find_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
5042 struct dpcls_subtable
*subtable
;
5044 CMAP_FOR_EACH_WITH_HASH (subtable
, cmap_node
, mask
->hash
,
5045 &cls
->subtables_map
) {
5046 if (netdev_flow_key_equal(&subtable
->mask
, mask
)) {
5050 return dpcls_create_subtable(cls
, mask
);
5054 /* Periodically sort the dpcls subtable vectors according to hit counts */
5056 dpcls_sort_subtable_vector(struct dpcls
*cls
)
5058 struct pvector
*pvec
= &cls
->subtables
;
5059 struct dpcls_subtable
*subtable
;
5061 PVECTOR_FOR_EACH (subtable
, pvec
) {
5062 pvector_change_priority(pvec
, subtable
, subtable
->hit_cnt
);
5063 subtable
->hit_cnt
= 0;
5065 pvector_publish(pvec
);
5069 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread
*pmd
)
5072 long long int now
= time_msec();
5074 if (now
> pmd
->next_optimization
) {
5075 /* Try to obtain the flow lock to block out revalidator threads.
5076 * If not possible, just try next time. */
5077 if (!ovs_mutex_trylock(&pmd
->flow_mutex
)) {
5078 /* Optimize each classifier */
5079 CMAP_FOR_EACH (cls
, node
, &pmd
->classifiers
) {
5080 dpcls_sort_subtable_vector(cls
);
5082 ovs_mutex_unlock(&pmd
->flow_mutex
);
5083 /* Start new measuring interval */
5084 pmd
->next_optimization
= now
+ DPCLS_OPTIMIZATION_INTERVAL
;
5089 /* Insert 'rule' into 'cls'. */
5091 dpcls_insert(struct dpcls
*cls
, struct dpcls_rule
*rule
,
5092 const struct netdev_flow_key
*mask
)
5094 struct dpcls_subtable
*subtable
= dpcls_find_subtable(cls
, mask
);
5096 /* Refer to subtable's mask, also for later removal. */
5097 rule
->mask
= &subtable
->mask
;
5098 cmap_insert(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
);
5101 /* Removes 'rule' from 'cls', also destructing the 'rule'. */
5103 dpcls_remove(struct dpcls
*cls
, struct dpcls_rule
*rule
)
5105 struct dpcls_subtable
*subtable
;
5107 ovs_assert(rule
->mask
);
5109 /* Get subtable from reference in rule->mask. */
5110 INIT_CONTAINER(subtable
, rule
->mask
, mask
);
5111 if (cmap_remove(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
)
5113 /* Delete empty subtable. */
5114 dpcls_destroy_subtable(cls
, subtable
);
5115 pvector_publish(&cls
->subtables
);
5119 /* Returns true if 'target' satisfies 'key' in 'mask', that is, if each 1-bit
5120 * in 'mask' the values in 'key' and 'target' are the same. */
5122 dpcls_rule_matches_key(const struct dpcls_rule
*rule
,
5123 const struct netdev_flow_key
*target
)
5125 const uint64_t *keyp
= miniflow_get_values(&rule
->flow
.mf
);
5126 const uint64_t *maskp
= miniflow_get_values(&rule
->mask
->mf
);
5129 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, target
, rule
->flow
.mf
.map
) {
5130 if (OVS_UNLIKELY((value
& *maskp
++) != *keyp
++)) {
5137 /* For each miniflow in 'keys' performs a classifier lookup writing the result
5138 * into the corresponding slot in 'rules'. If a particular entry in 'keys' is
5139 * NULL it is skipped.
5141 * This function is optimized for use in the userspace datapath and therefore
5142 * does not implement a lot of features available in the standard
5143 * classifier_lookup() function. Specifically, it does not implement
5144 * priorities, instead returning any rule which matches the flow.
5146 * Returns true if all miniflows found a corresponding rule. */
5148 dpcls_lookup(struct dpcls
*cls
, const struct netdev_flow_key keys
[],
5149 struct dpcls_rule
**rules
, const size_t cnt
,
5152 /* The received 'cnt' miniflows are the search-keys that will be processed
5153 * to find a matching entry into the available subtables.
5154 * The number of bits in map_type is equal to NETDEV_MAX_BURST. */
5155 typedef uint32_t map_type
;
5156 #define MAP_BITS (sizeof(map_type) * CHAR_BIT)
5157 BUILD_ASSERT_DECL(MAP_BITS
>= NETDEV_MAX_BURST
);
5159 struct dpcls_subtable
*subtable
;
5161 map_type keys_map
= TYPE_MAXIMUM(map_type
); /* Set all bits. */
5163 uint32_t hashes
[MAP_BITS
];
5164 const struct cmap_node
*nodes
[MAP_BITS
];
5166 if (cnt
!= MAP_BITS
) {
5167 keys_map
>>= MAP_BITS
- cnt
; /* Clear extra bits. */
5169 memset(rules
, 0, cnt
* sizeof *rules
);
5171 int lookups_match
= 0, subtable_pos
= 1;
5173 /* The Datapath classifier - aka dpcls - is composed of subtables.
5174 * Subtables are dynamically created as needed when new rules are inserted.
5175 * Each subtable collects rules with matches on a specific subset of packet
5176 * fields as defined by the subtable's mask. We proceed to process every
5177 * search-key against each subtable, but when a match is found for a
5178 * search-key, the search for that key can stop because the rules are
5179 * non-overlapping. */
5180 PVECTOR_FOR_EACH (subtable
, &cls
->subtables
) {
5183 /* Compute hashes for the remaining keys. Each search-key is
5184 * masked with the subtable's mask to avoid hashing the wildcarded
5186 ULLONG_FOR_EACH_1(i
, keys_map
) {
5187 hashes
[i
] = netdev_flow_key_hash_in_mask(&keys
[i
],
5191 found_map
= cmap_find_batch(&subtable
->rules
, keys_map
, hashes
, nodes
);
5192 /* Check results. When the i-th bit of found_map is set, it means
5193 * that a set of nodes with a matching hash value was found for the
5194 * i-th search-key. Due to possible hash collisions we need to check
5195 * which of the found rules, if any, really matches our masked
5197 ULLONG_FOR_EACH_1(i
, found_map
) {
5198 struct dpcls_rule
*rule
;
5200 CMAP_NODE_FOR_EACH (rule
, cmap_node
, nodes
[i
]) {
5201 if (OVS_LIKELY(dpcls_rule_matches_key(rule
, &keys
[i
]))) {
5203 /* Even at 20 Mpps the 32-bit hit_cnt cannot wrap
5204 * within one second optimization interval. */
5205 subtable
->hit_cnt
++;
5206 lookups_match
+= subtable_pos
;
5210 /* None of the found rules was a match. Reset the i-th bit to
5211 * keep searching this key in the next subtable. */
5212 ULLONG_SET0(found_map
, i
); /* Did not match. */
5214 ; /* Keep Sparse happy. */
5216 keys_map
&= ~found_map
; /* Clear the found rules. */
5218 if (num_lookups_p
) {
5219 *num_lookups_p
= lookups_match
;
5221 return true; /* All found. */
5225 if (num_lookups_p
) {
5226 *num_lookups_p
= lookups_match
;
5228 return false; /* Some misses. */