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"
39 #include "poll-loop.h"
42 #include "openvswitch/vlog.h"
44 #define MAX_QUEUE_LENGTH 512
45 #define UPCALL_MAX_BATCH 64
46 #define REVALIDATE_MAX_BATCH 50
48 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_upcall
);
50 COVERAGE_DEFINE(dumped_duplicate_flow
);
51 COVERAGE_DEFINE(dumped_new_flow
);
52 COVERAGE_DEFINE(handler_duplicate_upcall
);
53 COVERAGE_DEFINE(upcall_ukey_contention
);
54 COVERAGE_DEFINE(upcall_ukey_replace
);
55 COVERAGE_DEFINE(revalidate_missed_dp_flow
);
57 /* A thread that reads upcalls from dpif, forwards each upcall's packet,
58 * and possibly sets up a kernel flow as a cache. */
60 struct udpif
*udpif
; /* Parent udpif. */
61 pthread_t thread
; /* Thread ID. */
62 uint32_t handler_id
; /* Handler id. */
65 /* In the absence of a multiple-writer multiple-reader datastructure for
66 * storing udpif_keys ("ukeys"), we use a large number of cmaps, each with its
67 * own lock for writing. */
68 #define N_UMAPS 512 /* per udpif. */
70 struct ovs_mutex mutex
; /* Take for writing to the following. */
71 struct cmap cmap
; /* Datapath flow keys. */
74 /* A thread that processes datapath flows, updates OpenFlow statistics, and
75 * updates or removes them if necessary.
77 * Revalidator threads operate in two phases: "dump" and "sweep". In between
78 * each phase, all revalidators sync up so that all revalidator threads are
79 * either in one phase or the other, but not a combination.
81 * During the dump phase, revalidators fetch flows from the datapath and
82 * attribute the statistics to OpenFlow rules. Each datapath flow has a
83 * corresponding ukey which caches the most recently seen statistics. If
84 * a flow needs to be deleted (for example, because it is unused over a
85 * period of time), revalidator threads may delete the flow during the
86 * dump phase. The datapath is not guaranteed to reliably dump all flows
87 * from the datapath, and there is no mapping between datapath flows to
88 * revalidators, so a particular flow may be handled by zero or more
89 * revalidators during a single dump phase. To avoid duplicate attribution
90 * of statistics, ukeys are never deleted during this phase.
92 * During the sweep phase, each revalidator takes ownership of a different
93 * slice of umaps and sweeps through all ukeys in those umaps to figure out
94 * whether they need to be deleted. During this phase, revalidators may
95 * fetch individual flows which were not dumped during the dump phase to
96 * validate them and attribute statistics.
99 struct udpif
*udpif
; /* Parent udpif. */
100 pthread_t thread
; /* Thread ID. */
101 unsigned int id
; /* ovsthread_id_self(). */
104 /* An upcall handler for ofproto_dpif.
106 * udpif keeps records of two kind of logically separate units:
111 * - An array of 'struct handler's for upcall handling and flow
117 * - Revalidation threads which read the datapath flow table and maintains
121 struct ovs_list list_node
; /* In all_udpifs list. */
123 struct dpif
*dpif
; /* Datapath handle. */
124 struct dpif_backer
*backer
; /* Opaque dpif_backer pointer. */
126 struct handler
*handlers
; /* Upcall handlers. */
129 struct revalidator
*revalidators
; /* Flow revalidators. */
130 size_t n_revalidators
;
132 struct latch exit_latch
; /* Tells child threads to exit. */
135 struct seq
*reval_seq
; /* Incremented to force revalidation. */
136 bool reval_exit
; /* Set by leader on 'exit_latch. */
137 struct ovs_barrier reval_barrier
; /* Barrier used by revalidators. */
138 struct dpif_flow_dump
*dump
; /* DPIF flow dump state. */
139 long long int dump_duration
; /* Duration of the last flow dump. */
140 struct seq
*dump_seq
; /* Increments each dump iteration. */
141 atomic_bool enable_ufid
; /* If true, skip dumping flow attrs. */
143 /* These variables provide a mechanism for the main thread to pause
144 * all revalidation without having to completely shut the threads down.
145 * 'pause_latch' is shared between the main thread and the lead
146 * revalidator thread, so when it is desirable to halt revalidation, the
147 * main thread will set the latch. 'pause' and 'pause_barrier' are shared
148 * by revalidator threads. The lead revalidator will set 'pause' when it
149 * observes the latch has been set, and this will cause all revalidator
150 * threads to wait on 'pause_barrier' at the beginning of the next
151 * revalidation round. */
152 bool pause
; /* Set by leader on 'pause_latch. */
153 struct latch pause_latch
; /* Set to force revalidators pause. */
154 struct ovs_barrier pause_barrier
; /* Barrier used to pause all */
155 /* revalidators by main thread. */
157 /* There are 'N_UMAPS' maps containing 'struct udpif_key' elements.
159 * During the flow dump phase, revalidators insert into these with a random
160 * distribution. During the garbage collection phase, each revalidator
161 * takes care of garbage collecting a slice of these maps. */
164 /* Datapath flow statistics. */
165 unsigned int max_n_flows
;
166 unsigned int avg_n_flows
;
168 /* Following fields are accessed and modified by different threads. */
169 atomic_uint flow_limit
; /* Datapath flow hard limit. */
171 /* n_flows_mutex prevents multiple threads updating these concurrently. */
172 atomic_uint n_flows
; /* Number of flows in the datapath. */
173 atomic_llong n_flows_timestamp
; /* Last time n_flows was updated. */
174 struct ovs_mutex n_flows_mutex
;
176 /* Following fields are accessed and modified only from the main thread. */
177 struct unixctl_conn
**conns
; /* Connections waiting on dump_seq. */
178 uint64_t conn_seq
; /* Corresponds to 'dump_seq' when
179 conns[n_conns-1] was stored. */
180 size_t n_conns
; /* Number of connections waiting. */
184 BAD_UPCALL
, /* Some kind of bug somewhere. */
185 MISS_UPCALL
, /* A flow miss. */
186 SFLOW_UPCALL
, /* sFlow sample. */
187 FLOW_SAMPLE_UPCALL
, /* Per-flow sampling. */
188 IPFIX_UPCALL
/* Per-bridge sampling. */
198 struct ofproto_dpif
*ofproto
; /* Parent ofproto. */
199 const struct recirc_id_node
*recirc
; /* Recirculation context. */
200 bool have_recirc_ref
; /* Reference held on recirc ctx? */
202 /* The flow and packet are only required to be constant when using
203 * dpif-netdev. If a modification is absolutely necessary, a const cast
204 * may be used with other datapaths. */
205 const struct flow
*flow
; /* Parsed representation of the packet. */
206 const ovs_u128
*ufid
; /* Unique identifier for 'flow'. */
207 unsigned pmd_id
; /* Datapath poll mode driver id. */
208 const struct dp_packet
*packet
; /* Packet associated with this upcall. */
209 ofp_port_t in_port
; /* OpenFlow in port, or OFPP_NONE. */
210 uint16_t mru
; /* If !0, Maximum receive unit of
211 fragmented IP packet */
213 enum dpif_upcall_type type
; /* Datapath type of the upcall. */
214 const struct nlattr
*userdata
; /* Userdata for DPIF_UC_ACTION Upcalls. */
215 const struct nlattr
*actions
; /* Flow actions in DPIF_UC_ACTION Upcalls. */
217 bool xout_initialized
; /* True if 'xout' must be uninitialized. */
218 struct xlate_out xout
; /* Result of xlate_actions(). */
219 struct ofpbuf odp_actions
; /* Datapath actions from xlate_actions(). */
220 struct flow_wildcards wc
; /* Dependencies that megaflow must match. */
221 struct ofpbuf put_actions
; /* Actions 'put' in the fastpath. */
223 struct dpif_ipfix
*ipfix
; /* IPFIX pointer or NULL. */
224 struct dpif_sflow
*sflow
; /* SFlow pointer or NULL. */
226 struct udpif_key
*ukey
; /* Revalidator flow cache. */
227 bool ukey_persists
; /* Set true to keep 'ukey' beyond the
228 lifetime of this upcall. */
230 uint64_t dump_seq
; /* udpif->dump_seq at translation time. */
231 uint64_t reval_seq
; /* udpif->reval_seq at translation time. */
233 /* Not used by the upcall callback interface. */
234 const struct nlattr
*key
; /* Datapath flow key. */
235 size_t key_len
; /* Datapath flow key length. */
236 const struct nlattr
*out_tun_key
; /* Datapath output tunnel key. */
238 uint64_t odp_actions_stub
[1024 / 8]; /* Stub for odp_actions. */
241 /* Ukeys must transition through these states using transition_ukey(). */
244 UKEY_VISIBLE
, /* Ukey is in umap, datapath flow install is queued. */
245 UKEY_OPERATIONAL
, /* Ukey is in umap, datapath flow is installed. */
246 UKEY_EVICTING
, /* Ukey is in umap, datapath flow delete is queued. */
247 UKEY_EVICTED
, /* Ukey is in umap, datapath flow is deleted. */
248 UKEY_DELETED
, /* Ukey removed from umap, ukey free is deferred. */
250 #define N_UKEY_STATES (UKEY_DELETED + 1)
252 /* 'udpif_key's are responsible for tracking the little bit of state udpif
253 * needs to do flow expiration which can't be pulled directly from the
254 * datapath. They may be created by any handler or revalidator thread at any
255 * time, and read by any revalidator during the dump phase. They are however
256 * each owned by a single revalidator which takes care of destroying them
257 * during the garbage-collection phase.
259 * The mutex within the ukey protects some members of the ukey. The ukey
260 * itself is protected by RCU and is held within a umap in the parent udpif.
261 * Adding or removing a ukey from a umap is only safe when holding the
262 * corresponding umap lock. */
264 struct cmap_node cmap_node
; /* In parent revalidator 'ukeys' map. */
266 /* These elements are read only once created, and therefore aren't
267 * protected by a mutex. */
268 const struct nlattr
*key
; /* Datapath flow key. */
269 size_t key_len
; /* Length of 'key'. */
270 const struct nlattr
*mask
; /* Datapath flow mask. */
271 size_t mask_len
; /* Length of 'mask'. */
272 ovs_u128 ufid
; /* Unique flow identifier. */
273 bool ufid_present
; /* True if 'ufid' is in datapath. */
274 uint32_t hash
; /* Pre-computed hash for 'key'. */
275 unsigned pmd_id
; /* Datapath poll mode driver id. */
277 struct ovs_mutex mutex
; /* Guards the following. */
278 struct dpif_flow_stats stats OVS_GUARDED
; /* Last known stats.*/
279 long long int created OVS_GUARDED
; /* Estimate of creation time. */
280 uint64_t dump_seq OVS_GUARDED
; /* Tracks udpif->dump_seq. */
281 uint64_t reval_seq OVS_GUARDED
; /* Tracks udpif->reval_seq. */
282 enum ukey_state state OVS_GUARDED
; /* Tracks ukey lifetime. */
284 /* 'state' debug information. */
285 unsigned int state_thread OVS_GUARDED
; /* Thread that transitions. */
286 const char *state_where OVS_GUARDED
; /* transition_ukey() locator. */
288 /* Datapath flow actions as nlattrs. Protected by RCU. Read with
289 * ukey_get_actions(), and write with ukey_set_actions(). */
290 OVSRCU_TYPE(struct ofpbuf
*) actions
;
292 struct xlate_cache
*xcache OVS_GUARDED
; /* Cache for xlate entries that
293 * are affected by this ukey.
294 * Used for stats and learning.*/
296 struct odputil_keybuf buf
;
300 uint32_t key_recirc_id
; /* Non-zero if reference is held by the ukey. */
301 struct recirc_refs recircs
; /* Action recirc IDs with references held. */
304 /* Datapath operation with optional ukey attached. */
306 struct udpif_key
*ukey
;
307 struct dpif_flow_stats stats
; /* Stats for 'op'. */
308 struct dpif_op dop
; /* Flow operation. */
311 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
312 static struct ovs_list all_udpifs
= OVS_LIST_INITIALIZER(&all_udpifs
);
314 static size_t recv_upcalls(struct handler
*);
315 static int process_upcall(struct udpif
*, struct upcall
*,
316 struct ofpbuf
*odp_actions
, struct flow_wildcards
*);
317 static void handle_upcalls(struct udpif
*, struct upcall
*, size_t n_upcalls
);
318 static void udpif_stop_threads(struct udpif
*);
319 static void udpif_start_threads(struct udpif
*, size_t n_handlers
,
320 size_t n_revalidators
);
321 static void udpif_pause_revalidators(struct udpif
*);
322 static void udpif_resume_revalidators(struct udpif
*);
323 static void *udpif_upcall_handler(void *);
324 static void *udpif_revalidator(void *);
325 static unsigned long udpif_get_n_flows(struct udpif
*);
326 static void revalidate(struct revalidator
*);
327 static void revalidator_pause(struct revalidator
*);
328 static void revalidator_sweep(struct revalidator
*);
329 static void revalidator_purge(struct revalidator
*);
330 static void upcall_unixctl_show(struct unixctl_conn
*conn
, int argc
,
331 const char *argv
[], void *aux
);
332 static void upcall_unixctl_disable_megaflows(struct unixctl_conn
*, int argc
,
333 const char *argv
[], void *aux
);
334 static void upcall_unixctl_enable_megaflows(struct unixctl_conn
*, int argc
,
335 const char *argv
[], void *aux
);
336 static void upcall_unixctl_disable_ufid(struct unixctl_conn
*, int argc
,
337 const char *argv
[], void *aux
);
338 static void upcall_unixctl_enable_ufid(struct unixctl_conn
*, int argc
,
339 const char *argv
[], void *aux
);
340 static void upcall_unixctl_set_flow_limit(struct unixctl_conn
*conn
, int argc
,
341 const char *argv
[], void *aux
);
342 static void upcall_unixctl_dump_wait(struct unixctl_conn
*conn
, int argc
,
343 const char *argv
[], void *aux
);
344 static void upcall_unixctl_purge(struct unixctl_conn
*conn
, int argc
,
345 const char *argv
[], void *aux
);
347 static struct udpif_key
*ukey_create_from_upcall(struct upcall
*,
348 struct flow_wildcards
*);
349 static int ukey_create_from_dpif_flow(const struct udpif
*,
350 const struct dpif_flow
*,
351 struct udpif_key
**);
352 static void ukey_get_actions(struct udpif_key
*, const struct nlattr
**actions
,
354 static bool ukey_install__(struct udpif
*, struct udpif_key
*ukey
)
355 OVS_TRY_LOCK(true, ukey
->mutex
);
356 static bool ukey_install(struct udpif
*udpif
, struct udpif_key
*ukey
);
357 static void transition_ukey_at(struct udpif_key
*ukey
, enum ukey_state dst
,
359 OVS_REQUIRES(ukey
->mutex
);
360 #define transition_ukey(UKEY, DST) \
361 transition_ukey_at(UKEY, DST, OVS_SOURCE_LOCATOR)
362 static struct udpif_key
*ukey_lookup(struct udpif
*udpif
,
363 const ovs_u128
*ufid
,
364 const unsigned pmd_id
);
365 static int ukey_acquire(struct udpif
*, const struct dpif_flow
*,
366 struct udpif_key
**result
, int *error
);
367 static void ukey_delete__(struct udpif_key
*);
368 static void ukey_delete(struct umap
*, struct udpif_key
*);
369 static enum upcall_type
classify_upcall(enum dpif_upcall_type type
,
370 const struct nlattr
*userdata
);
372 static void put_op_init(struct ukey_op
*op
, struct udpif_key
*ukey
,
373 enum dpif_flow_put_flags flags
);
374 static void delete_op_init(struct udpif
*udpif
, struct ukey_op
*op
,
375 struct udpif_key
*ukey
);
377 static int upcall_receive(struct upcall
*, const struct dpif_backer
*,
378 const struct dp_packet
*packet
, enum dpif_upcall_type
,
379 const struct nlattr
*userdata
, const struct flow
*,
380 const unsigned int mru
,
381 const ovs_u128
*ufid
, const unsigned pmd_id
);
382 static void upcall_uninit(struct upcall
*);
384 static upcall_callback upcall_cb
;
385 static dp_purge_callback dp_purge_cb
;
387 static atomic_bool enable_megaflows
= ATOMIC_VAR_INIT(true);
388 static atomic_bool enable_ufid
= ATOMIC_VAR_INIT(true);
393 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
394 if (ovsthread_once_start(&once
)) {
395 unixctl_command_register("upcall/show", "", 0, 0, upcall_unixctl_show
,
397 unixctl_command_register("upcall/disable-megaflows", "", 0, 0,
398 upcall_unixctl_disable_megaflows
, NULL
);
399 unixctl_command_register("upcall/enable-megaflows", "", 0, 0,
400 upcall_unixctl_enable_megaflows
, NULL
);
401 unixctl_command_register("upcall/disable-ufid", "", 0, 0,
402 upcall_unixctl_disable_ufid
, NULL
);
403 unixctl_command_register("upcall/enable-ufid", "", 0, 0,
404 upcall_unixctl_enable_ufid
, NULL
);
405 unixctl_command_register("upcall/set-flow-limit", "flow-limit-number",
406 1, 1, upcall_unixctl_set_flow_limit
, NULL
);
407 unixctl_command_register("revalidator/wait", "", 0, 0,
408 upcall_unixctl_dump_wait
, NULL
);
409 unixctl_command_register("revalidator/purge", "", 0, 0,
410 upcall_unixctl_purge
, NULL
);
411 ovsthread_once_done(&once
);
416 udpif_create(struct dpif_backer
*backer
, struct dpif
*dpif
)
418 struct udpif
*udpif
= xzalloc(sizeof *udpif
);
421 udpif
->backer
= backer
;
422 atomic_init(&udpif
->flow_limit
, MIN(ofproto_flow_limit
, 10000));
423 udpif
->reval_seq
= seq_create();
424 udpif
->dump_seq
= seq_create();
425 latch_init(&udpif
->exit_latch
);
426 latch_init(&udpif
->pause_latch
);
427 ovs_list_push_back(&all_udpifs
, &udpif
->list_node
);
428 atomic_init(&udpif
->enable_ufid
, false);
429 atomic_init(&udpif
->n_flows
, 0);
430 atomic_init(&udpif
->n_flows_timestamp
, LLONG_MIN
);
431 ovs_mutex_init(&udpif
->n_flows_mutex
);
432 udpif
->ukeys
= xmalloc(N_UMAPS
* sizeof *udpif
->ukeys
);
433 for (int i
= 0; i
< N_UMAPS
; i
++) {
434 cmap_init(&udpif
->ukeys
[i
].cmap
);
435 ovs_mutex_init(&udpif
->ukeys
[i
].mutex
);
438 dpif_register_upcall_cb(dpif
, upcall_cb
, udpif
);
439 dpif_register_dp_purge_cb(dpif
, dp_purge_cb
, udpif
);
445 udpif_run(struct udpif
*udpif
)
447 if (udpif
->conns
&& udpif
->conn_seq
!= seq_read(udpif
->dump_seq
)) {
450 for (i
= 0; i
< udpif
->n_conns
; i
++) {
451 unixctl_command_reply(udpif
->conns
[i
], NULL
);
460 udpif_destroy(struct udpif
*udpif
)
462 udpif_stop_threads(udpif
);
464 dpif_register_dp_purge_cb(udpif
->dpif
, NULL
, udpif
);
465 dpif_register_upcall_cb(udpif
->dpif
, NULL
, udpif
);
467 for (int i
= 0; i
< N_UMAPS
; i
++) {
468 cmap_destroy(&udpif
->ukeys
[i
].cmap
);
469 ovs_mutex_destroy(&udpif
->ukeys
[i
].mutex
);
474 ovs_list_remove(&udpif
->list_node
);
475 latch_destroy(&udpif
->exit_latch
);
476 latch_destroy(&udpif
->pause_latch
);
477 seq_destroy(udpif
->reval_seq
);
478 seq_destroy(udpif
->dump_seq
);
479 ovs_mutex_destroy(&udpif
->n_flows_mutex
);
483 /* Stops the handler and revalidator threads, must be enclosed in
484 * ovsrcu quiescent state unless when destroying udpif. */
486 udpif_stop_threads(struct udpif
*udpif
)
488 if (udpif
&& (udpif
->n_handlers
!= 0 || udpif
->n_revalidators
!= 0)) {
491 latch_set(&udpif
->exit_latch
);
493 for (i
= 0; i
< udpif
->n_handlers
; i
++) {
494 struct handler
*handler
= &udpif
->handlers
[i
];
496 xpthread_join(handler
->thread
, NULL
);
499 for (i
= 0; i
< udpif
->n_revalidators
; i
++) {
500 xpthread_join(udpif
->revalidators
[i
].thread
, NULL
);
503 dpif_disable_upcall(udpif
->dpif
);
505 for (i
= 0; i
< udpif
->n_revalidators
; i
++) {
506 struct revalidator
*revalidator
= &udpif
->revalidators
[i
];
508 /* Delete ukeys, and delete all flows from the datapath to prevent
509 * double-counting stats. */
510 revalidator_purge(revalidator
);
513 latch_poll(&udpif
->exit_latch
);
515 ovs_barrier_destroy(&udpif
->reval_barrier
);
516 ovs_barrier_destroy(&udpif
->pause_barrier
);
518 free(udpif
->revalidators
);
519 udpif
->revalidators
= NULL
;
520 udpif
->n_revalidators
= 0;
522 free(udpif
->handlers
);
523 udpif
->handlers
= NULL
;
524 udpif
->n_handlers
= 0;
528 /* Starts the handler and revalidator threads, must be enclosed in
529 * ovsrcu quiescent state. */
531 udpif_start_threads(struct udpif
*udpif
, size_t n_handlers
,
532 size_t n_revalidators
)
534 if (udpif
&& n_handlers
&& n_revalidators
) {
538 udpif
->n_handlers
= n_handlers
;
539 udpif
->n_revalidators
= n_revalidators
;
541 udpif
->handlers
= xzalloc(udpif
->n_handlers
* sizeof *udpif
->handlers
);
542 for (i
= 0; i
< udpif
->n_handlers
; i
++) {
543 struct handler
*handler
= &udpif
->handlers
[i
];
545 handler
->udpif
= udpif
;
546 handler
->handler_id
= i
;
547 handler
->thread
= ovs_thread_create(
548 "handler", udpif_upcall_handler
, handler
);
551 enable_ufid
= udpif
->backer
->rt_support
.ufid
;
552 atomic_init(&udpif
->enable_ufid
, enable_ufid
);
553 dpif_enable_upcall(udpif
->dpif
);
555 ovs_barrier_init(&udpif
->reval_barrier
, udpif
->n_revalidators
);
556 ovs_barrier_init(&udpif
->pause_barrier
, udpif
->n_revalidators
+ 1);
557 udpif
->reval_exit
= false;
558 udpif
->pause
= false;
559 udpif
->revalidators
= xzalloc(udpif
->n_revalidators
560 * sizeof *udpif
->revalidators
);
561 for (i
= 0; i
< udpif
->n_revalidators
; i
++) {
562 struct revalidator
*revalidator
= &udpif
->revalidators
[i
];
564 revalidator
->udpif
= udpif
;
565 revalidator
->thread
= ovs_thread_create(
566 "revalidator", udpif_revalidator
, revalidator
);
571 /* Pauses all revalidators. Should only be called by the main thread.
572 * When function returns, all revalidators are paused and will proceed
573 * only after udpif_resume_revalidators() is called. */
575 udpif_pause_revalidators(struct udpif
*udpif
)
577 if (udpif
->backer
->recv_set_enable
) {
578 latch_set(&udpif
->pause_latch
);
579 ovs_barrier_block(&udpif
->pause_barrier
);
583 /* Resumes the pausing of revalidators. Should only be called by the
586 udpif_resume_revalidators(struct udpif
*udpif
)
588 if (udpif
->backer
->recv_set_enable
) {
589 latch_poll(&udpif
->pause_latch
);
590 ovs_barrier_block(&udpif
->pause_barrier
);
594 /* Tells 'udpif' how many threads it should use to handle upcalls.
595 * 'n_handlers' and 'n_revalidators' can never be zero. 'udpif''s
596 * datapath handle must have packet reception enabled before starting
599 udpif_set_threads(struct udpif
*udpif
, size_t n_handlers
,
600 size_t n_revalidators
)
603 ovs_assert(n_handlers
&& n_revalidators
);
605 ovsrcu_quiesce_start();
606 if (udpif
->n_handlers
!= n_handlers
607 || udpif
->n_revalidators
!= n_revalidators
) {
608 udpif_stop_threads(udpif
);
611 if (!udpif
->handlers
&& !udpif
->revalidators
) {
614 error
= dpif_handlers_set(udpif
->dpif
, n_handlers
);
616 VLOG_ERR("failed to configure handlers in dpif %s: %s",
617 dpif_name(udpif
->dpif
), ovs_strerror(error
));
621 udpif_start_threads(udpif
, n_handlers
, n_revalidators
);
623 ovsrcu_quiesce_end();
626 /* Waits for all ongoing upcall translations to complete. This ensures that
627 * there are no transient references to any removed ofprotos (or other
628 * objects). In particular, this should be called after an ofproto is removed
629 * (e.g. via xlate_remove_ofproto()) but before it is destroyed. */
631 udpif_synchronize(struct udpif
*udpif
)
633 /* This is stronger than necessary. It would be sufficient to ensure
634 * (somehow) that each handler and revalidator thread had passed through
635 * its main loop once. */
636 size_t n_handlers
= udpif
->n_handlers
;
637 size_t n_revalidators
= udpif
->n_revalidators
;
639 ovsrcu_quiesce_start();
640 udpif_stop_threads(udpif
);
641 udpif_start_threads(udpif
, n_handlers
, n_revalidators
);
642 ovsrcu_quiesce_end();
645 /* Notifies 'udpif' that something changed which may render previous
646 * xlate_actions() results invalid. */
648 udpif_revalidate(struct udpif
*udpif
)
650 seq_change(udpif
->reval_seq
);
653 /* Returns a seq which increments every time 'udpif' pulls stats from the
654 * datapath. Callers can use this to get a sense of when might be a good time
655 * to do periodic work which relies on relatively up to date statistics. */
657 udpif_dump_seq(struct udpif
*udpif
)
659 return udpif
->dump_seq
;
663 udpif_get_memory_usage(struct udpif
*udpif
, struct simap
*usage
)
667 simap_increase(usage
, "handlers", udpif
->n_handlers
);
669 simap_increase(usage
, "revalidators", udpif
->n_revalidators
);
670 for (i
= 0; i
< N_UMAPS
; i
++) {
671 simap_increase(usage
, "udpif keys", cmap_count(&udpif
->ukeys
[i
].cmap
));
675 /* Remove flows from a single datapath. */
677 udpif_flush(struct udpif
*udpif
)
679 size_t n_handlers
, n_revalidators
;
681 n_handlers
= udpif
->n_handlers
;
682 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 if (odp_flow_key_to_flow(dupcall
->key
, dupcall
->key_len
, flow
)
791 mru
= nl_attr_get_u16(dupcall
->mru
);
796 error
= upcall_receive(upcall
, udpif
->backer
, &dupcall
->packet
,
797 dupcall
->type
, dupcall
->userdata
, flow
, mru
,
798 &dupcall
->ufid
, PMD_ID_NULL
);
800 if (error
== ENODEV
) {
801 /* Received packet on datapath port for which we couldn't
802 * associate an ofproto. This can happen if a port is removed
803 * while traffic is being received. Print a rate-limited
804 * message in case it happens frequently. */
805 dpif_flow_put(udpif
->dpif
, DPIF_FP_CREATE
, dupcall
->key
,
806 dupcall
->key_len
, NULL
, 0, NULL
, 0,
807 &dupcall
->ufid
, PMD_ID_NULL
, NULL
);
808 VLOG_INFO_RL(&rl
, "received packet on unassociated datapath "
809 "port %"PRIu32
, flow
->in_port
.odp_port
);
814 upcall
->key
= dupcall
->key
;
815 upcall
->key_len
= dupcall
->key_len
;
816 upcall
->ufid
= &dupcall
->ufid
;
818 upcall
->out_tun_key
= dupcall
->out_tun_key
;
819 upcall
->actions
= dupcall
->actions
;
821 pkt_metadata_from_flow(&dupcall
->packet
.md
, flow
);
822 flow_extract(&dupcall
->packet
, flow
);
824 error
= process_upcall(udpif
, upcall
,
825 &upcall
->odp_actions
, &upcall
->wc
);
834 upcall_uninit(upcall
);
836 dp_packet_uninit(&dupcall
->packet
);
837 ofpbuf_uninit(recv_buf
);
841 handle_upcalls(handler
->udpif
, upcalls
, n_upcalls
);
842 for (i
= 0; i
< n_upcalls
; i
++) {
843 dp_packet_uninit(&dupcalls
[i
].packet
);
844 ofpbuf_uninit(&recv_bufs
[i
]);
845 upcall_uninit(&upcalls
[i
]);
853 udpif_revalidator(void *arg
)
855 /* Used by all revalidators. */
856 struct revalidator
*revalidator
= arg
;
857 struct udpif
*udpif
= revalidator
->udpif
;
858 bool leader
= revalidator
== &udpif
->revalidators
[0];
860 /* Used only by the leader. */
861 long long int start_time
= 0;
862 uint64_t last_reval_seq
= 0;
865 revalidator
->id
= ovsthread_id_self();
870 recirc_run(); /* Recirculation cleanup. */
872 reval_seq
= seq_read(udpif
->reval_seq
);
873 last_reval_seq
= reval_seq
;
875 n_flows
= udpif_get_n_flows(udpif
);
876 udpif
->max_n_flows
= MAX(n_flows
, udpif
->max_n_flows
);
877 udpif
->avg_n_flows
= (udpif
->avg_n_flows
+ n_flows
) / 2;
879 /* Only the leader checks the pause latch to prevent a race where
880 * some threads think it's false and proceed to block on
881 * reval_barrier and others think it's true and block indefinitely
882 * on the pause_barrier */
883 udpif
->pause
= latch_is_set(&udpif
->pause_latch
);
885 /* Only the leader checks the exit latch to prevent a race where
886 * some threads think it's true and exit and others think it's
887 * false and block indefinitely on the reval_barrier */
888 udpif
->reval_exit
= latch_is_set(&udpif
->exit_latch
);
890 start_time
= time_msec();
891 if (!udpif
->reval_exit
) {
894 terse_dump
= udpif_use_ufid(udpif
);
895 udpif
->dump
= dpif_flow_dump_create(udpif
->dpif
, terse_dump
,
900 /* Wait for the leader to start the flow dump. */
901 ovs_barrier_block(&udpif
->reval_barrier
);
903 revalidator_pause(revalidator
);
906 if (udpif
->reval_exit
) {
909 revalidate(revalidator
);
911 /* Wait for all flows to have been dumped before we garbage collect. */
912 ovs_barrier_block(&udpif
->reval_barrier
);
913 revalidator_sweep(revalidator
);
915 /* Wait for all revalidators to finish garbage collection. */
916 ovs_barrier_block(&udpif
->reval_barrier
);
919 unsigned int flow_limit
;
920 long long int duration
;
922 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
924 dpif_flow_dump_destroy(udpif
->dump
);
925 seq_change(udpif
->dump_seq
);
927 duration
= MAX(time_msec() - start_time
, 1);
928 udpif
->dump_duration
= duration
;
929 if (duration
> 2000) {
930 flow_limit
/= duration
/ 1000;
931 } else if (duration
> 1300) {
932 flow_limit
= flow_limit
* 3 / 4;
933 } else if (duration
< 1000 && n_flows
> 2000
934 && flow_limit
< n_flows
* 1000 / duration
) {
937 flow_limit
= MIN(ofproto_flow_limit
, MAX(flow_limit
, 1000));
938 atomic_store_relaxed(&udpif
->flow_limit
, flow_limit
);
940 if (duration
> 2000) {
941 VLOG_INFO("Spent an unreasonably long %lldms dumping flows",
945 poll_timer_wait_until(start_time
+ MIN(ofproto_max_idle
, 500));
946 seq_wait(udpif
->reval_seq
, last_reval_seq
);
947 latch_wait(&udpif
->exit_latch
);
948 latch_wait(&udpif
->pause_latch
);
951 if (!latch_is_set(&udpif
->pause_latch
) &&
952 !latch_is_set(&udpif
->exit_latch
)) {
953 long long int now
= time_msec();
954 /* Block again if we are woken up within 5ms of the last start
958 if (now
< start_time
) {
959 poll_timer_wait_until(start_time
);
960 latch_wait(&udpif
->exit_latch
);
961 latch_wait(&udpif
->pause_latch
);
971 static enum upcall_type
972 classify_upcall(enum dpif_upcall_type type
, const struct nlattr
*userdata
)
974 union user_action_cookie cookie
;
977 /* First look at the upcall type. */
985 case DPIF_N_UC_TYPES
:
987 VLOG_WARN_RL(&rl
, "upcall has unexpected type %"PRIu32
, type
);
991 /* "action" upcalls need a closer look. */
993 VLOG_WARN_RL(&rl
, "action upcall missing cookie");
996 userdata_len
= nl_attr_get_size(userdata
);
997 if (userdata_len
< sizeof cookie
.type
998 || userdata_len
> sizeof cookie
) {
999 VLOG_WARN_RL(&rl
, "action upcall cookie has unexpected size %"PRIuSIZE
,
1003 memset(&cookie
, 0, sizeof cookie
);
1004 memcpy(&cookie
, nl_attr_get(userdata
), userdata_len
);
1005 if (userdata_len
== MAX(8, sizeof cookie
.sflow
)
1006 && cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
1007 return SFLOW_UPCALL
;
1008 } else if (userdata_len
== MAX(8, sizeof cookie
.slow_path
)
1009 && cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
1011 } else if (userdata_len
== MAX(8, sizeof cookie
.flow_sample
)
1012 && cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
1013 return FLOW_SAMPLE_UPCALL
;
1014 } else if (userdata_len
== MAX(8, sizeof cookie
.ipfix
)
1015 && cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
1016 return IPFIX_UPCALL
;
1018 VLOG_WARN_RL(&rl
, "invalid user cookie of type %"PRIu16
1019 " and size %"PRIuSIZE
, cookie
.type
, userdata_len
);
1024 /* Calculates slow path actions for 'xout'. 'buf' must statically be
1025 * initialized with at least 128 bytes of space. */
1027 compose_slow_path(struct udpif
*udpif
, struct xlate_out
*xout
,
1028 const struct flow
*flow
, odp_port_t odp_in_port
,
1029 struct ofpbuf
*buf
, uint32_t slowpath_meter_id
,
1030 uint32_t controller_meter_id
)
1032 union user_action_cookie cookie
;
1036 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
1037 cookie
.slow_path
.unused
= 0;
1038 cookie
.slow_path
.reason
= xout
->slow
;
1040 port
= xout
->slow
& (SLOW_CFM
| SLOW_BFD
| SLOW_LACP
| SLOW_STP
)
1043 pid
= dpif_port_get_pid(udpif
->dpif
, port
, flow_hash_5tuple(flow
, 0));
1047 uint32_t meter_id
= xout
->slow
& SLOW_CONTROLLER
? controller_meter_id
1048 : slowpath_meter_id
;
1050 if (meter_id
!= UINT32_MAX
) {
1051 /* If slowpath meter is configured, generate clone(meter, userspace)
1053 offset
= nl_msg_start_nested(buf
, OVS_ACTION_ATTR_SAMPLE
);
1054 nl_msg_put_u32(buf
, OVS_SAMPLE_ATTR_PROBABILITY
, UINT32_MAX
);
1055 ac_offset
= nl_msg_start_nested(buf
, OVS_SAMPLE_ATTR_ACTIONS
);
1056 nl_msg_put_u32(buf
, OVS_ACTION_ATTR_METER
, meter_id
);
1059 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
.slow_path
,
1060 ODPP_NONE
, false, buf
);
1062 if (meter_id
!= UINT32_MAX
) {
1063 nl_msg_end_nested(buf
, ac_offset
);
1064 nl_msg_end_nested(buf
, offset
);
1068 /* If there is no error, the upcall must be destroyed with upcall_uninit()
1069 * before quiescing, as the referred objects are guaranteed to exist only
1070 * until the calling thread quiesces. Otherwise, do not call upcall_uninit()
1071 * since the 'upcall->put_actions' remains uninitialized. */
1073 upcall_receive(struct upcall
*upcall
, const struct dpif_backer
*backer
,
1074 const struct dp_packet
*packet
, enum dpif_upcall_type type
,
1075 const struct nlattr
*userdata
, const struct flow
*flow
,
1076 const unsigned int mru
,
1077 const ovs_u128
*ufid
, const unsigned pmd_id
)
1081 error
= xlate_lookup(backer
, flow
, &upcall
->ofproto
, &upcall
->ipfix
,
1082 &upcall
->sflow
, NULL
, &upcall
->in_port
);
1087 upcall
->recirc
= NULL
;
1088 upcall
->have_recirc_ref
= false;
1089 upcall
->flow
= flow
;
1090 upcall
->packet
= packet
;
1091 upcall
->ufid
= ufid
;
1092 upcall
->pmd_id
= pmd_id
;
1093 upcall
->type
= type
;
1094 upcall
->userdata
= userdata
;
1095 ofpbuf_use_stub(&upcall
->odp_actions
, upcall
->odp_actions_stub
,
1096 sizeof upcall
->odp_actions_stub
);
1097 ofpbuf_init(&upcall
->put_actions
, 0);
1099 upcall
->xout_initialized
= false;
1100 upcall
->ukey_persists
= false;
1102 upcall
->ukey
= NULL
;
1104 upcall
->key_len
= 0;
1107 upcall
->out_tun_key
= NULL
;
1108 upcall
->actions
= NULL
;
1114 upcall_xlate(struct udpif
*udpif
, struct upcall
*upcall
,
1115 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
1117 struct dpif_flow_stats stats
;
1118 struct xlate_in xin
;
1120 stats
.n_packets
= 1;
1121 stats
.n_bytes
= dp_packet_size(upcall
->packet
);
1122 stats
.used
= time_msec();
1123 stats
.tcp_flags
= ntohs(upcall
->flow
->tcp_flags
);
1125 xlate_in_init(&xin
, upcall
->ofproto
,
1126 ofproto_dpif_get_tables_version(upcall
->ofproto
),
1127 upcall
->flow
, upcall
->in_port
, NULL
,
1128 stats
.tcp_flags
, upcall
->packet
, wc
, odp_actions
);
1130 if (upcall
->type
== DPIF_UC_MISS
) {
1131 xin
.resubmit_stats
= &stats
;
1133 if (xin
.frozen_state
) {
1134 /* We may install a datapath flow only if we get a reference to the
1135 * recirculation context (otherwise we could have recirculation
1136 * upcalls using recirculation ID for which no context can be
1137 * found). We may still execute the flow's actions even if we
1138 * don't install the flow. */
1139 upcall
->recirc
= recirc_id_node_from_state(xin
.frozen_state
);
1140 upcall
->have_recirc_ref
= recirc_id_node_try_ref_rcu(upcall
->recirc
);
1143 /* For non-miss upcalls, we are either executing actions (one of which
1144 * is an userspace action) for an upcall, in which case the stats have
1145 * already been taken care of, or there's a flow in the datapath which
1146 * this packet was accounted to. Presumably the revalidators will deal
1147 * with pushing its stats eventually. */
1150 upcall
->dump_seq
= seq_read(udpif
->dump_seq
);
1151 upcall
->reval_seq
= seq_read(udpif
->reval_seq
);
1153 xlate_actions(&xin
, &upcall
->xout
);
1155 /* Convert the input port wildcard from OFP to ODP format. There's no
1156 * real way to do this for arbitrary bitmasks since the numbering spaces
1157 * aren't the same. However, flow translation always exact matches the
1158 * whole thing, so we can do the same here. */
1159 WC_MASK_FIELD(wc
, in_port
.odp_port
);
1162 upcall
->xout_initialized
= true;
1164 if (!upcall
->xout
.slow
) {
1165 ofpbuf_use_const(&upcall
->put_actions
,
1166 odp_actions
->data
, odp_actions
->size
);
1168 uint32_t smid
= upcall
->ofproto
->up
.slowpath_meter_id
;
1169 uint32_t cmid
= upcall
->ofproto
->up
.controller_meter_id
;
1170 /* upcall->put_actions already initialized by upcall_receive(). */
1171 compose_slow_path(udpif
, &upcall
->xout
, upcall
->flow
,
1172 upcall
->flow
->in_port
.odp_port
,
1173 &upcall
->put_actions
, smid
, cmid
);
1176 /* This function is also called for slow-pathed flows. As we are only
1177 * going to create new datapath flows for actual datapath misses, there is
1178 * no point in creating a ukey otherwise. */
1179 if (upcall
->type
== DPIF_UC_MISS
) {
1180 upcall
->ukey
= ukey_create_from_upcall(upcall
, wc
);
1185 upcall_uninit(struct upcall
*upcall
)
1188 if (upcall
->xout_initialized
) {
1189 xlate_out_uninit(&upcall
->xout
);
1191 ofpbuf_uninit(&upcall
->odp_actions
);
1192 ofpbuf_uninit(&upcall
->put_actions
);
1194 if (!upcall
->ukey_persists
) {
1195 ukey_delete__(upcall
->ukey
);
1197 } else if (upcall
->have_recirc_ref
) {
1198 /* The reference was transferred to the ukey if one was created. */
1199 recirc_id_node_unref(upcall
->recirc
);
1204 /* If there are less flows than the limit, and this is a miss upcall which
1206 * - Has no recirc_id, OR
1207 * - Has a recirc_id and we can get a reference on the recirc ctx,
1209 * Then we should install the flow (true). Otherwise, return false. */
1211 should_install_flow(struct udpif
*udpif
, struct upcall
*upcall
)
1213 unsigned int flow_limit
;
1215 if (upcall
->type
!= DPIF_UC_MISS
) {
1217 } else if (upcall
->recirc
&& !upcall
->have_recirc_ref
) {
1218 VLOG_DBG_RL(&rl
, "upcall: no reference for recirc flow");
1222 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
1223 if (udpif_get_n_flows(udpif
) >= flow_limit
) {
1224 VLOG_WARN_RL(&rl
, "upcall: datapath flow limit reached");
1232 upcall_cb(const struct dp_packet
*packet
, const struct flow
*flow
, ovs_u128
*ufid
,
1233 unsigned pmd_id
, enum dpif_upcall_type type
,
1234 const struct nlattr
*userdata
, struct ofpbuf
*actions
,
1235 struct flow_wildcards
*wc
, struct ofpbuf
*put_actions
, void *aux
)
1237 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
1238 struct udpif
*udpif
= aux
;
1239 struct upcall upcall
;
1243 atomic_read_relaxed(&enable_megaflows
, &megaflow
);
1245 error
= upcall_receive(&upcall
, udpif
->backer
, packet
, type
, userdata
,
1246 flow
, 0, ufid
, pmd_id
);
1251 error
= process_upcall(udpif
, &upcall
, actions
, wc
);
1256 if (upcall
.xout
.slow
&& put_actions
) {
1257 ofpbuf_put(put_actions
, upcall
.put_actions
.data
,
1258 upcall
.put_actions
.size
);
1261 if (OVS_UNLIKELY(!megaflow
&& wc
)) {
1262 flow_wildcards_init_for_packet(wc
, flow
);
1265 if (!should_install_flow(udpif
, &upcall
)) {
1270 if (upcall
.ukey
&& !ukey_install(udpif
, upcall
.ukey
)) {
1271 VLOG_WARN_RL(&rl
, "upcall_cb failure: ukey installation fails");
1276 upcall
.ukey_persists
= true;
1278 upcall_uninit(&upcall
);
1283 dpif_get_actions(struct udpif
*udpif
, struct upcall
*upcall
,
1284 const struct nlattr
**actions
)
1286 size_t actions_len
= 0;
1288 if (upcall
->actions
) {
1289 /* Actions were passed up from datapath. */
1290 *actions
= nl_attr_get(upcall
->actions
);
1291 actions_len
= nl_attr_get_size(upcall
->actions
);
1294 if (actions_len
== 0) {
1295 /* Lookup actions in userspace cache. */
1296 struct udpif_key
*ukey
= ukey_lookup(udpif
, upcall
->ufid
,
1299 ukey_get_actions(ukey
, actions
, &actions_len
);
1307 dpif_read_actions(struct udpif
*udpif
, struct upcall
*upcall
,
1308 const struct flow
*flow
, enum upcall_type type
,
1311 const struct nlattr
*actions
= NULL
;
1312 size_t actions_len
= dpif_get_actions(udpif
, upcall
, &actions
);
1314 if (!actions
|| !actions_len
) {
1320 dpif_sflow_read_actions(flow
, actions
, actions_len
, upcall_data
);
1322 case FLOW_SAMPLE_UPCALL
:
1324 dpif_ipfix_read_actions(flow
, actions
, actions_len
, upcall_data
);
1336 process_upcall(struct udpif
*udpif
, struct upcall
*upcall
,
1337 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
1339 const struct nlattr
*userdata
= upcall
->userdata
;
1340 const struct dp_packet
*packet
= upcall
->packet
;
1341 const struct flow
*flow
= upcall
->flow
;
1342 size_t actions_len
= 0;
1343 enum upcall_type upcall_type
= classify_upcall(upcall
->type
, userdata
);
1345 switch (upcall_type
) {
1347 upcall_xlate(udpif
, upcall
, odp_actions
, wc
);
1351 if (upcall
->sflow
) {
1352 union user_action_cookie cookie
;
1353 struct dpif_sflow_actions sflow_actions
;
1355 memset(&sflow_actions
, 0, sizeof sflow_actions
);
1356 memset(&cookie
, 0, sizeof cookie
);
1357 memcpy(&cookie
, nl_attr_get(userdata
), sizeof cookie
.sflow
);
1359 actions_len
= dpif_read_actions(udpif
, upcall
, flow
, upcall_type
,
1361 dpif_sflow_received(upcall
->sflow
, packet
, flow
,
1362 flow
->in_port
.odp_port
, &cookie
,
1363 actions_len
> 0 ? &sflow_actions
: NULL
);
1368 if (upcall
->ipfix
) {
1369 union user_action_cookie cookie
;
1370 struct flow_tnl output_tunnel_key
;
1371 struct dpif_ipfix_actions ipfix_actions
;
1373 memset(&cookie
, 0, sizeof cookie
);
1374 memcpy(&cookie
, nl_attr_get(userdata
), sizeof cookie
.ipfix
);
1375 memset(&ipfix_actions
, 0, sizeof ipfix_actions
);
1377 if (upcall
->out_tun_key
) {
1378 odp_tun_key_from_attr(upcall
->out_tun_key
, &output_tunnel_key
);
1381 actions_len
= dpif_read_actions(udpif
, upcall
, flow
, upcall_type
,
1383 dpif_ipfix_bridge_sample(upcall
->ipfix
, packet
, flow
,
1384 flow
->in_port
.odp_port
,
1385 cookie
.ipfix
.output_odp_port
,
1386 upcall
->out_tun_key
?
1387 &output_tunnel_key
: NULL
,
1388 actions_len
> 0 ? &ipfix_actions
: NULL
);
1392 case FLOW_SAMPLE_UPCALL
:
1393 if (upcall
->ipfix
) {
1394 union user_action_cookie cookie
;
1395 struct flow_tnl output_tunnel_key
;
1396 struct dpif_ipfix_actions ipfix_actions
;
1398 memset(&cookie
, 0, sizeof cookie
);
1399 memcpy(&cookie
, nl_attr_get(userdata
), sizeof cookie
.flow_sample
);
1400 memset(&ipfix_actions
, 0, sizeof ipfix_actions
);
1402 if (upcall
->out_tun_key
) {
1403 odp_tun_key_from_attr(upcall
->out_tun_key
, &output_tunnel_key
);
1406 actions_len
= dpif_read_actions(udpif
, upcall
, flow
, upcall_type
,
1408 /* The flow reflects exactly the contents of the packet.
1409 * Sample the packet using it. */
1410 dpif_ipfix_flow_sample(upcall
->ipfix
, packet
, flow
,
1411 &cookie
, flow
->in_port
.odp_port
,
1412 upcall
->out_tun_key
?
1413 &output_tunnel_key
: NULL
,
1414 actions_len
> 0 ? &ipfix_actions
: NULL
);
1426 handle_upcalls(struct udpif
*udpif
, struct upcall
*upcalls
,
1429 struct dpif_op
*opsp
[UPCALL_MAX_BATCH
* 2];
1430 struct ukey_op ops
[UPCALL_MAX_BATCH
* 2];
1431 size_t n_ops
, n_opsp
, i
;
1433 /* Handle the packets individually in order of arrival.
1435 * - For SLOW_CFM, SLOW_LACP, SLOW_STP, SLOW_BFD, and SLOW_LLDP,
1436 * translation is what processes received packets for these
1439 * - For SLOW_CONTROLLER, translation sends the packet to the OpenFlow
1442 * - For SLOW_ACTION, translation executes the actions directly.
1444 * The loop fills 'ops' with an array of operations to execute in the
1447 for (i
= 0; i
< n_upcalls
; i
++) {
1448 struct upcall
*upcall
= &upcalls
[i
];
1449 const struct dp_packet
*packet
= upcall
->packet
;
1452 if (should_install_flow(udpif
, upcall
)) {
1453 struct udpif_key
*ukey
= upcall
->ukey
;
1455 if (ukey_install(udpif
, ukey
)) {
1456 upcall
->ukey_persists
= true;
1457 put_op_init(&ops
[n_ops
++], ukey
, DPIF_FP_CREATE
);
1461 if (upcall
->odp_actions
.size
) {
1464 op
->dop
.type
= DPIF_OP_EXECUTE
;
1465 op
->dop
.u
.execute
.packet
= CONST_CAST(struct dp_packet
*, packet
);
1466 op
->dop
.u
.execute
.flow
= upcall
->flow
;
1467 odp_key_to_dp_packet(upcall
->key
, upcall
->key_len
,
1468 op
->dop
.u
.execute
.packet
);
1469 op
->dop
.u
.execute
.actions
= upcall
->odp_actions
.data
;
1470 op
->dop
.u
.execute
.actions_len
= upcall
->odp_actions
.size
;
1471 op
->dop
.u
.execute
.needs_help
= (upcall
->xout
.slow
& SLOW_ACTION
) != 0;
1472 op
->dop
.u
.execute
.probe
= false;
1473 op
->dop
.u
.execute
.mtu
= upcall
->mru
;
1477 /* Execute batch. */
1479 for (i
= 0; i
< n_ops
; i
++) {
1480 opsp
[n_opsp
++] = &ops
[i
].dop
;
1482 dpif_operate(udpif
->dpif
, opsp
, n_opsp
);
1483 for (i
= 0; i
< n_ops
; i
++) {
1484 struct udpif_key
*ukey
= ops
[i
].ukey
;
1487 ovs_mutex_lock(&ukey
->mutex
);
1488 if (ops
[i
].dop
.error
) {
1489 transition_ukey(ukey
, UKEY_EVICTED
);
1490 } else if (ukey
->state
< UKEY_OPERATIONAL
) {
1491 transition_ukey(ukey
, UKEY_OPERATIONAL
);
1493 ovs_mutex_unlock(&ukey
->mutex
);
1499 get_ukey_hash(const ovs_u128
*ufid
, const unsigned pmd_id
)
1501 return hash_2words(ufid
->u32
[0], pmd_id
);
1504 static struct udpif_key
*
1505 ukey_lookup(struct udpif
*udpif
, const ovs_u128
*ufid
, const unsigned pmd_id
)
1507 struct udpif_key
*ukey
;
1508 int idx
= get_ukey_hash(ufid
, pmd_id
) % N_UMAPS
;
1509 struct cmap
*cmap
= &udpif
->ukeys
[idx
].cmap
;
1511 CMAP_FOR_EACH_WITH_HASH (ukey
, cmap_node
,
1512 get_ukey_hash(ufid
, pmd_id
), cmap
) {
1513 if (ovs_u128_equals(ukey
->ufid
, *ufid
)) {
1520 /* Provides safe lockless access of RCU protected 'ukey->actions'. Callers may
1521 * alternatively access the field directly if they take 'ukey->mutex'. */
1523 ukey_get_actions(struct udpif_key
*ukey
, const struct nlattr
**actions
, size_t *size
)
1525 const struct ofpbuf
*buf
= ovsrcu_get(struct ofpbuf
*, &ukey
->actions
);
1526 *actions
= buf
->data
;
1531 ukey_set_actions(struct udpif_key
*ukey
, const struct ofpbuf
*actions
)
1533 ovsrcu_postpone(ofpbuf_delete
,
1534 ovsrcu_get_protected(struct ofpbuf
*, &ukey
->actions
));
1535 ovsrcu_set(&ukey
->actions
, ofpbuf_clone(actions
));
1538 static struct udpif_key
*
1539 ukey_create__(const struct nlattr
*key
, size_t key_len
,
1540 const struct nlattr
*mask
, size_t mask_len
,
1541 bool ufid_present
, const ovs_u128
*ufid
,
1542 const unsigned pmd_id
, const struct ofpbuf
*actions
,
1543 uint64_t dump_seq
, uint64_t reval_seq
, long long int used
,
1544 uint32_t key_recirc_id
, struct xlate_out
*xout
)
1545 OVS_NO_THREAD_SAFETY_ANALYSIS
1547 struct udpif_key
*ukey
= xmalloc(sizeof *ukey
);
1549 memcpy(&ukey
->keybuf
, key
, key_len
);
1550 ukey
->key
= &ukey
->keybuf
.nla
;
1551 ukey
->key_len
= key_len
;
1552 memcpy(&ukey
->maskbuf
, mask
, mask_len
);
1553 ukey
->mask
= &ukey
->maskbuf
.nla
;
1554 ukey
->mask_len
= mask_len
;
1555 ukey
->ufid_present
= ufid_present
;
1557 ukey
->pmd_id
= pmd_id
;
1558 ukey
->hash
= get_ukey_hash(&ukey
->ufid
, pmd_id
);
1560 ovsrcu_init(&ukey
->actions
, NULL
);
1561 ukey_set_actions(ukey
, actions
);
1563 ovs_mutex_init(&ukey
->mutex
);
1564 ukey
->dump_seq
= dump_seq
;
1565 ukey
->reval_seq
= reval_seq
;
1566 ukey
->state
= UKEY_CREATED
;
1567 ukey
->state_thread
= ovsthread_id_self();
1568 ukey
->state_where
= OVS_SOURCE_LOCATOR
;
1569 ukey
->created
= time_msec();
1570 memset(&ukey
->stats
, 0, sizeof ukey
->stats
);
1571 ukey
->stats
.used
= used
;
1572 ukey
->xcache
= NULL
;
1574 ukey
->key_recirc_id
= key_recirc_id
;
1575 recirc_refs_init(&ukey
->recircs
);
1577 /* Take ownership of the action recirc id references. */
1578 recirc_refs_swap(&ukey
->recircs
, &xout
->recircs
);
1584 static struct udpif_key
*
1585 ukey_create_from_upcall(struct upcall
*upcall
, struct flow_wildcards
*wc
)
1587 struct odputil_keybuf keystub
, maskstub
;
1588 struct ofpbuf keybuf
, maskbuf
;
1590 struct odp_flow_key_parms odp_parms
= {
1591 .flow
= upcall
->flow
,
1592 .mask
= wc
? &wc
->masks
: NULL
,
1595 odp_parms
.support
= upcall
->ofproto
->backer
->rt_support
.odp
;
1596 if (upcall
->key_len
) {
1597 ofpbuf_use_const(&keybuf
, upcall
->key
, upcall
->key_len
);
1599 /* dpif-netdev doesn't provide a netlink-formatted flow key in the
1600 * upcall, so convert the upcall's flow here. */
1601 ofpbuf_use_stack(&keybuf
, &keystub
, sizeof keystub
);
1602 odp_flow_key_from_flow(&odp_parms
, &keybuf
);
1605 atomic_read_relaxed(&enable_megaflows
, &megaflow
);
1606 ofpbuf_use_stack(&maskbuf
, &maskstub
, sizeof maskstub
);
1607 if (megaflow
&& wc
) {
1608 odp_parms
.key_buf
= &keybuf
;
1609 odp_flow_key_from_mask(&odp_parms
, &maskbuf
);
1612 return ukey_create__(keybuf
.data
, keybuf
.size
, maskbuf
.data
, maskbuf
.size
,
1613 true, upcall
->ufid
, upcall
->pmd_id
,
1614 &upcall
->put_actions
, upcall
->dump_seq
,
1615 upcall
->reval_seq
, 0,
1616 upcall
->have_recirc_ref
? upcall
->recirc
->id
: 0,
1621 ukey_create_from_dpif_flow(const struct udpif
*udpif
,
1622 const struct dpif_flow
*flow
,
1623 struct udpif_key
**ukey
)
1625 struct dpif_flow full_flow
;
1626 struct ofpbuf actions
;
1627 uint64_t dump_seq
, reval_seq
;
1628 uint64_t stub
[DPIF_FLOW_BUFSIZE
/ 8];
1629 const struct nlattr
*a
;
1632 if (!flow
->key_len
|| !flow
->actions_len
) {
1636 /* If the key or actions were not provided by the datapath, fetch the
1638 ofpbuf_use_stack(&buf
, &stub
, sizeof stub
);
1639 err
= dpif_flow_get(udpif
->dpif
, flow
->key
, flow
->key_len
,
1640 flow
->ufid_present
? &flow
->ufid
: NULL
,
1641 flow
->pmd_id
, &buf
, &full_flow
);
1648 /* Check the flow actions for recirculation action. As recirculation
1649 * relies on OVS userspace internal state, we need to delete all old
1650 * datapath flows with either a non-zero recirc_id in the key, or any
1651 * recirculation actions upon OVS restart. */
1652 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, flow
->key
, flow
->key_len
) {
1653 if (nl_attr_type(a
) == OVS_KEY_ATTR_RECIRC_ID
1654 && nl_attr_get_u32(a
) != 0) {
1658 NL_ATTR_FOR_EACH (a
, left
, flow
->actions
, flow
->actions_len
) {
1659 if (nl_attr_type(a
) == OVS_ACTION_ATTR_RECIRC
) {
1664 dump_seq
= seq_read(udpif
->dump_seq
);
1665 reval_seq
= seq_read(udpif
->reval_seq
) - 1; /* Ensure revalidation. */
1666 ofpbuf_use_const(&actions
, &flow
->actions
, flow
->actions_len
);
1667 *ukey
= ukey_create__(flow
->key
, flow
->key_len
,
1668 flow
->mask
, flow
->mask_len
, flow
->ufid_present
,
1669 &flow
->ufid
, flow
->pmd_id
, &actions
, dump_seq
,
1670 reval_seq
, flow
->stats
.used
, 0, NULL
);
1676 try_ukey_replace(struct umap
*umap
, struct udpif_key
*old_ukey
,
1677 struct udpif_key
*new_ukey
)
1678 OVS_REQUIRES(umap
->mutex
)
1679 OVS_TRY_LOCK(true, new_ukey
->mutex
)
1681 bool replaced
= false;
1683 if (!ovs_mutex_trylock(&old_ukey
->mutex
)) {
1684 if (old_ukey
->state
== UKEY_EVICTED
) {
1685 /* The flow was deleted during the current revalidator dump,
1686 * but its ukey won't be fully cleaned up until the sweep phase.
1687 * In the mean time, we are receiving upcalls for this traffic.
1688 * Expedite the (new) flow install by replacing the ukey. */
1689 ovs_mutex_lock(&new_ukey
->mutex
);
1690 cmap_replace(&umap
->cmap
, &old_ukey
->cmap_node
,
1691 &new_ukey
->cmap_node
, new_ukey
->hash
);
1692 ovsrcu_postpone(ukey_delete__
, old_ukey
);
1693 transition_ukey(old_ukey
, UKEY_DELETED
);
1694 transition_ukey(new_ukey
, UKEY_VISIBLE
);
1697 ovs_mutex_unlock(&old_ukey
->mutex
);
1701 COVERAGE_INC(upcall_ukey_replace
);
1703 COVERAGE_INC(handler_duplicate_upcall
);
1708 /* Attempts to insert a ukey into the shared ukey maps.
1710 * On success, returns true, installs the ukey and returns it in a locked
1711 * state. Otherwise, returns false. */
1713 ukey_install__(struct udpif
*udpif
, struct udpif_key
*new_ukey
)
1714 OVS_TRY_LOCK(true, new_ukey
->mutex
)
1717 struct udpif_key
*old_ukey
;
1719 bool locked
= false;
1721 idx
= new_ukey
->hash
% N_UMAPS
;
1722 umap
= &udpif
->ukeys
[idx
];
1723 ovs_mutex_lock(&umap
->mutex
);
1724 old_ukey
= ukey_lookup(udpif
, &new_ukey
->ufid
, new_ukey
->pmd_id
);
1726 /* Uncommon case: A ukey is already installed with the same UFID. */
1727 if (old_ukey
->key_len
== new_ukey
->key_len
1728 && !memcmp(old_ukey
->key
, new_ukey
->key
, new_ukey
->key_len
)) {
1729 locked
= try_ukey_replace(umap
, old_ukey
, new_ukey
);
1731 struct ds ds
= DS_EMPTY_INITIALIZER
;
1733 odp_format_ufid(&old_ukey
->ufid
, &ds
);
1734 ds_put_cstr(&ds
, " ");
1735 odp_flow_key_format(old_ukey
->key
, old_ukey
->key_len
, &ds
);
1736 ds_put_cstr(&ds
, "\n");
1737 odp_format_ufid(&new_ukey
->ufid
, &ds
);
1738 ds_put_cstr(&ds
, " ");
1739 odp_flow_key_format(new_ukey
->key
, new_ukey
->key_len
, &ds
);
1741 VLOG_WARN_RL(&rl
, "Conflicting ukey for flows:\n%s", ds_cstr(&ds
));
1745 ovs_mutex_lock(&new_ukey
->mutex
);
1746 cmap_insert(&umap
->cmap
, &new_ukey
->cmap_node
, new_ukey
->hash
);
1747 transition_ukey(new_ukey
, UKEY_VISIBLE
);
1750 ovs_mutex_unlock(&umap
->mutex
);
1756 transition_ukey_at(struct udpif_key
*ukey
, enum ukey_state dst
,
1758 OVS_REQUIRES(ukey
->mutex
)
1760 if (dst
< ukey
->state
) {
1761 VLOG_ABORT("Invalid ukey transition %d->%d (last transitioned from "
1762 "thread %u at %s)", ukey
->state
, dst
, ukey
->state_thread
,
1765 if (ukey
->state
== dst
&& dst
== UKEY_OPERATIONAL
) {
1769 /* Valid state transitions:
1770 * UKEY_CREATED -> UKEY_VISIBLE
1771 * Ukey is now visible in the umap.
1772 * UKEY_VISIBLE -> UKEY_OPERATIONAL
1773 * A handler has installed the flow, and the flow is in the datapath.
1774 * UKEY_VISIBLE -> UKEY_EVICTING
1775 * A handler installs the flow, then revalidator sweeps the ukey before
1776 * the flow is dumped. Most likely the flow was installed; start trying
1778 * UKEY_VISIBLE -> UKEY_EVICTED
1779 * A handler attempts to install the flow, but the datapath rejects it.
1780 * Consider that the datapath has already destroyed it.
1781 * UKEY_OPERATIONAL -> UKEY_EVICTING
1782 * A revalidator decides to evict the datapath flow.
1783 * UKEY_EVICTING -> UKEY_EVICTED
1784 * A revalidator has evicted the datapath flow.
1785 * UKEY_EVICTED -> UKEY_DELETED
1786 * A revalidator has removed the ukey from the umap and is deleting it.
1788 if (ukey
->state
== dst
- 1 || (ukey
->state
== UKEY_VISIBLE
&&
1789 dst
< UKEY_DELETED
)) {
1792 struct ds ds
= DS_EMPTY_INITIALIZER
;
1794 odp_format_ufid(&ukey
->ufid
, &ds
);
1795 VLOG_WARN_RL(&rl
, "Invalid state transition for ukey %s: %d -> %d",
1796 ds_cstr(&ds
), ukey
->state
, dst
);
1799 ukey
->state_thread
= ovsthread_id_self();
1800 ukey
->state_where
= where
;
1804 ukey_install(struct udpif
*udpif
, struct udpif_key
*ukey
)
1808 installed
= ukey_install__(udpif
, ukey
);
1810 ovs_mutex_unlock(&ukey
->mutex
);
1816 /* Searches for a ukey in 'udpif->ukeys' that matches 'flow' and attempts to
1817 * lock the ukey. If the ukey does not exist, create it.
1819 * Returns 0 on success, setting *result to the matching ukey and returning it
1820 * in a locked state. Otherwise, returns an errno and clears *result. EBUSY
1821 * indicates that another thread is handling this flow. Other errors indicate
1822 * an unexpected condition creating a new ukey.
1824 * *error is an output parameter provided to appease the threadsafety analyser,
1825 * and its value matches the return value. */
1827 ukey_acquire(struct udpif
*udpif
, const struct dpif_flow
*flow
,
1828 struct udpif_key
**result
, int *error
)
1829 OVS_TRY_LOCK(0, (*result
)->mutex
)
1831 struct udpif_key
*ukey
;
1834 ukey
= ukey_lookup(udpif
, &flow
->ufid
, flow
->pmd_id
);
1836 retval
= ovs_mutex_trylock(&ukey
->mutex
);
1838 /* Usually we try to avoid installing flows from revalidator threads,
1839 * because locking on a umap may cause handler threads to block.
1840 * However there are certain cases, like when ovs-vswitchd is
1841 * restarted, where it is desirable to handle flows that exist in the
1842 * datapath gracefully (ie, don't just clear the datapath). */
1845 retval
= ukey_create_from_dpif_flow(udpif
, flow
, &ukey
);
1849 install
= ukey_install__(udpif
, ukey
);
1853 ukey_delete__(ukey
);
1869 ukey_delete__(struct udpif_key
*ukey
)
1870 OVS_NO_THREAD_SAFETY_ANALYSIS
1873 if (ukey
->key_recirc_id
) {
1874 recirc_free_id(ukey
->key_recirc_id
);
1876 recirc_refs_unref(&ukey
->recircs
);
1877 xlate_cache_delete(ukey
->xcache
);
1878 ofpbuf_delete(ovsrcu_get(struct ofpbuf
*, &ukey
->actions
));
1879 ovs_mutex_destroy(&ukey
->mutex
);
1885 ukey_delete(struct umap
*umap
, struct udpif_key
*ukey
)
1886 OVS_REQUIRES(umap
->mutex
)
1888 ovs_mutex_lock(&ukey
->mutex
);
1889 if (ukey
->state
< UKEY_DELETED
) {
1890 cmap_remove(&umap
->cmap
, &ukey
->cmap_node
, ukey
->hash
);
1891 ovsrcu_postpone(ukey_delete__
, ukey
);
1892 transition_ukey(ukey
, UKEY_DELETED
);
1894 ovs_mutex_unlock(&ukey
->mutex
);
1898 should_revalidate(const struct udpif
*udpif
, uint64_t packets
,
1901 long long int metric
, now
, duration
;
1904 /* Always revalidate the first time a flow is dumped. */
1908 if (udpif
->dump_duration
< 200) {
1909 /* We are likely to handle full revalidation for the flows. */
1913 /* Calculate the mean time between seeing these packets. If this
1914 * exceeds the threshold, then delete the flow rather than performing
1915 * costly revalidation for flows that aren't being hit frequently.
1917 * This is targeted at situations where the dump_duration is high (~1s),
1918 * and revalidation is triggered by a call to udpif_revalidate(). In
1919 * these situations, revalidation of all flows causes fluctuations in the
1920 * flow_limit due to the interaction with the dump_duration and max_idle.
1921 * This tends to result in deletion of low-throughput flows anyway, so
1922 * skip the revalidation and just delete those flows. */
1923 packets
= MAX(packets
, 1);
1924 now
= MAX(used
, time_msec());
1925 duration
= now
- used
;
1926 metric
= duration
/ packets
;
1929 /* The flow is receiving more than ~5pps, so keep it. */
1935 struct reval_context
{
1936 /* Optional output parameters */
1937 struct flow_wildcards
*wc
;
1938 struct ofpbuf
*odp_actions
;
1939 struct netflow
**netflow
;
1940 struct xlate_cache
*xcache
;
1942 /* Required output parameters */
1943 struct xlate_out xout
;
1947 /* Translates 'key' into a flow, populating 'ctx' as it goes along.
1949 * Returns 0 on success, otherwise a positive errno value.
1951 * The caller is responsible for uninitializing ctx->xout on success.
1954 xlate_key(struct udpif
*udpif
, const struct nlattr
*key
, unsigned int len
,
1955 const struct dpif_flow_stats
*push
, struct reval_context
*ctx
)
1957 struct ofproto_dpif
*ofproto
;
1958 ofp_port_t ofp_in_port
;
1959 struct xlate_in xin
;
1962 if (odp_flow_key_to_flow(key
, len
, &ctx
->flow
) == ODP_FIT_ERROR
) {
1966 error
= xlate_lookup(udpif
->backer
, &ctx
->flow
, &ofproto
, NULL
, NULL
,
1967 ctx
->netflow
, &ofp_in_port
);
1972 xlate_in_init(&xin
, ofproto
, ofproto_dpif_get_tables_version(ofproto
),
1973 &ctx
->flow
, ofp_in_port
, NULL
, push
->tcp_flags
,
1974 NULL
, ctx
->wc
, ctx
->odp_actions
);
1975 if (push
->n_packets
) {
1976 xin
.resubmit_stats
= push
;
1977 xin
.allow_side_effects
= true;
1979 xin
.xcache
= ctx
->xcache
;
1980 xlate_actions(&xin
, &ctx
->xout
);
1986 xlate_ukey(struct udpif
*udpif
, const struct udpif_key
*ukey
,
1987 uint16_t tcp_flags
, struct reval_context
*ctx
)
1989 struct dpif_flow_stats push
= {
1990 .tcp_flags
= tcp_flags
,
1992 return xlate_key(udpif
, ukey
->key
, ukey
->key_len
, &push
, ctx
);
1996 populate_xcache(struct udpif
*udpif
, struct udpif_key
*ukey
,
1998 OVS_REQUIRES(ukey
->mutex
)
2000 struct reval_context ctx
= {
2001 .odp_actions
= NULL
,
2007 ovs_assert(!ukey
->xcache
);
2008 ukey
->xcache
= ctx
.xcache
= xlate_cache_new();
2009 error
= xlate_ukey(udpif
, ukey
, tcp_flags
, &ctx
);
2013 xlate_out_uninit(&ctx
.xout
);
2018 static enum reval_result
2019 revalidate_ukey__(struct udpif
*udpif
, const struct udpif_key
*ukey
,
2020 uint16_t tcp_flags
, struct ofpbuf
*odp_actions
,
2021 struct recirc_refs
*recircs
, struct xlate_cache
*xcache
)
2023 struct xlate_out
*xoutp
;
2024 struct netflow
*netflow
;
2025 struct flow_wildcards dp_mask
, wc
;
2026 enum reval_result result
;
2027 struct reval_context ctx
= {
2028 .odp_actions
= odp_actions
,
2029 .netflow
= &netflow
,
2034 result
= UKEY_DELETE
;
2038 if (xlate_ukey(udpif
, ukey
, tcp_flags
, &ctx
)) {
2043 if (xoutp
->avoid_caching
) {
2048 struct ofproto_dpif
*ofproto
;
2049 ofproto
= xlate_lookup_ofproto(udpif
->backer
, &ctx
.flow
, NULL
);
2050 uint32_t smid
= ofproto
->up
.slowpath_meter_id
;
2051 uint32_t cmid
= ofproto
->up
.controller_meter_id
;
2053 ofpbuf_clear(odp_actions
);
2054 compose_slow_path(udpif
, xoutp
, &ctx
.flow
, ctx
.flow
.in_port
.odp_port
,
2055 odp_actions
, smid
, cmid
);
2058 if (odp_flow_key_to_mask(ukey
->mask
, ukey
->mask_len
, &dp_mask
, &ctx
.flow
)
2063 /* Do not modify if any bit is wildcarded by the installed datapath flow,
2064 * but not the newly revalidated wildcard mask (wc), i.e., if revalidation
2065 * tells that the datapath flow is now too generic and must be narrowed
2066 * down. Note that we do not know if the datapath has ignored any of the
2067 * wildcarded bits, so we may be overtly conservative here. */
2068 if (flow_wildcards_has_extra(&dp_mask
, ctx
.wc
)) {
2072 if (!ofpbuf_equal(odp_actions
,
2073 ovsrcu_get(struct ofpbuf
*, &ukey
->actions
))) {
2074 /* The datapath mask was OK, but the actions seem to have changed.
2075 * Let's modify it in place. */
2076 result
= UKEY_MODIFY
;
2077 /* Transfer recirc action ID references to the caller. */
2078 recirc_refs_swap(recircs
, &xoutp
->recircs
);
2085 if (netflow
&& result
== UKEY_DELETE
) {
2086 netflow_flow_clear(netflow
, &ctx
.flow
);
2088 xlate_out_uninit(xoutp
);
2092 /* Verifies that the datapath actions of 'ukey' are still correct, and pushes
2095 * Returns a recommended action for 'ukey', options include:
2096 * UKEY_DELETE The ukey should be deleted.
2097 * UKEY_KEEP The ukey is fine as is.
2098 * UKEY_MODIFY The ukey's actions should be changed but is otherwise
2099 * fine. Callers should change the actions to those found
2100 * in the caller supplied 'odp_actions' buffer. The
2101 * recirculation references can be found in 'recircs' and
2102 * must be handled by the caller.
2104 * If the result is UKEY_MODIFY, then references to all recirc_ids used by the
2105 * new flow will be held within 'recircs' (which may be none).
2107 * The caller is responsible for both initializing 'recircs' prior this call,
2108 * and ensuring any references are eventually freed.
2110 static enum reval_result
2111 revalidate_ukey(struct udpif
*udpif
, struct udpif_key
*ukey
,
2112 const struct dpif_flow_stats
*stats
,
2113 struct ofpbuf
*odp_actions
, uint64_t reval_seq
,
2114 struct recirc_refs
*recircs
)
2115 OVS_REQUIRES(ukey
->mutex
)
2117 bool need_revalidate
= ukey
->reval_seq
!= reval_seq
;
2118 enum reval_result result
= UKEY_DELETE
;
2119 struct dpif_flow_stats push
;
2121 ofpbuf_clear(odp_actions
);
2123 push
.used
= stats
->used
;
2124 push
.tcp_flags
= stats
->tcp_flags
;
2125 push
.n_packets
= (stats
->n_packets
> ukey
->stats
.n_packets
2126 ? stats
->n_packets
- ukey
->stats
.n_packets
2128 push
.n_bytes
= (stats
->n_bytes
> ukey
->stats
.n_bytes
2129 ? stats
->n_bytes
- ukey
->stats
.n_bytes
2132 if (need_revalidate
) {
2133 if (should_revalidate(udpif
, push
.n_packets
, ukey
->stats
.used
)) {
2134 if (!ukey
->xcache
) {
2135 ukey
->xcache
= xlate_cache_new();
2137 xlate_cache_clear(ukey
->xcache
);
2139 result
= revalidate_ukey__(udpif
, ukey
, push
.tcp_flags
,
2140 odp_actions
, recircs
, ukey
->xcache
);
2141 } /* else delete; too expensive to revalidate */
2142 } else if (!push
.n_packets
|| ukey
->xcache
2143 || !populate_xcache(udpif
, ukey
, push
.tcp_flags
)) {
2147 /* Stats for deleted flows will be attributed upon flow deletion. Skip. */
2148 if (result
!= UKEY_DELETE
) {
2149 xlate_push_stats(ukey
->xcache
, &push
);
2150 ukey
->stats
= *stats
;
2151 ukey
->reval_seq
= reval_seq
;
2158 delete_op_init__(struct udpif
*udpif
, struct ukey_op
*op
,
2159 const struct dpif_flow
*flow
)
2162 op
->dop
.type
= DPIF_OP_FLOW_DEL
;
2163 op
->dop
.u
.flow_del
.key
= flow
->key
;
2164 op
->dop
.u
.flow_del
.key_len
= flow
->key_len
;
2165 op
->dop
.u
.flow_del
.ufid
= flow
->ufid_present
? &flow
->ufid
: NULL
;
2166 op
->dop
.u
.flow_del
.pmd_id
= flow
->pmd_id
;
2167 op
->dop
.u
.flow_del
.stats
= &op
->stats
;
2168 op
->dop
.u
.flow_del
.terse
= udpif_use_ufid(udpif
);
2172 delete_op_init(struct udpif
*udpif
, struct ukey_op
*op
, struct udpif_key
*ukey
)
2175 op
->dop
.type
= DPIF_OP_FLOW_DEL
;
2176 op
->dop
.u
.flow_del
.key
= ukey
->key
;
2177 op
->dop
.u
.flow_del
.key_len
= ukey
->key_len
;
2178 op
->dop
.u
.flow_del
.ufid
= ukey
->ufid_present
? &ukey
->ufid
: NULL
;
2179 op
->dop
.u
.flow_del
.pmd_id
= ukey
->pmd_id
;
2180 op
->dop
.u
.flow_del
.stats
= &op
->stats
;
2181 op
->dop
.u
.flow_del
.terse
= udpif_use_ufid(udpif
);
2185 put_op_init(struct ukey_op
*op
, struct udpif_key
*ukey
,
2186 enum dpif_flow_put_flags flags
)
2189 op
->dop
.type
= DPIF_OP_FLOW_PUT
;
2190 op
->dop
.u
.flow_put
.flags
= flags
;
2191 op
->dop
.u
.flow_put
.key
= ukey
->key
;
2192 op
->dop
.u
.flow_put
.key_len
= ukey
->key_len
;
2193 op
->dop
.u
.flow_put
.mask
= ukey
->mask
;
2194 op
->dop
.u
.flow_put
.mask_len
= ukey
->mask_len
;
2195 op
->dop
.u
.flow_put
.ufid
= ukey
->ufid_present
? &ukey
->ufid
: NULL
;
2196 op
->dop
.u
.flow_put
.pmd_id
= ukey
->pmd_id
;
2197 op
->dop
.u
.flow_put
.stats
= NULL
;
2198 ukey_get_actions(ukey
, &op
->dop
.u
.flow_put
.actions
,
2199 &op
->dop
.u
.flow_put
.actions_len
);
2202 /* Executes datapath operations 'ops' and attributes stats retrieved from the
2203 * datapath as part of those operations. */
2205 push_dp_ops(struct udpif
*udpif
, struct ukey_op
*ops
, size_t n_ops
)
2207 struct dpif_op
*opsp
[REVALIDATE_MAX_BATCH
];
2210 ovs_assert(n_ops
<= REVALIDATE_MAX_BATCH
);
2211 for (i
= 0; i
< n_ops
; i
++) {
2212 opsp
[i
] = &ops
[i
].dop
;
2214 dpif_operate(udpif
->dpif
, opsp
, n_ops
);
2216 for (i
= 0; i
< n_ops
; i
++) {
2217 struct ukey_op
*op
= &ops
[i
];
2218 struct dpif_flow_stats
*push
, *stats
, push_buf
;
2220 stats
= op
->dop
.u
.flow_del
.stats
;
2223 if (op
->dop
.type
!= DPIF_OP_FLOW_DEL
) {
2224 /* Only deleted flows need their stats pushed. */
2228 if (op
->dop
.error
) {
2229 /* flow_del error, 'stats' is unusable. */
2234 ovs_mutex_lock(&op
->ukey
->mutex
);
2235 transition_ukey(op
->ukey
, UKEY_EVICTED
);
2236 push
->used
= MAX(stats
->used
, op
->ukey
->stats
.used
);
2237 push
->tcp_flags
= stats
->tcp_flags
| op
->ukey
->stats
.tcp_flags
;
2238 push
->n_packets
= stats
->n_packets
- op
->ukey
->stats
.n_packets
;
2239 push
->n_bytes
= stats
->n_bytes
- op
->ukey
->stats
.n_bytes
;
2240 ovs_mutex_unlock(&op
->ukey
->mutex
);
2245 if (push
->n_packets
|| netflow_exists()) {
2246 const struct nlattr
*key
= op
->dop
.u
.flow_del
.key
;
2247 size_t key_len
= op
->dop
.u
.flow_del
.key_len
;
2248 struct netflow
*netflow
;
2249 struct reval_context ctx
= {
2250 .netflow
= &netflow
,
2255 ovs_mutex_lock(&op
->ukey
->mutex
);
2256 if (op
->ukey
->xcache
) {
2257 xlate_push_stats(op
->ukey
->xcache
, push
);
2258 ovs_mutex_unlock(&op
->ukey
->mutex
);
2261 ovs_mutex_unlock(&op
->ukey
->mutex
);
2262 key
= op
->ukey
->key
;
2263 key_len
= op
->ukey
->key_len
;
2266 error
= xlate_key(udpif
, key
, key_len
, push
, &ctx
);
2268 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2270 VLOG_WARN_RL(&rl
, "xlate_key failed (%s)!",
2271 ovs_strerror(error
));
2273 xlate_out_uninit(&ctx
.xout
);
2275 netflow_flow_clear(netflow
, &ctx
.flow
);
2282 /* Executes datapath operations 'ops', attributes stats retrieved from the
2283 * datapath, and deletes ukeys corresponding to deleted flows. */
2285 push_ukey_ops(struct udpif
*udpif
, struct umap
*umap
,
2286 struct ukey_op
*ops
, size_t n_ops
)
2290 push_dp_ops(udpif
, ops
, n_ops
);
2291 ovs_mutex_lock(&umap
->mutex
);
2292 for (i
= 0; i
< n_ops
; i
++) {
2293 if (ops
[i
].dop
.type
== DPIF_OP_FLOW_DEL
) {
2294 ukey_delete(umap
, ops
[i
].ukey
);
2297 ovs_mutex_unlock(&umap
->mutex
);
2301 log_unexpected_flow(const struct dpif_flow
*flow
, int error
)
2303 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 60);
2304 struct ds ds
= DS_EMPTY_INITIALIZER
;
2306 ds_put_format(&ds
, "Failed to acquire udpif_key corresponding to "
2307 "unexpected flow (%s): ", ovs_strerror(error
));
2308 odp_format_ufid(&flow
->ufid
, &ds
);
2309 VLOG_WARN_RL(&rl
, "%s", ds_cstr(&ds
));
2314 reval_op_init(struct ukey_op
*op
, enum reval_result result
,
2315 struct udpif
*udpif
, struct udpif_key
*ukey
,
2316 struct recirc_refs
*recircs
, struct ofpbuf
*odp_actions
)
2317 OVS_REQUIRES(ukey
->mutex
)
2319 if (result
== UKEY_DELETE
) {
2320 delete_op_init(udpif
, op
, ukey
);
2321 transition_ukey(ukey
, UKEY_EVICTING
);
2322 } else if (result
== UKEY_MODIFY
) {
2323 /* Store the new recircs. */
2324 recirc_refs_swap(&ukey
->recircs
, recircs
);
2325 /* Release old recircs. */
2326 recirc_refs_unref(recircs
);
2327 /* ukey->key_recirc_id remains, as the key is the same as before. */
2329 ukey_set_actions(ukey
, odp_actions
);
2330 put_op_init(op
, ukey
, DPIF_FP_MODIFY
);
2335 revalidate(struct revalidator
*revalidator
)
2337 uint64_t odp_actions_stub
[1024 / 8];
2338 struct ofpbuf odp_actions
= OFPBUF_STUB_INITIALIZER(odp_actions_stub
);
2340 struct udpif
*udpif
= revalidator
->udpif
;
2341 struct dpif_flow_dump_thread
*dump_thread
;
2342 uint64_t dump_seq
, reval_seq
;
2343 unsigned int flow_limit
;
2345 dump_seq
= seq_read(udpif
->dump_seq
);
2346 reval_seq
= seq_read(udpif
->reval_seq
);
2347 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
2348 dump_thread
= dpif_flow_dump_thread_create(udpif
->dump
);
2350 struct ukey_op ops
[REVALIDATE_MAX_BATCH
];
2353 struct dpif_flow flows
[REVALIDATE_MAX_BATCH
];
2354 const struct dpif_flow
*f
;
2357 long long int max_idle
;
2362 n_dumped
= dpif_flow_dump_next(dump_thread
, flows
, ARRAY_SIZE(flows
));
2369 /* In normal operation we want to keep flows around until they have
2370 * been idle for 'ofproto_max_idle' milliseconds. However:
2372 * - If the number of datapath flows climbs above 'flow_limit',
2373 * drop that down to 100 ms to try to bring the flows down to
2376 * - If the number of datapath flows climbs above twice
2377 * 'flow_limit', delete all the datapath flows as an emergency
2378 * measure. (We reassess this condition for the next batch of
2379 * datapath flows, so we will recover before all the flows are
2381 n_dp_flows
= udpif_get_n_flows(udpif
);
2382 kill_them_all
= n_dp_flows
> flow_limit
* 2;
2383 max_idle
= n_dp_flows
> flow_limit
? 100 : ofproto_max_idle
;
2385 for (f
= flows
; f
< &flows
[n_dumped
]; f
++) {
2386 long long int used
= f
->stats
.used
;
2387 struct recirc_refs recircs
= RECIRC_REFS_EMPTY_INITIALIZER
;
2388 enum reval_result result
;
2389 struct udpif_key
*ukey
;
2390 bool already_dumped
;
2393 if (ukey_acquire(udpif
, f
, &ukey
, &error
)) {
2394 if (error
== EBUSY
) {
2395 /* Another thread is processing this flow, so don't bother
2397 COVERAGE_INC(upcall_ukey_contention
);
2399 log_unexpected_flow(f
, error
);
2400 if (error
!= ENOENT
) {
2401 delete_op_init__(udpif
, &ops
[n_ops
++], f
);
2407 already_dumped
= ukey
->dump_seq
== dump_seq
;
2408 if (already_dumped
) {
2409 /* The flow has already been handled during this flow dump
2410 * operation. Skip it. */
2412 COVERAGE_INC(dumped_duplicate_flow
);
2414 COVERAGE_INC(dumped_new_flow
);
2416 ovs_mutex_unlock(&ukey
->mutex
);
2420 if (ukey
->state
<= UKEY_OPERATIONAL
) {
2421 /* The flow is now confirmed to be in the datapath. */
2422 transition_ukey(ukey
, UKEY_OPERATIONAL
);
2424 VLOG_INFO("Unexpected ukey transition from state %d "
2425 "(last transitioned from thread %u at %s)",
2426 ukey
->state
, ukey
->state_thread
, ukey
->state_where
);
2427 ovs_mutex_unlock(&ukey
->mutex
);
2432 used
= ukey
->created
;
2434 if (kill_them_all
|| (used
&& used
< now
- max_idle
)) {
2435 result
= UKEY_DELETE
;
2437 result
= revalidate_ukey(udpif
, ukey
, &f
->stats
, &odp_actions
,
2438 reval_seq
, &recircs
);
2440 ukey
->dump_seq
= dump_seq
;
2442 if (result
!= UKEY_KEEP
) {
2443 /* Takes ownership of 'recircs'. */
2444 reval_op_init(&ops
[n_ops
++], result
, udpif
, ukey
, &recircs
,
2447 ovs_mutex_unlock(&ukey
->mutex
);
2451 /* Push datapath ops but defer ukey deletion to 'sweep' phase. */
2452 push_dp_ops(udpif
, ops
, n_ops
);
2456 dpif_flow_dump_thread_destroy(dump_thread
);
2457 ofpbuf_uninit(&odp_actions
);
2460 /* Pauses the 'revalidator', can only proceed after main thread
2461 * calls udpif_resume_revalidators(). */
2463 revalidator_pause(struct revalidator
*revalidator
)
2465 /* The first block is for sync'ing the pause with main thread. */
2466 ovs_barrier_block(&revalidator
->udpif
->pause_barrier
);
2467 /* The second block is for pausing until main thread resumes. */
2468 ovs_barrier_block(&revalidator
->udpif
->pause_barrier
);
2472 revalidator_sweep__(struct revalidator
*revalidator
, bool purge
)
2474 struct udpif
*udpif
;
2475 uint64_t dump_seq
, reval_seq
;
2478 udpif
= revalidator
->udpif
;
2479 dump_seq
= seq_read(udpif
->dump_seq
);
2480 reval_seq
= seq_read(udpif
->reval_seq
);
2481 slice
= revalidator
- udpif
->revalidators
;
2482 ovs_assert(slice
< udpif
->n_revalidators
);
2484 for (int i
= slice
; i
< N_UMAPS
; i
+= udpif
->n_revalidators
) {
2485 uint64_t odp_actions_stub
[1024 / 8];
2486 struct ofpbuf odp_actions
= OFPBUF_STUB_INITIALIZER(odp_actions_stub
);
2488 struct ukey_op ops
[REVALIDATE_MAX_BATCH
];
2489 struct udpif_key
*ukey
;
2490 struct umap
*umap
= &udpif
->ukeys
[i
];
2493 CMAP_FOR_EACH(ukey
, cmap_node
, &umap
->cmap
) {
2494 enum ukey_state ukey_state
;
2496 /* Handler threads could be holding a ukey lock while it installs a
2497 * new flow, so don't hang around waiting for access to it. */
2498 if (ovs_mutex_trylock(&ukey
->mutex
)) {
2501 ukey_state
= ukey
->state
;
2502 if (ukey_state
== UKEY_OPERATIONAL
2503 || (ukey_state
== UKEY_VISIBLE
&& purge
)) {
2504 struct recirc_refs recircs
= RECIRC_REFS_EMPTY_INITIALIZER
;
2505 bool seq_mismatch
= (ukey
->dump_seq
!= dump_seq
2506 && ukey
->reval_seq
!= reval_seq
);
2507 enum reval_result result
;
2510 result
= UKEY_DELETE
;
2511 } else if (!seq_mismatch
) {
2514 struct dpif_flow_stats stats
;
2515 COVERAGE_INC(revalidate_missed_dp_flow
);
2516 memset(&stats
, 0, sizeof stats
);
2517 result
= revalidate_ukey(udpif
, ukey
, &stats
, &odp_actions
,
2518 reval_seq
, &recircs
);
2520 if (result
!= UKEY_KEEP
) {
2521 /* Clears 'recircs' if filled by revalidate_ukey(). */
2522 reval_op_init(&ops
[n_ops
++], result
, udpif
, ukey
, &recircs
,
2526 ovs_mutex_unlock(&ukey
->mutex
);
2528 if (ukey_state
== UKEY_EVICTED
) {
2529 /* The common flow deletion case involves deletion of the flow
2530 * during the dump phase and ukey deletion here. */
2531 ovs_mutex_lock(&umap
->mutex
);
2532 ukey_delete(umap
, ukey
);
2533 ovs_mutex_unlock(&umap
->mutex
);
2536 if (n_ops
== REVALIDATE_MAX_BATCH
) {
2537 /* Update/delete missed flows and clean up corresponding ukeys
2539 push_ukey_ops(udpif
, umap
, ops
, n_ops
);
2545 push_ukey_ops(udpif
, umap
, ops
, n_ops
);
2548 ofpbuf_uninit(&odp_actions
);
2554 revalidator_sweep(struct revalidator
*revalidator
)
2556 revalidator_sweep__(revalidator
, false);
2560 revalidator_purge(struct revalidator
*revalidator
)
2562 revalidator_sweep__(revalidator
, true);
2565 /* In reaction to dpif purge, purges all 'ukey's with same 'pmd_id'. */
2567 dp_purge_cb(void *aux
, unsigned pmd_id
)
2568 OVS_NO_THREAD_SAFETY_ANALYSIS
2570 struct udpif
*udpif
= aux
;
2573 udpif_pause_revalidators(udpif
);
2574 for (i
= 0; i
< N_UMAPS
; i
++) {
2575 struct ukey_op ops
[REVALIDATE_MAX_BATCH
];
2576 struct udpif_key
*ukey
;
2577 struct umap
*umap
= &udpif
->ukeys
[i
];
2580 CMAP_FOR_EACH(ukey
, cmap_node
, &umap
->cmap
) {
2581 if (ukey
->pmd_id
== pmd_id
) {
2582 delete_op_init(udpif
, &ops
[n_ops
++], ukey
);
2583 transition_ukey(ukey
, UKEY_EVICTING
);
2585 if (n_ops
== REVALIDATE_MAX_BATCH
) {
2586 push_ukey_ops(udpif
, umap
, ops
, n_ops
);
2593 push_ukey_ops(udpif
, umap
, ops
, n_ops
);
2598 udpif_resume_revalidators(udpif
);
2602 upcall_unixctl_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2603 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
2605 struct ds ds
= DS_EMPTY_INITIALIZER
;
2606 struct udpif
*udpif
;
2608 LIST_FOR_EACH (udpif
, list_node
, &all_udpifs
) {
2609 unsigned int flow_limit
;
2613 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
2614 ufid_enabled
= udpif_use_ufid(udpif
);
2616 ds_put_format(&ds
, "%s:\n", dpif_name(udpif
->dpif
));
2617 ds_put_format(&ds
, "\tflows : (current %lu)"
2618 " (avg %u) (max %u) (limit %u)\n", udpif_get_n_flows(udpif
),
2619 udpif
->avg_n_flows
, udpif
->max_n_flows
, flow_limit
);
2620 ds_put_format(&ds
, "\tdump duration : %lldms\n", udpif
->dump_duration
);
2621 ds_put_format(&ds
, "\tufid enabled : ");
2623 ds_put_format(&ds
, "true\n");
2625 ds_put_format(&ds
, "false\n");
2627 ds_put_char(&ds
, '\n');
2629 for (i
= 0; i
< n_revalidators
; i
++) {
2630 struct revalidator
*revalidator
= &udpif
->revalidators
[i
];
2631 int j
, elements
= 0;
2633 for (j
= i
; j
< N_UMAPS
; j
+= n_revalidators
) {
2634 elements
+= cmap_count(&udpif
->ukeys
[j
].cmap
);
2636 ds_put_format(&ds
, "\t%u: (keys %d)\n", revalidator
->id
, elements
);
2640 unixctl_command_reply(conn
, ds_cstr(&ds
));
2644 /* Disable using the megaflows.
2646 * This command is only needed for advanced debugging, so it's not
2647 * documented in the man page. */
2649 upcall_unixctl_disable_megaflows(struct unixctl_conn
*conn
,
2650 int argc OVS_UNUSED
,
2651 const char *argv
[] OVS_UNUSED
,
2652 void *aux OVS_UNUSED
)
2654 atomic_store_relaxed(&enable_megaflows
, false);
2655 udpif_flush_all_datapaths();
2656 unixctl_command_reply(conn
, "megaflows disabled");
2659 /* Re-enable using megaflows.
2661 * This command is only needed for advanced debugging, so it's not
2662 * documented in the man page. */
2664 upcall_unixctl_enable_megaflows(struct unixctl_conn
*conn
,
2665 int argc OVS_UNUSED
,
2666 const char *argv
[] OVS_UNUSED
,
2667 void *aux OVS_UNUSED
)
2669 atomic_store_relaxed(&enable_megaflows
, true);
2670 udpif_flush_all_datapaths();
2671 unixctl_command_reply(conn
, "megaflows enabled");
2674 /* Disable skipping flow attributes during flow dump.
2676 * This command is only needed for advanced debugging, so it's not
2677 * documented in the man page. */
2679 upcall_unixctl_disable_ufid(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2680 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
2682 atomic_store_relaxed(&enable_ufid
, false);
2683 unixctl_command_reply(conn
, "Datapath dumping tersely using UFID disabled");
2686 /* Re-enable skipping flow attributes during flow dump.
2688 * This command is only needed for advanced debugging, so it's not documented
2689 * in the man page. */
2691 upcall_unixctl_enable_ufid(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2692 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
2694 atomic_store_relaxed(&enable_ufid
, true);
2695 unixctl_command_reply(conn
, "Datapath dumping tersely using UFID enabled "
2696 "for supported datapaths");
2699 /* Set the flow limit.
2701 * This command is only needed for advanced debugging, so it's not
2702 * documented in the man page. */
2704 upcall_unixctl_set_flow_limit(struct unixctl_conn
*conn
,
2705 int argc OVS_UNUSED
,
2707 void *aux OVS_UNUSED
)
2709 struct ds ds
= DS_EMPTY_INITIALIZER
;
2710 struct udpif
*udpif
;
2711 unsigned int flow_limit
= atoi(argv
[1]);
2713 LIST_FOR_EACH (udpif
, list_node
, &all_udpifs
) {
2714 atomic_store_relaxed(&udpif
->flow_limit
, flow_limit
);
2716 ds_put_format(&ds
, "set flow_limit to %u\n", flow_limit
);
2717 unixctl_command_reply(conn
, ds_cstr(&ds
));
2722 upcall_unixctl_dump_wait(struct unixctl_conn
*conn
,
2723 int argc OVS_UNUSED
,
2724 const char *argv
[] OVS_UNUSED
,
2725 void *aux OVS_UNUSED
)
2727 if (ovs_list_is_singleton(&all_udpifs
)) {
2728 struct udpif
*udpif
= NULL
;
2731 udpif
= OBJECT_CONTAINING(ovs_list_front(&all_udpifs
), udpif
, list_node
);
2732 len
= (udpif
->n_conns
+ 1) * sizeof *udpif
->conns
;
2733 udpif
->conn_seq
= seq_read(udpif
->dump_seq
);
2734 udpif
->conns
= xrealloc(udpif
->conns
, len
);
2735 udpif
->conns
[udpif
->n_conns
++] = conn
;
2737 unixctl_command_reply_error(conn
, "can't wait on multiple udpifs.");
2742 upcall_unixctl_purge(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2743 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
2745 struct udpif
*udpif
;
2747 LIST_FOR_EACH (udpif
, list_node
, &all_udpifs
) {
2750 for (n
= 0; n
< udpif
->n_revalidators
; n
++) {
2751 revalidator_purge(&udpif
->revalidators
[n
]);
2754 unixctl_command_reply(conn
, "");