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