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