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