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