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>
38 #include "dp-packet.h"
40 #include "dpif-provider.h"
42 #include "fat-rwlock.h"
47 #include "netdev-dpdk.h"
48 #include "netdev-vport.h"
50 #include "odp-execute.h"
52 #include "openvswitch/dynamic-string.h"
53 #include "openvswitch/list.h"
54 #include "openvswitch/match.h"
55 #include "openvswitch/ofp-print.h"
56 #include "openvswitch/ofpbuf.h"
57 #include "openvswitch/vlog.h"
61 #include "poll-loop.h"
65 #include "openvswitch/shash.h"
68 #include "tnl-neigh-cache.h"
69 #include "tnl-ports.h"
73 VLOG_DEFINE_THIS_MODULE(dpif_netdev
);
75 #define FLOW_DUMP_MAX_BATCH 50
76 /* Use per thread recirc_depth to prevent recirculation loop. */
77 #define MAX_RECIRC_DEPTH 5
78 DEFINE_STATIC_PER_THREAD_DATA(uint32_t, recirc_depth
, 0)
80 /* Configuration parameters. */
81 enum { MAX_FLOWS
= 65536 }; /* Maximum number of flows in flow table. */
83 /* Protects against changes to 'dp_netdevs'. */
84 static struct ovs_mutex dp_netdev_mutex
= OVS_MUTEX_INITIALIZER
;
86 /* Contains all 'struct dp_netdev's. */
87 static struct shash dp_netdevs
OVS_GUARDED_BY(dp_netdev_mutex
)
88 = SHASH_INITIALIZER(&dp_netdevs
);
90 static struct vlog_rate_limit upcall_rl
= VLOG_RATE_LIMIT_INIT(600, 600);
92 static struct odp_support dp_netdev_support
= {
93 .max_mpls_depth
= SIZE_MAX
,
97 /* Stores a miniflow with inline values */
99 struct netdev_flow_key
{
100 uint32_t hash
; /* Hash function differs for different users. */
101 uint32_t len
; /* Length of the following miniflow (incl. map). */
103 uint64_t buf
[FLOW_MAX_PACKET_U64S
];
106 /* Exact match cache for frequently used flows
108 * The cache uses a 32-bit hash of the packet (which can be the RSS hash) to
109 * search its entries for a miniflow that matches exactly the miniflow of the
110 * packet. It stores the 'dpcls_rule' (rule) that matches the miniflow.
112 * A cache entry holds a reference to its 'dp_netdev_flow'.
114 * A miniflow with a given hash can be in one of EM_FLOW_HASH_SEGS different
115 * entries. The 32-bit hash is split into EM_FLOW_HASH_SEGS values (each of
116 * them is EM_FLOW_HASH_SHIFT bits wide and the remainder is thrown away). Each
117 * value is the index of a cache entry where the miniflow could be.
123 * Each pmd_thread has its own private exact match cache.
124 * If dp_netdev_input is not called from a pmd thread, a mutex is used.
127 #define EM_FLOW_HASH_SHIFT 13
128 #define EM_FLOW_HASH_ENTRIES (1u << EM_FLOW_HASH_SHIFT)
129 #define EM_FLOW_HASH_MASK (EM_FLOW_HASH_ENTRIES - 1)
130 #define EM_FLOW_HASH_SEGS 2
133 struct dp_netdev_flow
*flow
;
134 struct netdev_flow_key key
; /* key.hash used for emc hash value. */
138 struct emc_entry entries
[EM_FLOW_HASH_ENTRIES
];
139 int sweep_idx
; /* For emc_cache_slow_sweep(). */
142 /* Iterate in the exact match cache through every entry that might contain a
143 * miniflow with hash 'HASH'. */
144 #define EMC_FOR_EACH_POS_WITH_HASH(EMC, CURRENT_ENTRY, HASH) \
145 for (uint32_t i__ = 0, srch_hash__ = (HASH); \
146 (CURRENT_ENTRY) = &(EMC)->entries[srch_hash__ & EM_FLOW_HASH_MASK], \
147 i__ < EM_FLOW_HASH_SEGS; \
148 i__++, srch_hash__ >>= EM_FLOW_HASH_SHIFT)
150 /* Simple non-wildcarding single-priority classifier. */
153 struct cmap subtables_map
;
154 struct pvector subtables
;
157 /* A rule to be inserted to the classifier. */
159 struct cmap_node cmap_node
; /* Within struct dpcls_subtable 'rules'. */
160 struct netdev_flow_key
*mask
; /* Subtable's mask. */
161 struct netdev_flow_key flow
; /* Matching key. */
162 /* 'flow' must be the last field, additional space is allocated here. */
165 static void dpcls_init(struct dpcls
*);
166 static void dpcls_destroy(struct dpcls
*);
167 static void dpcls_insert(struct dpcls
*, struct dpcls_rule
*,
168 const struct netdev_flow_key
*mask
);
169 static void dpcls_remove(struct dpcls
*, struct dpcls_rule
*);
170 static bool dpcls_lookup(const struct dpcls
*cls
,
171 const struct netdev_flow_key keys
[],
172 struct dpcls_rule
**rules
, size_t cnt
);
174 /* Datapath based on the network device interface from netdev.h.
180 * Some members, marked 'const', are immutable. Accessing other members
181 * requires synchronization, as noted in more detail below.
183 * Acquisition order is, from outermost to innermost:
185 * dp_netdev_mutex (global)
190 const struct dpif_class
*const class;
191 const char *const name
;
193 struct ovs_refcount ref_cnt
;
194 atomic_flag destroyed
;
198 * Any lookup into 'ports' or any access to the dp_netdev_ports found
199 * through 'ports' requires taking 'port_mutex'. */
200 struct ovs_mutex port_mutex
;
202 struct seq
*port_seq
; /* Incremented whenever a port changes. */
204 /* Protects access to ofproto-dpif-upcall interface during revalidator
205 * thread synchronization. */
206 struct fat_rwlock upcall_rwlock
;
207 upcall_callback
*upcall_cb
; /* Callback function for executing upcalls. */
210 /* Callback function for notifying the purging of dp flows (during
211 * reseting pmd deletion). */
212 dp_purge_callback
*dp_purge_cb
;
215 /* Stores all 'struct dp_netdev_pmd_thread's. */
216 struct cmap poll_threads
;
218 /* Protects the access of the 'struct dp_netdev_pmd_thread'
219 * instance for non-pmd thread. */
220 struct ovs_mutex non_pmd_mutex
;
222 /* Each pmd thread will store its pointer to
223 * 'struct dp_netdev_pmd_thread' in 'per_pmd_key'. */
224 ovsthread_key_t per_pmd_key
;
226 /* Cpu mask for pin of pmd threads. */
227 char *requested_pmd_cmask
;
230 uint64_t last_tnl_conf_seq
;
233 static struct dp_netdev_port
*dp_netdev_lookup_port(const struct dp_netdev
*dp
,
235 OVS_REQUIRES(dp
->port_mutex
);
238 DP_STAT_EXACT_HIT
, /* Packets that had an exact match (emc). */
239 DP_STAT_MASKED_HIT
, /* Packets that matched in the flow table. */
240 DP_STAT_MISS
, /* Packets that did not match. */
241 DP_STAT_LOST
, /* Packets not passed up to the client. */
245 enum pmd_cycles_counter_type
{
246 PMD_CYCLES_POLLING
, /* Cycles spent polling NICs. */
247 PMD_CYCLES_PROCESSING
, /* Cycles spent processing packets */
251 #define XPS_TIMEOUT_MS 500LL
253 /* A port in a netdev-based datapath. */
254 struct dp_netdev_port
{
256 struct netdev
*netdev
;
257 struct hmap_node node
; /* Node in dp_netdev's 'ports'. */
258 struct netdev_saved_flags
*sf
;
259 unsigned n_rxq
; /* Number of elements in 'rxq' */
260 struct netdev_rxq
**rxq
;
261 bool dynamic_txqs
; /* If true XPS will be used. */
262 unsigned *txq_used
; /* Number of threads that uses each tx queue. */
263 struct ovs_mutex txq_used_mutex
;
264 char *type
; /* Port type as requested by user. */
267 /* Contained by struct dp_netdev_flow's 'stats' member. */
268 struct dp_netdev_flow_stats
{
269 atomic_llong used
; /* Last used time, in monotonic msecs. */
270 atomic_ullong packet_count
; /* Number of packets matched. */
271 atomic_ullong byte_count
; /* Number of bytes matched. */
272 atomic_uint16_t tcp_flags
; /* Bitwise-OR of seen tcp_flags values. */
275 /* A flow in 'dp_netdev_pmd_thread's 'flow_table'.
281 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
282 * its pmd thread's classifier. The text below calls this classifier 'cls'.
287 * The thread safety rules described here for "struct dp_netdev_flow" are
288 * motivated by two goals:
290 * - Prevent threads that read members of "struct dp_netdev_flow" from
291 * reading bad data due to changes by some thread concurrently modifying
294 * - Prevent two threads making changes to members of a given "struct
295 * dp_netdev_flow" from interfering with each other.
301 * A flow 'flow' may be accessed without a risk of being freed during an RCU
302 * grace period. Code that needs to hold onto a flow for a while
303 * should try incrementing 'flow->ref_cnt' with dp_netdev_flow_ref().
305 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
306 * flow from being deleted from 'cls' and it doesn't protect members of 'flow'
309 * Some members, marked 'const', are immutable. Accessing other members
310 * requires synchronization, as noted in more detail below.
312 struct dp_netdev_flow
{
313 const struct flow flow
; /* Unmasked flow that created this entry. */
314 /* Hash table index by unmasked flow. */
315 const struct cmap_node node
; /* In owning dp_netdev_pmd_thread's */
317 const ovs_u128 ufid
; /* Unique flow identifier. */
318 const unsigned pmd_id
; /* The 'core_id' of pmd thread owning this */
321 /* Number of references.
322 * The classifier owns one reference.
323 * Any thread trying to keep a rule from being freed should hold its own
325 struct ovs_refcount ref_cnt
;
330 struct dp_netdev_flow_stats stats
;
333 OVSRCU_TYPE(struct dp_netdev_actions
*) actions
;
335 /* While processing a group of input packets, the datapath uses the next
336 * member to store a pointer to the output batch for the flow. It is
337 * reset after the batch has been sent out (See dp_netdev_queue_batches(),
338 * packet_batch_per_flow_init() and packet_batch_per_flow_execute()). */
339 struct packet_batch_per_flow
*batch
;
341 /* Packet classification. */
342 struct dpcls_rule cr
; /* In owning dp_netdev's 'cls'. */
343 /* 'cr' must be the last member. */
346 static void dp_netdev_flow_unref(struct dp_netdev_flow
*);
347 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*);
348 static int dpif_netdev_flow_from_nlattrs(const struct nlattr
*, uint32_t,
351 /* A set of datapath actions within a "struct dp_netdev_flow".
357 * A struct dp_netdev_actions 'actions' is protected with RCU. */
358 struct dp_netdev_actions
{
359 /* These members are immutable: they do not change during the struct's
361 unsigned int size
; /* Size of 'actions', in bytes. */
362 struct nlattr actions
[]; /* Sequence of OVS_ACTION_ATTR_* attributes. */
365 struct dp_netdev_actions
*dp_netdev_actions_create(const struct nlattr
*,
367 struct dp_netdev_actions
*dp_netdev_flow_get_actions(
368 const struct dp_netdev_flow
*);
369 static void dp_netdev_actions_free(struct dp_netdev_actions
*);
371 /* Contained by struct dp_netdev_pmd_thread's 'stats' member. */
372 struct dp_netdev_pmd_stats
{
373 /* Indexed by DP_STAT_*. */
374 atomic_ullong n
[DP_N_STATS
];
377 /* Contained by struct dp_netdev_pmd_thread's 'cycle' member. */
378 struct dp_netdev_pmd_cycles
{
379 /* Indexed by PMD_CYCLES_*. */
380 atomic_ullong n
[PMD_N_CYCLES
];
383 /* Contained by struct dp_netdev_pmd_thread's 'poll_list' member. */
385 struct dp_netdev_port
*port
;
386 struct netdev_rxq
*rx
;
387 struct ovs_list node
;
390 /* Contained by struct dp_netdev_pmd_thread's 'port_cache' or 'tx_ports'. */
392 struct dp_netdev_port
*port
;
395 struct hmap_node node
;
398 /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
399 * the performance overhead of interrupt processing. Therefore netdev can
400 * not implement rx-wait for these devices. dpif-netdev needs to poll
401 * these device to check for recv buffer. pmd-thread does polling for
402 * devices assigned to itself.
404 * DPDK used PMD for accessing NIC.
406 * Note, instance with cpu core id NON_PMD_CORE_ID will be reserved for
407 * I/O of all non-pmd threads. There will be no actual thread created
410 * Each struct has its own flow table and classifier. Packets received
411 * from managed ports are looked up in the corresponding pmd thread's
412 * flow table, and are executed with the found actions.
414 struct dp_netdev_pmd_thread
{
415 struct dp_netdev
*dp
;
416 struct ovs_refcount ref_cnt
; /* Every reference must be refcount'ed. */
417 struct cmap_node node
; /* In 'dp->poll_threads'. */
419 pthread_cond_t cond
; /* For synchronizing pmd thread reload. */
420 struct ovs_mutex cond_mutex
; /* Mutex for condition variable. */
422 /* Per thread exact-match cache. Note, the instance for cpu core
423 * NON_PMD_CORE_ID can be accessed by multiple threads, and thusly
424 * need to be protected by 'non_pmd_mutex'. Every other instance
425 * will only be accessed by its own pmd thread. */
426 struct emc_cache flow_cache
;
428 /* Classifier and Flow-Table.
430 * Writers of 'flow_table' must take the 'flow_mutex'. Corresponding
431 * changes to 'cls' must be made while still holding the 'flow_mutex'.
433 struct ovs_mutex flow_mutex
;
435 struct cmap flow_table OVS_GUARDED
; /* Flow table. */
438 struct dp_netdev_pmd_stats stats
;
440 /* Cycles counters */
441 struct dp_netdev_pmd_cycles cycles
;
443 /* Used to count cicles. See 'cycles_counter_end()' */
444 unsigned long long last_cycles
;
446 struct latch exit_latch
; /* For terminating the pmd thread. */
447 atomic_uint change_seq
; /* For reloading pmd ports. */
449 unsigned core_id
; /* CPU core id of this pmd thread. */
450 int numa_id
; /* numa node id of this pmd thread. */
452 /* Queue id used by this pmd thread to send packets on all netdevs if
453 * XPS disabled for this netdev. All static_tx_qid's are unique and less
454 * than 'ovs_numa_get_n_cores() + 1'. */
455 atomic_int static_tx_qid
;
457 struct ovs_mutex port_mutex
; /* Mutex for 'poll_list' and 'tx_ports'. */
458 /* List of rx queues to poll. */
459 struct ovs_list poll_list OVS_GUARDED
;
460 /* Number of elements in 'poll_list' */
462 /* Map of 'tx_port's used for transmission. Written by the main thread,
463 * read by the pmd thread. */
464 struct hmap tx_ports OVS_GUARDED
;
466 /* Map of 'tx_port' used in the fast path. This is a thread-local copy of
467 * 'tx_ports'. The instance for cpu core NON_PMD_CORE_ID can be accessed
468 * by multiple threads, and thusly need to be protected by 'non_pmd_mutex'.
469 * Every other instance will only be accessed by its own pmd thread. */
470 struct hmap port_cache
;
472 /* Only a pmd thread can write on its own 'cycles' and 'stats'.
473 * The main thread keeps 'stats_zero' and 'cycles_zero' as base
474 * values and subtracts them from 'stats' and 'cycles' before
475 * reporting to the user */
476 unsigned long long stats_zero
[DP_N_STATS
];
477 uint64_t cycles_zero
[PMD_N_CYCLES
];
480 #define PMD_INITIAL_SEQ 1
482 /* Interface to netdev-based datapath. */
485 struct dp_netdev
*dp
;
486 uint64_t last_port_seq
;
489 static int get_port_by_number(struct dp_netdev
*dp
, odp_port_t port_no
,
490 struct dp_netdev_port
**portp
)
491 OVS_REQUIRES(dp
->port_mutex
);
492 static int get_port_by_name(struct dp_netdev
*dp
, const char *devname
,
493 struct dp_netdev_port
**portp
)
494 OVS_REQUIRES(dp
->port_mutex
);
495 static void dp_netdev_free(struct dp_netdev
*)
496 OVS_REQUIRES(dp_netdev_mutex
);
497 static int do_add_port(struct dp_netdev
*dp
, const char *devname
,
498 const char *type
, odp_port_t port_no
)
499 OVS_REQUIRES(dp
->port_mutex
);
500 static void do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*)
501 OVS_REQUIRES(dp
->port_mutex
);
502 static int dpif_netdev_open(const struct dpif_class
*, const char *name
,
503 bool create
, struct dpif
**);
504 static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
505 struct dp_packet_batch
*,
507 const struct nlattr
*actions
,
510 static void dp_netdev_input(struct dp_netdev_pmd_thread
*,
511 struct dp_packet_batch
*, odp_port_t port_no
);
512 static void dp_netdev_recirculate(struct dp_netdev_pmd_thread
*,
513 struct dp_packet_batch
*);
515 static void dp_netdev_disable_upcall(struct dp_netdev
*);
516 static void dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
);
517 static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
,
518 struct dp_netdev
*dp
, unsigned core_id
,
520 static void dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
);
521 static void dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
522 OVS_REQUIRES(dp
->port_mutex
);
524 static struct dp_netdev_pmd_thread
*dp_netdev_get_pmd(struct dp_netdev
*dp
,
526 static struct dp_netdev_pmd_thread
*
527 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
);
528 static void dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
);
529 static void dp_netdev_del_pmds_on_numa(struct dp_netdev
*dp
, int numa_id
);
530 static void dp_netdev_set_pmds_on_numa(struct dp_netdev
*dp
, int numa_id
)
531 OVS_REQUIRES(dp
->port_mutex
);
532 static void dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
);
533 static void dp_netdev_del_port_from_all_pmds(struct dp_netdev
*dp
,
534 struct dp_netdev_port
*port
);
535 static void dp_netdev_add_port_to_pmds(struct dp_netdev
*dp
,
536 struct dp_netdev_port
*port
);
537 static void dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
538 struct dp_netdev_port
*port
);
539 static void dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
540 struct dp_netdev_port
*port
,
541 struct netdev_rxq
*rx
);
542 static struct dp_netdev_pmd_thread
*
543 dp_netdev_less_loaded_pmd_on_numa(struct dp_netdev
*dp
, int numa_id
);
544 static void dp_netdev_reset_pmd_threads(struct dp_netdev
*dp
)
545 OVS_REQUIRES(dp
->port_mutex
);
546 static bool dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
);
547 static void dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
);
548 static void dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
);
549 static void pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
550 OVS_REQUIRES(pmd
->port_mutex
);
553 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
554 long long now
, bool purge
);
555 static int dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
556 struct tx_port
*tx
, long long now
);
558 static inline bool emc_entry_alive(struct emc_entry
*ce
);
559 static void emc_clear_entry(struct emc_entry
*ce
);
562 emc_cache_init(struct emc_cache
*flow_cache
)
566 flow_cache
->sweep_idx
= 0;
567 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
568 flow_cache
->entries
[i
].flow
= NULL
;
569 flow_cache
->entries
[i
].key
.hash
= 0;
570 flow_cache
->entries
[i
].key
.len
= sizeof(struct miniflow
);
571 flowmap_init(&flow_cache
->entries
[i
].key
.mf
.map
);
576 emc_cache_uninit(struct emc_cache
*flow_cache
)
580 for (i
= 0; i
< ARRAY_SIZE(flow_cache
->entries
); i
++) {
581 emc_clear_entry(&flow_cache
->entries
[i
]);
585 /* Check and clear dead flow references slowly (one entry at each
588 emc_cache_slow_sweep(struct emc_cache
*flow_cache
)
590 struct emc_entry
*entry
= &flow_cache
->entries
[flow_cache
->sweep_idx
];
592 if (!emc_entry_alive(entry
)) {
593 emc_clear_entry(entry
);
595 flow_cache
->sweep_idx
= (flow_cache
->sweep_idx
+ 1) & EM_FLOW_HASH_MASK
;
598 /* Returns true if 'dpif' is a netdev or dummy dpif, false otherwise. */
600 dpif_is_netdev(const struct dpif
*dpif
)
602 return dpif
->dpif_class
->open
== dpif_netdev_open
;
605 static struct dpif_netdev
*
606 dpif_netdev_cast(const struct dpif
*dpif
)
608 ovs_assert(dpif_is_netdev(dpif
));
609 return CONTAINER_OF(dpif
, struct dpif_netdev
, dpif
);
612 static struct dp_netdev
*
613 get_dp_netdev(const struct dpif
*dpif
)
615 return dpif_netdev_cast(dpif
)->dp
;
619 PMD_INFO_SHOW_STATS
, /* Show how cpu cycles are spent. */
620 PMD_INFO_CLEAR_STATS
, /* Set the cycles count to 0. */
621 PMD_INFO_SHOW_RXQ
/* Show poll-lists of pmd threads. */
625 pmd_info_show_stats(struct ds
*reply
,
626 struct dp_netdev_pmd_thread
*pmd
,
627 unsigned long long stats
[DP_N_STATS
],
628 uint64_t cycles
[PMD_N_CYCLES
])
630 unsigned long long total_packets
= 0;
631 uint64_t total_cycles
= 0;
634 /* These loops subtracts reference values ('*_zero') from the counters.
635 * Since loads and stores are relaxed, it might be possible for a '*_zero'
636 * value to be more recent than the current value we're reading from the
637 * counter. This is not a big problem, since these numbers are not
638 * supposed to be too accurate, but we should at least make sure that
639 * the result is not negative. */
640 for (i
= 0; i
< DP_N_STATS
; i
++) {
641 if (stats
[i
] > pmd
->stats_zero
[i
]) {
642 stats
[i
] -= pmd
->stats_zero
[i
];
647 if (i
!= DP_STAT_LOST
) {
648 /* Lost packets are already included in DP_STAT_MISS */
649 total_packets
+= stats
[i
];
653 for (i
= 0; i
< PMD_N_CYCLES
; i
++) {
654 if (cycles
[i
] > pmd
->cycles_zero
[i
]) {
655 cycles
[i
] -= pmd
->cycles_zero
[i
];
660 total_cycles
+= cycles
[i
];
663 ds_put_cstr(reply
, (pmd
->core_id
== NON_PMD_CORE_ID
)
664 ? "main thread" : "pmd thread");
666 if (pmd
->numa_id
!= OVS_NUMA_UNSPEC
) {
667 ds_put_format(reply
, " numa_id %d", pmd
->numa_id
);
669 if (pmd
->core_id
!= OVS_CORE_UNSPEC
&& pmd
->core_id
!= NON_PMD_CORE_ID
) {
670 ds_put_format(reply
, " core_id %u", pmd
->core_id
);
672 ds_put_cstr(reply
, ":\n");
675 "\temc hits:%llu\n\tmegaflow hits:%llu\n"
676 "\tmiss:%llu\n\tlost:%llu\n",
677 stats
[DP_STAT_EXACT_HIT
], stats
[DP_STAT_MASKED_HIT
],
678 stats
[DP_STAT_MISS
], stats
[DP_STAT_LOST
]);
680 if (total_cycles
== 0) {
685 "\tpolling cycles:%"PRIu64
" (%.02f%%)\n"
686 "\tprocessing cycles:%"PRIu64
" (%.02f%%)\n",
687 cycles
[PMD_CYCLES_POLLING
],
688 cycles
[PMD_CYCLES_POLLING
] / (double)total_cycles
* 100,
689 cycles
[PMD_CYCLES_PROCESSING
],
690 cycles
[PMD_CYCLES_PROCESSING
] / (double)total_cycles
* 100);
692 if (total_packets
== 0) {
697 "\tavg cycles per packet: %.02f (%"PRIu64
"/%llu)\n",
698 total_cycles
/ (double)total_packets
,
699 total_cycles
, total_packets
);
702 "\tavg processing cycles per packet: "
703 "%.02f (%"PRIu64
"/%llu)\n",
704 cycles
[PMD_CYCLES_PROCESSING
] / (double)total_packets
,
705 cycles
[PMD_CYCLES_PROCESSING
], total_packets
);
709 pmd_info_clear_stats(struct ds
*reply OVS_UNUSED
,
710 struct dp_netdev_pmd_thread
*pmd
,
711 unsigned long long stats
[DP_N_STATS
],
712 uint64_t cycles
[PMD_N_CYCLES
])
716 /* We cannot write 'stats' and 'cycles' (because they're written by other
717 * threads) and we shouldn't change 'stats' (because they're used to count
718 * datapath stats, which must not be cleared here). Instead, we save the
719 * current values and subtract them from the values to be displayed in the
721 for (i
= 0; i
< DP_N_STATS
; i
++) {
722 pmd
->stats_zero
[i
] = stats
[i
];
724 for (i
= 0; i
< PMD_N_CYCLES
; i
++) {
725 pmd
->cycles_zero
[i
] = cycles
[i
];
730 pmd_info_show_rxq(struct ds
*reply
, struct dp_netdev_pmd_thread
*pmd
)
732 if (pmd
->core_id
!= NON_PMD_CORE_ID
) {
733 struct rxq_poll
*poll
;
734 const char *prev_name
= NULL
;
736 ds_put_format(reply
, "pmd thread numa_id %d core_id %u:\n",
737 pmd
->numa_id
, pmd
->core_id
);
739 ovs_mutex_lock(&pmd
->port_mutex
);
740 LIST_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
741 const char *name
= netdev_get_name(poll
->port
->netdev
);
743 if (!prev_name
|| strcmp(name
, prev_name
)) {
745 ds_put_cstr(reply
, "\n");
747 ds_put_format(reply
, "\tport: %s\tqueue-id:",
748 netdev_get_name(poll
->port
->netdev
));
750 ds_put_format(reply
, " %d", netdev_rxq_get_queue_id(poll
->rx
));
753 ovs_mutex_unlock(&pmd
->port_mutex
);
754 ds_put_cstr(reply
, "\n");
759 dpif_netdev_pmd_info(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
762 struct ds reply
= DS_EMPTY_INITIALIZER
;
763 struct dp_netdev_pmd_thread
*pmd
;
764 struct dp_netdev
*dp
= NULL
;
765 enum pmd_info_type type
= *(enum pmd_info_type
*) aux
;
767 ovs_mutex_lock(&dp_netdev_mutex
);
770 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
771 } else if (shash_count(&dp_netdevs
) == 1) {
772 /* There's only one datapath */
773 dp
= shash_first(&dp_netdevs
)->data
;
777 ovs_mutex_unlock(&dp_netdev_mutex
);
778 unixctl_command_reply_error(conn
,
779 "please specify an existing datapath");
783 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
784 if (type
== PMD_INFO_SHOW_RXQ
) {
785 pmd_info_show_rxq(&reply
, pmd
);
787 unsigned long long stats
[DP_N_STATS
];
788 uint64_t cycles
[PMD_N_CYCLES
];
791 /* Read current stats and cycle counters */
792 for (i
= 0; i
< ARRAY_SIZE(stats
); i
++) {
793 atomic_read_relaxed(&pmd
->stats
.n
[i
], &stats
[i
]);
795 for (i
= 0; i
< ARRAY_SIZE(cycles
); i
++) {
796 atomic_read_relaxed(&pmd
->cycles
.n
[i
], &cycles
[i
]);
799 if (type
== PMD_INFO_CLEAR_STATS
) {
800 pmd_info_clear_stats(&reply
, pmd
, stats
, cycles
);
801 } else if (type
== PMD_INFO_SHOW_STATS
) {
802 pmd_info_show_stats(&reply
, pmd
, stats
, cycles
);
807 ovs_mutex_unlock(&dp_netdev_mutex
);
809 unixctl_command_reply(conn
, ds_cstr(&reply
));
814 dpif_netdev_init(void)
816 static enum pmd_info_type show_aux
= PMD_INFO_SHOW_STATS
,
817 clear_aux
= PMD_INFO_CLEAR_STATS
,
818 poll_aux
= PMD_INFO_SHOW_RXQ
;
820 unixctl_command_register("dpif-netdev/pmd-stats-show", "[dp]",
821 0, 1, dpif_netdev_pmd_info
,
823 unixctl_command_register("dpif-netdev/pmd-stats-clear", "[dp]",
824 0, 1, dpif_netdev_pmd_info
,
826 unixctl_command_register("dpif-netdev/pmd-rxq-show", "[dp]",
827 0, 1, dpif_netdev_pmd_info
,
833 dpif_netdev_enumerate(struct sset
*all_dps
,
834 const struct dpif_class
*dpif_class
)
836 struct shash_node
*node
;
838 ovs_mutex_lock(&dp_netdev_mutex
);
839 SHASH_FOR_EACH(node
, &dp_netdevs
) {
840 struct dp_netdev
*dp
= node
->data
;
841 if (dpif_class
!= dp
->class) {
842 /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs.
843 * If the class doesn't match, skip this dpif. */
846 sset_add(all_dps
, node
->name
);
848 ovs_mutex_unlock(&dp_netdev_mutex
);
854 dpif_netdev_class_is_dummy(const struct dpif_class
*class)
856 return class != &dpif_netdev_class
;
860 dpif_netdev_port_open_type(const struct dpif_class
*class, const char *type
)
862 return strcmp(type
, "internal") ? type
863 : dpif_netdev_class_is_dummy(class) ? "dummy"
868 create_dpif_netdev(struct dp_netdev
*dp
)
870 uint16_t netflow_id
= hash_string(dp
->name
, 0);
871 struct dpif_netdev
*dpif
;
873 ovs_refcount_ref(&dp
->ref_cnt
);
875 dpif
= xmalloc(sizeof *dpif
);
876 dpif_init(&dpif
->dpif
, dp
->class, dp
->name
, netflow_id
>> 8, netflow_id
);
878 dpif
->last_port_seq
= seq_read(dp
->port_seq
);
883 /* Choose an unused, non-zero port number and return it on success.
884 * Return ODPP_NONE on failure. */
886 choose_port(struct dp_netdev
*dp
, const char *name
)
887 OVS_REQUIRES(dp
->port_mutex
)
891 if (dp
->class != &dpif_netdev_class
) {
895 /* If the port name begins with "br", start the number search at
896 * 100 to make writing tests easier. */
897 if (!strncmp(name
, "br", 2)) {
901 /* If the port name contains a number, try to assign that port number.
902 * This can make writing unit tests easier because port numbers are
904 for (p
= name
; *p
!= '\0'; p
++) {
905 if (isdigit((unsigned char) *p
)) {
906 port_no
= start_no
+ strtol(p
, NULL
, 10);
907 if (port_no
> 0 && port_no
!= odp_to_u32(ODPP_NONE
)
908 && !dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
909 return u32_to_odp(port_no
);
916 for (port_no
= 1; port_no
<= UINT16_MAX
; port_no
++) {
917 if (!dp_netdev_lookup_port(dp
, u32_to_odp(port_no
))) {
918 return u32_to_odp(port_no
);
926 create_dp_netdev(const char *name
, const struct dpif_class
*class,
927 struct dp_netdev
**dpp
)
928 OVS_REQUIRES(dp_netdev_mutex
)
930 struct dp_netdev
*dp
;
933 dp
= xzalloc(sizeof *dp
);
934 shash_add(&dp_netdevs
, name
, dp
);
936 *CONST_CAST(const struct dpif_class
**, &dp
->class) = class;
937 *CONST_CAST(const char **, &dp
->name
) = xstrdup(name
);
938 ovs_refcount_init(&dp
->ref_cnt
);
939 atomic_flag_clear(&dp
->destroyed
);
941 ovs_mutex_init(&dp
->port_mutex
);
942 hmap_init(&dp
->ports
);
943 dp
->port_seq
= seq_create();
944 fat_rwlock_init(&dp
->upcall_rwlock
);
946 /* Disable upcalls by default. */
947 dp_netdev_disable_upcall(dp
);
948 dp
->upcall_aux
= NULL
;
949 dp
->upcall_cb
= NULL
;
951 cmap_init(&dp
->poll_threads
);
952 ovs_mutex_init_recursive(&dp
->non_pmd_mutex
);
953 ovsthread_key_create(&dp
->per_pmd_key
, NULL
);
955 ovs_mutex_lock(&dp
->port_mutex
);
956 dp_netdev_set_nonpmd(dp
);
958 error
= do_add_port(dp
, name
, "internal", ODPP_LOCAL
);
959 ovs_mutex_unlock(&dp
->port_mutex
);
965 dp
->last_tnl_conf_seq
= seq_read(tnl_conf_seq
);
971 dpif_netdev_open(const struct dpif_class
*class, const char *name
,
972 bool create
, struct dpif
**dpifp
)
974 struct dp_netdev
*dp
;
977 ovs_mutex_lock(&dp_netdev_mutex
);
978 dp
= shash_find_data(&dp_netdevs
, name
);
980 error
= create
? create_dp_netdev(name
, class, &dp
) : ENODEV
;
982 error
= (dp
->class != class ? EINVAL
987 *dpifp
= create_dpif_netdev(dp
);
990 ovs_mutex_unlock(&dp_netdev_mutex
);
996 dp_netdev_destroy_upcall_lock(struct dp_netdev
*dp
)
997 OVS_NO_THREAD_SAFETY_ANALYSIS
999 /* Check that upcalls are disabled, i.e. that the rwlock is taken */
1000 ovs_assert(fat_rwlock_tryrdlock(&dp
->upcall_rwlock
));
1002 /* Before freeing a lock we should release it */
1003 fat_rwlock_unlock(&dp
->upcall_rwlock
);
1004 fat_rwlock_destroy(&dp
->upcall_rwlock
);
1007 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
1008 * through the 'dp_netdevs' shash while freeing 'dp'. */
1010 dp_netdev_free(struct dp_netdev
*dp
)
1011 OVS_REQUIRES(dp_netdev_mutex
)
1013 struct dp_netdev_port
*port
, *next
;
1015 shash_find_and_delete(&dp_netdevs
, dp
->name
);
1017 dp_netdev_destroy_all_pmds(dp
);
1018 ovs_mutex_destroy(&dp
->non_pmd_mutex
);
1019 ovsthread_key_delete(dp
->per_pmd_key
);
1021 ovs_mutex_lock(&dp
->port_mutex
);
1022 HMAP_FOR_EACH_SAFE (port
, next
, node
, &dp
->ports
) {
1023 do_del_port(dp
, port
);
1025 ovs_mutex_unlock(&dp
->port_mutex
);
1026 cmap_destroy(&dp
->poll_threads
);
1028 seq_destroy(dp
->port_seq
);
1029 hmap_destroy(&dp
->ports
);
1030 ovs_mutex_destroy(&dp
->port_mutex
);
1032 /* Upcalls must be disabled at this point */
1033 dp_netdev_destroy_upcall_lock(dp
);
1035 free(dp
->pmd_cmask
);
1036 free(CONST_CAST(char *, dp
->name
));
1041 dp_netdev_unref(struct dp_netdev
*dp
)
1044 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
1045 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
1046 ovs_mutex_lock(&dp_netdev_mutex
);
1047 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1050 ovs_mutex_unlock(&dp_netdev_mutex
);
1055 dpif_netdev_close(struct dpif
*dpif
)
1057 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1059 dp_netdev_unref(dp
);
1064 dpif_netdev_destroy(struct dpif
*dpif
)
1066 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1068 if (!atomic_flag_test_and_set(&dp
->destroyed
)) {
1069 if (ovs_refcount_unref_relaxed(&dp
->ref_cnt
) == 1) {
1070 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
1078 /* Add 'n' to the atomic variable 'var' non-atomically and using relaxed
1079 * load/store semantics. While the increment is not atomic, the load and
1080 * store operations are, making it impossible to read inconsistent values.
1082 * This is used to update thread local stats counters. */
1084 non_atomic_ullong_add(atomic_ullong
*var
, unsigned long long n
)
1086 unsigned long long tmp
;
1088 atomic_read_relaxed(var
, &tmp
);
1090 atomic_store_relaxed(var
, tmp
);
1094 dpif_netdev_get_stats(const struct dpif
*dpif
, struct dpif_dp_stats
*stats
)
1096 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1097 struct dp_netdev_pmd_thread
*pmd
;
1099 stats
->n_flows
= stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
1100 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1101 unsigned long long n
;
1102 stats
->n_flows
+= cmap_count(&pmd
->flow_table
);
1104 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_MASKED_HIT
], &n
);
1106 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_EXACT_HIT
], &n
);
1108 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_MISS
], &n
);
1109 stats
->n_missed
+= n
;
1110 atomic_read_relaxed(&pmd
->stats
.n
[DP_STAT_LOST
], &n
);
1113 stats
->n_masks
= UINT32_MAX
;
1114 stats
->n_mask_hit
= UINT64_MAX
;
1120 dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread
*pmd
)
1124 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
1125 ovs_mutex_lock(&pmd
->dp
->non_pmd_mutex
);
1126 ovs_mutex_lock(&pmd
->port_mutex
);
1127 pmd_load_cached_ports(pmd
);
1128 ovs_mutex_unlock(&pmd
->port_mutex
);
1129 ovs_mutex_unlock(&pmd
->dp
->non_pmd_mutex
);
1133 ovs_mutex_lock(&pmd
->cond_mutex
);
1134 atomic_add_relaxed(&pmd
->change_seq
, 1, &old_seq
);
1135 ovs_mutex_cond_wait(&pmd
->cond
, &pmd
->cond_mutex
);
1136 ovs_mutex_unlock(&pmd
->cond_mutex
);
1140 hash_port_no(odp_port_t port_no
)
1142 return hash_int(odp_to_u32(port_no
), 0);
1146 port_create(const char *devname
, const char *open_type
, const char *type
,
1147 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1149 struct netdev_saved_flags
*sf
;
1150 struct dp_netdev_port
*port
;
1151 enum netdev_flags flags
;
1152 struct netdev
*netdev
;
1153 int n_open_rxqs
= 0;
1156 bool dynamic_txqs
= false;
1160 /* Open and validate network device. */
1161 error
= netdev_open(devname
, open_type
, &netdev
);
1165 /* XXX reject non-Ethernet devices */
1167 netdev_get_flags(netdev
, &flags
);
1168 if (flags
& NETDEV_LOOPBACK
) {
1169 VLOG_ERR("%s: cannot add a loopback device", devname
);
1174 if (netdev_is_pmd(netdev
)) {
1175 n_cores
= ovs_numa_get_n_cores();
1177 if (n_cores
== OVS_CORE_UNSPEC
) {
1178 VLOG_ERR("%s, cannot get cpu core info", devname
);
1182 /* There can only be ovs_numa_get_n_cores() pmd threads,
1183 * so creates a txq for each, and one extra for the non
1185 error
= netdev_set_tx_multiq(netdev
, n_cores
+ 1);
1186 if (error
&& (error
!= EOPNOTSUPP
)) {
1187 VLOG_ERR("%s, cannot set multiq", devname
);
1192 if (netdev_is_reconf_required(netdev
)) {
1193 error
= netdev_reconfigure(netdev
);
1199 if (netdev_is_pmd(netdev
)) {
1200 if (netdev_n_txq(netdev
) < n_cores
+ 1) {
1201 dynamic_txqs
= true;
1205 port
= xzalloc(sizeof *port
);
1206 port
->port_no
= port_no
;
1207 port
->netdev
= netdev
;
1208 port
->n_rxq
= netdev_n_rxq(netdev
);
1209 port
->rxq
= xcalloc(port
->n_rxq
, sizeof *port
->rxq
);
1210 port
->txq_used
= xcalloc(netdev_n_txq(netdev
), sizeof *port
->txq_used
);
1211 port
->type
= xstrdup(type
);
1212 ovs_mutex_init(&port
->txq_used_mutex
);
1213 port
->dynamic_txqs
= dynamic_txqs
;
1215 for (i
= 0; i
< port
->n_rxq
; i
++) {
1216 error
= netdev_rxq_open(netdev
, &port
->rxq
[i
], i
);
1218 VLOG_ERR("%s: cannot receive packets on this network device (%s)",
1219 devname
, ovs_strerror(errno
));
1225 error
= netdev_turn_flags_on(netdev
, NETDEV_PROMISC
, &sf
);
1236 for (i
= 0; i
< n_open_rxqs
; i
++) {
1237 netdev_rxq_close(port
->rxq
[i
]);
1239 ovs_mutex_destroy(&port
->txq_used_mutex
);
1241 free(port
->txq_used
);
1246 netdev_close(netdev
);
1251 do_add_port(struct dp_netdev
*dp
, const char *devname
, const char *type
,
1253 OVS_REQUIRES(dp
->port_mutex
)
1255 struct dp_netdev_port
*port
;
1258 /* Reject devices already in 'dp'. */
1259 if (!get_port_by_name(dp
, devname
, &port
)) {
1263 error
= port_create(devname
, dpif_netdev_port_open_type(dp
->class, type
),
1264 type
, port_no
, &port
);
1269 if (netdev_is_pmd(port
->netdev
)) {
1270 int numa_id
= netdev_get_numa_id(port
->netdev
);
1272 ovs_assert(ovs_numa_numa_id_is_valid(numa_id
));
1273 dp_netdev_set_pmds_on_numa(dp
, numa_id
);
1276 dp_netdev_add_port_to_pmds(dp
, port
);
1278 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
1279 seq_change(dp
->port_seq
);
1285 dpif_netdev_port_add(struct dpif
*dpif
, struct netdev
*netdev
,
1286 odp_port_t
*port_nop
)
1288 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1289 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1290 const char *dpif_port
;
1294 ovs_mutex_lock(&dp
->port_mutex
);
1295 dpif_port
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1296 if (*port_nop
!= ODPP_NONE
) {
1297 port_no
= *port_nop
;
1298 error
= dp_netdev_lookup_port(dp
, *port_nop
) ? EBUSY
: 0;
1300 port_no
= choose_port(dp
, dpif_port
);
1301 error
= port_no
== ODPP_NONE
? EFBIG
: 0;
1304 *port_nop
= port_no
;
1305 error
= do_add_port(dp
, dpif_port
, netdev_get_type(netdev
), port_no
);
1307 ovs_mutex_unlock(&dp
->port_mutex
);
1313 dpif_netdev_port_del(struct dpif
*dpif
, odp_port_t port_no
)
1315 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1318 ovs_mutex_lock(&dp
->port_mutex
);
1319 if (port_no
== ODPP_LOCAL
) {
1322 struct dp_netdev_port
*port
;
1324 error
= get_port_by_number(dp
, port_no
, &port
);
1326 do_del_port(dp
, port
);
1329 ovs_mutex_unlock(&dp
->port_mutex
);
1335 is_valid_port_number(odp_port_t port_no
)
1337 return port_no
!= ODPP_NONE
;
1340 static struct dp_netdev_port
*
1341 dp_netdev_lookup_port(const struct dp_netdev
*dp
, odp_port_t port_no
)
1342 OVS_REQUIRES(dp
->port_mutex
)
1344 struct dp_netdev_port
*port
;
1346 HMAP_FOR_EACH_WITH_HASH (port
, node
, hash_port_no(port_no
), &dp
->ports
) {
1347 if (port
->port_no
== port_no
) {
1355 get_port_by_number(struct dp_netdev
*dp
,
1356 odp_port_t port_no
, struct dp_netdev_port
**portp
)
1357 OVS_REQUIRES(dp
->port_mutex
)
1359 if (!is_valid_port_number(port_no
)) {
1363 *portp
= dp_netdev_lookup_port(dp
, port_no
);
1364 return *portp
? 0 : ENOENT
;
1369 port_destroy(struct dp_netdev_port
*port
)
1375 netdev_close(port
->netdev
);
1376 netdev_restore_flags(port
->sf
);
1378 for (unsigned i
= 0; i
< port
->n_rxq
; i
++) {
1379 netdev_rxq_close(port
->rxq
[i
]);
1381 ovs_mutex_destroy(&port
->txq_used_mutex
);
1382 free(port
->txq_used
);
1389 get_port_by_name(struct dp_netdev
*dp
,
1390 const char *devname
, struct dp_netdev_port
**portp
)
1391 OVS_REQUIRES(dp
->port_mutex
)
1393 struct dp_netdev_port
*port
;
1395 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1396 if (!strcmp(netdev_get_name(port
->netdev
), devname
)) {
1405 get_n_pmd_threads(struct dp_netdev
*dp
)
1407 /* There is one non pmd thread in dp->poll_threads */
1408 return cmap_count(&dp
->poll_threads
) - 1;
1412 get_n_pmd_threads_on_numa(struct dp_netdev
*dp
, int numa_id
)
1414 struct dp_netdev_pmd_thread
*pmd
;
1417 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1418 if (pmd
->numa_id
== numa_id
) {
1426 /* Returns 'true' if there is a port with pmd netdev and the netdev
1427 * is on numa node 'numa_id'. */
1429 has_pmd_port_for_numa(struct dp_netdev
*dp
, int numa_id
)
1430 OVS_REQUIRES(dp
->port_mutex
)
1432 struct dp_netdev_port
*port
;
1434 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
1435 if (netdev_is_pmd(port
->netdev
)
1436 && netdev_get_numa_id(port
->netdev
) == numa_id
) {
1446 do_del_port(struct dp_netdev
*dp
, struct dp_netdev_port
*port
)
1447 OVS_REQUIRES(dp
->port_mutex
)
1449 hmap_remove(&dp
->ports
, &port
->node
);
1450 seq_change(dp
->port_seq
);
1452 dp_netdev_del_port_from_all_pmds(dp
, port
);
1454 if (netdev_is_pmd(port
->netdev
)) {
1455 int numa_id
= netdev_get_numa_id(port
->netdev
);
1457 /* PMD threads can not be on invalid numa node. */
1458 ovs_assert(ovs_numa_numa_id_is_valid(numa_id
));
1459 /* If there is no netdev on the numa node, deletes the pmd threads
1461 if (!has_pmd_port_for_numa(dp
, numa_id
)) {
1462 dp_netdev_del_pmds_on_numa(dp
, numa_id
);
1470 answer_port_query(const struct dp_netdev_port
*port
,
1471 struct dpif_port
*dpif_port
)
1473 dpif_port
->name
= xstrdup(netdev_get_name(port
->netdev
));
1474 dpif_port
->type
= xstrdup(port
->type
);
1475 dpif_port
->port_no
= port
->port_no
;
1479 dpif_netdev_port_query_by_number(const struct dpif
*dpif
, odp_port_t port_no
,
1480 struct dpif_port
*dpif_port
)
1482 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1483 struct dp_netdev_port
*port
;
1486 ovs_mutex_lock(&dp
->port_mutex
);
1487 error
= get_port_by_number(dp
, port_no
, &port
);
1488 if (!error
&& dpif_port
) {
1489 answer_port_query(port
, dpif_port
);
1491 ovs_mutex_unlock(&dp
->port_mutex
);
1497 dpif_netdev_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
1498 struct dpif_port
*dpif_port
)
1500 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1501 struct dp_netdev_port
*port
;
1504 ovs_mutex_lock(&dp
->port_mutex
);
1505 error
= get_port_by_name(dp
, devname
, &port
);
1506 if (!error
&& dpif_port
) {
1507 answer_port_query(port
, dpif_port
);
1509 ovs_mutex_unlock(&dp
->port_mutex
);
1515 dp_netdev_flow_free(struct dp_netdev_flow
*flow
)
1517 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow
));
1521 static void dp_netdev_flow_unref(struct dp_netdev_flow
*flow
)
1523 if (ovs_refcount_unref_relaxed(&flow
->ref_cnt
) == 1) {
1524 ovsrcu_postpone(dp_netdev_flow_free
, flow
);
1529 dp_netdev_flow_hash(const ovs_u128
*ufid
)
1531 return ufid
->u32
[0];
1535 dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread
*pmd
,
1536 struct dp_netdev_flow
*flow
)
1537 OVS_REQUIRES(pmd
->flow_mutex
)
1539 struct cmap_node
*node
= CONST_CAST(struct cmap_node
*, &flow
->node
);
1541 dpcls_remove(&pmd
->cls
, &flow
->cr
);
1542 cmap_remove(&pmd
->flow_table
, node
, dp_netdev_flow_hash(&flow
->ufid
));
1545 dp_netdev_flow_unref(flow
);
1549 dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread
*pmd
)
1551 struct dp_netdev_flow
*netdev_flow
;
1553 ovs_mutex_lock(&pmd
->flow_mutex
);
1554 CMAP_FOR_EACH (netdev_flow
, node
, &pmd
->flow_table
) {
1555 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
1557 ovs_mutex_unlock(&pmd
->flow_mutex
);
1561 dpif_netdev_flow_flush(struct dpif
*dpif
)
1563 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1564 struct dp_netdev_pmd_thread
*pmd
;
1566 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
1567 dp_netdev_pmd_flow_flush(pmd
);
1573 struct dp_netdev_port_state
{
1574 struct hmap_position position
;
1579 dpif_netdev_port_dump_start(const struct dpif
*dpif OVS_UNUSED
, void **statep
)
1581 *statep
= xzalloc(sizeof(struct dp_netdev_port_state
));
1586 dpif_netdev_port_dump_next(const struct dpif
*dpif
, void *state_
,
1587 struct dpif_port
*dpif_port
)
1589 struct dp_netdev_port_state
*state
= state_
;
1590 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
1591 struct hmap_node
*node
;
1594 ovs_mutex_lock(&dp
->port_mutex
);
1595 node
= hmap_at_position(&dp
->ports
, &state
->position
);
1597 struct dp_netdev_port
*port
;
1599 port
= CONTAINER_OF(node
, struct dp_netdev_port
, node
);
1602 state
->name
= xstrdup(netdev_get_name(port
->netdev
));
1603 dpif_port
->name
= state
->name
;
1604 dpif_port
->type
= port
->type
;
1605 dpif_port
->port_no
= port
->port_no
;
1611 ovs_mutex_unlock(&dp
->port_mutex
);
1617 dpif_netdev_port_dump_done(const struct dpif
*dpif OVS_UNUSED
, void *state_
)
1619 struct dp_netdev_port_state
*state
= state_
;
1626 dpif_netdev_port_poll(const struct dpif
*dpif_
, char **devnamep OVS_UNUSED
)
1628 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
1629 uint64_t new_port_seq
;
1632 new_port_seq
= seq_read(dpif
->dp
->port_seq
);
1633 if (dpif
->last_port_seq
!= new_port_seq
) {
1634 dpif
->last_port_seq
= new_port_seq
;
1644 dpif_netdev_port_poll_wait(const struct dpif
*dpif_
)
1646 struct dpif_netdev
*dpif
= dpif_netdev_cast(dpif_
);
1648 seq_wait(dpif
->dp
->port_seq
, dpif
->last_port_seq
);
1651 static struct dp_netdev_flow
*
1652 dp_netdev_flow_cast(const struct dpcls_rule
*cr
)
1654 return cr
? CONTAINER_OF(cr
, struct dp_netdev_flow
, cr
) : NULL
;
1657 static bool dp_netdev_flow_ref(struct dp_netdev_flow
*flow
)
1659 return ovs_refcount_try_ref_rcu(&flow
->ref_cnt
);
1662 /* netdev_flow_key utilities.
1664 * netdev_flow_key is basically a miniflow. We use these functions
1665 * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow
1666 * functions (miniflow_clone_inline, miniflow_equal, ...), because:
1668 * - Since we are dealing exclusively with miniflows created by
1669 * miniflow_extract(), if the map is different the miniflow is different.
1670 * Therefore we can be faster by comparing the map and the miniflow in a
1672 * - These functions can be inlined by the compiler. */
1674 /* Given the number of bits set in miniflow's maps, returns the size of the
1675 * 'netdev_flow_key.mf' */
1676 static inline size_t
1677 netdev_flow_key_size(size_t flow_u64s
)
1679 return sizeof(struct miniflow
) + MINIFLOW_VALUES_SIZE(flow_u64s
);
1683 netdev_flow_key_equal(const struct netdev_flow_key
*a
,
1684 const struct netdev_flow_key
*b
)
1686 /* 'b->len' may be not set yet. */
1687 return a
->hash
== b
->hash
&& !memcmp(&a
->mf
, &b
->mf
, a
->len
);
1690 /* Used to compare 'netdev_flow_key' in the exact match cache to a miniflow.
1691 * The maps are compared bitwise, so both 'key->mf' and 'mf' must have been
1692 * generated by miniflow_extract. */
1694 netdev_flow_key_equal_mf(const struct netdev_flow_key
*key
,
1695 const struct miniflow
*mf
)
1697 return !memcmp(&key
->mf
, mf
, key
->len
);
1701 netdev_flow_key_clone(struct netdev_flow_key
*dst
,
1702 const struct netdev_flow_key
*src
)
1705 offsetof(struct netdev_flow_key
, mf
) + src
->len
);
1710 netdev_flow_key_from_flow(struct netdev_flow_key
*dst
,
1711 const struct flow
*src
)
1713 struct dp_packet packet
;
1714 uint64_t buf_stub
[512 / 8];
1716 dp_packet_use_stub(&packet
, buf_stub
, sizeof buf_stub
);
1717 pkt_metadata_from_flow(&packet
.md
, src
);
1718 flow_compose(&packet
, src
);
1719 miniflow_extract(&packet
, &dst
->mf
);
1720 dp_packet_uninit(&packet
);
1722 dst
->len
= netdev_flow_key_size(miniflow_n_values(&dst
->mf
));
1723 dst
->hash
= 0; /* Not computed yet. */
1726 /* Initialize a netdev_flow_key 'mask' from 'match'. */
1728 netdev_flow_mask_init(struct netdev_flow_key
*mask
,
1729 const struct match
*match
)
1731 uint64_t *dst
= miniflow_values(&mask
->mf
);
1732 struct flowmap fmap
;
1736 /* Only check masks that make sense for the flow. */
1737 flow_wc_map(&match
->flow
, &fmap
);
1738 flowmap_init(&mask
->mf
.map
);
1740 FLOWMAP_FOR_EACH_INDEX(idx
, fmap
) {
1741 uint64_t mask_u64
= flow_u64_value(&match
->wc
.masks
, idx
);
1744 flowmap_set(&mask
->mf
.map
, idx
, 1);
1746 hash
= hash_add64(hash
, mask_u64
);
1752 FLOWMAP_FOR_EACH_MAP (map
, mask
->mf
.map
) {
1753 hash
= hash_add64(hash
, map
);
1756 size_t n
= dst
- miniflow_get_values(&mask
->mf
);
1758 mask
->hash
= hash_finish(hash
, n
* 8);
1759 mask
->len
= netdev_flow_key_size(n
);
1762 /* Initializes 'dst' as a copy of 'flow' masked with 'mask'. */
1764 netdev_flow_key_init_masked(struct netdev_flow_key
*dst
,
1765 const struct flow
*flow
,
1766 const struct netdev_flow_key
*mask
)
1768 uint64_t *dst_u64
= miniflow_values(&dst
->mf
);
1769 const uint64_t *mask_u64
= miniflow_get_values(&mask
->mf
);
1773 dst
->len
= mask
->len
;
1774 dst
->mf
= mask
->mf
; /* Copy maps. */
1776 FLOW_FOR_EACH_IN_MAPS(value
, flow
, mask
->mf
.map
) {
1777 *dst_u64
= value
& *mask_u64
++;
1778 hash
= hash_add64(hash
, *dst_u64
++);
1780 dst
->hash
= hash_finish(hash
,
1781 (dst_u64
- miniflow_get_values(&dst
->mf
)) * 8);
1784 /* Iterate through netdev_flow_key TNL u64 values specified by 'FLOWMAP'. */
1785 #define NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(VALUE, KEY, FLOWMAP) \
1786 MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, &(KEY)->mf, FLOWMAP)
1788 /* Returns a hash value for the bits of 'key' where there are 1-bits in
1790 static inline uint32_t
1791 netdev_flow_key_hash_in_mask(const struct netdev_flow_key
*key
,
1792 const struct netdev_flow_key
*mask
)
1794 const uint64_t *p
= miniflow_get_values(&mask
->mf
);
1798 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, key
, mask
->mf
.map
) {
1799 hash
= hash_add64(hash
, value
& *p
++);
1802 return hash_finish(hash
, (p
- miniflow_get_values(&mask
->mf
)) * 8);
1806 emc_entry_alive(struct emc_entry
*ce
)
1808 return ce
->flow
&& !ce
->flow
->dead
;
1812 emc_clear_entry(struct emc_entry
*ce
)
1815 dp_netdev_flow_unref(ce
->flow
);
1821 emc_change_entry(struct emc_entry
*ce
, struct dp_netdev_flow
*flow
,
1822 const struct netdev_flow_key
*key
)
1824 if (ce
->flow
!= flow
) {
1826 dp_netdev_flow_unref(ce
->flow
);
1829 if (dp_netdev_flow_ref(flow
)) {
1836 netdev_flow_key_clone(&ce
->key
, key
);
1841 emc_insert(struct emc_cache
*cache
, const struct netdev_flow_key
*key
,
1842 struct dp_netdev_flow
*flow
)
1844 struct emc_entry
*to_be_replaced
= NULL
;
1845 struct emc_entry
*current_entry
;
1847 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
1848 if (netdev_flow_key_equal(¤t_entry
->key
, key
)) {
1849 /* We found the entry with the 'mf' miniflow */
1850 emc_change_entry(current_entry
, flow
, NULL
);
1854 /* Replacement policy: put the flow in an empty (not alive) entry, or
1855 * in the first entry where it can be */
1857 || (emc_entry_alive(to_be_replaced
)
1858 && !emc_entry_alive(current_entry
))
1859 || current_entry
->key
.hash
< to_be_replaced
->key
.hash
) {
1860 to_be_replaced
= current_entry
;
1863 /* We didn't find the miniflow in the cache.
1864 * The 'to_be_replaced' entry is where the new flow will be stored */
1866 emc_change_entry(to_be_replaced
, flow
, key
);
1869 static inline struct dp_netdev_flow
*
1870 emc_lookup(struct emc_cache
*cache
, const struct netdev_flow_key
*key
)
1872 struct emc_entry
*current_entry
;
1874 EMC_FOR_EACH_POS_WITH_HASH(cache
, current_entry
, key
->hash
) {
1875 if (current_entry
->key
.hash
== key
->hash
1876 && emc_entry_alive(current_entry
)
1877 && netdev_flow_key_equal_mf(¤t_entry
->key
, &key
->mf
)) {
1879 /* We found the entry with the 'key->mf' miniflow */
1880 return current_entry
->flow
;
1887 static struct dp_netdev_flow
*
1888 dp_netdev_pmd_lookup_flow(const struct dp_netdev_pmd_thread
*pmd
,
1889 const struct netdev_flow_key
*key
)
1891 struct dp_netdev_flow
*netdev_flow
;
1892 struct dpcls_rule
*rule
;
1894 dpcls_lookup(&pmd
->cls
, key
, &rule
, 1);
1895 netdev_flow
= dp_netdev_flow_cast(rule
);
1900 static struct dp_netdev_flow
*
1901 dp_netdev_pmd_find_flow(const struct dp_netdev_pmd_thread
*pmd
,
1902 const ovs_u128
*ufidp
, const struct nlattr
*key
,
1905 struct dp_netdev_flow
*netdev_flow
;
1909 /* If a UFID is not provided, determine one based on the key. */
1910 if (!ufidp
&& key
&& key_len
1911 && !dpif_netdev_flow_from_nlattrs(key
, key_len
, &flow
)) {
1912 dpif_flow_hash(pmd
->dp
->dpif
, &flow
, sizeof flow
, &ufid
);
1917 CMAP_FOR_EACH_WITH_HASH (netdev_flow
, node
, dp_netdev_flow_hash(ufidp
),
1919 if (ovs_u128_equals(netdev_flow
->ufid
, *ufidp
)) {
1929 get_dpif_flow_stats(const struct dp_netdev_flow
*netdev_flow_
,
1930 struct dpif_flow_stats
*stats
)
1932 struct dp_netdev_flow
*netdev_flow
;
1933 unsigned long long n
;
1937 netdev_flow
= CONST_CAST(struct dp_netdev_flow
*, netdev_flow_
);
1939 atomic_read_relaxed(&netdev_flow
->stats
.packet_count
, &n
);
1940 stats
->n_packets
= n
;
1941 atomic_read_relaxed(&netdev_flow
->stats
.byte_count
, &n
);
1943 atomic_read_relaxed(&netdev_flow
->stats
.used
, &used
);
1945 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
1946 stats
->tcp_flags
= flags
;
1949 /* Converts to the dpif_flow format, using 'key_buf' and 'mask_buf' for
1950 * storing the netlink-formatted key/mask. 'key_buf' may be the same as
1951 * 'mask_buf'. Actions will be returned without copying, by relying on RCU to
1954 dp_netdev_flow_to_dpif_flow(const struct dp_netdev_flow
*netdev_flow
,
1955 struct ofpbuf
*key_buf
, struct ofpbuf
*mask_buf
,
1956 struct dpif_flow
*flow
, bool terse
)
1959 memset(flow
, 0, sizeof *flow
);
1961 struct flow_wildcards wc
;
1962 struct dp_netdev_actions
*actions
;
1964 struct odp_flow_key_parms odp_parms
= {
1965 .flow
= &netdev_flow
->flow
,
1967 .support
= dp_netdev_support
,
1970 miniflow_expand(&netdev_flow
->cr
.mask
->mf
, &wc
.masks
);
1973 offset
= key_buf
->size
;
1974 flow
->key
= ofpbuf_tail(key_buf
);
1975 odp_flow_key_from_flow(&odp_parms
, key_buf
);
1976 flow
->key_len
= key_buf
->size
- offset
;
1979 offset
= mask_buf
->size
;
1980 flow
->mask
= ofpbuf_tail(mask_buf
);
1981 odp_parms
.key_buf
= key_buf
;
1982 odp_flow_key_from_mask(&odp_parms
, mask_buf
);
1983 flow
->mask_len
= mask_buf
->size
- offset
;
1986 actions
= dp_netdev_flow_get_actions(netdev_flow
);
1987 flow
->actions
= actions
->actions
;
1988 flow
->actions_len
= actions
->size
;
1991 flow
->ufid
= netdev_flow
->ufid
;
1992 flow
->ufid_present
= true;
1993 flow
->pmd_id
= netdev_flow
->pmd_id
;
1994 get_dpif_flow_stats(netdev_flow
, &flow
->stats
);
1998 dpif_netdev_mask_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
1999 const struct nlattr
*mask_key
,
2000 uint32_t mask_key_len
, const struct flow
*flow
,
2001 struct flow_wildcards
*wc
)
2003 enum odp_key_fitness fitness
;
2005 fitness
= odp_flow_key_to_mask_udpif(mask_key
, mask_key_len
, key
,
2008 /* This should not happen: it indicates that
2009 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
2010 * disagree on the acceptable form of a mask. Log the problem
2011 * as an error, with enough details to enable debugging. */
2012 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2014 if (!VLOG_DROP_ERR(&rl
)) {
2018 odp_flow_format(key
, key_len
, mask_key
, mask_key_len
, NULL
, &s
,
2020 VLOG_ERR("internal error parsing flow mask %s (%s)",
2021 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
2032 dpif_netdev_flow_from_nlattrs(const struct nlattr
*key
, uint32_t key_len
,
2037 if (odp_flow_key_to_flow_udpif(key
, key_len
, flow
)) {
2038 /* This should not happen: it indicates that odp_flow_key_from_flow()
2039 * and odp_flow_key_to_flow() disagree on the acceptable form of a
2040 * flow. Log the problem as an error, with enough details to enable
2042 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2044 if (!VLOG_DROP_ERR(&rl
)) {
2048 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
2049 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
2056 in_port
= flow
->in_port
.odp_port
;
2057 if (!is_valid_port_number(in_port
) && in_port
!= ODPP_NONE
) {
2061 /* Userspace datapath doesn't support conntrack. */
2062 if (flow
->ct_state
|| flow
->ct_zone
|| flow
->ct_mark
2063 || !ovs_u128_is_zero(flow
->ct_label
)) {
2071 dpif_netdev_flow_get(const struct dpif
*dpif
, const struct dpif_flow_get
*get
)
2073 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2074 struct dp_netdev_flow
*netdev_flow
;
2075 struct dp_netdev_pmd_thread
*pmd
;
2076 struct hmapx to_find
= HMAPX_INITIALIZER(&to_find
);
2077 struct hmapx_node
*node
;
2080 if (get
->pmd_id
== PMD_ID_NULL
) {
2081 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
2082 if (dp_netdev_pmd_try_ref(pmd
) && !hmapx_add(&to_find
, pmd
)) {
2083 dp_netdev_pmd_unref(pmd
);
2087 pmd
= dp_netdev_get_pmd(dp
, get
->pmd_id
);
2091 hmapx_add(&to_find
, pmd
);
2094 if (!hmapx_count(&to_find
)) {
2098 HMAPX_FOR_EACH (node
, &to_find
) {
2099 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
2100 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, get
->ufid
, get
->key
,
2103 dp_netdev_flow_to_dpif_flow(netdev_flow
, get
->buffer
, get
->buffer
,
2112 HMAPX_FOR_EACH (node
, &to_find
) {
2113 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
2114 dp_netdev_pmd_unref(pmd
);
2117 hmapx_destroy(&to_find
);
2121 static struct dp_netdev_flow
*
2122 dp_netdev_flow_add(struct dp_netdev_pmd_thread
*pmd
,
2123 struct match
*match
, const ovs_u128
*ufid
,
2124 const struct nlattr
*actions
, size_t actions_len
)
2125 OVS_REQUIRES(pmd
->flow_mutex
)
2127 struct dp_netdev_flow
*flow
;
2128 struct netdev_flow_key mask
;
2130 netdev_flow_mask_init(&mask
, match
);
2131 /* Make sure wc does not have metadata. */
2132 ovs_assert(!FLOWMAP_HAS_FIELD(&mask
.mf
.map
, metadata
)
2133 && !FLOWMAP_HAS_FIELD(&mask
.mf
.map
, regs
));
2135 /* Do not allocate extra space. */
2136 flow
= xmalloc(sizeof *flow
- sizeof flow
->cr
.flow
.mf
+ mask
.len
);
2137 memset(&flow
->stats
, 0, sizeof flow
->stats
);
2140 *CONST_CAST(unsigned *, &flow
->pmd_id
) = pmd
->core_id
;
2141 *CONST_CAST(struct flow
*, &flow
->flow
) = match
->flow
;
2142 *CONST_CAST(ovs_u128
*, &flow
->ufid
) = *ufid
;
2143 ovs_refcount_init(&flow
->ref_cnt
);
2144 ovsrcu_set(&flow
->actions
, dp_netdev_actions_create(actions
, actions_len
));
2146 netdev_flow_key_init_masked(&flow
->cr
.flow
, &match
->flow
, &mask
);
2147 dpcls_insert(&pmd
->cls
, &flow
->cr
, &mask
);
2149 cmap_insert(&pmd
->flow_table
, CONST_CAST(struct cmap_node
*, &flow
->node
),
2150 dp_netdev_flow_hash(&flow
->ufid
));
2152 if (OVS_UNLIKELY(VLOG_IS_DBG_ENABLED())) {
2153 struct ds ds
= DS_EMPTY_INITIALIZER
;
2154 struct ofpbuf key_buf
, mask_buf
;
2155 struct odp_flow_key_parms odp_parms
= {
2156 .flow
= &match
->flow
,
2157 .mask
= &match
->wc
.masks
,
2158 .support
= dp_netdev_support
,
2161 ofpbuf_init(&key_buf
, 0);
2162 ofpbuf_init(&mask_buf
, 0);
2164 odp_flow_key_from_flow(&odp_parms
, &key_buf
);
2165 odp_parms
.key_buf
= &key_buf
;
2166 odp_flow_key_from_mask(&odp_parms
, &mask_buf
);
2168 ds_put_cstr(&ds
, "flow_add: ");
2169 odp_format_ufid(ufid
, &ds
);
2170 ds_put_cstr(&ds
, " ");
2171 odp_flow_format(key_buf
.data
, key_buf
.size
,
2172 mask_buf
.data
, mask_buf
.size
,
2174 ds_put_cstr(&ds
, ", actions:");
2175 format_odp_actions(&ds
, actions
, actions_len
);
2177 VLOG_DBG_RL(&upcall_rl
, "%s", ds_cstr(&ds
));
2179 ofpbuf_uninit(&key_buf
);
2180 ofpbuf_uninit(&mask_buf
);
2188 dpif_netdev_flow_put(struct dpif
*dpif
, const struct dpif_flow_put
*put
)
2190 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2191 struct dp_netdev_flow
*netdev_flow
;
2192 struct netdev_flow_key key
;
2193 struct dp_netdev_pmd_thread
*pmd
;
2196 unsigned pmd_id
= put
->pmd_id
== PMD_ID_NULL
2197 ? NON_PMD_CORE_ID
: put
->pmd_id
;
2200 error
= dpif_netdev_flow_from_nlattrs(put
->key
, put
->key_len
, &match
.flow
);
2204 error
= dpif_netdev_mask_from_nlattrs(put
->key
, put
->key_len
,
2205 put
->mask
, put
->mask_len
,
2206 &match
.flow
, &match
.wc
);
2211 pmd
= dp_netdev_get_pmd(dp
, pmd_id
);
2216 /* Must produce a netdev_flow_key for lookup.
2217 * This interface is no longer performance critical, since it is not used
2218 * for upcall processing any more. */
2219 netdev_flow_key_from_flow(&key
, &match
.flow
);
2224 dpif_flow_hash(dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
2227 ovs_mutex_lock(&pmd
->flow_mutex
);
2228 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, &key
);
2230 if (put
->flags
& DPIF_FP_CREATE
) {
2231 if (cmap_count(&pmd
->flow_table
) < MAX_FLOWS
) {
2233 memset(put
->stats
, 0, sizeof *put
->stats
);
2235 dp_netdev_flow_add(pmd
, &match
, &ufid
, put
->actions
,
2245 if (put
->flags
& DPIF_FP_MODIFY
2246 && flow_equal(&match
.flow
, &netdev_flow
->flow
)) {
2247 struct dp_netdev_actions
*new_actions
;
2248 struct dp_netdev_actions
*old_actions
;
2250 new_actions
= dp_netdev_actions_create(put
->actions
,
2253 old_actions
= dp_netdev_flow_get_actions(netdev_flow
);
2254 ovsrcu_set(&netdev_flow
->actions
, new_actions
);
2257 get_dpif_flow_stats(netdev_flow
, put
->stats
);
2259 if (put
->flags
& DPIF_FP_ZERO_STATS
) {
2260 /* XXX: The userspace datapath uses thread local statistics
2261 * (for flows), which should be updated only by the owning
2262 * thread. Since we cannot write on stats memory here,
2263 * we choose not to support this flag. Please note:
2264 * - This feature is currently used only by dpctl commands with
2266 * - Should the need arise, this operation can be implemented
2267 * by keeping a base value (to be update here) for each
2268 * counter, and subtracting it before outputting the stats */
2272 ovsrcu_postpone(dp_netdev_actions_free
, old_actions
);
2273 } else if (put
->flags
& DPIF_FP_CREATE
) {
2276 /* Overlapping flow. */
2280 ovs_mutex_unlock(&pmd
->flow_mutex
);
2281 dp_netdev_pmd_unref(pmd
);
2287 dpif_netdev_flow_del(struct dpif
*dpif
, const struct dpif_flow_del
*del
)
2289 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2290 struct dp_netdev_flow
*netdev_flow
;
2291 struct dp_netdev_pmd_thread
*pmd
;
2292 unsigned pmd_id
= del
->pmd_id
== PMD_ID_NULL
2293 ? NON_PMD_CORE_ID
: del
->pmd_id
;
2296 pmd
= dp_netdev_get_pmd(dp
, pmd_id
);
2301 ovs_mutex_lock(&pmd
->flow_mutex
);
2302 netdev_flow
= dp_netdev_pmd_find_flow(pmd
, del
->ufid
, del
->key
,
2306 get_dpif_flow_stats(netdev_flow
, del
->stats
);
2308 dp_netdev_pmd_remove_flow(pmd
, netdev_flow
);
2312 ovs_mutex_unlock(&pmd
->flow_mutex
);
2313 dp_netdev_pmd_unref(pmd
);
2318 struct dpif_netdev_flow_dump
{
2319 struct dpif_flow_dump up
;
2320 struct cmap_position poll_thread_pos
;
2321 struct cmap_position flow_pos
;
2322 struct dp_netdev_pmd_thread
*cur_pmd
;
2324 struct ovs_mutex mutex
;
2327 static struct dpif_netdev_flow_dump
*
2328 dpif_netdev_flow_dump_cast(struct dpif_flow_dump
*dump
)
2330 return CONTAINER_OF(dump
, struct dpif_netdev_flow_dump
, up
);
2333 static struct dpif_flow_dump
*
2334 dpif_netdev_flow_dump_create(const struct dpif
*dpif_
, bool terse
)
2336 struct dpif_netdev_flow_dump
*dump
;
2338 dump
= xzalloc(sizeof *dump
);
2339 dpif_flow_dump_init(&dump
->up
, dpif_
);
2340 dump
->up
.terse
= terse
;
2341 ovs_mutex_init(&dump
->mutex
);
2347 dpif_netdev_flow_dump_destroy(struct dpif_flow_dump
*dump_
)
2349 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
2351 ovs_mutex_destroy(&dump
->mutex
);
2356 struct dpif_netdev_flow_dump_thread
{
2357 struct dpif_flow_dump_thread up
;
2358 struct dpif_netdev_flow_dump
*dump
;
2359 struct odputil_keybuf keybuf
[FLOW_DUMP_MAX_BATCH
];
2360 struct odputil_keybuf maskbuf
[FLOW_DUMP_MAX_BATCH
];
2363 static struct dpif_netdev_flow_dump_thread
*
2364 dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread
*thread
)
2366 return CONTAINER_OF(thread
, struct dpif_netdev_flow_dump_thread
, up
);
2369 static struct dpif_flow_dump_thread
*
2370 dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump
*dump_
)
2372 struct dpif_netdev_flow_dump
*dump
= dpif_netdev_flow_dump_cast(dump_
);
2373 struct dpif_netdev_flow_dump_thread
*thread
;
2375 thread
= xmalloc(sizeof *thread
);
2376 dpif_flow_dump_thread_init(&thread
->up
, &dump
->up
);
2377 thread
->dump
= dump
;
2382 dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread
*thread_
)
2384 struct dpif_netdev_flow_dump_thread
*thread
2385 = dpif_netdev_flow_dump_thread_cast(thread_
);
2391 dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread
*thread_
,
2392 struct dpif_flow
*flows
, int max_flows
)
2394 struct dpif_netdev_flow_dump_thread
*thread
2395 = dpif_netdev_flow_dump_thread_cast(thread_
);
2396 struct dpif_netdev_flow_dump
*dump
= thread
->dump
;
2397 struct dp_netdev_flow
*netdev_flows
[FLOW_DUMP_MAX_BATCH
];
2401 ovs_mutex_lock(&dump
->mutex
);
2402 if (!dump
->status
) {
2403 struct dpif_netdev
*dpif
= dpif_netdev_cast(thread
->up
.dpif
);
2404 struct dp_netdev
*dp
= get_dp_netdev(&dpif
->dpif
);
2405 struct dp_netdev_pmd_thread
*pmd
= dump
->cur_pmd
;
2406 int flow_limit
= MIN(max_flows
, FLOW_DUMP_MAX_BATCH
);
2408 /* First call to dump_next(), extracts the first pmd thread.
2409 * If there is no pmd thread, returns immediately. */
2411 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
2413 ovs_mutex_unlock(&dump
->mutex
);
2420 for (n_flows
= 0; n_flows
< flow_limit
; n_flows
++) {
2421 struct cmap_node
*node
;
2423 node
= cmap_next_position(&pmd
->flow_table
, &dump
->flow_pos
);
2427 netdev_flows
[n_flows
] = CONTAINER_OF(node
,
2428 struct dp_netdev_flow
,
2431 /* When finishing dumping the current pmd thread, moves to
2433 if (n_flows
< flow_limit
) {
2434 memset(&dump
->flow_pos
, 0, sizeof dump
->flow_pos
);
2435 dp_netdev_pmd_unref(pmd
);
2436 pmd
= dp_netdev_pmd_get_next(dp
, &dump
->poll_thread_pos
);
2442 /* Keeps the reference to next caller. */
2443 dump
->cur_pmd
= pmd
;
2445 /* If the current dump is empty, do not exit the loop, since the
2446 * remaining pmds could have flows to be dumped. Just dumps again
2447 * on the new 'pmd'. */
2450 ovs_mutex_unlock(&dump
->mutex
);
2452 for (i
= 0; i
< n_flows
; i
++) {
2453 struct odputil_keybuf
*maskbuf
= &thread
->maskbuf
[i
];
2454 struct odputil_keybuf
*keybuf
= &thread
->keybuf
[i
];
2455 struct dp_netdev_flow
*netdev_flow
= netdev_flows
[i
];
2456 struct dpif_flow
*f
= &flows
[i
];
2457 struct ofpbuf key
, mask
;
2459 ofpbuf_use_stack(&key
, keybuf
, sizeof *keybuf
);
2460 ofpbuf_use_stack(&mask
, maskbuf
, sizeof *maskbuf
);
2461 dp_netdev_flow_to_dpif_flow(netdev_flow
, &key
, &mask
, f
,
2469 dpif_netdev_execute(struct dpif
*dpif
, struct dpif_execute
*execute
)
2470 OVS_NO_THREAD_SAFETY_ANALYSIS
2472 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2473 struct dp_netdev_pmd_thread
*pmd
;
2474 struct dp_packet_batch pp
;
2476 if (dp_packet_size(execute
->packet
) < ETH_HEADER_LEN
||
2477 dp_packet_size(execute
->packet
) > UINT16_MAX
) {
2481 /* Tries finding the 'pmd'. If NULL is returned, that means
2482 * the current thread is a non-pmd thread and should use
2483 * dp_netdev_get_pmd(dp, NON_PMD_CORE_ID). */
2484 pmd
= ovsthread_getspecific(dp
->per_pmd_key
);
2486 pmd
= dp_netdev_get_pmd(dp
, NON_PMD_CORE_ID
);
2489 /* If the current thread is non-pmd thread, acquires
2490 * the 'non_pmd_mutex'. */
2491 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
2492 ovs_mutex_lock(&dp
->non_pmd_mutex
);
2495 /* The action processing expects the RSS hash to be valid, because
2496 * it's always initialized at the beginning of datapath processing.
2497 * In this case, though, 'execute->packet' may not have gone through
2498 * the datapath at all, it may have been generated by the upper layer
2499 * (OpenFlow packet-out, BFD frame, ...). */
2500 if (!dp_packet_rss_valid(execute
->packet
)) {
2501 dp_packet_set_rss_hash(execute
->packet
,
2502 flow_hash_5tuple(execute
->flow
, 0));
2505 packet_batch_init_packet(&pp
, execute
->packet
);
2506 dp_netdev_execute_actions(pmd
, &pp
, false, execute
->actions
,
2507 execute
->actions_len
, time_msec());
2509 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
2510 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
2511 dp_netdev_pmd_unref(pmd
);
2518 dpif_netdev_operate(struct dpif
*dpif
, struct dpif_op
**ops
, size_t n_ops
)
2522 for (i
= 0; i
< n_ops
; i
++) {
2523 struct dpif_op
*op
= ops
[i
];
2526 case DPIF_OP_FLOW_PUT
:
2527 op
->error
= dpif_netdev_flow_put(dpif
, &op
->u
.flow_put
);
2530 case DPIF_OP_FLOW_DEL
:
2531 op
->error
= dpif_netdev_flow_del(dpif
, &op
->u
.flow_del
);
2534 case DPIF_OP_EXECUTE
:
2535 op
->error
= dpif_netdev_execute(dpif
, &op
->u
.execute
);
2538 case DPIF_OP_FLOW_GET
:
2539 op
->error
= dpif_netdev_flow_get(dpif
, &op
->u
.flow_get
);
2545 /* Changes the number or the affinity of pmd threads. The changes are actually
2546 * applied in dpif_netdev_run(). */
2548 dpif_netdev_pmd_set(struct dpif
*dpif
, const char *cmask
)
2550 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2552 if (!nullable_string_is_equal(dp
->requested_pmd_cmask
, cmask
)) {
2553 free(dp
->requested_pmd_cmask
);
2554 dp
->requested_pmd_cmask
= nullable_xstrdup(cmask
);
2561 dpif_netdev_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
2562 uint32_t queue_id
, uint32_t *priority
)
2564 *priority
= queue_id
;
2569 /* Creates and returns a new 'struct dp_netdev_actions', whose actions are
2570 * a copy of the 'ofpacts_len' bytes of 'ofpacts'. */
2571 struct dp_netdev_actions
*
2572 dp_netdev_actions_create(const struct nlattr
*actions
, size_t size
)
2574 struct dp_netdev_actions
*netdev_actions
;
2576 netdev_actions
= xmalloc(sizeof *netdev_actions
+ size
);
2577 memcpy(netdev_actions
->actions
, actions
, size
);
2578 netdev_actions
->size
= size
;
2580 return netdev_actions
;
2583 struct dp_netdev_actions
*
2584 dp_netdev_flow_get_actions(const struct dp_netdev_flow
*flow
)
2586 return ovsrcu_get(struct dp_netdev_actions
*, &flow
->actions
);
2590 dp_netdev_actions_free(struct dp_netdev_actions
*actions
)
2595 static inline unsigned long long
2596 cycles_counter(void)
2599 return rte_get_tsc_cycles();
2605 /* Fake mutex to make sure that the calls to cycles_count_* are balanced */
2606 extern struct ovs_mutex cycles_counter_fake_mutex
;
2608 /* Start counting cycles. Must be followed by 'cycles_count_end()' */
2610 cycles_count_start(struct dp_netdev_pmd_thread
*pmd
)
2611 OVS_ACQUIRES(&cycles_counter_fake_mutex
)
2612 OVS_NO_THREAD_SAFETY_ANALYSIS
2614 pmd
->last_cycles
= cycles_counter();
2617 /* Stop counting cycles and add them to the counter 'type' */
2619 cycles_count_end(struct dp_netdev_pmd_thread
*pmd
,
2620 enum pmd_cycles_counter_type type
)
2621 OVS_RELEASES(&cycles_counter_fake_mutex
)
2622 OVS_NO_THREAD_SAFETY_ANALYSIS
2624 unsigned long long interval
= cycles_counter() - pmd
->last_cycles
;
2626 non_atomic_ullong_add(&pmd
->cycles
.n
[type
], interval
);
2630 dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread
*pmd
,
2631 struct dp_netdev_port
*port
,
2632 struct netdev_rxq
*rxq
)
2634 struct dp_packet_batch batch
;
2637 dp_packet_batch_init(&batch
);
2638 cycles_count_start(pmd
);
2639 error
= netdev_rxq_recv(rxq
, &batch
);
2640 cycles_count_end(pmd
, PMD_CYCLES_POLLING
);
2642 *recirc_depth_get() = 0;
2644 cycles_count_start(pmd
);
2645 dp_netdev_input(pmd
, &batch
, port
->port_no
);
2646 cycles_count_end(pmd
, PMD_CYCLES_PROCESSING
);
2647 } else if (error
!= EAGAIN
&& error
!= EOPNOTSUPP
) {
2648 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2650 VLOG_ERR_RL(&rl
, "error receiving data from %s: %s",
2651 netdev_get_name(port
->netdev
), ovs_strerror(error
));
2656 port_reconfigure(struct dp_netdev_port
*port
)
2658 struct netdev
*netdev
= port
->netdev
;
2661 if (!netdev_is_reconf_required(netdev
)) {
2665 /* Closes the existing 'rxq's. */
2666 for (i
= 0; i
< port
->n_rxq
; i
++) {
2667 netdev_rxq_close(port
->rxq
[i
]);
2668 port
->rxq
[i
] = NULL
;
2672 /* Allows 'netdev' to apply the pending configuration changes. */
2673 err
= netdev_reconfigure(netdev
);
2674 if (err
&& (err
!= EOPNOTSUPP
)) {
2675 VLOG_ERR("Failed to set interface %s new configuration",
2676 netdev_get_name(netdev
));
2679 /* If the netdev_reconfigure() above succeeds, reopens the 'rxq's. */
2680 port
->rxq
= xrealloc(port
->rxq
, sizeof *port
->rxq
* netdev_n_rxq(netdev
));
2681 /* Realloc 'used' counters for tx queues. */
2682 free(port
->txq_used
);
2683 port
->txq_used
= xcalloc(netdev_n_txq(netdev
), sizeof *port
->txq_used
);
2685 for (i
= 0; i
< netdev_n_rxq(netdev
); i
++) {
2686 err
= netdev_rxq_open(netdev
, &port
->rxq
[i
], i
);
2697 reconfigure_pmd_threads(struct dp_netdev
*dp
)
2698 OVS_REQUIRES(dp
->port_mutex
)
2700 struct dp_netdev_port
*port
, *next
;
2703 dp_netdev_destroy_all_pmds(dp
);
2705 /* Reconfigures the cpu mask. */
2706 ovs_numa_set_cpu_mask(dp
->requested_pmd_cmask
);
2707 free(dp
->pmd_cmask
);
2708 dp
->pmd_cmask
= nullable_xstrdup(dp
->requested_pmd_cmask
);
2710 n_cores
= ovs_numa_get_n_cores();
2711 if (n_cores
== OVS_CORE_UNSPEC
) {
2712 VLOG_ERR("Cannot get cpu core info");
2716 HMAP_FOR_EACH_SAFE (port
, next
, node
, &dp
->ports
) {
2719 err
= port_reconfigure(port
);
2721 hmap_remove(&dp
->ports
, &port
->node
);
2722 seq_change(dp
->port_seq
);
2725 port
->dynamic_txqs
= netdev_n_txq(port
->netdev
) < n_cores
+ 1;
2728 /* Restores the non-pmd. */
2729 dp_netdev_set_nonpmd(dp
);
2730 /* Restores all pmd threads. */
2731 dp_netdev_reset_pmd_threads(dp
);
2734 /* Returns true if one of the netdevs in 'dp' requires a reconfiguration */
2736 ports_require_restart(const struct dp_netdev
*dp
)
2737 OVS_REQUIRES(dp
->port_mutex
)
2739 struct dp_netdev_port
*port
;
2741 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
2742 if (netdev_is_reconf_required(port
->netdev
)) {
2750 /* Return true if needs to revalidate datapath flows. */
2752 dpif_netdev_run(struct dpif
*dpif
)
2754 struct dp_netdev_port
*port
;
2755 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2756 struct dp_netdev_pmd_thread
*non_pmd
= dp_netdev_get_pmd(dp
,
2758 uint64_t new_tnl_seq
;
2760 ovs_mutex_lock(&dp
->port_mutex
);
2761 ovs_mutex_lock(&dp
->non_pmd_mutex
);
2762 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
2763 if (!netdev_is_pmd(port
->netdev
)) {
2766 for (i
= 0; i
< port
->n_rxq
; i
++) {
2767 dp_netdev_process_rxq_port(non_pmd
, port
, port
->rxq
[i
]);
2771 dpif_netdev_xps_revalidate_pmd(non_pmd
, time_msec(), false);
2772 ovs_mutex_unlock(&dp
->non_pmd_mutex
);
2774 dp_netdev_pmd_unref(non_pmd
);
2776 if (!nullable_string_is_equal(dp
->pmd_cmask
, dp
->requested_pmd_cmask
)
2777 || ports_require_restart(dp
)) {
2778 reconfigure_pmd_threads(dp
);
2780 ovs_mutex_unlock(&dp
->port_mutex
);
2782 tnl_neigh_cache_run();
2784 new_tnl_seq
= seq_read(tnl_conf_seq
);
2786 if (dp
->last_tnl_conf_seq
!= new_tnl_seq
) {
2787 dp
->last_tnl_conf_seq
= new_tnl_seq
;
2794 dpif_netdev_wait(struct dpif
*dpif
)
2796 struct dp_netdev_port
*port
;
2797 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2799 ovs_mutex_lock(&dp_netdev_mutex
);
2800 ovs_mutex_lock(&dp
->port_mutex
);
2801 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
2802 netdev_wait_reconf_required(port
->netdev
);
2803 if (!netdev_is_pmd(port
->netdev
)) {
2806 for (i
= 0; i
< port
->n_rxq
; i
++) {
2807 netdev_rxq_wait(port
->rxq
[i
]);
2811 ovs_mutex_unlock(&dp
->port_mutex
);
2812 ovs_mutex_unlock(&dp_netdev_mutex
);
2813 seq_wait(tnl_conf_seq
, dp
->last_tnl_conf_seq
);
2817 pmd_free_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
2819 struct tx_port
*tx_port_cached
;
2821 /* Free all used tx queue ids. */
2822 dpif_netdev_xps_revalidate_pmd(pmd
, 0, true);
2824 HMAP_FOR_EACH_POP (tx_port_cached
, node
, &pmd
->port_cache
) {
2825 free(tx_port_cached
);
2829 /* Copies ports from 'pmd->tx_ports' (shared with the main thread) to
2830 * 'pmd->port_cache' (thread local) */
2832 pmd_load_cached_ports(struct dp_netdev_pmd_thread
*pmd
)
2833 OVS_REQUIRES(pmd
->port_mutex
)
2835 struct tx_port
*tx_port
, *tx_port_cached
;
2837 pmd_free_cached_ports(pmd
);
2838 hmap_shrink(&pmd
->port_cache
);
2840 HMAP_FOR_EACH (tx_port
, node
, &pmd
->tx_ports
) {
2841 tx_port_cached
= xmemdup(tx_port
, sizeof *tx_port_cached
);
2842 hmap_insert(&pmd
->port_cache
, &tx_port_cached
->node
,
2843 hash_port_no(tx_port_cached
->port
->port_no
));
2848 pmd_load_queues_and_ports(struct dp_netdev_pmd_thread
*pmd
,
2849 struct rxq_poll
**ppoll_list
)
2851 struct rxq_poll
*poll_list
= *ppoll_list
;
2852 struct rxq_poll
*poll
;
2855 ovs_mutex_lock(&pmd
->port_mutex
);
2856 poll_list
= xrealloc(poll_list
, pmd
->poll_cnt
* sizeof *poll_list
);
2859 LIST_FOR_EACH (poll
, node
, &pmd
->poll_list
) {
2860 poll_list
[i
++] = *poll
;
2863 pmd_load_cached_ports(pmd
);
2865 ovs_mutex_unlock(&pmd
->port_mutex
);
2867 *ppoll_list
= poll_list
;
2872 pmd_thread_main(void *f_
)
2874 struct dp_netdev_pmd_thread
*pmd
= f_
;
2875 unsigned int lc
= 0;
2876 struct rxq_poll
*poll_list
;
2877 unsigned int port_seq
= PMD_INITIAL_SEQ
;
2884 /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */
2885 ovsthread_setspecific(pmd
->dp
->per_pmd_key
, pmd
);
2886 ovs_numa_thread_setaffinity_core(pmd
->core_id
);
2887 dpdk_set_lcore_id(pmd
->core_id
);
2888 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
2890 emc_cache_init(&pmd
->flow_cache
);
2892 /* List port/core affinity */
2893 for (i
= 0; i
< poll_cnt
; i
++) {
2894 VLOG_DBG("Core %d processing port \'%s\' with queue-id %d\n",
2895 pmd
->core_id
, netdev_get_name(poll_list
[i
].port
->netdev
),
2896 netdev_rxq_get_queue_id(poll_list
[i
].rx
));
2900 for (i
= 0; i
< poll_cnt
; i
++) {
2901 dp_netdev_process_rxq_port(pmd
, poll_list
[i
].port
, poll_list
[i
].rx
);
2909 coverage_try_clear();
2910 if (!ovsrcu_try_quiesce()) {
2911 emc_cache_slow_sweep(&pmd
->flow_cache
);
2914 atomic_read_relaxed(&pmd
->change_seq
, &seq
);
2915 if (seq
!= port_seq
) {
2922 poll_cnt
= pmd_load_queues_and_ports(pmd
, &poll_list
);
2923 exiting
= latch_is_set(&pmd
->exit_latch
);
2924 /* Signal here to make sure the pmd finishes
2925 * reloading the updated configuration. */
2926 dp_netdev_pmd_reload_done(pmd
);
2928 emc_cache_uninit(&pmd
->flow_cache
);
2935 pmd_free_cached_ports(pmd
);
2940 dp_netdev_disable_upcall(struct dp_netdev
*dp
)
2941 OVS_ACQUIRES(dp
->upcall_rwlock
)
2943 fat_rwlock_wrlock(&dp
->upcall_rwlock
);
2947 dpif_netdev_disable_upcall(struct dpif
*dpif
)
2948 OVS_NO_THREAD_SAFETY_ANALYSIS
2950 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2951 dp_netdev_disable_upcall(dp
);
2955 dp_netdev_enable_upcall(struct dp_netdev
*dp
)
2956 OVS_RELEASES(dp
->upcall_rwlock
)
2958 fat_rwlock_unlock(&dp
->upcall_rwlock
);
2962 dpif_netdev_enable_upcall(struct dpif
*dpif
)
2963 OVS_NO_THREAD_SAFETY_ANALYSIS
2965 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
2966 dp_netdev_enable_upcall(dp
);
2970 dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread
*pmd
)
2972 ovs_mutex_lock(&pmd
->cond_mutex
);
2973 xpthread_cond_signal(&pmd
->cond
);
2974 ovs_mutex_unlock(&pmd
->cond_mutex
);
2977 /* Finds and refs the dp_netdev_pmd_thread on core 'core_id'. Returns
2978 * the pointer if succeeds, otherwise, NULL.
2980 * Caller must unrefs the returned reference. */
2981 static struct dp_netdev_pmd_thread
*
2982 dp_netdev_get_pmd(struct dp_netdev
*dp
, unsigned core_id
)
2984 struct dp_netdev_pmd_thread
*pmd
;
2985 const struct cmap_node
*pnode
;
2987 pnode
= cmap_find(&dp
->poll_threads
, hash_int(core_id
, 0));
2991 pmd
= CONTAINER_OF(pnode
, struct dp_netdev_pmd_thread
, node
);
2993 return dp_netdev_pmd_try_ref(pmd
) ? pmd
: NULL
;
2996 /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */
2998 dp_netdev_set_nonpmd(struct dp_netdev
*dp
)
2999 OVS_REQUIRES(dp
->port_mutex
)
3001 struct dp_netdev_pmd_thread
*non_pmd
;
3002 struct dp_netdev_port
*port
;
3004 non_pmd
= xzalloc(sizeof *non_pmd
);
3005 dp_netdev_configure_pmd(non_pmd
, dp
, NON_PMD_CORE_ID
, OVS_NUMA_UNSPEC
);
3007 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3008 dp_netdev_add_port_tx_to_pmd(non_pmd
, port
);
3011 dp_netdev_reload_pmd__(non_pmd
);
3014 /* Caller must have valid pointer to 'pmd'. */
3016 dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread
*pmd
)
3018 return ovs_refcount_try_ref_rcu(&pmd
->ref_cnt
);
3022 dp_netdev_pmd_unref(struct dp_netdev_pmd_thread
*pmd
)
3024 if (pmd
&& ovs_refcount_unref(&pmd
->ref_cnt
) == 1) {
3025 ovsrcu_postpone(dp_netdev_destroy_pmd
, pmd
);
3029 /* Given cmap position 'pos', tries to ref the next node. If try_ref()
3030 * fails, keeps checking for next node until reaching the end of cmap.
3032 * Caller must unrefs the returned reference. */
3033 static struct dp_netdev_pmd_thread
*
3034 dp_netdev_pmd_get_next(struct dp_netdev
*dp
, struct cmap_position
*pos
)
3036 struct dp_netdev_pmd_thread
*next
;
3039 struct cmap_node
*node
;
3041 node
= cmap_next_position(&dp
->poll_threads
, pos
);
3042 next
= node
? CONTAINER_OF(node
, struct dp_netdev_pmd_thread
, node
)
3044 } while (next
&& !dp_netdev_pmd_try_ref(next
));
3049 /* Configures the 'pmd' based on the input argument. */
3051 dp_netdev_configure_pmd(struct dp_netdev_pmd_thread
*pmd
, struct dp_netdev
*dp
,
3052 unsigned core_id
, int numa_id
)
3055 pmd
->core_id
= core_id
;
3056 pmd
->numa_id
= numa_id
;
3059 atomic_init(&pmd
->static_tx_qid
,
3060 (core_id
== NON_PMD_CORE_ID
)
3061 ? ovs_numa_get_n_cores()
3062 : get_n_pmd_threads(dp
));
3064 ovs_refcount_init(&pmd
->ref_cnt
);
3065 latch_init(&pmd
->exit_latch
);
3066 atomic_init(&pmd
->change_seq
, PMD_INITIAL_SEQ
);
3067 xpthread_cond_init(&pmd
->cond
, NULL
);
3068 ovs_mutex_init(&pmd
->cond_mutex
);
3069 ovs_mutex_init(&pmd
->flow_mutex
);
3070 ovs_mutex_init(&pmd
->port_mutex
);
3071 dpcls_init(&pmd
->cls
);
3072 cmap_init(&pmd
->flow_table
);
3073 ovs_list_init(&pmd
->poll_list
);
3074 hmap_init(&pmd
->tx_ports
);
3075 hmap_init(&pmd
->port_cache
);
3076 /* init the 'flow_cache' since there is no
3077 * actual thread created for NON_PMD_CORE_ID. */
3078 if (core_id
== NON_PMD_CORE_ID
) {
3079 emc_cache_init(&pmd
->flow_cache
);
3081 cmap_insert(&dp
->poll_threads
, CONST_CAST(struct cmap_node
*, &pmd
->node
),
3082 hash_int(core_id
, 0));
3086 dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread
*pmd
)
3088 dp_netdev_pmd_flow_flush(pmd
);
3089 dpcls_destroy(&pmd
->cls
);
3090 hmap_destroy(&pmd
->port_cache
);
3091 hmap_destroy(&pmd
->tx_ports
);
3092 cmap_destroy(&pmd
->flow_table
);
3093 ovs_mutex_destroy(&pmd
->flow_mutex
);
3094 latch_destroy(&pmd
->exit_latch
);
3095 xpthread_cond_destroy(&pmd
->cond
);
3096 ovs_mutex_destroy(&pmd
->cond_mutex
);
3097 ovs_mutex_destroy(&pmd
->port_mutex
);
3101 /* Stops the pmd thread, removes it from the 'dp->poll_threads',
3102 * and unrefs the struct. */
3104 dp_netdev_del_pmd(struct dp_netdev
*dp
, struct dp_netdev_pmd_thread
*pmd
)
3106 /* NON_PMD_CORE_ID doesn't have a thread, so we don't have to synchronize,
3107 * but extra cleanup is necessary */
3108 if (pmd
->core_id
== NON_PMD_CORE_ID
) {
3109 emc_cache_uninit(&pmd
->flow_cache
);
3110 pmd_free_cached_ports(pmd
);
3112 latch_set(&pmd
->exit_latch
);
3113 dp_netdev_reload_pmd__(pmd
);
3114 ovs_numa_unpin_core(pmd
->core_id
);
3115 xpthread_join(pmd
->thread
, NULL
);
3118 dp_netdev_pmd_clear_ports(pmd
);
3120 /* Purges the 'pmd''s flows after stopping the thread, but before
3121 * destroying the flows, so that the flow stats can be collected. */
3122 if (dp
->dp_purge_cb
) {
3123 dp
->dp_purge_cb(dp
->dp_purge_aux
, pmd
->core_id
);
3125 cmap_remove(&pmd
->dp
->poll_threads
, &pmd
->node
, hash_int(pmd
->core_id
, 0));
3126 dp_netdev_pmd_unref(pmd
);
3129 /* Destroys all pmd threads. */
3131 dp_netdev_destroy_all_pmds(struct dp_netdev
*dp
)
3133 struct dp_netdev_pmd_thread
*pmd
;
3134 struct dp_netdev_pmd_thread
**pmd_list
;
3135 size_t k
= 0, n_pmds
;
3137 n_pmds
= cmap_count(&dp
->poll_threads
);
3138 pmd_list
= xcalloc(n_pmds
, sizeof *pmd_list
);
3140 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3141 /* We cannot call dp_netdev_del_pmd(), since it alters
3142 * 'dp->poll_threads' (while we're iterating it) and it
3144 ovs_assert(k
< n_pmds
);
3145 pmd_list
[k
++] = pmd
;
3148 for (size_t i
= 0; i
< k
; i
++) {
3149 dp_netdev_del_pmd(dp
, pmd_list
[i
]);
3154 /* Deletes all pmd threads on numa node 'numa_id' and
3155 * fixes static_tx_qids of other threads to keep them sequential. */
3157 dp_netdev_del_pmds_on_numa(struct dp_netdev
*dp
, int numa_id
)
3159 struct dp_netdev_pmd_thread
*pmd
;
3160 int n_pmds_on_numa
, n_pmds
;
3161 int *free_idx
, k
= 0;
3162 struct dp_netdev_pmd_thread
**pmd_list
;
3164 n_pmds_on_numa
= get_n_pmd_threads_on_numa(dp
, numa_id
);
3165 free_idx
= xcalloc(n_pmds_on_numa
, sizeof *free_idx
);
3166 pmd_list
= xcalloc(n_pmds_on_numa
, sizeof *pmd_list
);
3168 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3169 /* We cannot call dp_netdev_del_pmd(), since it alters
3170 * 'dp->poll_threads' (while we're iterating it) and it
3172 if (pmd
->numa_id
== numa_id
) {
3173 atomic_read_relaxed(&pmd
->static_tx_qid
, &free_idx
[k
]);
3175 ovs_assert(k
< n_pmds_on_numa
);
3180 for (int i
= 0; i
< k
; i
++) {
3181 dp_netdev_del_pmd(dp
, pmd_list
[i
]);
3184 n_pmds
= get_n_pmd_threads(dp
);
3185 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3188 atomic_read_relaxed(&pmd
->static_tx_qid
, &old_tx_qid
);
3190 if (old_tx_qid
>= n_pmds
) {
3191 int new_tx_qid
= free_idx
[--k
];
3193 atomic_store_relaxed(&pmd
->static_tx_qid
, new_tx_qid
);
3201 /* Deletes all rx queues from pmd->poll_list and all the ports from
3204 dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread
*pmd
)
3206 struct rxq_poll
*poll
;
3207 struct tx_port
*port
;
3209 ovs_mutex_lock(&pmd
->port_mutex
);
3210 LIST_FOR_EACH_POP (poll
, node
, &pmd
->poll_list
) {
3214 HMAP_FOR_EACH_POP (port
, node
, &pmd
->tx_ports
) {
3217 ovs_mutex_unlock(&pmd
->port_mutex
);
3220 static struct tx_port
*
3221 tx_port_lookup(const struct hmap
*hmap
, odp_port_t port_no
)
3225 HMAP_FOR_EACH_IN_BUCKET (tx
, node
, hash_port_no(port_no
), hmap
) {
3226 if (tx
->port
->port_no
== port_no
) {
3234 /* Deletes all rx queues of 'port' from 'poll_list', and the 'port' from
3235 * 'tx_ports' of 'pmd' thread. Returns true if 'port' was found in 'pmd'
3236 * (therefore a restart is required). */
3238 dp_netdev_del_port_from_pmd__(struct dp_netdev_port
*port
,
3239 struct dp_netdev_pmd_thread
*pmd
)
3241 struct rxq_poll
*poll
, *next
;
3245 ovs_mutex_lock(&pmd
->port_mutex
);
3246 LIST_FOR_EACH_SAFE (poll
, next
, node
, &pmd
->poll_list
) {
3247 if (poll
->port
== port
) {
3249 ovs_list_remove(&poll
->node
);
3255 tx
= tx_port_lookup(&pmd
->tx_ports
, port
->port_no
);
3257 hmap_remove(&pmd
->tx_ports
, &tx
->node
);
3261 ovs_mutex_unlock(&pmd
->port_mutex
);
3266 /* Deletes 'port' from the 'poll_list' and from the 'tx_ports' of all the pmd
3267 * threads. The pmd threads that need to be restarted are inserted in
3270 dp_netdev_del_port_from_all_pmds__(struct dp_netdev
*dp
,
3271 struct dp_netdev_port
*port
,
3272 struct hmapx
*to_reload
)
3274 struct dp_netdev_pmd_thread
*pmd
;
3276 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3279 found
= dp_netdev_del_port_from_pmd__(port
, pmd
);
3282 hmapx_add(to_reload
, pmd
);
3287 /* Deletes 'port' from the 'poll_list' and from the 'tx_ports' of all the pmd
3288 * threads. Reloads the threads if needed. */
3290 dp_netdev_del_port_from_all_pmds(struct dp_netdev
*dp
,
3291 struct dp_netdev_port
*port
)
3293 struct dp_netdev_pmd_thread
*pmd
;
3294 struct hmapx to_reload
= HMAPX_INITIALIZER(&to_reload
);
3295 struct hmapx_node
*node
;
3297 dp_netdev_del_port_from_all_pmds__(dp
, port
, &to_reload
);
3299 HMAPX_FOR_EACH (node
, &to_reload
) {
3300 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3301 dp_netdev_reload_pmd__(pmd
);
3304 hmapx_destroy(&to_reload
);
3308 /* Returns PMD thread from this numa node with fewer rx queues to poll.
3309 * Returns NULL if there is no PMD threads on this numa node.
3310 * Can be called safely only by main thread. */
3311 static struct dp_netdev_pmd_thread
*
3312 dp_netdev_less_loaded_pmd_on_numa(struct dp_netdev
*dp
, int numa_id
)
3315 struct dp_netdev_pmd_thread
*pmd
, *res
= NULL
;
3317 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3318 if (pmd
->numa_id
== numa_id
3319 && (min_cnt
> pmd
->poll_cnt
|| res
== NULL
)) {
3320 min_cnt
= pmd
->poll_cnt
;
3328 /* Adds rx queue to poll_list of PMD thread. */
3330 dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
3331 struct dp_netdev_port
*port
, struct netdev_rxq
*rx
)
3332 OVS_REQUIRES(pmd
->port_mutex
)
3334 struct rxq_poll
*poll
= xmalloc(sizeof *poll
);
3339 ovs_list_push_back(&pmd
->poll_list
, &poll
->node
);
3343 /* Add 'port' to the tx port cache of 'pmd', which must be reloaded for the
3344 * changes to take effect. */
3346 dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread
*pmd
,
3347 struct dp_netdev_port
*port
)
3349 struct tx_port
*tx
= xzalloc(sizeof *tx
);
3354 ovs_mutex_lock(&pmd
->port_mutex
);
3355 hmap_insert(&pmd
->tx_ports
, &tx
->node
, hash_port_no(tx
->port
->port_no
));
3356 ovs_mutex_unlock(&pmd
->port_mutex
);
3359 /* Distribute all rx queues of 'port' between PMD threads in 'dp'. The pmd
3360 * threads that need to be restarted are inserted in 'to_reload'. */
3362 dp_netdev_add_port_rx_to_pmds(struct dp_netdev
*dp
,
3363 struct dp_netdev_port
*port
,
3364 struct hmapx
*to_reload
)
3366 int numa_id
= netdev_get_numa_id(port
->netdev
);
3369 if (!netdev_is_pmd(port
->netdev
)) {
3373 for (i
= 0; i
< port
->n_rxq
; i
++) {
3374 struct dp_netdev_pmd_thread
*pmd
;
3376 pmd
= dp_netdev_less_loaded_pmd_on_numa(dp
, numa_id
);
3378 VLOG_WARN("There's no pmd thread on numa node %d", numa_id
);
3382 ovs_mutex_lock(&pmd
->port_mutex
);
3383 dp_netdev_add_rxq_to_pmd(pmd
, port
, port
->rxq
[i
]);
3384 ovs_mutex_unlock(&pmd
->port_mutex
);
3386 hmapx_add(to_reload
, pmd
);
3390 /* Distributes all rx queues of 'port' between all PMD threads in 'dp' and
3391 * inserts 'port' in the PMD threads 'tx_ports'. The pmd threads that need to
3392 * be restarted are inserted in 'to_reload'. */
3394 dp_netdev_add_port_to_pmds__(struct dp_netdev
*dp
, struct dp_netdev_port
*port
,
3395 struct hmapx
*to_reload
)
3397 struct dp_netdev_pmd_thread
*pmd
;
3399 dp_netdev_add_port_rx_to_pmds(dp
, port
, to_reload
);
3401 CMAP_FOR_EACH (pmd
, node
, &dp
->poll_threads
) {
3402 dp_netdev_add_port_tx_to_pmd(pmd
, port
);
3403 hmapx_add(to_reload
, pmd
);
3407 /* Distributes all rx queues of 'port' between all PMD threads in 'dp', inserts
3408 * 'port' in the PMD threads 'tx_ports' and reloads them, if needed. */
3410 dp_netdev_add_port_to_pmds(struct dp_netdev
*dp
, struct dp_netdev_port
*port
)
3412 struct dp_netdev_pmd_thread
*pmd
;
3413 struct hmapx to_reload
= HMAPX_INITIALIZER(&to_reload
);
3414 struct hmapx_node
*node
;
3416 dp_netdev_add_port_to_pmds__(dp
, port
, &to_reload
);
3418 HMAPX_FOR_EACH (node
, &to_reload
) {
3419 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3420 dp_netdev_reload_pmd__(pmd
);
3423 hmapx_destroy(&to_reload
);
3426 /* Starts pmd threads for the numa node 'numa_id', if not already started.
3427 * The function takes care of filling the threads tx port cache. */
3429 dp_netdev_set_pmds_on_numa(struct dp_netdev
*dp
, int numa_id
)
3430 OVS_REQUIRES(dp
->port_mutex
)
3434 if (!ovs_numa_numa_id_is_valid(numa_id
)) {
3435 VLOG_WARN("Cannot create pmd threads due to numa id (%d) invalid",
3440 n_pmds
= get_n_pmd_threads_on_numa(dp
, numa_id
);
3442 /* If there are already pmd threads created for the numa node
3443 * in which 'netdev' is on, do nothing. Else, creates the
3444 * pmd threads for the numa node. */
3446 int can_have
, n_unpinned
, i
;
3448 n_unpinned
= ovs_numa_get_n_unpinned_cores_on_numa(numa_id
);
3450 VLOG_WARN("Cannot create pmd threads due to out of unpinned "
3451 "cores on numa node %d", numa_id
);
3455 /* If cpu mask is specified, uses all unpinned cores, otherwise
3456 * tries creating NR_PMD_THREADS pmd threads. */
3457 can_have
= dp
->pmd_cmask
? n_unpinned
: MIN(n_unpinned
, NR_PMD_THREADS
);
3458 for (i
= 0; i
< can_have
; i
++) {
3459 unsigned core_id
= ovs_numa_get_unpinned_core_on_numa(numa_id
);
3460 struct dp_netdev_pmd_thread
*pmd
= xzalloc(sizeof *pmd
);
3461 struct dp_netdev_port
*port
;
3463 dp_netdev_configure_pmd(pmd
, dp
, core_id
, numa_id
);
3465 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3466 dp_netdev_add_port_tx_to_pmd(pmd
, port
);
3469 pmd
->thread
= ovs_thread_create("pmd", pmd_thread_main
, pmd
);
3471 VLOG_INFO("Created %d pmd threads on numa node %d", can_have
, numa_id
);
3476 /* Called after pmd threads config change. Restarts pmd threads with
3477 * new configuration. */
3479 dp_netdev_reset_pmd_threads(struct dp_netdev
*dp
)
3480 OVS_REQUIRES(dp
->port_mutex
)
3482 struct hmapx to_reload
= HMAPX_INITIALIZER(&to_reload
);
3483 struct dp_netdev_pmd_thread
*pmd
;
3484 struct dp_netdev_port
*port
;
3485 struct hmapx_node
*node
;
3487 HMAP_FOR_EACH (port
, node
, &dp
->ports
) {
3488 if (netdev_is_pmd(port
->netdev
)) {
3489 int numa_id
= netdev_get_numa_id(port
->netdev
);
3491 dp_netdev_set_pmds_on_numa(dp
, numa_id
);
3493 dp_netdev_add_port_rx_to_pmds(dp
, port
, &to_reload
);
3496 HMAPX_FOR_EACH (node
, &to_reload
) {
3497 pmd
= (struct dp_netdev_pmd_thread
*) node
->data
;
3498 dp_netdev_reload_pmd__(pmd
);
3501 hmapx_destroy(&to_reload
);
3505 dpif_netdev_get_datapath_version(void)
3507 return xstrdup("<built-in>");
3511 dp_netdev_flow_used(struct dp_netdev_flow
*netdev_flow
, int cnt
, int size
,
3512 uint16_t tcp_flags
, long long now
)
3516 atomic_store_relaxed(&netdev_flow
->stats
.used
, now
);
3517 non_atomic_ullong_add(&netdev_flow
->stats
.packet_count
, cnt
);
3518 non_atomic_ullong_add(&netdev_flow
->stats
.byte_count
, size
);
3519 atomic_read_relaxed(&netdev_flow
->stats
.tcp_flags
, &flags
);
3521 atomic_store_relaxed(&netdev_flow
->stats
.tcp_flags
, flags
);
3525 dp_netdev_count_packet(struct dp_netdev_pmd_thread
*pmd
,
3526 enum dp_stat_type type
, int cnt
)
3528 non_atomic_ullong_add(&pmd
->stats
.n
[type
], cnt
);
3532 dp_netdev_upcall(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet
*packet_
,
3533 struct flow
*flow
, struct flow_wildcards
*wc
, ovs_u128
*ufid
,
3534 enum dpif_upcall_type type
, const struct nlattr
*userdata
,
3535 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
)
3537 struct dp_netdev
*dp
= pmd
->dp
;
3538 struct flow_tnl orig_tunnel
;
3541 if (OVS_UNLIKELY(!dp
->upcall_cb
)) {
3545 /* Upcall processing expects the Geneve options to be in the translated
3546 * format but we need to retain the raw format for datapath use. */
3547 orig_tunnel
.flags
= flow
->tunnel
.flags
;
3548 if (flow
->tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
3549 orig_tunnel
.metadata
.present
.len
= flow
->tunnel
.metadata
.present
.len
;
3550 memcpy(orig_tunnel
.metadata
.opts
.gnv
, flow
->tunnel
.metadata
.opts
.gnv
,
3551 flow
->tunnel
.metadata
.present
.len
);
3552 err
= tun_metadata_from_geneve_udpif(&orig_tunnel
, &orig_tunnel
,
3559 if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl
))) {
3560 struct ds ds
= DS_EMPTY_INITIALIZER
;
3563 struct odp_flow_key_parms odp_parms
= {
3566 .support
= dp_netdev_support
,
3569 ofpbuf_init(&key
, 0);
3570 odp_flow_key_from_flow(&odp_parms
, &key
);
3571 packet_str
= ofp_packet_to_string(dp_packet_data(packet_
),
3572 dp_packet_size(packet_
));
3574 odp_flow_key_format(key
.data
, key
.size
, &ds
);
3576 VLOG_DBG("%s: %s upcall:\n%s\n%s", dp
->name
,
3577 dpif_upcall_type_to_string(type
), ds_cstr(&ds
), packet_str
);
3579 ofpbuf_uninit(&key
);
3585 err
= dp
->upcall_cb(packet_
, flow
, ufid
, pmd
->core_id
, type
, userdata
,
3586 actions
, wc
, put_actions
, dp
->upcall_aux
);
3587 if (err
&& err
!= ENOSPC
) {
3591 /* Translate tunnel metadata masks to datapath format. */
3593 if (wc
->masks
.tunnel
.metadata
.present
.map
) {
3594 struct geneve_opt opts
[TLV_TOT_OPT_SIZE
/
3595 sizeof(struct geneve_opt
)];
3597 if (orig_tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
3598 tun_metadata_to_geneve_udpif_mask(&flow
->tunnel
,
3600 orig_tunnel
.metadata
.opts
.gnv
,
3601 orig_tunnel
.metadata
.present
.len
,
3604 orig_tunnel
.metadata
.present
.len
= 0;
3607 memset(&wc
->masks
.tunnel
.metadata
, 0,
3608 sizeof wc
->masks
.tunnel
.metadata
);
3609 memcpy(&wc
->masks
.tunnel
.metadata
.opts
.gnv
, opts
,
3610 orig_tunnel
.metadata
.present
.len
);
3612 wc
->masks
.tunnel
.metadata
.present
.len
= 0xff;
3615 /* Restore tunnel metadata. We need to use the saved options to ensure
3616 * that any unknown options are not lost. The generated mask will have
3617 * the same structure, matching on types and lengths but wildcarding
3618 * option data we don't care about. */
3619 if (orig_tunnel
.flags
& FLOW_TNL_F_UDPIF
) {
3620 memcpy(&flow
->tunnel
.metadata
.opts
.gnv
, orig_tunnel
.metadata
.opts
.gnv
,
3621 orig_tunnel
.metadata
.present
.len
);
3622 flow
->tunnel
.metadata
.present
.len
= orig_tunnel
.metadata
.present
.len
;
3623 flow
->tunnel
.flags
|= FLOW_TNL_F_UDPIF
;
3629 static inline uint32_t
3630 dpif_netdev_packet_get_rss_hash(struct dp_packet
*packet
,
3631 const struct miniflow
*mf
)
3633 uint32_t hash
, recirc_depth
;
3635 if (OVS_LIKELY(dp_packet_rss_valid(packet
))) {
3636 hash
= dp_packet_get_rss_hash(packet
);
3638 hash
= miniflow_hash_5tuple(mf
, 0);
3639 dp_packet_set_rss_hash(packet
, hash
);
3642 /* The RSS hash must account for the recirculation depth to avoid
3643 * collisions in the exact match cache */
3644 recirc_depth
= *recirc_depth_get_unsafe();
3645 if (OVS_UNLIKELY(recirc_depth
)) {
3646 hash
= hash_finish(hash
, recirc_depth
);
3647 dp_packet_set_rss_hash(packet
, hash
);
3652 struct packet_batch_per_flow
{
3653 unsigned int byte_count
;
3655 struct dp_netdev_flow
*flow
;
3657 struct dp_packet_batch array
;
3661 packet_batch_per_flow_update(struct packet_batch_per_flow
*batch
,
3662 struct dp_packet
*packet
,
3663 const struct miniflow
*mf
)
3665 batch
->byte_count
+= dp_packet_size(packet
);
3666 batch
->tcp_flags
|= miniflow_get_tcp_flags(mf
);
3667 batch
->array
.packets
[batch
->array
.count
++] = packet
;
3671 packet_batch_per_flow_init(struct packet_batch_per_flow
*batch
,
3672 struct dp_netdev_flow
*flow
)
3674 flow
->batch
= batch
;
3677 dp_packet_batch_init(&batch
->array
);
3678 batch
->byte_count
= 0;
3679 batch
->tcp_flags
= 0;
3683 packet_batch_per_flow_execute(struct packet_batch_per_flow
*batch
,
3684 struct dp_netdev_pmd_thread
*pmd
,
3687 struct dp_netdev_actions
*actions
;
3688 struct dp_netdev_flow
*flow
= batch
->flow
;
3690 dp_netdev_flow_used(flow
, batch
->array
.count
, batch
->byte_count
,
3691 batch
->tcp_flags
, now
);
3693 actions
= dp_netdev_flow_get_actions(flow
);
3695 dp_netdev_execute_actions(pmd
, &batch
->array
, true,
3696 actions
->actions
, actions
->size
, now
);
3700 dp_netdev_queue_batches(struct dp_packet
*pkt
,
3701 struct dp_netdev_flow
*flow
, const struct miniflow
*mf
,
3702 struct packet_batch_per_flow
*batches
, size_t *n_batches
)
3704 struct packet_batch_per_flow
*batch
= flow
->batch
;
3706 if (OVS_UNLIKELY(!batch
)) {
3707 batch
= &batches
[(*n_batches
)++];
3708 packet_batch_per_flow_init(batch
, flow
);
3711 packet_batch_per_flow_update(batch
, pkt
, mf
);
3714 /* Try to process all ('cnt') the 'packets' using only the exact match cache
3715 * 'pmd->flow_cache'. If a flow is not found for a packet 'packets[i]', the
3716 * miniflow is copied into 'keys' and the packet pointer is moved at the
3717 * beginning of the 'packets' array.
3719 * The function returns the number of packets that needs to be processed in the
3720 * 'packets' array (they have been moved to the beginning of the vector).
3722 * If 'md_is_valid' is false, the metadata in 'packets' is not valid and must be
3723 * initialized by this function using 'port_no'.
3725 static inline size_t
3726 emc_processing(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet_batch
*packets_
,
3727 struct netdev_flow_key
*keys
,
3728 struct packet_batch_per_flow batches
[], size_t *n_batches
,
3729 bool md_is_valid
, odp_port_t port_no
)
3731 struct emc_cache
*flow_cache
= &pmd
->flow_cache
;
3732 struct netdev_flow_key
*key
= &keys
[0];
3733 size_t i
, n_missed
= 0, n_dropped
= 0;
3734 struct dp_packet
**packets
= packets_
->packets
;
3735 int cnt
= packets_
->count
;
3737 for (i
= 0; i
< cnt
; i
++) {
3738 struct dp_netdev_flow
*flow
;
3739 struct dp_packet
*packet
= packets
[i
];
3741 if (OVS_UNLIKELY(dp_packet_size(packet
) < ETH_HEADER_LEN
)) {
3742 dp_packet_delete(packet
);
3748 /* Prefetch next packet data and metadata. */
3749 OVS_PREFETCH(dp_packet_data(packets
[i
+1]));
3750 pkt_metadata_prefetch_init(&packets
[i
+1]->md
);
3754 pkt_metadata_init(&packet
->md
, port_no
);
3756 miniflow_extract(packet
, &key
->mf
);
3757 key
->len
= 0; /* Not computed yet. */
3758 key
->hash
= dpif_netdev_packet_get_rss_hash(packet
, &key
->mf
);
3760 flow
= emc_lookup(flow_cache
, key
);
3761 if (OVS_LIKELY(flow
)) {
3762 dp_netdev_queue_batches(packet
, flow
, &key
->mf
, batches
,
3765 /* Exact match cache missed. Group missed packets together at
3766 * the beginning of the 'packets' array. */
3767 packets
[n_missed
] = packet
;
3768 /* 'key[n_missed]' contains the key of the current packet and it
3769 * must be returned to the caller. The next key should be extracted
3770 * to 'keys[n_missed + 1]'. */
3771 key
= &keys
[++n_missed
];
3775 dp_netdev_count_packet(pmd
, DP_STAT_EXACT_HIT
, cnt
- n_dropped
- n_missed
);
3781 handle_packet_upcall(struct dp_netdev_pmd_thread
*pmd
, struct dp_packet
*packet
,
3782 const struct netdev_flow_key
*key
,
3783 struct ofpbuf
*actions
, struct ofpbuf
*put_actions
,
3784 int *lost_cnt
, long long now
)
3786 struct ofpbuf
*add_actions
;
3787 struct dp_packet_batch b
;
3792 match
.tun_md
.valid
= false;
3793 miniflow_expand(&key
->mf
, &match
.flow
);
3795 ofpbuf_clear(actions
);
3796 ofpbuf_clear(put_actions
);
3798 dpif_flow_hash(pmd
->dp
->dpif
, &match
.flow
, sizeof match
.flow
, &ufid
);
3799 error
= dp_netdev_upcall(pmd
, packet
, &match
.flow
, &match
.wc
,
3800 &ufid
, DPIF_UC_MISS
, NULL
, actions
,
3802 if (OVS_UNLIKELY(error
&& error
!= ENOSPC
)) {
3803 dp_packet_delete(packet
);
3808 /* The Netlink encoding of datapath flow keys cannot express
3809 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
3810 * tag is interpreted as exact match on the fact that there is no
3811 * VLAN. Unless we refactor a lot of code that translates between
3812 * Netlink and struct flow representations, we have to do the same
3814 if (!match
.wc
.masks
.vlan_tci
) {
3815 match
.wc
.masks
.vlan_tci
= htons(0xffff);
3818 /* We can't allow the packet batching in the next loop to execute
3819 * the actions. Otherwise, if there are any slow path actions,
3820 * we'll send the packet up twice. */
3821 packet_batch_init_packet(&b
, packet
);
3822 dp_netdev_execute_actions(pmd
, &b
, true,
3823 actions
->data
, actions
->size
, now
);
3825 add_actions
= put_actions
->size
? put_actions
: actions
;
3826 if (OVS_LIKELY(error
!= ENOSPC
)) {
3827 struct dp_netdev_flow
*netdev_flow
;
3829 /* XXX: There's a race window where a flow covering this packet
3830 * could have already been installed since we last did the flow
3831 * lookup before upcall. This could be solved by moving the
3832 * mutex lock outside the loop, but that's an awful long time
3833 * to be locking everyone out of making flow installs. If we
3834 * move to a per-core classifier, it would be reasonable. */
3835 ovs_mutex_lock(&pmd
->flow_mutex
);
3836 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, key
);
3837 if (OVS_LIKELY(!netdev_flow
)) {
3838 netdev_flow
= dp_netdev_flow_add(pmd
, &match
, &ufid
,
3842 ovs_mutex_unlock(&pmd
->flow_mutex
);
3844 emc_insert(&pmd
->flow_cache
, key
, netdev_flow
);
3849 fast_path_processing(struct dp_netdev_pmd_thread
*pmd
,
3850 struct dp_packet_batch
*packets_
,
3851 struct netdev_flow_key
*keys
,
3852 struct packet_batch_per_flow batches
[], size_t *n_batches
,
3855 int cnt
= packets_
->count
;
3856 #if !defined(__CHECKER__) && !defined(_WIN32)
3857 const size_t PKT_ARRAY_SIZE
= cnt
;
3859 /* Sparse or MSVC doesn't like variable length array. */
3860 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
3862 struct dp_packet
**packets
= packets_
->packets
;
3863 struct dpcls_rule
*rules
[PKT_ARRAY_SIZE
];
3864 struct dp_netdev
*dp
= pmd
->dp
;
3865 struct emc_cache
*flow_cache
= &pmd
->flow_cache
;
3866 int miss_cnt
= 0, lost_cnt
= 0;
3870 for (i
= 0; i
< cnt
; i
++) {
3871 /* Key length is needed in all the cases, hash computed on demand. */
3872 keys
[i
].len
= netdev_flow_key_size(miniflow_n_values(&keys
[i
].mf
));
3874 any_miss
= !dpcls_lookup(&pmd
->cls
, keys
, rules
, cnt
);
3875 if (OVS_UNLIKELY(any_miss
) && !fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
3876 uint64_t actions_stub
[512 / 8], slow_stub
[512 / 8];
3877 struct ofpbuf actions
, put_actions
;
3879 ofpbuf_use_stub(&actions
, actions_stub
, sizeof actions_stub
);
3880 ofpbuf_use_stub(&put_actions
, slow_stub
, sizeof slow_stub
);
3882 for (i
= 0; i
< cnt
; i
++) {
3883 struct dp_netdev_flow
*netdev_flow
;
3885 if (OVS_LIKELY(rules
[i
])) {
3889 /* It's possible that an earlier slow path execution installed
3890 * a rule covering this flow. In this case, it's a lot cheaper
3891 * to catch it here than execute a miss. */
3892 netdev_flow
= dp_netdev_pmd_lookup_flow(pmd
, &keys
[i
]);
3894 rules
[i
] = &netdev_flow
->cr
;
3899 handle_packet_upcall(pmd
, packets
[i
], &keys
[i
], &actions
,
3900 &put_actions
, &lost_cnt
, now
);
3903 ofpbuf_uninit(&actions
);
3904 ofpbuf_uninit(&put_actions
);
3905 fat_rwlock_unlock(&dp
->upcall_rwlock
);
3906 dp_netdev_count_packet(pmd
, DP_STAT_LOST
, lost_cnt
);
3907 } else if (OVS_UNLIKELY(any_miss
)) {
3908 for (i
= 0; i
< cnt
; i
++) {
3909 if (OVS_UNLIKELY(!rules
[i
])) {
3910 dp_packet_delete(packets
[i
]);
3917 for (i
= 0; i
< cnt
; i
++) {
3918 struct dp_packet
*packet
= packets
[i
];
3919 struct dp_netdev_flow
*flow
;
3921 if (OVS_UNLIKELY(!rules
[i
])) {
3925 flow
= dp_netdev_flow_cast(rules
[i
]);
3927 emc_insert(flow_cache
, &keys
[i
], flow
);
3928 dp_netdev_queue_batches(packet
, flow
, &keys
[i
].mf
, batches
, n_batches
);
3931 dp_netdev_count_packet(pmd
, DP_STAT_MASKED_HIT
, cnt
- miss_cnt
);
3932 dp_netdev_count_packet(pmd
, DP_STAT_MISS
, miss_cnt
);
3933 dp_netdev_count_packet(pmd
, DP_STAT_LOST
, lost_cnt
);
3936 /* Packets enter the datapath from a port (or from recirculation) here.
3938 * For performance reasons a caller may choose not to initialize the metadata
3939 * in 'packets': in this case 'mdinit' is false and this function needs to
3940 * initialize it using 'port_no'. If the metadata in 'packets' is already
3941 * valid, 'md_is_valid' must be true and 'port_no' will be ignored. */
3943 dp_netdev_input__(struct dp_netdev_pmd_thread
*pmd
,
3944 struct dp_packet_batch
*packets
,
3945 bool md_is_valid
, odp_port_t port_no
)
3947 int cnt
= packets
->count
;
3948 #if !defined(__CHECKER__) && !defined(_WIN32)
3949 const size_t PKT_ARRAY_SIZE
= cnt
;
3951 /* Sparse or MSVC doesn't like variable length array. */
3952 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
3954 struct netdev_flow_key keys
[PKT_ARRAY_SIZE
];
3955 struct packet_batch_per_flow batches
[PKT_ARRAY_SIZE
];
3956 long long now
= time_msec();
3957 size_t newcnt
, n_batches
, i
;
3960 newcnt
= emc_processing(pmd
, packets
, keys
, batches
, &n_batches
,
3961 md_is_valid
, port_no
);
3962 if (OVS_UNLIKELY(newcnt
)) {
3963 packets
->count
= newcnt
;
3964 fast_path_processing(pmd
, packets
, keys
, batches
, &n_batches
, now
);
3967 for (i
= 0; i
< n_batches
; i
++) {
3968 batches
[i
].flow
->batch
= NULL
;
3971 for (i
= 0; i
< n_batches
; i
++) {
3972 packet_batch_per_flow_execute(&batches
[i
], pmd
, now
);
3977 dp_netdev_input(struct dp_netdev_pmd_thread
*pmd
,
3978 struct dp_packet_batch
*packets
,
3981 dp_netdev_input__(pmd
, packets
, false, port_no
);
3985 dp_netdev_recirculate(struct dp_netdev_pmd_thread
*pmd
,
3986 struct dp_packet_batch
*packets
)
3988 dp_netdev_input__(pmd
, packets
, true, 0);
3991 struct dp_netdev_execute_aux
{
3992 struct dp_netdev_pmd_thread
*pmd
;
3997 dpif_netdev_register_dp_purge_cb(struct dpif
*dpif
, dp_purge_callback
*cb
,
4000 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4001 dp
->dp_purge_aux
= aux
;
4002 dp
->dp_purge_cb
= cb
;
4006 dpif_netdev_register_upcall_cb(struct dpif
*dpif
, upcall_callback
*cb
,
4009 struct dp_netdev
*dp
= get_dp_netdev(dpif
);
4010 dp
->upcall_aux
= aux
;
4015 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread
*pmd
,
4016 long long now
, bool purge
)
4019 struct dp_netdev_port
*port
;
4022 HMAP_FOR_EACH (tx
, node
, &pmd
->port_cache
) {
4023 if (tx
->port
->dynamic_txqs
) {
4026 interval
= now
- tx
->last_used
;
4027 if (tx
->qid
>= 0 && (purge
|| interval
>= XPS_TIMEOUT_MS
)) {
4029 ovs_mutex_lock(&port
->txq_used_mutex
);
4030 port
->txq_used
[tx
->qid
]--;
4031 ovs_mutex_unlock(&port
->txq_used_mutex
);
4038 dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread
*pmd
,
4039 struct tx_port
*tx
, long long now
)
4041 struct dp_netdev_port
*port
;
4043 int i
, min_cnt
, min_qid
;
4045 if (OVS_UNLIKELY(!now
)) {
4049 interval
= now
- tx
->last_used
;
4050 tx
->last_used
= now
;
4052 if (OVS_LIKELY(tx
->qid
>= 0 && interval
< XPS_TIMEOUT_MS
)) {
4058 ovs_mutex_lock(&port
->txq_used_mutex
);
4060 port
->txq_used
[tx
->qid
]--;
4066 for (i
= 0; i
< netdev_n_txq(port
->netdev
); i
++) {
4067 if (port
->txq_used
[i
] < min_cnt
|| min_cnt
== -1) {
4068 min_cnt
= port
->txq_used
[i
];
4073 port
->txq_used
[min_qid
]++;
4076 ovs_mutex_unlock(&port
->txq_used_mutex
);
4078 dpif_netdev_xps_revalidate_pmd(pmd
, now
, false);
4080 VLOG_DBG("Core %d: New TX queue ID %d for port \'%s\'.",
4081 pmd
->core_id
, tx
->qid
, netdev_get_name(tx
->port
->netdev
));
4085 static struct tx_port
*
4086 pmd_tx_port_cache_lookup(const struct dp_netdev_pmd_thread
*pmd
,
4089 return tx_port_lookup(&pmd
->port_cache
, port_no
);
4093 push_tnl_action(const struct dp_netdev_pmd_thread
*pmd
,
4094 const struct nlattr
*attr
,
4095 struct dp_packet_batch
*batch
)
4097 struct tx_port
*tun_port
;
4098 const struct ovs_action_push_tnl
*data
;
4101 data
= nl_attr_get(attr
);
4103 tun_port
= pmd_tx_port_cache_lookup(pmd
, u32_to_odp(data
->tnl_port
));
4108 err
= netdev_push_header(tun_port
->port
->netdev
, batch
, data
);
4113 dp_packet_delete_batch(batch
, true);
4118 dp_execute_userspace_action(struct dp_netdev_pmd_thread
*pmd
,
4119 struct dp_packet
*packet
, bool may_steal
,
4120 struct flow
*flow
, ovs_u128
*ufid
,
4121 struct ofpbuf
*actions
,
4122 const struct nlattr
*userdata
, long long now
)
4124 struct dp_packet_batch b
;
4127 ofpbuf_clear(actions
);
4129 error
= dp_netdev_upcall(pmd
, packet
, flow
, NULL
, ufid
,
4130 DPIF_UC_ACTION
, userdata
, actions
,
4132 if (!error
|| error
== ENOSPC
) {
4133 packet_batch_init_packet(&b
, packet
);
4134 dp_netdev_execute_actions(pmd
, &b
, may_steal
,
4135 actions
->data
, actions
->size
, now
);
4136 } else if (may_steal
) {
4137 dp_packet_delete(packet
);
4142 dp_execute_cb(void *aux_
, struct dp_packet_batch
*packets_
,
4143 const struct nlattr
*a
, bool may_steal
)
4145 struct dp_netdev_execute_aux
*aux
= aux_
;
4146 uint32_t *depth
= recirc_depth_get();
4147 struct dp_netdev_pmd_thread
*pmd
= aux
->pmd
;
4148 struct dp_netdev
*dp
= pmd
->dp
;
4149 int type
= nl_attr_type(a
);
4150 long long now
= aux
->now
;
4153 switch ((enum ovs_action_attr
)type
) {
4154 case OVS_ACTION_ATTR_OUTPUT
:
4155 p
= pmd_tx_port_cache_lookup(pmd
, u32_to_odp(nl_attr_get_u32(a
)));
4156 if (OVS_LIKELY(p
)) {
4160 dynamic_txqs
= p
->port
->dynamic_txqs
;
4162 tx_qid
= dpif_netdev_xps_get_tx_qid(pmd
, p
, now
);
4164 atomic_read_relaxed(&pmd
->static_tx_qid
, &tx_qid
);
4167 netdev_send(p
->port
->netdev
, tx_qid
, packets_
, may_steal
,
4173 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
4174 if (*depth
< MAX_RECIRC_DEPTH
) {
4175 struct dp_packet_batch tnl_pkt
;
4176 struct dp_packet_batch
*orig_packets_
= packets_
;
4180 dp_packet_batch_clone(&tnl_pkt
, packets_
);
4181 packets_
= &tnl_pkt
;
4182 dp_packet_batch_reset_cutlen(orig_packets_
);
4185 dp_packet_batch_apply_cutlen(packets_
);
4187 err
= push_tnl_action(pmd
, a
, packets_
);
4190 dp_netdev_recirculate(pmd
, packets_
);
4197 case OVS_ACTION_ATTR_TUNNEL_POP
:
4198 if (*depth
< MAX_RECIRC_DEPTH
) {
4199 struct dp_packet_batch
*orig_packets_
= packets_
;
4200 odp_port_t portno
= u32_to_odp(nl_attr_get_u32(a
));
4202 p
= pmd_tx_port_cache_lookup(pmd
, portno
);
4204 struct dp_packet_batch tnl_pkt
;
4208 dp_packet_batch_clone(&tnl_pkt
, packets_
);
4209 packets_
= &tnl_pkt
;
4210 dp_packet_batch_reset_cutlen(orig_packets_
);
4213 dp_packet_batch_apply_cutlen(packets_
);
4215 netdev_pop_header(p
->port
->netdev
, packets_
);
4216 if (!packets_
->count
) {
4220 for (i
= 0; i
< packets_
->count
; i
++) {
4221 packets_
->packets
[i
]->md
.in_port
.odp_port
= portno
;
4225 dp_netdev_recirculate(pmd
, packets_
);
4232 case OVS_ACTION_ATTR_USERSPACE
:
4233 if (!fat_rwlock_tryrdlock(&dp
->upcall_rwlock
)) {
4234 struct dp_packet_batch
*orig_packets_
= packets_
;
4235 struct dp_packet
**packets
= packets_
->packets
;
4236 const struct nlattr
*userdata
;
4237 struct dp_packet_batch usr_pkt
;
4238 struct ofpbuf actions
;
4244 userdata
= nl_attr_find_nested(a
, OVS_USERSPACE_ATTR_USERDATA
);
4245 ofpbuf_init(&actions
, 0);
4247 if (packets_
->trunc
) {
4249 dp_packet_batch_clone(&usr_pkt
, packets_
);
4250 packets_
= &usr_pkt
;
4251 packets
= packets_
->packets
;
4253 dp_packet_batch_reset_cutlen(orig_packets_
);
4256 dp_packet_batch_apply_cutlen(packets_
);
4259 for (i
= 0; i
< packets_
->count
; i
++) {
4260 flow_extract(packets
[i
], &flow
);
4261 dpif_flow_hash(dp
->dpif
, &flow
, sizeof flow
, &ufid
);
4262 dp_execute_userspace_action(pmd
, packets
[i
], may_steal
, &flow
,
4263 &ufid
, &actions
, userdata
, now
);
4267 dp_packet_delete_batch(packets_
, true);
4270 ofpbuf_uninit(&actions
);
4271 fat_rwlock_unlock(&dp
->upcall_rwlock
);
4277 case OVS_ACTION_ATTR_RECIRC
:
4278 if (*depth
< MAX_RECIRC_DEPTH
) {
4279 struct dp_packet_batch recirc_pkts
;
4283 dp_packet_batch_clone(&recirc_pkts
, packets_
);
4284 packets_
= &recirc_pkts
;
4287 for (i
= 0; i
< packets_
->count
; i
++) {
4288 packets_
->packets
[i
]->md
.recirc_id
= nl_attr_get_u32(a
);
4292 dp_netdev_recirculate(pmd
, packets_
);
4298 VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
4301 case OVS_ACTION_ATTR_CT
:
4302 /* If a flow with this action is slow-pathed, datapath assistance is
4303 * required to implement it. However, we don't support this action
4304 * in the userspace datapath. */
4305 VLOG_WARN("Cannot execute conntrack action in userspace.");
4308 case OVS_ACTION_ATTR_PUSH_VLAN
:
4309 case OVS_ACTION_ATTR_POP_VLAN
:
4310 case OVS_ACTION_ATTR_PUSH_MPLS
:
4311 case OVS_ACTION_ATTR_POP_MPLS
:
4312 case OVS_ACTION_ATTR_SET
:
4313 case OVS_ACTION_ATTR_SET_MASKED
:
4314 case OVS_ACTION_ATTR_SAMPLE
:
4315 case OVS_ACTION_ATTR_HASH
:
4316 case OVS_ACTION_ATTR_UNSPEC
:
4317 case OVS_ACTION_ATTR_TRUNC
:
4318 case __OVS_ACTION_ATTR_MAX
:
4322 dp_packet_delete_batch(packets_
, may_steal
);
4326 dp_netdev_execute_actions(struct dp_netdev_pmd_thread
*pmd
,
4327 struct dp_packet_batch
*packets
,
4329 const struct nlattr
*actions
, size_t actions_len
,
4332 struct dp_netdev_execute_aux aux
= { pmd
, now
};
4334 odp_execute_actions(&aux
, packets
, may_steal
, actions
,
4335 actions_len
, dp_execute_cb
);
4338 const struct dpif_class dpif_netdev_class
= {
4341 dpif_netdev_enumerate
,
4342 dpif_netdev_port_open_type
,
4345 dpif_netdev_destroy
,
4348 dpif_netdev_get_stats
,
4349 dpif_netdev_port_add
,
4350 dpif_netdev_port_del
,
4351 dpif_netdev_port_query_by_number
,
4352 dpif_netdev_port_query_by_name
,
4353 NULL
, /* port_get_pid */
4354 dpif_netdev_port_dump_start
,
4355 dpif_netdev_port_dump_next
,
4356 dpif_netdev_port_dump_done
,
4357 dpif_netdev_port_poll
,
4358 dpif_netdev_port_poll_wait
,
4359 dpif_netdev_flow_flush
,
4360 dpif_netdev_flow_dump_create
,
4361 dpif_netdev_flow_dump_destroy
,
4362 dpif_netdev_flow_dump_thread_create
,
4363 dpif_netdev_flow_dump_thread_destroy
,
4364 dpif_netdev_flow_dump_next
,
4365 dpif_netdev_operate
,
4366 NULL
, /* recv_set */
4367 NULL
, /* handlers_set */
4368 dpif_netdev_pmd_set
,
4369 dpif_netdev_queue_to_priority
,
4371 NULL
, /* recv_wait */
4372 NULL
, /* recv_purge */
4373 dpif_netdev_register_dp_purge_cb
,
4374 dpif_netdev_register_upcall_cb
,
4375 dpif_netdev_enable_upcall
,
4376 dpif_netdev_disable_upcall
,
4377 dpif_netdev_get_datapath_version
,
4378 NULL
, /* ct_dump_start */
4379 NULL
, /* ct_dump_next */
4380 NULL
, /* ct_dump_done */
4381 NULL
, /* ct_flush */
4385 dpif_dummy_change_port_number(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
4386 const char *argv
[], void *aux OVS_UNUSED
)
4388 struct dp_netdev_port
*port
;
4389 struct dp_netdev
*dp
;
4392 ovs_mutex_lock(&dp_netdev_mutex
);
4393 dp
= shash_find_data(&dp_netdevs
, argv
[1]);
4394 if (!dp
|| !dpif_netdev_class_is_dummy(dp
->class)) {
4395 ovs_mutex_unlock(&dp_netdev_mutex
);
4396 unixctl_command_reply_error(conn
, "unknown datapath or not a dummy");
4399 ovs_refcount_ref(&dp
->ref_cnt
);
4400 ovs_mutex_unlock(&dp_netdev_mutex
);
4402 ovs_mutex_lock(&dp
->port_mutex
);
4403 if (get_port_by_name(dp
, argv
[2], &port
)) {
4404 unixctl_command_reply_error(conn
, "unknown port");
4408 port_no
= u32_to_odp(atoi(argv
[3]));
4409 if (!port_no
|| port_no
== ODPP_NONE
) {
4410 unixctl_command_reply_error(conn
, "bad port number");
4413 if (dp_netdev_lookup_port(dp
, port_no
)) {
4414 unixctl_command_reply_error(conn
, "port number already in use");
4419 hmap_remove(&dp
->ports
, &port
->node
);
4420 dp_netdev_del_port_from_all_pmds(dp
, port
);
4422 /* Reinsert with new port number. */
4423 port
->port_no
= port_no
;
4424 hmap_insert(&dp
->ports
, &port
->node
, hash_port_no(port_no
));
4425 dp_netdev_add_port_to_pmds(dp
, port
);
4427 seq_change(dp
->port_seq
);
4428 unixctl_command_reply(conn
, NULL
);
4431 ovs_mutex_unlock(&dp
->port_mutex
);
4432 dp_netdev_unref(dp
);
4436 dpif_dummy_register__(const char *type
)
4438 struct dpif_class
*class;
4440 class = xmalloc(sizeof *class);
4441 *class = dpif_netdev_class
;
4442 class->type
= xstrdup(type
);
4443 dp_register_provider(class);
4447 dpif_dummy_override(const char *type
)
4452 * Ignore EAFNOSUPPORT to allow --enable-dummy=system with
4453 * a userland-only build. It's useful for testsuite.
4455 error
= dp_unregister_provider(type
);
4456 if (error
== 0 || error
== EAFNOSUPPORT
) {
4457 dpif_dummy_register__(type
);
4462 dpif_dummy_register(enum dummy_level level
)
4464 if (level
== DUMMY_OVERRIDE_ALL
) {
4469 dp_enumerate_types(&types
);
4470 SSET_FOR_EACH (type
, &types
) {
4471 dpif_dummy_override(type
);
4473 sset_destroy(&types
);
4474 } else if (level
== DUMMY_OVERRIDE_SYSTEM
) {
4475 dpif_dummy_override("system");
4478 dpif_dummy_register__("dummy");
4480 unixctl_command_register("dpif-dummy/change-port-number",
4481 "dp port new-number",
4482 3, 3, dpif_dummy_change_port_number
, NULL
);
4485 /* Datapath Classifier. */
4487 /* A set of rules that all have the same fields wildcarded. */
4488 struct dpcls_subtable
{
4489 /* The fields are only used by writers. */
4490 struct cmap_node cmap_node OVS_GUARDED
; /* Within dpcls 'subtables_map'. */
4492 /* These fields are accessed by readers. */
4493 struct cmap rules
; /* Contains "struct dpcls_rule"s. */
4494 struct netdev_flow_key mask
; /* Wildcards for fields (const). */
4495 /* 'mask' must be the last field, additional space is allocated here. */
4498 /* Initializes 'cls' as a classifier that initially contains no classification
4501 dpcls_init(struct dpcls
*cls
)
4503 cmap_init(&cls
->subtables_map
);
4504 pvector_init(&cls
->subtables
);
4508 dpcls_destroy_subtable(struct dpcls
*cls
, struct dpcls_subtable
*subtable
)
4510 pvector_remove(&cls
->subtables
, subtable
);
4511 cmap_remove(&cls
->subtables_map
, &subtable
->cmap_node
,
4512 subtable
->mask
.hash
);
4513 cmap_destroy(&subtable
->rules
);
4514 ovsrcu_postpone(free
, subtable
);
4517 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
4518 * caller's responsibility.
4519 * May only be called after all the readers have been terminated. */
4521 dpcls_destroy(struct dpcls
*cls
)
4524 struct dpcls_subtable
*subtable
;
4526 CMAP_FOR_EACH (subtable
, cmap_node
, &cls
->subtables_map
) {
4527 ovs_assert(cmap_count(&subtable
->rules
) == 0);
4528 dpcls_destroy_subtable(cls
, subtable
);
4530 cmap_destroy(&cls
->subtables_map
);
4531 pvector_destroy(&cls
->subtables
);
4535 static struct dpcls_subtable
*
4536 dpcls_create_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
4538 struct dpcls_subtable
*subtable
;
4540 /* Need to add one. */
4541 subtable
= xmalloc(sizeof *subtable
4542 - sizeof subtable
->mask
.mf
+ mask
->len
);
4543 cmap_init(&subtable
->rules
);
4544 netdev_flow_key_clone(&subtable
->mask
, mask
);
4545 cmap_insert(&cls
->subtables_map
, &subtable
->cmap_node
, mask
->hash
);
4546 pvector_insert(&cls
->subtables
, subtable
, 0);
4547 pvector_publish(&cls
->subtables
);
4552 static inline struct dpcls_subtable
*
4553 dpcls_find_subtable(struct dpcls
*cls
, const struct netdev_flow_key
*mask
)
4555 struct dpcls_subtable
*subtable
;
4557 CMAP_FOR_EACH_WITH_HASH (subtable
, cmap_node
, mask
->hash
,
4558 &cls
->subtables_map
) {
4559 if (netdev_flow_key_equal(&subtable
->mask
, mask
)) {
4563 return dpcls_create_subtable(cls
, mask
);
4566 /* Insert 'rule' into 'cls'. */
4568 dpcls_insert(struct dpcls
*cls
, struct dpcls_rule
*rule
,
4569 const struct netdev_flow_key
*mask
)
4571 struct dpcls_subtable
*subtable
= dpcls_find_subtable(cls
, mask
);
4573 rule
->mask
= &subtable
->mask
;
4574 cmap_insert(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
);
4577 /* Removes 'rule' from 'cls', also destructing the 'rule'. */
4579 dpcls_remove(struct dpcls
*cls
, struct dpcls_rule
*rule
)
4581 struct dpcls_subtable
*subtable
;
4583 ovs_assert(rule
->mask
);
4585 INIT_CONTAINER(subtable
, rule
->mask
, mask
);
4587 if (cmap_remove(&subtable
->rules
, &rule
->cmap_node
, rule
->flow
.hash
)
4589 dpcls_destroy_subtable(cls
, subtable
);
4590 pvector_publish(&cls
->subtables
);
4594 /* Returns true if 'target' satisfies 'key' in 'mask', that is, if each 1-bit
4595 * in 'mask' the values in 'key' and 'target' are the same. */
4597 dpcls_rule_matches_key(const struct dpcls_rule
*rule
,
4598 const struct netdev_flow_key
*target
)
4600 const uint64_t *keyp
= miniflow_get_values(&rule
->flow
.mf
);
4601 const uint64_t *maskp
= miniflow_get_values(&rule
->mask
->mf
);
4604 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value
, target
, rule
->flow
.mf
.map
) {
4605 if (OVS_UNLIKELY((value
& *maskp
++) != *keyp
++)) {
4612 /* For each miniflow in 'flows' performs a classifier lookup writing the result
4613 * into the corresponding slot in 'rules'. If a particular entry in 'flows' is
4614 * NULL it is skipped.
4616 * This function is optimized for use in the userspace datapath and therefore
4617 * does not implement a lot of features available in the standard
4618 * classifier_lookup() function. Specifically, it does not implement
4619 * priorities, instead returning any rule which matches the flow.
4621 * Returns true if all flows found a corresponding rule. */
4623 dpcls_lookup(const struct dpcls
*cls
, const struct netdev_flow_key keys
[],
4624 struct dpcls_rule
**rules
, const size_t cnt
)
4626 /* The batch size 16 was experimentally found faster than 8 or 32. */
4627 typedef uint16_t map_type
;
4628 #define MAP_BITS (sizeof(map_type) * CHAR_BIT)
4630 #if !defined(__CHECKER__) && !defined(_WIN32)
4631 const int N_MAPS
= DIV_ROUND_UP(cnt
, MAP_BITS
);
4633 enum { N_MAPS
= DIV_ROUND_UP(NETDEV_MAX_BURST
, MAP_BITS
) };
4635 map_type maps
[N_MAPS
];
4636 struct dpcls_subtable
*subtable
;
4638 memset(maps
, 0xff, sizeof maps
);
4639 if (cnt
% MAP_BITS
) {
4640 maps
[N_MAPS
- 1] >>= MAP_BITS
- cnt
% MAP_BITS
; /* Clear extra bits. */
4642 memset(rules
, 0, cnt
* sizeof *rules
);
4644 PVECTOR_FOR_EACH (subtable
, &cls
->subtables
) {
4645 const struct netdev_flow_key
*mkeys
= keys
;
4646 struct dpcls_rule
**mrules
= rules
;
4647 map_type remains
= 0;
4650 BUILD_ASSERT_DECL(sizeof remains
== sizeof *maps
);
4652 for (m
= 0; m
< N_MAPS
; m
++, mkeys
+= MAP_BITS
, mrules
+= MAP_BITS
) {
4653 uint32_t hashes
[MAP_BITS
];
4654 const struct cmap_node
*nodes
[MAP_BITS
];
4655 unsigned long map
= maps
[m
];
4659 continue; /* Skip empty maps. */
4662 /* Compute hashes for the remaining keys. */
4663 ULLONG_FOR_EACH_1(i
, map
) {
4664 hashes
[i
] = netdev_flow_key_hash_in_mask(&mkeys
[i
],
4668 map
= cmap_find_batch(&subtable
->rules
, map
, hashes
, nodes
);
4669 /* Check results. */
4670 ULLONG_FOR_EACH_1(i
, map
) {
4671 struct dpcls_rule
*rule
;
4673 CMAP_NODE_FOR_EACH (rule
, cmap_node
, nodes
[i
]) {
4674 if (OVS_LIKELY(dpcls_rule_matches_key(rule
, &mkeys
[i
]))) {
4679 ULLONG_SET0(map
, i
); /* Did not match. */
4681 ; /* Keep Sparse happy. */
4683 maps
[m
] &= ~map
; /* Clear the found rules. */
4687 return true; /* All found. */
4690 return false; /* Some misses. */