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