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