]> git.proxmox.com Git - mirror_ovs.git/blob - lib/dpif-netdev.c
dpif-netdev: Trigger parallel pmd reloads.
[mirror_ovs.git] / lib / dpif-netdev.c
1 /*
2 * Copyright (c) 2009-2014, 2016-2018 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18 #include "dpif-netdev.h"
19
20 #include <ctype.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <inttypes.h>
24 #include <net/if.h>
25 #include <sys/types.h>
26 #include <netinet/in.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34
35 #include "bitmap.h"
36 #include "cmap.h"
37 #include "conntrack.h"
38 #include "coverage.h"
39 #include "ct-dpif.h"
40 #include "csum.h"
41 #include "dp-packet.h"
42 #include "dpif.h"
43 #include "dpif-netdev-perf.h"
44 #include "dpif-provider.h"
45 #include "dummy.h"
46 #include "fat-rwlock.h"
47 #include "flow.h"
48 #include "hmapx.h"
49 #include "id-pool.h"
50 #include "ipf.h"
51 #include "netdev.h"
52 #include "netdev-offload.h"
53 #include "netdev-provider.h"
54 #include "netdev-vport.h"
55 #include "netlink.h"
56 #include "odp-execute.h"
57 #include "odp-util.h"
58 #include "openvswitch/dynamic-string.h"
59 #include "openvswitch/list.h"
60 #include "openvswitch/match.h"
61 #include "openvswitch/ofp-parse.h"
62 #include "openvswitch/ofp-print.h"
63 #include "openvswitch/ofpbuf.h"
64 #include "openvswitch/shash.h"
65 #include "openvswitch/vlog.h"
66 #include "ovs-numa.h"
67 #include "ovs-rcu.h"
68 #include "packets.h"
69 #include "openvswitch/poll-loop.h"
70 #include "pvector.h"
71 #include "random.h"
72 #include "seq.h"
73 #include "smap.h"
74 #include "sset.h"
75 #include "timeval.h"
76 #include "tnl-neigh-cache.h"
77 #include "tnl-ports.h"
78 #include "unixctl.h"
79 #include "util.h"
80 #include "uuid.h"
81
82 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
83
84 /* Auto Load Balancing Defaults */
85 #define ALB_ACCEPTABLE_IMPROVEMENT 25
86 #define ALB_PMD_LOAD_THRESHOLD 95
87 #define ALB_PMD_REBALANCE_POLL_INTERVAL 1 /* 1 Min */
88 #define MIN_TO_MSEC 60000
89
90 #define FLOW_DUMP_MAX_BATCH 50
91 /* Use per thread recirc_depth to prevent recirculation loop. */
92 #define MAX_RECIRC_DEPTH 6
93 DEFINE_STATIC_PER_THREAD_DATA(uint32_t, recirc_depth, 0)
94
95 /* Use instant packet send by default. */
96 #define DEFAULT_TX_FLUSH_INTERVAL 0
97
98 /* Configuration parameters. */
99 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
100 enum { MAX_METERS = 65536 }; /* Maximum number of meters. */
101 enum { MAX_BANDS = 8 }; /* Maximum number of bands / meter. */
102 enum { N_METER_LOCKS = 64 }; /* Maximum number of meters. */
103
104 /* Protects against changes to 'dp_netdevs'. */
105 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
106
107 /* Contains all 'struct dp_netdev's. */
108 static struct shash dp_netdevs OVS_GUARDED_BY(dp_netdev_mutex)
109 = SHASH_INITIALIZER(&dp_netdevs);
110
111 static struct vlog_rate_limit upcall_rl = VLOG_RATE_LIMIT_INIT(600, 600);
112
113 #define DP_NETDEV_CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \
114 | CS_INVALID | CS_REPLY_DIR | CS_TRACKED \
115 | CS_SRC_NAT | CS_DST_NAT)
116 #define DP_NETDEV_CS_UNSUPPORTED_MASK (~(uint32_t)DP_NETDEV_CS_SUPPORTED_MASK)
117
118 static struct odp_support dp_netdev_support = {
119 .max_vlan_headers = SIZE_MAX,
120 .max_mpls_depth = SIZE_MAX,
121 .recirc = true,
122 .ct_state = true,
123 .ct_zone = true,
124 .ct_mark = true,
125 .ct_label = true,
126 .ct_state_nat = true,
127 .ct_orig_tuple = true,
128 .ct_orig_tuple6 = true,
129 };
130
131 /* Stores a miniflow with inline values */
132
133 struct netdev_flow_key {
134 uint32_t hash; /* Hash function differs for different users. */
135 uint32_t len; /* Length of the following miniflow (incl. map). */
136 struct miniflow mf;
137 uint64_t buf[FLOW_MAX_PACKET_U64S];
138 };
139
140 /* EMC cache and SMC cache compose the datapath flow cache (DFC)
141 *
142 * Exact match cache for frequently used flows
143 *
144 * The cache uses a 32-bit hash of the packet (which can be the RSS hash) to
145 * search its entries for a miniflow that matches exactly the miniflow of the
146 * packet. It stores the 'dpcls_rule' (rule) that matches the miniflow.
147 *
148 * A cache entry holds a reference to its 'dp_netdev_flow'.
149 *
150 * A miniflow with a given hash can be in one of EM_FLOW_HASH_SEGS different
151 * entries. The 32-bit hash is split into EM_FLOW_HASH_SEGS values (each of
152 * them is EM_FLOW_HASH_SHIFT bits wide and the remainder is thrown away). Each
153 * value is the index of a cache entry where the miniflow could be.
154 *
155 *
156 * Signature match cache (SMC)
157 *
158 * This cache stores a 16-bit signature for each flow without storing keys, and
159 * stores the corresponding 16-bit flow_table index to the 'dp_netdev_flow'.
160 * Each flow thus occupies 32bit which is much more memory efficient than EMC.
161 * SMC uses a set-associative design that each bucket contains
162 * SMC_ENTRY_PER_BUCKET number of entries.
163 * Since 16-bit flow_table index is used, if there are more than 2^16
164 * dp_netdev_flow, SMC will miss them that cannot be indexed by a 16-bit value.
165 *
166 *
167 * Thread-safety
168 * =============
169 *
170 * Each pmd_thread has its own private exact match cache.
171 * If dp_netdev_input is not called from a pmd thread, a mutex is used.
172 */
173
174 #define EM_FLOW_HASH_SHIFT 13
175 #define EM_FLOW_HASH_ENTRIES (1u << EM_FLOW_HASH_SHIFT)
176 #define EM_FLOW_HASH_MASK (EM_FLOW_HASH_ENTRIES - 1)
177 #define EM_FLOW_HASH_SEGS 2
178
179 /* SMC uses a set-associative design. A bucket contains a set of entries that
180 * a flow item can occupy. For now, it uses one hash function rather than two
181 * as for the EMC design. */
182 #define SMC_ENTRY_PER_BUCKET 4
183 #define SMC_ENTRIES (1u << 20)
184 #define SMC_BUCKET_CNT (SMC_ENTRIES / SMC_ENTRY_PER_BUCKET)
185 #define SMC_MASK (SMC_BUCKET_CNT - 1)
186
187 /* Default EMC insert probability is 1 / DEFAULT_EM_FLOW_INSERT_INV_PROB */
188 #define DEFAULT_EM_FLOW_INSERT_INV_PROB 100
189 #define DEFAULT_EM_FLOW_INSERT_MIN (UINT32_MAX / \
190 DEFAULT_EM_FLOW_INSERT_INV_PROB)
191
192 struct emc_entry {
193 struct dp_netdev_flow *flow;
194 struct netdev_flow_key key; /* key.hash used for emc hash value. */
195 };
196
197 struct emc_cache {
198 struct emc_entry entries[EM_FLOW_HASH_ENTRIES];
199 int sweep_idx; /* For emc_cache_slow_sweep(). */
200 };
201
202 struct smc_bucket {
203 uint16_t sig[SMC_ENTRY_PER_BUCKET];
204 uint16_t flow_idx[SMC_ENTRY_PER_BUCKET];
205 };
206
207 /* Signature match cache, differentiate from EMC cache */
208 struct smc_cache {
209 struct smc_bucket buckets[SMC_BUCKET_CNT];
210 };
211
212 struct dfc_cache {
213 struct emc_cache emc_cache;
214 struct smc_cache smc_cache;
215 };
216
217 /* Iterate in the exact match cache through every entry that might contain a
218 * miniflow with hash 'HASH'. */
219 #define EMC_FOR_EACH_POS_WITH_HASH(EMC, CURRENT_ENTRY, HASH) \
220 for (uint32_t i__ = 0, srch_hash__ = (HASH); \
221 (CURRENT_ENTRY) = &(EMC)->entries[srch_hash__ & EM_FLOW_HASH_MASK], \
222 i__ < EM_FLOW_HASH_SEGS; \
223 i__++, srch_hash__ >>= EM_FLOW_HASH_SHIFT)
224 \f
225 /* Simple non-wildcarding single-priority classifier. */
226
227 /* Time in microseconds between successive optimizations of the dpcls
228 * subtable vector */
229 #define DPCLS_OPTIMIZATION_INTERVAL 1000000LL
230
231 /* Time in microseconds of the interval in which rxq processing cycles used
232 * in rxq to pmd assignments is measured and stored. */
233 #define PMD_RXQ_INTERVAL_LEN 10000000LL
234
235 /* Number of intervals for which cycles are stored
236 * and used during rxq to pmd assignment. */
237 #define PMD_RXQ_INTERVAL_MAX 6
238
239 struct dpcls {
240 struct cmap_node node; /* Within dp_netdev_pmd_thread.classifiers */
241 odp_port_t in_port;
242 struct cmap subtables_map;
243 struct pvector subtables;
244 };
245
246 /* A rule to be inserted to the classifier. */
247 struct dpcls_rule {
248 struct cmap_node cmap_node; /* Within struct dpcls_subtable 'rules'. */
249 struct netdev_flow_key *mask; /* Subtable's mask. */
250 struct netdev_flow_key flow; /* Matching key. */
251 /* 'flow' must be the last field, additional space is allocated here. */
252 };
253
254 /* Data structure to keep packet order till fastpath processing. */
255 struct dp_packet_flow_map {
256 struct dp_packet *packet;
257 struct dp_netdev_flow *flow;
258 uint16_t tcp_flags;
259 };
260
261 static void dpcls_init(struct dpcls *);
262 static void dpcls_destroy(struct dpcls *);
263 static void dpcls_sort_subtable_vector(struct dpcls *);
264 static void dpcls_insert(struct dpcls *, struct dpcls_rule *,
265 const struct netdev_flow_key *mask);
266 static void dpcls_remove(struct dpcls *, struct dpcls_rule *);
267 static bool dpcls_lookup(struct dpcls *cls,
268 const struct netdev_flow_key *keys[],
269 struct dpcls_rule **rules, size_t cnt,
270 int *num_lookups_p);
271 static bool dpcls_rule_matches_key(const struct dpcls_rule *rule,
272 const struct netdev_flow_key *target);
273 /* Set of supported meter flags */
274 #define DP_SUPPORTED_METER_FLAGS_MASK \
275 (OFPMF13_STATS | OFPMF13_PKTPS | OFPMF13_KBPS | OFPMF13_BURST)
276
277 /* Set of supported meter band types */
278 #define DP_SUPPORTED_METER_BAND_TYPES \
279 ( 1 << OFPMBT13_DROP )
280
281 struct dp_meter_band {
282 struct ofputil_meter_band up; /* type, prec_level, pad, rate, burst_size */
283 uint32_t bucket; /* In 1/1000 packets (for PKTPS), or in bits (for KBPS) */
284 uint64_t packet_count;
285 uint64_t byte_count;
286 };
287
288 struct dp_meter {
289 uint16_t flags;
290 uint16_t n_bands;
291 uint32_t max_delta_t;
292 uint64_t used;
293 uint64_t packet_count;
294 uint64_t byte_count;
295 struct dp_meter_band bands[];
296 };
297
298 struct pmd_auto_lb {
299 bool auto_lb_requested; /* Auto load balancing requested by user. */
300 bool is_enabled; /* Current status of Auto load balancing. */
301 uint64_t rebalance_intvl;
302 uint64_t rebalance_poll_timer;
303 };
304
305 /* Datapath based on the network device interface from netdev.h.
306 *
307 *
308 * Thread-safety
309 * =============
310 *
311 * Some members, marked 'const', are immutable. Accessing other members
312 * requires synchronization, as noted in more detail below.
313 *
314 * Acquisition order is, from outermost to innermost:
315 *
316 * dp_netdev_mutex (global)
317 * port_mutex
318 * non_pmd_mutex
319 */
320 struct dp_netdev {
321 const struct dpif_class *const class;
322 const char *const name;
323 struct dpif *dpif;
324 struct ovs_refcount ref_cnt;
325 atomic_flag destroyed;
326
327 /* Ports.
328 *
329 * Any lookup into 'ports' or any access to the dp_netdev_ports found
330 * through 'ports' requires taking 'port_mutex'. */
331 struct ovs_mutex port_mutex;
332 struct hmap ports;
333 struct seq *port_seq; /* Incremented whenever a port changes. */
334
335 /* The time that a packet can wait in output batch for sending. */
336 atomic_uint32_t tx_flush_interval;
337
338 /* Meters. */
339 struct ovs_mutex meter_locks[N_METER_LOCKS];
340 struct dp_meter *meters[MAX_METERS]; /* Meter bands. */
341
342 /* Probability of EMC insertions is a factor of 'emc_insert_min'.*/
343 OVS_ALIGNED_VAR(CACHE_LINE_SIZE) atomic_uint32_t emc_insert_min;
344 /* Enable collection of PMD performance metrics. */
345 atomic_bool pmd_perf_metrics;
346 /* Enable the SMC cache from ovsdb config */
347 atomic_bool smc_enable_db;
348
349 /* Protects access to ofproto-dpif-upcall interface during revalidator
350 * thread synchronization. */
351 struct fat_rwlock upcall_rwlock;
352 upcall_callback *upcall_cb; /* Callback function for executing upcalls. */
353 void *upcall_aux;
354
355 /* Callback function for notifying the purging of dp flows (during
356 * reseting pmd deletion). */
357 dp_purge_callback *dp_purge_cb;
358 void *dp_purge_aux;
359
360 /* Stores all 'struct dp_netdev_pmd_thread's. */
361 struct cmap poll_threads;
362 /* id pool for per thread static_tx_qid. */
363 struct id_pool *tx_qid_pool;
364 struct ovs_mutex tx_qid_pool_mutex;
365 /* Use measured cycles for rxq to pmd assignment. */
366 bool pmd_rxq_assign_cyc;
367
368 /* Protects the access of the 'struct dp_netdev_pmd_thread'
369 * instance for non-pmd thread. */
370 struct ovs_mutex non_pmd_mutex;
371
372 /* Each pmd thread will store its pointer to
373 * 'struct dp_netdev_pmd_thread' in 'per_pmd_key'. */
374 ovsthread_key_t per_pmd_key;
375
376 struct seq *reconfigure_seq;
377 uint64_t last_reconfigure_seq;
378
379 /* Cpu mask for pin of pmd threads. */
380 char *pmd_cmask;
381
382 uint64_t last_tnl_conf_seq;
383
384 struct conntrack *conntrack;
385 struct pmd_auto_lb pmd_alb;
386 };
387
388 static void meter_lock(const struct dp_netdev *dp, uint32_t meter_id)
389 OVS_ACQUIRES(dp->meter_locks[meter_id % N_METER_LOCKS])
390 {
391 ovs_mutex_lock(&dp->meter_locks[meter_id % N_METER_LOCKS]);
392 }
393
394 static void meter_unlock(const struct dp_netdev *dp, uint32_t meter_id)
395 OVS_RELEASES(dp->meter_locks[meter_id % N_METER_LOCKS])
396 {
397 ovs_mutex_unlock(&dp->meter_locks[meter_id % N_METER_LOCKS]);
398 }
399
400
401 static struct dp_netdev_port *dp_netdev_lookup_port(const struct dp_netdev *dp,
402 odp_port_t)
403 OVS_REQUIRES(dp->port_mutex);
404
405 enum rxq_cycles_counter_type {
406 RXQ_CYCLES_PROC_CURR, /* Cycles spent successfully polling and
407 processing packets during the current
408 interval. */
409 RXQ_CYCLES_PROC_HIST, /* Total cycles of all intervals that are used
410 during rxq to pmd assignment. */
411 RXQ_N_CYCLES
412 };
413
414 enum {
415 DP_NETDEV_FLOW_OFFLOAD_OP_ADD,
416 DP_NETDEV_FLOW_OFFLOAD_OP_MOD,
417 DP_NETDEV_FLOW_OFFLOAD_OP_DEL,
418 };
419
420 struct dp_flow_offload_item {
421 struct dp_netdev_pmd_thread *pmd;
422 struct dp_netdev_flow *flow;
423 int op;
424 struct match match;
425 struct nlattr *actions;
426 size_t actions_len;
427
428 struct ovs_list node;
429 };
430
431 struct dp_flow_offload {
432 struct ovs_mutex mutex;
433 struct ovs_list list;
434 pthread_cond_t cond;
435 };
436
437 static struct dp_flow_offload dp_flow_offload = {
438 .mutex = OVS_MUTEX_INITIALIZER,
439 .list = OVS_LIST_INITIALIZER(&dp_flow_offload.list),
440 };
441
442 static struct ovsthread_once offload_thread_once
443 = OVSTHREAD_ONCE_INITIALIZER;
444
445 #define XPS_TIMEOUT 500000LL /* In microseconds. */
446
447 /* Contained by struct dp_netdev_port's 'rxqs' member. */
448 struct dp_netdev_rxq {
449 struct dp_netdev_port *port;
450 struct netdev_rxq *rx;
451 unsigned core_id; /* Core to which this queue should be
452 pinned. OVS_CORE_UNSPEC if the
453 queue doesn't need to be pinned to a
454 particular core. */
455 unsigned intrvl_idx; /* Write index for 'cycles_intrvl'. */
456 struct dp_netdev_pmd_thread *pmd; /* pmd thread that polls this queue. */
457 bool is_vhost; /* Is rxq of a vhost port. */
458
459 /* Counters of cycles spent successfully polling and processing pkts. */
460 atomic_ullong cycles[RXQ_N_CYCLES];
461 /* We store PMD_RXQ_INTERVAL_MAX intervals of data for an rxq and then
462 sum them to yield the cycles used for an rxq. */
463 atomic_ullong cycles_intrvl[PMD_RXQ_INTERVAL_MAX];
464 };
465
466 /* A port in a netdev-based datapath. */
467 struct dp_netdev_port {
468 odp_port_t port_no;
469 bool dynamic_txqs; /* If true XPS will be used. */
470 bool need_reconfigure; /* True if we should reconfigure netdev. */
471 struct netdev *netdev;
472 struct hmap_node node; /* Node in dp_netdev's 'ports'. */
473 struct netdev_saved_flags *sf;
474 struct dp_netdev_rxq *rxqs;
475 unsigned n_rxq; /* Number of elements in 'rxqs' */
476 unsigned *txq_used; /* Number of threads that use each tx queue. */
477 struct ovs_mutex txq_used_mutex;
478 bool emc_enabled; /* If true EMC will be used. */
479 char *type; /* Port type as requested by user. */
480 char *rxq_affinity_list; /* Requested affinity of rx queues. */
481 };
482
483 /* Contained by struct dp_netdev_flow's 'stats' member. */
484 struct dp_netdev_flow_stats {
485 atomic_llong used; /* Last used time, in monotonic msecs. */
486 atomic_ullong packet_count; /* Number of packets matched. */
487 atomic_ullong byte_count; /* Number of bytes matched. */
488 atomic_uint16_t tcp_flags; /* Bitwise-OR of seen tcp_flags values. */
489 };
490
491 /* A flow in 'dp_netdev_pmd_thread's 'flow_table'.
492 *
493 *
494 * Thread-safety
495 * =============
496 *
497 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
498 * its pmd thread's classifier. The text below calls this classifier 'cls'.
499 *
500 * Motivation
501 * ----------
502 *
503 * The thread safety rules described here for "struct dp_netdev_flow" are
504 * motivated by two goals:
505 *
506 * - Prevent threads that read members of "struct dp_netdev_flow" from
507 * reading bad data due to changes by some thread concurrently modifying
508 * those members.
509 *
510 * - Prevent two threads making changes to members of a given "struct
511 * dp_netdev_flow" from interfering with each other.
512 *
513 *
514 * Rules
515 * -----
516 *
517 * A flow 'flow' may be accessed without a risk of being freed during an RCU
518 * grace period. Code that needs to hold onto a flow for a while
519 * should try incrementing 'flow->ref_cnt' with dp_netdev_flow_ref().
520 *
521 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
522 * flow from being deleted from 'cls' and it doesn't protect members of 'flow'
523 * from modification.
524 *
525 * Some members, marked 'const', are immutable. Accessing other members
526 * requires synchronization, as noted in more detail below.
527 */
528 struct dp_netdev_flow {
529 const struct flow flow; /* Unmasked flow that created this entry. */
530 /* Hash table index by unmasked flow. */
531 const struct cmap_node node; /* In owning dp_netdev_pmd_thread's */
532 /* 'flow_table'. */
533 const struct cmap_node mark_node; /* In owning flow_mark's mark_to_flow */
534 const ovs_u128 ufid; /* Unique flow identifier. */
535 const ovs_u128 mega_ufid; /* Unique mega flow identifier. */
536 const unsigned pmd_id; /* The 'core_id' of pmd thread owning this */
537 /* flow. */
538
539 /* Number of references.
540 * The classifier owns one reference.
541 * Any thread trying to keep a rule from being freed should hold its own
542 * reference. */
543 struct ovs_refcount ref_cnt;
544
545 bool dead;
546 uint32_t mark; /* Unique flow mark assigned to a flow */
547
548 /* Statistics. */
549 struct dp_netdev_flow_stats stats;
550
551 /* Actions. */
552 OVSRCU_TYPE(struct dp_netdev_actions *) actions;
553
554 /* While processing a group of input packets, the datapath uses the next
555 * member to store a pointer to the output batch for the flow. It is
556 * reset after the batch has been sent out (See dp_netdev_queue_batches(),
557 * packet_batch_per_flow_init() and packet_batch_per_flow_execute()). */
558 struct packet_batch_per_flow *batch;
559
560 /* Packet classification. */
561 struct dpcls_rule cr; /* In owning dp_netdev's 'cls'. */
562 /* 'cr' must be the last member. */
563 };
564
565 static void dp_netdev_flow_unref(struct dp_netdev_flow *);
566 static bool dp_netdev_flow_ref(struct dp_netdev_flow *);
567 static int dpif_netdev_flow_from_nlattrs(const struct nlattr *, uint32_t,
568 struct flow *, bool);
569
570 /* A set of datapath actions within a "struct dp_netdev_flow".
571 *
572 *
573 * Thread-safety
574 * =============
575 *
576 * A struct dp_netdev_actions 'actions' is protected with RCU. */
577 struct dp_netdev_actions {
578 /* These members are immutable: they do not change during the struct's
579 * lifetime. */
580 unsigned int size; /* Size of 'actions', in bytes. */
581 struct nlattr actions[]; /* Sequence of OVS_ACTION_ATTR_* attributes. */
582 };
583
584 struct dp_netdev_actions *dp_netdev_actions_create(const struct nlattr *,
585 size_t);
586 struct dp_netdev_actions *dp_netdev_flow_get_actions(
587 const struct dp_netdev_flow *);
588 static void dp_netdev_actions_free(struct dp_netdev_actions *);
589
590 struct polled_queue {
591 struct dp_netdev_rxq *rxq;
592 odp_port_t port_no;
593 bool emc_enabled;
594 bool rxq_enabled;
595 uint64_t change_seq;
596 };
597
598 /* Contained by struct dp_netdev_pmd_thread's 'poll_list' member. */
599 struct rxq_poll {
600 struct dp_netdev_rxq *rxq;
601 struct hmap_node node;
602 };
603
604 /* Contained by struct dp_netdev_pmd_thread's 'send_port_cache',
605 * 'tnl_port_cache' or 'tx_ports'. */
606 struct tx_port {
607 struct dp_netdev_port *port;
608 int qid;
609 long long last_used;
610 struct hmap_node node;
611 long long flush_time;
612 struct dp_packet_batch output_pkts;
613 struct dp_netdev_rxq *output_pkts_rxqs[NETDEV_MAX_BURST];
614 };
615
616 /* A set of properties for the current processing loop that is not directly
617 * associated with the pmd thread itself, but with the packets being
618 * processed or the short-term system configuration (for example, time).
619 * Contained by struct dp_netdev_pmd_thread's 'ctx' member. */
620 struct dp_netdev_pmd_thread_ctx {
621 /* Latest measured time. See 'pmd_thread_ctx_time_update()'. */
622 long long now;
623 /* RX queue from which last packet was received. */
624 struct dp_netdev_rxq *last_rxq;
625 /* EMC insertion probability context for the current processing cycle. */
626 uint32_t emc_insert_min;
627 };
628
629 /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
630 * the performance overhead of interrupt processing. Therefore netdev can
631 * not implement rx-wait for these devices. dpif-netdev needs to poll
632 * these device to check for recv buffer. pmd-thread does polling for
633 * devices assigned to itself.
634 *
635 * DPDK used PMD for accessing NIC.
636 *
637 * Note, instance with cpu core id NON_PMD_CORE_ID will be reserved for
638 * I/O of all non-pmd threads. There will be no actual thread created
639 * for the instance.
640 *
641 * Each struct has its own flow cache and classifier per managed ingress port.
642 * For packets received on ingress port, a look up is done on corresponding PMD
643 * thread's flow cache and in case of a miss, lookup is performed in the
644 * corresponding classifier of port. Packets are executed with the found
645 * actions in either case.
646 * */
647 struct dp_netdev_pmd_thread {
648 struct dp_netdev *dp;
649 struct ovs_refcount ref_cnt; /* Every reference must be refcount'ed. */
650 struct cmap_node node; /* In 'dp->poll_threads'. */
651
652 /* Per thread exact-match cache. Note, the instance for cpu core
653 * NON_PMD_CORE_ID can be accessed by multiple threads, and thusly
654 * need to be protected by 'non_pmd_mutex'. Every other instance
655 * will only be accessed by its own pmd thread. */
656 OVS_ALIGNED_VAR(CACHE_LINE_SIZE) struct dfc_cache flow_cache;
657
658 /* Flow-Table and classifiers
659 *
660 * Writers of 'flow_table' must take the 'flow_mutex'. Corresponding
661 * changes to 'classifiers' must be made while still holding the
662 * 'flow_mutex'.
663 */
664 struct ovs_mutex flow_mutex;
665 struct cmap flow_table OVS_GUARDED; /* Flow table. */
666
667 /* One classifier per in_port polled by the pmd */
668 struct cmap classifiers;
669 /* Periodically sort subtable vectors according to hit frequencies */
670 long long int next_optimization;
671 /* End of the next time interval for which processing cycles
672 are stored for each polled rxq. */
673 long long int rxq_next_cycle_store;
674
675 /* Last interval timestamp. */
676 uint64_t intrvl_tsc_prev;
677 /* Last interval cycles. */
678 atomic_ullong intrvl_cycles;
679
680 /* Current context of the PMD thread. */
681 struct dp_netdev_pmd_thread_ctx ctx;
682
683 struct seq *reload_seq;
684 uint64_t last_reload_seq;
685 atomic_bool reload; /* Do we need to reload ports? */
686 atomic_bool exit; /* For terminating the pmd thread. */
687 pthread_t thread;
688 unsigned core_id; /* CPU core id of this pmd thread. */
689 int numa_id; /* numa node id of this pmd thread. */
690 bool isolated;
691
692 /* Queue id used by this pmd thread to send packets on all netdevs if
693 * XPS disabled for this netdev. All static_tx_qid's are unique and less
694 * than 'cmap_count(dp->poll_threads)'. */
695 uint32_t static_tx_qid;
696
697 /* Number of filled output batches. */
698 int n_output_batches;
699
700 struct ovs_mutex port_mutex; /* Mutex for 'poll_list' and 'tx_ports'. */
701 /* List of rx queues to poll. */
702 struct hmap poll_list OVS_GUARDED;
703 /* Map of 'tx_port's used for transmission. Written by the main thread,
704 * read by the pmd thread. */
705 struct hmap tx_ports OVS_GUARDED;
706
707 /* These are thread-local copies of 'tx_ports'. One contains only tunnel
708 * ports (that support push_tunnel/pop_tunnel), the other contains ports
709 * with at least one txq (that support send). A port can be in both.
710 *
711 * There are two separate maps to make sure that we don't try to execute
712 * OUTPUT on a device which has 0 txqs or PUSH/POP on a non-tunnel device.
713 *
714 * The instances for cpu core NON_PMD_CORE_ID can be accessed by multiple
715 * threads, and thusly need to be protected by 'non_pmd_mutex'. Every
716 * other instance will only be accessed by its own pmd thread. */
717 struct hmap tnl_port_cache;
718 struct hmap send_port_cache;
719
720 /* Keep track of detailed PMD performance statistics. */
721 struct pmd_perf_stats perf_stats;
722
723 /* Stats from previous iteration used by automatic pmd
724 * load balance logic. */
725 uint64_t prev_stats[PMD_N_STATS];
726 atomic_count pmd_overloaded;
727
728 /* Set to true if the pmd thread needs to be reloaded. */
729 bool need_reload;
730 };
731
732 /* Interface to netdev-based datapath. */
733 struct dpif_netdev {
734 struct dpif dpif;
735 struct dp_netdev *dp;
736 uint64_t last_port_seq;
737 };
738
739 static int get_port_by_number(struct dp_netdev *dp, odp_port_t port_no,
740 struct dp_netdev_port **portp)
741 OVS_REQUIRES(dp->port_mutex);
742 static int get_port_by_name(struct dp_netdev *dp, const char *devname,
743 struct dp_netdev_port **portp)
744 OVS_REQUIRES(dp->port_mutex);
745 static void dp_netdev_free(struct dp_netdev *)
746 OVS_REQUIRES(dp_netdev_mutex);
747 static int do_add_port(struct dp_netdev *dp, const char *devname,
748 const char *type, odp_port_t port_no)
749 OVS_REQUIRES(dp->port_mutex);
750 static void do_del_port(struct dp_netdev *dp, struct dp_netdev_port *)
751 OVS_REQUIRES(dp->port_mutex);
752 static int dpif_netdev_open(const struct dpif_class *, const char *name,
753 bool create, struct dpif **);
754 static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread *pmd,
755 struct dp_packet_batch *,
756 bool should_steal,
757 const struct flow *flow,
758 const struct nlattr *actions,
759 size_t actions_len);
760 static void dp_netdev_input(struct dp_netdev_pmd_thread *,
761 struct dp_packet_batch *, odp_port_t port_no);
762 static void dp_netdev_recirculate(struct dp_netdev_pmd_thread *,
763 struct dp_packet_batch *);
764
765 static void dp_netdev_disable_upcall(struct dp_netdev *);
766 static void dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread *pmd);
767 static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread *pmd,
768 struct dp_netdev *dp, unsigned core_id,
769 int numa_id);
770 static void dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread *pmd);
771 static void dp_netdev_set_nonpmd(struct dp_netdev *dp)
772 OVS_REQUIRES(dp->port_mutex);
773
774 static void *pmd_thread_main(void *);
775 static struct dp_netdev_pmd_thread *dp_netdev_get_pmd(struct dp_netdev *dp,
776 unsigned core_id);
777 static struct dp_netdev_pmd_thread *
778 dp_netdev_pmd_get_next(struct dp_netdev *dp, struct cmap_position *pos);
779 static void dp_netdev_del_pmd(struct dp_netdev *dp,
780 struct dp_netdev_pmd_thread *pmd);
781 static void dp_netdev_destroy_all_pmds(struct dp_netdev *dp, bool non_pmd);
782 static void dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread *pmd);
783 static void dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread *pmd,
784 struct dp_netdev_port *port)
785 OVS_REQUIRES(pmd->port_mutex);
786 static void dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread *pmd,
787 struct tx_port *tx)
788 OVS_REQUIRES(pmd->port_mutex);
789 static void dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread *pmd,
790 struct dp_netdev_rxq *rxq)
791 OVS_REQUIRES(pmd->port_mutex);
792 static void dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread *pmd,
793 struct rxq_poll *poll)
794 OVS_REQUIRES(pmd->port_mutex);
795 static int
796 dp_netdev_pmd_flush_output_packets(struct dp_netdev_pmd_thread *pmd,
797 bool force);
798
799 static void reconfigure_datapath(struct dp_netdev *dp)
800 OVS_REQUIRES(dp->port_mutex);
801 static bool dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread *pmd);
802 static void dp_netdev_pmd_unref(struct dp_netdev_pmd_thread *pmd);
803 static void dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread *pmd);
804 static void pmd_load_cached_ports(struct dp_netdev_pmd_thread *pmd)
805 OVS_REQUIRES(pmd->port_mutex);
806 static inline void
807 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread *pmd,
808 struct polled_queue *poll_list, int poll_cnt);
809 static void
810 dp_netdev_rxq_set_cycles(struct dp_netdev_rxq *rx,
811 enum rxq_cycles_counter_type type,
812 unsigned long long cycles);
813 static uint64_t
814 dp_netdev_rxq_get_cycles(struct dp_netdev_rxq *rx,
815 enum rxq_cycles_counter_type type);
816 static void
817 dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq *rx,
818 unsigned long long cycles);
819 static uint64_t
820 dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq *rx, unsigned idx);
821 static void
822 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread *pmd,
823 bool purge);
824 static int dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread *pmd,
825 struct tx_port *tx);
826
827 static inline bool emc_entry_alive(struct emc_entry *ce);
828 static void emc_clear_entry(struct emc_entry *ce);
829 static void smc_clear_entry(struct smc_bucket *b, int idx);
830
831 static void dp_netdev_request_reconfigure(struct dp_netdev *dp);
832 static inline bool
833 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread *pmd);
834 static void queue_netdev_flow_del(struct dp_netdev_pmd_thread *pmd,
835 struct dp_netdev_flow *flow);
836
837 static void
838 emc_cache_init(struct emc_cache *flow_cache)
839 {
840 int i;
841
842 flow_cache->sweep_idx = 0;
843 for (i = 0; i < ARRAY_SIZE(flow_cache->entries); i++) {
844 flow_cache->entries[i].flow = NULL;
845 flow_cache->entries[i].key.hash = 0;
846 flow_cache->entries[i].key.len = sizeof(struct miniflow);
847 flowmap_init(&flow_cache->entries[i].key.mf.map);
848 }
849 }
850
851 static void
852 smc_cache_init(struct smc_cache *smc_cache)
853 {
854 int i, j;
855 for (i = 0; i < SMC_BUCKET_CNT; i++) {
856 for (j = 0; j < SMC_ENTRY_PER_BUCKET; j++) {
857 smc_cache->buckets[i].flow_idx[j] = UINT16_MAX;
858 }
859 }
860 }
861
862 static void
863 dfc_cache_init(struct dfc_cache *flow_cache)
864 {
865 emc_cache_init(&flow_cache->emc_cache);
866 smc_cache_init(&flow_cache->smc_cache);
867 }
868
869 static void
870 emc_cache_uninit(struct emc_cache *flow_cache)
871 {
872 int i;
873
874 for (i = 0; i < ARRAY_SIZE(flow_cache->entries); i++) {
875 emc_clear_entry(&flow_cache->entries[i]);
876 }
877 }
878
879 static void
880 smc_cache_uninit(struct smc_cache *smc)
881 {
882 int i, j;
883
884 for (i = 0; i < SMC_BUCKET_CNT; i++) {
885 for (j = 0; j < SMC_ENTRY_PER_BUCKET; j++) {
886 smc_clear_entry(&(smc->buckets[i]), j);
887 }
888 }
889 }
890
891 static void
892 dfc_cache_uninit(struct dfc_cache *flow_cache)
893 {
894 smc_cache_uninit(&flow_cache->smc_cache);
895 emc_cache_uninit(&flow_cache->emc_cache);
896 }
897
898 /* Check and clear dead flow references slowly (one entry at each
899 * invocation). */
900 static void
901 emc_cache_slow_sweep(struct emc_cache *flow_cache)
902 {
903 struct emc_entry *entry = &flow_cache->entries[flow_cache->sweep_idx];
904
905 if (!emc_entry_alive(entry)) {
906 emc_clear_entry(entry);
907 }
908 flow_cache->sweep_idx = (flow_cache->sweep_idx + 1) & EM_FLOW_HASH_MASK;
909 }
910
911 /* Updates the time in PMD threads context and should be called in three cases:
912 *
913 * 1. PMD structure initialization:
914 * - dp_netdev_configure_pmd()
915 *
916 * 2. Before processing of the new packet batch:
917 * - dpif_netdev_execute()
918 * - dp_netdev_process_rxq_port()
919 *
920 * 3. At least once per polling iteration in main polling threads if no
921 * packets received on current iteration:
922 * - dpif_netdev_run()
923 * - pmd_thread_main()
924 *
925 * 'pmd->ctx.now' should be used without update in all other cases if possible.
926 */
927 static inline void
928 pmd_thread_ctx_time_update(struct dp_netdev_pmd_thread *pmd)
929 {
930 pmd->ctx.now = time_usec();
931 }
932
933 /* Returns true if 'dpif' is a netdev or dummy dpif, false otherwise. */
934 bool
935 dpif_is_netdev(const struct dpif *dpif)
936 {
937 return dpif->dpif_class->open == dpif_netdev_open;
938 }
939
940 static struct dpif_netdev *
941 dpif_netdev_cast(const struct dpif *dpif)
942 {
943 ovs_assert(dpif_is_netdev(dpif));
944 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
945 }
946
947 static struct dp_netdev *
948 get_dp_netdev(const struct dpif *dpif)
949 {
950 return dpif_netdev_cast(dpif)->dp;
951 }
952 \f
953 enum pmd_info_type {
954 PMD_INFO_SHOW_STATS, /* Show how cpu cycles are spent. */
955 PMD_INFO_CLEAR_STATS, /* Set the cycles count to 0. */
956 PMD_INFO_SHOW_RXQ, /* Show poll lists of pmd threads. */
957 PMD_INFO_PERF_SHOW, /* Show pmd performance details. */
958 };
959
960 static void
961 format_pmd_thread(struct ds *reply, struct dp_netdev_pmd_thread *pmd)
962 {
963 ds_put_cstr(reply, (pmd->core_id == NON_PMD_CORE_ID)
964 ? "main thread" : "pmd thread");
965 if (pmd->numa_id != OVS_NUMA_UNSPEC) {
966 ds_put_format(reply, " numa_id %d", pmd->numa_id);
967 }
968 if (pmd->core_id != OVS_CORE_UNSPEC && pmd->core_id != NON_PMD_CORE_ID) {
969 ds_put_format(reply, " core_id %u", pmd->core_id);
970 }
971 ds_put_cstr(reply, ":\n");
972 }
973
974 static void
975 pmd_info_show_stats(struct ds *reply,
976 struct dp_netdev_pmd_thread *pmd)
977 {
978 uint64_t stats[PMD_N_STATS];
979 uint64_t total_cycles, total_packets;
980 double passes_per_pkt = 0;
981 double lookups_per_hit = 0;
982 double packets_per_batch = 0;
983
984 pmd_perf_read_counters(&pmd->perf_stats, stats);
985 total_cycles = stats[PMD_CYCLES_ITER_IDLE]
986 + stats[PMD_CYCLES_ITER_BUSY];
987 total_packets = stats[PMD_STAT_RECV];
988
989 format_pmd_thread(reply, pmd);
990
991 if (total_packets > 0) {
992 passes_per_pkt = (total_packets + stats[PMD_STAT_RECIRC])
993 / (double) total_packets;
994 }
995 if (stats[PMD_STAT_MASKED_HIT] > 0) {
996 lookups_per_hit = stats[PMD_STAT_MASKED_LOOKUP]
997 / (double) stats[PMD_STAT_MASKED_HIT];
998 }
999 if (stats[PMD_STAT_SENT_BATCHES] > 0) {
1000 packets_per_batch = stats[PMD_STAT_SENT_PKTS]
1001 / (double) stats[PMD_STAT_SENT_BATCHES];
1002 }
1003
1004 ds_put_format(reply,
1005 " packets received: %"PRIu64"\n"
1006 " packet recirculations: %"PRIu64"\n"
1007 " avg. datapath passes per packet: %.02f\n"
1008 " emc hits: %"PRIu64"\n"
1009 " smc hits: %"PRIu64"\n"
1010 " megaflow hits: %"PRIu64"\n"
1011 " avg. subtable lookups per megaflow hit: %.02f\n"
1012 " miss with success upcall: %"PRIu64"\n"
1013 " miss with failed upcall: %"PRIu64"\n"
1014 " avg. packets per output batch: %.02f\n",
1015 total_packets, stats[PMD_STAT_RECIRC],
1016 passes_per_pkt, stats[PMD_STAT_EXACT_HIT],
1017 stats[PMD_STAT_SMC_HIT],
1018 stats[PMD_STAT_MASKED_HIT], lookups_per_hit,
1019 stats[PMD_STAT_MISS], stats[PMD_STAT_LOST],
1020 packets_per_batch);
1021
1022 if (total_cycles == 0) {
1023 return;
1024 }
1025
1026 ds_put_format(reply,
1027 " idle cycles: %"PRIu64" (%.02f%%)\n"
1028 " processing cycles: %"PRIu64" (%.02f%%)\n",
1029 stats[PMD_CYCLES_ITER_IDLE],
1030 stats[PMD_CYCLES_ITER_IDLE] / (double) total_cycles * 100,
1031 stats[PMD_CYCLES_ITER_BUSY],
1032 stats[PMD_CYCLES_ITER_BUSY] / (double) total_cycles * 100);
1033
1034 if (total_packets == 0) {
1035 return;
1036 }
1037
1038 ds_put_format(reply,
1039 " avg cycles per packet: %.02f (%"PRIu64"/%"PRIu64")\n",
1040 total_cycles / (double) total_packets,
1041 total_cycles, total_packets);
1042
1043 ds_put_format(reply,
1044 " avg processing cycles per packet: "
1045 "%.02f (%"PRIu64"/%"PRIu64")\n",
1046 stats[PMD_CYCLES_ITER_BUSY] / (double) total_packets,
1047 stats[PMD_CYCLES_ITER_BUSY], total_packets);
1048 }
1049
1050 static void
1051 pmd_info_show_perf(struct ds *reply,
1052 struct dp_netdev_pmd_thread *pmd,
1053 struct pmd_perf_params *par)
1054 {
1055 if (pmd->core_id != NON_PMD_CORE_ID) {
1056 char *time_str =
1057 xastrftime_msec("%H:%M:%S.###", time_wall_msec(), true);
1058 long long now = time_msec();
1059 double duration = (now - pmd->perf_stats.start_ms) / 1000.0;
1060
1061 ds_put_cstr(reply, "\n");
1062 ds_put_format(reply, "Time: %s\n", time_str);
1063 ds_put_format(reply, "Measurement duration: %.3f s\n", duration);
1064 ds_put_cstr(reply, "\n");
1065 format_pmd_thread(reply, pmd);
1066 ds_put_cstr(reply, "\n");
1067 pmd_perf_format_overall_stats(reply, &pmd->perf_stats, duration);
1068 if (pmd_perf_metrics_enabled(pmd)) {
1069 /* Prevent parallel clearing of perf metrics. */
1070 ovs_mutex_lock(&pmd->perf_stats.clear_mutex);
1071 if (par->histograms) {
1072 ds_put_cstr(reply, "\n");
1073 pmd_perf_format_histograms(reply, &pmd->perf_stats);
1074 }
1075 if (par->iter_hist_len > 0) {
1076 ds_put_cstr(reply, "\n");
1077 pmd_perf_format_iteration_history(reply, &pmd->perf_stats,
1078 par->iter_hist_len);
1079 }
1080 if (par->ms_hist_len > 0) {
1081 ds_put_cstr(reply, "\n");
1082 pmd_perf_format_ms_history(reply, &pmd->perf_stats,
1083 par->ms_hist_len);
1084 }
1085 ovs_mutex_unlock(&pmd->perf_stats.clear_mutex);
1086 }
1087 free(time_str);
1088 }
1089 }
1090
1091 static int
1092 compare_poll_list(const void *a_, const void *b_)
1093 {
1094 const struct rxq_poll *a = a_;
1095 const struct rxq_poll *b = b_;
1096
1097 const char *namea = netdev_rxq_get_name(a->rxq->rx);
1098 const char *nameb = netdev_rxq_get_name(b->rxq->rx);
1099
1100 int cmp = strcmp(namea, nameb);
1101 if (!cmp) {
1102 return netdev_rxq_get_queue_id(a->rxq->rx)
1103 - netdev_rxq_get_queue_id(b->rxq->rx);
1104 } else {
1105 return cmp;
1106 }
1107 }
1108
1109 static void
1110 sorted_poll_list(struct dp_netdev_pmd_thread *pmd, struct rxq_poll **list,
1111 size_t *n)
1112 OVS_REQUIRES(pmd->port_mutex)
1113 {
1114 struct rxq_poll *ret, *poll;
1115 size_t i;
1116
1117 *n = hmap_count(&pmd->poll_list);
1118 if (!*n) {
1119 ret = NULL;
1120 } else {
1121 ret = xcalloc(*n, sizeof *ret);
1122 i = 0;
1123 HMAP_FOR_EACH (poll, node, &pmd->poll_list) {
1124 ret[i] = *poll;
1125 i++;
1126 }
1127 ovs_assert(i == *n);
1128 qsort(ret, *n, sizeof *ret, compare_poll_list);
1129 }
1130
1131 *list = ret;
1132 }
1133
1134 static void
1135 pmd_info_show_rxq(struct ds *reply, struct dp_netdev_pmd_thread *pmd)
1136 {
1137 if (pmd->core_id != NON_PMD_CORE_ID) {
1138 struct rxq_poll *list;
1139 size_t n_rxq;
1140 uint64_t total_cycles = 0;
1141
1142 ds_put_format(reply,
1143 "pmd thread numa_id %d core_id %u:\n isolated : %s\n",
1144 pmd->numa_id, pmd->core_id, (pmd->isolated)
1145 ? "true" : "false");
1146
1147 ovs_mutex_lock(&pmd->port_mutex);
1148 sorted_poll_list(pmd, &list, &n_rxq);
1149
1150 /* Get the total pmd cycles for an interval. */
1151 atomic_read_relaxed(&pmd->intrvl_cycles, &total_cycles);
1152 /* Estimate the cycles to cover all intervals. */
1153 total_cycles *= PMD_RXQ_INTERVAL_MAX;
1154
1155 for (int i = 0; i < n_rxq; i++) {
1156 struct dp_netdev_rxq *rxq = list[i].rxq;
1157 const char *name = netdev_rxq_get_name(rxq->rx);
1158 uint64_t proc_cycles = 0;
1159
1160 for (int j = 0; j < PMD_RXQ_INTERVAL_MAX; j++) {
1161 proc_cycles += dp_netdev_rxq_get_intrvl_cycles(rxq, j);
1162 }
1163 ds_put_format(reply, " port: %-16s queue-id: %2d", name,
1164 netdev_rxq_get_queue_id(list[i].rxq->rx));
1165 ds_put_format(reply, " %s", netdev_rxq_enabled(list[i].rxq->rx)
1166 ? "(enabled) " : "(disabled)");
1167 ds_put_format(reply, " pmd usage: ");
1168 if (total_cycles) {
1169 ds_put_format(reply, "%2"PRIu64"",
1170 proc_cycles * 100 / total_cycles);
1171 ds_put_cstr(reply, " %");
1172 } else {
1173 ds_put_format(reply, "%s", "NOT AVAIL");
1174 }
1175 ds_put_cstr(reply, "\n");
1176 }
1177 ovs_mutex_unlock(&pmd->port_mutex);
1178 free(list);
1179 }
1180 }
1181
1182 static int
1183 compare_poll_thread_list(const void *a_, const void *b_)
1184 {
1185 const struct dp_netdev_pmd_thread *a, *b;
1186
1187 a = *(struct dp_netdev_pmd_thread **)a_;
1188 b = *(struct dp_netdev_pmd_thread **)b_;
1189
1190 if (a->core_id < b->core_id) {
1191 return -1;
1192 }
1193 if (a->core_id > b->core_id) {
1194 return 1;
1195 }
1196 return 0;
1197 }
1198
1199 /* Create a sorted list of pmd's from the dp->poll_threads cmap. We can use
1200 * this list, as long as we do not go to quiescent state. */
1201 static void
1202 sorted_poll_thread_list(struct dp_netdev *dp,
1203 struct dp_netdev_pmd_thread ***list,
1204 size_t *n)
1205 {
1206 struct dp_netdev_pmd_thread *pmd;
1207 struct dp_netdev_pmd_thread **pmd_list;
1208 size_t k = 0, n_pmds;
1209
1210 n_pmds = cmap_count(&dp->poll_threads);
1211 pmd_list = xcalloc(n_pmds, sizeof *pmd_list);
1212
1213 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
1214 if (k >= n_pmds) {
1215 break;
1216 }
1217 pmd_list[k++] = pmd;
1218 }
1219
1220 qsort(pmd_list, k, sizeof *pmd_list, compare_poll_thread_list);
1221
1222 *list = pmd_list;
1223 *n = k;
1224 }
1225
1226 static void
1227 dpif_netdev_pmd_rebalance(struct unixctl_conn *conn, int argc,
1228 const char *argv[], void *aux OVS_UNUSED)
1229 {
1230 struct ds reply = DS_EMPTY_INITIALIZER;
1231 struct dp_netdev *dp = NULL;
1232
1233 ovs_mutex_lock(&dp_netdev_mutex);
1234
1235 if (argc == 2) {
1236 dp = shash_find_data(&dp_netdevs, argv[1]);
1237 } else if (shash_count(&dp_netdevs) == 1) {
1238 /* There's only one datapath */
1239 dp = shash_first(&dp_netdevs)->data;
1240 }
1241
1242 if (!dp) {
1243 ovs_mutex_unlock(&dp_netdev_mutex);
1244 unixctl_command_reply_error(conn,
1245 "please specify an existing datapath");
1246 return;
1247 }
1248
1249 dp_netdev_request_reconfigure(dp);
1250 ovs_mutex_unlock(&dp_netdev_mutex);
1251 ds_put_cstr(&reply, "pmd rxq rebalance requested.\n");
1252 unixctl_command_reply(conn, ds_cstr(&reply));
1253 ds_destroy(&reply);
1254 }
1255
1256 static void
1257 dpif_netdev_pmd_info(struct unixctl_conn *conn, int argc, const char *argv[],
1258 void *aux)
1259 {
1260 struct ds reply = DS_EMPTY_INITIALIZER;
1261 struct dp_netdev_pmd_thread **pmd_list;
1262 struct dp_netdev *dp = NULL;
1263 enum pmd_info_type type = *(enum pmd_info_type *) aux;
1264 unsigned int core_id;
1265 bool filter_on_pmd = false;
1266 size_t n;
1267
1268 ovs_mutex_lock(&dp_netdev_mutex);
1269
1270 while (argc > 1) {
1271 if (!strcmp(argv[1], "-pmd") && argc > 2) {
1272 if (str_to_uint(argv[2], 10, &core_id)) {
1273 filter_on_pmd = true;
1274 }
1275 argc -= 2;
1276 argv += 2;
1277 } else {
1278 dp = shash_find_data(&dp_netdevs, argv[1]);
1279 argc -= 1;
1280 argv += 1;
1281 }
1282 }
1283
1284 if (!dp) {
1285 if (shash_count(&dp_netdevs) == 1) {
1286 /* There's only one datapath */
1287 dp = shash_first(&dp_netdevs)->data;
1288 } else {
1289 ovs_mutex_unlock(&dp_netdev_mutex);
1290 unixctl_command_reply_error(conn,
1291 "please specify an existing datapath");
1292 return;
1293 }
1294 }
1295
1296 sorted_poll_thread_list(dp, &pmd_list, &n);
1297 for (size_t i = 0; i < n; i++) {
1298 struct dp_netdev_pmd_thread *pmd = pmd_list[i];
1299 if (!pmd) {
1300 break;
1301 }
1302 if (filter_on_pmd && pmd->core_id != core_id) {
1303 continue;
1304 }
1305 if (type == PMD_INFO_SHOW_RXQ) {
1306 pmd_info_show_rxq(&reply, pmd);
1307 } else if (type == PMD_INFO_CLEAR_STATS) {
1308 pmd_perf_stats_clear(&pmd->perf_stats);
1309 } else if (type == PMD_INFO_SHOW_STATS) {
1310 pmd_info_show_stats(&reply, pmd);
1311 } else if (type == PMD_INFO_PERF_SHOW) {
1312 pmd_info_show_perf(&reply, pmd, (struct pmd_perf_params *)aux);
1313 }
1314 }
1315 free(pmd_list);
1316
1317 ovs_mutex_unlock(&dp_netdev_mutex);
1318
1319 unixctl_command_reply(conn, ds_cstr(&reply));
1320 ds_destroy(&reply);
1321 }
1322
1323 static void
1324 pmd_perf_show_cmd(struct unixctl_conn *conn, int argc,
1325 const char *argv[],
1326 void *aux OVS_UNUSED)
1327 {
1328 struct pmd_perf_params par;
1329 long int it_hist = 0, ms_hist = 0;
1330 par.histograms = true;
1331
1332 while (argc > 1) {
1333 if (!strcmp(argv[1], "-nh")) {
1334 par.histograms = false;
1335 argc -= 1;
1336 argv += 1;
1337 } else if (!strcmp(argv[1], "-it") && argc > 2) {
1338 it_hist = strtol(argv[2], NULL, 10);
1339 if (it_hist < 0) {
1340 it_hist = 0;
1341 } else if (it_hist > HISTORY_LEN) {
1342 it_hist = HISTORY_LEN;
1343 }
1344 argc -= 2;
1345 argv += 2;
1346 } else if (!strcmp(argv[1], "-ms") && argc > 2) {
1347 ms_hist = strtol(argv[2], NULL, 10);
1348 if (ms_hist < 0) {
1349 ms_hist = 0;
1350 } else if (ms_hist > HISTORY_LEN) {
1351 ms_hist = HISTORY_LEN;
1352 }
1353 argc -= 2;
1354 argv += 2;
1355 } else {
1356 break;
1357 }
1358 }
1359 par.iter_hist_len = it_hist;
1360 par.ms_hist_len = ms_hist;
1361 par.command_type = PMD_INFO_PERF_SHOW;
1362 dpif_netdev_pmd_info(conn, argc, argv, &par);
1363 }
1364 \f
1365 static int
1366 dpif_netdev_init(void)
1367 {
1368 static enum pmd_info_type show_aux = PMD_INFO_SHOW_STATS,
1369 clear_aux = PMD_INFO_CLEAR_STATS,
1370 poll_aux = PMD_INFO_SHOW_RXQ;
1371
1372 unixctl_command_register("dpif-netdev/pmd-stats-show", "[-pmd core] [dp]",
1373 0, 3, dpif_netdev_pmd_info,
1374 (void *)&show_aux);
1375 unixctl_command_register("dpif-netdev/pmd-stats-clear", "[-pmd core] [dp]",
1376 0, 3, dpif_netdev_pmd_info,
1377 (void *)&clear_aux);
1378 unixctl_command_register("dpif-netdev/pmd-rxq-show", "[-pmd core] [dp]",
1379 0, 3, dpif_netdev_pmd_info,
1380 (void *)&poll_aux);
1381 unixctl_command_register("dpif-netdev/pmd-perf-show",
1382 "[-nh] [-it iter-history-len]"
1383 " [-ms ms-history-len]"
1384 " [-pmd core] [dp]",
1385 0, 8, pmd_perf_show_cmd,
1386 NULL);
1387 unixctl_command_register("dpif-netdev/pmd-rxq-rebalance", "[dp]",
1388 0, 1, dpif_netdev_pmd_rebalance,
1389 NULL);
1390 unixctl_command_register("dpif-netdev/pmd-perf-log-set",
1391 "on|off [-b before] [-a after] [-e|-ne] "
1392 "[-us usec] [-q qlen]",
1393 0, 10, pmd_perf_log_set_cmd,
1394 NULL);
1395 return 0;
1396 }
1397
1398 static int
1399 dpif_netdev_enumerate(struct sset *all_dps,
1400 const struct dpif_class *dpif_class)
1401 {
1402 struct shash_node *node;
1403
1404 ovs_mutex_lock(&dp_netdev_mutex);
1405 SHASH_FOR_EACH(node, &dp_netdevs) {
1406 struct dp_netdev *dp = node->data;
1407 if (dpif_class != dp->class) {
1408 /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs.
1409 * If the class doesn't match, skip this dpif. */
1410 continue;
1411 }
1412 sset_add(all_dps, node->name);
1413 }
1414 ovs_mutex_unlock(&dp_netdev_mutex);
1415
1416 return 0;
1417 }
1418
1419 static bool
1420 dpif_netdev_class_is_dummy(const struct dpif_class *class)
1421 {
1422 return class != &dpif_netdev_class;
1423 }
1424
1425 static const char *
1426 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
1427 {
1428 return strcmp(type, "internal") ? type
1429 : dpif_netdev_class_is_dummy(class) ? "dummy-internal"
1430 : "tap";
1431 }
1432
1433 static struct dpif *
1434 create_dpif_netdev(struct dp_netdev *dp)
1435 {
1436 uint16_t netflow_id = hash_string(dp->name, 0);
1437 struct dpif_netdev *dpif;
1438
1439 ovs_refcount_ref(&dp->ref_cnt);
1440
1441 dpif = xmalloc(sizeof *dpif);
1442 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
1443 dpif->dp = dp;
1444 dpif->last_port_seq = seq_read(dp->port_seq);
1445
1446 return &dpif->dpif;
1447 }
1448
1449 /* Choose an unused, non-zero port number and return it on success.
1450 * Return ODPP_NONE on failure. */
1451 static odp_port_t
1452 choose_port(struct dp_netdev *dp, const char *name)
1453 OVS_REQUIRES(dp->port_mutex)
1454 {
1455 uint32_t port_no;
1456
1457 if (dp->class != &dpif_netdev_class) {
1458 const char *p;
1459 int start_no = 0;
1460
1461 /* If the port name begins with "br", start the number search at
1462 * 100 to make writing tests easier. */
1463 if (!strncmp(name, "br", 2)) {
1464 start_no = 100;
1465 }
1466
1467 /* If the port name contains a number, try to assign that port number.
1468 * This can make writing unit tests easier because port numbers are
1469 * predictable. */
1470 for (p = name; *p != '\0'; p++) {
1471 if (isdigit((unsigned char) *p)) {
1472 port_no = start_no + strtol(p, NULL, 10);
1473 if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE)
1474 && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
1475 return u32_to_odp(port_no);
1476 }
1477 break;
1478 }
1479 }
1480 }
1481
1482 for (port_no = 1; port_no <= UINT16_MAX; port_no++) {
1483 if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
1484 return u32_to_odp(port_no);
1485 }
1486 }
1487
1488 return ODPP_NONE;
1489 }
1490
1491 static int
1492 create_dp_netdev(const char *name, const struct dpif_class *class,
1493 struct dp_netdev **dpp)
1494 OVS_REQUIRES(dp_netdev_mutex)
1495 {
1496 struct dp_netdev *dp;
1497 int error;
1498
1499 dp = xzalloc(sizeof *dp);
1500 shash_add(&dp_netdevs, name, dp);
1501
1502 *CONST_CAST(const struct dpif_class **, &dp->class) = class;
1503 *CONST_CAST(const char **, &dp->name) = xstrdup(name);
1504 ovs_refcount_init(&dp->ref_cnt);
1505 atomic_flag_clear(&dp->destroyed);
1506
1507 ovs_mutex_init(&dp->port_mutex);
1508 hmap_init(&dp->ports);
1509 dp->port_seq = seq_create();
1510 fat_rwlock_init(&dp->upcall_rwlock);
1511
1512 dp->reconfigure_seq = seq_create();
1513 dp->last_reconfigure_seq = seq_read(dp->reconfigure_seq);
1514
1515 for (int i = 0; i < N_METER_LOCKS; ++i) {
1516 ovs_mutex_init_adaptive(&dp->meter_locks[i]);
1517 }
1518
1519 /* Disable upcalls by default. */
1520 dp_netdev_disable_upcall(dp);
1521 dp->upcall_aux = NULL;
1522 dp->upcall_cb = NULL;
1523
1524 dp->conntrack = conntrack_init();
1525
1526 atomic_init(&dp->emc_insert_min, DEFAULT_EM_FLOW_INSERT_MIN);
1527 atomic_init(&dp->tx_flush_interval, DEFAULT_TX_FLUSH_INTERVAL);
1528
1529 cmap_init(&dp->poll_threads);
1530 dp->pmd_rxq_assign_cyc = true;
1531
1532 ovs_mutex_init(&dp->tx_qid_pool_mutex);
1533 /* We need 1 Tx queue for each possible core + 1 for non-PMD threads. */
1534 dp->tx_qid_pool = id_pool_create(0, ovs_numa_get_n_cores() + 1);
1535
1536 ovs_mutex_init_recursive(&dp->non_pmd_mutex);
1537 ovsthread_key_create(&dp->per_pmd_key, NULL);
1538
1539 ovs_mutex_lock(&dp->port_mutex);
1540 /* non-PMD will be created before all other threads and will
1541 * allocate static_tx_qid = 0. */
1542 dp_netdev_set_nonpmd(dp);
1543
1544 error = do_add_port(dp, name, dpif_netdev_port_open_type(dp->class,
1545 "internal"),
1546 ODPP_LOCAL);
1547 ovs_mutex_unlock(&dp->port_mutex);
1548 if (error) {
1549 dp_netdev_free(dp);
1550 return error;
1551 }
1552
1553 dp->last_tnl_conf_seq = seq_read(tnl_conf_seq);
1554 *dpp = dp;
1555 return 0;
1556 }
1557
1558 static void
1559 dp_netdev_request_reconfigure(struct dp_netdev *dp)
1560 {
1561 seq_change(dp->reconfigure_seq);
1562 }
1563
1564 static bool
1565 dp_netdev_is_reconf_required(struct dp_netdev *dp)
1566 {
1567 return seq_read(dp->reconfigure_seq) != dp->last_reconfigure_seq;
1568 }
1569
1570 static int
1571 dpif_netdev_open(const struct dpif_class *class, const char *name,
1572 bool create, struct dpif **dpifp)
1573 {
1574 struct dp_netdev *dp;
1575 int error;
1576
1577 ovs_mutex_lock(&dp_netdev_mutex);
1578 dp = shash_find_data(&dp_netdevs, name);
1579 if (!dp) {
1580 error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
1581 } else {
1582 error = (dp->class != class ? EINVAL
1583 : create ? EEXIST
1584 : 0);
1585 }
1586 if (!error) {
1587 *dpifp = create_dpif_netdev(dp);
1588 dp->dpif = *dpifp;
1589 }
1590 ovs_mutex_unlock(&dp_netdev_mutex);
1591
1592 return error;
1593 }
1594
1595 static void
1596 dp_netdev_destroy_upcall_lock(struct dp_netdev *dp)
1597 OVS_NO_THREAD_SAFETY_ANALYSIS
1598 {
1599 /* Check that upcalls are disabled, i.e. that the rwlock is taken */
1600 ovs_assert(fat_rwlock_tryrdlock(&dp->upcall_rwlock));
1601
1602 /* Before freeing a lock we should release it */
1603 fat_rwlock_unlock(&dp->upcall_rwlock);
1604 fat_rwlock_destroy(&dp->upcall_rwlock);
1605 }
1606
1607 static void
1608 dp_delete_meter(struct dp_netdev *dp, uint32_t meter_id)
1609 OVS_REQUIRES(dp->meter_locks[meter_id % N_METER_LOCKS])
1610 {
1611 if (dp->meters[meter_id]) {
1612 free(dp->meters[meter_id]);
1613 dp->meters[meter_id] = NULL;
1614 }
1615 }
1616
1617 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
1618 * through the 'dp_netdevs' shash while freeing 'dp'. */
1619 static void
1620 dp_netdev_free(struct dp_netdev *dp)
1621 OVS_REQUIRES(dp_netdev_mutex)
1622 {
1623 struct dp_netdev_port *port, *next;
1624
1625 shash_find_and_delete(&dp_netdevs, dp->name);
1626
1627 ovs_mutex_lock(&dp->port_mutex);
1628 HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) {
1629 do_del_port(dp, port);
1630 }
1631 ovs_mutex_unlock(&dp->port_mutex);
1632
1633 dp_netdev_destroy_all_pmds(dp, true);
1634 cmap_destroy(&dp->poll_threads);
1635
1636 ovs_mutex_destroy(&dp->tx_qid_pool_mutex);
1637 id_pool_destroy(dp->tx_qid_pool);
1638
1639 ovs_mutex_destroy(&dp->non_pmd_mutex);
1640 ovsthread_key_delete(dp->per_pmd_key);
1641
1642 conntrack_destroy(dp->conntrack);
1643
1644
1645 seq_destroy(dp->reconfigure_seq);
1646
1647 seq_destroy(dp->port_seq);
1648 hmap_destroy(&dp->ports);
1649 ovs_mutex_destroy(&dp->port_mutex);
1650
1651 /* Upcalls must be disabled at this point */
1652 dp_netdev_destroy_upcall_lock(dp);
1653
1654 int i;
1655
1656 for (i = 0; i < MAX_METERS; ++i) {
1657 meter_lock(dp, i);
1658 dp_delete_meter(dp, i);
1659 meter_unlock(dp, i);
1660 }
1661 for (i = 0; i < N_METER_LOCKS; ++i) {
1662 ovs_mutex_destroy(&dp->meter_locks[i]);
1663 }
1664
1665 free(dp->pmd_cmask);
1666 free(CONST_CAST(char *, dp->name));
1667 free(dp);
1668 }
1669
1670 static void
1671 dp_netdev_unref(struct dp_netdev *dp)
1672 {
1673 if (dp) {
1674 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
1675 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
1676 ovs_mutex_lock(&dp_netdev_mutex);
1677 if (ovs_refcount_unref_relaxed(&dp->ref_cnt) == 1) {
1678 dp_netdev_free(dp);
1679 }
1680 ovs_mutex_unlock(&dp_netdev_mutex);
1681 }
1682 }
1683
1684 static void
1685 dpif_netdev_close(struct dpif *dpif)
1686 {
1687 struct dp_netdev *dp = get_dp_netdev(dpif);
1688
1689 dp_netdev_unref(dp);
1690 free(dpif);
1691 }
1692
1693 static int
1694 dpif_netdev_destroy(struct dpif *dpif)
1695 {
1696 struct dp_netdev *dp = get_dp_netdev(dpif);
1697
1698 if (!atomic_flag_test_and_set(&dp->destroyed)) {
1699 if (ovs_refcount_unref_relaxed(&dp->ref_cnt) == 1) {
1700 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
1701 OVS_NOT_REACHED();
1702 }
1703 }
1704
1705 return 0;
1706 }
1707
1708 /* Add 'n' to the atomic variable 'var' non-atomically and using relaxed
1709 * load/store semantics. While the increment is not atomic, the load and
1710 * store operations are, making it impossible to read inconsistent values.
1711 *
1712 * This is used to update thread local stats counters. */
1713 static void
1714 non_atomic_ullong_add(atomic_ullong *var, unsigned long long n)
1715 {
1716 unsigned long long tmp;
1717
1718 atomic_read_relaxed(var, &tmp);
1719 tmp += n;
1720 atomic_store_relaxed(var, tmp);
1721 }
1722
1723 static int
1724 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
1725 {
1726 struct dp_netdev *dp = get_dp_netdev(dpif);
1727 struct dp_netdev_pmd_thread *pmd;
1728 uint64_t pmd_stats[PMD_N_STATS];
1729
1730 stats->n_flows = stats->n_hit = stats->n_missed = stats->n_lost = 0;
1731 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
1732 stats->n_flows += cmap_count(&pmd->flow_table);
1733 pmd_perf_read_counters(&pmd->perf_stats, pmd_stats);
1734 stats->n_hit += pmd_stats[PMD_STAT_EXACT_HIT];
1735 stats->n_hit += pmd_stats[PMD_STAT_SMC_HIT];
1736 stats->n_hit += pmd_stats[PMD_STAT_MASKED_HIT];
1737 stats->n_missed += pmd_stats[PMD_STAT_MISS];
1738 stats->n_lost += pmd_stats[PMD_STAT_LOST];
1739 }
1740 stats->n_masks = UINT32_MAX;
1741 stats->n_mask_hit = UINT64_MAX;
1742
1743 return 0;
1744 }
1745
1746 static void
1747 dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread *pmd)
1748 {
1749 if (pmd->core_id == NON_PMD_CORE_ID) {
1750 ovs_mutex_lock(&pmd->dp->non_pmd_mutex);
1751 ovs_mutex_lock(&pmd->port_mutex);
1752 pmd_load_cached_ports(pmd);
1753 ovs_mutex_unlock(&pmd->port_mutex);
1754 ovs_mutex_unlock(&pmd->dp->non_pmd_mutex);
1755 return;
1756 }
1757
1758 seq_change(pmd->reload_seq);
1759 atomic_store_explicit(&pmd->reload, true, memory_order_release);
1760 }
1761
1762 static uint32_t
1763 hash_port_no(odp_port_t port_no)
1764 {
1765 return hash_int(odp_to_u32(port_no), 0);
1766 }
1767
1768 static int
1769 port_create(const char *devname, const char *type,
1770 odp_port_t port_no, struct dp_netdev_port **portp)
1771 {
1772 struct netdev_saved_flags *sf;
1773 struct dp_netdev_port *port;
1774 enum netdev_flags flags;
1775 struct netdev *netdev;
1776 int error;
1777
1778 *portp = NULL;
1779
1780 /* Open and validate network device. */
1781 error = netdev_open(devname, type, &netdev);
1782 if (error) {
1783 return error;
1784 }
1785 /* XXX reject non-Ethernet devices */
1786
1787 netdev_get_flags(netdev, &flags);
1788 if (flags & NETDEV_LOOPBACK) {
1789 VLOG_ERR("%s: cannot add a loopback device", devname);
1790 error = EINVAL;
1791 goto out;
1792 }
1793
1794 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
1795 if (error) {
1796 VLOG_ERR("%s: cannot set promisc flag", devname);
1797 goto out;
1798 }
1799
1800 port = xzalloc(sizeof *port);
1801 port->port_no = port_no;
1802 port->netdev = netdev;
1803 port->type = xstrdup(type);
1804 port->sf = sf;
1805 port->emc_enabled = true;
1806 port->need_reconfigure = true;
1807 ovs_mutex_init(&port->txq_used_mutex);
1808
1809 *portp = port;
1810
1811 return 0;
1812
1813 out:
1814 netdev_close(netdev);
1815 return error;
1816 }
1817
1818 static int
1819 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
1820 odp_port_t port_no)
1821 OVS_REQUIRES(dp->port_mutex)
1822 {
1823 struct dp_netdev_port *port;
1824 int error;
1825
1826 /* Reject devices already in 'dp'. */
1827 if (!get_port_by_name(dp, devname, &port)) {
1828 return EEXIST;
1829 }
1830
1831 error = port_create(devname, type, port_no, &port);
1832 if (error) {
1833 return error;
1834 }
1835
1836 hmap_insert(&dp->ports, &port->node, hash_port_no(port_no));
1837 seq_change(dp->port_seq);
1838
1839 reconfigure_datapath(dp);
1840
1841 return 0;
1842 }
1843
1844 static int
1845 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
1846 odp_port_t *port_nop)
1847 {
1848 struct dp_netdev *dp = get_dp_netdev(dpif);
1849 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1850 const char *dpif_port;
1851 odp_port_t port_no;
1852 int error;
1853
1854 ovs_mutex_lock(&dp->port_mutex);
1855 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1856 if (*port_nop != ODPP_NONE) {
1857 port_no = *port_nop;
1858 error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0;
1859 } else {
1860 port_no = choose_port(dp, dpif_port);
1861 error = port_no == ODPP_NONE ? EFBIG : 0;
1862 }
1863 if (!error) {
1864 *port_nop = port_no;
1865 error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
1866 }
1867 ovs_mutex_unlock(&dp->port_mutex);
1868
1869 return error;
1870 }
1871
1872 static int
1873 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
1874 {
1875 struct dp_netdev *dp = get_dp_netdev(dpif);
1876 int error;
1877
1878 ovs_mutex_lock(&dp->port_mutex);
1879 if (port_no == ODPP_LOCAL) {
1880 error = EINVAL;
1881 } else {
1882 struct dp_netdev_port *port;
1883
1884 error = get_port_by_number(dp, port_no, &port);
1885 if (!error) {
1886 do_del_port(dp, port);
1887 }
1888 }
1889 ovs_mutex_unlock(&dp->port_mutex);
1890
1891 return error;
1892 }
1893
1894 static bool
1895 is_valid_port_number(odp_port_t port_no)
1896 {
1897 return port_no != ODPP_NONE;
1898 }
1899
1900 static struct dp_netdev_port *
1901 dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no)
1902 OVS_REQUIRES(dp->port_mutex)
1903 {
1904 struct dp_netdev_port *port;
1905
1906 HMAP_FOR_EACH_WITH_HASH (port, node, hash_port_no(port_no), &dp->ports) {
1907 if (port->port_no == port_no) {
1908 return port;
1909 }
1910 }
1911 return NULL;
1912 }
1913
1914 static int
1915 get_port_by_number(struct dp_netdev *dp,
1916 odp_port_t port_no, struct dp_netdev_port **portp)
1917 OVS_REQUIRES(dp->port_mutex)
1918 {
1919 if (!is_valid_port_number(port_no)) {
1920 *portp = NULL;
1921 return EINVAL;
1922 } else {
1923 *portp = dp_netdev_lookup_port(dp, port_no);
1924 return *portp ? 0 : ENODEV;
1925 }
1926 }
1927
1928 static void
1929 port_destroy(struct dp_netdev_port *port)
1930 {
1931 if (!port) {
1932 return;
1933 }
1934
1935 netdev_close(port->netdev);
1936 netdev_restore_flags(port->sf);
1937
1938 for (unsigned i = 0; i < port->n_rxq; i++) {
1939 netdev_rxq_close(port->rxqs[i].rx);
1940 }
1941 ovs_mutex_destroy(&port->txq_used_mutex);
1942 free(port->rxq_affinity_list);
1943 free(port->txq_used);
1944 free(port->rxqs);
1945 free(port->type);
1946 free(port);
1947 }
1948
1949 static int
1950 get_port_by_name(struct dp_netdev *dp,
1951 const char *devname, struct dp_netdev_port **portp)
1952 OVS_REQUIRES(dp->port_mutex)
1953 {
1954 struct dp_netdev_port *port;
1955
1956 HMAP_FOR_EACH (port, node, &dp->ports) {
1957 if (!strcmp(netdev_get_name(port->netdev), devname)) {
1958 *portp = port;
1959 return 0;
1960 }
1961 }
1962
1963 /* Callers of dpif_netdev_port_query_by_name() expect ENODEV for a non
1964 * existing port. */
1965 return ENODEV;
1966 }
1967
1968 /* Returns 'true' if there is a port with pmd netdev. */
1969 static bool
1970 has_pmd_port(struct dp_netdev *dp)
1971 OVS_REQUIRES(dp->port_mutex)
1972 {
1973 struct dp_netdev_port *port;
1974
1975 HMAP_FOR_EACH (port, node, &dp->ports) {
1976 if (netdev_is_pmd(port->netdev)) {
1977 return true;
1978 }
1979 }
1980
1981 return false;
1982 }
1983
1984 static void
1985 do_del_port(struct dp_netdev *dp, struct dp_netdev_port *port)
1986 OVS_REQUIRES(dp->port_mutex)
1987 {
1988 hmap_remove(&dp->ports, &port->node);
1989 seq_change(dp->port_seq);
1990
1991 reconfigure_datapath(dp);
1992
1993 port_destroy(port);
1994 }
1995
1996 static void
1997 answer_port_query(const struct dp_netdev_port *port,
1998 struct dpif_port *dpif_port)
1999 {
2000 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
2001 dpif_port->type = xstrdup(port->type);
2002 dpif_port->port_no = port->port_no;
2003 }
2004
2005 static int
2006 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
2007 struct dpif_port *dpif_port)
2008 {
2009 struct dp_netdev *dp = get_dp_netdev(dpif);
2010 struct dp_netdev_port *port;
2011 int error;
2012
2013 ovs_mutex_lock(&dp->port_mutex);
2014 error = get_port_by_number(dp, port_no, &port);
2015 if (!error && dpif_port) {
2016 answer_port_query(port, dpif_port);
2017 }
2018 ovs_mutex_unlock(&dp->port_mutex);
2019
2020 return error;
2021 }
2022
2023 static int
2024 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
2025 struct dpif_port *dpif_port)
2026 {
2027 struct dp_netdev *dp = get_dp_netdev(dpif);
2028 struct dp_netdev_port *port;
2029 int error;
2030
2031 ovs_mutex_lock(&dp->port_mutex);
2032 error = get_port_by_name(dp, devname, &port);
2033 if (!error && dpif_port) {
2034 answer_port_query(port, dpif_port);
2035 }
2036 ovs_mutex_unlock(&dp->port_mutex);
2037
2038 return error;
2039 }
2040
2041 static void
2042 dp_netdev_flow_free(struct dp_netdev_flow *flow)
2043 {
2044 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow));
2045 free(flow);
2046 }
2047
2048 static void dp_netdev_flow_unref(struct dp_netdev_flow *flow)
2049 {
2050 if (ovs_refcount_unref_relaxed(&flow->ref_cnt) == 1) {
2051 ovsrcu_postpone(dp_netdev_flow_free, flow);
2052 }
2053 }
2054
2055 static uint32_t
2056 dp_netdev_flow_hash(const ovs_u128 *ufid)
2057 {
2058 return ufid->u32[0];
2059 }
2060
2061 static inline struct dpcls *
2062 dp_netdev_pmd_lookup_dpcls(struct dp_netdev_pmd_thread *pmd,
2063 odp_port_t in_port)
2064 {
2065 struct dpcls *cls;
2066 uint32_t hash = hash_port_no(in_port);
2067 CMAP_FOR_EACH_WITH_HASH (cls, node, hash, &pmd->classifiers) {
2068 if (cls->in_port == in_port) {
2069 /* Port classifier exists already */
2070 return cls;
2071 }
2072 }
2073 return NULL;
2074 }
2075
2076 static inline struct dpcls *
2077 dp_netdev_pmd_find_dpcls(struct dp_netdev_pmd_thread *pmd,
2078 odp_port_t in_port)
2079 OVS_REQUIRES(pmd->flow_mutex)
2080 {
2081 struct dpcls *cls = dp_netdev_pmd_lookup_dpcls(pmd, in_port);
2082 uint32_t hash = hash_port_no(in_port);
2083
2084 if (!cls) {
2085 /* Create new classifier for in_port */
2086 cls = xmalloc(sizeof(*cls));
2087 dpcls_init(cls);
2088 cls->in_port = in_port;
2089 cmap_insert(&pmd->classifiers, &cls->node, hash);
2090 VLOG_DBG("Creating dpcls %p for in_port %d", cls, in_port);
2091 }
2092 return cls;
2093 }
2094
2095 #define MAX_FLOW_MARK (UINT32_MAX - 1)
2096 #define INVALID_FLOW_MARK (UINT32_MAX)
2097
2098 struct megaflow_to_mark_data {
2099 const struct cmap_node node;
2100 ovs_u128 mega_ufid;
2101 uint32_t mark;
2102 };
2103
2104 struct flow_mark {
2105 struct cmap megaflow_to_mark;
2106 struct cmap mark_to_flow;
2107 struct id_pool *pool;
2108 };
2109
2110 static struct flow_mark flow_mark = {
2111 .megaflow_to_mark = CMAP_INITIALIZER,
2112 .mark_to_flow = CMAP_INITIALIZER,
2113 };
2114
2115 static uint32_t
2116 flow_mark_alloc(void)
2117 {
2118 uint32_t mark;
2119
2120 if (!flow_mark.pool) {
2121 /* Haven't initiated yet, do it here */
2122 flow_mark.pool = id_pool_create(0, MAX_FLOW_MARK);
2123 }
2124
2125 if (id_pool_alloc_id(flow_mark.pool, &mark)) {
2126 return mark;
2127 }
2128
2129 return INVALID_FLOW_MARK;
2130 }
2131
2132 static void
2133 flow_mark_free(uint32_t mark)
2134 {
2135 id_pool_free_id(flow_mark.pool, mark);
2136 }
2137
2138 /* associate megaflow with a mark, which is a 1:1 mapping */
2139 static void
2140 megaflow_to_mark_associate(const ovs_u128 *mega_ufid, uint32_t mark)
2141 {
2142 size_t hash = dp_netdev_flow_hash(mega_ufid);
2143 struct megaflow_to_mark_data *data = xzalloc(sizeof(*data));
2144
2145 data->mega_ufid = *mega_ufid;
2146 data->mark = mark;
2147
2148 cmap_insert(&flow_mark.megaflow_to_mark,
2149 CONST_CAST(struct cmap_node *, &data->node), hash);
2150 }
2151
2152 /* disassociate meagaflow with a mark */
2153 static void
2154 megaflow_to_mark_disassociate(const ovs_u128 *mega_ufid)
2155 {
2156 size_t hash = dp_netdev_flow_hash(mega_ufid);
2157 struct megaflow_to_mark_data *data;
2158
2159 CMAP_FOR_EACH_WITH_HASH (data, node, hash, &flow_mark.megaflow_to_mark) {
2160 if (ovs_u128_equals(*mega_ufid, data->mega_ufid)) {
2161 cmap_remove(&flow_mark.megaflow_to_mark,
2162 CONST_CAST(struct cmap_node *, &data->node), hash);
2163 ovsrcu_postpone(free, data);
2164 return;
2165 }
2166 }
2167
2168 VLOG_WARN("Masked ufid "UUID_FMT" is not associated with a mark?\n",
2169 UUID_ARGS((struct uuid *)mega_ufid));
2170 }
2171
2172 static inline uint32_t
2173 megaflow_to_mark_find(const ovs_u128 *mega_ufid)
2174 {
2175 size_t hash = dp_netdev_flow_hash(mega_ufid);
2176 struct megaflow_to_mark_data *data;
2177
2178 CMAP_FOR_EACH_WITH_HASH (data, node, hash, &flow_mark.megaflow_to_mark) {
2179 if (ovs_u128_equals(*mega_ufid, data->mega_ufid)) {
2180 return data->mark;
2181 }
2182 }
2183
2184 VLOG_DBG("Mark id for ufid "UUID_FMT" was not found\n",
2185 UUID_ARGS((struct uuid *)mega_ufid));
2186 return INVALID_FLOW_MARK;
2187 }
2188
2189 /* associate mark with a flow, which is 1:N mapping */
2190 static void
2191 mark_to_flow_associate(const uint32_t mark, struct dp_netdev_flow *flow)
2192 {
2193 dp_netdev_flow_ref(flow);
2194
2195 cmap_insert(&flow_mark.mark_to_flow,
2196 CONST_CAST(struct cmap_node *, &flow->mark_node),
2197 hash_int(mark, 0));
2198 flow->mark = mark;
2199
2200 VLOG_DBG("Associated dp_netdev flow %p with mark %u\n", flow, mark);
2201 }
2202
2203 static bool
2204 flow_mark_has_no_ref(uint32_t mark)
2205 {
2206 struct dp_netdev_flow *flow;
2207
2208 CMAP_FOR_EACH_WITH_HASH (flow, mark_node, hash_int(mark, 0),
2209 &flow_mark.mark_to_flow) {
2210 if (flow->mark == mark) {
2211 return false;
2212 }
2213 }
2214
2215 return true;
2216 }
2217
2218 static int
2219 mark_to_flow_disassociate(struct dp_netdev_pmd_thread *pmd,
2220 struct dp_netdev_flow *flow)
2221 {
2222 int ret = 0;
2223 uint32_t mark = flow->mark;
2224 struct cmap_node *mark_node = CONST_CAST(struct cmap_node *,
2225 &flow->mark_node);
2226
2227 cmap_remove(&flow_mark.mark_to_flow, mark_node, hash_int(mark, 0));
2228 flow->mark = INVALID_FLOW_MARK;
2229
2230 /*
2231 * no flow is referencing the mark any more? If so, let's
2232 * remove the flow from hardware and free the mark.
2233 */
2234 if (flow_mark_has_no_ref(mark)) {
2235 struct dp_netdev_port *port;
2236 odp_port_t in_port = flow->flow.in_port.odp_port;
2237
2238 ovs_mutex_lock(&pmd->dp->port_mutex);
2239 port = dp_netdev_lookup_port(pmd->dp, in_port);
2240 if (port) {
2241 ret = netdev_flow_del(port->netdev, &flow->mega_ufid, NULL);
2242 }
2243 ovs_mutex_unlock(&pmd->dp->port_mutex);
2244
2245 flow_mark_free(mark);
2246 VLOG_DBG("Freed flow mark %u\n", mark);
2247
2248 megaflow_to_mark_disassociate(&flow->mega_ufid);
2249 }
2250 dp_netdev_flow_unref(flow);
2251
2252 return ret;
2253 }
2254
2255 static void
2256 flow_mark_flush(struct dp_netdev_pmd_thread *pmd)
2257 {
2258 struct dp_netdev_flow *flow;
2259
2260 CMAP_FOR_EACH (flow, mark_node, &flow_mark.mark_to_flow) {
2261 if (flow->pmd_id == pmd->core_id) {
2262 queue_netdev_flow_del(pmd, flow);
2263 }
2264 }
2265 }
2266
2267 static struct dp_netdev_flow *
2268 mark_to_flow_find(const struct dp_netdev_pmd_thread *pmd,
2269 const uint32_t mark)
2270 {
2271 struct dp_netdev_flow *flow;
2272
2273 CMAP_FOR_EACH_WITH_HASH (flow, mark_node, hash_int(mark, 0),
2274 &flow_mark.mark_to_flow) {
2275 if (flow->mark == mark && flow->pmd_id == pmd->core_id &&
2276 flow->dead == false) {
2277 return flow;
2278 }
2279 }
2280
2281 return NULL;
2282 }
2283
2284 static struct dp_flow_offload_item *
2285 dp_netdev_alloc_flow_offload(struct dp_netdev_pmd_thread *pmd,
2286 struct dp_netdev_flow *flow,
2287 int op)
2288 {
2289 struct dp_flow_offload_item *offload;
2290
2291 offload = xzalloc(sizeof(*offload));
2292 offload->pmd = pmd;
2293 offload->flow = flow;
2294 offload->op = op;
2295
2296 dp_netdev_flow_ref(flow);
2297 dp_netdev_pmd_try_ref(pmd);
2298
2299 return offload;
2300 }
2301
2302 static void
2303 dp_netdev_free_flow_offload(struct dp_flow_offload_item *offload)
2304 {
2305 dp_netdev_pmd_unref(offload->pmd);
2306 dp_netdev_flow_unref(offload->flow);
2307
2308 free(offload->actions);
2309 free(offload);
2310 }
2311
2312 static void
2313 dp_netdev_append_flow_offload(struct dp_flow_offload_item *offload)
2314 {
2315 ovs_mutex_lock(&dp_flow_offload.mutex);
2316 ovs_list_push_back(&dp_flow_offload.list, &offload->node);
2317 xpthread_cond_signal(&dp_flow_offload.cond);
2318 ovs_mutex_unlock(&dp_flow_offload.mutex);
2319 }
2320
2321 static int
2322 dp_netdev_flow_offload_del(struct dp_flow_offload_item *offload)
2323 {
2324 return mark_to_flow_disassociate(offload->pmd, offload->flow);
2325 }
2326
2327 /*
2328 * There are two flow offload operations here: addition and modification.
2329 *
2330 * For flow addition, this function does:
2331 * - allocate a new flow mark id
2332 * - perform hardware flow offload
2333 * - associate the flow mark with flow and mega flow
2334 *
2335 * For flow modification, both flow mark and the associations are still
2336 * valid, thus only item 2 needed.
2337 */
2338 static int
2339 dp_netdev_flow_offload_put(struct dp_flow_offload_item *offload)
2340 {
2341 struct dp_netdev_port *port;
2342 struct dp_netdev_pmd_thread *pmd = offload->pmd;
2343 struct dp_netdev_flow *flow = offload->flow;
2344 odp_port_t in_port = flow->flow.in_port.odp_port;
2345 bool modification = offload->op == DP_NETDEV_FLOW_OFFLOAD_OP_MOD;
2346 struct offload_info info;
2347 uint32_t mark;
2348 int ret;
2349
2350 if (flow->dead) {
2351 return -1;
2352 }
2353
2354 if (modification) {
2355 mark = flow->mark;
2356 ovs_assert(mark != INVALID_FLOW_MARK);
2357 } else {
2358 /*
2359 * If a mega flow has already been offloaded (from other PMD
2360 * instances), do not offload it again.
2361 */
2362 mark = megaflow_to_mark_find(&flow->mega_ufid);
2363 if (mark != INVALID_FLOW_MARK) {
2364 VLOG_DBG("Flow has already been offloaded with mark %u\n", mark);
2365 if (flow->mark != INVALID_FLOW_MARK) {
2366 ovs_assert(flow->mark == mark);
2367 } else {
2368 mark_to_flow_associate(mark, flow);
2369 }
2370 return 0;
2371 }
2372
2373 mark = flow_mark_alloc();
2374 if (mark == INVALID_FLOW_MARK) {
2375 VLOG_ERR("Failed to allocate flow mark!\n");
2376 }
2377 }
2378 info.flow_mark = mark;
2379
2380 ovs_mutex_lock(&pmd->dp->port_mutex);
2381 port = dp_netdev_lookup_port(pmd->dp, in_port);
2382 if (!port || netdev_vport_is_vport_class(port->netdev->netdev_class)) {
2383 ovs_mutex_unlock(&pmd->dp->port_mutex);
2384 goto err_free;
2385 }
2386 ret = netdev_flow_put(port->netdev, &offload->match,
2387 CONST_CAST(struct nlattr *, offload->actions),
2388 offload->actions_len, &flow->mega_ufid, &info,
2389 NULL);
2390 ovs_mutex_unlock(&pmd->dp->port_mutex);
2391
2392 if (ret) {
2393 goto err_free;
2394 }
2395
2396 if (!modification) {
2397 megaflow_to_mark_associate(&flow->mega_ufid, mark);
2398 mark_to_flow_associate(mark, flow);
2399 }
2400 return 0;
2401
2402 err_free:
2403 if (!modification) {
2404 flow_mark_free(mark);
2405 } else {
2406 mark_to_flow_disassociate(pmd, flow);
2407 }
2408 return -1;
2409 }
2410
2411 static void *
2412 dp_netdev_flow_offload_main(void *data OVS_UNUSED)
2413 {
2414 struct dp_flow_offload_item *offload;
2415 struct ovs_list *list;
2416 const char *op;
2417 int ret;
2418
2419 for (;;) {
2420 ovs_mutex_lock(&dp_flow_offload.mutex);
2421 if (ovs_list_is_empty(&dp_flow_offload.list)) {
2422 ovsrcu_quiesce_start();
2423 ovs_mutex_cond_wait(&dp_flow_offload.cond,
2424 &dp_flow_offload.mutex);
2425 ovsrcu_quiesce_end();
2426 }
2427 list = ovs_list_pop_front(&dp_flow_offload.list);
2428 offload = CONTAINER_OF(list, struct dp_flow_offload_item, node);
2429 ovs_mutex_unlock(&dp_flow_offload.mutex);
2430
2431 switch (offload->op) {
2432 case DP_NETDEV_FLOW_OFFLOAD_OP_ADD:
2433 op = "add";
2434 ret = dp_netdev_flow_offload_put(offload);
2435 break;
2436 case DP_NETDEV_FLOW_OFFLOAD_OP_MOD:
2437 op = "modify";
2438 ret = dp_netdev_flow_offload_put(offload);
2439 break;
2440 case DP_NETDEV_FLOW_OFFLOAD_OP_DEL:
2441 op = "delete";
2442 ret = dp_netdev_flow_offload_del(offload);
2443 break;
2444 default:
2445 OVS_NOT_REACHED();
2446 }
2447
2448 VLOG_DBG("%s to %s netdev flow\n",
2449 ret == 0 ? "succeed" : "failed", op);
2450 dp_netdev_free_flow_offload(offload);
2451 }
2452
2453 return NULL;
2454 }
2455
2456 static void
2457 queue_netdev_flow_del(struct dp_netdev_pmd_thread *pmd,
2458 struct dp_netdev_flow *flow)
2459 {
2460 struct dp_flow_offload_item *offload;
2461
2462 if (ovsthread_once_start(&offload_thread_once)) {
2463 xpthread_cond_init(&dp_flow_offload.cond, NULL);
2464 ovs_thread_create("dp_netdev_flow_offload",
2465 dp_netdev_flow_offload_main, NULL);
2466 ovsthread_once_done(&offload_thread_once);
2467 }
2468
2469 offload = dp_netdev_alloc_flow_offload(pmd, flow,
2470 DP_NETDEV_FLOW_OFFLOAD_OP_DEL);
2471 dp_netdev_append_flow_offload(offload);
2472 }
2473
2474 static void
2475 queue_netdev_flow_put(struct dp_netdev_pmd_thread *pmd,
2476 struct dp_netdev_flow *flow, struct match *match,
2477 const struct nlattr *actions, size_t actions_len)
2478 {
2479 struct dp_flow_offload_item *offload;
2480 int op;
2481
2482 if (!netdev_is_flow_api_enabled()) {
2483 return;
2484 }
2485
2486 if (ovsthread_once_start(&offload_thread_once)) {
2487 xpthread_cond_init(&dp_flow_offload.cond, NULL);
2488 ovs_thread_create("dp_netdev_flow_offload",
2489 dp_netdev_flow_offload_main, NULL);
2490 ovsthread_once_done(&offload_thread_once);
2491 }
2492
2493 if (flow->mark != INVALID_FLOW_MARK) {
2494 op = DP_NETDEV_FLOW_OFFLOAD_OP_MOD;
2495 } else {
2496 op = DP_NETDEV_FLOW_OFFLOAD_OP_ADD;
2497 }
2498 offload = dp_netdev_alloc_flow_offload(pmd, flow, op);
2499 offload->match = *match;
2500 offload->actions = xmalloc(actions_len);
2501 memcpy(offload->actions, actions, actions_len);
2502 offload->actions_len = actions_len;
2503
2504 dp_netdev_append_flow_offload(offload);
2505 }
2506
2507 static void
2508 dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread *pmd,
2509 struct dp_netdev_flow *flow)
2510 OVS_REQUIRES(pmd->flow_mutex)
2511 {
2512 struct cmap_node *node = CONST_CAST(struct cmap_node *, &flow->node);
2513 struct dpcls *cls;
2514 odp_port_t in_port = flow->flow.in_port.odp_port;
2515
2516 cls = dp_netdev_pmd_lookup_dpcls(pmd, in_port);
2517 ovs_assert(cls != NULL);
2518 dpcls_remove(cls, &flow->cr);
2519 cmap_remove(&pmd->flow_table, node, dp_netdev_flow_hash(&flow->ufid));
2520 if (flow->mark != INVALID_FLOW_MARK) {
2521 queue_netdev_flow_del(pmd, flow);
2522 }
2523 flow->dead = true;
2524
2525 dp_netdev_flow_unref(flow);
2526 }
2527
2528 static void
2529 dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread *pmd)
2530 {
2531 struct dp_netdev_flow *netdev_flow;
2532
2533 ovs_mutex_lock(&pmd->flow_mutex);
2534 CMAP_FOR_EACH (netdev_flow, node, &pmd->flow_table) {
2535 dp_netdev_pmd_remove_flow(pmd, netdev_flow);
2536 }
2537 ovs_mutex_unlock(&pmd->flow_mutex);
2538 }
2539
2540 static int
2541 dpif_netdev_flow_flush(struct dpif *dpif)
2542 {
2543 struct dp_netdev *dp = get_dp_netdev(dpif);
2544 struct dp_netdev_pmd_thread *pmd;
2545
2546 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
2547 dp_netdev_pmd_flow_flush(pmd);
2548 }
2549
2550 return 0;
2551 }
2552
2553 struct dp_netdev_port_state {
2554 struct hmap_position position;
2555 char *name;
2556 };
2557
2558 static int
2559 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
2560 {
2561 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
2562 return 0;
2563 }
2564
2565 static int
2566 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
2567 struct dpif_port *dpif_port)
2568 {
2569 struct dp_netdev_port_state *state = state_;
2570 struct dp_netdev *dp = get_dp_netdev(dpif);
2571 struct hmap_node *node;
2572 int retval;
2573
2574 ovs_mutex_lock(&dp->port_mutex);
2575 node = hmap_at_position(&dp->ports, &state->position);
2576 if (node) {
2577 struct dp_netdev_port *port;
2578
2579 port = CONTAINER_OF(node, struct dp_netdev_port, node);
2580
2581 free(state->name);
2582 state->name = xstrdup(netdev_get_name(port->netdev));
2583 dpif_port->name = state->name;
2584 dpif_port->type = port->type;
2585 dpif_port->port_no = port->port_no;
2586
2587 retval = 0;
2588 } else {
2589 retval = EOF;
2590 }
2591 ovs_mutex_unlock(&dp->port_mutex);
2592
2593 return retval;
2594 }
2595
2596 static int
2597 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
2598 {
2599 struct dp_netdev_port_state *state = state_;
2600 free(state->name);
2601 free(state);
2602 return 0;
2603 }
2604
2605 static int
2606 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
2607 {
2608 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
2609 uint64_t new_port_seq;
2610 int error;
2611
2612 new_port_seq = seq_read(dpif->dp->port_seq);
2613 if (dpif->last_port_seq != new_port_seq) {
2614 dpif->last_port_seq = new_port_seq;
2615 error = ENOBUFS;
2616 } else {
2617 error = EAGAIN;
2618 }
2619
2620 return error;
2621 }
2622
2623 static void
2624 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
2625 {
2626 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
2627
2628 seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
2629 }
2630
2631 static struct dp_netdev_flow *
2632 dp_netdev_flow_cast(const struct dpcls_rule *cr)
2633 {
2634 return cr ? CONTAINER_OF(cr, struct dp_netdev_flow, cr) : NULL;
2635 }
2636
2637 static bool dp_netdev_flow_ref(struct dp_netdev_flow *flow)
2638 {
2639 return ovs_refcount_try_ref_rcu(&flow->ref_cnt);
2640 }
2641
2642 /* netdev_flow_key utilities.
2643 *
2644 * netdev_flow_key is basically a miniflow. We use these functions
2645 * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow
2646 * functions (miniflow_clone_inline, miniflow_equal, ...), because:
2647 *
2648 * - Since we are dealing exclusively with miniflows created by
2649 * miniflow_extract(), if the map is different the miniflow is different.
2650 * Therefore we can be faster by comparing the map and the miniflow in a
2651 * single memcmp().
2652 * - These functions can be inlined by the compiler. */
2653
2654 /* Given the number of bits set in miniflow's maps, returns the size of the
2655 * 'netdev_flow_key.mf' */
2656 static inline size_t
2657 netdev_flow_key_size(size_t flow_u64s)
2658 {
2659 return sizeof(struct miniflow) + MINIFLOW_VALUES_SIZE(flow_u64s);
2660 }
2661
2662 static inline bool
2663 netdev_flow_key_equal(const struct netdev_flow_key *a,
2664 const struct netdev_flow_key *b)
2665 {
2666 /* 'b->len' may be not set yet. */
2667 return a->hash == b->hash && !memcmp(&a->mf, &b->mf, a->len);
2668 }
2669
2670 /* Used to compare 'netdev_flow_key' in the exact match cache to a miniflow.
2671 * The maps are compared bitwise, so both 'key->mf' and 'mf' must have been
2672 * generated by miniflow_extract. */
2673 static inline bool
2674 netdev_flow_key_equal_mf(const struct netdev_flow_key *key,
2675 const struct miniflow *mf)
2676 {
2677 return !memcmp(&key->mf, mf, key->len);
2678 }
2679
2680 static inline void
2681 netdev_flow_key_clone(struct netdev_flow_key *dst,
2682 const struct netdev_flow_key *src)
2683 {
2684 memcpy(dst, src,
2685 offsetof(struct netdev_flow_key, mf) + src->len);
2686 }
2687
2688 /* Initialize a netdev_flow_key 'mask' from 'match'. */
2689 static inline void
2690 netdev_flow_mask_init(struct netdev_flow_key *mask,
2691 const struct match *match)
2692 {
2693 uint64_t *dst = miniflow_values(&mask->mf);
2694 struct flowmap fmap;
2695 uint32_t hash = 0;
2696 size_t idx;
2697
2698 /* Only check masks that make sense for the flow. */
2699 flow_wc_map(&match->flow, &fmap);
2700 flowmap_init(&mask->mf.map);
2701
2702 FLOWMAP_FOR_EACH_INDEX(idx, fmap) {
2703 uint64_t mask_u64 = flow_u64_value(&match->wc.masks, idx);
2704
2705 if (mask_u64) {
2706 flowmap_set(&mask->mf.map, idx, 1);
2707 *dst++ = mask_u64;
2708 hash = hash_add64(hash, mask_u64);
2709 }
2710 }
2711
2712 map_t map;
2713
2714 FLOWMAP_FOR_EACH_MAP (map, mask->mf.map) {
2715 hash = hash_add64(hash, map);
2716 }
2717
2718 size_t n = dst - miniflow_get_values(&mask->mf);
2719
2720 mask->hash = hash_finish(hash, n * 8);
2721 mask->len = netdev_flow_key_size(n);
2722 }
2723
2724 /* Initializes 'dst' as a copy of 'flow' masked with 'mask'. */
2725 static inline void
2726 netdev_flow_key_init_masked(struct netdev_flow_key *dst,
2727 const struct flow *flow,
2728 const struct netdev_flow_key *mask)
2729 {
2730 uint64_t *dst_u64 = miniflow_values(&dst->mf);
2731 const uint64_t *mask_u64 = miniflow_get_values(&mask->mf);
2732 uint32_t hash = 0;
2733 uint64_t value;
2734
2735 dst->len = mask->len;
2736 dst->mf = mask->mf; /* Copy maps. */
2737
2738 FLOW_FOR_EACH_IN_MAPS(value, flow, mask->mf.map) {
2739 *dst_u64 = value & *mask_u64++;
2740 hash = hash_add64(hash, *dst_u64++);
2741 }
2742 dst->hash = hash_finish(hash,
2743 (dst_u64 - miniflow_get_values(&dst->mf)) * 8);
2744 }
2745
2746 /* Iterate through netdev_flow_key TNL u64 values specified by 'FLOWMAP'. */
2747 #define NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(VALUE, KEY, FLOWMAP) \
2748 MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, &(KEY)->mf, FLOWMAP)
2749
2750 /* Returns a hash value for the bits of 'key' where there are 1-bits in
2751 * 'mask'. */
2752 static inline uint32_t
2753 netdev_flow_key_hash_in_mask(const struct netdev_flow_key *key,
2754 const struct netdev_flow_key *mask)
2755 {
2756 const uint64_t *p = miniflow_get_values(&mask->mf);
2757 uint32_t hash = 0;
2758 uint64_t value;
2759
2760 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value, key, mask->mf.map) {
2761 hash = hash_add64(hash, value & *p++);
2762 }
2763
2764 return hash_finish(hash, (p - miniflow_get_values(&mask->mf)) * 8);
2765 }
2766
2767 static inline bool
2768 emc_entry_alive(struct emc_entry *ce)
2769 {
2770 return ce->flow && !ce->flow->dead;
2771 }
2772
2773 static void
2774 emc_clear_entry(struct emc_entry *ce)
2775 {
2776 if (ce->flow) {
2777 dp_netdev_flow_unref(ce->flow);
2778 ce->flow = NULL;
2779 }
2780 }
2781
2782 static inline void
2783 emc_change_entry(struct emc_entry *ce, struct dp_netdev_flow *flow,
2784 const struct netdev_flow_key *key)
2785 {
2786 if (ce->flow != flow) {
2787 if (ce->flow) {
2788 dp_netdev_flow_unref(ce->flow);
2789 }
2790
2791 if (dp_netdev_flow_ref(flow)) {
2792 ce->flow = flow;
2793 } else {
2794 ce->flow = NULL;
2795 }
2796 }
2797 if (key) {
2798 netdev_flow_key_clone(&ce->key, key);
2799 }
2800 }
2801
2802 static inline void
2803 emc_insert(struct emc_cache *cache, const struct netdev_flow_key *key,
2804 struct dp_netdev_flow *flow)
2805 {
2806 struct emc_entry *to_be_replaced = NULL;
2807 struct emc_entry *current_entry;
2808
2809 EMC_FOR_EACH_POS_WITH_HASH(cache, current_entry, key->hash) {
2810 if (netdev_flow_key_equal(&current_entry->key, key)) {
2811 /* We found the entry with the 'mf' miniflow */
2812 emc_change_entry(current_entry, flow, NULL);
2813 return;
2814 }
2815
2816 /* Replacement policy: put the flow in an empty (not alive) entry, or
2817 * in the first entry where it can be */
2818 if (!to_be_replaced
2819 || (emc_entry_alive(to_be_replaced)
2820 && !emc_entry_alive(current_entry))
2821 || current_entry->key.hash < to_be_replaced->key.hash) {
2822 to_be_replaced = current_entry;
2823 }
2824 }
2825 /* We didn't find the miniflow in the cache.
2826 * The 'to_be_replaced' entry is where the new flow will be stored */
2827
2828 emc_change_entry(to_be_replaced, flow, key);
2829 }
2830
2831 static inline void
2832 emc_probabilistic_insert(struct dp_netdev_pmd_thread *pmd,
2833 const struct netdev_flow_key *key,
2834 struct dp_netdev_flow *flow)
2835 {
2836 /* Insert an entry into the EMC based on probability value 'min'. By
2837 * default the value is UINT32_MAX / 100 which yields an insertion
2838 * probability of 1/100 ie. 1% */
2839
2840 uint32_t min = pmd->ctx.emc_insert_min;
2841
2842 if (min && random_uint32() <= min) {
2843 emc_insert(&(pmd->flow_cache).emc_cache, key, flow);
2844 }
2845 }
2846
2847 static inline struct dp_netdev_flow *
2848 emc_lookup(struct emc_cache *cache, const struct netdev_flow_key *key)
2849 {
2850 struct emc_entry *current_entry;
2851
2852 EMC_FOR_EACH_POS_WITH_HASH(cache, current_entry, key->hash) {
2853 if (current_entry->key.hash == key->hash
2854 && emc_entry_alive(current_entry)
2855 && netdev_flow_key_equal_mf(&current_entry->key, &key->mf)) {
2856
2857 /* We found the entry with the 'key->mf' miniflow */
2858 return current_entry->flow;
2859 }
2860 }
2861
2862 return NULL;
2863 }
2864
2865 static inline const struct cmap_node *
2866 smc_entry_get(struct dp_netdev_pmd_thread *pmd, const uint32_t hash)
2867 {
2868 struct smc_cache *cache = &(pmd->flow_cache).smc_cache;
2869 struct smc_bucket *bucket = &cache->buckets[hash & SMC_MASK];
2870 uint16_t sig = hash >> 16;
2871 uint16_t index = UINT16_MAX;
2872
2873 for (int i = 0; i < SMC_ENTRY_PER_BUCKET; i++) {
2874 if (bucket->sig[i] == sig) {
2875 index = bucket->flow_idx[i];
2876 break;
2877 }
2878 }
2879 if (index != UINT16_MAX) {
2880 return cmap_find_by_index(&pmd->flow_table, index);
2881 }
2882 return NULL;
2883 }
2884
2885 static void
2886 smc_clear_entry(struct smc_bucket *b, int idx)
2887 {
2888 b->flow_idx[idx] = UINT16_MAX;
2889 }
2890
2891 /* Insert the flow_table index into SMC. Insertion may fail when 1) SMC is
2892 * turned off, 2) the flow_table index is larger than uint16_t can handle.
2893 * If there is already an SMC entry having same signature, the index will be
2894 * updated. If there is no existing entry, but an empty entry is available,
2895 * the empty entry will be taken. If no empty entry or existing same signature,
2896 * a random entry from the hashed bucket will be picked. */
2897 static inline void
2898 smc_insert(struct dp_netdev_pmd_thread *pmd,
2899 const struct netdev_flow_key *key,
2900 uint32_t hash)
2901 {
2902 struct smc_cache *smc_cache = &(pmd->flow_cache).smc_cache;
2903 struct smc_bucket *bucket = &smc_cache->buckets[key->hash & SMC_MASK];
2904 uint16_t index;
2905 uint32_t cmap_index;
2906 bool smc_enable_db;
2907 int i;
2908
2909 atomic_read_relaxed(&pmd->dp->smc_enable_db, &smc_enable_db);
2910 if (!smc_enable_db) {
2911 return;
2912 }
2913
2914 cmap_index = cmap_find_index(&pmd->flow_table, hash);
2915 index = (cmap_index >= UINT16_MAX) ? UINT16_MAX : (uint16_t)cmap_index;
2916
2917 /* If the index is larger than SMC can handle (uint16_t), we don't
2918 * insert */
2919 if (index == UINT16_MAX) {
2920 return;
2921 }
2922
2923 /* If an entry with same signature already exists, update the index */
2924 uint16_t sig = key->hash >> 16;
2925 for (i = 0; i < SMC_ENTRY_PER_BUCKET; i++) {
2926 if (bucket->sig[i] == sig) {
2927 bucket->flow_idx[i] = index;
2928 return;
2929 }
2930 }
2931 /* If there is an empty entry, occupy it. */
2932 for (i = 0; i < SMC_ENTRY_PER_BUCKET; i++) {
2933 if (bucket->flow_idx[i] == UINT16_MAX) {
2934 bucket->sig[i] = sig;
2935 bucket->flow_idx[i] = index;
2936 return;
2937 }
2938 }
2939 /* Otherwise, pick a random entry. */
2940 i = random_uint32() % SMC_ENTRY_PER_BUCKET;
2941 bucket->sig[i] = sig;
2942 bucket->flow_idx[i] = index;
2943 }
2944
2945 static struct dp_netdev_flow *
2946 dp_netdev_pmd_lookup_flow(struct dp_netdev_pmd_thread *pmd,
2947 const struct netdev_flow_key *key,
2948 int *lookup_num_p)
2949 {
2950 struct dpcls *cls;
2951 struct dpcls_rule *rule;
2952 odp_port_t in_port = u32_to_odp(MINIFLOW_GET_U32(&key->mf,
2953 in_port.odp_port));
2954 struct dp_netdev_flow *netdev_flow = NULL;
2955
2956 cls = dp_netdev_pmd_lookup_dpcls(pmd, in_port);
2957 if (OVS_LIKELY(cls)) {
2958 dpcls_lookup(cls, &key, &rule, 1, lookup_num_p);
2959 netdev_flow = dp_netdev_flow_cast(rule);
2960 }
2961 return netdev_flow;
2962 }
2963
2964 static struct dp_netdev_flow *
2965 dp_netdev_pmd_find_flow(const struct dp_netdev_pmd_thread *pmd,
2966 const ovs_u128 *ufidp, const struct nlattr *key,
2967 size_t key_len)
2968 {
2969 struct dp_netdev_flow *netdev_flow;
2970 struct flow flow;
2971 ovs_u128 ufid;
2972
2973 /* If a UFID is not provided, determine one based on the key. */
2974 if (!ufidp && key && key_len
2975 && !dpif_netdev_flow_from_nlattrs(key, key_len, &flow, false)) {
2976 dpif_flow_hash(pmd->dp->dpif, &flow, sizeof flow, &ufid);
2977 ufidp = &ufid;
2978 }
2979
2980 if (ufidp) {
2981 CMAP_FOR_EACH_WITH_HASH (netdev_flow, node, dp_netdev_flow_hash(ufidp),
2982 &pmd->flow_table) {
2983 if (ovs_u128_equals(netdev_flow->ufid, *ufidp)) {
2984 return netdev_flow;
2985 }
2986 }
2987 }
2988
2989 return NULL;
2990 }
2991
2992 static void
2993 get_dpif_flow_stats(const struct dp_netdev_flow *netdev_flow_,
2994 struct dpif_flow_stats *stats)
2995 {
2996 struct dp_netdev_flow *netdev_flow;
2997 unsigned long long n;
2998 long long used;
2999 uint16_t flags;
3000
3001 netdev_flow = CONST_CAST(struct dp_netdev_flow *, netdev_flow_);
3002
3003 atomic_read_relaxed(&netdev_flow->stats.packet_count, &n);
3004 stats->n_packets = n;
3005 atomic_read_relaxed(&netdev_flow->stats.byte_count, &n);
3006 stats->n_bytes = n;
3007 atomic_read_relaxed(&netdev_flow->stats.used, &used);
3008 stats->used = used;
3009 atomic_read_relaxed(&netdev_flow->stats.tcp_flags, &flags);
3010 stats->tcp_flags = flags;
3011 }
3012
3013 /* Converts to the dpif_flow format, using 'key_buf' and 'mask_buf' for
3014 * storing the netlink-formatted key/mask. 'key_buf' may be the same as
3015 * 'mask_buf'. Actions will be returned without copying, by relying on RCU to
3016 * protect them. */
3017 static void
3018 dp_netdev_flow_to_dpif_flow(const struct dp_netdev_flow *netdev_flow,
3019 struct ofpbuf *key_buf, struct ofpbuf *mask_buf,
3020 struct dpif_flow *flow, bool terse)
3021 {
3022 if (terse) {
3023 memset(flow, 0, sizeof *flow);
3024 } else {
3025 struct flow_wildcards wc;
3026 struct dp_netdev_actions *actions;
3027 size_t offset;
3028 struct odp_flow_key_parms odp_parms = {
3029 .flow = &netdev_flow->flow,
3030 .mask = &wc.masks,
3031 .support = dp_netdev_support,
3032 };
3033
3034 miniflow_expand(&netdev_flow->cr.mask->mf, &wc.masks);
3035 /* in_port is exact matched, but we have left it out from the mask for
3036 * optimnization reasons. Add in_port back to the mask. */
3037 wc.masks.in_port.odp_port = ODPP_NONE;
3038
3039 /* Key */
3040 offset = key_buf->size;
3041 flow->key = ofpbuf_tail(key_buf);
3042 odp_flow_key_from_flow(&odp_parms, key_buf);
3043 flow->key_len = key_buf->size - offset;
3044
3045 /* Mask */
3046 offset = mask_buf->size;
3047 flow->mask = ofpbuf_tail(mask_buf);
3048 odp_parms.key_buf = key_buf;
3049 odp_flow_key_from_mask(&odp_parms, mask_buf);
3050 flow->mask_len = mask_buf->size - offset;
3051
3052 /* Actions */
3053 actions = dp_netdev_flow_get_actions(netdev_flow);
3054 flow->actions = actions->actions;
3055 flow->actions_len = actions->size;
3056 }
3057
3058 flow->ufid = netdev_flow->ufid;
3059 flow->ufid_present = true;
3060 flow->pmd_id = netdev_flow->pmd_id;
3061 get_dpif_flow_stats(netdev_flow, &flow->stats);
3062
3063 flow->attrs.offloaded = false;
3064 flow->attrs.dp_layer = "ovs";
3065 }
3066
3067 static int
3068 dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len,
3069 const struct nlattr *mask_key,
3070 uint32_t mask_key_len, const struct flow *flow,
3071 struct flow_wildcards *wc, bool probe)
3072 {
3073 enum odp_key_fitness fitness;
3074
3075 fitness = odp_flow_key_to_mask(mask_key, mask_key_len, wc, flow, NULL);
3076 if (fitness) {
3077 if (!probe) {
3078 /* This should not happen: it indicates that
3079 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
3080 * disagree on the acceptable form of a mask. Log the problem
3081 * as an error, with enough details to enable debugging. */
3082 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3083
3084 if (!VLOG_DROP_ERR(&rl)) {
3085 struct ds s;
3086
3087 ds_init(&s);
3088 odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s,
3089 true);
3090 VLOG_ERR("internal error parsing flow mask %s (%s)",
3091 ds_cstr(&s), odp_key_fitness_to_string(fitness));
3092 ds_destroy(&s);
3093 }
3094 }
3095
3096 return EINVAL;
3097 }
3098
3099 return 0;
3100 }
3101
3102 static int
3103 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
3104 struct flow *flow, bool probe)
3105 {
3106 if (odp_flow_key_to_flow(key, key_len, flow, NULL)) {
3107 if (!probe) {
3108 /* This should not happen: it indicates that
3109 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
3110 * the acceptable form of a flow. Log the problem as an error,
3111 * with enough details to enable debugging. */
3112 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3113
3114 if (!VLOG_DROP_ERR(&rl)) {
3115 struct ds s;
3116
3117 ds_init(&s);
3118 odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
3119 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
3120 ds_destroy(&s);
3121 }
3122 }
3123
3124 return EINVAL;
3125 }
3126
3127 if (flow->ct_state & DP_NETDEV_CS_UNSUPPORTED_MASK) {
3128 return EINVAL;
3129 }
3130
3131 return 0;
3132 }
3133
3134 static int
3135 dpif_netdev_flow_get(const struct dpif *dpif, const struct dpif_flow_get *get)
3136 {
3137 struct dp_netdev *dp = get_dp_netdev(dpif);
3138 struct dp_netdev_flow *netdev_flow;
3139 struct dp_netdev_pmd_thread *pmd;
3140 struct hmapx to_find = HMAPX_INITIALIZER(&to_find);
3141 struct hmapx_node *node;
3142 int error = EINVAL;
3143
3144 if (get->pmd_id == PMD_ID_NULL) {
3145 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3146 if (dp_netdev_pmd_try_ref(pmd) && !hmapx_add(&to_find, pmd)) {
3147 dp_netdev_pmd_unref(pmd);
3148 }
3149 }
3150 } else {
3151 pmd = dp_netdev_get_pmd(dp, get->pmd_id);
3152 if (!pmd) {
3153 goto out;
3154 }
3155 hmapx_add(&to_find, pmd);
3156 }
3157
3158 if (!hmapx_count(&to_find)) {
3159 goto out;
3160 }
3161
3162 HMAPX_FOR_EACH (node, &to_find) {
3163 pmd = (struct dp_netdev_pmd_thread *) node->data;
3164 netdev_flow = dp_netdev_pmd_find_flow(pmd, get->ufid, get->key,
3165 get->key_len);
3166 if (netdev_flow) {
3167 dp_netdev_flow_to_dpif_flow(netdev_flow, get->buffer, get->buffer,
3168 get->flow, false);
3169 error = 0;
3170 break;
3171 } else {
3172 error = ENOENT;
3173 }
3174 }
3175
3176 HMAPX_FOR_EACH (node, &to_find) {
3177 pmd = (struct dp_netdev_pmd_thread *) node->data;
3178 dp_netdev_pmd_unref(pmd);
3179 }
3180 out:
3181 hmapx_destroy(&to_find);
3182 return error;
3183 }
3184
3185 static void
3186 dp_netdev_get_mega_ufid(const struct match *match, ovs_u128 *mega_ufid)
3187 {
3188 struct flow masked_flow;
3189 size_t i;
3190
3191 for (i = 0; i < sizeof(struct flow); i++) {
3192 ((uint8_t *)&masked_flow)[i] = ((uint8_t *)&match->flow)[i] &
3193 ((uint8_t *)&match->wc)[i];
3194 }
3195 dpif_flow_hash(NULL, &masked_flow, sizeof(struct flow), mega_ufid);
3196 }
3197
3198 static struct dp_netdev_flow *
3199 dp_netdev_flow_add(struct dp_netdev_pmd_thread *pmd,
3200 struct match *match, const ovs_u128 *ufid,
3201 const struct nlattr *actions, size_t actions_len)
3202 OVS_REQUIRES(pmd->flow_mutex)
3203 {
3204 struct dp_netdev_flow *flow;
3205 struct netdev_flow_key mask;
3206 struct dpcls *cls;
3207
3208 /* Make sure in_port is exact matched before we read it. */
3209 ovs_assert(match->wc.masks.in_port.odp_port == ODPP_NONE);
3210 odp_port_t in_port = match->flow.in_port.odp_port;
3211
3212 /* As we select the dpcls based on the port number, each netdev flow
3213 * belonging to the same dpcls will have the same odp_port value.
3214 * For performance reasons we wildcard odp_port here in the mask. In the
3215 * typical case dp_hash is also wildcarded, and the resulting 8-byte
3216 * chunk {dp_hash, in_port} will be ignored by netdev_flow_mask_init() and
3217 * will not be part of the subtable mask.
3218 * This will speed up the hash computation during dpcls_lookup() because
3219 * there is one less call to hash_add64() in this case. */
3220 match->wc.masks.in_port.odp_port = 0;
3221 netdev_flow_mask_init(&mask, match);
3222 match->wc.masks.in_port.odp_port = ODPP_NONE;
3223
3224 /* Make sure wc does not have metadata. */
3225 ovs_assert(!FLOWMAP_HAS_FIELD(&mask.mf.map, metadata)
3226 && !FLOWMAP_HAS_FIELD(&mask.mf.map, regs));
3227
3228 /* Do not allocate extra space. */
3229 flow = xmalloc(sizeof *flow - sizeof flow->cr.flow.mf + mask.len);
3230 memset(&flow->stats, 0, sizeof flow->stats);
3231 flow->dead = false;
3232 flow->batch = NULL;
3233 flow->mark = INVALID_FLOW_MARK;
3234 *CONST_CAST(unsigned *, &flow->pmd_id) = pmd->core_id;
3235 *CONST_CAST(struct flow *, &flow->flow) = match->flow;
3236 *CONST_CAST(ovs_u128 *, &flow->ufid) = *ufid;
3237 ovs_refcount_init(&flow->ref_cnt);
3238 ovsrcu_set(&flow->actions, dp_netdev_actions_create(actions, actions_len));
3239
3240 dp_netdev_get_mega_ufid(match, CONST_CAST(ovs_u128 *, &flow->mega_ufid));
3241 netdev_flow_key_init_masked(&flow->cr.flow, &match->flow, &mask);
3242
3243 /* Select dpcls for in_port. Relies on in_port to be exact match. */
3244 cls = dp_netdev_pmd_find_dpcls(pmd, in_port);
3245 dpcls_insert(cls, &flow->cr, &mask);
3246
3247 cmap_insert(&pmd->flow_table, CONST_CAST(struct cmap_node *, &flow->node),
3248 dp_netdev_flow_hash(&flow->ufid));
3249
3250 queue_netdev_flow_put(pmd, flow, match, actions, actions_len);
3251
3252 if (OVS_UNLIKELY(!VLOG_DROP_DBG((&upcall_rl)))) {
3253 struct ds ds = DS_EMPTY_INITIALIZER;
3254 struct ofpbuf key_buf, mask_buf;
3255 struct odp_flow_key_parms odp_parms = {
3256 .flow = &match->flow,
3257 .mask = &match->wc.masks,
3258 .support = dp_netdev_support,
3259 };
3260
3261 ofpbuf_init(&key_buf, 0);
3262 ofpbuf_init(&mask_buf, 0);
3263
3264 odp_flow_key_from_flow(&odp_parms, &key_buf);
3265 odp_parms.key_buf = &key_buf;
3266 odp_flow_key_from_mask(&odp_parms, &mask_buf);
3267
3268 ds_put_cstr(&ds, "flow_add: ");
3269 odp_format_ufid(ufid, &ds);
3270 ds_put_cstr(&ds, " ");
3271 odp_flow_format(key_buf.data, key_buf.size,
3272 mask_buf.data, mask_buf.size,
3273 NULL, &ds, false);
3274 ds_put_cstr(&ds, ", actions:");
3275 format_odp_actions(&ds, actions, actions_len, NULL);
3276
3277 VLOG_DBG("%s", ds_cstr(&ds));
3278
3279 ofpbuf_uninit(&key_buf);
3280 ofpbuf_uninit(&mask_buf);
3281
3282 /* Add a printout of the actual match installed. */
3283 struct match m;
3284 ds_clear(&ds);
3285 ds_put_cstr(&ds, "flow match: ");
3286 miniflow_expand(&flow->cr.flow.mf, &m.flow);
3287 miniflow_expand(&flow->cr.mask->mf, &m.wc.masks);
3288 memset(&m.tun_md, 0, sizeof m.tun_md);
3289 match_format(&m, NULL, &ds, OFP_DEFAULT_PRIORITY);
3290
3291 VLOG_DBG("%s", ds_cstr(&ds));
3292
3293 ds_destroy(&ds);
3294 }
3295
3296 return flow;
3297 }
3298
3299 static int
3300 flow_put_on_pmd(struct dp_netdev_pmd_thread *pmd,
3301 struct netdev_flow_key *key,
3302 struct match *match,
3303 ovs_u128 *ufid,
3304 const struct dpif_flow_put *put,
3305 struct dpif_flow_stats *stats)
3306 {
3307 struct dp_netdev_flow *netdev_flow;
3308 int error = 0;
3309
3310 if (stats) {
3311 memset(stats, 0, sizeof *stats);
3312 }
3313
3314 ovs_mutex_lock(&pmd->flow_mutex);
3315 netdev_flow = dp_netdev_pmd_lookup_flow(pmd, key, NULL);
3316 if (!netdev_flow) {
3317 if (put->flags & DPIF_FP_CREATE) {
3318 if (cmap_count(&pmd->flow_table) < MAX_FLOWS) {
3319 dp_netdev_flow_add(pmd, match, ufid, put->actions,
3320 put->actions_len);
3321 error = 0;
3322 } else {
3323 error = EFBIG;
3324 }
3325 } else {
3326 error = ENOENT;
3327 }
3328 } else {
3329 if (put->flags & DPIF_FP_MODIFY) {
3330 struct dp_netdev_actions *new_actions;
3331 struct dp_netdev_actions *old_actions;
3332
3333 new_actions = dp_netdev_actions_create(put->actions,
3334 put->actions_len);
3335
3336 old_actions = dp_netdev_flow_get_actions(netdev_flow);
3337 ovsrcu_set(&netdev_flow->actions, new_actions);
3338
3339 queue_netdev_flow_put(pmd, netdev_flow, match,
3340 put->actions, put->actions_len);
3341
3342 if (stats) {
3343 get_dpif_flow_stats(netdev_flow, stats);
3344 }
3345 if (put->flags & DPIF_FP_ZERO_STATS) {
3346 /* XXX: The userspace datapath uses thread local statistics
3347 * (for flows), which should be updated only by the owning
3348 * thread. Since we cannot write on stats memory here,
3349 * we choose not to support this flag. Please note:
3350 * - This feature is currently used only by dpctl commands with
3351 * option --clear.
3352 * - Should the need arise, this operation can be implemented
3353 * by keeping a base value (to be update here) for each
3354 * counter, and subtracting it before outputting the stats */
3355 error = EOPNOTSUPP;
3356 }
3357
3358 ovsrcu_postpone(dp_netdev_actions_free, old_actions);
3359 } else if (put->flags & DPIF_FP_CREATE) {
3360 error = EEXIST;
3361 } else {
3362 /* Overlapping flow. */
3363 error = EINVAL;
3364 }
3365 }
3366 ovs_mutex_unlock(&pmd->flow_mutex);
3367 return error;
3368 }
3369
3370 static int
3371 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
3372 {
3373 struct dp_netdev *dp = get_dp_netdev(dpif);
3374 struct netdev_flow_key key, mask;
3375 struct dp_netdev_pmd_thread *pmd;
3376 struct match match;
3377 ovs_u128 ufid;
3378 int error;
3379 bool probe = put->flags & DPIF_FP_PROBE;
3380
3381 if (put->stats) {
3382 memset(put->stats, 0, sizeof *put->stats);
3383 }
3384 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &match.flow,
3385 probe);
3386 if (error) {
3387 return error;
3388 }
3389 error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
3390 put->mask, put->mask_len,
3391 &match.flow, &match.wc, probe);
3392 if (error) {
3393 return error;
3394 }
3395
3396 if (put->ufid) {
3397 ufid = *put->ufid;
3398 } else {
3399 dpif_flow_hash(dpif, &match.flow, sizeof match.flow, &ufid);
3400 }
3401
3402 /* The Netlink encoding of datapath flow keys cannot express
3403 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
3404 * tag is interpreted as exact match on the fact that there is no
3405 * VLAN. Unless we refactor a lot of code that translates between
3406 * Netlink and struct flow representations, we have to do the same
3407 * here. This must be in sync with 'match' in handle_packet_upcall(). */
3408 if (!match.wc.masks.vlans[0].tci) {
3409 match.wc.masks.vlans[0].tci = htons(0xffff);
3410 }
3411
3412 /* Must produce a netdev_flow_key for lookup.
3413 * Use the same method as employed to create the key when adding
3414 * the flow to the dplcs to make sure they match. */
3415 netdev_flow_mask_init(&mask, &match);
3416 netdev_flow_key_init_masked(&key, &match.flow, &mask);
3417
3418 if (put->pmd_id == PMD_ID_NULL) {
3419 if (cmap_count(&dp->poll_threads) == 0) {
3420 return EINVAL;
3421 }
3422 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3423 struct dpif_flow_stats pmd_stats;
3424 int pmd_error;
3425
3426 pmd_error = flow_put_on_pmd(pmd, &key, &match, &ufid, put,
3427 &pmd_stats);
3428 if (pmd_error) {
3429 error = pmd_error;
3430 } else if (put->stats) {
3431 put->stats->n_packets += pmd_stats.n_packets;
3432 put->stats->n_bytes += pmd_stats.n_bytes;
3433 put->stats->used = MAX(put->stats->used, pmd_stats.used);
3434 put->stats->tcp_flags |= pmd_stats.tcp_flags;
3435 }
3436 }
3437 } else {
3438 pmd = dp_netdev_get_pmd(dp, put->pmd_id);
3439 if (!pmd) {
3440 return EINVAL;
3441 }
3442 error = flow_put_on_pmd(pmd, &key, &match, &ufid, put, put->stats);
3443 dp_netdev_pmd_unref(pmd);
3444 }
3445
3446 return error;
3447 }
3448
3449 static int
3450 flow_del_on_pmd(struct dp_netdev_pmd_thread *pmd,
3451 struct dpif_flow_stats *stats,
3452 const struct dpif_flow_del *del)
3453 {
3454 struct dp_netdev_flow *netdev_flow;
3455 int error = 0;
3456
3457 ovs_mutex_lock(&pmd->flow_mutex);
3458 netdev_flow = dp_netdev_pmd_find_flow(pmd, del->ufid, del->key,
3459 del->key_len);
3460 if (netdev_flow) {
3461 if (stats) {
3462 get_dpif_flow_stats(netdev_flow, stats);
3463 }
3464 dp_netdev_pmd_remove_flow(pmd, netdev_flow);
3465 } else {
3466 error = ENOENT;
3467 }
3468 ovs_mutex_unlock(&pmd->flow_mutex);
3469
3470 return error;
3471 }
3472
3473 static int
3474 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
3475 {
3476 struct dp_netdev *dp = get_dp_netdev(dpif);
3477 struct dp_netdev_pmd_thread *pmd;
3478 int error = 0;
3479
3480 if (del->stats) {
3481 memset(del->stats, 0, sizeof *del->stats);
3482 }
3483
3484 if (del->pmd_id == PMD_ID_NULL) {
3485 if (cmap_count(&dp->poll_threads) == 0) {
3486 return EINVAL;
3487 }
3488 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3489 struct dpif_flow_stats pmd_stats;
3490 int pmd_error;
3491
3492 pmd_error = flow_del_on_pmd(pmd, &pmd_stats, del);
3493 if (pmd_error) {
3494 error = pmd_error;
3495 } else if (del->stats) {
3496 del->stats->n_packets += pmd_stats.n_packets;
3497 del->stats->n_bytes += pmd_stats.n_bytes;
3498 del->stats->used = MAX(del->stats->used, pmd_stats.used);
3499 del->stats->tcp_flags |= pmd_stats.tcp_flags;
3500 }
3501 }
3502 } else {
3503 pmd = dp_netdev_get_pmd(dp, del->pmd_id);
3504 if (!pmd) {
3505 return EINVAL;
3506 }
3507 error = flow_del_on_pmd(pmd, del->stats, del);
3508 dp_netdev_pmd_unref(pmd);
3509 }
3510
3511
3512 return error;
3513 }
3514
3515 struct dpif_netdev_flow_dump {
3516 struct dpif_flow_dump up;
3517 struct cmap_position poll_thread_pos;
3518 struct cmap_position flow_pos;
3519 struct dp_netdev_pmd_thread *cur_pmd;
3520 int status;
3521 struct ovs_mutex mutex;
3522 };
3523
3524 static struct dpif_netdev_flow_dump *
3525 dpif_netdev_flow_dump_cast(struct dpif_flow_dump *dump)
3526 {
3527 return CONTAINER_OF(dump, struct dpif_netdev_flow_dump, up);
3528 }
3529
3530 static struct dpif_flow_dump *
3531 dpif_netdev_flow_dump_create(const struct dpif *dpif_, bool terse,
3532 struct dpif_flow_dump_types *types OVS_UNUSED)
3533 {
3534 struct dpif_netdev_flow_dump *dump;
3535
3536 dump = xzalloc(sizeof *dump);
3537 dpif_flow_dump_init(&dump->up, dpif_);
3538 dump->up.terse = terse;
3539 ovs_mutex_init(&dump->mutex);
3540
3541 return &dump->up;
3542 }
3543
3544 static int
3545 dpif_netdev_flow_dump_destroy(struct dpif_flow_dump *dump_)
3546 {
3547 struct dpif_netdev_flow_dump *dump = dpif_netdev_flow_dump_cast(dump_);
3548
3549 ovs_mutex_destroy(&dump->mutex);
3550 free(dump);
3551 return 0;
3552 }
3553
3554 struct dpif_netdev_flow_dump_thread {
3555 struct dpif_flow_dump_thread up;
3556 struct dpif_netdev_flow_dump *dump;
3557 struct odputil_keybuf keybuf[FLOW_DUMP_MAX_BATCH];
3558 struct odputil_keybuf maskbuf[FLOW_DUMP_MAX_BATCH];
3559 };
3560
3561 static struct dpif_netdev_flow_dump_thread *
3562 dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
3563 {
3564 return CONTAINER_OF(thread, struct dpif_netdev_flow_dump_thread, up);
3565 }
3566
3567 static struct dpif_flow_dump_thread *
3568 dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump *dump_)
3569 {
3570 struct dpif_netdev_flow_dump *dump = dpif_netdev_flow_dump_cast(dump_);
3571 struct dpif_netdev_flow_dump_thread *thread;
3572
3573 thread = xmalloc(sizeof *thread);
3574 dpif_flow_dump_thread_init(&thread->up, &dump->up);
3575 thread->dump = dump;
3576 return &thread->up;
3577 }
3578
3579 static void
3580 dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
3581 {
3582 struct dpif_netdev_flow_dump_thread *thread
3583 = dpif_netdev_flow_dump_thread_cast(thread_);
3584
3585 free(thread);
3586 }
3587
3588 static int
3589 dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread *thread_,
3590 struct dpif_flow *flows, int max_flows)
3591 {
3592 struct dpif_netdev_flow_dump_thread *thread
3593 = dpif_netdev_flow_dump_thread_cast(thread_);
3594 struct dpif_netdev_flow_dump *dump = thread->dump;
3595 struct dp_netdev_flow *netdev_flows[FLOW_DUMP_MAX_BATCH];
3596 int n_flows = 0;
3597 int i;
3598
3599 ovs_mutex_lock(&dump->mutex);
3600 if (!dump->status) {
3601 struct dpif_netdev *dpif = dpif_netdev_cast(thread->up.dpif);
3602 struct dp_netdev *dp = get_dp_netdev(&dpif->dpif);
3603 struct dp_netdev_pmd_thread *pmd = dump->cur_pmd;
3604 int flow_limit = MIN(max_flows, FLOW_DUMP_MAX_BATCH);
3605
3606 /* First call to dump_next(), extracts the first pmd thread.
3607 * If there is no pmd thread, returns immediately. */
3608 if (!pmd) {
3609 pmd = dp_netdev_pmd_get_next(dp, &dump->poll_thread_pos);
3610 if (!pmd) {
3611 ovs_mutex_unlock(&dump->mutex);
3612 return n_flows;
3613
3614 }
3615 }
3616
3617 do {
3618 for (n_flows = 0; n_flows < flow_limit; n_flows++) {
3619 struct cmap_node *node;
3620
3621 node = cmap_next_position(&pmd->flow_table, &dump->flow_pos);
3622 if (!node) {
3623 break;
3624 }
3625 netdev_flows[n_flows] = CONTAINER_OF(node,
3626 struct dp_netdev_flow,
3627 node);
3628 }
3629 /* When finishing dumping the current pmd thread, moves to
3630 * the next. */
3631 if (n_flows < flow_limit) {
3632 memset(&dump->flow_pos, 0, sizeof dump->flow_pos);
3633 dp_netdev_pmd_unref(pmd);
3634 pmd = dp_netdev_pmd_get_next(dp, &dump->poll_thread_pos);
3635 if (!pmd) {
3636 dump->status = EOF;
3637 break;
3638 }
3639 }
3640 /* Keeps the reference to next caller. */
3641 dump->cur_pmd = pmd;
3642
3643 /* If the current dump is empty, do not exit the loop, since the
3644 * remaining pmds could have flows to be dumped. Just dumps again
3645 * on the new 'pmd'. */
3646 } while (!n_flows);
3647 }
3648 ovs_mutex_unlock(&dump->mutex);
3649
3650 for (i = 0; i < n_flows; i++) {
3651 struct odputil_keybuf *maskbuf = &thread->maskbuf[i];
3652 struct odputil_keybuf *keybuf = &thread->keybuf[i];
3653 struct dp_netdev_flow *netdev_flow = netdev_flows[i];
3654 struct dpif_flow *f = &flows[i];
3655 struct ofpbuf key, mask;
3656
3657 ofpbuf_use_stack(&key, keybuf, sizeof *keybuf);
3658 ofpbuf_use_stack(&mask, maskbuf, sizeof *maskbuf);
3659 dp_netdev_flow_to_dpif_flow(netdev_flow, &key, &mask, f,
3660 dump->up.terse);
3661 }
3662
3663 return n_flows;
3664 }
3665
3666 static int
3667 dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
3668 OVS_NO_THREAD_SAFETY_ANALYSIS
3669 {
3670 struct dp_netdev *dp = get_dp_netdev(dpif);
3671 struct dp_netdev_pmd_thread *pmd;
3672 struct dp_packet_batch pp;
3673
3674 if (dp_packet_size(execute->packet) < ETH_HEADER_LEN ||
3675 dp_packet_size(execute->packet) > UINT16_MAX) {
3676 return EINVAL;
3677 }
3678
3679 /* Tries finding the 'pmd'. If NULL is returned, that means
3680 * the current thread is a non-pmd thread and should use
3681 * dp_netdev_get_pmd(dp, NON_PMD_CORE_ID). */
3682 pmd = ovsthread_getspecific(dp->per_pmd_key);
3683 if (!pmd) {
3684 pmd = dp_netdev_get_pmd(dp, NON_PMD_CORE_ID);
3685 if (!pmd) {
3686 return EBUSY;
3687 }
3688 }
3689
3690 if (execute->probe) {
3691 /* If this is part of a probe, Drop the packet, since executing
3692 * the action may actually cause spurious packets be sent into
3693 * the network. */
3694 if (pmd->core_id == NON_PMD_CORE_ID) {
3695 dp_netdev_pmd_unref(pmd);
3696 }
3697 return 0;
3698 }
3699
3700 /* If the current thread is non-pmd thread, acquires
3701 * the 'non_pmd_mutex'. */
3702 if (pmd->core_id == NON_PMD_CORE_ID) {
3703 ovs_mutex_lock(&dp->non_pmd_mutex);
3704 }
3705
3706 /* Update current time in PMD context. We don't care about EMC insertion
3707 * probability, because we are on a slow path. */
3708 pmd_thread_ctx_time_update(pmd);
3709
3710 /* The action processing expects the RSS hash to be valid, because
3711 * it's always initialized at the beginning of datapath processing.
3712 * In this case, though, 'execute->packet' may not have gone through
3713 * the datapath at all, it may have been generated by the upper layer
3714 * (OpenFlow packet-out, BFD frame, ...). */
3715 if (!dp_packet_rss_valid(execute->packet)) {
3716 dp_packet_set_rss_hash(execute->packet,
3717 flow_hash_5tuple(execute->flow, 0));
3718 }
3719
3720 dp_packet_batch_init_packet(&pp, execute->packet);
3721 pp.do_not_steal = true;
3722 dp_netdev_execute_actions(pmd, &pp, false, execute->flow,
3723 execute->actions, execute->actions_len);
3724 dp_netdev_pmd_flush_output_packets(pmd, true);
3725
3726 if (pmd->core_id == NON_PMD_CORE_ID) {
3727 ovs_mutex_unlock(&dp->non_pmd_mutex);
3728 dp_netdev_pmd_unref(pmd);
3729 }
3730
3731 return 0;
3732 }
3733
3734 static void
3735 dpif_netdev_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops,
3736 enum dpif_offload_type offload_type OVS_UNUSED)
3737 {
3738 size_t i;
3739
3740 for (i = 0; i < n_ops; i++) {
3741 struct dpif_op *op = ops[i];
3742
3743 switch (op->type) {
3744 case DPIF_OP_FLOW_PUT:
3745 op->error = dpif_netdev_flow_put(dpif, &op->flow_put);
3746 break;
3747
3748 case DPIF_OP_FLOW_DEL:
3749 op->error = dpif_netdev_flow_del(dpif, &op->flow_del);
3750 break;
3751
3752 case DPIF_OP_EXECUTE:
3753 op->error = dpif_netdev_execute(dpif, &op->execute);
3754 break;
3755
3756 case DPIF_OP_FLOW_GET:
3757 op->error = dpif_netdev_flow_get(dpif, &op->flow_get);
3758 break;
3759 }
3760 }
3761 }
3762
3763 /* Enable or Disable PMD auto load balancing. */
3764 static void
3765 set_pmd_auto_lb(struct dp_netdev *dp)
3766 {
3767 unsigned int cnt = 0;
3768 struct dp_netdev_pmd_thread *pmd;
3769 struct pmd_auto_lb *pmd_alb = &dp->pmd_alb;
3770
3771 bool enable_alb = false;
3772 bool multi_rxq = false;
3773 bool pmd_rxq_assign_cyc = dp->pmd_rxq_assign_cyc;
3774
3775 /* Ensure that there is at least 2 non-isolated PMDs and
3776 * one of them is polling more than one rxq. */
3777 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3778 if (pmd->core_id == NON_PMD_CORE_ID || pmd->isolated) {
3779 continue;
3780 }
3781
3782 if (hmap_count(&pmd->poll_list) > 1) {
3783 multi_rxq = true;
3784 }
3785 if (cnt && multi_rxq) {
3786 enable_alb = true;
3787 break;
3788 }
3789 cnt++;
3790 }
3791
3792 /* Enable auto LB if it is requested and cycle based assignment is true. */
3793 enable_alb = enable_alb && pmd_rxq_assign_cyc &&
3794 pmd_alb->auto_lb_requested;
3795
3796 if (pmd_alb->is_enabled != enable_alb) {
3797 pmd_alb->is_enabled = enable_alb;
3798 if (pmd_alb->is_enabled) {
3799 VLOG_INFO("PMD auto load balance is enabled "
3800 "(with rebalance interval:%"PRIu64" msec)",
3801 pmd_alb->rebalance_intvl);
3802 } else {
3803 pmd_alb->rebalance_poll_timer = 0;
3804 VLOG_INFO("PMD auto load balance is disabled");
3805 }
3806 }
3807
3808 }
3809
3810 /* Applies datapath configuration from the database. Some of the changes are
3811 * actually applied in dpif_netdev_run(). */
3812 static int
3813 dpif_netdev_set_config(struct dpif *dpif, const struct smap *other_config)
3814 {
3815 struct dp_netdev *dp = get_dp_netdev(dpif);
3816 const char *cmask = smap_get(other_config, "pmd-cpu-mask");
3817 const char *pmd_rxq_assign = smap_get_def(other_config, "pmd-rxq-assign",
3818 "cycles");
3819 unsigned long long insert_prob =
3820 smap_get_ullong(other_config, "emc-insert-inv-prob",
3821 DEFAULT_EM_FLOW_INSERT_INV_PROB);
3822 uint32_t insert_min, cur_min;
3823 uint32_t tx_flush_interval, cur_tx_flush_interval;
3824 uint64_t rebalance_intvl;
3825
3826 tx_flush_interval = smap_get_int(other_config, "tx-flush-interval",
3827 DEFAULT_TX_FLUSH_INTERVAL);
3828 atomic_read_relaxed(&dp->tx_flush_interval, &cur_tx_flush_interval);
3829 if (tx_flush_interval != cur_tx_flush_interval) {
3830 atomic_store_relaxed(&dp->tx_flush_interval, tx_flush_interval);
3831 VLOG_INFO("Flushing interval for tx queues set to %"PRIu32" us",
3832 tx_flush_interval);
3833 }
3834
3835 if (!nullable_string_is_equal(dp->pmd_cmask, cmask)) {
3836 free(dp->pmd_cmask);
3837 dp->pmd_cmask = nullable_xstrdup(cmask);
3838 dp_netdev_request_reconfigure(dp);
3839 }
3840
3841 atomic_read_relaxed(&dp->emc_insert_min, &cur_min);
3842 if (insert_prob <= UINT32_MAX) {
3843 insert_min = insert_prob == 0 ? 0 : UINT32_MAX / insert_prob;
3844 } else {
3845 insert_min = DEFAULT_EM_FLOW_INSERT_MIN;
3846 insert_prob = DEFAULT_EM_FLOW_INSERT_INV_PROB;
3847 }
3848
3849 if (insert_min != cur_min) {
3850 atomic_store_relaxed(&dp->emc_insert_min, insert_min);
3851 if (insert_min == 0) {
3852 VLOG_INFO("EMC insertion probability changed to zero");
3853 } else {
3854 VLOG_INFO("EMC insertion probability changed to 1/%llu (~%.2f%%)",
3855 insert_prob, (100 / (float)insert_prob));
3856 }
3857 }
3858
3859 bool perf_enabled = smap_get_bool(other_config, "pmd-perf-metrics", false);
3860 bool cur_perf_enabled;
3861 atomic_read_relaxed(&dp->pmd_perf_metrics, &cur_perf_enabled);
3862 if (perf_enabled != cur_perf_enabled) {
3863 atomic_store_relaxed(&dp->pmd_perf_metrics, perf_enabled);
3864 if (perf_enabled) {
3865 VLOG_INFO("PMD performance metrics collection enabled");
3866 } else {
3867 VLOG_INFO("PMD performance metrics collection disabled");
3868 }
3869 }
3870
3871 bool smc_enable = smap_get_bool(other_config, "smc-enable", false);
3872 bool cur_smc;
3873 atomic_read_relaxed(&dp->smc_enable_db, &cur_smc);
3874 if (smc_enable != cur_smc) {
3875 atomic_store_relaxed(&dp->smc_enable_db, smc_enable);
3876 if (smc_enable) {
3877 VLOG_INFO("SMC cache is enabled");
3878 } else {
3879 VLOG_INFO("SMC cache is disabled");
3880 }
3881 }
3882
3883 bool pmd_rxq_assign_cyc = !strcmp(pmd_rxq_assign, "cycles");
3884 if (!pmd_rxq_assign_cyc && strcmp(pmd_rxq_assign, "roundrobin")) {
3885 VLOG_WARN("Unsupported Rxq to PMD assignment mode in pmd-rxq-assign. "
3886 "Defaulting to 'cycles'.");
3887 pmd_rxq_assign_cyc = true;
3888 pmd_rxq_assign = "cycles";
3889 }
3890 if (dp->pmd_rxq_assign_cyc != pmd_rxq_assign_cyc) {
3891 dp->pmd_rxq_assign_cyc = pmd_rxq_assign_cyc;
3892 VLOG_INFO("Rxq to PMD assignment mode changed to: \'%s\'.",
3893 pmd_rxq_assign);
3894 dp_netdev_request_reconfigure(dp);
3895 }
3896
3897 struct pmd_auto_lb *pmd_alb = &dp->pmd_alb;
3898 pmd_alb->auto_lb_requested = smap_get_bool(other_config, "pmd-auto-lb",
3899 false);
3900
3901 rebalance_intvl = smap_get_int(other_config, "pmd-auto-lb-rebal-interval",
3902 ALB_PMD_REBALANCE_POLL_INTERVAL);
3903
3904 /* Input is in min, convert it to msec. */
3905 rebalance_intvl =
3906 rebalance_intvl ? rebalance_intvl * MIN_TO_MSEC : MIN_TO_MSEC;
3907
3908 if (pmd_alb->rebalance_intvl != rebalance_intvl) {
3909 pmd_alb->rebalance_intvl = rebalance_intvl;
3910 }
3911
3912 set_pmd_auto_lb(dp);
3913 return 0;
3914 }
3915
3916 /* Parses affinity list and returns result in 'core_ids'. */
3917 static int
3918 parse_affinity_list(const char *affinity_list, unsigned *core_ids, int n_rxq)
3919 {
3920 unsigned i;
3921 char *list, *copy, *key, *value;
3922 int error = 0;
3923
3924 for (i = 0; i < n_rxq; i++) {
3925 core_ids[i] = OVS_CORE_UNSPEC;
3926 }
3927
3928 if (!affinity_list) {
3929 return 0;
3930 }
3931
3932 list = copy = xstrdup(affinity_list);
3933
3934 while (ofputil_parse_key_value(&list, &key, &value)) {
3935 int rxq_id, core_id;
3936
3937 if (!str_to_int(key, 0, &rxq_id) || rxq_id < 0
3938 || !str_to_int(value, 0, &core_id) || core_id < 0) {
3939 error = EINVAL;
3940 break;
3941 }
3942
3943 if (rxq_id < n_rxq) {
3944 core_ids[rxq_id] = core_id;
3945 }
3946 }
3947
3948 free(copy);
3949 return error;
3950 }
3951
3952 /* Parses 'affinity_list' and applies configuration if it is valid. */
3953 static int
3954 dpif_netdev_port_set_rxq_affinity(struct dp_netdev_port *port,
3955 const char *affinity_list)
3956 {
3957 unsigned *core_ids, i;
3958 int error = 0;
3959
3960 core_ids = xmalloc(port->n_rxq * sizeof *core_ids);
3961 if (parse_affinity_list(affinity_list, core_ids, port->n_rxq)) {
3962 error = EINVAL;
3963 goto exit;
3964 }
3965
3966 for (i = 0; i < port->n_rxq; i++) {
3967 port->rxqs[i].core_id = core_ids[i];
3968 }
3969
3970 exit:
3971 free(core_ids);
3972 return error;
3973 }
3974
3975 /* Returns 'true' if one of the 'port's RX queues exists in 'poll_list'
3976 * of given PMD thread. */
3977 static bool
3978 dpif_netdev_pmd_polls_port(struct dp_netdev_pmd_thread *pmd,
3979 struct dp_netdev_port *port)
3980 OVS_EXCLUDED(pmd->port_mutex)
3981 {
3982 struct rxq_poll *poll;
3983 bool found = false;
3984
3985 ovs_mutex_lock(&pmd->port_mutex);
3986 HMAP_FOR_EACH (poll, node, &pmd->poll_list) {
3987 if (port == poll->rxq->port) {
3988 found = true;
3989 break;
3990 }
3991 }
3992 ovs_mutex_unlock(&pmd->port_mutex);
3993 return found;
3994 }
3995
3996 /* Updates port configuration from the database. The changes are actually
3997 * applied in dpif_netdev_run(). */
3998 static int
3999 dpif_netdev_port_set_config(struct dpif *dpif, odp_port_t port_no,
4000 const struct smap *cfg)
4001 {
4002 struct dp_netdev *dp = get_dp_netdev(dpif);
4003 struct dp_netdev_port *port;
4004 int error = 0;
4005 const char *affinity_list = smap_get(cfg, "pmd-rxq-affinity");
4006 bool emc_enabled = smap_get_bool(cfg, "emc-enable", true);
4007
4008 ovs_mutex_lock(&dp->port_mutex);
4009 error = get_port_by_number(dp, port_no, &port);
4010 if (error) {
4011 goto unlock;
4012 }
4013
4014 if (emc_enabled != port->emc_enabled) {
4015 struct dp_netdev_pmd_thread *pmd;
4016 struct ds ds = DS_EMPTY_INITIALIZER;
4017 uint32_t cur_min, insert_prob;
4018
4019 port->emc_enabled = emc_enabled;
4020 /* Mark for reload all the threads that polls this port and request
4021 * for reconfiguration for the actual reloading of threads. */
4022 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
4023 if (dpif_netdev_pmd_polls_port(pmd, port)) {
4024 pmd->need_reload = true;
4025 }
4026 }
4027 dp_netdev_request_reconfigure(dp);
4028
4029 ds_put_format(&ds, "%s: EMC has been %s.",
4030 netdev_get_name(port->netdev),
4031 (emc_enabled) ? "enabled" : "disabled");
4032 if (emc_enabled) {
4033 ds_put_cstr(&ds, " Current insertion probability is ");
4034 atomic_read_relaxed(&dp->emc_insert_min, &cur_min);
4035 if (!cur_min) {
4036 ds_put_cstr(&ds, "zero.");
4037 } else {
4038 insert_prob = UINT32_MAX / cur_min;
4039 ds_put_format(&ds, "1/%"PRIu32" (~%.2f%%).",
4040 insert_prob, 100 / (float) insert_prob);
4041 }
4042 }
4043 VLOG_INFO("%s", ds_cstr(&ds));
4044 ds_destroy(&ds);
4045 }
4046
4047 /* Checking for RXq affinity changes. */
4048 if (!netdev_is_pmd(port->netdev)
4049 || nullable_string_is_equal(affinity_list, port->rxq_affinity_list)) {
4050 goto unlock;
4051 }
4052
4053 error = dpif_netdev_port_set_rxq_affinity(port, affinity_list);
4054 if (error) {
4055 goto unlock;
4056 }
4057 free(port->rxq_affinity_list);
4058 port->rxq_affinity_list = nullable_xstrdup(affinity_list);
4059
4060 dp_netdev_request_reconfigure(dp);
4061 unlock:
4062 ovs_mutex_unlock(&dp->port_mutex);
4063 return error;
4064 }
4065
4066 static int
4067 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
4068 uint32_t queue_id, uint32_t *priority)
4069 {
4070 *priority = queue_id;
4071 return 0;
4072 }
4073
4074 \f
4075 /* Creates and returns a new 'struct dp_netdev_actions', whose actions are
4076 * a copy of the 'size' bytes of 'actions' input parameters. */
4077 struct dp_netdev_actions *
4078 dp_netdev_actions_create(const struct nlattr *actions, size_t size)
4079 {
4080 struct dp_netdev_actions *netdev_actions;
4081
4082 netdev_actions = xmalloc(sizeof *netdev_actions + size);
4083 memcpy(netdev_actions->actions, actions, size);
4084 netdev_actions->size = size;
4085
4086 return netdev_actions;
4087 }
4088
4089 struct dp_netdev_actions *
4090 dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow)
4091 {
4092 return ovsrcu_get(struct dp_netdev_actions *, &flow->actions);
4093 }
4094
4095 static void
4096 dp_netdev_actions_free(struct dp_netdev_actions *actions)
4097 {
4098 free(actions);
4099 }
4100 \f
4101 static void
4102 dp_netdev_rxq_set_cycles(struct dp_netdev_rxq *rx,
4103 enum rxq_cycles_counter_type type,
4104 unsigned long long cycles)
4105 {
4106 atomic_store_relaxed(&rx->cycles[type], cycles);
4107 }
4108
4109 static void
4110 dp_netdev_rxq_add_cycles(struct dp_netdev_rxq *rx,
4111 enum rxq_cycles_counter_type type,
4112 unsigned long long cycles)
4113 {
4114 non_atomic_ullong_add(&rx->cycles[type], cycles);
4115 }
4116
4117 static uint64_t
4118 dp_netdev_rxq_get_cycles(struct dp_netdev_rxq *rx,
4119 enum rxq_cycles_counter_type type)
4120 {
4121 unsigned long long processing_cycles;
4122 atomic_read_relaxed(&rx->cycles[type], &processing_cycles);
4123 return processing_cycles;
4124 }
4125
4126 static void
4127 dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq *rx,
4128 unsigned long long cycles)
4129 {
4130 unsigned int idx = rx->intrvl_idx++ % PMD_RXQ_INTERVAL_MAX;
4131 atomic_store_relaxed(&rx->cycles_intrvl[idx], cycles);
4132 }
4133
4134 static uint64_t
4135 dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq *rx, unsigned idx)
4136 {
4137 unsigned long long processing_cycles;
4138 atomic_read_relaxed(&rx->cycles_intrvl[idx], &processing_cycles);
4139 return processing_cycles;
4140 }
4141
4142 #if ATOMIC_ALWAYS_LOCK_FREE_8B
4143 static inline bool
4144 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread *pmd)
4145 {
4146 bool pmd_perf_enabled;
4147 atomic_read_relaxed(&pmd->dp->pmd_perf_metrics, &pmd_perf_enabled);
4148 return pmd_perf_enabled;
4149 }
4150 #else
4151 /* If stores and reads of 64-bit integers are not atomic, the full PMD
4152 * performance metrics are not available as locked access to 64 bit
4153 * integers would be prohibitively expensive. */
4154 static inline bool
4155 pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread *pmd OVS_UNUSED)
4156 {
4157 return false;
4158 }
4159 #endif
4160
4161 static int
4162 dp_netdev_pmd_flush_output_on_port(struct dp_netdev_pmd_thread *pmd,
4163 struct tx_port *p)
4164 {
4165 int i;
4166 int tx_qid;
4167 int output_cnt;
4168 bool dynamic_txqs;
4169 struct cycle_timer timer;
4170 uint64_t cycles;
4171 uint32_t tx_flush_interval;
4172
4173 cycle_timer_start(&pmd->perf_stats, &timer);
4174
4175 dynamic_txqs = p->port->dynamic_txqs;
4176 if (dynamic_txqs) {
4177 tx_qid = dpif_netdev_xps_get_tx_qid(pmd, p);
4178 } else {
4179 tx_qid = pmd->static_tx_qid;
4180 }
4181
4182 output_cnt = dp_packet_batch_size(&p->output_pkts);
4183 ovs_assert(output_cnt > 0);
4184
4185 netdev_send(p->port->netdev, tx_qid, &p->output_pkts, dynamic_txqs);
4186 dp_packet_batch_init(&p->output_pkts);
4187
4188 /* Update time of the next flush. */
4189 atomic_read_relaxed(&pmd->dp->tx_flush_interval, &tx_flush_interval);
4190 p->flush_time = pmd->ctx.now + tx_flush_interval;
4191
4192 ovs_assert(pmd->n_output_batches > 0);
4193 pmd->n_output_batches--;
4194
4195 pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_SENT_PKTS, output_cnt);
4196 pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_SENT_BATCHES, 1);
4197
4198 /* Distribute send cycles evenly among transmitted packets and assign to
4199 * their respective rx queues. */
4200 cycles = cycle_timer_stop(&pmd->perf_stats, &timer) / output_cnt;
4201 for (i = 0; i < output_cnt; i++) {
4202 if (p->output_pkts_rxqs[i]) {
4203 dp_netdev_rxq_add_cycles(p->output_pkts_rxqs[i],
4204 RXQ_CYCLES_PROC_CURR, cycles);
4205 }
4206 }
4207
4208 return output_cnt;
4209 }
4210
4211 static int
4212 dp_netdev_pmd_flush_output_packets(struct dp_netdev_pmd_thread *pmd,
4213 bool force)
4214 {
4215 struct tx_port *p;
4216 int output_cnt = 0;
4217
4218 if (!pmd->n_output_batches) {
4219 return 0;
4220 }
4221
4222 HMAP_FOR_EACH (p, node, &pmd->send_port_cache) {
4223 if (!dp_packet_batch_is_empty(&p->output_pkts)
4224 && (force || pmd->ctx.now >= p->flush_time)) {
4225 output_cnt += dp_netdev_pmd_flush_output_on_port(pmd, p);
4226 }
4227 }
4228 return output_cnt;
4229 }
4230
4231 static int
4232 dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread *pmd,
4233 struct dp_netdev_rxq *rxq,
4234 odp_port_t port_no)
4235 {
4236 struct pmd_perf_stats *s = &pmd->perf_stats;
4237 struct dp_packet_batch batch;
4238 struct cycle_timer timer;
4239 int error;
4240 int batch_cnt = 0;
4241 int rem_qlen = 0, *qlen_p = NULL;
4242 uint64_t cycles;
4243
4244 /* Measure duration for polling and processing rx burst. */
4245 cycle_timer_start(&pmd->perf_stats, &timer);
4246
4247 pmd->ctx.last_rxq = rxq;
4248 dp_packet_batch_init(&batch);
4249
4250 /* Fetch the rx queue length only for vhostuser ports. */
4251 if (pmd_perf_metrics_enabled(pmd) && rxq->is_vhost) {
4252 qlen_p = &rem_qlen;
4253 }
4254
4255 error = netdev_rxq_recv(rxq->rx, &batch, qlen_p);
4256 if (!error) {
4257 /* At least one packet received. */
4258 *recirc_depth_get() = 0;
4259 pmd_thread_ctx_time_update(pmd);
4260 batch_cnt = batch.count;
4261 if (pmd_perf_metrics_enabled(pmd)) {
4262 /* Update batch histogram. */
4263 s->current.batches++;
4264 histogram_add_sample(&s->pkts_per_batch, batch_cnt);
4265 /* Update the maximum vhost rx queue fill level. */
4266 if (rxq->is_vhost && rem_qlen >= 0) {
4267 uint32_t qfill = batch_cnt + rem_qlen;
4268 if (qfill > s->current.max_vhost_qfill) {
4269 s->current.max_vhost_qfill = qfill;
4270 }
4271 }
4272 }
4273 /* Process packet batch. */
4274 dp_netdev_input(pmd, &batch, port_no);
4275
4276 /* Assign processing cycles to rx queue. */
4277 cycles = cycle_timer_stop(&pmd->perf_stats, &timer);
4278 dp_netdev_rxq_add_cycles(rxq, RXQ_CYCLES_PROC_CURR, cycles);
4279
4280 dp_netdev_pmd_flush_output_packets(pmd, false);
4281 } else {
4282 /* Discard cycles. */
4283 cycle_timer_stop(&pmd->perf_stats, &timer);
4284 if (error != EAGAIN && error != EOPNOTSUPP) {
4285 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4286
4287 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
4288 netdev_rxq_get_name(rxq->rx), ovs_strerror(error));
4289 }
4290 }
4291
4292 pmd->ctx.last_rxq = NULL;
4293
4294 return batch_cnt;
4295 }
4296
4297 static struct tx_port *
4298 tx_port_lookup(const struct hmap *hmap, odp_port_t port_no)
4299 {
4300 struct tx_port *tx;
4301
4302 HMAP_FOR_EACH_IN_BUCKET (tx, node, hash_port_no(port_no), hmap) {
4303 if (tx->port->port_no == port_no) {
4304 return tx;
4305 }
4306 }
4307
4308 return NULL;
4309 }
4310
4311 static int
4312 port_reconfigure(struct dp_netdev_port *port)
4313 {
4314 struct netdev *netdev = port->netdev;
4315 int i, err;
4316
4317 /* Closes the existing 'rxq's. */
4318 for (i = 0; i < port->n_rxq; i++) {
4319 netdev_rxq_close(port->rxqs[i].rx);
4320 port->rxqs[i].rx = NULL;
4321 }
4322 unsigned last_nrxq = port->n_rxq;
4323 port->n_rxq = 0;
4324
4325 /* Allows 'netdev' to apply the pending configuration changes. */
4326 if (netdev_is_reconf_required(netdev) || port->need_reconfigure) {
4327 err = netdev_reconfigure(netdev);
4328 if (err && (err != EOPNOTSUPP)) {
4329 VLOG_ERR("Failed to set interface %s new configuration",
4330 netdev_get_name(netdev));
4331 return err;
4332 }
4333 }
4334 /* If the netdev_reconfigure() above succeeds, reopens the 'rxq's. */
4335 port->rxqs = xrealloc(port->rxqs,
4336 sizeof *port->rxqs * netdev_n_rxq(netdev));
4337 /* Realloc 'used' counters for tx queues. */
4338 free(port->txq_used);
4339 port->txq_used = xcalloc(netdev_n_txq(netdev), sizeof *port->txq_used);
4340
4341 for (i = 0; i < netdev_n_rxq(netdev); i++) {
4342 bool new_queue = i >= last_nrxq;
4343 if (new_queue) {
4344 memset(&port->rxqs[i], 0, sizeof port->rxqs[i]);
4345 }
4346
4347 port->rxqs[i].port = port;
4348 port->rxqs[i].is_vhost = !strncmp(port->type, "dpdkvhost", 9);
4349
4350 err = netdev_rxq_open(netdev, &port->rxqs[i].rx, i);
4351 if (err) {
4352 return err;
4353 }
4354 port->n_rxq++;
4355 }
4356
4357 /* Parse affinity list to apply configuration for new queues. */
4358 dpif_netdev_port_set_rxq_affinity(port, port->rxq_affinity_list);
4359
4360 /* If reconfiguration was successful mark it as such, so we can use it */
4361 port->need_reconfigure = false;
4362
4363 return 0;
4364 }
4365
4366 struct rr_numa_list {
4367 struct hmap numas; /* Contains 'struct rr_numa' */
4368 };
4369
4370 struct rr_numa {
4371 struct hmap_node node;
4372
4373 int numa_id;
4374
4375 /* Non isolated pmds on numa node 'numa_id' */
4376 struct dp_netdev_pmd_thread **pmds;
4377 int n_pmds;
4378
4379 int cur_index;
4380 bool idx_inc;
4381 };
4382
4383 static struct rr_numa *
4384 rr_numa_list_lookup(struct rr_numa_list *rr, int numa_id)
4385 {
4386 struct rr_numa *numa;
4387
4388 HMAP_FOR_EACH_WITH_HASH (numa, node, hash_int(numa_id, 0), &rr->numas) {
4389 if (numa->numa_id == numa_id) {
4390 return numa;
4391 }
4392 }
4393
4394 return NULL;
4395 }
4396
4397 /* Returns the next node in numa list following 'numa' in round-robin fashion.
4398 * Returns first node if 'numa' is a null pointer or the last node in 'rr'.
4399 * Returns NULL if 'rr' numa list is empty. */
4400 static struct rr_numa *
4401 rr_numa_list_next(struct rr_numa_list *rr, const struct rr_numa *numa)
4402 {
4403 struct hmap_node *node = NULL;
4404
4405 if (numa) {
4406 node = hmap_next(&rr->numas, &numa->node);
4407 }
4408 if (!node) {
4409 node = hmap_first(&rr->numas);
4410 }
4411
4412 return (node) ? CONTAINER_OF(node, struct rr_numa, node) : NULL;
4413 }
4414
4415 static void
4416 rr_numa_list_populate(struct dp_netdev *dp, struct rr_numa_list *rr)
4417 {
4418 struct dp_netdev_pmd_thread *pmd;
4419 struct rr_numa *numa;
4420
4421 hmap_init(&rr->numas);
4422
4423 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
4424 if (pmd->core_id == NON_PMD_CORE_ID || pmd->isolated) {
4425 continue;
4426 }
4427
4428 numa = rr_numa_list_lookup(rr, pmd->numa_id);
4429 if (!numa) {
4430 numa = xzalloc(sizeof *numa);
4431 numa->numa_id = pmd->numa_id;
4432 hmap_insert(&rr->numas, &numa->node, hash_int(pmd->numa_id, 0));
4433 }
4434 numa->n_pmds++;
4435 numa->pmds = xrealloc(numa->pmds, numa->n_pmds * sizeof *numa->pmds);
4436 numa->pmds[numa->n_pmds - 1] = pmd;
4437 /* At least one pmd so initialise curr_idx and idx_inc. */
4438 numa->cur_index = 0;
4439 numa->idx_inc = true;
4440 }
4441 }
4442
4443 /*
4444 * Returns the next pmd from the numa node.
4445 *
4446 * If 'updown' is 'true' it will alternate between selecting the next pmd in
4447 * either an up or down walk, switching between up/down when the first or last
4448 * core is reached. e.g. 1,2,3,3,2,1,1,2...
4449 *
4450 * If 'updown' is 'false' it will select the next pmd wrapping around when last
4451 * core reached. e.g. 1,2,3,1,2,3,1,2...
4452 */
4453 static struct dp_netdev_pmd_thread *
4454 rr_numa_get_pmd(struct rr_numa *numa, bool updown)
4455 {
4456 int numa_idx = numa->cur_index;
4457
4458 if (numa->idx_inc == true) {
4459 /* Incrementing through list of pmds. */
4460 if (numa->cur_index == numa->n_pmds-1) {
4461 /* Reached the last pmd. */
4462 if (updown) {
4463 numa->idx_inc = false;
4464 } else {
4465 numa->cur_index = 0;
4466 }
4467 } else {
4468 numa->cur_index++;
4469 }
4470 } else {
4471 /* Decrementing through list of pmds. */
4472 if (numa->cur_index == 0) {
4473 /* Reached the first pmd. */
4474 numa->idx_inc = true;
4475 } else {
4476 numa->cur_index--;
4477 }
4478 }
4479 return numa->pmds[numa_idx];
4480 }
4481
4482 static void
4483 rr_numa_list_destroy(struct rr_numa_list *rr)
4484 {
4485 struct rr_numa *numa;
4486
4487 HMAP_FOR_EACH_POP (numa, node, &rr->numas) {
4488 free(numa->pmds);
4489 free(numa);
4490 }
4491 hmap_destroy(&rr->numas);
4492 }
4493
4494 /* Sort Rx Queues by the processing cycles they are consuming. */
4495 static int
4496 compare_rxq_cycles(const void *a, const void *b)
4497 {
4498 struct dp_netdev_rxq *qa;
4499 struct dp_netdev_rxq *qb;
4500 uint64_t cycles_qa, cycles_qb;
4501
4502 qa = *(struct dp_netdev_rxq **) a;
4503 qb = *(struct dp_netdev_rxq **) b;
4504
4505 cycles_qa = dp_netdev_rxq_get_cycles(qa, RXQ_CYCLES_PROC_HIST);
4506 cycles_qb = dp_netdev_rxq_get_cycles(qb, RXQ_CYCLES_PROC_HIST);
4507
4508 if (cycles_qa != cycles_qb) {
4509 return (cycles_qa < cycles_qb) ? 1 : -1;
4510 } else {
4511 /* Cycles are the same so tiebreak on port/queue id.
4512 * Tiebreaking (as opposed to return 0) ensures consistent
4513 * sort results across multiple OS's. */
4514 uint32_t port_qa = odp_to_u32(qa->port->port_no);
4515 uint32_t port_qb = odp_to_u32(qb->port->port_no);
4516 if (port_qa != port_qb) {
4517 return port_qa > port_qb ? 1 : -1;
4518 } else {
4519 return netdev_rxq_get_queue_id(qa->rx)
4520 - netdev_rxq_get_queue_id(qb->rx);
4521 }
4522 }
4523 }
4524
4525 /* Assign pmds to queues. If 'pinned' is true, assign pmds to pinned
4526 * queues and marks the pmds as isolated. Otherwise, assign non isolated
4527 * pmds to unpinned queues.
4528 *
4529 * The function doesn't touch the pmd threads, it just stores the assignment
4530 * in the 'pmd' member of each rxq. */
4531 static void
4532 rxq_scheduling(struct dp_netdev *dp, bool pinned) OVS_REQUIRES(dp->port_mutex)
4533 {
4534 struct dp_netdev_port *port;
4535 struct rr_numa_list rr;
4536 struct rr_numa *non_local_numa = NULL;
4537 struct dp_netdev_rxq ** rxqs = NULL;
4538 int n_rxqs = 0;
4539 struct rr_numa *numa = NULL;
4540 int numa_id;
4541 bool assign_cyc = dp->pmd_rxq_assign_cyc;
4542
4543 HMAP_FOR_EACH (port, node, &dp->ports) {
4544 if (!netdev_is_pmd(port->netdev)) {
4545 continue;
4546 }
4547
4548 for (int qid = 0; qid < port->n_rxq; qid++) {
4549 struct dp_netdev_rxq *q = &port->rxqs[qid];
4550
4551 if (pinned && q->core_id != OVS_CORE_UNSPEC) {
4552 struct dp_netdev_pmd_thread *pmd;
4553
4554 pmd = dp_netdev_get_pmd(dp, q->core_id);
4555 if (!pmd) {
4556 VLOG_WARN("There is no PMD thread on core %d. Queue "
4557 "%d on port \'%s\' will not be polled.",
4558 q->core_id, qid, netdev_get_name(port->netdev));
4559 } else {
4560 q->pmd = pmd;
4561 pmd->isolated = true;
4562 dp_netdev_pmd_unref(pmd);
4563 }
4564 } else if (!pinned && q->core_id == OVS_CORE_UNSPEC) {
4565 uint64_t cycle_hist = 0;
4566
4567 if (n_rxqs == 0) {
4568 rxqs = xmalloc(sizeof *rxqs);
4569 } else {
4570 rxqs = xrealloc(rxqs, sizeof *rxqs * (n_rxqs + 1));
4571 }
4572
4573 if (assign_cyc) {
4574 /* Sum the queue intervals and store the cycle history. */
4575 for (unsigned i = 0; i < PMD_RXQ_INTERVAL_MAX; i++) {
4576 cycle_hist += dp_netdev_rxq_get_intrvl_cycles(q, i);
4577 }
4578 dp_netdev_rxq_set_cycles(q, RXQ_CYCLES_PROC_HIST,
4579 cycle_hist);
4580 }
4581 /* Store the queue. */
4582 rxqs[n_rxqs++] = q;
4583 }
4584 }
4585 }
4586
4587 if (n_rxqs > 1 && assign_cyc) {
4588 /* Sort the queues in order of the processing cycles
4589 * they consumed during their last pmd interval. */
4590 qsort(rxqs, n_rxqs, sizeof *rxqs, compare_rxq_cycles);
4591 }
4592
4593 rr_numa_list_populate(dp, &rr);
4594 /* Assign the sorted queues to pmds in round robin. */
4595 for (int i = 0; i < n_rxqs; i++) {
4596 numa_id = netdev_get_numa_id(rxqs[i]->port->netdev);
4597 numa = rr_numa_list_lookup(&rr, numa_id);
4598 if (!numa) {
4599 /* There are no pmds on the queue's local NUMA node.
4600 Round robin on the NUMA nodes that do have pmds. */
4601 non_local_numa = rr_numa_list_next(&rr, non_local_numa);
4602 if (!non_local_numa) {
4603 VLOG_ERR("There is no available (non-isolated) pmd "
4604 "thread for port \'%s\' queue %d. This queue "
4605 "will not be polled. Is pmd-cpu-mask set to "
4606 "zero? Or are all PMDs isolated to other "
4607 "queues?", netdev_rxq_get_name(rxqs[i]->rx),
4608 netdev_rxq_get_queue_id(rxqs[i]->rx));
4609 continue;
4610 }
4611 rxqs[i]->pmd = rr_numa_get_pmd(non_local_numa, assign_cyc);
4612 VLOG_WARN("There's no available (non-isolated) pmd thread "
4613 "on numa node %d. Queue %d on port \'%s\' will "
4614 "be assigned to the pmd on core %d "
4615 "(numa node %d). Expect reduced performance.",
4616 numa_id, netdev_rxq_get_queue_id(rxqs[i]->rx),
4617 netdev_rxq_get_name(rxqs[i]->rx),
4618 rxqs[i]->pmd->core_id, rxqs[i]->pmd->numa_id);
4619 } else {
4620 rxqs[i]->pmd = rr_numa_get_pmd(numa, assign_cyc);
4621 if (assign_cyc) {
4622 VLOG_INFO("Core %d on numa node %d assigned port \'%s\' "
4623 "rx queue %d "
4624 "(measured processing cycles %"PRIu64").",
4625 rxqs[i]->pmd->core_id, numa_id,
4626 netdev_rxq_get_name(rxqs[i]->rx),
4627 netdev_rxq_get_queue_id(rxqs[i]->rx),
4628 dp_netdev_rxq_get_cycles(rxqs[i],
4629 RXQ_CYCLES_PROC_HIST));
4630 } else {
4631 VLOG_INFO("Core %d on numa node %d assigned port \'%s\' "
4632 "rx queue %d.", rxqs[i]->pmd->core_id, numa_id,
4633 netdev_rxq_get_name(rxqs[i]->rx),
4634 netdev_rxq_get_queue_id(rxqs[i]->rx));
4635 }
4636 }
4637 }
4638
4639 rr_numa_list_destroy(&rr);
4640 free(rxqs);
4641 }
4642
4643 static void
4644 reload_affected_pmds(struct dp_netdev *dp)
4645 {
4646 struct dp_netdev_pmd_thread *pmd;
4647
4648 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
4649 if (pmd->need_reload) {
4650 flow_mark_flush(pmd);
4651 dp_netdev_reload_pmd__(pmd);
4652 }
4653 }
4654
4655 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
4656 if (pmd->need_reload) {
4657 if (pmd->core_id != NON_PMD_CORE_ID) {
4658 bool reload;
4659
4660 do {
4661 atomic_read_explicit(&pmd->reload, &reload,
4662 memory_order_acquire);
4663 } while (reload);
4664 }
4665 pmd->need_reload = false;
4666 }
4667 }
4668 }
4669
4670 static void
4671 reconfigure_pmd_threads(struct dp_netdev *dp)
4672 OVS_REQUIRES(dp->port_mutex)
4673 {
4674 struct dp_netdev_pmd_thread *pmd;
4675 struct ovs_numa_dump *pmd_cores;
4676 struct ovs_numa_info_core *core;
4677 struct hmapx to_delete = HMAPX_INITIALIZER(&to_delete);
4678 struct hmapx_node *node;
4679 bool changed = false;
4680 bool need_to_adjust_static_tx_qids = false;
4681
4682 /* The pmd threads should be started only if there's a pmd port in the
4683 * datapath. If the user didn't provide any "pmd-cpu-mask", we start
4684 * NR_PMD_THREADS per numa node. */
4685 if (!has_pmd_port(dp)) {
4686 pmd_cores = ovs_numa_dump_n_cores_per_numa(0);
4687 } else if (dp->pmd_cmask && dp->pmd_cmask[0]) {
4688 pmd_cores = ovs_numa_dump_cores_with_cmask(dp->pmd_cmask);
4689 } else {
4690 pmd_cores = ovs_numa_dump_n_cores_per_numa(NR_PMD_THREADS);
4691 }
4692
4693 /* We need to adjust 'static_tx_qid's only if we're reducing number of
4694 * PMD threads. Otherwise, new threads will allocate all the freed ids. */
4695 if (ovs_numa_dump_count(pmd_cores) < cmap_count(&dp->poll_threads) - 1) {
4696 /* Adjustment is required to keep 'static_tx_qid's sequential and
4697 * avoid possible issues, for example, imbalanced tx queue usage
4698 * and unnecessary locking caused by remapping on netdev level. */
4699 need_to_adjust_static_tx_qids = true;
4700 }
4701
4702 /* Check for unwanted pmd threads */
4703 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
4704 if (pmd->core_id == NON_PMD_CORE_ID) {
4705 continue;
4706 }
4707 if (!ovs_numa_dump_contains_core(pmd_cores, pmd->numa_id,
4708 pmd->core_id)) {
4709 hmapx_add(&to_delete, pmd);
4710 } else if (need_to_adjust_static_tx_qids) {
4711 pmd->need_reload = true;
4712 }
4713 }
4714
4715 HMAPX_FOR_EACH (node, &to_delete) {
4716 pmd = (struct dp_netdev_pmd_thread *) node->data;
4717 VLOG_INFO("PMD thread on numa_id: %d, core id: %2d destroyed.",
4718 pmd->numa_id, pmd->core_id);
4719 dp_netdev_del_pmd(dp, pmd);
4720 }
4721 changed = !hmapx_is_empty(&to_delete);
4722 hmapx_destroy(&to_delete);
4723
4724 if (need_to_adjust_static_tx_qids) {
4725 /* 'static_tx_qid's are not sequential now.
4726 * Reload remaining threads to fix this. */
4727 reload_affected_pmds(dp);
4728 }
4729
4730 /* Check for required new pmd threads */
4731 FOR_EACH_CORE_ON_DUMP(core, pmd_cores) {
4732 pmd = dp_netdev_get_pmd(dp, core->core_id);
4733 if (!pmd) {
4734 pmd = xzalloc(sizeof *pmd);
4735 dp_netdev_configure_pmd(pmd, dp, core->core_id, core->numa_id);
4736 pmd->thread = ovs_thread_create("pmd", pmd_thread_main, pmd);
4737 VLOG_INFO("PMD thread on numa_id: %d, core id: %2d created.",
4738 pmd->numa_id, pmd->core_id);
4739 changed = true;
4740 } else {
4741 dp_netdev_pmd_unref(pmd);
4742 }
4743 }
4744
4745 if (changed) {
4746 struct ovs_numa_info_numa *numa;
4747
4748 /* Log the number of pmd threads per numa node. */
4749 FOR_EACH_NUMA_ON_DUMP (numa, pmd_cores) {
4750 VLOG_INFO("There are %"PRIuSIZE" pmd threads on numa node %d",
4751 numa->n_cores, numa->numa_id);
4752 }
4753 }
4754
4755 ovs_numa_dump_destroy(pmd_cores);
4756 }
4757
4758 static void
4759 pmd_remove_stale_ports(struct dp_netdev *dp,
4760 struct dp_netdev_pmd_thread *pmd)
4761 OVS_EXCLUDED(pmd->port_mutex)
4762 OVS_REQUIRES(dp->port_mutex)
4763 {
4764 struct rxq_poll *poll, *poll_next;
4765 struct tx_port *tx, *tx_next;
4766
4767 ovs_mutex_lock(&pmd->port_mutex);
4768 HMAP_FOR_EACH_SAFE (poll, poll_next, node, &pmd->poll_list) {
4769 struct dp_netdev_port *port = poll->rxq->port;
4770
4771 if (port->need_reconfigure
4772 || !hmap_contains(&dp->ports, &port->node)) {
4773 dp_netdev_del_rxq_from_pmd(pmd, poll);
4774 }
4775 }
4776 HMAP_FOR_EACH_SAFE (tx, tx_next, node, &pmd->tx_ports) {
4777 struct dp_netdev_port *port = tx->port;
4778
4779 if (port->need_reconfigure
4780 || !hmap_contains(&dp->ports, &port->node)) {
4781 dp_netdev_del_port_tx_from_pmd(pmd, tx);
4782 }
4783 }
4784 ovs_mutex_unlock(&pmd->port_mutex);
4785 }
4786
4787 /* Must be called each time a port is added/removed or the cmask changes.
4788 * This creates and destroys pmd threads, reconfigures ports, opens their
4789 * rxqs and assigns all rxqs/txqs to pmd threads. */
4790 static void
4791 reconfigure_datapath(struct dp_netdev *dp)
4792 OVS_REQUIRES(dp->port_mutex)
4793 {
4794 struct dp_netdev_pmd_thread *pmd;
4795 struct dp_netdev_port *port;
4796 int wanted_txqs;
4797
4798 dp->last_reconfigure_seq = seq_read(dp->reconfigure_seq);
4799
4800 /* Step 1: Adjust the pmd threads based on the datapath ports, the cores
4801 * on the system and the user configuration. */
4802 reconfigure_pmd_threads(dp);
4803
4804 wanted_txqs = cmap_count(&dp->poll_threads);
4805
4806 /* The number of pmd threads might have changed, or a port can be new:
4807 * adjust the txqs. */
4808 HMAP_FOR_EACH (port, node, &dp->ports) {
4809 netdev_set_tx_multiq(port->netdev, wanted_txqs);
4810 }
4811
4812 /* Step 2: Remove from the pmd threads ports that have been removed or
4813 * need reconfiguration. */
4814
4815 /* Check for all the ports that need reconfiguration. We cache this in
4816 * 'port->need_reconfigure', because netdev_is_reconf_required() can
4817 * change at any time. */
4818 HMAP_FOR_EACH (port, node, &dp->ports) {
4819 if (netdev_is_reconf_required(port->netdev)) {
4820 port->need_reconfigure = true;
4821 }
4822 }
4823
4824 /* Remove from the pmd threads all the ports that have been deleted or
4825 * need reconfiguration. */
4826 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
4827 pmd_remove_stale_ports(dp, pmd);
4828 }
4829
4830 /* Reload affected pmd threads. We must wait for the pmd threads before
4831 * reconfiguring the ports, because a port cannot be reconfigured while
4832 * it's being used. */
4833 reload_affected_pmds(dp);
4834
4835 /* Step 3: Reconfigure ports. */
4836
4837 /* We only reconfigure the ports that we determined above, because they're
4838 * not being used by any pmd thread at the moment. If a port fails to
4839 * reconfigure we remove it from the datapath. */
4840 struct dp_netdev_port *next_port;
4841 HMAP_FOR_EACH_SAFE (port, next_port, node, &dp->ports) {
4842 int err;
4843
4844 if (!port->need_reconfigure) {
4845 continue;
4846 }
4847
4848 err = port_reconfigure(port);
4849 if (err) {
4850 hmap_remove(&dp->ports, &port->node);
4851 seq_change(dp->port_seq);
4852 port_destroy(port);
4853 } else {
4854 port->dynamic_txqs = netdev_n_txq(port->netdev) < wanted_txqs;
4855 }
4856 }
4857
4858 /* Step 4: Compute new rxq scheduling. We don't touch the pmd threads
4859 * for now, we just update the 'pmd' pointer in each rxq to point to the
4860 * wanted thread according to the scheduling policy. */
4861
4862 /* Reset all the pmd threads to non isolated. */
4863 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
4864 pmd->isolated = false;
4865 }
4866
4867 /* Reset all the queues to unassigned */
4868 HMAP_FOR_EACH (port, node, &dp->ports) {
4869 for (int i = 0; i < port->n_rxq; i++) {
4870 port->rxqs[i].pmd = NULL;
4871 }
4872 }
4873
4874 /* Add pinned queues and mark pmd threads isolated. */
4875 rxq_scheduling(dp, true);
4876
4877 /* Add non-pinned queues. */
4878 rxq_scheduling(dp, false);
4879
4880 /* Step 5: Remove queues not compliant with new scheduling. */
4881 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
4882 struct rxq_poll *poll, *poll_next;
4883
4884 ovs_mutex_lock(&pmd->port_mutex);
4885 HMAP_FOR_EACH_SAFE (poll, poll_next, node, &pmd->poll_list) {
4886 if (poll->rxq->pmd != pmd) {
4887 dp_netdev_del_rxq_from_pmd(pmd, poll);
4888 }
4889 }
4890 ovs_mutex_unlock(&pmd->port_mutex);
4891 }
4892
4893 /* Reload affected pmd threads. We must wait for the pmd threads to remove
4894 * the old queues before readding them, otherwise a queue can be polled by
4895 * two threads at the same time. */
4896 reload_affected_pmds(dp);
4897
4898 /* Step 6: Add queues from scheduling, if they're not there already. */
4899 HMAP_FOR_EACH (port, node, &dp->ports) {
4900 if (!netdev_is_pmd(port->netdev)) {
4901 continue;
4902 }
4903
4904 for (int qid = 0; qid < port->n_rxq; qid++) {
4905 struct dp_netdev_rxq *q = &port->rxqs[qid];
4906
4907 if (q->pmd) {
4908 ovs_mutex_lock(&q->pmd->port_mutex);
4909 dp_netdev_add_rxq_to_pmd(q->pmd, q);
4910 ovs_mutex_unlock(&q->pmd->port_mutex);
4911 }
4912 }
4913 }
4914
4915 /* Add every port to the tx cache of every pmd thread, if it's not
4916 * there already and if this pmd has at least one rxq to poll. */
4917 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
4918 ovs_mutex_lock(&pmd->port_mutex);
4919 if (hmap_count(&pmd->poll_list) || pmd->core_id == NON_PMD_CORE_ID) {
4920 HMAP_FOR_EACH (port, node, &dp->ports) {
4921 dp_netdev_add_port_tx_to_pmd(pmd, port);
4922 }
4923 }
4924 ovs_mutex_unlock(&pmd->port_mutex);
4925 }
4926
4927 /* Reload affected pmd threads. */
4928 reload_affected_pmds(dp);
4929
4930 /* Check if PMD Auto LB is to be enabled */
4931 set_pmd_auto_lb(dp);
4932 }
4933
4934 /* Returns true if one of the netdevs in 'dp' requires a reconfiguration */
4935 static bool
4936 ports_require_restart(const struct dp_netdev *dp)
4937 OVS_REQUIRES(dp->port_mutex)
4938 {
4939 struct dp_netdev_port *port;
4940
4941 HMAP_FOR_EACH (port, node, &dp->ports) {
4942 if (netdev_is_reconf_required(port->netdev)) {
4943 return true;
4944 }
4945 }
4946
4947 return false;
4948 }
4949
4950 /* Calculates variance in the values stored in array 'a'. 'n' is the number
4951 * of elements in array to be considered for calculating vairance.
4952 * Usage example: data array 'a' contains the processing load of each pmd and
4953 * 'n' is the number of PMDs. It returns the variance in processing load of
4954 * PMDs*/
4955 static uint64_t
4956 variance(uint64_t a[], int n)
4957 {
4958 /* Compute mean (average of elements). */
4959 uint64_t sum = 0;
4960 uint64_t mean = 0;
4961 uint64_t sqDiff = 0;
4962
4963 if (!n) {
4964 return 0;
4965 }
4966
4967 for (int i = 0; i < n; i++) {
4968 sum += a[i];
4969 }
4970
4971 if (sum) {
4972 mean = sum / n;
4973
4974 /* Compute sum squared differences with mean. */
4975 for (int i = 0; i < n; i++) {
4976 sqDiff += (a[i] - mean)*(a[i] - mean);
4977 }
4978 }
4979 return (sqDiff ? (sqDiff / n) : 0);
4980 }
4981
4982
4983 /* Returns the variance in the PMDs usage as part of dry run of rxqs
4984 * assignment to PMDs. */
4985 static bool
4986 get_dry_run_variance(struct dp_netdev *dp, uint32_t *core_list,
4987 uint32_t num_pmds, uint64_t *predicted_variance)
4988 OVS_REQUIRES(dp->port_mutex)
4989 {
4990 struct dp_netdev_port *port;
4991 struct dp_netdev_pmd_thread *pmd;
4992 struct dp_netdev_rxq **rxqs = NULL;
4993 struct rr_numa *numa = NULL;
4994 struct rr_numa_list rr;
4995 int n_rxqs = 0;
4996 bool ret = false;
4997 uint64_t *pmd_usage;
4998
4999 if (!predicted_variance) {
5000 return ret;
5001 }
5002
5003 pmd_usage = xcalloc(num_pmds, sizeof(uint64_t));
5004
5005 HMAP_FOR_EACH (port, node, &dp->ports) {
5006 if (!netdev_is_pmd(port->netdev)) {
5007 continue;
5008 }
5009
5010 for (int qid = 0; qid < port->n_rxq; qid++) {
5011 struct dp_netdev_rxq *q = &port->rxqs[qid];
5012 uint64_t cycle_hist = 0;
5013
5014 if (q->pmd->isolated) {
5015 continue;
5016 }
5017
5018 if (n_rxqs == 0) {
5019 rxqs = xmalloc(sizeof *rxqs);
5020 } else {
5021 rxqs = xrealloc(rxqs, sizeof *rxqs * (n_rxqs + 1));
5022 }
5023
5024 /* Sum the queue intervals and store the cycle history. */
5025 for (unsigned i = 0; i < PMD_RXQ_INTERVAL_MAX; i++) {
5026 cycle_hist += dp_netdev_rxq_get_intrvl_cycles(q, i);
5027 }
5028 dp_netdev_rxq_set_cycles(q, RXQ_CYCLES_PROC_HIST,
5029 cycle_hist);
5030 /* Store the queue. */
5031 rxqs[n_rxqs++] = q;
5032 }
5033 }
5034 if (n_rxqs > 1) {
5035 /* Sort the queues in order of the processing cycles
5036 * they consumed during their last pmd interval. */
5037 qsort(rxqs, n_rxqs, sizeof *rxqs, compare_rxq_cycles);
5038 }
5039 rr_numa_list_populate(dp, &rr);
5040
5041 for (int i = 0; i < n_rxqs; i++) {
5042 int numa_id = netdev_get_numa_id(rxqs[i]->port->netdev);
5043 numa = rr_numa_list_lookup(&rr, numa_id);
5044 if (!numa) {
5045 /* Abort if cross NUMA polling. */
5046 VLOG_DBG("PMD auto lb dry run."
5047 " Aborting due to cross-numa polling.");
5048 goto cleanup;
5049 }
5050
5051 pmd = rr_numa_get_pmd(numa, true);
5052 VLOG_DBG("PMD auto lb dry run. Predicted: Core %d on numa node %d "
5053 "to be assigned port \'%s\' rx queue %d "
5054 "(measured processing cycles %"PRIu64").",
5055 pmd->core_id, numa_id,
5056 netdev_rxq_get_name(rxqs[i]->rx),
5057 netdev_rxq_get_queue_id(rxqs[i]->rx),
5058 dp_netdev_rxq_get_cycles(rxqs[i], RXQ_CYCLES_PROC_HIST));
5059
5060 for (int id = 0; id < num_pmds; id++) {
5061 if (pmd->core_id == core_list[id]) {
5062 /* Add the processing cycles of rxq to pmd polling it. */
5063 pmd_usage[id] += dp_netdev_rxq_get_cycles(rxqs[i],
5064 RXQ_CYCLES_PROC_HIST);
5065 }
5066 }
5067 }
5068
5069 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
5070 uint64_t total_cycles = 0;
5071
5072 if ((pmd->core_id == NON_PMD_CORE_ID) || pmd->isolated) {
5073 continue;
5074 }
5075
5076 /* Get the total pmd cycles for an interval. */
5077 atomic_read_relaxed(&pmd->intrvl_cycles, &total_cycles);
5078 /* Estimate the cycles to cover all intervals. */
5079 total_cycles *= PMD_RXQ_INTERVAL_MAX;
5080 for (int id = 0; id < num_pmds; id++) {
5081 if (pmd->core_id == core_list[id]) {
5082 if (pmd_usage[id]) {
5083 pmd_usage[id] = (pmd_usage[id] * 100) / total_cycles;
5084 }
5085 VLOG_DBG("PMD auto lb dry run. Predicted: Core %d, "
5086 "usage %"PRIu64"", pmd->core_id, pmd_usage[id]);
5087 }
5088 }
5089 }
5090 *predicted_variance = variance(pmd_usage, num_pmds);
5091 ret = true;
5092
5093 cleanup:
5094 rr_numa_list_destroy(&rr);
5095 free(rxqs);
5096 free(pmd_usage);
5097 return ret;
5098 }
5099
5100 /* Does the dry run of Rxq assignment to PMDs and returns true if it gives
5101 * better distribution of load on PMDs. */
5102 static bool
5103 pmd_rebalance_dry_run(struct dp_netdev *dp)
5104 OVS_REQUIRES(dp->port_mutex)
5105 {
5106 struct dp_netdev_pmd_thread *pmd;
5107 uint64_t *curr_pmd_usage;
5108
5109 uint64_t curr_variance;
5110 uint64_t new_variance;
5111 uint64_t improvement = 0;
5112 uint32_t num_pmds;
5113 uint32_t *pmd_corelist;
5114 struct rxq_poll *poll;
5115 bool ret;
5116
5117 num_pmds = cmap_count(&dp->poll_threads);
5118
5119 if (num_pmds > 1) {
5120 curr_pmd_usage = xcalloc(num_pmds, sizeof(uint64_t));
5121 pmd_corelist = xcalloc(num_pmds, sizeof(uint32_t));
5122 } else {
5123 return false;
5124 }
5125
5126 num_pmds = 0;
5127 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
5128 uint64_t total_cycles = 0;
5129 uint64_t total_proc = 0;
5130
5131 if ((pmd->core_id == NON_PMD_CORE_ID) || pmd->isolated) {
5132 continue;
5133 }
5134
5135 /* Get the total pmd cycles for an interval. */
5136 atomic_read_relaxed(&pmd->intrvl_cycles, &total_cycles);
5137 /* Estimate the cycles to cover all intervals. */
5138 total_cycles *= PMD_RXQ_INTERVAL_MAX;
5139
5140 ovs_mutex_lock(&pmd->port_mutex);
5141 HMAP_FOR_EACH (poll, node, &pmd->poll_list) {
5142 for (unsigned i = 0; i < PMD_RXQ_INTERVAL_MAX; i++) {
5143 total_proc += dp_netdev_rxq_get_intrvl_cycles(poll->rxq, i);
5144 }
5145 }
5146 ovs_mutex_unlock(&pmd->port_mutex);
5147
5148 if (total_proc) {
5149 curr_pmd_usage[num_pmds] = (total_proc * 100) / total_cycles;
5150 }
5151
5152 VLOG_DBG("PMD auto lb dry run. Current: Core %d, usage %"PRIu64"",
5153 pmd->core_id, curr_pmd_usage[num_pmds]);
5154
5155 if (atomic_count_get(&pmd->pmd_overloaded)) {
5156 atomic_count_set(&pmd->pmd_overloaded, 0);
5157 }
5158
5159 pmd_corelist[num_pmds] = pmd->core_id;
5160 num_pmds++;
5161 }
5162
5163 curr_variance = variance(curr_pmd_usage, num_pmds);
5164 ret = get_dry_run_variance(dp, pmd_corelist, num_pmds, &new_variance);
5165
5166 if (ret) {
5167 VLOG_DBG("PMD auto lb dry run. Current PMD variance: %"PRIu64","
5168 " Predicted PMD variance: %"PRIu64"",
5169 curr_variance, new_variance);
5170
5171 if (new_variance < curr_variance) {
5172 improvement =
5173 ((curr_variance - new_variance) * 100) / curr_variance;
5174 }
5175 if (improvement < ALB_ACCEPTABLE_IMPROVEMENT) {
5176 ret = false;
5177 }
5178 }
5179
5180 free(curr_pmd_usage);
5181 free(pmd_corelist);
5182 return ret;
5183 }
5184
5185
5186 /* Return true if needs to revalidate datapath flows. */
5187 static bool
5188 dpif_netdev_run(struct dpif *dpif)
5189 {
5190 struct dp_netdev_port *port;
5191 struct dp_netdev *dp = get_dp_netdev(dpif);
5192 struct dp_netdev_pmd_thread *non_pmd;
5193 uint64_t new_tnl_seq;
5194 bool need_to_flush = true;
5195 bool pmd_rebalance = false;
5196 long long int now = time_msec();
5197 struct dp_netdev_pmd_thread *pmd;
5198
5199 ovs_mutex_lock(&dp->port_mutex);
5200 non_pmd = dp_netdev_get_pmd(dp, NON_PMD_CORE_ID);
5201 if (non_pmd) {
5202 ovs_mutex_lock(&dp->non_pmd_mutex);
5203 HMAP_FOR_EACH (port, node, &dp->ports) {
5204 if (!netdev_is_pmd(port->netdev)) {
5205 int i;
5206
5207 if (port->emc_enabled) {
5208 atomic_read_relaxed(&dp->emc_insert_min,
5209 &non_pmd->ctx.emc_insert_min);
5210 } else {
5211 non_pmd->ctx.emc_insert_min = 0;
5212 }
5213
5214 for (i = 0; i < port->n_rxq; i++) {
5215
5216 if (!netdev_rxq_enabled(port->rxqs[i].rx)) {
5217 continue;
5218 }
5219
5220 if (dp_netdev_process_rxq_port(non_pmd,
5221 &port->rxqs[i],
5222 port->port_no)) {
5223 need_to_flush = false;
5224 }
5225 }
5226 }
5227 }
5228 if (need_to_flush) {
5229 /* We didn't receive anything in the process loop.
5230 * Check if we need to send something.
5231 * There was no time updates on current iteration. */
5232 pmd_thread_ctx_time_update(non_pmd);
5233 dp_netdev_pmd_flush_output_packets(non_pmd, false);
5234 }
5235
5236 dpif_netdev_xps_revalidate_pmd(non_pmd, false);
5237 ovs_mutex_unlock(&dp->non_pmd_mutex);
5238
5239 dp_netdev_pmd_unref(non_pmd);
5240 }
5241
5242 struct pmd_auto_lb *pmd_alb = &dp->pmd_alb;
5243 if (pmd_alb->is_enabled) {
5244 if (!pmd_alb->rebalance_poll_timer) {
5245 pmd_alb->rebalance_poll_timer = now;
5246 } else if ((pmd_alb->rebalance_poll_timer +
5247 pmd_alb->rebalance_intvl) < now) {
5248 pmd_alb->rebalance_poll_timer = now;
5249 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
5250 if (atomic_count_get(&pmd->pmd_overloaded) >=
5251 PMD_RXQ_INTERVAL_MAX) {
5252 pmd_rebalance = true;
5253 break;
5254 }
5255 }
5256
5257 if (pmd_rebalance &&
5258 !dp_netdev_is_reconf_required(dp) &&
5259 !ports_require_restart(dp) &&
5260 pmd_rebalance_dry_run(dp)) {
5261 VLOG_INFO("PMD auto lb dry run."
5262 " requesting datapath reconfigure.");
5263 dp_netdev_request_reconfigure(dp);
5264 }
5265 }
5266 }
5267
5268 if (dp_netdev_is_reconf_required(dp) || ports_require_restart(dp)) {
5269 reconfigure_datapath(dp);
5270 }
5271 ovs_mutex_unlock(&dp->port_mutex);
5272
5273 tnl_neigh_cache_run();
5274 tnl_port_map_run();
5275 new_tnl_seq = seq_read(tnl_conf_seq);
5276
5277 if (dp->last_tnl_conf_seq != new_tnl_seq) {
5278 dp->last_tnl_conf_seq = new_tnl_seq;
5279 return true;
5280 }
5281 return false;
5282 }
5283
5284 static void
5285 dpif_netdev_wait(struct dpif *dpif)
5286 {
5287 struct dp_netdev_port *port;
5288 struct dp_netdev *dp = get_dp_netdev(dpif);
5289
5290 ovs_mutex_lock(&dp_netdev_mutex);
5291 ovs_mutex_lock(&dp->port_mutex);
5292 HMAP_FOR_EACH (port, node, &dp->ports) {
5293 netdev_wait_reconf_required(port->netdev);
5294 if (!netdev_is_pmd(port->netdev)) {
5295 int i;
5296
5297 for (i = 0; i < port->n_rxq; i++) {
5298 netdev_rxq_wait(port->rxqs[i].rx);
5299 }
5300 }
5301 }
5302 ovs_mutex_unlock(&dp->port_mutex);
5303 ovs_mutex_unlock(&dp_netdev_mutex);
5304 seq_wait(tnl_conf_seq, dp->last_tnl_conf_seq);
5305 }
5306
5307 static void
5308 pmd_free_cached_ports(struct dp_netdev_pmd_thread *pmd)
5309 {
5310 struct tx_port *tx_port_cached;
5311
5312 /* Flush all the queued packets. */
5313 dp_netdev_pmd_flush_output_packets(pmd, true);
5314 /* Free all used tx queue ids. */
5315 dpif_netdev_xps_revalidate_pmd(pmd, true);
5316
5317 HMAP_FOR_EACH_POP (tx_port_cached, node, &pmd->tnl_port_cache) {
5318 free(tx_port_cached);
5319 }
5320 HMAP_FOR_EACH_POP (tx_port_cached, node, &pmd->send_port_cache) {
5321 free(tx_port_cached);
5322 }
5323 }
5324
5325 /* Copies ports from 'pmd->tx_ports' (shared with the main thread) to
5326 * thread-local copies. Copy to 'pmd->tnl_port_cache' if it is a tunnel
5327 * device, otherwise to 'pmd->send_port_cache' if the port has at least
5328 * one txq. */
5329 static void
5330 pmd_load_cached_ports(struct dp_netdev_pmd_thread *pmd)
5331 OVS_REQUIRES(pmd->port_mutex)
5332 {
5333 struct tx_port *tx_port, *tx_port_cached;
5334
5335 pmd_free_cached_ports(pmd);
5336 hmap_shrink(&pmd->send_port_cache);
5337 hmap_shrink(&pmd->tnl_port_cache);
5338
5339 HMAP_FOR_EACH (tx_port, node, &pmd->tx_ports) {
5340 if (netdev_has_tunnel_push_pop(tx_port->port->netdev)) {
5341 tx_port_cached = xmemdup(tx_port, sizeof *tx_port_cached);
5342 hmap_insert(&pmd->tnl_port_cache, &tx_port_cached->node,
5343 hash_port_no(tx_port_cached->port->port_no));
5344 }
5345
5346 if (netdev_n_txq(tx_port->port->netdev)) {
5347 tx_port_cached = xmemdup(tx_port, sizeof *tx_port_cached);
5348 hmap_insert(&pmd->send_port_cache, &tx_port_cached->node,
5349 hash_port_no(tx_port_cached->port->port_no));
5350 }
5351 }
5352 }
5353
5354 static void
5355 pmd_alloc_static_tx_qid(struct dp_netdev_pmd_thread *pmd)
5356 {
5357 ovs_mutex_lock(&pmd->dp->tx_qid_pool_mutex);
5358 if (!id_pool_alloc_id(pmd->dp->tx_qid_pool, &pmd->static_tx_qid)) {
5359 VLOG_ABORT("static_tx_qid allocation failed for PMD on core %2d"
5360 ", numa_id %d.", pmd->core_id, pmd->numa_id);
5361 }
5362 ovs_mutex_unlock(&pmd->dp->tx_qid_pool_mutex);
5363
5364 VLOG_DBG("static_tx_qid = %d allocated for PMD thread on core %2d"
5365 ", numa_id %d.", pmd->static_tx_qid, pmd->core_id, pmd->numa_id);
5366 }
5367
5368 static void
5369 pmd_free_static_tx_qid(struct dp_netdev_pmd_thread *pmd)
5370 {
5371 ovs_mutex_lock(&pmd->dp->tx_qid_pool_mutex);
5372 id_pool_free_id(pmd->dp->tx_qid_pool, pmd->static_tx_qid);
5373 ovs_mutex_unlock(&pmd->dp->tx_qid_pool_mutex);
5374 }
5375
5376 static int
5377 pmd_load_queues_and_ports(struct dp_netdev_pmd_thread *pmd,
5378 struct polled_queue **ppoll_list)
5379 {
5380 struct polled_queue *poll_list = *ppoll_list;
5381 struct rxq_poll *poll;
5382 int i;
5383
5384 ovs_mutex_lock(&pmd->port_mutex);
5385 poll_list = xrealloc(poll_list, hmap_count(&pmd->poll_list)
5386 * sizeof *poll_list);
5387
5388 i = 0;
5389 HMAP_FOR_EACH (poll, node, &pmd->poll_list) {
5390 poll_list[i].rxq = poll->rxq;
5391 poll_list[i].port_no = poll->rxq->port->port_no;
5392 poll_list[i].emc_enabled = poll->rxq->port->emc_enabled;
5393 poll_list[i].rxq_enabled = netdev_rxq_enabled(poll->rxq->rx);
5394 poll_list[i].change_seq =
5395 netdev_get_change_seq(poll->rxq->port->netdev);
5396 i++;
5397 }
5398
5399 pmd_load_cached_ports(pmd);
5400
5401 ovs_mutex_unlock(&pmd->port_mutex);
5402
5403 *ppoll_list = poll_list;
5404 return i;
5405 }
5406
5407 static void *
5408 pmd_thread_main(void *f_)
5409 {
5410 struct dp_netdev_pmd_thread *pmd = f_;
5411 struct pmd_perf_stats *s = &pmd->perf_stats;
5412 unsigned int lc = 0;
5413 struct polled_queue *poll_list;
5414 bool exiting;
5415 int poll_cnt;
5416 int i;
5417 int process_packets = 0;
5418
5419 poll_list = NULL;
5420
5421 /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */
5422 ovsthread_setspecific(pmd->dp->per_pmd_key, pmd);
5423 ovs_numa_thread_setaffinity_core(pmd->core_id);
5424 dpdk_set_lcore_id(pmd->core_id);
5425 poll_cnt = pmd_load_queues_and_ports(pmd, &poll_list);
5426 dfc_cache_init(&pmd->flow_cache);
5427 reload:
5428 pmd_alloc_static_tx_qid(pmd);
5429
5430 atomic_count_init(&pmd->pmd_overloaded, 0);
5431
5432 /* List port/core affinity */
5433 for (i = 0; i < poll_cnt; i++) {
5434 VLOG_DBG("Core %d processing port \'%s\' with queue-id %d\n",
5435 pmd->core_id, netdev_rxq_get_name(poll_list[i].rxq->rx),
5436 netdev_rxq_get_queue_id(poll_list[i].rxq->rx));
5437 /* Reset the rxq current cycles counter. */
5438 dp_netdev_rxq_set_cycles(poll_list[i].rxq, RXQ_CYCLES_PROC_CURR, 0);
5439 }
5440
5441 if (!poll_cnt) {
5442 while (seq_read(pmd->reload_seq) == pmd->last_reload_seq) {
5443 seq_wait(pmd->reload_seq, pmd->last_reload_seq);
5444 poll_block();
5445 }
5446 lc = UINT_MAX;
5447 }
5448
5449 pmd->intrvl_tsc_prev = 0;
5450 atomic_store_relaxed(&pmd->intrvl_cycles, 0);
5451 cycles_counter_update(s);
5452 /* Protect pmd stats from external clearing while polling. */
5453 ovs_mutex_lock(&pmd->perf_stats.stats_mutex);
5454 for (;;) {
5455 uint64_t rx_packets = 0, tx_packets = 0;
5456
5457 pmd_perf_start_iteration(s);
5458
5459 for (i = 0; i < poll_cnt; i++) {
5460
5461 if (!poll_list[i].rxq_enabled) {
5462 continue;
5463 }
5464
5465 if (poll_list[i].emc_enabled) {
5466 atomic_read_relaxed(&pmd->dp->emc_insert_min,
5467 &pmd->ctx.emc_insert_min);
5468 } else {
5469 pmd->ctx.emc_insert_min = 0;
5470 }
5471
5472 process_packets =
5473 dp_netdev_process_rxq_port(pmd, poll_list[i].rxq,
5474 poll_list[i].port_no);
5475 rx_packets += process_packets;
5476 }
5477
5478 if (!rx_packets) {
5479 /* We didn't receive anything in the process loop.
5480 * Check if we need to send something.
5481 * There was no time updates on current iteration. */
5482 pmd_thread_ctx_time_update(pmd);
5483 tx_packets = dp_netdev_pmd_flush_output_packets(pmd, false);
5484 }
5485
5486 if (lc++ > 1024) {
5487 bool reload;
5488
5489 lc = 0;
5490
5491 coverage_try_clear();
5492 dp_netdev_pmd_try_optimize(pmd, poll_list, poll_cnt);
5493 if (!ovsrcu_try_quiesce()) {
5494 emc_cache_slow_sweep(&((pmd->flow_cache).emc_cache));
5495 }
5496
5497 atomic_read_explicit(&pmd->reload, &reload, memory_order_acquire);
5498 if (reload) {
5499 break;
5500 }
5501
5502 for (i = 0; i < poll_cnt; i++) {
5503 uint64_t current_seq =
5504 netdev_get_change_seq(poll_list[i].rxq->port->netdev);
5505 if (poll_list[i].change_seq != current_seq) {
5506 poll_list[i].change_seq = current_seq;
5507 poll_list[i].rxq_enabled =
5508 netdev_rxq_enabled(poll_list[i].rxq->rx);
5509 }
5510 }
5511 }
5512 pmd_perf_end_iteration(s, rx_packets, tx_packets,
5513 pmd_perf_metrics_enabled(pmd));
5514 }
5515 ovs_mutex_unlock(&pmd->perf_stats.stats_mutex);
5516
5517 poll_cnt = pmd_load_queues_and_ports(pmd, &poll_list);
5518 atomic_read_relaxed(&pmd->exit, &exiting);
5519 /* Signal here to make sure the pmd finishes
5520 * reloading the updated configuration. */
5521 dp_netdev_pmd_reload_done(pmd);
5522
5523 pmd_free_static_tx_qid(pmd);
5524
5525 if (!exiting) {
5526 goto reload;
5527 }
5528
5529 dfc_cache_uninit(&pmd->flow_cache);
5530 free(poll_list);
5531 pmd_free_cached_ports(pmd);
5532 return NULL;
5533 }
5534
5535 static void
5536 dp_netdev_disable_upcall(struct dp_netdev *dp)
5537 OVS_ACQUIRES(dp->upcall_rwlock)
5538 {
5539 fat_rwlock_wrlock(&dp->upcall_rwlock);
5540 }
5541
5542 \f
5543 /* Meters */
5544 static void
5545 dpif_netdev_meter_get_features(const struct dpif * dpif OVS_UNUSED,
5546 struct ofputil_meter_features *features)
5547 {
5548 features->max_meters = MAX_METERS;
5549 features->band_types = DP_SUPPORTED_METER_BAND_TYPES;
5550 features->capabilities = DP_SUPPORTED_METER_FLAGS_MASK;
5551 features->max_bands = MAX_BANDS;
5552 features->max_color = 0;
5553 }
5554
5555 /* Applies the meter identified by 'meter_id' to 'packets_'. Packets
5556 * that exceed a band are dropped in-place. */
5557 static void
5558 dp_netdev_run_meter(struct dp_netdev *dp, struct dp_packet_batch *packets_,
5559 uint32_t meter_id, long long int now)
5560 {
5561 struct dp_meter *meter;
5562 struct dp_meter_band *band;
5563 struct dp_packet *packet;
5564 long long int long_delta_t; /* msec */
5565 uint32_t delta_t; /* msec */
5566 const size_t cnt = dp_packet_batch_size(packets_);
5567 uint32_t bytes, volume;
5568 int exceeded_band[NETDEV_MAX_BURST];
5569 uint32_t exceeded_rate[NETDEV_MAX_BURST];
5570 int exceeded_pkt = cnt; /* First packet that exceeded a band rate. */
5571
5572 if (meter_id >= MAX_METERS) {
5573 return;
5574 }
5575
5576 meter_lock(dp, meter_id);
5577 meter = dp->meters[meter_id];
5578 if (!meter) {
5579 goto out;
5580 }
5581
5582 /* Initialize as negative values. */
5583 memset(exceeded_band, 0xff, cnt * sizeof *exceeded_band);
5584 /* Initialize as zeroes. */
5585 memset(exceeded_rate, 0, cnt * sizeof *exceeded_rate);
5586
5587 /* All packets will hit the meter at the same time. */
5588 long_delta_t = now / 1000 - meter->used / 1000; /* msec */
5589
5590 /* Make sure delta_t will not be too large, so that bucket will not
5591 * wrap around below. */
5592 delta_t = (long_delta_t > (long long int)meter->max_delta_t)
5593 ? meter->max_delta_t : (uint32_t)long_delta_t;
5594
5595 /* Update meter stats. */
5596 meter->used = now;
5597 meter->packet_count += cnt;
5598 bytes = 0;
5599 DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) {
5600 bytes += dp_packet_size(packet);
5601 }
5602 meter->byte_count += bytes;
5603
5604 /* Meters can operate in terms of packets per second or kilobits per
5605 * second. */
5606 if (meter->flags & OFPMF13_PKTPS) {
5607 /* Rate in packets/second, bucket 1/1000 packets. */
5608 /* msec * packets/sec = 1/1000 packets. */
5609 volume = cnt * 1000; /* Take 'cnt' packets from the bucket. */
5610 } else {
5611 /* Rate in kbps, bucket in bits. */
5612 /* msec * kbps = bits */
5613 volume = bytes * 8;
5614 }
5615
5616 /* Update all bands and find the one hit with the highest rate for each
5617 * packet (if any). */
5618 for (int m = 0; m < meter->n_bands; ++m) {
5619 band = &meter->bands[m];
5620
5621 /* Update band's bucket. */
5622 band->bucket += delta_t * band->up.rate;
5623 if (band->bucket > band->up.burst_size) {
5624 band->bucket = band->up.burst_size;
5625 }
5626
5627 /* Drain the bucket for all the packets, if possible. */
5628 if (band->bucket >= volume) {
5629 band->bucket -= volume;
5630 } else {
5631 int band_exceeded_pkt;
5632
5633 /* Band limit hit, must process packet-by-packet. */
5634 if (meter->flags & OFPMF13_PKTPS) {
5635 band_exceeded_pkt = band->bucket / 1000;
5636 band->bucket %= 1000; /* Remainder stays in bucket. */
5637
5638 /* Update the exceeding band for each exceeding packet.
5639 * (Only one band will be fired by a packet, and that
5640 * can be different for each packet.) */
5641 for (int i = band_exceeded_pkt; i < cnt; i++) {
5642 if (band->up.rate > exceeded_rate[i]) {
5643 exceeded_rate[i] = band->up.rate;
5644 exceeded_band[i] = m;
5645 }
5646 }
5647 } else {
5648 /* Packet sizes differ, must process one-by-one. */
5649 band_exceeded_pkt = cnt;
5650 DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) {
5651 uint32_t bits = dp_packet_size(packet) * 8;
5652
5653 if (band->bucket >= bits) {
5654 band->bucket -= bits;
5655 } else {
5656 if (i < band_exceeded_pkt) {
5657 band_exceeded_pkt = i;
5658 }
5659 /* Update the exceeding band for the exceeding packet.
5660 * (Only one band will be fired by a packet, and that
5661 * can be different for each packet.) */
5662 if (band->up.rate > exceeded_rate[i]) {
5663 exceeded_rate[i] = band->up.rate;
5664 exceeded_band[i] = m;
5665 }
5666 }
5667 }
5668 }
5669 /* Remember the first exceeding packet. */
5670 if (exceeded_pkt > band_exceeded_pkt) {
5671 exceeded_pkt = band_exceeded_pkt;
5672 }
5673 }
5674 }
5675
5676 /* Fire the highest rate band exceeded by each packet, and drop
5677 * packets if needed. */
5678 size_t j;
5679 DP_PACKET_BATCH_REFILL_FOR_EACH (j, cnt, packet, packets_) {
5680 if (exceeded_band[j] >= 0) {
5681 /* Meter drop packet. */
5682 band = &meter->bands[exceeded_band[j]];
5683 band->packet_count += 1;
5684 band->byte_count += dp_packet_size(packet);
5685
5686 dp_packet_delete(packet);
5687 } else {
5688 /* Meter accepts packet. */
5689 dp_packet_batch_refill(packets_, packet, j);
5690 }
5691 }
5692 out:
5693 meter_unlock(dp, meter_id);
5694 }
5695
5696 /* Meter set/get/del processing is still single-threaded. */
5697 static int
5698 dpif_netdev_meter_set(struct dpif *dpif, ofproto_meter_id meter_id,
5699 struct ofputil_meter_config *config)
5700 {
5701 struct dp_netdev *dp = get_dp_netdev(dpif);
5702 uint32_t mid = meter_id.uint32;
5703 struct dp_meter *meter;
5704 int i;
5705
5706 if (mid >= MAX_METERS) {
5707 return EFBIG; /* Meter_id out of range. */
5708 }
5709
5710 if (config->flags & ~DP_SUPPORTED_METER_FLAGS_MASK) {
5711 return EBADF; /* Unsupported flags set */
5712 }
5713
5714 if (config->n_bands > MAX_BANDS) {
5715 return EINVAL;
5716 }
5717
5718 for (i = 0; i < config->n_bands; ++i) {
5719 switch (config->bands[i].type) {
5720 case OFPMBT13_DROP:
5721 break;
5722 default:
5723 return ENODEV; /* Unsupported band type */
5724 }
5725 }
5726
5727 /* Allocate meter */
5728 meter = xzalloc(sizeof *meter
5729 + config->n_bands * sizeof(struct dp_meter_band));
5730
5731 meter->flags = config->flags;
5732 meter->n_bands = config->n_bands;
5733 meter->max_delta_t = 0;
5734 meter->used = time_usec();
5735
5736 /* set up bands */
5737 for (i = 0; i < config->n_bands; ++i) {
5738 uint32_t band_max_delta_t;
5739
5740 /* Set burst size to a workable value if none specified. */
5741 if (config->bands[i].burst_size == 0) {
5742 config->bands[i].burst_size = config->bands[i].rate;
5743 }
5744
5745 meter->bands[i].up = config->bands[i];
5746 /* Convert burst size to the bucket units: */
5747 /* pkts => 1/1000 packets, kilobits => bits. */
5748 meter->bands[i].up.burst_size *= 1000;
5749 /* Initialize bucket to empty. */
5750 meter->bands[i].bucket = 0;
5751
5752 /* Figure out max delta_t that is enough to fill any bucket. */
5753 band_max_delta_t
5754 = meter->bands[i].up.burst_size / meter->bands[i].up.rate;
5755 if (band_max_delta_t > meter->max_delta_t) {
5756 meter->max_delta_t = band_max_delta_t;
5757 }
5758 }
5759
5760 meter_lock(dp, mid);
5761 dp_delete_meter(dp, mid); /* Free existing meter, if any */
5762 dp->meters[mid] = meter;
5763 meter_unlock(dp, mid);
5764
5765 return 0;
5766 }
5767
5768 static int
5769 dpif_netdev_meter_get(const struct dpif *dpif,
5770 ofproto_meter_id meter_id_,
5771 struct ofputil_meter_stats *stats, uint16_t n_bands)
5772 {
5773 const struct dp_netdev *dp = get_dp_netdev(dpif);
5774 uint32_t meter_id = meter_id_.uint32;
5775 int retval = 0;
5776
5777 if (meter_id >= MAX_METERS) {
5778 return EFBIG;
5779 }
5780
5781 meter_lock(dp, meter_id);
5782 const struct dp_meter *meter = dp->meters[meter_id];
5783 if (!meter) {
5784 retval = ENOENT;
5785 goto done;
5786 }
5787 if (stats) {
5788 int i = 0;
5789
5790 stats->packet_in_count = meter->packet_count;
5791 stats->byte_in_count = meter->byte_count;
5792
5793 for (i = 0; i < n_bands && i < meter->n_bands; ++i) {
5794 stats->bands[i].packet_count = meter->bands[i].packet_count;
5795 stats->bands[i].byte_count = meter->bands[i].byte_count;
5796 }
5797
5798 stats->n_bands = i;
5799 }
5800
5801 done:
5802 meter_unlock(dp, meter_id);
5803 return retval;
5804 }
5805
5806 static int
5807 dpif_netdev_meter_del(struct dpif *dpif,
5808 ofproto_meter_id meter_id_,
5809 struct ofputil_meter_stats *stats, uint16_t n_bands)
5810 {
5811 struct dp_netdev *dp = get_dp_netdev(dpif);
5812 int error;
5813
5814 error = dpif_netdev_meter_get(dpif, meter_id_, stats, n_bands);
5815 if (!error) {
5816 uint32_t meter_id = meter_id_.uint32;
5817
5818 meter_lock(dp, meter_id);
5819 dp_delete_meter(dp, meter_id);
5820 meter_unlock(dp, meter_id);
5821 }
5822 return error;
5823 }
5824
5825 \f
5826 static void
5827 dpif_netdev_disable_upcall(struct dpif *dpif)
5828 OVS_NO_THREAD_SAFETY_ANALYSIS
5829 {
5830 struct dp_netdev *dp = get_dp_netdev(dpif);
5831 dp_netdev_disable_upcall(dp);
5832 }
5833
5834 static void
5835 dp_netdev_enable_upcall(struct dp_netdev *dp)
5836 OVS_RELEASES(dp->upcall_rwlock)
5837 {
5838 fat_rwlock_unlock(&dp->upcall_rwlock);
5839 }
5840
5841 static void
5842 dpif_netdev_enable_upcall(struct dpif *dpif)
5843 OVS_NO_THREAD_SAFETY_ANALYSIS
5844 {
5845 struct dp_netdev *dp = get_dp_netdev(dpif);
5846 dp_netdev_enable_upcall(dp);
5847 }
5848
5849 static void
5850 dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread *pmd)
5851 {
5852 pmd->last_reload_seq = seq_read(pmd->reload_seq);
5853 atomic_store_explicit(&pmd->reload, false, memory_order_release);
5854 }
5855
5856 /* Finds and refs the dp_netdev_pmd_thread on core 'core_id'. Returns
5857 * the pointer if succeeds, otherwise, NULL (it can return NULL even if
5858 * 'core_id' is NON_PMD_CORE_ID).
5859 *
5860 * Caller must unrefs the returned reference. */
5861 static struct dp_netdev_pmd_thread *
5862 dp_netdev_get_pmd(struct dp_netdev *dp, unsigned core_id)
5863 {
5864 struct dp_netdev_pmd_thread *pmd;
5865 const struct cmap_node *pnode;
5866
5867 pnode = cmap_find(&dp->poll_threads, hash_int(core_id, 0));
5868 if (!pnode) {
5869 return NULL;
5870 }
5871 pmd = CONTAINER_OF(pnode, struct dp_netdev_pmd_thread, node);
5872
5873 return dp_netdev_pmd_try_ref(pmd) ? pmd : NULL;
5874 }
5875
5876 /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */
5877 static void
5878 dp_netdev_set_nonpmd(struct dp_netdev *dp)
5879 OVS_REQUIRES(dp->port_mutex)
5880 {
5881 struct dp_netdev_pmd_thread *non_pmd;
5882
5883 non_pmd = xzalloc(sizeof *non_pmd);
5884 dp_netdev_configure_pmd(non_pmd, dp, NON_PMD_CORE_ID, OVS_NUMA_UNSPEC);
5885 }
5886
5887 /* Caller must have valid pointer to 'pmd'. */
5888 static bool
5889 dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread *pmd)
5890 {
5891 return ovs_refcount_try_ref_rcu(&pmd->ref_cnt);
5892 }
5893
5894 static void
5895 dp_netdev_pmd_unref(struct dp_netdev_pmd_thread *pmd)
5896 {
5897 if (pmd && ovs_refcount_unref(&pmd->ref_cnt) == 1) {
5898 ovsrcu_postpone(dp_netdev_destroy_pmd, pmd);
5899 }
5900 }
5901
5902 /* Given cmap position 'pos', tries to ref the next node. If try_ref()
5903 * fails, keeps checking for next node until reaching the end of cmap.
5904 *
5905 * Caller must unrefs the returned reference. */
5906 static struct dp_netdev_pmd_thread *
5907 dp_netdev_pmd_get_next(struct dp_netdev *dp, struct cmap_position *pos)
5908 {
5909 struct dp_netdev_pmd_thread *next;
5910
5911 do {
5912 struct cmap_node *node;
5913
5914 node = cmap_next_position(&dp->poll_threads, pos);
5915 next = node ? CONTAINER_OF(node, struct dp_netdev_pmd_thread, node)
5916 : NULL;
5917 } while (next && !dp_netdev_pmd_try_ref(next));
5918
5919 return next;
5920 }
5921
5922 /* Configures the 'pmd' based on the input argument. */
5923 static void
5924 dp_netdev_configure_pmd(struct dp_netdev_pmd_thread *pmd, struct dp_netdev *dp,
5925 unsigned core_id, int numa_id)
5926 {
5927 pmd->dp = dp;
5928 pmd->core_id = core_id;
5929 pmd->numa_id = numa_id;
5930 pmd->need_reload = false;
5931 pmd->n_output_batches = 0;
5932
5933 ovs_refcount_init(&pmd->ref_cnt);
5934 atomic_init(&pmd->exit, false);
5935 pmd->reload_seq = seq_create();
5936 pmd->last_reload_seq = seq_read(pmd->reload_seq);
5937 atomic_init(&pmd->reload, false);
5938 ovs_mutex_init(&pmd->flow_mutex);
5939 ovs_mutex_init(&pmd->port_mutex);
5940 cmap_init(&pmd->flow_table);
5941 cmap_init(&pmd->classifiers);
5942 pmd->ctx.last_rxq = NULL;
5943 pmd_thread_ctx_time_update(pmd);
5944 pmd->next_optimization = pmd->ctx.now + DPCLS_OPTIMIZATION_INTERVAL;
5945 pmd->rxq_next_cycle_store = pmd->ctx.now + PMD_RXQ_INTERVAL_LEN;
5946 hmap_init(&pmd->poll_list);
5947 hmap_init(&pmd->tx_ports);
5948 hmap_init(&pmd->tnl_port_cache);
5949 hmap_init(&pmd->send_port_cache);
5950 /* init the 'flow_cache' since there is no
5951 * actual thread created for NON_PMD_CORE_ID. */
5952 if (core_id == NON_PMD_CORE_ID) {
5953 dfc_cache_init(&pmd->flow_cache);
5954 pmd_alloc_static_tx_qid(pmd);
5955 }
5956 pmd_perf_stats_init(&pmd->perf_stats);
5957 cmap_insert(&dp->poll_threads, CONST_CAST(struct cmap_node *, &pmd->node),
5958 hash_int(core_id, 0));
5959 }
5960
5961 static void
5962 dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread *pmd)
5963 {
5964 struct dpcls *cls;
5965
5966 dp_netdev_pmd_flow_flush(pmd);
5967 hmap_destroy(&pmd->send_port_cache);
5968 hmap_destroy(&pmd->tnl_port_cache);
5969 hmap_destroy(&pmd->tx_ports);
5970 hmap_destroy(&pmd->poll_list);
5971 /* All flows (including their dpcls_rules) have been deleted already */
5972 CMAP_FOR_EACH (cls, node, &pmd->classifiers) {
5973 dpcls_destroy(cls);
5974 ovsrcu_postpone(free, cls);
5975 }
5976 cmap_destroy(&pmd->classifiers);
5977 cmap_destroy(&pmd->flow_table);
5978 ovs_mutex_destroy(&pmd->flow_mutex);
5979 seq_destroy(pmd->reload_seq);
5980 ovs_mutex_destroy(&pmd->port_mutex);
5981 free(pmd);
5982 }
5983
5984 /* Stops the pmd thread, removes it from the 'dp->poll_threads',
5985 * and unrefs the struct. */
5986 static void
5987 dp_netdev_del_pmd(struct dp_netdev *dp, struct dp_netdev_pmd_thread *pmd)
5988 {
5989 /* NON_PMD_CORE_ID doesn't have a thread, so we don't have to synchronize,
5990 * but extra cleanup is necessary */
5991 if (pmd->core_id == NON_PMD_CORE_ID) {
5992 ovs_mutex_lock(&dp->non_pmd_mutex);
5993 dfc_cache_uninit(&pmd->flow_cache);
5994 pmd_free_cached_ports(pmd);
5995 pmd_free_static_tx_qid(pmd);
5996 ovs_mutex_unlock(&dp->non_pmd_mutex);
5997 } else {
5998 atomic_store_relaxed(&pmd->exit, true);
5999 dp_netdev_reload_pmd__(pmd);
6000 xpthread_join(pmd->thread, NULL);
6001 }
6002
6003 dp_netdev_pmd_clear_ports(pmd);
6004
6005 /* Purges the 'pmd''s flows after stopping the thread, but before
6006 * destroying the flows, so that the flow stats can be collected. */
6007 if (dp->dp_purge_cb) {
6008 dp->dp_purge_cb(dp->dp_purge_aux, pmd->core_id);
6009 }
6010 cmap_remove(&pmd->dp->poll_threads, &pmd->node, hash_int(pmd->core_id, 0));
6011 dp_netdev_pmd_unref(pmd);
6012 }
6013
6014 /* Destroys all pmd threads. If 'non_pmd' is true it also destroys the non pmd
6015 * thread. */
6016 static void
6017 dp_netdev_destroy_all_pmds(struct dp_netdev *dp, bool non_pmd)
6018 {
6019 struct dp_netdev_pmd_thread *pmd;
6020 struct dp_netdev_pmd_thread **pmd_list;
6021 size_t k = 0, n_pmds;
6022
6023 n_pmds = cmap_count(&dp->poll_threads);
6024 pmd_list = xcalloc(n_pmds, sizeof *pmd_list);
6025
6026 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
6027 if (!non_pmd && pmd->core_id == NON_PMD_CORE_ID) {
6028 continue;
6029 }
6030 /* We cannot call dp_netdev_del_pmd(), since it alters
6031 * 'dp->poll_threads' (while we're iterating it) and it
6032 * might quiesce. */
6033 ovs_assert(k < n_pmds);
6034 pmd_list[k++] = pmd;
6035 }
6036
6037 for (size_t i = 0; i < k; i++) {
6038 dp_netdev_del_pmd(dp, pmd_list[i]);
6039 }
6040 free(pmd_list);
6041 }
6042
6043 /* Deletes all rx queues from pmd->poll_list and all the ports from
6044 * pmd->tx_ports. */
6045 static void
6046 dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread *pmd)
6047 {
6048 struct rxq_poll *poll;
6049 struct tx_port *port;
6050
6051 ovs_mutex_lock(&pmd->port_mutex);
6052 HMAP_FOR_EACH_POP (poll, node, &pmd->poll_list) {
6053 free(poll);
6054 }
6055 HMAP_FOR_EACH_POP (port, node, &pmd->tx_ports) {
6056 free(port);
6057 }
6058 ovs_mutex_unlock(&pmd->port_mutex);
6059 }
6060
6061 /* Adds rx queue to poll_list of PMD thread, if it's not there already. */
6062 static void
6063 dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread *pmd,
6064 struct dp_netdev_rxq *rxq)
6065 OVS_REQUIRES(pmd->port_mutex)
6066 {
6067 int qid = netdev_rxq_get_queue_id(rxq->rx);
6068 uint32_t hash = hash_2words(odp_to_u32(rxq->port->port_no), qid);
6069 struct rxq_poll *poll;
6070
6071 HMAP_FOR_EACH_WITH_HASH (poll, node, hash, &pmd->poll_list) {
6072 if (poll->rxq == rxq) {
6073 /* 'rxq' is already polled by this thread. Do nothing. */
6074 return;
6075 }
6076 }
6077
6078 poll = xmalloc(sizeof *poll);
6079 poll->rxq = rxq;
6080 hmap_insert(&pmd->poll_list, &poll->node, hash);
6081
6082 pmd->need_reload = true;
6083 }
6084
6085 /* Delete 'poll' from poll_list of PMD thread. */
6086 static void
6087 dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread *pmd,
6088 struct rxq_poll *poll)
6089 OVS_REQUIRES(pmd->port_mutex)
6090 {
6091 hmap_remove(&pmd->poll_list, &poll->node);
6092 free(poll);
6093
6094 pmd->need_reload = true;
6095 }
6096
6097 /* Add 'port' to the tx port cache of 'pmd', which must be reloaded for the
6098 * changes to take effect. */
6099 static void
6100 dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread *pmd,
6101 struct dp_netdev_port *port)
6102 OVS_REQUIRES(pmd->port_mutex)
6103 {
6104 struct tx_port *tx;
6105
6106 tx = tx_port_lookup(&pmd->tx_ports, port->port_no);
6107 if (tx) {
6108 /* 'port' is already on this thread tx cache. Do nothing. */
6109 return;
6110 }
6111
6112 tx = xzalloc(sizeof *tx);
6113
6114 tx->port = port;
6115 tx->qid = -1;
6116 tx->flush_time = 0LL;
6117 dp_packet_batch_init(&tx->output_pkts);
6118
6119 hmap_insert(&pmd->tx_ports, &tx->node, hash_port_no(tx->port->port_no));
6120 pmd->need_reload = true;
6121 }
6122
6123 /* Del 'tx' from the tx port cache of 'pmd', which must be reloaded for the
6124 * changes to take effect. */
6125 static void
6126 dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread *pmd,
6127 struct tx_port *tx)
6128 OVS_REQUIRES(pmd->port_mutex)
6129 {
6130 hmap_remove(&pmd->tx_ports, &tx->node);
6131 free(tx);
6132 pmd->need_reload = true;
6133 }
6134 \f
6135 static char *
6136 dpif_netdev_get_datapath_version(void)
6137 {
6138 return xstrdup("<built-in>");
6139 }
6140
6141 static void
6142 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow, int cnt, int size,
6143 uint16_t tcp_flags, long long now)
6144 {
6145 uint16_t flags;
6146
6147 atomic_store_relaxed(&netdev_flow->stats.used, now);
6148 non_atomic_ullong_add(&netdev_flow->stats.packet_count, cnt);
6149 non_atomic_ullong_add(&netdev_flow->stats.byte_count, size);
6150 atomic_read_relaxed(&netdev_flow->stats.tcp_flags, &flags);
6151 flags |= tcp_flags;
6152 atomic_store_relaxed(&netdev_flow->stats.tcp_flags, flags);
6153 }
6154
6155 static int
6156 dp_netdev_upcall(struct dp_netdev_pmd_thread *pmd, struct dp_packet *packet_,
6157 struct flow *flow, struct flow_wildcards *wc, ovs_u128 *ufid,
6158 enum dpif_upcall_type type, const struct nlattr *userdata,
6159 struct ofpbuf *actions, struct ofpbuf *put_actions)
6160 {
6161 struct dp_netdev *dp = pmd->dp;
6162
6163 if (OVS_UNLIKELY(!dp->upcall_cb)) {
6164 return ENODEV;
6165 }
6166
6167 if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl))) {
6168 struct ds ds = DS_EMPTY_INITIALIZER;
6169 char *packet_str;
6170 struct ofpbuf key;
6171 struct odp_flow_key_parms odp_parms = {
6172 .flow = flow,
6173 .mask = wc ? &wc->masks : NULL,
6174 .support = dp_netdev_support,
6175 };
6176
6177 ofpbuf_init(&key, 0);
6178 odp_flow_key_from_flow(&odp_parms, &key);
6179 packet_str = ofp_dp_packet_to_string(packet_);
6180
6181 odp_flow_key_format(key.data, key.size, &ds);
6182
6183 VLOG_DBG("%s: %s upcall:\n%s\n%s", dp->name,
6184 dpif_upcall_type_to_string(type), ds_cstr(&ds), packet_str);
6185
6186 ofpbuf_uninit(&key);
6187 free(packet_str);
6188
6189 ds_destroy(&ds);
6190 }
6191
6192 return dp->upcall_cb(packet_, flow, ufid, pmd->core_id, type, userdata,
6193 actions, wc, put_actions, dp->upcall_aux);
6194 }
6195
6196 static inline uint32_t
6197 dpif_netdev_packet_get_rss_hash_orig_pkt(struct dp_packet *packet,
6198 const struct miniflow *mf)
6199 {
6200 uint32_t hash;
6201
6202 if (OVS_LIKELY(dp_packet_rss_valid(packet))) {
6203 hash = dp_packet_get_rss_hash(packet);
6204 } else {
6205 hash = miniflow_hash_5tuple(mf, 0);
6206 dp_packet_set_rss_hash(packet, hash);
6207 }
6208
6209 return hash;
6210 }
6211
6212 static inline uint32_t
6213 dpif_netdev_packet_get_rss_hash(struct dp_packet *packet,
6214 const struct miniflow *mf)
6215 {
6216 uint32_t hash, recirc_depth;
6217
6218 if (OVS_LIKELY(dp_packet_rss_valid(packet))) {
6219 hash = dp_packet_get_rss_hash(packet);
6220 } else {
6221 hash = miniflow_hash_5tuple(mf, 0);
6222 dp_packet_set_rss_hash(packet, hash);
6223 }
6224
6225 /* The RSS hash must account for the recirculation depth to avoid
6226 * collisions in the exact match cache */
6227 recirc_depth = *recirc_depth_get_unsafe();
6228 if (OVS_UNLIKELY(recirc_depth)) {
6229 hash = hash_finish(hash, recirc_depth);
6230 dp_packet_set_rss_hash(packet, hash);
6231 }
6232 return hash;
6233 }
6234
6235 struct packet_batch_per_flow {
6236 unsigned int byte_count;
6237 uint16_t tcp_flags;
6238 struct dp_netdev_flow *flow;
6239
6240 struct dp_packet_batch array;
6241 };
6242
6243 static inline void
6244 packet_batch_per_flow_update(struct packet_batch_per_flow *batch,
6245 struct dp_packet *packet,
6246 uint16_t tcp_flags)
6247 {
6248 batch->byte_count += dp_packet_size(packet);
6249 batch->tcp_flags |= tcp_flags;
6250 batch->array.packets[batch->array.count++] = packet;
6251 }
6252
6253 static inline void
6254 packet_batch_per_flow_init(struct packet_batch_per_flow *batch,
6255 struct dp_netdev_flow *flow)
6256 {
6257 flow->batch = batch;
6258
6259 batch->flow = flow;
6260 dp_packet_batch_init(&batch->array);
6261 batch->byte_count = 0;
6262 batch->tcp_flags = 0;
6263 }
6264
6265 static inline void
6266 packet_batch_per_flow_execute(struct packet_batch_per_flow *batch,
6267 struct dp_netdev_pmd_thread *pmd)
6268 {
6269 struct dp_netdev_actions *actions;
6270 struct dp_netdev_flow *flow = batch->flow;
6271
6272 dp_netdev_flow_used(flow, batch->array.count, batch->byte_count,
6273 batch->tcp_flags, pmd->ctx.now / 1000);
6274
6275 actions = dp_netdev_flow_get_actions(flow);
6276
6277 dp_netdev_execute_actions(pmd, &batch->array, true, &flow->flow,
6278 actions->actions, actions->size);
6279 }
6280
6281 static inline void
6282 dp_netdev_queue_batches(struct dp_packet *pkt,
6283 struct dp_netdev_flow *flow, uint16_t tcp_flags,
6284 struct packet_batch_per_flow *batches,
6285 size_t *n_batches)
6286 {
6287 struct packet_batch_per_flow *batch = flow->batch;
6288
6289 if (OVS_UNLIKELY(!batch)) {
6290 batch = &batches[(*n_batches)++];
6291 packet_batch_per_flow_init(batch, flow);
6292 }
6293
6294 packet_batch_per_flow_update(batch, pkt, tcp_flags);
6295 }
6296
6297 static inline void
6298 packet_enqueue_to_flow_map(struct dp_packet *packet,
6299 struct dp_netdev_flow *flow,
6300 uint16_t tcp_flags,
6301 struct dp_packet_flow_map *flow_map,
6302 size_t index)
6303 {
6304 struct dp_packet_flow_map *map = &flow_map[index];
6305 map->flow = flow;
6306 map->packet = packet;
6307 map->tcp_flags = tcp_flags;
6308 }
6309
6310 /* SMC lookup function for a batch of packets.
6311 * By doing batching SMC lookup, we can use prefetch
6312 * to hide memory access latency.
6313 */
6314 static inline void
6315 smc_lookup_batch(struct dp_netdev_pmd_thread *pmd,
6316 struct netdev_flow_key *keys,
6317 struct netdev_flow_key **missed_keys,
6318 struct dp_packet_batch *packets_,
6319 const int cnt,
6320 struct dp_packet_flow_map *flow_map,
6321 uint8_t *index_map)
6322 {
6323 int i;
6324 struct dp_packet *packet;
6325 size_t n_smc_hit = 0, n_missed = 0;
6326 struct dfc_cache *cache = &pmd->flow_cache;
6327 struct smc_cache *smc_cache = &cache->smc_cache;
6328 const struct cmap_node *flow_node;
6329 int recv_idx;
6330 uint16_t tcp_flags;
6331
6332 /* Prefetch buckets for all packets */
6333 for (i = 0; i < cnt; i++) {
6334 OVS_PREFETCH(&smc_cache->buckets[keys[i].hash & SMC_MASK]);
6335 }
6336
6337 DP_PACKET_BATCH_REFILL_FOR_EACH (i, cnt, packet, packets_) {
6338 struct dp_netdev_flow *flow = NULL;
6339 flow_node = smc_entry_get(pmd, keys[i].hash);
6340 bool hit = false;
6341 /* Get the original order of this packet in received batch. */
6342 recv_idx = index_map[i];
6343
6344 if (OVS_LIKELY(flow_node != NULL)) {
6345 CMAP_NODE_FOR_EACH (flow, node, flow_node) {
6346 /* Since we dont have per-port megaflow to check the port
6347 * number, we need to verify that the input ports match. */
6348 if (OVS_LIKELY(dpcls_rule_matches_key(&flow->cr, &keys[i]) &&
6349 flow->flow.in_port.odp_port == packet->md.in_port.odp_port)) {
6350 tcp_flags = miniflow_get_tcp_flags(&keys[i].mf);
6351
6352 /* SMC hit and emc miss, we insert into EMC */
6353 keys[i].len =
6354 netdev_flow_key_size(miniflow_n_values(&keys[i].mf));
6355 emc_probabilistic_insert(pmd, &keys[i], flow);
6356 /* Add these packets into the flow map in the same order
6357 * as received.
6358 */
6359 packet_enqueue_to_flow_map(packet, flow, tcp_flags,
6360 flow_map, recv_idx);
6361 n_smc_hit++;
6362 hit = true;
6363 break;
6364 }
6365 }
6366 if (hit) {
6367 continue;
6368 }
6369 }
6370
6371 /* SMC missed. Group missed packets together at
6372 * the beginning of the 'packets' array. */
6373 dp_packet_batch_refill(packets_, packet, i);
6374
6375 /* Preserve the order of packet for flow batching. */
6376 index_map[n_missed] = recv_idx;
6377
6378 /* Put missed keys to the pointer arrays return to the caller */
6379 missed_keys[n_missed++] = &keys[i];
6380 }
6381
6382 pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_SMC_HIT, n_smc_hit);
6383 }
6384
6385 /* Try to process all ('cnt') the 'packets' using only the datapath flow cache
6386 * 'pmd->flow_cache'. If a flow is not found for a packet 'packets[i]', the
6387 * miniflow is copied into 'keys' and the packet pointer is moved at the
6388 * beginning of the 'packets' array. The pointers of missed keys are put in the
6389 * missed_keys pointer array for future processing.
6390 *
6391 * The function returns the number of packets that needs to be processed in the
6392 * 'packets' array (they have been moved to the beginning of the vector).
6393 *
6394 * For performance reasons a caller may choose not to initialize the metadata
6395 * in 'packets_'. If 'md_is_valid' is false, the metadata in 'packets'
6396 * is not valid and must be initialized by this function using 'port_no'.
6397 * If 'md_is_valid' is true, the metadata is already valid and 'port_no'
6398 * will be ignored.
6399 */
6400 static inline size_t
6401 dfc_processing(struct dp_netdev_pmd_thread *pmd,
6402 struct dp_packet_batch *packets_,
6403 struct netdev_flow_key *keys,
6404 struct netdev_flow_key **missed_keys,
6405 struct packet_batch_per_flow batches[], size_t *n_batches,
6406 struct dp_packet_flow_map *flow_map,
6407 size_t *n_flows, uint8_t *index_map,
6408 bool md_is_valid, odp_port_t port_no)
6409 {
6410 struct netdev_flow_key *key = &keys[0];
6411 size_t n_missed = 0, n_emc_hit = 0;
6412 struct dfc_cache *cache = &pmd->flow_cache;
6413 struct dp_packet *packet;
6414 const size_t cnt = dp_packet_batch_size(packets_);
6415 uint32_t cur_min = pmd->ctx.emc_insert_min;
6416 int i;
6417 uint16_t tcp_flags;
6418 bool smc_enable_db;
6419 size_t map_cnt = 0;
6420 bool batch_enable = true;
6421
6422 atomic_read_relaxed(&pmd->dp->smc_enable_db, &smc_enable_db);
6423 pmd_perf_update_counter(&pmd->perf_stats,
6424 md_is_valid ? PMD_STAT_RECIRC : PMD_STAT_RECV,
6425 cnt);
6426
6427 DP_PACKET_BATCH_REFILL_FOR_EACH (i, cnt, packet, packets_) {
6428 struct dp_netdev_flow *flow;
6429 uint32_t mark;
6430
6431 if (OVS_UNLIKELY(dp_packet_size(packet) < ETH_HEADER_LEN)) {
6432 dp_packet_delete(packet);
6433 continue;
6434 }
6435
6436 if (i != cnt - 1) {
6437 struct dp_packet **packets = packets_->packets;
6438 /* Prefetch next packet data and metadata. */
6439 OVS_PREFETCH(dp_packet_data(packets[i+1]));
6440 pkt_metadata_prefetch_init(&packets[i+1]->md);
6441 }
6442
6443 if (!md_is_valid) {
6444 pkt_metadata_init(&packet->md, port_no);
6445 }
6446
6447 if ((*recirc_depth_get() == 0) &&
6448 dp_packet_has_flow_mark(packet, &mark)) {
6449 flow = mark_to_flow_find(pmd, mark);
6450 if (OVS_LIKELY(flow)) {
6451 tcp_flags = parse_tcp_flags(packet);
6452 if (OVS_LIKELY(batch_enable)) {
6453 dp_netdev_queue_batches(packet, flow, tcp_flags, batches,
6454 n_batches);
6455 } else {
6456 /* Flow batching should be performed only after fast-path
6457 * processing is also completed for packets with emc miss
6458 * or else it will result in reordering of packets with
6459 * same datapath flows. */
6460 packet_enqueue_to_flow_map(packet, flow, tcp_flags,
6461 flow_map, map_cnt++);
6462 }
6463 continue;
6464 }
6465 }
6466
6467 miniflow_extract(packet, &key->mf);
6468 key->len = 0; /* Not computed yet. */
6469 key->hash =
6470 (md_is_valid == false)
6471 ? dpif_netdev_packet_get_rss_hash_orig_pkt(packet, &key->mf)
6472 : dpif_netdev_packet_get_rss_hash(packet, &key->mf);
6473
6474 /* If EMC is disabled skip emc_lookup */
6475 flow = (cur_min != 0) ? emc_lookup(&cache->emc_cache, key) : NULL;
6476 if (OVS_LIKELY(flow)) {
6477 tcp_flags = miniflow_get_tcp_flags(&key->mf);
6478 n_emc_hit++;
6479 if (OVS_LIKELY(batch_enable)) {
6480 dp_netdev_queue_batches(packet, flow, tcp_flags, batches,
6481 n_batches);
6482 } else {
6483 /* Flow batching should be performed only after fast-path
6484 * processing is also completed for packets with emc miss
6485 * or else it will result in reordering of packets with
6486 * same datapath flows. */
6487 packet_enqueue_to_flow_map(packet, flow, tcp_flags,
6488 flow_map, map_cnt++);
6489 }
6490 } else {
6491 /* Exact match cache missed. Group missed packets together at
6492 * the beginning of the 'packets' array. */
6493 dp_packet_batch_refill(packets_, packet, i);
6494
6495 /* Preserve the order of packet for flow batching. */
6496 index_map[n_missed] = map_cnt;
6497 flow_map[map_cnt++].flow = NULL;
6498
6499 /* 'key[n_missed]' contains the key of the current packet and it
6500 * will be passed to SMC lookup. The next key should be extracted
6501 * to 'keys[n_missed + 1]'.
6502 * We also maintain a pointer array to keys missed both SMC and EMC
6503 * which will be returned to the caller for future processing. */
6504 missed_keys[n_missed] = key;
6505 key = &keys[++n_missed];
6506
6507 /* Skip batching for subsequent packets to avoid reordering. */
6508 batch_enable = false;
6509 }
6510 }
6511 /* Count of packets which are not flow batched. */
6512 *n_flows = map_cnt;
6513
6514 pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_EXACT_HIT, n_emc_hit);
6515
6516 if (!smc_enable_db) {
6517 return dp_packet_batch_size(packets_);
6518 }
6519
6520 /* Packets miss EMC will do a batch lookup in SMC if enabled */
6521 smc_lookup_batch(pmd, keys, missed_keys, packets_,
6522 n_missed, flow_map, index_map);
6523
6524 return dp_packet_batch_size(packets_);
6525 }
6526
6527 static inline int
6528 handle_packet_upcall(struct dp_netdev_pmd_thread *pmd,
6529 struct dp_packet *packet,
6530 const struct netdev_flow_key *key,
6531 struct ofpbuf *actions, struct ofpbuf *put_actions)
6532 {
6533 struct ofpbuf *add_actions;
6534 struct dp_packet_batch b;
6535 struct match match;
6536 ovs_u128 ufid;
6537 int error;
6538 uint64_t cycles = cycles_counter_update(&pmd->perf_stats);
6539
6540 match.tun_md.valid = false;
6541 miniflow_expand(&key->mf, &match.flow);
6542
6543 ofpbuf_clear(actions);
6544 ofpbuf_clear(put_actions);
6545
6546 dpif_flow_hash(pmd->dp->dpif, &match.flow, sizeof match.flow, &ufid);
6547 error = dp_netdev_upcall(pmd, packet, &match.flow, &match.wc,
6548 &ufid, DPIF_UC_MISS, NULL, actions,
6549 put_actions);
6550 if (OVS_UNLIKELY(error && error != ENOSPC)) {
6551 dp_packet_delete(packet);
6552 return error;
6553 }
6554
6555 /* The Netlink encoding of datapath flow keys cannot express
6556 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
6557 * tag is interpreted as exact match on the fact that there is no
6558 * VLAN. Unless we refactor a lot of code that translates between
6559 * Netlink and struct flow representations, we have to do the same
6560 * here. This must be in sync with 'match' in dpif_netdev_flow_put(). */
6561 if (!match.wc.masks.vlans[0].tci) {
6562 match.wc.masks.vlans[0].tci = htons(0xffff);
6563 }
6564
6565 /* We can't allow the packet batching in the next loop to execute
6566 * the actions. Otherwise, if there are any slow path actions,
6567 * we'll send the packet up twice. */
6568 dp_packet_batch_init_packet(&b, packet);
6569 dp_netdev_execute_actions(pmd, &b, true, &match.flow,
6570 actions->data, actions->size);
6571
6572 add_actions = put_actions->size ? put_actions : actions;
6573 if (OVS_LIKELY(error != ENOSPC)) {
6574 struct dp_netdev_flow *netdev_flow;
6575
6576 /* XXX: There's a race window where a flow covering this packet
6577 * could have already been installed since we last did the flow
6578 * lookup before upcall. This could be solved by moving the
6579 * mutex lock outside the loop, but that's an awful long time
6580 * to be locking revalidators out of making flow modifications. */
6581 ovs_mutex_lock(&pmd->flow_mutex);
6582 netdev_flow = dp_netdev_pmd_lookup_flow(pmd, key, NULL);
6583 if (OVS_LIKELY(!netdev_flow)) {
6584 netdev_flow = dp_netdev_flow_add(pmd, &match, &ufid,
6585 add_actions->data,
6586 add_actions->size);
6587 }
6588 ovs_mutex_unlock(&pmd->flow_mutex);
6589 uint32_t hash = dp_netdev_flow_hash(&netdev_flow->ufid);
6590 smc_insert(pmd, key, hash);
6591 emc_probabilistic_insert(pmd, key, netdev_flow);
6592 }
6593 if (pmd_perf_metrics_enabled(pmd)) {
6594 /* Update upcall stats. */
6595 cycles = cycles_counter_update(&pmd->perf_stats) - cycles;
6596 struct pmd_perf_stats *s = &pmd->perf_stats;
6597 s->current.upcalls++;
6598 s->current.upcall_cycles += cycles;
6599 histogram_add_sample(&s->cycles_per_upcall, cycles);
6600 }
6601 return error;
6602 }
6603
6604 static inline void
6605 fast_path_processing(struct dp_netdev_pmd_thread *pmd,
6606 struct dp_packet_batch *packets_,
6607 struct netdev_flow_key **keys,
6608 struct dp_packet_flow_map *flow_map,
6609 uint8_t *index_map,
6610 odp_port_t in_port)
6611 {
6612 const size_t cnt = dp_packet_batch_size(packets_);
6613 #if !defined(__CHECKER__) && !defined(_WIN32)
6614 const size_t PKT_ARRAY_SIZE = cnt;
6615 #else
6616 /* Sparse or MSVC doesn't like variable length array. */
6617 enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST };
6618 #endif
6619 struct dp_packet *packet;
6620 struct dpcls *cls;
6621 struct dpcls_rule *rules[PKT_ARRAY_SIZE];
6622 struct dp_netdev *dp = pmd->dp;
6623 int upcall_ok_cnt = 0, upcall_fail_cnt = 0;
6624 int lookup_cnt = 0, add_lookup_cnt;
6625 bool any_miss;
6626
6627 for (size_t i = 0; i < cnt; i++) {
6628 /* Key length is needed in all the cases, hash computed on demand. */
6629 keys[i]->len = netdev_flow_key_size(miniflow_n_values(&keys[i]->mf));
6630 }
6631 /* Get the classifier for the in_port */
6632 cls = dp_netdev_pmd_lookup_dpcls(pmd, in_port);
6633 if (OVS_LIKELY(cls)) {
6634 any_miss = !dpcls_lookup(cls, (const struct netdev_flow_key **)keys,
6635 rules, cnt, &lookup_cnt);
6636 } else {
6637 any_miss = true;
6638 memset(rules, 0, sizeof(rules));
6639 }
6640 if (OVS_UNLIKELY(any_miss) && !fat_rwlock_tryrdlock(&dp->upcall_rwlock)) {
6641 uint64_t actions_stub[512 / 8], slow_stub[512 / 8];
6642 struct ofpbuf actions, put_actions;
6643
6644 ofpbuf_use_stub(&actions, actions_stub, sizeof actions_stub);
6645 ofpbuf_use_stub(&put_actions, slow_stub, sizeof slow_stub);
6646
6647 DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) {
6648 struct dp_netdev_flow *netdev_flow;
6649
6650 if (OVS_LIKELY(rules[i])) {
6651 continue;
6652 }
6653
6654 /* It's possible that an earlier slow path execution installed
6655 * a rule covering this flow. In this case, it's a lot cheaper
6656 * to catch it here than execute a miss. */
6657 netdev_flow = dp_netdev_pmd_lookup_flow(pmd, keys[i],
6658 &add_lookup_cnt);
6659 if (netdev_flow) {
6660 lookup_cnt += add_lookup_cnt;
6661 rules[i] = &netdev_flow->cr;
6662 continue;
6663 }
6664
6665 int error = handle_packet_upcall(pmd, packet, keys[i],
6666 &actions, &put_actions);
6667
6668 if (OVS_UNLIKELY(error)) {
6669 upcall_fail_cnt++;
6670 } else {
6671 upcall_ok_cnt++;
6672 }
6673 }
6674
6675 ofpbuf_uninit(&actions);
6676 ofpbuf_uninit(&put_actions);
6677 fat_rwlock_unlock(&dp->upcall_rwlock);
6678 } else if (OVS_UNLIKELY(any_miss)) {
6679 DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) {
6680 if (OVS_UNLIKELY(!rules[i])) {
6681 dp_packet_delete(packet);
6682 upcall_fail_cnt++;
6683 }
6684 }
6685 }
6686
6687 DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) {
6688 struct dp_netdev_flow *flow;
6689 /* Get the original order of this packet in received batch. */
6690 int recv_idx = index_map[i];
6691 uint16_t tcp_flags;
6692
6693 if (OVS_UNLIKELY(!rules[i])) {
6694 continue;
6695 }
6696
6697 flow = dp_netdev_flow_cast(rules[i]);
6698 uint32_t hash = dp_netdev_flow_hash(&flow->ufid);
6699 smc_insert(pmd, keys[i], hash);
6700
6701 emc_probabilistic_insert(pmd, keys[i], flow);
6702 /* Add these packets into the flow map in the same order
6703 * as received.
6704 */
6705 tcp_flags = miniflow_get_tcp_flags(&keys[i]->mf);
6706 packet_enqueue_to_flow_map(packet, flow, tcp_flags,
6707 flow_map, recv_idx);
6708 }
6709
6710 pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_MASKED_HIT,
6711 cnt - upcall_ok_cnt - upcall_fail_cnt);
6712 pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_MASKED_LOOKUP,
6713 lookup_cnt);
6714 pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_MISS,
6715 upcall_ok_cnt);
6716 pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_LOST,
6717 upcall_fail_cnt);
6718 }
6719
6720 /* Packets enter the datapath from a port (or from recirculation) here.
6721 *
6722 * When 'md_is_valid' is true the metadata in 'packets' are already valid.
6723 * When false the metadata in 'packets' need to be initialized. */
6724 static void
6725 dp_netdev_input__(struct dp_netdev_pmd_thread *pmd,
6726 struct dp_packet_batch *packets,
6727 bool md_is_valid, odp_port_t port_no)
6728 {
6729 #if !defined(__CHECKER__) && !defined(_WIN32)
6730 const size_t PKT_ARRAY_SIZE = dp_packet_batch_size(packets);
6731 #else
6732 /* Sparse or MSVC doesn't like variable length array. */
6733 enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST };
6734 #endif
6735 OVS_ALIGNED_VAR(CACHE_LINE_SIZE)
6736 struct netdev_flow_key keys[PKT_ARRAY_SIZE];
6737 struct netdev_flow_key *missed_keys[PKT_ARRAY_SIZE];
6738 struct packet_batch_per_flow batches[PKT_ARRAY_SIZE];
6739 size_t n_batches;
6740 struct dp_packet_flow_map flow_map[PKT_ARRAY_SIZE];
6741 uint8_t index_map[PKT_ARRAY_SIZE];
6742 size_t n_flows, i;
6743
6744 odp_port_t in_port;
6745
6746 n_batches = 0;
6747 dfc_processing(pmd, packets, keys, missed_keys, batches, &n_batches,
6748 flow_map, &n_flows, index_map, md_is_valid, port_no);
6749
6750 if (!dp_packet_batch_is_empty(packets)) {
6751 /* Get ingress port from first packet's metadata. */
6752 in_port = packets->packets[0]->md.in_port.odp_port;
6753 fast_path_processing(pmd, packets, missed_keys,
6754 flow_map, index_map, in_port);
6755 }
6756
6757 /* Batch rest of packets which are in flow map. */
6758 for (i = 0; i < n_flows; i++) {
6759 struct dp_packet_flow_map *map = &flow_map[i];
6760
6761 if (OVS_UNLIKELY(!map->flow)) {
6762 continue;
6763 }
6764 dp_netdev_queue_batches(map->packet, map->flow, map->tcp_flags,
6765 batches, &n_batches);
6766 }
6767
6768 /* All the flow batches need to be reset before any call to
6769 * packet_batch_per_flow_execute() as it could potentially trigger
6770 * recirculation. When a packet matching flow ‘j’ happens to be
6771 * recirculated, the nested call to dp_netdev_input__() could potentially
6772 * classify the packet as matching another flow - say 'k'. It could happen
6773 * that in the previous call to dp_netdev_input__() that same flow 'k' had
6774 * already its own batches[k] still waiting to be served. So if its
6775 * ‘batch’ member is not reset, the recirculated packet would be wrongly
6776 * appended to batches[k] of the 1st call to dp_netdev_input__(). */
6777 for (i = 0; i < n_batches; i++) {
6778 batches[i].flow->batch = NULL;
6779 }
6780
6781 for (i = 0; i < n_batches; i++) {
6782 packet_batch_per_flow_execute(&batches[i], pmd);
6783 }
6784 }
6785
6786 static void
6787 dp_netdev_input(struct dp_netdev_pmd_thread *pmd,
6788 struct dp_packet_batch *packets,
6789 odp_port_t port_no)
6790 {
6791 dp_netdev_input__(pmd, packets, false, port_no);
6792 }
6793
6794 static void
6795 dp_netdev_recirculate(struct dp_netdev_pmd_thread *pmd,
6796 struct dp_packet_batch *packets)
6797 {
6798 dp_netdev_input__(pmd, packets, true, 0);
6799 }
6800
6801 struct dp_netdev_execute_aux {
6802 struct dp_netdev_pmd_thread *pmd;
6803 const struct flow *flow;
6804 };
6805
6806 static void
6807 dpif_netdev_register_dp_purge_cb(struct dpif *dpif, dp_purge_callback *cb,
6808 void *aux)
6809 {
6810 struct dp_netdev *dp = get_dp_netdev(dpif);
6811 dp->dp_purge_aux = aux;
6812 dp->dp_purge_cb = cb;
6813 }
6814
6815 static void
6816 dpif_netdev_register_upcall_cb(struct dpif *dpif, upcall_callback *cb,
6817 void *aux)
6818 {
6819 struct dp_netdev *dp = get_dp_netdev(dpif);
6820 dp->upcall_aux = aux;
6821 dp->upcall_cb = cb;
6822 }
6823
6824 static void
6825 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread *pmd,
6826 bool purge)
6827 {
6828 struct tx_port *tx;
6829 struct dp_netdev_port *port;
6830 long long interval;
6831
6832 HMAP_FOR_EACH (tx, node, &pmd->send_port_cache) {
6833 if (!tx->port->dynamic_txqs) {
6834 continue;
6835 }
6836 interval = pmd->ctx.now - tx->last_used;
6837 if (tx->qid >= 0 && (purge || interval >= XPS_TIMEOUT)) {
6838 port = tx->port;
6839 ovs_mutex_lock(&port->txq_used_mutex);
6840 port->txq_used[tx->qid]--;
6841 ovs_mutex_unlock(&port->txq_used_mutex);
6842 tx->qid = -1;
6843 }
6844 }
6845 }
6846
6847 static int
6848 dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread *pmd,
6849 struct tx_port *tx)
6850 {
6851 struct dp_netdev_port *port;
6852 long long interval;
6853 int i, min_cnt, min_qid;
6854
6855 interval = pmd->ctx.now - tx->last_used;
6856 tx->last_used = pmd->ctx.now;
6857
6858 if (OVS_LIKELY(tx->qid >= 0 && interval < XPS_TIMEOUT)) {
6859 return tx->qid;
6860 }
6861
6862 port = tx->port;
6863
6864 ovs_mutex_lock(&port->txq_used_mutex);
6865 if (tx->qid >= 0) {
6866 port->txq_used[tx->qid]--;
6867 tx->qid = -1;
6868 }
6869
6870 min_cnt = -1;
6871 min_qid = 0;
6872 for (i = 0; i < netdev_n_txq(port->netdev); i++) {
6873 if (port->txq_used[i] < min_cnt || min_cnt == -1) {
6874 min_cnt = port->txq_used[i];
6875 min_qid = i;
6876 }
6877 }
6878
6879 port->txq_used[min_qid]++;
6880 tx->qid = min_qid;
6881
6882 ovs_mutex_unlock(&port->txq_used_mutex);
6883
6884 dpif_netdev_xps_revalidate_pmd(pmd, false);
6885
6886 VLOG_DBG("Core %d: New TX queue ID %d for port \'%s\'.",
6887 pmd->core_id, tx->qid, netdev_get_name(tx->port->netdev));
6888 return min_qid;
6889 }
6890
6891 static struct tx_port *
6892 pmd_tnl_port_cache_lookup(const struct dp_netdev_pmd_thread *pmd,
6893 odp_port_t port_no)
6894 {
6895 return tx_port_lookup(&pmd->tnl_port_cache, port_no);
6896 }
6897
6898 static struct tx_port *
6899 pmd_send_port_cache_lookup(const struct dp_netdev_pmd_thread *pmd,
6900 odp_port_t port_no)
6901 {
6902 return tx_port_lookup(&pmd->send_port_cache, port_no);
6903 }
6904
6905 static int
6906 push_tnl_action(const struct dp_netdev_pmd_thread *pmd,
6907 const struct nlattr *attr,
6908 struct dp_packet_batch *batch)
6909 {
6910 struct tx_port *tun_port;
6911 const struct ovs_action_push_tnl *data;
6912 int err;
6913
6914 data = nl_attr_get(attr);
6915
6916 tun_port = pmd_tnl_port_cache_lookup(pmd, data->tnl_port);
6917 if (!tun_port) {
6918 err = -EINVAL;
6919 goto error;
6920 }
6921 err = netdev_push_header(tun_port->port->netdev, batch, data);
6922 if (!err) {
6923 return 0;
6924 }
6925 error:
6926 dp_packet_delete_batch(batch, true);
6927 return err;
6928 }
6929
6930 static void
6931 dp_execute_userspace_action(struct dp_netdev_pmd_thread *pmd,
6932 struct dp_packet *packet, bool should_steal,
6933 struct flow *flow, ovs_u128 *ufid,
6934 struct ofpbuf *actions,
6935 const struct nlattr *userdata)
6936 {
6937 struct dp_packet_batch b;
6938 int error;
6939
6940 ofpbuf_clear(actions);
6941
6942 error = dp_netdev_upcall(pmd, packet, flow, NULL, ufid,
6943 DPIF_UC_ACTION, userdata, actions,
6944 NULL);
6945 if (!error || error == ENOSPC) {
6946 dp_packet_batch_init_packet(&b, packet);
6947 dp_netdev_execute_actions(pmd, &b, should_steal, flow,
6948 actions->data, actions->size);
6949 } else if (should_steal) {
6950 dp_packet_delete(packet);
6951 }
6952 }
6953
6954 static void
6955 dp_execute_cb(void *aux_, struct dp_packet_batch *packets_,
6956 const struct nlattr *a, bool should_steal)
6957 OVS_NO_THREAD_SAFETY_ANALYSIS
6958 {
6959 struct dp_netdev_execute_aux *aux = aux_;
6960 uint32_t *depth = recirc_depth_get();
6961 struct dp_netdev_pmd_thread *pmd = aux->pmd;
6962 struct dp_netdev *dp = pmd->dp;
6963 int type = nl_attr_type(a);
6964 struct tx_port *p;
6965
6966 switch ((enum ovs_action_attr)type) {
6967 case OVS_ACTION_ATTR_OUTPUT:
6968 p = pmd_send_port_cache_lookup(pmd, nl_attr_get_odp_port(a));
6969 if (OVS_LIKELY(p)) {
6970 struct dp_packet *packet;
6971 struct dp_packet_batch out;
6972
6973 if (!should_steal) {
6974 dp_packet_batch_clone(&out, packets_);
6975 dp_packet_batch_reset_cutlen(packets_);
6976 packets_ = &out;
6977 }
6978 dp_packet_batch_apply_cutlen(packets_);
6979
6980 #ifdef DPDK_NETDEV
6981 if (OVS_UNLIKELY(!dp_packet_batch_is_empty(&p->output_pkts)
6982 && packets_->packets[0]->source
6983 != p->output_pkts.packets[0]->source)) {
6984 /* XXX: netdev-dpdk assumes that all packets in a single
6985 * output batch has the same source. Flush here to
6986 * avoid memory access issues. */
6987 dp_netdev_pmd_flush_output_on_port(pmd, p);
6988 }
6989 #endif
6990 if (dp_packet_batch_size(&p->output_pkts)
6991 + dp_packet_batch_size(packets_) > NETDEV_MAX_BURST) {
6992 /* Flush here to avoid overflow. */
6993 dp_netdev_pmd_flush_output_on_port(pmd, p);
6994 }
6995
6996 if (dp_packet_batch_is_empty(&p->output_pkts)) {
6997 pmd->n_output_batches++;
6998 }
6999
7000 DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) {
7001 p->output_pkts_rxqs[dp_packet_batch_size(&p->output_pkts)] =
7002 pmd->ctx.last_rxq;
7003 dp_packet_batch_add(&p->output_pkts, packet);
7004 }
7005 return;
7006 }
7007 break;
7008
7009 case OVS_ACTION_ATTR_TUNNEL_PUSH:
7010 if (should_steal) {
7011 /* We're requested to push tunnel header, but also we need to take
7012 * the ownership of these packets. Thus, we can avoid performing
7013 * the action, because the caller will not use the result anyway.
7014 * Just break to free the batch. */
7015 break;
7016 }
7017 dp_packet_batch_apply_cutlen(packets_);
7018 push_tnl_action(pmd, a, packets_);
7019 return;
7020
7021 case OVS_ACTION_ATTR_TUNNEL_POP:
7022 if (*depth < MAX_RECIRC_DEPTH) {
7023 struct dp_packet_batch *orig_packets_ = packets_;
7024 odp_port_t portno = nl_attr_get_odp_port(a);
7025
7026 p = pmd_tnl_port_cache_lookup(pmd, portno);
7027 if (p) {
7028 struct dp_packet_batch tnl_pkt;
7029
7030 if (!should_steal) {
7031 dp_packet_batch_clone(&tnl_pkt, packets_);
7032 packets_ = &tnl_pkt;
7033 dp_packet_batch_reset_cutlen(orig_packets_);
7034 }
7035
7036 dp_packet_batch_apply_cutlen(packets_);
7037
7038 netdev_pop_header(p->port->netdev, packets_);
7039 if (dp_packet_batch_is_empty(packets_)) {
7040 return;
7041 }
7042
7043 struct dp_packet *packet;
7044 DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) {
7045 packet->md.in_port.odp_port = portno;
7046 }
7047
7048 (*depth)++;
7049 dp_netdev_recirculate(pmd, packets_);
7050 (*depth)--;
7051 return;
7052 }
7053 }
7054 break;
7055
7056 case OVS_ACTION_ATTR_USERSPACE:
7057 if (!fat_rwlock_tryrdlock(&dp->upcall_rwlock)) {
7058 struct dp_packet_batch *orig_packets_ = packets_;
7059 const struct nlattr *userdata;
7060 struct dp_packet_batch usr_pkt;
7061 struct ofpbuf actions;
7062 struct flow flow;
7063 ovs_u128 ufid;
7064 bool clone = false;
7065
7066 userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
7067 ofpbuf_init(&actions, 0);
7068
7069 if (packets_->trunc) {
7070 if (!should_steal) {
7071 dp_packet_batch_clone(&usr_pkt, packets_);
7072 packets_ = &usr_pkt;
7073 clone = true;
7074 dp_packet_batch_reset_cutlen(orig_packets_);
7075 }
7076
7077 dp_packet_batch_apply_cutlen(packets_);
7078 }
7079
7080 struct dp_packet *packet;
7081 DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) {
7082 flow_extract(packet, &flow);
7083 dpif_flow_hash(dp->dpif, &flow, sizeof flow, &ufid);
7084 dp_execute_userspace_action(pmd, packet, should_steal, &flow,
7085 &ufid, &actions, userdata);
7086 }
7087
7088 if (clone) {
7089 dp_packet_delete_batch(packets_, true);
7090 }
7091
7092 ofpbuf_uninit(&actions);
7093 fat_rwlock_unlock(&dp->upcall_rwlock);
7094
7095 return;
7096 }
7097 break;
7098
7099 case OVS_ACTION_ATTR_RECIRC:
7100 if (*depth < MAX_RECIRC_DEPTH) {
7101 struct dp_packet_batch recirc_pkts;
7102
7103 if (!should_steal) {
7104 dp_packet_batch_clone(&recirc_pkts, packets_);
7105 packets_ = &recirc_pkts;
7106 }
7107
7108 struct dp_packet *packet;
7109 DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) {
7110 packet->md.recirc_id = nl_attr_get_u32(a);
7111 }
7112
7113 (*depth)++;
7114 dp_netdev_recirculate(pmd, packets_);
7115 (*depth)--;
7116
7117 return;
7118 }
7119
7120 VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
7121 break;
7122
7123 case OVS_ACTION_ATTR_CT: {
7124 const struct nlattr *b;
7125 bool force = false;
7126 bool commit = false;
7127 unsigned int left;
7128 uint16_t zone = 0;
7129 const char *helper = NULL;
7130 const uint32_t *setmark = NULL;
7131 const struct ovs_key_ct_labels *setlabel = NULL;
7132 struct nat_action_info_t nat_action_info;
7133 struct nat_action_info_t *nat_action_info_ref = NULL;
7134 bool nat_config = false;
7135
7136 NL_ATTR_FOR_EACH_UNSAFE (b, left, nl_attr_get(a),
7137 nl_attr_get_size(a)) {
7138 enum ovs_ct_attr sub_type = nl_attr_type(b);
7139
7140 switch(sub_type) {
7141 case OVS_CT_ATTR_FORCE_COMMIT:
7142 force = true;
7143 /* fall through. */
7144 case OVS_CT_ATTR_COMMIT:
7145 commit = true;
7146 break;
7147 case OVS_CT_ATTR_ZONE:
7148 zone = nl_attr_get_u16(b);
7149 break;
7150 case OVS_CT_ATTR_HELPER:
7151 helper = nl_attr_get_string(b);
7152 break;
7153 case OVS_CT_ATTR_MARK:
7154 setmark = nl_attr_get(b);
7155 break;
7156 case OVS_CT_ATTR_LABELS:
7157 setlabel = nl_attr_get(b);
7158 break;
7159 case OVS_CT_ATTR_EVENTMASK:
7160 /* Silently ignored, as userspace datapath does not generate
7161 * netlink events. */
7162 break;
7163 case OVS_CT_ATTR_NAT: {
7164 const struct nlattr *b_nest;
7165 unsigned int left_nest;
7166 bool ip_min_specified = false;
7167 bool proto_num_min_specified = false;
7168 bool ip_max_specified = false;
7169 bool proto_num_max_specified = false;
7170 memset(&nat_action_info, 0, sizeof nat_action_info);
7171 nat_action_info_ref = &nat_action_info;
7172
7173 NL_NESTED_FOR_EACH_UNSAFE (b_nest, left_nest, b) {
7174 enum ovs_nat_attr sub_type_nest = nl_attr_type(b_nest);
7175
7176 switch (sub_type_nest) {
7177 case OVS_NAT_ATTR_SRC:
7178 case OVS_NAT_ATTR_DST:
7179 nat_config = true;
7180 nat_action_info.nat_action |=
7181 ((sub_type_nest == OVS_NAT_ATTR_SRC)
7182 ? NAT_ACTION_SRC : NAT_ACTION_DST);
7183 break;
7184 case OVS_NAT_ATTR_IP_MIN:
7185 memcpy(&nat_action_info.min_addr,
7186 nl_attr_get(b_nest),
7187 nl_attr_get_size(b_nest));
7188 ip_min_specified = true;
7189 break;
7190 case OVS_NAT_ATTR_IP_MAX:
7191 memcpy(&nat_action_info.max_addr,
7192 nl_attr_get(b_nest),
7193 nl_attr_get_size(b_nest));
7194 ip_max_specified = true;
7195 break;
7196 case OVS_NAT_ATTR_PROTO_MIN:
7197 nat_action_info.min_port =
7198 nl_attr_get_u16(b_nest);
7199 proto_num_min_specified = true;
7200 break;
7201 case OVS_NAT_ATTR_PROTO_MAX:
7202 nat_action_info.max_port =
7203 nl_attr_get_u16(b_nest);
7204 proto_num_max_specified = true;
7205 break;
7206 case OVS_NAT_ATTR_PERSISTENT:
7207 case OVS_NAT_ATTR_PROTO_HASH:
7208 case OVS_NAT_ATTR_PROTO_RANDOM:
7209 break;
7210 case OVS_NAT_ATTR_UNSPEC:
7211 case __OVS_NAT_ATTR_MAX:
7212 OVS_NOT_REACHED();
7213 }
7214 }
7215
7216 if (ip_min_specified && !ip_max_specified) {
7217 nat_action_info.max_addr = nat_action_info.min_addr;
7218 }
7219 if (proto_num_min_specified && !proto_num_max_specified) {
7220 nat_action_info.max_port = nat_action_info.min_port;
7221 }
7222 if (proto_num_min_specified || proto_num_max_specified) {
7223 if (nat_action_info.nat_action & NAT_ACTION_SRC) {
7224 nat_action_info.nat_action |= NAT_ACTION_SRC_PORT;
7225 } else if (nat_action_info.nat_action & NAT_ACTION_DST) {
7226 nat_action_info.nat_action |= NAT_ACTION_DST_PORT;
7227 }
7228 }
7229 break;
7230 }
7231 case OVS_CT_ATTR_UNSPEC:
7232 case __OVS_CT_ATTR_MAX:
7233 OVS_NOT_REACHED();
7234 }
7235 }
7236
7237 /* We won't be able to function properly in this case, hence
7238 * complain loudly. */
7239 if (nat_config && !commit) {
7240 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
7241 VLOG_WARN_RL(&rl, "NAT specified without commit.");
7242 }
7243
7244 conntrack_execute(dp->conntrack, packets_, aux->flow->dl_type, force,
7245 commit, zone, setmark, setlabel, aux->flow->tp_src,
7246 aux->flow->tp_dst, helper, nat_action_info_ref,
7247 pmd->ctx.now / 1000);
7248 break;
7249 }
7250
7251 case OVS_ACTION_ATTR_METER:
7252 dp_netdev_run_meter(pmd->dp, packets_, nl_attr_get_u32(a),
7253 pmd->ctx.now);
7254 break;
7255
7256 case OVS_ACTION_ATTR_PUSH_VLAN:
7257 case OVS_ACTION_ATTR_POP_VLAN:
7258 case OVS_ACTION_ATTR_PUSH_MPLS:
7259 case OVS_ACTION_ATTR_POP_MPLS:
7260 case OVS_ACTION_ATTR_SET:
7261 case OVS_ACTION_ATTR_SET_MASKED:
7262 case OVS_ACTION_ATTR_SAMPLE:
7263 case OVS_ACTION_ATTR_HASH:
7264 case OVS_ACTION_ATTR_UNSPEC:
7265 case OVS_ACTION_ATTR_TRUNC:
7266 case OVS_ACTION_ATTR_PUSH_ETH:
7267 case OVS_ACTION_ATTR_POP_ETH:
7268 case OVS_ACTION_ATTR_CLONE:
7269 case OVS_ACTION_ATTR_PUSH_NSH:
7270 case OVS_ACTION_ATTR_POP_NSH:
7271 case OVS_ACTION_ATTR_CT_CLEAR:
7272 case OVS_ACTION_ATTR_CHECK_PKT_LEN:
7273 case __OVS_ACTION_ATTR_MAX:
7274 OVS_NOT_REACHED();
7275 }
7276
7277 dp_packet_delete_batch(packets_, should_steal);
7278 }
7279
7280 static void
7281 dp_netdev_execute_actions(struct dp_netdev_pmd_thread *pmd,
7282 struct dp_packet_batch *packets,
7283 bool should_steal, const struct flow *flow,
7284 const struct nlattr *actions, size_t actions_len)
7285 {
7286 struct dp_netdev_execute_aux aux = { pmd, flow };
7287
7288 odp_execute_actions(&aux, packets, should_steal, actions,
7289 actions_len, dp_execute_cb);
7290 }
7291
7292 struct dp_netdev_ct_dump {
7293 struct ct_dpif_dump_state up;
7294 struct conntrack_dump dump;
7295 struct conntrack *ct;
7296 struct dp_netdev *dp;
7297 };
7298
7299 static int
7300 dpif_netdev_ct_dump_start(struct dpif *dpif, struct ct_dpif_dump_state **dump_,
7301 const uint16_t *pzone, int *ptot_bkts)
7302 {
7303 struct dp_netdev *dp = get_dp_netdev(dpif);
7304 struct dp_netdev_ct_dump *dump;
7305
7306 dump = xzalloc(sizeof *dump);
7307 dump->dp = dp;
7308 dump->ct = dp->conntrack;
7309
7310 conntrack_dump_start(dp->conntrack, &dump->dump, pzone, ptot_bkts);
7311
7312 *dump_ = &dump->up;
7313
7314 return 0;
7315 }
7316
7317 static int
7318 dpif_netdev_ct_dump_next(struct dpif *dpif OVS_UNUSED,
7319 struct ct_dpif_dump_state *dump_,
7320 struct ct_dpif_entry *entry)
7321 {
7322 struct dp_netdev_ct_dump *dump;
7323
7324 INIT_CONTAINER(dump, dump_, up);
7325
7326 return conntrack_dump_next(&dump->dump, entry);
7327 }
7328
7329 static int
7330 dpif_netdev_ct_dump_done(struct dpif *dpif OVS_UNUSED,
7331 struct ct_dpif_dump_state *dump_)
7332 {
7333 struct dp_netdev_ct_dump *dump;
7334 int err;
7335
7336 INIT_CONTAINER(dump, dump_, up);
7337
7338 err = conntrack_dump_done(&dump->dump);
7339
7340 free(dump);
7341
7342 return err;
7343 }
7344
7345 static int
7346 dpif_netdev_ct_flush(struct dpif *dpif, const uint16_t *zone,
7347 const struct ct_dpif_tuple *tuple)
7348 {
7349 struct dp_netdev *dp = get_dp_netdev(dpif);
7350
7351 if (tuple) {
7352 return conntrack_flush_tuple(dp->conntrack, tuple, zone ? *zone : 0);
7353 }
7354 return conntrack_flush(dp->conntrack, zone);
7355 }
7356
7357 static int
7358 dpif_netdev_ct_set_maxconns(struct dpif *dpif, uint32_t maxconns)
7359 {
7360 struct dp_netdev *dp = get_dp_netdev(dpif);
7361
7362 return conntrack_set_maxconns(dp->conntrack, maxconns);
7363 }
7364
7365 static int
7366 dpif_netdev_ct_get_maxconns(struct dpif *dpif, uint32_t *maxconns)
7367 {
7368 struct dp_netdev *dp = get_dp_netdev(dpif);
7369
7370 return conntrack_get_maxconns(dp->conntrack, maxconns);
7371 }
7372
7373 static int
7374 dpif_netdev_ct_get_nconns(struct dpif *dpif, uint32_t *nconns)
7375 {
7376 struct dp_netdev *dp = get_dp_netdev(dpif);
7377
7378 return conntrack_get_nconns(dp->conntrack, nconns);
7379 }
7380
7381 static int
7382 dpif_netdev_ipf_set_enabled(struct dpif *dpif, bool v6, bool enable)
7383 {
7384 struct dp_netdev *dp = get_dp_netdev(dpif);
7385 return ipf_set_enabled(conntrack_ipf_ctx(dp->conntrack), v6, enable);
7386 }
7387
7388 static int
7389 dpif_netdev_ipf_set_min_frag(struct dpif *dpif, bool v6, uint32_t min_frag)
7390 {
7391 struct dp_netdev *dp = get_dp_netdev(dpif);
7392 return ipf_set_min_frag(conntrack_ipf_ctx(dp->conntrack), v6, min_frag);
7393 }
7394
7395 static int
7396 dpif_netdev_ipf_set_max_nfrags(struct dpif *dpif, uint32_t max_frags)
7397 {
7398 struct dp_netdev *dp = get_dp_netdev(dpif);
7399 return ipf_set_max_nfrags(conntrack_ipf_ctx(dp->conntrack), max_frags);
7400 }
7401
7402 /* Adjust this function if 'dpif_ipf_status' and 'ipf_status' were to
7403 * diverge. */
7404 static int
7405 dpif_netdev_ipf_get_status(struct dpif *dpif,
7406 struct dpif_ipf_status *dpif_ipf_status)
7407 {
7408 struct dp_netdev *dp = get_dp_netdev(dpif);
7409 ipf_get_status(conntrack_ipf_ctx(dp->conntrack),
7410 (struct ipf_status *) dpif_ipf_status);
7411 return 0;
7412 }
7413
7414 static int
7415 dpif_netdev_ipf_dump_start(struct dpif *dpif OVS_UNUSED,
7416 struct ipf_dump_ctx **ipf_dump_ctx)
7417 {
7418 return ipf_dump_start(ipf_dump_ctx);
7419 }
7420
7421 static int
7422 dpif_netdev_ipf_dump_next(struct dpif *dpif, void *ipf_dump_ctx, char **dump)
7423 {
7424 struct dp_netdev *dp = get_dp_netdev(dpif);
7425 return ipf_dump_next(conntrack_ipf_ctx(dp->conntrack), ipf_dump_ctx,
7426 dump);
7427 }
7428
7429 static int
7430 dpif_netdev_ipf_dump_done(struct dpif *dpif OVS_UNUSED, void *ipf_dump_ctx)
7431 {
7432 return ipf_dump_done(ipf_dump_ctx);
7433
7434 }
7435
7436 const struct dpif_class dpif_netdev_class = {
7437 "netdev",
7438 true, /* cleanup_required */
7439 dpif_netdev_init,
7440 dpif_netdev_enumerate,
7441 dpif_netdev_port_open_type,
7442 dpif_netdev_open,
7443 dpif_netdev_close,
7444 dpif_netdev_destroy,
7445 dpif_netdev_run,
7446 dpif_netdev_wait,
7447 dpif_netdev_get_stats,
7448 dpif_netdev_port_add,
7449 dpif_netdev_port_del,
7450 dpif_netdev_port_set_config,
7451 dpif_netdev_port_query_by_number,
7452 dpif_netdev_port_query_by_name,
7453 NULL, /* port_get_pid */
7454 dpif_netdev_port_dump_start,
7455 dpif_netdev_port_dump_next,
7456 dpif_netdev_port_dump_done,
7457 dpif_netdev_port_poll,
7458 dpif_netdev_port_poll_wait,
7459 dpif_netdev_flow_flush,
7460 dpif_netdev_flow_dump_create,
7461 dpif_netdev_flow_dump_destroy,
7462 dpif_netdev_flow_dump_thread_create,
7463 dpif_netdev_flow_dump_thread_destroy,
7464 dpif_netdev_flow_dump_next,
7465 dpif_netdev_operate,
7466 NULL, /* recv_set */
7467 NULL, /* handlers_set */
7468 dpif_netdev_set_config,
7469 dpif_netdev_queue_to_priority,
7470 NULL, /* recv */
7471 NULL, /* recv_wait */
7472 NULL, /* recv_purge */
7473 dpif_netdev_register_dp_purge_cb,
7474 dpif_netdev_register_upcall_cb,
7475 dpif_netdev_enable_upcall,
7476 dpif_netdev_disable_upcall,
7477 dpif_netdev_get_datapath_version,
7478 dpif_netdev_ct_dump_start,
7479 dpif_netdev_ct_dump_next,
7480 dpif_netdev_ct_dump_done,
7481 dpif_netdev_ct_flush,
7482 dpif_netdev_ct_set_maxconns,
7483 dpif_netdev_ct_get_maxconns,
7484 dpif_netdev_ct_get_nconns,
7485 NULL, /* ct_set_limits */
7486 NULL, /* ct_get_limits */
7487 NULL, /* ct_del_limits */
7488 dpif_netdev_ipf_set_enabled,
7489 dpif_netdev_ipf_set_min_frag,
7490 dpif_netdev_ipf_set_max_nfrags,
7491 dpif_netdev_ipf_get_status,
7492 dpif_netdev_ipf_dump_start,
7493 dpif_netdev_ipf_dump_next,
7494 dpif_netdev_ipf_dump_done,
7495 dpif_netdev_meter_get_features,
7496 dpif_netdev_meter_set,
7497 dpif_netdev_meter_get,
7498 dpif_netdev_meter_del,
7499 };
7500
7501 static void
7502 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
7503 const char *argv[], void *aux OVS_UNUSED)
7504 {
7505 struct dp_netdev_port *port;
7506 struct dp_netdev *dp;
7507 odp_port_t port_no;
7508
7509 ovs_mutex_lock(&dp_netdev_mutex);
7510 dp = shash_find_data(&dp_netdevs, argv[1]);
7511 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
7512 ovs_mutex_unlock(&dp_netdev_mutex);
7513 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
7514 return;
7515 }
7516 ovs_refcount_ref(&dp->ref_cnt);
7517 ovs_mutex_unlock(&dp_netdev_mutex);
7518
7519 ovs_mutex_lock(&dp->port_mutex);
7520 if (get_port_by_name(dp, argv[2], &port)) {
7521 unixctl_command_reply_error(conn, "unknown port");
7522 goto exit;
7523 }
7524
7525 port_no = u32_to_odp(atoi(argv[3]));
7526 if (!port_no || port_no == ODPP_NONE) {
7527 unixctl_command_reply_error(conn, "bad port number");
7528 goto exit;
7529 }
7530 if (dp_netdev_lookup_port(dp, port_no)) {
7531 unixctl_command_reply_error(conn, "port number already in use");
7532 goto exit;
7533 }
7534
7535 /* Remove port. */
7536 hmap_remove(&dp->ports, &port->node);
7537 reconfigure_datapath(dp);
7538
7539 /* Reinsert with new port number. */
7540 port->port_no = port_no;
7541 hmap_insert(&dp->ports, &port->node, hash_port_no(port_no));
7542 reconfigure_datapath(dp);
7543
7544 seq_change(dp->port_seq);
7545 unixctl_command_reply(conn, NULL);
7546
7547 exit:
7548 ovs_mutex_unlock(&dp->port_mutex);
7549 dp_netdev_unref(dp);
7550 }
7551
7552 static void
7553 dpif_dummy_register__(const char *type)
7554 {
7555 struct dpif_class *class;
7556
7557 class = xmalloc(sizeof *class);
7558 *class = dpif_netdev_class;
7559 class->type = xstrdup(type);
7560 dp_register_provider(class);
7561 }
7562
7563 static void
7564 dpif_dummy_override(const char *type)
7565 {
7566 int error;
7567
7568 /*
7569 * Ignore EAFNOSUPPORT to allow --enable-dummy=system with
7570 * a userland-only build. It's useful for testsuite.
7571 */
7572 error = dp_unregister_provider(type);
7573 if (error == 0 || error == EAFNOSUPPORT) {
7574 dpif_dummy_register__(type);
7575 }
7576 }
7577
7578 void
7579 dpif_dummy_register(enum dummy_level level)
7580 {
7581 if (level == DUMMY_OVERRIDE_ALL) {
7582 struct sset types;
7583 const char *type;
7584
7585 sset_init(&types);
7586 dp_enumerate_types(&types);
7587 SSET_FOR_EACH (type, &types) {
7588 dpif_dummy_override(type);
7589 }
7590 sset_destroy(&types);
7591 } else if (level == DUMMY_OVERRIDE_SYSTEM) {
7592 dpif_dummy_override("system");
7593 }
7594
7595 dpif_dummy_register__("dummy");
7596
7597 unixctl_command_register("dpif-dummy/change-port-number",
7598 "dp port new-number",
7599 3, 3, dpif_dummy_change_port_number, NULL);
7600 }
7601 \f
7602 /* Datapath Classifier. */
7603
7604 /* A set of rules that all have the same fields wildcarded. */
7605 struct dpcls_subtable {
7606 /* The fields are only used by writers. */
7607 struct cmap_node cmap_node OVS_GUARDED; /* Within dpcls 'subtables_map'. */
7608
7609 /* These fields are accessed by readers. */
7610 struct cmap rules; /* Contains "struct dpcls_rule"s. */
7611 uint32_t hit_cnt; /* Number of match hits in subtable in current
7612 optimization interval. */
7613 struct netdev_flow_key mask; /* Wildcards for fields (const). */
7614 /* 'mask' must be the last field, additional space is allocated here. */
7615 };
7616
7617 static void
7618 dpcls_subtable_destroy_cb(struct dpcls_subtable *subtable)
7619 {
7620 cmap_destroy(&subtable->rules);
7621 ovsrcu_postpone(free, subtable);
7622 }
7623
7624 /* Initializes 'cls' as a classifier that initially contains no classification
7625 * rules. */
7626 static void
7627 dpcls_init(struct dpcls *cls)
7628 {
7629 cmap_init(&cls->subtables_map);
7630 pvector_init(&cls->subtables);
7631 }
7632
7633 static void
7634 dpcls_destroy_subtable(struct dpcls *cls, struct dpcls_subtable *subtable)
7635 {
7636 VLOG_DBG("Destroying subtable %p for in_port %d", subtable, cls->in_port);
7637 pvector_remove(&cls->subtables, subtable);
7638 cmap_remove(&cls->subtables_map, &subtable->cmap_node,
7639 subtable->mask.hash);
7640 ovsrcu_postpone(dpcls_subtable_destroy_cb, subtable);
7641 }
7642
7643 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
7644 * caller's responsibility.
7645 * May only be called after all the readers have been terminated. */
7646 static void
7647 dpcls_destroy(struct dpcls *cls)
7648 {
7649 if (cls) {
7650 struct dpcls_subtable *subtable;
7651
7652 CMAP_FOR_EACH (subtable, cmap_node, &cls->subtables_map) {
7653 ovs_assert(cmap_count(&subtable->rules) == 0);
7654 dpcls_destroy_subtable(cls, subtable);
7655 }
7656 cmap_destroy(&cls->subtables_map);
7657 pvector_destroy(&cls->subtables);
7658 }
7659 }
7660
7661 static struct dpcls_subtable *
7662 dpcls_create_subtable(struct dpcls *cls, const struct netdev_flow_key *mask)
7663 {
7664 struct dpcls_subtable *subtable;
7665
7666 /* Need to add one. */
7667 subtable = xmalloc(sizeof *subtable
7668 - sizeof subtable->mask.mf + mask->len);
7669 cmap_init(&subtable->rules);
7670 subtable->hit_cnt = 0;
7671 netdev_flow_key_clone(&subtable->mask, mask);
7672 cmap_insert(&cls->subtables_map, &subtable->cmap_node, mask->hash);
7673 /* Add the new subtable at the end of the pvector (with no hits yet) */
7674 pvector_insert(&cls->subtables, subtable, 0);
7675 VLOG_DBG("Creating %"PRIuSIZE". subtable %p for in_port %d",
7676 cmap_count(&cls->subtables_map), subtable, cls->in_port);
7677 pvector_publish(&cls->subtables);
7678
7679 return subtable;
7680 }
7681
7682 static inline struct dpcls_subtable *
7683 dpcls_find_subtable(struct dpcls *cls, const struct netdev_flow_key *mask)
7684 {
7685 struct dpcls_subtable *subtable;
7686
7687 CMAP_FOR_EACH_WITH_HASH (subtable, cmap_node, mask->hash,
7688 &cls->subtables_map) {
7689 if (netdev_flow_key_equal(&subtable->mask, mask)) {
7690 return subtable;
7691 }
7692 }
7693 return dpcls_create_subtable(cls, mask);
7694 }
7695
7696
7697 /* Periodically sort the dpcls subtable vectors according to hit counts */
7698 static void
7699 dpcls_sort_subtable_vector(struct dpcls *cls)
7700 {
7701 struct pvector *pvec = &cls->subtables;
7702 struct dpcls_subtable *subtable;
7703
7704 PVECTOR_FOR_EACH (subtable, pvec) {
7705 pvector_change_priority(pvec, subtable, subtable->hit_cnt);
7706 subtable->hit_cnt = 0;
7707 }
7708 pvector_publish(pvec);
7709 }
7710
7711 static inline void
7712 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread *pmd,
7713 struct polled_queue *poll_list, int poll_cnt)
7714 {
7715 struct dpcls *cls;
7716 uint64_t tot_idle = 0, tot_proc = 0;
7717 unsigned int pmd_load = 0;
7718
7719 if (pmd->ctx.now > pmd->rxq_next_cycle_store) {
7720 uint64_t curr_tsc;
7721 struct pmd_auto_lb *pmd_alb = &pmd->dp->pmd_alb;
7722 if (pmd_alb->is_enabled && !pmd->isolated
7723 && (pmd->perf_stats.counters.n[PMD_CYCLES_ITER_IDLE] >=
7724 pmd->prev_stats[PMD_CYCLES_ITER_IDLE])
7725 && (pmd->perf_stats.counters.n[PMD_CYCLES_ITER_BUSY] >=
7726 pmd->prev_stats[PMD_CYCLES_ITER_BUSY]))
7727 {
7728 tot_idle = pmd->perf_stats.counters.n[PMD_CYCLES_ITER_IDLE] -
7729 pmd->prev_stats[PMD_CYCLES_ITER_IDLE];
7730 tot_proc = pmd->perf_stats.counters.n[PMD_CYCLES_ITER_BUSY] -
7731 pmd->prev_stats[PMD_CYCLES_ITER_BUSY];
7732
7733 if (tot_proc) {
7734 pmd_load = ((tot_proc * 100) / (tot_idle + tot_proc));
7735 }
7736
7737 if (pmd_load >= ALB_PMD_LOAD_THRESHOLD) {
7738 atomic_count_inc(&pmd->pmd_overloaded);
7739 } else {
7740 atomic_count_set(&pmd->pmd_overloaded, 0);
7741 }
7742 }
7743
7744 pmd->prev_stats[PMD_CYCLES_ITER_IDLE] =
7745 pmd->perf_stats.counters.n[PMD_CYCLES_ITER_IDLE];
7746 pmd->prev_stats[PMD_CYCLES_ITER_BUSY] =
7747 pmd->perf_stats.counters.n[PMD_CYCLES_ITER_BUSY];
7748
7749 /* Get the cycles that were used to process each queue and store. */
7750 for (unsigned i = 0; i < poll_cnt; i++) {
7751 uint64_t rxq_cyc_curr = dp_netdev_rxq_get_cycles(poll_list[i].rxq,
7752 RXQ_CYCLES_PROC_CURR);
7753 dp_netdev_rxq_set_intrvl_cycles(poll_list[i].rxq, rxq_cyc_curr);
7754 dp_netdev_rxq_set_cycles(poll_list[i].rxq, RXQ_CYCLES_PROC_CURR,
7755 0);
7756 }
7757 curr_tsc = cycles_counter_update(&pmd->perf_stats);
7758 if (pmd->intrvl_tsc_prev) {
7759 /* There is a prev timestamp, store a new intrvl cycle count. */
7760 atomic_store_relaxed(&pmd->intrvl_cycles,
7761 curr_tsc - pmd->intrvl_tsc_prev);
7762 }
7763 pmd->intrvl_tsc_prev = curr_tsc;
7764 /* Start new measuring interval */
7765 pmd->rxq_next_cycle_store = pmd->ctx.now + PMD_RXQ_INTERVAL_LEN;
7766 }
7767
7768 if (pmd->ctx.now > pmd->next_optimization) {
7769 /* Try to obtain the flow lock to block out revalidator threads.
7770 * If not possible, just try next time. */
7771 if (!ovs_mutex_trylock(&pmd->flow_mutex)) {
7772 /* Optimize each classifier */
7773 CMAP_FOR_EACH (cls, node, &pmd->classifiers) {
7774 dpcls_sort_subtable_vector(cls);
7775 }
7776 ovs_mutex_unlock(&pmd->flow_mutex);
7777 /* Start new measuring interval */
7778 pmd->next_optimization = pmd->ctx.now
7779 + DPCLS_OPTIMIZATION_INTERVAL;
7780 }
7781 }
7782 }
7783
7784 /* Insert 'rule' into 'cls'. */
7785 static void
7786 dpcls_insert(struct dpcls *cls, struct dpcls_rule *rule,
7787 const struct netdev_flow_key *mask)
7788 {
7789 struct dpcls_subtable *subtable = dpcls_find_subtable(cls, mask);
7790
7791 /* Refer to subtable's mask, also for later removal. */
7792 rule->mask = &subtable->mask;
7793 cmap_insert(&subtable->rules, &rule->cmap_node, rule->flow.hash);
7794 }
7795
7796 /* Removes 'rule' from 'cls', also destructing the 'rule'. */
7797 static void
7798 dpcls_remove(struct dpcls *cls, struct dpcls_rule *rule)
7799 {
7800 struct dpcls_subtable *subtable;
7801
7802 ovs_assert(rule->mask);
7803
7804 /* Get subtable from reference in rule->mask. */
7805 INIT_CONTAINER(subtable, rule->mask, mask);
7806 if (cmap_remove(&subtable->rules, &rule->cmap_node, rule->flow.hash)
7807 == 0) {
7808 /* Delete empty subtable. */
7809 dpcls_destroy_subtable(cls, subtable);
7810 pvector_publish(&cls->subtables);
7811 }
7812 }
7813
7814 /* Returns true if 'target' satisfies 'key' in 'mask', that is, if each 1-bit
7815 * in 'mask' the values in 'key' and 'target' are the same. */
7816 static bool
7817 dpcls_rule_matches_key(const struct dpcls_rule *rule,
7818 const struct netdev_flow_key *target)
7819 {
7820 const uint64_t *keyp = miniflow_get_values(&rule->flow.mf);
7821 const uint64_t *maskp = miniflow_get_values(&rule->mask->mf);
7822 uint64_t value;
7823
7824 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value, target, rule->flow.mf.map) {
7825 if (OVS_UNLIKELY((value & *maskp++) != *keyp++)) {
7826 return false;
7827 }
7828 }
7829 return true;
7830 }
7831
7832 /* For each miniflow in 'keys' performs a classifier lookup writing the result
7833 * into the corresponding slot in 'rules'. If a particular entry in 'keys' is
7834 * NULL it is skipped.
7835 *
7836 * This function is optimized for use in the userspace datapath and therefore
7837 * does not implement a lot of features available in the standard
7838 * classifier_lookup() function. Specifically, it does not implement
7839 * priorities, instead returning any rule which matches the flow.
7840 *
7841 * Returns true if all miniflows found a corresponding rule. */
7842 static bool
7843 dpcls_lookup(struct dpcls *cls, const struct netdev_flow_key *keys[],
7844 struct dpcls_rule **rules, const size_t cnt,
7845 int *num_lookups_p)
7846 {
7847 /* The received 'cnt' miniflows are the search-keys that will be processed
7848 * to find a matching entry into the available subtables.
7849 * The number of bits in map_type is equal to NETDEV_MAX_BURST. */
7850 typedef uint32_t map_type;
7851 #define MAP_BITS (sizeof(map_type) * CHAR_BIT)
7852 BUILD_ASSERT_DECL(MAP_BITS >= NETDEV_MAX_BURST);
7853
7854 struct dpcls_subtable *subtable;
7855
7856 map_type keys_map = TYPE_MAXIMUM(map_type); /* Set all bits. */
7857 map_type found_map;
7858 uint32_t hashes[MAP_BITS];
7859 const struct cmap_node *nodes[MAP_BITS];
7860
7861 if (cnt != MAP_BITS) {
7862 keys_map >>= MAP_BITS - cnt; /* Clear extra bits. */
7863 }
7864 memset(rules, 0, cnt * sizeof *rules);
7865
7866 int lookups_match = 0, subtable_pos = 1;
7867
7868 /* The Datapath classifier - aka dpcls - is composed of subtables.
7869 * Subtables are dynamically created as needed when new rules are inserted.
7870 * Each subtable collects rules with matches on a specific subset of packet
7871 * fields as defined by the subtable's mask. We proceed to process every
7872 * search-key against each subtable, but when a match is found for a
7873 * search-key, the search for that key can stop because the rules are
7874 * non-overlapping. */
7875 PVECTOR_FOR_EACH (subtable, &cls->subtables) {
7876 int i;
7877
7878 /* Compute hashes for the remaining keys. Each search-key is
7879 * masked with the subtable's mask to avoid hashing the wildcarded
7880 * bits. */
7881 ULLONG_FOR_EACH_1(i, keys_map) {
7882 hashes[i] = netdev_flow_key_hash_in_mask(keys[i],
7883 &subtable->mask);
7884 }
7885 /* Lookup. */
7886 found_map = cmap_find_batch(&subtable->rules, keys_map, hashes, nodes);
7887 /* Check results. When the i-th bit of found_map is set, it means
7888 * that a set of nodes with a matching hash value was found for the
7889 * i-th search-key. Due to possible hash collisions we need to check
7890 * which of the found rules, if any, really matches our masked
7891 * search-key. */
7892 ULLONG_FOR_EACH_1(i, found_map) {
7893 struct dpcls_rule *rule;
7894
7895 CMAP_NODE_FOR_EACH (rule, cmap_node, nodes[i]) {
7896 if (OVS_LIKELY(dpcls_rule_matches_key(rule, keys[i]))) {
7897 rules[i] = rule;
7898 /* Even at 20 Mpps the 32-bit hit_cnt cannot wrap
7899 * within one second optimization interval. */
7900 subtable->hit_cnt++;
7901 lookups_match += subtable_pos;
7902 goto next;
7903 }
7904 }
7905 /* None of the found rules was a match. Reset the i-th bit to
7906 * keep searching this key in the next subtable. */
7907 ULLONG_SET0(found_map, i); /* Did not match. */
7908 next:
7909 ; /* Keep Sparse happy. */
7910 }
7911 keys_map &= ~found_map; /* Clear the found rules. */
7912 if (!keys_map) {
7913 if (num_lookups_p) {
7914 *num_lookups_p = lookups_match;
7915 }
7916 return true; /* All found. */
7917 }
7918 subtable_pos++;
7919 }
7920 if (num_lookups_p) {
7921 *num_lookups_p = lookups_match;
7922 }
7923 return false; /* Some misses. */
7924 }