1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
16 #include "ofproto-dpif-upcall.h"
26 #include "dynamic-string.h"
27 #include "fail-open.h"
28 #include "guarded-list.h"
33 #include "ofproto-dpif-ipfix.h"
34 #include "ofproto-dpif-sflow.h"
35 #include "ofproto-dpif-xlate.h"
38 #include "poll-loop.h"
41 #include "openvswitch/vlog.h"
43 #define MAX_QUEUE_LENGTH 512
44 #define UPCALL_MAX_BATCH 64
45 #define REVALIDATE_MAX_BATCH 50
47 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_upcall
);
49 COVERAGE_DEFINE(dumped_duplicate_flow
);
50 COVERAGE_DEFINE(dumped_new_flow
);
51 COVERAGE_DEFINE(handler_duplicate_upcall
);
52 COVERAGE_DEFINE(upcall_ukey_contention
);
53 COVERAGE_DEFINE(revalidate_missed_dp_flow
);
55 /* A thread that reads upcalls from dpif, forwards each upcall's packet,
56 * and possibly sets up a kernel flow as a cache. */
58 struct udpif
*udpif
; /* Parent udpif. */
59 pthread_t thread
; /* Thread ID. */
60 uint32_t handler_id
; /* Handler id. */
63 /* In the absence of a multiple-writer multiple-reader datastructure for
64 * storing udpif_keys ("ukeys"), we use a large number of cmaps, each with its
65 * own lock for writing. */
66 #define N_UMAPS 512 /* per udpif. */
68 struct ovs_mutex mutex
; /* Take for writing to the following. */
69 struct cmap cmap
; /* Datapath flow keys. */
72 /* A thread that processes datapath flows, updates OpenFlow statistics, and
73 * updates or removes them if necessary.
75 * Revalidator threads operate in two phases: "dump" and "sweep". In between
76 * each phase, all revalidators sync up so that all revalidator threads are
77 * either in one phase or the other, but not a combination.
79 * During the dump phase, revalidators fetch flows from the datapath and
80 * attribute the statistics to OpenFlow rules. Each datapath flow has a
81 * corresponding ukey which caches the most recently seen statistics. If
82 * a flow needs to be deleted (for example, because it is unused over a
83 * period of time), revalidator threads may delete the flow during the
84 * dump phase. The datapath is not guaranteed to reliably dump all flows
85 * from the datapath, and there is no mapping between datapath flows to
86 * revalidators, so a particular flow may be handled by zero or more
87 * revalidators during a single dump phase. To avoid duplicate attribution
88 * of statistics, ukeys are never deleted during this phase.
90 * During the sweep phase, each revalidator takes ownership of a different
91 * slice of umaps and sweeps through all ukeys in those umaps to figure out
92 * whether they need to be deleted. During this phase, revalidators may
93 * fetch individual flows which were not dumped during the dump phase to
94 * validate them and attribute statistics.
97 struct udpif
*udpif
; /* Parent udpif. */
98 pthread_t thread
; /* Thread ID. */
99 unsigned int id
; /* ovsthread_id_self(). */
102 /* An upcall handler for ofproto_dpif.
104 * udpif keeps records of two kind of logically separate units:
109 * - An array of 'struct handler's for upcall handling and flow
115 * - Revalidation threads which read the datapath flow table and maintains
119 struct ovs_list list_node
; /* In all_udpifs list. */
121 struct dpif
*dpif
; /* Datapath handle. */
122 struct dpif_backer
*backer
; /* Opaque dpif_backer pointer. */
124 struct handler
*handlers
; /* Upcall handlers. */
127 struct revalidator
*revalidators
; /* Flow revalidators. */
128 size_t n_revalidators
;
130 struct latch exit_latch
; /* Tells child threads to exit. */
133 struct seq
*reval_seq
; /* Incremented to force revalidation. */
134 bool reval_exit
; /* Set by leader on 'exit_latch. */
135 struct ovs_barrier reval_barrier
; /* Barrier used by revalidators. */
136 struct dpif_flow_dump
*dump
; /* DPIF flow dump state. */
137 long long int dump_duration
; /* Duration of the last flow dump. */
138 struct seq
*dump_seq
; /* Increments each dump iteration. */
139 atomic_bool enable_ufid
; /* If true, skip dumping flow attrs. */
141 /* These variables provide a mechanism for the main thread to pause
142 * all revalidation without having to completely shut the threads down.
143 * 'pause_latch' is shared between the main thread and the lead
144 * revalidator thread, so when it is desirable to halt revalidation, the
145 * main thread will set the latch. 'pause' and 'pause_barrier' are shared
146 * by revalidator threads. The lead revalidator will set 'pause' when it
147 * observes the latch has been set, and this will cause all revalidator
148 * threads to wait on 'pause_barrier' at the beginning of the next
149 * revalidation round. */
150 bool pause
; /* Set by leader on 'pause_latch. */
151 struct latch pause_latch
; /* Set to force revalidators pause. */
152 struct ovs_barrier pause_barrier
; /* Barrier used to pause all */
153 /* revalidators by main thread. */
155 /* There are 'N_UMAPS' maps containing 'struct udpif_key' elements.
157 * During the flow dump phase, revalidators insert into these with a random
158 * distribution. During the garbage collection phase, each revalidator
159 * takes care of garbage collecting a slice of these maps. */
162 /* Datapath flow statistics. */
163 unsigned int max_n_flows
;
164 unsigned int avg_n_flows
;
166 /* Following fields are accessed and modified by different threads. */
167 atomic_uint flow_limit
; /* Datapath flow hard limit. */
169 /* n_flows_mutex prevents multiple threads updating these concurrently. */
170 atomic_uint n_flows
; /* Number of flows in the datapath. */
171 atomic_llong n_flows_timestamp
; /* Last time n_flows was updated. */
172 struct ovs_mutex n_flows_mutex
;
174 /* Following fields are accessed and modified only from the main thread. */
175 struct unixctl_conn
**conns
; /* Connections waiting on dump_seq. */
176 uint64_t conn_seq
; /* Corresponds to 'dump_seq' when
177 conns[n_conns-1] was stored. */
178 size_t n_conns
; /* Number of connections waiting. */
182 BAD_UPCALL
, /* Some kind of bug somewhere. */
183 MISS_UPCALL
, /* A flow miss. */
184 SFLOW_UPCALL
, /* sFlow sample. */
185 FLOW_SAMPLE_UPCALL
, /* Per-flow sampling. */
186 IPFIX_UPCALL
/* Per-bridge sampling. */
196 struct ofproto_dpif
*ofproto
; /* Parent ofproto. */
197 const struct recirc_id_node
*recirc
; /* Recirculation context. */
198 bool have_recirc_ref
; /* Reference held on recirc ctx? */
200 /* The flow and packet are only required to be constant when using
201 * dpif-netdev. If a modification is absolutely necessary, a const cast
202 * may be used with other datapaths. */
203 const struct flow
*flow
; /* Parsed representation of the packet. */
204 const ovs_u128
*ufid
; /* Unique identifier for 'flow'. */
205 unsigned pmd_id
; /* Datapath poll mode driver id. */
206 const struct dp_packet
*packet
; /* Packet associated with this upcall. */
207 ofp_port_t in_port
; /* OpenFlow in port, or OFPP_NONE. */
208 uint16_t mru
; /* If !0, Maximum receive unit of
209 fragmented IP packet */
211 enum dpif_upcall_type type
; /* Datapath type of the upcall. */
212 const struct nlattr
*userdata
; /* Userdata for DPIF_UC_ACTION Upcalls. */
213 const struct nlattr
*actions
; /* Flow actions in DPIF_UC_ACTION Upcalls. */
215 bool xout_initialized
; /* True if 'xout' must be uninitialized. */
216 struct xlate_out xout
; /* Result of xlate_actions(). */
217 struct ofpbuf odp_actions
; /* Datapath actions from xlate_actions(). */
218 struct flow_wildcards wc
; /* Dependencies that megaflow must match. */
219 struct ofpbuf put_actions
; /* Actions 'put' in the fastpath. */
221 struct dpif_ipfix
*ipfix
; /* IPFIX pointer or NULL. */
222 struct dpif_sflow
*sflow
; /* SFlow pointer or NULL. */
224 bool vsp_adjusted
; /* 'packet' and 'flow' were adjusted for
225 VLAN splinters if true. */
227 struct udpif_key
*ukey
; /* Revalidator flow cache. */
228 bool ukey_persists
; /* Set true to keep 'ukey' beyond the
229 lifetime of this upcall. */
231 uint64_t dump_seq
; /* udpif->dump_seq at translation time. */
232 uint64_t reval_seq
; /* udpif->reval_seq at translation time. */
234 /* Not used by the upcall callback interface. */
235 const struct nlattr
*key
; /* Datapath flow key. */
236 size_t key_len
; /* Datapath flow key length. */
237 const struct nlattr
*out_tun_key
; /* Datapath output tunnel key. */
239 uint64_t odp_actions_stub
[1024 / 8]; /* Stub for odp_actions. */
242 /* 'udpif_key's are responsible for tracking the little bit of state udpif
243 * needs to do flow expiration which can't be pulled directly from the
244 * datapath. They may be created by any handler or revalidator thread at any
245 * time, and read by any revalidator during the dump phase. They are however
246 * each owned by a single revalidator which takes care of destroying them
247 * during the garbage-collection phase.
249 * The mutex within the ukey protects some members of the ukey. The ukey
250 * itself is protected by RCU and is held within a umap in the parent udpif.
251 * Adding or removing a ukey from a umap is only safe when holding the
252 * corresponding umap lock. */
254 struct cmap_node cmap_node
; /* In parent revalidator 'ukeys' map. */
256 /* These elements are read only once created, and therefore aren't
257 * protected by a mutex. */
258 const struct nlattr
*key
; /* Datapath flow key. */
259 size_t key_len
; /* Length of 'key'. */
260 const struct nlattr
*mask
; /* Datapath flow mask. */
261 size_t mask_len
; /* Length of 'mask'. */
262 ovs_u128 ufid
; /* Unique flow identifier. */
263 bool ufid_present
; /* True if 'ufid' is in datapath. */
264 uint32_t hash
; /* Pre-computed hash for 'key'. */
265 unsigned pmd_id
; /* Datapath poll mode driver id. */
267 struct ovs_mutex mutex
; /* Guards the following. */
268 struct dpif_flow_stats stats OVS_GUARDED
; /* Last known stats.*/
269 long long int created OVS_GUARDED
; /* Estimate of creation time. */
270 uint64_t dump_seq OVS_GUARDED
; /* Tracks udpif->dump_seq. */
271 uint64_t reval_seq OVS_GUARDED
; /* Tracks udpif->reval_seq. */
272 bool flow_exists OVS_GUARDED
; /* Ensures flows are only deleted
274 /* Datapath flow actions as nlattrs. Protected by RCU. Read with
275 * ukey_get_actions(), and write with ukey_set_actions(). */
276 OVSRCU_TYPE(struct ofpbuf
*) actions
;
278 struct xlate_cache
*xcache OVS_GUARDED
; /* Cache for xlate entries that
279 * are affected by this ukey.
280 * Used for stats and learning.*/
282 struct odputil_keybuf buf
;
286 uint32_t key_recirc_id
; /* Non-zero if reference is held by the ukey. */
287 struct recirc_refs recircs
; /* Action recirc IDs with references held. */
290 /* Datapath operation with optional ukey attached. */
292 struct udpif_key
*ukey
;
293 struct dpif_flow_stats stats
; /* Stats for 'op'. */
294 struct dpif_op dop
; /* Flow operation. */
297 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
298 static struct ovs_list all_udpifs
= OVS_LIST_INITIALIZER(&all_udpifs
);
300 static size_t recv_upcalls(struct handler
*);
301 static int process_upcall(struct udpif
*, struct upcall
*,
302 struct ofpbuf
*odp_actions
, struct flow_wildcards
*);
303 static void handle_upcalls(struct udpif
*, struct upcall
*, size_t n_upcalls
);
304 static void udpif_stop_threads(struct udpif
*);
305 static void udpif_start_threads(struct udpif
*, size_t n_handlers
,
306 size_t n_revalidators
);
307 static void udpif_pause_revalidators(struct udpif
*);
308 static void udpif_resume_revalidators(struct udpif
*);
309 static void *udpif_upcall_handler(void *);
310 static void *udpif_revalidator(void *);
311 static unsigned long udpif_get_n_flows(struct udpif
*);
312 static void revalidate(struct revalidator
*);
313 static void revalidator_pause(struct revalidator
*);
314 static void revalidator_sweep(struct revalidator
*);
315 static void revalidator_purge(struct revalidator
*);
316 static void upcall_unixctl_show(struct unixctl_conn
*conn
, int argc
,
317 const char *argv
[], void *aux
);
318 static void upcall_unixctl_disable_megaflows(struct unixctl_conn
*, int argc
,
319 const char *argv
[], void *aux
);
320 static void upcall_unixctl_enable_megaflows(struct unixctl_conn
*, int argc
,
321 const char *argv
[], void *aux
);
322 static void upcall_unixctl_disable_ufid(struct unixctl_conn
*, int argc
,
323 const char *argv
[], void *aux
);
324 static void upcall_unixctl_enable_ufid(struct unixctl_conn
*, int argc
,
325 const char *argv
[], void *aux
);
326 static void upcall_unixctl_set_flow_limit(struct unixctl_conn
*conn
, int argc
,
327 const char *argv
[], void *aux
);
328 static void upcall_unixctl_dump_wait(struct unixctl_conn
*conn
, int argc
,
329 const char *argv
[], void *aux
);
330 static void upcall_unixctl_purge(struct unixctl_conn
*conn
, int argc
,
331 const char *argv
[], void *aux
);
333 static struct udpif_key
*ukey_create_from_upcall(struct upcall
*,
334 struct flow_wildcards
*);
335 static int ukey_create_from_dpif_flow(const struct udpif
*,
336 const struct dpif_flow
*,
337 struct udpif_key
**);
338 static void ukey_get_actions(struct udpif_key
*, const struct nlattr
**actions
,
340 static bool ukey_install_start(struct udpif
*, struct udpif_key
*ukey
);
341 static bool ukey_install_finish(struct udpif_key
*ukey
, int error
);
342 static bool ukey_install(struct udpif
*udpif
, struct udpif_key
*ukey
);
343 static struct udpif_key
*ukey_lookup(struct udpif
*udpif
,
344 const ovs_u128
*ufid
);
345 static int ukey_acquire(struct udpif
*, const struct dpif_flow
*,
346 struct udpif_key
**result
, int *error
);
347 static void ukey_delete__(struct udpif_key
*);
348 static void ukey_delete(struct umap
*, struct udpif_key
*);
349 static enum upcall_type
classify_upcall(enum dpif_upcall_type type
,
350 const struct nlattr
*userdata
);
352 static int upcall_receive(struct upcall
*, const struct dpif_backer
*,
353 const struct dp_packet
*packet
, enum dpif_upcall_type
,
354 const struct nlattr
*userdata
, const struct flow
*,
355 const unsigned int mru
,
356 const ovs_u128
*ufid
, const unsigned pmd_id
);
357 static void upcall_uninit(struct upcall
*);
359 static upcall_callback upcall_cb
;
360 static dp_purge_callback dp_purge_cb
;
362 static atomic_bool enable_megaflows
= ATOMIC_VAR_INIT(true);
363 static atomic_bool enable_ufid
= ATOMIC_VAR_INIT(true);
368 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
369 if (ovsthread_once_start(&once
)) {
370 unixctl_command_register("upcall/show", "", 0, 0, upcall_unixctl_show
,
372 unixctl_command_register("upcall/disable-megaflows", "", 0, 0,
373 upcall_unixctl_disable_megaflows
, NULL
);
374 unixctl_command_register("upcall/enable-megaflows", "", 0, 0,
375 upcall_unixctl_enable_megaflows
, NULL
);
376 unixctl_command_register("upcall/disable-ufid", "", 0, 0,
377 upcall_unixctl_disable_ufid
, NULL
);
378 unixctl_command_register("upcall/enable-ufid", "", 0, 0,
379 upcall_unixctl_enable_ufid
, NULL
);
380 unixctl_command_register("upcall/set-flow-limit", "", 1, 1,
381 upcall_unixctl_set_flow_limit
, NULL
);
382 unixctl_command_register("revalidator/wait", "", 0, 0,
383 upcall_unixctl_dump_wait
, NULL
);
384 unixctl_command_register("revalidator/purge", "", 0, 0,
385 upcall_unixctl_purge
, NULL
);
386 ovsthread_once_done(&once
);
391 udpif_create(struct dpif_backer
*backer
, struct dpif
*dpif
)
393 struct udpif
*udpif
= xzalloc(sizeof *udpif
);
396 udpif
->backer
= backer
;
397 atomic_init(&udpif
->flow_limit
, MIN(ofproto_flow_limit
, 10000));
398 udpif
->reval_seq
= seq_create();
399 udpif
->dump_seq
= seq_create();
400 latch_init(&udpif
->exit_latch
);
401 latch_init(&udpif
->pause_latch
);
402 list_push_back(&all_udpifs
, &udpif
->list_node
);
403 atomic_init(&udpif
->enable_ufid
, false);
404 atomic_init(&udpif
->n_flows
, 0);
405 atomic_init(&udpif
->n_flows_timestamp
, LLONG_MIN
);
406 ovs_mutex_init(&udpif
->n_flows_mutex
);
407 udpif
->ukeys
= xmalloc(N_UMAPS
* sizeof *udpif
->ukeys
);
408 for (int i
= 0; i
< N_UMAPS
; i
++) {
409 cmap_init(&udpif
->ukeys
[i
].cmap
);
410 ovs_mutex_init(&udpif
->ukeys
[i
].mutex
);
413 dpif_register_upcall_cb(dpif
, upcall_cb
, udpif
);
414 dpif_register_dp_purge_cb(dpif
, dp_purge_cb
, udpif
);
420 udpif_run(struct udpif
*udpif
)
422 if (udpif
->conns
&& udpif
->conn_seq
!= seq_read(udpif
->dump_seq
)) {
425 for (i
= 0; i
< udpif
->n_conns
; i
++) {
426 unixctl_command_reply(udpif
->conns
[i
], NULL
);
435 udpif_destroy(struct udpif
*udpif
)
437 udpif_stop_threads(udpif
);
439 for (int i
= 0; i
< N_UMAPS
; i
++) {
440 cmap_destroy(&udpif
->ukeys
[i
].cmap
);
441 ovs_mutex_destroy(&udpif
->ukeys
[i
].mutex
);
446 list_remove(&udpif
->list_node
);
447 latch_destroy(&udpif
->exit_latch
);
448 latch_destroy(&udpif
->pause_latch
);
449 seq_destroy(udpif
->reval_seq
);
450 seq_destroy(udpif
->dump_seq
);
451 ovs_mutex_destroy(&udpif
->n_flows_mutex
);
455 /* Stops the handler and revalidator threads, must be enclosed in
456 * ovsrcu quiescent state unless when destroying udpif. */
458 udpif_stop_threads(struct udpif
*udpif
)
460 if (udpif
&& (udpif
->n_handlers
!= 0 || udpif
->n_revalidators
!= 0)) {
463 latch_set(&udpif
->exit_latch
);
465 for (i
= 0; i
< udpif
->n_handlers
; i
++) {
466 struct handler
*handler
= &udpif
->handlers
[i
];
468 xpthread_join(handler
->thread
, NULL
);
471 for (i
= 0; i
< udpif
->n_revalidators
; i
++) {
472 xpthread_join(udpif
->revalidators
[i
].thread
, NULL
);
475 dpif_disable_upcall(udpif
->dpif
);
477 for (i
= 0; i
< udpif
->n_revalidators
; i
++) {
478 struct revalidator
*revalidator
= &udpif
->revalidators
[i
];
480 /* Delete ukeys, and delete all flows from the datapath to prevent
481 * double-counting stats. */
482 revalidator_purge(revalidator
);
485 latch_poll(&udpif
->exit_latch
);
487 ovs_barrier_destroy(&udpif
->reval_barrier
);
488 ovs_barrier_destroy(&udpif
->pause_barrier
);
490 free(udpif
->revalidators
);
491 udpif
->revalidators
= NULL
;
492 udpif
->n_revalidators
= 0;
494 free(udpif
->handlers
);
495 udpif
->handlers
= NULL
;
496 udpif
->n_handlers
= 0;
500 /* Starts the handler and revalidator threads, must be enclosed in
501 * ovsrcu quiescent state. */
503 udpif_start_threads(struct udpif
*udpif
, size_t n_handlers
,
504 size_t n_revalidators
)
506 if (udpif
&& n_handlers
&& n_revalidators
) {
510 udpif
->n_handlers
= n_handlers
;
511 udpif
->n_revalidators
= n_revalidators
;
513 udpif
->handlers
= xzalloc(udpif
->n_handlers
* sizeof *udpif
->handlers
);
514 for (i
= 0; i
< udpif
->n_handlers
; i
++) {
515 struct handler
*handler
= &udpif
->handlers
[i
];
517 handler
->udpif
= udpif
;
518 handler
->handler_id
= i
;
519 handler
->thread
= ovs_thread_create(
520 "handler", udpif_upcall_handler
, handler
);
523 enable_ufid
= ofproto_dpif_get_enable_ufid(udpif
->backer
);
524 atomic_init(&udpif
->enable_ufid
, enable_ufid
);
525 dpif_enable_upcall(udpif
->dpif
);
527 ovs_barrier_init(&udpif
->reval_barrier
, udpif
->n_revalidators
);
528 ovs_barrier_init(&udpif
->pause_barrier
, udpif
->n_revalidators
+ 1);
529 udpif
->reval_exit
= false;
530 udpif
->pause
= false;
531 udpif
->revalidators
= xzalloc(udpif
->n_revalidators
532 * sizeof *udpif
->revalidators
);
533 for (i
= 0; i
< udpif
->n_revalidators
; i
++) {
534 struct revalidator
*revalidator
= &udpif
->revalidators
[i
];
536 revalidator
->udpif
= udpif
;
537 revalidator
->thread
= ovs_thread_create(
538 "revalidator", udpif_revalidator
, revalidator
);
543 /* Pauses all revalidators. Should only be called by the main thread.
544 * When function returns, all revalidators are paused and will proceed
545 * only after udpif_resume_revalidators() is called. */
547 udpif_pause_revalidators(struct udpif
*udpif
)
549 if (ofproto_dpif_backer_enabled(udpif
->backer
)) {
550 latch_set(&udpif
->pause_latch
);
551 ovs_barrier_block(&udpif
->pause_barrier
);
555 /* Resumes the pausing of revalidators. Should only be called by the
558 udpif_resume_revalidators(struct udpif
*udpif
)
560 if (ofproto_dpif_backer_enabled(udpif
->backer
)) {
561 latch_poll(&udpif
->pause_latch
);
562 ovs_barrier_block(&udpif
->pause_barrier
);
566 /* Tells 'udpif' how many threads it should use to handle upcalls.
567 * 'n_handlers' and 'n_revalidators' can never be zero. 'udpif''s
568 * datapath handle must have packet reception enabled before starting
571 udpif_set_threads(struct udpif
*udpif
, size_t n_handlers
,
572 size_t n_revalidators
)
575 ovs_assert(n_handlers
&& n_revalidators
);
577 ovsrcu_quiesce_start();
578 if (udpif
->n_handlers
!= n_handlers
579 || udpif
->n_revalidators
!= n_revalidators
) {
580 udpif_stop_threads(udpif
);
583 if (!udpif
->handlers
&& !udpif
->revalidators
) {
586 error
= dpif_handlers_set(udpif
->dpif
, n_handlers
);
588 VLOG_ERR("failed to configure handlers in dpif %s: %s",
589 dpif_name(udpif
->dpif
), ovs_strerror(error
));
593 udpif_start_threads(udpif
, n_handlers
, n_revalidators
);
595 ovsrcu_quiesce_end();
598 /* Waits for all ongoing upcall translations to complete. This ensures that
599 * there are no transient references to any removed ofprotos (or other
600 * objects). In particular, this should be called after an ofproto is removed
601 * (e.g. via xlate_remove_ofproto()) but before it is destroyed. */
603 udpif_synchronize(struct udpif
*udpif
)
605 /* This is stronger than necessary. It would be sufficient to ensure
606 * (somehow) that each handler and revalidator thread had passed through
607 * its main loop once. */
608 size_t n_handlers
= udpif
->n_handlers
;
609 size_t n_revalidators
= udpif
->n_revalidators
;
611 ovsrcu_quiesce_start();
612 udpif_stop_threads(udpif
);
613 udpif_start_threads(udpif
, n_handlers
, n_revalidators
);
614 ovsrcu_quiesce_end();
617 /* Notifies 'udpif' that something changed which may render previous
618 * xlate_actions() results invalid. */
620 udpif_revalidate(struct udpif
*udpif
)
622 seq_change(udpif
->reval_seq
);
625 /* Returns a seq which increments every time 'udpif' pulls stats from the
626 * datapath. Callers can use this to get a sense of when might be a good time
627 * to do periodic work which relies on relatively up to date statistics. */
629 udpif_dump_seq(struct udpif
*udpif
)
631 return udpif
->dump_seq
;
635 udpif_get_memory_usage(struct udpif
*udpif
, struct simap
*usage
)
639 simap_increase(usage
, "handlers", udpif
->n_handlers
);
641 simap_increase(usage
, "revalidators", udpif
->n_revalidators
);
642 for (i
= 0; i
< N_UMAPS
; i
++) {
643 simap_increase(usage
, "udpif keys", cmap_count(&udpif
->ukeys
[i
].cmap
));
647 /* Remove flows from a single datapath. */
649 udpif_flush(struct udpif
*udpif
)
651 size_t n_handlers
, n_revalidators
;
653 n_handlers
= udpif
->n_handlers
;
654 n_revalidators
= udpif
->n_revalidators
;
656 ovsrcu_quiesce_start();
658 udpif_stop_threads(udpif
);
659 dpif_flow_flush(udpif
->dpif
);
660 udpif_start_threads(udpif
, n_handlers
, n_revalidators
);
662 ovsrcu_quiesce_end();
665 /* Removes all flows from all datapaths. */
667 udpif_flush_all_datapaths(void)
671 LIST_FOR_EACH (udpif
, list_node
, &all_udpifs
) {
677 udpif_use_ufid(struct udpif
*udpif
)
681 atomic_read_relaxed(&enable_ufid
, &enable
);
682 return enable
&& ofproto_dpif_get_enable_ufid(udpif
->backer
);
687 udpif_get_n_flows(struct udpif
*udpif
)
689 long long int time
, now
;
690 unsigned long flow_count
;
693 atomic_read_relaxed(&udpif
->n_flows_timestamp
, &time
);
694 if (time
< now
- 100 && !ovs_mutex_trylock(&udpif
->n_flows_mutex
)) {
695 struct dpif_dp_stats stats
;
697 atomic_store_relaxed(&udpif
->n_flows_timestamp
, now
);
698 dpif_get_dp_stats(udpif
->dpif
, &stats
);
699 flow_count
= stats
.n_flows
;
700 atomic_store_relaxed(&udpif
->n_flows
, flow_count
);
701 ovs_mutex_unlock(&udpif
->n_flows_mutex
);
703 atomic_read_relaxed(&udpif
->n_flows
, &flow_count
);
708 /* The upcall handler thread tries to read a batch of UPCALL_MAX_BATCH
709 * upcalls from dpif, processes the batch and installs corresponding flows
712 udpif_upcall_handler(void *arg
)
714 struct handler
*handler
= arg
;
715 struct udpif
*udpif
= handler
->udpif
;
717 while (!latch_is_set(&handler
->udpif
->exit_latch
)) {
718 if (recv_upcalls(handler
)) {
719 poll_immediate_wake();
721 dpif_recv_wait(udpif
->dpif
, handler
->handler_id
);
722 latch_wait(&udpif
->exit_latch
);
731 recv_upcalls(struct handler
*handler
)
733 struct udpif
*udpif
= handler
->udpif
;
734 uint64_t recv_stubs
[UPCALL_MAX_BATCH
][512 / 8];
735 struct ofpbuf recv_bufs
[UPCALL_MAX_BATCH
];
736 struct dpif_upcall dupcalls
[UPCALL_MAX_BATCH
];
737 struct upcall upcalls
[UPCALL_MAX_BATCH
];
738 struct flow flows
[UPCALL_MAX_BATCH
];
742 while (n_upcalls
< UPCALL_MAX_BATCH
) {
743 struct ofpbuf
*recv_buf
= &recv_bufs
[n_upcalls
];
744 struct dpif_upcall
*dupcall
= &dupcalls
[n_upcalls
];
745 struct upcall
*upcall
= &upcalls
[n_upcalls
];
746 struct flow
*flow
= &flows
[n_upcalls
];
750 ofpbuf_use_stub(recv_buf
, recv_stubs
[n_upcalls
],
751 sizeof recv_stubs
[n_upcalls
]);
752 if (dpif_recv(udpif
->dpif
, handler
->handler_id
, dupcall
, recv_buf
)) {
753 ofpbuf_uninit(recv_buf
);
757 if (odp_flow_key_to_flow(dupcall
->key
, dupcall
->key_len
, flow
)
763 mru
= nl_attr_get_u16(dupcall
->mru
);
768 error
= upcall_receive(upcall
, udpif
->backer
, &dupcall
->packet
,
769 dupcall
->type
, dupcall
->userdata
, flow
, mru
,
770 &dupcall
->ufid
, PMD_ID_NULL
);
772 if (error
== ENODEV
) {
773 /* Received packet on datapath port for which we couldn't
774 * associate an ofproto. This can happen if a port is removed
775 * while traffic is being received. Print a rate-limited
776 * message in case it happens frequently. */
777 dpif_flow_put(udpif
->dpif
, DPIF_FP_CREATE
, dupcall
->key
,
778 dupcall
->key_len
, NULL
, 0, NULL
, 0,
779 &dupcall
->ufid
, PMD_ID_NULL
, NULL
);
780 VLOG_INFO_RL(&rl
, "received packet on unassociated datapath "
781 "port %"PRIu32
, flow
->in_port
.odp_port
);
786 upcall
->key
= dupcall
->key
;
787 upcall
->key_len
= dupcall
->key_len
;
788 upcall
->ufid
= &dupcall
->ufid
;
790 upcall
->out_tun_key
= dupcall
->out_tun_key
;
791 upcall
->actions
= dupcall
->actions
;
793 if (vsp_adjust_flow(upcall
->ofproto
, flow
, &dupcall
->packet
)) {
794 upcall
->vsp_adjusted
= true;
797 pkt_metadata_from_flow(&dupcall
->packet
.md
, flow
);
798 flow_extract(&dupcall
->packet
, flow
);
800 error
= process_upcall(udpif
, upcall
,
801 &upcall
->odp_actions
, &upcall
->wc
);
810 upcall_uninit(upcall
);
812 dp_packet_uninit(&dupcall
->packet
);
813 ofpbuf_uninit(recv_buf
);
817 handle_upcalls(handler
->udpif
, upcalls
, n_upcalls
);
818 for (i
= 0; i
< n_upcalls
; i
++) {
819 dp_packet_uninit(&dupcalls
[i
].packet
);
820 ofpbuf_uninit(&recv_bufs
[i
]);
821 upcall_uninit(&upcalls
[i
]);
829 udpif_revalidator(void *arg
)
831 /* Used by all revalidators. */
832 struct revalidator
*revalidator
= arg
;
833 struct udpif
*udpif
= revalidator
->udpif
;
834 bool leader
= revalidator
== &udpif
->revalidators
[0];
836 /* Used only by the leader. */
837 long long int start_time
= 0;
838 uint64_t last_reval_seq
= 0;
841 revalidator
->id
= ovsthread_id_self();
846 recirc_run(); /* Recirculation cleanup. */
848 reval_seq
= seq_read(udpif
->reval_seq
);
849 last_reval_seq
= reval_seq
;
851 n_flows
= udpif_get_n_flows(udpif
);
852 udpif
->max_n_flows
= MAX(n_flows
, udpif
->max_n_flows
);
853 udpif
->avg_n_flows
= (udpif
->avg_n_flows
+ n_flows
) / 2;
855 /* Only the leader checks the pause latch to prevent a race where
856 * some threads think it's false and proceed to block on
857 * reval_barrier and others think it's true and block indefinitely
858 * on the pause_barrier */
859 udpif
->pause
= latch_is_set(&udpif
->pause_latch
);
861 /* Only the leader checks the exit latch to prevent a race where
862 * some threads think it's true and exit and others think it's
863 * false and block indefinitely on the reval_barrier */
864 udpif
->reval_exit
= latch_is_set(&udpif
->exit_latch
);
866 start_time
= time_msec();
867 if (!udpif
->reval_exit
) {
870 terse_dump
= udpif_use_ufid(udpif
);
871 udpif
->dump
= dpif_flow_dump_create(udpif
->dpif
, terse_dump
);
875 /* Wait for the leader to start the flow dump. */
876 ovs_barrier_block(&udpif
->reval_barrier
);
878 revalidator_pause(revalidator
);
881 if (udpif
->reval_exit
) {
884 revalidate(revalidator
);
886 /* Wait for all flows to have been dumped before we garbage collect. */
887 ovs_barrier_block(&udpif
->reval_barrier
);
888 revalidator_sweep(revalidator
);
890 /* Wait for all revalidators to finish garbage collection. */
891 ovs_barrier_block(&udpif
->reval_barrier
);
894 unsigned int flow_limit
;
895 long long int duration
;
897 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
899 dpif_flow_dump_destroy(udpif
->dump
);
900 seq_change(udpif
->dump_seq
);
902 duration
= MAX(time_msec() - start_time
, 1);
903 udpif
->dump_duration
= duration
;
904 if (duration
> 2000) {
905 flow_limit
/= duration
/ 1000;
906 } else if (duration
> 1300) {
907 flow_limit
= flow_limit
* 3 / 4;
908 } else if (duration
< 1000 && n_flows
> 2000
909 && flow_limit
< n_flows
* 1000 / duration
) {
912 flow_limit
= MIN(ofproto_flow_limit
, MAX(flow_limit
, 1000));
913 atomic_store_relaxed(&udpif
->flow_limit
, flow_limit
);
915 if (duration
> 2000) {
916 VLOG_INFO("Spent an unreasonably long %lldms dumping flows",
920 poll_timer_wait_until(start_time
+ MIN(ofproto_max_idle
, 500));
921 seq_wait(udpif
->reval_seq
, last_reval_seq
);
922 latch_wait(&udpif
->exit_latch
);
923 latch_wait(&udpif
->pause_latch
);
931 static enum upcall_type
932 classify_upcall(enum dpif_upcall_type type
, const struct nlattr
*userdata
)
934 union user_action_cookie cookie
;
937 /* First look at the upcall type. */
945 case DPIF_N_UC_TYPES
:
947 VLOG_WARN_RL(&rl
, "upcall has unexpected type %"PRIu32
, type
);
951 /* "action" upcalls need a closer look. */
953 VLOG_WARN_RL(&rl
, "action upcall missing cookie");
956 userdata_len
= nl_attr_get_size(userdata
);
957 if (userdata_len
< sizeof cookie
.type
958 || userdata_len
> sizeof cookie
) {
959 VLOG_WARN_RL(&rl
, "action upcall cookie has unexpected size %"PRIuSIZE
,
963 memset(&cookie
, 0, sizeof cookie
);
964 memcpy(&cookie
, nl_attr_get(userdata
), userdata_len
);
965 if (userdata_len
== MAX(8, sizeof cookie
.sflow
)
966 && cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
968 } else if (userdata_len
== MAX(8, sizeof cookie
.slow_path
)
969 && cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
971 } else if (userdata_len
== MAX(8, sizeof cookie
.flow_sample
)
972 && cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
973 return FLOW_SAMPLE_UPCALL
;
974 } else if (userdata_len
== MAX(8, sizeof cookie
.ipfix
)
975 && cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
978 VLOG_WARN_RL(&rl
, "invalid user cookie of type %"PRIu16
979 " and size %"PRIuSIZE
, cookie
.type
, userdata_len
);
984 /* Calculates slow path actions for 'xout'. 'buf' must statically be
985 * initialized with at least 128 bytes of space. */
987 compose_slow_path(struct udpif
*udpif
, struct xlate_out
*xout
,
988 const struct flow
*flow
, odp_port_t odp_in_port
,
991 union user_action_cookie cookie
;
995 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
996 cookie
.slow_path
.unused
= 0;
997 cookie
.slow_path
.reason
= xout
->slow
;
999 port
= xout
->slow
& (SLOW_CFM
| SLOW_BFD
| SLOW_LACP
| SLOW_STP
)
1002 pid
= dpif_port_get_pid(udpif
->dpif
, port
, flow_hash_5tuple(flow
, 0));
1003 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
.slow_path
,
1004 ODPP_NONE
, false, buf
);
1007 /* If there is no error, the upcall must be destroyed with upcall_uninit()
1008 * before quiescing, as the referred objects are guaranteed to exist only
1009 * until the calling thread quiesces. Otherwise, do not call upcall_uninit()
1010 * since the 'upcall->put_actions' remains uninitialized. */
1012 upcall_receive(struct upcall
*upcall
, const struct dpif_backer
*backer
,
1013 const struct dp_packet
*packet
, enum dpif_upcall_type type
,
1014 const struct nlattr
*userdata
, const struct flow
*flow
,
1015 const unsigned int mru
,
1016 const ovs_u128
*ufid
, const unsigned pmd_id
)
1020 error
= xlate_lookup(backer
, flow
, &upcall
->ofproto
, &upcall
->ipfix
,
1021 &upcall
->sflow
, NULL
, &upcall
->in_port
);
1026 upcall
->recirc
= NULL
;
1027 upcall
->have_recirc_ref
= false;
1028 upcall
->flow
= flow
;
1029 upcall
->packet
= packet
;
1030 upcall
->ufid
= ufid
;
1031 upcall
->pmd_id
= pmd_id
;
1032 upcall
->type
= type
;
1033 upcall
->userdata
= userdata
;
1034 ofpbuf_use_stub(&upcall
->odp_actions
, upcall
->odp_actions_stub
,
1035 sizeof upcall
->odp_actions_stub
);
1036 ofpbuf_init(&upcall
->put_actions
, 0);
1038 upcall
->xout_initialized
= false;
1039 upcall
->vsp_adjusted
= false;
1040 upcall
->ukey_persists
= false;
1042 upcall
->ukey
= NULL
;
1044 upcall
->key_len
= 0;
1047 upcall
->out_tun_key
= NULL
;
1048 upcall
->actions
= NULL
;
1054 upcall_xlate(struct udpif
*udpif
, struct upcall
*upcall
,
1055 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
1057 struct dpif_flow_stats stats
;
1058 struct xlate_in xin
;
1060 stats
.n_packets
= 1;
1061 stats
.n_bytes
= dp_packet_size(upcall
->packet
);
1062 stats
.used
= time_msec();
1063 stats
.tcp_flags
= ntohs(upcall
->flow
->tcp_flags
);
1065 xlate_in_init(&xin
, upcall
->ofproto
, upcall
->flow
, upcall
->in_port
, NULL
,
1066 stats
.tcp_flags
, upcall
->packet
, wc
, odp_actions
);
1068 if (upcall
->type
== DPIF_UC_MISS
) {
1069 xin
.resubmit_stats
= &stats
;
1072 /* We may install a datapath flow only if we get a reference to the
1073 * recirculation context (otherwise we could have recirculation
1074 * upcalls using recirculation ID for which no context can be
1075 * found). We may still execute the flow's actions even if we
1076 * don't install the flow. */
1077 upcall
->recirc
= recirc_id_node_from_state(xin
.recirc
);
1078 upcall
->have_recirc_ref
= recirc_id_node_try_ref_rcu(upcall
->recirc
);
1081 /* For non-miss upcalls, we are either executing actions (one of which
1082 * is an userspace action) for an upcall, in which case the stats have
1083 * already been taken care of, or there's a flow in the datapath which
1084 * this packet was accounted to. Presumably the revalidators will deal
1085 * with pushing its stats eventually. */
1088 upcall
->dump_seq
= seq_read(udpif
->dump_seq
);
1089 upcall
->reval_seq
= seq_read(udpif
->reval_seq
);
1090 xlate_actions(&xin
, &upcall
->xout
);
1091 upcall
->xout_initialized
= true;
1093 if (!upcall
->xout
.slow
) {
1094 ofpbuf_use_const(&upcall
->put_actions
,
1095 odp_actions
->data
, odp_actions
->size
);
1097 /* upcall->put_actions already initialized by upcall_receive(). */
1098 compose_slow_path(udpif
, &upcall
->xout
, upcall
->flow
,
1099 upcall
->flow
->in_port
.odp_port
,
1100 &upcall
->put_actions
);
1103 /* This function is also called for slow-pathed flows. As we are only
1104 * going to create new datapath flows for actual datapath misses, there is
1105 * no point in creating a ukey otherwise. */
1106 if (upcall
->type
== DPIF_UC_MISS
) {
1107 upcall
->ukey
= ukey_create_from_upcall(upcall
, wc
);
1112 upcall_uninit(struct upcall
*upcall
)
1115 if (upcall
->xout_initialized
) {
1116 xlate_out_uninit(&upcall
->xout
);
1118 ofpbuf_uninit(&upcall
->odp_actions
);
1119 ofpbuf_uninit(&upcall
->put_actions
);
1121 if (!upcall
->ukey_persists
) {
1122 ukey_delete__(upcall
->ukey
);
1124 } else if (upcall
->have_recirc_ref
) {
1125 /* The reference was transferred to the ukey if one was created. */
1126 recirc_id_node_unref(upcall
->recirc
);
1132 upcall_cb(const struct dp_packet
*packet
, const struct flow
*flow
, ovs_u128
*ufid
,
1133 unsigned pmd_id
, enum dpif_upcall_type type
,
1134 const struct nlattr
*userdata
, struct ofpbuf
*actions
,
1135 struct flow_wildcards
*wc
, struct ofpbuf
*put_actions
, void *aux
)
1137 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
1138 struct udpif
*udpif
= aux
;
1139 unsigned int flow_limit
;
1140 struct upcall upcall
;
1144 atomic_read_relaxed(&enable_megaflows
, &megaflow
);
1145 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
1147 error
= upcall_receive(&upcall
, udpif
->backer
, packet
, type
, userdata
,
1148 flow
, 0, ufid
, pmd_id
);
1153 error
= process_upcall(udpif
, &upcall
, actions
, wc
);
1158 if (upcall
.xout
.slow
&& put_actions
) {
1159 ofpbuf_put(put_actions
, upcall
.put_actions
.data
,
1160 upcall
.put_actions
.size
);
1163 if (OVS_UNLIKELY(!megaflow
)) {
1164 flow_wildcards_init_for_packet(wc
, flow
);
1167 if (udpif_get_n_flows(udpif
) >= flow_limit
) {
1168 VLOG_WARN_RL(&rl
, "upcall_cb failure: datapath flow limit reached");
1173 /* Prevent miss flow installation if the key has recirculation ID but we
1174 * were not able to get a reference on it. */
1175 if (type
== DPIF_UC_MISS
&& upcall
.recirc
&& !upcall
.have_recirc_ref
) {
1176 VLOG_WARN_RL(&rl
, "upcall_cb failure: no reference for recirc flow");
1181 if (upcall
.ukey
&& !ukey_install(udpif
, upcall
.ukey
)) {
1182 VLOG_WARN_RL(&rl
, "upcall_cb failure: ukey installation fails");
1187 upcall
.ukey_persists
= true;
1189 upcall_uninit(&upcall
);
1194 process_upcall(struct udpif
*udpif
, struct upcall
*upcall
,
1195 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
1197 const struct nlattr
*userdata
= upcall
->userdata
;
1198 const struct dp_packet
*packet
= upcall
->packet
;
1199 const struct flow
*flow
= upcall
->flow
;
1201 switch (classify_upcall(upcall
->type
, userdata
)) {
1203 upcall_xlate(udpif
, upcall
, odp_actions
, wc
);
1207 if (upcall
->sflow
) {
1208 union user_action_cookie cookie
;
1209 const struct nlattr
*actions
;
1210 size_t actions_len
= 0;
1211 struct dpif_sflow_actions sflow_actions
;
1212 memset(&sflow_actions
, 0, sizeof sflow_actions
);
1213 memset(&cookie
, 0, sizeof cookie
);
1214 memcpy(&cookie
, nl_attr_get(userdata
), sizeof cookie
.sflow
);
1215 if (upcall
->actions
) {
1216 /* Actions were passed up from datapath. */
1217 actions
= nl_attr_get(upcall
->actions
);
1218 actions_len
= nl_attr_get_size(upcall
->actions
);
1219 if (actions
&& actions_len
) {
1220 dpif_sflow_read_actions(flow
, actions
, actions_len
,
1224 if (actions_len
== 0) {
1225 /* Lookup actions in userspace cache. */
1226 struct udpif_key
*ukey
= ukey_lookup(udpif
, upcall
->ufid
);
1228 ukey_get_actions(ukey
, &actions
, &actions_len
);
1229 dpif_sflow_read_actions(flow
, actions
, actions_len
,
1233 dpif_sflow_received(upcall
->sflow
, packet
, flow
,
1234 flow
->in_port
.odp_port
, &cookie
,
1235 actions_len
> 0 ? &sflow_actions
: NULL
);
1240 if (upcall
->ipfix
) {
1241 union user_action_cookie cookie
;
1242 struct flow_tnl output_tunnel_key
;
1244 memset(&cookie
, 0, sizeof cookie
);
1245 memcpy(&cookie
, nl_attr_get(userdata
), sizeof cookie
.ipfix
);
1247 if (upcall
->out_tun_key
) {
1248 odp_tun_key_from_attr(upcall
->out_tun_key
, false,
1249 &output_tunnel_key
);
1251 dpif_ipfix_bridge_sample(upcall
->ipfix
, packet
, flow
,
1252 flow
->in_port
.odp_port
,
1253 cookie
.ipfix
.output_odp_port
,
1254 upcall
->out_tun_key
?
1255 &output_tunnel_key
: NULL
);
1259 case FLOW_SAMPLE_UPCALL
:
1260 if (upcall
->ipfix
) {
1261 union user_action_cookie cookie
;
1263 memset(&cookie
, 0, sizeof cookie
);
1264 memcpy(&cookie
, nl_attr_get(userdata
), sizeof cookie
.flow_sample
);
1266 /* The flow reflects exactly the contents of the packet.
1267 * Sample the packet using it. */
1268 dpif_ipfix_flow_sample(upcall
->ipfix
, packet
, flow
,
1269 cookie
.flow_sample
.collector_set_id
,
1270 cookie
.flow_sample
.probability
,
1271 cookie
.flow_sample
.obs_domain_id
,
1272 cookie
.flow_sample
.obs_point_id
);
1284 handle_upcalls(struct udpif
*udpif
, struct upcall
*upcalls
,
1287 struct dpif_op
*opsp
[UPCALL_MAX_BATCH
* 2];
1288 struct ukey_op ops
[UPCALL_MAX_BATCH
* 2];
1289 unsigned int flow_limit
;
1290 size_t n_ops
, n_opsp
, i
;
1293 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
1295 may_put
= udpif_get_n_flows(udpif
) < flow_limit
;
1297 /* Handle the packets individually in order of arrival.
1299 * - For SLOW_CFM, SLOW_LACP, SLOW_STP, and SLOW_BFD, translation is what
1300 * processes received packets for these protocols.
1302 * - For SLOW_CONTROLLER, translation sends the packet to the OpenFlow
1305 * The loop fills 'ops' with an array of operations to execute in the
1308 for (i
= 0; i
< n_upcalls
; i
++) {
1309 struct upcall
*upcall
= &upcalls
[i
];
1310 const struct dp_packet
*packet
= upcall
->packet
;
1313 if (upcall
->vsp_adjusted
) {
1314 /* This packet was received on a VLAN splinter port. We added a
1315 * VLAN to the packet to make the packet resemble the flow, but the
1316 * actions were composed assuming that the packet contained no
1317 * VLAN. So, we must remove the VLAN header from the packet before
1318 * trying to execute the actions. */
1319 if (upcall
->odp_actions
.size
) {
1320 eth_pop_vlan(CONST_CAST(struct dp_packet
*, upcall
->packet
));
1323 /* Remove the flow vlan tags inserted by vlan splinter logic
1324 * to ensure megaflow masks generated match the data path flow. */
1325 CONST_CAST(struct flow
*, upcall
->flow
)->vlan_tci
= 0;
1328 /* Do not install a flow into the datapath if:
1330 * - The datapath already has too many flows.
1332 * - We received this packet via some flow installed in the kernel
1335 * - Upcall was a recirculation but we do not have a reference to
1336 * to the recirculation ID. */
1337 if (may_put
&& upcall
->type
== DPIF_UC_MISS
&&
1338 (!upcall
->recirc
|| upcall
->have_recirc_ref
)) {
1339 struct udpif_key
*ukey
= upcall
->ukey
;
1341 upcall
->ukey_persists
= true;
1345 op
->dop
.type
= DPIF_OP_FLOW_PUT
;
1346 op
->dop
.u
.flow_put
.flags
= DPIF_FP_CREATE
;
1347 op
->dop
.u
.flow_put
.key
= ukey
->key
;
1348 op
->dop
.u
.flow_put
.key_len
= ukey
->key_len
;
1349 op
->dop
.u
.flow_put
.mask
= ukey
->mask
;
1350 op
->dop
.u
.flow_put
.mask_len
= ukey
->mask_len
;
1351 op
->dop
.u
.flow_put
.ufid
= upcall
->ufid
;
1352 op
->dop
.u
.flow_put
.stats
= NULL
;
1353 ukey_get_actions(ukey
, &op
->dop
.u
.flow_put
.actions
,
1354 &op
->dop
.u
.flow_put
.actions_len
);
1357 if (upcall
->odp_actions
.size
) {
1360 op
->dop
.type
= DPIF_OP_EXECUTE
;
1361 op
->dop
.u
.execute
.packet
= CONST_CAST(struct dp_packet
*, packet
);
1362 odp_key_to_pkt_metadata(upcall
->key
, upcall
->key_len
,
1363 &op
->dop
.u
.execute
.packet
->md
);
1364 op
->dop
.u
.execute
.actions
= upcall
->odp_actions
.data
;
1365 op
->dop
.u
.execute
.actions_len
= upcall
->odp_actions
.size
;
1366 op
->dop
.u
.execute
.needs_help
= (upcall
->xout
.slow
& SLOW_ACTION
) != 0;
1367 op
->dop
.u
.execute
.probe
= false;
1368 op
->dop
.u
.execute
.mtu
= upcall
->mru
;
1374 * We install ukeys before installing the flows, locking them for exclusive
1375 * access by this thread for the period of installation. This ensures that
1376 * other threads won't attempt to delete the flows as we are creating them.
1379 for (i
= 0; i
< n_ops
; i
++) {
1380 struct udpif_key
*ukey
= ops
[i
].ukey
;
1383 /* If we can't install the ukey, don't install the flow. */
1384 if (!ukey_install_start(udpif
, ukey
)) {
1385 ukey_delete__(ukey
);
1390 opsp
[n_opsp
++] = &ops
[i
].dop
;
1392 dpif_operate(udpif
->dpif
, opsp
, n_opsp
);
1393 for (i
= 0; i
< n_ops
; i
++) {
1395 ukey_install_finish(ops
[i
].ukey
, ops
[i
].dop
.error
);
1401 get_ufid_hash(const ovs_u128
*ufid
)
1403 return ufid
->u32
[0];
1406 static struct udpif_key
*
1407 ukey_lookup(struct udpif
*udpif
, const ovs_u128
*ufid
)
1409 struct udpif_key
*ukey
;
1410 int idx
= get_ufid_hash(ufid
) % N_UMAPS
;
1411 struct cmap
*cmap
= &udpif
->ukeys
[idx
].cmap
;
1413 CMAP_FOR_EACH_WITH_HASH (ukey
, cmap_node
, get_ufid_hash(ufid
), cmap
) {
1414 if (ovs_u128_equals(&ukey
->ufid
, ufid
)) {
1421 /* Provides safe lockless access of RCU protected 'ukey->actions'. Callers may
1422 * alternatively access the field directly if they take 'ukey->mutex'. */
1424 ukey_get_actions(struct udpif_key
*ukey
, const struct nlattr
**actions
, size_t *size
)
1426 const struct ofpbuf
*buf
= ovsrcu_get(struct ofpbuf
*, &ukey
->actions
);
1427 *actions
= buf
->data
;
1432 ukey_set_actions(struct udpif_key
*ukey
, const struct ofpbuf
*actions
)
1434 ovsrcu_postpone(ofpbuf_delete
,
1435 ovsrcu_get_protected(struct ofpbuf
*, &ukey
->actions
));
1436 ovsrcu_set(&ukey
->actions
, ofpbuf_clone(actions
));
1439 static struct udpif_key
*
1440 ukey_create__(const struct nlattr
*key
, size_t key_len
,
1441 const struct nlattr
*mask
, size_t mask_len
,
1442 bool ufid_present
, const ovs_u128
*ufid
,
1443 const unsigned pmd_id
, const struct ofpbuf
*actions
,
1444 uint64_t dump_seq
, uint64_t reval_seq
, long long int used
,
1445 uint32_t key_recirc_id
, struct xlate_out
*xout
)
1446 OVS_NO_THREAD_SAFETY_ANALYSIS
1448 struct udpif_key
*ukey
= xmalloc(sizeof *ukey
);
1450 memcpy(&ukey
->keybuf
, key
, key_len
);
1451 ukey
->key
= &ukey
->keybuf
.nla
;
1452 ukey
->key_len
= key_len
;
1453 memcpy(&ukey
->maskbuf
, mask
, mask_len
);
1454 ukey
->mask
= &ukey
->maskbuf
.nla
;
1455 ukey
->mask_len
= mask_len
;
1456 ukey
->ufid_present
= ufid_present
;
1458 ukey
->pmd_id
= pmd_id
;
1459 ukey
->hash
= get_ufid_hash(&ukey
->ufid
);
1461 ovsrcu_init(&ukey
->actions
, NULL
);
1462 ukey_set_actions(ukey
, actions
);
1464 ovs_mutex_init(&ukey
->mutex
);
1465 ukey
->dump_seq
= dump_seq
;
1466 ukey
->reval_seq
= reval_seq
;
1467 ukey
->flow_exists
= false;
1468 ukey
->created
= time_msec();
1469 memset(&ukey
->stats
, 0, sizeof ukey
->stats
);
1470 ukey
->stats
.used
= used
;
1471 ukey
->xcache
= NULL
;
1473 ukey
->key_recirc_id
= key_recirc_id
;
1474 recirc_refs_init(&ukey
->recircs
);
1476 /* Take ownership of the action recirc id references. */
1477 recirc_refs_swap(&ukey
->recircs
, &xout
->recircs
);
1483 static struct udpif_key
*
1484 ukey_create_from_upcall(struct upcall
*upcall
, struct flow_wildcards
*wc
)
1486 struct odputil_keybuf keystub
, maskstub
;
1487 struct ofpbuf keybuf
, maskbuf
;
1489 struct odp_flow_key_parms odp_parms
= {
1490 .flow
= upcall
->flow
,
1494 odp_parms
.support
= ofproto_dpif_get_support(upcall
->ofproto
)->odp
;
1495 if (upcall
->key_len
) {
1496 ofpbuf_use_const(&keybuf
, upcall
->key
, upcall
->key_len
);
1498 /* dpif-netdev doesn't provide a netlink-formatted flow key in the
1499 * upcall, so convert the upcall's flow here. */
1500 ofpbuf_use_stack(&keybuf
, &keystub
, sizeof keystub
);
1501 odp_parms
.odp_in_port
= upcall
->flow
->in_port
.odp_port
;
1502 odp_flow_key_from_flow(&odp_parms
, &keybuf
);
1505 atomic_read_relaxed(&enable_megaflows
, &megaflow
);
1506 ofpbuf_use_stack(&maskbuf
, &maskstub
, sizeof maskstub
);
1508 odp_parms
.odp_in_port
= ODPP_NONE
;
1509 odp_parms
.key_buf
= &keybuf
;
1511 odp_flow_key_from_mask(&odp_parms
, &maskbuf
);
1514 return ukey_create__(keybuf
.data
, keybuf
.size
, maskbuf
.data
, maskbuf
.size
,
1515 true, upcall
->ufid
, upcall
->pmd_id
,
1516 &upcall
->put_actions
, upcall
->dump_seq
,
1517 upcall
->reval_seq
, 0,
1518 upcall
->have_recirc_ref
? upcall
->recirc
->id
: 0,
1523 ukey_create_from_dpif_flow(const struct udpif
*udpif
,
1524 const struct dpif_flow
*flow
,
1525 struct udpif_key
**ukey
)
1527 struct dpif_flow full_flow
;
1528 struct ofpbuf actions
;
1529 uint64_t dump_seq
, reval_seq
;
1530 uint64_t stub
[DPIF_FLOW_BUFSIZE
/ 8];
1531 const struct nlattr
*a
;
1534 if (!flow
->key_len
|| !flow
->actions_len
) {
1538 /* If the key or actions were not provided by the datapath, fetch the
1540 ofpbuf_use_stack(&buf
, &stub
, sizeof stub
);
1541 err
= dpif_flow_get(udpif
->dpif
, NULL
, 0, &flow
->ufid
,
1542 flow
->pmd_id
, &buf
, &full_flow
);
1549 /* Check the flow actions for recirculation action. As recirculation
1550 * relies on OVS userspace internal state, we need to delete all old
1551 * datapath flows with either a non-zero recirc_id in the key, or any
1552 * recirculation actions upon OVS restart. */
1553 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, flow
->key
, flow
->key_len
) {
1554 if (nl_attr_type(a
) == OVS_KEY_ATTR_RECIRC_ID
1555 && nl_attr_get_u32(a
) != 0) {
1559 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, flow
->actions
, flow
->actions_len
) {
1560 if (nl_attr_type(a
) == OVS_ACTION_ATTR_RECIRC
) {
1565 dump_seq
= seq_read(udpif
->dump_seq
);
1566 reval_seq
= seq_read(udpif
->reval_seq
);
1567 ofpbuf_use_const(&actions
, &flow
->actions
, flow
->actions_len
);
1568 *ukey
= ukey_create__(flow
->key
, flow
->key_len
,
1569 flow
->mask
, flow
->mask_len
, flow
->ufid_present
,
1570 &flow
->ufid
, flow
->pmd_id
, &actions
, dump_seq
,
1571 reval_seq
, flow
->stats
.used
, 0, NULL
);
1576 /* Attempts to insert a ukey into the shared ukey maps.
1578 * On success, returns true, installs the ukey and returns it in a locked
1579 * state. Otherwise, returns false. */
1581 ukey_install_start(struct udpif
*udpif
, struct udpif_key
*new_ukey
)
1582 OVS_TRY_LOCK(true, new_ukey
->mutex
)
1585 struct udpif_key
*old_ukey
;
1587 bool locked
= false;
1589 idx
= new_ukey
->hash
% N_UMAPS
;
1590 umap
= &udpif
->ukeys
[idx
];
1591 ovs_mutex_lock(&umap
->mutex
);
1592 old_ukey
= ukey_lookup(udpif
, &new_ukey
->ufid
);
1594 /* Uncommon case: A ukey is already installed with the same UFID. */
1595 if (old_ukey
->key_len
== new_ukey
->key_len
1596 && !memcmp(old_ukey
->key
, new_ukey
->key
, new_ukey
->key_len
)) {
1597 COVERAGE_INC(handler_duplicate_upcall
);
1599 struct ds ds
= DS_EMPTY_INITIALIZER
;
1601 odp_format_ufid(&old_ukey
->ufid
, &ds
);
1602 ds_put_cstr(&ds
, " ");
1603 odp_flow_key_format(old_ukey
->key
, old_ukey
->key_len
, &ds
);
1604 ds_put_cstr(&ds
, "\n");
1605 odp_format_ufid(&new_ukey
->ufid
, &ds
);
1606 ds_put_cstr(&ds
, " ");
1607 odp_flow_key_format(new_ukey
->key
, new_ukey
->key_len
, &ds
);
1609 VLOG_WARN_RL(&rl
, "Conflicting ukey for flows:\n%s", ds_cstr(&ds
));
1613 ovs_mutex_lock(&new_ukey
->mutex
);
1614 cmap_insert(&umap
->cmap
, &new_ukey
->cmap_node
, new_ukey
->hash
);
1617 ovs_mutex_unlock(&umap
->mutex
);
1623 ukey_install_finish__(struct udpif_key
*ukey
) OVS_REQUIRES(ukey
->mutex
)
1625 ukey
->flow_exists
= true;
1629 ukey_install_finish(struct udpif_key
*ukey
, int error
)
1630 OVS_RELEASES(ukey
->mutex
)
1633 ukey_install_finish__(ukey
);
1635 ovs_mutex_unlock(&ukey
->mutex
);
1641 ukey_install(struct udpif
*udpif
, struct udpif_key
*ukey
)
1643 /* The usual way to keep 'ukey->flow_exists' in sync with the datapath is
1644 * to call ukey_install_start(), install the corresponding datapath flow,
1645 * then call ukey_install_finish(). The netdev interface using upcall_cb()
1646 * doesn't provide a function to separately finish the flow installation,
1647 * so we perform the operations together here.
1649 * This is fine currently, as revalidator threads will only delete this
1650 * ukey during revalidator_sweep() and only if the dump_seq is mismatched.
1651 * It is unlikely for a revalidator thread to advance dump_seq and reach
1652 * the next GC phase between ukey creation and flow installation. */
1653 return ukey_install_start(udpif
, ukey
) && ukey_install_finish(ukey
, 0);
1656 /* Searches for a ukey in 'udpif->ukeys' that matches 'flow' and attempts to
1657 * lock the ukey. If the ukey does not exist, create it.
1659 * Returns 0 on success, setting *result to the matching ukey and returning it
1660 * in a locked state. Otherwise, returns an errno and clears *result. EBUSY
1661 * indicates that another thread is handling this flow. Other errors indicate
1662 * an unexpected condition creating a new ukey.
1664 * *error is an output parameter provided to appease the threadsafety analyser,
1665 * and its value matches the return value. */
1667 ukey_acquire(struct udpif
*udpif
, const struct dpif_flow
*flow
,
1668 struct udpif_key
**result
, int *error
)
1669 OVS_TRY_LOCK(0, (*result
)->mutex
)
1671 struct udpif_key
*ukey
;
1674 ukey
= ukey_lookup(udpif
, &flow
->ufid
);
1676 retval
= ovs_mutex_trylock(&ukey
->mutex
);
1678 /* Usually we try to avoid installing flows from revalidator threads,
1679 * because locking on a umap may cause handler threads to block.
1680 * However there are certain cases, like when ovs-vswitchd is
1681 * restarted, where it is desirable to handle flows that exist in the
1682 * datapath gracefully (ie, don't just clear the datapath). */
1685 retval
= ukey_create_from_dpif_flow(udpif
, flow
, &ukey
);
1689 install
= ukey_install_start(udpif
, ukey
);
1691 ukey_install_finish__(ukey
);
1694 ukey_delete__(ukey
);
1710 ukey_delete__(struct udpif_key
*ukey
)
1711 OVS_NO_THREAD_SAFETY_ANALYSIS
1714 if (ukey
->key_recirc_id
) {
1715 recirc_free_id(ukey
->key_recirc_id
);
1717 recirc_refs_unref(&ukey
->recircs
);
1718 xlate_cache_delete(ukey
->xcache
);
1719 ofpbuf_delete(ovsrcu_get(struct ofpbuf
*, &ukey
->actions
));
1720 ovs_mutex_destroy(&ukey
->mutex
);
1726 ukey_delete(struct umap
*umap
, struct udpif_key
*ukey
)
1727 OVS_REQUIRES(umap
->mutex
)
1729 cmap_remove(&umap
->cmap
, &ukey
->cmap_node
, ukey
->hash
);
1730 ovsrcu_postpone(ukey_delete__
, ukey
);
1734 should_revalidate(const struct udpif
*udpif
, uint64_t packets
,
1737 long long int metric
, now
, duration
;
1739 if (udpif
->dump_duration
< 200) {
1740 /* We are likely to handle full revalidation for the flows. */
1744 /* Calculate the mean time between seeing these packets. If this
1745 * exceeds the threshold, then delete the flow rather than performing
1746 * costly revalidation for flows that aren't being hit frequently.
1748 * This is targeted at situations where the dump_duration is high (~1s),
1749 * and revalidation is triggered by a call to udpif_revalidate(). In
1750 * these situations, revalidation of all flows causes fluctuations in the
1751 * flow_limit due to the interaction with the dump_duration and max_idle.
1752 * This tends to result in deletion of low-throughput flows anyway, so
1753 * skip the revalidation and just delete those flows. */
1754 packets
= MAX(packets
, 1);
1755 now
= MAX(used
, time_msec());
1756 duration
= now
- used
;
1757 metric
= duration
/ packets
;
1760 /* The flow is receiving more than ~5pps, so keep it. */
1766 /* Verifies that the datapath actions of 'ukey' are still correct, and pushes
1769 * Returns a recommended action for 'ukey', options include:
1770 * UKEY_DELETE The ukey should be deleted.
1771 * UKEY_KEEP The ukey is fine as is.
1772 * UKEY_MODIFY The ukey's actions should be changed but is otherwise
1773 * fine. Callers should change the actions to those found
1774 * in the caller supplied 'odp_actions' buffer. The
1775 * recirculation references can be found in 'recircs' and
1776 * must be handled by the caller.
1778 * If the result is UKEY_MODIFY, then references to all recirc_ids used by the
1779 * new flow will be held within 'recircs' (which may be none).
1781 * The caller is responsible for both initializing 'recircs' prior this call,
1782 * and ensuring any references are eventually freed.
1784 static enum reval_result
1785 revalidate_ukey(struct udpif
*udpif
, struct udpif_key
*ukey
,
1786 const struct dpif_flow_stats
*stats
,
1787 struct ofpbuf
*odp_actions
, uint64_t reval_seq
,
1788 struct recirc_refs
*recircs
)
1789 OVS_REQUIRES(ukey
->mutex
)
1791 struct xlate_out xout
, *xoutp
;
1792 struct netflow
*netflow
;
1793 struct ofproto_dpif
*ofproto
;
1794 struct dpif_flow_stats push
;
1796 struct flow_wildcards dp_mask
, wc
;
1797 enum reval_result result
;
1798 ofp_port_t ofp_in_port
;
1799 struct xlate_in xin
;
1800 long long int last_used
;
1802 bool need_revalidate
;
1804 result
= UKEY_DELETE
;
1808 ofpbuf_clear(odp_actions
);
1809 need_revalidate
= (ukey
->reval_seq
!= reval_seq
);
1810 last_used
= ukey
->stats
.used
;
1811 push
.used
= stats
->used
;
1812 push
.tcp_flags
= stats
->tcp_flags
;
1813 push
.n_packets
= (stats
->n_packets
> ukey
->stats
.n_packets
1814 ? stats
->n_packets
- ukey
->stats
.n_packets
1816 push
.n_bytes
= (stats
->n_bytes
> ukey
->stats
.n_bytes
1817 ? stats
->n_bytes
- ukey
->stats
.n_bytes
1820 if (need_revalidate
&& last_used
1821 && !should_revalidate(udpif
, push
.n_packets
, last_used
)) {
1825 /* We will push the stats, so update the ukey stats cache. */
1826 ukey
->stats
= *stats
;
1827 if (!push
.n_packets
&& !need_revalidate
) {
1832 if (ukey
->xcache
&& !need_revalidate
) {
1833 xlate_push_stats(ukey
->xcache
, &push
);
1838 if (odp_flow_key_to_flow(ukey
->key
, ukey
->key_len
, &flow
)
1843 error
= xlate_lookup(udpif
->backer
, &flow
, &ofproto
, NULL
, NULL
, &netflow
,
1849 if (need_revalidate
) {
1850 xlate_cache_clear(ukey
->xcache
);
1852 if (!ukey
->xcache
) {
1853 ukey
->xcache
= xlate_cache_new();
1856 xlate_in_init(&xin
, ofproto
, &flow
, ofp_in_port
, NULL
, push
.tcp_flags
,
1857 NULL
, need_revalidate
? &wc
: NULL
, odp_actions
);
1858 if (push
.n_packets
) {
1859 xin
.resubmit_stats
= &push
;
1860 xin
.may_learn
= true;
1862 xin
.xcache
= ukey
->xcache
;
1863 xlate_actions(&xin
, &xout
);
1866 if (!need_revalidate
) {
1872 ofpbuf_clear(odp_actions
);
1873 compose_slow_path(udpif
, &xout
, &flow
, flow
.in_port
.odp_port
,
1877 if (odp_flow_key_to_mask(ukey
->mask
, ukey
->mask_len
, ukey
->key
,
1878 ukey
->key_len
, &dp_mask
, &flow
)
1883 /* Do not modify if any bit is wildcarded by the installed datapath flow,
1884 * but not the newly revalidated wildcard mask (wc), i.e., if revalidation
1885 * tells that the datapath flow is now too generic and must be narrowed
1886 * down. Note that we do not know if the datapath has ignored any of the
1887 * wildcarded bits, so we may be overtly conservative here. */
1888 if (flow_wildcards_has_extra(&dp_mask
, &wc
)) {
1892 if (!ofpbuf_equal(odp_actions
,
1893 ovsrcu_get(struct ofpbuf
*, &ukey
->actions
))) {
1894 /* The datapath mask was OK, but the actions seem to have changed.
1895 * Let's modify it in place. */
1896 result
= UKEY_MODIFY
;
1897 /* Transfer recirc action ID references to the caller. */
1898 recirc_refs_swap(recircs
, &xoutp
->recircs
);
1905 if (result
!= UKEY_DELETE
) {
1906 ukey
->reval_seq
= reval_seq
;
1908 if (netflow
&& result
== UKEY_DELETE
) {
1909 netflow_flow_clear(netflow
, &flow
);
1911 xlate_out_uninit(xoutp
);
1916 delete_op_init__(struct udpif
*udpif
, struct ukey_op
*op
,
1917 const struct dpif_flow
*flow
)
1920 op
->dop
.type
= DPIF_OP_FLOW_DEL
;
1921 op
->dop
.u
.flow_del
.key
= flow
->key
;
1922 op
->dop
.u
.flow_del
.key_len
= flow
->key_len
;
1923 op
->dop
.u
.flow_del
.ufid
= flow
->ufid_present
? &flow
->ufid
: NULL
;
1924 op
->dop
.u
.flow_del
.pmd_id
= flow
->pmd_id
;
1925 op
->dop
.u
.flow_del
.stats
= &op
->stats
;
1926 op
->dop
.u
.flow_del
.terse
= udpif_use_ufid(udpif
);
1930 delete_op_init(struct udpif
*udpif
, struct ukey_op
*op
, struct udpif_key
*ukey
)
1933 op
->dop
.type
= DPIF_OP_FLOW_DEL
;
1934 op
->dop
.u
.flow_del
.key
= ukey
->key
;
1935 op
->dop
.u
.flow_del
.key_len
= ukey
->key_len
;
1936 op
->dop
.u
.flow_del
.ufid
= ukey
->ufid_present
? &ukey
->ufid
: NULL
;
1937 op
->dop
.u
.flow_del
.pmd_id
= ukey
->pmd_id
;
1938 op
->dop
.u
.flow_del
.stats
= &op
->stats
;
1939 op
->dop
.u
.flow_del
.terse
= udpif_use_ufid(udpif
);
1943 modify_op_init(struct ukey_op
*op
, struct udpif_key
*ukey
)
1946 op
->dop
.type
= DPIF_OP_FLOW_PUT
;
1947 op
->dop
.u
.flow_put
.flags
= DPIF_FP_MODIFY
;
1948 op
->dop
.u
.flow_put
.key
= ukey
->key
;
1949 op
->dop
.u
.flow_put
.key_len
= ukey
->key_len
;
1950 op
->dop
.u
.flow_put
.mask
= ukey
->mask
;
1951 op
->dop
.u
.flow_put
.mask_len
= ukey
->mask_len
;
1952 op
->dop
.u
.flow_put
.ufid
= &ukey
->ufid
;
1953 op
->dop
.u
.flow_put
.pmd_id
= ukey
->pmd_id
;
1954 op
->dop
.u
.flow_put
.stats
= NULL
;
1955 ukey_get_actions(ukey
, &op
->dop
.u
.flow_put
.actions
,
1956 &op
->dop
.u
.flow_put
.actions_len
);
1959 /* Executes datapath operations 'ops' and attributes stats retrieved from the
1960 * datapath as part of those operations. */
1962 push_dp_ops(struct udpif
*udpif
, struct ukey_op
*ops
, size_t n_ops
)
1964 struct dpif_op
*opsp
[REVALIDATE_MAX_BATCH
];
1967 ovs_assert(n_ops
<= REVALIDATE_MAX_BATCH
);
1968 for (i
= 0; i
< n_ops
; i
++) {
1969 opsp
[i
] = &ops
[i
].dop
;
1971 dpif_operate(udpif
->dpif
, opsp
, n_ops
);
1973 for (i
= 0; i
< n_ops
; i
++) {
1974 struct ukey_op
*op
= &ops
[i
];
1975 struct dpif_flow_stats
*push
, *stats
, push_buf
;
1977 stats
= op
->dop
.u
.flow_del
.stats
;
1980 if (op
->dop
.type
!= DPIF_OP_FLOW_DEL
) {
1981 /* Only deleted flows need their stats pushed. */
1985 if (op
->dop
.error
) {
1986 /* flow_del error, 'stats' is unusable. */
1991 ovs_mutex_lock(&op
->ukey
->mutex
);
1992 push
->used
= MAX(stats
->used
, op
->ukey
->stats
.used
);
1993 push
->tcp_flags
= stats
->tcp_flags
| op
->ukey
->stats
.tcp_flags
;
1994 push
->n_packets
= stats
->n_packets
- op
->ukey
->stats
.n_packets
;
1995 push
->n_bytes
= stats
->n_bytes
- op
->ukey
->stats
.n_bytes
;
1996 ovs_mutex_unlock(&op
->ukey
->mutex
);
2001 if (push
->n_packets
|| netflow_exists()) {
2002 const struct nlattr
*key
= op
->dop
.u
.flow_del
.key
;
2003 size_t key_len
= op
->dop
.u
.flow_del
.key_len
;
2004 struct ofproto_dpif
*ofproto
;
2005 struct netflow
*netflow
;
2006 ofp_port_t ofp_in_port
;
2011 ovs_mutex_lock(&op
->ukey
->mutex
);
2012 if (op
->ukey
->xcache
) {
2013 xlate_push_stats(op
->ukey
->xcache
, push
);
2014 ovs_mutex_unlock(&op
->ukey
->mutex
);
2017 ovs_mutex_unlock(&op
->ukey
->mutex
);
2018 key
= op
->ukey
->key
;
2019 key_len
= op
->ukey
->key_len
;
2022 if (odp_flow_key_to_flow(key
, key_len
, &flow
)
2027 error
= xlate_lookup(udpif
->backer
, &flow
, &ofproto
, NULL
, NULL
,
2028 &netflow
, &ofp_in_port
);
2030 struct xlate_in xin
;
2032 xlate_in_init(&xin
, ofproto
, &flow
, ofp_in_port
, NULL
,
2033 push
->tcp_flags
, NULL
, NULL
, NULL
);
2034 xin
.resubmit_stats
= push
->n_packets
? push
: NULL
;
2035 xin
.may_learn
= push
->n_packets
> 0;
2036 xlate_actions_for_side_effects(&xin
);
2039 netflow_flow_clear(netflow
, &flow
);
2046 /* Executes datapath operations 'ops', attributes stats retrieved from the
2047 * datapath, and deletes ukeys corresponding to deleted flows. */
2049 push_ukey_ops(struct udpif
*udpif
, struct umap
*umap
,
2050 struct ukey_op
*ops
, size_t n_ops
)
2054 push_dp_ops(udpif
, ops
, n_ops
);
2055 ovs_mutex_lock(&umap
->mutex
);
2056 for (i
= 0; i
< n_ops
; i
++) {
2057 if (ops
[i
].dop
.type
== DPIF_OP_FLOW_DEL
) {
2058 ukey_delete(umap
, ops
[i
].ukey
);
2061 ovs_mutex_unlock(&umap
->mutex
);
2065 log_unexpected_flow(const struct dpif_flow
*flow
, int error
)
2067 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 60);
2068 struct ds ds
= DS_EMPTY_INITIALIZER
;
2070 ds_put_format(&ds
, "Failed to acquire udpif_key corresponding to "
2071 "unexpected flow (%s): ", ovs_strerror(error
));
2072 odp_format_ufid(&flow
->ufid
, &ds
);
2073 VLOG_WARN_RL(&rl
, "%s", ds_cstr(&ds
));
2077 reval_op_init(struct ukey_op
*op
, enum reval_result result
,
2078 struct udpif
*udpif
, struct udpif_key
*ukey
,
2079 struct recirc_refs
*recircs
, struct ofpbuf
*odp_actions
)
2081 if (result
== UKEY_DELETE
) {
2082 delete_op_init(udpif
, op
, ukey
);
2083 } else if (result
== UKEY_MODIFY
) {
2084 /* Store the new recircs. */
2085 recirc_refs_swap(&ukey
->recircs
, recircs
);
2086 /* Release old recircs. */
2087 recirc_refs_unref(recircs
);
2088 /* ukey->key_recirc_id remains, as the key is the same as before. */
2090 ukey_set_actions(ukey
, odp_actions
);
2091 modify_op_init(op
, ukey
);
2096 revalidate(struct revalidator
*revalidator
)
2098 uint64_t odp_actions_stub
[1024 / 8];
2099 struct ofpbuf odp_actions
= OFPBUF_STUB_INITIALIZER(odp_actions_stub
);
2101 struct udpif
*udpif
= revalidator
->udpif
;
2102 struct dpif_flow_dump_thread
*dump_thread
;
2103 uint64_t dump_seq
, reval_seq
;
2104 unsigned int flow_limit
;
2106 dump_seq
= seq_read(udpif
->dump_seq
);
2107 reval_seq
= seq_read(udpif
->reval_seq
);
2108 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
2109 dump_thread
= dpif_flow_dump_thread_create(udpif
->dump
);
2111 struct ukey_op ops
[REVALIDATE_MAX_BATCH
];
2114 struct dpif_flow flows
[REVALIDATE_MAX_BATCH
];
2115 const struct dpif_flow
*f
;
2118 long long int max_idle
;
2123 n_dumped
= dpif_flow_dump_next(dump_thread
, flows
, ARRAY_SIZE(flows
));
2130 /* In normal operation we want to keep flows around until they have
2131 * been idle for 'ofproto_max_idle' milliseconds. However:
2133 * - If the number of datapath flows climbs above 'flow_limit',
2134 * drop that down to 100 ms to try to bring the flows down to
2137 * - If the number of datapath flows climbs above twice
2138 * 'flow_limit', delete all the datapath flows as an emergency
2139 * measure. (We reassess this condition for the next batch of
2140 * datapath flows, so we will recover before all the flows are
2142 n_dp_flows
= udpif_get_n_flows(udpif
);
2143 kill_them_all
= n_dp_flows
> flow_limit
* 2;
2144 max_idle
= n_dp_flows
> flow_limit
? 100 : ofproto_max_idle
;
2146 for (f
= flows
; f
< &flows
[n_dumped
]; f
++) {
2147 long long int used
= f
->stats
.used
;
2148 struct recirc_refs recircs
= RECIRC_REFS_EMPTY_INITIALIZER
;
2149 enum reval_result result
;
2150 struct udpif_key
*ukey
;
2151 bool already_dumped
;
2154 if (ukey_acquire(udpif
, f
, &ukey
, &error
)) {
2155 if (error
== EBUSY
) {
2156 /* Another thread is processing this flow, so don't bother
2158 COVERAGE_INC(upcall_ukey_contention
);
2160 log_unexpected_flow(f
, error
);
2161 if (error
!= ENOENT
) {
2162 delete_op_init__(udpif
, &ops
[n_ops
++], f
);
2168 already_dumped
= ukey
->dump_seq
== dump_seq
;
2169 if (already_dumped
) {
2170 /* The flow has already been handled during this flow dump
2171 * operation. Skip it. */
2173 COVERAGE_INC(dumped_duplicate_flow
);
2175 COVERAGE_INC(dumped_new_flow
);
2177 ovs_mutex_unlock(&ukey
->mutex
);
2182 used
= ukey
->created
;
2184 if (kill_them_all
|| (used
&& used
< now
- max_idle
)) {
2185 result
= UKEY_DELETE
;
2187 result
= revalidate_ukey(udpif
, ukey
, &f
->stats
, &odp_actions
,
2188 reval_seq
, &recircs
);
2190 ukey
->dump_seq
= dump_seq
;
2191 ukey
->flow_exists
= result
!= UKEY_DELETE
;
2193 if (result
!= UKEY_KEEP
) {
2194 /* Takes ownership of 'recircs'. */
2195 reval_op_init(&ops
[n_ops
++], result
, udpif
, ukey
, &recircs
,
2198 ovs_mutex_unlock(&ukey
->mutex
);
2202 /* Push datapath ops but defer ukey deletion to 'sweep' phase. */
2203 push_dp_ops(udpif
, ops
, n_ops
);
2207 dpif_flow_dump_thread_destroy(dump_thread
);
2208 ofpbuf_uninit(&odp_actions
);
2211 /* Pauses the 'revalidator', can only proceed after main thread
2212 * calls udpif_resume_revalidators(). */
2214 revalidator_pause(struct revalidator
*revalidator
)
2216 /* The first block is for sync'ing the pause with main thread. */
2217 ovs_barrier_block(&revalidator
->udpif
->pause_barrier
);
2218 /* The second block is for pausing until main thread resumes. */
2219 ovs_barrier_block(&revalidator
->udpif
->pause_barrier
);
2223 revalidator_sweep__(struct revalidator
*revalidator
, bool purge
)
2225 struct udpif
*udpif
;
2226 uint64_t dump_seq
, reval_seq
;
2229 udpif
= revalidator
->udpif
;
2230 dump_seq
= seq_read(udpif
->dump_seq
);
2231 reval_seq
= seq_read(udpif
->reval_seq
);
2232 slice
= revalidator
- udpif
->revalidators
;
2233 ovs_assert(slice
< udpif
->n_revalidators
);
2235 for (int i
= slice
; i
< N_UMAPS
; i
+= udpif
->n_revalidators
) {
2236 uint64_t odp_actions_stub
[1024 / 8];
2237 struct ofpbuf odp_actions
= OFPBUF_STUB_INITIALIZER(odp_actions_stub
);
2239 struct ukey_op ops
[REVALIDATE_MAX_BATCH
];
2240 struct udpif_key
*ukey
;
2241 struct umap
*umap
= &udpif
->ukeys
[i
];
2244 CMAP_FOR_EACH(ukey
, cmap_node
, &umap
->cmap
) {
2247 /* Handler threads could be holding a ukey lock while it installs a
2248 * new flow, so don't hang around waiting for access to it. */
2249 if (ovs_mutex_trylock(&ukey
->mutex
)) {
2252 flow_exists
= ukey
->flow_exists
;
2254 struct recirc_refs recircs
= RECIRC_REFS_EMPTY_INITIALIZER
;
2255 bool seq_mismatch
= (ukey
->dump_seq
!= dump_seq
2256 && ukey
->reval_seq
!= reval_seq
);
2257 enum reval_result result
;
2260 result
= UKEY_DELETE
;
2261 } else if (!seq_mismatch
) {
2264 struct dpif_flow_stats stats
;
2265 COVERAGE_INC(revalidate_missed_dp_flow
);
2266 memset(&stats
, 0, sizeof stats
);
2267 result
= revalidate_ukey(udpif
, ukey
, &stats
, &odp_actions
,
2268 reval_seq
, &recircs
);
2270 if (result
!= UKEY_KEEP
) {
2271 /* Clears 'recircs' if filled by revalidate_ukey(). */
2272 reval_op_init(&ops
[n_ops
++], result
, udpif
, ukey
, &recircs
,
2276 ovs_mutex_unlock(&ukey
->mutex
);
2279 /* The common flow deletion case involves deletion of the flow
2280 * during the dump phase and ukey deletion here. */
2281 ovs_mutex_lock(&umap
->mutex
);
2282 ukey_delete(umap
, ukey
);
2283 ovs_mutex_unlock(&umap
->mutex
);
2286 if (n_ops
== REVALIDATE_MAX_BATCH
) {
2287 /* Update/delete missed flows and clean up corresponding ukeys
2289 push_ukey_ops(udpif
, umap
, ops
, n_ops
);
2295 push_ukey_ops(udpif
, umap
, ops
, n_ops
);
2298 ofpbuf_uninit(&odp_actions
);
2304 revalidator_sweep(struct revalidator
*revalidator
)
2306 revalidator_sweep__(revalidator
, false);
2310 revalidator_purge(struct revalidator
*revalidator
)
2312 revalidator_sweep__(revalidator
, true);
2315 /* In reaction to dpif purge, purges all 'ukey's with same 'pmd_id'. */
2317 dp_purge_cb(void *aux
, unsigned pmd_id
)
2319 struct udpif
*udpif
= aux
;
2322 udpif_pause_revalidators(udpif
);
2323 for (i
= 0; i
< N_UMAPS
; i
++) {
2324 struct ukey_op ops
[REVALIDATE_MAX_BATCH
];
2325 struct udpif_key
*ukey
;
2326 struct umap
*umap
= &udpif
->ukeys
[i
];
2329 CMAP_FOR_EACH(ukey
, cmap_node
, &umap
->cmap
) {
2330 if (ukey
->pmd_id
== pmd_id
) {
2331 delete_op_init(udpif
, &ops
[n_ops
++], ukey
);
2332 if (n_ops
== REVALIDATE_MAX_BATCH
) {
2333 push_ukey_ops(udpif
, umap
, ops
, n_ops
);
2340 push_ukey_ops(udpif
, umap
, ops
, n_ops
);
2345 udpif_resume_revalidators(udpif
);
2349 upcall_unixctl_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2350 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
2352 struct ds ds
= DS_EMPTY_INITIALIZER
;
2353 struct udpif
*udpif
;
2355 LIST_FOR_EACH (udpif
, list_node
, &all_udpifs
) {
2356 unsigned int flow_limit
;
2360 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
2361 ufid_enabled
= udpif_use_ufid(udpif
);
2363 ds_put_format(&ds
, "%s:\n", dpif_name(udpif
->dpif
));
2364 ds_put_format(&ds
, "\tflows : (current %lu)"
2365 " (avg %u) (max %u) (limit %u)\n", udpif_get_n_flows(udpif
),
2366 udpif
->avg_n_flows
, udpif
->max_n_flows
, flow_limit
);
2367 ds_put_format(&ds
, "\tdump duration : %lldms\n", udpif
->dump_duration
);
2368 ds_put_format(&ds
, "\tufid enabled : ");
2370 ds_put_format(&ds
, "true\n");
2372 ds_put_format(&ds
, "false\n");
2374 ds_put_char(&ds
, '\n');
2376 for (i
= 0; i
< n_revalidators
; i
++) {
2377 struct revalidator
*revalidator
= &udpif
->revalidators
[i
];
2378 int j
, elements
= 0;
2380 for (j
= i
; j
< N_UMAPS
; j
+= n_revalidators
) {
2381 elements
+= cmap_count(&udpif
->ukeys
[j
].cmap
);
2383 ds_put_format(&ds
, "\t%u: (keys %d)\n", revalidator
->id
, elements
);
2387 unixctl_command_reply(conn
, ds_cstr(&ds
));
2391 /* Disable using the megaflows.
2393 * This command is only needed for advanced debugging, so it's not
2394 * documented in the man page. */
2396 upcall_unixctl_disable_megaflows(struct unixctl_conn
*conn
,
2397 int argc OVS_UNUSED
,
2398 const char *argv
[] OVS_UNUSED
,
2399 void *aux OVS_UNUSED
)
2401 atomic_store_relaxed(&enable_megaflows
, false);
2402 udpif_flush_all_datapaths();
2403 unixctl_command_reply(conn
, "megaflows disabled");
2406 /* Re-enable using megaflows.
2408 * This command is only needed for advanced debugging, so it's not
2409 * documented in the man page. */
2411 upcall_unixctl_enable_megaflows(struct unixctl_conn
*conn
,
2412 int argc OVS_UNUSED
,
2413 const char *argv
[] OVS_UNUSED
,
2414 void *aux OVS_UNUSED
)
2416 atomic_store_relaxed(&enable_megaflows
, true);
2417 udpif_flush_all_datapaths();
2418 unixctl_command_reply(conn
, "megaflows enabled");
2421 /* Disable skipping flow attributes during flow dump.
2423 * This command is only needed for advanced debugging, so it's not
2424 * documented in the man page. */
2426 upcall_unixctl_disable_ufid(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2427 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
2429 atomic_store_relaxed(&enable_ufid
, false);
2430 unixctl_command_reply(conn
, "Datapath dumping tersely using UFID disabled");
2433 /* Re-enable skipping flow attributes during flow dump.
2435 * This command is only needed for advanced debugging, so it's not documented
2436 * in the man page. */
2438 upcall_unixctl_enable_ufid(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2439 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
2441 atomic_store_relaxed(&enable_ufid
, true);
2442 unixctl_command_reply(conn
, "Datapath dumping tersely using UFID enabled "
2443 "for supported datapaths");
2446 /* Set the flow limit.
2448 * This command is only needed for advanced debugging, so it's not
2449 * documented in the man page. */
2451 upcall_unixctl_set_flow_limit(struct unixctl_conn
*conn
,
2452 int argc OVS_UNUSED
,
2453 const char *argv
[] OVS_UNUSED
,
2454 void *aux OVS_UNUSED
)
2456 struct ds ds
= DS_EMPTY_INITIALIZER
;
2457 struct udpif
*udpif
;
2458 unsigned int flow_limit
= atoi(argv
[1]);
2460 LIST_FOR_EACH (udpif
, list_node
, &all_udpifs
) {
2461 atomic_store_relaxed(&udpif
->flow_limit
, flow_limit
);
2463 ds_put_format(&ds
, "set flow_limit to %u\n", flow_limit
);
2464 unixctl_command_reply(conn
, ds_cstr(&ds
));
2469 upcall_unixctl_dump_wait(struct unixctl_conn
*conn
,
2470 int argc OVS_UNUSED
,
2471 const char *argv
[] OVS_UNUSED
,
2472 void *aux OVS_UNUSED
)
2474 if (list_is_singleton(&all_udpifs
)) {
2475 struct udpif
*udpif
= NULL
;
2478 udpif
= OBJECT_CONTAINING(list_front(&all_udpifs
), udpif
, list_node
);
2479 len
= (udpif
->n_conns
+ 1) * sizeof *udpif
->conns
;
2480 udpif
->conn_seq
= seq_read(udpif
->dump_seq
);
2481 udpif
->conns
= xrealloc(udpif
->conns
, len
);
2482 udpif
->conns
[udpif
->n_conns
++] = conn
;
2484 unixctl_command_reply_error(conn
, "can't wait on multiple udpifs.");
2489 upcall_unixctl_purge(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2490 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
2492 struct udpif
*udpif
;
2494 LIST_FOR_EACH (udpif
, list_node
, &all_udpifs
) {
2497 for (n
= 0; n
< udpif
->n_revalidators
; n
++) {
2498 revalidator_purge(&udpif
->revalidators
[n
]);
2501 unixctl_command_reply(conn
, "");