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