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