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