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