]>
Commit | Line | Data |
---|---|---|
72865317 | 1 | /* |
f582b6df | 2 | * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2016, 2017 Nicira, Inc. |
72865317 BP |
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> | |
db73f716 | 18 | #include "dpif-netdev.h" |
72865317 | 19 | |
72865317 BP |
20 | #include <ctype.h> |
21 | #include <errno.h> | |
22 | #include <fcntl.h> | |
23 | #include <inttypes.h> | |
7f3adc00 | 24 | #include <net/if.h> |
7daedce4 | 25 | #include <netinet/in.h> |
cdee00fd | 26 | #include <stdint.h> |
72865317 BP |
27 | #include <stdlib.h> |
28 | #include <string.h> | |
29 | #include <sys/ioctl.h> | |
7daedce4 | 30 | #include <sys/socket.h> |
72865317 | 31 | #include <sys/stat.h> |
72865317 BP |
32 | #include <unistd.h> |
33 | ||
01961bbd DDP |
34 | #ifdef DPDK_NETDEV |
35 | #include <rte_cycles.h> | |
36 | #endif | |
37 | ||
9f861c91 | 38 | #include "bitmap.h" |
59e6d833 | 39 | #include "cmap.h" |
5cf3edb3 | 40 | #include "conntrack.h" |
7daedce4 | 41 | #include "coverage.h" |
4d4e68ed | 42 | #include "ct-dpif.h" |
72865317 | 43 | #include "csum.h" |
e14deea0 | 44 | #include "dp-packet.h" |
614c4892 | 45 | #include "dpif.h" |
72865317 | 46 | #include "dpif-provider.h" |
614c4892 | 47 | #include "dummy.h" |
afae68b1 | 48 | #include "fat-rwlock.h" |
72865317 | 49 | #include "flow.h" |
762d146a | 50 | #include "hmapx.h" |
140dd699 | 51 | #include "id-pool.h" |
6c3eee82 | 52 | #include "latch.h" |
72865317 | 53 | #include "netdev.h" |
de281153 | 54 | #include "netdev-vport.h" |
cdee00fd | 55 | #include "netlink.h" |
f094af7b | 56 | #include "odp-execute.h" |
72865317 | 57 | #include "odp-util.h" |
25d436fb BW |
58 | #include "openvswitch/dynamic-string.h" |
59 | #include "openvswitch/list.h" | |
60 | #include "openvswitch/match.h" | |
61 | #include "openvswitch/ofp-print.h" | |
3eb67853 | 62 | #include "openvswitch/ofp-util.h" |
64c96779 | 63 | #include "openvswitch/ofpbuf.h" |
3eb67853 | 64 | #include "openvswitch/shash.h" |
25d436fb | 65 | #include "openvswitch/vlog.h" |
5a034064 | 66 | #include "ovs-numa.h" |
61e7deb1 | 67 | #include "ovs-rcu.h" |
72865317 | 68 | #include "packets.h" |
fd016ae3 | 69 | #include "openvswitch/poll-loop.h" |
0de8783a | 70 | #include "pvector.h" |
26c6b6cd | 71 | #include "random.h" |
d33ed218 | 72 | #include "seq.h" |
3eb67853 | 73 | #include "smap.h" |
0cbfe35d | 74 | #include "sset.h" |
72865317 | 75 | #include "timeval.h" |
53902038 | 76 | #include "tnl-neigh-cache.h" |
7f9b8504 | 77 | #include "tnl-ports.h" |
74cc3969 | 78 | #include "unixctl.h" |
72865317 | 79 | #include "util.h" |
7daedce4 | 80 | |
d98e6007 | 81 | VLOG_DEFINE_THIS_MODULE(dpif_netdev); |
72865317 | 82 | |
8bb113da | 83 | #define FLOW_DUMP_MAX_BATCH 50 |
adcf00ba | 84 | /* Use per thread recirc_depth to prevent recirculation loop. */ |
3f9d3836 | 85 | #define MAX_RECIRC_DEPTH 6 |
adcf00ba | 86 | DEFINE_STATIC_PER_THREAD_DATA(uint32_t, recirc_depth, 0) |
e4cfed38 | 87 | |
72865317 | 88 | /* Configuration parameters. */ |
72865317 | 89 | enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */ |
4b27db64 JR |
90 | enum { MAX_METERS = 65536 }; /* Maximum number of meters. */ |
91 | enum { MAX_BANDS = 8 }; /* Maximum number of bands / meter. */ | |
92 | enum { N_METER_LOCKS = 64 }; /* Maximum number of meters. */ | |
72865317 | 93 | |
8a4e3a85 BP |
94 | /* Protects against changes to 'dp_netdevs'. */ |
95 | static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER; | |
96 | ||
97 | /* Contains all 'struct dp_netdev's. */ | |
98 | static struct shash dp_netdevs OVS_GUARDED_BY(dp_netdev_mutex) | |
99 | = SHASH_INITIALIZER(&dp_netdevs); | |
100 | ||
623540e4 | 101 | static struct vlog_rate_limit upcall_rl = VLOG_RATE_LIMIT_INIT(600, 600); |
6b31e073 | 102 | |
5cf3edb3 | 103 | #define DP_NETDEV_CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \ |
4cddb1f0 DB |
104 | | CS_INVALID | CS_REPLY_DIR | CS_TRACKED \ |
105 | | CS_SRC_NAT | CS_DST_NAT) | |
5cf3edb3 DDP |
106 | #define DP_NETDEV_CS_UNSUPPORTED_MASK (~(uint32_t)DP_NETDEV_CS_SUPPORTED_MASK) |
107 | ||
2494ccd7 | 108 | static struct odp_support dp_netdev_support = { |
f0fb825a | 109 | .max_vlan_headers = SIZE_MAX, |
2494ccd7 JS |
110 | .max_mpls_depth = SIZE_MAX, |
111 | .recirc = true, | |
5cf3edb3 DDP |
112 | .ct_state = true, |
113 | .ct_zone = true, | |
114 | .ct_mark = true, | |
115 | .ct_label = true, | |
2575df07 JP |
116 | .ct_state_nat = true, |
117 | .ct_orig_tuple = true, | |
118 | .ct_orig_tuple6 = true, | |
2494ccd7 JS |
119 | }; |
120 | ||
79df317f | 121 | /* Stores a miniflow with inline values */ |
9bbf1c3d | 122 | |
9bbf1c3d | 123 | struct netdev_flow_key { |
caeb4906 JR |
124 | uint32_t hash; /* Hash function differs for different users. */ |
125 | uint32_t len; /* Length of the following miniflow (incl. map). */ | |
0de8783a | 126 | struct miniflow mf; |
8fd47924 | 127 | uint64_t buf[FLOW_MAX_PACKET_U64S]; |
9bbf1c3d DDP |
128 | }; |
129 | ||
130 | /* Exact match cache for frequently used flows | |
131 | * | |
132 | * The cache uses a 32-bit hash of the packet (which can be the RSS hash) to | |
133 | * search its entries for a miniflow that matches exactly the miniflow of the | |
0de8783a | 134 | * packet. It stores the 'dpcls_rule' (rule) that matches the miniflow. |
9bbf1c3d DDP |
135 | * |
136 | * A cache entry holds a reference to its 'dp_netdev_flow'. | |
137 | * | |
138 | * A miniflow with a given hash can be in one of EM_FLOW_HASH_SEGS different | |
139 | * entries. The 32-bit hash is split into EM_FLOW_HASH_SEGS values (each of | |
140 | * them is EM_FLOW_HASH_SHIFT bits wide and the remainder is thrown away). Each | |
141 | * value is the index of a cache entry where the miniflow could be. | |
142 | * | |
143 | * | |
144 | * Thread-safety | |
145 | * ============= | |
146 | * | |
147 | * Each pmd_thread has its own private exact match cache. | |
148 | * If dp_netdev_input is not called from a pmd thread, a mutex is used. | |
149 | */ | |
150 | ||
fc82e877 | 151 | #define EM_FLOW_HASH_SHIFT 13 |
9bbf1c3d DDP |
152 | #define EM_FLOW_HASH_ENTRIES (1u << EM_FLOW_HASH_SHIFT) |
153 | #define EM_FLOW_HASH_MASK (EM_FLOW_HASH_ENTRIES - 1) | |
154 | #define EM_FLOW_HASH_SEGS 2 | |
155 | ||
4c30b246 CL |
156 | /* Default EMC insert probability is 1 / DEFAULT_EM_FLOW_INSERT_INV_PROB */ |
157 | #define DEFAULT_EM_FLOW_INSERT_INV_PROB 100 | |
158 | #define DEFAULT_EM_FLOW_INSERT_MIN (UINT32_MAX / \ | |
159 | DEFAULT_EM_FLOW_INSERT_INV_PROB) | |
160 | ||
9bbf1c3d | 161 | struct emc_entry { |
9bbf1c3d | 162 | struct dp_netdev_flow *flow; |
0de8783a | 163 | struct netdev_flow_key key; /* key.hash used for emc hash value. */ |
9bbf1c3d DDP |
164 | }; |
165 | ||
166 | struct emc_cache { | |
167 | struct emc_entry entries[EM_FLOW_HASH_ENTRIES]; | |
67ad54cb | 168 | int sweep_idx; /* For emc_cache_slow_sweep(). */ |
9bbf1c3d DDP |
169 | }; |
170 | ||
171 | /* Iterate in the exact match cache through every entry that might contain a | |
172 | * miniflow with hash 'HASH'. */ | |
173 | #define EMC_FOR_EACH_POS_WITH_HASH(EMC, CURRENT_ENTRY, HASH) \ | |
174 | for (uint32_t i__ = 0, srch_hash__ = (HASH); \ | |
175 | (CURRENT_ENTRY) = &(EMC)->entries[srch_hash__ & EM_FLOW_HASH_MASK], \ | |
176 | i__ < EM_FLOW_HASH_SEGS; \ | |
177 | i__++, srch_hash__ >>= EM_FLOW_HASH_SHIFT) | |
0de8783a JR |
178 | \f |
179 | /* Simple non-wildcarding single-priority classifier. */ | |
180 | ||
3453b4d6 JS |
181 | /* Time in ms between successive optimizations of the dpcls subtable vector */ |
182 | #define DPCLS_OPTIMIZATION_INTERVAL 1000 | |
183 | ||
4809891b KT |
184 | /* Time in ms of the interval in which rxq processing cycles used in |
185 | * rxq to pmd assignments is measured and stored. */ | |
186 | #define PMD_RXQ_INTERVAL_LEN 10000 | |
187 | ||
c59e759f KT |
188 | /* Number of intervals for which cycles are stored |
189 | * and used during rxq to pmd assignment. */ | |
190 | #define PMD_RXQ_INTERVAL_MAX 6 | |
191 | ||
0de8783a | 192 | struct dpcls { |
3453b4d6 JS |
193 | struct cmap_node node; /* Within dp_netdev_pmd_thread.classifiers */ |
194 | odp_port_t in_port; | |
0de8783a | 195 | struct cmap subtables_map; |
da9cfca6 | 196 | struct pvector subtables; |
0de8783a | 197 | }; |
9bbf1c3d | 198 | |
0de8783a JR |
199 | /* A rule to be inserted to the classifier. */ |
200 | struct dpcls_rule { | |
201 | struct cmap_node cmap_node; /* Within struct dpcls_subtable 'rules'. */ | |
202 | struct netdev_flow_key *mask; /* Subtable's mask. */ | |
203 | struct netdev_flow_key flow; /* Matching key. */ | |
204 | /* 'flow' must be the last field, additional space is allocated here. */ | |
205 | }; | |
206 | ||
207 | static void dpcls_init(struct dpcls *); | |
208 | static void dpcls_destroy(struct dpcls *); | |
3453b4d6 | 209 | static void dpcls_sort_subtable_vector(struct dpcls *); |
0de8783a JR |
210 | static void dpcls_insert(struct dpcls *, struct dpcls_rule *, |
211 | const struct netdev_flow_key *mask); | |
212 | static void dpcls_remove(struct dpcls *, struct dpcls_rule *); | |
3453b4d6 | 213 | static bool dpcls_lookup(struct dpcls *cls, |
0de8783a | 214 | const struct netdev_flow_key keys[], |
3453b4d6 JS |
215 | struct dpcls_rule **rules, size_t cnt, |
216 | int *num_lookups_p); | |
0de8783a | 217 | \f |
4b27db64 JR |
218 | /* Set of supported meter flags */ |
219 | #define DP_SUPPORTED_METER_FLAGS_MASK \ | |
220 | (OFPMF13_STATS | OFPMF13_PKTPS | OFPMF13_KBPS | OFPMF13_BURST) | |
221 | ||
222 | /* Set of supported meter band types */ | |
223 | #define DP_SUPPORTED_METER_BAND_TYPES \ | |
224 | ( 1 << OFPMBT13_DROP ) | |
225 | ||
226 | struct dp_meter_band { | |
227 | struct ofputil_meter_band up; /* type, prec_level, pad, rate, burst_size */ | |
228 | uint32_t bucket; /* In 1/1000 packets (for PKTPS), or in bits (for KBPS) */ | |
229 | uint64_t packet_count; | |
230 | uint64_t byte_count; | |
231 | }; | |
232 | ||
233 | struct dp_meter { | |
234 | uint16_t flags; | |
235 | uint16_t n_bands; | |
236 | uint32_t max_delta_t; | |
237 | uint64_t used; | |
238 | uint64_t packet_count; | |
239 | uint64_t byte_count; | |
240 | struct dp_meter_band bands[]; | |
241 | }; | |
242 | ||
8a4e3a85 BP |
243 | /* Datapath based on the network device interface from netdev.h. |
244 | * | |
245 | * | |
246 | * Thread-safety | |
247 | * ============= | |
248 | * | |
249 | * Some members, marked 'const', are immutable. Accessing other members | |
250 | * requires synchronization, as noted in more detail below. | |
251 | * | |
252 | * Acquisition order is, from outermost to innermost: | |
253 | * | |
254 | * dp_netdev_mutex (global) | |
59e6d833 | 255 | * port_mutex |
d0cca6c3 | 256 | * non_pmd_mutex |
8a4e3a85 | 257 | */ |
72865317 | 258 | struct dp_netdev { |
8a4e3a85 BP |
259 | const struct dpif_class *const class; |
260 | const char *const name; | |
6b31e073 | 261 | struct dpif *dpif; |
6a8267c5 BP |
262 | struct ovs_refcount ref_cnt; |
263 | atomic_flag destroyed; | |
72865317 | 264 | |
8a4e3a85 BP |
265 | /* Ports. |
266 | * | |
e9985d6a DDP |
267 | * Any lookup into 'ports' or any access to the dp_netdev_ports found |
268 | * through 'ports' requires taking 'port_mutex'. */ | |
59e6d833 | 269 | struct ovs_mutex port_mutex; |
e9985d6a | 270 | struct hmap ports; |
d33ed218 | 271 | struct seq *port_seq; /* Incremented whenever a port changes. */ |
6c3eee82 | 272 | |
4b27db64 JR |
273 | /* Meters. */ |
274 | struct ovs_mutex meter_locks[N_METER_LOCKS]; | |
275 | struct dp_meter *meters[MAX_METERS]; /* Meter bands. */ | |
4b27db64 | 276 | |
65dcf3da BB |
277 | /* Probability of EMC insertions is a factor of 'emc_insert_min'.*/ |
278 | OVS_ALIGNED_VAR(CACHE_LINE_SIZE) atomic_uint32_t emc_insert_min; | |
279 | ||
6b31e073 RW |
280 | /* Protects access to ofproto-dpif-upcall interface during revalidator |
281 | * thread synchronization. */ | |
282 | struct fat_rwlock upcall_rwlock; | |
623540e4 EJ |
283 | upcall_callback *upcall_cb; /* Callback function for executing upcalls. */ |
284 | void *upcall_aux; | |
6b31e073 | 285 | |
e4e74c3a AW |
286 | /* Callback function for notifying the purging of dp flows (during |
287 | * reseting pmd deletion). */ | |
288 | dp_purge_callback *dp_purge_cb; | |
289 | void *dp_purge_aux; | |
290 | ||
65f13b50 AW |
291 | /* Stores all 'struct dp_netdev_pmd_thread's. */ |
292 | struct cmap poll_threads; | |
140dd699 IM |
293 | /* id pool for per thread static_tx_qid. */ |
294 | struct id_pool *tx_qid_pool; | |
295 | struct ovs_mutex tx_qid_pool_mutex; | |
65f13b50 AW |
296 | |
297 | /* Protects the access of the 'struct dp_netdev_pmd_thread' | |
298 | * instance for non-pmd thread. */ | |
299 | struct ovs_mutex non_pmd_mutex; | |
300 | ||
301 | /* Each pmd thread will store its pointer to | |
302 | * 'struct dp_netdev_pmd_thread' in 'per_pmd_key'. */ | |
303 | ovsthread_key_t per_pmd_key; | |
f2eee189 | 304 | |
a6a426d6 IM |
305 | struct seq *reconfigure_seq; |
306 | uint64_t last_reconfigure_seq; | |
307 | ||
a14b8947 | 308 | /* Cpu mask for pin of pmd threads. */ |
f2eee189 | 309 | char *pmd_cmask; |
6e3c6fa4 | 310 | |
a36de779 | 311 | uint64_t last_tnl_conf_seq; |
5cf3edb3 DDP |
312 | |
313 | struct conntrack conntrack; | |
72865317 BP |
314 | }; |
315 | ||
4b27db64 JR |
316 | static void meter_lock(const struct dp_netdev *dp, uint32_t meter_id) |
317 | OVS_ACQUIRES(dp->meter_locks[meter_id % N_METER_LOCKS]) | |
318 | { | |
319 | ovs_mutex_lock(&dp->meter_locks[meter_id % N_METER_LOCKS]); | |
320 | } | |
321 | ||
322 | static void meter_unlock(const struct dp_netdev *dp, uint32_t meter_id) | |
323 | OVS_RELEASES(dp->meter_locks[meter_id % N_METER_LOCKS]) | |
324 | { | |
325 | ovs_mutex_unlock(&dp->meter_locks[meter_id % N_METER_LOCKS]); | |
326 | } | |
327 | ||
328 | ||
8a4e3a85 | 329 | static struct dp_netdev_port *dp_netdev_lookup_port(const struct dp_netdev *dp, |
e9985d6a DDP |
330 | odp_port_t) |
331 | OVS_REQUIRES(dp->port_mutex); | |
ff073a71 | 332 | |
51852a57 | 333 | enum dp_stat_type { |
abcf3ef4 DDP |
334 | DP_STAT_EXACT_HIT, /* Packets that had an exact match (emc). */ |
335 | DP_STAT_MASKED_HIT, /* Packets that matched in the flow table. */ | |
51852a57 BP |
336 | DP_STAT_MISS, /* Packets that did not match. */ |
337 | DP_STAT_LOST, /* Packets not passed up to the client. */ | |
3453b4d6 JS |
338 | DP_STAT_LOOKUP_HIT, /* Number of subtable lookups for flow table |
339 | hits */ | |
51852a57 BP |
340 | DP_N_STATS |
341 | }; | |
342 | ||
55e3ca97 | 343 | enum pmd_cycles_counter_type { |
a2ac666d CL |
344 | PMD_CYCLES_IDLE, /* Cycles spent idle or unsuccessful polling */ |
345 | PMD_CYCLES_PROCESSING, /* Cycles spent successfully polling and | |
346 | * processing polled packets */ | |
55e3ca97 DDP |
347 | PMD_N_CYCLES |
348 | }; | |
349 | ||
c59e759f KT |
350 | enum rxq_cycles_counter_type { |
351 | RXQ_CYCLES_PROC_CURR, /* Cycles spent successfully polling and | |
352 | processing packets during the current | |
353 | interval. */ | |
354 | RXQ_CYCLES_PROC_HIST, /* Total cycles of all intervals that are used | |
355 | during rxq to pmd assignment. */ | |
356 | RXQ_N_CYCLES | |
357 | }; | |
358 | ||
324c8374 IM |
359 | #define XPS_TIMEOUT_MS 500LL |
360 | ||
3eb67853 IM |
361 | /* Contained by struct dp_netdev_port's 'rxqs' member. */ |
362 | struct dp_netdev_rxq { | |
947dc567 DDP |
363 | struct dp_netdev_port *port; |
364 | struct netdev_rxq *rx; | |
365 | unsigned core_id; /* Core to which this queue should be | |
366 | pinned. OVS_CORE_UNSPEC if the | |
367 | queue doesn't need to be pinned to a | |
368 | particular core. */ | |
ee42dd70 | 369 | unsigned intrvl_idx; /* Write index for 'cycles_intrvl'. */ |
47a45d86 | 370 | struct dp_netdev_pmd_thread *pmd; /* pmd thread that polls this queue. */ |
c59e759f KT |
371 | |
372 | /* Counters of cycles spent successfully polling and processing pkts. */ | |
373 | atomic_ullong cycles[RXQ_N_CYCLES]; | |
374 | /* We store PMD_RXQ_INTERVAL_MAX intervals of data for an rxq and then | |
375 | sum them to yield the cycles used for an rxq. */ | |
376 | atomic_ullong cycles_intrvl[PMD_RXQ_INTERVAL_MAX]; | |
3eb67853 IM |
377 | }; |
378 | ||
72865317 BP |
379 | /* A port in a netdev-based datapath. */ |
380 | struct dp_netdev_port { | |
35303d71 | 381 | odp_port_t port_no; |
ca62bb16 BB |
382 | bool dynamic_txqs; /* If true XPS will be used. */ |
383 | bool need_reconfigure; /* True if we should reconfigure netdev. */ | |
72865317 | 384 | struct netdev *netdev; |
e9985d6a | 385 | struct hmap_node node; /* Node in dp_netdev's 'ports'. */ |
4b609110 | 386 | struct netdev_saved_flags *sf; |
3eb67853 | 387 | struct dp_netdev_rxq *rxqs; |
85a4f238 | 388 | unsigned n_rxq; /* Number of elements in 'rxqs' */ |
47a45d86 | 389 | unsigned *txq_used; /* Number of threads that use each tx queue. */ |
324c8374 | 390 | struct ovs_mutex txq_used_mutex; |
0cbfe35d | 391 | char *type; /* Port type as requested by user. */ |
3eb67853 | 392 | char *rxq_affinity_list; /* Requested affinity of rx queues. */ |
72865317 BP |
393 | }; |
394 | ||
1c1e46ed AW |
395 | /* Contained by struct dp_netdev_flow's 'stats' member. */ |
396 | struct dp_netdev_flow_stats { | |
eb94da30 DDP |
397 | atomic_llong used; /* Last used time, in monotonic msecs. */ |
398 | atomic_ullong packet_count; /* Number of packets matched. */ | |
399 | atomic_ullong byte_count; /* Number of bytes matched. */ | |
400 | atomic_uint16_t tcp_flags; /* Bitwise-OR of seen tcp_flags values. */ | |
1c1e46ed AW |
401 | }; |
402 | ||
403 | /* A flow in 'dp_netdev_pmd_thread's 'flow_table'. | |
8a4e3a85 BP |
404 | * |
405 | * | |
406 | * Thread-safety | |
407 | * ============= | |
408 | * | |
409 | * Except near the beginning or ending of its lifespan, rule 'rule' belongs to | |
1c1e46ed | 410 | * its pmd thread's classifier. The text below calls this classifier 'cls'. |
8a4e3a85 BP |
411 | * |
412 | * Motivation | |
413 | * ---------- | |
414 | * | |
415 | * The thread safety rules described here for "struct dp_netdev_flow" are | |
416 | * motivated by two goals: | |
417 | * | |
418 | * - Prevent threads that read members of "struct dp_netdev_flow" from | |
419 | * reading bad data due to changes by some thread concurrently modifying | |
420 | * those members. | |
421 | * | |
422 | * - Prevent two threads making changes to members of a given "struct | |
423 | * dp_netdev_flow" from interfering with each other. | |
424 | * | |
425 | * | |
426 | * Rules | |
427 | * ----- | |
428 | * | |
ed79f89a DDP |
429 | * A flow 'flow' may be accessed without a risk of being freed during an RCU |
430 | * grace period. Code that needs to hold onto a flow for a while | |
431 | * should try incrementing 'flow->ref_cnt' with dp_netdev_flow_ref(). | |
8a4e3a85 BP |
432 | * |
433 | * 'flow->ref_cnt' protects 'flow' from being freed. It doesn't protect the | |
ed79f89a DDP |
434 | * flow from being deleted from 'cls' and it doesn't protect members of 'flow' |
435 | * from modification. | |
8a4e3a85 BP |
436 | * |
437 | * Some members, marked 'const', are immutable. Accessing other members | |
438 | * requires synchronization, as noted in more detail below. | |
439 | */ | |
72865317 | 440 | struct dp_netdev_flow { |
11e5cf1f | 441 | const struct flow flow; /* Unmasked flow that created this entry. */ |
8a4e3a85 | 442 | /* Hash table index by unmasked flow. */ |
1c1e46ed AW |
443 | const struct cmap_node node; /* In owning dp_netdev_pmd_thread's */ |
444 | /* 'flow_table'. */ | |
70e5ed6f | 445 | const ovs_u128 ufid; /* Unique flow identifier. */ |
bd5131ba | 446 | const unsigned pmd_id; /* The 'core_id' of pmd thread owning this */ |
1c1e46ed | 447 | /* flow. */ |
72865317 | 448 | |
ed79f89a DDP |
449 | /* Number of references. |
450 | * The classifier owns one reference. | |
451 | * Any thread trying to keep a rule from being freed should hold its own | |
452 | * reference. */ | |
453 | struct ovs_refcount ref_cnt; | |
454 | ||
11e5cf1f DDP |
455 | bool dead; |
456 | ||
1c1e46ed AW |
457 | /* Statistics. */ |
458 | struct dp_netdev_flow_stats stats; | |
8a4e3a85 | 459 | |
45c626a3 | 460 | /* Actions. */ |
61e7deb1 | 461 | OVSRCU_TYPE(struct dp_netdev_actions *) actions; |
0de8783a | 462 | |
11e5cf1f DDP |
463 | /* While processing a group of input packets, the datapath uses the next |
464 | * member to store a pointer to the output batch for the flow. It is | |
465 | * reset after the batch has been sent out (See dp_netdev_queue_batches(), | |
f7ce4811 PS |
466 | * packet_batch_per_flow_init() and packet_batch_per_flow_execute()). */ |
467 | struct packet_batch_per_flow *batch; | |
11e5cf1f | 468 | |
0de8783a JR |
469 | /* Packet classification. */ |
470 | struct dpcls_rule cr; /* In owning dp_netdev's 'cls'. */ | |
471 | /* 'cr' must be the last member. */ | |
72865317 BP |
472 | }; |
473 | ||
ed79f89a | 474 | static void dp_netdev_flow_unref(struct dp_netdev_flow *); |
9bbf1c3d | 475 | static bool dp_netdev_flow_ref(struct dp_netdev_flow *); |
70e5ed6f | 476 | static int dpif_netdev_flow_from_nlattrs(const struct nlattr *, uint32_t, |
f0fb825a | 477 | struct flow *, bool); |
8a4e3a85 | 478 | |
a84cb64a BP |
479 | /* A set of datapath actions within a "struct dp_netdev_flow". |
480 | * | |
481 | * | |
482 | * Thread-safety | |
483 | * ============= | |
484 | * | |
45c626a3 | 485 | * A struct dp_netdev_actions 'actions' is protected with RCU. */ |
a84cb64a | 486 | struct dp_netdev_actions { |
a84cb64a BP |
487 | /* These members are immutable: they do not change during the struct's |
488 | * lifetime. */ | |
a84cb64a | 489 | unsigned int size; /* Size of 'actions', in bytes. */ |
9ff55ae2 | 490 | struct nlattr actions[]; /* Sequence of OVS_ACTION_ATTR_* attributes. */ |
a84cb64a BP |
491 | }; |
492 | ||
493 | struct dp_netdev_actions *dp_netdev_actions_create(const struct nlattr *, | |
494 | size_t); | |
61e7deb1 BP |
495 | struct dp_netdev_actions *dp_netdev_flow_get_actions( |
496 | const struct dp_netdev_flow *); | |
497 | static void dp_netdev_actions_free(struct dp_netdev_actions *); | |
a84cb64a | 498 | |
1c1e46ed AW |
499 | /* Contained by struct dp_netdev_pmd_thread's 'stats' member. */ |
500 | struct dp_netdev_pmd_stats { | |
501 | /* Indexed by DP_STAT_*. */ | |
eb94da30 | 502 | atomic_ullong n[DP_N_STATS]; |
1c1e46ed AW |
503 | }; |
504 | ||
55e3ca97 DDP |
505 | /* Contained by struct dp_netdev_pmd_thread's 'cycle' member. */ |
506 | struct dp_netdev_pmd_cycles { | |
507 | /* Indexed by PMD_CYCLES_*. */ | |
508 | atomic_ullong n[PMD_N_CYCLES]; | |
509 | }; | |
510 | ||
947dc567 | 511 | struct polled_queue { |
922b28d4 | 512 | struct dp_netdev_rxq *rxq; |
947dc567 DDP |
513 | odp_port_t port_no; |
514 | }; | |
515 | ||
ae7ad0a1 IM |
516 | /* Contained by struct dp_netdev_pmd_thread's 'poll_list' member. */ |
517 | struct rxq_poll { | |
947dc567 DDP |
518 | struct dp_netdev_rxq *rxq; |
519 | struct hmap_node node; | |
ae7ad0a1 IM |
520 | }; |
521 | ||
57eebbb4 DDP |
522 | /* Contained by struct dp_netdev_pmd_thread's 'send_port_cache', |
523 | * 'tnl_port_cache' or 'tx_ports'. */ | |
d0cca6c3 | 524 | struct tx_port { |
324c8374 IM |
525 | struct dp_netdev_port *port; |
526 | int qid; | |
527 | long long last_used; | |
d0cca6c3 DDP |
528 | struct hmap_node node; |
529 | }; | |
530 | ||
e4cfed38 PS |
531 | /* PMD: Poll modes drivers. PMD accesses devices via polling to eliminate |
532 | * the performance overhead of interrupt processing. Therefore netdev can | |
533 | * not implement rx-wait for these devices. dpif-netdev needs to poll | |
534 | * these device to check for recv buffer. pmd-thread does polling for | |
1c1e46ed | 535 | * devices assigned to itself. |
e4cfed38 PS |
536 | * |
537 | * DPDK used PMD for accessing NIC. | |
538 | * | |
65f13b50 AW |
539 | * Note, instance with cpu core id NON_PMD_CORE_ID will be reserved for |
540 | * I/O of all non-pmd threads. There will be no actual thread created | |
541 | * for the instance. | |
1c1e46ed | 542 | * |
1859876c BB |
543 | * Each struct has its own flow cache and classifier per managed ingress port. |
544 | * For packets received on ingress port, a look up is done on corresponding PMD | |
545 | * thread's flow cache and in case of a miss, lookup is performed in the | |
546 | * corresponding classifier of port. Packets are executed with the found | |
547 | * actions in either case. | |
1c1e46ed | 548 | * */ |
65f13b50 | 549 | struct dp_netdev_pmd_thread { |
d9d73f84 IM |
550 | struct dp_netdev *dp; |
551 | struct ovs_refcount ref_cnt; /* Every reference must be refcount'ed. */ | |
552 | struct cmap_node node; /* In 'dp->poll_threads'. */ | |
553 | ||
554 | pthread_cond_t cond; /* For synchronizing pmd thread reload. */ | |
555 | struct ovs_mutex cond_mutex; /* Mutex for condition variable. */ | |
accf8626 | 556 | |
65f13b50 AW |
557 | /* Per thread exact-match cache. Note, the instance for cpu core |
558 | * NON_PMD_CORE_ID can be accessed by multiple threads, and thusly | |
d0cca6c3 DDP |
559 | * need to be protected by 'non_pmd_mutex'. Every other instance |
560 | * will only be accessed by its own pmd thread. */ | |
d9d73f84 | 561 | struct emc_cache flow_cache; |
1c1e46ed | 562 | |
3453b4d6 | 563 | /* Flow-Table and classifiers |
1c1e46ed AW |
564 | * |
565 | * Writers of 'flow_table' must take the 'flow_mutex'. Corresponding | |
3453b4d6 JS |
566 | * changes to 'classifiers' must be made while still holding the |
567 | * 'flow_mutex'. | |
1c1e46ed AW |
568 | */ |
569 | struct ovs_mutex flow_mutex; | |
d9d73f84 IM |
570 | struct cmap flow_table OVS_GUARDED; /* Flow table. */ |
571 | ||
572 | /* One classifier per in_port polled by the pmd */ | |
573 | struct cmap classifiers; | |
574 | /* Periodically sort subtable vectors according to hit frequencies */ | |
575 | long long int next_optimization; | |
576 | /* End of the next time interval for which processing cycles | |
577 | are stored for each polled rxq. */ | |
578 | long long int rxq_next_cycle_store; | |
579 | ||
580 | /* Statistics. */ | |
581 | struct dp_netdev_pmd_stats stats; | |
582 | ||
583 | /* Cycles counters */ | |
584 | struct dp_netdev_pmd_cycles cycles; | |
585 | ||
586 | /* Used to count cicles. See 'cycles_counter_end()' */ | |
587 | unsigned long long last_cycles; | |
588 | ||
589 | struct latch exit_latch; /* For terminating the pmd thread. */ | |
590 | struct seq *reload_seq; | |
591 | uint64_t last_reload_seq; | |
592 | atomic_bool reload; /* Do we need to reload ports? */ | |
593 | pthread_t thread; | |
594 | unsigned core_id; /* CPU core id of this pmd thread. */ | |
595 | int numa_id; /* numa node id of this pmd thread. */ | |
596 | bool isolated; | |
597 | ||
598 | /* Queue id used by this pmd thread to send packets on all netdevs if | |
599 | * XPS disabled for this netdev. All static_tx_qid's are unique and less | |
600 | * than 'cmap_count(dp->poll_threads)'. */ | |
601 | uint32_t static_tx_qid; | |
602 | ||
603 | struct ovs_mutex port_mutex; /* Mutex for 'poll_list' and 'tx_ports'. */ | |
604 | /* List of rx queues to poll. */ | |
605 | struct hmap poll_list OVS_GUARDED; | |
606 | /* Map of 'tx_port's used for transmission. Written by the main thread, | |
607 | * read by the pmd thread. */ | |
608 | struct hmap tx_ports OVS_GUARDED; | |
609 | ||
610 | /* These are thread-local copies of 'tx_ports'. One contains only tunnel | |
611 | * ports (that support push_tunnel/pop_tunnel), the other contains ports | |
612 | * with at least one txq (that support send). A port can be in both. | |
613 | * | |
614 | * There are two separate maps to make sure that we don't try to execute | |
615 | * OUTPUT on a device which has 0 txqs or PUSH/POP on a non-tunnel device. | |
616 | * | |
617 | * The instances for cpu core NON_PMD_CORE_ID can be accessed by multiple | |
618 | * threads, and thusly need to be protected by 'non_pmd_mutex'. Every | |
619 | * other instance will only be accessed by its own pmd thread. */ | |
620 | struct hmap tnl_port_cache; | |
621 | struct hmap send_port_cache; | |
622 | ||
623 | /* Only a pmd thread can write on its own 'cycles' and 'stats'. | |
624 | * The main thread keeps 'stats_zero' and 'cycles_zero' as base | |
625 | * values and subtracts them from 'stats' and 'cycles' before | |
626 | * reporting to the user */ | |
627 | unsigned long long stats_zero[DP_N_STATS]; | |
628 | uint64_t cycles_zero[PMD_N_CYCLES]; | |
629 | ||
630 | /* Set to true if the pmd thread needs to be reloaded. */ | |
631 | bool need_reload; | |
6c3eee82 BP |
632 | }; |
633 | ||
72865317 BP |
634 | /* Interface to netdev-based datapath. */ |
635 | struct dpif_netdev { | |
636 | struct dpif dpif; | |
637 | struct dp_netdev *dp; | |
d33ed218 | 638 | uint64_t last_port_seq; |
72865317 BP |
639 | }; |
640 | ||
8a4e3a85 | 641 | static int get_port_by_number(struct dp_netdev *dp, odp_port_t port_no, |
e9985d6a DDP |
642 | struct dp_netdev_port **portp) |
643 | OVS_REQUIRES(dp->port_mutex); | |
8a4e3a85 | 644 | static int get_port_by_name(struct dp_netdev *dp, const char *devname, |
e9985d6a DDP |
645 | struct dp_netdev_port **portp) |
646 | OVS_REQUIRES(dp->port_mutex); | |
8a4e3a85 BP |
647 | static void dp_netdev_free(struct dp_netdev *) |
648 | OVS_REQUIRES(dp_netdev_mutex); | |
8a4e3a85 BP |
649 | static int do_add_port(struct dp_netdev *dp, const char *devname, |
650 | const char *type, odp_port_t port_no) | |
59e6d833 | 651 | OVS_REQUIRES(dp->port_mutex); |
c40b890f | 652 | static void do_del_port(struct dp_netdev *dp, struct dp_netdev_port *) |
59e6d833 | 653 | OVS_REQUIRES(dp->port_mutex); |
614c4892 BP |
654 | static int dpif_netdev_open(const struct dpif_class *, const char *name, |
655 | bool create, struct dpif **); | |
65f13b50 | 656 | static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread *pmd, |
1895cc8d | 657 | struct dp_packet_batch *, |
66e4ad8a | 658 | bool may_steal, const struct flow *flow, |
4edb9ae9 | 659 | const struct nlattr *actions, |
324c8374 IM |
660 | size_t actions_len, |
661 | long long now); | |
65f13b50 | 662 | static void dp_netdev_input(struct dp_netdev_pmd_thread *, |
1895cc8d | 663 | struct dp_packet_batch *, odp_port_t port_no); |
a90ed026 | 664 | static void dp_netdev_recirculate(struct dp_netdev_pmd_thread *, |
1895cc8d | 665 | struct dp_packet_batch *); |
41ccaa24 | 666 | |
6b31e073 | 667 | static void dp_netdev_disable_upcall(struct dp_netdev *); |
ae7ad0a1 | 668 | static void dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread *pmd); |
65f13b50 | 669 | static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread *pmd, |
00873463 DDP |
670 | struct dp_netdev *dp, unsigned core_id, |
671 | int numa_id); | |
1c1e46ed | 672 | static void dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread *pmd); |
e9985d6a DDP |
673 | static void dp_netdev_set_nonpmd(struct dp_netdev *dp) |
674 | OVS_REQUIRES(dp->port_mutex); | |
675 | ||
e32971b8 | 676 | static void *pmd_thread_main(void *); |
b19befae | 677 | static struct dp_netdev_pmd_thread *dp_netdev_get_pmd(struct dp_netdev *dp, |
bd5131ba | 678 | unsigned core_id); |
1c1e46ed AW |
679 | static struct dp_netdev_pmd_thread * |
680 | dp_netdev_pmd_get_next(struct dp_netdev *dp, struct cmap_position *pos); | |
140dd699 IM |
681 | static void dp_netdev_del_pmd(struct dp_netdev *dp, |
682 | struct dp_netdev_pmd_thread *pmd); | |
e32971b8 | 683 | static void dp_netdev_destroy_all_pmds(struct dp_netdev *dp, bool non_pmd); |
d0cca6c3 | 684 | static void dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread *pmd); |
d0cca6c3 | 685 | static void dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread *pmd, |
e32971b8 DDP |
686 | struct dp_netdev_port *port) |
687 | OVS_REQUIRES(pmd->port_mutex); | |
688 | static void dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread *pmd, | |
689 | struct tx_port *tx) | |
690 | OVS_REQUIRES(pmd->port_mutex); | |
d0cca6c3 | 691 | static void dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread *pmd, |
947dc567 DDP |
692 | struct dp_netdev_rxq *rxq) |
693 | OVS_REQUIRES(pmd->port_mutex); | |
e32971b8 DDP |
694 | static void dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread *pmd, |
695 | struct rxq_poll *poll) | |
696 | OVS_REQUIRES(pmd->port_mutex); | |
697 | static void reconfigure_datapath(struct dp_netdev *dp) | |
3eb67853 | 698 | OVS_REQUIRES(dp->port_mutex); |
1c1e46ed AW |
699 | static bool dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread *pmd); |
700 | static void dp_netdev_pmd_unref(struct dp_netdev_pmd_thread *pmd); | |
701 | static void dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread *pmd); | |
d0cca6c3 DDP |
702 | static void pmd_load_cached_ports(struct dp_netdev_pmd_thread *pmd) |
703 | OVS_REQUIRES(pmd->port_mutex); | |
3453b4d6 | 704 | static inline void |
4809891b KT |
705 | dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread *pmd, |
706 | struct polled_queue *poll_list, int poll_cnt); | |
707 | static void | |
708 | dp_netdev_rxq_set_cycles(struct dp_netdev_rxq *rx, | |
709 | enum rxq_cycles_counter_type type, | |
710 | unsigned long long cycles); | |
711 | static uint64_t | |
712 | dp_netdev_rxq_get_cycles(struct dp_netdev_rxq *rx, | |
713 | enum rxq_cycles_counter_type type); | |
714 | static void | |
715 | dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq *rx, | |
716 | unsigned long long cycles); | |
655856ef KT |
717 | static uint64_t |
718 | dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq *rx, unsigned idx); | |
324c8374 IM |
719 | static void |
720 | dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread *pmd, | |
721 | long long now, bool purge); | |
722 | static int dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread *pmd, | |
723 | struct tx_port *tx, long long now); | |
724 | ||
67ad54cb | 725 | static inline bool emc_entry_alive(struct emc_entry *ce); |
9bbf1c3d DDP |
726 | static void emc_clear_entry(struct emc_entry *ce); |
727 | ||
cd995c73 KT |
728 | static void dp_netdev_request_reconfigure(struct dp_netdev *dp); |
729 | ||
9bbf1c3d DDP |
730 | static void |
731 | emc_cache_init(struct emc_cache *flow_cache) | |
732 | { | |
733 | int i; | |
734 | ||
67ad54cb | 735 | flow_cache->sweep_idx = 0; |
9bbf1c3d DDP |
736 | for (i = 0; i < ARRAY_SIZE(flow_cache->entries); i++) { |
737 | flow_cache->entries[i].flow = NULL; | |
0de8783a | 738 | flow_cache->entries[i].key.hash = 0; |
09b0fa9c | 739 | flow_cache->entries[i].key.len = sizeof(struct miniflow); |
5fcff47b | 740 | flowmap_init(&flow_cache->entries[i].key.mf.map); |
9bbf1c3d DDP |
741 | } |
742 | } | |
743 | ||
744 | static void | |
745 | emc_cache_uninit(struct emc_cache *flow_cache) | |
746 | { | |
747 | int i; | |
748 | ||
749 | for (i = 0; i < ARRAY_SIZE(flow_cache->entries); i++) { | |
750 | emc_clear_entry(&flow_cache->entries[i]); | |
751 | } | |
752 | } | |
753 | ||
67ad54cb AW |
754 | /* Check and clear dead flow references slowly (one entry at each |
755 | * invocation). */ | |
756 | static void | |
757 | emc_cache_slow_sweep(struct emc_cache *flow_cache) | |
758 | { | |
759 | struct emc_entry *entry = &flow_cache->entries[flow_cache->sweep_idx]; | |
760 | ||
761 | if (!emc_entry_alive(entry)) { | |
762 | emc_clear_entry(entry); | |
763 | } | |
764 | flow_cache->sweep_idx = (flow_cache->sweep_idx + 1) & EM_FLOW_HASH_MASK; | |
765 | } | |
766 | ||
c4ea7529 BP |
767 | /* Returns true if 'dpif' is a netdev or dummy dpif, false otherwise. */ |
768 | bool | |
769 | dpif_is_netdev(const struct dpif *dpif) | |
770 | { | |
771 | return dpif->dpif_class->open == dpif_netdev_open; | |
772 | } | |
773 | ||
72865317 BP |
774 | static struct dpif_netdev * |
775 | dpif_netdev_cast(const struct dpif *dpif) | |
776 | { | |
c4ea7529 | 777 | ovs_assert(dpif_is_netdev(dpif)); |
72865317 BP |
778 | return CONTAINER_OF(dpif, struct dpif_netdev, dpif); |
779 | } | |
780 | ||
781 | static struct dp_netdev * | |
782 | get_dp_netdev(const struct dpif *dpif) | |
783 | { | |
784 | return dpif_netdev_cast(dpif)->dp; | |
785 | } | |
6553d06b DDP |
786 | \f |
787 | enum pmd_info_type { | |
ce179f11 IM |
788 | PMD_INFO_SHOW_STATS, /* Show how cpu cycles are spent. */ |
789 | PMD_INFO_CLEAR_STATS, /* Set the cycles count to 0. */ | |
790 | PMD_INFO_SHOW_RXQ /* Show poll-lists of pmd threads. */ | |
6553d06b DDP |
791 | }; |
792 | ||
793 | static void | |
794 | pmd_info_show_stats(struct ds *reply, | |
795 | struct dp_netdev_pmd_thread *pmd, | |
796 | unsigned long long stats[DP_N_STATS], | |
797 | uint64_t cycles[PMD_N_CYCLES]) | |
798 | { | |
435c2797 | 799 | unsigned long long total_packets; |
6553d06b DDP |
800 | uint64_t total_cycles = 0; |
801 | int i; | |
802 | ||
803 | /* These loops subtracts reference values ('*_zero') from the counters. | |
804 | * Since loads and stores are relaxed, it might be possible for a '*_zero' | |
805 | * value to be more recent than the current value we're reading from the | |
806 | * counter. This is not a big problem, since these numbers are not | |
807 | * supposed to be too accurate, but we should at least make sure that | |
808 | * the result is not negative. */ | |
809 | for (i = 0; i < DP_N_STATS; i++) { | |
810 | if (stats[i] > pmd->stats_zero[i]) { | |
811 | stats[i] -= pmd->stats_zero[i]; | |
812 | } else { | |
813 | stats[i] = 0; | |
814 | } | |
6553d06b DDP |
815 | } |
816 | ||
435c2797 IM |
817 | /* Sum of all the matched and not matched packets gives the total. */ |
818 | total_packets = stats[DP_STAT_EXACT_HIT] + stats[DP_STAT_MASKED_HIT] | |
819 | + stats[DP_STAT_MISS]; | |
820 | ||
6553d06b DDP |
821 | for (i = 0; i < PMD_N_CYCLES; i++) { |
822 | if (cycles[i] > pmd->cycles_zero[i]) { | |
823 | cycles[i] -= pmd->cycles_zero[i]; | |
824 | } else { | |
825 | cycles[i] = 0; | |
826 | } | |
827 | ||
828 | total_cycles += cycles[i]; | |
829 | } | |
830 | ||
831 | ds_put_cstr(reply, (pmd->core_id == NON_PMD_CORE_ID) | |
832 | ? "main thread" : "pmd thread"); | |
833 | ||
834 | if (pmd->numa_id != OVS_NUMA_UNSPEC) { | |
835 | ds_put_format(reply, " numa_id %d", pmd->numa_id); | |
836 | } | |
d5c199ea | 837 | if (pmd->core_id != OVS_CORE_UNSPEC && pmd->core_id != NON_PMD_CORE_ID) { |
bd5131ba | 838 | ds_put_format(reply, " core_id %u", pmd->core_id); |
6553d06b DDP |
839 | } |
840 | ds_put_cstr(reply, ":\n"); | |
841 | ||
842 | ds_put_format(reply, | |
843 | "\temc hits:%llu\n\tmegaflow hits:%llu\n" | |
3453b4d6 | 844 | "\tavg. subtable lookups per hit:%.2f\n" |
6553d06b DDP |
845 | "\tmiss:%llu\n\tlost:%llu\n", |
846 | stats[DP_STAT_EXACT_HIT], stats[DP_STAT_MASKED_HIT], | |
3453b4d6 JS |
847 | stats[DP_STAT_MASKED_HIT] > 0 |
848 | ? (1.0*stats[DP_STAT_LOOKUP_HIT])/stats[DP_STAT_MASKED_HIT] | |
849 | : 0, | |
6553d06b DDP |
850 | stats[DP_STAT_MISS], stats[DP_STAT_LOST]); |
851 | ||
852 | if (total_cycles == 0) { | |
853 | return; | |
854 | } | |
855 | ||
856 | ds_put_format(reply, | |
a2ac666d | 857 | "\tidle cycles:%"PRIu64" (%.02f%%)\n" |
6553d06b | 858 | "\tprocessing cycles:%"PRIu64" (%.02f%%)\n", |
a2ac666d CL |
859 | cycles[PMD_CYCLES_IDLE], |
860 | cycles[PMD_CYCLES_IDLE] / (double)total_cycles * 100, | |
6553d06b DDP |
861 | cycles[PMD_CYCLES_PROCESSING], |
862 | cycles[PMD_CYCLES_PROCESSING] / (double)total_cycles * 100); | |
863 | ||
864 | if (total_packets == 0) { | |
865 | return; | |
866 | } | |
867 | ||
868 | ds_put_format(reply, | |
869 | "\tavg cycles per packet: %.02f (%"PRIu64"/%llu)\n", | |
870 | total_cycles / (double)total_packets, | |
871 | total_cycles, total_packets); | |
872 | ||
873 | ds_put_format(reply, | |
874 | "\tavg processing cycles per packet: " | |
875 | "%.02f (%"PRIu64"/%llu)\n", | |
876 | cycles[PMD_CYCLES_PROCESSING] / (double)total_packets, | |
877 | cycles[PMD_CYCLES_PROCESSING], total_packets); | |
878 | } | |
879 | ||
880 | static void | |
881 | pmd_info_clear_stats(struct ds *reply OVS_UNUSED, | |
882 | struct dp_netdev_pmd_thread *pmd, | |
883 | unsigned long long stats[DP_N_STATS], | |
884 | uint64_t cycles[PMD_N_CYCLES]) | |
885 | { | |
886 | int i; | |
887 | ||
888 | /* We cannot write 'stats' and 'cycles' (because they're written by other | |
889 | * threads) and we shouldn't change 'stats' (because they're used to count | |
890 | * datapath stats, which must not be cleared here). Instead, we save the | |
891 | * current values and subtract them from the values to be displayed in the | |
892 | * future */ | |
893 | for (i = 0; i < DP_N_STATS; i++) { | |
894 | pmd->stats_zero[i] = stats[i]; | |
895 | } | |
896 | for (i = 0; i < PMD_N_CYCLES; i++) { | |
897 | pmd->cycles_zero[i] = cycles[i]; | |
898 | } | |
899 | } | |
900 | ||
947dc567 DDP |
901 | static int |
902 | compare_poll_list(const void *a_, const void *b_) | |
903 | { | |
904 | const struct rxq_poll *a = a_; | |
905 | const struct rxq_poll *b = b_; | |
906 | ||
907 | const char *namea = netdev_rxq_get_name(a->rxq->rx); | |
908 | const char *nameb = netdev_rxq_get_name(b->rxq->rx); | |
909 | ||
910 | int cmp = strcmp(namea, nameb); | |
911 | if (!cmp) { | |
912 | return netdev_rxq_get_queue_id(a->rxq->rx) | |
913 | - netdev_rxq_get_queue_id(b->rxq->rx); | |
914 | } else { | |
915 | return cmp; | |
916 | } | |
917 | } | |
918 | ||
919 | static void | |
920 | sorted_poll_list(struct dp_netdev_pmd_thread *pmd, struct rxq_poll **list, | |
921 | size_t *n) | |
922 | { | |
923 | struct rxq_poll *ret, *poll; | |
924 | size_t i; | |
925 | ||
926 | *n = hmap_count(&pmd->poll_list); | |
927 | if (!*n) { | |
928 | ret = NULL; | |
929 | } else { | |
930 | ret = xcalloc(*n, sizeof *ret); | |
931 | i = 0; | |
932 | HMAP_FOR_EACH (poll, node, &pmd->poll_list) { | |
933 | ret[i] = *poll; | |
934 | i++; | |
935 | } | |
936 | ovs_assert(i == *n); | |
1cc1b5f6 | 937 | qsort(ret, *n, sizeof *ret, compare_poll_list); |
947dc567 DDP |
938 | } |
939 | ||
947dc567 DDP |
940 | *list = ret; |
941 | } | |
942 | ||
ce179f11 IM |
943 | static void |
944 | pmd_info_show_rxq(struct ds *reply, struct dp_netdev_pmd_thread *pmd) | |
945 | { | |
946 | if (pmd->core_id != NON_PMD_CORE_ID) { | |
ce179f11 | 947 | const char *prev_name = NULL; |
947dc567 DDP |
948 | struct rxq_poll *list; |
949 | size_t i, n; | |
ce179f11 | 950 | |
3eb67853 IM |
951 | ds_put_format(reply, |
952 | "pmd thread numa_id %d core_id %u:\n\tisolated : %s\n", | |
953 | pmd->numa_id, pmd->core_id, (pmd->isolated) | |
954 | ? "true" : "false"); | |
ce179f11 | 955 | |
d0cca6c3 | 956 | ovs_mutex_lock(&pmd->port_mutex); |
947dc567 DDP |
957 | sorted_poll_list(pmd, &list, &n); |
958 | for (i = 0; i < n; i++) { | |
959 | const char *name = netdev_rxq_get_name(list[i].rxq->rx); | |
ce179f11 IM |
960 | |
961 | if (!prev_name || strcmp(name, prev_name)) { | |
962 | if (prev_name) { | |
963 | ds_put_cstr(reply, "\n"); | |
964 | } | |
947dc567 | 965 | ds_put_format(reply, "\tport: %s\tqueue-id:", name); |
ce179f11 | 966 | } |
947dc567 DDP |
967 | ds_put_format(reply, " %d", |
968 | netdev_rxq_get_queue_id(list[i].rxq->rx)); | |
ce179f11 IM |
969 | prev_name = name; |
970 | } | |
d0cca6c3 | 971 | ovs_mutex_unlock(&pmd->port_mutex); |
ce179f11 | 972 | ds_put_cstr(reply, "\n"); |
947dc567 | 973 | free(list); |
ce179f11 IM |
974 | } |
975 | } | |
976 | ||
34d8e04b EC |
977 | static int |
978 | compare_poll_thread_list(const void *a_, const void *b_) | |
979 | { | |
980 | const struct dp_netdev_pmd_thread *a, *b; | |
981 | ||
982 | a = *(struct dp_netdev_pmd_thread **)a_; | |
983 | b = *(struct dp_netdev_pmd_thread **)b_; | |
984 | ||
985 | if (a->core_id < b->core_id) { | |
986 | return -1; | |
987 | } | |
988 | if (a->core_id > b->core_id) { | |
989 | return 1; | |
990 | } | |
991 | return 0; | |
992 | } | |
993 | ||
994 | /* Create a sorted list of pmd's from the dp->poll_threads cmap. We can use | |
995 | * this list, as long as we do not go to quiescent state. */ | |
996 | static void | |
997 | sorted_poll_thread_list(struct dp_netdev *dp, | |
998 | struct dp_netdev_pmd_thread ***list, | |
999 | size_t *n) | |
1000 | { | |
1001 | struct dp_netdev_pmd_thread *pmd; | |
1002 | struct dp_netdev_pmd_thread **pmd_list; | |
1003 | size_t k = 0, n_pmds; | |
1004 | ||
1005 | n_pmds = cmap_count(&dp->poll_threads); | |
1006 | pmd_list = xcalloc(n_pmds, sizeof *pmd_list); | |
1007 | ||
1008 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
1009 | if (k >= n_pmds) { | |
1010 | break; | |
1011 | } | |
1012 | pmd_list[k++] = pmd; | |
1013 | } | |
1014 | ||
1015 | qsort(pmd_list, k, sizeof *pmd_list, compare_poll_thread_list); | |
1016 | ||
1017 | *list = pmd_list; | |
1018 | *n = k; | |
1019 | } | |
1020 | ||
cd995c73 KT |
1021 | static void |
1022 | dpif_netdev_pmd_rebalance(struct unixctl_conn *conn, int argc, | |
1023 | const char *argv[], void *aux OVS_UNUSED) | |
1024 | { | |
1025 | struct ds reply = DS_EMPTY_INITIALIZER; | |
1026 | struct dp_netdev *dp = NULL; | |
1027 | ||
1028 | ovs_mutex_lock(&dp_netdev_mutex); | |
1029 | ||
1030 | if (argc == 2) { | |
1031 | dp = shash_find_data(&dp_netdevs, argv[1]); | |
1032 | } else if (shash_count(&dp_netdevs) == 1) { | |
1033 | /* There's only one datapath */ | |
1034 | dp = shash_first(&dp_netdevs)->data; | |
1035 | } | |
1036 | ||
1037 | if (!dp) { | |
1038 | ovs_mutex_unlock(&dp_netdev_mutex); | |
1039 | unixctl_command_reply_error(conn, | |
1040 | "please specify an existing datapath"); | |
1041 | return; | |
1042 | } | |
1043 | ||
1044 | dp_netdev_request_reconfigure(dp); | |
1045 | ovs_mutex_unlock(&dp_netdev_mutex); | |
1046 | ds_put_cstr(&reply, "pmd rxq rebalance requested.\n"); | |
1047 | unixctl_command_reply(conn, ds_cstr(&reply)); | |
1048 | ds_destroy(&reply); | |
1049 | } | |
1050 | ||
6553d06b DDP |
1051 | static void |
1052 | dpif_netdev_pmd_info(struct unixctl_conn *conn, int argc, const char *argv[], | |
1053 | void *aux) | |
1054 | { | |
1055 | struct ds reply = DS_EMPTY_INITIALIZER; | |
34d8e04b | 1056 | struct dp_netdev_pmd_thread **pmd_list; |
6553d06b | 1057 | struct dp_netdev *dp = NULL; |
34d8e04b | 1058 | size_t n; |
6553d06b DDP |
1059 | enum pmd_info_type type = *(enum pmd_info_type *) aux; |
1060 | ||
1061 | ovs_mutex_lock(&dp_netdev_mutex); | |
1062 | ||
1063 | if (argc == 2) { | |
1064 | dp = shash_find_data(&dp_netdevs, argv[1]); | |
1065 | } else if (shash_count(&dp_netdevs) == 1) { | |
1066 | /* There's only one datapath */ | |
1067 | dp = shash_first(&dp_netdevs)->data; | |
1068 | } | |
1069 | ||
1070 | if (!dp) { | |
1071 | ovs_mutex_unlock(&dp_netdev_mutex); | |
1072 | unixctl_command_reply_error(conn, | |
1073 | "please specify an existing datapath"); | |
1074 | return; | |
1075 | } | |
1076 | ||
34d8e04b EC |
1077 | sorted_poll_thread_list(dp, &pmd_list, &n); |
1078 | for (size_t i = 0; i < n; i++) { | |
1079 | struct dp_netdev_pmd_thread *pmd = pmd_list[i]; | |
1080 | if (!pmd) { | |
1081 | break; | |
1082 | } | |
1083 | ||
ce179f11 IM |
1084 | if (type == PMD_INFO_SHOW_RXQ) { |
1085 | pmd_info_show_rxq(&reply, pmd); | |
1086 | } else { | |
1087 | unsigned long long stats[DP_N_STATS]; | |
1088 | uint64_t cycles[PMD_N_CYCLES]; | |
6553d06b | 1089 | |
ce179f11 | 1090 | /* Read current stats and cycle counters */ |
71f21279 BP |
1091 | for (size_t j = 0; j < ARRAY_SIZE(stats); j++) { |
1092 | atomic_read_relaxed(&pmd->stats.n[j], &stats[j]); | |
ce179f11 | 1093 | } |
71f21279 BP |
1094 | for (size_t j = 0; j < ARRAY_SIZE(cycles); j++) { |
1095 | atomic_read_relaxed(&pmd->cycles.n[j], &cycles[j]); | |
ce179f11 | 1096 | } |
6553d06b | 1097 | |
ce179f11 IM |
1098 | if (type == PMD_INFO_CLEAR_STATS) { |
1099 | pmd_info_clear_stats(&reply, pmd, stats, cycles); | |
1100 | } else if (type == PMD_INFO_SHOW_STATS) { | |
1101 | pmd_info_show_stats(&reply, pmd, stats, cycles); | |
1102 | } | |
6553d06b DDP |
1103 | } |
1104 | } | |
34d8e04b | 1105 | free(pmd_list); |
6553d06b DDP |
1106 | |
1107 | ovs_mutex_unlock(&dp_netdev_mutex); | |
1108 | ||
1109 | unixctl_command_reply(conn, ds_cstr(&reply)); | |
1110 | ds_destroy(&reply); | |
1111 | } | |
1112 | \f | |
1113 | static int | |
1114 | dpif_netdev_init(void) | |
1115 | { | |
1116 | static enum pmd_info_type show_aux = PMD_INFO_SHOW_STATS, | |
ce179f11 IM |
1117 | clear_aux = PMD_INFO_CLEAR_STATS, |
1118 | poll_aux = PMD_INFO_SHOW_RXQ; | |
6553d06b DDP |
1119 | |
1120 | unixctl_command_register("dpif-netdev/pmd-stats-show", "[dp]", | |
1121 | 0, 1, dpif_netdev_pmd_info, | |
1122 | (void *)&show_aux); | |
1123 | unixctl_command_register("dpif-netdev/pmd-stats-clear", "[dp]", | |
1124 | 0, 1, dpif_netdev_pmd_info, | |
1125 | (void *)&clear_aux); | |
ce179f11 IM |
1126 | unixctl_command_register("dpif-netdev/pmd-rxq-show", "[dp]", |
1127 | 0, 1, dpif_netdev_pmd_info, | |
1128 | (void *)&poll_aux); | |
cd995c73 KT |
1129 | unixctl_command_register("dpif-netdev/pmd-rxq-rebalance", "[dp]", |
1130 | 0, 1, dpif_netdev_pmd_rebalance, | |
1131 | NULL); | |
6553d06b DDP |
1132 | return 0; |
1133 | } | |
72865317 | 1134 | |
2197d7ab | 1135 | static int |
2240af25 DDP |
1136 | dpif_netdev_enumerate(struct sset *all_dps, |
1137 | const struct dpif_class *dpif_class) | |
2197d7ab GL |
1138 | { |
1139 | struct shash_node *node; | |
1140 | ||
97be1538 | 1141 | ovs_mutex_lock(&dp_netdev_mutex); |
2197d7ab | 1142 | SHASH_FOR_EACH(node, &dp_netdevs) { |
2240af25 DDP |
1143 | struct dp_netdev *dp = node->data; |
1144 | if (dpif_class != dp->class) { | |
1145 | /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs. | |
1146 | * If the class doesn't match, skip this dpif. */ | |
1147 | continue; | |
1148 | } | |
2197d7ab GL |
1149 | sset_add(all_dps, node->name); |
1150 | } | |
97be1538 | 1151 | ovs_mutex_unlock(&dp_netdev_mutex); |
5279f8fd | 1152 | |
2197d7ab GL |
1153 | return 0; |
1154 | } | |
1155 | ||
add90f6f EJ |
1156 | static bool |
1157 | dpif_netdev_class_is_dummy(const struct dpif_class *class) | |
1158 | { | |
1159 | return class != &dpif_netdev_class; | |
1160 | } | |
1161 | ||
0aeaabc8 JP |
1162 | static const char * |
1163 | dpif_netdev_port_open_type(const struct dpif_class *class, const char *type) | |
1164 | { | |
1165 | return strcmp(type, "internal") ? type | |
e98d0cb3 | 1166 | : dpif_netdev_class_is_dummy(class) ? "dummy-internal" |
0aeaabc8 JP |
1167 | : "tap"; |
1168 | } | |
1169 | ||
72865317 BP |
1170 | static struct dpif * |
1171 | create_dpif_netdev(struct dp_netdev *dp) | |
1172 | { | |
462278db | 1173 | uint16_t netflow_id = hash_string(dp->name, 0); |
72865317 | 1174 | struct dpif_netdev *dpif; |
72865317 | 1175 | |
6a8267c5 | 1176 | ovs_refcount_ref(&dp->ref_cnt); |
72865317 | 1177 | |
72865317 | 1178 | dpif = xmalloc(sizeof *dpif); |
614c4892 | 1179 | dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id); |
72865317 | 1180 | dpif->dp = dp; |
d33ed218 | 1181 | dpif->last_port_seq = seq_read(dp->port_seq); |
72865317 BP |
1182 | |
1183 | return &dpif->dpif; | |
1184 | } | |
1185 | ||
4e022ec0 AW |
1186 | /* Choose an unused, non-zero port number and return it on success. |
1187 | * Return ODPP_NONE on failure. */ | |
1188 | static odp_port_t | |
e44768b7 | 1189 | choose_port(struct dp_netdev *dp, const char *name) |
59e6d833 | 1190 | OVS_REQUIRES(dp->port_mutex) |
e44768b7 | 1191 | { |
4e022ec0 | 1192 | uint32_t port_no; |
e44768b7 JP |
1193 | |
1194 | if (dp->class != &dpif_netdev_class) { | |
1195 | const char *p; | |
1196 | int start_no = 0; | |
1197 | ||
1198 | /* If the port name begins with "br", start the number search at | |
1199 | * 100 to make writing tests easier. */ | |
1200 | if (!strncmp(name, "br", 2)) { | |
1201 | start_no = 100; | |
1202 | } | |
1203 | ||
1204 | /* If the port name contains a number, try to assign that port number. | |
1205 | * This can make writing unit tests easier because port numbers are | |
1206 | * predictable. */ | |
1207 | for (p = name; *p != '\0'; p++) { | |
1208 | if (isdigit((unsigned char) *p)) { | |
1209 | port_no = start_no + strtol(p, NULL, 10); | |
ff073a71 BP |
1210 | if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE) |
1211 | && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) { | |
4e022ec0 | 1212 | return u32_to_odp(port_no); |
e44768b7 JP |
1213 | } |
1214 | break; | |
1215 | } | |
1216 | } | |
1217 | } | |
1218 | ||
ff073a71 BP |
1219 | for (port_no = 1; port_no <= UINT16_MAX; port_no++) { |
1220 | if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) { | |
4e022ec0 | 1221 | return u32_to_odp(port_no); |
e44768b7 JP |
1222 | } |
1223 | } | |
1224 | ||
4e022ec0 | 1225 | return ODPP_NONE; |
e44768b7 JP |
1226 | } |
1227 | ||
72865317 | 1228 | static int |
614c4892 BP |
1229 | create_dp_netdev(const char *name, const struct dpif_class *class, |
1230 | struct dp_netdev **dpp) | |
8a4e3a85 | 1231 | OVS_REQUIRES(dp_netdev_mutex) |
72865317 BP |
1232 | { |
1233 | struct dp_netdev *dp; | |
1234 | int error; | |
72865317 | 1235 | |
462278db | 1236 | dp = xzalloc(sizeof *dp); |
8a4e3a85 BP |
1237 | shash_add(&dp_netdevs, name, dp); |
1238 | ||
1239 | *CONST_CAST(const struct dpif_class **, &dp->class) = class; | |
1240 | *CONST_CAST(const char **, &dp->name) = xstrdup(name); | |
6a8267c5 | 1241 | ovs_refcount_init(&dp->ref_cnt); |
1a65ba85 | 1242 | atomic_flag_clear(&dp->destroyed); |
8a4e3a85 | 1243 | |
59e6d833 | 1244 | ovs_mutex_init(&dp->port_mutex); |
e9985d6a | 1245 | hmap_init(&dp->ports); |
d33ed218 | 1246 | dp->port_seq = seq_create(); |
6b31e073 RW |
1247 | fat_rwlock_init(&dp->upcall_rwlock); |
1248 | ||
a6a426d6 IM |
1249 | dp->reconfigure_seq = seq_create(); |
1250 | dp->last_reconfigure_seq = seq_read(dp->reconfigure_seq); | |
1251 | ||
4b27db64 JR |
1252 | for (int i = 0; i < N_METER_LOCKS; ++i) { |
1253 | ovs_mutex_init_adaptive(&dp->meter_locks[i]); | |
1254 | } | |
1255 | ||
6b31e073 RW |
1256 | /* Disable upcalls by default. */ |
1257 | dp_netdev_disable_upcall(dp); | |
623540e4 | 1258 | dp->upcall_aux = NULL; |
6b31e073 | 1259 | dp->upcall_cb = NULL; |
e44768b7 | 1260 | |
5cf3edb3 DDP |
1261 | conntrack_init(&dp->conntrack); |
1262 | ||
4c30b246 CL |
1263 | atomic_init(&dp->emc_insert_min, DEFAULT_EM_FLOW_INSERT_MIN); |
1264 | ||
65f13b50 | 1265 | cmap_init(&dp->poll_threads); |
140dd699 IM |
1266 | |
1267 | ovs_mutex_init(&dp->tx_qid_pool_mutex); | |
1268 | /* We need 1 Tx queue for each possible core + 1 for non-PMD threads. */ | |
1269 | dp->tx_qid_pool = id_pool_create(0, ovs_numa_get_n_cores() + 1); | |
1270 | ||
65f13b50 AW |
1271 | ovs_mutex_init_recursive(&dp->non_pmd_mutex); |
1272 | ovsthread_key_create(&dp->per_pmd_key, NULL); | |
1273 | ||
e9985d6a | 1274 | ovs_mutex_lock(&dp->port_mutex); |
140dd699 IM |
1275 | /* non-PMD will be created before all other threads and will |
1276 | * allocate static_tx_qid = 0. */ | |
f2eee189 | 1277 | dp_netdev_set_nonpmd(dp); |
65f13b50 | 1278 | |
a3e8437a TLSC |
1279 | error = do_add_port(dp, name, dpif_netdev_port_open_type(dp->class, |
1280 | "internal"), | |
1281 | ODPP_LOCAL); | |
59e6d833 | 1282 | ovs_mutex_unlock(&dp->port_mutex); |
72865317 BP |
1283 | if (error) { |
1284 | dp_netdev_free(dp); | |
462278db | 1285 | return error; |
72865317 BP |
1286 | } |
1287 | ||
a36de779 | 1288 | dp->last_tnl_conf_seq = seq_read(tnl_conf_seq); |
462278db | 1289 | *dpp = dp; |
72865317 BP |
1290 | return 0; |
1291 | } | |
1292 | ||
a6a426d6 IM |
1293 | static void |
1294 | dp_netdev_request_reconfigure(struct dp_netdev *dp) | |
1295 | { | |
1296 | seq_change(dp->reconfigure_seq); | |
1297 | } | |
1298 | ||
1299 | static bool | |
1300 | dp_netdev_is_reconf_required(struct dp_netdev *dp) | |
1301 | { | |
1302 | return seq_read(dp->reconfigure_seq) != dp->last_reconfigure_seq; | |
1303 | } | |
1304 | ||
72865317 | 1305 | static int |
614c4892 | 1306 | dpif_netdev_open(const struct dpif_class *class, const char *name, |
4a387741 | 1307 | bool create, struct dpif **dpifp) |
72865317 | 1308 | { |
462278db | 1309 | struct dp_netdev *dp; |
5279f8fd | 1310 | int error; |
462278db | 1311 | |
97be1538 | 1312 | ovs_mutex_lock(&dp_netdev_mutex); |
462278db BP |
1313 | dp = shash_find_data(&dp_netdevs, name); |
1314 | if (!dp) { | |
5279f8fd | 1315 | error = create ? create_dp_netdev(name, class, &dp) : ENODEV; |
72865317 | 1316 | } else { |
5279f8fd BP |
1317 | error = (dp->class != class ? EINVAL |
1318 | : create ? EEXIST | |
1319 | : 0); | |
1320 | } | |
1321 | if (!error) { | |
1322 | *dpifp = create_dpif_netdev(dp); | |
6b31e073 | 1323 | dp->dpif = *dpifp; |
72865317 | 1324 | } |
97be1538 | 1325 | ovs_mutex_unlock(&dp_netdev_mutex); |
462278db | 1326 | |
5279f8fd | 1327 | return error; |
72865317 BP |
1328 | } |
1329 | ||
88ace79b DDP |
1330 | static void |
1331 | dp_netdev_destroy_upcall_lock(struct dp_netdev *dp) | |
1332 | OVS_NO_THREAD_SAFETY_ANALYSIS | |
1333 | { | |
1334 | /* Check that upcalls are disabled, i.e. that the rwlock is taken */ | |
1335 | ovs_assert(fat_rwlock_tryrdlock(&dp->upcall_rwlock)); | |
1336 | ||
1337 | /* Before freeing a lock we should release it */ | |
1338 | fat_rwlock_unlock(&dp->upcall_rwlock); | |
1339 | fat_rwlock_destroy(&dp->upcall_rwlock); | |
1340 | } | |
1341 | ||
4b27db64 JR |
1342 | static void |
1343 | dp_delete_meter(struct dp_netdev *dp, uint32_t meter_id) | |
1344 | OVS_REQUIRES(dp->meter_locks[meter_id % N_METER_LOCKS]) | |
1345 | { | |
1346 | if (dp->meters[meter_id]) { | |
1347 | free(dp->meters[meter_id]); | |
1348 | dp->meters[meter_id] = NULL; | |
1349 | } | |
1350 | } | |
1351 | ||
8a4e3a85 BP |
1352 | /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp' |
1353 | * through the 'dp_netdevs' shash while freeing 'dp'. */ | |
1ba530f4 BP |
1354 | static void |
1355 | dp_netdev_free(struct dp_netdev *dp) | |
8a4e3a85 | 1356 | OVS_REQUIRES(dp_netdev_mutex) |
1ba530f4 | 1357 | { |
e9985d6a | 1358 | struct dp_netdev_port *port, *next; |
4ad28026 | 1359 | |
8a4e3a85 BP |
1360 | shash_find_and_delete(&dp_netdevs, dp->name); |
1361 | ||
59e6d833 | 1362 | ovs_mutex_lock(&dp->port_mutex); |
e9985d6a | 1363 | HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) { |
c40b890f | 1364 | do_del_port(dp, port); |
1ba530f4 | 1365 | } |
59e6d833 | 1366 | ovs_mutex_unlock(&dp->port_mutex); |
4b27db64 | 1367 | |
e32971b8 | 1368 | dp_netdev_destroy_all_pmds(dp, true); |
d916785c | 1369 | cmap_destroy(&dp->poll_threads); |
51852a57 | 1370 | |
140dd699 IM |
1371 | ovs_mutex_destroy(&dp->tx_qid_pool_mutex); |
1372 | id_pool_destroy(dp->tx_qid_pool); | |
1373 | ||
b9584f21 DDP |
1374 | ovs_mutex_destroy(&dp->non_pmd_mutex); |
1375 | ovsthread_key_delete(dp->per_pmd_key); | |
1376 | ||
1377 | conntrack_destroy(&dp->conntrack); | |
1378 | ||
1379 | ||
a6a426d6 IM |
1380 | seq_destroy(dp->reconfigure_seq); |
1381 | ||
d33ed218 | 1382 | seq_destroy(dp->port_seq); |
e9985d6a | 1383 | hmap_destroy(&dp->ports); |
3186ea46 | 1384 | ovs_mutex_destroy(&dp->port_mutex); |
88ace79b DDP |
1385 | |
1386 | /* Upcalls must be disabled at this point */ | |
1387 | dp_netdev_destroy_upcall_lock(dp); | |
9bbf1c3d | 1388 | |
4b27db64 JR |
1389 | int i; |
1390 | ||
1391 | for (i = 0; i < MAX_METERS; ++i) { | |
1392 | meter_lock(dp, i); | |
1393 | dp_delete_meter(dp, i); | |
1394 | meter_unlock(dp, i); | |
1395 | } | |
1396 | for (i = 0; i < N_METER_LOCKS; ++i) { | |
1397 | ovs_mutex_destroy(&dp->meter_locks[i]); | |
1398 | } | |
1399 | ||
f2eee189 | 1400 | free(dp->pmd_cmask); |
8a4e3a85 | 1401 | free(CONST_CAST(char *, dp->name)); |
72865317 BP |
1402 | free(dp); |
1403 | } | |
1404 | ||
8a4e3a85 BP |
1405 | static void |
1406 | dp_netdev_unref(struct dp_netdev *dp) | |
1407 | { | |
1408 | if (dp) { | |
1409 | /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't | |
1410 | * get a new reference to 'dp' through the 'dp_netdevs' shash. */ | |
1411 | ovs_mutex_lock(&dp_netdev_mutex); | |
24f83812 | 1412 | if (ovs_refcount_unref_relaxed(&dp->ref_cnt) == 1) { |
8a4e3a85 BP |
1413 | dp_netdev_free(dp); |
1414 | } | |
1415 | ovs_mutex_unlock(&dp_netdev_mutex); | |
1416 | } | |
1417 | } | |
1418 | ||
72865317 BP |
1419 | static void |
1420 | dpif_netdev_close(struct dpif *dpif) | |
1421 | { | |
1422 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
5279f8fd | 1423 | |
8a4e3a85 | 1424 | dp_netdev_unref(dp); |
72865317 BP |
1425 | free(dpif); |
1426 | } | |
1427 | ||
1428 | static int | |
7dab847a | 1429 | dpif_netdev_destroy(struct dpif *dpif) |
72865317 BP |
1430 | { |
1431 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
5279f8fd | 1432 | |
6a8267c5 | 1433 | if (!atomic_flag_test_and_set(&dp->destroyed)) { |
24f83812 | 1434 | if (ovs_refcount_unref_relaxed(&dp->ref_cnt) == 1) { |
6a8267c5 BP |
1435 | /* Can't happen: 'dpif' still owns a reference to 'dp'. */ |
1436 | OVS_NOT_REACHED(); | |
1437 | } | |
1438 | } | |
5279f8fd | 1439 | |
72865317 BP |
1440 | return 0; |
1441 | } | |
1442 | ||
eb94da30 DDP |
1443 | /* Add 'n' to the atomic variable 'var' non-atomically and using relaxed |
1444 | * load/store semantics. While the increment is not atomic, the load and | |
1445 | * store operations are, making it impossible to read inconsistent values. | |
1446 | * | |
1447 | * This is used to update thread local stats counters. */ | |
1448 | static void | |
1449 | non_atomic_ullong_add(atomic_ullong *var, unsigned long long n) | |
1450 | { | |
1451 | unsigned long long tmp; | |
1452 | ||
1453 | atomic_read_relaxed(var, &tmp); | |
1454 | tmp += n; | |
1455 | atomic_store_relaxed(var, tmp); | |
1456 | } | |
1457 | ||
72865317 | 1458 | static int |
a8d9304d | 1459 | dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats) |
72865317 BP |
1460 | { |
1461 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
1c1e46ed | 1462 | struct dp_netdev_pmd_thread *pmd; |
8a4e3a85 | 1463 | |
1c1e46ed AW |
1464 | stats->n_flows = stats->n_hit = stats->n_missed = stats->n_lost = 0; |
1465 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
eb94da30 | 1466 | unsigned long long n; |
1c1e46ed | 1467 | stats->n_flows += cmap_count(&pmd->flow_table); |
eb94da30 | 1468 | |
abcf3ef4 DDP |
1469 | atomic_read_relaxed(&pmd->stats.n[DP_STAT_MASKED_HIT], &n); |
1470 | stats->n_hit += n; | |
1471 | atomic_read_relaxed(&pmd->stats.n[DP_STAT_EXACT_HIT], &n); | |
eb94da30 DDP |
1472 | stats->n_hit += n; |
1473 | atomic_read_relaxed(&pmd->stats.n[DP_STAT_MISS], &n); | |
1474 | stats->n_missed += n; | |
1475 | atomic_read_relaxed(&pmd->stats.n[DP_STAT_LOST], &n); | |
1476 | stats->n_lost += n; | |
51852a57 | 1477 | } |
1ce3fa06 | 1478 | stats->n_masks = UINT32_MAX; |
847108dc | 1479 | stats->n_mask_hit = UINT64_MAX; |
5279f8fd | 1480 | |
72865317 BP |
1481 | return 0; |
1482 | } | |
1483 | ||
e4cfed38 | 1484 | static void |
65f13b50 | 1485 | dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread *pmd) |
e4cfed38 | 1486 | { |
accf8626 | 1487 | if (pmd->core_id == NON_PMD_CORE_ID) { |
d0cca6c3 DDP |
1488 | ovs_mutex_lock(&pmd->dp->non_pmd_mutex); |
1489 | ovs_mutex_lock(&pmd->port_mutex); | |
1490 | pmd_load_cached_ports(pmd); | |
1491 | ovs_mutex_unlock(&pmd->port_mutex); | |
1492 | ovs_mutex_unlock(&pmd->dp->non_pmd_mutex); | |
accf8626 AW |
1493 | return; |
1494 | } | |
1495 | ||
1496 | ovs_mutex_lock(&pmd->cond_mutex); | |
2788a1b1 | 1497 | seq_change(pmd->reload_seq); |
14e3e12a | 1498 | atomic_store_relaxed(&pmd->reload, true); |
accf8626 AW |
1499 | ovs_mutex_cond_wait(&pmd->cond, &pmd->cond_mutex); |
1500 | ovs_mutex_unlock(&pmd->cond_mutex); | |
65f13b50 | 1501 | } |
e4cfed38 | 1502 | |
59e6d833 BP |
1503 | static uint32_t |
1504 | hash_port_no(odp_port_t port_no) | |
1505 | { | |
1506 | return hash_int(odp_to_u32(port_no), 0); | |
1507 | } | |
1508 | ||
72865317 | 1509 | static int |
a3e8437a | 1510 | port_create(const char *devname, const char *type, |
b8d29252 | 1511 | odp_port_t port_no, struct dp_netdev_port **portp) |
72865317 | 1512 | { |
4b609110 | 1513 | struct netdev_saved_flags *sf; |
72865317 | 1514 | struct dp_netdev_port *port; |
2499a8ce | 1515 | enum netdev_flags flags; |
b8d29252 | 1516 | struct netdev *netdev; |
e32971b8 | 1517 | int error; |
72865317 | 1518 | |
b8d29252 | 1519 | *portp = NULL; |
72865317 BP |
1520 | |
1521 | /* Open and validate network device. */ | |
a3e8437a | 1522 | error = netdev_open(devname, type, &netdev); |
72865317 | 1523 | if (error) { |
b8d29252 | 1524 | return error; |
72865317 | 1525 | } |
72865317 BP |
1526 | /* XXX reject non-Ethernet devices */ |
1527 | ||
2499a8ce AC |
1528 | netdev_get_flags(netdev, &flags); |
1529 | if (flags & NETDEV_LOOPBACK) { | |
1530 | VLOG_ERR("%s: cannot add a loopback device", devname); | |
d17f4f08 | 1531 | error = EINVAL; |
b8d29252 | 1532 | goto out; |
2499a8ce AC |
1533 | } |
1534 | ||
e32971b8 DDP |
1535 | error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf); |
1536 | if (error) { | |
1537 | VLOG_ERR("%s: cannot set promisc flag", devname); | |
1538 | goto out; | |
324c8374 IM |
1539 | } |
1540 | ||
e4cfed38 | 1541 | port = xzalloc(sizeof *port); |
35303d71 | 1542 | port->port_no = port_no; |
e4cfed38 PS |
1543 | port->netdev = netdev; |
1544 | port->type = xstrdup(type); | |
4b609110 | 1545 | port->sf = sf; |
e32971b8 DDP |
1546 | port->need_reconfigure = true; |
1547 | ovs_mutex_init(&port->txq_used_mutex); | |
e4cfed38 | 1548 | |
b8d29252 | 1549 | *portp = port; |
72865317 BP |
1550 | |
1551 | return 0; | |
d17f4f08 | 1552 | |
d17f4f08 | 1553 | out: |
b8d29252 | 1554 | netdev_close(netdev); |
d17f4f08 | 1555 | return error; |
72865317 BP |
1556 | } |
1557 | ||
b8d29252 DDP |
1558 | static int |
1559 | do_add_port(struct dp_netdev *dp, const char *devname, const char *type, | |
1560 | odp_port_t port_no) | |
1561 | OVS_REQUIRES(dp->port_mutex) | |
1562 | { | |
1563 | struct dp_netdev_port *port; | |
1564 | int error; | |
1565 | ||
1566 | /* Reject devices already in 'dp'. */ | |
1567 | if (!get_port_by_name(dp, devname, &port)) { | |
1568 | return EEXIST; | |
1569 | } | |
1570 | ||
a3e8437a | 1571 | error = port_create(devname, type, port_no, &port); |
b8d29252 DDP |
1572 | if (error) { |
1573 | return error; | |
1574 | } | |
1575 | ||
e9985d6a | 1576 | hmap_insert(&dp->ports, &port->node, hash_port_no(port_no)); |
b8d29252 DDP |
1577 | seq_change(dp->port_seq); |
1578 | ||
e32971b8 DDP |
1579 | reconfigure_datapath(dp); |
1580 | ||
b8d29252 DDP |
1581 | return 0; |
1582 | } | |
1583 | ||
247527db BP |
1584 | static int |
1585 | dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev, | |
4e022ec0 | 1586 | odp_port_t *port_nop) |
247527db BP |
1587 | { |
1588 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
3aa30359 BP |
1589 | char namebuf[NETDEV_VPORT_NAME_BUFSIZE]; |
1590 | const char *dpif_port; | |
4e022ec0 | 1591 | odp_port_t port_no; |
5279f8fd | 1592 | int error; |
247527db | 1593 | |
59e6d833 | 1594 | ovs_mutex_lock(&dp->port_mutex); |
3aa30359 | 1595 | dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf); |
4e022ec0 | 1596 | if (*port_nop != ODPP_NONE) { |
ff073a71 BP |
1597 | port_no = *port_nop; |
1598 | error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0; | |
232dfa4a | 1599 | } else { |
3aa30359 | 1600 | port_no = choose_port(dp, dpif_port); |
5279f8fd | 1601 | error = port_no == ODPP_NONE ? EFBIG : 0; |
232dfa4a | 1602 | } |
5279f8fd | 1603 | if (!error) { |
247527db | 1604 | *port_nop = port_no; |
5279f8fd | 1605 | error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no); |
247527db | 1606 | } |
59e6d833 | 1607 | ovs_mutex_unlock(&dp->port_mutex); |
5279f8fd BP |
1608 | |
1609 | return error; | |
72865317 BP |
1610 | } |
1611 | ||
1612 | static int | |
4e022ec0 | 1613 | dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no) |
72865317 BP |
1614 | { |
1615 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
5279f8fd BP |
1616 | int error; |
1617 | ||
59e6d833 | 1618 | ovs_mutex_lock(&dp->port_mutex); |
c40b890f BP |
1619 | if (port_no == ODPP_LOCAL) { |
1620 | error = EINVAL; | |
1621 | } else { | |
1622 | struct dp_netdev_port *port; | |
1623 | ||
1624 | error = get_port_by_number(dp, port_no, &port); | |
1625 | if (!error) { | |
1626 | do_del_port(dp, port); | |
1627 | } | |
1628 | } | |
59e6d833 | 1629 | ovs_mutex_unlock(&dp->port_mutex); |
5279f8fd BP |
1630 | |
1631 | return error; | |
72865317 BP |
1632 | } |
1633 | ||
1634 | static bool | |
4e022ec0 | 1635 | is_valid_port_number(odp_port_t port_no) |
72865317 | 1636 | { |
ff073a71 BP |
1637 | return port_no != ODPP_NONE; |
1638 | } | |
1639 | ||
1640 | static struct dp_netdev_port * | |
1641 | dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no) | |
e9985d6a | 1642 | OVS_REQUIRES(dp->port_mutex) |
ff073a71 BP |
1643 | { |
1644 | struct dp_netdev_port *port; | |
1645 | ||
e9985d6a | 1646 | HMAP_FOR_EACH_WITH_HASH (port, node, hash_port_no(port_no), &dp->ports) { |
35303d71 | 1647 | if (port->port_no == port_no) { |
ff073a71 BP |
1648 | return port; |
1649 | } | |
1650 | } | |
1651 | return NULL; | |
72865317 BP |
1652 | } |
1653 | ||
1654 | static int | |
1655 | get_port_by_number(struct dp_netdev *dp, | |
4e022ec0 | 1656 | odp_port_t port_no, struct dp_netdev_port **portp) |
e9985d6a | 1657 | OVS_REQUIRES(dp->port_mutex) |
72865317 BP |
1658 | { |
1659 | if (!is_valid_port_number(port_no)) { | |
1660 | *portp = NULL; | |
1661 | return EINVAL; | |
1662 | } else { | |
ff073a71 | 1663 | *portp = dp_netdev_lookup_port(dp, port_no); |
0f6a066f | 1664 | return *portp ? 0 : ENODEV; |
72865317 BP |
1665 | } |
1666 | } | |
1667 | ||
b284085e | 1668 | static void |
62453dad | 1669 | port_destroy(struct dp_netdev_port *port) |
b284085e | 1670 | { |
62453dad DDP |
1671 | if (!port) { |
1672 | return; | |
b284085e | 1673 | } |
b284085e | 1674 | |
62453dad DDP |
1675 | netdev_close(port->netdev); |
1676 | netdev_restore_flags(port->sf); | |
accf8626 | 1677 | |
62453dad | 1678 | for (unsigned i = 0; i < port->n_rxq; i++) { |
947dc567 | 1679 | netdev_rxq_close(port->rxqs[i].rx); |
b284085e | 1680 | } |
324c8374 | 1681 | ovs_mutex_destroy(&port->txq_used_mutex); |
3eb67853 | 1682 | free(port->rxq_affinity_list); |
324c8374 | 1683 | free(port->txq_used); |
3eb67853 | 1684 | free(port->rxqs); |
62453dad DDP |
1685 | free(port->type); |
1686 | free(port); | |
b284085e PS |
1687 | } |
1688 | ||
72865317 BP |
1689 | static int |
1690 | get_port_by_name(struct dp_netdev *dp, | |
1691 | const char *devname, struct dp_netdev_port **portp) | |
59e6d833 | 1692 | OVS_REQUIRES(dp->port_mutex) |
72865317 BP |
1693 | { |
1694 | struct dp_netdev_port *port; | |
1695 | ||
e9985d6a | 1696 | HMAP_FOR_EACH (port, node, &dp->ports) { |
3efb6063 | 1697 | if (!strcmp(netdev_get_name(port->netdev), devname)) { |
72865317 BP |
1698 | *portp = port; |
1699 | return 0; | |
1700 | } | |
1701 | } | |
0f6a066f DDP |
1702 | |
1703 | /* Callers of dpif_netdev_port_query_by_name() expect ENODEV for a non | |
1704 | * existing port. */ | |
1705 | return ENODEV; | |
72865317 BP |
1706 | } |
1707 | ||
b9584f21 | 1708 | /* Returns 'true' if there is a port with pmd netdev. */ |
65f13b50 | 1709 | static bool |
b9584f21 | 1710 | has_pmd_port(struct dp_netdev *dp) |
e9985d6a | 1711 | OVS_REQUIRES(dp->port_mutex) |
65f13b50 AW |
1712 | { |
1713 | struct dp_netdev_port *port; | |
1714 | ||
e9985d6a | 1715 | HMAP_FOR_EACH (port, node, &dp->ports) { |
5dd57e80 | 1716 | if (netdev_is_pmd(port->netdev)) { |
b9584f21 | 1717 | return true; |
65f13b50 AW |
1718 | } |
1719 | } | |
1720 | ||
1721 | return false; | |
1722 | } | |
1723 | ||
c40b890f BP |
1724 | static void |
1725 | do_del_port(struct dp_netdev *dp, struct dp_netdev_port *port) | |
59e6d833 | 1726 | OVS_REQUIRES(dp->port_mutex) |
72865317 | 1727 | { |
e9985d6a | 1728 | hmap_remove(&dp->ports, &port->node); |
d33ed218 | 1729 | seq_change(dp->port_seq); |
d0cca6c3 | 1730 | |
e32971b8 | 1731 | reconfigure_datapath(dp); |
72865317 | 1732 | |
62453dad | 1733 | port_destroy(port); |
72865317 BP |
1734 | } |
1735 | ||
1736 | static void | |
4c738a8d BP |
1737 | answer_port_query(const struct dp_netdev_port *port, |
1738 | struct dpif_port *dpif_port) | |
72865317 | 1739 | { |
3efb6063 | 1740 | dpif_port->name = xstrdup(netdev_get_name(port->netdev)); |
0cbfe35d | 1741 | dpif_port->type = xstrdup(port->type); |
35303d71 | 1742 | dpif_port->port_no = port->port_no; |
72865317 BP |
1743 | } |
1744 | ||
1745 | static int | |
4e022ec0 | 1746 | dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no, |
4c738a8d | 1747 | struct dpif_port *dpif_port) |
72865317 BP |
1748 | { |
1749 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
1750 | struct dp_netdev_port *port; | |
1751 | int error; | |
1752 | ||
e9985d6a | 1753 | ovs_mutex_lock(&dp->port_mutex); |
72865317 | 1754 | error = get_port_by_number(dp, port_no, &port); |
4afba28d | 1755 | if (!error && dpif_port) { |
4c738a8d | 1756 | answer_port_query(port, dpif_port); |
72865317 | 1757 | } |
e9985d6a | 1758 | ovs_mutex_unlock(&dp->port_mutex); |
5279f8fd | 1759 | |
72865317 BP |
1760 | return error; |
1761 | } | |
1762 | ||
1763 | static int | |
1764 | dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname, | |
4c738a8d | 1765 | struct dpif_port *dpif_port) |
72865317 BP |
1766 | { |
1767 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
1768 | struct dp_netdev_port *port; | |
1769 | int error; | |
1770 | ||
59e6d833 | 1771 | ovs_mutex_lock(&dp->port_mutex); |
72865317 | 1772 | error = get_port_by_name(dp, devname, &port); |
4afba28d | 1773 | if (!error && dpif_port) { |
4c738a8d | 1774 | answer_port_query(port, dpif_port); |
72865317 | 1775 | } |
59e6d833 | 1776 | ovs_mutex_unlock(&dp->port_mutex); |
5279f8fd | 1777 | |
72865317 BP |
1778 | return error; |
1779 | } | |
1780 | ||
61e7deb1 BP |
1781 | static void |
1782 | dp_netdev_flow_free(struct dp_netdev_flow *flow) | |
1783 | { | |
61e7deb1 | 1784 | dp_netdev_actions_free(dp_netdev_flow_get_actions(flow)); |
61e7deb1 BP |
1785 | free(flow); |
1786 | } | |
1787 | ||
ed79f89a DDP |
1788 | static void dp_netdev_flow_unref(struct dp_netdev_flow *flow) |
1789 | { | |
1790 | if (ovs_refcount_unref_relaxed(&flow->ref_cnt) == 1) { | |
1791 | ovsrcu_postpone(dp_netdev_flow_free, flow); | |
1792 | } | |
1793 | } | |
1794 | ||
70e5ed6f JS |
1795 | static uint32_t |
1796 | dp_netdev_flow_hash(const ovs_u128 *ufid) | |
1797 | { | |
1798 | return ufid->u32[0]; | |
1799 | } | |
1800 | ||
3453b4d6 JS |
1801 | static inline struct dpcls * |
1802 | dp_netdev_pmd_lookup_dpcls(struct dp_netdev_pmd_thread *pmd, | |
1803 | odp_port_t in_port) | |
1804 | { | |
1805 | struct dpcls *cls; | |
1806 | uint32_t hash = hash_port_no(in_port); | |
1807 | CMAP_FOR_EACH_WITH_HASH (cls, node, hash, &pmd->classifiers) { | |
1808 | if (cls->in_port == in_port) { | |
1809 | /* Port classifier exists already */ | |
1810 | return cls; | |
1811 | } | |
1812 | } | |
1813 | return NULL; | |
1814 | } | |
1815 | ||
1816 | static inline struct dpcls * | |
1817 | dp_netdev_pmd_find_dpcls(struct dp_netdev_pmd_thread *pmd, | |
1818 | odp_port_t in_port) | |
1819 | OVS_REQUIRES(pmd->flow_mutex) | |
1820 | { | |
1821 | struct dpcls *cls = dp_netdev_pmd_lookup_dpcls(pmd, in_port); | |
1822 | uint32_t hash = hash_port_no(in_port); | |
1823 | ||
1824 | if (!cls) { | |
1825 | /* Create new classifier for in_port */ | |
1826 | cls = xmalloc(sizeof(*cls)); | |
1827 | dpcls_init(cls); | |
1828 | cls->in_port = in_port; | |
1829 | cmap_insert(&pmd->classifiers, &cls->node, hash); | |
1830 | VLOG_DBG("Creating dpcls %p for in_port %d", cls, in_port); | |
1831 | } | |
1832 | return cls; | |
1833 | } | |
1834 | ||
72865317 | 1835 | static void |
1c1e46ed AW |
1836 | dp_netdev_pmd_remove_flow(struct dp_netdev_pmd_thread *pmd, |
1837 | struct dp_netdev_flow *flow) | |
1838 | OVS_REQUIRES(pmd->flow_mutex) | |
72865317 | 1839 | { |
9f361d6b | 1840 | struct cmap_node *node = CONST_CAST(struct cmap_node *, &flow->node); |
3453b4d6 JS |
1841 | struct dpcls *cls; |
1842 | odp_port_t in_port = flow->flow.in_port.odp_port; | |
2c0ea78f | 1843 | |
3453b4d6 JS |
1844 | cls = dp_netdev_pmd_lookup_dpcls(pmd, in_port); |
1845 | ovs_assert(cls != NULL); | |
1846 | dpcls_remove(cls, &flow->cr); | |
1c1e46ed | 1847 | cmap_remove(&pmd->flow_table, node, dp_netdev_flow_hash(&flow->ufid)); |
9bbf1c3d | 1848 | flow->dead = true; |
ed79f89a DDP |
1849 | |
1850 | dp_netdev_flow_unref(flow); | |
72865317 BP |
1851 | } |
1852 | ||
1853 | static void | |
1c1e46ed | 1854 | dp_netdev_pmd_flow_flush(struct dp_netdev_pmd_thread *pmd) |
72865317 | 1855 | { |
78c8df12 | 1856 | struct dp_netdev_flow *netdev_flow; |
72865317 | 1857 | |
1c1e46ed AW |
1858 | ovs_mutex_lock(&pmd->flow_mutex); |
1859 | CMAP_FOR_EACH (netdev_flow, node, &pmd->flow_table) { | |
1860 | dp_netdev_pmd_remove_flow(pmd, netdev_flow); | |
72865317 | 1861 | } |
1c1e46ed | 1862 | ovs_mutex_unlock(&pmd->flow_mutex); |
72865317 BP |
1863 | } |
1864 | ||
1865 | static int | |
1866 | dpif_netdev_flow_flush(struct dpif *dpif) | |
1867 | { | |
1868 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
1c1e46ed AW |
1869 | struct dp_netdev_pmd_thread *pmd; |
1870 | ||
1871 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
1872 | dp_netdev_pmd_flow_flush(pmd); | |
1873 | } | |
5279f8fd | 1874 | |
72865317 BP |
1875 | return 0; |
1876 | } | |
1877 | ||
b0ec0f27 | 1878 | struct dp_netdev_port_state { |
e9985d6a | 1879 | struct hmap_position position; |
4c738a8d | 1880 | char *name; |
b0ec0f27 BP |
1881 | }; |
1882 | ||
1883 | static int | |
1884 | dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep) | |
1885 | { | |
1886 | *statep = xzalloc(sizeof(struct dp_netdev_port_state)); | |
1887 | return 0; | |
1888 | } | |
1889 | ||
72865317 | 1890 | static int |
b0ec0f27 | 1891 | dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_, |
4c738a8d | 1892 | struct dpif_port *dpif_port) |
72865317 | 1893 | { |
b0ec0f27 | 1894 | struct dp_netdev_port_state *state = state_; |
72865317 | 1895 | struct dp_netdev *dp = get_dp_netdev(dpif); |
e9985d6a | 1896 | struct hmap_node *node; |
ff073a71 | 1897 | int retval; |
72865317 | 1898 | |
e9985d6a DDP |
1899 | ovs_mutex_lock(&dp->port_mutex); |
1900 | node = hmap_at_position(&dp->ports, &state->position); | |
ff073a71 BP |
1901 | if (node) { |
1902 | struct dp_netdev_port *port; | |
5279f8fd | 1903 | |
ff073a71 BP |
1904 | port = CONTAINER_OF(node, struct dp_netdev_port, node); |
1905 | ||
1906 | free(state->name); | |
1907 | state->name = xstrdup(netdev_get_name(port->netdev)); | |
1908 | dpif_port->name = state->name; | |
1909 | dpif_port->type = port->type; | |
35303d71 | 1910 | dpif_port->port_no = port->port_no; |
ff073a71 BP |
1911 | |
1912 | retval = 0; | |
1913 | } else { | |
1914 | retval = EOF; | |
72865317 | 1915 | } |
e9985d6a | 1916 | ovs_mutex_unlock(&dp->port_mutex); |
5279f8fd | 1917 | |
ff073a71 | 1918 | return retval; |
b0ec0f27 BP |
1919 | } |
1920 | ||
1921 | static int | |
4c738a8d | 1922 | dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_) |
b0ec0f27 | 1923 | { |
4c738a8d BP |
1924 | struct dp_netdev_port_state *state = state_; |
1925 | free(state->name); | |
b0ec0f27 BP |
1926 | free(state); |
1927 | return 0; | |
72865317 BP |
1928 | } |
1929 | ||
1930 | static int | |
67a4917b | 1931 | dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED) |
72865317 BP |
1932 | { |
1933 | struct dpif_netdev *dpif = dpif_netdev_cast(dpif_); | |
d33ed218 | 1934 | uint64_t new_port_seq; |
5279f8fd BP |
1935 | int error; |
1936 | ||
d33ed218 BP |
1937 | new_port_seq = seq_read(dpif->dp->port_seq); |
1938 | if (dpif->last_port_seq != new_port_seq) { | |
1939 | dpif->last_port_seq = new_port_seq; | |
5279f8fd | 1940 | error = ENOBUFS; |
72865317 | 1941 | } else { |
5279f8fd | 1942 | error = EAGAIN; |
72865317 | 1943 | } |
5279f8fd BP |
1944 | |
1945 | return error; | |
72865317 BP |
1946 | } |
1947 | ||
1948 | static void | |
1949 | dpif_netdev_port_poll_wait(const struct dpif *dpif_) | |
1950 | { | |
1951 | struct dpif_netdev *dpif = dpif_netdev_cast(dpif_); | |
5279f8fd | 1952 | |
d33ed218 | 1953 | seq_wait(dpif->dp->port_seq, dpif->last_port_seq); |
8a4e3a85 BP |
1954 | } |
1955 | ||
1956 | static struct dp_netdev_flow * | |
0de8783a | 1957 | dp_netdev_flow_cast(const struct dpcls_rule *cr) |
8a4e3a85 BP |
1958 | { |
1959 | return cr ? CONTAINER_OF(cr, struct dp_netdev_flow, cr) : NULL; | |
72865317 BP |
1960 | } |
1961 | ||
9bbf1c3d DDP |
1962 | static bool dp_netdev_flow_ref(struct dp_netdev_flow *flow) |
1963 | { | |
1964 | return ovs_refcount_try_ref_rcu(&flow->ref_cnt); | |
1965 | } | |
1966 | ||
79df317f DDP |
1967 | /* netdev_flow_key utilities. |
1968 | * | |
1969 | * netdev_flow_key is basically a miniflow. We use these functions | |
1970 | * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow | |
1971 | * functions (miniflow_clone_inline, miniflow_equal, ...), because: | |
1972 | * | |
1973 | * - Since we are dealing exclusively with miniflows created by | |
1974 | * miniflow_extract(), if the map is different the miniflow is different. | |
1975 | * Therefore we can be faster by comparing the map and the miniflow in a | |
1976 | * single memcmp(). | |
5fcff47b | 1977 | * - These functions can be inlined by the compiler. */ |
79df317f | 1978 | |
361d808d | 1979 | /* Given the number of bits set in miniflow's maps, returns the size of the |
caeb4906 | 1980 | * 'netdev_flow_key.mf' */ |
361d808d JR |
1981 | static inline size_t |
1982 | netdev_flow_key_size(size_t flow_u64s) | |
79df317f | 1983 | { |
361d808d | 1984 | return sizeof(struct miniflow) + MINIFLOW_VALUES_SIZE(flow_u64s); |
79df317f DDP |
1985 | } |
1986 | ||
79df317f DDP |
1987 | static inline bool |
1988 | netdev_flow_key_equal(const struct netdev_flow_key *a, | |
0de8783a JR |
1989 | const struct netdev_flow_key *b) |
1990 | { | |
caeb4906 JR |
1991 | /* 'b->len' may be not set yet. */ |
1992 | return a->hash == b->hash && !memcmp(&a->mf, &b->mf, a->len); | |
0de8783a JR |
1993 | } |
1994 | ||
1995 | /* Used to compare 'netdev_flow_key' in the exact match cache to a miniflow. | |
d79a39fe | 1996 | * The maps are compared bitwise, so both 'key->mf' and 'mf' must have been |
0de8783a JR |
1997 | * generated by miniflow_extract. */ |
1998 | static inline bool | |
1999 | netdev_flow_key_equal_mf(const struct netdev_flow_key *key, | |
2000 | const struct miniflow *mf) | |
79df317f | 2001 | { |
caeb4906 | 2002 | return !memcmp(&key->mf, mf, key->len); |
79df317f DDP |
2003 | } |
2004 | ||
2005 | static inline void | |
2006 | netdev_flow_key_clone(struct netdev_flow_key *dst, | |
0de8783a JR |
2007 | const struct netdev_flow_key *src) |
2008 | { | |
caeb4906 JR |
2009 | memcpy(dst, src, |
2010 | offsetof(struct netdev_flow_key, mf) + src->len); | |
0de8783a JR |
2011 | } |
2012 | ||
0de8783a JR |
2013 | /* Initialize a netdev_flow_key 'mask' from 'match'. */ |
2014 | static inline void | |
2015 | netdev_flow_mask_init(struct netdev_flow_key *mask, | |
2016 | const struct match *match) | |
2017 | { | |
09b0fa9c | 2018 | uint64_t *dst = miniflow_values(&mask->mf); |
5fcff47b | 2019 | struct flowmap fmap; |
0de8783a | 2020 | uint32_t hash = 0; |
5fcff47b | 2021 | size_t idx; |
0de8783a JR |
2022 | |
2023 | /* Only check masks that make sense for the flow. */ | |
5fcff47b JR |
2024 | flow_wc_map(&match->flow, &fmap); |
2025 | flowmap_init(&mask->mf.map); | |
0de8783a | 2026 | |
5fcff47b JR |
2027 | FLOWMAP_FOR_EACH_INDEX(idx, fmap) { |
2028 | uint64_t mask_u64 = flow_u64_value(&match->wc.masks, idx); | |
0de8783a | 2029 | |
5fcff47b JR |
2030 | if (mask_u64) { |
2031 | flowmap_set(&mask->mf.map, idx, 1); | |
2032 | *dst++ = mask_u64; | |
2033 | hash = hash_add64(hash, mask_u64); | |
0de8783a | 2034 | } |
0de8783a JR |
2035 | } |
2036 | ||
5fcff47b | 2037 | map_t map; |
0de8783a | 2038 | |
5fcff47b JR |
2039 | FLOWMAP_FOR_EACH_MAP (map, mask->mf.map) { |
2040 | hash = hash_add64(hash, map); | |
2041 | } | |
0de8783a | 2042 | |
5fcff47b | 2043 | size_t n = dst - miniflow_get_values(&mask->mf); |
0de8783a | 2044 | |
d70e8c28 | 2045 | mask->hash = hash_finish(hash, n * 8); |
0de8783a JR |
2046 | mask->len = netdev_flow_key_size(n); |
2047 | } | |
2048 | ||
361d808d | 2049 | /* Initializes 'dst' as a copy of 'flow' masked with 'mask'. */ |
0de8783a JR |
2050 | static inline void |
2051 | netdev_flow_key_init_masked(struct netdev_flow_key *dst, | |
2052 | const struct flow *flow, | |
2053 | const struct netdev_flow_key *mask) | |
79df317f | 2054 | { |
09b0fa9c JR |
2055 | uint64_t *dst_u64 = miniflow_values(&dst->mf); |
2056 | const uint64_t *mask_u64 = miniflow_get_values(&mask->mf); | |
0de8783a | 2057 | uint32_t hash = 0; |
d70e8c28 | 2058 | uint64_t value; |
0de8783a JR |
2059 | |
2060 | dst->len = mask->len; | |
361d808d | 2061 | dst->mf = mask->mf; /* Copy maps. */ |
0de8783a | 2062 | |
5fcff47b | 2063 | FLOW_FOR_EACH_IN_MAPS(value, flow, mask->mf.map) { |
d70e8c28 JR |
2064 | *dst_u64 = value & *mask_u64++; |
2065 | hash = hash_add64(hash, *dst_u64++); | |
0de8783a | 2066 | } |
09b0fa9c JR |
2067 | dst->hash = hash_finish(hash, |
2068 | (dst_u64 - miniflow_get_values(&dst->mf)) * 8); | |
0de8783a JR |
2069 | } |
2070 | ||
5fcff47b JR |
2071 | /* Iterate through netdev_flow_key TNL u64 values specified by 'FLOWMAP'. */ |
2072 | #define NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(VALUE, KEY, FLOWMAP) \ | |
2073 | MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, &(KEY)->mf, FLOWMAP) | |
0de8783a JR |
2074 | |
2075 | /* Returns a hash value for the bits of 'key' where there are 1-bits in | |
2076 | * 'mask'. */ | |
2077 | static inline uint32_t | |
2078 | netdev_flow_key_hash_in_mask(const struct netdev_flow_key *key, | |
2079 | const struct netdev_flow_key *mask) | |
2080 | { | |
09b0fa9c | 2081 | const uint64_t *p = miniflow_get_values(&mask->mf); |
0de8783a | 2082 | uint32_t hash = 0; |
5fcff47b | 2083 | uint64_t value; |
0de8783a | 2084 | |
5fcff47b JR |
2085 | NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value, key, mask->mf.map) { |
2086 | hash = hash_add64(hash, value & *p++); | |
0de8783a JR |
2087 | } |
2088 | ||
09b0fa9c | 2089 | return hash_finish(hash, (p - miniflow_get_values(&mask->mf)) * 8); |
79df317f DDP |
2090 | } |
2091 | ||
9bbf1c3d DDP |
2092 | static inline bool |
2093 | emc_entry_alive(struct emc_entry *ce) | |
2094 | { | |
2095 | return ce->flow && !ce->flow->dead; | |
2096 | } | |
2097 | ||
2098 | static void | |
2099 | emc_clear_entry(struct emc_entry *ce) | |
2100 | { | |
2101 | if (ce->flow) { | |
2102 | dp_netdev_flow_unref(ce->flow); | |
2103 | ce->flow = NULL; | |
2104 | } | |
2105 | } | |
2106 | ||
2107 | static inline void | |
2108 | emc_change_entry(struct emc_entry *ce, struct dp_netdev_flow *flow, | |
0de8783a | 2109 | const struct netdev_flow_key *key) |
9bbf1c3d DDP |
2110 | { |
2111 | if (ce->flow != flow) { | |
2112 | if (ce->flow) { | |
2113 | dp_netdev_flow_unref(ce->flow); | |
2114 | } | |
2115 | ||
2116 | if (dp_netdev_flow_ref(flow)) { | |
2117 | ce->flow = flow; | |
2118 | } else { | |
2119 | ce->flow = NULL; | |
2120 | } | |
2121 | } | |
0de8783a JR |
2122 | if (key) { |
2123 | netdev_flow_key_clone(&ce->key, key); | |
9bbf1c3d DDP |
2124 | } |
2125 | } | |
2126 | ||
2127 | static inline void | |
0de8783a | 2128 | emc_insert(struct emc_cache *cache, const struct netdev_flow_key *key, |
9bbf1c3d DDP |
2129 | struct dp_netdev_flow *flow) |
2130 | { | |
2131 | struct emc_entry *to_be_replaced = NULL; | |
2132 | struct emc_entry *current_entry; | |
2133 | ||
0de8783a JR |
2134 | EMC_FOR_EACH_POS_WITH_HASH(cache, current_entry, key->hash) { |
2135 | if (netdev_flow_key_equal(¤t_entry->key, key)) { | |
9bbf1c3d | 2136 | /* We found the entry with the 'mf' miniflow */ |
0de8783a | 2137 | emc_change_entry(current_entry, flow, NULL); |
9bbf1c3d DDP |
2138 | return; |
2139 | } | |
2140 | ||
2141 | /* Replacement policy: put the flow in an empty (not alive) entry, or | |
2142 | * in the first entry where it can be */ | |
2143 | if (!to_be_replaced | |
2144 | || (emc_entry_alive(to_be_replaced) | |
2145 | && !emc_entry_alive(current_entry)) | |
0de8783a | 2146 | || current_entry->key.hash < to_be_replaced->key.hash) { |
9bbf1c3d DDP |
2147 | to_be_replaced = current_entry; |
2148 | } | |
2149 | } | |
2150 | /* We didn't find the miniflow in the cache. | |
2151 | * The 'to_be_replaced' entry is where the new flow will be stored */ | |
2152 | ||
0de8783a | 2153 | emc_change_entry(to_be_replaced, flow, key); |
9bbf1c3d DDP |
2154 | } |
2155 | ||
4c30b246 CL |
2156 | static inline void |
2157 | emc_probabilistic_insert(struct dp_netdev_pmd_thread *pmd, | |
2158 | const struct netdev_flow_key *key, | |
2159 | struct dp_netdev_flow *flow) | |
2160 | { | |
2161 | /* Insert an entry into the EMC based on probability value 'min'. By | |
2162 | * default the value is UINT32_MAX / 100 which yields an insertion | |
2163 | * probability of 1/100 ie. 1% */ | |
2164 | ||
2165 | uint32_t min; | |
2166 | atomic_read_relaxed(&pmd->dp->emc_insert_min, &min); | |
2167 | ||
656238ee | 2168 | if (min && random_uint32() <= min) { |
4c30b246 CL |
2169 | emc_insert(&pmd->flow_cache, key, flow); |
2170 | } | |
2171 | } | |
2172 | ||
9bbf1c3d | 2173 | static inline struct dp_netdev_flow * |
0de8783a | 2174 | emc_lookup(struct emc_cache *cache, const struct netdev_flow_key *key) |
9bbf1c3d DDP |
2175 | { |
2176 | struct emc_entry *current_entry; | |
2177 | ||
0de8783a JR |
2178 | EMC_FOR_EACH_POS_WITH_HASH(cache, current_entry, key->hash) { |
2179 | if (current_entry->key.hash == key->hash | |
2180 | && emc_entry_alive(current_entry) | |
2181 | && netdev_flow_key_equal_mf(¤t_entry->key, &key->mf)) { | |
9bbf1c3d | 2182 | |
0de8783a | 2183 | /* We found the entry with the 'key->mf' miniflow */ |
9bbf1c3d DDP |
2184 | return current_entry->flow; |
2185 | } | |
2186 | } | |
2187 | ||
2188 | return NULL; | |
2189 | } | |
2190 | ||
72865317 | 2191 | static struct dp_netdev_flow * |
3453b4d6 JS |
2192 | dp_netdev_pmd_lookup_flow(struct dp_netdev_pmd_thread *pmd, |
2193 | const struct netdev_flow_key *key, | |
2194 | int *lookup_num_p) | |
2c0ea78f | 2195 | { |
3453b4d6 | 2196 | struct dpcls *cls; |
0de8783a | 2197 | struct dpcls_rule *rule; |
3453b4d6 JS |
2198 | odp_port_t in_port = u32_to_odp(MINIFLOW_GET_U32(&key->mf, in_port)); |
2199 | struct dp_netdev_flow *netdev_flow = NULL; | |
2c0ea78f | 2200 | |
3453b4d6 JS |
2201 | cls = dp_netdev_pmd_lookup_dpcls(pmd, in_port); |
2202 | if (OVS_LIKELY(cls)) { | |
2203 | dpcls_lookup(cls, key, &rule, 1, lookup_num_p); | |
2204 | netdev_flow = dp_netdev_flow_cast(rule); | |
2205 | } | |
8a4e3a85 | 2206 | return netdev_flow; |
2c0ea78f GS |
2207 | } |
2208 | ||
2209 | static struct dp_netdev_flow * | |
1c1e46ed AW |
2210 | dp_netdev_pmd_find_flow(const struct dp_netdev_pmd_thread *pmd, |
2211 | const ovs_u128 *ufidp, const struct nlattr *key, | |
2212 | size_t key_len) | |
72865317 | 2213 | { |
1763b4b8 | 2214 | struct dp_netdev_flow *netdev_flow; |
70e5ed6f JS |
2215 | struct flow flow; |
2216 | ovs_u128 ufid; | |
2217 | ||
2218 | /* If a UFID is not provided, determine one based on the key. */ | |
2219 | if (!ufidp && key && key_len | |
f0fb825a | 2220 | && !dpif_netdev_flow_from_nlattrs(key, key_len, &flow, false)) { |
1c1e46ed | 2221 | dpif_flow_hash(pmd->dp->dpif, &flow, sizeof flow, &ufid); |
70e5ed6f JS |
2222 | ufidp = &ufid; |
2223 | } | |
72865317 | 2224 | |
70e5ed6f JS |
2225 | if (ufidp) { |
2226 | CMAP_FOR_EACH_WITH_HASH (netdev_flow, node, dp_netdev_flow_hash(ufidp), | |
1c1e46ed | 2227 | &pmd->flow_table) { |
2ff8484b | 2228 | if (ovs_u128_equals(netdev_flow->ufid, *ufidp)) { |
70e5ed6f JS |
2229 | return netdev_flow; |
2230 | } | |
72865317 BP |
2231 | } |
2232 | } | |
8a4e3a85 | 2233 | |
72865317 BP |
2234 | return NULL; |
2235 | } | |
2236 | ||
2237 | static void | |
eb94da30 | 2238 | get_dpif_flow_stats(const struct dp_netdev_flow *netdev_flow_, |
1763b4b8 | 2239 | struct dpif_flow_stats *stats) |
feebdea2 | 2240 | { |
eb94da30 DDP |
2241 | struct dp_netdev_flow *netdev_flow; |
2242 | unsigned long long n; | |
2243 | long long used; | |
2244 | uint16_t flags; | |
2245 | ||
2246 | netdev_flow = CONST_CAST(struct dp_netdev_flow *, netdev_flow_); | |
2247 | ||
2248 | atomic_read_relaxed(&netdev_flow->stats.packet_count, &n); | |
2249 | stats->n_packets = n; | |
2250 | atomic_read_relaxed(&netdev_flow->stats.byte_count, &n); | |
2251 | stats->n_bytes = n; | |
2252 | atomic_read_relaxed(&netdev_flow->stats.used, &used); | |
2253 | stats->used = used; | |
2254 | atomic_read_relaxed(&netdev_flow->stats.tcp_flags, &flags); | |
2255 | stats->tcp_flags = flags; | |
72865317 BP |
2256 | } |
2257 | ||
7af12bd7 JS |
2258 | /* Converts to the dpif_flow format, using 'key_buf' and 'mask_buf' for |
2259 | * storing the netlink-formatted key/mask. 'key_buf' may be the same as | |
2260 | * 'mask_buf'. Actions will be returned without copying, by relying on RCU to | |
2261 | * protect them. */ | |
6fe09f8c | 2262 | static void |
70e5ed6f | 2263 | dp_netdev_flow_to_dpif_flow(const struct dp_netdev_flow *netdev_flow, |
7af12bd7 | 2264 | struct ofpbuf *key_buf, struct ofpbuf *mask_buf, |
64bb477f | 2265 | struct dpif_flow *flow, bool terse) |
6fe09f8c | 2266 | { |
64bb477f JS |
2267 | if (terse) { |
2268 | memset(flow, 0, sizeof *flow); | |
2269 | } else { | |
2270 | struct flow_wildcards wc; | |
2271 | struct dp_netdev_actions *actions; | |
2272 | size_t offset; | |
5262eea1 JG |
2273 | struct odp_flow_key_parms odp_parms = { |
2274 | .flow = &netdev_flow->flow, | |
2275 | .mask = &wc.masks, | |
2494ccd7 | 2276 | .support = dp_netdev_support, |
5262eea1 | 2277 | }; |
64bb477f JS |
2278 | |
2279 | miniflow_expand(&netdev_flow->cr.mask->mf, &wc.masks); | |
f4b835bb JR |
2280 | /* in_port is exact matched, but we have left it out from the mask for |
2281 | * optimnization reasons. Add in_port back to the mask. */ | |
2282 | wc.masks.in_port.odp_port = ODPP_NONE; | |
64bb477f JS |
2283 | |
2284 | /* Key */ | |
6fd6ed71 | 2285 | offset = key_buf->size; |
64bb477f | 2286 | flow->key = ofpbuf_tail(key_buf); |
5262eea1 | 2287 | odp_flow_key_from_flow(&odp_parms, key_buf); |
6fd6ed71 | 2288 | flow->key_len = key_buf->size - offset; |
64bb477f JS |
2289 | |
2290 | /* Mask */ | |
6fd6ed71 | 2291 | offset = mask_buf->size; |
64bb477f | 2292 | flow->mask = ofpbuf_tail(mask_buf); |
ec1f6f32 | 2293 | odp_parms.key_buf = key_buf; |
5262eea1 | 2294 | odp_flow_key_from_mask(&odp_parms, mask_buf); |
6fd6ed71 | 2295 | flow->mask_len = mask_buf->size - offset; |
64bb477f JS |
2296 | |
2297 | /* Actions */ | |
2298 | actions = dp_netdev_flow_get_actions(netdev_flow); | |
2299 | flow->actions = actions->actions; | |
2300 | flow->actions_len = actions->size; | |
2301 | } | |
6fe09f8c | 2302 | |
70e5ed6f JS |
2303 | flow->ufid = netdev_flow->ufid; |
2304 | flow->ufid_present = true; | |
1c1e46ed | 2305 | flow->pmd_id = netdev_flow->pmd_id; |
6fe09f8c JS |
2306 | get_dpif_flow_stats(netdev_flow, &flow->stats); |
2307 | } | |
2308 | ||
36956a7d | 2309 | static int |
8c301900 JR |
2310 | dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len, |
2311 | const struct nlattr *mask_key, | |
2312 | uint32_t mask_key_len, const struct flow *flow, | |
f0fb825a | 2313 | struct flow_wildcards *wc, bool probe) |
8c301900 | 2314 | { |
ca8d3442 DDP |
2315 | enum odp_key_fitness fitness; |
2316 | ||
8d8ab6c2 | 2317 | fitness = odp_flow_key_to_mask(mask_key, mask_key_len, wc, flow); |
ca8d3442 | 2318 | if (fitness) { |
f0fb825a EG |
2319 | if (!probe) { |
2320 | /* This should not happen: it indicates that | |
2321 | * odp_flow_key_from_mask() and odp_flow_key_to_mask() | |
2322 | * disagree on the acceptable form of a mask. Log the problem | |
2323 | * as an error, with enough details to enable debugging. */ | |
2324 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
2325 | ||
2326 | if (!VLOG_DROP_ERR(&rl)) { | |
2327 | struct ds s; | |
2328 | ||
2329 | ds_init(&s); | |
2330 | odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s, | |
2331 | true); | |
2332 | VLOG_ERR("internal error parsing flow mask %s (%s)", | |
2333 | ds_cstr(&s), odp_key_fitness_to_string(fitness)); | |
2334 | ds_destroy(&s); | |
2335 | } | |
8c301900 | 2336 | } |
ca8d3442 DDP |
2337 | |
2338 | return EINVAL; | |
8c301900 JR |
2339 | } |
2340 | ||
2341 | return 0; | |
2342 | } | |
2343 | ||
2344 | static int | |
2345 | dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len, | |
f0fb825a | 2346 | struct flow *flow, bool probe) |
36956a7d | 2347 | { |
8d8ab6c2 | 2348 | if (odp_flow_key_to_flow(key, key_len, flow)) { |
f0fb825a EG |
2349 | if (!probe) { |
2350 | /* This should not happen: it indicates that | |
2351 | * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on | |
2352 | * the acceptable form of a flow. Log the problem as an error, | |
2353 | * with enough details to enable debugging. */ | |
2354 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
2355 | ||
2356 | if (!VLOG_DROP_ERR(&rl)) { | |
2357 | struct ds s; | |
2358 | ||
2359 | ds_init(&s); | |
2360 | odp_flow_format(key, key_len, NULL, 0, NULL, &s, true); | |
2361 | VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s)); | |
2362 | ds_destroy(&s); | |
2363 | } | |
36956a7d BP |
2364 | } |
2365 | ||
2366 | return EINVAL; | |
2367 | } | |
2368 | ||
5cf3edb3 | 2369 | if (flow->ct_state & DP_NETDEV_CS_UNSUPPORTED_MASK) { |
07659514 JS |
2370 | return EINVAL; |
2371 | } | |
2372 | ||
36956a7d BP |
2373 | return 0; |
2374 | } | |
2375 | ||
72865317 | 2376 | static int |
6fe09f8c | 2377 | dpif_netdev_flow_get(const struct dpif *dpif, const struct dpif_flow_get *get) |
72865317 BP |
2378 | { |
2379 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
1763b4b8 | 2380 | struct dp_netdev_flow *netdev_flow; |
1c1e46ed | 2381 | struct dp_netdev_pmd_thread *pmd; |
c673049c IM |
2382 | struct hmapx to_find = HMAPX_INITIALIZER(&to_find); |
2383 | struct hmapx_node *node; | |
2384 | int error = EINVAL; | |
2385 | ||
2386 | if (get->pmd_id == PMD_ID_NULL) { | |
2387 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
2388 | if (dp_netdev_pmd_try_ref(pmd) && !hmapx_add(&to_find, pmd)) { | |
2389 | dp_netdev_pmd_unref(pmd); | |
2390 | } | |
2391 | } | |
2392 | } else { | |
2393 | pmd = dp_netdev_get_pmd(dp, get->pmd_id); | |
2394 | if (!pmd) { | |
2395 | goto out; | |
2396 | } | |
2397 | hmapx_add(&to_find, pmd); | |
1c1e46ed AW |
2398 | } |
2399 | ||
c673049c IM |
2400 | if (!hmapx_count(&to_find)) { |
2401 | goto out; | |
72865317 | 2402 | } |
1c1e46ed | 2403 | |
c673049c IM |
2404 | HMAPX_FOR_EACH (node, &to_find) { |
2405 | pmd = (struct dp_netdev_pmd_thread *) node->data; | |
2406 | netdev_flow = dp_netdev_pmd_find_flow(pmd, get->ufid, get->key, | |
2407 | get->key_len); | |
2408 | if (netdev_flow) { | |
2409 | dp_netdev_flow_to_dpif_flow(netdev_flow, get->buffer, get->buffer, | |
2410 | get->flow, false); | |
2411 | error = 0; | |
2412 | break; | |
2413 | } else { | |
2414 | error = ENOENT; | |
2415 | } | |
2416 | } | |
bc4a05c6 | 2417 | |
c673049c IM |
2418 | HMAPX_FOR_EACH (node, &to_find) { |
2419 | pmd = (struct dp_netdev_pmd_thread *) node->data; | |
2420 | dp_netdev_pmd_unref(pmd); | |
2421 | } | |
2422 | out: | |
2423 | hmapx_destroy(&to_find); | |
5279f8fd | 2424 | return error; |
72865317 BP |
2425 | } |
2426 | ||
0de8783a | 2427 | static struct dp_netdev_flow * |
1c1e46ed AW |
2428 | dp_netdev_flow_add(struct dp_netdev_pmd_thread *pmd, |
2429 | struct match *match, const ovs_u128 *ufid, | |
ae2ceebd | 2430 | const struct nlattr *actions, size_t actions_len) |
1c1e46ed | 2431 | OVS_REQUIRES(pmd->flow_mutex) |
72865317 | 2432 | { |
0de8783a JR |
2433 | struct dp_netdev_flow *flow; |
2434 | struct netdev_flow_key mask; | |
3453b4d6 | 2435 | struct dpcls *cls; |
f4b835bb JR |
2436 | |
2437 | /* Make sure in_port is exact matched before we read it. */ | |
2438 | ovs_assert(match->wc.masks.in_port.odp_port == ODPP_NONE); | |
3453b4d6 | 2439 | odp_port_t in_port = match->flow.in_port.odp_port; |
ed79f89a | 2440 | |
f4b835bb JR |
2441 | /* As we select the dpcls based on the port number, each netdev flow |
2442 | * belonging to the same dpcls will have the same odp_port value. | |
2443 | * For performance reasons we wildcard odp_port here in the mask. In the | |
2444 | * typical case dp_hash is also wildcarded, and the resulting 8-byte | |
2445 | * chunk {dp_hash, in_port} will be ignored by netdev_flow_mask_init() and | |
2446 | * will not be part of the subtable mask. | |
2447 | * This will speed up the hash computation during dpcls_lookup() because | |
2448 | * there is one less call to hash_add64() in this case. */ | |
2449 | match->wc.masks.in_port.odp_port = 0; | |
0de8783a | 2450 | netdev_flow_mask_init(&mask, match); |
f4b835bb JR |
2451 | match->wc.masks.in_port.odp_port = ODPP_NONE; |
2452 | ||
0de8783a | 2453 | /* Make sure wc does not have metadata. */ |
5fcff47b JR |
2454 | ovs_assert(!FLOWMAP_HAS_FIELD(&mask.mf.map, metadata) |
2455 | && !FLOWMAP_HAS_FIELD(&mask.mf.map, regs)); | |
679ba04c | 2456 | |
0de8783a | 2457 | /* Do not allocate extra space. */ |
caeb4906 | 2458 | flow = xmalloc(sizeof *flow - sizeof flow->cr.flow.mf + mask.len); |
1c1e46ed | 2459 | memset(&flow->stats, 0, sizeof flow->stats); |
0de8783a | 2460 | flow->dead = false; |
11e5cf1f | 2461 | flow->batch = NULL; |
bd5131ba | 2462 | *CONST_CAST(unsigned *, &flow->pmd_id) = pmd->core_id; |
0de8783a | 2463 | *CONST_CAST(struct flow *, &flow->flow) = match->flow; |
70e5ed6f | 2464 | *CONST_CAST(ovs_u128 *, &flow->ufid) = *ufid; |
0de8783a | 2465 | ovs_refcount_init(&flow->ref_cnt); |
0de8783a | 2466 | ovsrcu_set(&flow->actions, dp_netdev_actions_create(actions, actions_len)); |
2c0ea78f | 2467 | |
0de8783a | 2468 | netdev_flow_key_init_masked(&flow->cr.flow, &match->flow, &mask); |
3453b4d6 | 2469 | |
f4b835bb | 2470 | /* Select dpcls for in_port. Relies on in_port to be exact match. */ |
3453b4d6 JS |
2471 | cls = dp_netdev_pmd_find_dpcls(pmd, in_port); |
2472 | dpcls_insert(cls, &flow->cr, &mask); | |
72865317 | 2473 | |
4c75aaab EJ |
2474 | cmap_insert(&pmd->flow_table, CONST_CAST(struct cmap_node *, &flow->node), |
2475 | dp_netdev_flow_hash(&flow->ufid)); | |
2476 | ||
beb75a40 | 2477 | if (OVS_UNLIKELY(!VLOG_DROP_DBG((&upcall_rl)))) { |
623540e4 | 2478 | struct ds ds = DS_EMPTY_INITIALIZER; |
9044f2c1 JG |
2479 | struct ofpbuf key_buf, mask_buf; |
2480 | struct odp_flow_key_parms odp_parms = { | |
2481 | .flow = &match->flow, | |
2482 | .mask = &match->wc.masks, | |
2483 | .support = dp_netdev_support, | |
2484 | }; | |
2485 | ||
2486 | ofpbuf_init(&key_buf, 0); | |
2487 | ofpbuf_init(&mask_buf, 0); | |
623540e4 | 2488 | |
9044f2c1 JG |
2489 | odp_flow_key_from_flow(&odp_parms, &key_buf); |
2490 | odp_parms.key_buf = &key_buf; | |
2491 | odp_flow_key_from_mask(&odp_parms, &mask_buf); | |
0de8783a | 2492 | |
623540e4 | 2493 | ds_put_cstr(&ds, "flow_add: "); |
70e5ed6f JS |
2494 | odp_format_ufid(ufid, &ds); |
2495 | ds_put_cstr(&ds, " "); | |
9044f2c1 JG |
2496 | odp_flow_format(key_buf.data, key_buf.size, |
2497 | mask_buf.data, mask_buf.size, | |
2498 | NULL, &ds, false); | |
623540e4 | 2499 | ds_put_cstr(&ds, ", actions:"); |
0722f341 | 2500 | format_odp_actions(&ds, actions, actions_len, NULL); |
623540e4 | 2501 | |
beb75a40 | 2502 | VLOG_DBG("%s", ds_cstr(&ds)); |
623540e4 | 2503 | |
9044f2c1 JG |
2504 | ofpbuf_uninit(&key_buf); |
2505 | ofpbuf_uninit(&mask_buf); | |
beb75a40 JS |
2506 | |
2507 | /* Add a printout of the actual match installed. */ | |
2508 | struct match m; | |
2509 | ds_clear(&ds); | |
2510 | ds_put_cstr(&ds, "flow match: "); | |
2511 | miniflow_expand(&flow->cr.flow.mf, &m.flow); | |
2512 | miniflow_expand(&flow->cr.mask->mf, &m.wc.masks); | |
b2f4b622 | 2513 | memset(&m.tun_md, 0, sizeof m.tun_md); |
beb75a40 JS |
2514 | match_format(&m, NULL, &ds, OFP_DEFAULT_PRIORITY); |
2515 | ||
2516 | VLOG_DBG("%s", ds_cstr(&ds)); | |
2517 | ||
623540e4 EJ |
2518 | ds_destroy(&ds); |
2519 | } | |
2520 | ||
0de8783a | 2521 | return flow; |
72865317 BP |
2522 | } |
2523 | ||
72865317 | 2524 | static int |
f5d317a1 DDP |
2525 | flow_put_on_pmd(struct dp_netdev_pmd_thread *pmd, |
2526 | struct netdev_flow_key *key, | |
2527 | struct match *match, | |
2528 | ovs_u128 *ufid, | |
2529 | const struct dpif_flow_put *put, | |
2530 | struct dpif_flow_stats *stats) | |
72865317 | 2531 | { |
1763b4b8 | 2532 | struct dp_netdev_flow *netdev_flow; |
f5d317a1 | 2533 | int error = 0; |
72865317 | 2534 | |
f5d317a1 DDP |
2535 | if (stats) { |
2536 | memset(stats, 0, sizeof *stats); | |
70e5ed6f JS |
2537 | } |
2538 | ||
1c1e46ed | 2539 | ovs_mutex_lock(&pmd->flow_mutex); |
f5d317a1 | 2540 | netdev_flow = dp_netdev_pmd_lookup_flow(pmd, key, NULL); |
1763b4b8 | 2541 | if (!netdev_flow) { |
89625d1e | 2542 | if (put->flags & DPIF_FP_CREATE) { |
1c1e46ed | 2543 | if (cmap_count(&pmd->flow_table) < MAX_FLOWS) { |
f5d317a1 | 2544 | dp_netdev_flow_add(pmd, match, ufid, put->actions, |
70e5ed6f | 2545 | put->actions_len); |
0de8783a | 2546 | error = 0; |
72865317 | 2547 | } else { |
5279f8fd | 2548 | error = EFBIG; |
72865317 BP |
2549 | } |
2550 | } else { | |
5279f8fd | 2551 | error = ENOENT; |
72865317 BP |
2552 | } |
2553 | } else { | |
beb75a40 | 2554 | if (put->flags & DPIF_FP_MODIFY) { |
8a4e3a85 BP |
2555 | struct dp_netdev_actions *new_actions; |
2556 | struct dp_netdev_actions *old_actions; | |
2557 | ||
2558 | new_actions = dp_netdev_actions_create(put->actions, | |
2559 | put->actions_len); | |
2560 | ||
61e7deb1 BP |
2561 | old_actions = dp_netdev_flow_get_actions(netdev_flow); |
2562 | ovsrcu_set(&netdev_flow->actions, new_actions); | |
679ba04c | 2563 | |
f5d317a1 DDP |
2564 | if (stats) { |
2565 | get_dpif_flow_stats(netdev_flow, stats); | |
a84cb64a BP |
2566 | } |
2567 | if (put->flags & DPIF_FP_ZERO_STATS) { | |
97447f55 DDP |
2568 | /* XXX: The userspace datapath uses thread local statistics |
2569 | * (for flows), which should be updated only by the owning | |
2570 | * thread. Since we cannot write on stats memory here, | |
2571 | * we choose not to support this flag. Please note: | |
2572 | * - This feature is currently used only by dpctl commands with | |
2573 | * option --clear. | |
2574 | * - Should the need arise, this operation can be implemented | |
2575 | * by keeping a base value (to be update here) for each | |
2576 | * counter, and subtracting it before outputting the stats */ | |
2577 | error = EOPNOTSUPP; | |
72865317 | 2578 | } |
8a4e3a85 | 2579 | |
61e7deb1 | 2580 | ovsrcu_postpone(dp_netdev_actions_free, old_actions); |
2c0ea78f | 2581 | } else if (put->flags & DPIF_FP_CREATE) { |
5279f8fd | 2582 | error = EEXIST; |
2c0ea78f GS |
2583 | } else { |
2584 | /* Overlapping flow. */ | |
2585 | error = EINVAL; | |
72865317 BP |
2586 | } |
2587 | } | |
1c1e46ed | 2588 | ovs_mutex_unlock(&pmd->flow_mutex); |
5279f8fd | 2589 | return error; |
72865317 BP |
2590 | } |
2591 | ||
72865317 | 2592 | static int |
f5d317a1 | 2593 | dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put) |
72865317 BP |
2594 | { |
2595 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
beb75a40 | 2596 | struct netdev_flow_key key, mask; |
1c1e46ed | 2597 | struct dp_netdev_pmd_thread *pmd; |
f5d317a1 DDP |
2598 | struct match match; |
2599 | ovs_u128 ufid; | |
2600 | int error; | |
f0fb825a | 2601 | bool probe = put->flags & DPIF_FP_PROBE; |
72865317 | 2602 | |
f5d317a1 DDP |
2603 | if (put->stats) { |
2604 | memset(put->stats, 0, sizeof *put->stats); | |
2605 | } | |
f0fb825a EG |
2606 | error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &match.flow, |
2607 | probe); | |
f5d317a1 DDP |
2608 | if (error) { |
2609 | return error; | |
2610 | } | |
2611 | error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len, | |
2612 | put->mask, put->mask_len, | |
f0fb825a | 2613 | &match.flow, &match.wc, probe); |
f5d317a1 DDP |
2614 | if (error) { |
2615 | return error; | |
1c1e46ed AW |
2616 | } |
2617 | ||
f5d317a1 DDP |
2618 | if (put->ufid) { |
2619 | ufid = *put->ufid; | |
2620 | } else { | |
2621 | dpif_flow_hash(dpif, &match.flow, sizeof match.flow, &ufid); | |
2622 | } | |
2623 | ||
2624 | /* Must produce a netdev_flow_key for lookup. | |
beb75a40 JS |
2625 | * Use the same method as employed to create the key when adding |
2626 | * the flow to the dplcs to make sure they match. */ | |
2627 | netdev_flow_mask_init(&mask, &match); | |
2628 | netdev_flow_key_init_masked(&key, &match.flow, &mask); | |
f5d317a1 DDP |
2629 | |
2630 | if (put->pmd_id == PMD_ID_NULL) { | |
2631 | if (cmap_count(&dp->poll_threads) == 0) { | |
2632 | return EINVAL; | |
2633 | } | |
2634 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
2635 | struct dpif_flow_stats pmd_stats; | |
2636 | int pmd_error; | |
2637 | ||
2638 | pmd_error = flow_put_on_pmd(pmd, &key, &match, &ufid, put, | |
2639 | &pmd_stats); | |
2640 | if (pmd_error) { | |
2641 | error = pmd_error; | |
2642 | } else if (put->stats) { | |
2643 | put->stats->n_packets += pmd_stats.n_packets; | |
2644 | put->stats->n_bytes += pmd_stats.n_bytes; | |
2645 | put->stats->used = MAX(put->stats->used, pmd_stats.used); | |
2646 | put->stats->tcp_flags |= pmd_stats.tcp_flags; | |
2647 | } | |
2648 | } | |
2649 | } else { | |
2650 | pmd = dp_netdev_get_pmd(dp, put->pmd_id); | |
2651 | if (!pmd) { | |
2652 | return EINVAL; | |
2653 | } | |
2654 | error = flow_put_on_pmd(pmd, &key, &match, &ufid, put, put->stats); | |
2655 | dp_netdev_pmd_unref(pmd); | |
2656 | } | |
2657 | ||
2658 | return error; | |
2659 | } | |
2660 | ||
2661 | static int | |
2662 | flow_del_on_pmd(struct dp_netdev_pmd_thread *pmd, | |
2663 | struct dpif_flow_stats *stats, | |
2664 | const struct dpif_flow_del *del) | |
2665 | { | |
2666 | struct dp_netdev_flow *netdev_flow; | |
2667 | int error = 0; | |
2668 | ||
1c1e46ed AW |
2669 | ovs_mutex_lock(&pmd->flow_mutex); |
2670 | netdev_flow = dp_netdev_pmd_find_flow(pmd, del->ufid, del->key, | |
2671 | del->key_len); | |
1763b4b8 | 2672 | if (netdev_flow) { |
f5d317a1 DDP |
2673 | if (stats) { |
2674 | get_dpif_flow_stats(netdev_flow, stats); | |
feebdea2 | 2675 | } |
1c1e46ed | 2676 | dp_netdev_pmd_remove_flow(pmd, netdev_flow); |
72865317 | 2677 | } else { |
5279f8fd | 2678 | error = ENOENT; |
72865317 | 2679 | } |
1c1e46ed | 2680 | ovs_mutex_unlock(&pmd->flow_mutex); |
f5d317a1 DDP |
2681 | |
2682 | return error; | |
2683 | } | |
2684 | ||
2685 | static int | |
2686 | dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del) | |
2687 | { | |
2688 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
2689 | struct dp_netdev_pmd_thread *pmd; | |
2690 | int error = 0; | |
2691 | ||
2692 | if (del->stats) { | |
2693 | memset(del->stats, 0, sizeof *del->stats); | |
2694 | } | |
2695 | ||
2696 | if (del->pmd_id == PMD_ID_NULL) { | |
2697 | if (cmap_count(&dp->poll_threads) == 0) { | |
2698 | return EINVAL; | |
2699 | } | |
2700 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
2701 | struct dpif_flow_stats pmd_stats; | |
2702 | int pmd_error; | |
2703 | ||
2704 | pmd_error = flow_del_on_pmd(pmd, &pmd_stats, del); | |
2705 | if (pmd_error) { | |
2706 | error = pmd_error; | |
2707 | } else if (del->stats) { | |
2708 | del->stats->n_packets += pmd_stats.n_packets; | |
2709 | del->stats->n_bytes += pmd_stats.n_bytes; | |
2710 | del->stats->used = MAX(del->stats->used, pmd_stats.used); | |
2711 | del->stats->tcp_flags |= pmd_stats.tcp_flags; | |
2712 | } | |
2713 | } | |
2714 | } else { | |
2715 | pmd = dp_netdev_get_pmd(dp, del->pmd_id); | |
2716 | if (!pmd) { | |
2717 | return EINVAL; | |
2718 | } | |
2719 | error = flow_del_on_pmd(pmd, del->stats, del); | |
2720 | dp_netdev_pmd_unref(pmd); | |
2721 | } | |
2722 | ||
5279f8fd BP |
2723 | |
2724 | return error; | |
72865317 BP |
2725 | } |
2726 | ||
ac64794a BP |
2727 | struct dpif_netdev_flow_dump { |
2728 | struct dpif_flow_dump up; | |
1c1e46ed AW |
2729 | struct cmap_position poll_thread_pos; |
2730 | struct cmap_position flow_pos; | |
2731 | struct dp_netdev_pmd_thread *cur_pmd; | |
d2ad7ef1 JS |
2732 | int status; |
2733 | struct ovs_mutex mutex; | |
e723fd32 JS |
2734 | }; |
2735 | ||
ac64794a BP |
2736 | static struct dpif_netdev_flow_dump * |
2737 | dpif_netdev_flow_dump_cast(struct dpif_flow_dump *dump) | |
72865317 | 2738 | { |
ac64794a | 2739 | return CONTAINER_OF(dump, struct dpif_netdev_flow_dump, up); |
e723fd32 JS |
2740 | } |
2741 | ||
ac64794a | 2742 | static struct dpif_flow_dump * |
7e8b7199 PB |
2743 | dpif_netdev_flow_dump_create(const struct dpif *dpif_, bool terse, |
2744 | char *type OVS_UNUSED) | |
e723fd32 | 2745 | { |
ac64794a | 2746 | struct dpif_netdev_flow_dump *dump; |
e723fd32 | 2747 | |
1c1e46ed | 2748 | dump = xzalloc(sizeof *dump); |
ac64794a | 2749 | dpif_flow_dump_init(&dump->up, dpif_); |
64bb477f | 2750 | dump->up.terse = terse; |
ac64794a BP |
2751 | ovs_mutex_init(&dump->mutex); |
2752 | ||
2753 | return &dump->up; | |
e723fd32 JS |
2754 | } |
2755 | ||
2756 | static int | |
ac64794a | 2757 | dpif_netdev_flow_dump_destroy(struct dpif_flow_dump *dump_) |
e723fd32 | 2758 | { |
ac64794a | 2759 | struct dpif_netdev_flow_dump *dump = dpif_netdev_flow_dump_cast(dump_); |
e723fd32 | 2760 | |
ac64794a BP |
2761 | ovs_mutex_destroy(&dump->mutex); |
2762 | free(dump); | |
704a1e09 BP |
2763 | return 0; |
2764 | } | |
2765 | ||
ac64794a BP |
2766 | struct dpif_netdev_flow_dump_thread { |
2767 | struct dpif_flow_dump_thread up; | |
2768 | struct dpif_netdev_flow_dump *dump; | |
8bb113da RW |
2769 | struct odputil_keybuf keybuf[FLOW_DUMP_MAX_BATCH]; |
2770 | struct odputil_keybuf maskbuf[FLOW_DUMP_MAX_BATCH]; | |
ac64794a BP |
2771 | }; |
2772 | ||
2773 | static struct dpif_netdev_flow_dump_thread * | |
2774 | dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread) | |
2775 | { | |
2776 | return CONTAINER_OF(thread, struct dpif_netdev_flow_dump_thread, up); | |
2777 | } | |
2778 | ||
2779 | static struct dpif_flow_dump_thread * | |
2780 | dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump *dump_) | |
2781 | { | |
2782 | struct dpif_netdev_flow_dump *dump = dpif_netdev_flow_dump_cast(dump_); | |
2783 | struct dpif_netdev_flow_dump_thread *thread; | |
2784 | ||
2785 | thread = xmalloc(sizeof *thread); | |
2786 | dpif_flow_dump_thread_init(&thread->up, &dump->up); | |
2787 | thread->dump = dump; | |
2788 | return &thread->up; | |
2789 | } | |
2790 | ||
2791 | static void | |
2792 | dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_) | |
2793 | { | |
2794 | struct dpif_netdev_flow_dump_thread *thread | |
2795 | = dpif_netdev_flow_dump_thread_cast(thread_); | |
2796 | ||
2797 | free(thread); | |
2798 | } | |
2799 | ||
704a1e09 | 2800 | static int |
ac64794a | 2801 | dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread *thread_, |
8bb113da | 2802 | struct dpif_flow *flows, int max_flows) |
ac64794a BP |
2803 | { |
2804 | struct dpif_netdev_flow_dump_thread *thread | |
2805 | = dpif_netdev_flow_dump_thread_cast(thread_); | |
2806 | struct dpif_netdev_flow_dump *dump = thread->dump; | |
8bb113da | 2807 | struct dp_netdev_flow *netdev_flows[FLOW_DUMP_MAX_BATCH]; |
8bb113da RW |
2808 | int n_flows = 0; |
2809 | int i; | |
14608a15 | 2810 | |
ac64794a | 2811 | ovs_mutex_lock(&dump->mutex); |
8bb113da | 2812 | if (!dump->status) { |
1c1e46ed AW |
2813 | struct dpif_netdev *dpif = dpif_netdev_cast(thread->up.dpif); |
2814 | struct dp_netdev *dp = get_dp_netdev(&dpif->dpif); | |
2815 | struct dp_netdev_pmd_thread *pmd = dump->cur_pmd; | |
2816 | int flow_limit = MIN(max_flows, FLOW_DUMP_MAX_BATCH); | |
2817 | ||
2818 | /* First call to dump_next(), extracts the first pmd thread. | |
2819 | * If there is no pmd thread, returns immediately. */ | |
2820 | if (!pmd) { | |
2821 | pmd = dp_netdev_pmd_get_next(dp, &dump->poll_thread_pos); | |
2822 | if (!pmd) { | |
2823 | ovs_mutex_unlock(&dump->mutex); | |
2824 | return n_flows; | |
8bb113da | 2825 | |
8bb113da | 2826 | } |
d2ad7ef1 | 2827 | } |
1c1e46ed AW |
2828 | |
2829 | do { | |
2830 | for (n_flows = 0; n_flows < flow_limit; n_flows++) { | |
2831 | struct cmap_node *node; | |
2832 | ||
2833 | node = cmap_next_position(&pmd->flow_table, &dump->flow_pos); | |
2834 | if (!node) { | |
2835 | break; | |
2836 | } | |
2837 | netdev_flows[n_flows] = CONTAINER_OF(node, | |
2838 | struct dp_netdev_flow, | |
2839 | node); | |
2840 | } | |
2841 | /* When finishing dumping the current pmd thread, moves to | |
2842 | * the next. */ | |
2843 | if (n_flows < flow_limit) { | |
2844 | memset(&dump->flow_pos, 0, sizeof dump->flow_pos); | |
2845 | dp_netdev_pmd_unref(pmd); | |
2846 | pmd = dp_netdev_pmd_get_next(dp, &dump->poll_thread_pos); | |
2847 | if (!pmd) { | |
2848 | dump->status = EOF; | |
2849 | break; | |
2850 | } | |
2851 | } | |
2852 | /* Keeps the reference to next caller. */ | |
2853 | dump->cur_pmd = pmd; | |
2854 | ||
2855 | /* If the current dump is empty, do not exit the loop, since the | |
2856 | * remaining pmds could have flows to be dumped. Just dumps again | |
2857 | * on the new 'pmd'. */ | |
2858 | } while (!n_flows); | |
8a4e3a85 | 2859 | } |
ac64794a | 2860 | ovs_mutex_unlock(&dump->mutex); |
ac64794a | 2861 | |
8bb113da RW |
2862 | for (i = 0; i < n_flows; i++) { |
2863 | struct odputil_keybuf *maskbuf = &thread->maskbuf[i]; | |
2864 | struct odputil_keybuf *keybuf = &thread->keybuf[i]; | |
2865 | struct dp_netdev_flow *netdev_flow = netdev_flows[i]; | |
2866 | struct dpif_flow *f = &flows[i]; | |
7af12bd7 | 2867 | struct ofpbuf key, mask; |
8bb113da | 2868 | |
7af12bd7 JS |
2869 | ofpbuf_use_stack(&key, keybuf, sizeof *keybuf); |
2870 | ofpbuf_use_stack(&mask, maskbuf, sizeof *maskbuf); | |
64bb477f JS |
2871 | dp_netdev_flow_to_dpif_flow(netdev_flow, &key, &mask, f, |
2872 | dump->up.terse); | |
8bb113da | 2873 | } |
feebdea2 | 2874 | |
8bb113da | 2875 | return n_flows; |
72865317 BP |
2876 | } |
2877 | ||
2878 | static int | |
758c456d | 2879 | dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute) |
65f13b50 | 2880 | OVS_NO_THREAD_SAFETY_ANALYSIS |
72865317 BP |
2881 | { |
2882 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
65f13b50 | 2883 | struct dp_netdev_pmd_thread *pmd; |
1895cc8d | 2884 | struct dp_packet_batch pp; |
72865317 | 2885 | |
cf62fa4c PS |
2886 | if (dp_packet_size(execute->packet) < ETH_HEADER_LEN || |
2887 | dp_packet_size(execute->packet) > UINT16_MAX) { | |
72865317 BP |
2888 | return EINVAL; |
2889 | } | |
2890 | ||
65f13b50 AW |
2891 | /* Tries finding the 'pmd'. If NULL is returned, that means |
2892 | * the current thread is a non-pmd thread and should use | |
b19befae | 2893 | * dp_netdev_get_pmd(dp, NON_PMD_CORE_ID). */ |
65f13b50 AW |
2894 | pmd = ovsthread_getspecific(dp->per_pmd_key); |
2895 | if (!pmd) { | |
b19befae | 2896 | pmd = dp_netdev_get_pmd(dp, NON_PMD_CORE_ID); |
546e57d4 DDP |
2897 | if (!pmd) { |
2898 | return EBUSY; | |
2899 | } | |
65f13b50 AW |
2900 | } |
2901 | ||
05267613 AZ |
2902 | if (execute->probe) { |
2903 | /* If this is part of a probe, Drop the packet, since executing | |
2904 | * the action may actually cause spurious packets be sent into | |
2905 | * the network. */ | |
d1ce9c20 YS |
2906 | if (pmd->core_id == NON_PMD_CORE_ID) { |
2907 | dp_netdev_pmd_unref(pmd); | |
2908 | } | |
05267613 AZ |
2909 | return 0; |
2910 | } | |
2911 | ||
65f13b50 AW |
2912 | /* If the current thread is non-pmd thread, acquires |
2913 | * the 'non_pmd_mutex'. */ | |
2914 | if (pmd->core_id == NON_PMD_CORE_ID) { | |
2915 | ovs_mutex_lock(&dp->non_pmd_mutex); | |
2916 | } | |
1c1e46ed | 2917 | |
36d8de17 DDP |
2918 | /* The action processing expects the RSS hash to be valid, because |
2919 | * it's always initialized at the beginning of datapath processing. | |
2920 | * In this case, though, 'execute->packet' may not have gone through | |
2921 | * the datapath at all, it may have been generated by the upper layer | |
2922 | * (OpenFlow packet-out, BFD frame, ...). */ | |
2923 | if (!dp_packet_rss_valid(execute->packet)) { | |
2924 | dp_packet_set_rss_hash(execute->packet, | |
2925 | flow_hash_5tuple(execute->flow, 0)); | |
2926 | } | |
2927 | ||
72c84bc2 | 2928 | dp_packet_batch_init_packet(&pp, execute->packet); |
66e4ad8a DDP |
2929 | dp_netdev_execute_actions(pmd, &pp, false, execute->flow, |
2930 | execute->actions, execute->actions_len, | |
2931 | time_msec()); | |
36d8de17 | 2932 | |
65f13b50 AW |
2933 | if (pmd->core_id == NON_PMD_CORE_ID) { |
2934 | ovs_mutex_unlock(&dp->non_pmd_mutex); | |
e9985d6a | 2935 | dp_netdev_pmd_unref(pmd); |
65f13b50 | 2936 | } |
8a4e3a85 | 2937 | |
758c456d | 2938 | return 0; |
72865317 BP |
2939 | } |
2940 | ||
1a0c894a BP |
2941 | static void |
2942 | dpif_netdev_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops) | |
2943 | { | |
2944 | size_t i; | |
2945 | ||
2946 | for (i = 0; i < n_ops; i++) { | |
2947 | struct dpif_op *op = ops[i]; | |
2948 | ||
2949 | switch (op->type) { | |
2950 | case DPIF_OP_FLOW_PUT: | |
2951 | op->error = dpif_netdev_flow_put(dpif, &op->u.flow_put); | |
2952 | break; | |
2953 | ||
2954 | case DPIF_OP_FLOW_DEL: | |
2955 | op->error = dpif_netdev_flow_del(dpif, &op->u.flow_del); | |
2956 | break; | |
2957 | ||
2958 | case DPIF_OP_EXECUTE: | |
2959 | op->error = dpif_netdev_execute(dpif, &op->u.execute); | |
2960 | break; | |
6fe09f8c JS |
2961 | |
2962 | case DPIF_OP_FLOW_GET: | |
2963 | op->error = dpif_netdev_flow_get(dpif, &op->u.flow_get); | |
2964 | break; | |
1a0c894a BP |
2965 | } |
2966 | } | |
2967 | } | |
2968 | ||
d4f6865c DDP |
2969 | /* Applies datapath configuration from the database. Some of the changes are |
2970 | * actually applied in dpif_netdev_run(). */ | |
f2eee189 | 2971 | static int |
d4f6865c | 2972 | dpif_netdev_set_config(struct dpif *dpif, const struct smap *other_config) |
f2eee189 AW |
2973 | { |
2974 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
d4f6865c | 2975 | const char *cmask = smap_get(other_config, "pmd-cpu-mask"); |
4c30b246 CL |
2976 | unsigned long long insert_prob = |
2977 | smap_get_ullong(other_config, "emc-insert-inv-prob", | |
2978 | DEFAULT_EM_FLOW_INSERT_INV_PROB); | |
2979 | uint32_t insert_min, cur_min; | |
f2eee189 | 2980 | |
a6a426d6 IM |
2981 | if (!nullable_string_is_equal(dp->pmd_cmask, cmask)) { |
2982 | free(dp->pmd_cmask); | |
2983 | dp->pmd_cmask = nullable_xstrdup(cmask); | |
2984 | dp_netdev_request_reconfigure(dp); | |
f2eee189 AW |
2985 | } |
2986 | ||
4c30b246 CL |
2987 | atomic_read_relaxed(&dp->emc_insert_min, &cur_min); |
2988 | if (insert_prob <= UINT32_MAX) { | |
2989 | insert_min = insert_prob == 0 ? 0 : UINT32_MAX / insert_prob; | |
2990 | } else { | |
2991 | insert_min = DEFAULT_EM_FLOW_INSERT_MIN; | |
2992 | insert_prob = DEFAULT_EM_FLOW_INSERT_INV_PROB; | |
2993 | } | |
2994 | ||
2995 | if (insert_min != cur_min) { | |
2996 | atomic_store_relaxed(&dp->emc_insert_min, insert_min); | |
2997 | if (insert_min == 0) { | |
2998 | VLOG_INFO("EMC has been disabled"); | |
2999 | } else { | |
3000 | VLOG_INFO("EMC insertion probability changed to 1/%llu (~%.2f%%)", | |
3001 | insert_prob, (100 / (float)insert_prob)); | |
3002 | } | |
3003 | } | |
3004 | ||
f2eee189 AW |
3005 | return 0; |
3006 | } | |
3007 | ||
3eb67853 IM |
3008 | /* Parses affinity list and returns result in 'core_ids'. */ |
3009 | static int | |
3010 | parse_affinity_list(const char *affinity_list, unsigned *core_ids, int n_rxq) | |
3011 | { | |
3012 | unsigned i; | |
3013 | char *list, *copy, *key, *value; | |
3014 | int error = 0; | |
3015 | ||
3016 | for (i = 0; i < n_rxq; i++) { | |
51c37a56 | 3017 | core_ids[i] = OVS_CORE_UNSPEC; |
3eb67853 IM |
3018 | } |
3019 | ||
3020 | if (!affinity_list) { | |
3021 | return 0; | |
3022 | } | |
3023 | ||
3024 | list = copy = xstrdup(affinity_list); | |
3025 | ||
3026 | while (ofputil_parse_key_value(&list, &key, &value)) { | |
3027 | int rxq_id, core_id; | |
3028 | ||
3029 | if (!str_to_int(key, 0, &rxq_id) || rxq_id < 0 | |
3030 | || !str_to_int(value, 0, &core_id) || core_id < 0) { | |
3031 | error = EINVAL; | |
3032 | break; | |
3033 | } | |
3034 | ||
3035 | if (rxq_id < n_rxq) { | |
3036 | core_ids[rxq_id] = core_id; | |
3037 | } | |
3038 | } | |
3039 | ||
3040 | free(copy); | |
3041 | return error; | |
3042 | } | |
3043 | ||
3044 | /* Parses 'affinity_list' and applies configuration if it is valid. */ | |
3045 | static int | |
3046 | dpif_netdev_port_set_rxq_affinity(struct dp_netdev_port *port, | |
3047 | const char *affinity_list) | |
3048 | { | |
3049 | unsigned *core_ids, i; | |
3050 | int error = 0; | |
3051 | ||
3052 | core_ids = xmalloc(port->n_rxq * sizeof *core_ids); | |
3053 | if (parse_affinity_list(affinity_list, core_ids, port->n_rxq)) { | |
3054 | error = EINVAL; | |
3055 | goto exit; | |
3056 | } | |
3057 | ||
3058 | for (i = 0; i < port->n_rxq; i++) { | |
3059 | port->rxqs[i].core_id = core_ids[i]; | |
3060 | } | |
3061 | ||
3062 | exit: | |
3063 | free(core_ids); | |
3064 | return error; | |
3065 | } | |
3066 | ||
3067 | /* Changes the affinity of port's rx queues. The changes are actually applied | |
3068 | * in dpif_netdev_run(). */ | |
3069 | static int | |
3070 | dpif_netdev_port_set_config(struct dpif *dpif, odp_port_t port_no, | |
3071 | const struct smap *cfg) | |
3072 | { | |
3073 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
3074 | struct dp_netdev_port *port; | |
3075 | int error = 0; | |
3076 | const char *affinity_list = smap_get(cfg, "pmd-rxq-affinity"); | |
3077 | ||
3078 | ovs_mutex_lock(&dp->port_mutex); | |
3079 | error = get_port_by_number(dp, port_no, &port); | |
3080 | if (error || !netdev_is_pmd(port->netdev) | |
3081 | || nullable_string_is_equal(affinity_list, port->rxq_affinity_list)) { | |
3082 | goto unlock; | |
3083 | } | |
3084 | ||
3085 | error = dpif_netdev_port_set_rxq_affinity(port, affinity_list); | |
3086 | if (error) { | |
3087 | goto unlock; | |
3088 | } | |
3089 | free(port->rxq_affinity_list); | |
3090 | port->rxq_affinity_list = nullable_xstrdup(affinity_list); | |
3091 | ||
3092 | dp_netdev_request_reconfigure(dp); | |
3093 | unlock: | |
3094 | ovs_mutex_unlock(&dp->port_mutex); | |
3095 | return error; | |
3096 | } | |
3097 | ||
5bf93d67 EJ |
3098 | static int |
3099 | dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED, | |
3100 | uint32_t queue_id, uint32_t *priority) | |
3101 | { | |
3102 | *priority = queue_id; | |
3103 | return 0; | |
3104 | } | |
3105 | ||
72865317 | 3106 | \f |
9ff55ae2 | 3107 | /* Creates and returns a new 'struct dp_netdev_actions', whose actions are |
1401f6de | 3108 | * a copy of the 'size' bytes of 'actions' input parameters. */ |
a84cb64a BP |
3109 | struct dp_netdev_actions * |
3110 | dp_netdev_actions_create(const struct nlattr *actions, size_t size) | |
3111 | { | |
3112 | struct dp_netdev_actions *netdev_actions; | |
3113 | ||
9ff55ae2 DDP |
3114 | netdev_actions = xmalloc(sizeof *netdev_actions + size); |
3115 | memcpy(netdev_actions->actions, actions, size); | |
a84cb64a BP |
3116 | netdev_actions->size = size; |
3117 | ||
3118 | return netdev_actions; | |
3119 | } | |
3120 | ||
a84cb64a | 3121 | struct dp_netdev_actions * |
61e7deb1 | 3122 | dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow) |
a84cb64a | 3123 | { |
61e7deb1 | 3124 | return ovsrcu_get(struct dp_netdev_actions *, &flow->actions); |
a84cb64a BP |
3125 | } |
3126 | ||
61e7deb1 BP |
3127 | static void |
3128 | dp_netdev_actions_free(struct dp_netdev_actions *actions) | |
a84cb64a | 3129 | { |
61e7deb1 | 3130 | free(actions); |
a84cb64a BP |
3131 | } |
3132 | \f | |
55e3ca97 DDP |
3133 | static inline unsigned long long |
3134 | cycles_counter(void) | |
3135 | { | |
3136 | #ifdef DPDK_NETDEV | |
3137 | return rte_get_tsc_cycles(); | |
3138 | #else | |
3139 | return 0; | |
3140 | #endif | |
3141 | } | |
3142 | ||
3143 | /* Fake mutex to make sure that the calls to cycles_count_* are balanced */ | |
3144 | extern struct ovs_mutex cycles_counter_fake_mutex; | |
3145 | ||
3146 | /* Start counting cycles. Must be followed by 'cycles_count_end()' */ | |
3147 | static inline void | |
3148 | cycles_count_start(struct dp_netdev_pmd_thread *pmd) | |
3149 | OVS_ACQUIRES(&cycles_counter_fake_mutex) | |
3150 | OVS_NO_THREAD_SAFETY_ANALYSIS | |
3151 | { | |
3152 | pmd->last_cycles = cycles_counter(); | |
3153 | } | |
3154 | ||
3155 | /* Stop counting cycles and add them to the counter 'type' */ | |
3156 | static inline void | |
3157 | cycles_count_end(struct dp_netdev_pmd_thread *pmd, | |
3158 | enum pmd_cycles_counter_type type) | |
3159 | OVS_RELEASES(&cycles_counter_fake_mutex) | |
3160 | OVS_NO_THREAD_SAFETY_ANALYSIS | |
3161 | { | |
3162 | unsigned long long interval = cycles_counter() - pmd->last_cycles; | |
3163 | ||
3164 | non_atomic_ullong_add(&pmd->cycles.n[type], interval); | |
3165 | } | |
e4cfed38 | 3166 | |
a2ac666d CL |
3167 | /* Calculate the intermediate cycle result and add to the counter 'type' */ |
3168 | static inline void | |
3169 | cycles_count_intermediate(struct dp_netdev_pmd_thread *pmd, | |
c59e759f | 3170 | struct dp_netdev_rxq *rxq, |
a2ac666d CL |
3171 | enum pmd_cycles_counter_type type) |
3172 | OVS_NO_THREAD_SAFETY_ANALYSIS | |
3173 | { | |
3174 | unsigned long long new_cycles = cycles_counter(); | |
3175 | unsigned long long interval = new_cycles - pmd->last_cycles; | |
3176 | pmd->last_cycles = new_cycles; | |
3177 | ||
3178 | non_atomic_ullong_add(&pmd->cycles.n[type], interval); | |
c59e759f KT |
3179 | if (rxq && (type == PMD_CYCLES_PROCESSING)) { |
3180 | /* Add to the amount of current processing cycles. */ | |
3181 | non_atomic_ullong_add(&rxq->cycles[RXQ_CYCLES_PROC_CURR], interval); | |
3182 | } | |
a2ac666d CL |
3183 | } |
3184 | ||
4809891b KT |
3185 | static void |
3186 | dp_netdev_rxq_set_cycles(struct dp_netdev_rxq *rx, | |
3187 | enum rxq_cycles_counter_type type, | |
3188 | unsigned long long cycles) | |
3189 | { | |
3190 | atomic_store_relaxed(&rx->cycles[type], cycles); | |
3191 | } | |
3192 | ||
3193 | static uint64_t | |
3194 | dp_netdev_rxq_get_cycles(struct dp_netdev_rxq *rx, | |
3195 | enum rxq_cycles_counter_type type) | |
3196 | { | |
3197 | unsigned long long processing_cycles; | |
3198 | atomic_read_relaxed(&rx->cycles[type], &processing_cycles); | |
3199 | return processing_cycles; | |
3200 | } | |
3201 | ||
3202 | static void | |
3203 | dp_netdev_rxq_set_intrvl_cycles(struct dp_netdev_rxq *rx, | |
3204 | unsigned long long cycles) | |
3205 | { | |
4ee87ad3 BP |
3206 | unsigned int idx = rx->intrvl_idx++ % PMD_RXQ_INTERVAL_MAX; |
3207 | atomic_store_relaxed(&rx->cycles_intrvl[idx], cycles); | |
4809891b KT |
3208 | } |
3209 | ||
655856ef KT |
3210 | static uint64_t |
3211 | dp_netdev_rxq_get_intrvl_cycles(struct dp_netdev_rxq *rx, unsigned idx) | |
3212 | { | |
3213 | unsigned long long processing_cycles; | |
3214 | atomic_read_relaxed(&rx->cycles_intrvl[idx], &processing_cycles); | |
3215 | return processing_cycles; | |
3216 | } | |
3217 | ||
a2ac666d | 3218 | static int |
65f13b50 | 3219 | dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread *pmd, |
947dc567 DDP |
3220 | struct netdev_rxq *rx, |
3221 | odp_port_t port_no) | |
e4cfed38 | 3222 | { |
1895cc8d PS |
3223 | struct dp_packet_batch batch; |
3224 | int error; | |
a2ac666d | 3225 | int batch_cnt = 0; |
e4cfed38 | 3226 | |
1895cc8d | 3227 | dp_packet_batch_init(&batch); |
947dc567 | 3228 | error = netdev_rxq_recv(rx, &batch); |
e4cfed38 | 3229 | if (!error) { |
3c33f0ff | 3230 | *recirc_depth_get() = 0; |
41ccaa24 | 3231 | |
a2ac666d | 3232 | batch_cnt = batch.count; |
947dc567 | 3233 | dp_netdev_input(pmd, &batch, port_no); |
e4cfed38 | 3234 | } else if (error != EAGAIN && error != EOPNOTSUPP) { |
3c33f0ff | 3235 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
e4cfed38 PS |
3236 | |
3237 | VLOG_ERR_RL(&rl, "error receiving data from %s: %s", | |
947dc567 | 3238 | netdev_rxq_get_name(rx), ovs_strerror(error)); |
e4cfed38 | 3239 | } |
a2ac666d CL |
3240 | |
3241 | return batch_cnt; | |
e4cfed38 PS |
3242 | } |
3243 | ||
e32971b8 DDP |
3244 | static struct tx_port * |
3245 | tx_port_lookup(const struct hmap *hmap, odp_port_t port_no) | |
3246 | { | |
3247 | struct tx_port *tx; | |
3248 | ||
3249 | HMAP_FOR_EACH_IN_BUCKET (tx, node, hash_port_no(port_no), hmap) { | |
3250 | if (tx->port->port_no == port_no) { | |
3251 | return tx; | |
3252 | } | |
3253 | } | |
3254 | ||
3255 | return NULL; | |
3256 | } | |
3257 | ||
dc36593c DDP |
3258 | static int |
3259 | port_reconfigure(struct dp_netdev_port *port) | |
3260 | { | |
3261 | struct netdev *netdev = port->netdev; | |
dc36593c DDP |
3262 | int i, err; |
3263 | ||
e32971b8 | 3264 | port->need_reconfigure = false; |
dc36593c DDP |
3265 | |
3266 | /* Closes the existing 'rxq's. */ | |
3267 | for (i = 0; i < port->n_rxq; i++) { | |
947dc567 DDP |
3268 | netdev_rxq_close(port->rxqs[i].rx); |
3269 | port->rxqs[i].rx = NULL; | |
dc36593c | 3270 | } |
4809891b | 3271 | unsigned last_nrxq = port->n_rxq; |
dc36593c DDP |
3272 | port->n_rxq = 0; |
3273 | ||
050c60bf | 3274 | /* Allows 'netdev' to apply the pending configuration changes. */ |
e32971b8 DDP |
3275 | if (netdev_is_reconf_required(netdev)) { |
3276 | err = netdev_reconfigure(netdev); | |
3277 | if (err && (err != EOPNOTSUPP)) { | |
3278 | VLOG_ERR("Failed to set interface %s new configuration", | |
3279 | netdev_get_name(netdev)); | |
3280 | return err; | |
3281 | } | |
dc36593c | 3282 | } |
050c60bf | 3283 | /* If the netdev_reconfigure() above succeeds, reopens the 'rxq's. */ |
3eb67853 IM |
3284 | port->rxqs = xrealloc(port->rxqs, |
3285 | sizeof *port->rxqs * netdev_n_rxq(netdev)); | |
324c8374 IM |
3286 | /* Realloc 'used' counters for tx queues. */ |
3287 | free(port->txq_used); | |
3288 | port->txq_used = xcalloc(netdev_n_txq(netdev), sizeof *port->txq_used); | |
3289 | ||
dc36593c | 3290 | for (i = 0; i < netdev_n_rxq(netdev); i++) { |
38259bd7 BP |
3291 | bool new_queue = i >= last_nrxq; |
3292 | if (new_queue) { | |
3293 | memset(&port->rxqs[i], 0, sizeof port->rxqs[i]); | |
3294 | } | |
3295 | ||
947dc567 | 3296 | port->rxqs[i].port = port; |
38259bd7 | 3297 | |
947dc567 | 3298 | err = netdev_rxq_open(netdev, &port->rxqs[i].rx, i); |
dc36593c DDP |
3299 | if (err) { |
3300 | return err; | |
3301 | } | |
3302 | port->n_rxq++; | |
3303 | } | |
3304 | ||
3eb67853 IM |
3305 | /* Parse affinity list to apply configuration for new queues. */ |
3306 | dpif_netdev_port_set_rxq_affinity(port, port->rxq_affinity_list); | |
3307 | ||
dc36593c DDP |
3308 | return 0; |
3309 | } | |
3310 | ||
e32971b8 DDP |
3311 | struct rr_numa_list { |
3312 | struct hmap numas; /* Contains 'struct rr_numa' */ | |
3313 | }; | |
3314 | ||
3315 | struct rr_numa { | |
3316 | struct hmap_node node; | |
3317 | ||
3318 | int numa_id; | |
3319 | ||
3320 | /* Non isolated pmds on numa node 'numa_id' */ | |
3321 | struct dp_netdev_pmd_thread **pmds; | |
3322 | int n_pmds; | |
3323 | ||
3324 | int cur_index; | |
79da1e41 | 3325 | bool idx_inc; |
e32971b8 DDP |
3326 | }; |
3327 | ||
3328 | static struct rr_numa * | |
3329 | rr_numa_list_lookup(struct rr_numa_list *rr, int numa_id) | |
3330 | { | |
3331 | struct rr_numa *numa; | |
3332 | ||
3333 | HMAP_FOR_EACH_WITH_HASH (numa, node, hash_int(numa_id, 0), &rr->numas) { | |
3334 | if (numa->numa_id == numa_id) { | |
3335 | return numa; | |
3336 | } | |
3337 | } | |
3338 | ||
3339 | return NULL; | |
3340 | } | |
3341 | ||
c37813fd BM |
3342 | /* Returns the next node in numa list following 'numa' in round-robin fashion. |
3343 | * Returns first node if 'numa' is a null pointer or the last node in 'rr'. | |
3344 | * Returns NULL if 'rr' numa list is empty. */ | |
3345 | static struct rr_numa * | |
3346 | rr_numa_list_next(struct rr_numa_list *rr, const struct rr_numa *numa) | |
3347 | { | |
3348 | struct hmap_node *node = NULL; | |
3349 | ||
3350 | if (numa) { | |
3351 | node = hmap_next(&rr->numas, &numa->node); | |
3352 | } | |
3353 | if (!node) { | |
3354 | node = hmap_first(&rr->numas); | |
3355 | } | |
3356 | ||
3357 | return (node) ? CONTAINER_OF(node, struct rr_numa, node) : NULL; | |
3358 | } | |
3359 | ||
e32971b8 DDP |
3360 | static void |
3361 | rr_numa_list_populate(struct dp_netdev *dp, struct rr_numa_list *rr) | |
3362 | { | |
3363 | struct dp_netdev_pmd_thread *pmd; | |
3364 | struct rr_numa *numa; | |
3365 | ||
3366 | hmap_init(&rr->numas); | |
3367 | ||
3368 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
3369 | if (pmd->core_id == NON_PMD_CORE_ID || pmd->isolated) { | |
3370 | continue; | |
3371 | } | |
3372 | ||
3373 | numa = rr_numa_list_lookup(rr, pmd->numa_id); | |
3374 | if (!numa) { | |
3375 | numa = xzalloc(sizeof *numa); | |
3376 | numa->numa_id = pmd->numa_id; | |
3377 | hmap_insert(&rr->numas, &numa->node, hash_int(pmd->numa_id, 0)); | |
3378 | } | |
3379 | numa->n_pmds++; | |
3380 | numa->pmds = xrealloc(numa->pmds, numa->n_pmds * sizeof *numa->pmds); | |
3381 | numa->pmds[numa->n_pmds - 1] = pmd; | |
79da1e41 KT |
3382 | /* At least one pmd so initialise curr_idx and idx_inc. */ |
3383 | numa->cur_index = 0; | |
3384 | numa->idx_inc = true; | |
e32971b8 DDP |
3385 | } |
3386 | } | |
3387 | ||
79da1e41 KT |
3388 | /* Returns the next pmd from the numa node in |
3389 | * incrementing or decrementing order. */ | |
e32971b8 DDP |
3390 | static struct dp_netdev_pmd_thread * |
3391 | rr_numa_get_pmd(struct rr_numa *numa) | |
3392 | { | |
79da1e41 KT |
3393 | int numa_idx = numa->cur_index; |
3394 | ||
3395 | if (numa->idx_inc == true) { | |
3396 | /* Incrementing through list of pmds. */ | |
3397 | if (numa->cur_index == numa->n_pmds-1) { | |
3398 | /* Reached the last pmd. */ | |
3399 | numa->idx_inc = false; | |
3400 | } else { | |
3401 | numa->cur_index++; | |
3402 | } | |
3403 | } else { | |
3404 | /* Decrementing through list of pmds. */ | |
3405 | if (numa->cur_index == 0) { | |
3406 | /* Reached the first pmd. */ | |
3407 | numa->idx_inc = true; | |
3408 | } else { | |
3409 | numa->cur_index--; | |
3410 | } | |
3411 | } | |
3412 | return numa->pmds[numa_idx]; | |
e32971b8 DDP |
3413 | } |
3414 | ||
3415 | static void | |
3416 | rr_numa_list_destroy(struct rr_numa_list *rr) | |
3417 | { | |
3418 | struct rr_numa *numa; | |
3419 | ||
3420 | HMAP_FOR_EACH_POP (numa, node, &rr->numas) { | |
3421 | free(numa->pmds); | |
3422 | free(numa); | |
3423 | } | |
3424 | hmap_destroy(&rr->numas); | |
3425 | } | |
3426 | ||
655856ef KT |
3427 | /* Sort Rx Queues by the processing cycles they are consuming. */ |
3428 | static int | |
cc131ac1 | 3429 | compare_rxq_cycles(const void *a, const void *b) |
655856ef | 3430 | { |
28080276 KT |
3431 | struct dp_netdev_rxq *qa; |
3432 | struct dp_netdev_rxq *qb; | |
8368866e | 3433 | uint64_t cycles_qa, cycles_qb; |
655856ef KT |
3434 | |
3435 | qa = *(struct dp_netdev_rxq **) a; | |
3436 | qb = *(struct dp_netdev_rxq **) b; | |
3437 | ||
8368866e KT |
3438 | cycles_qa = dp_netdev_rxq_get_cycles(qa, RXQ_CYCLES_PROC_HIST); |
3439 | cycles_qb = dp_netdev_rxq_get_cycles(qb, RXQ_CYCLES_PROC_HIST); | |
655856ef | 3440 | |
8368866e KT |
3441 | if (cycles_qa != cycles_qb) { |
3442 | return (cycles_qa < cycles_qb) ? 1 : -1; | |
a130f1a8 KT |
3443 | } else { |
3444 | /* Cycles are the same so tiebreak on port/queue id. | |
3445 | * Tiebreaking (as opposed to return 0) ensures consistent | |
3446 | * sort results across multiple OS's. */ | |
f0aa3801 BP |
3447 | uint32_t port_qa = odp_to_u32(qa->port->port_no); |
3448 | uint32_t port_qb = odp_to_u32(qb->port->port_no); | |
3449 | if (port_qa != port_qb) { | |
3450 | return port_qa > port_qb ? 1 : -1; | |
a130f1a8 KT |
3451 | } else { |
3452 | return netdev_rxq_get_queue_id(qa->rx) | |
3453 | - netdev_rxq_get_queue_id(qb->rx); | |
3454 | } | |
655856ef | 3455 | } |
655856ef KT |
3456 | } |
3457 | ||
e32971b8 DDP |
3458 | /* Assign pmds to queues. If 'pinned' is true, assign pmds to pinned |
3459 | * queues and marks the pmds as isolated. Otherwise, assign non isolated | |
3460 | * pmds to unpinned queues. | |
3461 | * | |
655856ef KT |
3462 | * If 'pinned' is false queues will be sorted by processing cycles they are |
3463 | * consuming and then assigned to pmds in round robin order. | |
3464 | * | |
e32971b8 DDP |
3465 | * The function doesn't touch the pmd threads, it just stores the assignment |
3466 | * in the 'pmd' member of each rxq. */ | |
3467 | static void | |
3468 | rxq_scheduling(struct dp_netdev *dp, bool pinned) OVS_REQUIRES(dp->port_mutex) | |
3469 | { | |
3470 | struct dp_netdev_port *port; | |
3471 | struct rr_numa_list rr; | |
c37813fd | 3472 | struct rr_numa *non_local_numa = NULL; |
655856ef KT |
3473 | struct dp_netdev_rxq ** rxqs = NULL; |
3474 | int i, n_rxqs = 0; | |
3475 | struct rr_numa *numa = NULL; | |
3476 | int numa_id; | |
e32971b8 DDP |
3477 | |
3478 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
e32971b8 DDP |
3479 | if (!netdev_is_pmd(port->netdev)) { |
3480 | continue; | |
3481 | } | |
3482 | ||
e32971b8 DDP |
3483 | for (int qid = 0; qid < port->n_rxq; qid++) { |
3484 | struct dp_netdev_rxq *q = &port->rxqs[qid]; | |
3485 | ||
3486 | if (pinned && q->core_id != OVS_CORE_UNSPEC) { | |
3487 | struct dp_netdev_pmd_thread *pmd; | |
3488 | ||
3489 | pmd = dp_netdev_get_pmd(dp, q->core_id); | |
3490 | if (!pmd) { | |
3491 | VLOG_WARN("There is no PMD thread on core %d. Queue " | |
3492 | "%d on port \'%s\' will not be polled.", | |
3493 | q->core_id, qid, netdev_get_name(port->netdev)); | |
3494 | } else { | |
3495 | q->pmd = pmd; | |
3496 | pmd->isolated = true; | |
3497 | dp_netdev_pmd_unref(pmd); | |
3498 | } | |
3499 | } else if (!pinned && q->core_id == OVS_CORE_UNSPEC) { | |
8368866e KT |
3500 | uint64_t cycle_hist = 0; |
3501 | ||
655856ef KT |
3502 | if (n_rxqs == 0) { |
3503 | rxqs = xmalloc(sizeof *rxqs); | |
e32971b8 | 3504 | } else { |
655856ef | 3505 | rxqs = xrealloc(rxqs, sizeof *rxqs * (n_rxqs + 1)); |
e32971b8 | 3506 | } |
8368866e KT |
3507 | /* Sum the queue intervals and store the cycle history. */ |
3508 | for (unsigned i = 0; i < PMD_RXQ_INTERVAL_MAX; i++) { | |
3509 | cycle_hist += dp_netdev_rxq_get_intrvl_cycles(q, i); | |
3510 | } | |
3511 | dp_netdev_rxq_set_cycles(q, RXQ_CYCLES_PROC_HIST, cycle_hist); | |
3512 | ||
655856ef KT |
3513 | /* Store the queue. */ |
3514 | rxqs[n_rxqs++] = q; | |
e32971b8 DDP |
3515 | } |
3516 | } | |
3517 | } | |
3518 | ||
655856ef KT |
3519 | if (n_rxqs > 1) { |
3520 | /* Sort the queues in order of the processing cycles | |
3521 | * they consumed during their last pmd interval. */ | |
cc131ac1 | 3522 | qsort(rxqs, n_rxqs, sizeof *rxqs, compare_rxq_cycles); |
655856ef KT |
3523 | } |
3524 | ||
3525 | rr_numa_list_populate(dp, &rr); | |
3526 | /* Assign the sorted queues to pmds in round robin. */ | |
3527 | for (i = 0; i < n_rxqs; i++) { | |
3528 | numa_id = netdev_get_numa_id(rxqs[i]->port->netdev); | |
3529 | numa = rr_numa_list_lookup(&rr, numa_id); | |
3530 | if (!numa) { | |
3531 | /* There are no pmds on the queue's local NUMA node. | |
3532 | Round robin on the NUMA nodes that do have pmds. */ | |
3533 | non_local_numa = rr_numa_list_next(&rr, non_local_numa); | |
3534 | if (!non_local_numa) { | |
3535 | VLOG_ERR("There is no available (non-isolated) pmd " | |
3536 | "thread for port \'%s\' queue %d. This queue " | |
3537 | "will not be polled. Is pmd-cpu-mask set to " | |
3538 | "zero? Or are all PMDs isolated to other " | |
3539 | "queues?", netdev_rxq_get_name(rxqs[i]->rx), | |
3540 | netdev_rxq_get_queue_id(rxqs[i]->rx)); | |
3541 | continue; | |
3542 | } | |
3543 | rxqs[i]->pmd = rr_numa_get_pmd(non_local_numa); | |
3544 | VLOG_WARN("There's no available (non-isolated) pmd thread " | |
3545 | "on numa node %d. Queue %d on port \'%s\' will " | |
3546 | "be assigned to the pmd on core %d " | |
3547 | "(numa node %d). Expect reduced performance.", | |
3548 | numa_id, netdev_rxq_get_queue_id(rxqs[i]->rx), | |
3549 | netdev_rxq_get_name(rxqs[i]->rx), | |
3550 | rxqs[i]->pmd->core_id, rxqs[i]->pmd->numa_id); | |
3551 | } else { | |
3552 | rxqs[i]->pmd = rr_numa_get_pmd(numa); | |
3553 | VLOG_INFO("Core %d on numa node %d assigned port \'%s\' " | |
3554 | "rx queue %d (measured processing cycles %"PRIu64").", | |
3555 | rxqs[i]->pmd->core_id, numa_id, | |
3556 | netdev_rxq_get_name(rxqs[i]->rx), | |
3557 | netdev_rxq_get_queue_id(rxqs[i]->rx), | |
3558 | dp_netdev_rxq_get_cycles(rxqs[i], RXQ_CYCLES_PROC_HIST)); | |
3559 | } | |
3560 | } | |
3561 | ||
e32971b8 | 3562 | rr_numa_list_destroy(&rr); |
655856ef | 3563 | free(rxqs); |
e32971b8 DDP |
3564 | } |
3565 | ||
140dd699 IM |
3566 | static void |
3567 | reload_affected_pmds(struct dp_netdev *dp) | |
3568 | { | |
3569 | struct dp_netdev_pmd_thread *pmd; | |
3570 | ||
3571 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
3572 | if (pmd->need_reload) { | |
3573 | dp_netdev_reload_pmd__(pmd); | |
3574 | pmd->need_reload = false; | |
3575 | } | |
3576 | } | |
3577 | } | |
3578 | ||
6e3c6fa4 DDP |
3579 | static void |
3580 | reconfigure_pmd_threads(struct dp_netdev *dp) | |
3581 | OVS_REQUIRES(dp->port_mutex) | |
3582 | { | |
e32971b8 DDP |
3583 | struct dp_netdev_pmd_thread *pmd; |
3584 | struct ovs_numa_dump *pmd_cores; | |
140dd699 IM |
3585 | struct ovs_numa_info_core *core; |
3586 | struct hmapx to_delete = HMAPX_INITIALIZER(&to_delete); | |
3587 | struct hmapx_node *node; | |
e32971b8 | 3588 | bool changed = false; |
140dd699 | 3589 | bool need_to_adjust_static_tx_qids = false; |
e32971b8 DDP |
3590 | |
3591 | /* The pmd threads should be started only if there's a pmd port in the | |
3592 | * datapath. If the user didn't provide any "pmd-cpu-mask", we start | |
3593 | * NR_PMD_THREADS per numa node. */ | |
3594 | if (!has_pmd_port(dp)) { | |
3595 | pmd_cores = ovs_numa_dump_n_cores_per_numa(0); | |
3596 | } else if (dp->pmd_cmask && dp->pmd_cmask[0]) { | |
3597 | pmd_cores = ovs_numa_dump_cores_with_cmask(dp->pmd_cmask); | |
3598 | } else { | |
3599 | pmd_cores = ovs_numa_dump_n_cores_per_numa(NR_PMD_THREADS); | |
3600 | } | |
3601 | ||
140dd699 IM |
3602 | /* We need to adjust 'static_tx_qid's only if we're reducing number of |
3603 | * PMD threads. Otherwise, new threads will allocate all the freed ids. */ | |
3604 | if (ovs_numa_dump_count(pmd_cores) < cmap_count(&dp->poll_threads) - 1) { | |
3605 | /* Adjustment is required to keep 'static_tx_qid's sequential and | |
3606 | * avoid possible issues, for example, imbalanced tx queue usage | |
3607 | * and unnecessary locking caused by remapping on netdev level. */ | |
3608 | need_to_adjust_static_tx_qids = true; | |
3609 | } | |
3610 | ||
3611 | /* Check for unwanted pmd threads */ | |
3612 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
3613 | if (pmd->core_id == NON_PMD_CORE_ID) { | |
3614 | continue; | |
3615 | } | |
3616 | if (!ovs_numa_dump_contains_core(pmd_cores, pmd->numa_id, | |
3617 | pmd->core_id)) { | |
3618 | hmapx_add(&to_delete, pmd); | |
3619 | } else if (need_to_adjust_static_tx_qids) { | |
3620 | pmd->need_reload = true; | |
e32971b8 DDP |
3621 | } |
3622 | } | |
3623 | ||
140dd699 IM |
3624 | HMAPX_FOR_EACH (node, &to_delete) { |
3625 | pmd = (struct dp_netdev_pmd_thread *) node->data; | |
3626 | VLOG_INFO("PMD thread on numa_id: %d, core id: %2d destroyed.", | |
3627 | pmd->numa_id, pmd->core_id); | |
3628 | dp_netdev_del_pmd(dp, pmd); | |
3629 | } | |
3630 | changed = !hmapx_is_empty(&to_delete); | |
3631 | hmapx_destroy(&to_delete); | |
e32971b8 | 3632 | |
140dd699 IM |
3633 | if (need_to_adjust_static_tx_qids) { |
3634 | /* 'static_tx_qid's are not sequential now. | |
3635 | * Reload remaining threads to fix this. */ | |
3636 | reload_affected_pmds(dp); | |
3637 | } | |
e32971b8 | 3638 | |
140dd699 IM |
3639 | /* Check for required new pmd threads */ |
3640 | FOR_EACH_CORE_ON_DUMP(core, pmd_cores) { | |
3641 | pmd = dp_netdev_get_pmd(dp, core->core_id); | |
3642 | if (!pmd) { | |
3643 | pmd = xzalloc(sizeof *pmd); | |
e32971b8 | 3644 | dp_netdev_configure_pmd(pmd, dp, core->core_id, core->numa_id); |
e32971b8 | 3645 | pmd->thread = ovs_thread_create("pmd", pmd_thread_main, pmd); |
140dd699 IM |
3646 | VLOG_INFO("PMD thread on numa_id: %d, core id: %2d created.", |
3647 | pmd->numa_id, pmd->core_id); | |
3648 | changed = true; | |
3649 | } else { | |
3650 | dp_netdev_pmd_unref(pmd); | |
e32971b8 | 3651 | } |
140dd699 IM |
3652 | } |
3653 | ||
3654 | if (changed) { | |
3655 | struct ovs_numa_info_numa *numa; | |
e32971b8 DDP |
3656 | |
3657 | /* Log the number of pmd threads per numa node. */ | |
3658 | FOR_EACH_NUMA_ON_DUMP (numa, pmd_cores) { | |
140dd699 | 3659 | VLOG_INFO("There are %"PRIuSIZE" pmd threads on numa node %d", |
e32971b8 DDP |
3660 | numa->n_cores, numa->numa_id); |
3661 | } | |
3662 | } | |
3663 | ||
3664 | ovs_numa_dump_destroy(pmd_cores); | |
3665 | } | |
3666 | ||
e32971b8 DDP |
3667 | static void |
3668 | pmd_remove_stale_ports(struct dp_netdev *dp, | |
3669 | struct dp_netdev_pmd_thread *pmd) | |
3670 | OVS_EXCLUDED(pmd->port_mutex) | |
3671 | OVS_REQUIRES(dp->port_mutex) | |
3672 | { | |
3673 | struct rxq_poll *poll, *poll_next; | |
3674 | struct tx_port *tx, *tx_next; | |
3675 | ||
3676 | ovs_mutex_lock(&pmd->port_mutex); | |
3677 | HMAP_FOR_EACH_SAFE (poll, poll_next, node, &pmd->poll_list) { | |
3678 | struct dp_netdev_port *port = poll->rxq->port; | |
3679 | ||
3680 | if (port->need_reconfigure | |
3681 | || !hmap_contains(&dp->ports, &port->node)) { | |
3682 | dp_netdev_del_rxq_from_pmd(pmd, poll); | |
3683 | } | |
3684 | } | |
3685 | HMAP_FOR_EACH_SAFE (tx, tx_next, node, &pmd->tx_ports) { | |
3686 | struct dp_netdev_port *port = tx->port; | |
3687 | ||
3688 | if (port->need_reconfigure | |
3689 | || !hmap_contains(&dp->ports, &port->node)) { | |
3690 | dp_netdev_del_port_tx_from_pmd(pmd, tx); | |
3691 | } | |
3692 | } | |
3693 | ovs_mutex_unlock(&pmd->port_mutex); | |
3694 | } | |
3695 | ||
3696 | /* Must be called each time a port is added/removed or the cmask changes. | |
3697 | * This creates and destroys pmd threads, reconfigures ports, opens their | |
3698 | * rxqs and assigns all rxqs/txqs to pmd threads. */ | |
3699 | static void | |
3700 | reconfigure_datapath(struct dp_netdev *dp) | |
3701 | OVS_REQUIRES(dp->port_mutex) | |
3702 | { | |
3703 | struct dp_netdev_pmd_thread *pmd; | |
3704 | struct dp_netdev_port *port; | |
3705 | int wanted_txqs; | |
6e3c6fa4 | 3706 | |
a6a426d6 IM |
3707 | dp->last_reconfigure_seq = seq_read(dp->reconfigure_seq); |
3708 | ||
e32971b8 DDP |
3709 | /* Step 1: Adjust the pmd threads based on the datapath ports, the cores |
3710 | * on the system and the user configuration. */ | |
3711 | reconfigure_pmd_threads(dp); | |
6e3c6fa4 | 3712 | |
e32971b8 | 3713 | wanted_txqs = cmap_count(&dp->poll_threads); |
324c8374 | 3714 | |
e32971b8 DDP |
3715 | /* The number of pmd threads might have changed, or a port can be new: |
3716 | * adjust the txqs. */ | |
3717 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
3718 | netdev_set_tx_multiq(port->netdev, wanted_txqs); | |
324c8374 IM |
3719 | } |
3720 | ||
e32971b8 DDP |
3721 | /* Step 2: Remove from the pmd threads ports that have been removed or |
3722 | * need reconfiguration. */ | |
3723 | ||
3724 | /* Check for all the ports that need reconfiguration. We cache this in | |
85a4f238 IM |
3725 | * 'port->need_reconfigure', because netdev_is_reconf_required() can |
3726 | * change at any time. */ | |
e32971b8 DDP |
3727 | HMAP_FOR_EACH (port, node, &dp->ports) { |
3728 | if (netdev_is_reconf_required(port->netdev)) { | |
3729 | port->need_reconfigure = true; | |
3730 | } | |
3731 | } | |
3732 | ||
3733 | /* Remove from the pmd threads all the ports that have been deleted or | |
3734 | * need reconfiguration. */ | |
3735 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
3736 | pmd_remove_stale_ports(dp, pmd); | |
3737 | } | |
3738 | ||
3739 | /* Reload affected pmd threads. We must wait for the pmd threads before | |
3740 | * reconfiguring the ports, because a port cannot be reconfigured while | |
3741 | * it's being used. */ | |
3742 | reload_affected_pmds(dp); | |
3743 | ||
3744 | /* Step 3: Reconfigure ports. */ | |
3745 | ||
3746 | /* We only reconfigure the ports that we determined above, because they're | |
3747 | * not being used by any pmd thread at the moment. If a port fails to | |
3748 | * reconfigure we remove it from the datapath. */ | |
f582b6df BP |
3749 | struct dp_netdev_port *next_port; |
3750 | HMAP_FOR_EACH_SAFE (port, next_port, node, &dp->ports) { | |
dc36593c | 3751 | int err; |
6e3c6fa4 | 3752 | |
e32971b8 DDP |
3753 | if (!port->need_reconfigure) { |
3754 | continue; | |
3755 | } | |
3756 | ||
dc36593c DDP |
3757 | err = port_reconfigure(port); |
3758 | if (err) { | |
3759 | hmap_remove(&dp->ports, &port->node); | |
3760 | seq_change(dp->port_seq); | |
3761 | port_destroy(port); | |
324c8374 | 3762 | } else { |
e32971b8 | 3763 | port->dynamic_txqs = netdev_n_txq(port->netdev) < wanted_txqs; |
6e3c6fa4 DDP |
3764 | } |
3765 | } | |
e32971b8 DDP |
3766 | |
3767 | /* Step 4: Compute new rxq scheduling. We don't touch the pmd threads | |
3768 | * for now, we just update the 'pmd' pointer in each rxq to point to the | |
3769 | * wanted thread according to the scheduling policy. */ | |
3770 | ||
3771 | /* Reset all the pmd threads to non isolated. */ | |
3772 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
3773 | pmd->isolated = false; | |
3774 | } | |
3775 | ||
3776 | /* Reset all the queues to unassigned */ | |
3777 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
3778 | for (int i = 0; i < port->n_rxq; i++) { | |
3779 | port->rxqs[i].pmd = NULL; | |
3780 | } | |
3781 | } | |
3782 | ||
3783 | /* Add pinned queues and mark pmd threads isolated. */ | |
3784 | rxq_scheduling(dp, true); | |
3785 | ||
3786 | /* Add non-pinned queues. */ | |
3787 | rxq_scheduling(dp, false); | |
3788 | ||
3789 | /* Step 5: Remove queues not compliant with new scheduling. */ | |
3790 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
3791 | struct rxq_poll *poll, *poll_next; | |
3792 | ||
3793 | ovs_mutex_lock(&pmd->port_mutex); | |
3794 | HMAP_FOR_EACH_SAFE (poll, poll_next, node, &pmd->poll_list) { | |
3795 | if (poll->rxq->pmd != pmd) { | |
3796 | dp_netdev_del_rxq_from_pmd(pmd, poll); | |
3797 | } | |
3798 | } | |
3799 | ovs_mutex_unlock(&pmd->port_mutex); | |
3800 | } | |
3801 | ||
3802 | /* Reload affected pmd threads. We must wait for the pmd threads to remove | |
3803 | * the old queues before readding them, otherwise a queue can be polled by | |
3804 | * two threads at the same time. */ | |
3805 | reload_affected_pmds(dp); | |
3806 | ||
3807 | /* Step 6: Add queues from scheduling, if they're not there already. */ | |
3808 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
3809 | if (!netdev_is_pmd(port->netdev)) { | |
3810 | continue; | |
3811 | } | |
3812 | ||
3813 | for (int qid = 0; qid < port->n_rxq; qid++) { | |
3814 | struct dp_netdev_rxq *q = &port->rxqs[qid]; | |
3815 | ||
3816 | if (q->pmd) { | |
3817 | ovs_mutex_lock(&q->pmd->port_mutex); | |
3818 | dp_netdev_add_rxq_to_pmd(q->pmd, q); | |
3819 | ovs_mutex_unlock(&q->pmd->port_mutex); | |
3820 | } | |
3821 | } | |
3822 | } | |
3823 | ||
3824 | /* Add every port to the tx cache of every pmd thread, if it's not | |
3825 | * there already and if this pmd has at least one rxq to poll. */ | |
3826 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
3827 | ovs_mutex_lock(&pmd->port_mutex); | |
3828 | if (hmap_count(&pmd->poll_list) || pmd->core_id == NON_PMD_CORE_ID) { | |
3829 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
3830 | dp_netdev_add_port_tx_to_pmd(pmd, port); | |
3831 | } | |
3832 | } | |
3833 | ovs_mutex_unlock(&pmd->port_mutex); | |
3834 | } | |
3835 | ||
3836 | /* Reload affected pmd threads. */ | |
3837 | reload_affected_pmds(dp); | |
6e3c6fa4 DDP |
3838 | } |
3839 | ||
050c60bf DDP |
3840 | /* Returns true if one of the netdevs in 'dp' requires a reconfiguration */ |
3841 | static bool | |
3842 | ports_require_restart(const struct dp_netdev *dp) | |
3843 | OVS_REQUIRES(dp->port_mutex) | |
3844 | { | |
3845 | struct dp_netdev_port *port; | |
3846 | ||
3847 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
3848 | if (netdev_is_reconf_required(port->netdev)) { | |
3849 | return true; | |
3850 | } | |
3851 | } | |
3852 | ||
3853 | return false; | |
3854 | } | |
3855 | ||
a36de779 PS |
3856 | /* Return true if needs to revalidate datapath flows. */ |
3857 | static bool | |
e4cfed38 PS |
3858 | dpif_netdev_run(struct dpif *dpif) |
3859 | { | |
3860 | struct dp_netdev_port *port; | |
3861 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
546e57d4 | 3862 | struct dp_netdev_pmd_thread *non_pmd; |
a36de779 | 3863 | uint64_t new_tnl_seq; |
a2ac666d | 3864 | int process_packets = 0; |
e4cfed38 | 3865 | |
e9985d6a | 3866 | ovs_mutex_lock(&dp->port_mutex); |
546e57d4 DDP |
3867 | non_pmd = dp_netdev_get_pmd(dp, NON_PMD_CORE_ID); |
3868 | if (non_pmd) { | |
3869 | ovs_mutex_lock(&dp->non_pmd_mutex); | |
a2ac666d | 3870 | cycles_count_start(non_pmd); |
546e57d4 DDP |
3871 | HMAP_FOR_EACH (port, node, &dp->ports) { |
3872 | if (!netdev_is_pmd(port->netdev)) { | |
3873 | int i; | |
55c955bd | 3874 | |
546e57d4 | 3875 | for (i = 0; i < port->n_rxq; i++) { |
a2ac666d CL |
3876 | process_packets = |
3877 | dp_netdev_process_rxq_port(non_pmd, | |
3878 | port->rxqs[i].rx, | |
3879 | port->port_no); | |
28080276 KT |
3880 | cycles_count_intermediate(non_pmd, NULL, |
3881 | process_packets | |
3882 | ? PMD_CYCLES_PROCESSING | |
3883 | : PMD_CYCLES_IDLE); | |
546e57d4 | 3884 | } |
55c955bd | 3885 | } |
e4cfed38 | 3886 | } |
a2ac666d | 3887 | cycles_count_end(non_pmd, PMD_CYCLES_IDLE); |
546e57d4 DDP |
3888 | dpif_netdev_xps_revalidate_pmd(non_pmd, time_msec(), false); |
3889 | ovs_mutex_unlock(&dp->non_pmd_mutex); | |
6e3c6fa4 | 3890 | |
546e57d4 DDP |
3891 | dp_netdev_pmd_unref(non_pmd); |
3892 | } | |
1c1e46ed | 3893 | |
a6a426d6 | 3894 | if (dp_netdev_is_reconf_required(dp) || ports_require_restart(dp)) { |
e32971b8 | 3895 | reconfigure_datapath(dp); |
6e3c6fa4 DDP |
3896 | } |
3897 | ovs_mutex_unlock(&dp->port_mutex); | |
3898 | ||
53902038 | 3899 | tnl_neigh_cache_run(); |
7f9b8504 | 3900 | tnl_port_map_run(); |
a36de779 PS |
3901 | new_tnl_seq = seq_read(tnl_conf_seq); |
3902 | ||
3903 | if (dp->last_tnl_conf_seq != new_tnl_seq) { | |
3904 | dp->last_tnl_conf_seq = new_tnl_seq; | |
3905 | return true; | |
3906 | } | |
3907 | return false; | |
e4cfed38 PS |
3908 | } |
3909 | ||
3910 | static void | |
3911 | dpif_netdev_wait(struct dpif *dpif) | |
3912 | { | |
3913 | struct dp_netdev_port *port; | |
3914 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
3915 | ||
59e6d833 | 3916 | ovs_mutex_lock(&dp_netdev_mutex); |
e9985d6a DDP |
3917 | ovs_mutex_lock(&dp->port_mutex); |
3918 | HMAP_FOR_EACH (port, node, &dp->ports) { | |
050c60bf | 3919 | netdev_wait_reconf_required(port->netdev); |
55c955bd PS |
3920 | if (!netdev_is_pmd(port->netdev)) { |
3921 | int i; | |
3922 | ||
490e82af | 3923 | for (i = 0; i < port->n_rxq; i++) { |
947dc567 | 3924 | netdev_rxq_wait(port->rxqs[i].rx); |
55c955bd | 3925 | } |
e4cfed38 PS |
3926 | } |
3927 | } | |
e9985d6a | 3928 | ovs_mutex_unlock(&dp->port_mutex); |
59e6d833 | 3929 | ovs_mutex_unlock(&dp_netdev_mutex); |
a36de779 | 3930 | seq_wait(tnl_conf_seq, dp->last_tnl_conf_seq); |
e4cfed38 PS |
3931 | } |
3932 | ||
d0cca6c3 DDP |
3933 | static void |
3934 | pmd_free_cached_ports(struct dp_netdev_pmd_thread *pmd) | |
3935 | { | |
3936 | struct tx_port *tx_port_cached; | |
3937 | ||
324c8374 IM |
3938 | /* Free all used tx queue ids. */ |
3939 | dpif_netdev_xps_revalidate_pmd(pmd, 0, true); | |
3940 | ||
57eebbb4 DDP |
3941 | HMAP_FOR_EACH_POP (tx_port_cached, node, &pmd->tnl_port_cache) { |
3942 | free(tx_port_cached); | |
3943 | } | |
3944 | HMAP_FOR_EACH_POP (tx_port_cached, node, &pmd->send_port_cache) { | |
d0cca6c3 DDP |
3945 | free(tx_port_cached); |
3946 | } | |
3947 | } | |
3948 | ||
3949 | /* Copies ports from 'pmd->tx_ports' (shared with the main thread) to | |
899363ed BB |
3950 | * thread-local copies. Copy to 'pmd->tnl_port_cache' if it is a tunnel |
3951 | * device, otherwise to 'pmd->send_port_cache' if the port has at least | |
3952 | * one txq. */ | |
d0cca6c3 DDP |
3953 | static void |
3954 | pmd_load_cached_ports(struct dp_netdev_pmd_thread *pmd) | |
3955 | OVS_REQUIRES(pmd->port_mutex) | |
3956 | { | |
3957 | struct tx_port *tx_port, *tx_port_cached; | |
3958 | ||
3959 | pmd_free_cached_ports(pmd); | |
57eebbb4 DDP |
3960 | hmap_shrink(&pmd->send_port_cache); |
3961 | hmap_shrink(&pmd->tnl_port_cache); | |
d0cca6c3 DDP |
3962 | |
3963 | HMAP_FOR_EACH (tx_port, node, &pmd->tx_ports) { | |
57eebbb4 DDP |
3964 | if (netdev_has_tunnel_push_pop(tx_port->port->netdev)) { |
3965 | tx_port_cached = xmemdup(tx_port, sizeof *tx_port_cached); | |
3966 | hmap_insert(&pmd->tnl_port_cache, &tx_port_cached->node, | |
3967 | hash_port_no(tx_port_cached->port->port_no)); | |
3968 | } | |
3969 | ||
3970 | if (netdev_n_txq(tx_port->port->netdev)) { | |
3971 | tx_port_cached = xmemdup(tx_port, sizeof *tx_port_cached); | |
3972 | hmap_insert(&pmd->send_port_cache, &tx_port_cached->node, | |
3973 | hash_port_no(tx_port_cached->port->port_no)); | |
3974 | } | |
d0cca6c3 DDP |
3975 | } |
3976 | } | |
3977 | ||
140dd699 IM |
3978 | static void |
3979 | pmd_alloc_static_tx_qid(struct dp_netdev_pmd_thread *pmd) | |
3980 | { | |
3981 | ovs_mutex_lock(&pmd->dp->tx_qid_pool_mutex); | |
3982 | if (!id_pool_alloc_id(pmd->dp->tx_qid_pool, &pmd->static_tx_qid)) { | |
3983 | VLOG_ABORT("static_tx_qid allocation failed for PMD on core %2d" | |
3984 | ", numa_id %d.", pmd->core_id, pmd->numa_id); | |
3985 | } | |
3986 | ovs_mutex_unlock(&pmd->dp->tx_qid_pool_mutex); | |
3987 | ||
3988 | VLOG_DBG("static_tx_qid = %d allocated for PMD thread on core %2d" | |
3989 | ", numa_id %d.", pmd->static_tx_qid, pmd->core_id, pmd->numa_id); | |
3990 | } | |
3991 | ||
3992 | static void | |
3993 | pmd_free_static_tx_qid(struct dp_netdev_pmd_thread *pmd) | |
3994 | { | |
3995 | ovs_mutex_lock(&pmd->dp->tx_qid_pool_mutex); | |
3996 | id_pool_free_id(pmd->dp->tx_qid_pool, pmd->static_tx_qid); | |
3997 | ovs_mutex_unlock(&pmd->dp->tx_qid_pool_mutex); | |
3998 | } | |
3999 | ||
e4cfed38 | 4000 | static int |
d0cca6c3 | 4001 | pmd_load_queues_and_ports(struct dp_netdev_pmd_thread *pmd, |
947dc567 | 4002 | struct polled_queue **ppoll_list) |
e4cfed38 | 4003 | { |
947dc567 | 4004 | struct polled_queue *poll_list = *ppoll_list; |
ae7ad0a1 IM |
4005 | struct rxq_poll *poll; |
4006 | int i; | |
e4cfed38 | 4007 | |
d0cca6c3 | 4008 | ovs_mutex_lock(&pmd->port_mutex); |
947dc567 DDP |
4009 | poll_list = xrealloc(poll_list, hmap_count(&pmd->poll_list) |
4010 | * sizeof *poll_list); | |
a1fdee13 | 4011 | |
ae7ad0a1 | 4012 | i = 0; |
947dc567 | 4013 | HMAP_FOR_EACH (poll, node, &pmd->poll_list) { |
922b28d4 | 4014 | poll_list[i].rxq = poll->rxq; |
947dc567 DDP |
4015 | poll_list[i].port_no = poll->rxq->port->port_no; |
4016 | i++; | |
e4cfed38 | 4017 | } |
d0cca6c3 DDP |
4018 | |
4019 | pmd_load_cached_ports(pmd); | |
4020 | ||
4021 | ovs_mutex_unlock(&pmd->port_mutex); | |
e4cfed38 | 4022 | |
e4cfed38 | 4023 | *ppoll_list = poll_list; |
d42f9307 | 4024 | return i; |
e4cfed38 PS |
4025 | } |
4026 | ||
6c3eee82 | 4027 | static void * |
e4cfed38 | 4028 | pmd_thread_main(void *f_) |
6c3eee82 | 4029 | { |
65f13b50 | 4030 | struct dp_netdev_pmd_thread *pmd = f_; |
e4cfed38 | 4031 | unsigned int lc = 0; |
947dc567 | 4032 | struct polled_queue *poll_list; |
d42f9307 | 4033 | bool exiting; |
e4cfed38 PS |
4034 | int poll_cnt; |
4035 | int i; | |
a2ac666d | 4036 | int process_packets = 0; |
6c3eee82 | 4037 | |
e4cfed38 PS |
4038 | poll_list = NULL; |
4039 | ||
65f13b50 AW |
4040 | /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */ |
4041 | ovsthread_setspecific(pmd->dp->per_pmd_key, pmd); | |
6930c7e0 DDP |
4042 | ovs_numa_thread_setaffinity_core(pmd->core_id); |
4043 | dpdk_set_lcore_id(pmd->core_id); | |
d0cca6c3 | 4044 | poll_cnt = pmd_load_queues_and_ports(pmd, &poll_list); |
e215018b | 4045 | emc_cache_init(&pmd->flow_cache); |
e4cfed38 | 4046 | reload: |
140dd699 | 4047 | pmd_alloc_static_tx_qid(pmd); |
ae7ad0a1 | 4048 | |
7dd671f0 MK |
4049 | /* List port/core affinity */ |
4050 | for (i = 0; i < poll_cnt; i++) { | |
ce179f11 | 4051 | VLOG_DBG("Core %d processing port \'%s\' with queue-id %d\n", |
922b28d4 KT |
4052 | pmd->core_id, netdev_rxq_get_name(poll_list[i].rxq->rx), |
4053 | netdev_rxq_get_queue_id(poll_list[i].rxq->rx)); | |
7dd671f0 MK |
4054 | } |
4055 | ||
2788a1b1 DDP |
4056 | if (!poll_cnt) { |
4057 | while (seq_read(pmd->reload_seq) == pmd->last_reload_seq) { | |
4058 | seq_wait(pmd->reload_seq, pmd->last_reload_seq); | |
4059 | poll_block(); | |
4060 | } | |
4061 | lc = UINT_MAX; | |
4062 | } | |
4063 | ||
a2ac666d | 4064 | cycles_count_start(pmd); |
e4cfed38 | 4065 | for (;;) { |
e4cfed38 | 4066 | for (i = 0; i < poll_cnt; i++) { |
a2ac666d | 4067 | process_packets = |
922b28d4 | 4068 | dp_netdev_process_rxq_port(pmd, poll_list[i].rxq->rx, |
a2ac666d | 4069 | poll_list[i].port_no); |
4809891b | 4070 | cycles_count_intermediate(pmd, poll_list[i].rxq, |
a2ac666d CL |
4071 | process_packets ? PMD_CYCLES_PROCESSING |
4072 | : PMD_CYCLES_IDLE); | |
e4cfed38 PS |
4073 | } |
4074 | ||
4075 | if (lc++ > 1024) { | |
14e3e12a | 4076 | bool reload; |
6c3eee82 | 4077 | |
e4cfed38 | 4078 | lc = 0; |
84067a4c | 4079 | |
fbe0962b | 4080 | coverage_try_clear(); |
4809891b | 4081 | dp_netdev_pmd_try_optimize(pmd, poll_list, poll_cnt); |
9dede5cf FL |
4082 | if (!ovsrcu_try_quiesce()) { |
4083 | emc_cache_slow_sweep(&pmd->flow_cache); | |
4084 | } | |
84067a4c | 4085 | |
14e3e12a DDP |
4086 | atomic_read_relaxed(&pmd->reload, &reload); |
4087 | if (reload) { | |
6c3eee82 BP |
4088 | break; |
4089 | } | |
4090 | } | |
e4cfed38 | 4091 | } |
6c3eee82 | 4092 | |
a2ac666d CL |
4093 | cycles_count_end(pmd, PMD_CYCLES_IDLE); |
4094 | ||
d0cca6c3 | 4095 | poll_cnt = pmd_load_queues_and_ports(pmd, &poll_list); |
d42f9307 DDP |
4096 | exiting = latch_is_set(&pmd->exit_latch); |
4097 | /* Signal here to make sure the pmd finishes | |
4098 | * reloading the updated configuration. */ | |
4099 | dp_netdev_pmd_reload_done(pmd); | |
4100 | ||
140dd699 | 4101 | pmd_free_static_tx_qid(pmd); |
9bbf1c3d | 4102 | |
d42f9307 | 4103 | if (!exiting) { |
e4cfed38 PS |
4104 | goto reload; |
4105 | } | |
6c3eee82 | 4106 | |
e215018b | 4107 | emc_cache_uninit(&pmd->flow_cache); |
e4cfed38 | 4108 | free(poll_list); |
d0cca6c3 | 4109 | pmd_free_cached_ports(pmd); |
6c3eee82 BP |
4110 | return NULL; |
4111 | } | |
4112 | ||
6b31e073 RW |
4113 | static void |
4114 | dp_netdev_disable_upcall(struct dp_netdev *dp) | |
4115 | OVS_ACQUIRES(dp->upcall_rwlock) | |
4116 | { | |
4117 | fat_rwlock_wrlock(&dp->upcall_rwlock); | |
4118 | } | |
4119 | ||
5dddf960 JR |
4120 | \f |
4121 | /* Meters */ | |
4122 | static void | |
4123 | dpif_netdev_meter_get_features(const struct dpif * dpif OVS_UNUSED, | |
4124 | struct ofputil_meter_features *features) | |
4125 | { | |
4b27db64 JR |
4126 | features->max_meters = MAX_METERS; |
4127 | features->band_types = DP_SUPPORTED_METER_BAND_TYPES; | |
4128 | features->capabilities = DP_SUPPORTED_METER_FLAGS_MASK; | |
4129 | features->max_bands = MAX_BANDS; | |
5dddf960 JR |
4130 | features->max_color = 0; |
4131 | } | |
4132 | ||
4b27db64 JR |
4133 | /* Returns false when packet needs to be dropped. */ |
4134 | static void | |
4135 | dp_netdev_run_meter(struct dp_netdev *dp, struct dp_packet_batch *packets_, | |
4136 | uint32_t meter_id, long long int now) | |
4137 | { | |
4138 | struct dp_meter *meter; | |
4139 | struct dp_meter_band *band; | |
79c81260 | 4140 | struct dp_packet *packet; |
4b27db64 JR |
4141 | long long int long_delta_t; /* msec */ |
4142 | uint32_t delta_t; /* msec */ | |
4143 | int i; | |
79c81260 | 4144 | const size_t cnt = dp_packet_batch_size(packets_); |
4b27db64 JR |
4145 | uint32_t bytes, volume; |
4146 | int exceeded_band[NETDEV_MAX_BURST]; | |
4147 | uint32_t exceeded_rate[NETDEV_MAX_BURST]; | |
4148 | int exceeded_pkt = cnt; /* First packet that exceeded a band rate. */ | |
4149 | ||
4150 | if (meter_id >= MAX_METERS) { | |
4151 | return; | |
4152 | } | |
4153 | ||
4154 | meter_lock(dp, meter_id); | |
4155 | meter = dp->meters[meter_id]; | |
4156 | if (!meter) { | |
4157 | goto out; | |
4158 | } | |
4159 | ||
4160 | /* Initialize as negative values. */ | |
4161 | memset(exceeded_band, 0xff, cnt * sizeof *exceeded_band); | |
4162 | /* Initialize as zeroes. */ | |
4163 | memset(exceeded_rate, 0, cnt * sizeof *exceeded_rate); | |
4164 | ||
4165 | /* All packets will hit the meter at the same time. */ | |
4166 | long_delta_t = (now - meter->used); /* msec */ | |
4167 | ||
4168 | /* Make sure delta_t will not be too large, so that bucket will not | |
4169 | * wrap around below. */ | |
4170 | delta_t = (long_delta_t > (long long int)meter->max_delta_t) | |
4171 | ? meter->max_delta_t : (uint32_t)long_delta_t; | |
4172 | ||
4173 | /* Update meter stats. */ | |
4174 | meter->used = now; | |
4175 | meter->packet_count += cnt; | |
4176 | bytes = 0; | |
79c81260 BB |
4177 | DP_PACKET_BATCH_FOR_EACH (packet, packets_) { |
4178 | bytes += dp_packet_size(packet); | |
4b27db64 JR |
4179 | } |
4180 | meter->byte_count += bytes; | |
4181 | ||
4182 | /* Meters can operate in terms of packets per second or kilobits per | |
4183 | * second. */ | |
4184 | if (meter->flags & OFPMF13_PKTPS) { | |
4185 | /* Rate in packets/second, bucket 1/1000 packets. */ | |
4186 | /* msec * packets/sec = 1/1000 packets. */ | |
4187 | volume = cnt * 1000; /* Take 'cnt' packets from the bucket. */ | |
4188 | } else { | |
4189 | /* Rate in kbps, bucket in bits. */ | |
4190 | /* msec * kbps = bits */ | |
4191 | volume = bytes * 8; | |
4192 | } | |
4193 | ||
4194 | /* Update all bands and find the one hit with the highest rate for each | |
4195 | * packet (if any). */ | |
4196 | for (int m = 0; m < meter->n_bands; ++m) { | |
4197 | band = &meter->bands[m]; | |
4198 | ||
4199 | /* Update band's bucket. */ | |
4200 | band->bucket += delta_t * band->up.rate; | |
4201 | if (band->bucket > band->up.burst_size) { | |
4202 | band->bucket = band->up.burst_size; | |
4203 | } | |
4204 | ||
4205 | /* Drain the bucket for all the packets, if possible. */ | |
4206 | if (band->bucket >= volume) { | |
4207 | band->bucket -= volume; | |
4208 | } else { | |
4209 | int band_exceeded_pkt; | |
4210 | ||
4211 | /* Band limit hit, must process packet-by-packet. */ | |
4212 | if (meter->flags & OFPMF13_PKTPS) { | |
4213 | band_exceeded_pkt = band->bucket / 1000; | |
4214 | band->bucket %= 1000; /* Remainder stays in bucket. */ | |
4215 | ||
4216 | /* Update the exceeding band for each exceeding packet. | |
4217 | * (Only one band will be fired by a packet, and that | |
4218 | * can be different for each packet.) */ | |
4219 | for (i = band_exceeded_pkt; i < cnt; i++) { | |
4220 | if (band->up.rate > exceeded_rate[i]) { | |
4221 | exceeded_rate[i] = band->up.rate; | |
4222 | exceeded_band[i] = m; | |
4223 | } | |
4224 | } | |
4225 | } else { | |
4226 | /* Packet sizes differ, must process one-by-one. */ | |
4227 | band_exceeded_pkt = cnt; | |
79c81260 BB |
4228 | DP_PACKET_BATCH_FOR_EACH (packet, packets_) { |
4229 | uint32_t bits = dp_packet_size(packet) * 8; | |
4b27db64 JR |
4230 | |
4231 | if (band->bucket >= bits) { | |
4232 | band->bucket -= bits; | |
4233 | } else { | |
4234 | if (i < band_exceeded_pkt) { | |
4235 | band_exceeded_pkt = i; | |
4236 | } | |
4237 | /* Update the exceeding band for the exceeding packet. | |
4238 | * (Only one band will be fired by a packet, and that | |
4239 | * can be different for each packet.) */ | |
4240 | if (band->up.rate > exceeded_rate[i]) { | |
4241 | exceeded_rate[i] = band->up.rate; | |
4242 | exceeded_band[i] = m; | |
4243 | } | |
4244 | } | |
4245 | } | |
4246 | } | |
4247 | /* Remember the first exceeding packet. */ | |
4248 | if (exceeded_pkt > band_exceeded_pkt) { | |
4249 | exceeded_pkt = band_exceeded_pkt; | |
4250 | } | |
4251 | } | |
4252 | } | |
4253 | ||
4254 | /* Fire the highest rate band exceeded by each packet. | |
4255 | * Drop packets if needed, by swapping packet to the end that will be | |
4256 | * ignored. */ | |
4b27db64 | 4257 | size_t j; |
79c81260 | 4258 | DP_PACKET_BATCH_REFILL_FOR_EACH (j, cnt, packet, packets_) { |
4b27db64 JR |
4259 | if (exceeded_band[j] >= 0) { |
4260 | /* Meter drop packet. */ | |
4261 | band = &meter->bands[exceeded_band[j]]; | |
4262 | band->packet_count += 1; | |
4263 | band->byte_count += dp_packet_size(packet); | |
4264 | ||
4265 | dp_packet_delete(packet); | |
4266 | } else { | |
4267 | /* Meter accepts packet. */ | |
4268 | dp_packet_batch_refill(packets_, packet, j); | |
4269 | } | |
4270 | } | |
4271 | out: | |
4272 | meter_unlock(dp, meter_id); | |
4273 | } | |
4274 | ||
4275 | /* Meter set/get/del processing is still single-threaded. */ | |
5dddf960 | 4276 | static int |
4b27db64 JR |
4277 | dpif_netdev_meter_set(struct dpif *dpif, ofproto_meter_id *meter_id, |
4278 | struct ofputil_meter_config *config) | |
5dddf960 | 4279 | { |
4b27db64 JR |
4280 | struct dp_netdev *dp = get_dp_netdev(dpif); |
4281 | uint32_t mid = meter_id->uint32; | |
4282 | struct dp_meter *meter; | |
4283 | int i; | |
4284 | ||
4b27db64 JR |
4285 | if (mid >= MAX_METERS) { |
4286 | return EFBIG; /* Meter_id out of range. */ | |
4287 | } | |
4288 | ||
4289 | if (config->flags & ~DP_SUPPORTED_METER_FLAGS_MASK || | |
4290 | !(config->flags & (OFPMF13_KBPS | OFPMF13_PKTPS))) { | |
4291 | return EBADF; /* Unsupported flags set */ | |
4292 | } | |
2029ce9a | 4293 | |
4b27db64 JR |
4294 | /* Validate bands */ |
4295 | if (config->n_bands == 0 || config->n_bands > MAX_BANDS) { | |
4296 | return EINVAL; /* Too many bands */ | |
4297 | } | |
2029ce9a AVA |
4298 | |
4299 | /* Validate rates */ | |
4300 | for (i = 0; i < config->n_bands; i++) { | |
4301 | if (config->bands[i].rate == 0) { | |
66a396d4 | 4302 | return EDOM; /* rate must be non-zero */ |
2029ce9a AVA |
4303 | } |
4304 | } | |
4305 | ||
4b27db64 JR |
4306 | for (i = 0; i < config->n_bands; ++i) { |
4307 | switch (config->bands[i].type) { | |
4308 | case OFPMBT13_DROP: | |
4309 | break; | |
4310 | default: | |
4311 | return ENODEV; /* Unsupported band type */ | |
4312 | } | |
4313 | } | |
4314 | ||
4315 | /* Allocate meter */ | |
4316 | meter = xzalloc(sizeof *meter | |
4317 | + config->n_bands * sizeof(struct dp_meter_band)); | |
4318 | if (meter) { | |
4319 | meter->flags = config->flags; | |
4320 | meter->n_bands = config->n_bands; | |
4321 | meter->max_delta_t = 0; | |
4322 | meter->used = time_msec(); | |
4323 | ||
4324 | /* set up bands */ | |
4325 | for (i = 0; i < config->n_bands; ++i) { | |
4326 | uint32_t band_max_delta_t; | |
4327 | ||
4328 | /* Set burst size to a workable value if none specified. */ | |
4329 | if (config->bands[i].burst_size == 0) { | |
4330 | config->bands[i].burst_size = config->bands[i].rate; | |
4331 | } | |
4332 | ||
4333 | meter->bands[i].up = config->bands[i]; | |
4334 | /* Convert burst size to the bucket units: */ | |
4335 | /* pkts => 1/1000 packets, kilobits => bits. */ | |
4336 | meter->bands[i].up.burst_size *= 1000; | |
4337 | /* Initialize bucket to empty. */ | |
4338 | meter->bands[i].bucket = 0; | |
4339 | ||
4340 | /* Figure out max delta_t that is enough to fill any bucket. */ | |
4341 | band_max_delta_t | |
4342 | = meter->bands[i].up.burst_size / meter->bands[i].up.rate; | |
4343 | if (band_max_delta_t > meter->max_delta_t) { | |
4344 | meter->max_delta_t = band_max_delta_t; | |
4345 | } | |
4346 | } | |
4347 | ||
4348 | meter_lock(dp, mid); | |
4349 | dp_delete_meter(dp, mid); /* Free existing meter, if any */ | |
4350 | dp->meters[mid] = meter; | |
4351 | meter_unlock(dp, mid); | |
4352 | ||
4b27db64 JR |
4353 | return 0; |
4354 | } | |
4355 | return ENOMEM; | |
5dddf960 JR |
4356 | } |
4357 | ||
4358 | static int | |
4b27db64 JR |
4359 | dpif_netdev_meter_get(const struct dpif *dpif, |
4360 | ofproto_meter_id meter_id_, | |
4361 | struct ofputil_meter_stats *stats, uint16_t n_bands) | |
5dddf960 | 4362 | { |
4b27db64 JR |
4363 | const struct dp_netdev *dp = get_dp_netdev(dpif); |
4364 | const struct dp_meter *meter; | |
4365 | uint32_t meter_id = meter_id_.uint32; | |
4366 | ||
4367 | if (meter_id >= MAX_METERS) { | |
4368 | return EFBIG; | |
4369 | } | |
4370 | meter = dp->meters[meter_id]; | |
4371 | if (!meter) { | |
4372 | return ENOENT; | |
4373 | } | |
4374 | if (stats) { | |
4375 | int i = 0; | |
4376 | ||
4377 | meter_lock(dp, meter_id); | |
4378 | stats->packet_in_count = meter->packet_count; | |
4379 | stats->byte_in_count = meter->byte_count; | |
4380 | ||
4381 | for (i = 0; i < n_bands && i < meter->n_bands; ++i) { | |
4382 | stats->bands[i].packet_count = meter->bands[i].packet_count; | |
4383 | stats->bands[i].byte_count = meter->bands[i].byte_count; | |
4384 | } | |
4385 | meter_unlock(dp, meter_id); | |
4386 | ||
4387 | stats->n_bands = i; | |
4388 | } | |
4389 | return 0; | |
5dddf960 JR |
4390 | } |
4391 | ||
4392 | static int | |
4b27db64 JR |
4393 | dpif_netdev_meter_del(struct dpif *dpif, |
4394 | ofproto_meter_id meter_id_, | |
4395 | struct ofputil_meter_stats *stats, uint16_t n_bands) | |
5dddf960 | 4396 | { |
4b27db64 JR |
4397 | struct dp_netdev *dp = get_dp_netdev(dpif); |
4398 | int error; | |
4399 | ||
4400 | error = dpif_netdev_meter_get(dpif, meter_id_, stats, n_bands); | |
4401 | if (!error) { | |
4402 | uint32_t meter_id = meter_id_.uint32; | |
4403 | ||
4404 | meter_lock(dp, meter_id); | |
4405 | dp_delete_meter(dp, meter_id); | |
4406 | meter_unlock(dp, meter_id); | |
4b27db64 JR |
4407 | } |
4408 | return error; | |
5dddf960 JR |
4409 | } |
4410 | ||
4411 | \f | |
6b31e073 RW |
4412 | static void |
4413 | dpif_netdev_disable_upcall(struct dpif *dpif) | |
4414 | OVS_NO_THREAD_SAFETY_ANALYSIS | |
4415 | { | |
4416 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
4417 | dp_netdev_disable_upcall(dp); | |
4418 | } | |
4419 | ||
4420 | static void | |
4421 | dp_netdev_enable_upcall(struct dp_netdev *dp) | |
4422 | OVS_RELEASES(dp->upcall_rwlock) | |
4423 | { | |
4424 | fat_rwlock_unlock(&dp->upcall_rwlock); | |
4425 | } | |
4426 | ||
4427 | static void | |
4428 | dpif_netdev_enable_upcall(struct dpif *dpif) | |
4429 | OVS_NO_THREAD_SAFETY_ANALYSIS | |
4430 | { | |
4431 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
4432 | dp_netdev_enable_upcall(dp); | |
4433 | } | |
4434 | ||
ae7ad0a1 | 4435 | static void |
accf8626 AW |
4436 | dp_netdev_pmd_reload_done(struct dp_netdev_pmd_thread *pmd) |
4437 | { | |
4438 | ovs_mutex_lock(&pmd->cond_mutex); | |
14e3e12a | 4439 | atomic_store_relaxed(&pmd->reload, false); |
2788a1b1 | 4440 | pmd->last_reload_seq = seq_read(pmd->reload_seq); |
accf8626 AW |
4441 | xpthread_cond_signal(&pmd->cond); |
4442 | ovs_mutex_unlock(&pmd->cond_mutex); | |
4443 | } | |
4444 | ||
1c1e46ed | 4445 | /* Finds and refs the dp_netdev_pmd_thread on core 'core_id'. Returns |
546e57d4 DDP |
4446 | * the pointer if succeeds, otherwise, NULL (it can return NULL even if |
4447 | * 'core_id' is NON_PMD_CORE_ID). | |
1c1e46ed AW |
4448 | * |
4449 | * Caller must unrefs the returned reference. */ | |
65f13b50 | 4450 | static struct dp_netdev_pmd_thread * |
bd5131ba | 4451 | dp_netdev_get_pmd(struct dp_netdev *dp, unsigned core_id) |
65f13b50 AW |
4452 | { |
4453 | struct dp_netdev_pmd_thread *pmd; | |
55847abe | 4454 | const struct cmap_node *pnode; |
65f13b50 | 4455 | |
b19befae | 4456 | pnode = cmap_find(&dp->poll_threads, hash_int(core_id, 0)); |
1c1e46ed AW |
4457 | if (!pnode) { |
4458 | return NULL; | |
4459 | } | |
65f13b50 AW |
4460 | pmd = CONTAINER_OF(pnode, struct dp_netdev_pmd_thread, node); |
4461 | ||
1c1e46ed | 4462 | return dp_netdev_pmd_try_ref(pmd) ? pmd : NULL; |
65f13b50 AW |
4463 | } |
4464 | ||
f2eee189 AW |
4465 | /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */ |
4466 | static void | |
4467 | dp_netdev_set_nonpmd(struct dp_netdev *dp) | |
e9985d6a | 4468 | OVS_REQUIRES(dp->port_mutex) |
f2eee189 AW |
4469 | { |
4470 | struct dp_netdev_pmd_thread *non_pmd; | |
4471 | ||
4472 | non_pmd = xzalloc(sizeof *non_pmd); | |
00873463 | 4473 | dp_netdev_configure_pmd(non_pmd, dp, NON_PMD_CORE_ID, OVS_NUMA_UNSPEC); |
f2eee189 AW |
4474 | } |
4475 | ||
1c1e46ed AW |
4476 | /* Caller must have valid pointer to 'pmd'. */ |
4477 | static bool | |
4478 | dp_netdev_pmd_try_ref(struct dp_netdev_pmd_thread *pmd) | |
4479 | { | |
4480 | return ovs_refcount_try_ref_rcu(&pmd->ref_cnt); | |
4481 | } | |
4482 | ||
4483 | static void | |
4484 | dp_netdev_pmd_unref(struct dp_netdev_pmd_thread *pmd) | |
4485 | { | |
4486 | if (pmd && ovs_refcount_unref(&pmd->ref_cnt) == 1) { | |
4487 | ovsrcu_postpone(dp_netdev_destroy_pmd, pmd); | |
4488 | } | |
4489 | } | |
4490 | ||
4491 | /* Given cmap position 'pos', tries to ref the next node. If try_ref() | |
4492 | * fails, keeps checking for next node until reaching the end of cmap. | |
4493 | * | |
4494 | * Caller must unrefs the returned reference. */ | |
4495 | static struct dp_netdev_pmd_thread * | |
4496 | dp_netdev_pmd_get_next(struct dp_netdev *dp, struct cmap_position *pos) | |
4497 | { | |
4498 | struct dp_netdev_pmd_thread *next; | |
4499 | ||
4500 | do { | |
4501 | struct cmap_node *node; | |
4502 | ||
4503 | node = cmap_next_position(&dp->poll_threads, pos); | |
4504 | next = node ? CONTAINER_OF(node, struct dp_netdev_pmd_thread, node) | |
4505 | : NULL; | |
4506 | } while (next && !dp_netdev_pmd_try_ref(next)); | |
4507 | ||
4508 | return next; | |
4509 | } | |
4510 | ||
65f13b50 | 4511 | /* Configures the 'pmd' based on the input argument. */ |
6c3eee82 | 4512 | static void |
65f13b50 | 4513 | dp_netdev_configure_pmd(struct dp_netdev_pmd_thread *pmd, struct dp_netdev *dp, |
00873463 | 4514 | unsigned core_id, int numa_id) |
65f13b50 AW |
4515 | { |
4516 | pmd->dp = dp; | |
65f13b50 AW |
4517 | pmd->core_id = core_id; |
4518 | pmd->numa_id = numa_id; | |
e32971b8 | 4519 | pmd->need_reload = false; |
1c1e46ed AW |
4520 | |
4521 | ovs_refcount_init(&pmd->ref_cnt); | |
65f13b50 | 4522 | latch_init(&pmd->exit_latch); |
2788a1b1 DDP |
4523 | pmd->reload_seq = seq_create(); |
4524 | pmd->last_reload_seq = seq_read(pmd->reload_seq); | |
14e3e12a | 4525 | atomic_init(&pmd->reload, false); |
accf8626 AW |
4526 | xpthread_cond_init(&pmd->cond, NULL); |
4527 | ovs_mutex_init(&pmd->cond_mutex); | |
1c1e46ed | 4528 | ovs_mutex_init(&pmd->flow_mutex); |
d0cca6c3 | 4529 | ovs_mutex_init(&pmd->port_mutex); |
1c1e46ed | 4530 | cmap_init(&pmd->flow_table); |
3453b4d6 JS |
4531 | cmap_init(&pmd->classifiers); |
4532 | pmd->next_optimization = time_msec() + DPCLS_OPTIMIZATION_INTERVAL; | |
64bf452e | 4533 | pmd->rxq_next_cycle_store = time_msec() + PMD_RXQ_INTERVAL_LEN; |
947dc567 | 4534 | hmap_init(&pmd->poll_list); |
d0cca6c3 | 4535 | hmap_init(&pmd->tx_ports); |
57eebbb4 DDP |
4536 | hmap_init(&pmd->tnl_port_cache); |
4537 | hmap_init(&pmd->send_port_cache); | |
65f13b50 AW |
4538 | /* init the 'flow_cache' since there is no |
4539 | * actual thread created for NON_PMD_CORE_ID. */ | |
4540 | if (core_id == NON_PMD_CORE_ID) { | |
4541 | emc_cache_init(&pmd->flow_cache); | |
140dd699 | 4542 | pmd_alloc_static_tx_qid(pmd); |
65f13b50 AW |
4543 | } |
4544 | cmap_insert(&dp->poll_threads, CONST_CAST(struct cmap_node *, &pmd->node), | |
4545 | hash_int(core_id, 0)); | |
4546 | } | |
4547 | ||
1c1e46ed AW |
4548 | static void |
4549 | dp_netdev_destroy_pmd(struct dp_netdev_pmd_thread *pmd) | |
4550 | { | |
3453b4d6 JS |
4551 | struct dpcls *cls; |
4552 | ||
1c1e46ed | 4553 | dp_netdev_pmd_flow_flush(pmd); |
57eebbb4 DDP |
4554 | hmap_destroy(&pmd->send_port_cache); |
4555 | hmap_destroy(&pmd->tnl_port_cache); | |
d0cca6c3 | 4556 | hmap_destroy(&pmd->tx_ports); |
947dc567 | 4557 | hmap_destroy(&pmd->poll_list); |
3453b4d6 JS |
4558 | /* All flows (including their dpcls_rules) have been deleted already */ |
4559 | CMAP_FOR_EACH (cls, node, &pmd->classifiers) { | |
4560 | dpcls_destroy(cls); | |
7c269972 | 4561 | ovsrcu_postpone(free, cls); |
3453b4d6 JS |
4562 | } |
4563 | cmap_destroy(&pmd->classifiers); | |
1c1e46ed AW |
4564 | cmap_destroy(&pmd->flow_table); |
4565 | ovs_mutex_destroy(&pmd->flow_mutex); | |
4566 | latch_destroy(&pmd->exit_latch); | |
2788a1b1 | 4567 | seq_destroy(pmd->reload_seq); |
1c1e46ed AW |
4568 | xpthread_cond_destroy(&pmd->cond); |
4569 | ovs_mutex_destroy(&pmd->cond_mutex); | |
d0cca6c3 | 4570 | ovs_mutex_destroy(&pmd->port_mutex); |
1c1e46ed AW |
4571 | free(pmd); |
4572 | } | |
4573 | ||
4574 | /* Stops the pmd thread, removes it from the 'dp->poll_threads', | |
4575 | * and unrefs the struct. */ | |
65f13b50 | 4576 | static void |
e4e74c3a | 4577 | dp_netdev_del_pmd(struct dp_netdev *dp, struct dp_netdev_pmd_thread *pmd) |
6c3eee82 | 4578 | { |
d0cca6c3 DDP |
4579 | /* NON_PMD_CORE_ID doesn't have a thread, so we don't have to synchronize, |
4580 | * but extra cleanup is necessary */ | |
65f13b50 | 4581 | if (pmd->core_id == NON_PMD_CORE_ID) { |
febf4a7a | 4582 | ovs_mutex_lock(&dp->non_pmd_mutex); |
65f13b50 | 4583 | emc_cache_uninit(&pmd->flow_cache); |
d0cca6c3 | 4584 | pmd_free_cached_ports(pmd); |
140dd699 | 4585 | pmd_free_static_tx_qid(pmd); |
febf4a7a | 4586 | ovs_mutex_unlock(&dp->non_pmd_mutex); |
65f13b50 AW |
4587 | } else { |
4588 | latch_set(&pmd->exit_latch); | |
4589 | dp_netdev_reload_pmd__(pmd); | |
65f13b50 AW |
4590 | xpthread_join(pmd->thread, NULL); |
4591 | } | |
ae7ad0a1 | 4592 | |
d0cca6c3 | 4593 | dp_netdev_pmd_clear_ports(pmd); |
ae7ad0a1 | 4594 | |
e4e74c3a AW |
4595 | /* Purges the 'pmd''s flows after stopping the thread, but before |
4596 | * destroying the flows, so that the flow stats can be collected. */ | |
4597 | if (dp->dp_purge_cb) { | |
4598 | dp->dp_purge_cb(dp->dp_purge_aux, pmd->core_id); | |
4599 | } | |
65f13b50 | 4600 | cmap_remove(&pmd->dp->poll_threads, &pmd->node, hash_int(pmd->core_id, 0)); |
1c1e46ed | 4601 | dp_netdev_pmd_unref(pmd); |
65f13b50 | 4602 | } |
6c3eee82 | 4603 | |
e32971b8 DDP |
4604 | /* Destroys all pmd threads. If 'non_pmd' is true it also destroys the non pmd |
4605 | * thread. */ | |
65f13b50 | 4606 | static void |
e32971b8 | 4607 | dp_netdev_destroy_all_pmds(struct dp_netdev *dp, bool non_pmd) |
65f13b50 AW |
4608 | { |
4609 | struct dp_netdev_pmd_thread *pmd; | |
d916785c DDP |
4610 | struct dp_netdev_pmd_thread **pmd_list; |
4611 | size_t k = 0, n_pmds; | |
4612 | ||
e32971b8 | 4613 | n_pmds = cmap_count(&dp->poll_threads); |
d916785c | 4614 | pmd_list = xcalloc(n_pmds, sizeof *pmd_list); |
65f13b50 AW |
4615 | |
4616 | CMAP_FOR_EACH (pmd, node, &dp->poll_threads) { | |
e32971b8 | 4617 | if (!non_pmd && pmd->core_id == NON_PMD_CORE_ID) { |
b9584f21 DDP |
4618 | continue; |
4619 | } | |
d916785c DDP |
4620 | /* We cannot call dp_netdev_del_pmd(), since it alters |
4621 | * 'dp->poll_threads' (while we're iterating it) and it | |
4622 | * might quiesce. */ | |
4623 | ovs_assert(k < n_pmds); | |
4624 | pmd_list[k++] = pmd; | |
6c3eee82 | 4625 | } |
d916785c DDP |
4626 | |
4627 | for (size_t i = 0; i < k; i++) { | |
4628 | dp_netdev_del_pmd(dp, pmd_list[i]); | |
4629 | } | |
4630 | free(pmd_list); | |
65f13b50 | 4631 | } |
6c3eee82 | 4632 | |
d0cca6c3 DDP |
4633 | /* Deletes all rx queues from pmd->poll_list and all the ports from |
4634 | * pmd->tx_ports. */ | |
cc245ce8 | 4635 | static void |
d0cca6c3 | 4636 | dp_netdev_pmd_clear_ports(struct dp_netdev_pmd_thread *pmd) |
cc245ce8 IM |
4637 | { |
4638 | struct rxq_poll *poll; | |
d0cca6c3 | 4639 | struct tx_port *port; |
cc245ce8 | 4640 | |
d0cca6c3 | 4641 | ovs_mutex_lock(&pmd->port_mutex); |
947dc567 | 4642 | HMAP_FOR_EACH_POP (poll, node, &pmd->poll_list) { |
cc245ce8 IM |
4643 | free(poll); |
4644 | } | |
d0cca6c3 DDP |
4645 | HMAP_FOR_EACH_POP (port, node, &pmd->tx_ports) { |
4646 | free(port); | |
4647 | } | |
4648 | ovs_mutex_unlock(&pmd->port_mutex); | |
cc245ce8 IM |
4649 | } |
4650 | ||
e32971b8 | 4651 | /* Adds rx queue to poll_list of PMD thread, if it's not there already. */ |
b68872d8 | 4652 | static void |
e32971b8 DDP |
4653 | dp_netdev_add_rxq_to_pmd(struct dp_netdev_pmd_thread *pmd, |
4654 | struct dp_netdev_rxq *rxq) | |
4655 | OVS_REQUIRES(pmd->port_mutex) | |
b68872d8 | 4656 | { |
e32971b8 DDP |
4657 | int qid = netdev_rxq_get_queue_id(rxq->rx); |
4658 | uint32_t hash = hash_2words(odp_to_u32(rxq->port->port_no), qid); | |
4659 | struct rxq_poll *poll; | |
b68872d8 | 4660 | |
e32971b8 DDP |
4661 | HMAP_FOR_EACH_WITH_HASH (poll, node, hash, &pmd->poll_list) { |
4662 | if (poll->rxq == rxq) { | |
4663 | /* 'rxq' is already polled by this thread. Do nothing. */ | |
4664 | return; | |
d0cca6c3 | 4665 | } |
cc245ce8 | 4666 | } |
cc245ce8 | 4667 | |
e32971b8 DDP |
4668 | poll = xmalloc(sizeof *poll); |
4669 | poll->rxq = rxq; | |
4670 | hmap_insert(&pmd->poll_list, &poll->node, hash); | |
b68872d8 | 4671 | |
e32971b8 | 4672 | pmd->need_reload = true; |
ae7ad0a1 IM |
4673 | } |
4674 | ||
e32971b8 | 4675 | /* Delete 'poll' from poll_list of PMD thread. */ |
ae7ad0a1 | 4676 | static void |
e32971b8 DDP |
4677 | dp_netdev_del_rxq_from_pmd(struct dp_netdev_pmd_thread *pmd, |
4678 | struct rxq_poll *poll) | |
d0cca6c3 | 4679 | OVS_REQUIRES(pmd->port_mutex) |
ae7ad0a1 | 4680 | { |
e32971b8 DDP |
4681 | hmap_remove(&pmd->poll_list, &poll->node); |
4682 | free(poll); | |
ae7ad0a1 | 4683 | |
e32971b8 | 4684 | pmd->need_reload = true; |
ae7ad0a1 IM |
4685 | } |
4686 | ||
d0cca6c3 DDP |
4687 | /* Add 'port' to the tx port cache of 'pmd', which must be reloaded for the |
4688 | * changes to take effect. */ | |
cc245ce8 | 4689 | static void |
d0cca6c3 DDP |
4690 | dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread *pmd, |
4691 | struct dp_netdev_port *port) | |
e32971b8 | 4692 | OVS_REQUIRES(pmd->port_mutex) |
d0cca6c3 | 4693 | { |
57eebbb4 DDP |
4694 | struct tx_port *tx; |
4695 | ||
e32971b8 DDP |
4696 | tx = tx_port_lookup(&pmd->tx_ports, port->port_no); |
4697 | if (tx) { | |
4698 | /* 'port' is already on this thread tx cache. Do nothing. */ | |
4699 | return; | |
4700 | } | |
4701 | ||
57eebbb4 | 4702 | tx = xzalloc(sizeof *tx); |
d0cca6c3 | 4703 | |
324c8374 IM |
4704 | tx->port = port; |
4705 | tx->qid = -1; | |
d0cca6c3 | 4706 | |
324c8374 | 4707 | hmap_insert(&pmd->tx_ports, &tx->node, hash_port_no(tx->port->port_no)); |
e32971b8 | 4708 | pmd->need_reload = true; |
d0cca6c3 DDP |
4709 | } |
4710 | ||
e32971b8 DDP |
4711 | /* Del 'tx' from the tx port cache of 'pmd', which must be reloaded for the |
4712 | * changes to take effect. */ | |
b9584f21 | 4713 | static void |
e32971b8 DDP |
4714 | dp_netdev_del_port_tx_from_pmd(struct dp_netdev_pmd_thread *pmd, |
4715 | struct tx_port *tx) | |
4716 | OVS_REQUIRES(pmd->port_mutex) | |
b9584f21 | 4717 | { |
e32971b8 DDP |
4718 | hmap_remove(&pmd->tx_ports, &tx->node); |
4719 | free(tx); | |
4720 | pmd->need_reload = true; | |
6c3eee82 BP |
4721 | } |
4722 | \f | |
b5cbbcf6 AZ |
4723 | static char * |
4724 | dpif_netdev_get_datapath_version(void) | |
4725 | { | |
4726 | return xstrdup("<built-in>"); | |
4727 | } | |
4728 | ||
72865317 | 4729 | static void |
1c1e46ed | 4730 | dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow, int cnt, int size, |
11bfdadd | 4731 | uint16_t tcp_flags, long long now) |
72865317 | 4732 | { |
eb94da30 | 4733 | uint16_t flags; |
72865317 | 4734 | |
eb94da30 DDP |
4735 | atomic_store_relaxed(&netdev_flow->stats.used, now); |
4736 | non_atomic_ullong_add(&netdev_flow->stats.packet_count, cnt); | |
4737 | non_atomic_ullong_add(&netdev_flow->stats.byte_count, size); | |
4738 | atomic_read_relaxed(&netdev_flow->stats.tcp_flags, &flags); | |
4739 | flags |= tcp_flags; | |
4740 | atomic_store_relaxed(&netdev_flow->stats.tcp_flags, flags); | |
51852a57 BP |
4741 | } |
4742 | ||
4743 | static void | |
1c1e46ed AW |
4744 | dp_netdev_count_packet(struct dp_netdev_pmd_thread *pmd, |
4745 | enum dp_stat_type type, int cnt) | |
51852a57 | 4746 | { |
eb94da30 | 4747 | non_atomic_ullong_add(&pmd->stats.n[type], cnt); |
51852a57 BP |
4748 | } |
4749 | ||
623540e4 | 4750 | static int |
e14deea0 | 4751 | dp_netdev_upcall(struct dp_netdev_pmd_thread *pmd, struct dp_packet *packet_, |
7af12bd7 | 4752 | struct flow *flow, struct flow_wildcards *wc, ovs_u128 *ufid, |
623540e4 EJ |
4753 | enum dpif_upcall_type type, const struct nlattr *userdata, |
4754 | struct ofpbuf *actions, struct ofpbuf *put_actions) | |
4755 | { | |
1c1e46ed | 4756 | struct dp_netdev *dp = pmd->dp; |
623540e4 | 4757 | |
623540e4 EJ |
4758 | if (OVS_UNLIKELY(!dp->upcall_cb)) { |
4759 | return ENODEV; | |
4760 | } | |
4761 | ||
4762 | if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl))) { | |
4763 | struct ds ds = DS_EMPTY_INITIALIZER; | |
623540e4 | 4764 | char *packet_str; |
cf62fa4c | 4765 | struct ofpbuf key; |
5262eea1 JG |
4766 | struct odp_flow_key_parms odp_parms = { |
4767 | .flow = flow, | |
1dea1435 | 4768 | .mask = wc ? &wc->masks : NULL, |
2494ccd7 | 4769 | .support = dp_netdev_support, |
5262eea1 | 4770 | }; |
623540e4 EJ |
4771 | |
4772 | ofpbuf_init(&key, 0); | |
5262eea1 | 4773 | odp_flow_key_from_flow(&odp_parms, &key); |
2482b0b0 | 4774 | packet_str = ofp_dp_packet_to_string(packet_); |
623540e4 | 4775 | |
6fd6ed71 | 4776 | odp_flow_key_format(key.data, key.size, &ds); |
623540e4 EJ |
4777 | |
4778 | VLOG_DBG("%s: %s upcall:\n%s\n%s", dp->name, | |
4779 | dpif_upcall_type_to_string(type), ds_cstr(&ds), packet_str); | |
4780 | ||
4781 | ofpbuf_uninit(&key); | |
4782 | free(packet_str); | |
6fd6ed71 | 4783 | |
623540e4 EJ |
4784 | ds_destroy(&ds); |
4785 | } | |
4786 | ||
8d8ab6c2 JG |
4787 | return dp->upcall_cb(packet_, flow, ufid, pmd->core_id, type, userdata, |
4788 | actions, wc, put_actions, dp->upcall_aux); | |
623540e4 EJ |
4789 | } |
4790 | ||
bde94613 FA |
4791 | static inline uint32_t |
4792 | dpif_netdev_packet_get_rss_hash_orig_pkt(struct dp_packet *packet, | |
4793 | const struct miniflow *mf) | |
4794 | { | |
4795 | uint32_t hash; | |
4796 | ||
4797 | if (OVS_LIKELY(dp_packet_rss_valid(packet))) { | |
4798 | hash = dp_packet_get_rss_hash(packet); | |
4799 | } else { | |
4800 | hash = miniflow_hash_5tuple(mf, 0); | |
4801 | dp_packet_set_rss_hash(packet, hash); | |
4802 | } | |
4803 | ||
4804 | return hash; | |
4805 | } | |
4806 | ||
9bbf1c3d | 4807 | static inline uint32_t |
048963aa DDP |
4808 | dpif_netdev_packet_get_rss_hash(struct dp_packet *packet, |
4809 | const struct miniflow *mf) | |
9bbf1c3d | 4810 | { |
048963aa | 4811 | uint32_t hash, recirc_depth; |
9bbf1c3d | 4812 | |
f2f44f5d DDP |
4813 | if (OVS_LIKELY(dp_packet_rss_valid(packet))) { |
4814 | hash = dp_packet_get_rss_hash(packet); | |
4815 | } else { | |
9bbf1c3d | 4816 | hash = miniflow_hash_5tuple(mf, 0); |
2bc1bbd2 | 4817 | dp_packet_set_rss_hash(packet, hash); |
9bbf1c3d | 4818 | } |
048963aa DDP |
4819 | |
4820 | /* The RSS hash must account for the recirculation depth to avoid | |
4821 | * collisions in the exact match cache */ | |
4822 | recirc_depth = *recirc_depth_get_unsafe(); | |
4823 | if (OVS_UNLIKELY(recirc_depth)) { | |
4824 | hash = hash_finish(hash, recirc_depth); | |
4825 | dp_packet_set_rss_hash(packet, hash); | |
4826 | } | |
9bbf1c3d DDP |
4827 | return hash; |
4828 | } | |
4829 | ||
f7ce4811 | 4830 | struct packet_batch_per_flow { |
8cbf4f47 DDP |
4831 | unsigned int byte_count; |
4832 | uint16_t tcp_flags; | |
8cbf4f47 DDP |
4833 | struct dp_netdev_flow *flow; |
4834 | ||
1895cc8d | 4835 | struct dp_packet_batch array; |
8cbf4f47 DDP |
4836 | }; |
4837 | ||
4838 | static inline void | |
f7ce4811 PS |
4839 | packet_batch_per_flow_update(struct packet_batch_per_flow *batch, |
4840 | struct dp_packet *packet, | |
4841 | const struct miniflow *mf) | |
8cbf4f47 | 4842 | { |
cf62fa4c | 4843 | batch->byte_count += dp_packet_size(packet); |
1895cc8d PS |
4844 | batch->tcp_flags |= miniflow_get_tcp_flags(mf); |
4845 | batch->array.packets[batch->array.count++] = packet; | |
8cbf4f47 DDP |
4846 | } |
4847 | ||
4848 | static inline void | |
f7ce4811 PS |
4849 | packet_batch_per_flow_init(struct packet_batch_per_flow *batch, |
4850 | struct dp_netdev_flow *flow) | |
8cbf4f47 | 4851 | { |
11e5cf1f | 4852 | flow->batch = batch; |
8cbf4f47 | 4853 | |
11e5cf1f | 4854 | batch->flow = flow; |
1895cc8d | 4855 | dp_packet_batch_init(&batch->array); |
8cbf4f47 DDP |
4856 | batch->byte_count = 0; |
4857 | batch->tcp_flags = 0; | |
8cbf4f47 DDP |
4858 | } |
4859 | ||
4860 | static inline void | |
f7ce4811 PS |
4861 | packet_batch_per_flow_execute(struct packet_batch_per_flow *batch, |
4862 | struct dp_netdev_pmd_thread *pmd, | |
4863 | long long now) | |
8cbf4f47 DDP |
4864 | { |
4865 | struct dp_netdev_actions *actions; | |
4866 | struct dp_netdev_flow *flow = batch->flow; | |
4867 | ||
1895cc8d | 4868 | dp_netdev_flow_used(flow, batch->array.count, batch->byte_count, |
11bfdadd | 4869 | batch->tcp_flags, now); |
8cbf4f47 DDP |
4870 | |
4871 | actions = dp_netdev_flow_get_actions(flow); | |
4872 | ||
66e4ad8a | 4873 | dp_netdev_execute_actions(pmd, &batch->array, true, &flow->flow, |
324c8374 | 4874 | actions->actions, actions->size, now); |
8cbf4f47 DDP |
4875 | } |
4876 | ||
8aaa125d | 4877 | static inline void |
e14deea0 | 4878 | dp_netdev_queue_batches(struct dp_packet *pkt, |
9bbf1c3d | 4879 | struct dp_netdev_flow *flow, const struct miniflow *mf, |
47a45d86 KT |
4880 | struct packet_batch_per_flow *batches, |
4881 | size_t *n_batches) | |
9bbf1c3d | 4882 | { |
f7ce4811 | 4883 | struct packet_batch_per_flow *batch = flow->batch; |
11e5cf1f | 4884 | |
f9fe365b AZ |
4885 | if (OVS_UNLIKELY(!batch)) { |
4886 | batch = &batches[(*n_batches)++]; | |
f7ce4811 | 4887 | packet_batch_per_flow_init(batch, flow); |
9bbf1c3d DDP |
4888 | } |
4889 | ||
f7ce4811 | 4890 | packet_batch_per_flow_update(batch, pkt, mf); |
9bbf1c3d DDP |
4891 | } |
4892 | ||
9bbf1c3d | 4893 | /* Try to process all ('cnt') the 'packets' using only the exact match cache |
a90ed026 | 4894 | * 'pmd->flow_cache'. If a flow is not found for a packet 'packets[i]', the |
8aaa125d DDP |
4895 | * miniflow is copied into 'keys' and the packet pointer is moved at the |
4896 | * beginning of the 'packets' array. | |
9bbf1c3d DDP |
4897 | * |
4898 | * The function returns the number of packets that needs to be processed in the | |
4899 | * 'packets' array (they have been moved to the beginning of the vector). | |
a90ed026 | 4900 | * |
02305520 FA |
4901 | * For performance reasons a caller may choose not to initialize the metadata |
4902 | * in 'packets_'. If 'md_is_valid' is false, the metadata in 'packets' | |
4903 | * is not valid and must be initialized by this function using 'port_no'. | |
4904 | * If 'md_is_valid' is true, the metadata is already valid and 'port_no' | |
4905 | * will be ignored. | |
9bbf1c3d DDP |
4906 | */ |
4907 | static inline size_t | |
72c84bc2 AZ |
4908 | emc_processing(struct dp_netdev_pmd_thread *pmd, |
4909 | struct dp_packet_batch *packets_, | |
1895cc8d | 4910 | struct netdev_flow_key *keys, |
f7ce4811 | 4911 | struct packet_batch_per_flow batches[], size_t *n_batches, |
a90ed026 | 4912 | bool md_is_valid, odp_port_t port_no) |
72865317 | 4913 | { |
65f13b50 | 4914 | struct emc_cache *flow_cache = &pmd->flow_cache; |
b89c678b | 4915 | struct netdev_flow_key *key = &keys[0]; |
72c84bc2 AZ |
4916 | size_t n_missed = 0, n_dropped = 0; |
4917 | struct dp_packet *packet; | |
45df9fef | 4918 | const size_t cnt = dp_packet_batch_size(packets_); |
f79b1ddb | 4919 | uint32_t cur_min; |
72c84bc2 | 4920 | int i; |
8cbf4f47 | 4921 | |
f79b1ddb BB |
4922 | atomic_read_relaxed(&pmd->dp->emc_insert_min, &cur_min); |
4923 | ||
45df9fef | 4924 | DP_PACKET_BATCH_REFILL_FOR_EACH (i, cnt, packet, packets_) { |
9bbf1c3d | 4925 | struct dp_netdev_flow *flow; |
9bbf1c3d | 4926 | |
5a2fed48 AZ |
4927 | if (OVS_UNLIKELY(dp_packet_size(packet) < ETH_HEADER_LEN)) { |
4928 | dp_packet_delete(packet); | |
3d88a620 | 4929 | n_dropped++; |
84d6d5eb EJ |
4930 | continue; |
4931 | } | |
8cbf4f47 | 4932 | |
45df9fef | 4933 | if (i != cnt - 1) { |
72c84bc2 | 4934 | struct dp_packet **packets = packets_->packets; |
a90ed026 | 4935 | /* Prefetch next packet data and metadata. */ |
72a5e2b8 | 4936 | OVS_PREFETCH(dp_packet_data(packets[i+1])); |
a90ed026 | 4937 | pkt_metadata_prefetch_init(&packets[i+1]->md); |
72a5e2b8 DDP |
4938 | } |
4939 | ||
a90ed026 DDP |
4940 | if (!md_is_valid) { |
4941 | pkt_metadata_init(&packet->md, port_no); | |
4942 | } | |
5a2fed48 | 4943 | miniflow_extract(packet, &key->mf); |
d262ac2c | 4944 | key->len = 0; /* Not computed yet. */ |
bde94613 FA |
4945 | /* If EMC is disabled skip hash computation and emc_lookup */ |
4946 | if (cur_min) { | |
4947 | if (!md_is_valid) { | |
4948 | key->hash = dpif_netdev_packet_get_rss_hash_orig_pkt(packet, | |
4949 | &key->mf); | |
4950 | } else { | |
4951 | key->hash = dpif_netdev_packet_get_rss_hash(packet, &key->mf); | |
4952 | } | |
4953 | flow = emc_lookup(flow_cache, key); | |
4954 | } else { | |
4955 | flow = NULL; | |
4956 | } | |
8aaa125d | 4957 | if (OVS_LIKELY(flow)) { |
5a2fed48 | 4958 | dp_netdev_queue_batches(packet, flow, &key->mf, batches, |
8aaa125d DDP |
4959 | n_batches); |
4960 | } else { | |
d1aa0b94 | 4961 | /* Exact match cache missed. Group missed packets together at |
72c84bc2 AZ |
4962 | * the beginning of the 'packets' array. */ |
4963 | dp_packet_batch_refill(packets_, packet, i); | |
400486f7 DDP |
4964 | /* 'key[n_missed]' contains the key of the current packet and it |
4965 | * must be returned to the caller. The next key should be extracted | |
4966 | * to 'keys[n_missed + 1]'. */ | |
4967 | key = &keys[++n_missed]; | |
9bbf1c3d DDP |
4968 | } |
4969 | } | |
4970 | ||
47a45d86 | 4971 | dp_netdev_count_packet(pmd, DP_STAT_EXACT_HIT, |
45df9fef | 4972 | cnt - n_dropped - n_missed); |
4f150744 | 4973 | |
72c84bc2 | 4974 | return dp_packet_batch_size(packets_); |
9bbf1c3d DDP |
4975 | } |
4976 | ||
a260d966 | 4977 | static inline void |
47a45d86 KT |
4978 | handle_packet_upcall(struct dp_netdev_pmd_thread *pmd, |
4979 | struct dp_packet *packet, | |
a260d966 PS |
4980 | const struct netdev_flow_key *key, |
4981 | struct ofpbuf *actions, struct ofpbuf *put_actions, | |
324c8374 | 4982 | int *lost_cnt, long long now) |
a260d966 PS |
4983 | { |
4984 | struct ofpbuf *add_actions; | |
4985 | struct dp_packet_batch b; | |
4986 | struct match match; | |
4987 | ovs_u128 ufid; | |
4988 | int error; | |
4989 | ||
4990 | match.tun_md.valid = false; | |
4991 | miniflow_expand(&key->mf, &match.flow); | |
4992 | ||
4993 | ofpbuf_clear(actions); | |
4994 | ofpbuf_clear(put_actions); | |
4995 | ||
4996 | dpif_flow_hash(pmd->dp->dpif, &match.flow, sizeof match.flow, &ufid); | |
4997 | error = dp_netdev_upcall(pmd, packet, &match.flow, &match.wc, | |
4998 | &ufid, DPIF_UC_MISS, NULL, actions, | |
4999 | put_actions); | |
5000 | if (OVS_UNLIKELY(error && error != ENOSPC)) { | |
5001 | dp_packet_delete(packet); | |
5002 | (*lost_cnt)++; | |
5003 | return; | |
5004 | } | |
5005 | ||
5006 | /* The Netlink encoding of datapath flow keys cannot express | |
5007 | * wildcarding the presence of a VLAN tag. Instead, a missing VLAN | |
5008 | * tag is interpreted as exact match on the fact that there is no | |
5009 | * VLAN. Unless we refactor a lot of code that translates between | |
5010 | * Netlink and struct flow representations, we have to do the same | |
5011 | * here. */ | |
f0fb825a EG |
5012 | if (!match.wc.masks.vlans[0].tci) { |
5013 | match.wc.masks.vlans[0].tci = htons(0xffff); | |
a260d966 PS |
5014 | } |
5015 | ||
5016 | /* We can't allow the packet batching in the next loop to execute | |
5017 | * the actions. Otherwise, if there are any slow path actions, | |
5018 | * we'll send the packet up twice. */ | |
72c84bc2 | 5019 | dp_packet_batch_init_packet(&b, packet); |
66e4ad8a | 5020 | dp_netdev_execute_actions(pmd, &b, true, &match.flow, |
324c8374 | 5021 | actions->data, actions->size, now); |
a260d966 PS |
5022 | |
5023 | add_actions = put_actions->size ? put_actions : actions; | |
5024 | if (OVS_LIKELY(error != ENOSPC)) { | |
5025 | struct dp_netdev_flow *netdev_flow; | |
5026 | ||
5027 | /* XXX: There's a race window where a flow covering this packet | |
5028 | * could have already been installed since we last did the flow | |
5029 | * lookup before upcall. This could be solved by moving the | |
5030 | * mutex lock outside the loop, but that's an awful long time | |
5031 | * to be locking everyone out of making flow installs. If we | |
5032 | * move to a per-core classifier, it would be reasonable. */ | |
5033 | ovs_mutex_lock(&pmd->flow_mutex); | |
3453b4d6 | 5034 | netdev_flow = dp_netdev_pmd_lookup_flow(pmd, key, NULL); |
a260d966 PS |
5035 | if (OVS_LIKELY(!netdev_flow)) { |
5036 | netdev_flow = dp_netdev_flow_add(pmd, &match, &ufid, | |
5037 | add_actions->data, | |
5038 | add_actions->size); | |
5039 | } | |
5040 | ovs_mutex_unlock(&pmd->flow_mutex); | |
4c30b246 | 5041 | emc_probabilistic_insert(pmd, key, netdev_flow); |
a260d966 PS |
5042 | } |
5043 | } | |
5044 | ||
9bbf1c3d | 5045 | static inline void |
65f13b50 | 5046 | fast_path_processing(struct dp_netdev_pmd_thread *pmd, |
1895cc8d | 5047 | struct dp_packet_batch *packets_, |
8aaa125d | 5048 | struct netdev_flow_key *keys, |
324c8374 | 5049 | struct packet_batch_per_flow batches[], size_t *n_batches, |
3453b4d6 | 5050 | odp_port_t in_port, |
324c8374 | 5051 | long long now) |
9bbf1c3d | 5052 | { |
31c82130 | 5053 | const size_t cnt = dp_packet_batch_size(packets_); |
1a0d5831 | 5054 | #if !defined(__CHECKER__) && !defined(_WIN32) |
9bbf1c3d DDP |
5055 | const size_t PKT_ARRAY_SIZE = cnt; |
5056 | #else | |
1a0d5831 | 5057 | /* Sparse or MSVC doesn't like variable length array. */ |
cd159f1a | 5058 | enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST }; |
9bbf1c3d | 5059 | #endif |
31c82130 | 5060 | struct dp_packet *packet; |
3453b4d6 | 5061 | struct dpcls *cls; |
0de8783a | 5062 | struct dpcls_rule *rules[PKT_ARRAY_SIZE]; |
65f13b50 | 5063 | struct dp_netdev *dp = pmd->dp; |
8aaa125d | 5064 | int miss_cnt = 0, lost_cnt = 0; |
3453b4d6 | 5065 | int lookup_cnt = 0, add_lookup_cnt; |
9bbf1c3d | 5066 | bool any_miss; |
8aaa125d | 5067 | size_t i; |
9bbf1c3d DDP |
5068 | |
5069 | for (i = 0; i < cnt; i++) { | |
0de8783a | 5070 | /* Key length is needed in all the cases, hash computed on demand. */ |
361d808d | 5071 | keys[i].len = netdev_flow_key_size(miniflow_n_values(&keys[i].mf)); |
9bbf1c3d | 5072 | } |
3453b4d6 JS |
5073 | /* Get the classifier for the in_port */ |
5074 | cls = dp_netdev_pmd_lookup_dpcls(pmd, in_port); | |
5075 | if (OVS_LIKELY(cls)) { | |
5076 | any_miss = !dpcls_lookup(cls, keys, rules, cnt, &lookup_cnt); | |
5077 | } else { | |
5078 | any_miss = true; | |
5079 | memset(rules, 0, sizeof(rules)); | |
5080 | } | |
623540e4 EJ |
5081 | if (OVS_UNLIKELY(any_miss) && !fat_rwlock_tryrdlock(&dp->upcall_rwlock)) { |
5082 | uint64_t actions_stub[512 / 8], slow_stub[512 / 8]; | |
5083 | struct ofpbuf actions, put_actions; | |
623540e4 EJ |
5084 | |
5085 | ofpbuf_use_stub(&actions, actions_stub, sizeof actions_stub); | |
5086 | ofpbuf_use_stub(&put_actions, slow_stub, sizeof slow_stub); | |
5087 | ||
31c82130 | 5088 | DP_PACKET_BATCH_FOR_EACH (packet, packets_) { |
0de8783a | 5089 | struct dp_netdev_flow *netdev_flow; |
623540e4 | 5090 | |
0de8783a | 5091 | if (OVS_LIKELY(rules[i])) { |
623540e4 EJ |
5092 | continue; |
5093 | } | |
5094 | ||
5095 | /* It's possible that an earlier slow path execution installed | |
0de8783a | 5096 | * a rule covering this flow. In this case, it's a lot cheaper |
623540e4 | 5097 | * to catch it here than execute a miss. */ |
3453b4d6 JS |
5098 | netdev_flow = dp_netdev_pmd_lookup_flow(pmd, &keys[i], |
5099 | &add_lookup_cnt); | |
623540e4 | 5100 | if (netdev_flow) { |
3453b4d6 | 5101 | lookup_cnt += add_lookup_cnt; |
0de8783a | 5102 | rules[i] = &netdev_flow->cr; |
623540e4 EJ |
5103 | continue; |
5104 | } | |
5105 | ||
60fc3b7b | 5106 | miss_cnt++; |
31c82130 | 5107 | handle_packet_upcall(pmd, packet, &keys[i], &actions, |
324c8374 | 5108 | &put_actions, &lost_cnt, now); |
623540e4 EJ |
5109 | } |
5110 | ||
5111 | ofpbuf_uninit(&actions); | |
5112 | ofpbuf_uninit(&put_actions); | |
5113 | fat_rwlock_unlock(&dp->upcall_rwlock); | |
ac8c2081 | 5114 | } else if (OVS_UNLIKELY(any_miss)) { |
31c82130 | 5115 | DP_PACKET_BATCH_FOR_EACH (packet, packets_) { |
0de8783a | 5116 | if (OVS_UNLIKELY(!rules[i])) { |
31c82130 | 5117 | dp_packet_delete(packet); |
8aaa125d DDP |
5118 | lost_cnt++; |
5119 | miss_cnt++; | |
ac8c2081 DDP |
5120 | } |
5121 | } | |
623540e4 | 5122 | } |
84d6d5eb | 5123 | |
31c82130 | 5124 | DP_PACKET_BATCH_FOR_EACH (packet, packets_) { |
84d6d5eb | 5125 | struct dp_netdev_flow *flow; |
8cbf4f47 | 5126 | |
0de8783a | 5127 | if (OVS_UNLIKELY(!rules[i])) { |
84d6d5eb EJ |
5128 | continue; |
5129 | } | |
5130 | ||
84d6d5eb | 5131 | flow = dp_netdev_flow_cast(rules[i]); |
0de8783a | 5132 | |
4c30b246 | 5133 | emc_probabilistic_insert(pmd, &keys[i], flow); |
8aaa125d | 5134 | dp_netdev_queue_batches(packet, flow, &keys[i].mf, batches, n_batches); |
8cbf4f47 DDP |
5135 | } |
5136 | ||
8aaa125d | 5137 | dp_netdev_count_packet(pmd, DP_STAT_MASKED_HIT, cnt - miss_cnt); |
3453b4d6 | 5138 | dp_netdev_count_packet(pmd, DP_STAT_LOOKUP_HIT, lookup_cnt); |
8aaa125d DDP |
5139 | dp_netdev_count_packet(pmd, DP_STAT_MISS, miss_cnt); |
5140 | dp_netdev_count_packet(pmd, DP_STAT_LOST, lost_cnt); | |
72865317 BP |
5141 | } |
5142 | ||
a90ed026 DDP |
5143 | /* Packets enter the datapath from a port (or from recirculation) here. |
5144 | * | |
02305520 FA |
5145 | * When 'md_is_valid' is true the metadata in 'packets' are already valid. |
5146 | * When false the metadata in 'packets' need to be initialized. */ | |
adcf00ba | 5147 | static void |
a90ed026 | 5148 | dp_netdev_input__(struct dp_netdev_pmd_thread *pmd, |
1895cc8d | 5149 | struct dp_packet_batch *packets, |
a90ed026 | 5150 | bool md_is_valid, odp_port_t port_no) |
9bbf1c3d | 5151 | { |
1a0d5831 | 5152 | #if !defined(__CHECKER__) && !defined(_WIN32) |
37eabc70 | 5153 | const size_t PKT_ARRAY_SIZE = dp_packet_batch_size(packets); |
9bbf1c3d | 5154 | #else |
1a0d5831 | 5155 | /* Sparse or MSVC doesn't like variable length array. */ |
cd159f1a | 5156 | enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST }; |
9bbf1c3d | 5157 | #endif |
47a45d86 KT |
5158 | OVS_ALIGNED_VAR(CACHE_LINE_SIZE) |
5159 | struct netdev_flow_key keys[PKT_ARRAY_SIZE]; | |
f7ce4811 | 5160 | struct packet_batch_per_flow batches[PKT_ARRAY_SIZE]; |
11bfdadd | 5161 | long long now = time_msec(); |
72c84bc2 | 5162 | size_t n_batches; |
3453b4d6 | 5163 | odp_port_t in_port; |
9bbf1c3d | 5164 | |
8aaa125d | 5165 | n_batches = 0; |
72c84bc2 | 5166 | emc_processing(pmd, packets, keys, batches, &n_batches, |
a90ed026 | 5167 | md_is_valid, port_no); |
72c84bc2 | 5168 | if (!dp_packet_batch_is_empty(packets)) { |
3453b4d6 JS |
5169 | /* Get ingress port from first packet's metadata. */ |
5170 | in_port = packets->packets[0]->md.in_port.odp_port; | |
47a45d86 KT |
5171 | fast_path_processing(pmd, packets, keys, batches, &n_batches, |
5172 | in_port, now); | |
8aaa125d DDP |
5173 | } |
5174 | ||
ad9f0581 BB |
5175 | /* All the flow batches need to be reset before any call to |
5176 | * packet_batch_per_flow_execute() as it could potentially trigger | |
5177 | * recirculation. When a packet matching flow ‘j’ happens to be | |
5178 | * recirculated, the nested call to dp_netdev_input__() could potentially | |
5179 | * classify the packet as matching another flow - say 'k'. It could happen | |
5180 | * that in the previous call to dp_netdev_input__() that same flow 'k' had | |
5181 | * already its own batches[k] still waiting to be served. So if its | |
5182 | * ‘batch’ member is not reset, the recirculated packet would be wrongly | |
5183 | * appended to batches[k] of the 1st call to dp_netdev_input__(). */ | |
72c84bc2 | 5184 | size_t i; |
603f2ce0 EJ |
5185 | for (i = 0; i < n_batches; i++) { |
5186 | batches[i].flow->batch = NULL; | |
5187 | } | |
5188 | ||
8aaa125d | 5189 | for (i = 0; i < n_batches; i++) { |
f7ce4811 | 5190 | packet_batch_per_flow_execute(&batches[i], pmd, now); |
9bbf1c3d DDP |
5191 | } |
5192 | } | |
5193 | ||
a90ed026 DDP |
5194 | static void |
5195 | dp_netdev_input(struct dp_netdev_pmd_thread *pmd, | |
1895cc8d | 5196 | struct dp_packet_batch *packets, |
a90ed026 DDP |
5197 | odp_port_t port_no) |
5198 | { | |
3453b4d6 | 5199 | dp_netdev_input__(pmd, packets, false, port_no); |
a90ed026 DDP |
5200 | } |
5201 | ||
5202 | static void | |
5203 | dp_netdev_recirculate(struct dp_netdev_pmd_thread *pmd, | |
1895cc8d | 5204 | struct dp_packet_batch *packets) |
a90ed026 | 5205 | { |
3453b4d6 | 5206 | dp_netdev_input__(pmd, packets, true, 0); |
a90ed026 DDP |
5207 | } |
5208 | ||
9080a111 | 5209 | struct dp_netdev_execute_aux { |
65f13b50 | 5210 | struct dp_netdev_pmd_thread *pmd; |
324c8374 | 5211 | long long now; |
66e4ad8a | 5212 | const struct flow *flow; |
9080a111 JR |
5213 | }; |
5214 | ||
e4e74c3a AW |
5215 | static void |
5216 | dpif_netdev_register_dp_purge_cb(struct dpif *dpif, dp_purge_callback *cb, | |
5217 | void *aux) | |
5218 | { | |
5219 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
5220 | dp->dp_purge_aux = aux; | |
5221 | dp->dp_purge_cb = cb; | |
5222 | } | |
5223 | ||
6b31e073 | 5224 | static void |
623540e4 EJ |
5225 | dpif_netdev_register_upcall_cb(struct dpif *dpif, upcall_callback *cb, |
5226 | void *aux) | |
6b31e073 RW |
5227 | { |
5228 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
623540e4 | 5229 | dp->upcall_aux = aux; |
6b31e073 RW |
5230 | dp->upcall_cb = cb; |
5231 | } | |
5232 | ||
324c8374 IM |
5233 | static void |
5234 | dpif_netdev_xps_revalidate_pmd(const struct dp_netdev_pmd_thread *pmd, | |
5235 | long long now, bool purge) | |
5236 | { | |
5237 | struct tx_port *tx; | |
5238 | struct dp_netdev_port *port; | |
5239 | long long interval; | |
5240 | ||
57eebbb4 | 5241 | HMAP_FOR_EACH (tx, node, &pmd->send_port_cache) { |
9f7a3035 | 5242 | if (!tx->port->dynamic_txqs) { |
324c8374 IM |
5243 | continue; |
5244 | } | |
5245 | interval = now - tx->last_used; | |
5246 | if (tx->qid >= 0 && (purge || interval >= XPS_TIMEOUT_MS)) { | |
5247 | port = tx->port; | |
5248 | ovs_mutex_lock(&port->txq_used_mutex); | |
5249 | port->txq_used[tx->qid]--; | |
5250 | ovs_mutex_unlock(&port->txq_used_mutex); | |
5251 | tx->qid = -1; | |
5252 | } | |
5253 | } | |
5254 | } | |
5255 | ||
5256 | static int | |
5257 | dpif_netdev_xps_get_tx_qid(const struct dp_netdev_pmd_thread *pmd, | |
5258 | struct tx_port *tx, long long now) | |
5259 | { | |
5260 | struct dp_netdev_port *port; | |
5261 | long long interval; | |
5262 | int i, min_cnt, min_qid; | |
5263 | ||
5264 | if (OVS_UNLIKELY(!now)) { | |
5265 | now = time_msec(); | |
5266 | } | |
5267 | ||
5268 | interval = now - tx->last_used; | |
5269 | tx->last_used = now; | |
5270 | ||
5271 | if (OVS_LIKELY(tx->qid >= 0 && interval < XPS_TIMEOUT_MS)) { | |
5272 | return tx->qid; | |
5273 | } | |
5274 | ||
5275 | port = tx->port; | |
5276 | ||
5277 | ovs_mutex_lock(&port->txq_used_mutex); | |
5278 | if (tx->qid >= 0) { | |
5279 | port->txq_used[tx->qid]--; | |
5280 | tx->qid = -1; | |
5281 | } | |
5282 | ||
5283 | min_cnt = -1; | |
5284 | min_qid = 0; | |
5285 | for (i = 0; i < netdev_n_txq(port->netdev); i++) { | |
5286 | if (port->txq_used[i] < min_cnt || min_cnt == -1) { | |
5287 | min_cnt = port->txq_used[i]; | |
5288 | min_qid = i; | |
5289 | } | |
5290 | } | |
5291 | ||
5292 | port->txq_used[min_qid]++; | |
5293 | tx->qid = min_qid; | |
5294 | ||
5295 | ovs_mutex_unlock(&port->txq_used_mutex); | |
5296 | ||
5297 | dpif_netdev_xps_revalidate_pmd(pmd, now, false); | |
5298 | ||
5299 | VLOG_DBG("Core %d: New TX queue ID %d for port \'%s\'.", | |
5300 | pmd->core_id, tx->qid, netdev_get_name(tx->port->netdev)); | |
5301 | return min_qid; | |
5302 | } | |
5303 | ||
d0cca6c3 | 5304 | static struct tx_port * |
57eebbb4 DDP |
5305 | pmd_tnl_port_cache_lookup(const struct dp_netdev_pmd_thread *pmd, |
5306 | odp_port_t port_no) | |
5307 | { | |
5308 | return tx_port_lookup(&pmd->tnl_port_cache, port_no); | |
5309 | } | |
5310 | ||
5311 | static struct tx_port * | |
5312 | pmd_send_port_cache_lookup(const struct dp_netdev_pmd_thread *pmd, | |
5313 | odp_port_t port_no) | |
d0cca6c3 | 5314 | { |
57eebbb4 | 5315 | return tx_port_lookup(&pmd->send_port_cache, port_no); |
d0cca6c3 DDP |
5316 | } |
5317 | ||
a36de779 | 5318 | static int |
d0cca6c3 | 5319 | push_tnl_action(const struct dp_netdev_pmd_thread *pmd, |
1895cc8d PS |
5320 | const struct nlattr *attr, |
5321 | struct dp_packet_batch *batch) | |
a36de779 | 5322 | { |
d0cca6c3 | 5323 | struct tx_port *tun_port; |
a36de779 | 5324 | const struct ovs_action_push_tnl *data; |
4c742796 | 5325 | int err; |
a36de779 PS |
5326 | |
5327 | data = nl_attr_get(attr); | |
5328 | ||
81765c00 | 5329 | tun_port = pmd_tnl_port_cache_lookup(pmd, data->tnl_port); |
a36de779 | 5330 | if (!tun_port) { |
4c742796 PS |
5331 | err = -EINVAL; |
5332 | goto error; | |
a36de779 | 5333 | } |
324c8374 | 5334 | err = netdev_push_header(tun_port->port->netdev, batch, data); |
4c742796 PS |
5335 | if (!err) { |
5336 | return 0; | |
5337 | } | |
5338 | error: | |
5339 | dp_packet_delete_batch(batch, true); | |
5340 | return err; | |
a36de779 PS |
5341 | } |
5342 | ||
66525ef3 PS |
5343 | static void |
5344 | dp_execute_userspace_action(struct dp_netdev_pmd_thread *pmd, | |
5345 | struct dp_packet *packet, bool may_steal, | |
5346 | struct flow *flow, ovs_u128 *ufid, | |
5347 | struct ofpbuf *actions, | |
324c8374 | 5348 | const struct nlattr *userdata, long long now) |
66525ef3 PS |
5349 | { |
5350 | struct dp_packet_batch b; | |
5351 | int error; | |
5352 | ||
5353 | ofpbuf_clear(actions); | |
5354 | ||
5355 | error = dp_netdev_upcall(pmd, packet, flow, NULL, ufid, | |
5356 | DPIF_UC_ACTION, userdata, actions, | |
5357 | NULL); | |
5358 | if (!error || error == ENOSPC) { | |
72c84bc2 | 5359 | dp_packet_batch_init_packet(&b, packet); |
66e4ad8a | 5360 | dp_netdev_execute_actions(pmd, &b, may_steal, flow, |
324c8374 | 5361 | actions->data, actions->size, now); |
66525ef3 PS |
5362 | } else if (may_steal) { |
5363 | dp_packet_delete(packet); | |
5364 | } | |
5365 | } | |
5366 | ||
a36de779 | 5367 | static void |
1895cc8d | 5368 | dp_execute_cb(void *aux_, struct dp_packet_batch *packets_, |
09f9da0b | 5369 | const struct nlattr *a, bool may_steal) |
4b27db64 | 5370 | OVS_NO_THREAD_SAFETY_ANALYSIS |
9080a111 JR |
5371 | { |
5372 | struct dp_netdev_execute_aux *aux = aux_; | |
623540e4 | 5373 | uint32_t *depth = recirc_depth_get(); |
28e2fa02 DDP |
5374 | struct dp_netdev_pmd_thread *pmd = aux->pmd; |
5375 | struct dp_netdev *dp = pmd->dp; | |
09f9da0b | 5376 | int type = nl_attr_type(a); |
324c8374 | 5377 | long long now = aux->now; |
d0cca6c3 | 5378 | struct tx_port *p; |
9080a111 | 5379 | |
09f9da0b JR |
5380 | switch ((enum ovs_action_attr)type) { |
5381 | case OVS_ACTION_ATTR_OUTPUT: | |
57eebbb4 | 5382 | p = pmd_send_port_cache_lookup(pmd, nl_attr_get_odp_port(a)); |
26a5075b | 5383 | if (OVS_LIKELY(p)) { |
347ba9bb | 5384 | int tx_qid; |
324c8374 | 5385 | bool dynamic_txqs; |
347ba9bb | 5386 | |
324c8374 IM |
5387 | dynamic_txqs = p->port->dynamic_txqs; |
5388 | if (dynamic_txqs) { | |
5389 | tx_qid = dpif_netdev_xps_get_tx_qid(pmd, p, now); | |
5390 | } else { | |
82d765f6 | 5391 | tx_qid = pmd->static_tx_qid; |
324c8374 | 5392 | } |
347ba9bb | 5393 | |
324c8374 IM |
5394 | netdev_send(p->port->netdev, tx_qid, packets_, may_steal, |
5395 | dynamic_txqs); | |
ac8c2081 | 5396 | return; |
8a4e3a85 | 5397 | } |
09f9da0b JR |
5398 | break; |
5399 | ||
a36de779 PS |
5400 | case OVS_ACTION_ATTR_TUNNEL_PUSH: |
5401 | if (*depth < MAX_RECIRC_DEPTH) { | |
aaca4fe0 | 5402 | dp_packet_batch_apply_cutlen(packets_); |
7c12dfc5 | 5403 | push_tnl_action(pmd, a, packets_); |
a36de779 PS |
5404 | return; |
5405 | } | |
5406 | break; | |
5407 | ||
5408 | case OVS_ACTION_ATTR_TUNNEL_POP: | |
5409 | if (*depth < MAX_RECIRC_DEPTH) { | |
aaca4fe0 | 5410 | struct dp_packet_batch *orig_packets_ = packets_; |
8611f9a4 | 5411 | odp_port_t portno = nl_attr_get_odp_port(a); |
a36de779 | 5412 | |
57eebbb4 | 5413 | p = pmd_tnl_port_cache_lookup(pmd, portno); |
a36de779 | 5414 | if (p) { |
1895cc8d | 5415 | struct dp_packet_batch tnl_pkt; |
a36de779 PS |
5416 | |
5417 | if (!may_steal) { | |
aaca4fe0 WT |
5418 | dp_packet_batch_clone(&tnl_pkt, packets_); |
5419 | packets_ = &tnl_pkt; | |
5420 | dp_packet_batch_reset_cutlen(orig_packets_); | |
a36de779 PS |
5421 | } |
5422 | ||
aaca4fe0 WT |
5423 | dp_packet_batch_apply_cutlen(packets_); |
5424 | ||
324c8374 | 5425 | netdev_pop_header(p->port->netdev, packets_); |
72c84bc2 | 5426 | if (dp_packet_batch_is_empty(packets_)) { |
1c8f98d9 PS |
5427 | return; |
5428 | } | |
9235b479 | 5429 | |
72c84bc2 AZ |
5430 | struct dp_packet *packet; |
5431 | DP_PACKET_BATCH_FOR_EACH (packet, packets_) { | |
5432 | packet->md.in_port.odp_port = portno; | |
a36de779 | 5433 | } |
9235b479 PS |
5434 | |
5435 | (*depth)++; | |
5436 | dp_netdev_recirculate(pmd, packets_); | |
5437 | (*depth)--; | |
a36de779 PS |
5438 | return; |
5439 | } | |
5440 | } | |
5441 | break; | |
5442 | ||
623540e4 EJ |
5443 | case OVS_ACTION_ATTR_USERSPACE: |
5444 | if (!fat_rwlock_tryrdlock(&dp->upcall_rwlock)) { | |
aaca4fe0 | 5445 | struct dp_packet_batch *orig_packets_ = packets_; |
623540e4 | 5446 | const struct nlattr *userdata; |
aaca4fe0 | 5447 | struct dp_packet_batch usr_pkt; |
623540e4 EJ |
5448 | struct ofpbuf actions; |
5449 | struct flow flow; | |
7af12bd7 | 5450 | ovs_u128 ufid; |
aaca4fe0 | 5451 | bool clone = false; |
4fc65926 | 5452 | |
623540e4 EJ |
5453 | userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA); |
5454 | ofpbuf_init(&actions, 0); | |
8cbf4f47 | 5455 | |
aaca4fe0 WT |
5456 | if (packets_->trunc) { |
5457 | if (!may_steal) { | |
5458 | dp_packet_batch_clone(&usr_pkt, packets_); | |
5459 | packets_ = &usr_pkt; | |
aaca4fe0 WT |
5460 | clone = true; |
5461 | dp_packet_batch_reset_cutlen(orig_packets_); | |
5462 | } | |
5463 | ||
5464 | dp_packet_batch_apply_cutlen(packets_); | |
5465 | } | |
5466 | ||
72c84bc2 AZ |
5467 | struct dp_packet *packet; |
5468 | DP_PACKET_BATCH_FOR_EACH (packet, packets_) { | |
5469 | flow_extract(packet, &flow); | |
7af12bd7 | 5470 | dpif_flow_hash(dp->dpif, &flow, sizeof flow, &ufid); |
72c84bc2 | 5471 | dp_execute_userspace_action(pmd, packet, may_steal, &flow, |
324c8374 | 5472 | &ufid, &actions, userdata, now); |
db73f716 | 5473 | } |
aaca4fe0 WT |
5474 | |
5475 | if (clone) { | |
5476 | dp_packet_delete_batch(packets_, true); | |
5477 | } | |
5478 | ||
623540e4 EJ |
5479 | ofpbuf_uninit(&actions); |
5480 | fat_rwlock_unlock(&dp->upcall_rwlock); | |
6b31e073 | 5481 | |
ac8c2081 DDP |
5482 | return; |
5483 | } | |
09f9da0b | 5484 | break; |
572f732a | 5485 | |
adcf00ba AZ |
5486 | case OVS_ACTION_ATTR_RECIRC: |
5487 | if (*depth < MAX_RECIRC_DEPTH) { | |
1895cc8d | 5488 | struct dp_packet_batch recirc_pkts; |
572f732a | 5489 | |
28e2fa02 | 5490 | if (!may_steal) { |
1895cc8d PS |
5491 | dp_packet_batch_clone(&recirc_pkts, packets_); |
5492 | packets_ = &recirc_pkts; | |
28e2fa02 | 5493 | } |
8cbf4f47 | 5494 | |
72c84bc2 AZ |
5495 | struct dp_packet *packet; |
5496 | DP_PACKET_BATCH_FOR_EACH (packet, packets_) { | |
5497 | packet->md.recirc_id = nl_attr_get_u32(a); | |
8cbf4f47 | 5498 | } |
28e2fa02 DDP |
5499 | |
5500 | (*depth)++; | |
1895cc8d | 5501 | dp_netdev_recirculate(pmd, packets_); |
adcf00ba AZ |
5502 | (*depth)--; |
5503 | ||
ac8c2081 | 5504 | return; |
adcf00ba | 5505 | } |
ac8c2081 DDP |
5506 | |
5507 | VLOG_WARN("Packet dropped. Max recirculation depth exceeded."); | |
572f732a | 5508 | break; |
572f732a | 5509 | |
5cf3edb3 DDP |
5510 | case OVS_ACTION_ATTR_CT: { |
5511 | const struct nlattr *b; | |
a76a37ef | 5512 | bool force = false; |
5cf3edb3 DDP |
5513 | bool commit = false; |
5514 | unsigned int left; | |
5515 | uint16_t zone = 0; | |
5516 | const char *helper = NULL; | |
5517 | const uint32_t *setmark = NULL; | |
5518 | const struct ovs_key_ct_labels *setlabel = NULL; | |
4cddb1f0 DB |
5519 | struct nat_action_info_t nat_action_info; |
5520 | struct nat_action_info_t *nat_action_info_ref = NULL; | |
5521 | bool nat_config = false; | |
5cf3edb3 DDP |
5522 | |
5523 | NL_ATTR_FOR_EACH_UNSAFE (b, left, nl_attr_get(a), | |
5524 | nl_attr_get_size(a)) { | |
5525 | enum ovs_ct_attr sub_type = nl_attr_type(b); | |
5526 | ||
5527 | switch(sub_type) { | |
b80e259f | 5528 | case OVS_CT_ATTR_FORCE_COMMIT: |
a76a37ef JR |
5529 | force = true; |
5530 | /* fall through. */ | |
5cf3edb3 DDP |
5531 | case OVS_CT_ATTR_COMMIT: |
5532 | commit = true; | |
5533 | break; | |
5534 | case OVS_CT_ATTR_ZONE: | |
5535 | zone = nl_attr_get_u16(b); | |
5536 | break; | |
5537 | case OVS_CT_ATTR_HELPER: | |
5538 | helper = nl_attr_get_string(b); | |
5539 | break; | |
5540 | case OVS_CT_ATTR_MARK: | |
5541 | setmark = nl_attr_get(b); | |
5542 | break; | |
5543 | case OVS_CT_ATTR_LABELS: | |
5544 | setlabel = nl_attr_get(b); | |
5545 | break; | |
8e83854c JR |
5546 | case OVS_CT_ATTR_EVENTMASK: |
5547 | /* Silently ignored, as userspace datapath does not generate | |
5548 | * netlink events. */ | |
5549 | break; | |
4cddb1f0 DB |
5550 | case OVS_CT_ATTR_NAT: { |
5551 | const struct nlattr *b_nest; | |
5552 | unsigned int left_nest; | |
5553 | bool ip_min_specified = false; | |
5554 | bool proto_num_min_specified = false; | |
5555 | bool ip_max_specified = false; | |
5556 | bool proto_num_max_specified = false; | |
5557 | memset(&nat_action_info, 0, sizeof nat_action_info); | |
5558 | nat_action_info_ref = &nat_action_info; | |
5559 | ||
5560 | NL_NESTED_FOR_EACH_UNSAFE (b_nest, left_nest, b) { | |
5561 | enum ovs_nat_attr sub_type_nest = nl_attr_type(b_nest); | |
5562 | ||
5563 | switch (sub_type_nest) { | |
5564 | case OVS_NAT_ATTR_SRC: | |
5565 | case OVS_NAT_ATTR_DST: | |
5566 | nat_config = true; | |
5567 | nat_action_info.nat_action |= | |
5568 | ((sub_type_nest == OVS_NAT_ATTR_SRC) | |
5569 | ? NAT_ACTION_SRC : NAT_ACTION_DST); | |
5570 | break; | |
5571 | case OVS_NAT_ATTR_IP_MIN: | |
5572 | memcpy(&nat_action_info.min_addr, | |
5573 | nl_attr_get(b_nest), | |
5574 | nl_attr_get_size(b_nest)); | |
5575 | ip_min_specified = true; | |
5576 | break; | |
5577 | case OVS_NAT_ATTR_IP_MAX: | |
5578 | memcpy(&nat_action_info.max_addr, | |
5579 | nl_attr_get(b_nest), | |
5580 | nl_attr_get_size(b_nest)); | |
5581 | ip_max_specified = true; | |
5582 | break; | |
5583 | case OVS_NAT_ATTR_PROTO_MIN: | |
5584 | nat_action_info.min_port = | |
5585 | nl_attr_get_u16(b_nest); | |
5586 | proto_num_min_specified = true; | |
5587 | break; | |
5588 | case OVS_NAT_ATTR_PROTO_MAX: | |
5589 | nat_action_info.max_port = | |
5590 | nl_attr_get_u16(b_nest); | |
5591 | proto_num_max_specified = true; | |
5592 | break; | |
5593 | case OVS_NAT_ATTR_PERSISTENT: | |
5594 | case OVS_NAT_ATTR_PROTO_HASH: | |
5595 | case OVS_NAT_ATTR_PROTO_RANDOM: | |
5596 | break; | |
5597 | case OVS_NAT_ATTR_UNSPEC: | |
5598 | case __OVS_NAT_ATTR_MAX: | |
5599 | OVS_NOT_REACHED(); | |
5600 | } | |
5601 | } | |
5602 | ||
5603 | if (ip_min_specified && !ip_max_specified) { | |
5604 | nat_action_info.max_addr = nat_action_info.min_addr; | |
5605 | } | |
5606 | if (proto_num_min_specified && !proto_num_max_specified) { | |
5607 | nat_action_info.max_port = nat_action_info.min_port; | |
5608 | } | |
5609 | if (proto_num_min_specified || proto_num_max_specified) { | |
5610 | if (nat_action_info.nat_action & NAT_ACTION_SRC) { | |
5611 | nat_action_info.nat_action |= NAT_ACTION_SRC_PORT; | |
5612 | } else if (nat_action_info.nat_action & NAT_ACTION_DST) { | |
5613 | nat_action_info.nat_action |= NAT_ACTION_DST_PORT; | |
5614 | } | |
5615 | } | |
5616 | break; | |
5617 | } | |
5cf3edb3 DDP |
5618 | case OVS_CT_ATTR_UNSPEC: |
5619 | case __OVS_CT_ATTR_MAX: | |
5620 | OVS_NOT_REACHED(); | |
5621 | } | |
5622 | } | |
5623 | ||
4cddb1f0 DB |
5624 | /* We won't be able to function properly in this case, hence |
5625 | * complain loudly. */ | |
5626 | if (nat_config && !commit) { | |
5627 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); | |
5628 | VLOG_WARN_RL(&rl, "NAT specified without commit."); | |
5629 | } | |
5630 | ||
a76a37ef | 5631 | conntrack_execute(&dp->conntrack, packets_, aux->flow->dl_type, force, |
4cddb1f0 | 5632 | commit, zone, setmark, setlabel, helper, |
94053e66 | 5633 | nat_action_info_ref, now); |
07659514 | 5634 | break; |
5cf3edb3 | 5635 | } |
07659514 | 5636 | |
5dddf960 | 5637 | case OVS_ACTION_ATTR_METER: |
4b27db64 JR |
5638 | dp_netdev_run_meter(pmd->dp, packets_, nl_attr_get_u32(a), |
5639 | time_msec()); | |
5640 | break; | |
5641 | ||
09f9da0b JR |
5642 | case OVS_ACTION_ATTR_PUSH_VLAN: |
5643 | case OVS_ACTION_ATTR_POP_VLAN: | |
5644 | case OVS_ACTION_ATTR_PUSH_MPLS: | |
5645 | case OVS_ACTION_ATTR_POP_MPLS: | |
5646 | case OVS_ACTION_ATTR_SET: | |
6d670e7f | 5647 | case OVS_ACTION_ATTR_SET_MASKED: |
09f9da0b | 5648 | case OVS_ACTION_ATTR_SAMPLE: |
53e1d6f1 | 5649 | case OVS_ACTION_ATTR_HASH: |
09f9da0b | 5650 | case OVS_ACTION_ATTR_UNSPEC: |
aaca4fe0 | 5651 | case OVS_ACTION_ATTR_TRUNC: |
6fcecb85 YY |
5652 | case OVS_ACTION_ATTR_PUSH_ETH: |
5653 | case OVS_ACTION_ATTR_POP_ETH: | |
535e3acf | 5654 | case OVS_ACTION_ATTR_CLONE: |
1fc11c59 JS |
5655 | case OVS_ACTION_ATTR_ENCAP_NSH: |
5656 | case OVS_ACTION_ATTR_DECAP_NSH: | |
09f9da0b JR |
5657 | case __OVS_ACTION_ATTR_MAX: |
5658 | OVS_NOT_REACHED(); | |
da546e07 | 5659 | } |
ac8c2081 | 5660 | |
1895cc8d | 5661 | dp_packet_delete_batch(packets_, may_steal); |
98403001 BP |
5662 | } |
5663 | ||
4edb9ae9 | 5664 | static void |
65f13b50 | 5665 | dp_netdev_execute_actions(struct dp_netdev_pmd_thread *pmd, |
1895cc8d | 5666 | struct dp_packet_batch *packets, |
66e4ad8a | 5667 | bool may_steal, const struct flow *flow, |
324c8374 IM |
5668 | const struct nlattr *actions, size_t actions_len, |
5669 | long long now) | |
72865317 | 5670 | { |
66e4ad8a | 5671 | struct dp_netdev_execute_aux aux = { pmd, now, flow }; |
9080a111 | 5672 | |
1895cc8d | 5673 | odp_execute_actions(&aux, packets, may_steal, actions, |
8cbf4f47 | 5674 | actions_len, dp_execute_cb); |
72865317 BP |
5675 | } |
5676 | ||
4d4e68ed DDP |
5677 | struct dp_netdev_ct_dump { |
5678 | struct ct_dpif_dump_state up; | |
5679 | struct conntrack_dump dump; | |
5680 | struct conntrack *ct; | |
5681 | struct dp_netdev *dp; | |
5682 | }; | |
5683 | ||
5684 | static int | |
5685 | dpif_netdev_ct_dump_start(struct dpif *dpif, struct ct_dpif_dump_state **dump_, | |
ded30c74 | 5686 | const uint16_t *pzone, int *ptot_bkts) |
4d4e68ed DDP |
5687 | { |
5688 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
5689 | struct dp_netdev_ct_dump *dump; | |
5690 | ||
5691 | dump = xzalloc(sizeof *dump); | |
5692 | dump->dp = dp; | |
5693 | dump->ct = &dp->conntrack; | |
5694 | ||
ded30c74 | 5695 | conntrack_dump_start(&dp->conntrack, &dump->dump, pzone, ptot_bkts); |
4d4e68ed DDP |
5696 | |
5697 | *dump_ = &dump->up; | |
5698 | ||
5699 | return 0; | |
5700 | } | |
5701 | ||
5702 | static int | |
5703 | dpif_netdev_ct_dump_next(struct dpif *dpif OVS_UNUSED, | |
5704 | struct ct_dpif_dump_state *dump_, | |
5705 | struct ct_dpif_entry *entry) | |
5706 | { | |
5707 | struct dp_netdev_ct_dump *dump; | |
5708 | ||
5709 | INIT_CONTAINER(dump, dump_, up); | |
5710 | ||
5711 | return conntrack_dump_next(&dump->dump, entry); | |
5712 | } | |
5713 | ||
5714 | static int | |
5715 | dpif_netdev_ct_dump_done(struct dpif *dpif OVS_UNUSED, | |
5716 | struct ct_dpif_dump_state *dump_) | |
5717 | { | |
5718 | struct dp_netdev_ct_dump *dump; | |
5719 | int err; | |
5720 | ||
5721 | INIT_CONTAINER(dump, dump_, up); | |
5722 | ||
5723 | err = conntrack_dump_done(&dump->dump); | |
5724 | ||
5725 | free(dump); | |
5726 | ||
5727 | return err; | |
5728 | } | |
5729 | ||
5d9cbb4c | 5730 | static int |
817a7657 YHW |
5731 | dpif_netdev_ct_flush(struct dpif *dpif, const uint16_t *zone, |
5732 | const struct ct_dpif_tuple *tuple) | |
5d9cbb4c DDP |
5733 | { |
5734 | struct dp_netdev *dp = get_dp_netdev(dpif); | |
5735 | ||
817a7657 YHW |
5736 | if (tuple) { |
5737 | return EOPNOTSUPP; | |
5738 | } | |
5d9cbb4c DDP |
5739 | return conntrack_flush(&dp->conntrack, zone); |
5740 | } | |
5741 | ||
72865317 | 5742 | const struct dpif_class dpif_netdev_class = { |
72865317 | 5743 | "netdev", |
6553d06b | 5744 | dpif_netdev_init, |
2197d7ab | 5745 | dpif_netdev_enumerate, |
0aeaabc8 | 5746 | dpif_netdev_port_open_type, |
72865317 BP |
5747 | dpif_netdev_open, |
5748 | dpif_netdev_close, | |
7dab847a | 5749 | dpif_netdev_destroy, |
e4cfed38 PS |
5750 | dpif_netdev_run, |
5751 | dpif_netdev_wait, | |
72865317 | 5752 | dpif_netdev_get_stats, |
72865317 BP |
5753 | dpif_netdev_port_add, |
5754 | dpif_netdev_port_del, | |
3eb67853 | 5755 | dpif_netdev_port_set_config, |
72865317 BP |
5756 | dpif_netdev_port_query_by_number, |
5757 | dpif_netdev_port_query_by_name, | |
98403001 | 5758 | NULL, /* port_get_pid */ |
b0ec0f27 BP |
5759 | dpif_netdev_port_dump_start, |
5760 | dpif_netdev_port_dump_next, | |
5761 | dpif_netdev_port_dump_done, | |
72865317 BP |
5762 | dpif_netdev_port_poll, |
5763 | dpif_netdev_port_poll_wait, | |
72865317 | 5764 | dpif_netdev_flow_flush, |
ac64794a BP |
5765 | dpif_netdev_flow_dump_create, |
5766 | dpif_netdev_flow_dump_destroy, | |
5767 | dpif_netdev_flow_dump_thread_create, | |
5768 | dpif_netdev_flow_dump_thread_destroy, | |
704a1e09 | 5769 | dpif_netdev_flow_dump_next, |
1a0c894a | 5770 | dpif_netdev_operate, |
6b31e073 RW |
5771 | NULL, /* recv_set */ |
5772 | NULL, /* handlers_set */ | |
d4f6865c | 5773 | dpif_netdev_set_config, |
5bf93d67 | 5774 | dpif_netdev_queue_to_priority, |
6b31e073 RW |
5775 | NULL, /* recv */ |
5776 | NULL, /* recv_wait */ | |
5777 | NULL, /* recv_purge */ | |
e4e74c3a | 5778 | dpif_netdev_register_dp_purge_cb, |
6b31e073 RW |
5779 | dpif_netdev_register_upcall_cb, |
5780 | dpif_netdev_enable_upcall, | |
5781 | dpif_netdev_disable_upcall, | |
b5cbbcf6 | 5782 | dpif_netdev_get_datapath_version, |
4d4e68ed DDP |
5783 | dpif_netdev_ct_dump_start, |
5784 | dpif_netdev_ct_dump_next, | |
5785 | dpif_netdev_ct_dump_done, | |
5d9cbb4c | 5786 | dpif_netdev_ct_flush, |
5dddf960 JR |
5787 | dpif_netdev_meter_get_features, |
5788 | dpif_netdev_meter_set, | |
5789 | dpif_netdev_meter_get, | |
5790 | dpif_netdev_meter_del, | |
72865317 | 5791 | }; |
614c4892 | 5792 | |
74cc3969 BP |
5793 | static void |
5794 | dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
5795 | const char *argv[], void *aux OVS_UNUSED) | |
5796 | { | |
e9985d6a | 5797 | struct dp_netdev_port *port; |
74cc3969 | 5798 | struct dp_netdev *dp; |
ff073a71 | 5799 | odp_port_t port_no; |
74cc3969 | 5800 | |
8a4e3a85 | 5801 | ovs_mutex_lock(&dp_netdev_mutex); |
74cc3969 BP |
5802 | dp = shash_find_data(&dp_netdevs, argv[1]); |
5803 | if (!dp || !dpif_netdev_class_is_dummy(dp->class)) { | |
8a4e3a85 | 5804 | ovs_mutex_unlock(&dp_netdev_mutex); |
74cc3969 BP |
5805 | unixctl_command_reply_error(conn, "unknown datapath or not a dummy"); |
5806 | return; | |
5807 | } | |
8a4e3a85 BP |
5808 | ovs_refcount_ref(&dp->ref_cnt); |
5809 | ovs_mutex_unlock(&dp_netdev_mutex); | |
74cc3969 | 5810 | |
59e6d833 | 5811 | ovs_mutex_lock(&dp->port_mutex); |
e9985d6a | 5812 | if (get_port_by_name(dp, argv[2], &port)) { |
74cc3969 | 5813 | unixctl_command_reply_error(conn, "unknown port"); |
8a4e3a85 | 5814 | goto exit; |
74cc3969 BP |
5815 | } |
5816 | ||
ff073a71 BP |
5817 | port_no = u32_to_odp(atoi(argv[3])); |
5818 | if (!port_no || port_no == ODPP_NONE) { | |
74cc3969 | 5819 | unixctl_command_reply_error(conn, "bad port number"); |
8a4e3a85 | 5820 | goto exit; |
74cc3969 | 5821 | } |
ff073a71 | 5822 | if (dp_netdev_lookup_port(dp, port_no)) { |
74cc3969 | 5823 | unixctl_command_reply_error(conn, "port number already in use"); |
8a4e3a85 | 5824 | goto exit; |
74cc3969 | 5825 | } |
59e6d833 | 5826 | |
e9985d6a DDP |
5827 | /* Remove port. */ |
5828 | hmap_remove(&dp->ports, &port->node); | |
e32971b8 | 5829 | reconfigure_datapath(dp); |
59e6d833 | 5830 | |
e9985d6a DDP |
5831 | /* Reinsert with new port number. */ |
5832 | port->port_no = port_no; | |
5833 | hmap_insert(&dp->ports, &port->node, hash_port_no(port_no)); | |
e32971b8 | 5834 | reconfigure_datapath(dp); |
59e6d833 | 5835 | |
d33ed218 | 5836 | seq_change(dp->port_seq); |
74cc3969 | 5837 | unixctl_command_reply(conn, NULL); |
8a4e3a85 BP |
5838 | |
5839 | exit: | |
59e6d833 | 5840 | ovs_mutex_unlock(&dp->port_mutex); |
8a4e3a85 | 5841 | dp_netdev_unref(dp); |
74cc3969 BP |
5842 | } |
5843 | ||
0cbfe35d BP |
5844 | static void |
5845 | dpif_dummy_register__(const char *type) | |
5846 | { | |
5847 | struct dpif_class *class; | |
5848 | ||
5849 | class = xmalloc(sizeof *class); | |
5850 | *class = dpif_netdev_class; | |
5851 | class->type = xstrdup(type); | |
5852 | dp_register_provider(class); | |
5853 | } | |
5854 | ||
8420c7ad BP |
5855 | static void |
5856 | dpif_dummy_override(const char *type) | |
5857 | { | |
65d43fdc YT |
5858 | int error; |
5859 | ||
5860 | /* | |
5861 | * Ignore EAFNOSUPPORT to allow --enable-dummy=system with | |
5862 | * a userland-only build. It's useful for testsuite. | |
5863 | */ | |
5864 | error = dp_unregister_provider(type); | |
5865 | if (error == 0 || error == EAFNOSUPPORT) { | |
8420c7ad BP |
5866 | dpif_dummy_register__(type); |
5867 | } | |
5868 | } | |
5869 | ||
614c4892 | 5870 | void |
8420c7ad | 5871 | dpif_dummy_register(enum dummy_level level) |
614c4892 | 5872 | { |
8420c7ad | 5873 | if (level == DUMMY_OVERRIDE_ALL) { |
0cbfe35d BP |
5874 | struct sset types; |
5875 | const char *type; | |
5876 | ||
5877 | sset_init(&types); | |
5878 | dp_enumerate_types(&types); | |
5879 | SSET_FOR_EACH (type, &types) { | |
8420c7ad | 5880 | dpif_dummy_override(type); |
0cbfe35d BP |
5881 | } |
5882 | sset_destroy(&types); | |
8420c7ad BP |
5883 | } else if (level == DUMMY_OVERRIDE_SYSTEM) { |
5884 | dpif_dummy_override("system"); | |
614c4892 | 5885 | } |
0cbfe35d BP |
5886 | |
5887 | dpif_dummy_register__("dummy"); | |
74cc3969 BP |
5888 | |
5889 | unixctl_command_register("dpif-dummy/change-port-number", | |
74467d5c | 5890 | "dp port new-number", |
74cc3969 | 5891 | 3, 3, dpif_dummy_change_port_number, NULL); |
614c4892 | 5892 | } |
0de8783a JR |
5893 | \f |
5894 | /* Datapath Classifier. */ | |
5895 | ||
5896 | /* A set of rules that all have the same fields wildcarded. */ | |
5897 | struct dpcls_subtable { | |
5898 | /* The fields are only used by writers. */ | |
5899 | struct cmap_node cmap_node OVS_GUARDED; /* Within dpcls 'subtables_map'. */ | |
5900 | ||
5901 | /* These fields are accessed by readers. */ | |
5902 | struct cmap rules; /* Contains "struct dpcls_rule"s. */ | |
3453b4d6 JS |
5903 | uint32_t hit_cnt; /* Number of match hits in subtable in current |
5904 | optimization interval. */ | |
0de8783a JR |
5905 | struct netdev_flow_key mask; /* Wildcards for fields (const). */ |
5906 | /* 'mask' must be the last field, additional space is allocated here. */ | |
5907 | }; | |
5908 | ||
5909 | /* Initializes 'cls' as a classifier that initially contains no classification | |
5910 | * rules. */ | |
5911 | static void | |
5912 | dpcls_init(struct dpcls *cls) | |
5913 | { | |
5914 | cmap_init(&cls->subtables_map); | |
da9cfca6 | 5915 | pvector_init(&cls->subtables); |
0de8783a JR |
5916 | } |
5917 | ||
5918 | static void | |
5919 | dpcls_destroy_subtable(struct dpcls *cls, struct dpcls_subtable *subtable) | |
5920 | { | |
3453b4d6 | 5921 | VLOG_DBG("Destroying subtable %p for in_port %d", subtable, cls->in_port); |
da9cfca6 | 5922 | pvector_remove(&cls->subtables, subtable); |
0de8783a JR |
5923 | cmap_remove(&cls->subtables_map, &subtable->cmap_node, |
5924 | subtable->mask.hash); | |
5925 | cmap_destroy(&subtable->rules); | |
5926 | ovsrcu_postpone(free, subtable); | |
5927 | } | |
5928 | ||
5929 | /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the | |
5930 | * caller's responsibility. | |
5931 | * May only be called after all the readers have been terminated. */ | |
5932 | static void | |
5933 | dpcls_destroy(struct dpcls *cls) | |
5934 | { | |
5935 | if (cls) { | |
5936 | struct dpcls_subtable *subtable; | |
5937 | ||
5938 | CMAP_FOR_EACH (subtable, cmap_node, &cls->subtables_map) { | |
361d808d | 5939 | ovs_assert(cmap_count(&subtable->rules) == 0); |
0de8783a JR |
5940 | dpcls_destroy_subtable(cls, subtable); |
5941 | } | |
5942 | cmap_destroy(&cls->subtables_map); | |
da9cfca6 | 5943 | pvector_destroy(&cls->subtables); |
0de8783a JR |
5944 | } |
5945 | } | |
5946 | ||
5947 | static struct dpcls_subtable * | |
5948 | dpcls_create_subtable(struct dpcls *cls, const struct netdev_flow_key *mask) | |
5949 | { | |
5950 | struct dpcls_subtable *subtable; | |
5951 | ||
5952 | /* Need to add one. */ | |
caeb4906 JR |
5953 | subtable = xmalloc(sizeof *subtable |
5954 | - sizeof subtable->mask.mf + mask->len); | |
0de8783a | 5955 | cmap_init(&subtable->rules); |
3453b4d6 | 5956 | subtable->hit_cnt = 0; |
0de8783a JR |
5957 | netdev_flow_key_clone(&subtable->mask, mask); |
5958 | cmap_insert(&cls->subtables_map, &subtable->cmap_node, mask->hash); | |
3453b4d6 | 5959 | /* Add the new subtable at the end of the pvector (with no hits yet) */ |
da9cfca6 | 5960 | pvector_insert(&cls->subtables, subtable, 0); |
84dbfb2b | 5961 | VLOG_DBG("Creating %"PRIuSIZE". subtable %p for in_port %d", |
3453b4d6 | 5962 | cmap_count(&cls->subtables_map), subtable, cls->in_port); |
da9cfca6 | 5963 | pvector_publish(&cls->subtables); |
0de8783a JR |
5964 | |
5965 | return subtable; | |
5966 | } | |
5967 | ||
5968 | static inline struct dpcls_subtable * | |
5969 | dpcls_find_subtable(struct dpcls *cls, const struct netdev_flow_key *mask) | |
5970 | { | |
5971 | struct dpcls_subtable *subtable; | |
5972 | ||
5973 | CMAP_FOR_EACH_WITH_HASH (subtable, cmap_node, mask->hash, | |
5974 | &cls->subtables_map) { | |
5975 | if (netdev_flow_key_equal(&subtable->mask, mask)) { | |
5976 | return subtable; | |
5977 | } | |
5978 | } | |
5979 | return dpcls_create_subtable(cls, mask); | |
5980 | } | |
5981 | ||
3453b4d6 JS |
5982 | |
5983 | /* Periodically sort the dpcls subtable vectors according to hit counts */ | |
5984 | static void | |
5985 | dpcls_sort_subtable_vector(struct dpcls *cls) | |
5986 | { | |
5987 | struct pvector *pvec = &cls->subtables; | |
5988 | struct dpcls_subtable *subtable; | |
5989 | ||
5990 | PVECTOR_FOR_EACH (subtable, pvec) { | |
5991 | pvector_change_priority(pvec, subtable, subtable->hit_cnt); | |
5992 | subtable->hit_cnt = 0; | |
5993 | } | |
5994 | pvector_publish(pvec); | |
5995 | } | |
5996 | ||
5997 | static inline void | |
4809891b KT |
5998 | dp_netdev_pmd_try_optimize(struct dp_netdev_pmd_thread *pmd, |
5999 | struct polled_queue *poll_list, int poll_cnt) | |
3453b4d6 JS |
6000 | { |
6001 | struct dpcls *cls; | |
6002 | long long int now = time_msec(); | |
6003 | ||
64bf452e | 6004 | if (now > pmd->rxq_next_cycle_store) { |
4809891b KT |
6005 | /* Get the cycles that were used to process each queue and store. */ |
6006 | for (unsigned i = 0; i < poll_cnt; i++) { | |
6007 | uint64_t rxq_cyc_curr = dp_netdev_rxq_get_cycles(poll_list[i].rxq, | |
6008 | RXQ_CYCLES_PROC_CURR); | |
6009 | dp_netdev_rxq_set_intrvl_cycles(poll_list[i].rxq, rxq_cyc_curr); | |
6010 | dp_netdev_rxq_set_cycles(poll_list[i].rxq, RXQ_CYCLES_PROC_CURR, | |
6011 | 0); | |
6012 | } | |
6013 | /* Start new measuring interval */ | |
64bf452e | 6014 | pmd->rxq_next_cycle_store = now + PMD_RXQ_INTERVAL_LEN; |
4809891b KT |
6015 | } |
6016 | ||
3453b4d6 JS |
6017 | if (now > pmd->next_optimization) { |
6018 | /* Try to obtain the flow lock to block out revalidator threads. | |
6019 | * If not possible, just try next time. */ | |
6020 | if (!ovs_mutex_trylock(&pmd->flow_mutex)) { | |
6021 | /* Optimize each classifier */ | |
6022 | CMAP_FOR_EACH (cls, node, &pmd->classifiers) { | |
6023 | dpcls_sort_subtable_vector(cls); | |
6024 | } | |
6025 | ovs_mutex_unlock(&pmd->flow_mutex); | |
6026 | /* Start new measuring interval */ | |
6027 | pmd->next_optimization = now + DPCLS_OPTIMIZATION_INTERVAL; | |
6028 | } | |
6029 | } | |
6030 | } | |
6031 | ||
0de8783a JR |
6032 | /* Insert 'rule' into 'cls'. */ |
6033 | static void | |
6034 | dpcls_insert(struct dpcls *cls, struct dpcls_rule *rule, | |
6035 | const struct netdev_flow_key *mask) | |
6036 | { | |
6037 | struct dpcls_subtable *subtable = dpcls_find_subtable(cls, mask); | |
6038 | ||
3453b4d6 | 6039 | /* Refer to subtable's mask, also for later removal. */ |
0de8783a JR |
6040 | rule->mask = &subtable->mask; |
6041 | cmap_insert(&subtable->rules, &rule->cmap_node, rule->flow.hash); | |
6042 | } | |
6043 | ||
6044 | /* Removes 'rule' from 'cls', also destructing the 'rule'. */ | |
6045 | static void | |
6046 | dpcls_remove(struct dpcls *cls, struct dpcls_rule *rule) | |
6047 | { | |
6048 | struct dpcls_subtable *subtable; | |
6049 | ||
6050 | ovs_assert(rule->mask); | |
6051 | ||
3453b4d6 | 6052 | /* Get subtable from reference in rule->mask. */ |
0de8783a | 6053 | INIT_CONTAINER(subtable, rule->mask, mask); |
0de8783a JR |
6054 | if (cmap_remove(&subtable->rules, &rule->cmap_node, rule->flow.hash) |
6055 | == 0) { | |
3453b4d6 | 6056 | /* Delete empty subtable. */ |
0de8783a | 6057 | dpcls_destroy_subtable(cls, subtable); |
da9cfca6 | 6058 | pvector_publish(&cls->subtables); |
0de8783a JR |
6059 | } |
6060 | } | |
6061 | ||
361d808d JR |
6062 | /* Returns true if 'target' satisfies 'key' in 'mask', that is, if each 1-bit |
6063 | * in 'mask' the values in 'key' and 'target' are the same. */ | |
0de8783a JR |
6064 | static inline bool |
6065 | dpcls_rule_matches_key(const struct dpcls_rule *rule, | |
6066 | const struct netdev_flow_key *target) | |
6067 | { | |
09b0fa9c JR |
6068 | const uint64_t *keyp = miniflow_get_values(&rule->flow.mf); |
6069 | const uint64_t *maskp = miniflow_get_values(&rule->mask->mf); | |
5fcff47b | 6070 | uint64_t value; |
0de8783a | 6071 | |
5fcff47b JR |
6072 | NETDEV_FLOW_KEY_FOR_EACH_IN_FLOWMAP(value, target, rule->flow.mf.map) { |
6073 | if (OVS_UNLIKELY((value & *maskp++) != *keyp++)) { | |
0de8783a JR |
6074 | return false; |
6075 | } | |
6076 | } | |
6077 | return true; | |
6078 | } | |
6079 | ||
5b1c9c78 FA |
6080 | /* For each miniflow in 'keys' performs a classifier lookup writing the result |
6081 | * into the corresponding slot in 'rules'. If a particular entry in 'keys' is | |
0de8783a JR |
6082 | * NULL it is skipped. |
6083 | * | |
6084 | * This function is optimized for use in the userspace datapath and therefore | |
6085 | * does not implement a lot of features available in the standard | |
6086 | * classifier_lookup() function. Specifically, it does not implement | |
6087 | * priorities, instead returning any rule which matches the flow. | |
6088 | * | |
5b1c9c78 | 6089 | * Returns true if all miniflows found a corresponding rule. */ |
0de8783a | 6090 | static bool |
3453b4d6 JS |
6091 | dpcls_lookup(struct dpcls *cls, const struct netdev_flow_key keys[], |
6092 | struct dpcls_rule **rules, const size_t cnt, | |
6093 | int *num_lookups_p) | |
0de8783a | 6094 | { |
5b1c9c78 | 6095 | /* The received 'cnt' miniflows are the search-keys that will be processed |
63906f18 BB |
6096 | * to find a matching entry into the available subtables. |
6097 | * The number of bits in map_type is equal to NETDEV_MAX_BURST. */ | |
6098 | typedef uint32_t map_type; | |
0de8783a | 6099 | #define MAP_BITS (sizeof(map_type) * CHAR_BIT) |
63906f18 | 6100 | BUILD_ASSERT_DECL(MAP_BITS >= NETDEV_MAX_BURST); |
0de8783a | 6101 | |
0de8783a JR |
6102 | struct dpcls_subtable *subtable; |
6103 | ||
63906f18 BB |
6104 | map_type keys_map = TYPE_MAXIMUM(map_type); /* Set all bits. */ |
6105 | map_type found_map; | |
6106 | uint32_t hashes[MAP_BITS]; | |
6107 | const struct cmap_node *nodes[MAP_BITS]; | |
6108 | ||
6109 | if (cnt != MAP_BITS) { | |
6110 | keys_map >>= MAP_BITS - cnt; /* Clear extra bits. */ | |
0de8783a JR |
6111 | } |
6112 | memset(rules, 0, cnt * sizeof *rules); | |
6113 | ||
3453b4d6 JS |
6114 | int lookups_match = 0, subtable_pos = 1; |
6115 | ||
5b1c9c78 FA |
6116 | /* The Datapath classifier - aka dpcls - is composed of subtables. |
6117 | * Subtables are dynamically created as needed when new rules are inserted. | |
6118 | * Each subtable collects rules with matches on a specific subset of packet | |
6119 | * fields as defined by the subtable's mask. We proceed to process every | |
6120 | * search-key against each subtable, but when a match is found for a | |
6121 | * search-key, the search for that key can stop because the rules are | |
6122 | * non-overlapping. */ | |
da9cfca6 | 6123 | PVECTOR_FOR_EACH (subtable, &cls->subtables) { |
63906f18 BB |
6124 | int i; |
6125 | ||
6126 | /* Compute hashes for the remaining keys. Each search-key is | |
6127 | * masked with the subtable's mask to avoid hashing the wildcarded | |
6128 | * bits. */ | |
6129 | ULLONG_FOR_EACH_1(i, keys_map) { | |
6130 | hashes[i] = netdev_flow_key_hash_in_mask(&keys[i], | |
6131 | &subtable->mask); | |
6132 | } | |
6133 | /* Lookup. */ | |
6134 | found_map = cmap_find_batch(&subtable->rules, keys_map, hashes, nodes); | |
6135 | /* Check results. When the i-th bit of found_map is set, it means | |
6136 | * that a set of nodes with a matching hash value was found for the | |
6137 | * i-th search-key. Due to possible hash collisions we need to check | |
6138 | * which of the found rules, if any, really matches our masked | |
6139 | * search-key. */ | |
6140 | ULLONG_FOR_EACH_1(i, found_map) { | |
6141 | struct dpcls_rule *rule; | |
6142 | ||
6143 | CMAP_NODE_FOR_EACH (rule, cmap_node, nodes[i]) { | |
6144 | if (OVS_LIKELY(dpcls_rule_matches_key(rule, &keys[i]))) { | |
6145 | rules[i] = rule; | |
6146 | /* Even at 20 Mpps the 32-bit hit_cnt cannot wrap | |
6147 | * within one second optimization interval. */ | |
6148 | subtable->hit_cnt++; | |
6149 | lookups_match += subtable_pos; | |
6150 | goto next; | |
0de8783a | 6151 | } |
0de8783a | 6152 | } |
63906f18 BB |
6153 | /* None of the found rules was a match. Reset the i-th bit to |
6154 | * keep searching this key in the next subtable. */ | |
6155 | ULLONG_SET0(found_map, i); /* Did not match. */ | |
6156 | next: | |
6157 | ; /* Keep Sparse happy. */ | |
0de8783a | 6158 | } |
63906f18 BB |
6159 | keys_map &= ~found_map; /* Clear the found rules. */ |
6160 | if (!keys_map) { | |
3453b4d6 JS |
6161 | if (num_lookups_p) { |
6162 | *num_lookups_p = lookups_match; | |
6163 | } | |
0de8783a JR |
6164 | return true; /* All found. */ |
6165 | } | |
3453b4d6 JS |
6166 | subtable_pos++; |
6167 | } | |
6168 | if (num_lookups_p) { | |
6169 | *num_lookups_p = lookups_match; | |
0de8783a JR |
6170 | } |
6171 | return false; /* Some misses. */ | |
6172 | } |