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