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