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