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