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