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