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 "openvswitch/dynamic-string.h"
27 #include "fail-open.h"
28 #include "guarded-list.h"
30 #include "openvswitch/list.h"
32 #include "openvswitch/ofpbuf.h"
33 #include "ofproto-dpif-ipfix.h"
34 #include "ofproto-dpif-sflow.h"
35 #include "ofproto-dpif-xlate.h"
36 #include "ofproto-dpif-xlate-cache.h"
37 #include "ofproto-dpif-trace.h"
40 #include "openvswitch/poll-loop.h"
44 #include "openvswitch/vlog.h"
46 #define MAX_QUEUE_LENGTH 512
47 #define UPCALL_MAX_BATCH 64
48 #define REVALIDATE_MAX_BATCH 50
50 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_upcall
);
52 COVERAGE_DEFINE(dumped_duplicate_flow
);
53 COVERAGE_DEFINE(dumped_new_flow
);
54 COVERAGE_DEFINE(handler_duplicate_upcall
);
55 COVERAGE_DEFINE(upcall_ukey_contention
);
56 COVERAGE_DEFINE(upcall_ukey_replace
);
57 COVERAGE_DEFINE(revalidate_missed_dp_flow
);
59 /* A thread that reads upcalls from dpif, forwards each upcall's packet,
60 * and possibly sets up a kernel flow as a cache. */
62 struct udpif
*udpif
; /* Parent udpif. */
63 pthread_t thread
; /* Thread ID. */
64 uint32_t handler_id
; /* Handler id. */
67 /* In the absence of a multiple-writer multiple-reader datastructure for
68 * storing udpif_keys ("ukeys"), we use a large number of cmaps, each with its
69 * own lock for writing. */
70 #define N_UMAPS 512 /* per udpif. */
72 struct ovs_mutex mutex
; /* Take for writing to the following. */
73 struct cmap cmap
; /* Datapath flow keys. */
76 /* A thread that processes datapath flows, updates OpenFlow statistics, and
77 * updates or removes them if necessary.
79 * Revalidator threads operate in two phases: "dump" and "sweep". In between
80 * each phase, all revalidators sync up so that all revalidator threads are
81 * either in one phase or the other, but not a combination.
83 * During the dump phase, revalidators fetch flows from the datapath and
84 * attribute the statistics to OpenFlow rules. Each datapath flow has a
85 * corresponding ukey which caches the most recently seen statistics. If
86 * a flow needs to be deleted (for example, because it is unused over a
87 * period of time), revalidator threads may delete the flow during the
88 * dump phase. The datapath is not guaranteed to reliably dump all flows
89 * from the datapath, and there is no mapping between datapath flows to
90 * revalidators, so a particular flow may be handled by zero or more
91 * revalidators during a single dump phase. To avoid duplicate attribution
92 * of statistics, ukeys are never deleted during this phase.
94 * During the sweep phase, each revalidator takes ownership of a different
95 * slice of umaps and sweeps through all ukeys in those umaps to figure out
96 * whether they need to be deleted. During this phase, revalidators may
97 * fetch individual flows which were not dumped during the dump phase to
98 * validate them and attribute statistics.
101 struct udpif
*udpif
; /* Parent udpif. */
102 pthread_t thread
; /* Thread ID. */
103 unsigned int id
; /* ovsthread_id_self(). */
106 /* An upcall handler for ofproto_dpif.
108 * udpif keeps records of two kind of logically separate units:
113 * - An array of 'struct handler's for upcall handling and flow
119 * - Revalidation threads which read the datapath flow table and maintains
123 struct ovs_list list_node
; /* In all_udpifs list. */
125 struct dpif
*dpif
; /* Datapath handle. */
126 struct dpif_backer
*backer
; /* Opaque dpif_backer pointer. */
128 struct handler
*handlers
; /* Upcall handlers. */
131 struct revalidator
*revalidators
; /* Flow revalidators. */
132 size_t n_revalidators
;
134 struct latch exit_latch
; /* Tells child threads to exit. */
137 struct seq
*reval_seq
; /* Incremented to force revalidation. */
138 bool reval_exit
; /* Set by leader on 'exit_latch. */
139 struct ovs_barrier reval_barrier
; /* Barrier used by revalidators. */
140 struct dpif_flow_dump
*dump
; /* DPIF flow dump state. */
141 long long int dump_duration
; /* Duration of the last flow dump. */
142 struct seq
*dump_seq
; /* Increments each dump iteration. */
143 atomic_bool enable_ufid
; /* If true, skip dumping flow attrs. */
145 /* These variables provide a mechanism for the main thread to pause
146 * all revalidation without having to completely shut the threads down.
147 * 'pause_latch' is shared between the main thread and the lead
148 * revalidator thread, so when it is desirable to halt revalidation, the
149 * main thread will set the latch. 'pause' and 'pause_barrier' are shared
150 * by revalidator threads. The lead revalidator will set 'pause' when it
151 * observes the latch has been set, and this will cause all revalidator
152 * threads to wait on 'pause_barrier' at the beginning of the next
153 * revalidation round. */
154 bool pause
; /* Set by leader on 'pause_latch. */
155 struct latch pause_latch
; /* Set to force revalidators pause. */
156 struct ovs_barrier pause_barrier
; /* Barrier used to pause all */
157 /* revalidators by main thread. */
159 /* There are 'N_UMAPS' maps containing 'struct udpif_key' elements.
161 * During the flow dump phase, revalidators insert into these with a random
162 * distribution. During the garbage collection phase, each revalidator
163 * takes care of garbage collecting a slice of these maps. */
166 /* Datapath flow statistics. */
167 unsigned int max_n_flows
;
168 unsigned int avg_n_flows
;
170 /* Following fields are accessed and modified by different threads. */
171 atomic_uint flow_limit
; /* Datapath flow hard limit. */
173 /* n_flows_mutex prevents multiple threads updating these concurrently. */
174 atomic_uint n_flows
; /* Number of flows in the datapath. */
175 atomic_llong n_flows_timestamp
; /* Last time n_flows was updated. */
176 struct ovs_mutex n_flows_mutex
;
178 /* Following fields are accessed and modified only from the main thread. */
179 struct unixctl_conn
**conns
; /* Connections waiting on dump_seq. */
180 uint64_t conn_seq
; /* Corresponds to 'dump_seq' when
181 conns[n_conns-1] was stored. */
182 size_t n_conns
; /* Number of connections waiting. */
186 BAD_UPCALL
, /* Some kind of bug somewhere. */
187 MISS_UPCALL
, /* A flow miss. */
188 SLOW_PATH_UPCALL
, /* Slow path upcall. */
189 SFLOW_UPCALL
, /* sFlow sample. */
190 FLOW_SAMPLE_UPCALL
, /* Per-flow sampling. */
191 IPFIX_UPCALL
, /* Per-bridge sampling. */
192 CONTROLLER_UPCALL
/* Destined for the controller. */
202 struct ofproto_dpif
*ofproto
; /* Parent ofproto. */
203 const struct recirc_id_node
*recirc
; /* Recirculation context. */
204 bool have_recirc_ref
; /* Reference held on recirc ctx? */
206 /* The flow and packet are only required to be constant when using
207 * dpif-netdev. If a modification is absolutely necessary, a const cast
208 * may be used with other datapaths. */
209 const struct flow
*flow
; /* Parsed representation of the packet. */
210 enum odp_key_fitness fitness
; /* Fitness of 'flow' relative to ODP key. */
211 const ovs_u128
*ufid
; /* Unique identifier for 'flow'. */
212 unsigned pmd_id
; /* Datapath poll mode driver id. */
213 const struct dp_packet
*packet
; /* Packet associated with this upcall. */
214 ofp_port_t ofp_in_port
; /* OpenFlow in port, or OFPP_NONE. */
215 uint16_t mru
; /* If !0, Maximum receive unit of
216 fragmented IP packet */
218 enum upcall_type type
; /* Type of the upcall. */
219 const struct nlattr
*actions
; /* Flow actions in DPIF_UC_ACTION Upcalls. */
221 bool xout_initialized
; /* True if 'xout' must be uninitialized. */
222 struct xlate_out xout
; /* Result of xlate_actions(). */
223 struct ofpbuf odp_actions
; /* Datapath actions from xlate_actions(). */
224 struct flow_wildcards wc
; /* Dependencies that megaflow must match. */
225 struct ofpbuf put_actions
; /* Actions 'put' in the fastpath. */
227 struct dpif_ipfix
*ipfix
; /* IPFIX pointer or NULL. */
228 struct dpif_sflow
*sflow
; /* SFlow pointer or NULL. */
230 struct udpif_key
*ukey
; /* Revalidator flow cache. */
231 bool ukey_persists
; /* Set true to keep 'ukey' beyond the
232 lifetime of this upcall. */
234 uint64_t reval_seq
; /* udpif->reval_seq at translation time. */
236 /* Not used by the upcall callback interface. */
237 const struct nlattr
*key
; /* Datapath flow key. */
238 size_t key_len
; /* Datapath flow key length. */
239 const struct nlattr
*out_tun_key
; /* Datapath output tunnel key. */
241 struct user_action_cookie cookie
;
243 uint64_t odp_actions_stub
[1024 / 8]; /* Stub for odp_actions. */
246 /* Ukeys must transition through these states using transition_ukey(). */
249 UKEY_VISIBLE
, /* Ukey is in umap, datapath flow install is queued. */
250 UKEY_OPERATIONAL
, /* Ukey is in umap, datapath flow is installed. */
251 UKEY_EVICTING
, /* Ukey is in umap, datapath flow delete is queued. */
252 UKEY_EVICTED
, /* Ukey is in umap, datapath flow is deleted. */
253 UKEY_DELETED
, /* Ukey removed from umap, ukey free is deferred. */
255 #define N_UKEY_STATES (UKEY_DELETED + 1)
257 /* 'udpif_key's are responsible for tracking the little bit of state udpif
258 * needs to do flow expiration which can't be pulled directly from the
259 * datapath. They may be created by any handler or revalidator thread at any
260 * time, and read by any revalidator during the dump phase. They are however
261 * each owned by a single revalidator which takes care of destroying them
262 * during the garbage-collection phase.
264 * The mutex within the ukey protects some members of the ukey. The ukey
265 * itself is protected by RCU and is held within a umap in the parent udpif.
266 * Adding or removing a ukey from a umap is only safe when holding the
267 * corresponding umap lock. */
269 struct cmap_node cmap_node
; /* In parent revalidator 'ukeys' map. */
271 /* These elements are read only once created, and therefore aren't
272 * protected by a mutex. */
273 const struct nlattr
*key
; /* Datapath flow key. */
274 size_t key_len
; /* Length of 'key'. */
275 const struct nlattr
*mask
; /* Datapath flow mask. */
276 size_t mask_len
; /* Length of 'mask'. */
277 ovs_u128 ufid
; /* Unique flow identifier. */
278 bool ufid_present
; /* True if 'ufid' is in datapath. */
279 uint32_t hash
; /* Pre-computed hash for 'key'. */
280 unsigned pmd_id
; /* Datapath poll mode driver id. */
282 struct ovs_mutex mutex
; /* Guards the following. */
283 struct dpif_flow_stats stats OVS_GUARDED
; /* Last known stats.*/
284 long long int created OVS_GUARDED
; /* Estimate of creation time. */
285 uint64_t dump_seq OVS_GUARDED
; /* Tracks udpif->dump_seq. */
286 uint64_t reval_seq OVS_GUARDED
; /* Tracks udpif->reval_seq. */
287 enum ukey_state state OVS_GUARDED
; /* Tracks ukey lifetime. */
289 /* 'state' debug information. */
290 unsigned int state_thread OVS_GUARDED
; /* Thread that transitions. */
291 const char *state_where OVS_GUARDED
; /* transition_ukey() locator. */
293 /* Datapath flow actions as nlattrs. Protected by RCU. Read with
294 * ukey_get_actions(), and write with ukey_set_actions(). */
295 OVSRCU_TYPE(struct ofpbuf
*) actions
;
297 struct xlate_cache
*xcache OVS_GUARDED
; /* Cache for xlate entries that
298 * are affected by this ukey.
299 * Used for stats and learning.*/
301 struct odputil_keybuf buf
;
305 uint32_t key_recirc_id
; /* Non-zero if reference is held by the ukey. */
306 struct recirc_refs recircs
; /* Action recirc IDs with references held. */
309 /* Datapath operation with optional ukey attached. */
311 struct udpif_key
*ukey
;
312 struct dpif_flow_stats stats
; /* Stats for 'op'. */
313 struct dpif_op dop
; /* Flow operation. */
316 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
317 static struct ovs_list all_udpifs
= OVS_LIST_INITIALIZER(&all_udpifs
);
319 static size_t recv_upcalls(struct handler
*);
320 static int process_upcall(struct udpif
*, struct upcall
*,
321 struct ofpbuf
*odp_actions
, struct flow_wildcards
*);
322 static void handle_upcalls(struct udpif
*, struct upcall
*, size_t n_upcalls
);
323 static void udpif_stop_threads(struct udpif
*);
324 static void udpif_start_threads(struct udpif
*, size_t n_handlers
,
325 size_t n_revalidators
);
326 static void udpif_pause_revalidators(struct udpif
*);
327 static void udpif_resume_revalidators(struct udpif
*);
328 static void *udpif_upcall_handler(void *);
329 static void *udpif_revalidator(void *);
330 static unsigned long udpif_get_n_flows(struct udpif
*);
331 static void revalidate(struct revalidator
*);
332 static void revalidator_pause(struct revalidator
*);
333 static void revalidator_sweep(struct revalidator
*);
334 static void revalidator_purge(struct revalidator
*);
335 static void upcall_unixctl_show(struct unixctl_conn
*conn
, int argc
,
336 const char *argv
[], void *aux
);
337 static void upcall_unixctl_disable_megaflows(struct unixctl_conn
*, int argc
,
338 const char *argv
[], void *aux
);
339 static void upcall_unixctl_enable_megaflows(struct unixctl_conn
*, int argc
,
340 const char *argv
[], void *aux
);
341 static void upcall_unixctl_disable_ufid(struct unixctl_conn
*, int argc
,
342 const char *argv
[], void *aux
);
343 static void upcall_unixctl_enable_ufid(struct unixctl_conn
*, int argc
,
344 const char *argv
[], void *aux
);
345 static void upcall_unixctl_set_flow_limit(struct unixctl_conn
*conn
, int argc
,
346 const char *argv
[], void *aux
);
347 static void upcall_unixctl_dump_wait(struct unixctl_conn
*conn
, int argc
,
348 const char *argv
[], void *aux
);
349 static void upcall_unixctl_purge(struct unixctl_conn
*conn
, int argc
,
350 const char *argv
[], void *aux
);
352 static struct udpif_key
*ukey_create_from_upcall(struct upcall
*,
353 struct flow_wildcards
*);
354 static int ukey_create_from_dpif_flow(const struct udpif
*,
355 const struct dpif_flow
*,
356 struct udpif_key
**);
357 static void ukey_get_actions(struct udpif_key
*, const struct nlattr
**actions
,
359 static bool ukey_install__(struct udpif
*, struct udpif_key
*ukey
)
360 OVS_TRY_LOCK(true, ukey
->mutex
);
361 static bool ukey_install(struct udpif
*udpif
, struct udpif_key
*ukey
);
362 static void transition_ukey_at(struct udpif_key
*ukey
, enum ukey_state dst
,
364 OVS_REQUIRES(ukey
->mutex
);
365 #define transition_ukey(UKEY, DST) \
366 transition_ukey_at(UKEY, DST, OVS_SOURCE_LOCATOR)
367 static struct udpif_key
*ukey_lookup(struct udpif
*udpif
,
368 const ovs_u128
*ufid
,
369 const unsigned pmd_id
);
370 static int ukey_acquire(struct udpif
*, const struct dpif_flow
*,
371 struct udpif_key
**result
, int *error
);
372 static void ukey_delete__(struct udpif_key
*);
373 static void ukey_delete(struct umap
*, struct udpif_key
*);
374 static enum upcall_type
classify_upcall(enum dpif_upcall_type type
,
375 const struct nlattr
*userdata
,
376 struct user_action_cookie
*cookie
);
378 static void put_op_init(struct ukey_op
*op
, struct udpif_key
*ukey
,
379 enum dpif_flow_put_flags flags
);
380 static void delete_op_init(struct udpif
*udpif
, struct ukey_op
*op
,
381 struct udpif_key
*ukey
);
383 static int upcall_receive(struct upcall
*, const struct dpif_backer
*,
384 const struct dp_packet
*packet
, enum dpif_upcall_type
,
385 const struct nlattr
*userdata
, const struct flow
*,
386 const unsigned int mru
,
387 const ovs_u128
*ufid
, const unsigned pmd_id
);
388 static void upcall_uninit(struct upcall
*);
390 static upcall_callback upcall_cb
;
391 static dp_purge_callback dp_purge_cb
;
393 static atomic_bool enable_megaflows
= ATOMIC_VAR_INIT(true);
394 static atomic_bool enable_ufid
= ATOMIC_VAR_INIT(true);
399 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
400 if (ovsthread_once_start(&once
)) {
401 unixctl_command_register("upcall/show", "", 0, 0, upcall_unixctl_show
,
403 unixctl_command_register("upcall/disable-megaflows", "", 0, 0,
404 upcall_unixctl_disable_megaflows
, NULL
);
405 unixctl_command_register("upcall/enable-megaflows", "", 0, 0,
406 upcall_unixctl_enable_megaflows
, NULL
);
407 unixctl_command_register("upcall/disable-ufid", "", 0, 0,
408 upcall_unixctl_disable_ufid
, NULL
);
409 unixctl_command_register("upcall/enable-ufid", "", 0, 0,
410 upcall_unixctl_enable_ufid
, NULL
);
411 unixctl_command_register("upcall/set-flow-limit", "flow-limit-number",
412 1, 1, upcall_unixctl_set_flow_limit
, NULL
);
413 unixctl_command_register("revalidator/wait", "", 0, 0,
414 upcall_unixctl_dump_wait
, NULL
);
415 unixctl_command_register("revalidator/purge", "", 0, 0,
416 upcall_unixctl_purge
, NULL
);
417 ovsthread_once_done(&once
);
422 udpif_create(struct dpif_backer
*backer
, struct dpif
*dpif
)
424 struct udpif
*udpif
= xzalloc(sizeof *udpif
);
427 udpif
->backer
= backer
;
428 atomic_init(&udpif
->flow_limit
, MIN(ofproto_flow_limit
, 10000));
429 udpif
->reval_seq
= seq_create();
430 udpif
->dump_seq
= seq_create();
431 latch_init(&udpif
->exit_latch
);
432 latch_init(&udpif
->pause_latch
);
433 ovs_list_push_back(&all_udpifs
, &udpif
->list_node
);
434 atomic_init(&udpif
->enable_ufid
, false);
435 atomic_init(&udpif
->n_flows
, 0);
436 atomic_init(&udpif
->n_flows_timestamp
, LLONG_MIN
);
437 ovs_mutex_init(&udpif
->n_flows_mutex
);
438 udpif
->ukeys
= xmalloc(N_UMAPS
* sizeof *udpif
->ukeys
);
439 for (int i
= 0; i
< N_UMAPS
; i
++) {
440 cmap_init(&udpif
->ukeys
[i
].cmap
);
441 ovs_mutex_init(&udpif
->ukeys
[i
].mutex
);
444 dpif_register_upcall_cb(dpif
, upcall_cb
, udpif
);
445 dpif_register_dp_purge_cb(dpif
, dp_purge_cb
, udpif
);
451 udpif_run(struct udpif
*udpif
)
453 if (udpif
->conns
&& udpif
->conn_seq
!= seq_read(udpif
->dump_seq
)) {
456 for (i
= 0; i
< udpif
->n_conns
; i
++) {
457 unixctl_command_reply(udpif
->conns
[i
], NULL
);
466 udpif_destroy(struct udpif
*udpif
)
468 udpif_stop_threads(udpif
);
470 dpif_register_dp_purge_cb(udpif
->dpif
, NULL
, udpif
);
471 dpif_register_upcall_cb(udpif
->dpif
, NULL
, udpif
);
473 for (int i
= 0; i
< N_UMAPS
; i
++) {
474 cmap_destroy(&udpif
->ukeys
[i
].cmap
);
475 ovs_mutex_destroy(&udpif
->ukeys
[i
].mutex
);
480 ovs_list_remove(&udpif
->list_node
);
481 latch_destroy(&udpif
->exit_latch
);
482 latch_destroy(&udpif
->pause_latch
);
483 seq_destroy(udpif
->reval_seq
);
484 seq_destroy(udpif
->dump_seq
);
485 ovs_mutex_destroy(&udpif
->n_flows_mutex
);
489 /* Stops the handler and revalidator threads, must be enclosed in
490 * ovsrcu quiescent state unless when destroying udpif. */
492 udpif_stop_threads(struct udpif
*udpif
)
494 if (udpif
&& (udpif
->n_handlers
!= 0 || udpif
->n_revalidators
!= 0)) {
497 latch_set(&udpif
->exit_latch
);
499 for (i
= 0; i
< udpif
->n_handlers
; i
++) {
500 struct handler
*handler
= &udpif
->handlers
[i
];
502 xpthread_join(handler
->thread
, NULL
);
505 for (i
= 0; i
< udpif
->n_revalidators
; i
++) {
506 xpthread_join(udpif
->revalidators
[i
].thread
, NULL
);
509 dpif_disable_upcall(udpif
->dpif
);
511 for (i
= 0; i
< udpif
->n_revalidators
; i
++) {
512 struct revalidator
*revalidator
= &udpif
->revalidators
[i
];
514 /* Delete ukeys, and delete all flows from the datapath to prevent
515 * double-counting stats. */
516 revalidator_purge(revalidator
);
519 latch_poll(&udpif
->exit_latch
);
521 ovs_barrier_destroy(&udpif
->reval_barrier
);
522 ovs_barrier_destroy(&udpif
->pause_barrier
);
524 free(udpif
->revalidators
);
525 udpif
->revalidators
= NULL
;
526 udpif
->n_revalidators
= 0;
528 free(udpif
->handlers
);
529 udpif
->handlers
= NULL
;
530 udpif
->n_handlers
= 0;
534 /* Starts the handler and revalidator threads, must be enclosed in
535 * ovsrcu quiescent state. */
537 udpif_start_threads(struct udpif
*udpif
, size_t n_handlers_
,
538 size_t n_revalidators_
)
540 if (udpif
&& n_handlers_
&& n_revalidators_
) {
541 udpif
->n_handlers
= n_handlers_
;
542 udpif
->n_revalidators
= n_revalidators_
;
544 udpif
->handlers
= xzalloc(udpif
->n_handlers
* sizeof *udpif
->handlers
);
545 for (size_t i
= 0; i
< udpif
->n_handlers
; i
++) {
546 struct handler
*handler
= &udpif
->handlers
[i
];
548 handler
->udpif
= udpif
;
549 handler
->handler_id
= i
;
550 handler
->thread
= ovs_thread_create(
551 "handler", udpif_upcall_handler
, handler
);
554 atomic_init(&udpif
->enable_ufid
, udpif
->backer
->rt_support
.ufid
);
555 dpif_enable_upcall(udpif
->dpif
);
557 ovs_barrier_init(&udpif
->reval_barrier
, udpif
->n_revalidators
);
558 ovs_barrier_init(&udpif
->pause_barrier
, udpif
->n_revalidators
+ 1);
559 udpif
->reval_exit
= false;
560 udpif
->pause
= false;
561 udpif
->revalidators
= xzalloc(udpif
->n_revalidators
562 * sizeof *udpif
->revalidators
);
563 for (size_t i
= 0; i
< udpif
->n_revalidators
; i
++) {
564 struct revalidator
*revalidator
= &udpif
->revalidators
[i
];
566 revalidator
->udpif
= udpif
;
567 revalidator
->thread
= ovs_thread_create(
568 "revalidator", udpif_revalidator
, revalidator
);
573 /* Pauses all revalidators. Should only be called by the main thread.
574 * When function returns, all revalidators are paused and will proceed
575 * only after udpif_resume_revalidators() is called. */
577 udpif_pause_revalidators(struct udpif
*udpif
)
579 if (udpif
->backer
->recv_set_enable
) {
580 latch_set(&udpif
->pause_latch
);
581 ovs_barrier_block(&udpif
->pause_barrier
);
585 /* Resumes the pausing of revalidators. Should only be called by the
588 udpif_resume_revalidators(struct udpif
*udpif
)
590 if (udpif
->backer
->recv_set_enable
) {
591 latch_poll(&udpif
->pause_latch
);
592 ovs_barrier_block(&udpif
->pause_barrier
);
596 /* Tells 'udpif' how many threads it should use to handle upcalls.
597 * 'n_handlers_' and 'n_revalidators_' can never be zero. 'udpif''s
598 * datapath handle must have packet reception enabled before starting
601 udpif_set_threads(struct udpif
*udpif
, size_t n_handlers_
,
602 size_t n_revalidators_
)
605 ovs_assert(n_handlers_
&& n_revalidators_
);
607 ovsrcu_quiesce_start();
608 if (udpif
->n_handlers
!= n_handlers_
609 || udpif
->n_revalidators
!= n_revalidators_
) {
610 udpif_stop_threads(udpif
);
613 if (!udpif
->handlers
&& !udpif
->revalidators
) {
616 error
= dpif_handlers_set(udpif
->dpif
, n_handlers_
);
618 VLOG_ERR("failed to configure handlers in dpif %s: %s",
619 dpif_name(udpif
->dpif
), ovs_strerror(error
));
623 udpif_start_threads(udpif
, n_handlers_
, n_revalidators_
);
625 ovsrcu_quiesce_end();
628 /* Waits for all ongoing upcall translations to complete. This ensures that
629 * there are no transient references to any removed ofprotos (or other
630 * objects). In particular, this should be called after an ofproto is removed
631 * (e.g. via xlate_remove_ofproto()) but before it is destroyed. */
633 udpif_synchronize(struct udpif
*udpif
)
635 /* This is stronger than necessary. It would be sufficient to ensure
636 * (somehow) that each handler and revalidator thread had passed through
637 * its main loop once. */
638 size_t n_handlers_
= udpif
->n_handlers
;
639 size_t n_revalidators_
= udpif
->n_revalidators
;
641 ovsrcu_quiesce_start();
642 udpif_stop_threads(udpif
);
643 udpif_start_threads(udpif
, n_handlers_
, n_revalidators_
);
644 ovsrcu_quiesce_end();
647 /* Notifies 'udpif' that something changed which may render previous
648 * xlate_actions() results invalid. */
650 udpif_revalidate(struct udpif
*udpif
)
652 seq_change(udpif
->reval_seq
);
655 /* Returns a seq which increments every time 'udpif' pulls stats from the
656 * datapath. Callers can use this to get a sense of when might be a good time
657 * to do periodic work which relies on relatively up to date statistics. */
659 udpif_dump_seq(struct udpif
*udpif
)
661 return udpif
->dump_seq
;
665 udpif_get_memory_usage(struct udpif
*udpif
, struct simap
*usage
)
669 simap_increase(usage
, "handlers", udpif
->n_handlers
);
671 simap_increase(usage
, "revalidators", udpif
->n_revalidators
);
672 for (i
= 0; i
< N_UMAPS
; i
++) {
673 simap_increase(usage
, "udpif keys", cmap_count(&udpif
->ukeys
[i
].cmap
));
677 /* Remove flows from a single datapath. */
679 udpif_flush(struct udpif
*udpif
)
681 size_t n_handlers_
= udpif
->n_handlers
;
682 size_t n_revalidators_
= udpif
->n_revalidators
;
684 ovsrcu_quiesce_start();
686 udpif_stop_threads(udpif
);
687 dpif_flow_flush(udpif
->dpif
);
688 udpif_start_threads(udpif
, n_handlers_
, n_revalidators_
);
690 ovsrcu_quiesce_end();
693 /* Removes all flows from all datapaths. */
695 udpif_flush_all_datapaths(void)
699 LIST_FOR_EACH (udpif
, list_node
, &all_udpifs
) {
705 udpif_use_ufid(struct udpif
*udpif
)
709 atomic_read_relaxed(&enable_ufid
, &enable
);
710 return enable
&& udpif
->backer
->rt_support
.ufid
;
715 udpif_get_n_flows(struct udpif
*udpif
)
717 long long int time
, now
;
718 unsigned long flow_count
;
721 atomic_read_relaxed(&udpif
->n_flows_timestamp
, &time
);
722 if (time
< now
- 100 && !ovs_mutex_trylock(&udpif
->n_flows_mutex
)) {
723 struct dpif_dp_stats stats
;
725 atomic_store_relaxed(&udpif
->n_flows_timestamp
, now
);
726 dpif_get_dp_stats(udpif
->dpif
, &stats
);
727 flow_count
= stats
.n_flows
;
728 atomic_store_relaxed(&udpif
->n_flows
, flow_count
);
729 ovs_mutex_unlock(&udpif
->n_flows_mutex
);
731 atomic_read_relaxed(&udpif
->n_flows
, &flow_count
);
736 /* The upcall handler thread tries to read a batch of UPCALL_MAX_BATCH
737 * upcalls from dpif, processes the batch and installs corresponding flows
740 udpif_upcall_handler(void *arg
)
742 struct handler
*handler
= arg
;
743 struct udpif
*udpif
= handler
->udpif
;
745 while (!latch_is_set(&handler
->udpif
->exit_latch
)) {
746 if (recv_upcalls(handler
)) {
747 poll_immediate_wake();
749 dpif_recv_wait(udpif
->dpif
, handler
->handler_id
);
750 latch_wait(&udpif
->exit_latch
);
759 recv_upcalls(struct handler
*handler
)
761 struct udpif
*udpif
= handler
->udpif
;
762 uint64_t recv_stubs
[UPCALL_MAX_BATCH
][512 / 8];
763 struct ofpbuf recv_bufs
[UPCALL_MAX_BATCH
];
764 struct dpif_upcall dupcalls
[UPCALL_MAX_BATCH
];
765 struct upcall upcalls
[UPCALL_MAX_BATCH
];
766 struct flow flows
[UPCALL_MAX_BATCH
];
770 while (n_upcalls
< UPCALL_MAX_BATCH
) {
771 struct ofpbuf
*recv_buf
= &recv_bufs
[n_upcalls
];
772 struct dpif_upcall
*dupcall
= &dupcalls
[n_upcalls
];
773 struct upcall
*upcall
= &upcalls
[n_upcalls
];
774 struct flow
*flow
= &flows
[n_upcalls
];
778 ofpbuf_use_stub(recv_buf
, recv_stubs
[n_upcalls
],
779 sizeof recv_stubs
[n_upcalls
]);
780 if (dpif_recv(udpif
->dpif
, handler
->handler_id
, dupcall
, recv_buf
)) {
781 ofpbuf_uninit(recv_buf
);
785 upcall
->fitness
= odp_flow_key_to_flow(dupcall
->key
, dupcall
->key_len
,
787 if (upcall
->fitness
== ODP_FIT_ERROR
) {
792 mru
= nl_attr_get_u16(dupcall
->mru
);
797 error
= upcall_receive(upcall
, udpif
->backer
, &dupcall
->packet
,
798 dupcall
->type
, dupcall
->userdata
, flow
, mru
,
799 &dupcall
->ufid
, PMD_ID_NULL
);
801 if (error
== ENODEV
) {
802 /* Received packet on datapath port for which we couldn't
803 * associate an ofproto. This can happen if a port is removed
804 * while traffic is being received. Print a rate-limited
805 * message in case it happens frequently. */
806 dpif_flow_put(udpif
->dpif
, DPIF_FP_CREATE
, dupcall
->key
,
807 dupcall
->key_len
, NULL
, 0, NULL
, 0,
808 &dupcall
->ufid
, PMD_ID_NULL
, NULL
);
809 VLOG_INFO_RL(&rl
, "received packet on unassociated datapath "
810 "port %"PRIu32
, flow
->in_port
.odp_port
);
815 upcall
->key
= dupcall
->key
;
816 upcall
->key_len
= dupcall
->key_len
;
817 upcall
->ufid
= &dupcall
->ufid
;
819 upcall
->out_tun_key
= dupcall
->out_tun_key
;
820 upcall
->actions
= dupcall
->actions
;
822 pkt_metadata_from_flow(&dupcall
->packet
.md
, flow
);
823 flow_extract(&dupcall
->packet
, flow
);
825 error
= process_upcall(udpif
, upcall
,
826 &upcall
->odp_actions
, &upcall
->wc
);
835 upcall_uninit(upcall
);
837 dp_packet_uninit(&dupcall
->packet
);
838 ofpbuf_uninit(recv_buf
);
842 handle_upcalls(handler
->udpif
, upcalls
, n_upcalls
);
843 for (i
= 0; i
< n_upcalls
; i
++) {
844 dp_packet_uninit(&dupcalls
[i
].packet
);
845 ofpbuf_uninit(&recv_bufs
[i
]);
846 upcall_uninit(&upcalls
[i
]);
854 udpif_revalidator(void *arg
)
856 /* Used by all revalidators. */
857 struct revalidator
*revalidator
= arg
;
858 struct udpif
*udpif
= revalidator
->udpif
;
859 bool leader
= revalidator
== &udpif
->revalidators
[0];
861 /* Used only by the leader. */
862 long long int start_time
= 0;
863 uint64_t last_reval_seq
= 0;
866 revalidator
->id
= ovsthread_id_self();
871 recirc_run(); /* Recirculation cleanup. */
873 reval_seq
= seq_read(udpif
->reval_seq
);
874 last_reval_seq
= reval_seq
;
876 n_flows
= udpif_get_n_flows(udpif
);
877 udpif
->max_n_flows
= MAX(n_flows
, udpif
->max_n_flows
);
878 udpif
->avg_n_flows
= (udpif
->avg_n_flows
+ n_flows
) / 2;
880 /* Only the leader checks the pause latch to prevent a race where
881 * some threads think it's false and proceed to block on
882 * reval_barrier and others think it's true and block indefinitely
883 * on the pause_barrier */
884 udpif
->pause
= latch_is_set(&udpif
->pause_latch
);
886 /* Only the leader checks the exit latch to prevent a race where
887 * some threads think it's true and exit and others think it's
888 * false and block indefinitely on the reval_barrier */
889 udpif
->reval_exit
= latch_is_set(&udpif
->exit_latch
);
891 start_time
= time_msec();
892 if (!udpif
->reval_exit
) {
895 terse_dump
= udpif_use_ufid(udpif
);
896 udpif
->dump
= dpif_flow_dump_create(udpif
->dpif
, terse_dump
,
901 /* Wait for the leader to start the flow dump. */
902 ovs_barrier_block(&udpif
->reval_barrier
);
904 revalidator_pause(revalidator
);
907 if (udpif
->reval_exit
) {
910 revalidate(revalidator
);
912 /* Wait for all flows to have been dumped before we garbage collect. */
913 ovs_barrier_block(&udpif
->reval_barrier
);
914 revalidator_sweep(revalidator
);
916 /* Wait for all revalidators to finish garbage collection. */
917 ovs_barrier_block(&udpif
->reval_barrier
);
920 unsigned int flow_limit
;
921 long long int duration
;
923 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
925 dpif_flow_dump_destroy(udpif
->dump
);
926 seq_change(udpif
->dump_seq
);
928 duration
= MAX(time_msec() - start_time
, 1);
929 udpif
->dump_duration
= duration
;
930 if (duration
> 2000) {
931 flow_limit
/= duration
/ 1000;
932 } else if (duration
> 1300) {
933 flow_limit
= flow_limit
* 3 / 4;
934 } else if (duration
< 1000 && n_flows
> 2000
935 && flow_limit
< n_flows
* 1000 / duration
) {
938 flow_limit
= MIN(ofproto_flow_limit
, MAX(flow_limit
, 1000));
939 atomic_store_relaxed(&udpif
->flow_limit
, flow_limit
);
941 if (duration
> 2000) {
942 VLOG_INFO("Spent an unreasonably long %lldms dumping flows",
946 poll_timer_wait_until(start_time
+ MIN(ofproto_max_idle
, 500));
947 seq_wait(udpif
->reval_seq
, last_reval_seq
);
948 latch_wait(&udpif
->exit_latch
);
949 latch_wait(&udpif
->pause_latch
);
952 if (!latch_is_set(&udpif
->pause_latch
) &&
953 !latch_is_set(&udpif
->exit_latch
)) {
954 long long int now
= time_msec();
955 /* Block again if we are woken up within 5ms of the last start
959 if (now
< start_time
) {
960 poll_timer_wait_until(start_time
);
961 latch_wait(&udpif
->exit_latch
);
962 latch_wait(&udpif
->pause_latch
);
972 static enum upcall_type
973 classify_upcall(enum dpif_upcall_type type
, const struct nlattr
*userdata
,
974 struct user_action_cookie
*cookie
)
976 /* First look at the upcall type. */
984 case DPIF_N_UC_TYPES
:
986 VLOG_WARN_RL(&rl
, "upcall has unexpected type %"PRIu32
, type
);
990 /* "action" upcalls need a closer look. */
992 VLOG_WARN_RL(&rl
, "action upcall missing cookie");
996 size_t userdata_len
= nl_attr_get_size(userdata
);
997 if (userdata_len
!= sizeof *cookie
) {
998 VLOG_WARN_RL(&rl
, "action upcall cookie has unexpected size %"PRIuSIZE
,
1002 memcpy(cookie
, nl_attr_get(userdata
), sizeof *cookie
);
1003 if (cookie
->type
== USER_ACTION_COOKIE_SFLOW
) {
1004 return SFLOW_UPCALL
;
1005 } else if (cookie
->type
== USER_ACTION_COOKIE_SLOW_PATH
) {
1006 return SLOW_PATH_UPCALL
;
1007 } else if (cookie
->type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
1008 return FLOW_SAMPLE_UPCALL
;
1009 } else if (cookie
->type
== USER_ACTION_COOKIE_IPFIX
) {
1010 return IPFIX_UPCALL
;
1011 } else if (cookie
->type
== USER_ACTION_COOKIE_CONTROLLER
) {
1012 return CONTROLLER_UPCALL
;
1014 VLOG_WARN_RL(&rl
, "invalid user cookie of type %"PRIu16
1015 " and size %"PRIuSIZE
, cookie
->type
, userdata_len
);
1020 /* Calculates slow path actions for 'xout'. 'buf' must statically be
1021 * initialized with at least 128 bytes of space. */
1023 compose_slow_path(struct udpif
*udpif
, struct xlate_out
*xout
,
1024 const struct flow
*flow
,
1025 odp_port_t odp_in_port
, ofp_port_t ofp_in_port
,
1026 struct ofpbuf
*buf
, uint32_t meter_id
,
1027 struct uuid
*ofproto_uuid
)
1029 struct user_action_cookie cookie
;
1033 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
1034 cookie
.ofp_in_port
= ofp_in_port
;
1035 cookie
.ofproto_uuid
= *ofproto_uuid
;
1036 cookie
.slow_path
.reason
= xout
->slow
;
1038 port
= xout
->slow
& (SLOW_CFM
| SLOW_BFD
| SLOW_LACP
| SLOW_STP
)
1041 pid
= dpif_port_get_pid(udpif
->dpif
, port
, flow_hash_5tuple(flow
, 0));
1045 if (meter_id
!= UINT32_MAX
) {
1046 /* If slowpath meter is configured, generate clone(meter, userspace)
1048 offset
= nl_msg_start_nested(buf
, OVS_ACTION_ATTR_SAMPLE
);
1049 nl_msg_put_u32(buf
, OVS_SAMPLE_ATTR_PROBABILITY
, UINT32_MAX
);
1050 ac_offset
= nl_msg_start_nested(buf
, OVS_SAMPLE_ATTR_ACTIONS
);
1051 nl_msg_put_u32(buf
, OVS_ACTION_ATTR_METER
, meter_id
);
1054 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
,
1055 ODPP_NONE
, false, buf
);
1057 if (meter_id
!= UINT32_MAX
) {
1058 nl_msg_end_nested(buf
, ac_offset
);
1059 nl_msg_end_nested(buf
, offset
);
1063 /* If there is no error, the upcall must be destroyed with upcall_uninit()
1064 * before quiescing, as the referred objects are guaranteed to exist only
1065 * until the calling thread quiesces. Otherwise, do not call upcall_uninit()
1066 * since the 'upcall->put_actions' remains uninitialized. */
1068 upcall_receive(struct upcall
*upcall
, const struct dpif_backer
*backer
,
1069 const struct dp_packet
*packet
, enum dpif_upcall_type type
,
1070 const struct nlattr
*userdata
, const struct flow
*flow
,
1071 const unsigned int mru
,
1072 const ovs_u128
*ufid
, const unsigned pmd_id
)
1076 upcall
->type
= classify_upcall(type
, userdata
, &upcall
->cookie
);
1077 if (upcall
->type
== BAD_UPCALL
) {
1079 } else if (upcall
->type
== MISS_UPCALL
) {
1080 error
= xlate_lookup(backer
, flow
, &upcall
->ofproto
, &upcall
->ipfix
,
1081 &upcall
->sflow
, NULL
, &upcall
->ofp_in_port
);
1086 struct ofproto_dpif
*ofproto
1087 = ofproto_dpif_lookup_by_uuid(&upcall
->cookie
.ofproto_uuid
);
1089 VLOG_INFO_RL(&rl
, "upcall could not find ofproto");
1092 upcall
->ofproto
= ofproto
;
1093 upcall
->ipfix
= ofproto
->ipfix
;
1094 upcall
->sflow
= ofproto
->sflow
;
1095 upcall
->ofp_in_port
= upcall
->cookie
.ofp_in_port
;
1098 upcall
->recirc
= NULL
;
1099 upcall
->have_recirc_ref
= false;
1100 upcall
->flow
= flow
;
1101 upcall
->packet
= packet
;
1102 upcall
->ufid
= ufid
;
1103 upcall
->pmd_id
= pmd_id
;
1104 ofpbuf_use_stub(&upcall
->odp_actions
, upcall
->odp_actions_stub
,
1105 sizeof upcall
->odp_actions_stub
);
1106 ofpbuf_init(&upcall
->put_actions
, 0);
1108 upcall
->xout_initialized
= false;
1109 upcall
->ukey_persists
= false;
1111 upcall
->ukey
= NULL
;
1113 upcall
->key_len
= 0;
1116 upcall
->out_tun_key
= NULL
;
1117 upcall
->actions
= NULL
;
1123 upcall_xlate(struct udpif
*udpif
, struct upcall
*upcall
,
1124 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
1126 struct dpif_flow_stats stats
;
1127 enum xlate_error xerr
;
1128 struct xlate_in xin
;
1131 stats
.n_packets
= 1;
1132 stats
.n_bytes
= dp_packet_size(upcall
->packet
);
1133 stats
.used
= time_msec();
1134 stats
.tcp_flags
= ntohs(upcall
->flow
->tcp_flags
);
1136 xlate_in_init(&xin
, upcall
->ofproto
,
1137 ofproto_dpif_get_tables_version(upcall
->ofproto
),
1138 upcall
->flow
, upcall
->ofp_in_port
, NULL
,
1139 stats
.tcp_flags
, upcall
->packet
, wc
, odp_actions
);
1141 if (upcall
->type
== MISS_UPCALL
) {
1142 xin
.resubmit_stats
= &stats
;
1144 if (xin
.frozen_state
) {
1145 /* We may install a datapath flow only if we get a reference to the
1146 * recirculation context (otherwise we could have recirculation
1147 * upcalls using recirculation ID for which no context can be
1148 * found). We may still execute the flow's actions even if we
1149 * don't install the flow. */
1150 upcall
->recirc
= recirc_id_node_from_state(xin
.frozen_state
);
1151 upcall
->have_recirc_ref
= recirc_id_node_try_ref_rcu(upcall
->recirc
);
1154 /* For non-miss upcalls, we are either executing actions (one of which
1155 * is an userspace action) for an upcall, in which case the stats have
1156 * already been taken care of, or there's a flow in the datapath which
1157 * this packet was accounted to. Presumably the revalidators will deal
1158 * with pushing its stats eventually. */
1161 upcall
->reval_seq
= seq_read(udpif
->reval_seq
);
1163 xerr
= xlate_actions(&xin
, &upcall
->xout
);
1165 /* Translate again and log the ofproto trace for
1166 * these two error types. */
1167 if (xerr
== XLATE_RECURSION_TOO_DEEP
||
1168 xerr
== XLATE_TOO_MANY_RESUBMITS
) {
1169 static struct vlog_rate_limit rll
= VLOG_RATE_LIMIT_INIT(1, 1);
1171 /* This is a huge log, so be conservative. */
1172 if (!VLOG_DROP_WARN(&rll
)) {
1174 ofproto_trace(upcall
->ofproto
, upcall
->flow
,
1175 upcall
->packet
, NULL
, 0, NULL
, &output
);
1176 VLOG_WARN("%s", ds_cstr(&output
));
1177 ds_destroy(&output
);
1182 /* Convert the input port wildcard from OFP to ODP format. There's no
1183 * real way to do this for arbitrary bitmasks since the numbering spaces
1184 * aren't the same. However, flow translation always exact matches the
1185 * whole thing, so we can do the same here. */
1186 WC_MASK_FIELD(wc
, in_port
.odp_port
);
1189 upcall
->xout_initialized
= true;
1191 if (upcall
->fitness
== ODP_FIT_TOO_LITTLE
) {
1192 upcall
->xout
.slow
|= SLOW_MATCH
;
1194 if (!upcall
->xout
.slow
) {
1195 ofpbuf_use_const(&upcall
->put_actions
,
1196 odp_actions
->data
, odp_actions
->size
);
1198 /* upcall->put_actions already initialized by upcall_receive(). */
1199 compose_slow_path(udpif
, &upcall
->xout
, upcall
->flow
,
1200 upcall
->flow
->in_port
.odp_port
, upcall
->ofp_in_port
,
1201 &upcall
->put_actions
,
1202 upcall
->ofproto
->up
.slowpath_meter_id
,
1203 &upcall
->ofproto
->uuid
);
1206 /* This function is also called for slow-pathed flows. As we are only
1207 * going to create new datapath flows for actual datapath misses, there is
1208 * no point in creating a ukey otherwise. */
1209 if (upcall
->type
== MISS_UPCALL
) {
1210 upcall
->ukey
= ukey_create_from_upcall(upcall
, wc
);
1215 upcall_uninit(struct upcall
*upcall
)
1218 if (upcall
->xout_initialized
) {
1219 xlate_out_uninit(&upcall
->xout
);
1221 ofpbuf_uninit(&upcall
->odp_actions
);
1222 ofpbuf_uninit(&upcall
->put_actions
);
1224 if (!upcall
->ukey_persists
) {
1225 ukey_delete__(upcall
->ukey
);
1227 } else if (upcall
->have_recirc_ref
) {
1228 /* The reference was transferred to the ukey if one was created. */
1229 recirc_id_node_unref(upcall
->recirc
);
1234 /* If there are less flows than the limit, and this is a miss upcall which
1236 * - Has no recirc_id, OR
1237 * - Has a recirc_id and we can get a reference on the recirc ctx,
1239 * Then we should install the flow (true). Otherwise, return false. */
1241 should_install_flow(struct udpif
*udpif
, struct upcall
*upcall
)
1243 unsigned int flow_limit
;
1245 if (upcall
->type
!= MISS_UPCALL
) {
1247 } else if (upcall
->recirc
&& !upcall
->have_recirc_ref
) {
1248 VLOG_DBG_RL(&rl
, "upcall: no reference for recirc flow");
1252 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
1253 if (udpif_get_n_flows(udpif
) >= flow_limit
) {
1254 VLOG_WARN_RL(&rl
, "upcall: datapath flow limit reached");
1262 upcall_cb(const struct dp_packet
*packet
, const struct flow
*flow
, ovs_u128
*ufid
,
1263 unsigned pmd_id
, enum dpif_upcall_type type
,
1264 const struct nlattr
*userdata
, struct ofpbuf
*actions
,
1265 struct flow_wildcards
*wc
, struct ofpbuf
*put_actions
, void *aux
)
1267 struct udpif
*udpif
= aux
;
1268 struct upcall upcall
;
1272 atomic_read_relaxed(&enable_megaflows
, &megaflow
);
1274 error
= upcall_receive(&upcall
, udpif
->backer
, packet
, type
, userdata
,
1275 flow
, 0, ufid
, pmd_id
);
1280 upcall
.fitness
= ODP_FIT_PERFECT
;
1281 error
= process_upcall(udpif
, &upcall
, actions
, wc
);
1286 if (upcall
.xout
.slow
&& put_actions
) {
1287 ofpbuf_put(put_actions
, upcall
.put_actions
.data
,
1288 upcall
.put_actions
.size
);
1291 if (OVS_UNLIKELY(!megaflow
&& wc
)) {
1292 flow_wildcards_init_for_packet(wc
, flow
);
1295 if (!should_install_flow(udpif
, &upcall
)) {
1300 if (upcall
.ukey
&& !ukey_install(udpif
, upcall
.ukey
)) {
1301 static struct vlog_rate_limit rll
= VLOG_RATE_LIMIT_INIT(1, 1);
1302 VLOG_WARN_RL(&rll
, "upcall_cb failure: ukey installation fails");
1307 upcall
.ukey_persists
= true;
1309 upcall_uninit(&upcall
);
1314 dpif_get_actions(struct udpif
*udpif
, struct upcall
*upcall
,
1315 const struct nlattr
**actions
)
1317 size_t actions_len
= 0;
1319 if (upcall
->actions
) {
1320 /* Actions were passed up from datapath. */
1321 *actions
= nl_attr_get(upcall
->actions
);
1322 actions_len
= nl_attr_get_size(upcall
->actions
);
1325 if (actions_len
== 0) {
1326 /* Lookup actions in userspace cache. */
1327 struct udpif_key
*ukey
= ukey_lookup(udpif
, upcall
->ufid
,
1330 ukey_get_actions(ukey
, actions
, &actions_len
);
1338 dpif_read_actions(struct udpif
*udpif
, struct upcall
*upcall
,
1339 const struct flow
*flow
, enum upcall_type type
,
1342 const struct nlattr
*actions
= NULL
;
1343 size_t actions_len
= dpif_get_actions(udpif
, upcall
, &actions
);
1345 if (!actions
|| !actions_len
) {
1351 dpif_sflow_read_actions(flow
, actions
, actions_len
, upcall_data
, true);
1353 case FLOW_SAMPLE_UPCALL
:
1355 dpif_ipfix_read_actions(flow
, actions
, actions_len
, upcall_data
);
1359 case SLOW_PATH_UPCALL
:
1360 case CONTROLLER_UPCALL
:
1369 process_upcall(struct udpif
*udpif
, struct upcall
*upcall
,
1370 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
1372 const struct dp_packet
*packet
= upcall
->packet
;
1373 const struct flow
*flow
= upcall
->flow
;
1374 size_t actions_len
= 0;
1376 switch (upcall
->type
) {
1378 case SLOW_PATH_UPCALL
:
1379 upcall_xlate(udpif
, upcall
, odp_actions
, wc
);
1383 if (upcall
->sflow
) {
1384 struct dpif_sflow_actions sflow_actions
;
1386 memset(&sflow_actions
, 0, sizeof sflow_actions
);
1388 actions_len
= dpif_read_actions(udpif
, upcall
, flow
,
1389 upcall
->type
, &sflow_actions
);
1390 dpif_sflow_received(upcall
->sflow
, packet
, flow
,
1391 flow
->in_port
.odp_port
, &upcall
->cookie
,
1392 actions_len
> 0 ? &sflow_actions
: NULL
);
1397 case FLOW_SAMPLE_UPCALL
:
1398 if (upcall
->ipfix
) {
1399 struct flow_tnl output_tunnel_key
;
1400 struct dpif_ipfix_actions ipfix_actions
;
1402 memset(&ipfix_actions
, 0, sizeof ipfix_actions
);
1404 if (upcall
->out_tun_key
) {
1405 odp_tun_key_from_attr(upcall
->out_tun_key
, &output_tunnel_key
);
1408 actions_len
= dpif_read_actions(udpif
, upcall
, flow
,
1409 upcall
->type
, &ipfix_actions
);
1410 if (upcall
->type
== IPFIX_UPCALL
) {
1411 dpif_ipfix_bridge_sample(upcall
->ipfix
, packet
, flow
,
1412 flow
->in_port
.odp_port
,
1413 upcall
->cookie
.ipfix
.output_odp_port
,
1414 upcall
->out_tun_key
?
1415 &output_tunnel_key
: NULL
,
1417 &ipfix_actions
: NULL
);
1419 /* The flow reflects exactly the contents of the packet.
1420 * Sample the packet using it. */
1421 dpif_ipfix_flow_sample(upcall
->ipfix
, packet
, flow
,
1422 &upcall
->cookie
, flow
->in_port
.odp_port
,
1423 upcall
->out_tun_key
?
1424 &output_tunnel_key
: NULL
,
1425 actions_len
> 0 ? &ipfix_actions
: NULL
);
1430 case CONTROLLER_UPCALL
:
1432 struct user_action_cookie
*cookie
= &upcall
->cookie
;
1434 if (cookie
->controller
.dont_send
) {
1438 uint32_t recirc_id
= cookie
->controller
.recirc_id
;
1443 const struct recirc_id_node
*recirc_node
1444 = recirc_id_node_find(recirc_id
);
1449 const struct frozen_state
*state
= &recirc_node
->state
;
1451 struct ofproto_async_msg
*am
= xmalloc(sizeof *am
);
1452 *am
= (struct ofproto_async_msg
) {
1453 .controller_id
= cookie
->controller
.controller_id
,
1454 .oam
= OAM_PACKET_IN
,
1458 .packet
= xmemdup(dp_packet_data(packet
),
1459 dp_packet_size(packet
)),
1460 .packet_len
= dp_packet_size(packet
),
1461 .reason
= cookie
->controller
.reason
,
1462 .table_id
= state
->table_id
,
1463 .cookie
= get_32aligned_be64(
1464 &cookie
->controller
.rule_cookie
),
1465 .userdata
= (recirc_node
->state
.userdata_len
1466 ? xmemdup(recirc_node
->state
.userdata
,
1467 recirc_node
->state
.userdata_len
)
1469 .userdata_len
= recirc_node
->state
.userdata_len
,
1472 .max_len
= cookie
->controller
.max_len
,
1476 if (cookie
->controller
.continuation
) {
1477 am
->pin
.up
.stack
= (state
->stack_size
1478 ? xmemdup(state
->stack
, state
->stack_size
)
1480 am
->pin
.up
.stack_size
= state
->stack_size
,
1481 am
->pin
.up
.mirrors
= state
->mirrors
,
1482 am
->pin
.up
.conntracked
= state
->conntracked
,
1483 am
->pin
.up
.actions
= (state
->ofpacts_len
1484 ? xmemdup(state
->ofpacts
,
1485 state
->ofpacts_len
) : NULL
),
1486 am
->pin
.up
.actions_len
= state
->ofpacts_len
,
1487 am
->pin
.up
.action_set
= (state
->action_set_len
1488 ? xmemdup(state
->action_set
,
1489 state
->action_set_len
)
1491 am
->pin
.up
.action_set_len
= state
->action_set_len
,
1492 am
->pin
.up
.bridge
= upcall
->ofproto
->uuid
;
1495 /* We don't want to use the upcall 'flow', since it may be
1496 * more specific than the point at which the "controller"
1497 * action was specified. */
1498 struct flow frozen_flow
;
1500 frozen_flow
= *flow
;
1501 if (!state
->conntracked
) {
1502 flow_clear_conntrack(&frozen_flow
);
1505 frozen_metadata_to_flow(&state
->metadata
, &frozen_flow
);
1506 flow_get_metadata(&frozen_flow
, &am
->pin
.up
.base
.flow_metadata
);
1508 ofproto_dpif_send_async_msg(upcall
->ofproto
, am
);
1520 handle_upcalls(struct udpif
*udpif
, struct upcall
*upcalls
,
1523 struct dpif_op
*opsp
[UPCALL_MAX_BATCH
* 2];
1524 struct ukey_op ops
[UPCALL_MAX_BATCH
* 2];
1525 size_t n_ops
, n_opsp
, i
;
1527 /* Handle the packets individually in order of arrival.
1529 * - For SLOW_CFM, SLOW_LACP, SLOW_STP, SLOW_BFD, and SLOW_LLDP,
1530 * translation is what processes received packets for these
1533 * - For SLOW_ACTION, translation executes the actions directly.
1535 * The loop fills 'ops' with an array of operations to execute in the
1538 for (i
= 0; i
< n_upcalls
; i
++) {
1539 struct upcall
*upcall
= &upcalls
[i
];
1540 const struct dp_packet
*packet
= upcall
->packet
;
1543 if (should_install_flow(udpif
, upcall
)) {
1544 struct udpif_key
*ukey
= upcall
->ukey
;
1546 if (ukey_install(udpif
, ukey
)) {
1547 upcall
->ukey_persists
= true;
1548 put_op_init(&ops
[n_ops
++], ukey
, DPIF_FP_CREATE
);
1552 if (upcall
->odp_actions
.size
) {
1555 op
->dop
.type
= DPIF_OP_EXECUTE
;
1556 op
->dop
.execute
.packet
= CONST_CAST(struct dp_packet
*, packet
);
1557 op
->dop
.execute
.flow
= upcall
->flow
;
1558 odp_key_to_dp_packet(upcall
->key
, upcall
->key_len
,
1559 op
->dop
.execute
.packet
);
1560 op
->dop
.execute
.actions
= upcall
->odp_actions
.data
;
1561 op
->dop
.execute
.actions_len
= upcall
->odp_actions
.size
;
1562 op
->dop
.execute
.needs_help
= (upcall
->xout
.slow
& SLOW_ACTION
) != 0;
1563 op
->dop
.execute
.probe
= false;
1564 op
->dop
.execute
.mtu
= upcall
->mru
;
1568 /* Execute batch. */
1570 for (i
= 0; i
< n_ops
; i
++) {
1571 opsp
[n_opsp
++] = &ops
[i
].dop
;
1573 dpif_operate(udpif
->dpif
, opsp
, n_opsp
);
1574 for (i
= 0; i
< n_ops
; i
++) {
1575 struct udpif_key
*ukey
= ops
[i
].ukey
;
1578 ovs_mutex_lock(&ukey
->mutex
);
1579 if (ops
[i
].dop
.error
) {
1580 transition_ukey(ukey
, UKEY_EVICTED
);
1581 } else if (ukey
->state
< UKEY_OPERATIONAL
) {
1582 transition_ukey(ukey
, UKEY_OPERATIONAL
);
1584 ovs_mutex_unlock(&ukey
->mutex
);
1590 get_ukey_hash(const ovs_u128
*ufid
, const unsigned pmd_id
)
1592 return hash_2words(ufid
->u32
[0], pmd_id
);
1595 static struct udpif_key
*
1596 ukey_lookup(struct udpif
*udpif
, const ovs_u128
*ufid
, const unsigned pmd_id
)
1598 struct udpif_key
*ukey
;
1599 int idx
= get_ukey_hash(ufid
, pmd_id
) % N_UMAPS
;
1600 struct cmap
*cmap
= &udpif
->ukeys
[idx
].cmap
;
1602 CMAP_FOR_EACH_WITH_HASH (ukey
, cmap_node
,
1603 get_ukey_hash(ufid
, pmd_id
), cmap
) {
1604 if (ovs_u128_equals(ukey
->ufid
, *ufid
)) {
1611 /* Provides safe lockless access of RCU protected 'ukey->actions'. Callers may
1612 * alternatively access the field directly if they take 'ukey->mutex'. */
1614 ukey_get_actions(struct udpif_key
*ukey
, const struct nlattr
**actions
, size_t *size
)
1616 const struct ofpbuf
*buf
= ovsrcu_get(struct ofpbuf
*, &ukey
->actions
);
1617 *actions
= buf
->data
;
1622 ukey_set_actions(struct udpif_key
*ukey
, const struct ofpbuf
*actions
)
1624 struct ofpbuf
*old_actions
= ovsrcu_get_protected(struct ofpbuf
*,
1628 ovsrcu_postpone(ofpbuf_delete
, old_actions
);
1631 ovsrcu_set(&ukey
->actions
, ofpbuf_clone(actions
));
1634 static struct udpif_key
*
1635 ukey_create__(const struct nlattr
*key
, size_t key_len
,
1636 const struct nlattr
*mask
, size_t mask_len
,
1637 bool ufid_present
, const ovs_u128
*ufid
,
1638 const unsigned pmd_id
, const struct ofpbuf
*actions
,
1639 uint64_t reval_seq
, long long int used
,
1640 uint32_t key_recirc_id
, struct xlate_out
*xout
)
1641 OVS_NO_THREAD_SAFETY_ANALYSIS
1643 struct udpif_key
*ukey
= xmalloc(sizeof *ukey
);
1645 memcpy(&ukey
->keybuf
, key
, key_len
);
1646 ukey
->key
= &ukey
->keybuf
.nla
;
1647 ukey
->key_len
= key_len
;
1648 memcpy(&ukey
->maskbuf
, mask
, mask_len
);
1649 ukey
->mask
= &ukey
->maskbuf
.nla
;
1650 ukey
->mask_len
= mask_len
;
1651 ukey
->ufid_present
= ufid_present
;
1653 ukey
->pmd_id
= pmd_id
;
1654 ukey
->hash
= get_ukey_hash(&ukey
->ufid
, pmd_id
);
1656 ovsrcu_init(&ukey
->actions
, NULL
);
1657 ukey_set_actions(ukey
, actions
);
1659 ovs_mutex_init(&ukey
->mutex
);
1660 ukey
->dump_seq
= 0; /* Not yet dumped */
1661 ukey
->reval_seq
= reval_seq
;
1662 ukey
->state
= UKEY_CREATED
;
1663 ukey
->state_thread
= ovsthread_id_self();
1664 ukey
->state_where
= OVS_SOURCE_LOCATOR
;
1665 ukey
->created
= time_msec();
1666 memset(&ukey
->stats
, 0, sizeof ukey
->stats
);
1667 ukey
->stats
.used
= used
;
1668 ukey
->xcache
= NULL
;
1670 ukey
->key_recirc_id
= key_recirc_id
;
1671 recirc_refs_init(&ukey
->recircs
);
1673 /* Take ownership of the action recirc id references. */
1674 recirc_refs_swap(&ukey
->recircs
, &xout
->recircs
);
1680 static struct udpif_key
*
1681 ukey_create_from_upcall(struct upcall
*upcall
, struct flow_wildcards
*wc
)
1683 struct odputil_keybuf keystub
, maskstub
;
1684 struct ofpbuf keybuf
, maskbuf
;
1686 struct odp_flow_key_parms odp_parms
= {
1687 .flow
= upcall
->flow
,
1688 .mask
= wc
? &wc
->masks
: NULL
,
1691 odp_parms
.support
= upcall
->ofproto
->backer
->rt_support
.odp
;
1692 if (upcall
->key_len
) {
1693 ofpbuf_use_const(&keybuf
, upcall
->key
, upcall
->key_len
);
1695 /* dpif-netdev doesn't provide a netlink-formatted flow key in the
1696 * upcall, so convert the upcall's flow here. */
1697 ofpbuf_use_stack(&keybuf
, &keystub
, sizeof keystub
);
1698 odp_flow_key_from_flow(&odp_parms
, &keybuf
);
1701 atomic_read_relaxed(&enable_megaflows
, &megaflow
);
1702 ofpbuf_use_stack(&maskbuf
, &maskstub
, sizeof maskstub
);
1703 if (megaflow
&& wc
) {
1704 odp_parms
.key_buf
= &keybuf
;
1705 odp_flow_key_from_mask(&odp_parms
, &maskbuf
);
1708 return ukey_create__(keybuf
.data
, keybuf
.size
, maskbuf
.data
, maskbuf
.size
,
1709 true, upcall
->ufid
, upcall
->pmd_id
,
1710 &upcall
->put_actions
, upcall
->reval_seq
, 0,
1711 upcall
->have_recirc_ref
? upcall
->recirc
->id
: 0,
1716 ukey_create_from_dpif_flow(const struct udpif
*udpif
,
1717 const struct dpif_flow
*flow
,
1718 struct udpif_key
**ukey
)
1720 struct dpif_flow full_flow
;
1721 struct ofpbuf actions
;
1723 uint64_t stub
[DPIF_FLOW_BUFSIZE
/ 8];
1724 const struct nlattr
*a
;
1727 if (!flow
->key_len
|| !flow
->actions_len
) {
1731 /* If the key or actions were not provided by the datapath, fetch the
1733 ofpbuf_use_stack(&buf
, &stub
, sizeof stub
);
1734 err
= dpif_flow_get(udpif
->dpif
, flow
->key
, flow
->key_len
,
1735 flow
->ufid_present
? &flow
->ufid
: NULL
,
1736 flow
->pmd_id
, &buf
, &full_flow
);
1743 /* Check the flow actions for recirculation action. As recirculation
1744 * relies on OVS userspace internal state, we need to delete all old
1745 * datapath flows with either a non-zero recirc_id in the key, or any
1746 * recirculation actions upon OVS restart. */
1747 NL_ATTR_FOR_EACH (a
, left
, flow
->key
, flow
->key_len
) {
1748 if (nl_attr_type(a
) == OVS_KEY_ATTR_RECIRC_ID
1749 && nl_attr_get_u32(a
) != 0) {
1753 NL_ATTR_FOR_EACH (a
, left
, flow
->actions
, flow
->actions_len
) {
1754 if (nl_attr_type(a
) == OVS_ACTION_ATTR_RECIRC
) {
1759 reval_seq
= seq_read(udpif
->reval_seq
) - 1; /* Ensure revalidation. */
1760 ofpbuf_use_const(&actions
, &flow
->actions
, flow
->actions_len
);
1761 *ukey
= ukey_create__(flow
->key
, flow
->key_len
,
1762 flow
->mask
, flow
->mask_len
, flow
->ufid_present
,
1763 &flow
->ufid
, flow
->pmd_id
, &actions
,
1764 reval_seq
, flow
->stats
.used
, 0, NULL
);
1770 try_ukey_replace(struct umap
*umap
, struct udpif_key
*old_ukey
,
1771 struct udpif_key
*new_ukey
)
1772 OVS_REQUIRES(umap
->mutex
)
1773 OVS_TRY_LOCK(true, new_ukey
->mutex
)
1775 bool replaced
= false;
1777 if (!ovs_mutex_trylock(&old_ukey
->mutex
)) {
1778 if (old_ukey
->state
== UKEY_EVICTED
) {
1779 /* The flow was deleted during the current revalidator dump,
1780 * but its ukey won't be fully cleaned up until the sweep phase.
1781 * In the mean time, we are receiving upcalls for this traffic.
1782 * Expedite the (new) flow install by replacing the ukey. */
1783 ovs_mutex_lock(&new_ukey
->mutex
);
1784 cmap_replace(&umap
->cmap
, &old_ukey
->cmap_node
,
1785 &new_ukey
->cmap_node
, new_ukey
->hash
);
1786 ovsrcu_postpone(ukey_delete__
, old_ukey
);
1787 transition_ukey(old_ukey
, UKEY_DELETED
);
1788 transition_ukey(new_ukey
, UKEY_VISIBLE
);
1791 ovs_mutex_unlock(&old_ukey
->mutex
);
1795 COVERAGE_INC(upcall_ukey_replace
);
1797 COVERAGE_INC(handler_duplicate_upcall
);
1802 /* Attempts to insert a ukey into the shared ukey maps.
1804 * On success, returns true, installs the ukey and returns it in a locked
1805 * state. Otherwise, returns false. */
1807 ukey_install__(struct udpif
*udpif
, struct udpif_key
*new_ukey
)
1808 OVS_TRY_LOCK(true, new_ukey
->mutex
)
1811 struct udpif_key
*old_ukey
;
1813 bool locked
= false;
1815 idx
= new_ukey
->hash
% N_UMAPS
;
1816 umap
= &udpif
->ukeys
[idx
];
1817 ovs_mutex_lock(&umap
->mutex
);
1818 old_ukey
= ukey_lookup(udpif
, &new_ukey
->ufid
, new_ukey
->pmd_id
);
1820 /* Uncommon case: A ukey is already installed with the same UFID. */
1821 if (old_ukey
->key_len
== new_ukey
->key_len
1822 && !memcmp(old_ukey
->key
, new_ukey
->key
, new_ukey
->key_len
)) {
1823 locked
= try_ukey_replace(umap
, old_ukey
, new_ukey
);
1825 struct ds ds
= DS_EMPTY_INITIALIZER
;
1827 odp_format_ufid(&old_ukey
->ufid
, &ds
);
1828 ds_put_cstr(&ds
, " ");
1829 odp_flow_key_format(old_ukey
->key
, old_ukey
->key_len
, &ds
);
1830 ds_put_cstr(&ds
, "\n");
1831 odp_format_ufid(&new_ukey
->ufid
, &ds
);
1832 ds_put_cstr(&ds
, " ");
1833 odp_flow_key_format(new_ukey
->key
, new_ukey
->key_len
, &ds
);
1835 VLOG_WARN_RL(&rl
, "Conflicting ukey for flows:\n%s", ds_cstr(&ds
));
1839 ovs_mutex_lock(&new_ukey
->mutex
);
1840 cmap_insert(&umap
->cmap
, &new_ukey
->cmap_node
, new_ukey
->hash
);
1841 transition_ukey(new_ukey
, UKEY_VISIBLE
);
1844 ovs_mutex_unlock(&umap
->mutex
);
1850 transition_ukey_at(struct udpif_key
*ukey
, enum ukey_state dst
,
1852 OVS_REQUIRES(ukey
->mutex
)
1854 if (dst
< ukey
->state
) {
1855 VLOG_ABORT("Invalid ukey transition %d->%d (last transitioned from "
1856 "thread %u at %s)", ukey
->state
, dst
, ukey
->state_thread
,
1859 if (ukey
->state
== dst
&& dst
== UKEY_OPERATIONAL
) {
1863 /* Valid state transitions:
1864 * UKEY_CREATED -> UKEY_VISIBLE
1865 * Ukey is now visible in the umap.
1866 * UKEY_VISIBLE -> UKEY_OPERATIONAL
1867 * A handler has installed the flow, and the flow is in the datapath.
1868 * UKEY_VISIBLE -> UKEY_EVICTING
1869 * A handler installs the flow, then revalidator sweeps the ukey before
1870 * the flow is dumped. Most likely the flow was installed; start trying
1872 * UKEY_VISIBLE -> UKEY_EVICTED
1873 * A handler attempts to install the flow, but the datapath rejects it.
1874 * Consider that the datapath has already destroyed it.
1875 * UKEY_OPERATIONAL -> UKEY_EVICTING
1876 * A revalidator decides to evict the datapath flow.
1877 * UKEY_EVICTING -> UKEY_EVICTED
1878 * A revalidator has evicted the datapath flow.
1879 * UKEY_EVICTED -> UKEY_DELETED
1880 * A revalidator has removed the ukey from the umap and is deleting it.
1882 if (ukey
->state
== dst
- 1 || (ukey
->state
== UKEY_VISIBLE
&&
1883 dst
< UKEY_DELETED
)) {
1886 struct ds ds
= DS_EMPTY_INITIALIZER
;
1888 odp_format_ufid(&ukey
->ufid
, &ds
);
1889 VLOG_WARN_RL(&rl
, "Invalid state transition for ukey %s: %d -> %d",
1890 ds_cstr(&ds
), ukey
->state
, dst
);
1893 ukey
->state_thread
= ovsthread_id_self();
1894 ukey
->state_where
= where
;
1898 ukey_install(struct udpif
*udpif
, struct udpif_key
*ukey
)
1902 installed
= ukey_install__(udpif
, ukey
);
1904 ovs_mutex_unlock(&ukey
->mutex
);
1910 /* Searches for a ukey in 'udpif->ukeys' that matches 'flow' and attempts to
1911 * lock the ukey. If the ukey does not exist, create it.
1913 * Returns 0 on success, setting *result to the matching ukey and returning it
1914 * in a locked state. Otherwise, returns an errno and clears *result. EBUSY
1915 * indicates that another thread is handling this flow. Other errors indicate
1916 * an unexpected condition creating a new ukey.
1918 * *error is an output parameter provided to appease the threadsafety analyser,
1919 * and its value matches the return value. */
1921 ukey_acquire(struct udpif
*udpif
, const struct dpif_flow
*flow
,
1922 struct udpif_key
**result
, int *error
)
1923 OVS_TRY_LOCK(0, (*result
)->mutex
)
1925 struct udpif_key
*ukey
;
1928 ukey
= ukey_lookup(udpif
, &flow
->ufid
, flow
->pmd_id
);
1930 retval
= ovs_mutex_trylock(&ukey
->mutex
);
1932 /* Usually we try to avoid installing flows from revalidator threads,
1933 * because locking on a umap may cause handler threads to block.
1934 * However there are certain cases, like when ovs-vswitchd is
1935 * restarted, where it is desirable to handle flows that exist in the
1936 * datapath gracefully (ie, don't just clear the datapath). */
1939 retval
= ukey_create_from_dpif_flow(udpif
, flow
, &ukey
);
1943 install
= ukey_install__(udpif
, ukey
);
1947 ukey_delete__(ukey
);
1963 ukey_delete__(struct udpif_key
*ukey
)
1964 OVS_NO_THREAD_SAFETY_ANALYSIS
1967 if (ukey
->key_recirc_id
) {
1968 recirc_free_id(ukey
->key_recirc_id
);
1970 recirc_refs_unref(&ukey
->recircs
);
1971 xlate_cache_delete(ukey
->xcache
);
1972 ofpbuf_delete(ovsrcu_get(struct ofpbuf
*, &ukey
->actions
));
1973 ovs_mutex_destroy(&ukey
->mutex
);
1979 ukey_delete(struct umap
*umap
, struct udpif_key
*ukey
)
1980 OVS_REQUIRES(umap
->mutex
)
1982 ovs_mutex_lock(&ukey
->mutex
);
1983 if (ukey
->state
< UKEY_DELETED
) {
1984 cmap_remove(&umap
->cmap
, &ukey
->cmap_node
, ukey
->hash
);
1985 ovsrcu_postpone(ukey_delete__
, ukey
);
1986 transition_ukey(ukey
, UKEY_DELETED
);
1988 ovs_mutex_unlock(&ukey
->mutex
);
1992 should_revalidate(const struct udpif
*udpif
, uint64_t packets
,
1995 long long int metric
, now
, duration
;
1998 /* Always revalidate the first time a flow is dumped. */
2002 if (udpif
->dump_duration
< 200) {
2003 /* We are likely to handle full revalidation for the flows. */
2007 /* Calculate the mean time between seeing these packets. If this
2008 * exceeds the threshold, then delete the flow rather than performing
2009 * costly revalidation for flows that aren't being hit frequently.
2011 * This is targeted at situations where the dump_duration is high (~1s),
2012 * and revalidation is triggered by a call to udpif_revalidate(). In
2013 * these situations, revalidation of all flows causes fluctuations in the
2014 * flow_limit due to the interaction with the dump_duration and max_idle.
2015 * This tends to result in deletion of low-throughput flows anyway, so
2016 * skip the revalidation and just delete those flows. */
2017 packets
= MAX(packets
, 1);
2018 now
= MAX(used
, time_msec());
2019 duration
= now
- used
;
2020 metric
= duration
/ packets
;
2023 /* The flow is receiving more than ~5pps, so keep it. */
2029 struct reval_context
{
2030 /* Optional output parameters */
2031 struct flow_wildcards
*wc
;
2032 struct ofpbuf
*odp_actions
;
2033 struct netflow
**netflow
;
2034 struct xlate_cache
*xcache
;
2036 /* Required output parameters */
2037 struct xlate_out xout
;
2041 /* Translates 'key' into a flow, populating 'ctx' as it goes along.
2043 * Returns 0 on success, otherwise a positive errno value.
2045 * The caller is responsible for uninitializing ctx->xout on success.
2048 xlate_key(struct udpif
*udpif
, const struct nlattr
*key
, unsigned int len
,
2049 const struct dpif_flow_stats
*push
, struct reval_context
*ctx
)
2051 struct ofproto_dpif
*ofproto
;
2052 ofp_port_t ofp_in_port
;
2053 enum odp_key_fitness fitness
;
2054 struct xlate_in xin
;
2057 fitness
= odp_flow_key_to_flow(key
, len
, &ctx
->flow
);
2058 if (fitness
== ODP_FIT_ERROR
) {
2062 error
= xlate_lookup(udpif
->backer
, &ctx
->flow
, &ofproto
, NULL
, NULL
,
2063 ctx
->netflow
, &ofp_in_port
);
2068 xlate_in_init(&xin
, ofproto
, ofproto_dpif_get_tables_version(ofproto
),
2069 &ctx
->flow
, ofp_in_port
, NULL
, push
->tcp_flags
,
2070 NULL
, ctx
->wc
, ctx
->odp_actions
);
2071 if (push
->n_packets
) {
2072 xin
.resubmit_stats
= push
;
2073 xin
.allow_side_effects
= true;
2075 xin
.xcache
= ctx
->xcache
;
2076 xlate_actions(&xin
, &ctx
->xout
);
2077 if (fitness
== ODP_FIT_TOO_LITTLE
) {
2078 ctx
->xout
.slow
|= SLOW_MATCH
;
2085 xlate_ukey(struct udpif
*udpif
, const struct udpif_key
*ukey
,
2086 uint16_t tcp_flags
, struct reval_context
*ctx
)
2088 struct dpif_flow_stats push
= {
2089 .tcp_flags
= tcp_flags
,
2091 return xlate_key(udpif
, ukey
->key
, ukey
->key_len
, &push
, ctx
);
2095 populate_xcache(struct udpif
*udpif
, struct udpif_key
*ukey
,
2097 OVS_REQUIRES(ukey
->mutex
)
2099 struct reval_context ctx
= {
2100 .odp_actions
= NULL
,
2106 ovs_assert(!ukey
->xcache
);
2107 ukey
->xcache
= ctx
.xcache
= xlate_cache_new();
2108 error
= xlate_ukey(udpif
, ukey
, tcp_flags
, &ctx
);
2112 xlate_out_uninit(&ctx
.xout
);
2117 static enum reval_result
2118 revalidate_ukey__(struct udpif
*udpif
, const struct udpif_key
*ukey
,
2119 uint16_t tcp_flags
, struct ofpbuf
*odp_actions
,
2120 struct recirc_refs
*recircs
, struct xlate_cache
*xcache
)
2122 struct xlate_out
*xoutp
;
2123 struct netflow
*netflow
;
2124 struct flow_wildcards dp_mask
, wc
;
2125 enum reval_result result
;
2126 struct reval_context ctx
= {
2127 .odp_actions
= odp_actions
,
2128 .netflow
= &netflow
,
2133 result
= UKEY_DELETE
;
2137 if (xlate_ukey(udpif
, ukey
, tcp_flags
, &ctx
)) {
2142 if (xoutp
->avoid_caching
) {
2147 struct ofproto_dpif
*ofproto
;
2148 ofp_port_t ofp_in_port
;
2150 ofproto
= xlate_lookup_ofproto(udpif
->backer
, &ctx
.flow
, &ofp_in_port
);
2152 ofpbuf_clear(odp_actions
);
2158 compose_slow_path(udpif
, xoutp
, &ctx
.flow
, ctx
.flow
.in_port
.odp_port
,
2159 ofp_in_port
, odp_actions
,
2160 ofproto
->up
.slowpath_meter_id
, &ofproto
->uuid
);
2163 if (odp_flow_key_to_mask(ukey
->mask
, ukey
->mask_len
, &dp_mask
, &ctx
.flow
)
2168 /* Do not modify if any bit is wildcarded by the installed datapath flow,
2169 * but not the newly revalidated wildcard mask (wc), i.e., if revalidation
2170 * tells that the datapath flow is now too generic and must be narrowed
2171 * down. Note that we do not know if the datapath has ignored any of the
2172 * wildcarded bits, so we may be overly conservative here. */
2173 if (flow_wildcards_has_extra(&dp_mask
, ctx
.wc
)) {
2177 if (!ofpbuf_equal(odp_actions
,
2178 ovsrcu_get(struct ofpbuf
*, &ukey
->actions
))) {
2179 /* The datapath mask was OK, but the actions seem to have changed.
2180 * Let's modify it in place. */
2181 result
= UKEY_MODIFY
;
2182 /* Transfer recirc action ID references to the caller. */
2183 recirc_refs_swap(recircs
, &xoutp
->recircs
);
2190 if (netflow
&& result
== UKEY_DELETE
) {
2191 netflow_flow_clear(netflow
, &ctx
.flow
);
2193 xlate_out_uninit(xoutp
);
2197 /* Verifies that the datapath actions of 'ukey' are still correct, and pushes
2200 * Returns a recommended action for 'ukey', options include:
2201 * UKEY_DELETE The ukey should be deleted.
2202 * UKEY_KEEP The ukey is fine as is.
2203 * UKEY_MODIFY The ukey's actions should be changed but is otherwise
2204 * fine. Callers should change the actions to those found
2205 * in the caller supplied 'odp_actions' buffer. The
2206 * recirculation references can be found in 'recircs' and
2207 * must be handled by the caller.
2209 * If the result is UKEY_MODIFY, then references to all recirc_ids used by the
2210 * new flow will be held within 'recircs' (which may be none).
2212 * The caller is responsible for both initializing 'recircs' prior this call,
2213 * and ensuring any references are eventually freed.
2215 static enum reval_result
2216 revalidate_ukey(struct udpif
*udpif
, struct udpif_key
*ukey
,
2217 const struct dpif_flow_stats
*stats
,
2218 struct ofpbuf
*odp_actions
, uint64_t reval_seq
,
2219 struct recirc_refs
*recircs
)
2220 OVS_REQUIRES(ukey
->mutex
)
2222 bool need_revalidate
= ukey
->reval_seq
!= reval_seq
;
2223 enum reval_result result
= UKEY_DELETE
;
2224 struct dpif_flow_stats push
;
2226 ofpbuf_clear(odp_actions
);
2228 push
.used
= stats
->used
;
2229 push
.tcp_flags
= stats
->tcp_flags
;
2230 push
.n_packets
= (stats
->n_packets
> ukey
->stats
.n_packets
2231 ? stats
->n_packets
- ukey
->stats
.n_packets
2233 push
.n_bytes
= (stats
->n_bytes
> ukey
->stats
.n_bytes
2234 ? stats
->n_bytes
- ukey
->stats
.n_bytes
2237 if (need_revalidate
) {
2238 if (should_revalidate(udpif
, push
.n_packets
, ukey
->stats
.used
)) {
2239 if (!ukey
->xcache
) {
2240 ukey
->xcache
= xlate_cache_new();
2242 xlate_cache_clear(ukey
->xcache
);
2244 result
= revalidate_ukey__(udpif
, ukey
, push
.tcp_flags
,
2245 odp_actions
, recircs
, ukey
->xcache
);
2246 } /* else delete; too expensive to revalidate */
2247 } else if (!push
.n_packets
|| ukey
->xcache
2248 || !populate_xcache(udpif
, ukey
, push
.tcp_flags
)) {
2252 /* Stats for deleted flows will be attributed upon flow deletion. Skip. */
2253 if (result
!= UKEY_DELETE
) {
2254 xlate_push_stats(ukey
->xcache
, &push
);
2255 ukey
->stats
= *stats
;
2256 ukey
->reval_seq
= reval_seq
;
2263 delete_op_init__(struct udpif
*udpif
, struct ukey_op
*op
,
2264 const struct dpif_flow
*flow
)
2267 op
->dop
.type
= DPIF_OP_FLOW_DEL
;
2268 op
->dop
.flow_del
.key
= flow
->key
;
2269 op
->dop
.flow_del
.key_len
= flow
->key_len
;
2270 op
->dop
.flow_del
.ufid
= flow
->ufid_present
? &flow
->ufid
: NULL
;
2271 op
->dop
.flow_del
.pmd_id
= flow
->pmd_id
;
2272 op
->dop
.flow_del
.stats
= &op
->stats
;
2273 op
->dop
.flow_del
.terse
= udpif_use_ufid(udpif
);
2277 delete_op_init(struct udpif
*udpif
, struct ukey_op
*op
, struct udpif_key
*ukey
)
2280 op
->dop
.type
= DPIF_OP_FLOW_DEL
;
2281 op
->dop
.flow_del
.key
= ukey
->key
;
2282 op
->dop
.flow_del
.key_len
= ukey
->key_len
;
2283 op
->dop
.flow_del
.ufid
= ukey
->ufid_present
? &ukey
->ufid
: NULL
;
2284 op
->dop
.flow_del
.pmd_id
= ukey
->pmd_id
;
2285 op
->dop
.flow_del
.stats
= &op
->stats
;
2286 op
->dop
.flow_del
.terse
= udpif_use_ufid(udpif
);
2290 put_op_init(struct ukey_op
*op
, struct udpif_key
*ukey
,
2291 enum dpif_flow_put_flags flags
)
2294 op
->dop
.type
= DPIF_OP_FLOW_PUT
;
2295 op
->dop
.flow_put
.flags
= flags
;
2296 op
->dop
.flow_put
.key
= ukey
->key
;
2297 op
->dop
.flow_put
.key_len
= ukey
->key_len
;
2298 op
->dop
.flow_put
.mask
= ukey
->mask
;
2299 op
->dop
.flow_put
.mask_len
= ukey
->mask_len
;
2300 op
->dop
.flow_put
.ufid
= ukey
->ufid_present
? &ukey
->ufid
: NULL
;
2301 op
->dop
.flow_put
.pmd_id
= ukey
->pmd_id
;
2302 op
->dop
.flow_put
.stats
= NULL
;
2303 ukey_get_actions(ukey
, &op
->dop
.flow_put
.actions
,
2304 &op
->dop
.flow_put
.actions_len
);
2307 /* Executes datapath operations 'ops' and attributes stats retrieved from the
2308 * datapath as part of those operations. */
2310 push_dp_ops(struct udpif
*udpif
, struct ukey_op
*ops
, size_t n_ops
)
2312 struct dpif_op
*opsp
[REVALIDATE_MAX_BATCH
];
2315 ovs_assert(n_ops
<= REVALIDATE_MAX_BATCH
);
2316 for (i
= 0; i
< n_ops
; i
++) {
2317 opsp
[i
] = &ops
[i
].dop
;
2319 dpif_operate(udpif
->dpif
, opsp
, n_ops
);
2321 for (i
= 0; i
< n_ops
; i
++) {
2322 struct ukey_op
*op
= &ops
[i
];
2323 struct dpif_flow_stats
*push
, *stats
, push_buf
;
2325 stats
= op
->dop
.flow_del
.stats
;
2328 if (op
->dop
.type
!= DPIF_OP_FLOW_DEL
) {
2329 /* Only deleted flows need their stats pushed. */
2333 if (op
->dop
.error
) {
2334 /* flow_del error, 'stats' is unusable. */
2336 ovs_mutex_lock(&op
->ukey
->mutex
);
2337 transition_ukey(op
->ukey
, UKEY_EVICTED
);
2338 ovs_mutex_unlock(&op
->ukey
->mutex
);
2344 ovs_mutex_lock(&op
->ukey
->mutex
);
2345 transition_ukey(op
->ukey
, UKEY_EVICTED
);
2346 push
->used
= MAX(stats
->used
, op
->ukey
->stats
.used
);
2347 push
->tcp_flags
= stats
->tcp_flags
| op
->ukey
->stats
.tcp_flags
;
2348 push
->n_packets
= stats
->n_packets
- op
->ukey
->stats
.n_packets
;
2349 push
->n_bytes
= stats
->n_bytes
- op
->ukey
->stats
.n_bytes
;
2350 ovs_mutex_unlock(&op
->ukey
->mutex
);
2355 if (push
->n_packets
|| netflow_exists()) {
2356 const struct nlattr
*key
= op
->dop
.flow_del
.key
;
2357 size_t key_len
= op
->dop
.flow_del
.key_len
;
2358 struct netflow
*netflow
;
2359 struct reval_context ctx
= {
2360 .netflow
= &netflow
,
2365 ovs_mutex_lock(&op
->ukey
->mutex
);
2366 if (op
->ukey
->xcache
) {
2367 xlate_push_stats(op
->ukey
->xcache
, push
);
2368 ovs_mutex_unlock(&op
->ukey
->mutex
);
2371 ovs_mutex_unlock(&op
->ukey
->mutex
);
2372 key
= op
->ukey
->key
;
2373 key_len
= op
->ukey
->key_len
;
2376 error
= xlate_key(udpif
, key
, key_len
, push
, &ctx
);
2378 static struct vlog_rate_limit rll
= VLOG_RATE_LIMIT_INIT(1, 5);
2379 VLOG_WARN_RL(&rll
, "xlate_key failed (%s)!",
2380 ovs_strerror(error
));
2382 xlate_out_uninit(&ctx
.xout
);
2384 netflow_flow_clear(netflow
, &ctx
.flow
);
2391 /* Executes datapath operations 'ops', attributes stats retrieved from the
2392 * datapath, and deletes ukeys corresponding to deleted flows. */
2394 push_ukey_ops(struct udpif
*udpif
, struct umap
*umap
,
2395 struct ukey_op
*ops
, size_t n_ops
)
2399 push_dp_ops(udpif
, ops
, n_ops
);
2400 ovs_mutex_lock(&umap
->mutex
);
2401 for (i
= 0; i
< n_ops
; i
++) {
2402 if (ops
[i
].dop
.type
== DPIF_OP_FLOW_DEL
) {
2403 ukey_delete(umap
, ops
[i
].ukey
);
2406 ovs_mutex_unlock(&umap
->mutex
);
2410 log_unexpected_flow(const struct dpif_flow
*flow
, int error
)
2412 struct ds ds
= DS_EMPTY_INITIALIZER
;
2414 ds_put_format(&ds
, "Failed to acquire udpif_key corresponding to "
2415 "unexpected flow (%s): ", ovs_strerror(error
));
2416 odp_format_ufid(&flow
->ufid
, &ds
);
2418 static struct vlog_rate_limit rll
= VLOG_RATE_LIMIT_INIT(10, 60);
2419 VLOG_WARN_RL(&rll
, "%s", ds_cstr(&ds
));
2425 reval_op_init(struct ukey_op
*op
, enum reval_result result
,
2426 struct udpif
*udpif
, struct udpif_key
*ukey
,
2427 struct recirc_refs
*recircs
, struct ofpbuf
*odp_actions
)
2428 OVS_REQUIRES(ukey
->mutex
)
2430 if (result
== UKEY_DELETE
) {
2431 delete_op_init(udpif
, op
, ukey
);
2432 transition_ukey(ukey
, UKEY_EVICTING
);
2433 } else if (result
== UKEY_MODIFY
) {
2434 /* Store the new recircs. */
2435 recirc_refs_swap(&ukey
->recircs
, recircs
);
2436 /* Release old recircs. */
2437 recirc_refs_unref(recircs
);
2438 /* ukey->key_recirc_id remains, as the key is the same as before. */
2440 ukey_set_actions(ukey
, odp_actions
);
2441 put_op_init(op
, ukey
, DPIF_FP_MODIFY
);
2446 revalidate(struct revalidator
*revalidator
)
2448 uint64_t odp_actions_stub
[1024 / 8];
2449 struct ofpbuf odp_actions
= OFPBUF_STUB_INITIALIZER(odp_actions_stub
);
2451 struct udpif
*udpif
= revalidator
->udpif
;
2452 struct dpif_flow_dump_thread
*dump_thread
;
2453 uint64_t dump_seq
, reval_seq
;
2454 unsigned int flow_limit
;
2456 dump_seq
= seq_read(udpif
->dump_seq
);
2457 reval_seq
= seq_read(udpif
->reval_seq
);
2458 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
2459 dump_thread
= dpif_flow_dump_thread_create(udpif
->dump
);
2461 struct ukey_op ops
[REVALIDATE_MAX_BATCH
];
2464 struct dpif_flow flows
[REVALIDATE_MAX_BATCH
];
2465 const struct dpif_flow
*f
;
2468 long long int max_idle
;
2473 n_dumped
= dpif_flow_dump_next(dump_thread
, flows
, ARRAY_SIZE(flows
));
2480 /* In normal operation we want to keep flows around until they have
2481 * been idle for 'ofproto_max_idle' milliseconds. However:
2483 * - If the number of datapath flows climbs above 'flow_limit',
2484 * drop that down to 100 ms to try to bring the flows down to
2487 * - If the number of datapath flows climbs above twice
2488 * 'flow_limit', delete all the datapath flows as an emergency
2489 * measure. (We reassess this condition for the next batch of
2490 * datapath flows, so we will recover before all the flows are
2492 n_dp_flows
= udpif_get_n_flows(udpif
);
2493 kill_them_all
= n_dp_flows
> flow_limit
* 2;
2494 max_idle
= n_dp_flows
> flow_limit
? 100 : ofproto_max_idle
;
2496 for (f
= flows
; f
< &flows
[n_dumped
]; f
++) {
2497 long long int used
= f
->stats
.used
;
2498 struct recirc_refs recircs
= RECIRC_REFS_EMPTY_INITIALIZER
;
2499 enum reval_result result
;
2500 struct udpif_key
*ukey
;
2501 bool already_dumped
;
2504 if (ukey_acquire(udpif
, f
, &ukey
, &error
)) {
2505 if (error
== EBUSY
) {
2506 /* Another thread is processing this flow, so don't bother
2508 COVERAGE_INC(upcall_ukey_contention
);
2510 log_unexpected_flow(f
, error
);
2511 if (error
!= ENOENT
) {
2512 delete_op_init__(udpif
, &ops
[n_ops
++], f
);
2518 already_dumped
= ukey
->dump_seq
== dump_seq
;
2519 if (already_dumped
) {
2520 /* The flow has already been handled during this flow dump
2521 * operation. Skip it. */
2523 COVERAGE_INC(dumped_duplicate_flow
);
2525 COVERAGE_INC(dumped_new_flow
);
2527 ovs_mutex_unlock(&ukey
->mutex
);
2531 if (ukey
->state
<= UKEY_OPERATIONAL
) {
2532 /* The flow is now confirmed to be in the datapath. */
2533 transition_ukey(ukey
, UKEY_OPERATIONAL
);
2535 VLOG_INFO("Unexpected ukey transition from state %d "
2536 "(last transitioned from thread %u at %s)",
2537 ukey
->state
, ukey
->state_thread
, ukey
->state_where
);
2538 ovs_mutex_unlock(&ukey
->mutex
);
2543 used
= ukey
->created
;
2545 if (kill_them_all
|| (used
&& used
< now
- max_idle
)) {
2546 result
= UKEY_DELETE
;
2548 result
= revalidate_ukey(udpif
, ukey
, &f
->stats
, &odp_actions
,
2549 reval_seq
, &recircs
);
2551 ukey
->dump_seq
= dump_seq
;
2553 if (result
!= UKEY_KEEP
) {
2554 /* Takes ownership of 'recircs'. */
2555 reval_op_init(&ops
[n_ops
++], result
, udpif
, ukey
, &recircs
,
2558 ovs_mutex_unlock(&ukey
->mutex
);
2562 /* Push datapath ops but defer ukey deletion to 'sweep' phase. */
2563 push_dp_ops(udpif
, ops
, n_ops
);
2567 dpif_flow_dump_thread_destroy(dump_thread
);
2568 ofpbuf_uninit(&odp_actions
);
2571 /* Pauses the 'revalidator', can only proceed after main thread
2572 * calls udpif_resume_revalidators(). */
2574 revalidator_pause(struct revalidator
*revalidator
)
2576 /* The first block is for sync'ing the pause with main thread. */
2577 ovs_barrier_block(&revalidator
->udpif
->pause_barrier
);
2578 /* The second block is for pausing until main thread resumes. */
2579 ovs_barrier_block(&revalidator
->udpif
->pause_barrier
);
2583 revalidator_sweep__(struct revalidator
*revalidator
, bool purge
)
2585 struct udpif
*udpif
;
2586 uint64_t dump_seq
, reval_seq
;
2589 udpif
= revalidator
->udpif
;
2590 dump_seq
= seq_read(udpif
->dump_seq
);
2591 reval_seq
= seq_read(udpif
->reval_seq
);
2592 slice
= revalidator
- udpif
->revalidators
;
2593 ovs_assert(slice
< udpif
->n_revalidators
);
2595 for (int i
= slice
; i
< N_UMAPS
; i
+= udpif
->n_revalidators
) {
2596 uint64_t odp_actions_stub
[1024 / 8];
2597 struct ofpbuf odp_actions
= OFPBUF_STUB_INITIALIZER(odp_actions_stub
);
2599 struct ukey_op ops
[REVALIDATE_MAX_BATCH
];
2600 struct udpif_key
*ukey
;
2601 struct umap
*umap
= &udpif
->ukeys
[i
];
2604 CMAP_FOR_EACH(ukey
, cmap_node
, &umap
->cmap
) {
2605 enum ukey_state ukey_state
;
2607 /* Handler threads could be holding a ukey lock while it installs a
2608 * new flow, so don't hang around waiting for access to it. */
2609 if (ovs_mutex_trylock(&ukey
->mutex
)) {
2612 ukey_state
= ukey
->state
;
2613 if (ukey_state
== UKEY_OPERATIONAL
2614 || (ukey_state
== UKEY_VISIBLE
&& purge
)) {
2615 struct recirc_refs recircs
= RECIRC_REFS_EMPTY_INITIALIZER
;
2616 bool seq_mismatch
= (ukey
->dump_seq
!= dump_seq
2617 && ukey
->reval_seq
!= reval_seq
);
2618 enum reval_result result
;
2621 result
= UKEY_DELETE
;
2622 } else if (!seq_mismatch
) {
2625 struct dpif_flow_stats stats
;
2626 COVERAGE_INC(revalidate_missed_dp_flow
);
2627 memset(&stats
, 0, sizeof stats
);
2628 result
= revalidate_ukey(udpif
, ukey
, &stats
, &odp_actions
,
2629 reval_seq
, &recircs
);
2631 if (result
!= UKEY_KEEP
) {
2632 /* Clears 'recircs' if filled by revalidate_ukey(). */
2633 reval_op_init(&ops
[n_ops
++], result
, udpif
, ukey
, &recircs
,
2637 ovs_mutex_unlock(&ukey
->mutex
);
2639 if (ukey_state
== UKEY_EVICTED
) {
2640 /* The common flow deletion case involves deletion of the flow
2641 * during the dump phase and ukey deletion here. */
2642 ovs_mutex_lock(&umap
->mutex
);
2643 ukey_delete(umap
, ukey
);
2644 ovs_mutex_unlock(&umap
->mutex
);
2647 if (n_ops
== REVALIDATE_MAX_BATCH
) {
2648 /* Update/delete missed flows and clean up corresponding ukeys
2650 push_ukey_ops(udpif
, umap
, ops
, n_ops
);
2656 push_ukey_ops(udpif
, umap
, ops
, n_ops
);
2659 ofpbuf_uninit(&odp_actions
);
2665 revalidator_sweep(struct revalidator
*revalidator
)
2667 revalidator_sweep__(revalidator
, false);
2671 revalidator_purge(struct revalidator
*revalidator
)
2673 revalidator_sweep__(revalidator
, true);
2676 /* In reaction to dpif purge, purges all 'ukey's with same 'pmd_id'. */
2678 dp_purge_cb(void *aux
, unsigned pmd_id
)
2679 OVS_NO_THREAD_SAFETY_ANALYSIS
2681 struct udpif
*udpif
= aux
;
2684 udpif_pause_revalidators(udpif
);
2685 for (i
= 0; i
< N_UMAPS
; i
++) {
2686 struct ukey_op ops
[REVALIDATE_MAX_BATCH
];
2687 struct udpif_key
*ukey
;
2688 struct umap
*umap
= &udpif
->ukeys
[i
];
2691 CMAP_FOR_EACH(ukey
, cmap_node
, &umap
->cmap
) {
2692 if (ukey
->pmd_id
== pmd_id
) {
2693 delete_op_init(udpif
, &ops
[n_ops
++], ukey
);
2694 transition_ukey(ukey
, UKEY_EVICTING
);
2696 if (n_ops
== REVALIDATE_MAX_BATCH
) {
2697 push_ukey_ops(udpif
, umap
, ops
, n_ops
);
2704 push_ukey_ops(udpif
, umap
, ops
, n_ops
);
2709 udpif_resume_revalidators(udpif
);
2713 upcall_unixctl_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2714 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
2716 struct ds ds
= DS_EMPTY_INITIALIZER
;
2717 struct udpif
*udpif
;
2719 LIST_FOR_EACH (udpif
, list_node
, &all_udpifs
) {
2720 unsigned int flow_limit
;
2724 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
2725 ufid_enabled
= udpif_use_ufid(udpif
);
2727 ds_put_format(&ds
, "%s:\n", dpif_name(udpif
->dpif
));
2728 ds_put_format(&ds
, " flows : (current %lu)"
2729 " (avg %u) (max %u) (limit %u)\n", udpif_get_n_flows(udpif
),
2730 udpif
->avg_n_flows
, udpif
->max_n_flows
, flow_limit
);
2731 ds_put_format(&ds
, " dump duration : %lldms\n", udpif
->dump_duration
);
2732 ds_put_format(&ds
, " ufid enabled : ");
2734 ds_put_format(&ds
, "true\n");
2736 ds_put_format(&ds
, "false\n");
2738 ds_put_char(&ds
, '\n');
2740 for (i
= 0; i
< n_revalidators
; i
++) {
2741 struct revalidator
*revalidator
= &udpif
->revalidators
[i
];
2742 int j
, elements
= 0;
2744 for (j
= i
; j
< N_UMAPS
; j
+= n_revalidators
) {
2745 elements
+= cmap_count(&udpif
->ukeys
[j
].cmap
);
2747 ds_put_format(&ds
, " %u: (keys %d)\n", revalidator
->id
, elements
);
2751 unixctl_command_reply(conn
, ds_cstr(&ds
));
2755 /* Disable using the megaflows.
2757 * This command is only needed for advanced debugging, so it's not
2758 * documented in the man page. */
2760 upcall_unixctl_disable_megaflows(struct unixctl_conn
*conn
,
2761 int argc OVS_UNUSED
,
2762 const char *argv
[] OVS_UNUSED
,
2763 void *aux OVS_UNUSED
)
2765 atomic_store_relaxed(&enable_megaflows
, false);
2766 udpif_flush_all_datapaths();
2767 unixctl_command_reply(conn
, "megaflows disabled");
2770 /* Re-enable using megaflows.
2772 * This command is only needed for advanced debugging, so it's not
2773 * documented in the man page. */
2775 upcall_unixctl_enable_megaflows(struct unixctl_conn
*conn
,
2776 int argc OVS_UNUSED
,
2777 const char *argv
[] OVS_UNUSED
,
2778 void *aux OVS_UNUSED
)
2780 atomic_store_relaxed(&enable_megaflows
, true);
2781 udpif_flush_all_datapaths();
2782 unixctl_command_reply(conn
, "megaflows enabled");
2785 /* Disable skipping flow attributes during flow dump.
2787 * This command is only needed for advanced debugging, so it's not
2788 * documented in the man page. */
2790 upcall_unixctl_disable_ufid(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2791 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
2793 atomic_store_relaxed(&enable_ufid
, false);
2794 unixctl_command_reply(conn
, "Datapath dumping tersely using UFID disabled");
2797 /* Re-enable skipping flow attributes during flow dump.
2799 * This command is only needed for advanced debugging, so it's not documented
2800 * in the man page. */
2802 upcall_unixctl_enable_ufid(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2803 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
2805 atomic_store_relaxed(&enable_ufid
, true);
2806 unixctl_command_reply(conn
, "Datapath dumping tersely using UFID enabled "
2807 "for supported datapaths");
2810 /* Set the flow limit.
2812 * This command is only needed for advanced debugging, so it's not
2813 * documented in the man page. */
2815 upcall_unixctl_set_flow_limit(struct unixctl_conn
*conn
,
2816 int argc OVS_UNUSED
,
2818 void *aux OVS_UNUSED
)
2820 struct ds ds
= DS_EMPTY_INITIALIZER
;
2821 struct udpif
*udpif
;
2822 unsigned int flow_limit
= atoi(argv
[1]);
2824 LIST_FOR_EACH (udpif
, list_node
, &all_udpifs
) {
2825 atomic_store_relaxed(&udpif
->flow_limit
, flow_limit
);
2827 ds_put_format(&ds
, "set flow_limit to %u\n", flow_limit
);
2828 unixctl_command_reply(conn
, ds_cstr(&ds
));
2833 upcall_unixctl_dump_wait(struct unixctl_conn
*conn
,
2834 int argc OVS_UNUSED
,
2835 const char *argv
[] OVS_UNUSED
,
2836 void *aux OVS_UNUSED
)
2838 if (ovs_list_is_singleton(&all_udpifs
)) {
2839 struct udpif
*udpif
= NULL
;
2842 udpif
= OBJECT_CONTAINING(ovs_list_front(&all_udpifs
), udpif
, list_node
);
2843 len
= (udpif
->n_conns
+ 1) * sizeof *udpif
->conns
;
2844 udpif
->conn_seq
= seq_read(udpif
->dump_seq
);
2845 udpif
->conns
= xrealloc(udpif
->conns
, len
);
2846 udpif
->conns
[udpif
->n_conns
++] = conn
;
2848 unixctl_command_reply_error(conn
, "can't wait on multiple udpifs.");
2853 upcall_unixctl_purge(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2854 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
2856 struct udpif
*udpif
;
2858 LIST_FOR_EACH (udpif
, list_node
, &all_udpifs
) {
2861 for (n
= 0; n
< udpif
->n_revalidators
; n
++) {
2862 revalidator_purge(&udpif
->revalidators
[n
]);
2865 unixctl_command_reply(conn
, "");