]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2009-2014, 2016-2018 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 | #include "dpif-netdev-private.h" | |
20 | ||
21 | #include <ctype.h> | |
22 | #include <errno.h> | |
23 | #include <fcntl.h> | |
24 | #include <inttypes.h> | |
25 | #include <net/if.h> | |
26 | #include <sys/types.h> | |
27 | #include <netinet/in.h> | |
28 | #include <stdint.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #include <sys/ioctl.h> | |
32 | #include <sys/socket.h> | |
33 | #include <sys/stat.h> | |
34 | #include <unistd.h> | |
35 | ||
36 | #include "bitmap.h" | |
37 | #include "cmap.h" | |
38 | #include "conntrack.h" | |
39 | #include "coverage.h" | |
40 | #include "ct-dpif.h" | |
41 | #include "csum.h" | |
42 | #include "dp-packet.h" | |
43 | #include "dpif.h" | |
44 | #include "dpif-netdev-perf.h" | |
45 | #include "dpif-provider.h" | |
46 | #include "dummy.h" | |
47 | #include "fat-rwlock.h" | |
48 | #include "flow.h" | |
49 | #include "hmapx.h" | |
50 | #include "id-pool.h" | |
51 | #include "ipf.h" | |
52 | #include "netdev.h" | |
53 | #include "netdev-offload.h" | |
54 | #include "netdev-provider.h" | |
55 | #include "netdev-vport.h" | |
56 | #include "netlink.h" | |
57 | #include "odp-execute.h" | |
58 | #include "odp-util.h" | |
59 | #include "openvswitch/dynamic-string.h" | |
60 | #include "openvswitch/list.h" | |
61 | #include "openvswitch/match.h" | |
62 | #include "openvswitch/ofp-parse.h" | |
63 | #include "openvswitch/ofp-print.h" | |
64 | #include "openvswitch/ofpbuf.h" | |
65 | #include "openvswitch/shash.h" | |
66 | #include "openvswitch/vlog.h" | |
67 | #include "ovs-numa.h" | |
68 | #include "ovs-rcu.h" | |
69 | #include "packets.h" | |
70 | #include "openvswitch/poll-loop.h" | |
71 | #include "pvector.h" | |
72 | #include "random.h" | |
73 | #include "seq.h" | |
74 | #include "smap.h" | |
75 | #include "sset.h" | |
76 | #include "timeval.h" | |
77 | #include "tnl-neigh-cache.h" | |
78 | #include "tnl-ports.h" | |
79 | #include "unixctl.h" | |
80 | #include "util.h" | |
81 | #include "uuid.h" | |
82 | ||
83 | VLOG_DEFINE_THIS_MODULE(dpif_netdev); | |
84 | ||
85 | /* Auto Load Balancing Defaults */ | |
86 | #define ALB_ACCEPTABLE_IMPROVEMENT 25 | |
87 | #define ALB_PMD_LOAD_THRESHOLD 95 | |
88 | #define ALB_PMD_REBALANCE_POLL_INTERVAL 1 /* 1 Min */ | |
89 | #define MIN_TO_MSEC 60000 | |
90 | ||
91 | #define FLOW_DUMP_MAX_BATCH 50 | |
92 | /* Use per thread recirc_depth to prevent recirculation loop. */ | |
93 | #define MAX_RECIRC_DEPTH 6 | |
94 | DEFINE_STATIC_PER_THREAD_DATA(uint32_t, recirc_depth, 0) | |
95 | ||
96 | /* Use instant packet send by default. */ | |
97 | #define DEFAULT_TX_FLUSH_INTERVAL 0 | |
98 | ||
99 | /* Configuration parameters. */ | |
100 | enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */ | |
101 | enum { MAX_METERS = 65536 }; /* Maximum number of meters. */ | |
102 | enum { MAX_BANDS = 8 }; /* Maximum number of bands / meter. */ | |
103 | enum { N_METER_LOCKS = 64 }; /* Maximum number of meters. */ | |
104 | ||
105 | /* Protects against changes to 'dp_netdevs'. */ | |
106 | static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER; | |
107 | ||
108 | /* Contains all 'struct dp_netdev's. */ | |
109 | static struct shash dp_netdevs OVS_GUARDED_BY(dp_netdev_mutex) | |
110 | = SHASH_INITIALIZER(&dp_netdevs); | |
111 | ||
112 | static struct vlog_rate_limit upcall_rl = VLOG_RATE_LIMIT_INIT(600, 600); | |
113 | ||
114 | #define DP_NETDEV_CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \ | |
115 | | CS_INVALID | CS_REPLY_DIR | CS_TRACKED \ | |
116 | | CS_SRC_NAT | CS_DST_NAT) | |
117 | #define DP_NETDEV_CS_UNSUPPORTED_MASK (~(uint32_t)DP_NETDEV_CS_SUPPORTED_MASK) | |
118 | ||
119 | static struct odp_support dp_netdev_support = { | |
120 | .max_vlan_headers = SIZE_MAX, | |
121 | .max_mpls_depth = SIZE_MAX, | |
122 | .recirc = true, | |
123 | .ct_state = true, | |
124 | .ct_zone = true, | |
125 | .ct_mark = true, | |
126 | .ct_label = true, | |
127 | .ct_state_nat = true, | |
128 | .ct_orig_tuple = true, | |
129 | .ct_orig_tuple6 = true, | |
130 | }; | |
131 | ||
132 | /* EMC cache and SMC cache compose the datapath flow cache (DFC) | |
133 | * | |
134 | * Exact match cache for frequently used flows | |
135 | * | |
136 | * The cache uses a 32-bit hash of the packet (which can be the RSS hash) to | |
137 | * search its entries for a miniflow that matches exactly the miniflow of the | |
138 | * packet. It stores the 'dpcls_rule' (rule) that matches the miniflow. | |
139 | * | |
140 | * A cache entry holds a reference to its 'dp_netdev_flow'. | |
141 | * | |
142 | * A miniflow with a given hash can be in one of EM_FLOW_HASH_SEGS different | |
143 | * entries. The 32-bit hash is split into EM_FLOW_HASH_SEGS values (each of | |
144 | * them is EM_FLOW_HASH_SHIFT bits wide and the remainder is thrown away). Each | |
145 | * value is the index of a cache entry where the miniflow could be. | |
146 | * | |
147 | * | |
148 | * Signature match cache (SMC) | |
149 | * | |
150 | * This cache stores a 16-bit signature for each flow without storing keys, and | |
151 | * stores the corresponding 16-bit flow_table index to the 'dp_netdev_flow'. | |
152 | * Each flow thus occupies 32bit which is much more memory efficient than EMC. | |
153 | * SMC uses a set-associative design that each bucket contains | |
154 | * SMC_ENTRY_PER_BUCKET number of entries. | |
155 | * Since 16-bit flow_table index is used, if there are more than 2^16 | |
156 | * dp_netdev_flow, SMC will miss them that cannot be indexed by a 16-bit value. | |
157 | * | |
158 | * | |
159 | * Thread-safety | |
160 | * ============= | |
161 | * | |
162 | * Each pmd_thread has its own private exact match cache. | |
163 | * If dp_netdev_input is not called from a pmd thread, a mutex is used. | |
164 | */ | |
165 | ||
166 | #define EM_FLOW_HASH_SHIFT 13 | |
167 | #define EM_FLOW_HASH_ENTRIES (1u << EM_FLOW_HASH_SHIFT) | |
168 | #define EM_FLOW_HASH_MASK (EM_FLOW_HASH_ENTRIES - 1) | |
169 | #define EM_FLOW_HASH_SEGS 2 | |
170 | ||
171 | /* SMC uses a set-associative design. A bucket contains a set of entries that | |
172 | * a flow item can occupy. For now, it uses one hash function rather than two | |
173 | * as for the EMC design. */ | |
174 | #define SMC_ENTRY_PER_BUCKET 4 | |
175 | #define SMC_ENTRIES (1u << 20) | |
176 | #define SMC_BUCKET_CNT (SMC_ENTRIES / SMC_ENTRY_PER_BUCKET) | |
177 | #define SMC_MASK (SMC_BUCKET_CNT - 1) | |
178 | ||
179 | /* Default EMC insert probability is 1 / DEFAULT_EM_FLOW_INSERT_INV_PROB */ | |
180 | #define DEFAULT_EM_FLOW_INSERT_INV_PROB 100 | |
181 | #define DEFAULT_EM_FLOW_INSERT_MIN (UINT32_MAX / \ | |
182 | DEFAULT_EM_FLOW_INSERT_INV_PROB) | |
183 | ||
184 | struct emc_entry { | |
185 | struct dp_netdev_flow *flow; | |
186 | struct netdev_flow_key key; /* key.hash used for emc hash value. */ | |
187 | }; | |
188 | ||
189 | struct emc_cache { | |
190 | struct emc_entry entries[EM_FLOW_HASH_ENTRIES]; | |
191 | int sweep_idx; /* For emc_cache_slow_sweep(). */ | |
192 | }; | |
193 | ||
194 | struct smc_bucket { | |
195 | uint16_t sig[SMC_ENTRY_PER_BUCKET]; | |
196 | uint16_t flow_idx[SMC_ENTRY_PER_BUCKET]; | |
197 | }; | |
198 | ||
199 | /* Signature match cache, differentiate from EMC cache */ | |
200 | struct smc_cache { | |
201 | struct smc_bucket buckets[SMC_BUCKET_CNT]; | |
202 | }; | |
203 | ||
204 | struct dfc_cache { | |
205 | struct emc_cache emc_cache; | |
206 | struct smc_cache smc_cache; | |
207 | }; | |
208 | ||
209 | /* Iterate in the exact match cache through every entry that might contain a | |
210 | * miniflow with hash 'HASH'. */ | |
211 | #define EMC_FOR_EACH_POS_WITH_HASH(EMC, CURRENT_ENTRY, HASH) \ | |
212 | for (uint32_t i__ = 0, srch_hash__ = (HASH); \ | |
213 | (CURRENT_ENTRY) = &(EMC)->entries[srch_hash__ & EM_FLOW_HASH_MASK], \ | |
214 | i__ < EM_FLOW_HASH_SEGS; \ | |
215 | i__++, srch_hash__ >>= EM_FLOW_HASH_SHIFT) | |
216 | \f | |
217 | /* Simple non-wildcarding single-priority classifier. */ | |
218 | ||
219 | /* Time in microseconds between successive optimizations of the dpcls | |
220 | * subtable vector */ | |
221 | #define DPCLS_OPTIMIZATION_INTERVAL 1000000LL | |
222 | ||
223 | /* Time in microseconds of the interval in which rxq processing cycles used | |
224 | * in rxq to pmd assignments is measured and stored. */ | |
225 | #define PMD_RXQ_INTERVAL_LEN 10000000LL | |
226 | ||
227 | /* Number of intervals for which cycles are stored | |
228 | * and used during rxq to pmd assignment. */ | |
229 | #define PMD_RXQ_INTERVAL_MAX 6 | |
230 | ||
231 | struct dpcls { | |
232 | struct cmap_node node; /* Within dp_netdev_pmd_thread.classifiers */ | |
233 | odp_port_t in_port; | |
234 | struct cmap subtables_map; | |
235 | struct pvector subtables; | |
236 | }; | |
237 | ||
238 | /* Data structure to keep packet order till fastpath processing. */ | |
239 | struct dp_packet_flow_map { | |
240 | struct dp_packet *packet; | |
241 | struct dp_netdev_flow *flow; | |
242 | uint16_t tcp_flags; | |
243 | }; | |
244 | ||
245 | static void dpcls_init(struct dpcls *); | |
246 | static void dpcls_destroy(struct dpcls *); | |
247 | static void dpcls_sort_subtable_vector(struct dpcls *); | |
248 | static void dpcls_insert(struct dpcls *, struct dpcls_rule *, | |
249 | const struct netdev_flow_key *mask); | |
250 | static void dpcls_remove(struct dpcls *, struct dpcls_rule *); | |
251 | static bool dpcls_lookup(struct dpcls *cls, | |
252 | const struct netdev_flow_key *keys[], | |
253 | struct dpcls_rule **rules, size_t cnt, | |
254 | int *num_lookups_p); | |
255 | ||
256 | /* Set of supported meter flags */ | |
257 | #define DP_SUPPORTED_METER_FLAGS_MASK \ | |
258 | (OFPMF13_STATS | OFPMF13_PKTPS | OFPMF13_KBPS | OFPMF13_BURST) | |
259 | ||
260 | /* Set of supported meter band types */ | |
261 | #define DP_SUPPORTED_METER_BAND_TYPES \ | |
262 | ( 1 << OFPMBT13_DROP ) | |
263 | ||
264 | struct dp_meter_band { | |
265 | struct ofputil_meter_band up; /* type, prec_level, pad, rate, burst_size */ | |
266 | uint32_t bucket; /* In 1/1000 packets (for PKTPS), or in bits (for KBPS) */ | |
267 | uint64_t packet_count; | |
268 | uint64_t byte_count; | |
269 | }; | |
270 | ||
271 | struct dp_meter { | |
272 | uint16_t flags; | |
273 | uint16_t n_bands; | |
274 | uint32_t max_delta_t; | |
275 | uint64_t used; | |
276 | uint64_t packet_count; | |
277 | uint64_t byte_count; | |
278 | struct dp_meter_band bands[]; | |
279 | }; | |
280 | ||
281 | struct pmd_auto_lb { | |
282 | bool auto_lb_requested; /* Auto load balancing requested by user. */ | |
283 | bool is_enabled; /* Current status of Auto load balancing. */ | |
284 | uint64_t rebalance_intvl; | |
285 | uint64_t rebalance_poll_timer; | |
286 | }; | |
287 | ||
288 | /* Datapath based on the network device interface from netdev.h. | |
289 | * | |
290 | * | |
291 | * Thread-safety | |
292 | * ============= | |
293 | * | |
294 | * Some members, marked 'const', are immutable. Accessing other members | |
295 | * requires synchronization, as noted in more detail below. | |
296 | * | |
297 | * Acquisition order is, from outermost to innermost: | |
298 | * | |
299 | * dp_netdev_mutex (global) | |
300 | * port_mutex | |
301 | * non_pmd_mutex | |
302 | */ | |
303 | struct dp_netdev { | |
304 | const struct dpif_class *const class; | |
305 | const char *const name; | |
306 | struct dpif *dpif; | |
307 | struct ovs_refcount ref_cnt; | |
308 | atomic_flag destroyed; | |
309 | ||
310 | /* Ports. | |
311 | * | |
312 | * Any lookup into 'ports' or any access to the dp_netdev_ports found | |
313 | * through 'ports' requires taking 'port_mutex'. */ | |
314 | struct ovs_mutex port_mutex; | |
315 | struct hmap ports; | |
316 | struct seq *port_seq; /* Incremented whenever a port changes. */ | |
317 | ||
318 | /* The time that a packet can wait in output batch for sending. */ | |
319 | atomic_uint32_t tx_flush_interval; | |
320 | ||
321 | /* Meters. */ | |
322 | struct ovs_mutex meter_locks[N_METER_LOCKS]; | |
323 | struct dp_meter *meters[MAX_METERS]; /* Meter bands. */ | |
324 | ||
325 | /* Probability of EMC insertions is a factor of 'emc_insert_min'.*/ | |
326 | OVS_ALIGNED_VAR(CACHE_LINE_SIZE) atomic_uint32_t emc_insert_min; | |
327 | /* Enable collection of PMD performance metrics. */ | |
328 | atomic_bool pmd_perf_metrics; | |
329 | /* Enable the SMC cache from ovsdb config */ | |
330 | atomic_bool smc_enable_db; | |
331 | ||
332 | /* Protects access to ofproto-dpif-upcall interface during revalidator | |
333 | * thread synchronization. */ | |
334 | struct fat_rwlock upcall_rwlock; | |
335 | upcall_callback *upcall_cb; /* Callback function for executing upcalls. */ | |
336 | void *upcall_aux; | |
337 | ||
338 | /* Callback function for notifying the purging of dp flows (during | |
339 | * reseting pmd deletion). */ | |
340 | dp_purge_callback *dp_purge_cb; | |
341 | void *dp_purge_aux; | |
342 | ||
343 | /* Stores all 'struct dp_netdev_pmd_thread's. */ | |
344 | struct cmap poll_threads; | |
345 | /* id pool for per thread static_tx_qid. */ | |
346 | struct id_pool *tx_qid_pool; | |
347 | struct ovs_mutex tx_qid_pool_mutex; | |
348 | /* Use measured cycles for rxq to pmd assignment. */ | |
349 | bool pmd_rxq_assign_cyc; | |
350 | ||
351 | /* Protects the access of the 'struct dp_netdev_pmd_thread' | |
352 | * instance for non-pmd thread. */ | |
353 | struct ovs_mutex non_pmd_mutex; | |
354 | ||
355 | /* Each pmd thread will store its pointer to | |
356 | * 'struct dp_netdev_pmd_thread' in 'per_pmd_key'. */ | |
357 | ovsthread_key_t per_pmd_key; | |
358 | ||
359 | struct seq *reconfigure_seq; | |
360 | uint64_t last_reconfigure_seq; | |
361 | ||
362 | /* Cpu mask for pin of pmd threads. */ | |
363 | char *pmd_cmask; | |
364 | ||
365 | uint64_t last_tnl_conf_seq; | |
366 | ||
367 | struct conntrack *conntrack; | |
368 | struct pmd_auto_lb pmd_alb; | |
369 | }; | |
370 | ||
371 | static void meter_lock(const struct dp_netdev *dp, uint32_t meter_id) | |
372 | OVS_ACQUIRES(dp->meter_locks[meter_id % N_METER_LOCKS]) | |
373 | { | |
374 | ovs_mutex_lock(&dp->meter_locks[meter_id % N_METER_LOCKS]); | |
375 | } | |
376 | ||
377 | static void meter_unlock(const struct dp_netdev *dp, uint32_t meter_id) | |
378 | OVS_RELEASES(dp->meter_locks[meter_id % N_METER_LOCKS]) | |
379 | { | |
380 | ovs_mutex_unlock(&dp->meter_locks[meter_id % N_METER_LOCKS]); | |
381 | } | |
382 | ||
383 | ||
384 | static struct dp_netdev_port *dp_netdev_lookup_port(const struct dp_netdev *dp, | |
385 | odp_port_t) | |
386 | OVS_REQUIRES(dp->port_mutex); | |
387 | ||
388 | enum rxq_cycles_counter_type { | |
389 | RXQ_CYCLES_PROC_CURR, /* Cycles spent successfully polling and | |
390 | processing packets during the current | |
391 | interval. */ | |
392 | RXQ_CYCLES_PROC_HIST, /* Total cycles of all intervals that are used | |
393 | during rxq to pmd assignment. */ | |
394 | RXQ_N_CYCLES | |
395 | }; | |
396 | ||
397 | enum { | |
398 | DP_NETDEV_FLOW_OFFLOAD_OP_ADD, | |
399 | DP_NETDEV_FLOW_OFFLOAD_OP_MOD, | |
400 | DP_NETDEV_FLOW_OFFLOAD_OP_DEL, | |
401 | }; | |
402 | ||
403 | struct dp_flow_offload_item { | |
404 | struct dp_netdev_pmd_thread *pmd; | |
405 | struct dp_netdev_flow *flow; | |
406 | int op; | |
407 | struct match match; | |
408 | struct nlattr *actions; | |
409 | size_t actions_len; | |
410 | ||
411 | struct ovs_list node; | |
412 | }; | |
413 | ||
414 | struct dp_flow_offload { | |
415 | struct ovs_mutex mutex; | |
416 | struct ovs_list list; | |
417 | pthread_cond_t cond; | |
418 | }; | |
419 | ||
420 | static struct dp_flow_offload dp_flow_offload = { | |
421 | .mutex = OVS_MUTEX_INITIALIZER, | |
422 | .list = OVS_LIST_INITIALIZER(&dp_flow_offload.list), | |
423 | }; | |
424 | ||
425 | static struct ovsthread_once offload_thread_once | |
426 | = OVSTHREAD_ONCE_INITIALIZER; | |
427 | ||
428 | #define XPS_TIMEOUT 500000LL /* In microseconds. */ | |
429 | ||
430 | /* Contained by struct dp_netdev_port's 'rxqs' member. */ | |
431 | struct dp_netdev_rxq { | |
432 | struct dp_netdev_port *port; | |
433 | struct netdev_rxq *rx; | |
434 | unsigned core_id; /* Core to which this queue should be | |
435 | pinned. OVS_CORE_UNSPEC if the | |
436 | queue doesn't need to be pinned to a | |
437 | particular core. */ | |
438 | unsigned intrvl_idx; /* Write index for 'cycles_intrvl'. */ | |
439 | struct dp_netdev_pmd_thread *pmd; /* pmd thread that polls this queue. */ | |
440 | bool is_vhost; /* Is rxq of a vhost port. */ | |
441 | ||
442 | /* Counters of cycles spent successfully polling and processing pkts. */ | |
443 | atomic_ullong cycles[RXQ_N_CYCLES]; | |
444 | /* We store PMD_RXQ_INTERVAL_MAX intervals of data for an rxq and then | |
445 | sum them to yield the cycles used for an rxq. */ | |
446 | atomic_ullong cycles_intrvl[PMD_RXQ_INTERVAL_MAX]; | |
447 | }; | |
448 | ||
449 | /* A port in a netdev-based datapath. */ | |
450 | struct dp_netdev_port { | |
451 | odp_port_t port_no; | |
452 | bool dynamic_txqs; /* If true XPS will be used. */ | |
453 | bool need_reconfigure; /* True if we should reconfigure netdev. */ | |
454 | struct netdev *netdev; | |
455 | struct hmap_node node; /* Node in dp_netdev's 'ports'. */ | |
456 | struct netdev_saved_flags *sf; | |
457 | struct dp_netdev_rxq *rxqs; | |
458 | unsigned n_rxq; /* Number of elements in 'rxqs' */ | |
459 | unsigned *txq_used; /* Number of threads that use each tx queue. */ | |
460 | struct ovs_mutex txq_used_mutex; | |
461 | bool emc_enabled; /* If true EMC will be used. */ | |
462 | char *type; /* Port type as requested by user. */ | |
463 | char *rxq_affinity_list; /* Requested affinity of rx queues. */ | |
464 | }; | |
465 | ||
466 | /* Contained by struct dp_netdev_flow's 'stats' member. */ | |
467 | struct dp_netdev_flow_stats { | |
468 | atomic_llong used; /* Last used time, in monotonic msecs. */ | |
469 | atomic_ullong packet_count; /* Number of packets matched. */ | |
470 | atomic_ullong byte_count; /* Number of bytes matched. */ | |
471 | atomic_uint16_t tcp_flags; /* Bitwise-OR of seen tcp_flags values. */ | |
472 | }; | |
473 | ||
474 | /* A flow in 'dp_netdev_pmd_thread's 'flow_table'. | |
475 | * | |
476 | * | |
477 | * Thread-safety | |
478 | * ============= | |
479 | * | |
480 | * Except near the beginning or ending of its lifespan, rule 'rule' belongs to | |
481 | * its pmd thread's classifier. The text below calls this classifier 'cls'. | |
482 | * | |
483 | * Motivation | |
484 | * ---------- | |
485 | * | |
486 | * The thread safety rules described here for "struct dp_netdev_flow" are | |
487 | * motivated by two goals: | |
488 | * | |
489 | * - Prevent threads that read members of "struct dp_netdev_flow" from | |
490 | * reading bad data due to changes by some thread concurrently modifying | |
491 | * those members. | |
492 | * | |
493 | * - Prevent two threads making changes to members of a given "struct | |
494 | * dp_netdev_flow" from interfering with each other. | |
495 | * | |
496 | * | |
497 | * Rules | |
498 | * ----- | |
499 | * | |
500 | * A flow 'flow' may be accessed without a risk of being freed during an RCU | |
501 | * grace period. Code that needs to hold onto a flow for a while | |
502 | * should try incrementing 'flow->ref_cnt' with dp_netdev_flow_ref(). | |
503 | * | |
504 | * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the | |
505 | * flow from being deleted from 'cls' and it doesn't protect members of 'flow' | |
506 | * from modification. | |
507 | * | |
508 | * Some members, marked 'const', are immutable. Accessing other members | |
509 | * requires synchronization, as noted in more detail below. | |
510 | */ | |
511 | struct dp_netdev_flow { | |
512 | const struct flow flow; /* Unmasked flow that created this entry. */ | |
513 | /* Hash table index by unmasked flow. */ | |
514 | const struct cmap_node node; /* In owning dp_netdev_pmd_thread's */ | |
515 | /* 'flow_table'. */ | |
516 | const struct cmap_node mark_node; /* In owning flow_mark's mark_to_flow */ | |
517 | const ovs_u128 ufid; /* Unique flow identifier. */ | |
518 | const ovs_u128 mega_ufid; /* Unique mega flow identifier. */ | |
519 | const unsigned pmd_id; /* The 'core_id' of pmd thread owning this */ | |
520 | /* flow. */ | |
521 | ||
522 | /* Number of references. | |
523 | * The classifier owns one reference. | |
524 | * Any thread trying to keep a rule from being freed should hold its own | |
525 | * reference. */ | |
526 | struct ovs_refcount ref_cnt; | |
527 | ||
528 | bool dead; | |
529 | uint32_t mark; /* Unique flow mark assigned to a flow */ | |
530 | ||
531 | /* Statistics. */ | |
532 | struct dp_netdev_flow_stats stats; | |
533 | ||
534 | /* Actions. */ | |
535 | OVSRCU_TYPE(struct dp_netdev_actions *) actions; | |
536 | ||
537 | /* While processing a group of input packets, the datapath uses the next | |
538 | * member to store a pointer to the output batch for the flow. It is | |
539 | * reset after the batch has been sent out (See dp_netdev_queue_batches(), | |
540 | * packet_batch_per_flow_init() and packet_batch_per_flow_execute()). */ | |
541 | struct packet_batch_per_flow *batch; | |
542 | ||
543 | /* Packet classification. */ | |
544 | struct dpcls_rule cr; /* In owning dp_netdev's 'cls'. */ | |
545 | /* 'cr' must be the last member. */ | |
546 | }; | |
547 | ||
548 | static void dp_netdev_flow_unref(struct dp_netdev_flow *); | |
549 | static bool dp_netdev_flow_ref(struct dp_netdev_flow *); | |
550 | static int dpif_netdev_flow_from_nlattrs(const struct nlattr *, uint32_t, | |
551 | struct flow *, bool); | |
552 | ||
553 | /* A set of datapath actions within a "struct dp_netdev_flow". | |
554 | * | |
555 | * | |
556 | * Thread-safety | |
557 | * ============= | |
558 | * | |
559 | * A struct dp_netdev_actions 'actions' is protected with RCU. */ | |
560 | struct dp_netdev_actions { | |
561 | /* These members are immutable: they do not change during the struct's | |
562 | * lifetime. */ | |
563 | unsigned int size; /* Size of 'actions', in bytes. */ | |
564 | struct nlattr actions[]; /* Sequence of OVS_ACTION_ATTR_* attributes. */ | |
565 | }; | |
566 | ||
567 | struct dp_netdev_actions *dp_netdev_actions_create(const struct nlattr *, | |
568 | size_t); | |
569 | struct dp_netdev_actions *dp_netdev_flow_get_actions( | |
570 | const struct dp_netdev_flow *); | |
571 | static void dp_netdev_actions_free(struct dp_netdev_actions *); | |
572 | ||
573 | struct polled_queue { | |
574 | struct dp_netdev_rxq *rxq; | |
575 | odp_port_t port_no; | |
576 | bool emc_enabled; | |
577 | bool rxq_enabled; | |
578 | uint64_t change_seq; | |
579 | }; | |
580 | ||
581 | /* Contained by struct dp_netdev_pmd_thread's 'poll_list' member. */ | |
582 | struct rxq_poll { | |
583 | struct dp_netdev_rxq *rxq; | |
584 | struct hmap_node node; | |
585 | }; | |
586 | ||
587 | /* Contained by struct dp_netdev_pmd_thread's 'send_port_cache', | |
588 | * 'tnl_port_cache' or 'tx_ports'. */ | |
589 | struct tx_port { | |
590 | struct dp_netdev_port *port; | |
591 | int qid; | |
592 | long long last_used; | |
593 | struct hmap_node node; | |
594 | long long flush_time; | |
595 | struct dp_packet_batch output_pkts; | |
596 | struct dp_netdev_rxq *output_pkts_rxqs[NETDEV_MAX_BURST]; | |
597 | }; | |
598 | ||
599 | /* A set of properties for the current processing loop that is not directly | |
600 | * associated with the pmd thread itself, but with the packets being | |
601 | * processed or the short-term system configuration (for example, time). | |
602 | * Contained by struct dp_netdev_pmd_thread's 'ctx' member. */ | |
603 | struct dp_netdev_pmd_thread_ctx { | |
604 | /* Latest measured time. See 'pmd_thread_ctx_time_update()'. */ | |
605 | long long now; | |
606 | /* RX queue from which last packet was received. */ | |
607 | struct dp_netdev_rxq *last_rxq; | |
608 | /* EMC insertion probability context for the current processing cycle. */ | |
609 | uint32_t emc_insert_min; | |
610 | }; | |
611 | ||
612 | /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate | |
613 | * the performance overhead of interrupt processing. Therefore netdev can | |
614 | * not implement rx-wait for these devices. dpif-netdev needs to poll | |
615 | * these device to check for recv buffer. pmd-thread does polling for | |
616 | * devices assigned to itself. | |
617 | * | |
618 | * DPDK used PMD for accessing NIC. | |
619 | * | |
620 | * Note, instance with cpu core id NON_PMD_CORE_ID will be reserved for | |
621 | * I/O of all non-pmd threads. There will be no actual thread created | |
622 | * for the instance. | |
623 | * | |
624 | * Each struct has its own flow cache and classifier per managed ingress port. | |
625 | * For packets received on ingress port, a look up is done on corresponding PMD | |
626 | * thread's flow cache and in case of a miss, lookup is performed in the | |
627 | * corresponding classifier of port. Packets are executed with the found | |
628 | * actions in either case. | |
629 | * */ | |
630 | struct dp_netdev_pmd_thread { | |
631 | struct dp_netdev *dp; | |
632 | struct ovs_refcount ref_cnt; /* Every reference must be refcount'ed. */ | |
633 | struct cmap_node node; /* In 'dp->poll_threads'. */ | |
634 | ||
635 | /* Per thread exact-match cache. Note, the instance for cpu core | |
636 | * NON_PMD_CORE_ID can be accessed by multiple threads, and thusly | |
637 | * need to be protected by 'non_pmd_mutex'. Every other instance | |
638 | * will only be accessed by its own pmd thread. */ | |
639 | OVS_ALIGNED_VAR(CACHE_LINE_SIZE) struct dfc_cache flow_cache; | |
640 | ||
641 | /* Flow-Table and classifiers | |
642 | * | |
643 | * Writers of 'flow_table' must take the 'flow_mutex'. Corresponding | |
644 | * changes to 'classifiers' must be made while still holding the | |
645 | * 'flow_mutex'. | |
646 | */ | |
647 | struct ovs_mutex flow_mutex; | |
648 | struct cmap flow_table OVS_GUARDED; /* Flow table. */ | |
649 | ||
650 | /* One classifier per in_port polled by the pmd */ | |
651 | struct cmap classifiers; | |
652 | /* Periodically sort subtable vectors according to hit frequencies */ | |
653 | long long int next_optimization; | |
654 | /* End of the next time interval for which processing cycles | |
655 | are stored for each polled rxq. */ | |
656 | long long int rxq_next_cycle_store; | |
657 | ||
658 | /* Last interval timestamp. */ | |
659 | uint64_t intrvl_tsc_prev; | |
660 | /* Last interval cycles. */ | |
661 | atomic_ullong intrvl_cycles; | |
662 | ||
663 | /* Current context of the PMD thread. */ | |
664 | struct dp_netdev_pmd_thread_ctx ctx; | |
665 | ||
666 | struct seq *reload_seq; | |
667 | uint64_t last_reload_seq; | |
668 | ||
669 | /* These are atomic variables used as a synchronization and configuration | |
670 | * points for thread reload/exit. | |
671 | * | |
672 | * 'reload' atomic is the main one and it's used as a memory | |
673 | * synchronization point for all other knobs and data. | |
674 | * | |
675 | * For a thread that requests PMD reload: | |
676 | * | |
677 | * * All changes that should be visible to the PMD thread must be made | |
678 | * before setting the 'reload'. These changes could use any memory | |
679 | * ordering model including 'relaxed'. | |
680 | * * Setting the 'reload' atomic should occur in the same thread where | |
681 | * all other PMD configuration options updated. | |
682 | * * Setting the 'reload' atomic should be done with 'release' memory | |
683 | * ordering model or stricter. This will guarantee that all previous | |
684 | * changes (including non-atomic and 'relaxed') will be visible to | |
685 | * the PMD thread. | |
686 | * * To check that reload is done, thread should poll the 'reload' atomic | |
687 | * to become 'false'. Polling should be done with 'acquire' memory | |
688 | * ordering model or stricter. This ensures that PMD thread completed | |
689 | * the reload process. | |
690 | * | |
691 | * For the PMD thread: | |
692 | * | |
693 | * * PMD thread should read 'reload' atomic with 'acquire' memory | |
694 | * ordering model or stricter. This will guarantee that all changes | |
695 | * made before setting the 'reload' in the requesting thread will be | |
696 | * visible to the PMD thread. | |
697 | * * All other configuration data could be read with any memory | |
698 | * ordering model (including non-atomic and 'relaxed') but *only after* | |
699 | * reading the 'reload' atomic set to 'true'. | |
700 | * * When the PMD reload done, PMD should (optionally) set all the below | |
701 | * knobs except the 'reload' to their default ('false') values and | |
702 | * (mandatory), as the last step, set the 'reload' to 'false' using | |
703 | * 'release' memory ordering model or stricter. This will inform the | |
704 | * requesting thread that PMD has completed a reload cycle. | |
705 | */ | |
706 | atomic_bool reload; /* Do we need to reload ports? */ | |
707 | atomic_bool wait_for_reload; /* Can we busy wait for the next reload? */ | |
708 | atomic_bool reload_tx_qid; /* Do we need to reload static_tx_qid? */ | |
709 | atomic_bool exit; /* For terminating the pmd thread. */ | |
710 | ||
711 | pthread_t thread; | |
712 | unsigned core_id; /* CPU core id of this pmd thread. */ | |
713 | int numa_id; /* numa node id of this pmd thread. */ | |
714 | bool isolated; | |
715 | ||
716 | /* Queue id used by this pmd thread to send packets on all netdevs if | |
717 | * XPS disabled for this netdev. All static_tx_qid's are unique and less | |
718 | * than 'cmap_count(dp->poll_threads)'. */ | |
719 | uint32_t static_tx_qid; | |
720 | ||
721 | /* Number of filled output batches. */ | |
722 | int n_output_batches; | |
723 | ||
724 | struct ovs_mutex port_mutex; /* Mutex for 'poll_list' and 'tx_ports'. */ | |
725 | /* List of rx queues to poll. */ | |
726 | struct hmap poll_list OVS_GUARDED; | |
727 | /* Map of 'tx_port's used for transmission. Written by the main thread, | |
728 | * read by the pmd thread. */ | |
729 | struct hmap tx_ports OVS_GUARDED; | |
730 | ||
731 | /* These are thread-local copies of 'tx_ports'. One contains only tunnel | |
732 | * ports (that support push_tunnel/pop_tunnel), the other contains ports | |
733 | * with at least one txq (that support send). A port can be in both. | |
734 | * | |
735 | * There are two separate maps to make sure that we don't try to execute | |
736 | * OUTPUT on a device which has 0 txqs or PUSH/POP on a non-tunnel device. | |
737 | * | |
738 | * The instances for cpu core NON_PMD_CORE_ID can be accessed by multiple | |
739 | * threads, and thusly need to be protected by 'non_pmd_mutex'. Every | |
740 | * other instance will only be accessed by its own pmd thread. */ | |
741 | struct hmap tnl_port_cache; | |
742 | struct hmap send_port_cache; | |
743 | ||
744 | /* Keep track of detailed PMD performance statistics. */ | |
745 | struct pmd_perf_stats perf_stats; | |
746 | ||
747 | /* Stats from previous iteration used by automatic pmd | |
748 | * load balance logic. */ | |
749 | uint64_t prev_stats[PMD_N_STATS]; | |
750 | atomic_count pmd_overloaded; | |
751 | ||
752 | /* Set to true if the pmd thread needs to be reloaded. */ | |
753 | bool need_reload; | |
754 | }; | |
755 | ||
756 | /* Interface to netdev-based datapath. */ | |
757 | struct dpif_netdev { | |
758 | struct dpif dpif; | |
759 | struct dp_netdev *dp; | |
760 | uint64_t last_port_seq; | |
761 | }; | |
762 | ||
763 | static int get_port_by_number(struct dp_netdev *dp, odp_port_t port_no, | |
764 | struct dp_netdev_port **portp) | |
765 | OVS_REQUIRES(dp->port_mutex); | |
766 | static int get_port_by_name(struct dp_netdev *dp, const char *devname, | |
767 | struct dp_netdev_port **portp) | |
768 | OVS_REQUIRES(dp->port_mutex); | |
769 | static void dp_netdev_free(struct dp_netdev *) | |
770 | OVS_REQUIRES(dp_netdev_mutex); | |
771 | static int do_add_port(struct dp_netdev *dp, const char *devname, | |
772 | const char *type, odp_port_t port_no) | |
773 | OVS_REQUIRES(dp->port_mutex); | |
774 | static void do_del_port(struct dp_netdev *dp, struct dp_netdev_port *) | |
775 | OVS_REQUIRES(dp->port_mutex); | |
776 | static int dpif_netdev_open(const struct dpif_class *, const char *name, | |
777 | bool create, struct dpif **); | |
778 | static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread *pmd, | |
779 | struct dp_packet_batch *, | |
780 | bool should_steal, | |
781 | const struct flow *flow, | |
782 | const struct nlattr *actions, | |
783 | size_t actions_len); | |
784 | static void dp_netdev_input(struct dp_netdev_pmd_thread *, | |
785 | struct dp_packet_batch *, odp_port_t port_no); | |
786 | static void dp_netdev_recirculate(struct dp_netdev_pmd_thread *, | |
787 | struct dp_packet_batch *); | |
788 | ||
789 | static void dp_netdev_disable_upcall(struct dp_netdev *); | |
790 | static void dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread *pmd); | |
791 | static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread *pmd, | |
792 | struct dp_netdev *dp, unsigned core_id, | |
793 | int numa_id); | |
794 | static void dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread *pmd); | |
795 | static void dp_netdev_set_nonpmd(struct dp_netdev *dp) | |
796 | OVS_REQUIRES(dp->port_mutex); | |
797 | ||
798 | static void *pmd_thread_main(void *); | |
799 | static struct dp_netdev_pmd_thread *dp_netdev_get_pmd(struct dp_netdev *dp, | |
800 | unsigned core_id); | |
801 | static struct dp_netdev_pmd_thread * | |
802 | dp_netdev_pmd_get_next(struct dp_netdev *dp, struct cmap_position *pos); | |
803 | static void dp_netdev_del_pmd(struct dp_netdev *dp, | |
804 | struct dp_netdev_pmd_thread *pmd); | |
805 | static void dp_netdev_destroy_all_pmds(struct dp_netdev *dp, bool non_pmd); | |
806 | static void dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread *pmd); | |
807 | static void dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread *pmd, | |
808 | struct dp_netdev_port *port) | |
809 | OVS_REQUIRES(pmd->port_mutex); | |
810 | static void dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread *pmd, | |
811 | struct tx_port *tx) | |
812 | OVS_REQUIRES(pmd->port_mutex); | |
813 | static void dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread *pmd, | |
814 | struct dp_netdev_rxq *rxq) | |
815 | OVS_REQUIRES(pmd->port_mutex); | |
816 | static void dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread *pmd, | |
817 | struct rxq_poll *poll) | |
818 | OVS_REQUIRES(pmd->port_mutex); | |
819 | static int | |
820 | dp_netdev_pmd_flush_output_packets(struct dp_netdev_pmd_thread *pmd, | |
821 | bool force); | |
822 | ||
823 | static void reconfigure_datapath(struct dp_netdev *dp) | |
824 | OVS_REQUIRES(dp->port_mutex); | |
825 | static bool dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread *pmd); | |
826 | static void dp_netdev_pmd_unref(struct dp_netdev_pmd_thread *pmd); | |
827 | static void dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread *pmd); | |
828 | static void pmd_load_cached_ports(struct dp_netdev_pmd_thread *pmd) | |
829 | OVS_REQUIRES(pmd->port_mutex); | |
830 | static inline void | |
831 | dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread *pmd, | |
832 | struct polled_queue *poll_list, int poll_cnt); | |
833 | static void | |
834 | dp_netdev_rxq_set_cycles(struct dp_netdev_rxq *rx, | |
835 | enum rxq_cycles_counter_type type, | |
836 | unsigned long long cycles); | |
837 | static uint64_t | |
838 | dp_netdev_rxq_get_cycles(struct dp_netdev_rxq *rx, | |
839 | enum rxq_cycles_counter_type type); | |
840 | static void | |
841 | dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq *rx, | |
842 | unsigned long long cycles); | |
843 | static uint64_t | |
844 | dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq *rx, unsigned idx); | |
845 | static void | |
846 | dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread *pmd, | |
847 | bool purge); | |
848 | static int dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread *pmd, | |
849 | struct tx_port *tx); | |
850 | ||
851 | static inline bool emc_entry_alive(struct emc_entry *ce); | |
852 | static void emc_clear_entry(struct emc_entry *ce); | |
853 | static void smc_clear_entry(struct smc_bucket *b, int idx); | |
854 | ||
855 | static void dp_netdev_request_reconfigure(struct dp_netdev *dp); | |
856 | static inline bool | |
857 | pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread *pmd); | |
858 | static void queue_netdev_flow_del(struct dp_netdev_pmd_thread *pmd, | |
859 | struct dp_netdev_flow *flow); | |
860 | ||
861 | static void | |
862 | emc_cache_init(struct emc_cache *flow_cache) | |
863 | { | |
864 | int i; | |
865 | ||
866 | flow_cache->sweep_idx = 0; | |
867 | for (i = 0; i < ARRAY_SIZE(flow_cache->entries); i++) { | |
868 | flow_cache->entries[i].flow = NULL; | |
869 | flow_cache->entries[i].key.hash = 0; | |
870 | flow_cache->entries[i].key.len = sizeof(struct miniflow); | |
871 | flowmap_init(&flow_cache->entries[i].key.mf.map); | |
872 | } | |
873 | } | |
874 | ||
875 | static void | |
876 | smc_cache_init(struct smc_cache *smc_cache) | |
877 | { | |
878 | int i, j; | |
879 | for (i = 0; i < SMC_BUCKET_CNT; i++) { | |
880 | for (j = 0; j < SMC_ENTRY_PER_BUCKET; j++) { | |
881 | smc_cache->buckets[i].flow_idx[j] = UINT16_MAX; | |
882 | } | |
883 | } | |
884 | } | |
885 | ||
886 | static void | |
887 | dfc_cache_init(struct dfc_cache *flow_cache) | |
888 | { | |
889 | emc_cache_init(&flow_cache->emc_cache); | |
890 | smc_cache_init(&flow_cache->smc_cache); | |
891 | } | |
892 | ||
893 | static void | |
894 | emc_cache_uninit(struct emc_cache *flow_cache) | |
895 | { | |
896 | int i; | |
897 | ||
898 | for (i = 0; i < ARRAY_SIZE(flow_cache->entries); i++) { | |
899 | emc_clear_entry(&flow_cache->entries[i]); | |
900 | } | |
901 | } | |
902 | ||
903 | static void | |
904 | smc_cache_uninit(struct smc_cache *smc) | |
905 | { | |
906 | int i, j; | |
907 | ||
908 | for (i = 0; i < SMC_BUCKET_CNT; i++) { | |
909 | for (j = 0; j < SMC_ENTRY_PER_BUCKET; j++) { | |
910 | smc_clear_entry(&(smc->buckets[i]), j); | |
911 | } | |
912 | } | |
913 | } | |
914 | ||
915 | static void | |
916 | dfc_cache_uninit(struct dfc_cache *flow_cache) | |
917 | { | |
918 | smc_cache_uninit(&flow_cache->smc_cache); | |
919 | emc_cache_uninit(&flow_cache->emc_cache); | |
920 | } | |
921 | ||
922 | /* Check and clear dead flow references slowly (one entry at each | |
923 | * invocation). */ | |
924 | static void | |
925 | emc_cache_slow_sweep(struct emc_cache *flow_cache) | |
926 | { | |
927 | struct emc_entry *entry = &flow_cache->entries[flow_cache->sweep_idx]; | |
928 | ||
929 | if (!emc_entry_alive(entry)) { | |
930 | emc_clear_entry(entry); | |
931 | } | |
932 | flow_cache->sweep_idx = (flow_cache->sweep_idx + 1) & EM_FLOW_HASH_MASK; | |
933 | } | |
934 | ||
935 | /* Updates the time in PMD threads context and should be called in three cases: | |
936 | * | |
937 | * 1. PMD structure initialization: | |
938 | * - dp_netdev_configure_pmd() | |
939 | * | |
940 | * 2. Before processing of the new packet batch: | |
941 | * - dpif_netdev_execute() | |
942 | * - dp_netdev_process_rxq_port() | |
943 | * | |
944 | * 3. At least once per polling iteration in main polling threads if no | |
945 | * packets received on current iteration: | |
946 | * - dpif_netdev_run() | |
947 | * - pmd_thread_main() | |
948 | * | |
949 | * 'pmd->ctx.now' should be used without update in all other cases if possible. | |
950 | */ | |
951 | static inline void | |
952 | pmd_thread_ctx_time_update(struct dp_netdev_pmd_thread *pmd) | |
953 | { | |
954 | pmd->ctx.now = time_usec(); | |
955 | } | |
956 | ||
957 | /* Returns true if 'dpif' is a netdev or dummy dpif, false otherwise. */ | |
958 | bool | |
959 | dpif_is_netdev(const struct dpif *dpif) | |
960 | { | |
961 | return dpif->dpif_class->open == dpif_netdev_open; | |
962 | } | |
963 | ||
964 | static struct dpif_netdev * | |
965 | dpif_netdev_cast(const struct dpif *dpif) | |
966 | { | |
967 | ovs_assert(dpif_is_netdev(dpif)); | |
968 | return CONTAINER_OF(dpif, struct dpif_netdev, dpif); | |
969 | } | |
970 | ||
971 | static struct dp_netdev * | |
972 | get_dp_netdev(const struct dpif *dpif) | |
973 | { | |
974 | return dpif_netdev_cast(dpif)->dp; | |
975 | } | |
976 | \f | |
977 | enum pmd_info_type { | |
978 | PMD_INFO_SHOW_STATS, /* Show how cpu cycles are spent. */ | |
979 | PMD_INFO_CLEAR_STATS, /* Set the cycles count to 0. */ | |
980 | PMD_INFO_SHOW_RXQ, /* Show poll lists of pmd threads. */ | |
981 | PMD_INFO_PERF_SHOW, /* Show pmd performance details. */ | |
982 | }; | |
983 | ||
984 | static void | |
985 | format_pmd_thread(struct ds *reply, struct dp_netdev_pmd_thread *pmd) | |
986 | { | |
987 | ds_put_cstr(reply, (pmd->core_id == NON_PMD_CORE_ID) | |
988 | ? "main thread" : "pmd thread"); | |
989 | if (pmd->numa_id != OVS_NUMA_UNSPEC) { | |
990 | ds_put_format(reply, " numa_id %d", pmd->numa_id); | |
991 | } | |
992 | if (pmd->core_id != OVS_CORE_UNSPEC && pmd->core_id != NON_PMD_CORE_ID) { | |
993 | ds_put_format(reply, " core_id %u", pmd->core_id); | |
994 | } | |
995 | ds_put_cstr(reply, ":\n"); | |
996 | } | |
997 | ||
998 | static void | |
999 | pmd_info_show_stats(struct ds *reply, | |
1000 | struct dp_netdev_pmd_thread *pmd) | |
1001 | { | |
1002 | uint64_t stats[PMD_N_STATS]; | |
1003 | uint64_t total_cycles, total_packets; | |
1004 | double passes_per_pkt = 0; | |
1005 | double lookups_per_hit = 0; | |
1006 | double packets_per_batch = 0; | |
1007 | ||
1008 | pmd_perf_read_counters(&pmd->perf_stats, stats); | |
1009 | total_cycles = stats[PMD_CYCLES_ITER_IDLE] | |
1010 | + stats[PMD_CYCLES_ITER_BUSY]; | |
1011 | total_packets = stats[PMD_STAT_RECV]; | |
1012 | ||
1013 | format_pmd_thread(reply, pmd); | |
1014 | ||
1015 | if (total_packets > 0) { | |
1016 | passes_per_pkt = (total_packets + stats[PMD_STAT_RECIRC]) | |
1017 | / (double) total_packets; | |
1018 | } | |
1019 | if (stats[PMD_STAT_MASKED_HIT] > 0) { | |
1020 | lookups_per_hit = stats[PMD_STAT_MASKED_LOOKUP] | |
1021 | / (double) stats[PMD_STAT_MASKED_HIT]; | |
1022 | } | |
1023 | if (stats[PMD_STAT_SENT_BATCHES] > 0) { | |
1024 | packets_per_batch = stats[PMD_STAT_SENT_PKTS] | |
1025 | / (double) stats[PMD_STAT_SENT_BATCHES]; | |
1026 | } | |
1027 | ||
1028 | ds_put_format(reply, | |
1029 | " packets received: %"PRIu64"\n" | |
1030 | " packet recirculations: %"PRIu64"\n" | |
1031 | " avg. datapath passes per packet: %.02f\n" | |
1032 | " emc hits: %"PRIu64"\n" | |
1033 | " smc hits: %"PRIu64"\n" | |
1034 | " megaflow hits: %"PRIu64"\n" | |
1035 | " avg. subtable lookups per megaflow hit: %.02f\n" | |
1036 | " miss with success upcall: %"PRIu64"\n" | |
1037 | " miss with failed upcall: %"PRIu64"\n" | |
1038 | " avg. packets per output batch: %.02f\n", | |
1039 | total_packets, stats[PMD_STAT_RECIRC], | |
1040 | passes_per_pkt, stats[PMD_STAT_EXACT_HIT], | |
1041 | stats[PMD_STAT_SMC_HIT], | |
1042 | stats[PMD_STAT_MASKED_HIT], lookups_per_hit, | |
1043 | stats[PMD_STAT_MISS], stats[PMD_STAT_LOST], | |
1044 | packets_per_batch); | |
1045 | ||
1046 | if (total_cycles == 0) { | |
1047 | return; | |
1048 | } | |
1049 | ||
1050 | ds_put_format(reply, | |
1051 | " idle cycles: %"PRIu64" (%.02f%%)\n" | |
1052 | " processing cycles: %"PRIu64" (%.02f%%)\n", | |
1053 | stats[PMD_CYCLES_ITER_IDLE], | |
1054 | stats[PMD_CYCLES_ITER_IDLE] / (double) total_cycles * 100, | |
1055 | stats[PMD_CYCLES_ITER_BUSY], | |
1056 | stats[PMD_CYCLES_ITER_BUSY] / (double) total_cycles * 100); | |
1057 | ||
1058 | if (total_packets == 0) { | |
1059 | return; | |
1060 | } | |
1061 | ||
1062 | ds_put_format(reply, | |
1063 | " avg cycles per packet: %.02f (%"PRIu64"/%"PRIu64")\n", | |
1064 | total_cycles / (double) total_packets, | |
1065 | total_cycles, total_packets); | |
1066 | ||
1067 | ds_put_format(reply, | |
1068 | " avg processing cycles per packet: " | |
1069 | "%.02f (%"PRIu64"/%"PRIu64")\n", | |
1070 | stats[PMD_CYCLES_ITER_BUSY] / (double) total_packets, | |
1071 | stats[PMD_CYCLES_ITER_BUSY], total_packets); | |
1072 | } | |
1073 | ||
1074 | static void | |
1075 | pmd_info_show_perf(struct ds *reply, | |
1076 | struct dp_netdev_pmd_thread *pmd, | |
1077 | struct pmd_perf_params *par) | |
1078 | { | |
1079 | if (pmd->core_id != NON_PMD_CORE_ID) { | |
1080 | char *time_str = | |
1081 | xastrftime_msec("%H:%M:%S.###", time_wall_msec(), true); | |
1082 | long long now = time_msec(); | |
1083 | double duration = (now - pmd->perf_stats.start_ms) / 1000.0; | |
1084 | ||
1085 | ds_put_cstr(reply, "\n"); | |
1086 | ds_put_format(reply, "Time: %s\n", time_str); | |
1087 | ds_put_format(reply, "Measurement duration: %.3f s\n", duration); | |
1088 | ds_put_cstr(reply, "\n"); | |
1089 | format_pmd_thread(reply, pmd); | |
1090 | ds_put_cstr(reply, "\n"); | |
1091 | pmd_perf_format_overall_stats(reply, &pmd->perf_stats, duration); | |
1092 | if (pmd_perf_metrics_enabled(pmd)) { | |
1093 | /* Prevent parallel clearing of perf metrics. */ | |
1094 | ovs_mutex_lock(&pmd->perf_stats.clear_mutex); | |
1095 | if (par->histograms) { | |
1096 | ds_put_cstr(reply, "\n"); | |
1097 | pmd_perf_format_histograms(reply, &pmd->perf_stats); | |
1098 | } | |
1099 | if (par->iter_hist_len > 0) { | |
1100 | ds_put_cstr(reply, "\n"); | |
1101 | pmd_perf_format_iteration_history(reply, &pmd->perf_stats, | |
1102 | par->iter_hist_len); | |
1103 | } | |
1104 | if (par->ms_hist_len > 0) { | |
1105 | ds_put_cstr(reply, "\n"); | |
1106 | pmd_perf_format_ms_history(reply, &pmd->perf_stats, | |
1107 | par->ms_hist_len); | |
1108 | } | |
1109 | ovs_mutex_unlock(&pmd->perf_stats.clear_mutex); | |
1110 | } | |
1111 | free(time_str); | |
1112 | } | |
1113 | } | |
1114 | ||
1115 | static int | |
1116 | compare_poll_list(const void *a_, const void *b_) | |
1117 | { | |
1118 | const struct rxq_poll *a = a_; | |
1119 | const struct rxq_poll *b = b_; | |
1120 | ||
1121 | const char *namea = netdev_rxq_get_name(a->rxq->rx); | |
1122 | const char *nameb = netdev_rxq_get_name(b->rxq->rx); | |
1123 | ||
1124 | int cmp = strcmp(namea, nameb); | |
1125 | if (!cmp) { | |
1126 | return netdev_rxq_get_queue_id(a->rxq->rx) | |
1127 | - netdev_rxq_get_queue_id(b->rxq->rx); | |
1128 | } else { | |
1129 | return cmp; | |
1130 | } | |
1131 | } | |
1132 | ||
1133 | static void | |
1134 | sorted_poll_list(struct dp_netdev_pmd_thread *pmd, struct rxq_poll **list, | |
1135 | size_t *n) | |
1136 | OVS_REQUIRES(pmd->port_mutex) | |
1137 | { | |
1138 | struct rxq_poll *ret, *poll; | |
1139 | size_t i; | |
1140 | ||
1141 | *n = hmap_count(&pmd->poll_list); | |
1142 | if (!*n) { | |
1143 | ret = NULL; | |
1144 | } else { | |
1145 | ret = xcalloc(*n, sizeof *ret); | |
1146 | i = 0; | |
1147 | HMAP_FOR_EACH (poll, node, &pmd->poll_list) { | |
1148 | ret[i] = *poll; | |
1149 | i++; | |
1150 | } | |
1151 | ovs_assert(i == *n); | |
1152 | qsort(ret, *n, sizeof *ret, compare_poll_list); | |
1153 | } | |
1154 | ||
1155 | *list = ret; | |
1156 | } | |
1157 | ||
1158 | static void | |
1159 | pmd_info_show_rxq(struct ds *reply, struct dp_netdev_pmd_thread *pmd) | |
1160 | { | |
1161 | if (pmd->core_id != NON_PMD_CORE_ID) { | |
1162 | struct rxq_poll *list; | |
1163 | size_t n_rxq; | |
1164 | uint64_t total_cycles = 0; | |
1165 | ||
1166 | ds_put_format(reply, | |
1167 | "pmd thread numa_id %d core_id %u:\n isolated : %s\n", | |
1168 | pmd->numa_id, pmd->core_id, (pmd->isolated) | |
1169 | ? "true" : "false"); | |
1170 | ||
1171 | ovs_mutex_lock(&pmd->port_mutex); | |
1172 | sorted_poll_list(pmd, &list, &n_rxq); | |
1173 | ||
1174 | /* Get the total pmd cycles for an interval. */ | |
1175 | atomic_read_relaxed(&pmd->intrvl_cycles, &total_cycles); | |
1176 | /* Estimate the cycles to cover all intervals. */ | |
1177 | total_cycles *= PMD_RXQ_INTERVAL_MAX; | |
1178 | ||
1179 | for (int i = 0; i < n_rxq; i++) { | |
1180 | struct dp_netdev_rxq *rxq = list[i].rxq; | |
1181 | const char *name = netdev_rxq_get_name(rxq->rx); | |
1182 | uint64_t proc_cycles = 0; | |
1183 | ||
1184 | for (int j = 0; j < PMD_RXQ_INTERVAL_MAX; j++) { | |
1185 | proc_cycles += dp_netdev_rxq_get_intrvl_cycles(rxq, j); | |
1186 | } | |
1187 | ds_put_format(reply, " port: %-16s queue-id: %2d", name, | |
1188 | netdev_rxq_get_queue_id(list[i].rxq->rx)); | |
1189 | ds_put_format(reply, " %s", netdev_rxq_enabled(list[i].rxq->rx) | |
1190 | ? "(enabled) " : "(disabled)"); | |
1191 | ds_put_format(reply, " pmd usage: "); | |
1192 | if (total_cycles) { | |
1193 | ds_put_format(reply, "%2"PRIu64"", | |
1194 | proc_cycles * 100 / total_cycles); | |
1195 | ds_put_cstr(reply, " %"); | |
1196 | } else { | |
1197 | ds_put_format(reply, "%s", "NOT AVAIL"); | |
1198 | } | |
1199 | ds_put_cstr(reply, "\n"); | |
1200 | } | |
1201 | ovs_mutex_unlock(&pmd->port_mutex); | |
1202 | free(list); | |
1203 | } | |
1204 | } | |
1205 | ||
1206 | static int | |
1207 | compare_poll_thread_list(const void *a_, const void *b_) | |
1208 | { | |
1209 | const struct dp_netdev_pmd_thread *a, *b; | |
1210 | ||
1211 | a = *(struct dp_netdev_pmd_thread **)a_; | |
1212 | b = *(struct dp_netdev_pmd_thread **)b_; | |
1213 | ||
1214 | if (a->core_id < b->core_id) { | |
1215 | return -1; | |
1216 | } | |
1217 | if (a->core_id > b->core_id) { | |
1218 | return 1; | |
1219 | } | |
1220 | return 0; | |
1221 | } | |
1222 | ||
1223 | /* Create a sorted list of pmd's from the dp->poll_threads cmap. We can use | |
1224 | * this list, as long as we do not go to quiescent state. */ | |
1225 | static void | |
1226 | sorted_poll_thread_list(struct dp_netdev *dp, | |
1227 | struct dp_netdev_pmd_thread ***list, | |
1228 | size_t *n) | |
1229 | { | |
1230 | struct dp_netdev_pmd_thread *pmd; | |
1231 | struct dp_netdev_pmd_thread **pmd_list; | |
1232 | size_t k = 0, n_pmds; | |
1233 | ||
1234 | n_pmds = cmap_count(&dp->poll_threads); | |
1235 | pmd_list = xcalloc(n_pmds, sizeof *pmd_list); | |
1236 | ||
1237 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
1238 | if (k >= n_pmds) { | |
1239 | break; | |
1240 | } | |
1241 | pmd_list[k++] = pmd; | |
1242 | } | |
1243 | ||
1244 | qsort(pmd_list, k, sizeof *pmd_list, compare_poll_thread_list); | |
1245 | ||
1246 | *list = pmd_list; | |
1247 | *n = k; | |
1248 | } | |
1249 | ||
1250 | static void | |
1251 | dpif_netdev_pmd_rebalance(struct unixctl_conn *conn, int argc, | |
1252 | const char *argv[], void *aux OVS_UNUSED) | |
1253 | { | |
1254 | struct ds reply = DS_EMPTY_INITIALIZER; | |
1255 | struct dp_netdev *dp = NULL; | |
1256 | ||
1257 | ovs_mutex_lock(&dp_netdev_mutex); | |
1258 | ||
1259 | if (argc == 2) { | |
1260 | dp = shash_find_data(&dp_netdevs, argv[1]); | |
1261 | } else if (shash_count(&dp_netdevs) == 1) { | |
1262 | /* There's only one datapath */ | |
1263 | dp = shash_first(&dp_netdevs)->data; | |
1264 | } | |
1265 | ||
1266 | if (!dp) { | |
1267 | ovs_mutex_unlock(&dp_netdev_mutex); | |
1268 | unixctl_command_reply_error(conn, | |
1269 | "please specify an existing datapath"); | |
1270 | return; | |
1271 | } | |
1272 | ||
1273 | dp_netdev_request_reconfigure(dp); | |
1274 | ovs_mutex_unlock(&dp_netdev_mutex); | |
1275 | ds_put_cstr(&reply, "pmd rxq rebalance requested.\n"); | |
1276 | unixctl_command_reply(conn, ds_cstr(&reply)); | |
1277 | ds_destroy(&reply); | |
1278 | } | |
1279 | ||
1280 | static void | |
1281 | dpif_netdev_pmd_info(struct unixctl_conn *conn, int argc, const char *argv[], | |
1282 | void *aux) | |
1283 | { | |
1284 | struct ds reply = DS_EMPTY_INITIALIZER; | |
1285 | struct dp_netdev_pmd_thread **pmd_list; | |
1286 | struct dp_netdev *dp = NULL; | |
1287 | enum pmd_info_type type = *(enum pmd_info_type *) aux; | |
1288 | unsigned int core_id; | |
1289 | bool filter_on_pmd = false; | |
1290 | size_t n; | |
1291 | ||
1292 | ovs_mutex_lock(&dp_netdev_mutex); | |
1293 | ||
1294 | while (argc > 1) { | |
1295 | if (!strcmp(argv[1], "-pmd") && argc > 2) { | |
1296 | if (str_to_uint(argv[2], 10, &core_id)) { | |
1297 | filter_on_pmd = true; | |
1298 | } | |
1299 | argc -= 2; | |
1300 | argv += 2; | |
1301 | } else { | |
1302 | dp = shash_find_data(&dp_netdevs, argv[1]); | |
1303 | argc -= 1; | |
1304 | argv += 1; | |
1305 | } | |
1306 | } | |
1307 | ||
1308 | if (!dp) { | |
1309 | if (shash_count(&dp_netdevs) == 1) { | |
1310 | /* There's only one datapath */ | |
1311 | dp = shash_first(&dp_netdevs)->data; | |
1312 | } else { | |
1313 | ovs_mutex_unlock(&dp_netdev_mutex); | |
1314 | unixctl_command_reply_error(conn, | |
1315 | "please specify an existing datapath"); | |
1316 | return; | |
1317 | } | |
1318 | } | |
1319 | ||
1320 | sorted_poll_thread_list(dp, &pmd_list, &n); | |
1321 | for (size_t i = 0; i < n; i++) { | |
1322 | struct dp_netdev_pmd_thread *pmd = pmd_list[i]; | |
1323 | if (!pmd) { | |
1324 | break; | |
1325 | } | |
1326 | if (filter_on_pmd && pmd->core_id != core_id) { | |
1327 | continue; | |
1328 | } | |
1329 | if (type == PMD_INFO_SHOW_RXQ) { | |
1330 | pmd_info_show_rxq(&reply, pmd); | |
1331 | } else if (type == PMD_INFO_CLEAR_STATS) { | |
1332 | pmd_perf_stats_clear(&pmd->perf_stats); | |
1333 | } else if (type == PMD_INFO_SHOW_STATS) { | |
1334 | pmd_info_show_stats(&reply, pmd); | |
1335 | } else if (type == PMD_INFO_PERF_SHOW) { | |
1336 | pmd_info_show_perf(&reply, pmd, (struct pmd_perf_params *)aux); | |
1337 | } | |
1338 | } | |
1339 | free(pmd_list); | |
1340 | ||
1341 | ovs_mutex_unlock(&dp_netdev_mutex); | |
1342 | ||
1343 | unixctl_command_reply(conn, ds_cstr(&reply)); | |
1344 | ds_destroy(&reply); | |
1345 | } | |
1346 | ||
1347 | static void | |
1348 | pmd_perf_show_cmd(struct unixctl_conn *conn, int argc, | |
1349 | const char *argv[], | |
1350 | void *aux OVS_UNUSED) | |
1351 | { | |
1352 | struct pmd_perf_params par; | |
1353 | long int it_hist = 0, ms_hist = 0; | |
1354 | par.histograms = true; | |
1355 | ||
1356 | while (argc > 1) { | |
1357 | if (!strcmp(argv[1], "-nh")) { | |
1358 | par.histograms = false; | |
1359 | argc -= 1; | |
1360 | argv += 1; | |
1361 | } else if (!strcmp(argv[1], "-it") && argc > 2) { | |
1362 | it_hist = strtol(argv[2], NULL, 10); | |
1363 | if (it_hist < 0) { | |
1364 | it_hist = 0; | |
1365 | } else if (it_hist > HISTORY_LEN) { | |
1366 | it_hist = HISTORY_LEN; | |
1367 | } | |
1368 | argc -= 2; | |
1369 | argv += 2; | |
1370 | } else if (!strcmp(argv[1], "-ms") && argc > 2) { | |
1371 | ms_hist = strtol(argv[2], NULL, 10); | |
1372 | if (ms_hist < 0) { | |
1373 | ms_hist = 0; | |
1374 | } else if (ms_hist > HISTORY_LEN) { | |
1375 | ms_hist = HISTORY_LEN; | |
1376 | } | |
1377 | argc -= 2; | |
1378 | argv += 2; | |
1379 | } else { | |
1380 | break; | |
1381 | } | |
1382 | } | |
1383 | par.iter_hist_len = it_hist; | |
1384 | par.ms_hist_len = ms_hist; | |
1385 | par.command_type = PMD_INFO_PERF_SHOW; | |
1386 | dpif_netdev_pmd_info(conn, argc, argv, &par); | |
1387 | } | |
1388 | \f | |
1389 | static int | |
1390 | dpif_netdev_init(void) | |
1391 | { | |
1392 | static enum pmd_info_type show_aux = PMD_INFO_SHOW_STATS, | |
1393 | clear_aux = PMD_INFO_CLEAR_STATS, | |
1394 | poll_aux = PMD_INFO_SHOW_RXQ; | |
1395 | ||
1396 | unixctl_command_register("dpif-netdev/pmd-stats-show", "[-pmd core] [dp]", | |
1397 | 0, 3, dpif_netdev_pmd_info, | |
1398 | (void *)&show_aux); | |
1399 | unixctl_command_register("dpif-netdev/pmd-stats-clear", "[-pmd core] [dp]", | |
1400 | 0, 3, dpif_netdev_pmd_info, | |
1401 | (void *)&clear_aux); | |
1402 | unixctl_command_register("dpif-netdev/pmd-rxq-show", "[-pmd core] [dp]", | |
1403 | 0, 3, dpif_netdev_pmd_info, | |
1404 | (void *)&poll_aux); | |
1405 | unixctl_command_register("dpif-netdev/pmd-perf-show", | |
1406 | "[-nh] [-it iter-history-len]" | |
1407 | " [-ms ms-history-len]" | |
1408 | " [-pmd core] [dp]", | |
1409 | 0, 8, pmd_perf_show_cmd, | |
1410 | NULL); | |
1411 | unixctl_command_register("dpif-netdev/pmd-rxq-rebalance", "[dp]", | |
1412 | 0, 1, dpif_netdev_pmd_rebalance, | |
1413 | NULL); | |
1414 | unixctl_command_register("dpif-netdev/pmd-perf-log-set", | |
1415 | "on|off [-b before] [-a after] [-e|-ne] " | |
1416 | "[-us usec] [-q qlen]", | |
1417 | 0, 10, pmd_perf_log_set_cmd, | |
1418 | NULL); | |
1419 | return 0; | |
1420 | } | |
1421 | ||
1422 | static int | |
1423 | dpif_netdev_enumerate(struct sset *all_dps, | |
1424 | const struct dpif_class *dpif_class) | |
1425 | { | |
1426 | struct shash_node *node; | |
1427 | ||
1428 | ovs_mutex_lock(&dp_netdev_mutex); | |
1429 | SHASH_FOR_EACH(node, &dp_netdevs) { | |
1430 | struct dp_netdev *dp = node->data; | |
1431 | if (dpif_class != dp->class) { | |
1432 | /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs. | |
1433 | * If the class doesn't match, skip this dpif. */ | |
1434 | continue; | |
1435 | } | |
1436 | sset_add(all_dps, node->name); | |
1437 | } | |
1438 | ovs_mutex_unlock(&dp_netdev_mutex); | |
1439 | ||
1440 | return 0; | |
1441 | } | |
1442 | ||
1443 | static bool | |
1444 | dpif_netdev_class_is_dummy(const struct dpif_class *class) | |
1445 | { | |
1446 | return class != &dpif_netdev_class; | |
1447 | } | |
1448 | ||
1449 | static const char * | |
1450 | dpif_netdev_port_open_type(const struct dpif_class *class, const char *type) | |
1451 | { | |
1452 | return strcmp(type, "internal") ? type | |
1453 | : dpif_netdev_class_is_dummy(class) ? "dummy-internal" | |
1454 | : "tap"; | |
1455 | } | |
1456 | ||
1457 | static struct dpif * | |
1458 | create_dpif_netdev(struct dp_netdev *dp) | |
1459 | { | |
1460 | uint16_t netflow_id = hash_string(dp->name, 0); | |
1461 | struct dpif_netdev *dpif; | |
1462 | ||
1463 | ovs_refcount_ref(&dp->ref_cnt); | |
1464 | ||
1465 | dpif = xmalloc(sizeof *dpif); | |
1466 | dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id); | |
1467 | dpif->dp = dp; | |
1468 | dpif->last_port_seq = seq_read(dp->port_seq); | |
1469 | ||
1470 | return &dpif->dpif; | |
1471 | } | |
1472 | ||
1473 | /* Choose an unused, non-zero port number and return it on success. | |
1474 | * Return ODPP_NONE on failure. */ | |
1475 | static odp_port_t | |
1476 | choose_port(struct dp_netdev *dp, const char *name) | |
1477 | OVS_REQUIRES(dp->port_mutex) | |
1478 | { | |
1479 | uint32_t port_no; | |
1480 | ||
1481 | if (dp->class != &dpif_netdev_class) { | |
1482 | const char *p; | |
1483 | int start_no = 0; | |
1484 | ||
1485 | /* If the port name begins with "br", start the number search at | |
1486 | * 100 to make writing tests easier. */ | |
1487 | if (!strncmp(name, "br", 2)) { | |
1488 | start_no = 100; | |
1489 | } | |
1490 | ||
1491 | /* If the port name contains a number, try to assign that port number. | |
1492 | * This can make writing unit tests easier because port numbers are | |
1493 | * predictable. */ | |
1494 | for (p = name; *p != '\0'; p++) { | |
1495 | if (isdigit((unsigned char) *p)) { | |
1496 | port_no = start_no + strtol(p, NULL, 10); | |
1497 | if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE) | |
1498 | && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) { | |
1499 | return u32_to_odp(port_no); | |
1500 | } | |
1501 | break; | |
1502 | } | |
1503 | } | |
1504 | } | |
1505 | ||
1506 | for (port_no = 1; port_no <= UINT16_MAX; port_no++) { | |
1507 | if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) { | |
1508 | return u32_to_odp(port_no); | |
1509 | } | |
1510 | } | |
1511 | ||
1512 | return ODPP_NONE; | |
1513 | } | |
1514 | ||
1515 | static int | |
1516 | create_dp_netdev(const char *name, const struct dpif_class *class, | |
1517 | struct dp_netdev **dpp) | |
1518 | OVS_REQUIRES(dp_netdev_mutex) | |
1519 | { | |
1520 | static struct ovsthread_once tsc_freq_check = OVSTHREAD_ONCE_INITIALIZER; | |
1521 | struct dp_netdev *dp; | |
1522 | int error; | |
1523 | ||
1524 | /* Avoid estimating TSC frequency for dummy datapath to not slow down | |
1525 | * unit tests. */ | |
1526 | if (!dpif_netdev_class_is_dummy(class) | |
1527 | && ovsthread_once_start(&tsc_freq_check)) { | |
1528 | pmd_perf_estimate_tsc_frequency(); | |
1529 | ovsthread_once_done(&tsc_freq_check); | |
1530 | } | |
1531 | ||
1532 | dp = xzalloc(sizeof *dp); | |
1533 | shash_add(&dp_netdevs, name, dp); | |
1534 | ||
1535 | *CONST_CAST(const struct dpif_class **, &dp->class) = class; | |
1536 | *CONST_CAST(const char **, &dp->name) = xstrdup(name); | |
1537 | ovs_refcount_init(&dp->ref_cnt); | |
1538 | atomic_flag_clear(&dp->destroyed); | |
1539 | ||
1540 | ovs_mutex_init(&dp->port_mutex); | |
1541 | hmap_init(&dp->ports); | |
1542 | dp->port_seq = seq_create(); | |
1543 | fat_rwlock_init(&dp->upcall_rwlock); | |
1544 | ||
1545 | dp->reconfigure_seq = seq_create(); | |
1546 | dp->last_reconfigure_seq = seq_read(dp->reconfigure_seq); | |
1547 | ||
1548 | for (int i = 0; i < N_METER_LOCKS; ++i) { | |
1549 | ovs_mutex_init_adaptive(&dp->meter_locks[i]); | |
1550 | } | |
1551 | ||
1552 | /* Disable upcalls by default. */ | |
1553 | dp_netdev_disable_upcall(dp); | |
1554 | dp->upcall_aux = NULL; | |
1555 | dp->upcall_cb = NULL; | |
1556 | ||
1557 | dp->conntrack = conntrack_init(); | |
1558 | ||
1559 | atomic_init(&dp->emc_insert_min, DEFAULT_EM_FLOW_INSERT_MIN); | |
1560 | atomic_init(&dp->tx_flush_interval, DEFAULT_TX_FLUSH_INTERVAL); | |
1561 | ||
1562 | cmap_init(&dp->poll_threads); | |
1563 | dp->pmd_rxq_assign_cyc = true; | |
1564 | ||
1565 | ovs_mutex_init(&dp->tx_qid_pool_mutex); | |
1566 | /* We need 1 Tx queue for each possible core + 1 for non-PMD threads. */ | |
1567 | dp->tx_qid_pool = id_pool_create(0, ovs_numa_get_n_cores() + 1); | |
1568 | ||
1569 | ovs_mutex_init_recursive(&dp->non_pmd_mutex); | |
1570 | ovsthread_key_create(&dp->per_pmd_key, NULL); | |
1571 | ||
1572 | ovs_mutex_lock(&dp->port_mutex); | |
1573 | /* non-PMD will be created before all other threads and will | |
1574 | * allocate static_tx_qid = 0. */ | |
1575 | dp_netdev_set_nonpmd(dp); | |
1576 | ||
1577 | error = do_add_port(dp, name, dpif_netdev_port_open_type(dp->class, | |
1578 | "internal"), | |
1579 | ODPP_LOCAL); | |
1580 | ovs_mutex_unlock(&dp->port_mutex); | |
1581 | if (error) { | |
1582 | dp_netdev_free(dp); | |
1583 | return error; | |
1584 | } | |
1585 | ||
1586 | dp->last_tnl_conf_seq = seq_read(tnl_conf_seq); | |
1587 | *dpp = dp; | |
1588 | return 0; | |
1589 | } | |
1590 | ||
1591 | static void | |
1592 | dp_netdev_request_reconfigure(struct dp_netdev *dp) | |
1593 | { | |
1594 | seq_change(dp->reconfigure_seq); | |
1595 | } | |
1596 | ||
1597 | static bool | |
1598 | dp_netdev_is_reconf_required(struct dp_netdev *dp) | |
1599 | { | |
1600 | return seq_read(dp->reconfigure_seq) != dp->last_reconfigure_seq; | |
1601 | } | |
1602 | ||
1603 | static int | |
1604 | dpif_netdev_open(const struct dpif_class *class, const char *name, | |
1605 | bool create, struct dpif **dpifp) | |
1606 | { | |
1607 | struct dp_netdev *dp; | |
1608 | int error; | |
1609 | ||
1610 | ovs_mutex_lock(&dp_netdev_mutex); | |
1611 | dp = shash_find_data(&dp_netdevs, name); | |
1612 | if (!dp) { | |
1613 | error = create ? create_dp_netdev(name, class, &dp) : ENODEV; | |
1614 | } else { | |
1615 | error = (dp->class != class ? EINVAL | |
1616 | : create ? EEXIST | |
1617 | : 0); | |
1618 | } | |
1619 | if (!error) { | |
1620 | *dpifp = create_dpif_netdev(dp); | |
1621 | dp->dpif = *dpifp; | |
1622 | } | |
1623 | ovs_mutex_unlock(&dp_netdev_mutex); | |
1624 | ||
1625 | return error; | |
1626 | } | |
1627 | ||
1628 | static void | |
1629 | dp_netdev_destroy_upcall_lock(struct dp_netdev *dp) | |
1630 | OVS_NO_THREAD_SAFETY_ANALYSIS | |
1631 | { | |
1632 | /* Check that upcalls are disabled, i.e. that the rwlock is taken */ | |
1633 | ovs_assert(fat_rwlock_tryrdlock(&dp->upcall_rwlock)); | |
1634 | ||
1635 | /* Before freeing a lock we should release it */ | |
1636 | fat_rwlock_unlock(&dp->upcall_rwlock); | |
1637 | fat_rwlock_destroy(&dp->upcall_rwlock); | |
1638 | } | |
1639 | ||
1640 | static void | |
1641 | dp_delete_meter(struct dp_netdev *dp, uint32_t meter_id) | |
1642 | OVS_REQUIRES(dp->meter_locks[meter_id % N_METER_LOCKS]) | |
1643 | { | |
1644 | if (dp->meters[meter_id]) { | |
1645 | free(dp->meters[meter_id]); | |
1646 | dp->meters[meter_id] = NULL; | |
1647 | } | |
1648 | } | |
1649 | ||
1650 | /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp' | |
1651 | * through the 'dp_netdevs' shash while freeing 'dp'. */ | |
1652 | static void | |
1653 | dp_netdev_free(struct dp_netdev *dp) | |
1654 | OVS_REQUIRES(dp_netdev_mutex) | |
1655 | { | |
1656 | struct dp_netdev_port *port, *next; | |
1657 | ||
1658 | shash_find_and_delete(&dp_netdevs, dp->name); | |
1659 | ||
1660 | ovs_mutex_lock(&dp->port_mutex); | |
1661 | HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) { | |
1662 | do_del_port(dp, port); | |
1663 | } | |
1664 | ovs_mutex_unlock(&dp->port_mutex); | |
1665 | ||
1666 | dp_netdev_destroy_all_pmds(dp, true); | |
1667 | cmap_destroy(&dp->poll_threads); | |
1668 | ||
1669 | ovs_mutex_destroy(&dp->tx_qid_pool_mutex); | |
1670 | id_pool_destroy(dp->tx_qid_pool); | |
1671 | ||
1672 | ovs_mutex_destroy(&dp->non_pmd_mutex); | |
1673 | ovsthread_key_delete(dp->per_pmd_key); | |
1674 | ||
1675 | conntrack_destroy(dp->conntrack); | |
1676 | ||
1677 | ||
1678 | seq_destroy(dp->reconfigure_seq); | |
1679 | ||
1680 | seq_destroy(dp->port_seq); | |
1681 | hmap_destroy(&dp->ports); | |
1682 | ovs_mutex_destroy(&dp->port_mutex); | |
1683 | ||
1684 | /* Upcalls must be disabled at this point */ | |
1685 | dp_netdev_destroy_upcall_lock(dp); | |
1686 | ||
1687 | int i; | |
1688 | ||
1689 | for (i = 0; i < MAX_METERS; ++i) { | |
1690 | meter_lock(dp, i); | |
1691 | dp_delete_meter(dp, i); | |
1692 | meter_unlock(dp, i); | |
1693 | } | |
1694 | for (i = 0; i < N_METER_LOCKS; ++i) { | |
1695 | ovs_mutex_destroy(&dp->meter_locks[i]); | |
1696 | } | |
1697 | ||
1698 | free(dp->pmd_cmask); | |
1699 | free(CONST_CAST(char *, dp->name)); | |
1700 | free(dp); | |
1701 | } | |
1702 | ||
1703 | static void | |
1704 | dp_netdev_unref(struct dp_netdev *dp) | |
1705 | { | |
1706 | if (dp) { | |
1707 | /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't | |
1708 | * get a new reference to 'dp' through the 'dp_netdevs' shash. */ | |
1709 | ovs_mutex_lock(&dp_netdev_mutex); | |
1710 | if (ovs_refcount_unref_relaxed(&dp->ref_cnt) == 1) { | |
1711 | dp_netdev_free(dp); | |
1712 | } | |
1713 | ovs_mutex_unlock(&dp_netdev_mutex); | |
1714 | } | |
1715 | } | |
1716 | ||
1717 | static void | |
1718 | dpif_netdev_close(struct dpif *dpif) | |
1719 | { | |
1720 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
1721 | ||
1722 | dp_netdev_unref(dp); | |
1723 | free(dpif); | |
1724 | } | |
1725 | ||
1726 | static int | |
1727 | dpif_netdev_destroy(struct dpif *dpif) | |
1728 | { | |
1729 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
1730 | ||
1731 | if (!atomic_flag_test_and_set(&dp->destroyed)) { | |
1732 | if (ovs_refcount_unref_relaxed(&dp->ref_cnt) == 1) { | |
1733 | /* Can't happen: 'dpif' still owns a reference to 'dp'. */ | |
1734 | OVS_NOT_REACHED(); | |
1735 | } | |
1736 | } | |
1737 | ||
1738 | return 0; | |
1739 | } | |
1740 | ||
1741 | /* Add 'n' to the atomic variable 'var' non-atomically and using relaxed | |
1742 | * load/store semantics. While the increment is not atomic, the load and | |
1743 | * store operations are, making it impossible to read inconsistent values. | |
1744 | * | |
1745 | * This is used to update thread local stats counters. */ | |
1746 | static void | |
1747 | non_atomic_ullong_add(atomic_ullong *var, unsigned long long n) | |
1748 | { | |
1749 | unsigned long long tmp; | |
1750 | ||
1751 | atomic_read_relaxed(var, &tmp); | |
1752 | tmp += n; | |
1753 | atomic_store_relaxed(var, tmp); | |
1754 | } | |
1755 | ||
1756 | static int | |
1757 | dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats) | |
1758 | { | |
1759 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
1760 | struct dp_netdev_pmd_thread *pmd; | |
1761 | uint64_t pmd_stats[PMD_N_STATS]; | |
1762 | ||
1763 | stats->n_flows = stats->n_hit = stats->n_missed = stats->n_lost = 0; | |
1764 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
1765 | stats->n_flows += cmap_count(&pmd->flow_table); | |
1766 | pmd_perf_read_counters(&pmd->perf_stats, pmd_stats); | |
1767 | stats->n_hit += pmd_stats[PMD_STAT_EXACT_HIT]; | |
1768 | stats->n_hit += pmd_stats[PMD_STAT_SMC_HIT]; | |
1769 | stats->n_hit += pmd_stats[PMD_STAT_MASKED_HIT]; | |
1770 | stats->n_missed += pmd_stats[PMD_STAT_MISS]; | |
1771 | stats->n_lost += pmd_stats[PMD_STAT_LOST]; | |
1772 | } | |
1773 | stats->n_masks = UINT32_MAX; | |
1774 | stats->n_mask_hit = UINT64_MAX; | |
1775 | ||
1776 | return 0; | |
1777 | } | |
1778 | ||
1779 | static void | |
1780 | dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread *pmd) | |
1781 | { | |
1782 | if (pmd->core_id == NON_PMD_CORE_ID) { | |
1783 | ovs_mutex_lock(&pmd->dp->non_pmd_mutex); | |
1784 | ovs_mutex_lock(&pmd->port_mutex); | |
1785 | pmd_load_cached_ports(pmd); | |
1786 | ovs_mutex_unlock(&pmd->port_mutex); | |
1787 | ovs_mutex_unlock(&pmd->dp->non_pmd_mutex); | |
1788 | return; | |
1789 | } | |
1790 | ||
1791 | seq_change(pmd->reload_seq); | |
1792 | atomic_store_explicit(&pmd->reload, true, memory_order_release); | |
1793 | } | |
1794 | ||
1795 | static uint32_t | |
1796 | hash_port_no(odp_port_t port_no) | |
1797 | { | |
1798 | return hash_int(odp_to_u32(port_no), 0); | |
1799 | } | |
1800 | ||
1801 | static int | |
1802 | port_create(const char *devname, const char *type, | |
1803 | odp_port_t port_no, struct dp_netdev_port **portp) | |
1804 | { | |
1805 | struct netdev_saved_flags *sf; | |
1806 | struct dp_netdev_port *port; | |
1807 | enum netdev_flags flags; | |
1808 | struct netdev *netdev; | |
1809 | int error; | |
1810 | ||
1811 | *portp = NULL; | |
1812 | ||
1813 | /* Open and validate network device. */ | |
1814 | error = netdev_open(devname, type, &netdev); | |
1815 | if (error) { | |
1816 | return error; | |
1817 | } | |
1818 | /* XXX reject non-Ethernet devices */ | |
1819 | ||
1820 | netdev_get_flags(netdev, &flags); | |
1821 | if (flags & NETDEV_LOOPBACK) { | |
1822 | VLOG_ERR("%s: cannot add a loopback device", devname); | |
1823 | error = EINVAL; | |
1824 | goto out; | |
1825 | } | |
1826 | ||
1827 | error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf); | |
1828 | if (error) { | |
1829 | VLOG_ERR("%s: cannot set promisc flag", devname); | |
1830 | goto out; | |
1831 | } | |
1832 | ||
1833 | port = xzalloc(sizeof *port); | |
1834 | port->port_no = port_no; | |
1835 | port->netdev = netdev; | |
1836 | port->type = xstrdup(type); | |
1837 | port->sf = sf; | |
1838 | port->emc_enabled = true; | |
1839 | port->need_reconfigure = true; | |
1840 | ovs_mutex_init(&port->txq_used_mutex); | |
1841 | ||
1842 | *portp = port; | |
1843 | ||
1844 | return 0; | |
1845 | ||
1846 | out: | |
1847 | netdev_close(netdev); | |
1848 | return error; | |
1849 | } | |
1850 | ||
1851 | static int | |
1852 | do_add_port(struct dp_netdev *dp, const char *devname, const char *type, | |
1853 | odp_port_t port_no) | |
1854 | OVS_REQUIRES(dp->port_mutex) | |
1855 | { | |
1856 | struct dp_netdev_port *port; | |
1857 | int error; | |
1858 | ||
1859 | /* Reject devices already in 'dp'. */ | |
1860 | if (!get_port_by_name(dp, devname, &port)) { | |
1861 | return EEXIST; | |
1862 | } | |
1863 | ||
1864 | error = port_create(devname, type, port_no, &port); | |
1865 | if (error) { | |
1866 | return error; | |
1867 | } | |
1868 | ||
1869 | hmap_insert(&dp->ports, &port->node, hash_port_no(port_no)); | |
1870 | seq_change(dp->port_seq); | |
1871 | ||
1872 | reconfigure_datapath(dp); | |
1873 | ||
1874 | /* Check that port was successfully configured. */ | |
1875 | return dp_netdev_lookup_port(dp, port_no) ? 0 : EINVAL; | |
1876 | } | |
1877 | ||
1878 | static int | |
1879 | dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev, | |
1880 | odp_port_t *port_nop) | |
1881 | { | |
1882 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
1883 | char namebuf[NETDEV_VPORT_NAME_BUFSIZE]; | |
1884 | const char *dpif_port; | |
1885 | odp_port_t port_no; | |
1886 | int error; | |
1887 | ||
1888 | ovs_mutex_lock(&dp->port_mutex); | |
1889 | dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf); | |
1890 | if (*port_nop != ODPP_NONE) { | |
1891 | port_no = *port_nop; | |
1892 | error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0; | |
1893 | } else { | |
1894 | port_no = choose_port(dp, dpif_port); | |
1895 | error = port_no == ODPP_NONE ? EFBIG : 0; | |
1896 | } | |
1897 | if (!error) { | |
1898 | *port_nop = port_no; | |
1899 | error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no); | |
1900 | } | |
1901 | ovs_mutex_unlock(&dp->port_mutex); | |
1902 | ||
1903 | return error; | |
1904 | } | |
1905 | ||
1906 | static int | |
1907 | dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no) | |
1908 | { | |
1909 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
1910 | int error; | |
1911 | ||
1912 | ovs_mutex_lock(&dp->port_mutex); | |
1913 | if (port_no == ODPP_LOCAL) { | |
1914 | error = EINVAL; | |
1915 | } else { | |
1916 | struct dp_netdev_port *port; | |
1917 | ||
1918 | error = get_port_by_number(dp, port_no, &port); | |
1919 | if (!error) { | |
1920 | do_del_port(dp, port); | |
1921 | } | |
1922 | } | |
1923 | ovs_mutex_unlock(&dp->port_mutex); | |
1924 | ||
1925 | return error; | |
1926 | } | |
1927 | ||
1928 | static bool | |
1929 | is_valid_port_number(odp_port_t port_no) | |
1930 | { | |
1931 | return port_no != ODPP_NONE; | |
1932 | } | |
1933 | ||
1934 | static struct dp_netdev_port * | |
1935 | dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no) | |
1936 | OVS_REQUIRES(dp->port_mutex) | |
1937 | { | |
1938 | struct dp_netdev_port *port; | |
1939 | ||
1940 | HMAP_FOR_EACH_WITH_HASH (port, node, hash_port_no(port_no), &dp->ports) { | |
1941 | if (port->port_no == port_no) { | |
1942 | return port; | |
1943 | } | |
1944 | } | |
1945 | return NULL; | |
1946 | } | |
1947 | ||
1948 | static int | |
1949 | get_port_by_number(struct dp_netdev *dp, | |
1950 | odp_port_t port_no, struct dp_netdev_port **portp) | |
1951 | OVS_REQUIRES(dp->port_mutex) | |
1952 | { | |
1953 | if (!is_valid_port_number(port_no)) { | |
1954 | *portp = NULL; | |
1955 | return EINVAL; | |
1956 | } else { | |
1957 | *portp = dp_netdev_lookup_port(dp, port_no); | |
1958 | return *portp ? 0 : ENODEV; | |
1959 | } | |
1960 | } | |
1961 | ||
1962 | static void | |
1963 | port_destroy(struct dp_netdev_port *port) | |
1964 | { | |
1965 | if (!port) { | |
1966 | return; | |
1967 | } | |
1968 | ||
1969 | netdev_close(port->netdev); | |
1970 | netdev_restore_flags(port->sf); | |
1971 | ||
1972 | for (unsigned i = 0; i < port->n_rxq; i++) { | |
1973 | netdev_rxq_close(port->rxqs[i].rx); | |
1974 | } | |
1975 | ovs_mutex_destroy(&port->txq_used_mutex); | |
1976 | free(port->rxq_affinity_list); | |
1977 | free(port->txq_used); | |
1978 | free(port->rxqs); | |
1979 | free(port->type); | |
1980 | free(port); | |
1981 | } | |
1982 | ||
1983 | static int | |
1984 | get_port_by_name(struct dp_netdev *dp, | |
1985 | const char *devname, struct dp_netdev_port **portp) | |
1986 | OVS_REQUIRES(dp->port_mutex) | |
1987 | { | |
1988 | struct dp_netdev_port *port; | |
1989 | ||
1990 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
1991 | if (!strcmp(netdev_get_name(port->netdev), devname)) { | |
1992 | *portp = port; | |
1993 | return 0; | |
1994 | } | |
1995 | } | |
1996 | ||
1997 | /* Callers of dpif_netdev_port_query_by_name() expect ENODEV for a non | |
1998 | * existing port. */ | |
1999 | return ENODEV; | |
2000 | } | |
2001 | ||
2002 | /* Returns 'true' if there is a port with pmd netdev. */ | |
2003 | static bool | |
2004 | has_pmd_port(struct dp_netdev *dp) | |
2005 | OVS_REQUIRES(dp->port_mutex) | |
2006 | { | |
2007 | struct dp_netdev_port *port; | |
2008 | ||
2009 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
2010 | if (netdev_is_pmd(port->netdev)) { | |
2011 | return true; | |
2012 | } | |
2013 | } | |
2014 | ||
2015 | return false; | |
2016 | } | |
2017 | ||
2018 | static void | |
2019 | do_del_port(struct dp_netdev *dp, struct dp_netdev_port *port) | |
2020 | OVS_REQUIRES(dp->port_mutex) | |
2021 | { | |
2022 | hmap_remove(&dp->ports, &port->node); | |
2023 | seq_change(dp->port_seq); | |
2024 | ||
2025 | reconfigure_datapath(dp); | |
2026 | ||
2027 | port_destroy(port); | |
2028 | } | |
2029 | ||
2030 | static void | |
2031 | answer_port_query(const struct dp_netdev_port *port, | |
2032 | struct dpif_port *dpif_port) | |
2033 | { | |
2034 | dpif_port->name = xstrdup(netdev_get_name(port->netdev)); | |
2035 | dpif_port->type = xstrdup(port->type); | |
2036 | dpif_port->port_no = port->port_no; | |
2037 | } | |
2038 | ||
2039 | static int | |
2040 | dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no, | |
2041 | struct dpif_port *dpif_port) | |
2042 | { | |
2043 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
2044 | struct dp_netdev_port *port; | |
2045 | int error; | |
2046 | ||
2047 | ovs_mutex_lock(&dp->port_mutex); | |
2048 | error = get_port_by_number(dp, port_no, &port); | |
2049 | if (!error && dpif_port) { | |
2050 | answer_port_query(port, dpif_port); | |
2051 | } | |
2052 | ovs_mutex_unlock(&dp->port_mutex); | |
2053 | ||
2054 | return error; | |
2055 | } | |
2056 | ||
2057 | static int | |
2058 | dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname, | |
2059 | struct dpif_port *dpif_port) | |
2060 | { | |
2061 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
2062 | struct dp_netdev_port *port; | |
2063 | int error; | |
2064 | ||
2065 | ovs_mutex_lock(&dp->port_mutex); | |
2066 | error = get_port_by_name(dp, devname, &port); | |
2067 | if (!error && dpif_port) { | |
2068 | answer_port_query(port, dpif_port); | |
2069 | } | |
2070 | ovs_mutex_unlock(&dp->port_mutex); | |
2071 | ||
2072 | return error; | |
2073 | } | |
2074 | ||
2075 | static void | |
2076 | dp_netdev_flow_free(struct dp_netdev_flow *flow) | |
2077 | { | |
2078 | dp_netdev_actions_free(dp_netdev_flow_get_actions(flow)); | |
2079 | free(flow); | |
2080 | } | |
2081 | ||
2082 | static void dp_netdev_flow_unref(struct dp_netdev_flow *flow) | |
2083 | { | |
2084 | if (ovs_refcount_unref_relaxed(&flow->ref_cnt) == 1) { | |
2085 | ovsrcu_postpone(dp_netdev_flow_free, flow); | |
2086 | } | |
2087 | } | |
2088 | ||
2089 | static uint32_t | |
2090 | dp_netdev_flow_hash(const ovs_u128 *ufid) | |
2091 | { | |
2092 | return ufid->u32[0]; | |
2093 | } | |
2094 | ||
2095 | static inline struct dpcls * | |
2096 | dp_netdev_pmd_lookup_dpcls(struct dp_netdev_pmd_thread *pmd, | |
2097 | odp_port_t in_port) | |
2098 | { | |
2099 | struct dpcls *cls; | |
2100 | uint32_t hash = hash_port_no(in_port); | |
2101 | CMAP_FOR_EACH_WITH_HASH (cls, node, hash, &pmd->classifiers) { | |
2102 | if (cls->in_port == in_port) { | |
2103 | /* Port classifier exists already */ | |
2104 | return cls; | |
2105 | } | |
2106 | } | |
2107 | return NULL; | |
2108 | } | |
2109 | ||
2110 | static inline struct dpcls * | |
2111 | dp_netdev_pmd_find_dpcls(struct dp_netdev_pmd_thread *pmd, | |
2112 | odp_port_t in_port) | |
2113 | OVS_REQUIRES(pmd->flow_mutex) | |
2114 | { | |
2115 | struct dpcls *cls = dp_netdev_pmd_lookup_dpcls(pmd, in_port); | |
2116 | uint32_t hash = hash_port_no(in_port); | |
2117 | ||
2118 | if (!cls) { | |
2119 | /* Create new classifier for in_port */ | |
2120 | cls = xmalloc(sizeof(*cls)); | |
2121 | dpcls_init(cls); | |
2122 | cls->in_port = in_port; | |
2123 | cmap_insert(&pmd->classifiers, &cls->node, hash); | |
2124 | VLOG_DBG("Creating dpcls %p for in_port %d", cls, in_port); | |
2125 | } | |
2126 | return cls; | |
2127 | } | |
2128 | ||
2129 | #define MAX_FLOW_MARK (UINT32_MAX - 1) | |
2130 | #define INVALID_FLOW_MARK (UINT32_MAX) | |
2131 | ||
2132 | struct megaflow_to_mark_data { | |
2133 | const struct cmap_node node; | |
2134 | ovs_u128 mega_ufid; | |
2135 | uint32_t mark; | |
2136 | }; | |
2137 | ||
2138 | struct flow_mark { | |
2139 | struct cmap megaflow_to_mark; | |
2140 | struct cmap mark_to_flow; | |
2141 | struct id_pool *pool; | |
2142 | }; | |
2143 | ||
2144 | static struct flow_mark flow_mark = { | |
2145 | .megaflow_to_mark = CMAP_INITIALIZER, | |
2146 | .mark_to_flow = CMAP_INITIALIZER, | |
2147 | }; | |
2148 | ||
2149 | static uint32_t | |
2150 | flow_mark_alloc(void) | |
2151 | { | |
2152 | uint32_t mark; | |
2153 | ||
2154 | if (!flow_mark.pool) { | |
2155 | /* Haven't initiated yet, do it here */ | |
2156 | flow_mark.pool = id_pool_create(0, MAX_FLOW_MARK); | |
2157 | } | |
2158 | ||
2159 | if (id_pool_alloc_id(flow_mark.pool, &mark)) { | |
2160 | return mark; | |
2161 | } | |
2162 | ||
2163 | return INVALID_FLOW_MARK; | |
2164 | } | |
2165 | ||
2166 | static void | |
2167 | flow_mark_free(uint32_t mark) | |
2168 | { | |
2169 | id_pool_free_id(flow_mark.pool, mark); | |
2170 | } | |
2171 | ||
2172 | /* associate megaflow with a mark, which is a 1:1 mapping */ | |
2173 | static void | |
2174 | megaflow_to_mark_associate(const ovs_u128 *mega_ufid, uint32_t mark) | |
2175 | { | |
2176 | size_t hash = dp_netdev_flow_hash(mega_ufid); | |
2177 | struct megaflow_to_mark_data *data = xzalloc(sizeof(*data)); | |
2178 | ||
2179 | data->mega_ufid = *mega_ufid; | |
2180 | data->mark = mark; | |
2181 | ||
2182 | cmap_insert(&flow_mark.megaflow_to_mark, | |
2183 | CONST_CAST(struct cmap_node *, &data->node), hash); | |
2184 | } | |
2185 | ||
2186 | /* disassociate meagaflow with a mark */ | |
2187 | static void | |
2188 | megaflow_to_mark_disassociate(const ovs_u128 *mega_ufid) | |
2189 | { | |
2190 | size_t hash = dp_netdev_flow_hash(mega_ufid); | |
2191 | struct megaflow_to_mark_data *data; | |
2192 | ||
2193 | CMAP_FOR_EACH_WITH_HASH (data, node, hash, &flow_mark.megaflow_to_mark) { | |
2194 | if (ovs_u128_equals(*mega_ufid, data->mega_ufid)) { | |
2195 | cmap_remove(&flow_mark.megaflow_to_mark, | |
2196 | CONST_CAST(struct cmap_node *, &data->node), hash); | |
2197 | ovsrcu_postpone(free, data); | |
2198 | return; | |
2199 | } | |
2200 | } | |
2201 | ||
2202 | VLOG_WARN("Masked ufid "UUID_FMT" is not associated with a mark?\n", | |
2203 | UUID_ARGS((struct uuid *)mega_ufid)); | |
2204 | } | |
2205 | ||
2206 | static inline uint32_t | |
2207 | megaflow_to_mark_find(const ovs_u128 *mega_ufid) | |
2208 | { | |
2209 | size_t hash = dp_netdev_flow_hash(mega_ufid); | |
2210 | struct megaflow_to_mark_data *data; | |
2211 | ||
2212 | CMAP_FOR_EACH_WITH_HASH (data, node, hash, &flow_mark.megaflow_to_mark) { | |
2213 | if (ovs_u128_equals(*mega_ufid, data->mega_ufid)) { | |
2214 | return data->mark; | |
2215 | } | |
2216 | } | |
2217 | ||
2218 | VLOG_DBG("Mark id for ufid "UUID_FMT" was not found\n", | |
2219 | UUID_ARGS((struct uuid *)mega_ufid)); | |
2220 | return INVALID_FLOW_MARK; | |
2221 | } | |
2222 | ||
2223 | /* associate mark with a flow, which is 1:N mapping */ | |
2224 | static void | |
2225 | mark_to_flow_associate(const uint32_t mark, struct dp_netdev_flow *flow) | |
2226 | { | |
2227 | dp_netdev_flow_ref(flow); | |
2228 | ||
2229 | cmap_insert(&flow_mark.mark_to_flow, | |
2230 | CONST_CAST(struct cmap_node *, &flow->mark_node), | |
2231 | hash_int(mark, 0)); | |
2232 | flow->mark = mark; | |
2233 | ||
2234 | VLOG_DBG("Associated dp_netdev flow %p with mark %u\n", flow, mark); | |
2235 | } | |
2236 | ||
2237 | static bool | |
2238 | flow_mark_has_no_ref(uint32_t mark) | |
2239 | { | |
2240 | struct dp_netdev_flow *flow; | |
2241 | ||
2242 | CMAP_FOR_EACH_WITH_HASH (flow, mark_node, hash_int(mark, 0), | |
2243 | &flow_mark.mark_to_flow) { | |
2244 | if (flow->mark == mark) { | |
2245 | return false; | |
2246 | } | |
2247 | } | |
2248 | ||
2249 | return true; | |
2250 | } | |
2251 | ||
2252 | static int | |
2253 | mark_to_flow_disassociate(struct dp_netdev_pmd_thread *pmd, | |
2254 | struct dp_netdev_flow *flow) | |
2255 | { | |
2256 | int ret = 0; | |
2257 | uint32_t mark = flow->mark; | |
2258 | struct cmap_node *mark_node = CONST_CAST(struct cmap_node *, | |
2259 | &flow->mark_node); | |
2260 | ||
2261 | cmap_remove(&flow_mark.mark_to_flow, mark_node, hash_int(mark, 0)); | |
2262 | flow->mark = INVALID_FLOW_MARK; | |
2263 | ||
2264 | /* | |
2265 | * no flow is referencing the mark any more? If so, let's | |
2266 | * remove the flow from hardware and free the mark. | |
2267 | */ | |
2268 | if (flow_mark_has_no_ref(mark)) { | |
2269 | struct dp_netdev_port *port; | |
2270 | odp_port_t in_port = flow->flow.in_port.odp_port; | |
2271 | ||
2272 | ovs_mutex_lock(&pmd->dp->port_mutex); | |
2273 | port = dp_netdev_lookup_port(pmd->dp, in_port); | |
2274 | if (port) { | |
2275 | ret = netdev_flow_del(port->netdev, &flow->mega_ufid, NULL); | |
2276 | } | |
2277 | ovs_mutex_unlock(&pmd->dp->port_mutex); | |
2278 | ||
2279 | flow_mark_free(mark); | |
2280 | VLOG_DBG("Freed flow mark %u\n", mark); | |
2281 | ||
2282 | megaflow_to_mark_disassociate(&flow->mega_ufid); | |
2283 | } | |
2284 | dp_netdev_flow_unref(flow); | |
2285 | ||
2286 | return ret; | |
2287 | } | |
2288 | ||
2289 | static void | |
2290 | flow_mark_flush(struct dp_netdev_pmd_thread *pmd) | |
2291 | { | |
2292 | struct dp_netdev_flow *flow; | |
2293 | ||
2294 | CMAP_FOR_EACH (flow, mark_node, &flow_mark.mark_to_flow) { | |
2295 | if (flow->pmd_id == pmd->core_id) { | |
2296 | queue_netdev_flow_del(pmd, flow); | |
2297 | } | |
2298 | } | |
2299 | } | |
2300 | ||
2301 | static struct dp_netdev_flow * | |
2302 | mark_to_flow_find(const struct dp_netdev_pmd_thread *pmd, | |
2303 | const uint32_t mark) | |
2304 | { | |
2305 | struct dp_netdev_flow *flow; | |
2306 | ||
2307 | CMAP_FOR_EACH_WITH_HASH (flow, mark_node, hash_int(mark, 0), | |
2308 | &flow_mark.mark_to_flow) { | |
2309 | if (flow->mark == mark && flow->pmd_id == pmd->core_id && | |
2310 | flow->dead == false) { | |
2311 | return flow; | |
2312 | } | |
2313 | } | |
2314 | ||
2315 | return NULL; | |
2316 | } | |
2317 | ||
2318 | static struct dp_flow_offload_item * | |
2319 | dp_netdev_alloc_flow_offload(struct dp_netdev_pmd_thread *pmd, | |
2320 | struct dp_netdev_flow *flow, | |
2321 | int op) | |
2322 | { | |
2323 | struct dp_flow_offload_item *offload; | |
2324 | ||
2325 | offload = xzalloc(sizeof(*offload)); | |
2326 | offload->pmd = pmd; | |
2327 | offload->flow = flow; | |
2328 | offload->op = op; | |
2329 | ||
2330 | dp_netdev_flow_ref(flow); | |
2331 | dp_netdev_pmd_try_ref(pmd); | |
2332 | ||
2333 | return offload; | |
2334 | } | |
2335 | ||
2336 | static void | |
2337 | dp_netdev_free_flow_offload(struct dp_flow_offload_item *offload) | |
2338 | { | |
2339 | dp_netdev_pmd_unref(offload->pmd); | |
2340 | dp_netdev_flow_unref(offload->flow); | |
2341 | ||
2342 | free(offload->actions); | |
2343 | free(offload); | |
2344 | } | |
2345 | ||
2346 | static void | |
2347 | dp_netdev_append_flow_offload(struct dp_flow_offload_item *offload) | |
2348 | { | |
2349 | ovs_mutex_lock(&dp_flow_offload.mutex); | |
2350 | ovs_list_push_back(&dp_flow_offload.list, &offload->node); | |
2351 | xpthread_cond_signal(&dp_flow_offload.cond); | |
2352 | ovs_mutex_unlock(&dp_flow_offload.mutex); | |
2353 | } | |
2354 | ||
2355 | static int | |
2356 | dp_netdev_flow_offload_del(struct dp_flow_offload_item *offload) | |
2357 | { | |
2358 | return mark_to_flow_disassociate(offload->pmd, offload->flow); | |
2359 | } | |
2360 | ||
2361 | /* | |
2362 | * There are two flow offload operations here: addition and modification. | |
2363 | * | |
2364 | * For flow addition, this function does: | |
2365 | * - allocate a new flow mark id | |
2366 | * - perform hardware flow offload | |
2367 | * - associate the flow mark with flow and mega flow | |
2368 | * | |
2369 | * For flow modification, both flow mark and the associations are still | |
2370 | * valid, thus only item 2 needed. | |
2371 | */ | |
2372 | static int | |
2373 | dp_netdev_flow_offload_put(struct dp_flow_offload_item *offload) | |
2374 | { | |
2375 | struct dp_netdev_port *port; | |
2376 | struct dp_netdev_pmd_thread *pmd = offload->pmd; | |
2377 | struct dp_netdev_flow *flow = offload->flow; | |
2378 | odp_port_t in_port = flow->flow.in_port.odp_port; | |
2379 | bool modification = offload->op == DP_NETDEV_FLOW_OFFLOAD_OP_MOD; | |
2380 | struct offload_info info; | |
2381 | uint32_t mark; | |
2382 | int ret; | |
2383 | ||
2384 | if (flow->dead) { | |
2385 | return -1; | |
2386 | } | |
2387 | ||
2388 | if (modification) { | |
2389 | mark = flow->mark; | |
2390 | ovs_assert(mark != INVALID_FLOW_MARK); | |
2391 | } else { | |
2392 | /* | |
2393 | * If a mega flow has already been offloaded (from other PMD | |
2394 | * instances), do not offload it again. | |
2395 | */ | |
2396 | mark = megaflow_to_mark_find(&flow->mega_ufid); | |
2397 | if (mark != INVALID_FLOW_MARK) { | |
2398 | VLOG_DBG("Flow has already been offloaded with mark %u\n", mark); | |
2399 | if (flow->mark != INVALID_FLOW_MARK) { | |
2400 | ovs_assert(flow->mark == mark); | |
2401 | } else { | |
2402 | mark_to_flow_associate(mark, flow); | |
2403 | } | |
2404 | return 0; | |
2405 | } | |
2406 | ||
2407 | mark = flow_mark_alloc(); | |
2408 | if (mark == INVALID_FLOW_MARK) { | |
2409 | VLOG_ERR("Failed to allocate flow mark!\n"); | |
2410 | } | |
2411 | } | |
2412 | info.flow_mark = mark; | |
2413 | ||
2414 | ovs_mutex_lock(&pmd->dp->port_mutex); | |
2415 | port = dp_netdev_lookup_port(pmd->dp, in_port); | |
2416 | if (!port || netdev_vport_is_vport_class(port->netdev->netdev_class)) { | |
2417 | ovs_mutex_unlock(&pmd->dp->port_mutex); | |
2418 | goto err_free; | |
2419 | } | |
2420 | ret = netdev_flow_put(port->netdev, &offload->match, | |
2421 | CONST_CAST(struct nlattr *, offload->actions), | |
2422 | offload->actions_len, &flow->mega_ufid, &info, | |
2423 | NULL); | |
2424 | ovs_mutex_unlock(&pmd->dp->port_mutex); | |
2425 | ||
2426 | if (ret) { | |
2427 | goto err_free; | |
2428 | } | |
2429 | ||
2430 | if (!modification) { | |
2431 | megaflow_to_mark_associate(&flow->mega_ufid, mark); | |
2432 | mark_to_flow_associate(mark, flow); | |
2433 | } | |
2434 | return 0; | |
2435 | ||
2436 | err_free: | |
2437 | if (!modification) { | |
2438 | flow_mark_free(mark); | |
2439 | } else { | |
2440 | mark_to_flow_disassociate(pmd, flow); | |
2441 | } | |
2442 | return -1; | |
2443 | } | |
2444 | ||
2445 | static void * | |
2446 | dp_netdev_flow_offload_main(void *data OVS_UNUSED) | |
2447 | { | |
2448 | struct dp_flow_offload_item *offload; | |
2449 | struct ovs_list *list; | |
2450 | const char *op; | |
2451 | int ret; | |
2452 | ||
2453 | for (;;) { | |
2454 | ovs_mutex_lock(&dp_flow_offload.mutex); | |
2455 | if (ovs_list_is_empty(&dp_flow_offload.list)) { | |
2456 | ovsrcu_quiesce_start(); | |
2457 | ovs_mutex_cond_wait(&dp_flow_offload.cond, | |
2458 | &dp_flow_offload.mutex); | |
2459 | ovsrcu_quiesce_end(); | |
2460 | } | |
2461 | list = ovs_list_pop_front(&dp_flow_offload.list); | |
2462 | offload = CONTAINER_OF(list, struct dp_flow_offload_item, node); | |
2463 | ovs_mutex_unlock(&dp_flow_offload.mutex); | |
2464 | ||
2465 | switch (offload->op) { | |
2466 | case DP_NETDEV_FLOW_OFFLOAD_OP_ADD: | |
2467 | op = "add"; | |
2468 | ret = dp_netdev_flow_offload_put(offload); | |
2469 | break; | |
2470 | case DP_NETDEV_FLOW_OFFLOAD_OP_MOD: | |
2471 | op = "modify"; | |
2472 | ret = dp_netdev_flow_offload_put(offload); | |
2473 | break; | |
2474 | case DP_NETDEV_FLOW_OFFLOAD_OP_DEL: | |
2475 | op = "delete"; | |
2476 | ret = dp_netdev_flow_offload_del(offload); | |
2477 | break; | |
2478 | default: | |
2479 | OVS_NOT_REACHED(); | |
2480 | } | |
2481 | ||
2482 | VLOG_DBG("%s to %s netdev flow\n", | |
2483 | ret == 0 ? "succeed" : "failed", op); | |
2484 | dp_netdev_free_flow_offload(offload); | |
2485 | } | |
2486 | ||
2487 | return NULL; | |
2488 | } | |
2489 | ||
2490 | static void | |
2491 | queue_netdev_flow_del(struct dp_netdev_pmd_thread *pmd, | |
2492 | struct dp_netdev_flow *flow) | |
2493 | { | |
2494 | struct dp_flow_offload_item *offload; | |
2495 | ||
2496 | if (ovsthread_once_start(&offload_thread_once)) { | |
2497 | xpthread_cond_init(&dp_flow_offload.cond, NULL); | |
2498 | ovs_thread_create("dp_netdev_flow_offload", | |
2499 | dp_netdev_flow_offload_main, NULL); | |
2500 | ovsthread_once_done(&offload_thread_once); | |
2501 | } | |
2502 | ||
2503 | offload = dp_netdev_alloc_flow_offload(pmd, flow, | |
2504 | DP_NETDEV_FLOW_OFFLOAD_OP_DEL); | |
2505 | dp_netdev_append_flow_offload(offload); | |
2506 | } | |
2507 | ||
2508 | static void | |
2509 | queue_netdev_flow_put(struct dp_netdev_pmd_thread *pmd, | |
2510 | struct dp_netdev_flow *flow, struct match *match, | |
2511 | const struct nlattr *actions, size_t actions_len) | |
2512 | { | |
2513 | struct dp_flow_offload_item *offload; | |
2514 | int op; | |
2515 | ||
2516 | if (!netdev_is_flow_api_enabled()) { | |
2517 | return; | |
2518 | } | |
2519 | ||
2520 | if (ovsthread_once_start(&offload_thread_once)) { | |
2521 | xpthread_cond_init(&dp_flow_offload.cond, NULL); | |
2522 | ovs_thread_create("dp_netdev_flow_offload", | |
2523 | dp_netdev_flow_offload_main, NULL); | |
2524 | ovsthread_once_done(&offload_thread_once); | |
2525 | } | |
2526 | ||
2527 | if (flow->mark != INVALID_FLOW_MARK) { | |
2528 | op = DP_NETDEV_FLOW_OFFLOAD_OP_MOD; | |
2529 | } else { | |
2530 | op = DP_NETDEV_FLOW_OFFLOAD_OP_ADD; | |
2531 | } | |
2532 | offload = dp_netdev_alloc_flow_offload(pmd, flow, op); | |
2533 | offload->match = *match; | |
2534 | offload->actions = xmalloc(actions_len); | |
2535 | memcpy(offload->actions, actions, actions_len); | |
2536 | offload->actions_len = actions_len; | |
2537 | ||
2538 | dp_netdev_append_flow_offload(offload); | |
2539 | } | |
2540 | ||
2541 | static void | |
2542 | dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread *pmd, | |
2543 | struct dp_netdev_flow *flow) | |
2544 | OVS_REQUIRES(pmd->flow_mutex) | |
2545 | { | |
2546 | struct cmap_node *node = CONST_CAST(struct cmap_node *, &flow->node); | |
2547 | struct dpcls *cls; | |
2548 | odp_port_t in_port = flow->flow.in_port.odp_port; | |
2549 | ||
2550 | cls = dp_netdev_pmd_lookup_dpcls(pmd, in_port); | |
2551 | ovs_assert(cls != NULL); | |
2552 | dpcls_remove(cls, &flow->cr); | |
2553 | cmap_remove(&pmd->flow_table, node, dp_netdev_flow_hash(&flow->ufid)); | |
2554 | if (flow->mark != INVALID_FLOW_MARK) { | |
2555 | queue_netdev_flow_del(pmd, flow); | |
2556 | } | |
2557 | flow->dead = true; | |
2558 | ||
2559 | dp_netdev_flow_unref(flow); | |
2560 | } | |
2561 | ||
2562 | static void | |
2563 | dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread *pmd) | |
2564 | { | |
2565 | struct dp_netdev_flow *netdev_flow; | |
2566 | ||
2567 | ovs_mutex_lock(&pmd->flow_mutex); | |
2568 | CMAP_FOR_EACH (netdev_flow, node, &pmd->flow_table) { | |
2569 | dp_netdev_pmd_remove_flow(pmd, netdev_flow); | |
2570 | } | |
2571 | ovs_mutex_unlock(&pmd->flow_mutex); | |
2572 | } | |
2573 | ||
2574 | static int | |
2575 | dpif_netdev_flow_flush(struct dpif *dpif) | |
2576 | { | |
2577 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
2578 | struct dp_netdev_pmd_thread *pmd; | |
2579 | ||
2580 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
2581 | dp_netdev_pmd_flow_flush(pmd); | |
2582 | } | |
2583 | ||
2584 | return 0; | |
2585 | } | |
2586 | ||
2587 | struct dp_netdev_port_state { | |
2588 | struct hmap_position position; | |
2589 | char *name; | |
2590 | }; | |
2591 | ||
2592 | static int | |
2593 | dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep) | |
2594 | { | |
2595 | *statep = xzalloc(sizeof(struct dp_netdev_port_state)); | |
2596 | return 0; | |
2597 | } | |
2598 | ||
2599 | static int | |
2600 | dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_, | |
2601 | struct dpif_port *dpif_port) | |
2602 | { | |
2603 | struct dp_netdev_port_state *state = state_; | |
2604 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
2605 | struct hmap_node *node; | |
2606 | int retval; | |
2607 | ||
2608 | ovs_mutex_lock(&dp->port_mutex); | |
2609 | node = hmap_at_position(&dp->ports, &state->position); | |
2610 | if (node) { | |
2611 | struct dp_netdev_port *port; | |
2612 | ||
2613 | port = CONTAINER_OF(node, struct dp_netdev_port, node); | |
2614 | ||
2615 | free(state->name); | |
2616 | state->name = xstrdup(netdev_get_name(port->netdev)); | |
2617 | dpif_port->name = state->name; | |
2618 | dpif_port->type = port->type; | |
2619 | dpif_port->port_no = port->port_no; | |
2620 | ||
2621 | retval = 0; | |
2622 | } else { | |
2623 | retval = EOF; | |
2624 | } | |
2625 | ovs_mutex_unlock(&dp->port_mutex); | |
2626 | ||
2627 | return retval; | |
2628 | } | |
2629 | ||
2630 | static int | |
2631 | dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_) | |
2632 | { | |
2633 | struct dp_netdev_port_state *state = state_; | |
2634 | free(state->name); | |
2635 | free(state); | |
2636 | return 0; | |
2637 | } | |
2638 | ||
2639 | static int | |
2640 | dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED) | |
2641 | { | |
2642 | struct dpif_netdev *dpif = dpif_netdev_cast(dpif_); | |
2643 | uint64_t new_port_seq; | |
2644 | int error; | |
2645 | ||
2646 | new_port_seq = seq_read(dpif->dp->port_seq); | |
2647 | if (dpif->last_port_seq != new_port_seq) { | |
2648 | dpif->last_port_seq = new_port_seq; | |
2649 | error = ENOBUFS; | |
2650 | } else { | |
2651 | error = EAGAIN; | |
2652 | } | |
2653 | ||
2654 | return error; | |
2655 | } | |
2656 | ||
2657 | static void | |
2658 | dpif_netdev_port_poll_wait(const struct dpif *dpif_) | |
2659 | { | |
2660 | struct dpif_netdev *dpif = dpif_netdev_cast(dpif_); | |
2661 | ||
2662 | seq_wait(dpif->dp->port_seq, dpif->last_port_seq); | |
2663 | } | |
2664 | ||
2665 | static struct dp_netdev_flow * | |
2666 | dp_netdev_flow_cast(const struct dpcls_rule *cr) | |
2667 | { | |
2668 | return cr ? CONTAINER_OF(cr, struct dp_netdev_flow, cr) : NULL; | |
2669 | } | |
2670 | ||
2671 | static bool dp_netdev_flow_ref(struct dp_netdev_flow *flow) | |
2672 | { | |
2673 | return ovs_refcount_try_ref_rcu(&flow->ref_cnt); | |
2674 | } | |
2675 | ||
2676 | /* netdev_flow_key utilities. | |
2677 | * | |
2678 | * netdev_flow_key is basically a miniflow. We use these functions | |
2679 | * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow | |
2680 | * functions (miniflow_clone_inline, miniflow_equal, ...), because: | |
2681 | * | |
2682 | * - Since we are dealing exclusively with miniflows created by | |
2683 | * miniflow_extract(), if the map is different the miniflow is different. | |
2684 | * Therefore we can be faster by comparing the map and the miniflow in a | |
2685 | * single memcmp(). | |
2686 | * - These functions can be inlined by the compiler. */ | |
2687 | ||
2688 | /* Given the number of bits set in miniflow's maps, returns the size of the | |
2689 | * 'netdev_flow_key.mf' */ | |
2690 | static inline size_t | |
2691 | netdev_flow_key_size(size_t flow_u64s) | |
2692 | { | |
2693 | return sizeof(struct miniflow) + MINIFLOW_VALUES_SIZE(flow_u64s); | |
2694 | } | |
2695 | ||
2696 | static inline bool | |
2697 | netdev_flow_key_equal(const struct netdev_flow_key *a, | |
2698 | const struct netdev_flow_key *b) | |
2699 | { | |
2700 | /* 'b->len' may be not set yet. */ | |
2701 | return a->hash == b->hash && !memcmp(&a->mf, &b->mf, a->len); | |
2702 | } | |
2703 | ||
2704 | /* Used to compare 'netdev_flow_key' in the exact match cache to a miniflow. | |
2705 | * The maps are compared bitwise, so both 'key->mf' and 'mf' must have been | |
2706 | * generated by miniflow_extract. */ | |
2707 | static inline bool | |
2708 | netdev_flow_key_equal_mf(const struct netdev_flow_key *key, | |
2709 | const struct miniflow *mf) | |
2710 | { | |
2711 | return !memcmp(&key->mf, mf, key->len); | |
2712 | } | |
2713 | ||
2714 | static inline void | |
2715 | netdev_flow_key_clone(struct netdev_flow_key *dst, | |
2716 | const struct netdev_flow_key *src) | |
2717 | { | |
2718 | memcpy(dst, src, | |
2719 | offsetof(struct netdev_flow_key, mf) + src->len); | |
2720 | } | |
2721 | ||
2722 | /* Initialize a netdev_flow_key 'mask' from 'match'. */ | |
2723 | static inline void | |
2724 | netdev_flow_mask_init(struct netdev_flow_key *mask, | |
2725 | const struct match *match) | |
2726 | { | |
2727 | uint64_t *dst = miniflow_values(&mask->mf); | |
2728 | struct flowmap fmap; | |
2729 | uint32_t hash = 0; | |
2730 | size_t idx; | |
2731 | ||
2732 | /* Only check masks that make sense for the flow. */ | |
2733 | flow_wc_map(&match->flow, &fmap); | |
2734 | flowmap_init(&mask->mf.map); | |
2735 | ||
2736 | FLOWMAP_FOR_EACH_INDEX(idx, fmap) { | |
2737 | uint64_t mask_u64 = flow_u64_value(&match->wc.masks, idx); | |
2738 | ||
2739 | if (mask_u64) { | |
2740 | flowmap_set(&mask->mf.map, idx, 1); | |
2741 | *dst++ = mask_u64; | |
2742 | hash = hash_add64(hash, mask_u64); | |
2743 | } | |
2744 | } | |
2745 | ||
2746 | map_t map; | |
2747 | ||
2748 | FLOWMAP_FOR_EACH_MAP (map, mask->mf.map) { | |
2749 | hash = hash_add64(hash, map); | |
2750 | } | |
2751 | ||
2752 | size_t n = dst - miniflow_get_values(&mask->mf); | |
2753 | ||
2754 | mask->hash = hash_finish(hash, n * 8); | |
2755 | mask->len = netdev_flow_key_size(n); | |
2756 | } | |
2757 | ||
2758 | /* Initializes 'dst' as a copy of 'flow' masked with 'mask'. */ | |
2759 | static inline void | |
2760 | netdev_flow_key_init_masked(struct netdev_flow_key *dst, | |
2761 | const struct flow *flow, | |
2762 | const struct netdev_flow_key *mask) | |
2763 | { | |
2764 | uint64_t *dst_u64 = miniflow_values(&dst->mf); | |
2765 | const uint64_t *mask_u64 = miniflow_get_values(&mask->mf); | |
2766 | uint32_t hash = 0; | |
2767 | uint64_t value; | |
2768 | ||
2769 | dst->len = mask->len; | |
2770 | dst->mf = mask->mf; /* Copy maps. */ | |
2771 | ||
2772 | FLOW_FOR_EACH_IN_MAPS(value, flow, mask->mf.map) { | |
2773 | *dst_u64 = value & *mask_u64++; | |
2774 | hash = hash_add64(hash, *dst_u64++); | |
2775 | } | |
2776 | dst->hash = hash_finish(hash, | |
2777 | (dst_u64 - miniflow_get_values(&dst->mf)) * 8); | |
2778 | } | |
2779 | ||
2780 | static inline bool | |
2781 | emc_entry_alive(struct emc_entry *ce) | |
2782 | { | |
2783 | return ce->flow && !ce->flow->dead; | |
2784 | } | |
2785 | ||
2786 | static void | |
2787 | emc_clear_entry(struct emc_entry *ce) | |
2788 | { | |
2789 | if (ce->flow) { | |
2790 | dp_netdev_flow_unref(ce->flow); | |
2791 | ce->flow = NULL; | |
2792 | } | |
2793 | } | |
2794 | ||
2795 | static inline void | |
2796 | emc_change_entry(struct emc_entry *ce, struct dp_netdev_flow *flow, | |
2797 | const struct netdev_flow_key *key) | |
2798 | { | |
2799 | if (ce->flow != flow) { | |
2800 | if (ce->flow) { | |
2801 | dp_netdev_flow_unref(ce->flow); | |
2802 | } | |
2803 | ||
2804 | if (dp_netdev_flow_ref(flow)) { | |
2805 | ce->flow = flow; | |
2806 | } else { | |
2807 | ce->flow = NULL; | |
2808 | } | |
2809 | } | |
2810 | if (key) { | |
2811 | netdev_flow_key_clone(&ce->key, key); | |
2812 | } | |
2813 | } | |
2814 | ||
2815 | static inline void | |
2816 | emc_insert(struct emc_cache *cache, const struct netdev_flow_key *key, | |
2817 | struct dp_netdev_flow *flow) | |
2818 | { | |
2819 | struct emc_entry *to_be_replaced = NULL; | |
2820 | struct emc_entry *current_entry; | |
2821 | ||
2822 | EMC_FOR_EACH_POS_WITH_HASH(cache, current_entry, key->hash) { | |
2823 | if (netdev_flow_key_equal(¤t_entry->key, key)) { | |
2824 | /* We found the entry with the 'mf' miniflow */ | |
2825 | emc_change_entry(current_entry, flow, NULL); | |
2826 | return; | |
2827 | } | |
2828 | ||
2829 | /* Replacement policy: put the flow in an empty (not alive) entry, or | |
2830 | * in the first entry where it can be */ | |
2831 | if (!to_be_replaced | |
2832 | || (emc_entry_alive(to_be_replaced) | |
2833 | && !emc_entry_alive(current_entry)) | |
2834 | || current_entry->key.hash < to_be_replaced->key.hash) { | |
2835 | to_be_replaced = current_entry; | |
2836 | } | |
2837 | } | |
2838 | /* We didn't find the miniflow in the cache. | |
2839 | * The 'to_be_replaced' entry is where the new flow will be stored */ | |
2840 | ||
2841 | emc_change_entry(to_be_replaced, flow, key); | |
2842 | } | |
2843 | ||
2844 | static inline void | |
2845 | emc_probabilistic_insert(struct dp_netdev_pmd_thread *pmd, | |
2846 | const struct netdev_flow_key *key, | |
2847 | struct dp_netdev_flow *flow) | |
2848 | { | |
2849 | /* Insert an entry into the EMC based on probability value 'min'. By | |
2850 | * default the value is UINT32_MAX / 100 which yields an insertion | |
2851 | * probability of 1/100 ie. 1% */ | |
2852 | ||
2853 | uint32_t min = pmd->ctx.emc_insert_min; | |
2854 | ||
2855 | if (min && random_uint32() <= min) { | |
2856 | emc_insert(&(pmd->flow_cache).emc_cache, key, flow); | |
2857 | } | |
2858 | } | |
2859 | ||
2860 | static inline struct dp_netdev_flow * | |
2861 | emc_lookup(struct emc_cache *cache, const struct netdev_flow_key *key) | |
2862 | { | |
2863 | struct emc_entry *current_entry; | |
2864 | ||
2865 | EMC_FOR_EACH_POS_WITH_HASH(cache, current_entry, key->hash) { | |
2866 | if (current_entry->key.hash == key->hash | |
2867 | && emc_entry_alive(current_entry) | |
2868 | && netdev_flow_key_equal_mf(¤t_entry->key, &key->mf)) { | |
2869 | ||
2870 | /* We found the entry with the 'key->mf' miniflow */ | |
2871 | return current_entry->flow; | |
2872 | } | |
2873 | } | |
2874 | ||
2875 | return NULL; | |
2876 | } | |
2877 | ||
2878 | static inline const struct cmap_node * | |
2879 | smc_entry_get(struct dp_netdev_pmd_thread *pmd, const uint32_t hash) | |
2880 | { | |
2881 | struct smc_cache *cache = &(pmd->flow_cache).smc_cache; | |
2882 | struct smc_bucket *bucket = &cache->buckets[hash & SMC_MASK]; | |
2883 | uint16_t sig = hash >> 16; | |
2884 | uint16_t index = UINT16_MAX; | |
2885 | ||
2886 | for (int i = 0; i < SMC_ENTRY_PER_BUCKET; i++) { | |
2887 | if (bucket->sig[i] == sig) { | |
2888 | index = bucket->flow_idx[i]; | |
2889 | break; | |
2890 | } | |
2891 | } | |
2892 | if (index != UINT16_MAX) { | |
2893 | return cmap_find_by_index(&pmd->flow_table, index); | |
2894 | } | |
2895 | return NULL; | |
2896 | } | |
2897 | ||
2898 | static void | |
2899 | smc_clear_entry(struct smc_bucket *b, int idx) | |
2900 | { | |
2901 | b->flow_idx[idx] = UINT16_MAX; | |
2902 | } | |
2903 | ||
2904 | /* Insert the flow_table index into SMC. Insertion may fail when 1) SMC is | |
2905 | * turned off, 2) the flow_table index is larger than uint16_t can handle. | |
2906 | * If there is already an SMC entry having same signature, the index will be | |
2907 | * updated. If there is no existing entry, but an empty entry is available, | |
2908 | * the empty entry will be taken. If no empty entry or existing same signature, | |
2909 | * a random entry from the hashed bucket will be picked. */ | |
2910 | static inline void | |
2911 | smc_insert(struct dp_netdev_pmd_thread *pmd, | |
2912 | const struct netdev_flow_key *key, | |
2913 | uint32_t hash) | |
2914 | { | |
2915 | struct smc_cache *smc_cache = &(pmd->flow_cache).smc_cache; | |
2916 | struct smc_bucket *bucket = &smc_cache->buckets[key->hash & SMC_MASK]; | |
2917 | uint16_t index; | |
2918 | uint32_t cmap_index; | |
2919 | bool smc_enable_db; | |
2920 | int i; | |
2921 | ||
2922 | atomic_read_relaxed(&pmd->dp->smc_enable_db, &smc_enable_db); | |
2923 | if (!smc_enable_db) { | |
2924 | return; | |
2925 | } | |
2926 | ||
2927 | cmap_index = cmap_find_index(&pmd->flow_table, hash); | |
2928 | index = (cmap_index >= UINT16_MAX) ? UINT16_MAX : (uint16_t)cmap_index; | |
2929 | ||
2930 | /* If the index is larger than SMC can handle (uint16_t), we don't | |
2931 | * insert */ | |
2932 | if (index == UINT16_MAX) { | |
2933 | return; | |
2934 | } | |
2935 | ||
2936 | /* If an entry with same signature already exists, update the index */ | |
2937 | uint16_t sig = key->hash >> 16; | |
2938 | for (i = 0; i < SMC_ENTRY_PER_BUCKET; i++) { | |
2939 | if (bucket->sig[i] == sig) { | |
2940 | bucket->flow_idx[i] = index; | |
2941 | return; | |
2942 | } | |
2943 | } | |
2944 | /* If there is an empty entry, occupy it. */ | |
2945 | for (i = 0; i < SMC_ENTRY_PER_BUCKET; i++) { | |
2946 | if (bucket->flow_idx[i] == UINT16_MAX) { | |
2947 | bucket->sig[i] = sig; | |
2948 | bucket->flow_idx[i] = index; | |
2949 | return; | |
2950 | } | |
2951 | } | |
2952 | /* Otherwise, pick a random entry. */ | |
2953 | i = random_uint32() % SMC_ENTRY_PER_BUCKET; | |
2954 | bucket->sig[i] = sig; | |
2955 | bucket->flow_idx[i] = index; | |
2956 | } | |
2957 | ||
2958 | static struct dp_netdev_flow * | |
2959 | dp_netdev_pmd_lookup_flow(struct dp_netdev_pmd_thread *pmd, | |
2960 | const struct netdev_flow_key *key, | |
2961 | int *lookup_num_p) | |
2962 | { | |
2963 | struct dpcls *cls; | |
2964 | struct dpcls_rule *rule; | |
2965 | odp_port_t in_port = u32_to_odp(MINIFLOW_GET_U32(&key->mf, | |
2966 | in_port.odp_port)); | |
2967 | struct dp_netdev_flow *netdev_flow = NULL; | |
2968 | ||
2969 | cls = dp_netdev_pmd_lookup_dpcls(pmd, in_port); | |
2970 | if (OVS_LIKELY(cls)) { | |
2971 | dpcls_lookup(cls, &key, &rule, 1, lookup_num_p); | |
2972 | netdev_flow = dp_netdev_flow_cast(rule); | |
2973 | } | |
2974 | return netdev_flow; | |
2975 | } | |
2976 | ||
2977 | static struct dp_netdev_flow * | |
2978 | dp_netdev_pmd_find_flow(const struct dp_netdev_pmd_thread *pmd, | |
2979 | const ovs_u128 *ufidp, const struct nlattr *key, | |
2980 | size_t key_len) | |
2981 | { | |
2982 | struct dp_netdev_flow *netdev_flow; | |
2983 | struct flow flow; | |
2984 | ovs_u128 ufid; | |
2985 | ||
2986 | /* If a UFID is not provided, determine one based on the key. */ | |
2987 | if (!ufidp && key && key_len | |
2988 | && !dpif_netdev_flow_from_nlattrs(key, key_len, &flow, false)) { | |
2989 | dpif_flow_hash(pmd->dp->dpif, &flow, sizeof flow, &ufid); | |
2990 | ufidp = &ufid; | |
2991 | } | |
2992 | ||
2993 | if (ufidp) { | |
2994 | CMAP_FOR_EACH_WITH_HASH (netdev_flow, node, dp_netdev_flow_hash(ufidp), | |
2995 | &pmd->flow_table) { | |
2996 | if (ovs_u128_equals(netdev_flow->ufid, *ufidp)) { | |
2997 | return netdev_flow; | |
2998 | } | |
2999 | } | |
3000 | } | |
3001 | ||
3002 | return NULL; | |
3003 | } | |
3004 | ||
3005 | static void | |
3006 | get_dpif_flow_stats(const struct dp_netdev_flow *netdev_flow_, | |
3007 | struct dpif_flow_stats *stats) | |
3008 | { | |
3009 | struct dp_netdev_flow *netdev_flow; | |
3010 | unsigned long long n; | |
3011 | long long used; | |
3012 | uint16_t flags; | |
3013 | ||
3014 | netdev_flow = CONST_CAST(struct dp_netdev_flow *, netdev_flow_); | |
3015 | ||
3016 | atomic_read_relaxed(&netdev_flow->stats.packet_count, &n); | |
3017 | stats->n_packets = n; | |
3018 | atomic_read_relaxed(&netdev_flow->stats.byte_count, &n); | |
3019 | stats->n_bytes = n; | |
3020 | atomic_read_relaxed(&netdev_flow->stats.used, &used); | |
3021 | stats->used = used; | |
3022 | atomic_read_relaxed(&netdev_flow->stats.tcp_flags, &flags); | |
3023 | stats->tcp_flags = flags; | |
3024 | } | |
3025 | ||
3026 | /* Converts to the dpif_flow format, using 'key_buf' and 'mask_buf' for | |
3027 | * storing the netlink-formatted key/mask. 'key_buf' may be the same as | |
3028 | * 'mask_buf'. Actions will be returned without copying, by relying on RCU to | |
3029 | * protect them. */ | |
3030 | static void | |
3031 | dp_netdev_flow_to_dpif_flow(const struct dp_netdev_flow *netdev_flow, | |
3032 | struct ofpbuf *key_buf, struct ofpbuf *mask_buf, | |
3033 | struct dpif_flow *flow, bool terse) | |
3034 | { | |
3035 | if (terse) { | |
3036 | memset(flow, 0, sizeof *flow); | |
3037 | } else { | |
3038 | struct flow_wildcards wc; | |
3039 | struct dp_netdev_actions *actions; | |
3040 | size_t offset; | |
3041 | struct odp_flow_key_parms odp_parms = { | |
3042 | .flow = &netdev_flow->flow, | |
3043 | .mask = &wc.masks, | |
3044 | .support = dp_netdev_support, | |
3045 | }; | |
3046 | ||
3047 | miniflow_expand(&netdev_flow->cr.mask->mf, &wc.masks); | |
3048 | /* in_port is exact matched, but we have left it out from the mask for | |
3049 | * optimnization reasons. Add in_port back to the mask. */ | |
3050 | wc.masks.in_port.odp_port = ODPP_NONE; | |
3051 | ||
3052 | /* Key */ | |
3053 | offset = key_buf->size; | |
3054 | flow->key = ofpbuf_tail(key_buf); | |
3055 | odp_flow_key_from_flow(&odp_parms, key_buf); | |
3056 | flow->key_len = key_buf->size - offset; | |
3057 | ||
3058 | /* Mask */ | |
3059 | offset = mask_buf->size; | |
3060 | flow->mask = ofpbuf_tail(mask_buf); | |
3061 | odp_parms.key_buf = key_buf; | |
3062 | odp_flow_key_from_mask(&odp_parms, mask_buf); | |
3063 | flow->mask_len = mask_buf->size - offset; | |
3064 | ||
3065 | /* Actions */ | |
3066 | actions = dp_netdev_flow_get_actions(netdev_flow); | |
3067 | flow->actions = actions->actions; | |
3068 | flow->actions_len = actions->size; | |
3069 | } | |
3070 | ||
3071 | flow->ufid = netdev_flow->ufid; | |
3072 | flow->ufid_present = true; | |
3073 | flow->pmd_id = netdev_flow->pmd_id; | |
3074 | get_dpif_flow_stats(netdev_flow, &flow->stats); | |
3075 | ||
3076 | flow->attrs.offloaded = false; | |
3077 | flow->attrs.dp_layer = "ovs"; | |
3078 | } | |
3079 | ||
3080 | static int | |
3081 | dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len, | |
3082 | const struct nlattr *mask_key, | |
3083 | uint32_t mask_key_len, const struct flow *flow, | |
3084 | struct flow_wildcards *wc, bool probe) | |
3085 | { | |
3086 | enum odp_key_fitness fitness; | |
3087 | ||
3088 | fitness = odp_flow_key_to_mask(mask_key, mask_key_len, wc, flow, NULL); | |
3089 | if (fitness) { | |
3090 | if (!probe) { | |
3091 | /* This should not happen: it indicates that | |
3092 | * odp_flow_key_from_mask() and odp_flow_key_to_mask() | |
3093 | * disagree on the acceptable form of a mask. Log the problem | |
3094 | * as an error, with enough details to enable debugging. */ | |
3095 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
3096 | ||
3097 | if (!VLOG_DROP_ERR(&rl)) { | |
3098 | struct ds s; | |
3099 | ||
3100 | ds_init(&s); | |
3101 | odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s, | |
3102 | true); | |
3103 | VLOG_ERR("internal error parsing flow mask %s (%s)", | |
3104 | ds_cstr(&s), odp_key_fitness_to_string(fitness)); | |
3105 | ds_destroy(&s); | |
3106 | } | |
3107 | } | |
3108 | ||
3109 | return EINVAL; | |
3110 | } | |
3111 | ||
3112 | return 0; | |
3113 | } | |
3114 | ||
3115 | static int | |
3116 | dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len, | |
3117 | struct flow *flow, bool probe) | |
3118 | { | |
3119 | if (odp_flow_key_to_flow(key, key_len, flow, NULL)) { | |
3120 | if (!probe) { | |
3121 | /* This should not happen: it indicates that | |
3122 | * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on | |
3123 | * the acceptable form of a flow. Log the problem as an error, | |
3124 | * with enough details to enable debugging. */ | |
3125 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
3126 | ||
3127 | if (!VLOG_DROP_ERR(&rl)) { | |
3128 | struct ds s; | |
3129 | ||
3130 | ds_init(&s); | |
3131 | odp_flow_format(key, key_len, NULL, 0, NULL, &s, true); | |
3132 | VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s)); | |
3133 | ds_destroy(&s); | |
3134 | } | |
3135 | } | |
3136 | ||
3137 | return EINVAL; | |
3138 | } | |
3139 | ||
3140 | if (flow->ct_state & DP_NETDEV_CS_UNSUPPORTED_MASK) { | |
3141 | return EINVAL; | |
3142 | } | |
3143 | ||
3144 | return 0; | |
3145 | } | |
3146 | ||
3147 | static int | |
3148 | dpif_netdev_flow_get(const struct dpif *dpif, const struct dpif_flow_get *get) | |
3149 | { | |
3150 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
3151 | struct dp_netdev_flow *netdev_flow; | |
3152 | struct dp_netdev_pmd_thread *pmd; | |
3153 | struct hmapx to_find = HMAPX_INITIALIZER(&to_find); | |
3154 | struct hmapx_node *node; | |
3155 | int error = EINVAL; | |
3156 | ||
3157 | if (get->pmd_id == PMD_ID_NULL) { | |
3158 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
3159 | if (dp_netdev_pmd_try_ref(pmd) && !hmapx_add(&to_find, pmd)) { | |
3160 | dp_netdev_pmd_unref(pmd); | |
3161 | } | |
3162 | } | |
3163 | } else { | |
3164 | pmd = dp_netdev_get_pmd(dp, get->pmd_id); | |
3165 | if (!pmd) { | |
3166 | goto out; | |
3167 | } | |
3168 | hmapx_add(&to_find, pmd); | |
3169 | } | |
3170 | ||
3171 | if (!hmapx_count(&to_find)) { | |
3172 | goto out; | |
3173 | } | |
3174 | ||
3175 | HMAPX_FOR_EACH (node, &to_find) { | |
3176 | pmd = (struct dp_netdev_pmd_thread *) node->data; | |
3177 | netdev_flow = dp_netdev_pmd_find_flow(pmd, get->ufid, get->key, | |
3178 | get->key_len); | |
3179 | if (netdev_flow) { | |
3180 | dp_netdev_flow_to_dpif_flow(netdev_flow, get->buffer, get->buffer, | |
3181 | get->flow, false); | |
3182 | error = 0; | |
3183 | break; | |
3184 | } else { | |
3185 | error = ENOENT; | |
3186 | } | |
3187 | } | |
3188 | ||
3189 | HMAPX_FOR_EACH (node, &to_find) { | |
3190 | pmd = (struct dp_netdev_pmd_thread *) node->data; | |
3191 | dp_netdev_pmd_unref(pmd); | |
3192 | } | |
3193 | out: | |
3194 | hmapx_destroy(&to_find); | |
3195 | return error; | |
3196 | } | |
3197 | ||
3198 | static void | |
3199 | dp_netdev_get_mega_ufid(const struct match *match, ovs_u128 *mega_ufid) | |
3200 | { | |
3201 | struct flow masked_flow; | |
3202 | size_t i; | |
3203 | ||
3204 | for (i = 0; i < sizeof(struct flow); i++) { | |
3205 | ((uint8_t *)&masked_flow)[i] = ((uint8_t *)&match->flow)[i] & | |
3206 | ((uint8_t *)&match->wc)[i]; | |
3207 | } | |
3208 | dpif_flow_hash(NULL, &masked_flow, sizeof(struct flow), mega_ufid); | |
3209 | } | |
3210 | ||
3211 | static struct dp_netdev_flow * | |
3212 | dp_netdev_flow_add(struct dp_netdev_pmd_thread *pmd, | |
3213 | struct match *match, const ovs_u128 *ufid, | |
3214 | const struct nlattr *actions, size_t actions_len) | |
3215 | OVS_REQUIRES(pmd->flow_mutex) | |
3216 | { | |
3217 | struct dp_netdev_flow *flow; | |
3218 | struct netdev_flow_key mask; | |
3219 | struct dpcls *cls; | |
3220 | ||
3221 | /* Make sure in_port is exact matched before we read it. */ | |
3222 | ovs_assert(match->wc.masks.in_port.odp_port == ODPP_NONE); | |
3223 | odp_port_t in_port = match->flow.in_port.odp_port; | |
3224 | ||
3225 | /* As we select the dpcls based on the port number, each netdev flow | |
3226 | * belonging to the same dpcls will have the same odp_port value. | |
3227 | * For performance reasons we wildcard odp_port here in the mask. In the | |
3228 | * typical case dp_hash is also wildcarded, and the resulting 8-byte | |
3229 | * chunk {dp_hash, in_port} will be ignored by netdev_flow_mask_init() and | |
3230 | * will not be part of the subtable mask. | |
3231 | * This will speed up the hash computation during dpcls_lookup() because | |
3232 | * there is one less call to hash_add64() in this case. */ | |
3233 | match->wc.masks.in_port.odp_port = 0; | |
3234 | netdev_flow_mask_init(&mask, match); | |
3235 | match->wc.masks.in_port.odp_port = ODPP_NONE; | |
3236 | ||
3237 | /* Make sure wc does not have metadata. */ | |
3238 | ovs_assert(!FLOWMAP_HAS_FIELD(&mask.mf.map, metadata) | |
3239 | && !FLOWMAP_HAS_FIELD(&mask.mf.map, regs)); | |
3240 | ||
3241 | /* Do not allocate extra space. */ | |
3242 | flow = xmalloc(sizeof *flow - sizeof flow->cr.flow.mf + mask.len); | |
3243 | memset(&flow->stats, 0, sizeof flow->stats); | |
3244 | flow->dead = false; | |
3245 | flow->batch = NULL; | |
3246 | flow->mark = INVALID_FLOW_MARK; | |
3247 | *CONST_CAST(unsigned *, &flow->pmd_id) = pmd->core_id; | |
3248 | *CONST_CAST(struct flow *, &flow->flow) = match->flow; | |
3249 | *CONST_CAST(ovs_u128 *, &flow->ufid) = *ufid; | |
3250 | ovs_refcount_init(&flow->ref_cnt); | |
3251 | ovsrcu_set(&flow->actions, dp_netdev_actions_create(actions, actions_len)); | |
3252 | ||
3253 | dp_netdev_get_mega_ufid(match, CONST_CAST(ovs_u128 *, &flow->mega_ufid)); | |
3254 | netdev_flow_key_init_masked(&flow->cr.flow, &match->flow, &mask); | |
3255 | ||
3256 | /* Select dpcls for in_port. Relies on in_port to be exact match. */ | |
3257 | cls = dp_netdev_pmd_find_dpcls(pmd, in_port); | |
3258 | dpcls_insert(cls, &flow->cr, &mask); | |
3259 | ||
3260 | cmap_insert(&pmd->flow_table, CONST_CAST(struct cmap_node *, &flow->node), | |
3261 | dp_netdev_flow_hash(&flow->ufid)); | |
3262 | ||
3263 | queue_netdev_flow_put(pmd, flow, match, actions, actions_len); | |
3264 | ||
3265 | if (OVS_UNLIKELY(!VLOG_DROP_DBG((&upcall_rl)))) { | |
3266 | struct ds ds = DS_EMPTY_INITIALIZER; | |
3267 | struct ofpbuf key_buf, mask_buf; | |
3268 | struct odp_flow_key_parms odp_parms = { | |
3269 | .flow = &match->flow, | |
3270 | .mask = &match->wc.masks, | |
3271 | .support = dp_netdev_support, | |
3272 | }; | |
3273 | ||
3274 | ofpbuf_init(&key_buf, 0); | |
3275 | ofpbuf_init(&mask_buf, 0); | |
3276 | ||
3277 | odp_flow_key_from_flow(&odp_parms, &key_buf); | |
3278 | odp_parms.key_buf = &key_buf; | |
3279 | odp_flow_key_from_mask(&odp_parms, &mask_buf); | |
3280 | ||
3281 | ds_put_cstr(&ds, "flow_add: "); | |
3282 | odp_format_ufid(ufid, &ds); | |
3283 | ds_put_cstr(&ds, " "); | |
3284 | odp_flow_format(key_buf.data, key_buf.size, | |
3285 | mask_buf.data, mask_buf.size, | |
3286 | NULL, &ds, false); | |
3287 | ds_put_cstr(&ds, ", actions:"); | |
3288 | format_odp_actions(&ds, actions, actions_len, NULL); | |
3289 | ||
3290 | VLOG_DBG("%s", ds_cstr(&ds)); | |
3291 | ||
3292 | ofpbuf_uninit(&key_buf); | |
3293 | ofpbuf_uninit(&mask_buf); | |
3294 | ||
3295 | /* Add a printout of the actual match installed. */ | |
3296 | struct match m; | |
3297 | ds_clear(&ds); | |
3298 | ds_put_cstr(&ds, "flow match: "); | |
3299 | miniflow_expand(&flow->cr.flow.mf, &m.flow); | |
3300 | miniflow_expand(&flow->cr.mask->mf, &m.wc.masks); | |
3301 | memset(&m.tun_md, 0, sizeof m.tun_md); | |
3302 | match_format(&m, NULL, &ds, OFP_DEFAULT_PRIORITY); | |
3303 | ||
3304 | VLOG_DBG("%s", ds_cstr(&ds)); | |
3305 | ||
3306 | ds_destroy(&ds); | |
3307 | } | |
3308 | ||
3309 | return flow; | |
3310 | } | |
3311 | ||
3312 | static int | |
3313 | flow_put_on_pmd(struct dp_netdev_pmd_thread *pmd, | |
3314 | struct netdev_flow_key *key, | |
3315 | struct match *match, | |
3316 | ovs_u128 *ufid, | |
3317 | const struct dpif_flow_put *put, | |
3318 | struct dpif_flow_stats *stats) | |
3319 | { | |
3320 | struct dp_netdev_flow *netdev_flow; | |
3321 | int error = 0; | |
3322 | ||
3323 | if (stats) { | |
3324 | memset(stats, 0, sizeof *stats); | |
3325 | } | |
3326 | ||
3327 | ovs_mutex_lock(&pmd->flow_mutex); | |
3328 | netdev_flow = dp_netdev_pmd_lookup_flow(pmd, key, NULL); | |
3329 | if (!netdev_flow) { | |
3330 | if (put->flags & DPIF_FP_CREATE) { | |
3331 | if (cmap_count(&pmd->flow_table) < MAX_FLOWS) { | |
3332 | dp_netdev_flow_add(pmd, match, ufid, put->actions, | |
3333 | put->actions_len); | |
3334 | error = 0; | |
3335 | } else { | |
3336 | error = EFBIG; | |
3337 | } | |
3338 | } else { | |
3339 | error = ENOENT; | |
3340 | } | |
3341 | } else { | |
3342 | if (put->flags & DPIF_FP_MODIFY) { | |
3343 | struct dp_netdev_actions *new_actions; | |
3344 | struct dp_netdev_actions *old_actions; | |
3345 | ||
3346 | new_actions = dp_netdev_actions_create(put->actions, | |
3347 | put->actions_len); | |
3348 | ||
3349 | old_actions = dp_netdev_flow_get_actions(netdev_flow); | |
3350 | ovsrcu_set(&netdev_flow->actions, new_actions); | |
3351 | ||
3352 | queue_netdev_flow_put(pmd, netdev_flow, match, | |
3353 | put->actions, put->actions_len); | |
3354 | ||
3355 | if (stats) { | |
3356 | get_dpif_flow_stats(netdev_flow, stats); | |
3357 | } | |
3358 | if (put->flags & DPIF_FP_ZERO_STATS) { | |
3359 | /* XXX: The userspace datapath uses thread local statistics | |
3360 | * (for flows), which should be updated only by the owning | |
3361 | * thread. Since we cannot write on stats memory here, | |
3362 | * we choose not to support this flag. Please note: | |
3363 | * - This feature is currently used only by dpctl commands with | |
3364 | * option --clear. | |
3365 | * - Should the need arise, this operation can be implemented | |
3366 | * by keeping a base value (to be update here) for each | |
3367 | * counter, and subtracting it before outputting the stats */ | |
3368 | error = EOPNOTSUPP; | |
3369 | } | |
3370 | ||
3371 | ovsrcu_postpone(dp_netdev_actions_free, old_actions); | |
3372 | } else if (put->flags & DPIF_FP_CREATE) { | |
3373 | error = EEXIST; | |
3374 | } else { | |
3375 | /* Overlapping flow. */ | |
3376 | error = EINVAL; | |
3377 | } | |
3378 | } | |
3379 | ovs_mutex_unlock(&pmd->flow_mutex); | |
3380 | return error; | |
3381 | } | |
3382 | ||
3383 | static int | |
3384 | dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put) | |
3385 | { | |
3386 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
3387 | struct netdev_flow_key key, mask; | |
3388 | struct dp_netdev_pmd_thread *pmd; | |
3389 | struct match match; | |
3390 | ovs_u128 ufid; | |
3391 | int error; | |
3392 | bool probe = put->flags & DPIF_FP_PROBE; | |
3393 | ||
3394 | if (put->stats) { | |
3395 | memset(put->stats, 0, sizeof *put->stats); | |
3396 | } | |
3397 | error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &match.flow, | |
3398 | probe); | |
3399 | if (error) { | |
3400 | return error; | |
3401 | } | |
3402 | error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len, | |
3403 | put->mask, put->mask_len, | |
3404 | &match.flow, &match.wc, probe); | |
3405 | if (error) { | |
3406 | return error; | |
3407 | } | |
3408 | ||
3409 | if (put->ufid) { | |
3410 | ufid = *put->ufid; | |
3411 | } else { | |
3412 | dpif_flow_hash(dpif, &match.flow, sizeof match.flow, &ufid); | |
3413 | } | |
3414 | ||
3415 | /* The Netlink encoding of datapath flow keys cannot express | |
3416 | * wildcarding the presence of a VLAN tag. Instead, a missing VLAN | |
3417 | * tag is interpreted as exact match on the fact that there is no | |
3418 | * VLAN. Unless we refactor a lot of code that translates between | |
3419 | * Netlink and struct flow representations, we have to do the same | |
3420 | * here. This must be in sync with 'match' in handle_packet_upcall(). */ | |
3421 | if (!match.wc.masks.vlans[0].tci) { | |
3422 | match.wc.masks.vlans[0].tci = htons(0xffff); | |
3423 | } | |
3424 | ||
3425 | /* Must produce a netdev_flow_key for lookup. | |
3426 | * Use the same method as employed to create the key when adding | |
3427 | * the flow to the dplcs to make sure they match. */ | |
3428 | netdev_flow_mask_init(&mask, &match); | |
3429 | netdev_flow_key_init_masked(&key, &match.flow, &mask); | |
3430 | ||
3431 | if (put->pmd_id == PMD_ID_NULL) { | |
3432 | if (cmap_count(&dp->poll_threads) == 0) { | |
3433 | return EINVAL; | |
3434 | } | |
3435 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
3436 | struct dpif_flow_stats pmd_stats; | |
3437 | int pmd_error; | |
3438 | ||
3439 | pmd_error = flow_put_on_pmd(pmd, &key, &match, &ufid, put, | |
3440 | &pmd_stats); | |
3441 | if (pmd_error) { | |
3442 | error = pmd_error; | |
3443 | } else if (put->stats) { | |
3444 | put->stats->n_packets += pmd_stats.n_packets; | |
3445 | put->stats->n_bytes += pmd_stats.n_bytes; | |
3446 | put->stats->used = MAX(put->stats->used, pmd_stats.used); | |
3447 | put->stats->tcp_flags |= pmd_stats.tcp_flags; | |
3448 | } | |
3449 | } | |
3450 | } else { | |
3451 | pmd = dp_netdev_get_pmd(dp, put->pmd_id); | |
3452 | if (!pmd) { | |
3453 | return EINVAL; | |
3454 | } | |
3455 | error = flow_put_on_pmd(pmd, &key, &match, &ufid, put, put->stats); | |
3456 | dp_netdev_pmd_unref(pmd); | |
3457 | } | |
3458 | ||
3459 | return error; | |
3460 | } | |
3461 | ||
3462 | static int | |
3463 | flow_del_on_pmd(struct dp_netdev_pmd_thread *pmd, | |
3464 | struct dpif_flow_stats *stats, | |
3465 | const struct dpif_flow_del *del) | |
3466 | { | |
3467 | struct dp_netdev_flow *netdev_flow; | |
3468 | int error = 0; | |
3469 | ||
3470 | ovs_mutex_lock(&pmd->flow_mutex); | |
3471 | netdev_flow = dp_netdev_pmd_find_flow(pmd, del->ufid, del->key, | |
3472 | del->key_len); | |
3473 | if (netdev_flow) { | |
3474 | if (stats) { | |
3475 | get_dpif_flow_stats(netdev_flow, stats); | |
3476 | } | |
3477 | dp_netdev_pmd_remove_flow(pmd, netdev_flow); | |
3478 | } else { | |
3479 | error = ENOENT; | |
3480 | } | |
3481 | ovs_mutex_unlock(&pmd->flow_mutex); | |
3482 | ||
3483 | return error; | |
3484 | } | |
3485 | ||
3486 | static int | |
3487 | dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del) | |
3488 | { | |
3489 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
3490 | struct dp_netdev_pmd_thread *pmd; | |
3491 | int error = 0; | |
3492 | ||
3493 | if (del->stats) { | |
3494 | memset(del->stats, 0, sizeof *del->stats); | |
3495 | } | |
3496 | ||
3497 | if (del->pmd_id == PMD_ID_NULL) { | |
3498 | if (cmap_count(&dp->poll_threads) == 0) { | |
3499 | return EINVAL; | |
3500 | } | |
3501 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
3502 | struct dpif_flow_stats pmd_stats; | |
3503 | int pmd_error; | |
3504 | ||
3505 | pmd_error = flow_del_on_pmd(pmd, &pmd_stats, del); | |
3506 | if (pmd_error) { | |
3507 | error = pmd_error; | |
3508 | } else if (del->stats) { | |
3509 | del->stats->n_packets += pmd_stats.n_packets; | |
3510 | del->stats->n_bytes += pmd_stats.n_bytes; | |
3511 | del->stats->used = MAX(del->stats->used, pmd_stats.used); | |
3512 | del->stats->tcp_flags |= pmd_stats.tcp_flags; | |
3513 | } | |
3514 | } | |
3515 | } else { | |
3516 | pmd = dp_netdev_get_pmd(dp, del->pmd_id); | |
3517 | if (!pmd) { | |
3518 | return EINVAL; | |
3519 | } | |
3520 | error = flow_del_on_pmd(pmd, del->stats, del); | |
3521 | dp_netdev_pmd_unref(pmd); | |
3522 | } | |
3523 | ||
3524 | ||
3525 | return error; | |
3526 | } | |
3527 | ||
3528 | struct dpif_netdev_flow_dump { | |
3529 | struct dpif_flow_dump up; | |
3530 | struct cmap_position poll_thread_pos; | |
3531 | struct cmap_position flow_pos; | |
3532 | struct dp_netdev_pmd_thread *cur_pmd; | |
3533 | int status; | |
3534 | struct ovs_mutex mutex; | |
3535 | }; | |
3536 | ||
3537 | static struct dpif_netdev_flow_dump * | |
3538 | dpif_netdev_flow_dump_cast(struct dpif_flow_dump *dump) | |
3539 | { | |
3540 | return CONTAINER_OF(dump, struct dpif_netdev_flow_dump, up); | |
3541 | } | |
3542 | ||
3543 | static struct dpif_flow_dump * | |
3544 | dpif_netdev_flow_dump_create(const struct dpif *dpif_, bool terse, | |
3545 | struct dpif_flow_dump_types *types OVS_UNUSED) | |
3546 | { | |
3547 | struct dpif_netdev_flow_dump *dump; | |
3548 | ||
3549 | dump = xzalloc(sizeof *dump); | |
3550 | dpif_flow_dump_init(&dump->up, dpif_); | |
3551 | dump->up.terse = terse; | |
3552 | ovs_mutex_init(&dump->mutex); | |
3553 | ||
3554 | return &dump->up; | |
3555 | } | |
3556 | ||
3557 | static int | |
3558 | dpif_netdev_flow_dump_destroy(struct dpif_flow_dump *dump_) | |
3559 | { | |
3560 | struct dpif_netdev_flow_dump *dump = dpif_netdev_flow_dump_cast(dump_); | |
3561 | ||
3562 | ovs_mutex_destroy(&dump->mutex); | |
3563 | free(dump); | |
3564 | return 0; | |
3565 | } | |
3566 | ||
3567 | struct dpif_netdev_flow_dump_thread { | |
3568 | struct dpif_flow_dump_thread up; | |
3569 | struct dpif_netdev_flow_dump *dump; | |
3570 | struct odputil_keybuf keybuf[FLOW_DUMP_MAX_BATCH]; | |
3571 | struct odputil_keybuf maskbuf[FLOW_DUMP_MAX_BATCH]; | |
3572 | }; | |
3573 | ||
3574 | static struct dpif_netdev_flow_dump_thread * | |
3575 | dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread) | |
3576 | { | |
3577 | return CONTAINER_OF(thread, struct dpif_netdev_flow_dump_thread, up); | |
3578 | } | |
3579 | ||
3580 | static struct dpif_flow_dump_thread * | |
3581 | dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump *dump_) | |
3582 | { | |
3583 | struct dpif_netdev_flow_dump *dump = dpif_netdev_flow_dump_cast(dump_); | |
3584 | struct dpif_netdev_flow_dump_thread *thread; | |
3585 | ||
3586 | thread = xmalloc(sizeof *thread); | |
3587 | dpif_flow_dump_thread_init(&thread->up, &dump->up); | |
3588 | thread->dump = dump; | |
3589 | return &thread->up; | |
3590 | } | |
3591 | ||
3592 | static void | |
3593 | dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_) | |
3594 | { | |
3595 | struct dpif_netdev_flow_dump_thread *thread | |
3596 | = dpif_netdev_flow_dump_thread_cast(thread_); | |
3597 | ||
3598 | free(thread); | |
3599 | } | |
3600 | ||
3601 | static int | |
3602 | dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread *thread_, | |
3603 | struct dpif_flow *flows, int max_flows) | |
3604 | { | |
3605 | struct dpif_netdev_flow_dump_thread *thread | |
3606 | = dpif_netdev_flow_dump_thread_cast(thread_); | |
3607 | struct dpif_netdev_flow_dump *dump = thread->dump; | |
3608 | struct dp_netdev_flow *netdev_flows[FLOW_DUMP_MAX_BATCH]; | |
3609 | int n_flows = 0; | |
3610 | int i; | |
3611 | ||
3612 | ovs_mutex_lock(&dump->mutex); | |
3613 | if (!dump->status) { | |
3614 | struct dpif_netdev *dpif = dpif_netdev_cast(thread->up.dpif); | |
3615 | struct dp_netdev *dp = get_dp_netdev(&dpif->dpif); | |
3616 | struct dp_netdev_pmd_thread *pmd = dump->cur_pmd; | |
3617 | int flow_limit = MIN(max_flows, FLOW_DUMP_MAX_BATCH); | |
3618 | ||
3619 | /* First call to dump_next(), extracts the first pmd thread. | |
3620 | * If there is no pmd thread, returns immediately. */ | |
3621 | if (!pmd) { | |
3622 | pmd = dp_netdev_pmd_get_next(dp, &dump->poll_thread_pos); | |
3623 | if (!pmd) { | |
3624 | ovs_mutex_unlock(&dump->mutex); | |
3625 | return n_flows; | |
3626 | ||
3627 | } | |
3628 | } | |
3629 | ||
3630 | do { | |
3631 | for (n_flows = 0; n_flows < flow_limit; n_flows++) { | |
3632 | struct cmap_node *node; | |
3633 | ||
3634 | node = cmap_next_position(&pmd->flow_table, &dump->flow_pos); | |
3635 | if (!node) { | |
3636 | break; | |
3637 | } | |
3638 | netdev_flows[n_flows] = CONTAINER_OF(node, | |
3639 | struct dp_netdev_flow, | |
3640 | node); | |
3641 | } | |
3642 | /* When finishing dumping the current pmd thread, moves to | |
3643 | * the next. */ | |
3644 | if (n_flows < flow_limit) { | |
3645 | memset(&dump->flow_pos, 0, sizeof dump->flow_pos); | |
3646 | dp_netdev_pmd_unref(pmd); | |
3647 | pmd = dp_netdev_pmd_get_next(dp, &dump->poll_thread_pos); | |
3648 | if (!pmd) { | |
3649 | dump->status = EOF; | |
3650 | break; | |
3651 | } | |
3652 | } | |
3653 | /* Keeps the reference to next caller. */ | |
3654 | dump->cur_pmd = pmd; | |
3655 | ||
3656 | /* If the current dump is empty, do not exit the loop, since the | |
3657 | * remaining pmds could have flows to be dumped. Just dumps again | |
3658 | * on the new 'pmd'. */ | |
3659 | } while (!n_flows); | |
3660 | } | |
3661 | ovs_mutex_unlock(&dump->mutex); | |
3662 | ||
3663 | for (i = 0; i < n_flows; i++) { | |
3664 | struct odputil_keybuf *maskbuf = &thread->maskbuf[i]; | |
3665 | struct odputil_keybuf *keybuf = &thread->keybuf[i]; | |
3666 | struct dp_netdev_flow *netdev_flow = netdev_flows[i]; | |
3667 | struct dpif_flow *f = &flows[i]; | |
3668 | struct ofpbuf key, mask; | |
3669 | ||
3670 | ofpbuf_use_stack(&key, keybuf, sizeof *keybuf); | |
3671 | ofpbuf_use_stack(&mask, maskbuf, sizeof *maskbuf); | |
3672 | dp_netdev_flow_to_dpif_flow(netdev_flow, &key, &mask, f, | |
3673 | dump->up.terse); | |
3674 | } | |
3675 | ||
3676 | return n_flows; | |
3677 | } | |
3678 | ||
3679 | static int | |
3680 | dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute) | |
3681 | OVS_NO_THREAD_SAFETY_ANALYSIS | |
3682 | { | |
3683 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
3684 | struct dp_netdev_pmd_thread *pmd; | |
3685 | struct dp_packet_batch pp; | |
3686 | ||
3687 | if (dp_packet_size(execute->packet) < ETH_HEADER_LEN || | |
3688 | dp_packet_size(execute->packet) > UINT16_MAX) { | |
3689 | return EINVAL; | |
3690 | } | |
3691 | ||
3692 | /* Tries finding the 'pmd'. If NULL is returned, that means | |
3693 | * the current thread is a non-pmd thread and should use | |
3694 | * dp_netdev_get_pmd(dp, NON_PMD_CORE_ID). */ | |
3695 | pmd = ovsthread_getspecific(dp->per_pmd_key); | |
3696 | if (!pmd) { | |
3697 | pmd = dp_netdev_get_pmd(dp, NON_PMD_CORE_ID); | |
3698 | if (!pmd) { | |
3699 | return EBUSY; | |
3700 | } | |
3701 | } | |
3702 | ||
3703 | if (execute->probe) { | |
3704 | /* If this is part of a probe, Drop the packet, since executing | |
3705 | * the action may actually cause spurious packets be sent into | |
3706 | * the network. */ | |
3707 | if (pmd->core_id == NON_PMD_CORE_ID) { | |
3708 | dp_netdev_pmd_unref(pmd); | |
3709 | } | |
3710 | return 0; | |
3711 | } | |
3712 | ||
3713 | /* If the current thread is non-pmd thread, acquires | |
3714 | * the 'non_pmd_mutex'. */ | |
3715 | if (pmd->core_id == NON_PMD_CORE_ID) { | |
3716 | ovs_mutex_lock(&dp->non_pmd_mutex); | |
3717 | } | |
3718 | ||
3719 | /* Update current time in PMD context. We don't care about EMC insertion | |
3720 | * probability, because we are on a slow path. */ | |
3721 | pmd_thread_ctx_time_update(pmd); | |
3722 | ||
3723 | /* The action processing expects the RSS hash to be valid, because | |
3724 | * it's always initialized at the beginning of datapath processing. | |
3725 | * In this case, though, 'execute->packet' may not have gone through | |
3726 | * the datapath at all, it may have been generated by the upper layer | |
3727 | * (OpenFlow packet-out, BFD frame, ...). */ | |
3728 | if (!dp_packet_rss_valid(execute->packet)) { | |
3729 | dp_packet_set_rss_hash(execute->packet, | |
3730 | flow_hash_5tuple(execute->flow, 0)); | |
3731 | } | |
3732 | ||
3733 | dp_packet_batch_init_packet(&pp, execute->packet); | |
3734 | pp.do_not_steal = true; | |
3735 | dp_netdev_execute_actions(pmd, &pp, false, execute->flow, | |
3736 | execute->actions, execute->actions_len); | |
3737 | dp_netdev_pmd_flush_output_packets(pmd, true); | |
3738 | ||
3739 | if (pmd->core_id == NON_PMD_CORE_ID) { | |
3740 | ovs_mutex_unlock(&dp->non_pmd_mutex); | |
3741 | dp_netdev_pmd_unref(pmd); | |
3742 | } | |
3743 | ||
3744 | return 0; | |
3745 | } | |
3746 | ||
3747 | static void | |
3748 | dpif_netdev_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops, | |
3749 | enum dpif_offload_type offload_type OVS_UNUSED) | |
3750 | { | |
3751 | size_t i; | |
3752 | ||
3753 | for (i = 0; i < n_ops; i++) { | |
3754 | struct dpif_op *op = ops[i]; | |
3755 | ||
3756 | switch (op->type) { | |
3757 | case DPIF_OP_FLOW_PUT: | |
3758 | op->error = dpif_netdev_flow_put(dpif, &op->flow_put); | |
3759 | break; | |
3760 | ||
3761 | case DPIF_OP_FLOW_DEL: | |
3762 | op->error = dpif_netdev_flow_del(dpif, &op->flow_del); | |
3763 | break; | |
3764 | ||
3765 | case DPIF_OP_EXECUTE: | |
3766 | op->error = dpif_netdev_execute(dpif, &op->execute); | |
3767 | break; | |
3768 | ||
3769 | case DPIF_OP_FLOW_GET: | |
3770 | op->error = dpif_netdev_flow_get(dpif, &op->flow_get); | |
3771 | break; | |
3772 | } | |
3773 | } | |
3774 | } | |
3775 | ||
3776 | /* Enable or Disable PMD auto load balancing. */ | |
3777 | static void | |
3778 | set_pmd_auto_lb(struct dp_netdev *dp) | |
3779 | { | |
3780 | unsigned int cnt = 0; | |
3781 | struct dp_netdev_pmd_thread *pmd; | |
3782 | struct pmd_auto_lb *pmd_alb = &dp->pmd_alb; | |
3783 | ||
3784 | bool enable_alb = false; | |
3785 | bool multi_rxq = false; | |
3786 | bool pmd_rxq_assign_cyc = dp->pmd_rxq_assign_cyc; | |
3787 | ||
3788 | /* Ensure that there is at least 2 non-isolated PMDs and | |
3789 | * one of them is polling more than one rxq. */ | |
3790 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
3791 | if (pmd->core_id == NON_PMD_CORE_ID || pmd->isolated) { | |
3792 | continue; | |
3793 | } | |
3794 | ||
3795 | if (hmap_count(&pmd->poll_list) > 1) { | |
3796 | multi_rxq = true; | |
3797 | } | |
3798 | if (cnt && multi_rxq) { | |
3799 | enable_alb = true; | |
3800 | break; | |
3801 | } | |
3802 | cnt++; | |
3803 | } | |
3804 | ||
3805 | /* Enable auto LB if it is requested and cycle based assignment is true. */ | |
3806 | enable_alb = enable_alb && pmd_rxq_assign_cyc && | |
3807 | pmd_alb->auto_lb_requested; | |
3808 | ||
3809 | if (pmd_alb->is_enabled != enable_alb) { | |
3810 | pmd_alb->is_enabled = enable_alb; | |
3811 | if (pmd_alb->is_enabled) { | |
3812 | VLOG_INFO("PMD auto load balance is enabled " | |
3813 | "(with rebalance interval:%"PRIu64" msec)", | |
3814 | pmd_alb->rebalance_intvl); | |
3815 | } else { | |
3816 | pmd_alb->rebalance_poll_timer = 0; | |
3817 | VLOG_INFO("PMD auto load balance is disabled"); | |
3818 | } | |
3819 | } | |
3820 | ||
3821 | } | |
3822 | ||
3823 | /* Applies datapath configuration from the database. Some of the changes are | |
3824 | * actually applied in dpif_netdev_run(). */ | |
3825 | static int | |
3826 | dpif_netdev_set_config(struct dpif *dpif, const struct smap *other_config) | |
3827 | { | |
3828 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
3829 | const char *cmask = smap_get(other_config, "pmd-cpu-mask"); | |
3830 | const char *pmd_rxq_assign = smap_get_def(other_config, "pmd-rxq-assign", | |
3831 | "cycles"); | |
3832 | unsigned long long insert_prob = | |
3833 | smap_get_ullong(other_config, "emc-insert-inv-prob", | |
3834 | DEFAULT_EM_FLOW_INSERT_INV_PROB); | |
3835 | uint32_t insert_min, cur_min; | |
3836 | uint32_t tx_flush_interval, cur_tx_flush_interval; | |
3837 | uint64_t rebalance_intvl; | |
3838 | ||
3839 | tx_flush_interval = smap_get_int(other_config, "tx-flush-interval", | |
3840 | DEFAULT_TX_FLUSH_INTERVAL); | |
3841 | atomic_read_relaxed(&dp->tx_flush_interval, &cur_tx_flush_interval); | |
3842 | if (tx_flush_interval != cur_tx_flush_interval) { | |
3843 | atomic_store_relaxed(&dp->tx_flush_interval, tx_flush_interval); | |
3844 | VLOG_INFO("Flushing interval for tx queues set to %"PRIu32" us", | |
3845 | tx_flush_interval); | |
3846 | } | |
3847 | ||
3848 | if (!nullable_string_is_equal(dp->pmd_cmask, cmask)) { | |
3849 | free(dp->pmd_cmask); | |
3850 | dp->pmd_cmask = nullable_xstrdup(cmask); | |
3851 | dp_netdev_request_reconfigure(dp); | |
3852 | } | |
3853 | ||
3854 | atomic_read_relaxed(&dp->emc_insert_min, &cur_min); | |
3855 | if (insert_prob <= UINT32_MAX) { | |
3856 | insert_min = insert_prob == 0 ? 0 : UINT32_MAX / insert_prob; | |
3857 | } else { | |
3858 | insert_min = DEFAULT_EM_FLOW_INSERT_MIN; | |
3859 | insert_prob = DEFAULT_EM_FLOW_INSERT_INV_PROB; | |
3860 | } | |
3861 | ||
3862 | if (insert_min != cur_min) { | |
3863 | atomic_store_relaxed(&dp->emc_insert_min, insert_min); | |
3864 | if (insert_min == 0) { | |
3865 | VLOG_INFO("EMC insertion probability changed to zero"); | |
3866 | } else { | |
3867 | VLOG_INFO("EMC insertion probability changed to 1/%llu (~%.2f%%)", | |
3868 | insert_prob, (100 / (float)insert_prob)); | |
3869 | } | |
3870 | } | |
3871 | ||
3872 | bool perf_enabled = smap_get_bool(other_config, "pmd-perf-metrics", false); | |
3873 | bool cur_perf_enabled; | |
3874 | atomic_read_relaxed(&dp->pmd_perf_metrics, &cur_perf_enabled); | |
3875 | if (perf_enabled != cur_perf_enabled) { | |
3876 | atomic_store_relaxed(&dp->pmd_perf_metrics, perf_enabled); | |
3877 | if (perf_enabled) { | |
3878 | VLOG_INFO("PMD performance metrics collection enabled"); | |
3879 | } else { | |
3880 | VLOG_INFO("PMD performance metrics collection disabled"); | |
3881 | } | |
3882 | } | |
3883 | ||
3884 | bool smc_enable = smap_get_bool(other_config, "smc-enable", false); | |
3885 | bool cur_smc; | |
3886 | atomic_read_relaxed(&dp->smc_enable_db, &cur_smc); | |
3887 | if (smc_enable != cur_smc) { | |
3888 | atomic_store_relaxed(&dp->smc_enable_db, smc_enable); | |
3889 | if (smc_enable) { | |
3890 | VLOG_INFO("SMC cache is enabled"); | |
3891 | } else { | |
3892 | VLOG_INFO("SMC cache is disabled"); | |
3893 | } | |
3894 | } | |
3895 | ||
3896 | bool pmd_rxq_assign_cyc = !strcmp(pmd_rxq_assign, "cycles"); | |
3897 | if (!pmd_rxq_assign_cyc && strcmp(pmd_rxq_assign, "roundrobin")) { | |
3898 | VLOG_WARN("Unsupported Rxq to PMD assignment mode in pmd-rxq-assign. " | |
3899 | "Defaulting to 'cycles'."); | |
3900 | pmd_rxq_assign_cyc = true; | |
3901 | pmd_rxq_assign = "cycles"; | |
3902 | } | |
3903 | if (dp->pmd_rxq_assign_cyc != pmd_rxq_assign_cyc) { | |
3904 | dp->pmd_rxq_assign_cyc = pmd_rxq_assign_cyc; | |
3905 | VLOG_INFO("Rxq to PMD assignment mode changed to: \'%s\'.", | |
3906 | pmd_rxq_assign); | |
3907 | dp_netdev_request_reconfigure(dp); | |
3908 | } | |
3909 | ||
3910 | struct pmd_auto_lb *pmd_alb = &dp->pmd_alb; | |
3911 | pmd_alb->auto_lb_requested = smap_get_bool(other_config, "pmd-auto-lb", | |
3912 | false); | |
3913 | ||
3914 | rebalance_intvl = smap_get_int(other_config, "pmd-auto-lb-rebal-interval", | |
3915 | ALB_PMD_REBALANCE_POLL_INTERVAL); | |
3916 | ||
3917 | /* Input is in min, convert it to msec. */ | |
3918 | rebalance_intvl = | |
3919 | rebalance_intvl ? rebalance_intvl * MIN_TO_MSEC : MIN_TO_MSEC; | |
3920 | ||
3921 | if (pmd_alb->rebalance_intvl != rebalance_intvl) { | |
3922 | pmd_alb->rebalance_intvl = rebalance_intvl; | |
3923 | } | |
3924 | ||
3925 | set_pmd_auto_lb(dp); | |
3926 | return 0; | |
3927 | } | |
3928 | ||
3929 | /* Parses affinity list and returns result in 'core_ids'. */ | |
3930 | static int | |
3931 | parse_affinity_list(const char *affinity_list, unsigned *core_ids, int n_rxq) | |
3932 | { | |
3933 | unsigned i; | |
3934 | char *list, *copy, *key, *value; | |
3935 | int error = 0; | |
3936 | ||
3937 | for (i = 0; i < n_rxq; i++) { | |
3938 | core_ids[i] = OVS_CORE_UNSPEC; | |
3939 | } | |
3940 | ||
3941 | if (!affinity_list) { | |
3942 | return 0; | |
3943 | } | |
3944 | ||
3945 | list = copy = xstrdup(affinity_list); | |
3946 | ||
3947 | while (ofputil_parse_key_value(&list, &key, &value)) { | |
3948 | int rxq_id, core_id; | |
3949 | ||
3950 | if (!str_to_int(key, 0, &rxq_id) || rxq_id < 0 | |
3951 | || !str_to_int(value, 0, &core_id) || core_id < 0) { | |
3952 | error = EINVAL; | |
3953 | break; | |
3954 | } | |
3955 | ||
3956 | if (rxq_id < n_rxq) { | |
3957 | core_ids[rxq_id] = core_id; | |
3958 | } | |
3959 | } | |
3960 | ||
3961 | free(copy); | |
3962 | return error; | |
3963 | } | |
3964 | ||
3965 | /* Parses 'affinity_list' and applies configuration if it is valid. */ | |
3966 | static int | |
3967 | dpif_netdev_port_set_rxq_affinity(struct dp_netdev_port *port, | |
3968 | const char *affinity_list) | |
3969 | { | |
3970 | unsigned *core_ids, i; | |
3971 | int error = 0; | |
3972 | ||
3973 | core_ids = xmalloc(port->n_rxq * sizeof *core_ids); | |
3974 | if (parse_affinity_list(affinity_list, core_ids, port->n_rxq)) { | |
3975 | error = EINVAL; | |
3976 | goto exit; | |
3977 | } | |
3978 | ||
3979 | for (i = 0; i < port->n_rxq; i++) { | |
3980 | port->rxqs[i].core_id = core_ids[i]; | |
3981 | } | |
3982 | ||
3983 | exit: | |
3984 | free(core_ids); | |
3985 | return error; | |
3986 | } | |
3987 | ||
3988 | /* Returns 'true' if one of the 'port's RX queues exists in 'poll_list' | |
3989 | * of given PMD thread. */ | |
3990 | static bool | |
3991 | dpif_netdev_pmd_polls_port(struct dp_netdev_pmd_thread *pmd, | |
3992 | struct dp_netdev_port *port) | |
3993 | OVS_EXCLUDED(pmd->port_mutex) | |
3994 | { | |
3995 | struct rxq_poll *poll; | |
3996 | bool found = false; | |
3997 | ||
3998 | ovs_mutex_lock(&pmd->port_mutex); | |
3999 | HMAP_FOR_EACH (poll, node, &pmd->poll_list) { | |
4000 | if (port == poll->rxq->port) { | |
4001 | found = true; | |
4002 | break; | |
4003 | } | |
4004 | } | |
4005 | ovs_mutex_unlock(&pmd->port_mutex); | |
4006 | return found; | |
4007 | } | |
4008 | ||
4009 | /* Updates port configuration from the database. The changes are actually | |
4010 | * applied in dpif_netdev_run(). */ | |
4011 | static int | |
4012 | dpif_netdev_port_set_config(struct dpif *dpif, odp_port_t port_no, | |
4013 | const struct smap *cfg) | |
4014 | { | |
4015 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
4016 | struct dp_netdev_port *port; | |
4017 | int error = 0; | |
4018 | const char *affinity_list = smap_get(cfg, "pmd-rxq-affinity"); | |
4019 | bool emc_enabled = smap_get_bool(cfg, "emc-enable", true); | |
4020 | ||
4021 | ovs_mutex_lock(&dp->port_mutex); | |
4022 | error = get_port_by_number(dp, port_no, &port); | |
4023 | if (error) { | |
4024 | goto unlock; | |
4025 | } | |
4026 | ||
4027 | if (emc_enabled != port->emc_enabled) { | |
4028 | struct dp_netdev_pmd_thread *pmd; | |
4029 | struct ds ds = DS_EMPTY_INITIALIZER; | |
4030 | uint32_t cur_min, insert_prob; | |
4031 | ||
4032 | port->emc_enabled = emc_enabled; | |
4033 | /* Mark for reload all the threads that polls this port and request | |
4034 | * for reconfiguration for the actual reloading of threads. */ | |
4035 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
4036 | if (dpif_netdev_pmd_polls_port(pmd, port)) { | |
4037 | pmd->need_reload = true; | |
4038 | } | |
4039 | } | |
4040 | dp_netdev_request_reconfigure(dp); | |
4041 | ||
4042 | ds_put_format(&ds, "%s: EMC has been %s.", | |
4043 | netdev_get_name(port->netdev), | |
4044 | (emc_enabled) ? "enabled" : "disabled"); | |
4045 | if (emc_enabled) { | |
4046 | ds_put_cstr(&ds, " Current insertion probability is "); | |
4047 | atomic_read_relaxed(&dp->emc_insert_min, &cur_min); | |
4048 | if (!cur_min) { | |
4049 | ds_put_cstr(&ds, "zero."); | |
4050 | } else { | |
4051 | insert_prob = UINT32_MAX / cur_min; | |
4052 | ds_put_format(&ds, "1/%"PRIu32" (~%.2f%%).", | |
4053 | insert_prob, 100 / (float) insert_prob); | |
4054 | } | |
4055 | } | |
4056 | VLOG_INFO("%s", ds_cstr(&ds)); | |
4057 | ds_destroy(&ds); | |
4058 | } | |
4059 | ||
4060 | /* Checking for RXq affinity changes. */ | |
4061 | if (!netdev_is_pmd(port->netdev) | |
4062 | || nullable_string_is_equal(affinity_list, port->rxq_affinity_list)) { | |
4063 | goto unlock; | |
4064 | } | |
4065 | ||
4066 | error = dpif_netdev_port_set_rxq_affinity(port, affinity_list); | |
4067 | if (error) { | |
4068 | goto unlock; | |
4069 | } | |
4070 | free(port->rxq_affinity_list); | |
4071 | port->rxq_affinity_list = nullable_xstrdup(affinity_list); | |
4072 | ||
4073 | dp_netdev_request_reconfigure(dp); | |
4074 | unlock: | |
4075 | ovs_mutex_unlock(&dp->port_mutex); | |
4076 | return error; | |
4077 | } | |
4078 | ||
4079 | static int | |
4080 | dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED, | |
4081 | uint32_t queue_id, uint32_t *priority) | |
4082 | { | |
4083 | *priority = queue_id; | |
4084 | return 0; | |
4085 | } | |
4086 | ||
4087 | \f | |
4088 | /* Creates and returns a new 'struct dp_netdev_actions', whose actions are | |
4089 | * a copy of the 'size' bytes of 'actions' input parameters. */ | |
4090 | struct dp_netdev_actions * | |
4091 | dp_netdev_actions_create(const struct nlattr *actions, size_t size) | |
4092 | { | |
4093 | struct dp_netdev_actions *netdev_actions; | |
4094 | ||
4095 | netdev_actions = xmalloc(sizeof *netdev_actions + size); | |
4096 | memcpy(netdev_actions->actions, actions, size); | |
4097 | netdev_actions->size = size; | |
4098 | ||
4099 | return netdev_actions; | |
4100 | } | |
4101 | ||
4102 | struct dp_netdev_actions * | |
4103 | dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow) | |
4104 | { | |
4105 | return ovsrcu_get(struct dp_netdev_actions *, &flow->actions); | |
4106 | } | |
4107 | ||
4108 | static void | |
4109 | dp_netdev_actions_free(struct dp_netdev_actions *actions) | |
4110 | { | |
4111 | free(actions); | |
4112 | } | |
4113 | \f | |
4114 | static void | |
4115 | dp_netdev_rxq_set_cycles(struct dp_netdev_rxq *rx, | |
4116 | enum rxq_cycles_counter_type type, | |
4117 | unsigned long long cycles) | |
4118 | { | |
4119 | atomic_store_relaxed(&rx->cycles[type], cycles); | |
4120 | } | |
4121 | ||
4122 | static void | |
4123 | dp_netdev_rxq_add_cycles(struct dp_netdev_rxq *rx, | |
4124 | enum rxq_cycles_counter_type type, | |
4125 | unsigned long long cycles) | |
4126 | { | |
4127 | non_atomic_ullong_add(&rx->cycles[type], cycles); | |
4128 | } | |
4129 | ||
4130 | static uint64_t | |
4131 | dp_netdev_rxq_get_cycles(struct dp_netdev_rxq *rx, | |
4132 | enum rxq_cycles_counter_type type) | |
4133 | { | |
4134 | unsigned long long processing_cycles; | |
4135 | atomic_read_relaxed(&rx->cycles[type], &processing_cycles); | |
4136 | return processing_cycles; | |
4137 | } | |
4138 | ||
4139 | static void | |
4140 | dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq *rx, | |
4141 | unsigned long long cycles) | |
4142 | { | |
4143 | unsigned int idx = rx->intrvl_idx++ % PMD_RXQ_INTERVAL_MAX; | |
4144 | atomic_store_relaxed(&rx->cycles_intrvl[idx], cycles); | |
4145 | } | |
4146 | ||
4147 | static uint64_t | |
4148 | dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq *rx, unsigned idx) | |
4149 | { | |
4150 | unsigned long long processing_cycles; | |
4151 | atomic_read_relaxed(&rx->cycles_intrvl[idx], &processing_cycles); | |
4152 | return processing_cycles; | |
4153 | } | |
4154 | ||
4155 | #if ATOMIC_ALWAYS_LOCK_FREE_8B | |
4156 | static inline bool | |
4157 | pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread *pmd) | |
4158 | { | |
4159 | bool pmd_perf_enabled; | |
4160 | atomic_read_relaxed(&pmd->dp->pmd_perf_metrics, &pmd_perf_enabled); | |
4161 | return pmd_perf_enabled; | |
4162 | } | |
4163 | #else | |
4164 | /* If stores and reads of 64-bit integers are not atomic, the full PMD | |
4165 | * performance metrics are not available as locked access to 64 bit | |
4166 | * integers would be prohibitively expensive. */ | |
4167 | static inline bool | |
4168 | pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread *pmd OVS_UNUSED) | |
4169 | { | |
4170 | return false; | |
4171 | } | |
4172 | #endif | |
4173 | ||
4174 | static int | |
4175 | dp_netdev_pmd_flush_output_on_port(struct dp_netdev_pmd_thread *pmd, | |
4176 | struct tx_port *p) | |
4177 | { | |
4178 | int i; | |
4179 | int tx_qid; | |
4180 | int output_cnt; | |
4181 | bool dynamic_txqs; | |
4182 | struct cycle_timer timer; | |
4183 | uint64_t cycles; | |
4184 | uint32_t tx_flush_interval; | |
4185 | ||
4186 | cycle_timer_start(&pmd->perf_stats, &timer); | |
4187 | ||
4188 | dynamic_txqs = p->port->dynamic_txqs; | |
4189 | if (dynamic_txqs) { | |
4190 | tx_qid = dpif_netdev_xps_get_tx_qid(pmd, p); | |
4191 | } else { | |
4192 | tx_qid = pmd->static_tx_qid; | |
4193 | } | |
4194 | ||
4195 | output_cnt = dp_packet_batch_size(&p->output_pkts); | |
4196 | ovs_assert(output_cnt > 0); | |
4197 | ||
4198 | netdev_send(p->port->netdev, tx_qid, &p->output_pkts, dynamic_txqs); | |
4199 | dp_packet_batch_init(&p->output_pkts); | |
4200 | ||
4201 | /* Update time of the next flush. */ | |
4202 | atomic_read_relaxed(&pmd->dp->tx_flush_interval, &tx_flush_interval); | |
4203 | p->flush_time = pmd->ctx.now + tx_flush_interval; | |
4204 | ||
4205 | ovs_assert(pmd->n_output_batches > 0); | |
4206 | pmd->n_output_batches--; | |
4207 | ||
4208 | pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_SENT_PKTS, output_cnt); | |
4209 | pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_SENT_BATCHES, 1); | |
4210 | ||
4211 | /* Distribute send cycles evenly among transmitted packets and assign to | |
4212 | * their respective rx queues. */ | |
4213 | cycles = cycle_timer_stop(&pmd->perf_stats, &timer) / output_cnt; | |
4214 | for (i = 0; i < output_cnt; i++) { | |
4215 | if (p->output_pkts_rxqs[i]) { | |
4216 | dp_netdev_rxq_add_cycles(p->output_pkts_rxqs[i], | |
4217 | RXQ_CYCLES_PROC_CURR, cycles); | |
4218 | } | |
4219 | } | |
4220 | ||
4221 | return output_cnt; | |
4222 | } | |
4223 | ||
4224 | static int | |
4225 | dp_netdev_pmd_flush_output_packets(struct dp_netdev_pmd_thread *pmd, | |
4226 | bool force) | |
4227 | { | |
4228 | struct tx_port *p; | |
4229 | int output_cnt = 0; | |
4230 | ||
4231 | if (!pmd->n_output_batches) { | |
4232 | return 0; | |
4233 | } | |
4234 | ||
4235 | HMAP_FOR_EACH (p, node, &pmd->send_port_cache) { | |
4236 | if (!dp_packet_batch_is_empty(&p->output_pkts) | |
4237 | && (force || pmd->ctx.now >= p->flush_time)) { | |
4238 | output_cnt += dp_netdev_pmd_flush_output_on_port(pmd, p); | |
4239 | } | |
4240 | } | |
4241 | return output_cnt; | |
4242 | } | |
4243 | ||
4244 | static int | |
4245 | dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread *pmd, | |
4246 | struct dp_netdev_rxq *rxq, | |
4247 | odp_port_t port_no) | |
4248 | { | |
4249 | struct pmd_perf_stats *s = &pmd->perf_stats; | |
4250 | struct dp_packet_batch batch; | |
4251 | struct cycle_timer timer; | |
4252 | int error; | |
4253 | int batch_cnt = 0; | |
4254 | int rem_qlen = 0, *qlen_p = NULL; | |
4255 | uint64_t cycles; | |
4256 | ||
4257 | /* Measure duration for polling and processing rx burst. */ | |
4258 | cycle_timer_start(&pmd->perf_stats, &timer); | |
4259 | ||
4260 | pmd->ctx.last_rxq = rxq; | |
4261 | dp_packet_batch_init(&batch); | |
4262 | ||
4263 | /* Fetch the rx queue length only for vhostuser ports. */ | |
4264 | if (pmd_perf_metrics_enabled(pmd) && rxq->is_vhost) { | |
4265 | qlen_p = &rem_qlen; | |
4266 | } | |
4267 | ||
4268 | error = netdev_rxq_recv(rxq->rx, &batch, qlen_p); | |
4269 | if (!error) { | |
4270 | /* At least one packet received. */ | |
4271 | *recirc_depth_get() = 0; | |
4272 | pmd_thread_ctx_time_update(pmd); | |
4273 | batch_cnt = dp_packet_batch_size(&batch); | |
4274 | if (pmd_perf_metrics_enabled(pmd)) { | |
4275 | /* Update batch histogram. */ | |
4276 | s->current.batches++; | |
4277 | histogram_add_sample(&s->pkts_per_batch, batch_cnt); | |
4278 | /* Update the maximum vhost rx queue fill level. */ | |
4279 | if (rxq->is_vhost && rem_qlen >= 0) { | |
4280 | uint32_t qfill = batch_cnt + rem_qlen; | |
4281 | if (qfill > s->current.max_vhost_qfill) { | |
4282 | s->current.max_vhost_qfill = qfill; | |
4283 | } | |
4284 | } | |
4285 | } | |
4286 | /* Process packet batch. */ | |
4287 | dp_netdev_input(pmd, &batch, port_no); | |
4288 | ||
4289 | /* Assign processing cycles to rx queue. */ | |
4290 | cycles = cycle_timer_stop(&pmd->perf_stats, &timer); | |
4291 | dp_netdev_rxq_add_cycles(rxq, RXQ_CYCLES_PROC_CURR, cycles); | |
4292 | ||
4293 | dp_netdev_pmd_flush_output_packets(pmd, false); | |
4294 | } else { | |
4295 | /* Discard cycles. */ | |
4296 | cycle_timer_stop(&pmd->perf_stats, &timer); | |
4297 | if (error != EAGAIN && error != EOPNOTSUPP) { | |
4298 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4299 | ||
4300 | VLOG_ERR_RL(&rl, "error receiving data from %s: %s", | |
4301 | netdev_rxq_get_name(rxq->rx), ovs_strerror(error)); | |
4302 | } | |
4303 | } | |
4304 | ||
4305 | pmd->ctx.last_rxq = NULL; | |
4306 | ||
4307 | return batch_cnt; | |
4308 | } | |
4309 | ||
4310 | static struct tx_port * | |
4311 | tx_port_lookup(const struct hmap *hmap, odp_port_t port_no) | |
4312 | { | |
4313 | struct tx_port *tx; | |
4314 | ||
4315 | HMAP_FOR_EACH_IN_BUCKET (tx, node, hash_port_no(port_no), hmap) { | |
4316 | if (tx->port->port_no == port_no) { | |
4317 | return tx; | |
4318 | } | |
4319 | } | |
4320 | ||
4321 | return NULL; | |
4322 | } | |
4323 | ||
4324 | static int | |
4325 | port_reconfigure(struct dp_netdev_port *port) | |
4326 | { | |
4327 | struct netdev *netdev = port->netdev; | |
4328 | int i, err; | |
4329 | ||
4330 | /* Closes the existing 'rxq's. */ | |
4331 | for (i = 0; i < port->n_rxq; i++) { | |
4332 | netdev_rxq_close(port->rxqs[i].rx); | |
4333 | port->rxqs[i].rx = NULL; | |
4334 | } | |
4335 | unsigned last_nrxq = port->n_rxq; | |
4336 | port->n_rxq = 0; | |
4337 | ||
4338 | /* Allows 'netdev' to apply the pending configuration changes. */ | |
4339 | if (netdev_is_reconf_required(netdev) || port->need_reconfigure) { | |
4340 | err = netdev_reconfigure(netdev); | |
4341 | if (err && (err != EOPNOTSUPP)) { | |
4342 | VLOG_ERR("Failed to set interface %s new configuration", | |
4343 | netdev_get_name(netdev)); | |
4344 | return err; | |
4345 | } | |
4346 | } | |
4347 | /* If the netdev_reconfigure() above succeeds, reopens the 'rxq's. */ | |
4348 | port->rxqs = xrealloc(port->rxqs, | |
4349 | sizeof *port->rxqs * netdev_n_rxq(netdev)); | |
4350 | /* Realloc 'used' counters for tx queues. */ | |
4351 | free(port->txq_used); | |
4352 | port->txq_used = xcalloc(netdev_n_txq(netdev), sizeof *port->txq_used); | |
4353 | ||
4354 | for (i = 0; i < netdev_n_rxq(netdev); i++) { | |
4355 | bool new_queue = i >= last_nrxq; | |
4356 | if (new_queue) { | |
4357 | memset(&port->rxqs[i], 0, sizeof port->rxqs[i]); | |
4358 | } | |
4359 | ||
4360 | port->rxqs[i].port = port; | |
4361 | port->rxqs[i].is_vhost = !strncmp(port->type, "dpdkvhost", 9); | |
4362 | ||
4363 | err = netdev_rxq_open(netdev, &port->rxqs[i].rx, i); | |
4364 | if (err) { | |
4365 | return err; | |
4366 | } | |
4367 | port->n_rxq++; | |
4368 | } | |
4369 | ||
4370 | /* Parse affinity list to apply configuration for new queues. */ | |
4371 | dpif_netdev_port_set_rxq_affinity(port, port->rxq_affinity_list); | |
4372 | ||
4373 | /* If reconfiguration was successful mark it as such, so we can use it */ | |
4374 | port->need_reconfigure = false; | |
4375 | ||
4376 | return 0; | |
4377 | } | |
4378 | ||
4379 | struct rr_numa_list { | |
4380 | struct hmap numas; /* Contains 'struct rr_numa' */ | |
4381 | }; | |
4382 | ||
4383 | struct rr_numa { | |
4384 | struct hmap_node node; | |
4385 | ||
4386 | int numa_id; | |
4387 | ||
4388 | /* Non isolated pmds on numa node 'numa_id' */ | |
4389 | struct dp_netdev_pmd_thread **pmds; | |
4390 | int n_pmds; | |
4391 | ||
4392 | int cur_index; | |
4393 | bool idx_inc; | |
4394 | }; | |
4395 | ||
4396 | static struct rr_numa * | |
4397 | rr_numa_list_lookup(struct rr_numa_list *rr, int numa_id) | |
4398 | { | |
4399 | struct rr_numa *numa; | |
4400 | ||
4401 | HMAP_FOR_EACH_WITH_HASH (numa, node, hash_int(numa_id, 0), &rr->numas) { | |
4402 | if (numa->numa_id == numa_id) { | |
4403 | return numa; | |
4404 | } | |
4405 | } | |
4406 | ||
4407 | return NULL; | |
4408 | } | |
4409 | ||
4410 | /* Returns the next node in numa list following 'numa' in round-robin fashion. | |
4411 | * Returns first node if 'numa' is a null pointer or the last node in 'rr'. | |
4412 | * Returns NULL if 'rr' numa list is empty. */ | |
4413 | static struct rr_numa * | |
4414 | rr_numa_list_next(struct rr_numa_list *rr, const struct rr_numa *numa) | |
4415 | { | |
4416 | struct hmap_node *node = NULL; | |
4417 | ||
4418 | if (numa) { | |
4419 | node = hmap_next(&rr->numas, &numa->node); | |
4420 | } | |
4421 | if (!node) { | |
4422 | node = hmap_first(&rr->numas); | |
4423 | } | |
4424 | ||
4425 | return (node) ? CONTAINER_OF(node, struct rr_numa, node) : NULL; | |
4426 | } | |
4427 | ||
4428 | static void | |
4429 | rr_numa_list_populate(struct dp_netdev *dp, struct rr_numa_list *rr) | |
4430 | { | |
4431 | struct dp_netdev_pmd_thread *pmd; | |
4432 | struct rr_numa *numa; | |
4433 | ||
4434 | hmap_init(&rr->numas); | |
4435 | ||
4436 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
4437 | if (pmd->core_id == NON_PMD_CORE_ID || pmd->isolated) { | |
4438 | continue; | |
4439 | } | |
4440 | ||
4441 | numa = rr_numa_list_lookup(rr, pmd->numa_id); | |
4442 | if (!numa) { | |
4443 | numa = xzalloc(sizeof *numa); | |
4444 | numa->numa_id = pmd->numa_id; | |
4445 | hmap_insert(&rr->numas, &numa->node, hash_int(pmd->numa_id, 0)); | |
4446 | } | |
4447 | numa->n_pmds++; | |
4448 | numa->pmds = xrealloc(numa->pmds, numa->n_pmds * sizeof *numa->pmds); | |
4449 | numa->pmds[numa->n_pmds - 1] = pmd; | |
4450 | /* At least one pmd so initialise curr_idx and idx_inc. */ | |
4451 | numa->cur_index = 0; | |
4452 | numa->idx_inc = true; | |
4453 | } | |
4454 | } | |
4455 | ||
4456 | /* | |
4457 | * Returns the next pmd from the numa node. | |
4458 | * | |
4459 | * If 'updown' is 'true' it will alternate between selecting the next pmd in | |
4460 | * either an up or down walk, switching between up/down when the first or last | |
4461 | * core is reached. e.g. 1,2,3,3,2,1,1,2... | |
4462 | * | |
4463 | * If 'updown' is 'false' it will select the next pmd wrapping around when last | |
4464 | * core reached. e.g. 1,2,3,1,2,3,1,2... | |
4465 | */ | |
4466 | static struct dp_netdev_pmd_thread * | |
4467 | rr_numa_get_pmd(struct rr_numa *numa, bool updown) | |
4468 | { | |
4469 | int numa_idx = numa->cur_index; | |
4470 | ||
4471 | if (numa->idx_inc == true) { | |
4472 | /* Incrementing through list of pmds. */ | |
4473 | if (numa->cur_index == numa->n_pmds-1) { | |
4474 | /* Reached the last pmd. */ | |
4475 | if (updown) { | |
4476 | numa->idx_inc = false; | |
4477 | } else { | |
4478 | numa->cur_index = 0; | |
4479 | } | |
4480 | } else { | |
4481 | numa->cur_index++; | |
4482 | } | |
4483 | } else { | |
4484 | /* Decrementing through list of pmds. */ | |
4485 | if (numa->cur_index == 0) { | |
4486 | /* Reached the first pmd. */ | |
4487 | numa->idx_inc = true; | |
4488 | } else { | |
4489 | numa->cur_index--; | |
4490 | } | |
4491 | } | |
4492 | return numa->pmds[numa_idx]; | |
4493 | } | |
4494 | ||
4495 | static void | |
4496 | rr_numa_list_destroy(struct rr_numa_list *rr) | |
4497 | { | |
4498 | struct rr_numa *numa; | |
4499 | ||
4500 | HMAP_FOR_EACH_POP (numa, node, &rr->numas) { | |
4501 | free(numa->pmds); | |
4502 | free(numa); | |
4503 | } | |
4504 | hmap_destroy(&rr->numas); | |
4505 | } | |
4506 | ||
4507 | /* Sort Rx Queues by the processing cycles they are consuming. */ | |
4508 | static int | |
4509 | compare_rxq_cycles(const void *a, const void *b) | |
4510 | { | |
4511 | struct dp_netdev_rxq *qa; | |
4512 | struct dp_netdev_rxq *qb; | |
4513 | uint64_t cycles_qa, cycles_qb; | |
4514 | ||
4515 | qa = *(struct dp_netdev_rxq **) a; | |
4516 | qb = *(struct dp_netdev_rxq **) b; | |
4517 | ||
4518 | cycles_qa = dp_netdev_rxq_get_cycles(qa, RXQ_CYCLES_PROC_HIST); | |
4519 | cycles_qb = dp_netdev_rxq_get_cycles(qb, RXQ_CYCLES_PROC_HIST); | |
4520 | ||
4521 | if (cycles_qa != cycles_qb) { | |
4522 | return (cycles_qa < cycles_qb) ? 1 : -1; | |
4523 | } else { | |
4524 | /* Cycles are the same so tiebreak on port/queue id. | |
4525 | * Tiebreaking (as opposed to return 0) ensures consistent | |
4526 | * sort results across multiple OS's. */ | |
4527 | uint32_t port_qa = odp_to_u32(qa->port->port_no); | |
4528 | uint32_t port_qb = odp_to_u32(qb->port->port_no); | |
4529 | if (port_qa != port_qb) { | |
4530 | return port_qa > port_qb ? 1 : -1; | |
4531 | } else { | |
4532 | return netdev_rxq_get_queue_id(qa->rx) | |
4533 | - netdev_rxq_get_queue_id(qb->rx); | |
4534 | } | |
4535 | } | |
4536 | } | |
4537 | ||
4538 | /* Assign pmds to queues. If 'pinned' is true, assign pmds to pinned | |
4539 | * queues and marks the pmds as isolated. Otherwise, assign non isolated | |
4540 | * pmds to unpinned queues. | |
4541 | * | |
4542 | * The function doesn't touch the pmd threads, it just stores the assignment | |
4543 | * in the 'pmd' member of each rxq. */ | |
4544 | static void | |
4545 | rxq_scheduling(struct dp_netdev *dp, bool pinned) OVS_REQUIRES(dp->port_mutex) | |
4546 | { | |
4547 | struct dp_netdev_port *port; | |
4548 | struct rr_numa_list rr; | |
4549 | struct rr_numa *non_local_numa = NULL; | |
4550 | struct dp_netdev_rxq ** rxqs = NULL; | |
4551 | int n_rxqs = 0; | |
4552 | struct rr_numa *numa = NULL; | |
4553 | int numa_id; | |
4554 | bool assign_cyc = dp->pmd_rxq_assign_cyc; | |
4555 | ||
4556 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
4557 | if (!netdev_is_pmd(port->netdev)) { | |
4558 | continue; | |
4559 | } | |
4560 | ||
4561 | for (int qid = 0; qid < port->n_rxq; qid++) { | |
4562 | struct dp_netdev_rxq *q = &port->rxqs[qid]; | |
4563 | ||
4564 | if (pinned && q->core_id != OVS_CORE_UNSPEC) { | |
4565 | struct dp_netdev_pmd_thread *pmd; | |
4566 | ||
4567 | pmd = dp_netdev_get_pmd(dp, q->core_id); | |
4568 | if (!pmd) { | |
4569 | VLOG_WARN("There is no PMD thread on core %d. Queue " | |
4570 | "%d on port \'%s\' will not be polled.", | |
4571 | q->core_id, qid, netdev_get_name(port->netdev)); | |
4572 | } else { | |
4573 | q->pmd = pmd; | |
4574 | pmd->isolated = true; | |
4575 | VLOG_INFO("Core %d on numa node %d assigned port \'%s\' " | |
4576 | "rx queue %d.", pmd->core_id, pmd->numa_id, | |
4577 | netdev_rxq_get_name(q->rx), | |
4578 | netdev_rxq_get_queue_id(q->rx)); | |
4579 | dp_netdev_pmd_unref(pmd); | |
4580 | } | |
4581 | } else if (!pinned && q->core_id == OVS_CORE_UNSPEC) { | |
4582 | uint64_t cycle_hist = 0; | |
4583 | ||
4584 | if (n_rxqs == 0) { | |
4585 | rxqs = xmalloc(sizeof *rxqs); | |
4586 | } else { | |
4587 | rxqs = xrealloc(rxqs, sizeof *rxqs * (n_rxqs + 1)); | |
4588 | } | |
4589 | ||
4590 | if (assign_cyc) { | |
4591 | /* Sum the queue intervals and store the cycle history. */ | |
4592 | for (unsigned i = 0; i < PMD_RXQ_INTERVAL_MAX; i++) { | |
4593 | cycle_hist += dp_netdev_rxq_get_intrvl_cycles(q, i); | |
4594 | } | |
4595 | dp_netdev_rxq_set_cycles(q, RXQ_CYCLES_PROC_HIST, | |
4596 | cycle_hist); | |
4597 | } | |
4598 | /* Store the queue. */ | |
4599 | rxqs[n_rxqs++] = q; | |
4600 | } | |
4601 | } | |
4602 | } | |
4603 | ||
4604 | if (n_rxqs > 1 && assign_cyc) { | |
4605 | /* Sort the queues in order of the processing cycles | |
4606 | * they consumed during their last pmd interval. */ | |
4607 | qsort(rxqs, n_rxqs, sizeof *rxqs, compare_rxq_cycles); | |
4608 | } | |
4609 | ||
4610 | rr_numa_list_populate(dp, &rr); | |
4611 | /* Assign the sorted queues to pmds in round robin. */ | |
4612 | for (int i = 0; i < n_rxqs; i++) { | |
4613 | numa_id = netdev_get_numa_id(rxqs[i]->port->netdev); | |
4614 | numa = rr_numa_list_lookup(&rr, numa_id); | |
4615 | if (!numa) { | |
4616 | /* There are no pmds on the queue's local NUMA node. | |
4617 | Round robin on the NUMA nodes that do have pmds. */ | |
4618 | non_local_numa = rr_numa_list_next(&rr, non_local_numa); | |
4619 | if (!non_local_numa) { | |
4620 | VLOG_ERR("There is no available (non-isolated) pmd " | |
4621 | "thread for port \'%s\' queue %d. This queue " | |
4622 | "will not be polled. Is pmd-cpu-mask set to " | |
4623 | "zero? Or are all PMDs isolated to other " | |
4624 | "queues?", netdev_rxq_get_name(rxqs[i]->rx), | |
4625 | netdev_rxq_get_queue_id(rxqs[i]->rx)); | |
4626 | continue; | |
4627 | } | |
4628 | rxqs[i]->pmd = rr_numa_get_pmd(non_local_numa, assign_cyc); | |
4629 | VLOG_WARN("There's no available (non-isolated) pmd thread " | |
4630 | "on numa node %d. Queue %d on port \'%s\' will " | |
4631 | "be assigned to the pmd on core %d " | |
4632 | "(numa node %d). Expect reduced performance.", | |
4633 | numa_id, netdev_rxq_get_queue_id(rxqs[i]->rx), | |
4634 | netdev_rxq_get_name(rxqs[i]->rx), | |
4635 | rxqs[i]->pmd->core_id, rxqs[i]->pmd->numa_id); | |
4636 | } else { | |
4637 | rxqs[i]->pmd = rr_numa_get_pmd(numa, assign_cyc); | |
4638 | if (assign_cyc) { | |
4639 | VLOG_INFO("Core %d on numa node %d assigned port \'%s\' " | |
4640 | "rx queue %d " | |
4641 | "(measured processing cycles %"PRIu64").", | |
4642 | rxqs[i]->pmd->core_id, numa_id, | |
4643 | netdev_rxq_get_name(rxqs[i]->rx), | |
4644 | netdev_rxq_get_queue_id(rxqs[i]->rx), | |
4645 | dp_netdev_rxq_get_cycles(rxqs[i], | |
4646 | RXQ_CYCLES_PROC_HIST)); | |
4647 | } else { | |
4648 | VLOG_INFO("Core %d on numa node %d assigned port \'%s\' " | |
4649 | "rx queue %d.", rxqs[i]->pmd->core_id, numa_id, | |
4650 | netdev_rxq_get_name(rxqs[i]->rx), | |
4651 | netdev_rxq_get_queue_id(rxqs[i]->rx)); | |
4652 | } | |
4653 | } | |
4654 | } | |
4655 | ||
4656 | rr_numa_list_destroy(&rr); | |
4657 | free(rxqs); | |
4658 | } | |
4659 | ||
4660 | static void | |
4661 | reload_affected_pmds(struct dp_netdev *dp) | |
4662 | { | |
4663 | struct dp_netdev_pmd_thread *pmd; | |
4664 | ||
4665 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
4666 | if (pmd->need_reload) { | |
4667 | flow_mark_flush(pmd); | |
4668 | dp_netdev_reload_pmd__(pmd); | |
4669 | } | |
4670 | } | |
4671 | ||
4672 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
4673 | if (pmd->need_reload) { | |
4674 | if (pmd->core_id != NON_PMD_CORE_ID) { | |
4675 | bool reload; | |
4676 | ||
4677 | do { | |
4678 | atomic_read_explicit(&pmd->reload, &reload, | |
4679 | memory_order_acquire); | |
4680 | } while (reload); | |
4681 | } | |
4682 | pmd->need_reload = false; | |
4683 | } | |
4684 | } | |
4685 | } | |
4686 | ||
4687 | static void | |
4688 | reconfigure_pmd_threads(struct dp_netdev *dp) | |
4689 | OVS_REQUIRES(dp->port_mutex) | |
4690 | { | |
4691 | struct dp_netdev_pmd_thread *pmd; | |
4692 | struct ovs_numa_dump *pmd_cores; | |
4693 | struct ovs_numa_info_core *core; | |
4694 | struct hmapx to_delete = HMAPX_INITIALIZER(&to_delete); | |
4695 | struct hmapx_node *node; | |
4696 | bool changed = false; | |
4697 | bool need_to_adjust_static_tx_qids = false; | |
4698 | ||
4699 | /* The pmd threads should be started only if there's a pmd port in the | |
4700 | * datapath. If the user didn't provide any "pmd-cpu-mask", we start | |
4701 | * NR_PMD_THREADS per numa node. */ | |
4702 | if (!has_pmd_port(dp)) { | |
4703 | pmd_cores = ovs_numa_dump_n_cores_per_numa(0); | |
4704 | } else if (dp->pmd_cmask && dp->pmd_cmask[0]) { | |
4705 | pmd_cores = ovs_numa_dump_cores_with_cmask(dp->pmd_cmask); | |
4706 | } else { | |
4707 | pmd_cores = ovs_numa_dump_n_cores_per_numa(NR_PMD_THREADS); | |
4708 | } | |
4709 | ||
4710 | /* We need to adjust 'static_tx_qid's only if we're reducing number of | |
4711 | * PMD threads. Otherwise, new threads will allocate all the freed ids. */ | |
4712 | if (ovs_numa_dump_count(pmd_cores) < cmap_count(&dp->poll_threads) - 1) { | |
4713 | /* Adjustment is required to keep 'static_tx_qid's sequential and | |
4714 | * avoid possible issues, for example, imbalanced tx queue usage | |
4715 | * and unnecessary locking caused by remapping on netdev level. */ | |
4716 | need_to_adjust_static_tx_qids = true; | |
4717 | } | |
4718 | ||
4719 | /* Check for unwanted pmd threads */ | |
4720 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
4721 | if (pmd->core_id == NON_PMD_CORE_ID) { | |
4722 | continue; | |
4723 | } | |
4724 | if (!ovs_numa_dump_contains_core(pmd_cores, pmd->numa_id, | |
4725 | pmd->core_id)) { | |
4726 | hmapx_add(&to_delete, pmd); | |
4727 | } else if (need_to_adjust_static_tx_qids) { | |
4728 | atomic_store_relaxed(&pmd->reload_tx_qid, true); | |
4729 | pmd->need_reload = true; | |
4730 | } | |
4731 | } | |
4732 | ||
4733 | HMAPX_FOR_EACH (node, &to_delete) { | |
4734 | pmd = (struct dp_netdev_pmd_thread *) node->data; | |
4735 | VLOG_INFO("PMD thread on numa_id: %d, core id: %2d destroyed.", | |
4736 | pmd->numa_id, pmd->core_id); | |
4737 | dp_netdev_del_pmd(dp, pmd); | |
4738 | } | |
4739 | changed = !hmapx_is_empty(&to_delete); | |
4740 | hmapx_destroy(&to_delete); | |
4741 | ||
4742 | if (need_to_adjust_static_tx_qids) { | |
4743 | /* 'static_tx_qid's are not sequential now. | |
4744 | * Reload remaining threads to fix this. */ | |
4745 | reload_affected_pmds(dp); | |
4746 | } | |
4747 | ||
4748 | /* Check for required new pmd threads */ | |
4749 | FOR_EACH_CORE_ON_DUMP(core, pmd_cores) { | |
4750 | pmd = dp_netdev_get_pmd(dp, core->core_id); | |
4751 | if (!pmd) { | |
4752 | struct ds name = DS_EMPTY_INITIALIZER; | |
4753 | ||
4754 | pmd = xzalloc(sizeof *pmd); | |
4755 | dp_netdev_configure_pmd(pmd, dp, core->core_id, core->numa_id); | |
4756 | ||
4757 | ds_put_format(&name, "pmd-c%02d/id:", core->core_id); | |
4758 | pmd->thread = ovs_thread_create(ds_cstr(&name), | |
4759 | pmd_thread_main, pmd); | |
4760 | ds_destroy(&name); | |
4761 | ||
4762 | VLOG_INFO("PMD thread on numa_id: %d, core id: %2d created.", | |
4763 | pmd->numa_id, pmd->core_id); | |
4764 | changed = true; | |
4765 | } else { | |
4766 | dp_netdev_pmd_unref(pmd); | |
4767 | } | |
4768 | } | |
4769 | ||
4770 | if (changed) { | |
4771 | struct ovs_numa_info_numa *numa; | |
4772 | ||
4773 | /* Log the number of pmd threads per numa node. */ | |
4774 | FOR_EACH_NUMA_ON_DUMP (numa, pmd_cores) { | |
4775 | VLOG_INFO("There are %"PRIuSIZE" pmd threads on numa node %d", | |
4776 | numa->n_cores, numa->numa_id); | |
4777 | } | |
4778 | } | |
4779 | ||
4780 | ovs_numa_dump_destroy(pmd_cores); | |
4781 | } | |
4782 | ||
4783 | static void | |
4784 | pmd_remove_stale_ports(struct dp_netdev *dp, | |
4785 | struct dp_netdev_pmd_thread *pmd) | |
4786 | OVS_EXCLUDED(pmd->port_mutex) | |
4787 | OVS_REQUIRES(dp->port_mutex) | |
4788 | { | |
4789 | struct rxq_poll *poll, *poll_next; | |
4790 | struct tx_port *tx, *tx_next; | |
4791 | ||
4792 | ovs_mutex_lock(&pmd->port_mutex); | |
4793 | HMAP_FOR_EACH_SAFE (poll, poll_next, node, &pmd->poll_list) { | |
4794 | struct dp_netdev_port *port = poll->rxq->port; | |
4795 | ||
4796 | if (port->need_reconfigure | |
4797 | || !hmap_contains(&dp->ports, &port->node)) { | |
4798 | dp_netdev_del_rxq_from_pmd(pmd, poll); | |
4799 | } | |
4800 | } | |
4801 | HMAP_FOR_EACH_SAFE (tx, tx_next, node, &pmd->tx_ports) { | |
4802 | struct dp_netdev_port *port = tx->port; | |
4803 | ||
4804 | if (port->need_reconfigure | |
4805 | || !hmap_contains(&dp->ports, &port->node)) { | |
4806 | dp_netdev_del_port_tx_from_pmd(pmd, tx); | |
4807 | } | |
4808 | } | |
4809 | ovs_mutex_unlock(&pmd->port_mutex); | |
4810 | } | |
4811 | ||
4812 | /* Must be called each time a port is added/removed or the cmask changes. | |
4813 | * This creates and destroys pmd threads, reconfigures ports, opens their | |
4814 | * rxqs and assigns all rxqs/txqs to pmd threads. */ | |
4815 | static void | |
4816 | reconfigure_datapath(struct dp_netdev *dp) | |
4817 | OVS_REQUIRES(dp->port_mutex) | |
4818 | { | |
4819 | struct hmapx busy_threads = HMAPX_INITIALIZER(&busy_threads); | |
4820 | struct dp_netdev_pmd_thread *pmd; | |
4821 | struct dp_netdev_port *port; | |
4822 | int wanted_txqs; | |
4823 | ||
4824 | dp->last_reconfigure_seq = seq_read(dp->reconfigure_seq); | |
4825 | ||
4826 | /* Step 1: Adjust the pmd threads based on the datapath ports, the cores | |
4827 | * on the system and the user configuration. */ | |
4828 | reconfigure_pmd_threads(dp); | |
4829 | ||
4830 | wanted_txqs = cmap_count(&dp->poll_threads); | |
4831 | ||
4832 | /* The number of pmd threads might have changed, or a port can be new: | |
4833 | * adjust the txqs. */ | |
4834 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
4835 | netdev_set_tx_multiq(port->netdev, wanted_txqs); | |
4836 | } | |
4837 | ||
4838 | /* Step 2: Remove from the pmd threads ports that have been removed or | |
4839 | * need reconfiguration. */ | |
4840 | ||
4841 | /* Check for all the ports that need reconfiguration. We cache this in | |
4842 | * 'port->need_reconfigure', because netdev_is_reconf_required() can | |
4843 | * change at any time. */ | |
4844 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
4845 | if (netdev_is_reconf_required(port->netdev)) { | |
4846 | port->need_reconfigure = true; | |
4847 | } | |
4848 | } | |
4849 | ||
4850 | /* Remove from the pmd threads all the ports that have been deleted or | |
4851 | * need reconfiguration. */ | |
4852 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
4853 | pmd_remove_stale_ports(dp, pmd); | |
4854 | } | |
4855 | ||
4856 | /* Reload affected pmd threads. We must wait for the pmd threads before | |
4857 | * reconfiguring the ports, because a port cannot be reconfigured while | |
4858 | * it's being used. */ | |
4859 | reload_affected_pmds(dp); | |
4860 | ||
4861 | /* Step 3: Reconfigure ports. */ | |
4862 | ||
4863 | /* We only reconfigure the ports that we determined above, because they're | |
4864 | * not being used by any pmd thread at the moment. If a port fails to | |
4865 | * reconfigure we remove it from the datapath. */ | |
4866 | struct dp_netdev_port *next_port; | |
4867 | HMAP_FOR_EACH_SAFE (port, next_port, node, &dp->ports) { | |
4868 | int err; | |
4869 | ||
4870 | if (!port->need_reconfigure) { | |
4871 | continue; | |
4872 | } | |
4873 | ||
4874 | err = port_reconfigure(port); | |
4875 | if (err) { | |
4876 | hmap_remove(&dp->ports, &port->node); | |
4877 | seq_change(dp->port_seq); | |
4878 | port_destroy(port); | |
4879 | } else { | |
4880 | port->dynamic_txqs = netdev_n_txq(port->netdev) < wanted_txqs; | |
4881 | } | |
4882 | } | |
4883 | ||
4884 | /* Step 4: Compute new rxq scheduling. We don't touch the pmd threads | |
4885 | * for now, we just update the 'pmd' pointer in each rxq to point to the | |
4886 | * wanted thread according to the scheduling policy. */ | |
4887 | ||
4888 | /* Reset all the pmd threads to non isolated. */ | |
4889 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
4890 | pmd->isolated = false; | |
4891 | } | |
4892 | ||
4893 | /* Reset all the queues to unassigned */ | |
4894 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
4895 | for (int i = 0; i < port->n_rxq; i++) { | |
4896 | port->rxqs[i].pmd = NULL; | |
4897 | } | |
4898 | } | |
4899 | ||
4900 | /* Add pinned queues and mark pmd threads isolated. */ | |
4901 | rxq_scheduling(dp, true); | |
4902 | ||
4903 | /* Add non-pinned queues. */ | |
4904 | rxq_scheduling(dp, false); | |
4905 | ||
4906 | /* Step 5: Remove queues not compliant with new scheduling. */ | |
4907 | ||
4908 | /* Count all the threads that will have at least one queue to poll. */ | |
4909 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
4910 | for (int qid = 0; qid < port->n_rxq; qid++) { | |
4911 | struct dp_netdev_rxq *q = &port->rxqs[qid]; | |
4912 | ||
4913 | if (q->pmd) { | |
4914 | hmapx_add(&busy_threads, q->pmd); | |
4915 | } | |
4916 | } | |
4917 | } | |
4918 | ||
4919 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
4920 | struct rxq_poll *poll, *poll_next; | |
4921 | ||
4922 | ovs_mutex_lock(&pmd->port_mutex); | |
4923 | HMAP_FOR_EACH_SAFE (poll, poll_next, node, &pmd->poll_list) { | |
4924 | if (poll->rxq->pmd != pmd) { | |
4925 | dp_netdev_del_rxq_from_pmd(pmd, poll); | |
4926 | ||
4927 | /* This pmd might sleep after this step if it has no rxq | |
4928 | * remaining. Tell it to busy wait for new assignment if it | |
4929 | * has at least one scheduled queue. */ | |
4930 | if (hmap_count(&pmd->poll_list) == 0 && | |
4931 | hmapx_contains(&busy_threads, pmd)) { | |
4932 | atomic_store_relaxed(&pmd->wait_for_reload, true); | |
4933 | } | |
4934 | } | |
4935 | } | |
4936 | ovs_mutex_unlock(&pmd->port_mutex); | |
4937 | } | |
4938 | ||
4939 | hmapx_destroy(&busy_threads); | |
4940 | ||
4941 | /* Reload affected pmd threads. We must wait for the pmd threads to remove | |
4942 | * the old queues before readding them, otherwise a queue can be polled by | |
4943 | * two threads at the same time. */ | |
4944 | reload_affected_pmds(dp); | |
4945 | ||
4946 | /* Step 6: Add queues from scheduling, if they're not there already. */ | |
4947 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
4948 | if (!netdev_is_pmd(port->netdev)) { | |
4949 | continue; | |
4950 | } | |
4951 | ||
4952 | for (int qid = 0; qid < port->n_rxq; qid++) { | |
4953 | struct dp_netdev_rxq *q = &port->rxqs[qid]; | |
4954 | ||
4955 | if (q->pmd) { | |
4956 | ovs_mutex_lock(&q->pmd->port_mutex); | |
4957 | dp_netdev_add_rxq_to_pmd(q->pmd, q); | |
4958 | ovs_mutex_unlock(&q->pmd->port_mutex); | |
4959 | } | |
4960 | } | |
4961 | } | |
4962 | ||
4963 | /* Add every port to the tx cache of every pmd thread, if it's not | |
4964 | * there already and if this pmd has at least one rxq to poll. */ | |
4965 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
4966 | ovs_mutex_lock(&pmd->port_mutex); | |
4967 | if (hmap_count(&pmd->poll_list) || pmd->core_id == NON_PMD_CORE_ID) { | |
4968 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
4969 | dp_netdev_add_port_tx_to_pmd(pmd, port); | |
4970 | } | |
4971 | } | |
4972 | ovs_mutex_unlock(&pmd->port_mutex); | |
4973 | } | |
4974 | ||
4975 | /* Reload affected pmd threads. */ | |
4976 | reload_affected_pmds(dp); | |
4977 | ||
4978 | /* Check if PMD Auto LB is to be enabled */ | |
4979 | set_pmd_auto_lb(dp); | |
4980 | } | |
4981 | ||
4982 | /* Returns true if one of the netdevs in 'dp' requires a reconfiguration */ | |
4983 | static bool | |
4984 | ports_require_restart(const struct dp_netdev *dp) | |
4985 | OVS_REQUIRES(dp->port_mutex) | |
4986 | { | |
4987 | struct dp_netdev_port *port; | |
4988 | ||
4989 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
4990 | if (netdev_is_reconf_required(port->netdev)) { | |
4991 | return true; | |
4992 | } | |
4993 | } | |
4994 | ||
4995 | return false; | |
4996 | } | |
4997 | ||
4998 | /* Calculates variance in the values stored in array 'a'. 'n' is the number | |
4999 | * of elements in array to be considered for calculating vairance. | |
5000 | * Usage example: data array 'a' contains the processing load of each pmd and | |
5001 | * 'n' is the number of PMDs. It returns the variance in processing load of | |
5002 | * PMDs*/ | |
5003 | static uint64_t | |
5004 | variance(uint64_t a[], int n) | |
5005 | { | |
5006 | /* Compute mean (average of elements). */ | |
5007 | uint64_t sum = 0; | |
5008 | uint64_t mean = 0; | |
5009 | uint64_t sqDiff = 0; | |
5010 | ||
5011 | if (!n) { | |
5012 | return 0; | |
5013 | } | |
5014 | ||
5015 | for (int i = 0; i < n; i++) { | |
5016 | sum += a[i]; | |
5017 | } | |
5018 | ||
5019 | if (sum) { | |
5020 | mean = sum / n; | |
5021 | ||
5022 | /* Compute sum squared differences with mean. */ | |
5023 | for (int i = 0; i < n; i++) { | |
5024 | sqDiff += (a[i] - mean)*(a[i] - mean); | |
5025 | } | |
5026 | } | |
5027 | return (sqDiff ? (sqDiff / n) : 0); | |
5028 | } | |
5029 | ||
5030 | ||
5031 | /* Returns the variance in the PMDs usage as part of dry run of rxqs | |
5032 | * assignment to PMDs. */ | |
5033 | static bool | |
5034 | get_dry_run_variance(struct dp_netdev *dp, uint32_t *core_list, | |
5035 | uint32_t num_pmds, uint64_t *predicted_variance) | |
5036 | OVS_REQUIRES(dp->port_mutex) | |
5037 | { | |
5038 | struct dp_netdev_port *port; | |
5039 | struct dp_netdev_pmd_thread *pmd; | |
5040 | struct dp_netdev_rxq **rxqs = NULL; | |
5041 | struct rr_numa *numa = NULL; | |
5042 | struct rr_numa_list rr; | |
5043 | int n_rxqs = 0; | |
5044 | bool ret = false; | |
5045 | uint64_t *pmd_usage; | |
5046 | ||
5047 | if (!predicted_variance) { | |
5048 | return ret; | |
5049 | } | |
5050 | ||
5051 | pmd_usage = xcalloc(num_pmds, sizeof(uint64_t)); | |
5052 | ||
5053 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
5054 | if (!netdev_is_pmd(port->netdev)) { | |
5055 | continue; | |
5056 | } | |
5057 | ||
5058 | for (int qid = 0; qid < port->n_rxq; qid++) { | |
5059 | struct dp_netdev_rxq *q = &port->rxqs[qid]; | |
5060 | uint64_t cycle_hist = 0; | |
5061 | ||
5062 | if (q->pmd->isolated) { | |
5063 | continue; | |
5064 | } | |
5065 | ||
5066 | if (n_rxqs == 0) { | |
5067 | rxqs = xmalloc(sizeof *rxqs); | |
5068 | } else { | |
5069 | rxqs = xrealloc(rxqs, sizeof *rxqs * (n_rxqs + 1)); | |
5070 | } | |
5071 | ||
5072 | /* Sum the queue intervals and store the cycle history. */ | |
5073 | for (unsigned i = 0; i < PMD_RXQ_INTERVAL_MAX; i++) { | |
5074 | cycle_hist += dp_netdev_rxq_get_intrvl_cycles(q, i); | |
5075 | } | |
5076 | dp_netdev_rxq_set_cycles(q, RXQ_CYCLES_PROC_HIST, | |
5077 | cycle_hist); | |
5078 | /* Store the queue. */ | |
5079 | rxqs[n_rxqs++] = q; | |
5080 | } | |
5081 | } | |
5082 | if (n_rxqs > 1) { | |
5083 | /* Sort the queues in order of the processing cycles | |
5084 | * they consumed during their last pmd interval. */ | |
5085 | qsort(rxqs, n_rxqs, sizeof *rxqs, compare_rxq_cycles); | |
5086 | } | |
5087 | rr_numa_list_populate(dp, &rr); | |
5088 | ||
5089 | for (int i = 0; i < n_rxqs; i++) { | |
5090 | int numa_id = netdev_get_numa_id(rxqs[i]->port->netdev); | |
5091 | numa = rr_numa_list_lookup(&rr, numa_id); | |
5092 | if (!numa) { | |
5093 | /* Abort if cross NUMA polling. */ | |
5094 | VLOG_DBG("PMD auto lb dry run." | |
5095 | " Aborting due to cross-numa polling."); | |
5096 | goto cleanup; | |
5097 | } | |
5098 | ||
5099 | pmd = rr_numa_get_pmd(numa, true); | |
5100 | VLOG_DBG("PMD auto lb dry run. Predicted: Core %d on numa node %d " | |
5101 | "to be assigned port \'%s\' rx queue %d " | |
5102 | "(measured processing cycles %"PRIu64").", | |
5103 | pmd->core_id, numa_id, | |
5104 | netdev_rxq_get_name(rxqs[i]->rx), | |
5105 | netdev_rxq_get_queue_id(rxqs[i]->rx), | |
5106 | dp_netdev_rxq_get_cycles(rxqs[i], RXQ_CYCLES_PROC_HIST)); | |
5107 | ||
5108 | for (int id = 0; id < num_pmds; id++) { | |
5109 | if (pmd->core_id == core_list[id]) { | |
5110 | /* Add the processing cycles of rxq to pmd polling it. */ | |
5111 | pmd_usage[id] += dp_netdev_rxq_get_cycles(rxqs[i], | |
5112 | RXQ_CYCLES_PROC_HIST); | |
5113 | } | |
5114 | } | |
5115 | } | |
5116 | ||
5117 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
5118 | uint64_t total_cycles = 0; | |
5119 | ||
5120 | if ((pmd->core_id == NON_PMD_CORE_ID) || pmd->isolated) { | |
5121 | continue; | |
5122 | } | |
5123 | ||
5124 | /* Get the total pmd cycles for an interval. */ | |
5125 | atomic_read_relaxed(&pmd->intrvl_cycles, &total_cycles); | |
5126 | /* Estimate the cycles to cover all intervals. */ | |
5127 | total_cycles *= PMD_RXQ_INTERVAL_MAX; | |
5128 | for (int id = 0; id < num_pmds; id++) { | |
5129 | if (pmd->core_id == core_list[id]) { | |
5130 | if (pmd_usage[id]) { | |
5131 | pmd_usage[id] = (pmd_usage[id] * 100) / total_cycles; | |
5132 | } | |
5133 | VLOG_DBG("PMD auto lb dry run. Predicted: Core %d, " | |
5134 | "usage %"PRIu64"", pmd->core_id, pmd_usage[id]); | |
5135 | } | |
5136 | } | |
5137 | } | |
5138 | *predicted_variance = variance(pmd_usage, num_pmds); | |
5139 | ret = true; | |
5140 | ||
5141 | cleanup: | |
5142 | rr_numa_list_destroy(&rr); | |
5143 | free(rxqs); | |
5144 | free(pmd_usage); | |
5145 | return ret; | |
5146 | } | |
5147 | ||
5148 | /* Does the dry run of Rxq assignment to PMDs and returns true if it gives | |
5149 | * better distribution of load on PMDs. */ | |
5150 | static bool | |
5151 | pmd_rebalance_dry_run(struct dp_netdev *dp) | |
5152 | OVS_REQUIRES(dp->port_mutex) | |
5153 | { | |
5154 | struct dp_netdev_pmd_thread *pmd; | |
5155 | uint64_t *curr_pmd_usage; | |
5156 | ||
5157 | uint64_t curr_variance; | |
5158 | uint64_t new_variance; | |
5159 | uint64_t improvement = 0; | |
5160 | uint32_t num_pmds; | |
5161 | uint32_t *pmd_corelist; | |
5162 | struct rxq_poll *poll; | |
5163 | bool ret; | |
5164 | ||
5165 | num_pmds = cmap_count(&dp->poll_threads); | |
5166 | ||
5167 | if (num_pmds > 1) { | |
5168 | curr_pmd_usage = xcalloc(num_pmds, sizeof(uint64_t)); | |
5169 | pmd_corelist = xcalloc(num_pmds, sizeof(uint32_t)); | |
5170 | } else { | |
5171 | return false; | |
5172 | } | |
5173 | ||
5174 | num_pmds = 0; | |
5175 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
5176 | uint64_t total_cycles = 0; | |
5177 | uint64_t total_proc = 0; | |
5178 | ||
5179 | if ((pmd->core_id == NON_PMD_CORE_ID) || pmd->isolated) { | |
5180 | continue; | |
5181 | } | |
5182 | ||
5183 | /* Get the total pmd cycles for an interval. */ | |
5184 | atomic_read_relaxed(&pmd->intrvl_cycles, &total_cycles); | |
5185 | /* Estimate the cycles to cover all intervals. */ | |
5186 | total_cycles *= PMD_RXQ_INTERVAL_MAX; | |
5187 | ||
5188 | ovs_mutex_lock(&pmd->port_mutex); | |
5189 | HMAP_FOR_EACH (poll, node, &pmd->poll_list) { | |
5190 | for (unsigned i = 0; i < PMD_RXQ_INTERVAL_MAX; i++) { | |
5191 | total_proc += dp_netdev_rxq_get_intrvl_cycles(poll->rxq, i); | |
5192 | } | |
5193 | } | |
5194 | ovs_mutex_unlock(&pmd->port_mutex); | |
5195 | ||
5196 | if (total_proc) { | |
5197 | curr_pmd_usage[num_pmds] = (total_proc * 100) / total_cycles; | |
5198 | } | |
5199 | ||
5200 | VLOG_DBG("PMD auto lb dry run. Current: Core %d, usage %"PRIu64"", | |
5201 | pmd->core_id, curr_pmd_usage[num_pmds]); | |
5202 | ||
5203 | if (atomic_count_get(&pmd->pmd_overloaded)) { | |
5204 | atomic_count_set(&pmd->pmd_overloaded, 0); | |
5205 | } | |
5206 | ||
5207 | pmd_corelist[num_pmds] = pmd->core_id; | |
5208 | num_pmds++; | |
5209 | } | |
5210 | ||
5211 | curr_variance = variance(curr_pmd_usage, num_pmds); | |
5212 | ret = get_dry_run_variance(dp, pmd_corelist, num_pmds, &new_variance); | |
5213 | ||
5214 | if (ret) { | |
5215 | VLOG_DBG("PMD auto lb dry run. Current PMD variance: %"PRIu64"," | |
5216 | " Predicted PMD variance: %"PRIu64"", | |
5217 | curr_variance, new_variance); | |
5218 | ||
5219 | if (new_variance < curr_variance) { | |
5220 | improvement = | |
5221 | ((curr_variance - new_variance) * 100) / curr_variance; | |
5222 | } | |
5223 | if (improvement < ALB_ACCEPTABLE_IMPROVEMENT) { | |
5224 | ret = false; | |
5225 | } | |
5226 | } | |
5227 | ||
5228 | free(curr_pmd_usage); | |
5229 | free(pmd_corelist); | |
5230 | return ret; | |
5231 | } | |
5232 | ||
5233 | ||
5234 | /* Return true if needs to revalidate datapath flows. */ | |
5235 | static bool | |
5236 | dpif_netdev_run(struct dpif *dpif) | |
5237 | { | |
5238 | struct dp_netdev_port *port; | |
5239 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
5240 | struct dp_netdev_pmd_thread *non_pmd; | |
5241 | uint64_t new_tnl_seq; | |
5242 | bool need_to_flush = true; | |
5243 | bool pmd_rebalance = false; | |
5244 | long long int now = time_msec(); | |
5245 | struct dp_netdev_pmd_thread *pmd; | |
5246 | ||
5247 | ovs_mutex_lock(&dp->port_mutex); | |
5248 | non_pmd = dp_netdev_get_pmd(dp, NON_PMD_CORE_ID); | |
5249 | if (non_pmd) { | |
5250 | ovs_mutex_lock(&dp->non_pmd_mutex); | |
5251 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
5252 | if (!netdev_is_pmd(port->netdev)) { | |
5253 | int i; | |
5254 | ||
5255 | if (port->emc_enabled) { | |
5256 | atomic_read_relaxed(&dp->emc_insert_min, | |
5257 | &non_pmd->ctx.emc_insert_min); | |
5258 | } else { | |
5259 | non_pmd->ctx.emc_insert_min = 0; | |
5260 | } | |
5261 | ||
5262 | for (i = 0; i < port->n_rxq; i++) { | |
5263 | ||
5264 | if (!netdev_rxq_enabled(port->rxqs[i].rx)) { | |
5265 | continue; | |
5266 | } | |
5267 | ||
5268 | if (dp_netdev_process_rxq_port(non_pmd, | |
5269 | &port->rxqs[i], | |
5270 | port->port_no)) { | |
5271 | need_to_flush = false; | |
5272 | } | |
5273 | } | |
5274 | } | |
5275 | } | |
5276 | if (need_to_flush) { | |
5277 | /* We didn't receive anything in the process loop. | |
5278 | * Check if we need to send something. | |
5279 | * There was no time updates on current iteration. */ | |
5280 | pmd_thread_ctx_time_update(non_pmd); | |
5281 | dp_netdev_pmd_flush_output_packets(non_pmd, false); | |
5282 | } | |
5283 | ||
5284 | dpif_netdev_xps_revalidate_pmd(non_pmd, false); | |
5285 | ovs_mutex_unlock(&dp->non_pmd_mutex); | |
5286 | ||
5287 | dp_netdev_pmd_unref(non_pmd); | |
5288 | } | |
5289 | ||
5290 | struct pmd_auto_lb *pmd_alb = &dp->pmd_alb; | |
5291 | if (pmd_alb->is_enabled) { | |
5292 | if (!pmd_alb->rebalance_poll_timer) { | |
5293 | pmd_alb->rebalance_poll_timer = now; | |
5294 | } else if ((pmd_alb->rebalance_poll_timer + | |
5295 | pmd_alb->rebalance_intvl) < now) { | |
5296 | pmd_alb->rebalance_poll_timer = now; | |
5297 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
5298 | if (atomic_count_get(&pmd->pmd_overloaded) >= | |
5299 | PMD_RXQ_INTERVAL_MAX) { | |
5300 | pmd_rebalance = true; | |
5301 | break; | |
5302 | } | |
5303 | } | |
5304 | ||
5305 | if (pmd_rebalance && | |
5306 | !dp_netdev_is_reconf_required(dp) && | |
5307 | !ports_require_restart(dp) && | |
5308 | pmd_rebalance_dry_run(dp)) { | |
5309 | VLOG_INFO("PMD auto lb dry run." | |
5310 | " requesting datapath reconfigure."); | |
5311 | dp_netdev_request_reconfigure(dp); | |
5312 | } | |
5313 | } | |
5314 | } | |
5315 | ||
5316 | if (dp_netdev_is_reconf_required(dp) || ports_require_restart(dp)) { | |
5317 | reconfigure_datapath(dp); | |
5318 | } | |
5319 | ovs_mutex_unlock(&dp->port_mutex); | |
5320 | ||
5321 | tnl_neigh_cache_run(); | |
5322 | tnl_port_map_run(); | |
5323 | new_tnl_seq = seq_read(tnl_conf_seq); | |
5324 | ||
5325 | if (dp->last_tnl_conf_seq != new_tnl_seq) { | |
5326 | dp->last_tnl_conf_seq = new_tnl_seq; | |
5327 | return true; | |
5328 | } | |
5329 | return false; | |
5330 | } | |
5331 | ||
5332 | static void | |
5333 | dpif_netdev_wait(struct dpif *dpif) | |
5334 | { | |
5335 | struct dp_netdev_port *port; | |
5336 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
5337 | ||
5338 | ovs_mutex_lock(&dp_netdev_mutex); | |
5339 | ovs_mutex_lock(&dp->port_mutex); | |
5340 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
5341 | netdev_wait_reconf_required(port->netdev); | |
5342 | if (!netdev_is_pmd(port->netdev)) { | |
5343 | int i; | |
5344 | ||
5345 | for (i = 0; i < port->n_rxq; i++) { | |
5346 | netdev_rxq_wait(port->rxqs[i].rx); | |
5347 | } | |
5348 | } | |
5349 | } | |
5350 | ovs_mutex_unlock(&dp->port_mutex); | |
5351 | ovs_mutex_unlock(&dp_netdev_mutex); | |
5352 | seq_wait(tnl_conf_seq, dp->last_tnl_conf_seq); | |
5353 | } | |
5354 | ||
5355 | static void | |
5356 | pmd_free_cached_ports(struct dp_netdev_pmd_thread *pmd) | |
5357 | { | |
5358 | struct tx_port *tx_port_cached; | |
5359 | ||
5360 | /* Flush all the queued packets. */ | |
5361 | dp_netdev_pmd_flush_output_packets(pmd, true); | |
5362 | /* Free all used tx queue ids. */ | |
5363 | dpif_netdev_xps_revalidate_pmd(pmd, true); | |
5364 | ||
5365 | HMAP_FOR_EACH_POP (tx_port_cached, node, &pmd->tnl_port_cache) { | |
5366 | free(tx_port_cached); | |
5367 | } | |
5368 | HMAP_FOR_EACH_POP (tx_port_cached, node, &pmd->send_port_cache) { | |
5369 | free(tx_port_cached); | |
5370 | } | |
5371 | } | |
5372 | ||
5373 | /* Copies ports from 'pmd->tx_ports' (shared with the main thread) to | |
5374 | * thread-local copies. Copy to 'pmd->tnl_port_cache' if it is a tunnel | |
5375 | * device, otherwise to 'pmd->send_port_cache' if the port has at least | |
5376 | * one txq. */ | |
5377 | static void | |
5378 | pmd_load_cached_ports(struct dp_netdev_pmd_thread *pmd) | |
5379 | OVS_REQUIRES(pmd->port_mutex) | |
5380 | { | |
5381 | struct tx_port *tx_port, *tx_port_cached; | |
5382 | ||
5383 | pmd_free_cached_ports(pmd); | |
5384 | hmap_shrink(&pmd->send_port_cache); | |
5385 | hmap_shrink(&pmd->tnl_port_cache); | |
5386 | ||
5387 | HMAP_FOR_EACH (tx_port, node, &pmd->tx_ports) { | |
5388 | if (netdev_has_tunnel_push_pop(tx_port->port->netdev)) { | |
5389 | tx_port_cached = xmemdup(tx_port, sizeof *tx_port_cached); | |
5390 | hmap_insert(&pmd->tnl_port_cache, &tx_port_cached->node, | |
5391 | hash_port_no(tx_port_cached->port->port_no)); | |
5392 | } | |
5393 | ||
5394 | if (netdev_n_txq(tx_port->port->netdev)) { | |
5395 | tx_port_cached = xmemdup(tx_port, sizeof *tx_port_cached); | |
5396 | hmap_insert(&pmd->send_port_cache, &tx_port_cached->node, | |
5397 | hash_port_no(tx_port_cached->port->port_no)); | |
5398 | } | |
5399 | } | |
5400 | } | |
5401 | ||
5402 | static void | |
5403 | pmd_alloc_static_tx_qid(struct dp_netdev_pmd_thread *pmd) | |
5404 | { | |
5405 | ovs_mutex_lock(&pmd->dp->tx_qid_pool_mutex); | |
5406 | if (!id_pool_alloc_id(pmd->dp->tx_qid_pool, &pmd->static_tx_qid)) { | |
5407 | VLOG_ABORT("static_tx_qid allocation failed for PMD on core %2d" | |
5408 | ", numa_id %d.", pmd->core_id, pmd->numa_id); | |
5409 | } | |
5410 | ovs_mutex_unlock(&pmd->dp->tx_qid_pool_mutex); | |
5411 | ||
5412 | VLOG_DBG("static_tx_qid = %d allocated for PMD thread on core %2d" | |
5413 | ", numa_id %d.", pmd->static_tx_qid, pmd->core_id, pmd->numa_id); | |
5414 | } | |
5415 | ||
5416 | static void | |
5417 | pmd_free_static_tx_qid(struct dp_netdev_pmd_thread *pmd) | |
5418 | { | |
5419 | ovs_mutex_lock(&pmd->dp->tx_qid_pool_mutex); | |
5420 | id_pool_free_id(pmd->dp->tx_qid_pool, pmd->static_tx_qid); | |
5421 | ovs_mutex_unlock(&pmd->dp->tx_qid_pool_mutex); | |
5422 | } | |
5423 | ||
5424 | static int | |
5425 | pmd_load_queues_and_ports(struct dp_netdev_pmd_thread *pmd, | |
5426 | struct polled_queue **ppoll_list) | |
5427 | { | |
5428 | struct polled_queue *poll_list = *ppoll_list; | |
5429 | struct rxq_poll *poll; | |
5430 | int i; | |
5431 | ||
5432 | ovs_mutex_lock(&pmd->port_mutex); | |
5433 | poll_list = xrealloc(poll_list, hmap_count(&pmd->poll_list) | |
5434 | * sizeof *poll_list); | |
5435 | ||
5436 | i = 0; | |
5437 | HMAP_FOR_EACH (poll, node, &pmd->poll_list) { | |
5438 | poll_list[i].rxq = poll->rxq; | |
5439 | poll_list[i].port_no = poll->rxq->port->port_no; | |
5440 | poll_list[i].emc_enabled = poll->rxq->port->emc_enabled; | |
5441 | poll_list[i].rxq_enabled = netdev_rxq_enabled(poll->rxq->rx); | |
5442 | poll_list[i].change_seq = | |
5443 | netdev_get_change_seq(poll->rxq->port->netdev); | |
5444 | i++; | |
5445 | } | |
5446 | ||
5447 | pmd_load_cached_ports(pmd); | |
5448 | ||
5449 | ovs_mutex_unlock(&pmd->port_mutex); | |
5450 | ||
5451 | *ppoll_list = poll_list; | |
5452 | return i; | |
5453 | } | |
5454 | ||
5455 | static void * | |
5456 | pmd_thread_main(void *f_) | |
5457 | { | |
5458 | struct dp_netdev_pmd_thread *pmd = f_; | |
5459 | struct pmd_perf_stats *s = &pmd->perf_stats; | |
5460 | unsigned int lc = 0; | |
5461 | struct polled_queue *poll_list; | |
5462 | bool wait_for_reload = false; | |
5463 | bool reload_tx_qid; | |
5464 | bool exiting; | |
5465 | bool reload; | |
5466 | int poll_cnt; | |
5467 | int i; | |
5468 | int process_packets = 0; | |
5469 | ||
5470 | poll_list = NULL; | |
5471 | ||
5472 | /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */ | |
5473 | ovsthread_setspecific(pmd->dp->per_pmd_key, pmd); | |
5474 | ovs_numa_thread_setaffinity_core(pmd->core_id); | |
5475 | dpdk_set_lcore_id(pmd->core_id); | |
5476 | poll_cnt = pmd_load_queues_and_ports(pmd, &poll_list); | |
5477 | dfc_cache_init(&pmd->flow_cache); | |
5478 | pmd_alloc_static_tx_qid(pmd); | |
5479 | ||
5480 | reload: | |
5481 | atomic_count_init(&pmd->pmd_overloaded, 0); | |
5482 | ||
5483 | /* List port/core affinity */ | |
5484 | for (i = 0; i < poll_cnt; i++) { | |
5485 | VLOG_DBG("Core %d processing port \'%s\' with queue-id %d\n", | |
5486 | pmd->core_id, netdev_rxq_get_name(poll_list[i].rxq->rx), | |
5487 | netdev_rxq_get_queue_id(poll_list[i].rxq->rx)); | |
5488 | /* Reset the rxq current cycles counter. */ | |
5489 | dp_netdev_rxq_set_cycles(poll_list[i].rxq, RXQ_CYCLES_PROC_CURR, 0); | |
5490 | } | |
5491 | ||
5492 | if (!poll_cnt) { | |
5493 | if (wait_for_reload) { | |
5494 | /* Don't sleep, control thread will ask for a reload shortly. */ | |
5495 | do { | |
5496 | atomic_read_explicit(&pmd->reload, &reload, | |
5497 | memory_order_acquire); | |
5498 | } while (!reload); | |
5499 | } else { | |
5500 | while (seq_read(pmd->reload_seq) == pmd->last_reload_seq) { | |
5501 | seq_wait(pmd->reload_seq, pmd->last_reload_seq); | |
5502 | poll_block(); | |
5503 | } | |
5504 | } | |
5505 | } | |
5506 | ||
5507 | pmd->intrvl_tsc_prev = 0; | |
5508 | atomic_store_relaxed(&pmd->intrvl_cycles, 0); | |
5509 | cycles_counter_update(s); | |
5510 | /* Protect pmd stats from external clearing while polling. */ | |
5511 | ovs_mutex_lock(&pmd->perf_stats.stats_mutex); | |
5512 | for (;;) { | |
5513 | uint64_t rx_packets = 0, tx_packets = 0; | |
5514 | ||
5515 | pmd_perf_start_iteration(s); | |
5516 | ||
5517 | for (i = 0; i < poll_cnt; i++) { | |
5518 | ||
5519 | if (!poll_list[i].rxq_enabled) { | |
5520 | continue; | |
5521 | } | |
5522 | ||
5523 | if (poll_list[i].emc_enabled) { | |
5524 | atomic_read_relaxed(&pmd->dp->emc_insert_min, | |
5525 | &pmd->ctx.emc_insert_min); | |
5526 | } else { | |
5527 | pmd->ctx.emc_insert_min = 0; | |
5528 | } | |
5529 | ||
5530 | process_packets = | |
5531 | dp_netdev_process_rxq_port(pmd, poll_list[i].rxq, | |
5532 | poll_list[i].port_no); | |
5533 | rx_packets += process_packets; | |
5534 | } | |
5535 | ||
5536 | if (!rx_packets) { | |
5537 | /* We didn't receive anything in the process loop. | |
5538 | * Check if we need to send something. | |
5539 | * There was no time updates on current iteration. */ | |
5540 | pmd_thread_ctx_time_update(pmd); | |
5541 | tx_packets = dp_netdev_pmd_flush_output_packets(pmd, false); | |
5542 | } | |
5543 | ||
5544 | if (lc++ > 1024) { | |
5545 | lc = 0; | |
5546 | ||
5547 | coverage_try_clear(); | |
5548 | dp_netdev_pmd_try_optimize(pmd, poll_list, poll_cnt); | |
5549 | if (!ovsrcu_try_quiesce()) { | |
5550 | emc_cache_slow_sweep(&((pmd->flow_cache).emc_cache)); | |
5551 | } | |
5552 | ||
5553 | for (i = 0; i < poll_cnt; i++) { | |
5554 | uint64_t current_seq = | |
5555 | netdev_get_change_seq(poll_list[i].rxq->port->netdev); | |
5556 | if (poll_list[i].change_seq != current_seq) { | |
5557 | poll_list[i].change_seq = current_seq; | |
5558 | poll_list[i].rxq_enabled = | |
5559 | netdev_rxq_enabled(poll_list[i].rxq->rx); | |
5560 | } | |
5561 | } | |
5562 | } | |
5563 | ||
5564 | atomic_read_explicit(&pmd->reload, &reload, memory_order_acquire); | |
5565 | if (OVS_UNLIKELY(reload)) { | |
5566 | break; | |
5567 | } | |
5568 | ||
5569 | pmd_perf_end_iteration(s, rx_packets, tx_packets, | |
5570 | pmd_perf_metrics_enabled(pmd)); | |
5571 | } | |
5572 | ovs_mutex_unlock(&pmd->perf_stats.stats_mutex); | |
5573 | ||
5574 | poll_cnt = pmd_load_queues_and_ports(pmd, &poll_list); | |
5575 | atomic_read_relaxed(&pmd->wait_for_reload, &wait_for_reload); | |
5576 | atomic_read_relaxed(&pmd->reload_tx_qid, &reload_tx_qid); | |
5577 | atomic_read_relaxed(&pmd->exit, &exiting); | |
5578 | /* Signal here to make sure the pmd finishes | |
5579 | * reloading the updated configuration. */ | |
5580 | dp_netdev_pmd_reload_done(pmd); | |
5581 | ||
5582 | if (reload_tx_qid) { | |
5583 | pmd_free_static_tx_qid(pmd); | |
5584 | pmd_alloc_static_tx_qid(pmd); | |
5585 | } | |
5586 | ||
5587 | if (!exiting) { | |
5588 | goto reload; | |
5589 | } | |
5590 | ||
5591 | pmd_free_static_tx_qid(pmd); | |
5592 | dfc_cache_uninit(&pmd->flow_cache); | |
5593 | free(poll_list); | |
5594 | pmd_free_cached_ports(pmd); | |
5595 | return NULL; | |
5596 | } | |
5597 | ||
5598 | static void | |
5599 | dp_netdev_disable_upcall(struct dp_netdev *dp) | |
5600 | OVS_ACQUIRES(dp->upcall_rwlock) | |
5601 | { | |
5602 | fat_rwlock_wrlock(&dp->upcall_rwlock); | |
5603 | } | |
5604 | ||
5605 | \f | |
5606 | /* Meters */ | |
5607 | static void | |
5608 | dpif_netdev_meter_get_features(const struct dpif * dpif OVS_UNUSED, | |
5609 | struct ofputil_meter_features *features) | |
5610 | { | |
5611 | features->max_meters = MAX_METERS; | |
5612 | features->band_types = DP_SUPPORTED_METER_BAND_TYPES; | |
5613 | features->capabilities = DP_SUPPORTED_METER_FLAGS_MASK; | |
5614 | features->max_bands = MAX_BANDS; | |
5615 | features->max_color = 0; | |
5616 | } | |
5617 | ||
5618 | /* Applies the meter identified by 'meter_id' to 'packets_'. Packets | |
5619 | * that exceed a band are dropped in-place. */ | |
5620 | static void | |
5621 | dp_netdev_run_meter(struct dp_netdev *dp, struct dp_packet_batch *packets_, | |
5622 | uint32_t meter_id, long long int now) | |
5623 | { | |
5624 | struct dp_meter *meter; | |
5625 | struct dp_meter_band *band; | |
5626 | struct dp_packet *packet; | |
5627 | long long int long_delta_t; /* msec */ | |
5628 | uint32_t delta_t; /* msec */ | |
5629 | const size_t cnt = dp_packet_batch_size(packets_); | |
5630 | uint32_t bytes, volume; | |
5631 | int exceeded_band[NETDEV_MAX_BURST]; | |
5632 | uint32_t exceeded_rate[NETDEV_MAX_BURST]; | |
5633 | int exceeded_pkt = cnt; /* First packet that exceeded a band rate. */ | |
5634 | ||
5635 | if (meter_id >= MAX_METERS) { | |
5636 | return; | |
5637 | } | |
5638 | ||
5639 | meter_lock(dp, meter_id); | |
5640 | meter = dp->meters[meter_id]; | |
5641 | if (!meter) { | |
5642 | goto out; | |
5643 | } | |
5644 | ||
5645 | /* Initialize as negative values. */ | |
5646 | memset(exceeded_band, 0xff, cnt * sizeof *exceeded_band); | |
5647 | /* Initialize as zeroes. */ | |
5648 | memset(exceeded_rate, 0, cnt * sizeof *exceeded_rate); | |
5649 | ||
5650 | /* All packets will hit the meter at the same time. */ | |
5651 | long_delta_t = now / 1000 - meter->used / 1000; /* msec */ | |
5652 | ||
5653 | if (long_delta_t < 0) { | |
5654 | /* This condition means that we have several threads fighting for a | |
5655 | meter lock, and the one who received the packets a bit later wins. | |
5656 | Assuming that all racing threads received packets at the same time | |
5657 | to avoid overflow. */ | |
5658 | long_delta_t = 0; | |
5659 | } | |
5660 | ||
5661 | /* Make sure delta_t will not be too large, so that bucket will not | |
5662 | * wrap around below. */ | |
5663 | delta_t = (long_delta_t > (long long int)meter->max_delta_t) | |
5664 | ? meter->max_delta_t : (uint32_t)long_delta_t; | |
5665 | ||
5666 | /* Update meter stats. */ | |
5667 | meter->used = now; | |
5668 | meter->packet_count += cnt; | |
5669 | bytes = 0; | |
5670 | DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) { | |
5671 | bytes += dp_packet_size(packet); | |
5672 | } | |
5673 | meter->byte_count += bytes; | |
5674 | ||
5675 | /* Meters can operate in terms of packets per second or kilobits per | |
5676 | * second. */ | |
5677 | if (meter->flags & OFPMF13_PKTPS) { | |
5678 | /* Rate in packets/second, bucket 1/1000 packets. */ | |
5679 | /* msec * packets/sec = 1/1000 packets. */ | |
5680 | volume = cnt * 1000; /* Take 'cnt' packets from the bucket. */ | |
5681 | } else { | |
5682 | /* Rate in kbps, bucket in bits. */ | |
5683 | /* msec * kbps = bits */ | |
5684 | volume = bytes * 8; | |
5685 | } | |
5686 | ||
5687 | /* Update all bands and find the one hit with the highest rate for each | |
5688 | * packet (if any). */ | |
5689 | for (int m = 0; m < meter->n_bands; ++m) { | |
5690 | band = &meter->bands[m]; | |
5691 | ||
5692 | /* Update band's bucket. */ | |
5693 | band->bucket += delta_t * band->up.rate; | |
5694 | if (band->bucket > band->up.burst_size) { | |
5695 | band->bucket = band->up.burst_size; | |
5696 | } | |
5697 | ||
5698 | /* Drain the bucket for all the packets, if possible. */ | |
5699 | if (band->bucket >= volume) { | |
5700 | band->bucket -= volume; | |
5701 | } else { | |
5702 | int band_exceeded_pkt; | |
5703 | ||
5704 | /* Band limit hit, must process packet-by-packet. */ | |
5705 | if (meter->flags & OFPMF13_PKTPS) { | |
5706 | band_exceeded_pkt = band->bucket / 1000; | |
5707 | band->bucket %= 1000; /* Remainder stays in bucket. */ | |
5708 | ||
5709 | /* Update the exceeding band for each exceeding packet. | |
5710 | * (Only one band will be fired by a packet, and that | |
5711 | * can be different for each packet.) */ | |
5712 | for (int i = band_exceeded_pkt; i < cnt; i++) { | |
5713 | if (band->up.rate > exceeded_rate[i]) { | |
5714 | exceeded_rate[i] = band->up.rate; | |
5715 | exceeded_band[i] = m; | |
5716 | } | |
5717 | } | |
5718 | } else { | |
5719 | /* Packet sizes differ, must process one-by-one. */ | |
5720 | band_exceeded_pkt = cnt; | |
5721 | DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) { | |
5722 | uint32_t bits = dp_packet_size(packet) * 8; | |
5723 | ||
5724 | if (band->bucket >= bits) { | |
5725 | band->bucket -= bits; | |
5726 | } else { | |
5727 | if (i < band_exceeded_pkt) { | |
5728 | band_exceeded_pkt = i; | |
5729 | } | |
5730 | /* Update the exceeding band for the exceeding packet. | |
5731 | * (Only one band will be fired by a packet, and that | |
5732 | * can be different for each packet.) */ | |
5733 | if (band->up.rate > exceeded_rate[i]) { | |
5734 | exceeded_rate[i] = band->up.rate; | |
5735 | exceeded_band[i] = m; | |
5736 | } | |
5737 | } | |
5738 | } | |
5739 | } | |
5740 | /* Remember the first exceeding packet. */ | |
5741 | if (exceeded_pkt > band_exceeded_pkt) { | |
5742 | exceeded_pkt = band_exceeded_pkt; | |
5743 | } | |
5744 | } | |
5745 | } | |
5746 | ||
5747 | /* Fire the highest rate band exceeded by each packet, and drop | |
5748 | * packets if needed. */ | |
5749 | size_t j; | |
5750 | DP_PACKET_BATCH_REFILL_FOR_EACH (j, cnt, packet, packets_) { | |
5751 | if (exceeded_band[j] >= 0) { | |
5752 | /* Meter drop packet. */ | |
5753 | band = &meter->bands[exceeded_band[j]]; | |
5754 | band->packet_count += 1; | |
5755 | band->byte_count += dp_packet_size(packet); | |
5756 | ||
5757 | dp_packet_delete(packet); | |
5758 | } else { | |
5759 | /* Meter accepts packet. */ | |
5760 | dp_packet_batch_refill(packets_, packet, j); | |
5761 | } | |
5762 | } | |
5763 | out: | |
5764 | meter_unlock(dp, meter_id); | |
5765 | } | |
5766 | ||
5767 | /* Meter set/get/del processing is still single-threaded. */ | |
5768 | static int | |
5769 | dpif_netdev_meter_set(struct dpif *dpif, ofproto_meter_id meter_id, | |
5770 | struct ofputil_meter_config *config) | |
5771 | { | |
5772 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
5773 | uint32_t mid = meter_id.uint32; | |
5774 | struct dp_meter *meter; | |
5775 | int i; | |
5776 | ||
5777 | if (mid >= MAX_METERS) { | |
5778 | return EFBIG; /* Meter_id out of range. */ | |
5779 | } | |
5780 | ||
5781 | if (config->flags & ~DP_SUPPORTED_METER_FLAGS_MASK) { | |
5782 | return EBADF; /* Unsupported flags set */ | |
5783 | } | |
5784 | ||
5785 | if (config->n_bands > MAX_BANDS) { | |
5786 | return EINVAL; | |
5787 | } | |
5788 | ||
5789 | for (i = 0; i < config->n_bands; ++i) { | |
5790 | switch (config->bands[i].type) { | |
5791 | case OFPMBT13_DROP: | |
5792 | break; | |
5793 | default: | |
5794 | return ENODEV; /* Unsupported band type */ | |
5795 | } | |
5796 | } | |
5797 | ||
5798 | /* Allocate meter */ | |
5799 | meter = xzalloc(sizeof *meter | |
5800 | + config->n_bands * sizeof(struct dp_meter_band)); | |
5801 | ||
5802 | meter->flags = config->flags; | |
5803 | meter->n_bands = config->n_bands; | |
5804 | meter->max_delta_t = 0; | |
5805 | meter->used = time_usec(); | |
5806 | ||
5807 | /* set up bands */ | |
5808 | for (i = 0; i < config->n_bands; ++i) { | |
5809 | uint32_t band_max_delta_t; | |
5810 | ||
5811 | /* Set burst size to a workable value if none specified. */ | |
5812 | if (config->bands[i].burst_size == 0) { | |
5813 | config->bands[i].burst_size = config->bands[i].rate; | |
5814 | } | |
5815 | ||
5816 | meter->bands[i].up = config->bands[i]; | |
5817 | /* Convert burst size to the bucket units: */ | |
5818 | /* pkts => 1/1000 packets, kilobits => bits. */ | |
5819 | meter->bands[i].up.burst_size *= 1000; | |
5820 | /* Initialize bucket to empty. */ | |
5821 | meter->bands[i].bucket = 0; | |
5822 | ||
5823 | /* Figure out max delta_t that is enough to fill any bucket. */ | |
5824 | band_max_delta_t | |
5825 | = meter->bands[i].up.burst_size / meter->bands[i].up.rate; | |
5826 | if (band_max_delta_t > meter->max_delta_t) { | |
5827 | meter->max_delta_t = band_max_delta_t; | |
5828 | } | |
5829 | } | |
5830 | ||
5831 | meter_lock(dp, mid); | |
5832 | dp_delete_meter(dp, mid); /* Free existing meter, if any */ | |
5833 | dp->meters[mid] = meter; | |
5834 | meter_unlock(dp, mid); | |
5835 | ||
5836 | return 0; | |
5837 | } | |
5838 | ||
5839 | static int | |
5840 | dpif_netdev_meter_get(const struct dpif *dpif, | |
5841 | ofproto_meter_id meter_id_, | |
5842 | struct ofputil_meter_stats *stats, uint16_t n_bands) | |
5843 | { | |
5844 | const struct dp_netdev *dp = get_dp_netdev(dpif); | |
5845 | uint32_t meter_id = meter_id_.uint32; | |
5846 | int retval = 0; | |
5847 | ||
5848 | if (meter_id >= MAX_METERS) { | |
5849 | return EFBIG; | |
5850 | } | |
5851 | ||
5852 | meter_lock(dp, meter_id); | |
5853 | const struct dp_meter *meter = dp->meters[meter_id]; | |
5854 | if (!meter) { | |
5855 | retval = ENOENT; | |
5856 | goto done; | |
5857 | } | |
5858 | if (stats) { | |
5859 | int i = 0; | |
5860 | ||
5861 | stats->packet_in_count = meter->packet_count; | |
5862 | stats->byte_in_count = meter->byte_count; | |
5863 | ||
5864 | for (i = 0; i < n_bands && i < meter->n_bands; ++i) { | |
5865 | stats->bands[i].packet_count = meter->bands[i].packet_count; | |
5866 | stats->bands[i].byte_count = meter->bands[i].byte_count; | |
5867 | } | |
5868 | ||
5869 | stats->n_bands = i; | |
5870 | } | |
5871 | ||
5872 | done: | |
5873 | meter_unlock(dp, meter_id); | |
5874 | return retval; | |
5875 | } | |
5876 | ||
5877 | static int | |
5878 | dpif_netdev_meter_del(struct dpif *dpif, | |
5879 | ofproto_meter_id meter_id_, | |
5880 | struct ofputil_meter_stats *stats, uint16_t n_bands) | |
5881 | { | |
5882 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
5883 | int error; | |
5884 | ||
5885 | error = dpif_netdev_meter_get(dpif, meter_id_, stats, n_bands); | |
5886 | if (!error) { | |
5887 | uint32_t meter_id = meter_id_.uint32; | |
5888 | ||
5889 | meter_lock(dp, meter_id); | |
5890 | dp_delete_meter(dp, meter_id); | |
5891 | meter_unlock(dp, meter_id); | |
5892 | } | |
5893 | return error; | |
5894 | } | |
5895 | ||
5896 | \f | |
5897 | static void | |
5898 | dpif_netdev_disable_upcall(struct dpif *dpif) | |
5899 | OVS_NO_THREAD_SAFETY_ANALYSIS | |
5900 | { | |
5901 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
5902 | dp_netdev_disable_upcall(dp); | |
5903 | } | |
5904 | ||
5905 | static void | |
5906 | dp_netdev_enable_upcall(struct dp_netdev *dp) | |
5907 | OVS_RELEASES(dp->upcall_rwlock) | |
5908 | { | |
5909 | fat_rwlock_unlock(&dp->upcall_rwlock); | |
5910 | } | |
5911 | ||
5912 | static void | |
5913 | dpif_netdev_enable_upcall(struct dpif *dpif) | |
5914 | OVS_NO_THREAD_SAFETY_ANALYSIS | |
5915 | { | |
5916 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
5917 | dp_netdev_enable_upcall(dp); | |
5918 | } | |
5919 | ||
5920 | static void | |
5921 | dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread *pmd) | |
5922 | { | |
5923 | atomic_store_relaxed(&pmd->wait_for_reload, false); | |
5924 | atomic_store_relaxed(&pmd->reload_tx_qid, false); | |
5925 | pmd->last_reload_seq = seq_read(pmd->reload_seq); | |
5926 | atomic_store_explicit(&pmd->reload, false, memory_order_release); | |
5927 | } | |
5928 | ||
5929 | /* Finds and refs the dp_netdev_pmd_thread on core 'core_id'. Returns | |
5930 | * the pointer if succeeds, otherwise, NULL (it can return NULL even if | |
5931 | * 'core_id' is NON_PMD_CORE_ID). | |
5932 | * | |
5933 | * Caller must unrefs the returned reference. */ | |
5934 | static struct dp_netdev_pmd_thread * | |
5935 | dp_netdev_get_pmd(struct dp_netdev *dp, unsigned core_id) | |
5936 | { | |
5937 | struct dp_netdev_pmd_thread *pmd; | |
5938 | const struct cmap_node *pnode; | |
5939 | ||
5940 | pnode = cmap_find(&dp->poll_threads, hash_int(core_id, 0)); | |
5941 | if (!pnode) { | |
5942 | return NULL; | |
5943 | } | |
5944 | pmd = CONTAINER_OF(pnode, struct dp_netdev_pmd_thread, node); | |
5945 | ||
5946 | return dp_netdev_pmd_try_ref(pmd) ? pmd : NULL; | |
5947 | } | |
5948 | ||
5949 | /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */ | |
5950 | static void | |
5951 | dp_netdev_set_nonpmd(struct dp_netdev *dp) | |
5952 | OVS_REQUIRES(dp->port_mutex) | |
5953 | { | |
5954 | struct dp_netdev_pmd_thread *non_pmd; | |
5955 | ||
5956 | non_pmd = xzalloc(sizeof *non_pmd); | |
5957 | dp_netdev_configure_pmd(non_pmd, dp, NON_PMD_CORE_ID, OVS_NUMA_UNSPEC); | |
5958 | } | |
5959 | ||
5960 | /* Caller must have valid pointer to 'pmd'. */ | |
5961 | static bool | |
5962 | dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread *pmd) | |
5963 | { | |
5964 | return ovs_refcount_try_ref_rcu(&pmd->ref_cnt); | |
5965 | } | |
5966 | ||
5967 | static void | |
5968 | dp_netdev_pmd_unref(struct dp_netdev_pmd_thread *pmd) | |
5969 | { | |
5970 | if (pmd && ovs_refcount_unref(&pmd->ref_cnt) == 1) { | |
5971 | ovsrcu_postpone(dp_netdev_destroy_pmd, pmd); | |
5972 | } | |
5973 | } | |
5974 | ||
5975 | /* Given cmap position 'pos', tries to ref the next node. If try_ref() | |
5976 | * fails, keeps checking for next node until reaching the end of cmap. | |
5977 | * | |
5978 | * Caller must unrefs the returned reference. */ | |
5979 | static struct dp_netdev_pmd_thread * | |
5980 | dp_netdev_pmd_get_next(struct dp_netdev *dp, struct cmap_position *pos) | |
5981 | { | |
5982 | struct dp_netdev_pmd_thread *next; | |
5983 | ||
5984 | do { | |
5985 | struct cmap_node *node; | |
5986 | ||
5987 | node = cmap_next_position(&dp->poll_threads, pos); | |
5988 | next = node ? CONTAINER_OF(node, struct dp_netdev_pmd_thread, node) | |
5989 | : NULL; | |
5990 | } while (next && !dp_netdev_pmd_try_ref(next)); | |
5991 | ||
5992 | return next; | |
5993 | } | |
5994 | ||
5995 | /* Configures the 'pmd' based on the input argument. */ | |
5996 | static void | |
5997 | dp_netdev_configure_pmd(struct dp_netdev_pmd_thread *pmd, struct dp_netdev *dp, | |
5998 | unsigned core_id, int numa_id) | |
5999 | { | |
6000 | pmd->dp = dp; | |
6001 | pmd->core_id = core_id; | |
6002 | pmd->numa_id = numa_id; | |
6003 | pmd->need_reload = false; | |
6004 | pmd->n_output_batches = 0; | |
6005 | ||
6006 | ovs_refcount_init(&pmd->ref_cnt); | |
6007 | atomic_init(&pmd->exit, false); | |
6008 | pmd->reload_seq = seq_create(); | |
6009 | pmd->last_reload_seq = seq_read(pmd->reload_seq); | |
6010 | atomic_init(&pmd->reload, false); | |
6011 | ovs_mutex_init(&pmd->flow_mutex); | |
6012 | ovs_mutex_init(&pmd->port_mutex); | |
6013 | cmap_init(&pmd->flow_table); | |
6014 | cmap_init(&pmd->classifiers); | |
6015 | pmd->ctx.last_rxq = NULL; | |
6016 | pmd_thread_ctx_time_update(pmd); | |
6017 | pmd->next_optimization = pmd->ctx.now + DPCLS_OPTIMIZATION_INTERVAL; | |
6018 | pmd->rxq_next_cycle_store = pmd->ctx.now + PMD_RXQ_INTERVAL_LEN; | |
6019 | hmap_init(&pmd->poll_list); | |
6020 | hmap_init(&pmd->tx_ports); | |
6021 | hmap_init(&pmd->tnl_port_cache); | |
6022 | hmap_init(&pmd->send_port_cache); | |
6023 | /* init the 'flow_cache' since there is no | |
6024 | * actual thread created for NON_PMD_CORE_ID. */ | |
6025 | if (core_id == NON_PMD_CORE_ID) { | |
6026 | dfc_cache_init(&pmd->flow_cache); | |
6027 | pmd_alloc_static_tx_qid(pmd); | |
6028 | } | |
6029 | pmd_perf_stats_init(&pmd->perf_stats); | |
6030 | cmap_insert(&dp->poll_threads, CONST_CAST(struct cmap_node *, &pmd->node), | |
6031 | hash_int(core_id, 0)); | |
6032 | } | |
6033 | ||
6034 | static void | |
6035 | dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread *pmd) | |
6036 | { | |
6037 | struct dpcls *cls; | |
6038 | ||
6039 | dp_netdev_pmd_flow_flush(pmd); | |
6040 | hmap_destroy(&pmd->send_port_cache); | |
6041 | hmap_destroy(&pmd->tnl_port_cache); | |
6042 | hmap_destroy(&pmd->tx_ports); | |
6043 | hmap_destroy(&pmd->poll_list); | |
6044 | /* All flows (including their dpcls_rules) have been deleted already */ | |
6045 | CMAP_FOR_EACH (cls, node, &pmd->classifiers) { | |
6046 | dpcls_destroy(cls); | |
6047 | ovsrcu_postpone(free, cls); | |
6048 | } | |
6049 | cmap_destroy(&pmd->classifiers); | |
6050 | cmap_destroy(&pmd->flow_table); | |
6051 | ovs_mutex_destroy(&pmd->flow_mutex); | |
6052 | seq_destroy(pmd->reload_seq); | |
6053 | ovs_mutex_destroy(&pmd->port_mutex); | |
6054 | free(pmd); | |
6055 | } | |
6056 | ||
6057 | /* Stops the pmd thread, removes it from the 'dp->poll_threads', | |
6058 | * and unrefs the struct. */ | |
6059 | static void | |
6060 | dp_netdev_del_pmd(struct dp_netdev *dp, struct dp_netdev_pmd_thread *pmd) | |
6061 | { | |
6062 | /* NON_PMD_CORE_ID doesn't have a thread, so we don't have to synchronize, | |
6063 | * but extra cleanup is necessary */ | |
6064 | if (pmd->core_id == NON_PMD_CORE_ID) { | |
6065 | ovs_mutex_lock(&dp->non_pmd_mutex); | |
6066 | dfc_cache_uninit(&pmd->flow_cache); | |
6067 | pmd_free_cached_ports(pmd); | |
6068 | pmd_free_static_tx_qid(pmd); | |
6069 | ovs_mutex_unlock(&dp->non_pmd_mutex); | |
6070 | } else { | |
6071 | atomic_store_relaxed(&pmd->exit, true); | |
6072 | dp_netdev_reload_pmd__(pmd); | |
6073 | xpthread_join(pmd->thread, NULL); | |
6074 | } | |
6075 | ||
6076 | dp_netdev_pmd_clear_ports(pmd); | |
6077 | ||
6078 | /* Purges the 'pmd''s flows after stopping the thread, but before | |
6079 | * destroying the flows, so that the flow stats can be collected. */ | |
6080 | if (dp->dp_purge_cb) { | |
6081 | dp->dp_purge_cb(dp->dp_purge_aux, pmd->core_id); | |
6082 | } | |
6083 | cmap_remove(&pmd->dp->poll_threads, &pmd->node, hash_int(pmd->core_id, 0)); | |
6084 | dp_netdev_pmd_unref(pmd); | |
6085 | } | |
6086 | ||
6087 | /* Destroys all pmd threads. If 'non_pmd' is true it also destroys the non pmd | |
6088 | * thread. */ | |
6089 | static void | |
6090 | dp_netdev_destroy_all_pmds(struct dp_netdev *dp, bool non_pmd) | |
6091 | { | |
6092 | struct dp_netdev_pmd_thread *pmd; | |
6093 | struct dp_netdev_pmd_thread **pmd_list; | |
6094 | size_t k = 0, n_pmds; | |
6095 | ||
6096 | n_pmds = cmap_count(&dp->poll_threads); | |
6097 | pmd_list = xcalloc(n_pmds, sizeof *pmd_list); | |
6098 | ||
6099 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
6100 | if (!non_pmd && pmd->core_id == NON_PMD_CORE_ID) { | |
6101 | continue; | |
6102 | } | |
6103 | /* We cannot call dp_netdev_del_pmd(), since it alters | |
6104 | * 'dp->poll_threads' (while we're iterating it) and it | |
6105 | * might quiesce. */ | |
6106 | ovs_assert(k < n_pmds); | |
6107 | pmd_list[k++] = pmd; | |
6108 | } | |
6109 | ||
6110 | for (size_t i = 0; i < k; i++) { | |
6111 | dp_netdev_del_pmd(dp, pmd_list[i]); | |
6112 | } | |
6113 | free(pmd_list); | |
6114 | } | |
6115 | ||
6116 | /* Deletes all rx queues from pmd->poll_list and all the ports from | |
6117 | * pmd->tx_ports. */ | |
6118 | static void | |
6119 | dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread *pmd) | |
6120 | { | |
6121 | struct rxq_poll *poll; | |
6122 | struct tx_port *port; | |
6123 | ||
6124 | ovs_mutex_lock(&pmd->port_mutex); | |
6125 | HMAP_FOR_EACH_POP (poll, node, &pmd->poll_list) { | |
6126 | free(poll); | |
6127 | } | |
6128 | HMAP_FOR_EACH_POP (port, node, &pmd->tx_ports) { | |
6129 | free(port); | |
6130 | } | |
6131 | ovs_mutex_unlock(&pmd->port_mutex); | |
6132 | } | |
6133 | ||
6134 | /* Adds rx queue to poll_list of PMD thread, if it's not there already. */ | |
6135 | static void | |
6136 | dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread *pmd, | |
6137 | struct dp_netdev_rxq *rxq) | |
6138 | OVS_REQUIRES(pmd->port_mutex) | |
6139 | { | |
6140 | int qid = netdev_rxq_get_queue_id(rxq->rx); | |
6141 | uint32_t hash = hash_2words(odp_to_u32(rxq->port->port_no), qid); | |
6142 | struct rxq_poll *poll; | |
6143 | ||
6144 | HMAP_FOR_EACH_WITH_HASH (poll, node, hash, &pmd->poll_list) { | |
6145 | if (poll->rxq == rxq) { | |
6146 | /* 'rxq' is already polled by this thread. Do nothing. */ | |
6147 | return; | |
6148 | } | |
6149 | } | |
6150 | ||
6151 | poll = xmalloc(sizeof *poll); | |
6152 | poll->rxq = rxq; | |
6153 | hmap_insert(&pmd->poll_list, &poll->node, hash); | |
6154 | ||
6155 | pmd->need_reload = true; | |
6156 | } | |
6157 | ||
6158 | /* Delete 'poll' from poll_list of PMD thread. */ | |
6159 | static void | |
6160 | dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread *pmd, | |
6161 | struct rxq_poll *poll) | |
6162 | OVS_REQUIRES(pmd->port_mutex) | |
6163 | { | |
6164 | hmap_remove(&pmd->poll_list, &poll->node); | |
6165 | free(poll); | |
6166 | ||
6167 | pmd->need_reload = true; | |
6168 | } | |
6169 | ||
6170 | /* Add 'port' to the tx port cache of 'pmd', which must be reloaded for the | |
6171 | * changes to take effect. */ | |
6172 | static void | |
6173 | dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread *pmd, | |
6174 | struct dp_netdev_port *port) | |
6175 | OVS_REQUIRES(pmd->port_mutex) | |
6176 | { | |
6177 | struct tx_port *tx; | |
6178 | ||
6179 | tx = tx_port_lookup(&pmd->tx_ports, port->port_no); | |
6180 | if (tx) { | |
6181 | /* 'port' is already on this thread tx cache. Do nothing. */ | |
6182 | return; | |
6183 | } | |
6184 | ||
6185 | tx = xzalloc(sizeof *tx); | |
6186 | ||
6187 | tx->port = port; | |
6188 | tx->qid = -1; | |
6189 | tx->flush_time = 0LL; | |
6190 | dp_packet_batch_init(&tx->output_pkts); | |
6191 | ||
6192 | hmap_insert(&pmd->tx_ports, &tx->node, hash_port_no(tx->port->port_no)); | |
6193 | pmd->need_reload = true; | |
6194 | } | |
6195 | ||
6196 | /* Del 'tx' from the tx port cache of 'pmd', which must be reloaded for the | |
6197 | * changes to take effect. */ | |
6198 | static void | |
6199 | dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread *pmd, | |
6200 | struct tx_port *tx) | |
6201 | OVS_REQUIRES(pmd->port_mutex) | |
6202 | { | |
6203 | hmap_remove(&pmd->tx_ports, &tx->node); | |
6204 | free(tx); | |
6205 | pmd->need_reload = true; | |
6206 | } | |
6207 | \f | |
6208 | static char * | |
6209 | dpif_netdev_get_datapath_version(void) | |
6210 | { | |
6211 | return xstrdup("<built-in>"); | |
6212 | } | |
6213 | ||
6214 | static void | |
6215 | dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow, int cnt, int size, | |
6216 | uint16_t tcp_flags, long long now) | |
6217 | { | |
6218 | uint16_t flags; | |
6219 | ||
6220 | atomic_store_relaxed(&netdev_flow->stats.used, now); | |
6221 | non_atomic_ullong_add(&netdev_flow->stats.packet_count, cnt); | |
6222 | non_atomic_ullong_add(&netdev_flow->stats.byte_count, size); | |
6223 | atomic_read_relaxed(&netdev_flow->stats.tcp_flags, &flags); | |
6224 | flags |= tcp_flags; | |
6225 | atomic_store_relaxed(&netdev_flow->stats.tcp_flags, flags); | |
6226 | } | |
6227 | ||
6228 | static int | |
6229 | dp_netdev_upcall(struct dp_netdev_pmd_thread *pmd, struct dp_packet *packet_, | |
6230 | struct flow *flow, struct flow_wildcards *wc, ovs_u128 *ufid, | |
6231 | enum dpif_upcall_type type, const struct nlattr *userdata, | |
6232 | struct ofpbuf *actions, struct ofpbuf *put_actions) | |
6233 | { | |
6234 | struct dp_netdev *dp = pmd->dp; | |
6235 | ||
6236 | if (OVS_UNLIKELY(!dp->upcall_cb)) { | |
6237 | return ENODEV; | |
6238 | } | |
6239 | ||
6240 | if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl))) { | |
6241 | struct ds ds = DS_EMPTY_INITIALIZER; | |
6242 | char *packet_str; | |
6243 | struct ofpbuf key; | |
6244 | struct odp_flow_key_parms odp_parms = { | |
6245 | .flow = flow, | |
6246 | .mask = wc ? &wc->masks : NULL, | |
6247 | .support = dp_netdev_support, | |
6248 | }; | |
6249 | ||
6250 | ofpbuf_init(&key, 0); | |
6251 | odp_flow_key_from_flow(&odp_parms, &key); | |
6252 | packet_str = ofp_dp_packet_to_string(packet_); | |
6253 | ||
6254 | odp_flow_key_format(key.data, key.size, &ds); | |
6255 | ||
6256 | VLOG_DBG("%s: %s upcall:\n%s\n%s", dp->name, | |
6257 | dpif_upcall_type_to_string(type), ds_cstr(&ds), packet_str); | |
6258 | ||
6259 | ofpbuf_uninit(&key); | |
6260 | free(packet_str); | |
6261 | ||
6262 | ds_destroy(&ds); | |
6263 | } | |
6264 | ||
6265 | return dp->upcall_cb(packet_, flow, ufid, pmd->core_id, type, userdata, | |
6266 | actions, wc, put_actions, dp->upcall_aux); | |
6267 | } | |
6268 | ||
6269 | static inline uint32_t | |
6270 | dpif_netdev_packet_get_rss_hash_orig_pkt(struct dp_packet *packet, | |
6271 | const struct miniflow *mf) | |
6272 | { | |
6273 | uint32_t hash; | |
6274 | ||
6275 | if (OVS_LIKELY(dp_packet_rss_valid(packet))) { | |
6276 | hash = dp_packet_get_rss_hash(packet); | |
6277 | } else { | |
6278 | hash = miniflow_hash_5tuple(mf, 0); | |
6279 | dp_packet_set_rss_hash(packet, hash); | |
6280 | } | |
6281 | ||
6282 | return hash; | |
6283 | } | |
6284 | ||
6285 | static inline uint32_t | |
6286 | dpif_netdev_packet_get_rss_hash(struct dp_packet *packet, | |
6287 | const struct miniflow *mf) | |
6288 | { | |
6289 | uint32_t hash, recirc_depth; | |
6290 | ||
6291 | if (OVS_LIKELY(dp_packet_rss_valid(packet))) { | |
6292 | hash = dp_packet_get_rss_hash(packet); | |
6293 | } else { | |
6294 | hash = miniflow_hash_5tuple(mf, 0); | |
6295 | dp_packet_set_rss_hash(packet, hash); | |
6296 | } | |
6297 | ||
6298 | /* The RSS hash must account for the recirculation depth to avoid | |
6299 | * collisions in the exact match cache */ | |
6300 | recirc_depth = *recirc_depth_get_unsafe(); | |
6301 | if (OVS_UNLIKELY(recirc_depth)) { | |
6302 | hash = hash_finish(hash, recirc_depth); | |
6303 | } | |
6304 | return hash; | |
6305 | } | |
6306 | ||
6307 | struct packet_batch_per_flow { | |
6308 | unsigned int byte_count; | |
6309 | uint16_t tcp_flags; | |
6310 | struct dp_netdev_flow *flow; | |
6311 | ||
6312 | struct dp_packet_batch array; | |
6313 | }; | |
6314 | ||
6315 | static inline void | |
6316 | packet_batch_per_flow_update(struct packet_batch_per_flow *batch, | |
6317 | struct dp_packet *packet, | |
6318 | uint16_t tcp_flags) | |
6319 | { | |
6320 | batch->byte_count += dp_packet_size(packet); | |
6321 | batch->tcp_flags |= tcp_flags; | |
6322 | dp_packet_batch_add(&batch->array, packet); | |
6323 | } | |
6324 | ||
6325 | static inline void | |
6326 | packet_batch_per_flow_init(struct packet_batch_per_flow *batch, | |
6327 | struct dp_netdev_flow *flow) | |
6328 | { | |
6329 | flow->batch = batch; | |
6330 | ||
6331 | batch->flow = flow; | |
6332 | dp_packet_batch_init(&batch->array); | |
6333 | batch->byte_count = 0; | |
6334 | batch->tcp_flags = 0; | |
6335 | } | |
6336 | ||
6337 | static inline void | |
6338 | packet_batch_per_flow_execute(struct packet_batch_per_flow *batch, | |
6339 | struct dp_netdev_pmd_thread *pmd) | |
6340 | { | |
6341 | struct dp_netdev_actions *actions; | |
6342 | struct dp_netdev_flow *flow = batch->flow; | |
6343 | ||
6344 | dp_netdev_flow_used(flow, dp_packet_batch_size(&batch->array), | |
6345 | batch->byte_count, | |
6346 | batch->tcp_flags, pmd->ctx.now / 1000); | |
6347 | ||
6348 | actions = dp_netdev_flow_get_actions(flow); | |
6349 | ||
6350 | dp_netdev_execute_actions(pmd, &batch->array, true, &flow->flow, | |
6351 | actions->actions, actions->size); | |
6352 | } | |
6353 | ||
6354 | static inline void | |
6355 | dp_netdev_queue_batches(struct dp_packet *pkt, | |
6356 | struct dp_netdev_flow *flow, uint16_t tcp_flags, | |
6357 | struct packet_batch_per_flow *batches, | |
6358 | size_t *n_batches) | |
6359 | { | |
6360 | struct packet_batch_per_flow *batch = flow->batch; | |
6361 | ||
6362 | if (OVS_UNLIKELY(!batch)) { | |
6363 | batch = &batches[(*n_batches)++]; | |
6364 | packet_batch_per_flow_init(batch, flow); | |
6365 | } | |
6366 | ||
6367 | packet_batch_per_flow_update(batch, pkt, tcp_flags); | |
6368 | } | |
6369 | ||
6370 | static inline void | |
6371 | packet_enqueue_to_flow_map(struct dp_packet *packet, | |
6372 | struct dp_netdev_flow *flow, | |
6373 | uint16_t tcp_flags, | |
6374 | struct dp_packet_flow_map *flow_map, | |
6375 | size_t index) | |
6376 | { | |
6377 | struct dp_packet_flow_map *map = &flow_map[index]; | |
6378 | map->flow = flow; | |
6379 | map->packet = packet; | |
6380 | map->tcp_flags = tcp_flags; | |
6381 | } | |
6382 | ||
6383 | /* SMC lookup function for a batch of packets. | |
6384 | * By doing batching SMC lookup, we can use prefetch | |
6385 | * to hide memory access latency. | |
6386 | */ | |
6387 | static inline void | |
6388 | smc_lookup_batch(struct dp_netdev_pmd_thread *pmd, | |
6389 | struct netdev_flow_key *keys, | |
6390 | struct netdev_flow_key **missed_keys, | |
6391 | struct dp_packet_batch *packets_, | |
6392 | const int cnt, | |
6393 | struct dp_packet_flow_map *flow_map, | |
6394 | uint8_t *index_map) | |
6395 | { | |
6396 | int i; | |
6397 | struct dp_packet *packet; | |
6398 | size_t n_smc_hit = 0, n_missed = 0; | |
6399 | struct dfc_cache *cache = &pmd->flow_cache; | |
6400 | struct smc_cache *smc_cache = &cache->smc_cache; | |
6401 | const struct cmap_node *flow_node; | |
6402 | int recv_idx; | |
6403 | uint16_t tcp_flags; | |
6404 | ||
6405 | /* Prefetch buckets for all packets */ | |
6406 | for (i = 0; i < cnt; i++) { | |
6407 | OVS_PREFETCH(&smc_cache->buckets[keys[i].hash & SMC_MASK]); | |
6408 | } | |
6409 | ||
6410 | DP_PACKET_BATCH_REFILL_FOR_EACH (i, cnt, packet, packets_) { | |
6411 | struct dp_netdev_flow *flow = NULL; | |
6412 | flow_node = smc_entry_get(pmd, keys[i].hash); | |
6413 | bool hit = false; | |
6414 | /* Get the original order of this packet in received batch. */ | |
6415 | recv_idx = index_map[i]; | |
6416 | ||
6417 | if (OVS_LIKELY(flow_node != NULL)) { | |
6418 | CMAP_NODE_FOR_EACH (flow, node, flow_node) { | |
6419 | /* Since we dont have per-port megaflow to check the port | |
6420 | * number, we need to verify that the input ports match. */ | |
6421 | if (OVS_LIKELY(dpcls_rule_matches_key(&flow->cr, &keys[i]) && | |
6422 | flow->flow.in_port.odp_port == packet->md.in_port.odp_port)) { | |
6423 | tcp_flags = miniflow_get_tcp_flags(&keys[i].mf); | |
6424 | ||
6425 | /* SMC hit and emc miss, we insert into EMC */ | |
6426 | keys[i].len = | |
6427 | netdev_flow_key_size(miniflow_n_values(&keys[i].mf)); | |
6428 | emc_probabilistic_insert(pmd, &keys[i], flow); | |
6429 | /* Add these packets into the flow map in the same order | |
6430 | * as received. | |
6431 | */ | |
6432 | packet_enqueue_to_flow_map(packet, flow, tcp_flags, | |
6433 | flow_map, recv_idx); | |
6434 | n_smc_hit++; | |
6435 | hit = true; | |
6436 | break; | |
6437 | } | |
6438 | } | |
6439 | if (hit) { | |
6440 | continue; | |
6441 | } | |
6442 | } | |
6443 | ||
6444 | /* SMC missed. Group missed packets together at | |
6445 | * the beginning of the 'packets' array. */ | |
6446 | dp_packet_batch_refill(packets_, packet, i); | |
6447 | ||
6448 | /* Preserve the order of packet for flow batching. */ | |
6449 | index_map[n_missed] = recv_idx; | |
6450 | ||
6451 | /* Put missed keys to the pointer arrays return to the caller */ | |
6452 | missed_keys[n_missed++] = &keys[i]; | |
6453 | } | |
6454 | ||
6455 | pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_SMC_HIT, n_smc_hit); | |
6456 | } | |
6457 | ||
6458 | /* Try to process all ('cnt') the 'packets' using only the datapath flow cache | |
6459 | * 'pmd->flow_cache'. If a flow is not found for a packet 'packets[i]', the | |
6460 | * miniflow is copied into 'keys' and the packet pointer is moved at the | |
6461 | * beginning of the 'packets' array. The pointers of missed keys are put in the | |
6462 | * missed_keys pointer array for future processing. | |
6463 | * | |
6464 | * The function returns the number of packets that needs to be processed in the | |
6465 | * 'packets' array (they have been moved to the beginning of the vector). | |
6466 | * | |
6467 | * For performance reasons a caller may choose not to initialize the metadata | |
6468 | * in 'packets_'. If 'md_is_valid' is false, the metadata in 'packets' | |
6469 | * is not valid and must be initialized by this function using 'port_no'. | |
6470 | * If 'md_is_valid' is true, the metadata is already valid and 'port_no' | |
6471 | * will be ignored. | |
6472 | */ | |
6473 | static inline size_t | |
6474 | dfc_processing(struct dp_netdev_pmd_thread *pmd, | |
6475 | struct dp_packet_batch *packets_, | |
6476 | struct netdev_flow_key *keys, | |
6477 | struct netdev_flow_key **missed_keys, | |
6478 | struct packet_batch_per_flow batches[], size_t *n_batches, | |
6479 | struct dp_packet_flow_map *flow_map, | |
6480 | size_t *n_flows, uint8_t *index_map, | |
6481 | bool md_is_valid, odp_port_t port_no) | |
6482 | { | |
6483 | struct netdev_flow_key *key = &keys[0]; | |
6484 | size_t n_missed = 0, n_emc_hit = 0; | |
6485 | struct dfc_cache *cache = &pmd->flow_cache; | |
6486 | struct dp_packet *packet; | |
6487 | const size_t cnt = dp_packet_batch_size(packets_); | |
6488 | uint32_t cur_min = pmd->ctx.emc_insert_min; | |
6489 | int i; | |
6490 | uint16_t tcp_flags; | |
6491 | bool smc_enable_db; | |
6492 | size_t map_cnt = 0; | |
6493 | bool batch_enable = true; | |
6494 | ||
6495 | atomic_read_relaxed(&pmd->dp->smc_enable_db, &smc_enable_db); | |
6496 | pmd_perf_update_counter(&pmd->perf_stats, | |
6497 | md_is_valid ? PMD_STAT_RECIRC : PMD_STAT_RECV, | |
6498 | cnt); | |
6499 | ||
6500 | DP_PACKET_BATCH_REFILL_FOR_EACH (i, cnt, packet, packets_) { | |
6501 | struct dp_netdev_flow *flow; | |
6502 | uint32_t mark; | |
6503 | ||
6504 | if (OVS_UNLIKELY(dp_packet_size(packet) < ETH_HEADER_LEN)) { | |
6505 | dp_packet_delete(packet); | |
6506 | continue; | |
6507 | } | |
6508 | ||
6509 | if (i != cnt - 1) { | |
6510 | struct dp_packet **packets = packets_->packets; | |
6511 | /* Prefetch next packet data and metadata. */ | |
6512 | OVS_PREFETCH(dp_packet_data(packets[i+1])); | |
6513 | pkt_metadata_prefetch_init(&packets[i+1]->md); | |
6514 | } | |
6515 | ||
6516 | if (!md_is_valid) { | |
6517 | pkt_metadata_init(&packet->md, port_no); | |
6518 | } | |
6519 | ||
6520 | if ((*recirc_depth_get() == 0) && | |
6521 | dp_packet_has_flow_mark(packet, &mark)) { | |
6522 | flow = mark_to_flow_find(pmd, mark); | |
6523 | if (OVS_LIKELY(flow)) { | |
6524 | tcp_flags = parse_tcp_flags(packet); | |
6525 | if (OVS_LIKELY(batch_enable)) { | |
6526 | dp_netdev_queue_batches(packet, flow, tcp_flags, batches, | |
6527 | n_batches); | |
6528 | } else { | |
6529 | /* Flow batching should be performed only after fast-path | |
6530 | * processing is also completed for packets with emc miss | |
6531 | * or else it will result in reordering of packets with | |
6532 | * same datapath flows. */ | |
6533 | packet_enqueue_to_flow_map(packet, flow, tcp_flags, | |
6534 | flow_map, map_cnt++); | |
6535 | } | |
6536 | continue; | |
6537 | } | |
6538 | } | |
6539 | ||
6540 | miniflow_extract(packet, &key->mf); | |
6541 | key->len = 0; /* Not computed yet. */ | |
6542 | key->hash = | |
6543 | (md_is_valid == false) | |
6544 | ? dpif_netdev_packet_get_rss_hash_orig_pkt(packet, &key->mf) | |
6545 | : dpif_netdev_packet_get_rss_hash(packet, &key->mf); | |
6546 | ||
6547 | /* If EMC is disabled skip emc_lookup */ | |
6548 | flow = (cur_min != 0) ? emc_lookup(&cache->emc_cache, key) : NULL; | |
6549 | if (OVS_LIKELY(flow)) { | |
6550 | tcp_flags = miniflow_get_tcp_flags(&key->mf); | |
6551 | n_emc_hit++; | |
6552 | if (OVS_LIKELY(batch_enable)) { | |
6553 | dp_netdev_queue_batches(packet, flow, tcp_flags, batches, | |
6554 | n_batches); | |
6555 | } else { | |
6556 | /* Flow batching should be performed only after fast-path | |
6557 | * processing is also completed for packets with emc miss | |
6558 | * or else it will result in reordering of packets with | |
6559 | * same datapath flows. */ | |
6560 | packet_enqueue_to_flow_map(packet, flow, tcp_flags, | |
6561 | flow_map, map_cnt++); | |
6562 | } | |
6563 | } else { | |
6564 | /* Exact match cache missed. Group missed packets together at | |
6565 | * the beginning of the 'packets' array. */ | |
6566 | dp_packet_batch_refill(packets_, packet, i); | |
6567 | ||
6568 | /* Preserve the order of packet for flow batching. */ | |
6569 | index_map[n_missed] = map_cnt; | |
6570 | flow_map[map_cnt++].flow = NULL; | |
6571 | ||
6572 | /* 'key[n_missed]' contains the key of the current packet and it | |
6573 | * will be passed to SMC lookup. The next key should be extracted | |
6574 | * to 'keys[n_missed + 1]'. | |
6575 | * We also maintain a pointer array to keys missed both SMC and EMC | |
6576 | * which will be returned to the caller for future processing. */ | |
6577 | missed_keys[n_missed] = key; | |
6578 | key = &keys[++n_missed]; | |
6579 | ||
6580 | /* Skip batching for subsequent packets to avoid reordering. */ | |
6581 | batch_enable = false; | |
6582 | } | |
6583 | } | |
6584 | /* Count of packets which are not flow batched. */ | |
6585 | *n_flows = map_cnt; | |
6586 | ||
6587 | pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_EXACT_HIT, n_emc_hit); | |
6588 | ||
6589 | if (!smc_enable_db) { | |
6590 | return dp_packet_batch_size(packets_); | |
6591 | } | |
6592 | ||
6593 | /* Packets miss EMC will do a batch lookup in SMC if enabled */ | |
6594 | smc_lookup_batch(pmd, keys, missed_keys, packets_, | |
6595 | n_missed, flow_map, index_map); | |
6596 | ||
6597 | return dp_packet_batch_size(packets_); | |
6598 | } | |
6599 | ||
6600 | static inline int | |
6601 | handle_packet_upcall(struct dp_netdev_pmd_thread *pmd, | |
6602 | struct dp_packet *packet, | |
6603 | const struct netdev_flow_key *key, | |
6604 | struct ofpbuf *actions, struct ofpbuf *put_actions) | |
6605 | { | |
6606 | struct ofpbuf *add_actions; | |
6607 | struct dp_packet_batch b; | |
6608 | struct match match; | |
6609 | ovs_u128 ufid; | |
6610 | int error; | |
6611 | uint64_t cycles = cycles_counter_update(&pmd->perf_stats); | |
6612 | ||
6613 | match.tun_md.valid = false; | |
6614 | miniflow_expand(&key->mf, &match.flow); | |
6615 | memset(&match.wc, 0, sizeof match.wc); | |
6616 | ||
6617 | ofpbuf_clear(actions); | |
6618 | ofpbuf_clear(put_actions); | |
6619 | ||
6620 | dpif_flow_hash(pmd->dp->dpif, &match.flow, sizeof match.flow, &ufid); | |
6621 | error = dp_netdev_upcall(pmd, packet, &match.flow, &match.wc, | |
6622 | &ufid, DPIF_UC_MISS, NULL, actions, | |
6623 | put_actions); | |
6624 | if (OVS_UNLIKELY(error && error != ENOSPC)) { | |
6625 | dp_packet_delete(packet); | |
6626 | return error; | |
6627 | } | |
6628 | ||
6629 | /* The Netlink encoding of datapath flow keys cannot express | |
6630 | * wildcarding the presence of a VLAN tag. Instead, a missing VLAN | |
6631 | * tag is interpreted as exact match on the fact that there is no | |
6632 | * VLAN. Unless we refactor a lot of code that translates between | |
6633 | * Netlink and struct flow representations, we have to do the same | |
6634 | * here. This must be in sync with 'match' in dpif_netdev_flow_put(). */ | |
6635 | if (!match.wc.masks.vlans[0].tci) { | |
6636 | match.wc.masks.vlans[0].tci = htons(0xffff); | |
6637 | } | |
6638 | ||
6639 | /* We can't allow the packet batching in the next loop to execute | |
6640 | * the actions. Otherwise, if there are any slow path actions, | |
6641 | * we'll send the packet up twice. */ | |
6642 | dp_packet_batch_init_packet(&b, packet); | |
6643 | dp_netdev_execute_actions(pmd, &b, true, &match.flow, | |
6644 | actions->data, actions->size); | |
6645 | ||
6646 | add_actions = put_actions->size ? put_actions : actions; | |
6647 | if (OVS_LIKELY(error != ENOSPC)) { | |
6648 | struct dp_netdev_flow *netdev_flow; | |
6649 | ||
6650 | /* XXX: There's a race window where a flow covering this packet | |
6651 | * could have already been installed since we last did the flow | |
6652 | * lookup before upcall. This could be solved by moving the | |
6653 | * mutex lock outside the loop, but that's an awful long time | |
6654 | * to be locking revalidators out of making flow modifications. */ | |
6655 | ovs_mutex_lock(&pmd->flow_mutex); | |
6656 | netdev_flow = dp_netdev_pmd_lookup_flow(pmd, key, NULL); | |
6657 | if (OVS_LIKELY(!netdev_flow)) { | |
6658 | netdev_flow = dp_netdev_flow_add(pmd, &match, &ufid, | |
6659 | add_actions->data, | |
6660 | add_actions->size); | |
6661 | } | |
6662 | ovs_mutex_unlock(&pmd->flow_mutex); | |
6663 | uint32_t hash = dp_netdev_flow_hash(&netdev_flow->ufid); | |
6664 | smc_insert(pmd, key, hash); | |
6665 | emc_probabilistic_insert(pmd, key, netdev_flow); | |
6666 | } | |
6667 | if (pmd_perf_metrics_enabled(pmd)) { | |
6668 | /* Update upcall stats. */ | |
6669 | cycles = cycles_counter_update(&pmd->perf_stats) - cycles; | |
6670 | struct pmd_perf_stats *s = &pmd->perf_stats; | |
6671 | s->current.upcalls++; | |
6672 | s->current.upcall_cycles += cycles; | |
6673 | histogram_add_sample(&s->cycles_per_upcall, cycles); | |
6674 | } | |
6675 | return error; | |
6676 | } | |
6677 | ||
6678 | static inline void | |
6679 | fast_path_processing(struct dp_netdev_pmd_thread *pmd, | |
6680 | struct dp_packet_batch *packets_, | |
6681 | struct netdev_flow_key **keys, | |
6682 | struct dp_packet_flow_map *flow_map, | |
6683 | uint8_t *index_map, | |
6684 | odp_port_t in_port) | |
6685 | { | |
6686 | const size_t cnt = dp_packet_batch_size(packets_); | |
6687 | #if !defined(__CHECKER__) && !defined(_WIN32) | |
6688 | const size_t PKT_ARRAY_SIZE = cnt; | |
6689 | #else | |
6690 | /* Sparse or MSVC doesn't like variable length array. */ | |
6691 | enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST }; | |
6692 | #endif | |
6693 | struct dp_packet *packet; | |
6694 | struct dpcls *cls; | |
6695 | struct dpcls_rule *rules[PKT_ARRAY_SIZE]; | |
6696 | struct dp_netdev *dp = pmd->dp; | |
6697 | int upcall_ok_cnt = 0, upcall_fail_cnt = 0; | |
6698 | int lookup_cnt = 0, add_lookup_cnt; | |
6699 | bool any_miss; | |
6700 | ||
6701 | for (size_t i = 0; i < cnt; i++) { | |
6702 | /* Key length is needed in all the cases, hash computed on demand. */ | |
6703 | keys[i]->len = netdev_flow_key_size(miniflow_n_values(&keys[i]->mf)); | |
6704 | } | |
6705 | /* Get the classifier for the in_port */ | |
6706 | cls = dp_netdev_pmd_lookup_dpcls(pmd, in_port); | |
6707 | if (OVS_LIKELY(cls)) { | |
6708 | any_miss = !dpcls_lookup(cls, (const struct netdev_flow_key **)keys, | |
6709 | rules, cnt, &lookup_cnt); | |
6710 | } else { | |
6711 | any_miss = true; | |
6712 | memset(rules, 0, sizeof(rules)); | |
6713 | } | |
6714 | if (OVS_UNLIKELY(any_miss) && !fat_rwlock_tryrdlock(&dp->upcall_rwlock)) { | |
6715 | uint64_t actions_stub[512 / 8], slow_stub[512 / 8]; | |
6716 | struct ofpbuf actions, put_actions; | |
6717 | ||
6718 | ofpbuf_use_stub(&actions, actions_stub, sizeof actions_stub); | |
6719 | ofpbuf_use_stub(&put_actions, slow_stub, sizeof slow_stub); | |
6720 | ||
6721 | DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) { | |
6722 | struct dp_netdev_flow *netdev_flow; | |
6723 | ||
6724 | if (OVS_LIKELY(rules[i])) { | |
6725 | continue; | |
6726 | } | |
6727 | ||
6728 | /* It's possible that an earlier slow path execution installed | |
6729 | * a rule covering this flow. In this case, it's a lot cheaper | |
6730 | * to catch it here than execute a miss. */ | |
6731 | netdev_flow = dp_netdev_pmd_lookup_flow(pmd, keys[i], | |
6732 | &add_lookup_cnt); | |
6733 | if (netdev_flow) { | |
6734 | lookup_cnt += add_lookup_cnt; | |
6735 | rules[i] = &netdev_flow->cr; | |
6736 | continue; | |
6737 | } | |
6738 | ||
6739 | int error = handle_packet_upcall(pmd, packet, keys[i], | |
6740 | &actions, &put_actions); | |
6741 | ||
6742 | if (OVS_UNLIKELY(error)) { | |
6743 | upcall_fail_cnt++; | |
6744 | } else { | |
6745 | upcall_ok_cnt++; | |
6746 | } | |
6747 | } | |
6748 | ||
6749 | ofpbuf_uninit(&actions); | |
6750 | ofpbuf_uninit(&put_actions); | |
6751 | fat_rwlock_unlock(&dp->upcall_rwlock); | |
6752 | } else if (OVS_UNLIKELY(any_miss)) { | |
6753 | DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) { | |
6754 | if (OVS_UNLIKELY(!rules[i])) { | |
6755 | dp_packet_delete(packet); | |
6756 | upcall_fail_cnt++; | |
6757 | } | |
6758 | } | |
6759 | } | |
6760 | ||
6761 | DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) { | |
6762 | struct dp_netdev_flow *flow; | |
6763 | /* Get the original order of this packet in received batch. */ | |
6764 | int recv_idx = index_map[i]; | |
6765 | uint16_t tcp_flags; | |
6766 | ||
6767 | if (OVS_UNLIKELY(!rules[i])) { | |
6768 | continue; | |
6769 | } | |
6770 | ||
6771 | flow = dp_netdev_flow_cast(rules[i]); | |
6772 | uint32_t hash = dp_netdev_flow_hash(&flow->ufid); | |
6773 | smc_insert(pmd, keys[i], hash); | |
6774 | ||
6775 | emc_probabilistic_insert(pmd, keys[i], flow); | |
6776 | /* Add these packets into the flow map in the same order | |
6777 | * as received. | |
6778 | */ | |
6779 | tcp_flags = miniflow_get_tcp_flags(&keys[i]->mf); | |
6780 | packet_enqueue_to_flow_map(packet, flow, tcp_flags, | |
6781 | flow_map, recv_idx); | |
6782 | } | |
6783 | ||
6784 | pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_MASKED_HIT, | |
6785 | cnt - upcall_ok_cnt - upcall_fail_cnt); | |
6786 | pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_MASKED_LOOKUP, | |
6787 | lookup_cnt); | |
6788 | pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_MISS, | |
6789 | upcall_ok_cnt); | |
6790 | pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_LOST, | |
6791 | upcall_fail_cnt); | |
6792 | } | |
6793 | ||
6794 | /* Packets enter the datapath from a port (or from recirculation) here. | |
6795 | * | |
6796 | * When 'md_is_valid' is true the metadata in 'packets' are already valid. | |
6797 | * When false the metadata in 'packets' need to be initialized. */ | |
6798 | static void | |
6799 | dp_netdev_input__(struct dp_netdev_pmd_thread *pmd, | |
6800 | struct dp_packet_batch *packets, | |
6801 | bool md_is_valid, odp_port_t port_no) | |
6802 | { | |
6803 | #if !defined(__CHECKER__) && !defined(_WIN32) | |
6804 | const size_t PKT_ARRAY_SIZE = dp_packet_batch_size(packets); | |
6805 | #else | |
6806 | /* Sparse or MSVC doesn't like variable length array. */ | |
6807 | enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST }; | |
6808 | #endif | |
6809 | OVS_ALIGNED_VAR(CACHE_LINE_SIZE) | |
6810 | struct netdev_flow_key keys[PKT_ARRAY_SIZE]; | |
6811 | struct netdev_flow_key *missed_keys[PKT_ARRAY_SIZE]; | |
6812 | struct packet_batch_per_flow batches[PKT_ARRAY_SIZE]; | |
6813 | size_t n_batches; | |
6814 | struct dp_packet_flow_map flow_map[PKT_ARRAY_SIZE]; | |
6815 | uint8_t index_map[PKT_ARRAY_SIZE]; | |
6816 | size_t n_flows, i; | |
6817 | ||
6818 | odp_port_t in_port; | |
6819 | ||
6820 | n_batches = 0; | |
6821 | dfc_processing(pmd, packets, keys, missed_keys, batches, &n_batches, | |
6822 | flow_map, &n_flows, index_map, md_is_valid, port_no); | |
6823 | ||
6824 | if (!dp_packet_batch_is_empty(packets)) { | |
6825 | /* Get ingress port from first packet's metadata. */ | |
6826 | in_port = packets->packets[0]->md.in_port.odp_port; | |
6827 | fast_path_processing(pmd, packets, missed_keys, | |
6828 | flow_map, index_map, in_port); | |
6829 | } | |
6830 | ||
6831 | /* Batch rest of packets which are in flow map. */ | |
6832 | for (i = 0; i < n_flows; i++) { | |
6833 | struct dp_packet_flow_map *map = &flow_map[i]; | |
6834 | ||
6835 | if (OVS_UNLIKELY(!map->flow)) { | |
6836 | continue; | |
6837 | } | |
6838 | dp_netdev_queue_batches(map->packet, map->flow, map->tcp_flags, | |
6839 | batches, &n_batches); | |
6840 | } | |
6841 | ||
6842 | /* All the flow batches need to be reset before any call to | |
6843 | * packet_batch_per_flow_execute() as it could potentially trigger | |
6844 | * recirculation. When a packet matching flow ‘j’ happens to be | |
6845 | * recirculated, the nested call to dp_netdev_input__() could potentially | |
6846 | * classify the packet as matching another flow - say 'k'. It could happen | |
6847 | * that in the previous call to dp_netdev_input__() that same flow 'k' had | |
6848 | * already its own batches[k] still waiting to be served. So if its | |
6849 | * ‘batch’ member is not reset, the recirculated packet would be wrongly | |
6850 | * appended to batches[k] of the 1st call to dp_netdev_input__(). */ | |
6851 | for (i = 0; i < n_batches; i++) { | |
6852 | batches[i].flow->batch = NULL; | |
6853 | } | |
6854 | ||
6855 | for (i = 0; i < n_batches; i++) { | |
6856 | packet_batch_per_flow_execute(&batches[i], pmd); | |
6857 | } | |
6858 | } | |
6859 | ||
6860 | static void | |
6861 | dp_netdev_input(struct dp_netdev_pmd_thread *pmd, | |
6862 | struct dp_packet_batch *packets, | |
6863 | odp_port_t port_no) | |
6864 | { | |
6865 | dp_netdev_input__(pmd, packets, false, port_no); | |
6866 | } | |
6867 | ||
6868 | static void | |
6869 | dp_netdev_recirculate(struct dp_netdev_pmd_thread *pmd, | |
6870 | struct dp_packet_batch *packets) | |
6871 | { | |
6872 | dp_netdev_input__(pmd, packets, true, 0); | |
6873 | } | |
6874 | ||
6875 | struct dp_netdev_execute_aux { | |
6876 | struct dp_netdev_pmd_thread *pmd; | |
6877 | const struct flow *flow; | |
6878 | }; | |
6879 | ||
6880 | static void | |
6881 | dpif_netdev_register_dp_purge_cb(struct dpif *dpif, dp_purge_callback *cb, | |
6882 | void *aux) | |
6883 | { | |
6884 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
6885 | dp->dp_purge_aux = aux; | |
6886 | dp->dp_purge_cb = cb; | |
6887 | } | |
6888 | ||
6889 | static void | |
6890 | dpif_netdev_register_upcall_cb(struct dpif *dpif, upcall_callback *cb, | |
6891 | void *aux) | |
6892 | { | |
6893 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
6894 | dp->upcall_aux = aux; | |
6895 | dp->upcall_cb = cb; | |
6896 | } | |
6897 | ||
6898 | static void | |
6899 | dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread *pmd, | |
6900 | bool purge) | |
6901 | { | |
6902 | struct tx_port *tx; | |
6903 | struct dp_netdev_port *port; | |
6904 | long long interval; | |
6905 | ||
6906 | HMAP_FOR_EACH (tx, node, &pmd->send_port_cache) { | |
6907 | if (!tx->port->dynamic_txqs) { | |
6908 | continue; | |
6909 | } | |
6910 | interval = pmd->ctx.now - tx->last_used; | |
6911 | if (tx->qid >= 0 && (purge || interval >= XPS_TIMEOUT)) { | |
6912 | port = tx->port; | |
6913 | ovs_mutex_lock(&port->txq_used_mutex); | |
6914 | port->txq_used[tx->qid]--; | |
6915 | ovs_mutex_unlock(&port->txq_used_mutex); | |
6916 | tx->qid = -1; | |
6917 | } | |
6918 | } | |
6919 | } | |
6920 | ||
6921 | static int | |
6922 | dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread *pmd, | |
6923 | struct tx_port *tx) | |
6924 | { | |
6925 | struct dp_netdev_port *port; | |
6926 | long long interval; | |
6927 | int i, min_cnt, min_qid; | |
6928 | ||
6929 | interval = pmd->ctx.now - tx->last_used; | |
6930 | tx->last_used = pmd->ctx.now; | |
6931 | ||
6932 | if (OVS_LIKELY(tx->qid >= 0 && interval < XPS_TIMEOUT)) { | |
6933 | return tx->qid; | |
6934 | } | |
6935 | ||
6936 | port = tx->port; | |
6937 | ||
6938 | ovs_mutex_lock(&port->txq_used_mutex); | |
6939 | if (tx->qid >= 0) { | |
6940 | port->txq_used[tx->qid]--; | |
6941 | tx->qid = -1; | |
6942 | } | |
6943 | ||
6944 | min_cnt = -1; | |
6945 | min_qid = 0; | |
6946 | for (i = 0; i < netdev_n_txq(port->netdev); i++) { | |
6947 | if (port->txq_used[i] < min_cnt || min_cnt == -1) { | |
6948 | min_cnt = port->txq_used[i]; | |
6949 | min_qid = i; | |
6950 | } | |
6951 | } | |
6952 | ||
6953 | port->txq_used[min_qid]++; | |
6954 | tx->qid = min_qid; | |
6955 | ||
6956 | ovs_mutex_unlock(&port->txq_used_mutex); | |
6957 | ||
6958 | dpif_netdev_xps_revalidate_pmd(pmd, false); | |
6959 | ||
6960 | VLOG_DBG("Core %d: New TX queue ID %d for port \'%s\'.", | |
6961 | pmd->core_id, tx->qid, netdev_get_name(tx->port->netdev)); | |
6962 | return min_qid; | |
6963 | } | |
6964 | ||
6965 | static struct tx_port * | |
6966 | pmd_tnl_port_cache_lookup(const struct dp_netdev_pmd_thread *pmd, | |
6967 | odp_port_t port_no) | |
6968 | { | |
6969 | return tx_port_lookup(&pmd->tnl_port_cache, port_no); | |
6970 | } | |
6971 | ||
6972 | static struct tx_port * | |
6973 | pmd_send_port_cache_lookup(const struct dp_netdev_pmd_thread *pmd, | |
6974 | odp_port_t port_no) | |
6975 | { | |
6976 | return tx_port_lookup(&pmd->send_port_cache, port_no); | |
6977 | } | |
6978 | ||
6979 | static int | |
6980 | push_tnl_action(const struct dp_netdev_pmd_thread *pmd, | |
6981 | const struct nlattr *attr, | |
6982 | struct dp_packet_batch *batch) | |
6983 | { | |
6984 | struct tx_port *tun_port; | |
6985 | const struct ovs_action_push_tnl *data; | |
6986 | int err; | |
6987 | ||
6988 | data = nl_attr_get(attr); | |
6989 | ||
6990 | tun_port = pmd_tnl_port_cache_lookup(pmd, data->tnl_port); | |
6991 | if (!tun_port) { | |
6992 | err = -EINVAL; | |
6993 | goto error; | |
6994 | } | |
6995 | err = netdev_push_header(tun_port->port->netdev, batch, data); | |
6996 | if (!err) { | |
6997 | return 0; | |
6998 | } | |
6999 | error: | |
7000 | dp_packet_delete_batch(batch, true); | |
7001 | return err; | |
7002 | } | |
7003 | ||
7004 | static void | |
7005 | dp_execute_userspace_action(struct dp_netdev_pmd_thread *pmd, | |
7006 | struct dp_packet *packet, bool should_steal, | |
7007 | struct flow *flow, ovs_u128 *ufid, | |
7008 | struct ofpbuf *actions, | |
7009 | const struct nlattr *userdata) | |
7010 | { | |
7011 | struct dp_packet_batch b; | |
7012 | int error; | |
7013 | ||
7014 | ofpbuf_clear(actions); | |
7015 | ||
7016 | error = dp_netdev_upcall(pmd, packet, flow, NULL, ufid, | |
7017 | DPIF_UC_ACTION, userdata, actions, | |
7018 | NULL); | |
7019 | if (!error || error == ENOSPC) { | |
7020 | dp_packet_batch_init_packet(&b, packet); | |
7021 | dp_netdev_execute_actions(pmd, &b, should_steal, flow, | |
7022 | actions->data, actions->size); | |
7023 | } else if (should_steal) { | |
7024 | dp_packet_delete(packet); | |
7025 | } | |
7026 | } | |
7027 | ||
7028 | static void | |
7029 | dp_execute_cb(void *aux_, struct dp_packet_batch *packets_, | |
7030 | const struct nlattr *a, bool should_steal) | |
7031 | OVS_NO_THREAD_SAFETY_ANALYSIS | |
7032 | { | |
7033 | struct dp_netdev_execute_aux *aux = aux_; | |
7034 | uint32_t *depth = recirc_depth_get(); | |
7035 | struct dp_netdev_pmd_thread *pmd = aux->pmd; | |
7036 | struct dp_netdev *dp = pmd->dp; | |
7037 | int type = nl_attr_type(a); | |
7038 | struct tx_port *p; | |
7039 | ||
7040 | switch ((enum ovs_action_attr)type) { | |
7041 | case OVS_ACTION_ATTR_OUTPUT: | |
7042 | p = pmd_send_port_cache_lookup(pmd, nl_attr_get_odp_port(a)); | |
7043 | if (OVS_LIKELY(p)) { | |
7044 | struct dp_packet *packet; | |
7045 | struct dp_packet_batch out; | |
7046 | ||
7047 | if (!should_steal) { | |
7048 | dp_packet_batch_clone(&out, packets_); | |
7049 | dp_packet_batch_reset_cutlen(packets_); | |
7050 | packets_ = &out; | |
7051 | } | |
7052 | dp_packet_batch_apply_cutlen(packets_); | |
7053 | ||
7054 | #ifdef DPDK_NETDEV | |
7055 | if (OVS_UNLIKELY(!dp_packet_batch_is_empty(&p->output_pkts) | |
7056 | && packets_->packets[0]->source | |
7057 | != p->output_pkts.packets[0]->source)) { | |
7058 | /* XXX: netdev-dpdk assumes that all packets in a single | |
7059 | * output batch has the same source. Flush here to | |
7060 | * avoid memory access issues. */ | |
7061 | dp_netdev_pmd_flush_output_on_port(pmd, p); | |
7062 | } | |
7063 | #endif | |
7064 | if (dp_packet_batch_size(&p->output_pkts) | |
7065 | + dp_packet_batch_size(packets_) > NETDEV_MAX_BURST) { | |
7066 | /* Flush here to avoid overflow. */ | |
7067 | dp_netdev_pmd_flush_output_on_port(pmd, p); | |
7068 | } | |
7069 | ||
7070 | if (dp_packet_batch_is_empty(&p->output_pkts)) { | |
7071 | pmd->n_output_batches++; | |
7072 | } | |
7073 | ||
7074 | DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) { | |
7075 | p->output_pkts_rxqs[dp_packet_batch_size(&p->output_pkts)] = | |
7076 | pmd->ctx.last_rxq; | |
7077 | dp_packet_batch_add(&p->output_pkts, packet); | |
7078 | } | |
7079 | return; | |
7080 | } | |
7081 | break; | |
7082 | ||
7083 | case OVS_ACTION_ATTR_TUNNEL_PUSH: | |
7084 | if (should_steal) { | |
7085 | /* We're requested to push tunnel header, but also we need to take | |
7086 | * the ownership of these packets. Thus, we can avoid performing | |
7087 | * the action, because the caller will not use the result anyway. | |
7088 | * Just break to free the batch. */ | |
7089 | break; | |
7090 | } | |
7091 | dp_packet_batch_apply_cutlen(packets_); | |
7092 | push_tnl_action(pmd, a, packets_); | |
7093 | return; | |
7094 | ||
7095 | case OVS_ACTION_ATTR_TUNNEL_POP: | |
7096 | if (*depth < MAX_RECIRC_DEPTH) { | |
7097 | struct dp_packet_batch *orig_packets_ = packets_; | |
7098 | odp_port_t portno = nl_attr_get_odp_port(a); | |
7099 | ||
7100 | p = pmd_tnl_port_cache_lookup(pmd, portno); | |
7101 | if (p) { | |
7102 | struct dp_packet_batch tnl_pkt; | |
7103 | ||
7104 | if (!should_steal) { | |
7105 | dp_packet_batch_clone(&tnl_pkt, packets_); | |
7106 | packets_ = &tnl_pkt; | |
7107 | dp_packet_batch_reset_cutlen(orig_packets_); | |
7108 | } | |
7109 | ||
7110 | dp_packet_batch_apply_cutlen(packets_); | |
7111 | ||
7112 | netdev_pop_header(p->port->netdev, packets_); | |
7113 | if (dp_packet_batch_is_empty(packets_)) { | |
7114 | return; | |
7115 | } | |
7116 | ||
7117 | struct dp_packet *packet; | |
7118 | DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) { | |
7119 | packet->md.in_port.odp_port = portno; | |
7120 | } | |
7121 | ||
7122 | (*depth)++; | |
7123 | dp_netdev_recirculate(pmd, packets_); | |
7124 | (*depth)--; | |
7125 | return; | |
7126 | } | |
7127 | } | |
7128 | break; | |
7129 | ||
7130 | case OVS_ACTION_ATTR_USERSPACE: | |
7131 | if (!fat_rwlock_tryrdlock(&dp->upcall_rwlock)) { | |
7132 | struct dp_packet_batch *orig_packets_ = packets_; | |
7133 | const struct nlattr *userdata; | |
7134 | struct dp_packet_batch usr_pkt; | |
7135 | struct ofpbuf actions; | |
7136 | struct flow flow; | |
7137 | ovs_u128 ufid; | |
7138 | bool clone = false; | |
7139 | ||
7140 | userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA); | |
7141 | ofpbuf_init(&actions, 0); | |
7142 | ||
7143 | if (packets_->trunc) { | |
7144 | if (!should_steal) { | |
7145 | dp_packet_batch_clone(&usr_pkt, packets_); | |
7146 | packets_ = &usr_pkt; | |
7147 | clone = true; | |
7148 | dp_packet_batch_reset_cutlen(orig_packets_); | |
7149 | } | |
7150 | ||
7151 | dp_packet_batch_apply_cutlen(packets_); | |
7152 | } | |
7153 | ||
7154 | struct dp_packet *packet; | |
7155 | DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) { | |
7156 | flow_extract(packet, &flow); | |
7157 | dpif_flow_hash(dp->dpif, &flow, sizeof flow, &ufid); | |
7158 | dp_execute_userspace_action(pmd, packet, should_steal, &flow, | |
7159 | &ufid, &actions, userdata); | |
7160 | } | |
7161 | ||
7162 | if (clone) { | |
7163 | dp_packet_delete_batch(packets_, true); | |
7164 | } | |
7165 | ||
7166 | ofpbuf_uninit(&actions); | |
7167 | fat_rwlock_unlock(&dp->upcall_rwlock); | |
7168 | ||
7169 | return; | |
7170 | } | |
7171 | break; | |
7172 | ||
7173 | case OVS_ACTION_ATTR_RECIRC: | |
7174 | if (*depth < MAX_RECIRC_DEPTH) { | |
7175 | struct dp_packet_batch recirc_pkts; | |
7176 | ||
7177 | if (!should_steal) { | |
7178 | dp_packet_batch_clone(&recirc_pkts, packets_); | |
7179 | packets_ = &recirc_pkts; | |
7180 | } | |
7181 | ||
7182 | struct dp_packet *packet; | |
7183 | DP_PACKET_BATCH_FOR_EACH (i, packet, packets_) { | |
7184 | packet->md.recirc_id = nl_attr_get_u32(a); | |
7185 | } | |
7186 | ||
7187 | (*depth)++; | |
7188 | dp_netdev_recirculate(pmd, packets_); | |
7189 | (*depth)--; | |
7190 | ||
7191 | return; | |
7192 | } | |
7193 | ||
7194 | VLOG_WARN("Packet dropped. Max recirculation depth exceeded."); | |
7195 | break; | |
7196 | ||
7197 | case OVS_ACTION_ATTR_CT: { | |
7198 | const struct nlattr *b; | |
7199 | bool force = false; | |
7200 | bool commit = false; | |
7201 | unsigned int left; | |
7202 | uint16_t zone = 0; | |
7203 | const char *helper = NULL; | |
7204 | const uint32_t *setmark = NULL; | |
7205 | const struct ovs_key_ct_labels *setlabel = NULL; | |
7206 | struct nat_action_info_t nat_action_info; | |
7207 | struct nat_action_info_t *nat_action_info_ref = NULL; | |
7208 | bool nat_config = false; | |
7209 | ||
7210 | NL_ATTR_FOR_EACH_UNSAFE (b, left, nl_attr_get(a), | |
7211 | nl_attr_get_size(a)) { | |
7212 | enum ovs_ct_attr sub_type = nl_attr_type(b); | |
7213 | ||
7214 | switch(sub_type) { | |
7215 | case OVS_CT_ATTR_FORCE_COMMIT: | |
7216 | force = true; | |
7217 | /* fall through. */ | |
7218 | case OVS_CT_ATTR_COMMIT: | |
7219 | commit = true; | |
7220 | break; | |
7221 | case OVS_CT_ATTR_ZONE: | |
7222 | zone = nl_attr_get_u16(b); | |
7223 | break; | |
7224 | case OVS_CT_ATTR_HELPER: | |
7225 | helper = nl_attr_get_string(b); | |
7226 | break; | |
7227 | case OVS_CT_ATTR_MARK: | |
7228 | setmark = nl_attr_get(b); | |
7229 | break; | |
7230 | case OVS_CT_ATTR_LABELS: | |
7231 | setlabel = nl_attr_get(b); | |
7232 | break; | |
7233 | case OVS_CT_ATTR_EVENTMASK: | |
7234 | /* Silently ignored, as userspace datapath does not generate | |
7235 | * netlink events. */ | |
7236 | break; | |
7237 | case OVS_CT_ATTR_TIMEOUT: | |
7238 | /* Userspace datapath does not support customized timeout | |
7239 | * policy yet. */ | |
7240 | break; | |
7241 | case OVS_CT_ATTR_NAT: { | |
7242 | const struct nlattr *b_nest; | |
7243 | unsigned int left_nest; | |
7244 | bool ip_min_specified = false; | |
7245 | bool proto_num_min_specified = false; | |
7246 | bool ip_max_specified = false; | |
7247 | bool proto_num_max_specified = false; | |
7248 | memset(&nat_action_info, 0, sizeof nat_action_info); | |
7249 | nat_action_info_ref = &nat_action_info; | |
7250 | ||
7251 | NL_NESTED_FOR_EACH_UNSAFE (b_nest, left_nest, b) { | |
7252 | enum ovs_nat_attr sub_type_nest = nl_attr_type(b_nest); | |
7253 | ||
7254 | switch (sub_type_nest) { | |
7255 | case OVS_NAT_ATTR_SRC: | |
7256 | case OVS_NAT_ATTR_DST: | |
7257 | nat_config = true; | |
7258 | nat_action_info.nat_action |= | |
7259 | ((sub_type_nest == OVS_NAT_ATTR_SRC) | |
7260 | ? NAT_ACTION_SRC : NAT_ACTION_DST); | |
7261 | break; | |
7262 | case OVS_NAT_ATTR_IP_MIN: | |
7263 | memcpy(&nat_action_info.min_addr, | |
7264 | nl_attr_get(b_nest), | |
7265 | nl_attr_get_size(b_nest)); | |
7266 | ip_min_specified = true; | |
7267 | break; | |
7268 | case OVS_NAT_ATTR_IP_MAX: | |
7269 | memcpy(&nat_action_info.max_addr, | |
7270 | nl_attr_get(b_nest), | |
7271 | nl_attr_get_size(b_nest)); | |
7272 | ip_max_specified = true; | |
7273 | break; | |
7274 | case OVS_NAT_ATTR_PROTO_MIN: | |
7275 | nat_action_info.min_port = | |
7276 | nl_attr_get_u16(b_nest); | |
7277 | proto_num_min_specified = true; | |
7278 | break; | |
7279 | case OVS_NAT_ATTR_PROTO_MAX: | |
7280 | nat_action_info.max_port = | |
7281 | nl_attr_get_u16(b_nest); | |
7282 | proto_num_max_specified = true; | |
7283 | break; | |
7284 | case OVS_NAT_ATTR_PERSISTENT: | |
7285 | case OVS_NAT_ATTR_PROTO_HASH: | |
7286 | case OVS_NAT_ATTR_PROTO_RANDOM: | |
7287 | break; | |
7288 | case OVS_NAT_ATTR_UNSPEC: | |
7289 | case __OVS_NAT_ATTR_MAX: | |
7290 | OVS_NOT_REACHED(); | |
7291 | } | |
7292 | } | |
7293 | ||
7294 | if (ip_min_specified && !ip_max_specified) { | |
7295 | nat_action_info.max_addr = nat_action_info.min_addr; | |
7296 | } | |
7297 | if (proto_num_min_specified && !proto_num_max_specified) { | |
7298 | nat_action_info.max_port = nat_action_info.min_port; | |
7299 | } | |
7300 | if (proto_num_min_specified || proto_num_max_specified) { | |
7301 | if (nat_action_info.nat_action & NAT_ACTION_SRC) { | |
7302 | nat_action_info.nat_action |= NAT_ACTION_SRC_PORT; | |
7303 | } else if (nat_action_info.nat_action & NAT_ACTION_DST) { | |
7304 | nat_action_info.nat_action |= NAT_ACTION_DST_PORT; | |
7305 | } | |
7306 | } | |
7307 | break; | |
7308 | } | |
7309 | case OVS_CT_ATTR_UNSPEC: | |
7310 | case __OVS_CT_ATTR_MAX: | |
7311 | OVS_NOT_REACHED(); | |
7312 | } | |
7313 | } | |
7314 | ||
7315 | /* We won't be able to function properly in this case, hence | |
7316 | * complain loudly. */ | |
7317 | if (nat_config && !commit) { | |
7318 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); | |
7319 | VLOG_WARN_RL(&rl, "NAT specified without commit."); | |
7320 | } | |
7321 | ||
7322 | conntrack_execute(dp->conntrack, packets_, aux->flow->dl_type, force, | |
7323 | commit, zone, setmark, setlabel, aux->flow->tp_src, | |
7324 | aux->flow->tp_dst, helper, nat_action_info_ref, | |
7325 | pmd->ctx.now / 1000); | |
7326 | break; | |
7327 | } | |
7328 | ||
7329 | case OVS_ACTION_ATTR_METER: | |
7330 | dp_netdev_run_meter(pmd->dp, packets_, nl_attr_get_u32(a), | |
7331 | pmd->ctx.now); | |
7332 | break; | |
7333 | ||
7334 | case OVS_ACTION_ATTR_PUSH_VLAN: | |
7335 | case OVS_ACTION_ATTR_POP_VLAN: | |
7336 | case OVS_ACTION_ATTR_PUSH_MPLS: | |
7337 | case OVS_ACTION_ATTR_POP_MPLS: | |
7338 | case OVS_ACTION_ATTR_SET: | |
7339 | case OVS_ACTION_ATTR_SET_MASKED: | |
7340 | case OVS_ACTION_ATTR_SAMPLE: | |
7341 | case OVS_ACTION_ATTR_HASH: | |
7342 | case OVS_ACTION_ATTR_UNSPEC: | |
7343 | case OVS_ACTION_ATTR_TRUNC: | |
7344 | case OVS_ACTION_ATTR_PUSH_ETH: | |
7345 | case OVS_ACTION_ATTR_POP_ETH: | |
7346 | case OVS_ACTION_ATTR_CLONE: | |
7347 | case OVS_ACTION_ATTR_PUSH_NSH: | |
7348 | case OVS_ACTION_ATTR_POP_NSH: | |
7349 | case OVS_ACTION_ATTR_CT_CLEAR: | |
7350 | case OVS_ACTION_ATTR_CHECK_PKT_LEN: | |
7351 | case __OVS_ACTION_ATTR_MAX: | |
7352 | OVS_NOT_REACHED(); | |
7353 | } | |
7354 | ||
7355 | dp_packet_delete_batch(packets_, should_steal); | |
7356 | } | |
7357 | ||
7358 | static void | |
7359 | dp_netdev_execute_actions(struct dp_netdev_pmd_thread *pmd, | |
7360 | struct dp_packet_batch *packets, | |
7361 | bool should_steal, const struct flow *flow, | |
7362 | const struct nlattr *actions, size_t actions_len) | |
7363 | { | |
7364 | struct dp_netdev_execute_aux aux = { pmd, flow }; | |
7365 | ||
7366 | odp_execute_actions(&aux, packets, should_steal, actions, | |
7367 | actions_len, dp_execute_cb); | |
7368 | } | |
7369 | ||
7370 | struct dp_netdev_ct_dump { | |
7371 | struct ct_dpif_dump_state up; | |
7372 | struct conntrack_dump dump; | |
7373 | struct conntrack *ct; | |
7374 | struct dp_netdev *dp; | |
7375 | }; | |
7376 | ||
7377 | static int | |
7378 | dpif_netdev_ct_dump_start(struct dpif *dpif, struct ct_dpif_dump_state **dump_, | |
7379 | const uint16_t *pzone, int *ptot_bkts) | |
7380 | { | |
7381 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
7382 | struct dp_netdev_ct_dump *dump; | |
7383 | ||
7384 | dump = xzalloc(sizeof *dump); | |
7385 | dump->dp = dp; | |
7386 | dump->ct = dp->conntrack; | |
7387 | ||
7388 | conntrack_dump_start(dp->conntrack, &dump->dump, pzone, ptot_bkts); | |
7389 | ||
7390 | *dump_ = &dump->up; | |
7391 | ||
7392 | return 0; | |
7393 | } | |
7394 | ||
7395 | static int | |
7396 | dpif_netdev_ct_dump_next(struct dpif *dpif OVS_UNUSED, | |
7397 | struct ct_dpif_dump_state *dump_, | |
7398 | struct ct_dpif_entry *entry) | |
7399 | { | |
7400 | struct dp_netdev_ct_dump *dump; | |
7401 | ||
7402 | INIT_CONTAINER(dump, dump_, up); | |
7403 | ||
7404 | return conntrack_dump_next(&dump->dump, entry); | |
7405 | } | |
7406 | ||
7407 | static int | |
7408 | dpif_netdev_ct_dump_done(struct dpif *dpif OVS_UNUSED, | |
7409 | struct ct_dpif_dump_state *dump_) | |
7410 | { | |
7411 | struct dp_netdev_ct_dump *dump; | |
7412 | int err; | |
7413 | ||
7414 | INIT_CONTAINER(dump, dump_, up); | |
7415 | ||
7416 | err = conntrack_dump_done(&dump->dump); | |
7417 | ||
7418 | free(dump); | |
7419 | ||
7420 | return err; | |
7421 | } | |
7422 | ||
7423 | static int | |
7424 | dpif_netdev_ct_flush(struct dpif *dpif, const uint16_t *zone, | |
7425 | const struct ct_dpif_tuple *tuple) | |
7426 | { | |
7427 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
7428 | ||
7429 | if (tuple) { | |
7430 | return conntrack_flush_tuple(dp->conntrack, tuple, zone ? *zone : 0); | |
7431 | } | |
7432 | return conntrack_flush(dp->conntrack, zone); | |
7433 | } | |
7434 | ||
7435 | static int | |
7436 | dpif_netdev_ct_set_maxconns(struct dpif *dpif, uint32_t maxconns) | |
7437 | { | |
7438 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
7439 | ||
7440 | return conntrack_set_maxconns(dp->conntrack, maxconns); | |
7441 | } | |
7442 | ||
7443 | static int | |
7444 | dpif_netdev_ct_get_maxconns(struct dpif *dpif, uint32_t *maxconns) | |
7445 | { | |
7446 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
7447 | ||
7448 | return conntrack_get_maxconns(dp->conntrack, maxconns); | |
7449 | } | |
7450 | ||
7451 | static int | |
7452 | dpif_netdev_ct_get_nconns(struct dpif *dpif, uint32_t *nconns) | |
7453 | { | |
7454 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
7455 | ||
7456 | return conntrack_get_nconns(dp->conntrack, nconns); | |
7457 | } | |
7458 | ||
7459 | static int | |
7460 | dpif_netdev_ct_set_tcp_seq_chk(struct dpif *dpif, bool enabled) | |
7461 | { | |
7462 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
7463 | ||
7464 | return conntrack_set_tcp_seq_chk(dp->conntrack, enabled); | |
7465 | } | |
7466 | ||
7467 | static int | |
7468 | dpif_netdev_ct_get_tcp_seq_chk(struct dpif *dpif, bool *enabled) | |
7469 | { | |
7470 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
7471 | *enabled = conntrack_get_tcp_seq_chk(dp->conntrack); | |
7472 | return 0; | |
7473 | } | |
7474 | ||
7475 | static int | |
7476 | dpif_netdev_ipf_set_enabled(struct dpif *dpif, bool v6, bool enable) | |
7477 | { | |
7478 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
7479 | return ipf_set_enabled(conntrack_ipf_ctx(dp->conntrack), v6, enable); | |
7480 | } | |
7481 | ||
7482 | static int | |
7483 | dpif_netdev_ipf_set_min_frag(struct dpif *dpif, bool v6, uint32_t min_frag) | |
7484 | { | |
7485 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
7486 | return ipf_set_min_frag(conntrack_ipf_ctx(dp->conntrack), v6, min_frag); | |
7487 | } | |
7488 | ||
7489 | static int | |
7490 | dpif_netdev_ipf_set_max_nfrags(struct dpif *dpif, uint32_t max_frags) | |
7491 | { | |
7492 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
7493 | return ipf_set_max_nfrags(conntrack_ipf_ctx(dp->conntrack), max_frags); | |
7494 | } | |
7495 | ||
7496 | /* Adjust this function if 'dpif_ipf_status' and 'ipf_status' were to | |
7497 | * diverge. */ | |
7498 | static int | |
7499 | dpif_netdev_ipf_get_status(struct dpif *dpif, | |
7500 | struct dpif_ipf_status *dpif_ipf_status) | |
7501 | { | |
7502 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
7503 | ipf_get_status(conntrack_ipf_ctx(dp->conntrack), | |
7504 | (struct ipf_status *) dpif_ipf_status); | |
7505 | return 0; | |
7506 | } | |
7507 | ||
7508 | static int | |
7509 | dpif_netdev_ipf_dump_start(struct dpif *dpif OVS_UNUSED, | |
7510 | struct ipf_dump_ctx **ipf_dump_ctx) | |
7511 | { | |
7512 | return ipf_dump_start(ipf_dump_ctx); | |
7513 | } | |
7514 | ||
7515 | static int | |
7516 | dpif_netdev_ipf_dump_next(struct dpif *dpif, void *ipf_dump_ctx, char **dump) | |
7517 | { | |
7518 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
7519 | return ipf_dump_next(conntrack_ipf_ctx(dp->conntrack), ipf_dump_ctx, | |
7520 | dump); | |
7521 | } | |
7522 | ||
7523 | static int | |
7524 | dpif_netdev_ipf_dump_done(struct dpif *dpif OVS_UNUSED, void *ipf_dump_ctx) | |
7525 | { | |
7526 | return ipf_dump_done(ipf_dump_ctx); | |
7527 | ||
7528 | } | |
7529 | ||
7530 | const struct dpif_class dpif_netdev_class = { | |
7531 | "netdev", | |
7532 | true, /* cleanup_required */ | |
7533 | dpif_netdev_init, | |
7534 | dpif_netdev_enumerate, | |
7535 | dpif_netdev_port_open_type, | |
7536 | dpif_netdev_open, | |
7537 | dpif_netdev_close, | |
7538 | dpif_netdev_destroy, | |
7539 | dpif_netdev_run, | |
7540 | dpif_netdev_wait, | |
7541 | dpif_netdev_get_stats, | |
7542 | dpif_netdev_port_add, | |
7543 | dpif_netdev_port_del, | |
7544 | dpif_netdev_port_set_config, | |
7545 | dpif_netdev_port_query_by_number, | |
7546 | dpif_netdev_port_query_by_name, | |
7547 | NULL, /* port_get_pid */ | |
7548 | dpif_netdev_port_dump_start, | |
7549 | dpif_netdev_port_dump_next, | |
7550 | dpif_netdev_port_dump_done, | |
7551 | dpif_netdev_port_poll, | |
7552 | dpif_netdev_port_poll_wait, | |
7553 | dpif_netdev_flow_flush, | |
7554 | dpif_netdev_flow_dump_create, | |
7555 | dpif_netdev_flow_dump_destroy, | |
7556 | dpif_netdev_flow_dump_thread_create, | |
7557 | dpif_netdev_flow_dump_thread_destroy, | |
7558 | dpif_netdev_flow_dump_next, | |
7559 | dpif_netdev_operate, | |
7560 | NULL, /* recv_set */ | |
7561 | NULL, /* handlers_set */ | |
7562 | dpif_netdev_set_config, | |
7563 | dpif_netdev_queue_to_priority, | |
7564 | NULL, /* recv */ | |
7565 | NULL, /* recv_wait */ | |
7566 | NULL, /* recv_purge */ | |
7567 | dpif_netdev_register_dp_purge_cb, | |
7568 | dpif_netdev_register_upcall_cb, | |
7569 | dpif_netdev_enable_upcall, | |
7570 | dpif_netdev_disable_upcall, | |
7571 | dpif_netdev_get_datapath_version, | |
7572 | dpif_netdev_ct_dump_start, | |
7573 | dpif_netdev_ct_dump_next, | |
7574 | dpif_netdev_ct_dump_done, | |
7575 | dpif_netdev_ct_flush, | |
7576 | dpif_netdev_ct_set_maxconns, | |
7577 | dpif_netdev_ct_get_maxconns, | |
7578 | dpif_netdev_ct_get_nconns, | |
7579 | dpif_netdev_ct_set_tcp_seq_chk, | |
7580 | dpif_netdev_ct_get_tcp_seq_chk, | |
7581 | NULL, /* ct_set_limits */ | |
7582 | NULL, /* ct_get_limits */ | |
7583 | NULL, /* ct_del_limits */ | |
7584 | NULL, /* ct_set_timeout_policy */ | |
7585 | NULL, /* ct_get_timeout_policy */ | |
7586 | NULL, /* ct_del_timeout_policy */ | |
7587 | NULL, /* ct_timeout_policy_dump_start */ | |
7588 | NULL, /* ct_timeout_policy_dump_next */ | |
7589 | NULL, /* ct_timeout_policy_dump_done */ | |
7590 | NULL, /* ct_get_timeout_policy_name */ | |
7591 | dpif_netdev_ipf_set_enabled, | |
7592 | dpif_netdev_ipf_set_min_frag, | |
7593 | dpif_netdev_ipf_set_max_nfrags, | |
7594 | dpif_netdev_ipf_get_status, | |
7595 | dpif_netdev_ipf_dump_start, | |
7596 | dpif_netdev_ipf_dump_next, | |
7597 | dpif_netdev_ipf_dump_done, | |
7598 | dpif_netdev_meter_get_features, | |
7599 | dpif_netdev_meter_set, | |
7600 | dpif_netdev_meter_get, | |
7601 | dpif_netdev_meter_del, | |
7602 | }; | |
7603 | ||
7604 | static void | |
7605 | dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
7606 | const char *argv[], void *aux OVS_UNUSED) | |
7607 | { | |
7608 | struct dp_netdev_port *port; | |
7609 | struct dp_netdev *dp; | |
7610 | odp_port_t port_no; | |
7611 | ||
7612 | ovs_mutex_lock(&dp_netdev_mutex); | |
7613 | dp = shash_find_data(&dp_netdevs, argv[1]); | |
7614 | if (!dp || !dpif_netdev_class_is_dummy(dp->class)) { | |
7615 | ovs_mutex_unlock(&dp_netdev_mutex); | |
7616 | unixctl_command_reply_error(conn, "unknown datapath or not a dummy"); | |
7617 | return; | |
7618 | } | |
7619 | ovs_refcount_ref(&dp->ref_cnt); | |
7620 | ovs_mutex_unlock(&dp_netdev_mutex); | |
7621 | ||
7622 | ovs_mutex_lock(&dp->port_mutex); | |
7623 | if (get_port_by_name(dp, argv[2], &port)) { | |
7624 | unixctl_command_reply_error(conn, "unknown port"); | |
7625 | goto exit; | |
7626 | } | |
7627 | ||
7628 | port_no = u32_to_odp(atoi(argv[3])); | |
7629 | if (!port_no || port_no == ODPP_NONE) { | |
7630 | unixctl_command_reply_error(conn, "bad port number"); | |
7631 | goto exit; | |
7632 | } | |
7633 | if (dp_netdev_lookup_port(dp, port_no)) { | |
7634 | unixctl_command_reply_error(conn, "port number already in use"); | |
7635 | goto exit; | |
7636 | } | |
7637 | ||
7638 | /* Remove port. */ | |
7639 | hmap_remove(&dp->ports, &port->node); | |
7640 | reconfigure_datapath(dp); | |
7641 | ||
7642 | /* Reinsert with new port number. */ | |
7643 | port->port_no = port_no; | |
7644 | hmap_insert(&dp->ports, &port->node, hash_port_no(port_no)); | |
7645 | reconfigure_datapath(dp); | |
7646 | ||
7647 | seq_change(dp->port_seq); | |
7648 | unixctl_command_reply(conn, NULL); | |
7649 | ||
7650 | exit: | |
7651 | ovs_mutex_unlock(&dp->port_mutex); | |
7652 | dp_netdev_unref(dp); | |
7653 | } | |
7654 | ||
7655 | static void | |
7656 | dpif_dummy_register__(const char *type) | |
7657 | { | |
7658 | struct dpif_class *class; | |
7659 | ||
7660 | class = xmalloc(sizeof *class); | |
7661 | *class = dpif_netdev_class; | |
7662 | class->type = xstrdup(type); | |
7663 | dp_register_provider(class); | |
7664 | } | |
7665 | ||
7666 | static void | |
7667 | dpif_dummy_override(const char *type) | |
7668 | { | |
7669 | int error; | |
7670 | ||
7671 | /* | |
7672 | * Ignore EAFNOSUPPORT to allow --enable-dummy=system with | |
7673 | * a userland-only build. It's useful for testsuite. | |
7674 | */ | |
7675 | error = dp_unregister_provider(type); | |
7676 | if (error == 0 || error == EAFNOSUPPORT) { | |
7677 | dpif_dummy_register__(type); | |
7678 | } | |
7679 | } | |
7680 | ||
7681 | void | |
7682 | dpif_dummy_register(enum dummy_level level) | |
7683 | { | |
7684 | if (level == DUMMY_OVERRIDE_ALL) { | |
7685 | struct sset types; | |
7686 | const char *type; | |
7687 | ||
7688 | sset_init(&types); | |
7689 | dp_enumerate_types(&types); | |
7690 | SSET_FOR_EACH (type, &types) { | |
7691 | dpif_dummy_override(type); | |
7692 | } | |
7693 | sset_destroy(&types); | |
7694 | } else if (level == DUMMY_OVERRIDE_SYSTEM) { | |
7695 | dpif_dummy_override("system"); | |
7696 | } | |
7697 | ||
7698 | dpif_dummy_register__("dummy"); | |
7699 | ||
7700 | unixctl_command_register("dpif-dummy/change-port-number", | |
7701 | "dp port new-number", | |
7702 | 3, 3, dpif_dummy_change_port_number, NULL); | |
7703 | } | |
7704 | \f | |
7705 | /* Datapath Classifier. */ | |
7706 | ||
7707 | static void | |
7708 | dpcls_subtable_destroy_cb(struct dpcls_subtable *subtable) | |
7709 | { | |
7710 | cmap_destroy(&subtable->rules); | |
7711 | ovsrcu_postpone(free, subtable->mf_masks); | |
7712 | ovsrcu_postpone(free, subtable); | |
7713 | } | |
7714 | ||
7715 | /* Initializes 'cls' as a classifier that initially contains no classification | |
7716 | * rules. */ | |
7717 | static void | |
7718 | dpcls_init(struct dpcls *cls) | |
7719 | { | |
7720 | cmap_init(&cls->subtables_map); | |
7721 | pvector_init(&cls->subtables); | |
7722 | } | |
7723 | ||
7724 | static void | |
7725 | dpcls_destroy_subtable(struct dpcls *cls, struct dpcls_subtable *subtable) | |
7726 | { | |
7727 | VLOG_DBG("Destroying subtable %p for in_port %d", subtable, cls->in_port); | |
7728 | pvector_remove(&cls->subtables, subtable); | |
7729 | cmap_remove(&cls->subtables_map, &subtable->cmap_node, | |
7730 | subtable->mask.hash); | |
7731 | ovsrcu_postpone(dpcls_subtable_destroy_cb, subtable); | |
7732 | } | |
7733 | ||
7734 | /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the | |
7735 | * caller's responsibility. | |
7736 | * May only be called after all the readers have been terminated. */ | |
7737 | static void | |
7738 | dpcls_destroy(struct dpcls *cls) | |
7739 | { | |
7740 | if (cls) { | |
7741 | struct dpcls_subtable *subtable; | |
7742 | ||
7743 | CMAP_FOR_EACH (subtable, cmap_node, &cls->subtables_map) { | |
7744 | ovs_assert(cmap_count(&subtable->rules) == 0); | |
7745 | dpcls_destroy_subtable(cls, subtable); | |
7746 | } | |
7747 | cmap_destroy(&cls->subtables_map); | |
7748 | pvector_destroy(&cls->subtables); | |
7749 | } | |
7750 | } | |
7751 | ||
7752 | static struct dpcls_subtable * | |
7753 | dpcls_create_subtable(struct dpcls *cls, const struct netdev_flow_key *mask) | |
7754 | { | |
7755 | struct dpcls_subtable *subtable; | |
7756 | ||
7757 | /* Need to add one. */ | |
7758 | subtable = xmalloc(sizeof *subtable | |
7759 | - sizeof subtable->mask.mf + mask->len); | |
7760 | cmap_init(&subtable->rules); | |
7761 | subtable->hit_cnt = 0; | |
7762 | netdev_flow_key_clone(&subtable->mask, mask); | |
7763 | ||
7764 | /* The count of bits in the mask defines the space required for masks. | |
7765 | * Then call gen_masks() to create the appropriate masks, avoiding the cost | |
7766 | * of doing runtime calculations. */ | |
7767 | uint32_t unit0 = count_1bits(mask->mf.map.bits[0]); | |
7768 | uint32_t unit1 = count_1bits(mask->mf.map.bits[1]); | |
7769 | subtable->mf_bits_set_unit0 = unit0; | |
7770 | subtable->mf_bits_set_unit1 = unit1; | |
7771 | subtable->mf_masks = xmalloc(sizeof(uint64_t) * (unit0 + unit1)); | |
7772 | netdev_flow_key_gen_masks(mask, subtable->mf_masks, unit0, unit1); | |
7773 | ||
7774 | /* Probe for a specialized generic lookup function. */ | |
7775 | subtable->lookup_func = dpcls_subtable_generic_probe(unit0, unit1); | |
7776 | ||
7777 | /* If not set, assign generic lookup. Generic works for any miniflow. */ | |
7778 | if (!subtable->lookup_func) { | |
7779 | subtable->lookup_func = dpcls_subtable_lookup_generic; | |
7780 | } | |
7781 | ||
7782 | cmap_insert(&cls->subtables_map, &subtable->cmap_node, mask->hash); | |
7783 | /* Add the new subtable at the end of the pvector (with no hits yet) */ | |
7784 | pvector_insert(&cls->subtables, subtable, 0); | |
7785 | VLOG_DBG("Creating %"PRIuSIZE". subtable %p for in_port %d", | |
7786 | cmap_count(&cls->subtables_map), subtable, cls->in_port); | |
7787 | pvector_publish(&cls->subtables); | |
7788 | ||
7789 | return subtable; | |
7790 | } | |
7791 | ||
7792 | static inline struct dpcls_subtable * | |
7793 | dpcls_find_subtable(struct dpcls *cls, const struct netdev_flow_key *mask) | |
7794 | { | |
7795 | struct dpcls_subtable *subtable; | |
7796 | ||
7797 | CMAP_FOR_EACH_WITH_HASH (subtable, cmap_node, mask->hash, | |
7798 | &cls->subtables_map) { | |
7799 | if (netdev_flow_key_equal(&subtable->mask, mask)) { | |
7800 | return subtable; | |
7801 | } | |
7802 | } | |
7803 | return dpcls_create_subtable(cls, mask); | |
7804 | } | |
7805 | ||
7806 | ||
7807 | /* Periodically sort the dpcls subtable vectors according to hit counts */ | |
7808 | static void | |
7809 | dpcls_sort_subtable_vector(struct dpcls *cls) | |
7810 | { | |
7811 | struct pvector *pvec = &cls->subtables; | |
7812 | struct dpcls_subtable *subtable; | |
7813 | ||
7814 | PVECTOR_FOR_EACH (subtable, pvec) { | |
7815 | pvector_change_priority(pvec, subtable, subtable->hit_cnt); | |
7816 | subtable->hit_cnt = 0; | |
7817 | } | |
7818 | pvector_publish(pvec); | |
7819 | } | |
7820 | ||
7821 | static inline void | |
7822 | dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread *pmd, | |
7823 | struct polled_queue *poll_list, int poll_cnt) | |
7824 | { | |
7825 | struct dpcls *cls; | |
7826 | uint64_t tot_idle = 0, tot_proc = 0; | |
7827 | unsigned int pmd_load = 0; | |
7828 | ||
7829 | if (pmd->ctx.now > pmd->rxq_next_cycle_store) { | |
7830 | uint64_t curr_tsc; | |
7831 | struct pmd_auto_lb *pmd_alb = &pmd->dp->pmd_alb; | |
7832 | if (pmd_alb->is_enabled && !pmd->isolated | |
7833 | && (pmd->perf_stats.counters.n[PMD_CYCLES_ITER_IDLE] >= | |
7834 | pmd->prev_stats[PMD_CYCLES_ITER_IDLE]) | |
7835 | && (pmd->perf_stats.counters.n[PMD_CYCLES_ITER_BUSY] >= | |
7836 | pmd->prev_stats[PMD_CYCLES_ITER_BUSY])) | |
7837 | { | |
7838 | tot_idle = pmd->perf_stats.counters.n[PMD_CYCLES_ITER_IDLE] - | |
7839 | pmd->prev_stats[PMD_CYCLES_ITER_IDLE]; | |
7840 | tot_proc = pmd->perf_stats.counters.n[PMD_CYCLES_ITER_BUSY] - | |
7841 | pmd->prev_stats[PMD_CYCLES_ITER_BUSY]; | |
7842 | ||
7843 | if (tot_proc) { | |
7844 | pmd_load = ((tot_proc * 100) / (tot_idle + tot_proc)); | |
7845 | } | |
7846 | ||
7847 | if (pmd_load >= ALB_PMD_LOAD_THRESHOLD) { | |
7848 | atomic_count_inc(&pmd->pmd_overloaded); | |
7849 | } else { | |
7850 | atomic_count_set(&pmd->pmd_overloaded, 0); | |
7851 | } | |
7852 | } | |
7853 | ||
7854 | pmd->prev_stats[PMD_CYCLES_ITER_IDLE] = | |
7855 | pmd->perf_stats.counters.n[PMD_CYCLES_ITER_IDLE]; | |
7856 | pmd->prev_stats[PMD_CYCLES_ITER_BUSY] = | |
7857 | pmd->perf_stats.counters.n[PMD_CYCLES_ITER_BUSY]; | |
7858 | ||
7859 | /* Get the cycles that were used to process each queue and store. */ | |
7860 | for (unsigned i = 0; i < poll_cnt; i++) { | |
7861 | uint64_t rxq_cyc_curr = dp_netdev_rxq_get_cycles(poll_list[i].rxq, | |
7862 | RXQ_CYCLES_PROC_CURR); | |
7863 | dp_netdev_rxq_set_intrvl_cycles(poll_list[i].rxq, rxq_cyc_curr); | |
7864 | dp_netdev_rxq_set_cycles(poll_list[i].rxq, RXQ_CYCLES_PROC_CURR, | |
7865 | 0); | |
7866 | } | |
7867 | curr_tsc = cycles_counter_update(&pmd->perf_stats); | |
7868 | if (pmd->intrvl_tsc_prev) { | |
7869 | /* There is a prev timestamp, store a new intrvl cycle count. */ | |
7870 | atomic_store_relaxed(&pmd->intrvl_cycles, | |
7871 | curr_tsc - pmd->intrvl_tsc_prev); | |
7872 | } | |
7873 | pmd->intrvl_tsc_prev = curr_tsc; | |
7874 | /* Start new measuring interval */ | |
7875 | pmd->rxq_next_cycle_store = pmd->ctx.now + PMD_RXQ_INTERVAL_LEN; | |
7876 | } | |
7877 | ||
7878 | if (pmd->ctx.now > pmd->next_optimization) { | |
7879 | /* Try to obtain the flow lock to block out revalidator threads. | |
7880 | * If not possible, just try next time. */ | |
7881 | if (!ovs_mutex_trylock(&pmd->flow_mutex)) { | |
7882 | /* Optimize each classifier */ | |
7883 | CMAP_FOR_EACH (cls, node, &pmd->classifiers) { | |
7884 | dpcls_sort_subtable_vector(cls); | |
7885 | } | |
7886 | ovs_mutex_unlock(&pmd->flow_mutex); | |
7887 | /* Start new measuring interval */ | |
7888 | pmd->next_optimization = pmd->ctx.now | |
7889 | + DPCLS_OPTIMIZATION_INTERVAL; | |
7890 | } | |
7891 | } | |
7892 | } | |
7893 | ||
7894 | /* Insert 'rule' into 'cls'. */ | |
7895 | static void | |
7896 | dpcls_insert(struct dpcls *cls, struct dpcls_rule *rule, | |
7897 | const struct netdev_flow_key *mask) | |
7898 | { | |
7899 | struct dpcls_subtable *subtable = dpcls_find_subtable(cls, mask); | |
7900 | ||
7901 | /* Refer to subtable's mask, also for later removal. */ | |
7902 | rule->mask = &subtable->mask; | |
7903 | cmap_insert(&subtable->rules, &rule->cmap_node, rule->flow.hash); | |
7904 | } | |
7905 | ||
7906 | /* Removes 'rule' from 'cls', also destructing the 'rule'. */ | |
7907 | static void | |
7908 | dpcls_remove(struct dpcls *cls, struct dpcls_rule *rule) | |
7909 | { | |
7910 | struct dpcls_subtable *subtable; | |
7911 | ||
7912 | ovs_assert(rule->mask); | |
7913 | ||
7914 | /* Get subtable from reference in rule->mask. */ | |
7915 | INIT_CONTAINER(subtable, rule->mask, mask); | |
7916 | if (cmap_remove(&subtable->rules, &rule->cmap_node, rule->flow.hash) | |
7917 | == 0) { | |
7918 | /* Delete empty subtable. */ | |
7919 | dpcls_destroy_subtable(cls, subtable); | |
7920 | pvector_publish(&cls->subtables); | |
7921 | } | |
7922 | } | |
7923 | ||
7924 | /* Inner loop for mask generation of a unit, see netdev_flow_key_gen_masks. */ | |
7925 | static inline void | |
7926 | netdev_flow_key_gen_mask_unit(uint64_t iter, | |
7927 | const uint64_t count, | |
7928 | uint64_t *mf_masks) | |
7929 | { | |
7930 | int i; | |
7931 | for (i = 0; i < count; i++) { | |
7932 | uint64_t lowest_bit = (iter & -iter); | |
7933 | iter &= ~lowest_bit; | |
7934 | mf_masks[i] = (lowest_bit - 1); | |
7935 | } | |
7936 | /* Checks that count has covered all bits in the iter bitmap. */ | |
7937 | ovs_assert(iter == 0); | |
7938 | } | |
7939 | ||
7940 | /* Generate a mask for each block in the miniflow, based on the bits set. This | |
7941 | * allows easily masking packets with the generated array here, without | |
7942 | * calculations. This replaces runtime-calculating the masks. | |
7943 | * @param key The table to generate the mf_masks for | |
7944 | * @param mf_masks Pointer to a u64 array of at least *mf_bits* in size | |
7945 | * @param mf_bits_total Number of bits set in the whole miniflow (both units) | |
7946 | * @param mf_bits_unit0 Number of bits set in unit0 of the miniflow | |
7947 | */ | |
7948 | void | |
7949 | netdev_flow_key_gen_masks(const struct netdev_flow_key *tbl, | |
7950 | uint64_t *mf_masks, | |
7951 | const uint32_t mf_bits_u0, | |
7952 | const uint32_t mf_bits_u1) | |
7953 | { | |
7954 | uint64_t iter_u0 = tbl->mf.map.bits[0]; | |
7955 | uint64_t iter_u1 = tbl->mf.map.bits[1]; | |
7956 | ||
7957 | netdev_flow_key_gen_mask_unit(iter_u0, mf_bits_u0, &mf_masks[0]); | |
7958 | netdev_flow_key_gen_mask_unit(iter_u1, mf_bits_u1, &mf_masks[mf_bits_u0]); | |
7959 | } | |
7960 | ||
7961 | /* Returns true if 'target' satisfies 'key' in 'mask', that is, if each 1-bit | |
7962 | * in 'mask' the values in 'key' and 'target' are the same. */ | |
7963 | bool | |
7964 | dpcls_rule_matches_key(const struct dpcls_rule *rule, | |
7965 | const struct netdev_flow_key *target) | |
7966 | { | |
7967 | const uint64_t *keyp = miniflow_get_values(&rule->flow.mf); | |
7968 | const uint64_t *maskp = miniflow_get_values(&rule->mask->mf); | |
7969 | uint64_t value; | |
7970 | ||
7971 | NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value, target, rule->flow.mf.map) { | |
7972 | if (OVS_UNLIKELY((value & *maskp++) != *keyp++)) { | |
7973 | return false; | |
7974 | } | |
7975 | } | |
7976 | return true; | |
7977 | } | |
7978 | ||
7979 | /* For each miniflow in 'keys' performs a classifier lookup writing the result | |
7980 | * into the corresponding slot in 'rules'. If a particular entry in 'keys' is | |
7981 | * NULL it is skipped. | |
7982 | * | |
7983 | * This function is optimized for use in the userspace datapath and therefore | |
7984 | * does not implement a lot of features available in the standard | |
7985 | * classifier_lookup() function. Specifically, it does not implement | |
7986 | * priorities, instead returning any rule which matches the flow. | |
7987 | * | |
7988 | * Returns true if all miniflows found a corresponding rule. */ | |
7989 | static bool | |
7990 | dpcls_lookup(struct dpcls *cls, const struct netdev_flow_key *keys[], | |
7991 | struct dpcls_rule **rules, const size_t cnt, | |
7992 | int *num_lookups_p) | |
7993 | { | |
7994 | /* The received 'cnt' miniflows are the search-keys that will be processed | |
7995 | * to find a matching entry into the available subtables. | |
7996 | * The number of bits in map_type is equal to NETDEV_MAX_BURST. */ | |
7997 | #define MAP_BITS (sizeof(uint32_t) * CHAR_BIT) | |
7998 | BUILD_ASSERT_DECL(MAP_BITS >= NETDEV_MAX_BURST); | |
7999 | ||
8000 | struct dpcls_subtable *subtable; | |
8001 | uint32_t keys_map = TYPE_MAXIMUM(uint32_t); /* Set all bits. */ | |
8002 | ||
8003 | if (cnt != MAP_BITS) { | |
8004 | keys_map >>= MAP_BITS - cnt; /* Clear extra bits. */ | |
8005 | } | |
8006 | memset(rules, 0, cnt * sizeof *rules); | |
8007 | ||
8008 | int lookups_match = 0, subtable_pos = 1; | |
8009 | uint32_t found_map; | |
8010 | ||
8011 | /* The Datapath classifier - aka dpcls - is composed of subtables. | |
8012 | * Subtables are dynamically created as needed when new rules are inserted. | |
8013 | * Each subtable collects rules with matches on a specific subset of packet | |
8014 | * fields as defined by the subtable's mask. We proceed to process every | |
8015 | * search-key against each subtable, but when a match is found for a | |
8016 | * search-key, the search for that key can stop because the rules are | |
8017 | * non-overlapping. */ | |
8018 | PVECTOR_FOR_EACH (subtable, &cls->subtables) { | |
8019 | /* Call the subtable specific lookup function. */ | |
8020 | found_map = subtable->lookup_func(subtable, keys_map, keys, rules); | |
8021 | ||
8022 | /* Count the number of subtables searched for this packet match. This | |
8023 | * estimates the "spread" of subtables looked at per matched packet. */ | |
8024 | uint32_t pkts_matched = count_1bits(found_map); | |
8025 | lookups_match += pkts_matched * subtable_pos; | |
8026 | ||
8027 | /* Clear the found rules, and return early if all packets are found. */ | |
8028 | keys_map &= ~found_map; | |
8029 | if (!keys_map) { | |
8030 | if (num_lookups_p) { | |
8031 | *num_lookups_p = lookups_match; | |
8032 | } | |
8033 | return true; | |
8034 | } | |
8035 | subtable_pos++; | |
8036 | } | |
8037 | ||
8038 | if (num_lookups_p) { | |
8039 | *num_lookups_p = lookups_match; | |
8040 | } | |
8041 | return false; | |
8042 | } |