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