]> git.proxmox.com Git - mirror_ovs.git/blob - lib/dpif-netdev.c
userspace: Add packet_type in dp_packet and flow
[mirror_ovs.git] / lib / dpif-netdev.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2016 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 <netinet/in.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33
34 #ifdef DPDK_NETDEV
35 #include <rte_cycles.h>
36 #endif
37
38 #include "bitmap.h"
39 #include "cmap.h"
40 #include "conntrack.h"
41 #include "coverage.h"
42 #include "ct-dpif.h"
43 #include "csum.h"
44 #include "dp-packet.h"
45 #include "dpif.h"
46 #include "dpif-provider.h"
47 #include "dummy.h"
48 #include "fat-rwlock.h"
49 #include "flow.h"
50 #include "hmapx.h"
51 #include "latch.h"
52 #include "netdev.h"
53 #include "netdev-vport.h"
54 #include "netlink.h"
55 #include "odp-execute.h"
56 #include "odp-util.h"
57 #include "openvswitch/dynamic-string.h"
58 #include "openvswitch/list.h"
59 #include "openvswitch/match.h"
60 #include "openvswitch/ofp-print.h"
61 #include "openvswitch/ofp-util.h"
62 #include "openvswitch/ofpbuf.h"
63 #include "openvswitch/shash.h"
64 #include "openvswitch/vlog.h"
65 #include "ovs-numa.h"
66 #include "ovs-rcu.h"
67 #include "packets.h"
68 #include "poll-loop.h"
69 #include "pvector.h"
70 #include "random.h"
71 #include "seq.h"
72 #include "smap.h"
73 #include "sset.h"
74 #include "timeval.h"
75 #include "tnl-neigh-cache.h"
76 #include "tnl-ports.h"
77 #include "unixctl.h"
78 #include "util.h"
79
80 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
81
82 #define FLOW_DUMP_MAX_BATCH 50
83 /* Use per thread recirc_depth to prevent recirculation loop. */
84 #define MAX_RECIRC_DEPTH 5
85 DEFINE_STATIC_PER_THREAD_DATA(uint32_t, recirc_depth, 0)
86
87 /* Configuration parameters. */
88 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
89 enum { MAX_METERS = 65536 }; /* Maximum number of meters. */
90 enum { MAX_BANDS = 8 }; /* Maximum number of bands / meter. */
91 enum { N_METER_LOCKS = 64 }; /* Maximum number of meters. */
92
93 /* Protects against changes to 'dp_netdevs'. */
94 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
95
96 /* Contains all 'struct dp_netdev's. */
97 static struct shash dp_netdevs OVS_GUARDED_BY(dp_netdev_mutex)
98 = SHASH_INITIALIZER(&dp_netdevs);
99
100 static struct vlog_rate_limit upcall_rl = VLOG_RATE_LIMIT_INIT(600, 600);
101
102 #define DP_NETDEV_CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \
103 | CS_INVALID | CS_REPLY_DIR | CS_TRACKED)
104 #define DP_NETDEV_CS_UNSUPPORTED_MASK (~(uint32_t)DP_NETDEV_CS_SUPPORTED_MASK)
105
106 static struct odp_support dp_netdev_support = {
107 .max_vlan_headers = SIZE_MAX,
108 .max_mpls_depth = SIZE_MAX,
109 .recirc = true,
110 .ct_state = true,
111 .ct_zone = true,
112 .ct_mark = true,
113 .ct_label = true,
114 };
115
116 /* Stores a miniflow with inline values */
117
118 struct netdev_flow_key {
119 uint32_t hash; /* Hash function differs for different users. */
120 uint32_t len; /* Length of the following miniflow (incl. map). */
121 struct miniflow mf;
122 uint64_t buf[FLOW_MAX_PACKET_U64S];
123 };
124
125 /* Exact match cache for frequently used flows
126 *
127 * The cache uses a 32-bit hash of the packet (which can be the RSS hash) to
128 * search its entries for a miniflow that matches exactly the miniflow of the
129 * packet. It stores the 'dpcls_rule' (rule) that matches the miniflow.
130 *
131 * A cache entry holds a reference to its 'dp_netdev_flow'.
132 *
133 * A miniflow with a given hash can be in one of EM_FLOW_HASH_SEGS different
134 * entries. The 32-bit hash is split into EM_FLOW_HASH_SEGS values (each of
135 * them is EM_FLOW_HASH_SHIFT bits wide and the remainder is thrown away). Each
136 * value is the index of a cache entry where the miniflow could be.
137 *
138 *
139 * Thread-safety
140 * =============
141 *
142 * Each pmd_thread has its own private exact match cache.
143 * If dp_netdev_input is not called from a pmd thread, a mutex is used.
144 */
145
146 #define EM_FLOW_HASH_SHIFT 13
147 #define EM_FLOW_HASH_ENTRIES (1u << EM_FLOW_HASH_SHIFT)
148 #define EM_FLOW_HASH_MASK (EM_FLOW_HASH_ENTRIES - 1)
149 #define EM_FLOW_HASH_SEGS 2
150
151 /* Default EMC insert probability is 1 / DEFAULT_EM_FLOW_INSERT_INV_PROB */
152 #define DEFAULT_EM_FLOW_INSERT_INV_PROB 100
153 #define DEFAULT_EM_FLOW_INSERT_MIN (UINT32_MAX / \
154 DEFAULT_EM_FLOW_INSERT_INV_PROB)
155
156 struct emc_entry {
157 struct dp_netdev_flow *flow;
158 struct netdev_flow_key key; /* key.hash used for emc hash value. */
159 };
160
161 struct emc_cache {
162 struct emc_entry entries[EM_FLOW_HASH_ENTRIES];
163 int sweep_idx; /* For emc_cache_slow_sweep(). */
164 };
165
166 /* Iterate in the exact match cache through every entry that might contain a
167 * miniflow with hash 'HASH'. */
168 #define EMC_FOR_EACH_POS_WITH_HASH(EMC, CURRENT_ENTRY, HASH) \
169 for (uint32_t i__ = 0, srch_hash__ = (HASH); \
170 (CURRENT_ENTRY) = &(EMC)->entries[srch_hash__ & EM_FLOW_HASH_MASK], \
171 i__ < EM_FLOW_HASH_SEGS; \
172 i__++, srch_hash__ >>= EM_FLOW_HASH_SHIFT)
173 \f
174 /* Simple non-wildcarding single-priority classifier. */
175
176 /* Time in ms between successive optimizations of the dpcls subtable vector */
177 #define DPCLS_OPTIMIZATION_INTERVAL 1000
178
179 struct dpcls {
180 struct cmap_node node; /* Within dp_netdev_pmd_thread.classifiers */
181 odp_port_t in_port;
182 struct cmap subtables_map;
183 struct pvector subtables;
184 };
185
186 /* A rule to be inserted to the classifier. */
187 struct dpcls_rule {
188 struct cmap_node cmap_node; /* Within struct dpcls_subtable 'rules'. */
189 struct netdev_flow_key *mask; /* Subtable's mask. */
190 struct netdev_flow_key flow; /* Matching key. */
191 /* 'flow' must be the last field, additional space is allocated here. */
192 };
193
194 static void dpcls_init(struct dpcls *);
195 static void dpcls_destroy(struct dpcls *);
196 static void dpcls_sort_subtable_vector(struct dpcls *);
197 static void dpcls_insert(struct dpcls *, struct dpcls_rule *,
198 const struct netdev_flow_key *mask);
199 static void dpcls_remove(struct dpcls *, struct dpcls_rule *);
200 static bool dpcls_lookup(struct dpcls *cls,
201 const struct netdev_flow_key keys[],
202 struct dpcls_rule **rules, size_t cnt,
203 int *num_lookups_p);
204 \f
205 /* Set of supported meter flags */
206 #define DP_SUPPORTED_METER_FLAGS_MASK \
207 (OFPMF13_STATS | OFPMF13_PKTPS | OFPMF13_KBPS | OFPMF13_BURST)
208
209 /* Set of supported meter band types */
210 #define DP_SUPPORTED_METER_BAND_TYPES \
211 ( 1 << OFPMBT13_DROP )
212
213 struct dp_meter_band {
214 struct ofputil_meter_band up; /* type, prec_level, pad, rate, burst_size */
215 uint32_t bucket; /* In 1/1000 packets (for PKTPS), or in bits (for KBPS) */
216 uint64_t packet_count;
217 uint64_t byte_count;
218 };
219
220 struct dp_meter {
221 uint16_t flags;
222 uint16_t n_bands;
223 uint32_t max_delta_t;
224 uint64_t used;
225 uint64_t packet_count;
226 uint64_t byte_count;
227 struct dp_meter_band bands[];
228 };
229
230 /* Datapath based on the network device interface from netdev.h.
231 *
232 *
233 * Thread-safety
234 * =============
235 *
236 * Some members, marked 'const', are immutable. Accessing other members
237 * requires synchronization, as noted in more detail below.
238 *
239 * Acquisition order is, from outermost to innermost:
240 *
241 * dp_netdev_mutex (global)
242 * port_mutex
243 * non_pmd_mutex
244 */
245 struct dp_netdev {
246 const struct dpif_class *const class;
247 const char *const name;
248 struct dpif *dpif;
249 struct ovs_refcount ref_cnt;
250 atomic_flag destroyed;
251
252 /* Ports.
253 *
254 * Any lookup into 'ports' or any access to the dp_netdev_ports found
255 * through 'ports' requires taking 'port_mutex'. */
256 struct ovs_mutex port_mutex;
257 struct hmap ports;
258 struct seq *port_seq; /* Incremented whenever a port changes. */
259
260 /* Meters. */
261 struct ovs_mutex meter_locks[N_METER_LOCKS];
262 struct dp_meter *meters[MAX_METERS]; /* Meter bands. */
263
264 /* Protects access to ofproto-dpif-upcall interface during revalidator
265 * thread synchronization. */
266 struct fat_rwlock upcall_rwlock;
267 upcall_callback *upcall_cb; /* Callback function for executing upcalls. */
268 void *upcall_aux;
269
270 /* Callback function for notifying the purging of dp flows (during
271 * reseting pmd deletion). */
272 dp_purge_callback *dp_purge_cb;
273 void *dp_purge_aux;
274
275 /* Stores all 'struct dp_netdev_pmd_thread's. */
276 struct cmap poll_threads;
277
278 /* Protects the access of the 'struct dp_netdev_pmd_thread'
279 * instance for non-pmd thread. */
280 struct ovs_mutex non_pmd_mutex;
281
282 /* Each pmd thread will store its pointer to
283 * 'struct dp_netdev_pmd_thread' in 'per_pmd_key'. */
284 ovsthread_key_t per_pmd_key;
285
286 struct seq *reconfigure_seq;
287 uint64_t last_reconfigure_seq;
288
289 /* Cpu mask for pin of pmd threads. */
290 char *pmd_cmask;
291
292 uint64_t last_tnl_conf_seq;
293
294 struct conntrack conntrack;
295
296 /* Probability of EMC insertions is a factor of 'emc_insert_min'.*/
297 OVS_ALIGNED_VAR(CACHE_LINE_SIZE) atomic_uint32_t emc_insert_min;
298 };
299
300 static void meter_lock(const struct dp_netdev *dp, uint32_t meter_id)
301 OVS_ACQUIRES(dp->meter_locks[meter_id % N_METER_LOCKS])
302 {
303 ovs_mutex_lock(&dp->meter_locks[meter_id % N_METER_LOCKS]);
304 }
305
306 static void meter_unlock(const struct dp_netdev *dp, uint32_t meter_id)
307 OVS_RELEASES(dp->meter_locks[meter_id % N_METER_LOCKS])
308 {
309 ovs_mutex_unlock(&dp->meter_locks[meter_id % N_METER_LOCKS]);
310 }
311
312
313 static struct dp_netdev_port *dp_netdev_lookup_port(const struct dp_netdev *dp,
314 odp_port_t)
315 OVS_REQUIRES(dp->port_mutex);
316
317 enum dp_stat_type {
318 DP_STAT_EXACT_HIT, /* Packets that had an exact match (emc). */
319 DP_STAT_MASKED_HIT, /* Packets that matched in the flow table. */
320 DP_STAT_MISS, /* Packets that did not match. */
321 DP_STAT_LOST, /* Packets not passed up to the client. */
322 DP_STAT_LOOKUP_HIT, /* Number of subtable lookups for flow table
323 hits */
324 DP_N_STATS
325 };
326
327 enum pmd_cycles_counter_type {
328 PMD_CYCLES_POLLING, /* Cycles spent polling NICs. */
329 PMD_CYCLES_PROCESSING, /* Cycles spent processing packets */
330 PMD_N_CYCLES
331 };
332
333 #define XPS_TIMEOUT_MS 500LL
334
335 /* Contained by struct dp_netdev_port's 'rxqs' member. */
336 struct dp_netdev_rxq {
337 struct dp_netdev_port *port;
338 struct netdev_rxq *rx;
339 unsigned core_id; /* Core to which this queue should be
340 pinned. OVS_CORE_UNSPEC if the
341 queue doesn't need to be pinned to a
342 particular core. */
343 struct dp_netdev_pmd_thread *pmd; /* pmd thread that will poll this queue. */
344 };
345
346 /* A port in a netdev-based datapath. */
347 struct dp_netdev_port {
348 odp_port_t port_no;
349 struct netdev *netdev;
350 struct hmap_node node; /* Node in dp_netdev's 'ports'. */
351 struct netdev_saved_flags *sf;
352 struct dp_netdev_rxq *rxqs;
353 unsigned n_rxq; /* Number of elements in 'rxq' */
354 bool dynamic_txqs; /* If true XPS will be used. */
355 unsigned *txq_used; /* Number of threads that uses each tx queue. */
356 struct ovs_mutex txq_used_mutex;
357 char *type; /* Port type as requested by user. */
358 char *rxq_affinity_list; /* Requested affinity of rx queues. */
359 bool need_reconfigure; /* True if we should reconfigure netdev. */
360 };
361
362 /* Contained by struct dp_netdev_flow's 'stats' member. */
363 struct dp_netdev_flow_stats {
364 atomic_llong used; /* Last used time, in monotonic msecs. */
365 atomic_ullong packet_count; /* Number of packets matched. */
366 atomic_ullong byte_count; /* Number of bytes matched. */
367 atomic_uint16_t tcp_flags; /* Bitwise-OR of seen tcp_flags values. */
368 };
369
370 /* A flow in 'dp_netdev_pmd_thread's 'flow_table'.
371 *
372 *
373 * Thread-safety
374 * =============
375 *
376 * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
377 * its pmd thread's classifier. The text below calls this classifier 'cls'.
378 *
379 * Motivation
380 * ----------
381 *
382 * The thread safety rules described here for "struct dp_netdev_flow" are
383 * motivated by two goals:
384 *
385 * - Prevent threads that read members of "struct dp_netdev_flow" from
386 * reading bad data due to changes by some thread concurrently modifying
387 * those members.
388 *
389 * - Prevent two threads making changes to members of a given "struct
390 * dp_netdev_flow" from interfering with each other.
391 *
392 *
393 * Rules
394 * -----
395 *
396 * A flow 'flow' may be accessed without a risk of being freed during an RCU
397 * grace period. Code that needs to hold onto a flow for a while
398 * should try incrementing 'flow->ref_cnt' with dp_netdev_flow_ref().
399 *
400 * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the
401 * flow from being deleted from 'cls' and it doesn't protect members of 'flow'
402 * from modification.
403 *
404 * Some members, marked 'const', are immutable. Accessing other members
405 * requires synchronization, as noted in more detail below.
406 */
407 struct dp_netdev_flow {
408 const struct flow flow; /* Unmasked flow that created this entry. */
409 /* Hash table index by unmasked flow. */
410 const struct cmap_node node; /* In owning dp_netdev_pmd_thread's */
411 /* 'flow_table'. */
412 const ovs_u128 ufid; /* Unique flow identifier. */
413 const unsigned pmd_id; /* The 'core_id' of pmd thread owning this */
414 /* flow. */
415
416 /* Number of references.
417 * The classifier owns one reference.
418 * Any thread trying to keep a rule from being freed should hold its own
419 * reference. */
420 struct ovs_refcount ref_cnt;
421
422 bool dead;
423
424 /* Statistics. */
425 struct dp_netdev_flow_stats stats;
426
427 /* Actions. */
428 OVSRCU_TYPE(struct dp_netdev_actions *) actions;
429
430 /* While processing a group of input packets, the datapath uses the next
431 * member to store a pointer to the output batch for the flow. It is
432 * reset after the batch has been sent out (See dp_netdev_queue_batches(),
433 * packet_batch_per_flow_init() and packet_batch_per_flow_execute()). */
434 struct packet_batch_per_flow *batch;
435
436 /* Packet classification. */
437 struct dpcls_rule cr; /* In owning dp_netdev's 'cls'. */
438 /* 'cr' must be the last member. */
439 };
440
441 static void dp_netdev_flow_unref(struct dp_netdev_flow *);
442 static bool dp_netdev_flow_ref(struct dp_netdev_flow *);
443 static int dpif_netdev_flow_from_nlattrs(const struct nlattr *, uint32_t,
444 struct flow *, bool);
445
446 /* A set of datapath actions within a "struct dp_netdev_flow".
447 *
448 *
449 * Thread-safety
450 * =============
451 *
452 * A struct dp_netdev_actions 'actions' is protected with RCU. */
453 struct dp_netdev_actions {
454 /* These members are immutable: they do not change during the struct's
455 * lifetime. */
456 unsigned int size; /* Size of 'actions', in bytes. */
457 struct nlattr actions[]; /* Sequence of OVS_ACTION_ATTR_* attributes. */
458 };
459
460 struct dp_netdev_actions *dp_netdev_actions_create(const struct nlattr *,
461 size_t);
462 struct dp_netdev_actions *dp_netdev_flow_get_actions(
463 const struct dp_netdev_flow *);
464 static void dp_netdev_actions_free(struct dp_netdev_actions *);
465
466 /* Contained by struct dp_netdev_pmd_thread's 'stats' member. */
467 struct dp_netdev_pmd_stats {
468 /* Indexed by DP_STAT_*. */
469 atomic_ullong n[DP_N_STATS];
470 };
471
472 /* Contained by struct dp_netdev_pmd_thread's 'cycle' member. */
473 struct dp_netdev_pmd_cycles {
474 /* Indexed by PMD_CYCLES_*. */
475 atomic_ullong n[PMD_N_CYCLES];
476 };
477
478 struct polled_queue {
479 struct netdev_rxq *rx;
480 odp_port_t port_no;
481 };
482
483 /* Contained by struct dp_netdev_pmd_thread's 'poll_list' member. */
484 struct rxq_poll {
485 struct dp_netdev_rxq *rxq;
486 struct hmap_node node;
487 };
488
489 /* Contained by struct dp_netdev_pmd_thread's 'send_port_cache',
490 * 'tnl_port_cache' or 'tx_ports'. */
491 struct tx_port {
492 struct dp_netdev_port *port;
493 int qid;
494 long long last_used;
495 struct hmap_node node;
496 };
497
498 /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate
499 * the performance overhead of interrupt processing. Therefore netdev can
500 * not implement rx-wait for these devices. dpif-netdev needs to poll
501 * these device to check for recv buffer. pmd-thread does polling for
502 * devices assigned to itself.
503 *
504 * DPDK used PMD for accessing NIC.
505 *
506 * Note, instance with cpu core id NON_PMD_CORE_ID will be reserved for
507 * I/O of all non-pmd threads. There will be no actual thread created
508 * for the instance.
509 *
510 * Each struct has its own flow table and classifier. Packets received
511 * from managed ports are looked up in the corresponding pmd thread's
512 * flow table, and are executed with the found actions.
513 * */
514 struct dp_netdev_pmd_thread {
515 struct dp_netdev *dp;
516 struct ovs_refcount ref_cnt; /* Every reference must be refcount'ed. */
517 struct cmap_node node; /* In 'dp->poll_threads'. */
518
519 pthread_cond_t cond; /* For synchronizing pmd thread reload. */
520 struct ovs_mutex cond_mutex; /* Mutex for condition variable. */
521
522 /* Per thread exact-match cache. Note, the instance for cpu core
523 * NON_PMD_CORE_ID can be accessed by multiple threads, and thusly
524 * need to be protected by 'non_pmd_mutex'. Every other instance
525 * will only be accessed by its own pmd thread. */
526 struct emc_cache flow_cache;
527
528 /* Flow-Table and classifiers
529 *
530 * Writers of 'flow_table' must take the 'flow_mutex'. Corresponding
531 * changes to 'classifiers' must be made while still holding the
532 * 'flow_mutex'.
533 */
534 struct ovs_mutex flow_mutex;
535 struct cmap flow_table OVS_GUARDED; /* Flow table. */
536
537 /* One classifier per in_port polled by the pmd */
538 struct cmap classifiers;
539 /* Periodically sort subtable vectors according to hit frequencies */
540 long long int next_optimization;
541
542 /* Statistics. */
543 struct dp_netdev_pmd_stats stats;
544
545 /* Cycles counters */
546 struct dp_netdev_pmd_cycles cycles;
547
548 /* Used to count cicles. See 'cycles_counter_end()' */
549 unsigned long long last_cycles;
550
551 struct latch exit_latch; /* For terminating the pmd thread. */
552 struct seq *reload_seq;
553 uint64_t last_reload_seq;
554 atomic_bool reload; /* Do we need to reload ports? */
555 pthread_t thread;
556 unsigned core_id; /* CPU core id of this pmd thread. */
557 int numa_id; /* numa node id of this pmd thread. */
558 bool isolated;
559
560 /* Queue id used by this pmd thread to send packets on all netdevs if
561 * XPS disabled for this netdev. All static_tx_qid's are unique and less
562 * than 'cmap_count(dp->poll_threads)'. */
563 const int static_tx_qid;
564
565 struct ovs_mutex port_mutex; /* Mutex for 'poll_list' and 'tx_ports'. */
566 /* List of rx queues to poll. */
567 struct hmap poll_list OVS_GUARDED;
568 /* Map of 'tx_port's used for transmission. Written by the main thread,
569 * read by the pmd thread. */
570 struct hmap tx_ports OVS_GUARDED;
571
572 /* These are thread-local copies of 'tx_ports'. One contains only tunnel
573 * ports (that support push_tunnel/pop_tunnel), the other contains ports
574 * with at least one txq (that support send). A port can be in both.
575 *
576 * There are two separate maps to make sure that we don't try to execute
577 * OUTPUT on a device which has 0 txqs or PUSH/POP on a non-tunnel device.
578 *
579 * The instances for cpu core NON_PMD_CORE_ID can be accessed by multiple
580 * threads, and thusly need to be protected by 'non_pmd_mutex'. Every
581 * other instance will only be accessed by its own pmd thread. */
582 struct hmap tnl_port_cache;
583 struct hmap send_port_cache;
584
585 /* Only a pmd thread can write on its own 'cycles' and 'stats'.
586 * The main thread keeps 'stats_zero' and 'cycles_zero' as base
587 * values and subtracts them from 'stats' and 'cycles' before
588 * reporting to the user */
589 unsigned long long stats_zero[DP_N_STATS];
590 uint64_t cycles_zero[PMD_N_CYCLES];
591
592 /* Set to true if the pmd thread needs to be reloaded. */
593 bool need_reload;
594 };
595
596 /* Interface to netdev-based datapath. */
597 struct dpif_netdev {
598 struct dpif dpif;
599 struct dp_netdev *dp;
600 uint64_t last_port_seq;
601 };
602
603 static int get_port_by_number(struct dp_netdev *dp, odp_port_t port_no,
604 struct dp_netdev_port **portp)
605 OVS_REQUIRES(dp->port_mutex);
606 static int get_port_by_name(struct dp_netdev *dp, const char *devname,
607 struct dp_netdev_port **portp)
608 OVS_REQUIRES(dp->port_mutex);
609 static void dp_netdev_free(struct dp_netdev *)
610 OVS_REQUIRES(dp_netdev_mutex);
611 static int do_add_port(struct dp_netdev *dp, const char *devname,
612 const char *type, odp_port_t port_no)
613 OVS_REQUIRES(dp->port_mutex);
614 static void do_del_port(struct dp_netdev *dp, struct dp_netdev_port *)
615 OVS_REQUIRES(dp->port_mutex);
616 static int dpif_netdev_open(const struct dpif_class *, const char *name,
617 bool create, struct dpif **);
618 static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread *pmd,
619 struct dp_packet_batch *,
620 bool may_steal, const struct flow *flow,
621 const struct nlattr *actions,
622 size_t actions_len,
623 long long now);
624 static void dp_netdev_input(struct dp_netdev_pmd_thread *,
625 struct dp_packet_batch *, odp_port_t port_no);
626 static void dp_netdev_recirculate(struct dp_netdev_pmd_thread *,
627 struct dp_packet_batch *);
628
629 static void dp_netdev_disable_upcall(struct dp_netdev *);
630 static void dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread *pmd);
631 static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread *pmd,
632 struct dp_netdev *dp, unsigned core_id,
633 int numa_id);
634 static void dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread *pmd);
635 static void dp_netdev_set_nonpmd(struct dp_netdev *dp)
636 OVS_REQUIRES(dp->port_mutex);
637
638 static void *pmd_thread_main(void *);
639 static struct dp_netdev_pmd_thread *dp_netdev_get_pmd(struct dp_netdev *dp,
640 unsigned core_id);
641 static struct dp_netdev_pmd_thread *
642 dp_netdev_pmd_get_next(struct dp_netdev *dp, struct cmap_position *pos);
643 static void dp_netdev_destroy_all_pmds(struct dp_netdev *dp, bool non_pmd);
644 static void dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread *pmd);
645 static void dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread *pmd,
646 struct dp_netdev_port *port)
647 OVS_REQUIRES(pmd->port_mutex);
648 static void dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread *pmd,
649 struct tx_port *tx)
650 OVS_REQUIRES(pmd->port_mutex);
651 static void dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread *pmd,
652 struct dp_netdev_rxq *rxq)
653 OVS_REQUIRES(pmd->port_mutex);
654 static void dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread *pmd,
655 struct rxq_poll *poll)
656 OVS_REQUIRES(pmd->port_mutex);
657 static void reconfigure_datapath(struct dp_netdev *dp)
658 OVS_REQUIRES(dp->port_mutex);
659 static bool dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread *pmd);
660 static void dp_netdev_pmd_unref(struct dp_netdev_pmd_thread *pmd);
661 static void dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread *pmd);
662 static void pmd_load_cached_ports(struct dp_netdev_pmd_thread *pmd)
663 OVS_REQUIRES(pmd->port_mutex);
664 static inline void
665 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread *pmd);
666
667 static void
668 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread *pmd,
669 long long now, bool purge);
670 static int dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread *pmd,
671 struct tx_port *tx, long long now);
672
673 static inline bool emc_entry_alive(struct emc_entry *ce);
674 static void emc_clear_entry(struct emc_entry *ce);
675
676 static void
677 emc_cache_init(struct emc_cache *flow_cache)
678 {
679 int i;
680
681 flow_cache->sweep_idx = 0;
682 for (i = 0; i < ARRAY_SIZE(flow_cache->entries); i++) {
683 flow_cache->entries[i].flow = NULL;
684 flow_cache->entries[i].key.hash = 0;
685 flow_cache->entries[i].key.len = sizeof(struct miniflow);
686 flowmap_init(&flow_cache->entries[i].key.mf.map);
687 }
688 }
689
690 static void
691 emc_cache_uninit(struct emc_cache *flow_cache)
692 {
693 int i;
694
695 for (i = 0; i < ARRAY_SIZE(flow_cache->entries); i++) {
696 emc_clear_entry(&flow_cache->entries[i]);
697 }
698 }
699
700 /* Check and clear dead flow references slowly (one entry at each
701 * invocation). */
702 static void
703 emc_cache_slow_sweep(struct emc_cache *flow_cache)
704 {
705 struct emc_entry *entry = &flow_cache->entries[flow_cache->sweep_idx];
706
707 if (!emc_entry_alive(entry)) {
708 emc_clear_entry(entry);
709 }
710 flow_cache->sweep_idx = (flow_cache->sweep_idx + 1) & EM_FLOW_HASH_MASK;
711 }
712
713 /* Returns true if 'dpif' is a netdev or dummy dpif, false otherwise. */
714 bool
715 dpif_is_netdev(const struct dpif *dpif)
716 {
717 return dpif->dpif_class->open == dpif_netdev_open;
718 }
719
720 static struct dpif_netdev *
721 dpif_netdev_cast(const struct dpif *dpif)
722 {
723 ovs_assert(dpif_is_netdev(dpif));
724 return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
725 }
726
727 static struct dp_netdev *
728 get_dp_netdev(const struct dpif *dpif)
729 {
730 return dpif_netdev_cast(dpif)->dp;
731 }
732 \f
733 enum pmd_info_type {
734 PMD_INFO_SHOW_STATS, /* Show how cpu cycles are spent. */
735 PMD_INFO_CLEAR_STATS, /* Set the cycles count to 0. */
736 PMD_INFO_SHOW_RXQ /* Show poll-lists of pmd threads. */
737 };
738
739 static void
740 pmd_info_show_stats(struct ds *reply,
741 struct dp_netdev_pmd_thread *pmd,
742 unsigned long long stats[DP_N_STATS],
743 uint64_t cycles[PMD_N_CYCLES])
744 {
745 unsigned long long total_packets = 0;
746 uint64_t total_cycles = 0;
747 int i;
748
749 /* These loops subtracts reference values ('*_zero') from the counters.
750 * Since loads and stores are relaxed, it might be possible for a '*_zero'
751 * value to be more recent than the current value we're reading from the
752 * counter. This is not a big problem, since these numbers are not
753 * supposed to be too accurate, but we should at least make sure that
754 * the result is not negative. */
755 for (i = 0; i < DP_N_STATS; i++) {
756 if (stats[i] > pmd->stats_zero[i]) {
757 stats[i] -= pmd->stats_zero[i];
758 } else {
759 stats[i] = 0;
760 }
761
762 if (i != DP_STAT_LOST) {
763 /* Lost packets are already included in DP_STAT_MISS */
764 total_packets += stats[i];
765 }
766 }
767
768 for (i = 0; i < PMD_N_CYCLES; i++) {
769 if (cycles[i] > pmd->cycles_zero[i]) {
770 cycles[i] -= pmd->cycles_zero[i];
771 } else {
772 cycles[i] = 0;
773 }
774
775 total_cycles += cycles[i];
776 }
777
778 ds_put_cstr(reply, (pmd->core_id == NON_PMD_CORE_ID)
779 ? "main thread" : "pmd thread");
780
781 if (pmd->numa_id != OVS_NUMA_UNSPEC) {
782 ds_put_format(reply, " numa_id %d", pmd->numa_id);
783 }
784 if (pmd->core_id != OVS_CORE_UNSPEC && pmd->core_id != NON_PMD_CORE_ID) {
785 ds_put_format(reply, " core_id %u", pmd->core_id);
786 }
787 ds_put_cstr(reply, ":\n");
788
789 ds_put_format(reply,
790 "\temc hits:%llu\n\tmegaflow hits:%llu\n"
791 "\tavg. subtable lookups per hit:%.2f\n"
792 "\tmiss:%llu\n\tlost:%llu\n",
793 stats[DP_STAT_EXACT_HIT], stats[DP_STAT_MASKED_HIT],
794 stats[DP_STAT_MASKED_HIT] > 0
795 ? (1.0*stats[DP_STAT_LOOKUP_HIT])/stats[DP_STAT_MASKED_HIT]
796 : 0,
797 stats[DP_STAT_MISS], stats[DP_STAT_LOST]);
798
799 if (total_cycles == 0) {
800 return;
801 }
802
803 ds_put_format(reply,
804 "\tpolling cycles:%"PRIu64" (%.02f%%)\n"
805 "\tprocessing cycles:%"PRIu64" (%.02f%%)\n",
806 cycles[PMD_CYCLES_POLLING],
807 cycles[PMD_CYCLES_POLLING] / (double)total_cycles * 100,
808 cycles[PMD_CYCLES_PROCESSING],
809 cycles[PMD_CYCLES_PROCESSING] / (double)total_cycles * 100);
810
811 if (total_packets == 0) {
812 return;
813 }
814
815 ds_put_format(reply,
816 "\tavg cycles per packet: %.02f (%"PRIu64"/%llu)\n",
817 total_cycles / (double)total_packets,
818 total_cycles, total_packets);
819
820 ds_put_format(reply,
821 "\tavg processing cycles per packet: "
822 "%.02f (%"PRIu64"/%llu)\n",
823 cycles[PMD_CYCLES_PROCESSING] / (double)total_packets,
824 cycles[PMD_CYCLES_PROCESSING], total_packets);
825 }
826
827 static void
828 pmd_info_clear_stats(struct ds *reply OVS_UNUSED,
829 struct dp_netdev_pmd_thread *pmd,
830 unsigned long long stats[DP_N_STATS],
831 uint64_t cycles[PMD_N_CYCLES])
832 {
833 int i;
834
835 /* We cannot write 'stats' and 'cycles' (because they're written by other
836 * threads) and we shouldn't change 'stats' (because they're used to count
837 * datapath stats, which must not be cleared here). Instead, we save the
838 * current values and subtract them from the values to be displayed in the
839 * future */
840 for (i = 0; i < DP_N_STATS; i++) {
841 pmd->stats_zero[i] = stats[i];
842 }
843 for (i = 0; i < PMD_N_CYCLES; i++) {
844 pmd->cycles_zero[i] = cycles[i];
845 }
846 }
847
848 static int
849 compare_poll_list(const void *a_, const void *b_)
850 {
851 const struct rxq_poll *a = a_;
852 const struct rxq_poll *b = b_;
853
854 const char *namea = netdev_rxq_get_name(a->rxq->rx);
855 const char *nameb = netdev_rxq_get_name(b->rxq->rx);
856
857 int cmp = strcmp(namea, nameb);
858 if (!cmp) {
859 return netdev_rxq_get_queue_id(a->rxq->rx)
860 - netdev_rxq_get_queue_id(b->rxq->rx);
861 } else {
862 return cmp;
863 }
864 }
865
866 static void
867 sorted_poll_list(struct dp_netdev_pmd_thread *pmd, struct rxq_poll **list,
868 size_t *n)
869 {
870 struct rxq_poll *ret, *poll;
871 size_t i;
872
873 *n = hmap_count(&pmd->poll_list);
874 if (!*n) {
875 ret = NULL;
876 } else {
877 ret = xcalloc(*n, sizeof *ret);
878 i = 0;
879 HMAP_FOR_EACH (poll, node, &pmd->poll_list) {
880 ret[i] = *poll;
881 i++;
882 }
883 ovs_assert(i == *n);
884 }
885
886 qsort(ret, *n, sizeof *ret, compare_poll_list);
887
888 *list = ret;
889 }
890
891 static void
892 pmd_info_show_rxq(struct ds *reply, struct dp_netdev_pmd_thread *pmd)
893 {
894 if (pmd->core_id != NON_PMD_CORE_ID) {
895 const char *prev_name = NULL;
896 struct rxq_poll *list;
897 size_t i, n;
898
899 ds_put_format(reply,
900 "pmd thread numa_id %d core_id %u:\n\tisolated : %s\n",
901 pmd->numa_id, pmd->core_id, (pmd->isolated)
902 ? "true" : "false");
903
904 ovs_mutex_lock(&pmd->port_mutex);
905 sorted_poll_list(pmd, &list, &n);
906 for (i = 0; i < n; i++) {
907 const char *name = netdev_rxq_get_name(list[i].rxq->rx);
908
909 if (!prev_name || strcmp(name, prev_name)) {
910 if (prev_name) {
911 ds_put_cstr(reply, "\n");
912 }
913 ds_put_format(reply, "\tport: %s\tqueue-id:", name);
914 }
915 ds_put_format(reply, " %d",
916 netdev_rxq_get_queue_id(list[i].rxq->rx));
917 prev_name = name;
918 }
919 ovs_mutex_unlock(&pmd->port_mutex);
920 ds_put_cstr(reply, "\n");
921 free(list);
922 }
923 }
924
925 static void
926 dpif_netdev_pmd_info(struct unixctl_conn *conn, int argc, const char *argv[],
927 void *aux)
928 {
929 struct ds reply = DS_EMPTY_INITIALIZER;
930 struct dp_netdev_pmd_thread *pmd;
931 struct dp_netdev *dp = NULL;
932 enum pmd_info_type type = *(enum pmd_info_type *) aux;
933
934 ovs_mutex_lock(&dp_netdev_mutex);
935
936 if (argc == 2) {
937 dp = shash_find_data(&dp_netdevs, argv[1]);
938 } else if (shash_count(&dp_netdevs) == 1) {
939 /* There's only one datapath */
940 dp = shash_first(&dp_netdevs)->data;
941 }
942
943 if (!dp) {
944 ovs_mutex_unlock(&dp_netdev_mutex);
945 unixctl_command_reply_error(conn,
946 "please specify an existing datapath");
947 return;
948 }
949
950 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
951 if (type == PMD_INFO_SHOW_RXQ) {
952 pmd_info_show_rxq(&reply, pmd);
953 } else {
954 unsigned long long stats[DP_N_STATS];
955 uint64_t cycles[PMD_N_CYCLES];
956 int i;
957
958 /* Read current stats and cycle counters */
959 for (i = 0; i < ARRAY_SIZE(stats); i++) {
960 atomic_read_relaxed(&pmd->stats.n[i], &stats[i]);
961 }
962 for (i = 0; i < ARRAY_SIZE(cycles); i++) {
963 atomic_read_relaxed(&pmd->cycles.n[i], &cycles[i]);
964 }
965
966 if (type == PMD_INFO_CLEAR_STATS) {
967 pmd_info_clear_stats(&reply, pmd, stats, cycles);
968 } else if (type == PMD_INFO_SHOW_STATS) {
969 pmd_info_show_stats(&reply, pmd, stats, cycles);
970 }
971 }
972 }
973
974 ovs_mutex_unlock(&dp_netdev_mutex);
975
976 unixctl_command_reply(conn, ds_cstr(&reply));
977 ds_destroy(&reply);
978 }
979 \f
980 static int
981 dpif_netdev_init(void)
982 {
983 static enum pmd_info_type show_aux = PMD_INFO_SHOW_STATS,
984 clear_aux = PMD_INFO_CLEAR_STATS,
985 poll_aux = PMD_INFO_SHOW_RXQ;
986
987 unixctl_command_register("dpif-netdev/pmd-stats-show", "[dp]",
988 0, 1, dpif_netdev_pmd_info,
989 (void *)&show_aux);
990 unixctl_command_register("dpif-netdev/pmd-stats-clear", "[dp]",
991 0, 1, dpif_netdev_pmd_info,
992 (void *)&clear_aux);
993 unixctl_command_register("dpif-netdev/pmd-rxq-show", "[dp]",
994 0, 1, dpif_netdev_pmd_info,
995 (void *)&poll_aux);
996 return 0;
997 }
998
999 static int
1000 dpif_netdev_enumerate(struct sset *all_dps,
1001 const struct dpif_class *dpif_class)
1002 {
1003 struct shash_node *node;
1004
1005 ovs_mutex_lock(&dp_netdev_mutex);
1006 SHASH_FOR_EACH(node, &dp_netdevs) {
1007 struct dp_netdev *dp = node->data;
1008 if (dpif_class != dp->class) {
1009 /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs.
1010 * If the class doesn't match, skip this dpif. */
1011 continue;
1012 }
1013 sset_add(all_dps, node->name);
1014 }
1015 ovs_mutex_unlock(&dp_netdev_mutex);
1016
1017 return 0;
1018 }
1019
1020 static bool
1021 dpif_netdev_class_is_dummy(const struct dpif_class *class)
1022 {
1023 return class != &dpif_netdev_class;
1024 }
1025
1026 static const char *
1027 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
1028 {
1029 return strcmp(type, "internal") ? type
1030 : dpif_netdev_class_is_dummy(class) ? "dummy-internal"
1031 : "tap";
1032 }
1033
1034 static struct dpif *
1035 create_dpif_netdev(struct dp_netdev *dp)
1036 {
1037 uint16_t netflow_id = hash_string(dp->name, 0);
1038 struct dpif_netdev *dpif;
1039
1040 ovs_refcount_ref(&dp->ref_cnt);
1041
1042 dpif = xmalloc(sizeof *dpif);
1043 dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
1044 dpif->dp = dp;
1045 dpif->last_port_seq = seq_read(dp->port_seq);
1046
1047 return &dpif->dpif;
1048 }
1049
1050 /* Choose an unused, non-zero port number and return it on success.
1051 * Return ODPP_NONE on failure. */
1052 static odp_port_t
1053 choose_port(struct dp_netdev *dp, const char *name)
1054 OVS_REQUIRES(dp->port_mutex)
1055 {
1056 uint32_t port_no;
1057
1058 if (dp->class != &dpif_netdev_class) {
1059 const char *p;
1060 int start_no = 0;
1061
1062 /* If the port name begins with "br", start the number search at
1063 * 100 to make writing tests easier. */
1064 if (!strncmp(name, "br", 2)) {
1065 start_no = 100;
1066 }
1067
1068 /* If the port name contains a number, try to assign that port number.
1069 * This can make writing unit tests easier because port numbers are
1070 * predictable. */
1071 for (p = name; *p != '\0'; p++) {
1072 if (isdigit((unsigned char) *p)) {
1073 port_no = start_no + strtol(p, NULL, 10);
1074 if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE)
1075 && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
1076 return u32_to_odp(port_no);
1077 }
1078 break;
1079 }
1080 }
1081 }
1082
1083 for (port_no = 1; port_no <= UINT16_MAX; port_no++) {
1084 if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
1085 return u32_to_odp(port_no);
1086 }
1087 }
1088
1089 return ODPP_NONE;
1090 }
1091
1092 static int
1093 create_dp_netdev(const char *name, const struct dpif_class *class,
1094 struct dp_netdev **dpp)
1095 OVS_REQUIRES(dp_netdev_mutex)
1096 {
1097 struct dp_netdev *dp;
1098 int error;
1099
1100 dp = xzalloc(sizeof *dp);
1101 shash_add(&dp_netdevs, name, dp);
1102
1103 *CONST_CAST(const struct dpif_class **, &dp->class) = class;
1104 *CONST_CAST(const char **, &dp->name) = xstrdup(name);
1105 ovs_refcount_init(&dp->ref_cnt);
1106 atomic_flag_clear(&dp->destroyed);
1107
1108 ovs_mutex_init(&dp->port_mutex);
1109 hmap_init(&dp->ports);
1110 dp->port_seq = seq_create();
1111 fat_rwlock_init(&dp->upcall_rwlock);
1112
1113 dp->reconfigure_seq = seq_create();
1114 dp->last_reconfigure_seq = seq_read(dp->reconfigure_seq);
1115
1116 for (int i = 0; i < N_METER_LOCKS; ++i) {
1117 ovs_mutex_init_adaptive(&dp->meter_locks[i]);
1118 }
1119
1120 /* Disable upcalls by default. */
1121 dp_netdev_disable_upcall(dp);
1122 dp->upcall_aux = NULL;
1123 dp->upcall_cb = NULL;
1124
1125 conntrack_init(&dp->conntrack);
1126
1127 atomic_init(&dp->emc_insert_min, DEFAULT_EM_FLOW_INSERT_MIN);
1128
1129 cmap_init(&dp->poll_threads);
1130 ovs_mutex_init_recursive(&dp->non_pmd_mutex);
1131 ovsthread_key_create(&dp->per_pmd_key, NULL);
1132
1133 ovs_mutex_lock(&dp->port_mutex);
1134 dp_netdev_set_nonpmd(dp);
1135
1136 error = do_add_port(dp, name, dpif_netdev_port_open_type(dp->class,
1137 "internal"),
1138 ODPP_LOCAL);
1139 ovs_mutex_unlock(&dp->port_mutex);
1140 if (error) {
1141 dp_netdev_free(dp);
1142 return error;
1143 }
1144
1145 dp->last_tnl_conf_seq = seq_read(tnl_conf_seq);
1146 *dpp = dp;
1147 return 0;
1148 }
1149
1150 static void
1151 dp_netdev_request_reconfigure(struct dp_netdev *dp)
1152 {
1153 seq_change(dp->reconfigure_seq);
1154 }
1155
1156 static bool
1157 dp_netdev_is_reconf_required(struct dp_netdev *dp)
1158 {
1159 return seq_read(dp->reconfigure_seq) != dp->last_reconfigure_seq;
1160 }
1161
1162 static int
1163 dpif_netdev_open(const struct dpif_class *class, const char *name,
1164 bool create, struct dpif **dpifp)
1165 {
1166 struct dp_netdev *dp;
1167 int error;
1168
1169 ovs_mutex_lock(&dp_netdev_mutex);
1170 dp = shash_find_data(&dp_netdevs, name);
1171 if (!dp) {
1172 error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
1173 } else {
1174 error = (dp->class != class ? EINVAL
1175 : create ? EEXIST
1176 : 0);
1177 }
1178 if (!error) {
1179 *dpifp = create_dpif_netdev(dp);
1180 dp->dpif = *dpifp;
1181 }
1182 ovs_mutex_unlock(&dp_netdev_mutex);
1183
1184 return error;
1185 }
1186
1187 static void
1188 dp_netdev_destroy_upcall_lock(struct dp_netdev *dp)
1189 OVS_NO_THREAD_SAFETY_ANALYSIS
1190 {
1191 /* Check that upcalls are disabled, i.e. that the rwlock is taken */
1192 ovs_assert(fat_rwlock_tryrdlock(&dp->upcall_rwlock));
1193
1194 /* Before freeing a lock we should release it */
1195 fat_rwlock_unlock(&dp->upcall_rwlock);
1196 fat_rwlock_destroy(&dp->upcall_rwlock);
1197 }
1198
1199 static void
1200 dp_delete_meter(struct dp_netdev *dp, uint32_t meter_id)
1201 OVS_REQUIRES(dp->meter_locks[meter_id % N_METER_LOCKS])
1202 {
1203 if (dp->meters[meter_id]) {
1204 free(dp->meters[meter_id]);
1205 dp->meters[meter_id] = NULL;
1206 }
1207 }
1208
1209 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
1210 * through the 'dp_netdevs' shash while freeing 'dp'. */
1211 static void
1212 dp_netdev_free(struct dp_netdev *dp)
1213 OVS_REQUIRES(dp_netdev_mutex)
1214 {
1215 struct dp_netdev_port *port, *next;
1216
1217 shash_find_and_delete(&dp_netdevs, dp->name);
1218
1219 ovs_mutex_lock(&dp->port_mutex);
1220 HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) {
1221 do_del_port(dp, port);
1222 }
1223 ovs_mutex_unlock(&dp->port_mutex);
1224
1225 dp_netdev_destroy_all_pmds(dp, true);
1226 cmap_destroy(&dp->poll_threads);
1227
1228 ovs_mutex_destroy(&dp->non_pmd_mutex);
1229 ovsthread_key_delete(dp->per_pmd_key);
1230
1231 conntrack_destroy(&dp->conntrack);
1232
1233
1234 seq_destroy(dp->reconfigure_seq);
1235
1236 seq_destroy(dp->port_seq);
1237 hmap_destroy(&dp->ports);
1238 ovs_mutex_destroy(&dp->port_mutex);
1239
1240 /* Upcalls must be disabled at this point */
1241 dp_netdev_destroy_upcall_lock(dp);
1242
1243 int i;
1244
1245 for (i = 0; i < MAX_METERS; ++i) {
1246 meter_lock(dp, i);
1247 dp_delete_meter(dp, i);
1248 meter_unlock(dp, i);
1249 }
1250 for (i = 0; i < N_METER_LOCKS; ++i) {
1251 ovs_mutex_destroy(&dp->meter_locks[i]);
1252 }
1253
1254 free(dp->pmd_cmask);
1255 free(CONST_CAST(char *, dp->name));
1256 free(dp);
1257 }
1258
1259 static void
1260 dp_netdev_unref(struct dp_netdev *dp)
1261 {
1262 if (dp) {
1263 /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
1264 * get a new reference to 'dp' through the 'dp_netdevs' shash. */
1265 ovs_mutex_lock(&dp_netdev_mutex);
1266 if (ovs_refcount_unref_relaxed(&dp->ref_cnt) == 1) {
1267 dp_netdev_free(dp);
1268 }
1269 ovs_mutex_unlock(&dp_netdev_mutex);
1270 }
1271 }
1272
1273 static void
1274 dpif_netdev_close(struct dpif *dpif)
1275 {
1276 struct dp_netdev *dp = get_dp_netdev(dpif);
1277
1278 dp_netdev_unref(dp);
1279 free(dpif);
1280 }
1281
1282 static int
1283 dpif_netdev_destroy(struct dpif *dpif)
1284 {
1285 struct dp_netdev *dp = get_dp_netdev(dpif);
1286
1287 if (!atomic_flag_test_and_set(&dp->destroyed)) {
1288 if (ovs_refcount_unref_relaxed(&dp->ref_cnt) == 1) {
1289 /* Can't happen: 'dpif' still owns a reference to 'dp'. */
1290 OVS_NOT_REACHED();
1291 }
1292 }
1293
1294 return 0;
1295 }
1296
1297 /* Add 'n' to the atomic variable 'var' non-atomically and using relaxed
1298 * load/store semantics. While the increment is not atomic, the load and
1299 * store operations are, making it impossible to read inconsistent values.
1300 *
1301 * This is used to update thread local stats counters. */
1302 static void
1303 non_atomic_ullong_add(atomic_ullong *var, unsigned long long n)
1304 {
1305 unsigned long long tmp;
1306
1307 atomic_read_relaxed(var, &tmp);
1308 tmp += n;
1309 atomic_store_relaxed(var, tmp);
1310 }
1311
1312 static int
1313 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
1314 {
1315 struct dp_netdev *dp = get_dp_netdev(dpif);
1316 struct dp_netdev_pmd_thread *pmd;
1317
1318 stats->n_flows = stats->n_hit = stats->n_missed = stats->n_lost = 0;
1319 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
1320 unsigned long long n;
1321 stats->n_flows += cmap_count(&pmd->flow_table);
1322
1323 atomic_read_relaxed(&pmd->stats.n[DP_STAT_MASKED_HIT], &n);
1324 stats->n_hit += n;
1325 atomic_read_relaxed(&pmd->stats.n[DP_STAT_EXACT_HIT], &n);
1326 stats->n_hit += n;
1327 atomic_read_relaxed(&pmd->stats.n[DP_STAT_MISS], &n);
1328 stats->n_missed += n;
1329 atomic_read_relaxed(&pmd->stats.n[DP_STAT_LOST], &n);
1330 stats->n_lost += n;
1331 }
1332 stats->n_masks = UINT32_MAX;
1333 stats->n_mask_hit = UINT64_MAX;
1334
1335 return 0;
1336 }
1337
1338 static void
1339 dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread *pmd)
1340 {
1341 if (pmd->core_id == NON_PMD_CORE_ID) {
1342 ovs_mutex_lock(&pmd->dp->non_pmd_mutex);
1343 ovs_mutex_lock(&pmd->port_mutex);
1344 pmd_load_cached_ports(pmd);
1345 ovs_mutex_unlock(&pmd->port_mutex);
1346 ovs_mutex_unlock(&pmd->dp->non_pmd_mutex);
1347 return;
1348 }
1349
1350 ovs_mutex_lock(&pmd->cond_mutex);
1351 seq_change(pmd->reload_seq);
1352 atomic_store_relaxed(&pmd->reload, true);
1353 ovs_mutex_cond_wait(&pmd->cond, &pmd->cond_mutex);
1354 ovs_mutex_unlock(&pmd->cond_mutex);
1355 }
1356
1357 static uint32_t
1358 hash_port_no(odp_port_t port_no)
1359 {
1360 return hash_int(odp_to_u32(port_no), 0);
1361 }
1362
1363 static int
1364 port_create(const char *devname, const char *type,
1365 odp_port_t port_no, struct dp_netdev_port **portp)
1366 {
1367 struct netdev_saved_flags *sf;
1368 struct dp_netdev_port *port;
1369 enum netdev_flags flags;
1370 struct netdev *netdev;
1371 int error;
1372
1373 *portp = NULL;
1374
1375 /* Open and validate network device. */
1376 error = netdev_open(devname, type, &netdev);
1377 if (error) {
1378 return error;
1379 }
1380 /* XXX reject non-Ethernet devices */
1381
1382 netdev_get_flags(netdev, &flags);
1383 if (flags & NETDEV_LOOPBACK) {
1384 VLOG_ERR("%s: cannot add a loopback device", devname);
1385 error = EINVAL;
1386 goto out;
1387 }
1388
1389 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
1390 if (error) {
1391 VLOG_ERR("%s: cannot set promisc flag", devname);
1392 goto out;
1393 }
1394
1395 port = xzalloc(sizeof *port);
1396 port->port_no = port_no;
1397 port->netdev = netdev;
1398 port->type = xstrdup(type);
1399 port->sf = sf;
1400 port->need_reconfigure = true;
1401 ovs_mutex_init(&port->txq_used_mutex);
1402
1403 *portp = port;
1404
1405 return 0;
1406
1407 out:
1408 netdev_close(netdev);
1409 return error;
1410 }
1411
1412 static int
1413 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
1414 odp_port_t port_no)
1415 OVS_REQUIRES(dp->port_mutex)
1416 {
1417 struct dp_netdev_port *port;
1418 int error;
1419
1420 /* Reject devices already in 'dp'. */
1421 if (!get_port_by_name(dp, devname, &port)) {
1422 return EEXIST;
1423 }
1424
1425 error = port_create(devname, type, port_no, &port);
1426 if (error) {
1427 return error;
1428 }
1429
1430 hmap_insert(&dp->ports, &port->node, hash_port_no(port_no));
1431 seq_change(dp->port_seq);
1432
1433 reconfigure_datapath(dp);
1434
1435 return 0;
1436 }
1437
1438 static int
1439 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
1440 odp_port_t *port_nop)
1441 {
1442 struct dp_netdev *dp = get_dp_netdev(dpif);
1443 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1444 const char *dpif_port;
1445 odp_port_t port_no;
1446 int error;
1447
1448 ovs_mutex_lock(&dp->port_mutex);
1449 dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1450 if (*port_nop != ODPP_NONE) {
1451 port_no = *port_nop;
1452 error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0;
1453 } else {
1454 port_no = choose_port(dp, dpif_port);
1455 error = port_no == ODPP_NONE ? EFBIG : 0;
1456 }
1457 if (!error) {
1458 *port_nop = port_no;
1459 error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
1460 }
1461 ovs_mutex_unlock(&dp->port_mutex);
1462
1463 return error;
1464 }
1465
1466 static int
1467 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
1468 {
1469 struct dp_netdev *dp = get_dp_netdev(dpif);
1470 int error;
1471
1472 ovs_mutex_lock(&dp->port_mutex);
1473 if (port_no == ODPP_LOCAL) {
1474 error = EINVAL;
1475 } else {
1476 struct dp_netdev_port *port;
1477
1478 error = get_port_by_number(dp, port_no, &port);
1479 if (!error) {
1480 do_del_port(dp, port);
1481 }
1482 }
1483 ovs_mutex_unlock(&dp->port_mutex);
1484
1485 return error;
1486 }
1487
1488 static bool
1489 is_valid_port_number(odp_port_t port_no)
1490 {
1491 return port_no != ODPP_NONE;
1492 }
1493
1494 static struct dp_netdev_port *
1495 dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no)
1496 OVS_REQUIRES(dp->port_mutex)
1497 {
1498 struct dp_netdev_port *port;
1499
1500 HMAP_FOR_EACH_WITH_HASH (port, node, hash_port_no(port_no), &dp->ports) {
1501 if (port->port_no == port_no) {
1502 return port;
1503 }
1504 }
1505 return NULL;
1506 }
1507
1508 static int
1509 get_port_by_number(struct dp_netdev *dp,
1510 odp_port_t port_no, struct dp_netdev_port **portp)
1511 OVS_REQUIRES(dp->port_mutex)
1512 {
1513 if (!is_valid_port_number(port_no)) {
1514 *portp = NULL;
1515 return EINVAL;
1516 } else {
1517 *portp = dp_netdev_lookup_port(dp, port_no);
1518 return *portp ? 0 : ENODEV;
1519 }
1520 }
1521
1522 static void
1523 port_destroy(struct dp_netdev_port *port)
1524 {
1525 if (!port) {
1526 return;
1527 }
1528
1529 netdev_close(port->netdev);
1530 netdev_restore_flags(port->sf);
1531
1532 for (unsigned i = 0; i < port->n_rxq; i++) {
1533 netdev_rxq_close(port->rxqs[i].rx);
1534 }
1535 ovs_mutex_destroy(&port->txq_used_mutex);
1536 free(port->rxq_affinity_list);
1537 free(port->txq_used);
1538 free(port->rxqs);
1539 free(port->type);
1540 free(port);
1541 }
1542
1543 static int
1544 get_port_by_name(struct dp_netdev *dp,
1545 const char *devname, struct dp_netdev_port **portp)
1546 OVS_REQUIRES(dp->port_mutex)
1547 {
1548 struct dp_netdev_port *port;
1549
1550 HMAP_FOR_EACH (port, node, &dp->ports) {
1551 if (!strcmp(netdev_get_name(port->netdev), devname)) {
1552 *portp = port;
1553 return 0;
1554 }
1555 }
1556
1557 /* Callers of dpif_netdev_port_query_by_name() expect ENODEV for a non
1558 * existing port. */
1559 return ENODEV;
1560 }
1561
1562 /* Returns 'true' if there is a port with pmd netdev. */
1563 static bool
1564 has_pmd_port(struct dp_netdev *dp)
1565 OVS_REQUIRES(dp->port_mutex)
1566 {
1567 struct dp_netdev_port *port;
1568
1569 HMAP_FOR_EACH (port, node, &dp->ports) {
1570 if (netdev_is_pmd(port->netdev)) {
1571 return true;
1572 }
1573 }
1574
1575 return false;
1576 }
1577
1578 static void
1579 do_del_port(struct dp_netdev *dp, struct dp_netdev_port *port)
1580 OVS_REQUIRES(dp->port_mutex)
1581 {
1582 hmap_remove(&dp->ports, &port->node);
1583 seq_change(dp->port_seq);
1584
1585 reconfigure_datapath(dp);
1586
1587 port_destroy(port);
1588 }
1589
1590 static void
1591 answer_port_query(const struct dp_netdev_port *port,
1592 struct dpif_port *dpif_port)
1593 {
1594 dpif_port->name = xstrdup(netdev_get_name(port->netdev));
1595 dpif_port->type = xstrdup(port->type);
1596 dpif_port->port_no = port->port_no;
1597 }
1598
1599 static int
1600 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
1601 struct dpif_port *dpif_port)
1602 {
1603 struct dp_netdev *dp = get_dp_netdev(dpif);
1604 struct dp_netdev_port *port;
1605 int error;
1606
1607 ovs_mutex_lock(&dp->port_mutex);
1608 error = get_port_by_number(dp, port_no, &port);
1609 if (!error && dpif_port) {
1610 answer_port_query(port, dpif_port);
1611 }
1612 ovs_mutex_unlock(&dp->port_mutex);
1613
1614 return error;
1615 }
1616
1617 static int
1618 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
1619 struct dpif_port *dpif_port)
1620 {
1621 struct dp_netdev *dp = get_dp_netdev(dpif);
1622 struct dp_netdev_port *port;
1623 int error;
1624
1625 ovs_mutex_lock(&dp->port_mutex);
1626 error = get_port_by_name(dp, devname, &port);
1627 if (!error && dpif_port) {
1628 answer_port_query(port, dpif_port);
1629 }
1630 ovs_mutex_unlock(&dp->port_mutex);
1631
1632 return error;
1633 }
1634
1635 static void
1636 dp_netdev_flow_free(struct dp_netdev_flow *flow)
1637 {
1638 dp_netdev_actions_free(dp_netdev_flow_get_actions(flow));
1639 free(flow);
1640 }
1641
1642 static void dp_netdev_flow_unref(struct dp_netdev_flow *flow)
1643 {
1644 if (ovs_refcount_unref_relaxed(&flow->ref_cnt) == 1) {
1645 ovsrcu_postpone(dp_netdev_flow_free, flow);
1646 }
1647 }
1648
1649 static uint32_t
1650 dp_netdev_flow_hash(const ovs_u128 *ufid)
1651 {
1652 return ufid->u32[0];
1653 }
1654
1655 static inline struct dpcls *
1656 dp_netdev_pmd_lookup_dpcls(struct dp_netdev_pmd_thread *pmd,
1657 odp_port_t in_port)
1658 {
1659 struct dpcls *cls;
1660 uint32_t hash = hash_port_no(in_port);
1661 CMAP_FOR_EACH_WITH_HASH (cls, node, hash, &pmd->classifiers) {
1662 if (cls->in_port == in_port) {
1663 /* Port classifier exists already */
1664 return cls;
1665 }
1666 }
1667 return NULL;
1668 }
1669
1670 static inline struct dpcls *
1671 dp_netdev_pmd_find_dpcls(struct dp_netdev_pmd_thread *pmd,
1672 odp_port_t in_port)
1673 OVS_REQUIRES(pmd->flow_mutex)
1674 {
1675 struct dpcls *cls = dp_netdev_pmd_lookup_dpcls(pmd, in_port);
1676 uint32_t hash = hash_port_no(in_port);
1677
1678 if (!cls) {
1679 /* Create new classifier for in_port */
1680 cls = xmalloc(sizeof(*cls));
1681 dpcls_init(cls);
1682 cls->in_port = in_port;
1683 cmap_insert(&pmd->classifiers, &cls->node, hash);
1684 VLOG_DBG("Creating dpcls %p for in_port %d", cls, in_port);
1685 }
1686 return cls;
1687 }
1688
1689 static void
1690 dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread *pmd,
1691 struct dp_netdev_flow *flow)
1692 OVS_REQUIRES(pmd->flow_mutex)
1693 {
1694 struct cmap_node *node = CONST_CAST(struct cmap_node *, &flow->node);
1695 struct dpcls *cls;
1696 odp_port_t in_port = flow->flow.in_port.odp_port;
1697
1698 cls = dp_netdev_pmd_lookup_dpcls(pmd, in_port);
1699 ovs_assert(cls != NULL);
1700 dpcls_remove(cls, &flow->cr);
1701 cmap_remove(&pmd->flow_table, node, dp_netdev_flow_hash(&flow->ufid));
1702 flow->dead = true;
1703
1704 dp_netdev_flow_unref(flow);
1705 }
1706
1707 static void
1708 dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread *pmd)
1709 {
1710 struct dp_netdev_flow *netdev_flow;
1711
1712 ovs_mutex_lock(&pmd->flow_mutex);
1713 CMAP_FOR_EACH (netdev_flow, node, &pmd->flow_table) {
1714 dp_netdev_pmd_remove_flow(pmd, netdev_flow);
1715 }
1716 ovs_mutex_unlock(&pmd->flow_mutex);
1717 }
1718
1719 static int
1720 dpif_netdev_flow_flush(struct dpif *dpif)
1721 {
1722 struct dp_netdev *dp = get_dp_netdev(dpif);
1723 struct dp_netdev_pmd_thread *pmd;
1724
1725 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
1726 dp_netdev_pmd_flow_flush(pmd);
1727 }
1728
1729 return 0;
1730 }
1731
1732 struct dp_netdev_port_state {
1733 struct hmap_position position;
1734 char *name;
1735 };
1736
1737 static int
1738 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
1739 {
1740 *statep = xzalloc(sizeof(struct dp_netdev_port_state));
1741 return 0;
1742 }
1743
1744 static int
1745 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
1746 struct dpif_port *dpif_port)
1747 {
1748 struct dp_netdev_port_state *state = state_;
1749 struct dp_netdev *dp = get_dp_netdev(dpif);
1750 struct hmap_node *node;
1751 int retval;
1752
1753 ovs_mutex_lock(&dp->port_mutex);
1754 node = hmap_at_position(&dp->ports, &state->position);
1755 if (node) {
1756 struct dp_netdev_port *port;
1757
1758 port = CONTAINER_OF(node, struct dp_netdev_port, node);
1759
1760 free(state->name);
1761 state->name = xstrdup(netdev_get_name(port->netdev));
1762 dpif_port->name = state->name;
1763 dpif_port->type = port->type;
1764 dpif_port->port_no = port->port_no;
1765
1766 retval = 0;
1767 } else {
1768 retval = EOF;
1769 }
1770 ovs_mutex_unlock(&dp->port_mutex);
1771
1772 return retval;
1773 }
1774
1775 static int
1776 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1777 {
1778 struct dp_netdev_port_state *state = state_;
1779 free(state->name);
1780 free(state);
1781 return 0;
1782 }
1783
1784 static int
1785 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
1786 {
1787 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
1788 uint64_t new_port_seq;
1789 int error;
1790
1791 new_port_seq = seq_read(dpif->dp->port_seq);
1792 if (dpif->last_port_seq != new_port_seq) {
1793 dpif->last_port_seq = new_port_seq;
1794 error = ENOBUFS;
1795 } else {
1796 error = EAGAIN;
1797 }
1798
1799 return error;
1800 }
1801
1802 static void
1803 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
1804 {
1805 struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
1806
1807 seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
1808 }
1809
1810 static struct dp_netdev_flow *
1811 dp_netdev_flow_cast(const struct dpcls_rule *cr)
1812 {
1813 return cr ? CONTAINER_OF(cr, struct dp_netdev_flow, cr) : NULL;
1814 }
1815
1816 static bool dp_netdev_flow_ref(struct dp_netdev_flow *flow)
1817 {
1818 return ovs_refcount_try_ref_rcu(&flow->ref_cnt);
1819 }
1820
1821 /* netdev_flow_key utilities.
1822 *
1823 * netdev_flow_key is basically a miniflow. We use these functions
1824 * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow
1825 * functions (miniflow_clone_inline, miniflow_equal, ...), because:
1826 *
1827 * - Since we are dealing exclusively with miniflows created by
1828 * miniflow_extract(), if the map is different the miniflow is different.
1829 * Therefore we can be faster by comparing the map and the miniflow in a
1830 * single memcmp().
1831 * - These functions can be inlined by the compiler. */
1832
1833 /* Given the number of bits set in miniflow's maps, returns the size of the
1834 * 'netdev_flow_key.mf' */
1835 static inline size_t
1836 netdev_flow_key_size(size_t flow_u64s)
1837 {
1838 return sizeof(struct miniflow) + MINIFLOW_VALUES_SIZE(flow_u64s);
1839 }
1840
1841 static inline bool
1842 netdev_flow_key_equal(const struct netdev_flow_key *a,
1843 const struct netdev_flow_key *b)
1844 {
1845 /* 'b->len' may be not set yet. */
1846 return a->hash == b->hash && !memcmp(&a->mf, &b->mf, a->len);
1847 }
1848
1849 /* Used to compare 'netdev_flow_key' in the exact match cache to a miniflow.
1850 * The maps are compared bitwise, so both 'key->mf' and 'mf' must have been
1851 * generated by miniflow_extract. */
1852 static inline bool
1853 netdev_flow_key_equal_mf(const struct netdev_flow_key *key,
1854 const struct miniflow *mf)
1855 {
1856 return !memcmp(&key->mf, mf, key->len);
1857 }
1858
1859 static inline void
1860 netdev_flow_key_clone(struct netdev_flow_key *dst,
1861 const struct netdev_flow_key *src)
1862 {
1863 memcpy(dst, src,
1864 offsetof(struct netdev_flow_key, mf) + src->len);
1865 }
1866
1867 /* Slow. */
1868 static void
1869 netdev_flow_key_from_flow(struct netdev_flow_key *dst,
1870 const struct flow *src)
1871 {
1872 struct dp_packet packet;
1873 uint64_t buf_stub[512 / 8];
1874
1875 dp_packet_use_stub(&packet, buf_stub, sizeof buf_stub);
1876 pkt_metadata_from_flow(&packet.md, src);
1877 flow_compose(&packet, src);
1878 miniflow_extract(&packet, &dst->mf);
1879 dp_packet_uninit(&packet);
1880
1881 dst->len = netdev_flow_key_size(miniflow_n_values(&dst->mf));
1882 dst->hash = 0; /* Not computed yet. */
1883 }
1884
1885 /* Initialize a netdev_flow_key 'mask' from 'match'. */
1886 static inline void
1887 netdev_flow_mask_init(struct netdev_flow_key *mask,
1888 const struct match *match)
1889 {
1890 uint64_t *dst = miniflow_values(&mask->mf);
1891 struct flowmap fmap;
1892 uint32_t hash = 0;
1893 size_t idx;
1894
1895 /* Only check masks that make sense for the flow. */
1896 flow_wc_map(&match->flow, &fmap);
1897 flowmap_init(&mask->mf.map);
1898
1899 FLOWMAP_FOR_EACH_INDEX(idx, fmap) {
1900 uint64_t mask_u64 = flow_u64_value(&match->wc.masks, idx);
1901
1902 if (mask_u64) {
1903 flowmap_set(&mask->mf.map, idx, 1);
1904 *dst++ = mask_u64;
1905 hash = hash_add64(hash, mask_u64);
1906 }
1907 }
1908
1909 map_t map;
1910
1911 FLOWMAP_FOR_EACH_MAP (map, mask->mf.map) {
1912 hash = hash_add64(hash, map);
1913 }
1914
1915 size_t n = dst - miniflow_get_values(&mask->mf);
1916
1917 mask->hash = hash_finish(hash, n * 8);
1918 mask->len = netdev_flow_key_size(n);
1919 }
1920
1921 /* Initializes 'dst' as a copy of 'flow' masked with 'mask'. */
1922 static inline void
1923 netdev_flow_key_init_masked(struct netdev_flow_key *dst,
1924 const struct flow *flow,
1925 const struct netdev_flow_key *mask)
1926 {
1927 uint64_t *dst_u64 = miniflow_values(&dst->mf);
1928 const uint64_t *mask_u64 = miniflow_get_values(&mask->mf);
1929 uint32_t hash = 0;
1930 uint64_t value;
1931
1932 dst->len = mask->len;
1933 dst->mf = mask->mf; /* Copy maps. */
1934
1935 FLOW_FOR_EACH_IN_MAPS(value, flow, mask->mf.map) {
1936 *dst_u64 = value & *mask_u64++;
1937 hash = hash_add64(hash, *dst_u64++);
1938 }
1939 dst->hash = hash_finish(hash,
1940 (dst_u64 - miniflow_get_values(&dst->mf)) * 8);
1941 }
1942
1943 /* Iterate through netdev_flow_key TNL u64 values specified by 'FLOWMAP'. */
1944 #define NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(VALUE, KEY, FLOWMAP) \
1945 MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, &(KEY)->mf, FLOWMAP)
1946
1947 /* Returns a hash value for the bits of 'key' where there are 1-bits in
1948 * 'mask'. */
1949 static inline uint32_t
1950 netdev_flow_key_hash_in_mask(const struct netdev_flow_key *key,
1951 const struct netdev_flow_key *mask)
1952 {
1953 const uint64_t *p = miniflow_get_values(&mask->mf);
1954 uint32_t hash = 0;
1955 uint64_t value;
1956
1957 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value, key, mask->mf.map) {
1958 hash = hash_add64(hash, value & *p++);
1959 }
1960
1961 return hash_finish(hash, (p - miniflow_get_values(&mask->mf)) * 8);
1962 }
1963
1964 static inline bool
1965 emc_entry_alive(struct emc_entry *ce)
1966 {
1967 return ce->flow && !ce->flow->dead;
1968 }
1969
1970 static void
1971 emc_clear_entry(struct emc_entry *ce)
1972 {
1973 if (ce->flow) {
1974 dp_netdev_flow_unref(ce->flow);
1975 ce->flow = NULL;
1976 }
1977 }
1978
1979 static inline void
1980 emc_change_entry(struct emc_entry *ce, struct dp_netdev_flow *flow,
1981 const struct netdev_flow_key *key)
1982 {
1983 if (ce->flow != flow) {
1984 if (ce->flow) {
1985 dp_netdev_flow_unref(ce->flow);
1986 }
1987
1988 if (dp_netdev_flow_ref(flow)) {
1989 ce->flow = flow;
1990 } else {
1991 ce->flow = NULL;
1992 }
1993 }
1994 if (key) {
1995 netdev_flow_key_clone(&ce->key, key);
1996 }
1997 }
1998
1999 static inline void
2000 emc_insert(struct emc_cache *cache, const struct netdev_flow_key *key,
2001 struct dp_netdev_flow *flow)
2002 {
2003 struct emc_entry *to_be_replaced = NULL;
2004 struct emc_entry *current_entry;
2005
2006 EMC_FOR_EACH_POS_WITH_HASH(cache, current_entry, key->hash) {
2007 if (netdev_flow_key_equal(&current_entry->key, key)) {
2008 /* We found the entry with the 'mf' miniflow */
2009 emc_change_entry(current_entry, flow, NULL);
2010 return;
2011 }
2012
2013 /* Replacement policy: put the flow in an empty (not alive) entry, or
2014 * in the first entry where it can be */
2015 if (!to_be_replaced
2016 || (emc_entry_alive(to_be_replaced)
2017 && !emc_entry_alive(current_entry))
2018 || current_entry->key.hash < to_be_replaced->key.hash) {
2019 to_be_replaced = current_entry;
2020 }
2021 }
2022 /* We didn't find the miniflow in the cache.
2023 * The 'to_be_replaced' entry is where the new flow will be stored */
2024
2025 emc_change_entry(to_be_replaced, flow, key);
2026 }
2027
2028 static inline void
2029 emc_probabilistic_insert(struct dp_netdev_pmd_thread *pmd,
2030 const struct netdev_flow_key *key,
2031 struct dp_netdev_flow *flow)
2032 {
2033 /* Insert an entry into the EMC based on probability value 'min'. By
2034 * default the value is UINT32_MAX / 100 which yields an insertion
2035 * probability of 1/100 ie. 1% */
2036
2037 uint32_t min;
2038 atomic_read_relaxed(&pmd->dp->emc_insert_min, &min);
2039
2040 #ifdef DPDK_NETDEV
2041 if (min && (key->hash ^ (uint32_t) pmd->last_cycles) <= min) {
2042 #else
2043 if (min && (key->hash ^ random_uint32()) <= min) {
2044 #endif
2045 emc_insert(&pmd->flow_cache, key, flow);
2046 }
2047 }
2048
2049 static inline struct dp_netdev_flow *
2050 emc_lookup(struct emc_cache *cache, const struct netdev_flow_key *key)
2051 {
2052 struct emc_entry *current_entry;
2053
2054 EMC_FOR_EACH_POS_WITH_HASH(cache, current_entry, key->hash) {
2055 if (current_entry->key.hash == key->hash
2056 && emc_entry_alive(current_entry)
2057 && netdev_flow_key_equal_mf(&current_entry->key, &key->mf)) {
2058
2059 /* We found the entry with the 'key->mf' miniflow */
2060 return current_entry->flow;
2061 }
2062 }
2063
2064 return NULL;
2065 }
2066
2067 static struct dp_netdev_flow *
2068 dp_netdev_pmd_lookup_flow(struct dp_netdev_pmd_thread *pmd,
2069 const struct netdev_flow_key *key,
2070 int *lookup_num_p)
2071 {
2072 struct dpcls *cls;
2073 struct dpcls_rule *rule;
2074 odp_port_t in_port = u32_to_odp(MINIFLOW_GET_U32(&key->mf, in_port));
2075 struct dp_netdev_flow *netdev_flow = NULL;
2076
2077 cls = dp_netdev_pmd_lookup_dpcls(pmd, in_port);
2078 if (OVS_LIKELY(cls)) {
2079 dpcls_lookup(cls, key, &rule, 1, lookup_num_p);
2080 netdev_flow = dp_netdev_flow_cast(rule);
2081 }
2082 return netdev_flow;
2083 }
2084
2085 static struct dp_netdev_flow *
2086 dp_netdev_pmd_find_flow(const struct dp_netdev_pmd_thread *pmd,
2087 const ovs_u128 *ufidp, const struct nlattr *key,
2088 size_t key_len)
2089 {
2090 struct dp_netdev_flow *netdev_flow;
2091 struct flow flow;
2092 ovs_u128 ufid;
2093
2094 /* If a UFID is not provided, determine one based on the key. */
2095 if (!ufidp && key && key_len
2096 && !dpif_netdev_flow_from_nlattrs(key, key_len, &flow, false)) {
2097 dpif_flow_hash(pmd->dp->dpif, &flow, sizeof flow, &ufid);
2098 ufidp = &ufid;
2099 }
2100
2101 if (ufidp) {
2102 CMAP_FOR_EACH_WITH_HASH (netdev_flow, node, dp_netdev_flow_hash(ufidp),
2103 &pmd->flow_table) {
2104 if (ovs_u128_equals(netdev_flow->ufid, *ufidp)) {
2105 return netdev_flow;
2106 }
2107 }
2108 }
2109
2110 return NULL;
2111 }
2112
2113 static void
2114 get_dpif_flow_stats(const struct dp_netdev_flow *netdev_flow_,
2115 struct dpif_flow_stats *stats)
2116 {
2117 struct dp_netdev_flow *netdev_flow;
2118 unsigned long long n;
2119 long long used;
2120 uint16_t flags;
2121
2122 netdev_flow = CONST_CAST(struct dp_netdev_flow *, netdev_flow_);
2123
2124 atomic_read_relaxed(&netdev_flow->stats.packet_count, &n);
2125 stats->n_packets = n;
2126 atomic_read_relaxed(&netdev_flow->stats.byte_count, &n);
2127 stats->n_bytes = n;
2128 atomic_read_relaxed(&netdev_flow->stats.used, &used);
2129 stats->used = used;
2130 atomic_read_relaxed(&netdev_flow->stats.tcp_flags, &flags);
2131 stats->tcp_flags = flags;
2132 }
2133
2134 /* Converts to the dpif_flow format, using 'key_buf' and 'mask_buf' for
2135 * storing the netlink-formatted key/mask. 'key_buf' may be the same as
2136 * 'mask_buf'. Actions will be returned without copying, by relying on RCU to
2137 * protect them. */
2138 static void
2139 dp_netdev_flow_to_dpif_flow(const struct dp_netdev_flow *netdev_flow,
2140 struct ofpbuf *key_buf, struct ofpbuf *mask_buf,
2141 struct dpif_flow *flow, bool terse)
2142 {
2143 if (terse) {
2144 memset(flow, 0, sizeof *flow);
2145 } else {
2146 struct flow_wildcards wc;
2147 struct dp_netdev_actions *actions;
2148 size_t offset;
2149 struct odp_flow_key_parms odp_parms = {
2150 .flow = &netdev_flow->flow,
2151 .mask = &wc.masks,
2152 .support = dp_netdev_support,
2153 };
2154
2155 miniflow_expand(&netdev_flow->cr.mask->mf, &wc.masks);
2156 /* in_port is exact matched, but we have left it out from the mask for
2157 * optimnization reasons. Add in_port back to the mask. */
2158 wc.masks.in_port.odp_port = ODPP_NONE;
2159
2160 /* Key */
2161 offset = key_buf->size;
2162 flow->key = ofpbuf_tail(key_buf);
2163 odp_flow_key_from_flow(&odp_parms, key_buf);
2164 flow->key_len = key_buf->size - offset;
2165
2166 /* Mask */
2167 offset = mask_buf->size;
2168 flow->mask = ofpbuf_tail(mask_buf);
2169 odp_parms.key_buf = key_buf;
2170 odp_flow_key_from_mask(&odp_parms, mask_buf);
2171 flow->mask_len = mask_buf->size - offset;
2172
2173 /* Actions */
2174 actions = dp_netdev_flow_get_actions(netdev_flow);
2175 flow->actions = actions->actions;
2176 flow->actions_len = actions->size;
2177 }
2178
2179 flow->ufid = netdev_flow->ufid;
2180 flow->ufid_present = true;
2181 flow->pmd_id = netdev_flow->pmd_id;
2182 get_dpif_flow_stats(netdev_flow, &flow->stats);
2183 }
2184
2185 static int
2186 dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len,
2187 const struct nlattr *mask_key,
2188 uint32_t mask_key_len, const struct flow *flow,
2189 struct flow_wildcards *wc, bool probe)
2190 {
2191 enum odp_key_fitness fitness;
2192
2193 fitness = odp_flow_key_to_mask(mask_key, mask_key_len, wc, flow);
2194 if (fitness) {
2195 if (!probe) {
2196 /* This should not happen: it indicates that
2197 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
2198 * disagree on the acceptable form of a mask. Log the problem
2199 * as an error, with enough details to enable debugging. */
2200 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2201
2202 if (!VLOG_DROP_ERR(&rl)) {
2203 struct ds s;
2204
2205 ds_init(&s);
2206 odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s,
2207 true);
2208 VLOG_ERR("internal error parsing flow mask %s (%s)",
2209 ds_cstr(&s), odp_key_fitness_to_string(fitness));
2210 ds_destroy(&s);
2211 }
2212 }
2213
2214 return EINVAL;
2215 }
2216
2217 return 0;
2218 }
2219
2220 static int
2221 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
2222 struct flow *flow, bool probe)
2223 {
2224 odp_port_t in_port;
2225
2226 if (odp_flow_key_to_flow(key, key_len, flow)) {
2227 if (!probe) {
2228 /* This should not happen: it indicates that
2229 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
2230 * the acceptable form of a flow. Log the problem as an error,
2231 * with enough details to enable debugging. */
2232 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2233
2234 if (!VLOG_DROP_ERR(&rl)) {
2235 struct ds s;
2236
2237 ds_init(&s);
2238 odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
2239 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
2240 ds_destroy(&s);
2241 }
2242 }
2243
2244 return EINVAL;
2245 }
2246
2247 in_port = flow->in_port.odp_port;
2248 if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
2249 return EINVAL;
2250 }
2251
2252 if (flow->ct_state & DP_NETDEV_CS_UNSUPPORTED_MASK) {
2253 return EINVAL;
2254 }
2255
2256 return 0;
2257 }
2258
2259 static int
2260 dpif_netdev_flow_get(const struct dpif *dpif, const struct dpif_flow_get *get)
2261 {
2262 struct dp_netdev *dp = get_dp_netdev(dpif);
2263 struct dp_netdev_flow *netdev_flow;
2264 struct dp_netdev_pmd_thread *pmd;
2265 struct hmapx to_find = HMAPX_INITIALIZER(&to_find);
2266 struct hmapx_node *node;
2267 int error = EINVAL;
2268
2269 if (get->pmd_id == PMD_ID_NULL) {
2270 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
2271 if (dp_netdev_pmd_try_ref(pmd) && !hmapx_add(&to_find, pmd)) {
2272 dp_netdev_pmd_unref(pmd);
2273 }
2274 }
2275 } else {
2276 pmd = dp_netdev_get_pmd(dp, get->pmd_id);
2277 if (!pmd) {
2278 goto out;
2279 }
2280 hmapx_add(&to_find, pmd);
2281 }
2282
2283 if (!hmapx_count(&to_find)) {
2284 goto out;
2285 }
2286
2287 HMAPX_FOR_EACH (node, &to_find) {
2288 pmd = (struct dp_netdev_pmd_thread *) node->data;
2289 netdev_flow = dp_netdev_pmd_find_flow(pmd, get->ufid, get->key,
2290 get->key_len);
2291 if (netdev_flow) {
2292 dp_netdev_flow_to_dpif_flow(netdev_flow, get->buffer, get->buffer,
2293 get->flow, false);
2294 error = 0;
2295 break;
2296 } else {
2297 error = ENOENT;
2298 }
2299 }
2300
2301 HMAPX_FOR_EACH (node, &to_find) {
2302 pmd = (struct dp_netdev_pmd_thread *) node->data;
2303 dp_netdev_pmd_unref(pmd);
2304 }
2305 out:
2306 hmapx_destroy(&to_find);
2307 return error;
2308 }
2309
2310 static struct dp_netdev_flow *
2311 dp_netdev_flow_add(struct dp_netdev_pmd_thread *pmd,
2312 struct match *match, const ovs_u128 *ufid,
2313 const struct nlattr *actions, size_t actions_len)
2314 OVS_REQUIRES(pmd->flow_mutex)
2315 {
2316 struct dp_netdev_flow *flow;
2317 struct netdev_flow_key mask;
2318 struct dpcls *cls;
2319
2320 /* Make sure in_port is exact matched before we read it. */
2321 ovs_assert(match->wc.masks.in_port.odp_port == ODPP_NONE);
2322 odp_port_t in_port = match->flow.in_port.odp_port;
2323
2324 /* As we select the dpcls based on the port number, each netdev flow
2325 * belonging to the same dpcls will have the same odp_port value.
2326 * For performance reasons we wildcard odp_port here in the mask. In the
2327 * typical case dp_hash is also wildcarded, and the resulting 8-byte
2328 * chunk {dp_hash, in_port} will be ignored by netdev_flow_mask_init() and
2329 * will not be part of the subtable mask.
2330 * This will speed up the hash computation during dpcls_lookup() because
2331 * there is one less call to hash_add64() in this case. */
2332 match->wc.masks.in_port.odp_port = 0;
2333 netdev_flow_mask_init(&mask, match);
2334 match->wc.masks.in_port.odp_port = ODPP_NONE;
2335
2336 /* Make sure wc does not have metadata. */
2337 ovs_assert(!FLOWMAP_HAS_FIELD(&mask.mf.map, metadata)
2338 && !FLOWMAP_HAS_FIELD(&mask.mf.map, regs));
2339
2340 /* Do not allocate extra space. */
2341 flow = xmalloc(sizeof *flow - sizeof flow->cr.flow.mf + mask.len);
2342 memset(&flow->stats, 0, sizeof flow->stats);
2343 flow->dead = false;
2344 flow->batch = NULL;
2345 *CONST_CAST(unsigned *, &flow->pmd_id) = pmd->core_id;
2346 *CONST_CAST(struct flow *, &flow->flow) = match->flow;
2347 *CONST_CAST(ovs_u128 *, &flow->ufid) = *ufid;
2348 ovs_refcount_init(&flow->ref_cnt);
2349 ovsrcu_set(&flow->actions, dp_netdev_actions_create(actions, actions_len));
2350
2351 netdev_flow_key_init_masked(&flow->cr.flow, &match->flow, &mask);
2352
2353 /* Select dpcls for in_port. Relies on in_port to be exact match. */
2354 cls = dp_netdev_pmd_find_dpcls(pmd, in_port);
2355 dpcls_insert(cls, &flow->cr, &mask);
2356
2357 cmap_insert(&pmd->flow_table, CONST_CAST(struct cmap_node *, &flow->node),
2358 dp_netdev_flow_hash(&flow->ufid));
2359
2360 if (OVS_UNLIKELY(VLOG_IS_DBG_ENABLED())) {
2361 struct ds ds = DS_EMPTY_INITIALIZER;
2362 struct ofpbuf key_buf, mask_buf;
2363 struct odp_flow_key_parms odp_parms = {
2364 .flow = &match->flow,
2365 .mask = &match->wc.masks,
2366 .support = dp_netdev_support,
2367 };
2368
2369 ofpbuf_init(&key_buf, 0);
2370 ofpbuf_init(&mask_buf, 0);
2371
2372 odp_flow_key_from_flow(&odp_parms, &key_buf);
2373 odp_parms.key_buf = &key_buf;
2374 odp_flow_key_from_mask(&odp_parms, &mask_buf);
2375
2376 ds_put_cstr(&ds, "flow_add: ");
2377 odp_format_ufid(ufid, &ds);
2378 ds_put_cstr(&ds, " ");
2379 odp_flow_format(key_buf.data, key_buf.size,
2380 mask_buf.data, mask_buf.size,
2381 NULL, &ds, false);
2382 ds_put_cstr(&ds, ", actions:");
2383 format_odp_actions(&ds, actions, actions_len);
2384
2385 VLOG_DBG_RL(&upcall_rl, "%s", ds_cstr(&ds));
2386
2387 ofpbuf_uninit(&key_buf);
2388 ofpbuf_uninit(&mask_buf);
2389 ds_destroy(&ds);
2390 }
2391
2392 return flow;
2393 }
2394
2395 static int
2396 flow_put_on_pmd(struct dp_netdev_pmd_thread *pmd,
2397 struct netdev_flow_key *key,
2398 struct match *match,
2399 ovs_u128 *ufid,
2400 const struct dpif_flow_put *put,
2401 struct dpif_flow_stats *stats)
2402 {
2403 struct dp_netdev_flow *netdev_flow;
2404 int error = 0;
2405
2406 if (stats) {
2407 memset(stats, 0, sizeof *stats);
2408 }
2409
2410 ovs_mutex_lock(&pmd->flow_mutex);
2411 netdev_flow = dp_netdev_pmd_lookup_flow(pmd, key, NULL);
2412 if (!netdev_flow) {
2413 if (put->flags & DPIF_FP_CREATE) {
2414 if (cmap_count(&pmd->flow_table) < MAX_FLOWS) {
2415 dp_netdev_flow_add(pmd, match, ufid, put->actions,
2416 put->actions_len);
2417 error = 0;
2418 } else {
2419 error = EFBIG;
2420 }
2421 } else {
2422 error = ENOENT;
2423 }
2424 } else {
2425 if (put->flags & DPIF_FP_MODIFY
2426 && flow_equal(&match->flow, &netdev_flow->flow)) {
2427 struct dp_netdev_actions *new_actions;
2428 struct dp_netdev_actions *old_actions;
2429
2430 new_actions = dp_netdev_actions_create(put->actions,
2431 put->actions_len);
2432
2433 old_actions = dp_netdev_flow_get_actions(netdev_flow);
2434 ovsrcu_set(&netdev_flow->actions, new_actions);
2435
2436 if (stats) {
2437 get_dpif_flow_stats(netdev_flow, stats);
2438 }
2439 if (put->flags & DPIF_FP_ZERO_STATS) {
2440 /* XXX: The userspace datapath uses thread local statistics
2441 * (for flows), which should be updated only by the owning
2442 * thread. Since we cannot write on stats memory here,
2443 * we choose not to support this flag. Please note:
2444 * - This feature is currently used only by dpctl commands with
2445 * option --clear.
2446 * - Should the need arise, this operation can be implemented
2447 * by keeping a base value (to be update here) for each
2448 * counter, and subtracting it before outputting the stats */
2449 error = EOPNOTSUPP;
2450 }
2451
2452 ovsrcu_postpone(dp_netdev_actions_free, old_actions);
2453 } else if (put->flags & DPIF_FP_CREATE) {
2454 error = EEXIST;
2455 } else {
2456 /* Overlapping flow. */
2457 error = EINVAL;
2458 }
2459 }
2460 ovs_mutex_unlock(&pmd->flow_mutex);
2461 return error;
2462 }
2463
2464 static int
2465 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
2466 {
2467 struct dp_netdev *dp = get_dp_netdev(dpif);
2468 struct netdev_flow_key key;
2469 struct dp_netdev_pmd_thread *pmd;
2470 struct match match;
2471 ovs_u128 ufid;
2472 int error;
2473 bool probe = put->flags & DPIF_FP_PROBE;
2474
2475 if (put->stats) {
2476 memset(put->stats, 0, sizeof *put->stats);
2477 }
2478 error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &match.flow,
2479 probe);
2480 if (error) {
2481 return error;
2482 }
2483 error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
2484 put->mask, put->mask_len,
2485 &match.flow, &match.wc, probe);
2486 if (error) {
2487 return error;
2488 }
2489
2490 if (put->ufid) {
2491 ufid = *put->ufid;
2492 } else {
2493 dpif_flow_hash(dpif, &match.flow, sizeof match.flow, &ufid);
2494 }
2495
2496 /* Must produce a netdev_flow_key for lookup.
2497 * This interface is no longer performance critical, since it is not used
2498 * for upcall processing any more. */
2499 netdev_flow_key_from_flow(&key, &match.flow);
2500
2501 if (put->pmd_id == PMD_ID_NULL) {
2502 if (cmap_count(&dp->poll_threads) == 0) {
2503 return EINVAL;
2504 }
2505 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
2506 struct dpif_flow_stats pmd_stats;
2507 int pmd_error;
2508
2509 pmd_error = flow_put_on_pmd(pmd, &key, &match, &ufid, put,
2510 &pmd_stats);
2511 if (pmd_error) {
2512 error = pmd_error;
2513 } else if (put->stats) {
2514 put->stats->n_packets += pmd_stats.n_packets;
2515 put->stats->n_bytes += pmd_stats.n_bytes;
2516 put->stats->used = MAX(put->stats->used, pmd_stats.used);
2517 put->stats->tcp_flags |= pmd_stats.tcp_flags;
2518 }
2519 }
2520 } else {
2521 pmd = dp_netdev_get_pmd(dp, put->pmd_id);
2522 if (!pmd) {
2523 return EINVAL;
2524 }
2525 error = flow_put_on_pmd(pmd, &key, &match, &ufid, put, put->stats);
2526 dp_netdev_pmd_unref(pmd);
2527 }
2528
2529 return error;
2530 }
2531
2532 static int
2533 flow_del_on_pmd(struct dp_netdev_pmd_thread *pmd,
2534 struct dpif_flow_stats *stats,
2535 const struct dpif_flow_del *del)
2536 {
2537 struct dp_netdev_flow *netdev_flow;
2538 int error = 0;
2539
2540 ovs_mutex_lock(&pmd->flow_mutex);
2541 netdev_flow = dp_netdev_pmd_find_flow(pmd, del->ufid, del->key,
2542 del->key_len);
2543 if (netdev_flow) {
2544 if (stats) {
2545 get_dpif_flow_stats(netdev_flow, stats);
2546 }
2547 dp_netdev_pmd_remove_flow(pmd, netdev_flow);
2548 } else {
2549 error = ENOENT;
2550 }
2551 ovs_mutex_unlock(&pmd->flow_mutex);
2552
2553 return error;
2554 }
2555
2556 static int
2557 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
2558 {
2559 struct dp_netdev *dp = get_dp_netdev(dpif);
2560 struct dp_netdev_pmd_thread *pmd;
2561 int error = 0;
2562
2563 if (del->stats) {
2564 memset(del->stats, 0, sizeof *del->stats);
2565 }
2566
2567 if (del->pmd_id == PMD_ID_NULL) {
2568 if (cmap_count(&dp->poll_threads) == 0) {
2569 return EINVAL;
2570 }
2571 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
2572 struct dpif_flow_stats pmd_stats;
2573 int pmd_error;
2574
2575 pmd_error = flow_del_on_pmd(pmd, &pmd_stats, del);
2576 if (pmd_error) {
2577 error = pmd_error;
2578 } else if (del->stats) {
2579 del->stats->n_packets += pmd_stats.n_packets;
2580 del->stats->n_bytes += pmd_stats.n_bytes;
2581 del->stats->used = MAX(del->stats->used, pmd_stats.used);
2582 del->stats->tcp_flags |= pmd_stats.tcp_flags;
2583 }
2584 }
2585 } else {
2586 pmd = dp_netdev_get_pmd(dp, del->pmd_id);
2587 if (!pmd) {
2588 return EINVAL;
2589 }
2590 error = flow_del_on_pmd(pmd, del->stats, del);
2591 dp_netdev_pmd_unref(pmd);
2592 }
2593
2594
2595 return error;
2596 }
2597
2598 struct dpif_netdev_flow_dump {
2599 struct dpif_flow_dump up;
2600 struct cmap_position poll_thread_pos;
2601 struct cmap_position flow_pos;
2602 struct dp_netdev_pmd_thread *cur_pmd;
2603 int status;
2604 struct ovs_mutex mutex;
2605 };
2606
2607 static struct dpif_netdev_flow_dump *
2608 dpif_netdev_flow_dump_cast(struct dpif_flow_dump *dump)
2609 {
2610 return CONTAINER_OF(dump, struct dpif_netdev_flow_dump, up);
2611 }
2612
2613 static struct dpif_flow_dump *
2614 dpif_netdev_flow_dump_create(const struct dpif *dpif_, bool terse)
2615 {
2616 struct dpif_netdev_flow_dump *dump;
2617
2618 dump = xzalloc(sizeof *dump);
2619 dpif_flow_dump_init(&dump->up, dpif_);
2620 dump->up.terse = terse;
2621 ovs_mutex_init(&dump->mutex);
2622
2623 return &dump->up;
2624 }
2625
2626 static int
2627 dpif_netdev_flow_dump_destroy(struct dpif_flow_dump *dump_)
2628 {
2629 struct dpif_netdev_flow_dump *dump = dpif_netdev_flow_dump_cast(dump_);
2630
2631 ovs_mutex_destroy(&dump->mutex);
2632 free(dump);
2633 return 0;
2634 }
2635
2636 struct dpif_netdev_flow_dump_thread {
2637 struct dpif_flow_dump_thread up;
2638 struct dpif_netdev_flow_dump *dump;
2639 struct odputil_keybuf keybuf[FLOW_DUMP_MAX_BATCH];
2640 struct odputil_keybuf maskbuf[FLOW_DUMP_MAX_BATCH];
2641 };
2642
2643 static struct dpif_netdev_flow_dump_thread *
2644 dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
2645 {
2646 return CONTAINER_OF(thread, struct dpif_netdev_flow_dump_thread, up);
2647 }
2648
2649 static struct dpif_flow_dump_thread *
2650 dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump *dump_)
2651 {
2652 struct dpif_netdev_flow_dump *dump = dpif_netdev_flow_dump_cast(dump_);
2653 struct dpif_netdev_flow_dump_thread *thread;
2654
2655 thread = xmalloc(sizeof *thread);
2656 dpif_flow_dump_thread_init(&thread->up, &dump->up);
2657 thread->dump = dump;
2658 return &thread->up;
2659 }
2660
2661 static void
2662 dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
2663 {
2664 struct dpif_netdev_flow_dump_thread *thread
2665 = dpif_netdev_flow_dump_thread_cast(thread_);
2666
2667 free(thread);
2668 }
2669
2670 static int
2671 dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread *thread_,
2672 struct dpif_flow *flows, int max_flows)
2673 {
2674 struct dpif_netdev_flow_dump_thread *thread
2675 = dpif_netdev_flow_dump_thread_cast(thread_);
2676 struct dpif_netdev_flow_dump *dump = thread->dump;
2677 struct dp_netdev_flow *netdev_flows[FLOW_DUMP_MAX_BATCH];
2678 int n_flows = 0;
2679 int i;
2680
2681 ovs_mutex_lock(&dump->mutex);
2682 if (!dump->status) {
2683 struct dpif_netdev *dpif = dpif_netdev_cast(thread->up.dpif);
2684 struct dp_netdev *dp = get_dp_netdev(&dpif->dpif);
2685 struct dp_netdev_pmd_thread *pmd = dump->cur_pmd;
2686 int flow_limit = MIN(max_flows, FLOW_DUMP_MAX_BATCH);
2687
2688 /* First call to dump_next(), extracts the first pmd thread.
2689 * If there is no pmd thread, returns immediately. */
2690 if (!pmd) {
2691 pmd = dp_netdev_pmd_get_next(dp, &dump->poll_thread_pos);
2692 if (!pmd) {
2693 ovs_mutex_unlock(&dump->mutex);
2694 return n_flows;
2695
2696 }
2697 }
2698
2699 do {
2700 for (n_flows = 0; n_flows < flow_limit; n_flows++) {
2701 struct cmap_node *node;
2702
2703 node = cmap_next_position(&pmd->flow_table, &dump->flow_pos);
2704 if (!node) {
2705 break;
2706 }
2707 netdev_flows[n_flows] = CONTAINER_OF(node,
2708 struct dp_netdev_flow,
2709 node);
2710 }
2711 /* When finishing dumping the current pmd thread, moves to
2712 * the next. */
2713 if (n_flows < flow_limit) {
2714 memset(&dump->flow_pos, 0, sizeof dump->flow_pos);
2715 dp_netdev_pmd_unref(pmd);
2716 pmd = dp_netdev_pmd_get_next(dp, &dump->poll_thread_pos);
2717 if (!pmd) {
2718 dump->status = EOF;
2719 break;
2720 }
2721 }
2722 /* Keeps the reference to next caller. */
2723 dump->cur_pmd = pmd;
2724
2725 /* If the current dump is empty, do not exit the loop, since the
2726 * remaining pmds could have flows to be dumped. Just dumps again
2727 * on the new 'pmd'. */
2728 } while (!n_flows);
2729 }
2730 ovs_mutex_unlock(&dump->mutex);
2731
2732 for (i = 0; i < n_flows; i++) {
2733 struct odputil_keybuf *maskbuf = &thread->maskbuf[i];
2734 struct odputil_keybuf *keybuf = &thread->keybuf[i];
2735 struct dp_netdev_flow *netdev_flow = netdev_flows[i];
2736 struct dpif_flow *f = &flows[i];
2737 struct ofpbuf key, mask;
2738
2739 ofpbuf_use_stack(&key, keybuf, sizeof *keybuf);
2740 ofpbuf_use_stack(&mask, maskbuf, sizeof *maskbuf);
2741 dp_netdev_flow_to_dpif_flow(netdev_flow, &key, &mask, f,
2742 dump->up.terse);
2743 }
2744
2745 return n_flows;
2746 }
2747
2748 static int
2749 dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
2750 OVS_NO_THREAD_SAFETY_ANALYSIS
2751 {
2752 struct dp_netdev *dp = get_dp_netdev(dpif);
2753 struct dp_netdev_pmd_thread *pmd;
2754 struct dp_packet_batch pp;
2755
2756 if (dp_packet_size(execute->packet) < ETH_HEADER_LEN ||
2757 dp_packet_size(execute->packet) > UINT16_MAX) {
2758 return EINVAL;
2759 }
2760
2761 /* Tries finding the 'pmd'. If NULL is returned, that means
2762 * the current thread is a non-pmd thread and should use
2763 * dp_netdev_get_pmd(dp, NON_PMD_CORE_ID). */
2764 pmd = ovsthread_getspecific(dp->per_pmd_key);
2765 if (!pmd) {
2766 pmd = dp_netdev_get_pmd(dp, NON_PMD_CORE_ID);
2767 if (!pmd) {
2768 return EBUSY;
2769 }
2770 }
2771
2772 if (execute->probe) {
2773 /* If this is part of a probe, Drop the packet, since executing
2774 * the action may actually cause spurious packets be sent into
2775 * the network. */
2776 return 0;
2777 }
2778
2779 /* If the current thread is non-pmd thread, acquires
2780 * the 'non_pmd_mutex'. */
2781 if (pmd->core_id == NON_PMD_CORE_ID) {
2782 ovs_mutex_lock(&dp->non_pmd_mutex);
2783 }
2784
2785 /* The action processing expects the RSS hash to be valid, because
2786 * it's always initialized at the beginning of datapath processing.
2787 * In this case, though, 'execute->packet' may not have gone through
2788 * the datapath at all, it may have been generated by the upper layer
2789 * (OpenFlow packet-out, BFD frame, ...). */
2790 if (!dp_packet_rss_valid(execute->packet)) {
2791 dp_packet_set_rss_hash(execute->packet,
2792 flow_hash_5tuple(execute->flow, 0));
2793 }
2794
2795 dp_packet_batch_init_packet(&pp, execute->packet);
2796 dp_netdev_execute_actions(pmd, &pp, false, execute->flow,
2797 execute->actions, execute->actions_len,
2798 time_msec());
2799
2800 if (pmd->core_id == NON_PMD_CORE_ID) {
2801 ovs_mutex_unlock(&dp->non_pmd_mutex);
2802 dp_netdev_pmd_unref(pmd);
2803 }
2804
2805 return 0;
2806 }
2807
2808 static void
2809 dpif_netdev_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
2810 {
2811 size_t i;
2812
2813 for (i = 0; i < n_ops; i++) {
2814 struct dpif_op *op = ops[i];
2815
2816 switch (op->type) {
2817 case DPIF_OP_FLOW_PUT:
2818 op->error = dpif_netdev_flow_put(dpif, &op->u.flow_put);
2819 break;
2820
2821 case DPIF_OP_FLOW_DEL:
2822 op->error = dpif_netdev_flow_del(dpif, &op->u.flow_del);
2823 break;
2824
2825 case DPIF_OP_EXECUTE:
2826 op->error = dpif_netdev_execute(dpif, &op->u.execute);
2827 break;
2828
2829 case DPIF_OP_FLOW_GET:
2830 op->error = dpif_netdev_flow_get(dpif, &op->u.flow_get);
2831 break;
2832 }
2833 }
2834 }
2835
2836 /* Applies datapath configuration from the database. Some of the changes are
2837 * actually applied in dpif_netdev_run(). */
2838 static int
2839 dpif_netdev_set_config(struct dpif *dpif, const struct smap *other_config)
2840 {
2841 struct dp_netdev *dp = get_dp_netdev(dpif);
2842 const char *cmask = smap_get(other_config, "pmd-cpu-mask");
2843 unsigned long long insert_prob =
2844 smap_get_ullong(other_config, "emc-insert-inv-prob",
2845 DEFAULT_EM_FLOW_INSERT_INV_PROB);
2846 uint32_t insert_min, cur_min;
2847
2848 if (!nullable_string_is_equal(dp->pmd_cmask, cmask)) {
2849 free(dp->pmd_cmask);
2850 dp->pmd_cmask = nullable_xstrdup(cmask);
2851 dp_netdev_request_reconfigure(dp);
2852 }
2853
2854 atomic_read_relaxed(&dp->emc_insert_min, &cur_min);
2855 if (insert_prob <= UINT32_MAX) {
2856 insert_min = insert_prob == 0 ? 0 : UINT32_MAX / insert_prob;
2857 } else {
2858 insert_min = DEFAULT_EM_FLOW_INSERT_MIN;
2859 insert_prob = DEFAULT_EM_FLOW_INSERT_INV_PROB;
2860 }
2861
2862 if (insert_min != cur_min) {
2863 atomic_store_relaxed(&dp->emc_insert_min, insert_min);
2864 if (insert_min == 0) {
2865 VLOG_INFO("EMC has been disabled");
2866 } else {
2867 VLOG_INFO("EMC insertion probability changed to 1/%llu (~%.2f%%)",
2868 insert_prob, (100 / (float)insert_prob));
2869 }
2870 }
2871
2872 return 0;
2873 }
2874
2875 /* Parses affinity list and returns result in 'core_ids'. */
2876 static int
2877 parse_affinity_list(const char *affinity_list, unsigned *core_ids, int n_rxq)
2878 {
2879 unsigned i;
2880 char *list, *copy, *key, *value;
2881 int error = 0;
2882
2883 for (i = 0; i < n_rxq; i++) {
2884 core_ids[i] = OVS_CORE_UNSPEC;
2885 }
2886
2887 if (!affinity_list) {
2888 return 0;
2889 }
2890
2891 list = copy = xstrdup(affinity_list);
2892
2893 while (ofputil_parse_key_value(&list, &key, &value)) {
2894 int rxq_id, core_id;
2895
2896 if (!str_to_int(key, 0, &rxq_id) || rxq_id < 0
2897 || !str_to_int(value, 0, &core_id) || core_id < 0) {
2898 error = EINVAL;
2899 break;
2900 }
2901
2902 if (rxq_id < n_rxq) {
2903 core_ids[rxq_id] = core_id;
2904 }
2905 }
2906
2907 free(copy);
2908 return error;
2909 }
2910
2911 /* Parses 'affinity_list' and applies configuration if it is valid. */
2912 static int
2913 dpif_netdev_port_set_rxq_affinity(struct dp_netdev_port *port,
2914 const char *affinity_list)
2915 {
2916 unsigned *core_ids, i;
2917 int error = 0;
2918
2919 core_ids = xmalloc(port->n_rxq * sizeof *core_ids);
2920 if (parse_affinity_list(affinity_list, core_ids, port->n_rxq)) {
2921 error = EINVAL;
2922 goto exit;
2923 }
2924
2925 for (i = 0; i < port->n_rxq; i++) {
2926 port->rxqs[i].core_id = core_ids[i];
2927 }
2928
2929 exit:
2930 free(core_ids);
2931 return error;
2932 }
2933
2934 /* Changes the affinity of port's rx queues. The changes are actually applied
2935 * in dpif_netdev_run(). */
2936 static int
2937 dpif_netdev_port_set_config(struct dpif *dpif, odp_port_t port_no,
2938 const struct smap *cfg)
2939 {
2940 struct dp_netdev *dp = get_dp_netdev(dpif);
2941 struct dp_netdev_port *port;
2942 int error = 0;
2943 const char *affinity_list = smap_get(cfg, "pmd-rxq-affinity");
2944
2945 ovs_mutex_lock(&dp->port_mutex);
2946 error = get_port_by_number(dp, port_no, &port);
2947 if (error || !netdev_is_pmd(port->netdev)
2948 || nullable_string_is_equal(affinity_list, port->rxq_affinity_list)) {
2949 goto unlock;
2950 }
2951
2952 error = dpif_netdev_port_set_rxq_affinity(port, affinity_list);
2953 if (error) {
2954 goto unlock;
2955 }
2956 free(port->rxq_affinity_list);
2957 port->rxq_affinity_list = nullable_xstrdup(affinity_list);
2958
2959 dp_netdev_request_reconfigure(dp);
2960 unlock:
2961 ovs_mutex_unlock(&dp->port_mutex);
2962 return error;
2963 }
2964
2965 static int
2966 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
2967 uint32_t queue_id, uint32_t *priority)
2968 {
2969 *priority = queue_id;
2970 return 0;
2971 }
2972
2973 \f
2974 /* Creates and returns a new 'struct dp_netdev_actions', whose actions are
2975 * a copy of the 'ofpacts_len' bytes of 'ofpacts'. */
2976 struct dp_netdev_actions *
2977 dp_netdev_actions_create(const struct nlattr *actions, size_t size)
2978 {
2979 struct dp_netdev_actions *netdev_actions;
2980
2981 netdev_actions = xmalloc(sizeof *netdev_actions + size);
2982 memcpy(netdev_actions->actions, actions, size);
2983 netdev_actions->size = size;
2984
2985 return netdev_actions;
2986 }
2987
2988 struct dp_netdev_actions *
2989 dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow)
2990 {
2991 return ovsrcu_get(struct dp_netdev_actions *, &flow->actions);
2992 }
2993
2994 static void
2995 dp_netdev_actions_free(struct dp_netdev_actions *actions)
2996 {
2997 free(actions);
2998 }
2999 \f
3000 static inline unsigned long long
3001 cycles_counter(void)
3002 {
3003 #ifdef DPDK_NETDEV
3004 return rte_get_tsc_cycles();
3005 #else
3006 return 0;
3007 #endif
3008 }
3009
3010 /* Fake mutex to make sure that the calls to cycles_count_* are balanced */
3011 extern struct ovs_mutex cycles_counter_fake_mutex;
3012
3013 /* Start counting cycles. Must be followed by 'cycles_count_end()' */
3014 static inline void
3015 cycles_count_start(struct dp_netdev_pmd_thread *pmd)
3016 OVS_ACQUIRES(&cycles_counter_fake_mutex)
3017 OVS_NO_THREAD_SAFETY_ANALYSIS
3018 {
3019 pmd->last_cycles = cycles_counter();
3020 }
3021
3022 /* Stop counting cycles and add them to the counter 'type' */
3023 static inline void
3024 cycles_count_end(struct dp_netdev_pmd_thread *pmd,
3025 enum pmd_cycles_counter_type type)
3026 OVS_RELEASES(&cycles_counter_fake_mutex)
3027 OVS_NO_THREAD_SAFETY_ANALYSIS
3028 {
3029 unsigned long long interval = cycles_counter() - pmd->last_cycles;
3030
3031 non_atomic_ullong_add(&pmd->cycles.n[type], interval);
3032 }
3033
3034 static void
3035 dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread *pmd,
3036 struct netdev_rxq *rx,
3037 odp_port_t port_no)
3038 {
3039 struct dp_packet_batch batch;
3040 int error;
3041
3042 dp_packet_batch_init(&batch);
3043 cycles_count_start(pmd);
3044 error = netdev_rxq_recv(rx, &batch);
3045 cycles_count_end(pmd, PMD_CYCLES_POLLING);
3046 if (!error) {
3047 *recirc_depth_get() = 0;
3048
3049 cycles_count_start(pmd);
3050 dp_netdev_input(pmd, &batch, port_no);
3051 cycles_count_end(pmd, PMD_CYCLES_PROCESSING);
3052 } else if (error != EAGAIN && error != EOPNOTSUPP) {
3053 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3054
3055 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
3056 netdev_rxq_get_name(rx), ovs_strerror(error));
3057 }
3058 }
3059
3060 static struct tx_port *
3061 tx_port_lookup(const struct hmap *hmap, odp_port_t port_no)
3062 {
3063 struct tx_port *tx;
3064
3065 HMAP_FOR_EACH_IN_BUCKET (tx, node, hash_port_no(port_no), hmap) {
3066 if (tx->port->port_no == port_no) {
3067 return tx;
3068 }
3069 }
3070
3071 return NULL;
3072 }
3073
3074 static int
3075 port_reconfigure(struct dp_netdev_port *port)
3076 {
3077 struct netdev *netdev = port->netdev;
3078 int i, err;
3079
3080 port->need_reconfigure = false;
3081
3082 /* Closes the existing 'rxq's. */
3083 for (i = 0; i < port->n_rxq; i++) {
3084 netdev_rxq_close(port->rxqs[i].rx);
3085 port->rxqs[i].rx = NULL;
3086 }
3087 port->n_rxq = 0;
3088
3089 /* Allows 'netdev' to apply the pending configuration changes. */
3090 if (netdev_is_reconf_required(netdev)) {
3091 err = netdev_reconfigure(netdev);
3092 if (err && (err != EOPNOTSUPP)) {
3093 VLOG_ERR("Failed to set interface %s new configuration",
3094 netdev_get_name(netdev));
3095 return err;
3096 }
3097 }
3098 /* If the netdev_reconfigure() above succeeds, reopens the 'rxq's. */
3099 port->rxqs = xrealloc(port->rxqs,
3100 sizeof *port->rxqs * netdev_n_rxq(netdev));
3101 /* Realloc 'used' counters for tx queues. */
3102 free(port->txq_used);
3103 port->txq_used = xcalloc(netdev_n_txq(netdev), sizeof *port->txq_used);
3104
3105 for (i = 0; i < netdev_n_rxq(netdev); i++) {
3106 port->rxqs[i].port = port;
3107 err = netdev_rxq_open(netdev, &port->rxqs[i].rx, i);
3108 if (err) {
3109 return err;
3110 }
3111 port->n_rxq++;
3112 }
3113
3114 /* Parse affinity list to apply configuration for new queues. */
3115 dpif_netdev_port_set_rxq_affinity(port, port->rxq_affinity_list);
3116
3117 return 0;
3118 }
3119
3120 struct rr_numa_list {
3121 struct hmap numas; /* Contains 'struct rr_numa' */
3122 };
3123
3124 struct rr_numa {
3125 struct hmap_node node;
3126
3127 int numa_id;
3128
3129 /* Non isolated pmds on numa node 'numa_id' */
3130 struct dp_netdev_pmd_thread **pmds;
3131 int n_pmds;
3132
3133 int cur_index;
3134 };
3135
3136 static struct rr_numa *
3137 rr_numa_list_lookup(struct rr_numa_list *rr, int numa_id)
3138 {
3139 struct rr_numa *numa;
3140
3141 HMAP_FOR_EACH_WITH_HASH (numa, node, hash_int(numa_id, 0), &rr->numas) {
3142 if (numa->numa_id == numa_id) {
3143 return numa;
3144 }
3145 }
3146
3147 return NULL;
3148 }
3149
3150 static void
3151 rr_numa_list_populate(struct dp_netdev *dp, struct rr_numa_list *rr)
3152 {
3153 struct dp_netdev_pmd_thread *pmd;
3154 struct rr_numa *numa;
3155
3156 hmap_init(&rr->numas);
3157
3158 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3159 if (pmd->core_id == NON_PMD_CORE_ID || pmd->isolated) {
3160 continue;
3161 }
3162
3163 numa = rr_numa_list_lookup(rr, pmd->numa_id);
3164 if (!numa) {
3165 numa = xzalloc(sizeof *numa);
3166 numa->numa_id = pmd->numa_id;
3167 hmap_insert(&rr->numas, &numa->node, hash_int(pmd->numa_id, 0));
3168 }
3169 numa->n_pmds++;
3170 numa->pmds = xrealloc(numa->pmds, numa->n_pmds * sizeof *numa->pmds);
3171 numa->pmds[numa->n_pmds - 1] = pmd;
3172 }
3173 }
3174
3175 static struct dp_netdev_pmd_thread *
3176 rr_numa_get_pmd(struct rr_numa *numa)
3177 {
3178 return numa->pmds[numa->cur_index++ % numa->n_pmds];
3179 }
3180
3181 static void
3182 rr_numa_list_destroy(struct rr_numa_list *rr)
3183 {
3184 struct rr_numa *numa;
3185
3186 HMAP_FOR_EACH_POP (numa, node, &rr->numas) {
3187 free(numa->pmds);
3188 free(numa);
3189 }
3190 hmap_destroy(&rr->numas);
3191 }
3192
3193 /* Assign pmds to queues. If 'pinned' is true, assign pmds to pinned
3194 * queues and marks the pmds as isolated. Otherwise, assign non isolated
3195 * pmds to unpinned queues.
3196 *
3197 * The function doesn't touch the pmd threads, it just stores the assignment
3198 * in the 'pmd' member of each rxq. */
3199 static void
3200 rxq_scheduling(struct dp_netdev *dp, bool pinned) OVS_REQUIRES(dp->port_mutex)
3201 {
3202 struct dp_netdev_port *port;
3203 struct rr_numa_list rr;
3204
3205 rr_numa_list_populate(dp, &rr);
3206
3207 HMAP_FOR_EACH (port, node, &dp->ports) {
3208 struct rr_numa *numa;
3209 int numa_id;
3210
3211 if (!netdev_is_pmd(port->netdev)) {
3212 continue;
3213 }
3214
3215 numa_id = netdev_get_numa_id(port->netdev);
3216 numa = rr_numa_list_lookup(&rr, numa_id);
3217
3218 for (int qid = 0; qid < port->n_rxq; qid++) {
3219 struct dp_netdev_rxq *q = &port->rxqs[qid];
3220
3221 if (pinned && q->core_id != OVS_CORE_UNSPEC) {
3222 struct dp_netdev_pmd_thread *pmd;
3223
3224 pmd = dp_netdev_get_pmd(dp, q->core_id);
3225 if (!pmd) {
3226 VLOG_WARN("There is no PMD thread on core %d. Queue "
3227 "%d on port \'%s\' will not be polled.",
3228 q->core_id, qid, netdev_get_name(port->netdev));
3229 } else {
3230 q->pmd = pmd;
3231 pmd->isolated = true;
3232 dp_netdev_pmd_unref(pmd);
3233 }
3234 } else if (!pinned && q->core_id == OVS_CORE_UNSPEC) {
3235 if (!numa) {
3236 VLOG_WARN("There's no available (non isolated) pmd thread "
3237 "on numa node %d. Queue %d on port \'%s\' will "
3238 "not be polled.",
3239 numa_id, qid, netdev_get_name(port->netdev));
3240 } else {
3241 q->pmd = rr_numa_get_pmd(numa);
3242 }
3243 }
3244 }
3245 }
3246
3247 rr_numa_list_destroy(&rr);
3248 }
3249
3250 static void
3251 reconfigure_pmd_threads(struct dp_netdev *dp)
3252 OVS_REQUIRES(dp->port_mutex)
3253 {
3254 struct dp_netdev_pmd_thread *pmd;
3255 struct ovs_numa_dump *pmd_cores;
3256 bool changed = false;
3257
3258 /* The pmd threads should be started only if there's a pmd port in the
3259 * datapath. If the user didn't provide any "pmd-cpu-mask", we start
3260 * NR_PMD_THREADS per numa node. */
3261 if (!has_pmd_port(dp)) {
3262 pmd_cores = ovs_numa_dump_n_cores_per_numa(0);
3263 } else if (dp->pmd_cmask && dp->pmd_cmask[0]) {
3264 pmd_cores = ovs_numa_dump_cores_with_cmask(dp->pmd_cmask);
3265 } else {
3266 pmd_cores = ovs_numa_dump_n_cores_per_numa(NR_PMD_THREADS);
3267 }
3268
3269 /* Check for changed configuration */
3270 if (ovs_numa_dump_count(pmd_cores) != cmap_count(&dp->poll_threads) - 1) {
3271 changed = true;
3272 } else {
3273 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3274 if (pmd->core_id != NON_PMD_CORE_ID
3275 && !ovs_numa_dump_contains_core(pmd_cores,
3276 pmd->numa_id,
3277 pmd->core_id)) {
3278 changed = true;
3279 break;
3280 }
3281 }
3282 }
3283
3284 /* Destroy the old and recreate the new pmd threads. We don't perform an
3285 * incremental update because we would have to adjust 'static_tx_qid'. */
3286 if (changed) {
3287 struct ovs_numa_info_core *core;
3288 struct ovs_numa_info_numa *numa;
3289
3290 /* Do not destroy the non pmd thread. */
3291 dp_netdev_destroy_all_pmds(dp, false);
3292 FOR_EACH_CORE_ON_DUMP (core, pmd_cores) {
3293 struct dp_netdev_pmd_thread *pmd = xzalloc(sizeof *pmd);
3294
3295 dp_netdev_configure_pmd(pmd, dp, core->core_id, core->numa_id);
3296
3297 pmd->thread = ovs_thread_create("pmd", pmd_thread_main, pmd);
3298 }
3299
3300 /* Log the number of pmd threads per numa node. */
3301 FOR_EACH_NUMA_ON_DUMP (numa, pmd_cores) {
3302 VLOG_INFO("Created %"PRIuSIZE" pmd threads on numa node %d",
3303 numa->n_cores, numa->numa_id);
3304 }
3305 }
3306
3307 ovs_numa_dump_destroy(pmd_cores);
3308 }
3309
3310 static void
3311 reload_affected_pmds(struct dp_netdev *dp)
3312 {
3313 struct dp_netdev_pmd_thread *pmd;
3314
3315 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3316 if (pmd->need_reload) {
3317 dp_netdev_reload_pmd__(pmd);
3318 pmd->need_reload = false;
3319 }
3320 }
3321 }
3322
3323 static void
3324 pmd_remove_stale_ports(struct dp_netdev *dp,
3325 struct dp_netdev_pmd_thread *pmd)
3326 OVS_EXCLUDED(pmd->port_mutex)
3327 OVS_REQUIRES(dp->port_mutex)
3328 {
3329 struct rxq_poll *poll, *poll_next;
3330 struct tx_port *tx, *tx_next;
3331
3332 ovs_mutex_lock(&pmd->port_mutex);
3333 HMAP_FOR_EACH_SAFE (poll, poll_next, node, &pmd->poll_list) {
3334 struct dp_netdev_port *port = poll->rxq->port;
3335
3336 if (port->need_reconfigure
3337 || !hmap_contains(&dp->ports, &port->node)) {
3338 dp_netdev_del_rxq_from_pmd(pmd, poll);
3339 }
3340 }
3341 HMAP_FOR_EACH_SAFE (tx, tx_next, node, &pmd->tx_ports) {
3342 struct dp_netdev_port *port = tx->port;
3343
3344 if (port->need_reconfigure
3345 || !hmap_contains(&dp->ports, &port->node)) {
3346 dp_netdev_del_port_tx_from_pmd(pmd, tx);
3347 }
3348 }
3349 ovs_mutex_unlock(&pmd->port_mutex);
3350 }
3351
3352 /* Must be called each time a port is added/removed or the cmask changes.
3353 * This creates and destroys pmd threads, reconfigures ports, opens their
3354 * rxqs and assigns all rxqs/txqs to pmd threads. */
3355 static void
3356 reconfigure_datapath(struct dp_netdev *dp)
3357 OVS_REQUIRES(dp->port_mutex)
3358 {
3359 struct dp_netdev_pmd_thread *pmd;
3360 struct dp_netdev_port *port;
3361 int wanted_txqs;
3362
3363 dp->last_reconfigure_seq = seq_read(dp->reconfigure_seq);
3364
3365 /* Step 1: Adjust the pmd threads based on the datapath ports, the cores
3366 * on the system and the user configuration. */
3367 reconfigure_pmd_threads(dp);
3368
3369 wanted_txqs = cmap_count(&dp->poll_threads);
3370
3371 /* The number of pmd threads might have changed, or a port can be new:
3372 * adjust the txqs. */
3373 HMAP_FOR_EACH (port, node, &dp->ports) {
3374 netdev_set_tx_multiq(port->netdev, wanted_txqs);
3375 }
3376
3377 /* Step 2: Remove from the pmd threads ports that have been removed or
3378 * need reconfiguration. */
3379
3380 /* Check for all the ports that need reconfiguration. We cache this in
3381 * 'port->reconfigure', because netdev_is_reconf_required() can change at
3382 * any time. */
3383 HMAP_FOR_EACH (port, node, &dp->ports) {
3384 if (netdev_is_reconf_required(port->netdev)) {
3385 port->need_reconfigure = true;
3386 }
3387 }
3388
3389 /* Remove from the pmd threads all the ports that have been deleted or
3390 * need reconfiguration. */
3391 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3392 pmd_remove_stale_ports(dp, pmd);
3393 }
3394
3395 /* Reload affected pmd threads. We must wait for the pmd threads before
3396 * reconfiguring the ports, because a port cannot be reconfigured while
3397 * it's being used. */
3398 reload_affected_pmds(dp);
3399
3400 /* Step 3: Reconfigure ports. */
3401
3402 /* We only reconfigure the ports that we determined above, because they're
3403 * not being used by any pmd thread at the moment. If a port fails to
3404 * reconfigure we remove it from the datapath. */
3405 HMAP_FOR_EACH (port, node, &dp->ports) {
3406 int err;
3407
3408 if (!port->need_reconfigure) {
3409 continue;
3410 }
3411
3412 err = port_reconfigure(port);
3413 if (err) {
3414 hmap_remove(&dp->ports, &port->node);
3415 seq_change(dp->port_seq);
3416 port_destroy(port);
3417 } else {
3418 port->dynamic_txqs = netdev_n_txq(port->netdev) < wanted_txqs;
3419 }
3420 }
3421
3422 /* Step 4: Compute new rxq scheduling. We don't touch the pmd threads
3423 * for now, we just update the 'pmd' pointer in each rxq to point to the
3424 * wanted thread according to the scheduling policy. */
3425
3426 /* Reset all the pmd threads to non isolated. */
3427 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3428 pmd->isolated = false;
3429 }
3430
3431 /* Reset all the queues to unassigned */
3432 HMAP_FOR_EACH (port, node, &dp->ports) {
3433 for (int i = 0; i < port->n_rxq; i++) {
3434 port->rxqs[i].pmd = NULL;
3435 }
3436 }
3437
3438 /* Add pinned queues and mark pmd threads isolated. */
3439 rxq_scheduling(dp, true);
3440
3441 /* Add non-pinned queues. */
3442 rxq_scheduling(dp, false);
3443
3444 /* Step 5: Remove queues not compliant with new scheduling. */
3445 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3446 struct rxq_poll *poll, *poll_next;
3447
3448 ovs_mutex_lock(&pmd->port_mutex);
3449 HMAP_FOR_EACH_SAFE (poll, poll_next, node, &pmd->poll_list) {
3450 if (poll->rxq->pmd != pmd) {
3451 dp_netdev_del_rxq_from_pmd(pmd, poll);
3452 }
3453 }
3454 ovs_mutex_unlock(&pmd->port_mutex);
3455 }
3456
3457 /* Reload affected pmd threads. We must wait for the pmd threads to remove
3458 * the old queues before readding them, otherwise a queue can be polled by
3459 * two threads at the same time. */
3460 reload_affected_pmds(dp);
3461
3462 /* Step 6: Add queues from scheduling, if they're not there already. */
3463 HMAP_FOR_EACH (port, node, &dp->ports) {
3464 if (!netdev_is_pmd(port->netdev)) {
3465 continue;
3466 }
3467
3468 for (int qid = 0; qid < port->n_rxq; qid++) {
3469 struct dp_netdev_rxq *q = &port->rxqs[qid];
3470
3471 if (q->pmd) {
3472 ovs_mutex_lock(&q->pmd->port_mutex);
3473 dp_netdev_add_rxq_to_pmd(q->pmd, q);
3474 ovs_mutex_unlock(&q->pmd->port_mutex);
3475 }
3476 }
3477 }
3478
3479 /* Add every port to the tx cache of every pmd thread, if it's not
3480 * there already and if this pmd has at least one rxq to poll. */
3481 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
3482 ovs_mutex_lock(&pmd->port_mutex);
3483 if (hmap_count(&pmd->poll_list) || pmd->core_id == NON_PMD_CORE_ID) {
3484 HMAP_FOR_EACH (port, node, &dp->ports) {
3485 dp_netdev_add_port_tx_to_pmd(pmd, port);
3486 }
3487 }
3488 ovs_mutex_unlock(&pmd->port_mutex);
3489 }
3490
3491 /* Reload affected pmd threads. */
3492 reload_affected_pmds(dp);
3493 }
3494
3495 /* Returns true if one of the netdevs in 'dp' requires a reconfiguration */
3496 static bool
3497 ports_require_restart(const struct dp_netdev *dp)
3498 OVS_REQUIRES(dp->port_mutex)
3499 {
3500 struct dp_netdev_port *port;
3501
3502 HMAP_FOR_EACH (port, node, &dp->ports) {
3503 if (netdev_is_reconf_required(port->netdev)) {
3504 return true;
3505 }
3506 }
3507
3508 return false;
3509 }
3510
3511 /* Return true if needs to revalidate datapath flows. */
3512 static bool
3513 dpif_netdev_run(struct dpif *dpif)
3514 {
3515 struct dp_netdev_port *port;
3516 struct dp_netdev *dp = get_dp_netdev(dpif);
3517 struct dp_netdev_pmd_thread *non_pmd;
3518 uint64_t new_tnl_seq;
3519
3520 ovs_mutex_lock(&dp->port_mutex);
3521 non_pmd = dp_netdev_get_pmd(dp, NON_PMD_CORE_ID);
3522 if (non_pmd) {
3523 ovs_mutex_lock(&dp->non_pmd_mutex);
3524 HMAP_FOR_EACH (port, node, &dp->ports) {
3525 if (!netdev_is_pmd(port->netdev)) {
3526 int i;
3527
3528 for (i = 0; i < port->n_rxq; i++) {
3529 dp_netdev_process_rxq_port(non_pmd, port->rxqs[i].rx,
3530 port->port_no);
3531 }
3532 }
3533 }
3534 dpif_netdev_xps_revalidate_pmd(non_pmd, time_msec(), false);
3535 ovs_mutex_unlock(&dp->non_pmd_mutex);
3536
3537 dp_netdev_pmd_unref(non_pmd);
3538 }
3539
3540 if (dp_netdev_is_reconf_required(dp) || ports_require_restart(dp)) {
3541 reconfigure_datapath(dp);
3542 }
3543 ovs_mutex_unlock(&dp->port_mutex);
3544
3545 tnl_neigh_cache_run();
3546 tnl_port_map_run();
3547 new_tnl_seq = seq_read(tnl_conf_seq);
3548
3549 if (dp->last_tnl_conf_seq != new_tnl_seq) {
3550 dp->last_tnl_conf_seq = new_tnl_seq;
3551 return true;
3552 }
3553 return false;
3554 }
3555
3556 static void
3557 dpif_netdev_wait(struct dpif *dpif)
3558 {
3559 struct dp_netdev_port *port;
3560 struct dp_netdev *dp = get_dp_netdev(dpif);
3561
3562 ovs_mutex_lock(&dp_netdev_mutex);
3563 ovs_mutex_lock(&dp->port_mutex);
3564 HMAP_FOR_EACH (port, node, &dp->ports) {
3565 netdev_wait_reconf_required(port->netdev);
3566 if (!netdev_is_pmd(port->netdev)) {
3567 int i;
3568
3569 for (i = 0; i < port->n_rxq; i++) {
3570 netdev_rxq_wait(port->rxqs[i].rx);
3571 }
3572 }
3573 }
3574 ovs_mutex_unlock(&dp->port_mutex);
3575 ovs_mutex_unlock(&dp_netdev_mutex);
3576 seq_wait(tnl_conf_seq, dp->last_tnl_conf_seq);
3577 }
3578
3579 static void
3580 pmd_free_cached_ports(struct dp_netdev_pmd_thread *pmd)
3581 {
3582 struct tx_port *tx_port_cached;
3583
3584 /* Free all used tx queue ids. */
3585 dpif_netdev_xps_revalidate_pmd(pmd, 0, true);
3586
3587 HMAP_FOR_EACH_POP (tx_port_cached, node, &pmd->tnl_port_cache) {
3588 free(tx_port_cached);
3589 }
3590 HMAP_FOR_EACH_POP (tx_port_cached, node, &pmd->send_port_cache) {
3591 free(tx_port_cached);
3592 }
3593 }
3594
3595 /* Copies ports from 'pmd->tx_ports' (shared with the main thread) to
3596 * 'pmd->port_cache' (thread local) */
3597 static void
3598 pmd_load_cached_ports(struct dp_netdev_pmd_thread *pmd)
3599 OVS_REQUIRES(pmd->port_mutex)
3600 {
3601 struct tx_port *tx_port, *tx_port_cached;
3602
3603 pmd_free_cached_ports(pmd);
3604 hmap_shrink(&pmd->send_port_cache);
3605 hmap_shrink(&pmd->tnl_port_cache);
3606
3607 HMAP_FOR_EACH (tx_port, node, &pmd->tx_ports) {
3608 if (netdev_has_tunnel_push_pop(tx_port->port->netdev)) {
3609 tx_port_cached = xmemdup(tx_port, sizeof *tx_port_cached);
3610 hmap_insert(&pmd->tnl_port_cache, &tx_port_cached->node,
3611 hash_port_no(tx_port_cached->port->port_no));
3612 }
3613
3614 if (netdev_n_txq(tx_port->port->netdev)) {
3615 tx_port_cached = xmemdup(tx_port, sizeof *tx_port_cached);
3616 hmap_insert(&pmd->send_port_cache, &tx_port_cached->node,
3617 hash_port_no(tx_port_cached->port->port_no));
3618 }
3619 }
3620 }
3621
3622 static int
3623 pmd_load_queues_and_ports(struct dp_netdev_pmd_thread *pmd,
3624 struct polled_queue **ppoll_list)
3625 {
3626 struct polled_queue *poll_list = *ppoll_list;
3627 struct rxq_poll *poll;
3628 int i;
3629
3630 ovs_mutex_lock(&pmd->port_mutex);
3631 poll_list = xrealloc(poll_list, hmap_count(&pmd->poll_list)
3632 * sizeof *poll_list);
3633
3634 i = 0;
3635 HMAP_FOR_EACH (poll, node, &pmd->poll_list) {
3636 poll_list[i].rx = poll->rxq->rx;
3637 poll_list[i].port_no = poll->rxq->port->port_no;
3638 i++;
3639 }
3640
3641 pmd_load_cached_ports(pmd);
3642
3643 ovs_mutex_unlock(&pmd->port_mutex);
3644
3645 *ppoll_list = poll_list;
3646 return i;
3647 }
3648
3649 static void *
3650 pmd_thread_main(void *f_)
3651 {
3652 struct dp_netdev_pmd_thread *pmd = f_;
3653 unsigned int lc = 0;
3654 struct polled_queue *poll_list;
3655 bool exiting;
3656 int poll_cnt;
3657 int i;
3658
3659 poll_list = NULL;
3660
3661 /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */
3662 ovsthread_setspecific(pmd->dp->per_pmd_key, pmd);
3663 ovs_numa_thread_setaffinity_core(pmd->core_id);
3664 dpdk_set_lcore_id(pmd->core_id);
3665 poll_cnt = pmd_load_queues_and_ports(pmd, &poll_list);
3666 reload:
3667 emc_cache_init(&pmd->flow_cache);
3668
3669 /* List port/core affinity */
3670 for (i = 0; i < poll_cnt; i++) {
3671 VLOG_DBG("Core %d processing port \'%s\' with queue-id %d\n",
3672 pmd->core_id, netdev_rxq_get_name(poll_list[i].rx),
3673 netdev_rxq_get_queue_id(poll_list[i].rx));
3674 }
3675
3676 if (!poll_cnt) {
3677 while (seq_read(pmd->reload_seq) == pmd->last_reload_seq) {
3678 seq_wait(pmd->reload_seq, pmd->last_reload_seq);
3679 poll_block();
3680 }
3681 lc = UINT_MAX;
3682 }
3683
3684 for (;;) {
3685 for (i = 0; i < poll_cnt; i++) {
3686 dp_netdev_process_rxq_port(pmd, poll_list[i].rx,
3687 poll_list[i].port_no);
3688 }
3689
3690 if (lc++ > 1024) {
3691 bool reload;
3692
3693 lc = 0;
3694
3695 coverage_try_clear();
3696 dp_netdev_pmd_try_optimize(pmd);
3697 if (!ovsrcu_try_quiesce()) {
3698 emc_cache_slow_sweep(&pmd->flow_cache);
3699 }
3700
3701 atomic_read_relaxed(&pmd->reload, &reload);
3702 if (reload) {
3703 break;
3704 }
3705 }
3706 }
3707
3708 poll_cnt = pmd_load_queues_and_ports(pmd, &poll_list);
3709 exiting = latch_is_set(&pmd->exit_latch);
3710 /* Signal here to make sure the pmd finishes
3711 * reloading the updated configuration. */
3712 dp_netdev_pmd_reload_done(pmd);
3713
3714 emc_cache_uninit(&pmd->flow_cache);
3715
3716 if (!exiting) {
3717 goto reload;
3718 }
3719
3720 free(poll_list);
3721 pmd_free_cached_ports(pmd);
3722 return NULL;
3723 }
3724
3725 static void
3726 dp_netdev_disable_upcall(struct dp_netdev *dp)
3727 OVS_ACQUIRES(dp->upcall_rwlock)
3728 {
3729 fat_rwlock_wrlock(&dp->upcall_rwlock);
3730 }
3731
3732 \f
3733 /* Meters */
3734 static void
3735 dpif_netdev_meter_get_features(const struct dpif * dpif OVS_UNUSED,
3736 struct ofputil_meter_features *features)
3737 {
3738 features->max_meters = MAX_METERS;
3739 features->band_types = DP_SUPPORTED_METER_BAND_TYPES;
3740 features->capabilities = DP_SUPPORTED_METER_FLAGS_MASK;
3741 features->max_bands = MAX_BANDS;
3742 features->max_color = 0;
3743 }
3744
3745 /* Returns false when packet needs to be dropped. */
3746 static void
3747 dp_netdev_run_meter(struct dp_netdev *dp, struct dp_packet_batch *packets_,
3748 uint32_t meter_id, long long int now)
3749 {
3750 struct dp_meter *meter;
3751 struct dp_meter_band *band;
3752 long long int long_delta_t; /* msec */
3753 uint32_t delta_t; /* msec */
3754 int i;
3755 int cnt = packets_->count;
3756 uint32_t bytes, volume;
3757 int exceeded_band[NETDEV_MAX_BURST];
3758 uint32_t exceeded_rate[NETDEV_MAX_BURST];
3759 int exceeded_pkt = cnt; /* First packet that exceeded a band rate. */
3760
3761 if (meter_id >= MAX_METERS) {
3762 return;
3763 }
3764
3765 meter_lock(dp, meter_id);
3766 meter = dp->meters[meter_id];
3767 if (!meter) {
3768 goto out;
3769 }
3770
3771 /* Initialize as negative values. */
3772 memset(exceeded_band, 0xff, cnt * sizeof *exceeded_band);
3773 /* Initialize as zeroes. */
3774 memset(exceeded_rate, 0, cnt * sizeof *exceeded_rate);
3775
3776 /* All packets will hit the meter at the same time. */
3777 long_delta_t = (now - meter->used); /* msec */
3778
3779 /* Make sure delta_t will not be too large, so that bucket will not
3780 * wrap around below. */
3781 delta_t = (long_delta_t > (long long int)meter->max_delta_t)
3782 ? meter->max_delta_t : (uint32_t)long_delta_t;
3783
3784 /* Update meter stats. */
3785 meter->used = now;
3786 meter->packet_count += cnt;
3787 bytes = 0;
3788 for (i = 0; i < cnt; i++) {
3789 bytes += dp_packet_size(packets_->packets[i]);
3790 }
3791 meter->byte_count += bytes;
3792
3793 /* Meters can operate in terms of packets per second or kilobits per
3794 * second. */
3795 if (meter->flags & OFPMF13_PKTPS) {
3796 /* Rate in packets/second, bucket 1/1000 packets. */
3797 /* msec * packets/sec = 1/1000 packets. */
3798 volume = cnt * 1000; /* Take 'cnt' packets from the bucket. */
3799 } else {
3800 /* Rate in kbps, bucket in bits. */
3801 /* msec * kbps = bits */
3802 volume = bytes * 8;
3803 }
3804
3805 /* Update all bands and find the one hit with the highest rate for each
3806 * packet (if any). */
3807 for (int m = 0; m < meter->n_bands; ++m) {
3808 band = &meter->bands[m];
3809
3810 /* Update band's bucket. */
3811 band->bucket += delta_t * band->up.rate;
3812 if (band->bucket > band->up.burst_size) {
3813 band->bucket = band->up.burst_size;
3814 }
3815
3816 /* Drain the bucket for all the packets, if possible. */
3817 if (band->bucket >= volume) {
3818 band->bucket -= volume;
3819 } else {
3820 int band_exceeded_pkt;
3821
3822 /* Band limit hit, must process packet-by-packet. */
3823 if (meter->flags & OFPMF13_PKTPS) {
3824 band_exceeded_pkt = band->bucket / 1000;
3825 band->bucket %= 1000; /* Remainder stays in bucket. */
3826
3827 /* Update the exceeding band for each exceeding packet.
3828 * (Only one band will be fired by a packet, and that
3829 * can be different for each packet.) */
3830 for (i = band_exceeded_pkt; i < cnt; i++) {
3831 if (band->up.rate > exceeded_rate[i]) {
3832 exceeded_rate[i] = band->up.rate;
3833 exceeded_band[i] = m;
3834 }
3835 }
3836 } else {
3837 /* Packet sizes differ, must process one-by-one. */
3838 band_exceeded_pkt = cnt;
3839 for (i = 0; i < cnt; i++) {
3840 uint32_t bits = dp_packet_size(packets_->packets[i]) * 8;
3841
3842 if (band->bucket >= bits) {
3843 band->bucket -= bits;
3844 } else {
3845 if (i < band_exceeded_pkt) {
3846 band_exceeded_pkt = i;
3847 }
3848 /* Update the exceeding band for the exceeding packet.
3849 * (Only one band will be fired by a packet, and that
3850 * can be different for each packet.) */
3851 if (band->up.rate > exceeded_rate[i]) {
3852 exceeded_rate[i] = band->up.rate;
3853 exceeded_band[i] = m;
3854 }
3855 }
3856 }
3857 }
3858 /* Remember the first exceeding packet. */
3859 if (exceeded_pkt > band_exceeded_pkt) {
3860 exceeded_pkt = band_exceeded_pkt;
3861 }
3862 }
3863 }
3864
3865 /* Fire the highest rate band exceeded by each packet.
3866 * Drop packets if needed, by swapping packet to the end that will be
3867 * ignored. */
3868 const size_t size = dp_packet_batch_size(packets_);
3869 struct dp_packet *packet;
3870 size_t j;
3871 DP_PACKET_BATCH_REFILL_FOR_EACH (j, size, packet, packets_) {
3872 if (exceeded_band[j] >= 0) {
3873 /* Meter drop packet. */
3874 band = &meter->bands[exceeded_band[j]];
3875 band->packet_count += 1;
3876 band->byte_count += dp_packet_size(packet);
3877
3878 dp_packet_delete(packet);
3879 } else {
3880 /* Meter accepts packet. */
3881 dp_packet_batch_refill(packets_, packet, j);
3882 }
3883 }
3884 out:
3885 meter_unlock(dp, meter_id);
3886 }
3887
3888 /* Meter set/get/del processing is still single-threaded. */
3889 static int
3890 dpif_netdev_meter_set(struct dpif *dpif, ofproto_meter_id *meter_id,
3891 struct ofputil_meter_config *config)
3892 {
3893 struct dp_netdev *dp = get_dp_netdev(dpif);
3894 uint32_t mid = meter_id->uint32;
3895 struct dp_meter *meter;
3896 int i;
3897
3898 if (mid >= MAX_METERS) {
3899 return EFBIG; /* Meter_id out of range. */
3900 }
3901
3902 if (config->flags & ~DP_SUPPORTED_METER_FLAGS_MASK ||
3903 !(config->flags & (OFPMF13_KBPS | OFPMF13_PKTPS))) {
3904 return EBADF; /* Unsupported flags set */
3905 }
3906 /* Validate bands */
3907 if (config->n_bands == 0 || config->n_bands > MAX_BANDS) {
3908 return EINVAL; /* Too many bands */
3909 }
3910 for (i = 0; i < config->n_bands; ++i) {
3911 switch (config->bands[i].type) {
3912 case OFPMBT13_DROP:
3913 break;
3914 default:
3915 return ENODEV; /* Unsupported band type */
3916 }
3917 }
3918
3919 /* Allocate meter */
3920 meter = xzalloc(sizeof *meter
3921 + config->n_bands * sizeof(struct dp_meter_band));
3922 if (meter) {
3923 meter->flags = config->flags;
3924 meter->n_bands = config->n_bands;
3925 meter->max_delta_t = 0;
3926 meter->used = time_msec();
3927
3928 /* set up bands */
3929 for (i = 0; i < config->n_bands; ++i) {
3930 uint32_t band_max_delta_t;
3931
3932 /* Set burst size to a workable value if none specified. */
3933 if (config->bands[i].burst_size == 0) {
3934 config->bands[i].burst_size = config->bands[i].rate;
3935 }
3936
3937 meter->bands[i].up = config->bands[i];
3938 /* Convert burst size to the bucket units: */
3939 /* pkts => 1/1000 packets, kilobits => bits. */
3940 meter->bands[i].up.burst_size *= 1000;
3941 /* Initialize bucket to empty. */
3942 meter->bands[i].bucket = 0;
3943
3944 /* Figure out max delta_t that is enough to fill any bucket. */
3945 band_max_delta_t
3946 = meter->bands[i].up.burst_size / meter->bands[i].up.rate;
3947 if (band_max_delta_t > meter->max_delta_t) {
3948 meter->max_delta_t = band_max_delta_t;
3949 }
3950 }
3951
3952 meter_lock(dp, mid);
3953 dp_delete_meter(dp, mid); /* Free existing meter, if any */
3954 dp->meters[mid] = meter;
3955 meter_unlock(dp, mid);
3956
3957 return 0;
3958 }
3959 return ENOMEM;
3960 }
3961
3962 static int
3963 dpif_netdev_meter_get(const struct dpif *dpif,
3964 ofproto_meter_id meter_id_,
3965 struct ofputil_meter_stats *stats, uint16_t n_bands)
3966 {
3967 const struct dp_netdev *dp = get_dp_netdev(dpif);
3968 const struct dp_meter *meter;
3969 uint32_t meter_id = meter_id_.uint32;
3970
3971 if (meter_id >= MAX_METERS) {
3972 return EFBIG;
3973 }
3974 meter = dp->meters[meter_id];
3975 if (!meter) {
3976 return ENOENT;
3977 }
3978 if (stats) {
3979 int i = 0;
3980
3981 meter_lock(dp, meter_id);
3982 stats->packet_in_count = meter->packet_count;
3983 stats->byte_in_count = meter->byte_count;
3984
3985 for (i = 0; i < n_bands && i < meter->n_bands; ++i) {
3986 stats->bands[i].packet_count = meter->bands[i].packet_count;
3987 stats->bands[i].byte_count = meter->bands[i].byte_count;
3988 }
3989 meter_unlock(dp, meter_id);
3990
3991 stats->n_bands = i;
3992 }
3993 return 0;
3994 }
3995
3996 static int
3997 dpif_netdev_meter_del(struct dpif *dpif,
3998 ofproto_meter_id meter_id_,
3999 struct ofputil_meter_stats *stats, uint16_t n_bands)
4000 {
4001 struct dp_netdev *dp = get_dp_netdev(dpif);
4002 int error;
4003
4004 error = dpif_netdev_meter_get(dpif, meter_id_, stats, n_bands);
4005 if (!error) {
4006 uint32_t meter_id = meter_id_.uint32;
4007
4008 meter_lock(dp, meter_id);
4009 dp_delete_meter(dp, meter_id);
4010 meter_unlock(dp, meter_id);
4011 }
4012 return error;
4013 }
4014
4015 \f
4016 static void
4017 dpif_netdev_disable_upcall(struct dpif *dpif)
4018 OVS_NO_THREAD_SAFETY_ANALYSIS
4019 {
4020 struct dp_netdev *dp = get_dp_netdev(dpif);
4021 dp_netdev_disable_upcall(dp);
4022 }
4023
4024 static void
4025 dp_netdev_enable_upcall(struct dp_netdev *dp)
4026 OVS_RELEASES(dp->upcall_rwlock)
4027 {
4028 fat_rwlock_unlock(&dp->upcall_rwlock);
4029 }
4030
4031 static void
4032 dpif_netdev_enable_upcall(struct dpif *dpif)
4033 OVS_NO_THREAD_SAFETY_ANALYSIS
4034 {
4035 struct dp_netdev *dp = get_dp_netdev(dpif);
4036 dp_netdev_enable_upcall(dp);
4037 }
4038
4039 static void
4040 dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread *pmd)
4041 {
4042 ovs_mutex_lock(&pmd->cond_mutex);
4043 atomic_store_relaxed(&pmd->reload, false);
4044 pmd->last_reload_seq = seq_read(pmd->reload_seq);
4045 xpthread_cond_signal(&pmd->cond);
4046 ovs_mutex_unlock(&pmd->cond_mutex);
4047 }
4048
4049 /* Finds and refs the dp_netdev_pmd_thread on core 'core_id'. Returns
4050 * the pointer if succeeds, otherwise, NULL (it can return NULL even if
4051 * 'core_id' is NON_PMD_CORE_ID).
4052 *
4053 * Caller must unrefs the returned reference. */
4054 static struct dp_netdev_pmd_thread *
4055 dp_netdev_get_pmd(struct dp_netdev *dp, unsigned core_id)
4056 {
4057 struct dp_netdev_pmd_thread *pmd;
4058 const struct cmap_node *pnode;
4059
4060 pnode = cmap_find(&dp->poll_threads, hash_int(core_id, 0));
4061 if (!pnode) {
4062 return NULL;
4063 }
4064 pmd = CONTAINER_OF(pnode, struct dp_netdev_pmd_thread, node);
4065
4066 return dp_netdev_pmd_try_ref(pmd) ? pmd : NULL;
4067 }
4068
4069 /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */
4070 static void
4071 dp_netdev_set_nonpmd(struct dp_netdev *dp)
4072 OVS_REQUIRES(dp->port_mutex)
4073 {
4074 struct dp_netdev_pmd_thread *non_pmd;
4075
4076 non_pmd = xzalloc(sizeof *non_pmd);
4077 dp_netdev_configure_pmd(non_pmd, dp, NON_PMD_CORE_ID, OVS_NUMA_UNSPEC);
4078 }
4079
4080 /* Caller must have valid pointer to 'pmd'. */
4081 static bool
4082 dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread *pmd)
4083 {
4084 return ovs_refcount_try_ref_rcu(&pmd->ref_cnt);
4085 }
4086
4087 static void
4088 dp_netdev_pmd_unref(struct dp_netdev_pmd_thread *pmd)
4089 {
4090 if (pmd && ovs_refcount_unref(&pmd->ref_cnt) == 1) {
4091 ovsrcu_postpone(dp_netdev_destroy_pmd, pmd);
4092 }
4093 }
4094
4095 /* Given cmap position 'pos', tries to ref the next node. If try_ref()
4096 * fails, keeps checking for next node until reaching the end of cmap.
4097 *
4098 * Caller must unrefs the returned reference. */
4099 static struct dp_netdev_pmd_thread *
4100 dp_netdev_pmd_get_next(struct dp_netdev *dp, struct cmap_position *pos)
4101 {
4102 struct dp_netdev_pmd_thread *next;
4103
4104 do {
4105 struct cmap_node *node;
4106
4107 node = cmap_next_position(&dp->poll_threads, pos);
4108 next = node ? CONTAINER_OF(node, struct dp_netdev_pmd_thread, node)
4109 : NULL;
4110 } while (next && !dp_netdev_pmd_try_ref(next));
4111
4112 return next;
4113 }
4114
4115 /* Configures the 'pmd' based on the input argument. */
4116 static void
4117 dp_netdev_configure_pmd(struct dp_netdev_pmd_thread *pmd, struct dp_netdev *dp,
4118 unsigned core_id, int numa_id)
4119 {
4120 pmd->dp = dp;
4121 pmd->core_id = core_id;
4122 pmd->numa_id = numa_id;
4123 pmd->need_reload = false;
4124
4125 *CONST_CAST(int *, &pmd->static_tx_qid) = cmap_count(&dp->poll_threads);
4126
4127 ovs_refcount_init(&pmd->ref_cnt);
4128 latch_init(&pmd->exit_latch);
4129 pmd->reload_seq = seq_create();
4130 pmd->last_reload_seq = seq_read(pmd->reload_seq);
4131 atomic_init(&pmd->reload, false);
4132 xpthread_cond_init(&pmd->cond, NULL);
4133 ovs_mutex_init(&pmd->cond_mutex);
4134 ovs_mutex_init(&pmd->flow_mutex);
4135 ovs_mutex_init(&pmd->port_mutex);
4136 cmap_init(&pmd->flow_table);
4137 cmap_init(&pmd->classifiers);
4138 pmd->next_optimization = time_msec() + DPCLS_OPTIMIZATION_INTERVAL;
4139 hmap_init(&pmd->poll_list);
4140 hmap_init(&pmd->tx_ports);
4141 hmap_init(&pmd->tnl_port_cache);
4142 hmap_init(&pmd->send_port_cache);
4143 /* init the 'flow_cache' since there is no
4144 * actual thread created for NON_PMD_CORE_ID. */
4145 if (core_id == NON_PMD_CORE_ID) {
4146 emc_cache_init(&pmd->flow_cache);
4147 }
4148 cmap_insert(&dp->poll_threads, CONST_CAST(struct cmap_node *, &pmd->node),
4149 hash_int(core_id, 0));
4150 }
4151
4152 static void
4153 dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread *pmd)
4154 {
4155 struct dpcls *cls;
4156
4157 dp_netdev_pmd_flow_flush(pmd);
4158 hmap_destroy(&pmd->send_port_cache);
4159 hmap_destroy(&pmd->tnl_port_cache);
4160 hmap_destroy(&pmd->tx_ports);
4161 hmap_destroy(&pmd->poll_list);
4162 /* All flows (including their dpcls_rules) have been deleted already */
4163 CMAP_FOR_EACH (cls, node, &pmd->classifiers) {
4164 dpcls_destroy(cls);
4165 ovsrcu_postpone(free, cls);
4166 }
4167 cmap_destroy(&pmd->classifiers);
4168 cmap_destroy(&pmd->flow_table);
4169 ovs_mutex_destroy(&pmd->flow_mutex);
4170 latch_destroy(&pmd->exit_latch);
4171 seq_destroy(pmd->reload_seq);
4172 xpthread_cond_destroy(&pmd->cond);
4173 ovs_mutex_destroy(&pmd->cond_mutex);
4174 ovs_mutex_destroy(&pmd->port_mutex);
4175 free(pmd);
4176 }
4177
4178 /* Stops the pmd thread, removes it from the 'dp->poll_threads',
4179 * and unrefs the struct. */
4180 static void
4181 dp_netdev_del_pmd(struct dp_netdev *dp, struct dp_netdev_pmd_thread *pmd)
4182 {
4183 /* NON_PMD_CORE_ID doesn't have a thread, so we don't have to synchronize,
4184 * but extra cleanup is necessary */
4185 if (pmd->core_id == NON_PMD_CORE_ID) {
4186 ovs_mutex_lock(&dp->non_pmd_mutex);
4187 emc_cache_uninit(&pmd->flow_cache);
4188 pmd_free_cached_ports(pmd);
4189 ovs_mutex_unlock(&dp->non_pmd_mutex);
4190 } else {
4191 latch_set(&pmd->exit_latch);
4192 dp_netdev_reload_pmd__(pmd);
4193 xpthread_join(pmd->thread, NULL);
4194 }
4195
4196 dp_netdev_pmd_clear_ports(pmd);
4197
4198 /* Purges the 'pmd''s flows after stopping the thread, but before
4199 * destroying the flows, so that the flow stats can be collected. */
4200 if (dp->dp_purge_cb) {
4201 dp->dp_purge_cb(dp->dp_purge_aux, pmd->core_id);
4202 }
4203 cmap_remove(&pmd->dp->poll_threads, &pmd->node, hash_int(pmd->core_id, 0));
4204 dp_netdev_pmd_unref(pmd);
4205 }
4206
4207 /* Destroys all pmd threads. If 'non_pmd' is true it also destroys the non pmd
4208 * thread. */
4209 static void
4210 dp_netdev_destroy_all_pmds(struct dp_netdev *dp, bool non_pmd)
4211 {
4212 struct dp_netdev_pmd_thread *pmd;
4213 struct dp_netdev_pmd_thread **pmd_list;
4214 size_t k = 0, n_pmds;
4215
4216 n_pmds = cmap_count(&dp->poll_threads);
4217 pmd_list = xcalloc(n_pmds, sizeof *pmd_list);
4218
4219 CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
4220 if (!non_pmd && pmd->core_id == NON_PMD_CORE_ID) {
4221 continue;
4222 }
4223 /* We cannot call dp_netdev_del_pmd(), since it alters
4224 * 'dp->poll_threads' (while we're iterating it) and it
4225 * might quiesce. */
4226 ovs_assert(k < n_pmds);
4227 pmd_list[k++] = pmd;
4228 }
4229
4230 for (size_t i = 0; i < k; i++) {
4231 dp_netdev_del_pmd(dp, pmd_list[i]);
4232 }
4233 free(pmd_list);
4234 }
4235
4236 /* Deletes all rx queues from pmd->poll_list and all the ports from
4237 * pmd->tx_ports. */
4238 static void
4239 dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread *pmd)
4240 {
4241 struct rxq_poll *poll;
4242 struct tx_port *port;
4243
4244 ovs_mutex_lock(&pmd->port_mutex);
4245 HMAP_FOR_EACH_POP (poll, node, &pmd->poll_list) {
4246 free(poll);
4247 }
4248 HMAP_FOR_EACH_POP (port, node, &pmd->tx_ports) {
4249 free(port);
4250 }
4251 ovs_mutex_unlock(&pmd->port_mutex);
4252 }
4253
4254 /* Adds rx queue to poll_list of PMD thread, if it's not there already. */
4255 static void
4256 dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread *pmd,
4257 struct dp_netdev_rxq *rxq)
4258 OVS_REQUIRES(pmd->port_mutex)
4259 {
4260 int qid = netdev_rxq_get_queue_id(rxq->rx);
4261 uint32_t hash = hash_2words(odp_to_u32(rxq->port->port_no), qid);
4262 struct rxq_poll *poll;
4263
4264 HMAP_FOR_EACH_WITH_HASH (poll, node, hash, &pmd->poll_list) {
4265 if (poll->rxq == rxq) {
4266 /* 'rxq' is already polled by this thread. Do nothing. */
4267 return;
4268 }
4269 }
4270
4271 poll = xmalloc(sizeof *poll);
4272 poll->rxq = rxq;
4273 hmap_insert(&pmd->poll_list, &poll->node, hash);
4274
4275 pmd->need_reload = true;
4276 }
4277
4278 /* Delete 'poll' from poll_list of PMD thread. */
4279 static void
4280 dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread *pmd,
4281 struct rxq_poll *poll)
4282 OVS_REQUIRES(pmd->port_mutex)
4283 {
4284 hmap_remove(&pmd->poll_list, &poll->node);
4285 free(poll);
4286
4287 pmd->need_reload = true;
4288 }
4289
4290 /* Add 'port' to the tx port cache of 'pmd', which must be reloaded for the
4291 * changes to take effect. */
4292 static void
4293 dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread *pmd,
4294 struct dp_netdev_port *port)
4295 OVS_REQUIRES(pmd->port_mutex)
4296 {
4297 struct tx_port *tx;
4298
4299 tx = tx_port_lookup(&pmd->tx_ports, port->port_no);
4300 if (tx) {
4301 /* 'port' is already on this thread tx cache. Do nothing. */
4302 return;
4303 }
4304
4305 tx = xzalloc(sizeof *tx);
4306
4307 tx->port = port;
4308 tx->qid = -1;
4309
4310 hmap_insert(&pmd->tx_ports, &tx->node, hash_port_no(tx->port->port_no));
4311 pmd->need_reload = true;
4312 }
4313
4314 /* Del 'tx' from the tx port cache of 'pmd', which must be reloaded for the
4315 * changes to take effect. */
4316 static void
4317 dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread *pmd,
4318 struct tx_port *tx)
4319 OVS_REQUIRES(pmd->port_mutex)
4320 {
4321 hmap_remove(&pmd->tx_ports, &tx->node);
4322 free(tx);
4323 pmd->need_reload = true;
4324 }
4325 \f
4326 static char *
4327 dpif_netdev_get_datapath_version(void)
4328 {
4329 return xstrdup("<built-in>");
4330 }
4331
4332 static void
4333 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow, int cnt, int size,
4334 uint16_t tcp_flags, long long now)
4335 {
4336 uint16_t flags;
4337
4338 atomic_store_relaxed(&netdev_flow->stats.used, now);
4339 non_atomic_ullong_add(&netdev_flow->stats.packet_count, cnt);
4340 non_atomic_ullong_add(&netdev_flow->stats.byte_count, size);
4341 atomic_read_relaxed(&netdev_flow->stats.tcp_flags, &flags);
4342 flags |= tcp_flags;
4343 atomic_store_relaxed(&netdev_flow->stats.tcp_flags, flags);
4344 }
4345
4346 static void
4347 dp_netdev_count_packet(struct dp_netdev_pmd_thread *pmd,
4348 enum dp_stat_type type, int cnt)
4349 {
4350 non_atomic_ullong_add(&pmd->stats.n[type], cnt);
4351 }
4352
4353 static int
4354 dp_netdev_upcall(struct dp_netdev_pmd_thread *pmd, struct dp_packet *packet_,
4355 struct flow *flow, struct flow_wildcards *wc, ovs_u128 *ufid,
4356 enum dpif_upcall_type type, const struct nlattr *userdata,
4357 struct ofpbuf *actions, struct ofpbuf *put_actions)
4358 {
4359 struct dp_netdev *dp = pmd->dp;
4360
4361 if (OVS_UNLIKELY(!dp->upcall_cb)) {
4362 return ENODEV;
4363 }
4364
4365 if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl))) {
4366 struct ds ds = DS_EMPTY_INITIALIZER;
4367 char *packet_str;
4368 struct ofpbuf key;
4369 struct odp_flow_key_parms odp_parms = {
4370 .flow = flow,
4371 .mask = wc ? &wc->masks : NULL,
4372 .support = dp_netdev_support,
4373 };
4374
4375 ofpbuf_init(&key, 0);
4376 odp_flow_key_from_flow(&odp_parms, &key);
4377 packet_str = ofp_dp_packet_to_string(packet_);
4378
4379 odp_flow_key_format(key.data, key.size, &ds);
4380
4381 VLOG_DBG("%s: %s upcall:\n%s\n%s", dp->name,
4382 dpif_upcall_type_to_string(type), ds_cstr(&ds), packet_str);
4383
4384 ofpbuf_uninit(&key);
4385 free(packet_str);
4386
4387 ds_destroy(&ds);
4388 }
4389
4390 return dp->upcall_cb(packet_, flow, ufid, pmd->core_id, type, userdata,
4391 actions, wc, put_actions, dp->upcall_aux);
4392 }
4393
4394 static inline uint32_t
4395 dpif_netdev_packet_get_rss_hash(struct dp_packet *packet,
4396 const struct miniflow *mf)
4397 {
4398 uint32_t hash, recirc_depth;
4399
4400 if (OVS_LIKELY(dp_packet_rss_valid(packet))) {
4401 hash = dp_packet_get_rss_hash(packet);
4402 } else {
4403 hash = miniflow_hash_5tuple(mf, 0);
4404 dp_packet_set_rss_hash(packet, hash);
4405 }
4406
4407 /* The RSS hash must account for the recirculation depth to avoid
4408 * collisions in the exact match cache */
4409 recirc_depth = *recirc_depth_get_unsafe();
4410 if (OVS_UNLIKELY(recirc_depth)) {
4411 hash = hash_finish(hash, recirc_depth);
4412 dp_packet_set_rss_hash(packet, hash);
4413 }
4414 return hash;
4415 }
4416
4417 struct packet_batch_per_flow {
4418 unsigned int byte_count;
4419 uint16_t tcp_flags;
4420 struct dp_netdev_flow *flow;
4421
4422 struct dp_packet_batch array;
4423 };
4424
4425 static inline void
4426 packet_batch_per_flow_update(struct packet_batch_per_flow *batch,
4427 struct dp_packet *packet,
4428 const struct miniflow *mf)
4429 {
4430 batch->byte_count += dp_packet_size(packet);
4431 batch->tcp_flags |= miniflow_get_tcp_flags(mf);
4432 batch->array.packets[batch->array.count++] = packet;
4433 }
4434
4435 static inline void
4436 packet_batch_per_flow_init(struct packet_batch_per_flow *batch,
4437 struct dp_netdev_flow *flow)
4438 {
4439 flow->batch = batch;
4440
4441 batch->flow = flow;
4442 dp_packet_batch_init(&batch->array);
4443 batch->byte_count = 0;
4444 batch->tcp_flags = 0;
4445 }
4446
4447 static inline void
4448 packet_batch_per_flow_execute(struct packet_batch_per_flow *batch,
4449 struct dp_netdev_pmd_thread *pmd,
4450 long long now)
4451 {
4452 struct dp_netdev_actions *actions;
4453 struct dp_netdev_flow *flow = batch->flow;
4454
4455 dp_netdev_flow_used(flow, batch->array.count, batch->byte_count,
4456 batch->tcp_flags, now);
4457
4458 actions = dp_netdev_flow_get_actions(flow);
4459
4460 dp_netdev_execute_actions(pmd, &batch->array, true, &flow->flow,
4461 actions->actions, actions->size, now);
4462 }
4463
4464 static inline void
4465 dp_netdev_queue_batches(struct dp_packet *pkt,
4466 struct dp_netdev_flow *flow, const struct miniflow *mf,
4467 struct packet_batch_per_flow *batches, size_t *n_batches)
4468 {
4469 struct packet_batch_per_flow *batch = flow->batch;
4470
4471 if (OVS_UNLIKELY(!batch)) {
4472 batch = &batches[(*n_batches)++];
4473 packet_batch_per_flow_init(batch, flow);
4474 }
4475
4476 packet_batch_per_flow_update(batch, pkt, mf);
4477 }
4478
4479 /* Try to process all ('cnt') the 'packets' using only the exact match cache
4480 * 'pmd->flow_cache'. If a flow is not found for a packet 'packets[i]', the
4481 * miniflow is copied into 'keys' and the packet pointer is moved at the
4482 * beginning of the 'packets' array.
4483 *
4484 * The function returns the number of packets that needs to be processed in the
4485 * 'packets' array (they have been moved to the beginning of the vector).
4486 *
4487 * If 'md_is_valid' is false, the metadata in 'packets' is not valid and must be
4488 * initialized by this function using 'port_no'.
4489 */
4490 static inline size_t
4491 emc_processing(struct dp_netdev_pmd_thread *pmd,
4492 struct dp_packet_batch *packets_,
4493 struct netdev_flow_key *keys,
4494 struct packet_batch_per_flow batches[], size_t *n_batches,
4495 bool md_is_valid, odp_port_t port_no)
4496 {
4497 struct emc_cache *flow_cache = &pmd->flow_cache;
4498 struct netdev_flow_key *key = &keys[0];
4499 size_t n_missed = 0, n_dropped = 0;
4500 struct dp_packet *packet;
4501 const size_t size = dp_packet_batch_size(packets_);
4502 int i;
4503
4504 DP_PACKET_BATCH_REFILL_FOR_EACH (i, size, packet, packets_) {
4505 struct dp_netdev_flow *flow;
4506
4507 if (OVS_UNLIKELY(dp_packet_size(packet) < ETH_HEADER_LEN)) {
4508 dp_packet_delete(packet);
4509 n_dropped++;
4510 continue;
4511 }
4512
4513 if (i != size - 1) {
4514 struct dp_packet **packets = packets_->packets;
4515 /* Prefetch next packet data and metadata. */
4516 OVS_PREFETCH(dp_packet_data(packets[i+1]));
4517 pkt_metadata_prefetch_init(&packets[i+1]->md);
4518 }
4519
4520 if (!md_is_valid) {
4521 pkt_metadata_init(&packet->md, port_no);
4522 }
4523 miniflow_extract(packet, &key->mf);
4524 key->len = 0; /* Not computed yet. */
4525 key->hash = dpif_netdev_packet_get_rss_hash(packet, &key->mf);
4526
4527 flow = emc_lookup(flow_cache, key);
4528 if (OVS_LIKELY(flow)) {
4529 dp_netdev_queue_batches(packet, flow, &key->mf, batches,
4530 n_batches);
4531 } else {
4532 /* Exact match cache missed. Group missed packets together at
4533 * the beginning of the 'packets' array. */
4534 dp_packet_batch_refill(packets_, packet, i);
4535 /* 'key[n_missed]' contains the key of the current packet and it
4536 * must be returned to the caller. The next key should be extracted
4537 * to 'keys[n_missed + 1]'. */
4538 key = &keys[++n_missed];
4539 }
4540 }
4541
4542 dp_netdev_count_packet(pmd, DP_STAT_EXACT_HIT, size - n_dropped - n_missed);
4543
4544 return dp_packet_batch_size(packets_);
4545 }
4546
4547 static inline void
4548 handle_packet_upcall(struct dp_netdev_pmd_thread *pmd, struct dp_packet *packet,
4549 const struct netdev_flow_key *key,
4550 struct ofpbuf *actions, struct ofpbuf *put_actions,
4551 int *lost_cnt, long long now)
4552 {
4553 struct ofpbuf *add_actions;
4554 struct dp_packet_batch b;
4555 struct match match;
4556 ovs_u128 ufid;
4557 int error;
4558
4559 match.tun_md.valid = false;
4560 miniflow_expand(&key->mf, &match.flow);
4561
4562 ofpbuf_clear(actions);
4563 ofpbuf_clear(put_actions);
4564
4565 dpif_flow_hash(pmd->dp->dpif, &match.flow, sizeof match.flow, &ufid);
4566 error = dp_netdev_upcall(pmd, packet, &match.flow, &match.wc,
4567 &ufid, DPIF_UC_MISS, NULL, actions,
4568 put_actions);
4569 if (OVS_UNLIKELY(error && error != ENOSPC)) {
4570 dp_packet_delete(packet);
4571 (*lost_cnt)++;
4572 return;
4573 }
4574
4575 /* The Netlink encoding of datapath flow keys cannot express
4576 * wildcarding the presence of a VLAN tag. Instead, a missing VLAN
4577 * tag is interpreted as exact match on the fact that there is no
4578 * VLAN. Unless we refactor a lot of code that translates between
4579 * Netlink and struct flow representations, we have to do the same
4580 * here. */
4581 if (!match.wc.masks.vlans[0].tci) {
4582 match.wc.masks.vlans[0].tci = htons(0xffff);
4583 }
4584
4585 /* We can't allow the packet batching in the next loop to execute
4586 * the actions. Otherwise, if there are any slow path actions,
4587 * we'll send the packet up twice. */
4588 dp_packet_batch_init_packet(&b, packet);
4589 dp_netdev_execute_actions(pmd, &b, true, &match.flow,
4590 actions->data, actions->size, now);
4591
4592 add_actions = put_actions->size ? put_actions : actions;
4593 if (OVS_LIKELY(error != ENOSPC)) {
4594 struct dp_netdev_flow *netdev_flow;
4595
4596 /* XXX: There's a race window where a flow covering this packet
4597 * could have already been installed since we last did the flow
4598 * lookup before upcall. This could be solved by moving the
4599 * mutex lock outside the loop, but that's an awful long time
4600 * to be locking everyone out of making flow installs. If we
4601 * move to a per-core classifier, it would be reasonable. */
4602 ovs_mutex_lock(&pmd->flow_mutex);
4603 netdev_flow = dp_netdev_pmd_lookup_flow(pmd, key, NULL);
4604 if (OVS_LIKELY(!netdev_flow)) {
4605 netdev_flow = dp_netdev_flow_add(pmd, &match, &ufid,
4606 add_actions->data,
4607 add_actions->size);
4608 }
4609 ovs_mutex_unlock(&pmd->flow_mutex);
4610 emc_probabilistic_insert(pmd, key, netdev_flow);
4611 }
4612 }
4613
4614 static inline void
4615 fast_path_processing(struct dp_netdev_pmd_thread *pmd,
4616 struct dp_packet_batch *packets_,
4617 struct netdev_flow_key *keys,
4618 struct packet_batch_per_flow batches[], size_t *n_batches,
4619 odp_port_t in_port,
4620 long long now)
4621 {
4622 int cnt = packets_->count;
4623 #if !defined(__CHECKER__) && !defined(_WIN32)
4624 const size_t PKT_ARRAY_SIZE = cnt;
4625 #else
4626 /* Sparse or MSVC doesn't like variable length array. */
4627 enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST };
4628 #endif
4629 struct dp_packet **packets = packets_->packets;
4630 struct dpcls *cls;
4631 struct dpcls_rule *rules[PKT_ARRAY_SIZE];
4632 struct dp_netdev *dp = pmd->dp;
4633 int miss_cnt = 0, lost_cnt = 0;
4634 int lookup_cnt = 0, add_lookup_cnt;
4635 bool any_miss;
4636 size_t i;
4637
4638 for (i = 0; i < cnt; i++) {
4639 /* Key length is needed in all the cases, hash computed on demand. */
4640 keys[i].len = netdev_flow_key_size(miniflow_n_values(&keys[i].mf));
4641 }
4642 /* Get the classifier for the in_port */
4643 cls = dp_netdev_pmd_lookup_dpcls(pmd, in_port);
4644 if (OVS_LIKELY(cls)) {
4645 any_miss = !dpcls_lookup(cls, keys, rules, cnt, &lookup_cnt);
4646 } else {
4647 any_miss = true;
4648 memset(rules, 0, sizeof(rules));
4649 }
4650 if (OVS_UNLIKELY(any_miss) && !fat_rwlock_tryrdlock(&dp->upcall_rwlock)) {
4651 uint64_t actions_stub[512 / 8], slow_stub[512 / 8];
4652 struct ofpbuf actions, put_actions;
4653
4654 ofpbuf_use_stub(&actions, actions_stub, sizeof actions_stub);
4655 ofpbuf_use_stub(&put_actions, slow_stub, sizeof slow_stub);
4656
4657 for (i = 0; i < cnt; i++) {
4658 struct dp_netdev_flow *netdev_flow;
4659
4660 if (OVS_LIKELY(rules[i])) {
4661 continue;
4662 }
4663
4664 /* It's possible that an earlier slow path execution installed
4665 * a rule covering this flow. In this case, it's a lot cheaper
4666 * to catch it here than execute a miss. */
4667 netdev_flow = dp_netdev_pmd_lookup_flow(pmd, &keys[i],
4668 &add_lookup_cnt);
4669 if (netdev_flow) {
4670 lookup_cnt += add_lookup_cnt;
4671 rules[i] = &netdev_flow->cr;
4672 continue;
4673 }
4674
4675 miss_cnt++;
4676 handle_packet_upcall(pmd, packets[i], &keys[i], &actions,
4677 &put_actions, &lost_cnt, now);
4678 }
4679
4680 ofpbuf_uninit(&actions);
4681 ofpbuf_uninit(&put_actions);
4682 fat_rwlock_unlock(&dp->upcall_rwlock);
4683 } else if (OVS_UNLIKELY(any_miss)) {
4684 for (i = 0; i < cnt; i++) {
4685 if (OVS_UNLIKELY(!rules[i])) {
4686 dp_packet_delete(packets[i]);
4687 lost_cnt++;
4688 miss_cnt++;
4689 }
4690 }
4691 }
4692
4693 for (i = 0; i < cnt; i++) {
4694 struct dp_packet *packet = packets[i];
4695 struct dp_netdev_flow *flow;
4696
4697 if (OVS_UNLIKELY(!rules[i])) {
4698 continue;
4699 }
4700
4701 flow = dp_netdev_flow_cast(rules[i]);
4702
4703 emc_probabilistic_insert(pmd, &keys[i], flow);
4704 dp_netdev_queue_batches(packet, flow, &keys[i].mf, batches, n_batches);
4705 }
4706
4707 dp_netdev_count_packet(pmd, DP_STAT_MASKED_HIT, cnt - miss_cnt);
4708 dp_netdev_count_packet(pmd, DP_STAT_LOOKUP_HIT, lookup_cnt);
4709 dp_netdev_count_packet(pmd, DP_STAT_MISS, miss_cnt);
4710 dp_netdev_count_packet(pmd, DP_STAT_LOST, lost_cnt);
4711 }
4712
4713 /* Packets enter the datapath from a port (or from recirculation) here.
4714 *
4715 * For performance reasons a caller may choose not to initialize the metadata
4716 * in 'packets': in this case 'mdinit' is false and this function needs to
4717 * initialize it using 'port_no'. If the metadata in 'packets' is already
4718 * valid, 'md_is_valid' must be true and 'port_no' will be ignored. */
4719 static void
4720 dp_netdev_input__(struct dp_netdev_pmd_thread *pmd,
4721 struct dp_packet_batch *packets,
4722 bool md_is_valid, odp_port_t port_no)
4723 {
4724 int cnt = packets->count;
4725 #if !defined(__CHECKER__) && !defined(_WIN32)
4726 const size_t PKT_ARRAY_SIZE = cnt;
4727 #else
4728 /* Sparse or MSVC doesn't like variable length array. */
4729 enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST };
4730 #endif
4731 OVS_ALIGNED_VAR(CACHE_LINE_SIZE) struct netdev_flow_key keys[PKT_ARRAY_SIZE];
4732 struct packet_batch_per_flow batches[PKT_ARRAY_SIZE];
4733 long long now = time_msec();
4734 size_t n_batches;
4735 odp_port_t in_port;
4736
4737 n_batches = 0;
4738 emc_processing(pmd, packets, keys, batches, &n_batches,
4739 md_is_valid, port_no);
4740 if (!dp_packet_batch_is_empty(packets)) {
4741 /* Get ingress port from first packet's metadata. */
4742 in_port = packets->packets[0]->md.in_port.odp_port;
4743 fast_path_processing(pmd, packets, keys, batches, &n_batches, in_port, now);
4744 }
4745
4746 /* All the flow batches need to be reset before any call to
4747 * packet_batch_per_flow_execute() as it could potentially trigger
4748 * recirculation. When a packet matching flow ‘j’ happens to be
4749 * recirculated, the nested call to dp_netdev_input__() could potentially
4750 * classify the packet as matching another flow - say 'k'. It could happen
4751 * that in the previous call to dp_netdev_input__() that same flow 'k' had
4752 * already its own batches[k] still waiting to be served. So if its
4753 * ‘batch’ member is not reset, the recirculated packet would be wrongly
4754 * appended to batches[k] of the 1st call to dp_netdev_input__(). */
4755 size_t i;
4756 for (i = 0; i < n_batches; i++) {
4757 batches[i].flow->batch = NULL;
4758 }
4759
4760 for (i = 0; i < n_batches; i++) {
4761 packet_batch_per_flow_execute(&batches[i], pmd, now);
4762 }
4763 }
4764
4765 static void
4766 dp_netdev_input(struct dp_netdev_pmd_thread *pmd,
4767 struct dp_packet_batch *packets,
4768 odp_port_t port_no)
4769 {
4770 dp_netdev_input__(pmd, packets, false, port_no);
4771 }
4772
4773 static void
4774 dp_netdev_recirculate(struct dp_netdev_pmd_thread *pmd,
4775 struct dp_packet_batch *packets)
4776 {
4777 dp_netdev_input__(pmd, packets, true, 0);
4778 }
4779
4780 struct dp_netdev_execute_aux {
4781 struct dp_netdev_pmd_thread *pmd;
4782 long long now;
4783 const struct flow *flow;
4784 };
4785
4786 static void
4787 dpif_netdev_register_dp_purge_cb(struct dpif *dpif, dp_purge_callback *cb,
4788 void *aux)
4789 {
4790 struct dp_netdev *dp = get_dp_netdev(dpif);
4791 dp->dp_purge_aux = aux;
4792 dp->dp_purge_cb = cb;
4793 }
4794
4795 static void
4796 dpif_netdev_register_upcall_cb(struct dpif *dpif, upcall_callback *cb,
4797 void *aux)
4798 {
4799 struct dp_netdev *dp = get_dp_netdev(dpif);
4800 dp->upcall_aux = aux;
4801 dp->upcall_cb = cb;
4802 }
4803
4804 static void
4805 dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread *pmd,
4806 long long now, bool purge)
4807 {
4808 struct tx_port *tx;
4809 struct dp_netdev_port *port;
4810 long long interval;
4811
4812 HMAP_FOR_EACH (tx, node, &pmd->send_port_cache) {
4813 if (!tx->port->dynamic_txqs) {
4814 continue;
4815 }
4816 interval = now - tx->last_used;
4817 if (tx->qid >= 0 && (purge || interval >= XPS_TIMEOUT_MS)) {
4818 port = tx->port;
4819 ovs_mutex_lock(&port->txq_used_mutex);
4820 port->txq_used[tx->qid]--;
4821 ovs_mutex_unlock(&port->txq_used_mutex);
4822 tx->qid = -1;
4823 }
4824 }
4825 }
4826
4827 static int
4828 dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread *pmd,
4829 struct tx_port *tx, long long now)
4830 {
4831 struct dp_netdev_port *port;
4832 long long interval;
4833 int i, min_cnt, min_qid;
4834
4835 if (OVS_UNLIKELY(!now)) {
4836 now = time_msec();
4837 }
4838
4839 interval = now - tx->last_used;
4840 tx->last_used = now;
4841
4842 if (OVS_LIKELY(tx->qid >= 0 && interval < XPS_TIMEOUT_MS)) {
4843 return tx->qid;
4844 }
4845
4846 port = tx->port;
4847
4848 ovs_mutex_lock(&port->txq_used_mutex);
4849 if (tx->qid >= 0) {
4850 port->txq_used[tx->qid]--;
4851 tx->qid = -1;
4852 }
4853
4854 min_cnt = -1;
4855 min_qid = 0;
4856 for (i = 0; i < netdev_n_txq(port->netdev); i++) {
4857 if (port->txq_used[i] < min_cnt || min_cnt == -1) {
4858 min_cnt = port->txq_used[i];
4859 min_qid = i;
4860 }
4861 }
4862
4863 port->txq_used[min_qid]++;
4864 tx->qid = min_qid;
4865
4866 ovs_mutex_unlock(&port->txq_used_mutex);
4867
4868 dpif_netdev_xps_revalidate_pmd(pmd, now, false);
4869
4870 VLOG_DBG("Core %d: New TX queue ID %d for port \'%s\'.",
4871 pmd->core_id, tx->qid, netdev_get_name(tx->port->netdev));
4872 return min_qid;
4873 }
4874
4875 static struct tx_port *
4876 pmd_tnl_port_cache_lookup(const struct dp_netdev_pmd_thread *pmd,
4877 odp_port_t port_no)
4878 {
4879 return tx_port_lookup(&pmd->tnl_port_cache, port_no);
4880 }
4881
4882 static struct tx_port *
4883 pmd_send_port_cache_lookup(const struct dp_netdev_pmd_thread *pmd,
4884 odp_port_t port_no)
4885 {
4886 return tx_port_lookup(&pmd->send_port_cache, port_no);
4887 }
4888
4889 static int
4890 push_tnl_action(const struct dp_netdev_pmd_thread *pmd,
4891 const struct nlattr *attr,
4892 struct dp_packet_batch *batch)
4893 {
4894 struct tx_port *tun_port;
4895 const struct ovs_action_push_tnl *data;
4896 int err;
4897
4898 data = nl_attr_get(attr);
4899
4900 tun_port = pmd_tnl_port_cache_lookup(pmd, u32_to_odp(data->tnl_port));
4901 if (!tun_port) {
4902 err = -EINVAL;
4903 goto error;
4904 }
4905 err = netdev_push_header(tun_port->port->netdev, batch, data);
4906 if (!err) {
4907 return 0;
4908 }
4909 error:
4910 dp_packet_delete_batch(batch, true);
4911 return err;
4912 }
4913
4914 static void
4915 dp_execute_userspace_action(struct dp_netdev_pmd_thread *pmd,
4916 struct dp_packet *packet, bool may_steal,
4917 struct flow *flow, ovs_u128 *ufid,
4918 struct ofpbuf *actions,
4919 const struct nlattr *userdata, long long now)
4920 {
4921 struct dp_packet_batch b;
4922 int error;
4923
4924 ofpbuf_clear(actions);
4925
4926 error = dp_netdev_upcall(pmd, packet, flow, NULL, ufid,
4927 DPIF_UC_ACTION, userdata, actions,
4928 NULL);
4929 if (!error || error == ENOSPC) {
4930 dp_packet_batch_init_packet(&b, packet);
4931 dp_netdev_execute_actions(pmd, &b, may_steal, flow,
4932 actions->data, actions->size, now);
4933 } else if (may_steal) {
4934 dp_packet_delete(packet);
4935 }
4936 }
4937
4938 static void
4939 dp_execute_cb(void *aux_, struct dp_packet_batch *packets_,
4940 const struct nlattr *a, bool may_steal)
4941 OVS_NO_THREAD_SAFETY_ANALYSIS
4942 {
4943 struct dp_netdev_execute_aux *aux = aux_;
4944 uint32_t *depth = recirc_depth_get();
4945 struct dp_netdev_pmd_thread *pmd = aux->pmd;
4946 struct dp_netdev *dp = pmd->dp;
4947 int type = nl_attr_type(a);
4948 long long now = aux->now;
4949 struct tx_port *p;
4950
4951 switch ((enum ovs_action_attr)type) {
4952 case OVS_ACTION_ATTR_OUTPUT:
4953 p = pmd_send_port_cache_lookup(pmd, nl_attr_get_odp_port(a));
4954 if (OVS_LIKELY(p)) {
4955 int tx_qid;
4956 bool dynamic_txqs;
4957
4958 dynamic_txqs = p->port->dynamic_txqs;
4959 if (dynamic_txqs) {
4960 tx_qid = dpif_netdev_xps_get_tx_qid(pmd, p, now);
4961 } else {
4962 tx_qid = pmd->static_tx_qid;
4963 }
4964
4965 netdev_send(p->port->netdev, tx_qid, packets_, may_steal,
4966 dynamic_txqs);
4967 return;
4968 }
4969 break;
4970
4971 case OVS_ACTION_ATTR_TUNNEL_PUSH:
4972 if (*depth < MAX_RECIRC_DEPTH) {
4973 dp_packet_batch_apply_cutlen(packets_);
4974 push_tnl_action(pmd, a, packets_);
4975 return;
4976 }
4977 break;
4978
4979 case OVS_ACTION_ATTR_TUNNEL_POP:
4980 if (*depth < MAX_RECIRC_DEPTH) {
4981 struct dp_packet_batch *orig_packets_ = packets_;
4982 odp_port_t portno = nl_attr_get_odp_port(a);
4983
4984 p = pmd_tnl_port_cache_lookup(pmd, portno);
4985 if (p) {
4986 struct dp_packet_batch tnl_pkt;
4987
4988 if (!may_steal) {
4989 dp_packet_batch_clone(&tnl_pkt, packets_);
4990 packets_ = &tnl_pkt;
4991 dp_packet_batch_reset_cutlen(orig_packets_);
4992 }
4993
4994 dp_packet_batch_apply_cutlen(packets_);
4995
4996 netdev_pop_header(p->port->netdev, packets_);
4997 if (dp_packet_batch_is_empty(packets_)) {
4998 return;
4999 }
5000
5001 struct dp_packet *packet;
5002 DP_PACKET_BATCH_FOR_EACH (packet, packets_) {
5003 packet->md.in_port.odp_port = portno;
5004 }
5005
5006 (*depth)++;
5007 dp_netdev_recirculate(pmd, packets_);
5008 (*depth)--;
5009 return;
5010 }
5011 }
5012 break;
5013
5014 case OVS_ACTION_ATTR_USERSPACE:
5015 if (!fat_rwlock_tryrdlock(&dp->upcall_rwlock)) {
5016 struct dp_packet_batch *orig_packets_ = packets_;
5017 const struct nlattr *userdata;
5018 struct dp_packet_batch usr_pkt;
5019 struct ofpbuf actions;
5020 struct flow flow;
5021 ovs_u128 ufid;
5022 bool clone = false;
5023
5024 userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
5025 ofpbuf_init(&actions, 0);
5026
5027 if (packets_->trunc) {
5028 if (!may_steal) {
5029 dp_packet_batch_clone(&usr_pkt, packets_);
5030 packets_ = &usr_pkt;
5031 clone = true;
5032 dp_packet_batch_reset_cutlen(orig_packets_);
5033 }
5034
5035 dp_packet_batch_apply_cutlen(packets_);
5036 }
5037
5038 struct dp_packet *packet;
5039 DP_PACKET_BATCH_FOR_EACH (packet, packets_) {
5040 flow_extract(packet, &flow);
5041 dpif_flow_hash(dp->dpif, &flow, sizeof flow, &ufid);
5042 dp_execute_userspace_action(pmd, packet, may_steal, &flow,
5043 &ufid, &actions, userdata, now);
5044 }
5045
5046 if (clone) {
5047 dp_packet_delete_batch(packets_, true);
5048 }
5049
5050 ofpbuf_uninit(&actions);
5051 fat_rwlock_unlock(&dp->upcall_rwlock);
5052
5053 return;
5054 }
5055 break;
5056
5057 case OVS_ACTION_ATTR_RECIRC:
5058 if (*depth < MAX_RECIRC_DEPTH) {
5059 struct dp_packet_batch recirc_pkts;
5060
5061 if (!may_steal) {
5062 dp_packet_batch_clone(&recirc_pkts, packets_);
5063 packets_ = &recirc_pkts;
5064 }
5065
5066 struct dp_packet *packet;
5067 DP_PACKET_BATCH_FOR_EACH (packet, packets_) {
5068 packet->md.recirc_id = nl_attr_get_u32(a);
5069 }
5070
5071 (*depth)++;
5072 dp_netdev_recirculate(pmd, packets_);
5073 (*depth)--;
5074
5075 return;
5076 }
5077
5078 VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
5079 break;
5080
5081 case OVS_ACTION_ATTR_CT: {
5082 const struct nlattr *b;
5083 bool force = false;
5084 bool commit = false;
5085 unsigned int left;
5086 uint16_t zone = 0;
5087 const char *helper = NULL;
5088 const uint32_t *setmark = NULL;
5089 const struct ovs_key_ct_labels *setlabel = NULL;
5090
5091 NL_ATTR_FOR_EACH_UNSAFE (b, left, nl_attr_get(a),
5092 nl_attr_get_size(a)) {
5093 enum ovs_ct_attr sub_type = nl_attr_type(b);
5094
5095 switch(sub_type) {
5096 case OVS_CT_ATTR_FORCE_COMMIT:
5097 force = true;
5098 /* fall through. */
5099 case OVS_CT_ATTR_COMMIT:
5100 commit = true;
5101 break;
5102 case OVS_CT_ATTR_ZONE:
5103 zone = nl_attr_get_u16(b);
5104 break;
5105 case OVS_CT_ATTR_HELPER:
5106 helper = nl_attr_get_string(b);
5107 break;
5108 case OVS_CT_ATTR_MARK:
5109 setmark = nl_attr_get(b);
5110 break;
5111 case OVS_CT_ATTR_LABELS:
5112 setlabel = nl_attr_get(b);
5113 break;
5114 case OVS_CT_ATTR_EVENTMASK:
5115 /* Silently ignored, as userspace datapath does not generate
5116 * netlink events. */
5117 break;
5118 case OVS_CT_ATTR_NAT:
5119 case OVS_CT_ATTR_UNSPEC:
5120 case __OVS_CT_ATTR_MAX:
5121 OVS_NOT_REACHED();
5122 }
5123 }
5124
5125 conntrack_execute(&dp->conntrack, packets_, aux->flow->dl_type, force,
5126 commit, zone, setmark, setlabel, helper);
5127 break;
5128 }
5129
5130 case OVS_ACTION_ATTR_METER:
5131 dp_netdev_run_meter(pmd->dp, packets_, nl_attr_get_u32(a),
5132 time_msec());
5133 break;
5134
5135 case OVS_ACTION_ATTR_PUSH_VLAN:
5136 case OVS_ACTION_ATTR_POP_VLAN:
5137 case OVS_ACTION_ATTR_PUSH_MPLS:
5138 case OVS_ACTION_ATTR_POP_MPLS:
5139 case OVS_ACTION_ATTR_SET:
5140 case OVS_ACTION_ATTR_SET_MASKED:
5141 case OVS_ACTION_ATTR_SAMPLE:
5142 case OVS_ACTION_ATTR_HASH:
5143 case OVS_ACTION_ATTR_UNSPEC:
5144 case OVS_ACTION_ATTR_TRUNC:
5145 case OVS_ACTION_ATTR_PUSH_ETH:
5146 case OVS_ACTION_ATTR_POP_ETH:
5147 case OVS_ACTION_ATTR_CLONE:
5148 case __OVS_ACTION_ATTR_MAX:
5149 OVS_NOT_REACHED();
5150 }
5151
5152 dp_packet_delete_batch(packets_, may_steal);
5153 }
5154
5155 static void
5156 dp_netdev_execute_actions(struct dp_netdev_pmd_thread *pmd,
5157 struct dp_packet_batch *packets,
5158 bool may_steal, const struct flow *flow,
5159 const struct nlattr *actions, size_t actions_len,
5160 long long now)
5161 {
5162 struct dp_netdev_execute_aux aux = { pmd, now, flow };
5163
5164 odp_execute_actions(&aux, packets, may_steal, actions,
5165 actions_len, dp_execute_cb);
5166 }
5167
5168 struct dp_netdev_ct_dump {
5169 struct ct_dpif_dump_state up;
5170 struct conntrack_dump dump;
5171 struct conntrack *ct;
5172 struct dp_netdev *dp;
5173 };
5174
5175 static int
5176 dpif_netdev_ct_dump_start(struct dpif *dpif, struct ct_dpif_dump_state **dump_,
5177 const uint16_t *pzone)
5178 {
5179 struct dp_netdev *dp = get_dp_netdev(dpif);
5180 struct dp_netdev_ct_dump *dump;
5181
5182 dump = xzalloc(sizeof *dump);
5183 dump->dp = dp;
5184 dump->ct = &dp->conntrack;
5185
5186 conntrack_dump_start(&dp->conntrack, &dump->dump, pzone);
5187
5188 *dump_ = &dump->up;
5189
5190 return 0;
5191 }
5192
5193 static int
5194 dpif_netdev_ct_dump_next(struct dpif *dpif OVS_UNUSED,
5195 struct ct_dpif_dump_state *dump_,
5196 struct ct_dpif_entry *entry)
5197 {
5198 struct dp_netdev_ct_dump *dump;
5199
5200 INIT_CONTAINER(dump, dump_, up);
5201
5202 return conntrack_dump_next(&dump->dump, entry);
5203 }
5204
5205 static int
5206 dpif_netdev_ct_dump_done(struct dpif *dpif OVS_UNUSED,
5207 struct ct_dpif_dump_state *dump_)
5208 {
5209 struct dp_netdev_ct_dump *dump;
5210 int err;
5211
5212 INIT_CONTAINER(dump, dump_, up);
5213
5214 err = conntrack_dump_done(&dump->dump);
5215
5216 free(dump);
5217
5218 return err;
5219 }
5220
5221 static int
5222 dpif_netdev_ct_flush(struct dpif *dpif, const uint16_t *zone)
5223 {
5224 struct dp_netdev *dp = get_dp_netdev(dpif);
5225
5226 return conntrack_flush(&dp->conntrack, zone);
5227 }
5228
5229 const struct dpif_class dpif_netdev_class = {
5230 "netdev",
5231 dpif_netdev_init,
5232 dpif_netdev_enumerate,
5233 dpif_netdev_port_open_type,
5234 dpif_netdev_open,
5235 dpif_netdev_close,
5236 dpif_netdev_destroy,
5237 dpif_netdev_run,
5238 dpif_netdev_wait,
5239 dpif_netdev_get_stats,
5240 dpif_netdev_port_add,
5241 dpif_netdev_port_del,
5242 dpif_netdev_port_set_config,
5243 dpif_netdev_port_query_by_number,
5244 dpif_netdev_port_query_by_name,
5245 NULL, /* port_get_pid */
5246 dpif_netdev_port_dump_start,
5247 dpif_netdev_port_dump_next,
5248 dpif_netdev_port_dump_done,
5249 dpif_netdev_port_poll,
5250 dpif_netdev_port_poll_wait,
5251 dpif_netdev_flow_flush,
5252 dpif_netdev_flow_dump_create,
5253 dpif_netdev_flow_dump_destroy,
5254 dpif_netdev_flow_dump_thread_create,
5255 dpif_netdev_flow_dump_thread_destroy,
5256 dpif_netdev_flow_dump_next,
5257 dpif_netdev_operate,
5258 NULL, /* recv_set */
5259 NULL, /* handlers_set */
5260 dpif_netdev_set_config,
5261 dpif_netdev_queue_to_priority,
5262 NULL, /* recv */
5263 NULL, /* recv_wait */
5264 NULL, /* recv_purge */
5265 dpif_netdev_register_dp_purge_cb,
5266 dpif_netdev_register_upcall_cb,
5267 dpif_netdev_enable_upcall,
5268 dpif_netdev_disable_upcall,
5269 dpif_netdev_get_datapath_version,
5270 dpif_netdev_ct_dump_start,
5271 dpif_netdev_ct_dump_next,
5272 dpif_netdev_ct_dump_done,
5273 dpif_netdev_ct_flush,
5274 dpif_netdev_meter_get_features,
5275 dpif_netdev_meter_set,
5276 dpif_netdev_meter_get,
5277 dpif_netdev_meter_del,
5278 };
5279
5280 static void
5281 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
5282 const char *argv[], void *aux OVS_UNUSED)
5283 {
5284 struct dp_netdev_port *port;
5285 struct dp_netdev *dp;
5286 odp_port_t port_no;
5287
5288 ovs_mutex_lock(&dp_netdev_mutex);
5289 dp = shash_find_data(&dp_netdevs, argv[1]);
5290 if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
5291 ovs_mutex_unlock(&dp_netdev_mutex);
5292 unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
5293 return;
5294 }
5295 ovs_refcount_ref(&dp->ref_cnt);
5296 ovs_mutex_unlock(&dp_netdev_mutex);
5297
5298 ovs_mutex_lock(&dp->port_mutex);
5299 if (get_port_by_name(dp, argv[2], &port)) {
5300 unixctl_command_reply_error(conn, "unknown port");
5301 goto exit;
5302 }
5303
5304 port_no = u32_to_odp(atoi(argv[3]));
5305 if (!port_no || port_no == ODPP_NONE) {
5306 unixctl_command_reply_error(conn, "bad port number");
5307 goto exit;
5308 }
5309 if (dp_netdev_lookup_port(dp, port_no)) {
5310 unixctl_command_reply_error(conn, "port number already in use");
5311 goto exit;
5312 }
5313
5314 /* Remove port. */
5315 hmap_remove(&dp->ports, &port->node);
5316 reconfigure_datapath(dp);
5317
5318 /* Reinsert with new port number. */
5319 port->port_no = port_no;
5320 hmap_insert(&dp->ports, &port->node, hash_port_no(port_no));
5321 reconfigure_datapath(dp);
5322
5323 seq_change(dp->port_seq);
5324 unixctl_command_reply(conn, NULL);
5325
5326 exit:
5327 ovs_mutex_unlock(&dp->port_mutex);
5328 dp_netdev_unref(dp);
5329 }
5330
5331 static void
5332 dpif_dummy_register__(const char *type)
5333 {
5334 struct dpif_class *class;
5335
5336 class = xmalloc(sizeof *class);
5337 *class = dpif_netdev_class;
5338 class->type = xstrdup(type);
5339 dp_register_provider(class);
5340 }
5341
5342 static void
5343 dpif_dummy_override(const char *type)
5344 {
5345 int error;
5346
5347 /*
5348 * Ignore EAFNOSUPPORT to allow --enable-dummy=system with
5349 * a userland-only build. It's useful for testsuite.
5350 */
5351 error = dp_unregister_provider(type);
5352 if (error == 0 || error == EAFNOSUPPORT) {
5353 dpif_dummy_register__(type);
5354 }
5355 }
5356
5357 void
5358 dpif_dummy_register(enum dummy_level level)
5359 {
5360 if (level == DUMMY_OVERRIDE_ALL) {
5361 struct sset types;
5362 const char *type;
5363
5364 sset_init(&types);
5365 dp_enumerate_types(&types);
5366 SSET_FOR_EACH (type, &types) {
5367 dpif_dummy_override(type);
5368 }
5369 sset_destroy(&types);
5370 } else if (level == DUMMY_OVERRIDE_SYSTEM) {
5371 dpif_dummy_override("system");
5372 }
5373
5374 dpif_dummy_register__("dummy");
5375
5376 unixctl_command_register("dpif-dummy/change-port-number",
5377 "dp port new-number",
5378 3, 3, dpif_dummy_change_port_number, NULL);
5379 }
5380 \f
5381 /* Datapath Classifier. */
5382
5383 /* A set of rules that all have the same fields wildcarded. */
5384 struct dpcls_subtable {
5385 /* The fields are only used by writers. */
5386 struct cmap_node cmap_node OVS_GUARDED; /* Within dpcls 'subtables_map'. */
5387
5388 /* These fields are accessed by readers. */
5389 struct cmap rules; /* Contains "struct dpcls_rule"s. */
5390 uint32_t hit_cnt; /* Number of match hits in subtable in current
5391 optimization interval. */
5392 struct netdev_flow_key mask; /* Wildcards for fields (const). */
5393 /* 'mask' must be the last field, additional space is allocated here. */
5394 };
5395
5396 /* Initializes 'cls' as a classifier that initially contains no classification
5397 * rules. */
5398 static void
5399 dpcls_init(struct dpcls *cls)
5400 {
5401 cmap_init(&cls->subtables_map);
5402 pvector_init(&cls->subtables);
5403 }
5404
5405 static void
5406 dpcls_destroy_subtable(struct dpcls *cls, struct dpcls_subtable *subtable)
5407 {
5408 VLOG_DBG("Destroying subtable %p for in_port %d", subtable, cls->in_port);
5409 pvector_remove(&cls->subtables, subtable);
5410 cmap_remove(&cls->subtables_map, &subtable->cmap_node,
5411 subtable->mask.hash);
5412 cmap_destroy(&subtable->rules);
5413 ovsrcu_postpone(free, subtable);
5414 }
5415
5416 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
5417 * caller's responsibility.
5418 * May only be called after all the readers have been terminated. */
5419 static void
5420 dpcls_destroy(struct dpcls *cls)
5421 {
5422 if (cls) {
5423 struct dpcls_subtable *subtable;
5424
5425 CMAP_FOR_EACH (subtable, cmap_node, &cls->subtables_map) {
5426 ovs_assert(cmap_count(&subtable->rules) == 0);
5427 dpcls_destroy_subtable(cls, subtable);
5428 }
5429 cmap_destroy(&cls->subtables_map);
5430 pvector_destroy(&cls->subtables);
5431 }
5432 }
5433
5434 static struct dpcls_subtable *
5435 dpcls_create_subtable(struct dpcls *cls, const struct netdev_flow_key *mask)
5436 {
5437 struct dpcls_subtable *subtable;
5438
5439 /* Need to add one. */
5440 subtable = xmalloc(sizeof *subtable
5441 - sizeof subtable->mask.mf + mask->len);
5442 cmap_init(&subtable->rules);
5443 subtable->hit_cnt = 0;
5444 netdev_flow_key_clone(&subtable->mask, mask);
5445 cmap_insert(&cls->subtables_map, &subtable->cmap_node, mask->hash);
5446 /* Add the new subtable at the end of the pvector (with no hits yet) */
5447 pvector_insert(&cls->subtables, subtable, 0);
5448 VLOG_DBG("Creating %"PRIuSIZE". subtable %p for in_port %d",
5449 cmap_count(&cls->subtables_map), subtable, cls->in_port);
5450 pvector_publish(&cls->subtables);
5451
5452 return subtable;
5453 }
5454
5455 static inline struct dpcls_subtable *
5456 dpcls_find_subtable(struct dpcls *cls, const struct netdev_flow_key *mask)
5457 {
5458 struct dpcls_subtable *subtable;
5459
5460 CMAP_FOR_EACH_WITH_HASH (subtable, cmap_node, mask->hash,
5461 &cls->subtables_map) {
5462 if (netdev_flow_key_equal(&subtable->mask, mask)) {
5463 return subtable;
5464 }
5465 }
5466 return dpcls_create_subtable(cls, mask);
5467 }
5468
5469
5470 /* Periodically sort the dpcls subtable vectors according to hit counts */
5471 static void
5472 dpcls_sort_subtable_vector(struct dpcls *cls)
5473 {
5474 struct pvector *pvec = &cls->subtables;
5475 struct dpcls_subtable *subtable;
5476
5477 PVECTOR_FOR_EACH (subtable, pvec) {
5478 pvector_change_priority(pvec, subtable, subtable->hit_cnt);
5479 subtable->hit_cnt = 0;
5480 }
5481 pvector_publish(pvec);
5482 }
5483
5484 static inline void
5485 dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread *pmd)
5486 {
5487 struct dpcls *cls;
5488 long long int now = time_msec();
5489
5490 if (now > pmd->next_optimization) {
5491 /* Try to obtain the flow lock to block out revalidator threads.
5492 * If not possible, just try next time. */
5493 if (!ovs_mutex_trylock(&pmd->flow_mutex)) {
5494 /* Optimize each classifier */
5495 CMAP_FOR_EACH (cls, node, &pmd->classifiers) {
5496 dpcls_sort_subtable_vector(cls);
5497 }
5498 ovs_mutex_unlock(&pmd->flow_mutex);
5499 /* Start new measuring interval */
5500 pmd->next_optimization = now + DPCLS_OPTIMIZATION_INTERVAL;
5501 }
5502 }
5503 }
5504
5505 /* Insert 'rule' into 'cls'. */
5506 static void
5507 dpcls_insert(struct dpcls *cls, struct dpcls_rule *rule,
5508 const struct netdev_flow_key *mask)
5509 {
5510 struct dpcls_subtable *subtable = dpcls_find_subtable(cls, mask);
5511
5512 /* Refer to subtable's mask, also for later removal. */
5513 rule->mask = &subtable->mask;
5514 cmap_insert(&subtable->rules, &rule->cmap_node, rule->flow.hash);
5515 }
5516
5517 /* Removes 'rule' from 'cls', also destructing the 'rule'. */
5518 static void
5519 dpcls_remove(struct dpcls *cls, struct dpcls_rule *rule)
5520 {
5521 struct dpcls_subtable *subtable;
5522
5523 ovs_assert(rule->mask);
5524
5525 /* Get subtable from reference in rule->mask. */
5526 INIT_CONTAINER(subtable, rule->mask, mask);
5527 if (cmap_remove(&subtable->rules, &rule->cmap_node, rule->flow.hash)
5528 == 0) {
5529 /* Delete empty subtable. */
5530 dpcls_destroy_subtable(cls, subtable);
5531 pvector_publish(&cls->subtables);
5532 }
5533 }
5534
5535 /* Returns true if 'target' satisfies 'key' in 'mask', that is, if each 1-bit
5536 * in 'mask' the values in 'key' and 'target' are the same. */
5537 static inline bool
5538 dpcls_rule_matches_key(const struct dpcls_rule *rule,
5539 const struct netdev_flow_key *target)
5540 {
5541 const uint64_t *keyp = miniflow_get_values(&rule->flow.mf);
5542 const uint64_t *maskp = miniflow_get_values(&rule->mask->mf);
5543 uint64_t value;
5544
5545 NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value, target, rule->flow.mf.map) {
5546 if (OVS_UNLIKELY((value & *maskp++) != *keyp++)) {
5547 return false;
5548 }
5549 }
5550 return true;
5551 }
5552
5553 /* For each miniflow in 'keys' performs a classifier lookup writing the result
5554 * into the corresponding slot in 'rules'. If a particular entry in 'keys' is
5555 * NULL it is skipped.
5556 *
5557 * This function is optimized for use in the userspace datapath and therefore
5558 * does not implement a lot of features available in the standard
5559 * classifier_lookup() function. Specifically, it does not implement
5560 * priorities, instead returning any rule which matches the flow.
5561 *
5562 * Returns true if all miniflows found a corresponding rule. */
5563 static bool
5564 dpcls_lookup(struct dpcls *cls, const struct netdev_flow_key keys[],
5565 struct dpcls_rule **rules, const size_t cnt,
5566 int *num_lookups_p)
5567 {
5568 /* The received 'cnt' miniflows are the search-keys that will be processed
5569 * to find a matching entry into the available subtables.
5570 * The number of bits in map_type is equal to NETDEV_MAX_BURST. */
5571 typedef uint32_t map_type;
5572 #define MAP_BITS (sizeof(map_type) * CHAR_BIT)
5573 BUILD_ASSERT_DECL(MAP_BITS >= NETDEV_MAX_BURST);
5574
5575 struct dpcls_subtable *subtable;
5576
5577 map_type keys_map = TYPE_MAXIMUM(map_type); /* Set all bits. */
5578 map_type found_map;
5579 uint32_t hashes[MAP_BITS];
5580 const struct cmap_node *nodes[MAP_BITS];
5581
5582 if (cnt != MAP_BITS) {
5583 keys_map >>= MAP_BITS - cnt; /* Clear extra bits. */
5584 }
5585 memset(rules, 0, cnt * sizeof *rules);
5586
5587 int lookups_match = 0, subtable_pos = 1;
5588
5589 /* The Datapath classifier - aka dpcls - is composed of subtables.
5590 * Subtables are dynamically created as needed when new rules are inserted.
5591 * Each subtable collects rules with matches on a specific subset of packet
5592 * fields as defined by the subtable's mask. We proceed to process every
5593 * search-key against each subtable, but when a match is found for a
5594 * search-key, the search for that key can stop because the rules are
5595 * non-overlapping. */
5596 PVECTOR_FOR_EACH (subtable, &cls->subtables) {
5597 int i;
5598
5599 /* Compute hashes for the remaining keys. Each search-key is
5600 * masked with the subtable's mask to avoid hashing the wildcarded
5601 * bits. */
5602 ULLONG_FOR_EACH_1(i, keys_map) {
5603 hashes[i] = netdev_flow_key_hash_in_mask(&keys[i],
5604 &subtable->mask);
5605 }
5606 /* Lookup. */
5607 found_map = cmap_find_batch(&subtable->rules, keys_map, hashes, nodes);
5608 /* Check results. When the i-th bit of found_map is set, it means
5609 * that a set of nodes with a matching hash value was found for the
5610 * i-th search-key. Due to possible hash collisions we need to check
5611 * which of the found rules, if any, really matches our masked
5612 * search-key. */
5613 ULLONG_FOR_EACH_1(i, found_map) {
5614 struct dpcls_rule *rule;
5615
5616 CMAP_NODE_FOR_EACH (rule, cmap_node, nodes[i]) {
5617 if (OVS_LIKELY(dpcls_rule_matches_key(rule, &keys[i]))) {
5618 rules[i] = rule;
5619 /* Even at 20 Mpps the 32-bit hit_cnt cannot wrap
5620 * within one second optimization interval. */
5621 subtable->hit_cnt++;
5622 lookups_match += subtable_pos;
5623 goto next;
5624 }
5625 }
5626 /* None of the found rules was a match. Reset the i-th bit to
5627 * keep searching this key in the next subtable. */
5628 ULLONG_SET0(found_map, i); /* Did not match. */
5629 next:
5630 ; /* Keep Sparse happy. */
5631 }
5632 keys_map &= ~found_map; /* Clear the found rules. */
5633 if (!keys_map) {
5634 if (num_lookups_p) {
5635 *num_lookups_p = lookups_match;
5636 }
5637 return true; /* All found. */
5638 }
5639 subtable_pos++;
5640 }
5641 if (num_lookups_p) {
5642 *num_lookups_p = lookups_match;
5643 }
5644 return false; /* Some misses. */
5645 }