1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
16 #include "ofproto-dpif-upcall.h"
26 #include "dynamic-string.h"
27 #include "fail-open.h"
28 #include "guarded-list.h"
33 #include "ofproto-dpif-ipfix.h"
34 #include "ofproto-dpif-sflow.h"
35 #include "ofproto-dpif-xlate.h"
38 #include "poll-loop.h"
41 #include "openvswitch/vlog.h"
43 #define MAX_QUEUE_LENGTH 512
44 #define UPCALL_MAX_BATCH 64
45 #define REVALIDATE_MAX_BATCH 50
47 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_upcall
);
49 COVERAGE_DEFINE(dumped_duplicate_flow
);
50 COVERAGE_DEFINE(dumped_new_flow
);
51 COVERAGE_DEFINE(handler_duplicate_upcall
);
52 COVERAGE_DEFINE(upcall_ukey_contention
);
53 COVERAGE_DEFINE(revalidate_missed_dp_flow
);
55 /* A thread that reads upcalls from dpif, forwards each upcall's packet,
56 * and possibly sets up a kernel flow as a cache. */
58 struct udpif
*udpif
; /* Parent udpif. */
59 pthread_t thread
; /* Thread ID. */
60 uint32_t handler_id
; /* Handler id. */
63 /* In the absence of a multiple-writer multiple-reader datastructure for
64 * storing ukeys, we use a large number of cmaps, each with its own lock for
66 #define N_UMAPS 512 /* per udpif. */
68 struct ovs_mutex mutex
; /* Take for writing to the following. */
69 struct cmap cmap
; /* Datapath flow keys. */
72 /* A thread that processes datapath flows, updates OpenFlow statistics, and
73 * updates or removes them if necessary. */
75 struct udpif
*udpif
; /* Parent udpif. */
76 pthread_t thread
; /* Thread ID. */
77 unsigned int id
; /* ovsthread_id_self(). */
80 /* An upcall handler for ofproto_dpif.
82 * udpif keeps records of two kind of logically separate units:
87 * - An array of 'struct handler's for upcall handling and flow
93 * - Revalidation threads which read the datapath flow table and maintains
97 struct ovs_list list_node
; /* In all_udpifs list. */
99 struct dpif
*dpif
; /* Datapath handle. */
100 struct dpif_backer
*backer
; /* Opaque dpif_backer pointer. */
102 struct handler
*handlers
; /* Upcall handlers. */
105 struct revalidator
*revalidators
; /* Flow revalidators. */
106 size_t n_revalidators
;
108 struct latch exit_latch
; /* Tells child threads to exit. */
111 struct seq
*reval_seq
; /* Incremented to force revalidation. */
112 bool reval_exit
; /* Set by leader on 'exit_latch. */
113 struct ovs_barrier reval_barrier
; /* Barrier used by revalidators. */
114 struct dpif_flow_dump
*dump
; /* DPIF flow dump state. */
115 long long int dump_duration
; /* Duration of the last flow dump. */
116 struct seq
*dump_seq
; /* Increments each dump iteration. */
117 atomic_bool enable_ufid
; /* If true, skip dumping flow attrs. */
119 /* There are 'N_UMAPS' maps containing 'struct udpif_key' elements.
121 * During the flow dump phase, revalidators insert into these with a random
122 * distribution. During the garbage collection phase, each revalidator
123 * takes care of garbage collecting a slice of these maps. */
126 /* Datapath flow statistics. */
127 unsigned int max_n_flows
;
128 unsigned int avg_n_flows
;
130 /* Following fields are accessed and modified by different threads. */
131 atomic_uint flow_limit
; /* Datapath flow hard limit. */
133 /* n_flows_mutex prevents multiple threads updating these concurrently. */
134 atomic_uint n_flows
; /* Number of flows in the datapath. */
135 atomic_llong n_flows_timestamp
; /* Last time n_flows was updated. */
136 struct ovs_mutex n_flows_mutex
;
138 /* Following fields are accessed and modified only from the main thread. */
139 struct unixctl_conn
**conns
; /* Connections waiting on dump_seq. */
140 uint64_t conn_seq
; /* Corresponds to 'dump_seq' when
141 conns[n_conns-1] was stored. */
142 size_t n_conns
; /* Number of connections waiting. */
146 BAD_UPCALL
, /* Some kind of bug somewhere. */
147 MISS_UPCALL
, /* A flow miss. */
148 SFLOW_UPCALL
, /* sFlow sample. */
149 FLOW_SAMPLE_UPCALL
, /* Per-flow sampling. */
150 IPFIX_UPCALL
/* Per-bridge sampling. */
154 struct ofproto_dpif
*ofproto
; /* Parent ofproto. */
155 const struct recirc_id_node
*recirc
; /* Recirculation context. */
156 bool have_recirc_ref
; /* Reference held on recirc ctx? */
158 /* The flow and packet are only required to be constant when using
159 * dpif-netdev. If a modification is absolutely necessary, a const cast
160 * may be used with other datapaths. */
161 const struct flow
*flow
; /* Parsed representation of the packet. */
162 const ovs_u128
*ufid
; /* Unique identifier for 'flow'. */
163 int pmd_id
; /* Datapath poll mode driver id. */
164 const struct dp_packet
*packet
; /* Packet associated with this upcall. */
165 ofp_port_t in_port
; /* OpenFlow in port, or OFPP_NONE. */
167 enum dpif_upcall_type type
; /* Datapath type of the upcall. */
168 const struct nlattr
*userdata
; /* Userdata for DPIF_UC_ACTION Upcalls. */
170 bool xout_initialized
; /* True if 'xout' must be uninitialized. */
171 struct xlate_out xout
; /* Result of xlate_actions(). */
172 struct ofpbuf put_actions
; /* Actions 'put' in the fastapath. */
174 struct dpif_ipfix
*ipfix
; /* IPFIX pointer or NULL. */
175 struct dpif_sflow
*sflow
; /* SFlow pointer or NULL. */
177 bool vsp_adjusted
; /* 'packet' and 'flow' were adjusted for
178 VLAN splinters if true. */
180 struct udpif_key
*ukey
; /* Revalidator flow cache. */
181 bool ukey_persists
; /* Set true to keep 'ukey' beyond the
182 lifetime of this upcall. */
184 uint64_t dump_seq
; /* udpif->dump_seq at translation time. */
185 uint64_t reval_seq
; /* udpif->reval_seq at translation time. */
187 /* Not used by the upcall callback interface. */
188 const struct nlattr
*key
; /* Datapath flow key. */
189 size_t key_len
; /* Datapath flow key length. */
190 const struct nlattr
*out_tun_key
; /* Datapath output tunnel key. */
193 /* 'udpif_key's are responsible for tracking the little bit of state udpif
194 * needs to do flow expiration which can't be pulled directly from the
195 * datapath. They may be created by any handler or revalidator thread at any
196 * time, and read by any revalidator during the dump phase. They are however
197 * each owned by a single revalidator which takes care of destroying them
198 * during the garbage-collection phase.
200 * The mutex within the ukey protects some members of the ukey. The ukey
201 * itself is protected by RCU and is held within a umap in the parent udpif.
202 * Adding or removing a ukey from a umap is only safe when holding the
203 * corresponding umap lock. */
205 struct cmap_node cmap_node
; /* In parent revalidator 'ukeys' map. */
207 /* These elements are read only once created, and therefore aren't
208 * protected by a mutex. */
209 const struct nlattr
*key
; /* Datapath flow key. */
210 size_t key_len
; /* Length of 'key'. */
211 const struct nlattr
*mask
; /* Datapath flow mask. */
212 size_t mask_len
; /* Length of 'mask'. */
213 struct ofpbuf
*actions
; /* Datapath flow actions as nlattrs. */
214 ovs_u128 ufid
; /* Unique flow identifier. */
215 bool ufid_present
; /* True if 'ufid' is in datapath. */
216 uint32_t hash
; /* Pre-computed hash for 'key'. */
217 int pmd_id
; /* Datapath poll mode driver id. */
219 struct ovs_mutex mutex
; /* Guards the following. */
220 struct dpif_flow_stats stats OVS_GUARDED
; /* Last known stats.*/
221 long long int created OVS_GUARDED
; /* Estimate of creation time. */
222 uint64_t dump_seq OVS_GUARDED
; /* Tracks udpif->dump_seq. */
223 uint64_t reval_seq OVS_GUARDED
; /* Tracks udpif->reval_seq. */
224 bool flow_exists OVS_GUARDED
; /* Ensures flows are only deleted
227 struct xlate_cache
*xcache OVS_GUARDED
; /* Cache for xlate entries that
228 * are affected by this ukey.
229 * Used for stats and learning.*/
231 struct odputil_keybuf buf
;
235 /* Recirculation IDs with references held by the ukey. */
237 uint32_t recircs
[]; /* 'n_recircs' id's for which references are held. */
240 /* Datapath operation with optional ukey attached. */
242 struct udpif_key
*ukey
;
243 struct dpif_flow_stats stats
; /* Stats for 'op'. */
244 struct dpif_op dop
; /* Flow operation. */
247 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
248 static struct ovs_list all_udpifs
= OVS_LIST_INITIALIZER(&all_udpifs
);
250 static size_t recv_upcalls(struct handler
*);
251 static int process_upcall(struct udpif
*, struct upcall
*,
252 struct ofpbuf
*odp_actions
);
253 static void handle_upcalls(struct udpif
*, struct upcall
*, size_t n_upcalls
);
254 static void udpif_stop_threads(struct udpif
*);
255 static void udpif_start_threads(struct udpif
*, size_t n_handlers
,
256 size_t n_revalidators
);
257 static void *udpif_upcall_handler(void *);
258 static void *udpif_revalidator(void *);
259 static unsigned long udpif_get_n_flows(struct udpif
*);
260 static void revalidate(struct revalidator
*);
261 static void revalidator_sweep(struct revalidator
*);
262 static void revalidator_purge(struct revalidator
*);
263 static void upcall_unixctl_show(struct unixctl_conn
*conn
, int argc
,
264 const char *argv
[], void *aux
);
265 static void upcall_unixctl_disable_megaflows(struct unixctl_conn
*, int argc
,
266 const char *argv
[], void *aux
);
267 static void upcall_unixctl_enable_megaflows(struct unixctl_conn
*, int argc
,
268 const char *argv
[], void *aux
);
269 static void upcall_unixctl_disable_ufid(struct unixctl_conn
*, int argc
,
270 const char *argv
[], void *aux
);
271 static void upcall_unixctl_enable_ufid(struct unixctl_conn
*, int argc
,
272 const char *argv
[], void *aux
);
273 static void upcall_unixctl_set_flow_limit(struct unixctl_conn
*conn
, int argc
,
274 const char *argv
[], void *aux
);
275 static void upcall_unixctl_dump_wait(struct unixctl_conn
*conn
, int argc
,
276 const char *argv
[], void *aux
);
277 static void upcall_unixctl_purge(struct unixctl_conn
*conn
, int argc
,
278 const char *argv
[], void *aux
);
280 static struct udpif_key
*ukey_create_from_upcall(struct upcall
*);
281 static int ukey_create_from_dpif_flow(const struct udpif
*,
282 const struct dpif_flow
*,
283 struct udpif_key
**);
284 static bool ukey_install_start(struct udpif
*, struct udpif_key
*ukey
);
285 static bool ukey_install_finish(struct udpif_key
*ukey
, int error
);
286 static bool ukey_install(struct udpif
*udpif
, struct udpif_key
*ukey
);
287 static struct udpif_key
*ukey_lookup(struct udpif
*udpif
,
288 const ovs_u128
*ufid
);
289 static int ukey_acquire(struct udpif
*, const struct dpif_flow
*,
290 struct udpif_key
**result
, int *error
);
291 static void ukey_delete__(struct udpif_key
*);
292 static void ukey_delete(struct umap
*, struct udpif_key
*);
293 static enum upcall_type
classify_upcall(enum dpif_upcall_type type
,
294 const struct nlattr
*userdata
);
296 static int upcall_receive(struct upcall
*, const struct dpif_backer
*,
297 const struct dp_packet
*packet
, enum dpif_upcall_type
,
298 const struct nlattr
*userdata
, const struct flow
*,
299 const ovs_u128
*ufid
, const int pmd_id
);
300 static void upcall_uninit(struct upcall
*);
302 static upcall_callback upcall_cb
;
304 static atomic_bool enable_megaflows
= ATOMIC_VAR_INIT(true);
305 static atomic_bool enable_ufid
= ATOMIC_VAR_INIT(true);
308 udpif_create(struct dpif_backer
*backer
, struct dpif
*dpif
)
310 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
311 struct udpif
*udpif
= xzalloc(sizeof *udpif
);
313 if (ovsthread_once_start(&once
)) {
314 unixctl_command_register("upcall/show", "", 0, 0, upcall_unixctl_show
,
316 unixctl_command_register("upcall/disable-megaflows", "", 0, 0,
317 upcall_unixctl_disable_megaflows
, NULL
);
318 unixctl_command_register("upcall/enable-megaflows", "", 0, 0,
319 upcall_unixctl_enable_megaflows
, NULL
);
320 unixctl_command_register("upcall/disable-ufid", "", 0, 0,
321 upcall_unixctl_disable_ufid
, NULL
);
322 unixctl_command_register("upcall/enable-ufid", "", 0, 0,
323 upcall_unixctl_enable_ufid
, NULL
);
324 unixctl_command_register("upcall/set-flow-limit", "", 1, 1,
325 upcall_unixctl_set_flow_limit
, NULL
);
326 unixctl_command_register("revalidator/wait", "", 0, 0,
327 upcall_unixctl_dump_wait
, NULL
);
328 unixctl_command_register("revalidator/purge", "", 0, 0,
329 upcall_unixctl_purge
, NULL
);
330 ovsthread_once_done(&once
);
334 udpif
->backer
= backer
;
335 atomic_init(&udpif
->flow_limit
, MIN(ofproto_flow_limit
, 10000));
336 udpif
->reval_seq
= seq_create();
337 udpif
->dump_seq
= seq_create();
338 latch_init(&udpif
->exit_latch
);
339 list_push_back(&all_udpifs
, &udpif
->list_node
);
340 atomic_init(&udpif
->enable_ufid
, false);
341 atomic_init(&udpif
->n_flows
, 0);
342 atomic_init(&udpif
->n_flows_timestamp
, LLONG_MIN
);
343 ovs_mutex_init(&udpif
->n_flows_mutex
);
344 udpif
->ukeys
= xmalloc(N_UMAPS
* sizeof *udpif
->ukeys
);
345 for (int i
= 0; i
< N_UMAPS
; i
++) {
346 cmap_init(&udpif
->ukeys
[i
].cmap
);
347 ovs_mutex_init(&udpif
->ukeys
[i
].mutex
);
350 dpif_register_upcall_cb(dpif
, upcall_cb
, udpif
);
356 udpif_run(struct udpif
*udpif
)
358 if (udpif
->conns
&& udpif
->conn_seq
!= seq_read(udpif
->dump_seq
)) {
361 for (i
= 0; i
< udpif
->n_conns
; i
++) {
362 unixctl_command_reply(udpif
->conns
[i
], NULL
);
371 udpif_destroy(struct udpif
*udpif
)
373 udpif_stop_threads(udpif
);
375 for (int i
= 0; i
< N_UMAPS
; i
++) {
376 cmap_destroy(&udpif
->ukeys
[i
].cmap
);
377 ovs_mutex_destroy(&udpif
->ukeys
[i
].mutex
);
382 list_remove(&udpif
->list_node
);
383 latch_destroy(&udpif
->exit_latch
);
384 seq_destroy(udpif
->reval_seq
);
385 seq_destroy(udpif
->dump_seq
);
386 ovs_mutex_destroy(&udpif
->n_flows_mutex
);
390 /* Stops the handler and revalidator threads, must be enclosed in
391 * ovsrcu quiescent state unless when destroying udpif. */
393 udpif_stop_threads(struct udpif
*udpif
)
395 if (udpif
&& (udpif
->n_handlers
!= 0 || udpif
->n_revalidators
!= 0)) {
398 latch_set(&udpif
->exit_latch
);
400 for (i
= 0; i
< udpif
->n_handlers
; i
++) {
401 struct handler
*handler
= &udpif
->handlers
[i
];
403 xpthread_join(handler
->thread
, NULL
);
406 for (i
= 0; i
< udpif
->n_revalidators
; i
++) {
407 xpthread_join(udpif
->revalidators
[i
].thread
, NULL
);
410 dpif_disable_upcall(udpif
->dpif
);
412 for (i
= 0; i
< udpif
->n_revalidators
; i
++) {
413 struct revalidator
*revalidator
= &udpif
->revalidators
[i
];
415 /* Delete ukeys, and delete all flows from the datapath to prevent
416 * double-counting stats. */
417 revalidator_purge(revalidator
);
420 latch_poll(&udpif
->exit_latch
);
422 ovs_barrier_destroy(&udpif
->reval_barrier
);
424 free(udpif
->revalidators
);
425 udpif
->revalidators
= NULL
;
426 udpif
->n_revalidators
= 0;
428 free(udpif
->handlers
);
429 udpif
->handlers
= NULL
;
430 udpif
->n_handlers
= 0;
434 /* Starts the handler and revalidator threads, must be enclosed in
435 * ovsrcu quiescent state. */
437 udpif_start_threads(struct udpif
*udpif
, size_t n_handlers
,
438 size_t n_revalidators
)
440 if (udpif
&& n_handlers
&& n_revalidators
) {
444 udpif
->n_handlers
= n_handlers
;
445 udpif
->n_revalidators
= n_revalidators
;
447 udpif
->handlers
= xzalloc(udpif
->n_handlers
* sizeof *udpif
->handlers
);
448 for (i
= 0; i
< udpif
->n_handlers
; i
++) {
449 struct handler
*handler
= &udpif
->handlers
[i
];
451 handler
->udpif
= udpif
;
452 handler
->handler_id
= i
;
453 handler
->thread
= ovs_thread_create(
454 "handler", udpif_upcall_handler
, handler
);
457 enable_ufid
= ofproto_dpif_get_enable_ufid(udpif
->backer
);
458 atomic_init(&udpif
->enable_ufid
, enable_ufid
);
459 dpif_enable_upcall(udpif
->dpif
);
461 ovs_barrier_init(&udpif
->reval_barrier
, udpif
->n_revalidators
);
462 udpif
->reval_exit
= false;
463 udpif
->revalidators
= xzalloc(udpif
->n_revalidators
464 * sizeof *udpif
->revalidators
);
465 for (i
= 0; i
< udpif
->n_revalidators
; i
++) {
466 struct revalidator
*revalidator
= &udpif
->revalidators
[i
];
468 revalidator
->udpif
= udpif
;
469 revalidator
->thread
= ovs_thread_create(
470 "revalidator", udpif_revalidator
, revalidator
);
475 /* Tells 'udpif' how many threads it should use to handle upcalls.
476 * 'n_handlers' and 'n_revalidators' can never be zero. 'udpif''s
477 * datapath handle must have packet reception enabled before starting
480 udpif_set_threads(struct udpif
*udpif
, size_t n_handlers
,
481 size_t n_revalidators
)
484 ovs_assert(n_handlers
&& n_revalidators
);
486 ovsrcu_quiesce_start();
487 if (udpif
->n_handlers
!= n_handlers
488 || udpif
->n_revalidators
!= n_revalidators
) {
489 udpif_stop_threads(udpif
);
492 if (!udpif
->handlers
&& !udpif
->revalidators
) {
495 error
= dpif_handlers_set(udpif
->dpif
, n_handlers
);
497 VLOG_ERR("failed to configure handlers in dpif %s: %s",
498 dpif_name(udpif
->dpif
), ovs_strerror(error
));
502 udpif_start_threads(udpif
, n_handlers
, n_revalidators
);
504 ovsrcu_quiesce_end();
507 /* Waits for all ongoing upcall translations to complete. This ensures that
508 * there are no transient references to any removed ofprotos (or other
509 * objects). In particular, this should be called after an ofproto is removed
510 * (e.g. via xlate_remove_ofproto()) but before it is destroyed. */
512 udpif_synchronize(struct udpif
*udpif
)
514 /* This is stronger than necessary. It would be sufficient to ensure
515 * (somehow) that each handler and revalidator thread had passed through
516 * its main loop once. */
517 size_t n_handlers
= udpif
->n_handlers
;
518 size_t n_revalidators
= udpif
->n_revalidators
;
520 ovsrcu_quiesce_start();
521 udpif_stop_threads(udpif
);
522 udpif_start_threads(udpif
, n_handlers
, n_revalidators
);
523 ovsrcu_quiesce_end();
526 /* Notifies 'udpif' that something changed which may render previous
527 * xlate_actions() results invalid. */
529 udpif_revalidate(struct udpif
*udpif
)
531 seq_change(udpif
->reval_seq
);
534 /* Returns a seq which increments every time 'udpif' pulls stats from the
535 * datapath. Callers can use this to get a sense of when might be a good time
536 * to do periodic work which relies on relatively up to date statistics. */
538 udpif_dump_seq(struct udpif
*udpif
)
540 return udpif
->dump_seq
;
544 udpif_get_memory_usage(struct udpif
*udpif
, struct simap
*usage
)
548 simap_increase(usage
, "handlers", udpif
->n_handlers
);
550 simap_increase(usage
, "revalidators", udpif
->n_revalidators
);
551 for (i
= 0; i
< N_UMAPS
; i
++) {
552 simap_increase(usage
, "udpif keys", cmap_count(&udpif
->ukeys
[i
].cmap
));
556 /* Remove flows from a single datapath. */
558 udpif_flush(struct udpif
*udpif
)
560 size_t n_handlers
, n_revalidators
;
562 n_handlers
= udpif
->n_handlers
;
563 n_revalidators
= udpif
->n_revalidators
;
565 ovsrcu_quiesce_start();
567 udpif_stop_threads(udpif
);
568 dpif_flow_flush(udpif
->dpif
);
569 udpif_start_threads(udpif
, n_handlers
, n_revalidators
);
571 ovsrcu_quiesce_end();
574 /* Removes all flows from all datapaths. */
576 udpif_flush_all_datapaths(void)
580 LIST_FOR_EACH (udpif
, list_node
, &all_udpifs
) {
586 udpif_use_ufid(struct udpif
*udpif
)
590 atomic_read_relaxed(&enable_ufid
, &enable
);
591 return enable
&& ofproto_dpif_get_enable_ufid(udpif
->backer
);
596 udpif_get_n_flows(struct udpif
*udpif
)
598 long long int time
, now
;
599 unsigned long flow_count
;
602 atomic_read_relaxed(&udpif
->n_flows_timestamp
, &time
);
603 if (time
< now
- 100 && !ovs_mutex_trylock(&udpif
->n_flows_mutex
)) {
604 struct dpif_dp_stats stats
;
606 atomic_store_relaxed(&udpif
->n_flows_timestamp
, now
);
607 dpif_get_dp_stats(udpif
->dpif
, &stats
);
608 flow_count
= stats
.n_flows
;
609 atomic_store_relaxed(&udpif
->n_flows
, flow_count
);
610 ovs_mutex_unlock(&udpif
->n_flows_mutex
);
612 atomic_read_relaxed(&udpif
->n_flows
, &flow_count
);
617 /* The upcall handler thread tries to read a batch of UPCALL_MAX_BATCH
618 * upcalls from dpif, processes the batch and installs corresponding flows
621 udpif_upcall_handler(void *arg
)
623 struct handler
*handler
= arg
;
624 struct udpif
*udpif
= handler
->udpif
;
626 while (!latch_is_set(&handler
->udpif
->exit_latch
)) {
627 if (recv_upcalls(handler
)) {
628 poll_immediate_wake();
630 dpif_recv_wait(udpif
->dpif
, handler
->handler_id
);
631 latch_wait(&udpif
->exit_latch
);
640 recv_upcalls(struct handler
*handler
)
642 struct udpif
*udpif
= handler
->udpif
;
643 uint64_t recv_stubs
[UPCALL_MAX_BATCH
][512 / 8];
644 struct ofpbuf recv_bufs
[UPCALL_MAX_BATCH
];
645 struct dpif_upcall dupcalls
[UPCALL_MAX_BATCH
];
646 struct upcall upcalls
[UPCALL_MAX_BATCH
];
647 struct flow flows
[UPCALL_MAX_BATCH
];
651 while (n_upcalls
< UPCALL_MAX_BATCH
) {
652 struct ofpbuf
*recv_buf
= &recv_bufs
[n_upcalls
];
653 struct dpif_upcall
*dupcall
= &dupcalls
[n_upcalls
];
654 struct upcall
*upcall
= &upcalls
[n_upcalls
];
655 struct flow
*flow
= &flows
[n_upcalls
];
658 ofpbuf_use_stub(recv_buf
, recv_stubs
[n_upcalls
],
659 sizeof recv_stubs
[n_upcalls
]);
660 if (dpif_recv(udpif
->dpif
, handler
->handler_id
, dupcall
, recv_buf
)) {
661 ofpbuf_uninit(recv_buf
);
665 if (odp_flow_key_to_flow(dupcall
->key
, dupcall
->key_len
, flow
)
670 error
= upcall_receive(upcall
, udpif
->backer
, &dupcall
->packet
,
671 dupcall
->type
, dupcall
->userdata
, flow
,
672 &dupcall
->ufid
, PMD_ID_NULL
);
674 if (error
== ENODEV
) {
675 /* Received packet on datapath port for which we couldn't
676 * associate an ofproto. This can happen if a port is removed
677 * while traffic is being received. Print a rate-limited
678 * message in case it happens frequently. */
679 dpif_flow_put(udpif
->dpif
, DPIF_FP_CREATE
, dupcall
->key
,
680 dupcall
->key_len
, NULL
, 0, NULL
, 0,
681 &dupcall
->ufid
, PMD_ID_NULL
, NULL
);
682 VLOG_INFO_RL(&rl
, "received packet on unassociated datapath "
683 "port %"PRIu32
, flow
->in_port
.odp_port
);
688 upcall
->key
= dupcall
->key
;
689 upcall
->key_len
= dupcall
->key_len
;
690 upcall
->ufid
= &dupcall
->ufid
;
692 upcall
->out_tun_key
= dupcall
->out_tun_key
;
694 if (vsp_adjust_flow(upcall
->ofproto
, flow
, &dupcall
->packet
)) {
695 upcall
->vsp_adjusted
= true;
698 pkt_metadata_from_flow(&dupcall
->packet
.md
, flow
);
699 flow_extract(&dupcall
->packet
, flow
);
701 error
= process_upcall(udpif
, upcall
, NULL
);
710 upcall_uninit(upcall
);
712 dp_packet_uninit(&dupcall
->packet
);
713 ofpbuf_uninit(recv_buf
);
717 handle_upcalls(handler
->udpif
, upcalls
, n_upcalls
);
718 for (i
= 0; i
< n_upcalls
; i
++) {
719 dp_packet_uninit(&dupcalls
[i
].packet
);
720 ofpbuf_uninit(&recv_bufs
[i
]);
721 upcall_uninit(&upcalls
[i
]);
729 udpif_revalidator(void *arg
)
731 /* Used by all revalidators. */
732 struct revalidator
*revalidator
= arg
;
733 struct udpif
*udpif
= revalidator
->udpif
;
734 bool leader
= revalidator
== &udpif
->revalidators
[0];
736 /* Used only by the leader. */
737 long long int start_time
= 0;
738 uint64_t last_reval_seq
= 0;
741 revalidator
->id
= ovsthread_id_self();
746 recirc_run(); /* Recirculation cleanup. */
748 reval_seq
= seq_read(udpif
->reval_seq
);
749 last_reval_seq
= reval_seq
;
751 n_flows
= udpif_get_n_flows(udpif
);
752 udpif
->max_n_flows
= MAX(n_flows
, udpif
->max_n_flows
);
753 udpif
->avg_n_flows
= (udpif
->avg_n_flows
+ n_flows
) / 2;
755 /* Only the leader checks the exit latch to prevent a race where
756 * some threads think it's true and exit and others think it's
757 * false and block indefinitely on the reval_barrier */
758 udpif
->reval_exit
= latch_is_set(&udpif
->exit_latch
);
760 start_time
= time_msec();
761 if (!udpif
->reval_exit
) {
764 terse_dump
= udpif_use_ufid(udpif
);
765 udpif
->dump
= dpif_flow_dump_create(udpif
->dpif
, terse_dump
);
769 /* Wait for the leader to start the flow dump. */
770 ovs_barrier_block(&udpif
->reval_barrier
);
771 if (udpif
->reval_exit
) {
774 revalidate(revalidator
);
776 /* Wait for all flows to have been dumped before we garbage collect. */
777 ovs_barrier_block(&udpif
->reval_barrier
);
778 revalidator_sweep(revalidator
);
780 /* Wait for all revalidators to finish garbage collection. */
781 ovs_barrier_block(&udpif
->reval_barrier
);
784 unsigned int flow_limit
;
785 long long int duration
;
787 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
789 dpif_flow_dump_destroy(udpif
->dump
);
790 seq_change(udpif
->dump_seq
);
792 duration
= MAX(time_msec() - start_time
, 1);
793 udpif
->dump_duration
= duration
;
794 if (duration
> 2000) {
795 flow_limit
/= duration
/ 1000;
796 } else if (duration
> 1300) {
797 flow_limit
= flow_limit
* 3 / 4;
798 } else if (duration
< 1000 && n_flows
> 2000
799 && flow_limit
< n_flows
* 1000 / duration
) {
802 flow_limit
= MIN(ofproto_flow_limit
, MAX(flow_limit
, 1000));
803 atomic_store_relaxed(&udpif
->flow_limit
, flow_limit
);
805 if (duration
> 2000) {
806 VLOG_INFO("Spent an unreasonably long %lldms dumping flows",
810 poll_timer_wait_until(start_time
+ MIN(ofproto_max_idle
, 500));
811 seq_wait(udpif
->reval_seq
, last_reval_seq
);
812 latch_wait(&udpif
->exit_latch
);
820 static enum upcall_type
821 classify_upcall(enum dpif_upcall_type type
, const struct nlattr
*userdata
)
823 union user_action_cookie cookie
;
826 /* First look at the upcall type. */
834 case DPIF_N_UC_TYPES
:
836 VLOG_WARN_RL(&rl
, "upcall has unexpected type %"PRIu32
, type
);
840 /* "action" upcalls need a closer look. */
842 VLOG_WARN_RL(&rl
, "action upcall missing cookie");
845 userdata_len
= nl_attr_get_size(userdata
);
846 if (userdata_len
< sizeof cookie
.type
847 || userdata_len
> sizeof cookie
) {
848 VLOG_WARN_RL(&rl
, "action upcall cookie has unexpected size %"PRIuSIZE
,
852 memset(&cookie
, 0, sizeof cookie
);
853 memcpy(&cookie
, nl_attr_get(userdata
), userdata_len
);
854 if (userdata_len
== MAX(8, sizeof cookie
.sflow
)
855 && cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
857 } else if (userdata_len
== MAX(8, sizeof cookie
.slow_path
)
858 && cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
860 } else if (userdata_len
== MAX(8, sizeof cookie
.flow_sample
)
861 && cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
862 return FLOW_SAMPLE_UPCALL
;
863 } else if (userdata_len
== MAX(8, sizeof cookie
.ipfix
)
864 && cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
867 VLOG_WARN_RL(&rl
, "invalid user cookie of type %"PRIu16
868 " and size %"PRIuSIZE
, cookie
.type
, userdata_len
);
873 /* Calculates slow path actions for 'xout'. 'buf' must statically be
874 * initialized with at least 128 bytes of space. */
876 compose_slow_path(struct udpif
*udpif
, struct xlate_out
*xout
,
877 const struct flow
*flow
, odp_port_t odp_in_port
,
880 union user_action_cookie cookie
;
884 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
885 cookie
.slow_path
.unused
= 0;
886 cookie
.slow_path
.reason
= xout
->slow
;
888 port
= xout
->slow
& (SLOW_CFM
| SLOW_BFD
| SLOW_LACP
| SLOW_STP
)
891 pid
= dpif_port_get_pid(udpif
->dpif
, port
, flow_hash_5tuple(flow
, 0));
892 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
.slow_path
, ODPP_NONE
,
896 /* If there is no error, the upcall must be destroyed with upcall_uninit()
897 * before quiescing, as the referred objects are guaranteed to exist only
898 * until the calling thread quiesces. Otherwise, do not call upcall_uninit()
899 * since the 'upcall->put_actions' remains uninitialized. */
901 upcall_receive(struct upcall
*upcall
, const struct dpif_backer
*backer
,
902 const struct dp_packet
*packet
, enum dpif_upcall_type type
,
903 const struct nlattr
*userdata
, const struct flow
*flow
,
904 const ovs_u128
*ufid
, const int pmd_id
)
908 error
= xlate_lookup(backer
, flow
, &upcall
->ofproto
, &upcall
->ipfix
,
909 &upcall
->sflow
, NULL
, &upcall
->in_port
);
914 upcall
->recirc
= NULL
;
915 upcall
->have_recirc_ref
= false;
917 upcall
->packet
= packet
;
919 upcall
->pmd_id
= pmd_id
;
921 upcall
->userdata
= userdata
;
922 ofpbuf_init(&upcall
->put_actions
, 0);
924 upcall
->xout_initialized
= false;
925 upcall
->vsp_adjusted
= false;
926 upcall
->ukey_persists
= false;
932 upcall
->out_tun_key
= NULL
;
938 upcall_xlate(struct udpif
*udpif
, struct upcall
*upcall
,
939 struct ofpbuf
*odp_actions
)
941 struct dpif_flow_stats stats
;
945 stats
.n_bytes
= dp_packet_size(upcall
->packet
);
946 stats
.used
= time_msec();
947 stats
.tcp_flags
= ntohs(upcall
->flow
->tcp_flags
);
949 xlate_in_init(&xin
, upcall
->ofproto
, upcall
->flow
, upcall
->in_port
, NULL
,
950 stats
.tcp_flags
, upcall
->packet
);
951 xin
.odp_actions
= odp_actions
;
953 if (upcall
->type
== DPIF_UC_MISS
) {
954 xin
.resubmit_stats
= &stats
;
957 /* We may install a datapath flow only if we get a reference to the
958 * recirculation context (otherwise we could have recirculation
959 * upcalls using recirculation ID for which no context can be
960 * found). We may still execute the flow's actions even if we
961 * don't install the flow. */
962 upcall
->recirc
= xin
.recirc
;
963 upcall
->have_recirc_ref
= recirc_id_node_try_ref_rcu(xin
.recirc
);
966 /* For non-miss upcalls, we are either executing actions (one of which
967 * is an userspace action) for an upcall, in which case the stats have
968 * already been taken care of, or there's a flow in the datapath which
969 * this packet was accounted to. Presumably the revalidators will deal
970 * with pushing its stats eventually. */
973 upcall
->dump_seq
= seq_read(udpif
->dump_seq
);
974 upcall
->reval_seq
= seq_read(udpif
->reval_seq
);
975 xlate_actions(&xin
, &upcall
->xout
);
976 upcall
->xout_initialized
= true;
978 /* Special case for fail-open mode.
980 * If we are in fail-open mode, but we are connected to a controller too,
981 * then we should send the packet up to the controller in the hope that it
982 * will try to set up a flow and thereby allow us to exit fail-open.
984 * See the top-level comment in fail-open.c for more information.
986 * Copy packets before they are modified by execution. */
987 if (upcall
->xout
.fail_open
) {
988 const struct dp_packet
*packet
= upcall
->packet
;
989 struct ofproto_packet_in
*pin
;
991 pin
= xmalloc(sizeof *pin
);
992 pin
->up
.packet
= xmemdup(dp_packet_data(packet
), dp_packet_size(packet
));
993 pin
->up
.packet_len
= dp_packet_size(packet
);
994 pin
->up
.reason
= OFPR_NO_MATCH
;
995 pin
->up
.table_id
= 0;
996 pin
->up
.cookie
= OVS_BE64_MAX
;
997 flow_get_metadata(upcall
->flow
, &pin
->up
.fmd
);
998 pin
->send_len
= 0; /* Not used for flow table misses. */
999 pin
->miss_type
= OFPROTO_PACKET_IN_NO_MISS
;
1000 ofproto_dpif_send_packet_in(upcall
->ofproto
, pin
);
1003 if (!upcall
->xout
.slow
) {
1004 ofpbuf_use_const(&upcall
->put_actions
,
1005 upcall
->xout
.odp_actions
->data
,
1006 upcall
->xout
.odp_actions
->size
);
1008 ofpbuf_init(&upcall
->put_actions
, 0);
1009 compose_slow_path(udpif
, &upcall
->xout
, upcall
->flow
,
1010 upcall
->flow
->in_port
.odp_port
,
1011 &upcall
->put_actions
);
1014 /* This function is also called for slow-pathed flows. As we are only
1015 * going to create new datapath flows for actual datapath misses, there is
1016 * no point in creating a ukey otherwise. */
1017 if (upcall
->type
== DPIF_UC_MISS
) {
1018 upcall
->ukey
= ukey_create_from_upcall(upcall
);
1023 upcall_uninit(struct upcall
*upcall
)
1026 if (upcall
->xout_initialized
) {
1027 xlate_out_uninit(&upcall
->xout
);
1029 ofpbuf_uninit(&upcall
->put_actions
);
1031 if (!upcall
->ukey_persists
) {
1032 ukey_delete__(upcall
->ukey
);
1034 } else if (upcall
->have_recirc_ref
) {
1035 /* The reference was transferred to the ukey if one was created. */
1036 recirc_id_node_unref(upcall
->recirc
);
1042 upcall_cb(const struct dp_packet
*packet
, const struct flow
*flow
, ovs_u128
*ufid
,
1043 int pmd_id
, enum dpif_upcall_type type
,
1044 const struct nlattr
*userdata
, struct ofpbuf
*actions
,
1045 struct flow_wildcards
*wc
, struct ofpbuf
*put_actions
, void *aux
)
1047 struct udpif
*udpif
= aux
;
1048 unsigned int flow_limit
;
1049 struct upcall upcall
;
1053 atomic_read_relaxed(&enable_megaflows
, &megaflow
);
1054 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
1056 error
= upcall_receive(&upcall
, udpif
->backer
, packet
, type
, userdata
,
1057 flow
, ufid
, pmd_id
);
1062 error
= process_upcall(udpif
, &upcall
, actions
);
1067 if (upcall
.xout
.slow
&& put_actions
) {
1068 ofpbuf_put(put_actions
, upcall
.put_actions
.data
,
1069 upcall
.put_actions
.size
);
1072 if (OVS_LIKELY(wc
)) {
1074 /* XXX: This could be avoided with sufficient API changes. */
1075 *wc
= upcall
.xout
.wc
;
1077 flow_wildcards_init_for_packet(wc
, flow
);
1081 if (udpif_get_n_flows(udpif
) >= flow_limit
) {
1086 /* Prevent miss flow installation if the key has recirculation ID but we
1087 * were not able to get a reference on it. */
1088 if (type
== DPIF_UC_MISS
&& upcall
.recirc
&& !upcall
.have_recirc_ref
) {
1093 if (upcall
.ukey
&& !ukey_install(udpif
, upcall
.ukey
)) {
1098 upcall
.ukey_persists
= true;
1100 upcall_uninit(&upcall
);
1105 process_upcall(struct udpif
*udpif
, struct upcall
*upcall
,
1106 struct ofpbuf
*odp_actions
)
1108 const struct nlattr
*userdata
= upcall
->userdata
;
1109 const struct dp_packet
*packet
= upcall
->packet
;
1110 const struct flow
*flow
= upcall
->flow
;
1112 switch (classify_upcall(upcall
->type
, userdata
)) {
1114 upcall_xlate(udpif
, upcall
, odp_actions
);
1118 if (upcall
->sflow
) {
1119 union user_action_cookie cookie
;
1121 memset(&cookie
, 0, sizeof cookie
);
1122 memcpy(&cookie
, nl_attr_get(userdata
), sizeof cookie
.sflow
);
1123 dpif_sflow_received(upcall
->sflow
, packet
, flow
,
1124 flow
->in_port
.odp_port
, &cookie
);
1129 if (upcall
->ipfix
) {
1130 union user_action_cookie cookie
;
1131 struct flow_tnl output_tunnel_key
;
1133 memset(&cookie
, 0, sizeof cookie
);
1134 memcpy(&cookie
, nl_attr_get(userdata
), sizeof cookie
.ipfix
);
1136 if (upcall
->out_tun_key
) {
1137 memset(&output_tunnel_key
, 0, sizeof output_tunnel_key
);
1138 odp_tun_key_from_attr(upcall
->out_tun_key
,
1139 &output_tunnel_key
);
1141 dpif_ipfix_bridge_sample(upcall
->ipfix
, packet
, flow
,
1142 flow
->in_port
.odp_port
,
1143 cookie
.ipfix
.output_odp_port
,
1144 upcall
->out_tun_key
?
1145 &output_tunnel_key
: NULL
);
1149 case FLOW_SAMPLE_UPCALL
:
1150 if (upcall
->ipfix
) {
1151 union user_action_cookie cookie
;
1153 memset(&cookie
, 0, sizeof cookie
);
1154 memcpy(&cookie
, nl_attr_get(userdata
), sizeof cookie
.flow_sample
);
1156 /* The flow reflects exactly the contents of the packet.
1157 * Sample the packet using it. */
1158 dpif_ipfix_flow_sample(upcall
->ipfix
, packet
, flow
,
1159 cookie
.flow_sample
.collector_set_id
,
1160 cookie
.flow_sample
.probability
,
1161 cookie
.flow_sample
.obs_domain_id
,
1162 cookie
.flow_sample
.obs_point_id
);
1174 handle_upcalls(struct udpif
*udpif
, struct upcall
*upcalls
,
1177 struct dpif_op
*opsp
[UPCALL_MAX_BATCH
* 2];
1178 struct ukey_op ops
[UPCALL_MAX_BATCH
* 2];
1179 unsigned int flow_limit
;
1180 size_t n_ops
, n_opsp
, i
;
1184 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
1185 atomic_read_relaxed(&enable_megaflows
, &megaflow
);
1187 may_put
= udpif_get_n_flows(udpif
) < flow_limit
;
1189 /* Handle the packets individually in order of arrival.
1191 * - For SLOW_CFM, SLOW_LACP, SLOW_STP, and SLOW_BFD, translation is what
1192 * processes received packets for these protocols.
1194 * - For SLOW_CONTROLLER, translation sends the packet to the OpenFlow
1197 * The loop fills 'ops' with an array of operations to execute in the
1200 for (i
= 0; i
< n_upcalls
; i
++) {
1201 struct upcall
*upcall
= &upcalls
[i
];
1202 const struct dp_packet
*packet
= upcall
->packet
;
1205 if (upcall
->vsp_adjusted
) {
1206 /* This packet was received on a VLAN splinter port. We added a
1207 * VLAN to the packet to make the packet resemble the flow, but the
1208 * actions were composed assuming that the packet contained no
1209 * VLAN. So, we must remove the VLAN header from the packet before
1210 * trying to execute the actions. */
1211 if (upcall
->xout
.odp_actions
->size
) {
1212 eth_pop_vlan(CONST_CAST(struct dp_packet
*, upcall
->packet
));
1215 /* Remove the flow vlan tags inserted by vlan splinter logic
1216 * to ensure megaflow masks generated match the data path flow. */
1217 CONST_CAST(struct flow
*, upcall
->flow
)->vlan_tci
= 0;
1220 /* Do not install a flow into the datapath if:
1222 * - The datapath already has too many flows.
1224 * - We received this packet via some flow installed in the kernel
1227 * - Upcall was a recirculation but we do not have a reference to
1228 * to the recirculation ID. */
1229 if (may_put
&& upcall
->type
== DPIF_UC_MISS
&&
1230 (!upcall
->recirc
|| upcall
->have_recirc_ref
)) {
1231 struct udpif_key
*ukey
= upcall
->ukey
;
1233 upcall
->ukey_persists
= true;
1237 op
->dop
.type
= DPIF_OP_FLOW_PUT
;
1238 op
->dop
.u
.flow_put
.flags
= DPIF_FP_CREATE
;
1239 op
->dop
.u
.flow_put
.key
= ukey
->key
;
1240 op
->dop
.u
.flow_put
.key_len
= ukey
->key_len
;
1241 op
->dop
.u
.flow_put
.mask
= ukey
->mask
;
1242 op
->dop
.u
.flow_put
.mask_len
= ukey
->mask_len
;
1243 op
->dop
.u
.flow_put
.ufid
= upcall
->ufid
;
1244 op
->dop
.u
.flow_put
.stats
= NULL
;
1245 op
->dop
.u
.flow_put
.actions
= ukey
->actions
->data
;
1246 op
->dop
.u
.flow_put
.actions_len
= ukey
->actions
->size
;
1249 if (upcall
->xout
.odp_actions
->size
) {
1252 op
->dop
.type
= DPIF_OP_EXECUTE
;
1253 op
->dop
.u
.execute
.packet
= CONST_CAST(struct dp_packet
*, packet
);
1254 odp_key_to_pkt_metadata(upcall
->key
, upcall
->key_len
,
1255 &op
->dop
.u
.execute
.packet
->md
);
1256 op
->dop
.u
.execute
.actions
= upcall
->xout
.odp_actions
->data
;
1257 op
->dop
.u
.execute
.actions_len
= upcall
->xout
.odp_actions
->size
;
1258 op
->dop
.u
.execute
.needs_help
= (upcall
->xout
.slow
& SLOW_ACTION
) != 0;
1259 op
->dop
.u
.execute
.probe
= false;
1265 * We install ukeys before installing the flows, locking them for exclusive
1266 * access by this thread for the period of installation. This ensures that
1267 * other threads won't attempt to delete the flows as we are creating them.
1270 for (i
= 0; i
< n_ops
; i
++) {
1271 struct udpif_key
*ukey
= ops
[i
].ukey
;
1274 /* If we can't install the ukey, don't install the flow. */
1275 if (!ukey_install_start(udpif
, ukey
)) {
1276 ukey_delete__(ukey
);
1281 opsp
[n_opsp
++] = &ops
[i
].dop
;
1283 dpif_operate(udpif
->dpif
, opsp
, n_opsp
);
1284 for (i
= 0; i
< n_ops
; i
++) {
1286 ukey_install_finish(ops
[i
].ukey
, ops
[i
].dop
.error
);
1292 get_ufid_hash(const ovs_u128
*ufid
)
1294 return ufid
->u32
[0];
1297 static struct udpif_key
*
1298 ukey_lookup(struct udpif
*udpif
, const ovs_u128
*ufid
)
1300 struct udpif_key
*ukey
;
1301 int idx
= get_ufid_hash(ufid
) % N_UMAPS
;
1302 struct cmap
*cmap
= &udpif
->ukeys
[idx
].cmap
;
1304 CMAP_FOR_EACH_WITH_HASH (ukey
, cmap_node
, get_ufid_hash(ufid
), cmap
) {
1305 if (ovs_u128_equal(&ukey
->ufid
, ufid
)) {
1312 static struct udpif_key
*
1313 ukey_create__(const struct nlattr
*key
, size_t key_len
,
1314 const struct nlattr
*mask
, size_t mask_len
,
1315 bool ufid_present
, const ovs_u128
*ufid
,
1316 const int pmd_id
, const struct ofpbuf
*actions
,
1317 uint64_t dump_seq
, uint64_t reval_seq
, long long int used
,
1318 const struct recirc_id_node
*key_recirc
, struct xlate_out
*xout
)
1319 OVS_NO_THREAD_SAFETY_ANALYSIS
1321 unsigned n_recircs
= (key_recirc
? 1 : 0) + (xout
? xout
->n_recircs
: 0);
1322 struct udpif_key
*ukey
= xmalloc(sizeof *ukey
+
1323 n_recircs
* sizeof *ukey
->recircs
);
1325 memcpy(&ukey
->keybuf
, key
, key_len
);
1326 ukey
->key
= &ukey
->keybuf
.nla
;
1327 ukey
->key_len
= key_len
;
1328 memcpy(&ukey
->maskbuf
, mask
, mask_len
);
1329 ukey
->mask
= &ukey
->maskbuf
.nla
;
1330 ukey
->mask_len
= mask_len
;
1331 ukey
->ufid_present
= ufid_present
;
1333 ukey
->pmd_id
= pmd_id
;
1334 ukey
->hash
= get_ufid_hash(&ukey
->ufid
);
1335 ukey
->actions
= ofpbuf_clone(actions
);
1337 ovs_mutex_init(&ukey
->mutex
);
1338 ukey
->dump_seq
= dump_seq
;
1339 ukey
->reval_seq
= reval_seq
;
1340 ukey
->flow_exists
= false;
1341 ukey
->created
= time_msec();
1342 memset(&ukey
->stats
, 0, sizeof ukey
->stats
);
1343 ukey
->stats
.used
= used
;
1344 ukey
->xcache
= NULL
;
1346 ukey
->n_recircs
= n_recircs
;
1348 ukey
->recircs
[0] = key_recirc
->id
;
1350 if (xout
&& xout
->n_recircs
) {
1351 const uint32_t *act_recircs
= xlate_out_get_recircs(xout
);
1353 memcpy(ukey
->recircs
+ (key_recirc
? 1 : 0), act_recircs
,
1354 xout
->n_recircs
* sizeof *ukey
->recircs
);
1355 xlate_out_take_recircs(xout
);
1360 static struct udpif_key
*
1361 ukey_create_from_upcall(struct upcall
*upcall
)
1363 struct odputil_keybuf keystub
, maskstub
;
1364 struct ofpbuf keybuf
, maskbuf
;
1365 bool recirc
, megaflow
;
1367 if (upcall
->key_len
) {
1368 ofpbuf_use_const(&keybuf
, upcall
->key
, upcall
->key_len
);
1370 /* dpif-netdev doesn't provide a netlink-formatted flow key in the
1371 * upcall, so convert the upcall's flow here. */
1372 ofpbuf_use_stack(&keybuf
, &keystub
, sizeof keystub
);
1373 odp_flow_key_from_flow(&keybuf
, upcall
->flow
, &upcall
->xout
.wc
.masks
,
1374 upcall
->flow
->in_port
.odp_port
, true);
1377 atomic_read_relaxed(&enable_megaflows
, &megaflow
);
1378 recirc
= ofproto_dpif_get_enable_recirc(upcall
->ofproto
);
1379 ofpbuf_use_stack(&maskbuf
, &maskstub
, sizeof maskstub
);
1383 max_mpls
= ofproto_dpif_get_max_mpls_depth(upcall
->ofproto
);
1384 odp_flow_key_from_mask(&maskbuf
, &upcall
->xout
.wc
.masks
, upcall
->flow
,
1385 UINT32_MAX
, max_mpls
, recirc
);
1388 return ukey_create__(keybuf
.data
, keybuf
.size
, maskbuf
.data
, maskbuf
.size
,
1389 true, upcall
->ufid
, upcall
->pmd_id
,
1390 &upcall
->put_actions
, upcall
->dump_seq
,
1391 upcall
->reval_seq
, 0,
1392 upcall
->have_recirc_ref
? upcall
->recirc
: NULL
,
1397 ukey_create_from_dpif_flow(const struct udpif
*udpif
,
1398 const struct dpif_flow
*flow
,
1399 struct udpif_key
**ukey
)
1401 struct dpif_flow full_flow
;
1402 struct ofpbuf actions
;
1403 uint64_t dump_seq
, reval_seq
;
1404 uint64_t stub
[DPIF_FLOW_BUFSIZE
/ 8];
1405 const struct nlattr
*a
;
1408 if (!flow
->key_len
|| !flow
->actions_len
) {
1412 /* If the key or actions were not provided by the datapath, fetch the
1414 ofpbuf_use_stack(&buf
, &stub
, sizeof stub
);
1415 err
= dpif_flow_get(udpif
->dpif
, NULL
, 0, &flow
->ufid
,
1416 flow
->pmd_id
, &buf
, &full_flow
);
1423 /* Check the flow actions for recirculation action. As recirculation
1424 * relies on OVS userspace internal state, we need to delete all old
1425 * datapath flows with recirculation upon OVS restart. */
1426 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, flow
->actions
, flow
->actions_len
) {
1427 if (nl_attr_type(a
) == OVS_ACTION_ATTR_RECIRC
) {
1432 dump_seq
= seq_read(udpif
->dump_seq
);
1433 reval_seq
= seq_read(udpif
->reval_seq
);
1434 ofpbuf_use_const(&actions
, &flow
->actions
, flow
->actions_len
);
1435 *ukey
= ukey_create__(flow
->key
, flow
->key_len
,
1436 flow
->mask
, flow
->mask_len
, flow
->ufid_present
,
1437 &flow
->ufid
, flow
->pmd_id
, &actions
, dump_seq
,
1438 reval_seq
, flow
->stats
.used
, NULL
, NULL
);
1443 /* Attempts to insert a ukey into the shared ukey maps.
1445 * On success, returns true, installs the ukey and returns it in a locked
1446 * state. Otherwise, returns false. */
1448 ukey_install_start(struct udpif
*udpif
, struct udpif_key
*new_ukey
)
1449 OVS_TRY_LOCK(true, new_ukey
->mutex
)
1452 struct udpif_key
*old_ukey
;
1454 bool locked
= false;
1456 idx
= new_ukey
->hash
% N_UMAPS
;
1457 umap
= &udpif
->ukeys
[idx
];
1458 ovs_mutex_lock(&umap
->mutex
);
1459 old_ukey
= ukey_lookup(udpif
, &new_ukey
->ufid
);
1461 /* Uncommon case: A ukey is already installed with the same UFID. */
1462 if (old_ukey
->key_len
== new_ukey
->key_len
1463 && !memcmp(old_ukey
->key
, new_ukey
->key
, new_ukey
->key_len
)) {
1464 COVERAGE_INC(handler_duplicate_upcall
);
1466 struct ds ds
= DS_EMPTY_INITIALIZER
;
1468 odp_format_ufid(&old_ukey
->ufid
, &ds
);
1469 ds_put_cstr(&ds
, " ");
1470 odp_flow_key_format(old_ukey
->key
, old_ukey
->key_len
, &ds
);
1471 ds_put_cstr(&ds
, "\n");
1472 odp_format_ufid(&new_ukey
->ufid
, &ds
);
1473 ds_put_cstr(&ds
, " ");
1474 odp_flow_key_format(new_ukey
->key
, new_ukey
->key_len
, &ds
);
1476 VLOG_WARN_RL(&rl
, "Conflicting ukey for flows:\n%s", ds_cstr(&ds
));
1480 ovs_mutex_lock(&new_ukey
->mutex
);
1481 cmap_insert(&umap
->cmap
, &new_ukey
->cmap_node
, new_ukey
->hash
);
1484 ovs_mutex_unlock(&umap
->mutex
);
1490 ukey_install_finish__(struct udpif_key
*ukey
) OVS_REQUIRES(ukey
->mutex
)
1492 ukey
->flow_exists
= true;
1496 ukey_install_finish(struct udpif_key
*ukey
, int error
)
1497 OVS_RELEASES(ukey
->mutex
)
1500 ukey_install_finish__(ukey
);
1502 ovs_mutex_unlock(&ukey
->mutex
);
1508 ukey_install(struct udpif
*udpif
, struct udpif_key
*ukey
)
1510 /* The usual way to keep 'ukey->flow_exists' in sync with the datapath is
1511 * to call ukey_install_start(), install the corresponding datapath flow,
1512 * then call ukey_install_finish(). The netdev interface using upcall_cb()
1513 * doesn't provide a function to separately finish the flow installation,
1514 * so we perform the operations together here.
1516 * This is fine currently, as revalidator threads will only delete this
1517 * ukey during revalidator_sweep() and only if the dump_seq is mismatched.
1518 * It is unlikely for a revalidator thread to advance dump_seq and reach
1519 * the next GC phase between ukey creation and flow installation. */
1520 return ukey_install_start(udpif
, ukey
) && ukey_install_finish(ukey
, 0);
1523 /* Searches for a ukey in 'udpif->ukeys' that matches 'flow' and attempts to
1524 * lock the ukey. If the ukey does not exist, create it.
1526 * Returns 0 on success, setting *result to the matching ukey and returning it
1527 * in a locked state. Otherwise, returns an errno and clears *result. EBUSY
1528 * indicates that another thread is handling this flow. Other errors indicate
1529 * an unexpected condition creating a new ukey.
1531 * *error is an output parameter provided to appease the threadsafety analyser,
1532 * and its value matches the return value. */
1534 ukey_acquire(struct udpif
*udpif
, const struct dpif_flow
*flow
,
1535 struct udpif_key
**result
, int *error
)
1536 OVS_TRY_LOCK(0, (*result
)->mutex
)
1538 struct udpif_key
*ukey
;
1541 ukey
= ukey_lookup(udpif
, &flow
->ufid
);
1543 retval
= ovs_mutex_trylock(&ukey
->mutex
);
1545 /* Usually we try to avoid installing flows from revalidator threads,
1546 * because locking on a umap may cause handler threads to block.
1547 * However there are certain cases, like when ovs-vswitchd is
1548 * restarted, where it is desirable to handle flows that exist in the
1549 * datapath gracefully (ie, don't just clear the datapath). */
1552 retval
= ukey_create_from_dpif_flow(udpif
, flow
, &ukey
);
1556 install
= ukey_install_start(udpif
, ukey
);
1558 ukey_install_finish__(ukey
);
1561 ukey_delete__(ukey
);
1577 ukey_delete__(struct udpif_key
*ukey
)
1578 OVS_NO_THREAD_SAFETY_ANALYSIS
1581 for (int i
= 0; i
< ukey
->n_recircs
; i
++) {
1582 recirc_free_id(ukey
->recircs
[i
]);
1584 xlate_cache_delete(ukey
->xcache
);
1585 ofpbuf_delete(ukey
->actions
);
1586 ovs_mutex_destroy(&ukey
->mutex
);
1592 ukey_delete(struct umap
*umap
, struct udpif_key
*ukey
)
1593 OVS_REQUIRES(umap
->mutex
)
1595 cmap_remove(&umap
->cmap
, &ukey
->cmap_node
, ukey
->hash
);
1596 ovsrcu_postpone(ukey_delete__
, ukey
);
1600 should_revalidate(const struct udpif
*udpif
, uint64_t packets
,
1603 long long int metric
, now
, duration
;
1605 if (udpif
->dump_duration
< 200) {
1606 /* We are likely to handle full revalidation for the flows. */
1610 /* Calculate the mean time between seeing these packets. If this
1611 * exceeds the threshold, then delete the flow rather than performing
1612 * costly revalidation for flows that aren't being hit frequently.
1614 * This is targeted at situations where the dump_duration is high (~1s),
1615 * and revalidation is triggered by a call to udpif_revalidate(). In
1616 * these situations, revalidation of all flows causes fluctuations in the
1617 * flow_limit due to the interaction with the dump_duration and max_idle.
1618 * This tends to result in deletion of low-throughput flows anyway, so
1619 * skip the revalidation and just delete those flows. */
1620 packets
= MAX(packets
, 1);
1621 now
= MAX(used
, time_msec());
1622 duration
= now
- used
;
1623 metric
= duration
/ packets
;
1626 /* The flow is receiving more than ~5pps, so keep it. */
1633 revalidate_ukey(struct udpif
*udpif
, struct udpif_key
*ukey
,
1634 const struct dpif_flow_stats
*stats
, uint64_t reval_seq
)
1635 OVS_REQUIRES(ukey
->mutex
)
1637 uint64_t slow_path_buf
[128 / 8];
1638 struct xlate_out xout
, *xoutp
;
1639 struct netflow
*netflow
;
1640 struct ofproto_dpif
*ofproto
;
1641 struct dpif_flow_stats push
;
1642 struct ofpbuf xout_actions
;
1643 struct flow flow
, dp_mask
;
1644 uint64_t *dp64
, *xout64
;
1645 ofp_port_t ofp_in_port
;
1646 struct xlate_in xin
;
1647 long long int last_used
;
1651 bool need_revalidate
;
1657 need_revalidate
= (ukey
->reval_seq
!= reval_seq
);
1658 last_used
= ukey
->stats
.used
;
1659 push
.used
= stats
->used
;
1660 push
.tcp_flags
= stats
->tcp_flags
;
1661 push
.n_packets
= (stats
->n_packets
> ukey
->stats
.n_packets
1662 ? stats
->n_packets
- ukey
->stats
.n_packets
1664 push
.n_bytes
= (stats
->n_bytes
> ukey
->stats
.n_bytes
1665 ? stats
->n_bytes
- ukey
->stats
.n_bytes
1668 if (need_revalidate
&& last_used
1669 && !should_revalidate(udpif
, push
.n_packets
, last_used
)) {
1674 /* We will push the stats, so update the ukey stats cache. */
1675 ukey
->stats
= *stats
;
1676 if (!push
.n_packets
&& !need_revalidate
) {
1681 if (ukey
->xcache
&& !need_revalidate
) {
1682 xlate_push_stats(ukey
->xcache
, &push
);
1687 if (odp_flow_key_to_flow(ukey
->key
, ukey
->key_len
, &flow
)
1692 error
= xlate_lookup(udpif
->backer
, &flow
, &ofproto
, NULL
, NULL
, &netflow
,
1698 if (need_revalidate
) {
1699 xlate_cache_clear(ukey
->xcache
);
1701 if (!ukey
->xcache
) {
1702 ukey
->xcache
= xlate_cache_new();
1705 xlate_in_init(&xin
, ofproto
, &flow
, ofp_in_port
, NULL
, push
.tcp_flags
,
1707 if (push
.n_packets
) {
1708 xin
.resubmit_stats
= &push
;
1709 xin
.may_learn
= true;
1711 xin
.xcache
= ukey
->xcache
;
1712 xin
.skip_wildcards
= !need_revalidate
;
1713 xlate_actions(&xin
, &xout
);
1716 if (!need_revalidate
) {
1722 ofpbuf_use_const(&xout_actions
, xout
.odp_actions
->data
,
1723 xout
.odp_actions
->size
);
1725 ofpbuf_use_stack(&xout_actions
, slow_path_buf
, sizeof slow_path_buf
);
1726 compose_slow_path(udpif
, &xout
, &flow
, flow
.in_port
.odp_port
,
1730 if (!ofpbuf_equal(&xout_actions
, ukey
->actions
)) {
1734 if (odp_flow_key_to_mask(ukey
->mask
, ukey
->mask_len
, &dp_mask
, &flow
)
1739 /* Since the kernel is free to ignore wildcarded bits in the mask, we can't
1740 * directly check that the masks are the same. Instead we check that the
1741 * mask in the kernel is more specific i.e. less wildcarded, than what
1742 * we've calculated here. This guarantees we don't catch any packets we
1743 * shouldn't with the megaflow. */
1744 dp64
= (uint64_t *) &dp_mask
;
1745 xout64
= (uint64_t *) &xout
.wc
.masks
;
1746 for (i
= 0; i
< FLOW_U64S
; i
++) {
1747 if ((dp64
[i
] | xout64
[i
]) != dp64
[i
]) {
1756 ukey
->reval_seq
= reval_seq
;
1758 if (netflow
&& !ok
) {
1759 netflow_flow_clear(netflow
, &flow
);
1761 xlate_out_uninit(xoutp
);
1766 delete_op_init__(struct udpif
*udpif
, struct ukey_op
*op
,
1767 const struct dpif_flow
*flow
)
1770 op
->dop
.type
= DPIF_OP_FLOW_DEL
;
1771 op
->dop
.u
.flow_del
.key
= flow
->key
;
1772 op
->dop
.u
.flow_del
.key_len
= flow
->key_len
;
1773 op
->dop
.u
.flow_del
.ufid
= flow
->ufid_present
? &flow
->ufid
: NULL
;
1774 op
->dop
.u
.flow_del
.pmd_id
= flow
->pmd_id
;
1775 op
->dop
.u
.flow_del
.stats
= &op
->stats
;
1776 op
->dop
.u
.flow_del
.terse
= udpif_use_ufid(udpif
);
1780 delete_op_init(struct udpif
*udpif
, struct ukey_op
*op
, struct udpif_key
*ukey
)
1783 op
->dop
.type
= DPIF_OP_FLOW_DEL
;
1784 op
->dop
.u
.flow_del
.key
= ukey
->key
;
1785 op
->dop
.u
.flow_del
.key_len
= ukey
->key_len
;
1786 op
->dop
.u
.flow_del
.ufid
= ukey
->ufid_present
? &ukey
->ufid
: NULL
;
1787 op
->dop
.u
.flow_del
.pmd_id
= ukey
->pmd_id
;
1788 op
->dop
.u
.flow_del
.stats
= &op
->stats
;
1789 op
->dop
.u
.flow_del
.terse
= udpif_use_ufid(udpif
);
1793 push_ukey_ops__(struct udpif
*udpif
, struct ukey_op
*ops
, size_t n_ops
)
1795 struct dpif_op
*opsp
[REVALIDATE_MAX_BATCH
];
1798 ovs_assert(n_ops
<= REVALIDATE_MAX_BATCH
);
1799 for (i
= 0; i
< n_ops
; i
++) {
1800 opsp
[i
] = &ops
[i
].dop
;
1802 dpif_operate(udpif
->dpif
, opsp
, n_ops
);
1804 for (i
= 0; i
< n_ops
; i
++) {
1805 struct ukey_op
*op
= &ops
[i
];
1806 struct dpif_flow_stats
*push
, *stats
, push_buf
;
1808 stats
= op
->dop
.u
.flow_del
.stats
;
1812 ovs_mutex_lock(&op
->ukey
->mutex
);
1813 push
->used
= MAX(stats
->used
, op
->ukey
->stats
.used
);
1814 push
->tcp_flags
= stats
->tcp_flags
| op
->ukey
->stats
.tcp_flags
;
1815 push
->n_packets
= stats
->n_packets
- op
->ukey
->stats
.n_packets
;
1816 push
->n_bytes
= stats
->n_bytes
- op
->ukey
->stats
.n_bytes
;
1817 ovs_mutex_unlock(&op
->ukey
->mutex
);
1822 if (push
->n_packets
|| netflow_exists()) {
1823 const struct nlattr
*key
= op
->dop
.u
.flow_del
.key
;
1824 size_t key_len
= op
->dop
.u
.flow_del
.key_len
;
1825 struct ofproto_dpif
*ofproto
;
1826 struct netflow
*netflow
;
1827 ofp_port_t ofp_in_port
;
1832 ovs_mutex_lock(&op
->ukey
->mutex
);
1833 if (op
->ukey
->xcache
) {
1834 xlate_push_stats(op
->ukey
->xcache
, push
);
1835 ovs_mutex_unlock(&op
->ukey
->mutex
);
1838 ovs_mutex_unlock(&op
->ukey
->mutex
);
1839 key
= op
->ukey
->key
;
1840 key_len
= op
->ukey
->key_len
;
1843 if (odp_flow_key_to_flow(key
, key_len
, &flow
)
1848 error
= xlate_lookup(udpif
->backer
, &flow
, &ofproto
, NULL
, NULL
,
1849 &netflow
, &ofp_in_port
);
1851 struct xlate_in xin
;
1853 xlate_in_init(&xin
, ofproto
, &flow
, ofp_in_port
, NULL
,
1854 push
->tcp_flags
, NULL
);
1855 xin
.resubmit_stats
= push
->n_packets
? push
: NULL
;
1856 xin
.may_learn
= push
->n_packets
> 0;
1857 xin
.skip_wildcards
= true;
1858 xlate_actions_for_side_effects(&xin
);
1861 netflow_flow_clear(netflow
, &flow
);
1869 push_ukey_ops(struct udpif
*udpif
, struct umap
*umap
,
1870 struct ukey_op
*ops
, size_t n_ops
)
1874 push_ukey_ops__(udpif
, ops
, n_ops
);
1875 ovs_mutex_lock(&umap
->mutex
);
1876 for (i
= 0; i
< n_ops
; i
++) {
1877 ukey_delete(umap
, ops
[i
].ukey
);
1879 ovs_mutex_unlock(&umap
->mutex
);
1883 log_unexpected_flow(const struct dpif_flow
*flow
, int error
)
1885 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 60);
1886 struct ds ds
= DS_EMPTY_INITIALIZER
;
1888 ds_put_format(&ds
, "Failed to acquire udpif_key corresponding to "
1889 "unexpected flow (%s): ", ovs_strerror(error
));
1890 odp_format_ufid(&flow
->ufid
, &ds
);
1891 VLOG_WARN_RL(&rl
, "%s", ds_cstr(&ds
));
1895 revalidate(struct revalidator
*revalidator
)
1897 struct udpif
*udpif
= revalidator
->udpif
;
1898 struct dpif_flow_dump_thread
*dump_thread
;
1899 uint64_t dump_seq
, reval_seq
;
1900 unsigned int flow_limit
;
1902 dump_seq
= seq_read(udpif
->dump_seq
);
1903 reval_seq
= seq_read(udpif
->reval_seq
);
1904 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
1905 dump_thread
= dpif_flow_dump_thread_create(udpif
->dump
);
1907 struct ukey_op ops
[REVALIDATE_MAX_BATCH
];
1910 struct dpif_flow flows
[REVALIDATE_MAX_BATCH
];
1911 const struct dpif_flow
*f
;
1914 long long int max_idle
;
1919 n_dumped
= dpif_flow_dump_next(dump_thread
, flows
, ARRAY_SIZE(flows
));
1926 /* In normal operation we want to keep flows around until they have
1927 * been idle for 'ofproto_max_idle' milliseconds. However:
1929 * - If the number of datapath flows climbs above 'flow_limit',
1930 * drop that down to 100 ms to try to bring the flows down to
1933 * - If the number of datapath flows climbs above twice
1934 * 'flow_limit', delete all the datapath flows as an emergency
1935 * measure. (We reassess this condition for the next batch of
1936 * datapath flows, so we will recover before all the flows are
1938 n_dp_flows
= udpif_get_n_flows(udpif
);
1939 kill_them_all
= n_dp_flows
> flow_limit
* 2;
1940 max_idle
= n_dp_flows
> flow_limit
? 100 : ofproto_max_idle
;
1942 for (f
= flows
; f
< &flows
[n_dumped
]; f
++) {
1943 long long int used
= f
->stats
.used
;
1944 struct udpif_key
*ukey
;
1945 bool already_dumped
, keep
;
1948 if (ukey_acquire(udpif
, f
, &ukey
, &error
)) {
1949 if (error
== EBUSY
) {
1950 /* Another thread is processing this flow, so don't bother
1952 COVERAGE_INC(upcall_ukey_contention
);
1954 log_unexpected_flow(f
, error
);
1955 if (error
!= ENOENT
) {
1956 delete_op_init__(udpif
, &ops
[n_ops
++], f
);
1962 already_dumped
= ukey
->dump_seq
== dump_seq
;
1963 if (already_dumped
) {
1964 /* The flow has already been handled during this flow dump
1965 * operation. Skip it. */
1967 COVERAGE_INC(dumped_duplicate_flow
);
1969 COVERAGE_INC(dumped_new_flow
);
1971 ovs_mutex_unlock(&ukey
->mutex
);
1976 used
= ukey
->created
;
1978 if (kill_them_all
|| (used
&& used
< now
- max_idle
)) {
1981 keep
= revalidate_ukey(udpif
, ukey
, &f
->stats
, reval_seq
);
1983 ukey
->dump_seq
= dump_seq
;
1984 ukey
->flow_exists
= keep
;
1987 delete_op_init(udpif
, &ops
[n_ops
++], ukey
);
1989 ovs_mutex_unlock(&ukey
->mutex
);
1993 push_ukey_ops__(udpif
, ops
, n_ops
);
1997 dpif_flow_dump_thread_destroy(dump_thread
);
2001 handle_missed_revalidation(struct udpif
*udpif
, uint64_t reval_seq
,
2002 struct udpif_key
*ukey
)
2004 struct dpif_flow_stats stats
;
2007 COVERAGE_INC(revalidate_missed_dp_flow
);
2009 memset(&stats
, 0, sizeof stats
);
2010 ovs_mutex_lock(&ukey
->mutex
);
2011 keep
= revalidate_ukey(udpif
, ukey
, &stats
, reval_seq
);
2012 ovs_mutex_unlock(&ukey
->mutex
);
2018 revalidator_sweep__(struct revalidator
*revalidator
, bool purge
)
2020 struct udpif
*udpif
;
2021 uint64_t dump_seq
, reval_seq
;
2024 udpif
= revalidator
->udpif
;
2025 dump_seq
= seq_read(udpif
->dump_seq
);
2026 reval_seq
= seq_read(udpif
->reval_seq
);
2027 slice
= revalidator
- udpif
->revalidators
;
2028 ovs_assert(slice
< udpif
->n_revalidators
);
2030 for (int i
= slice
; i
< N_UMAPS
; i
+= udpif
->n_revalidators
) {
2031 struct ukey_op ops
[REVALIDATE_MAX_BATCH
];
2032 struct udpif_key
*ukey
;
2033 struct umap
*umap
= &udpif
->ukeys
[i
];
2036 CMAP_FOR_EACH(ukey
, cmap_node
, &umap
->cmap
) {
2037 bool flow_exists
, seq_mismatch
;
2039 /* Handler threads could be holding a ukey lock while it installs a
2040 * new flow, so don't hang around waiting for access to it. */
2041 if (ovs_mutex_trylock(&ukey
->mutex
)) {
2044 flow_exists
= ukey
->flow_exists
;
2045 seq_mismatch
= (ukey
->dump_seq
!= dump_seq
2046 && ukey
->reval_seq
!= reval_seq
);
2047 ovs_mutex_unlock(&ukey
->mutex
);
2052 && !handle_missed_revalidation(udpif
, reval_seq
,
2054 struct ukey_op
*op
= &ops
[n_ops
++];
2056 delete_op_init(udpif
, op
, ukey
);
2057 if (n_ops
== REVALIDATE_MAX_BATCH
) {
2058 push_ukey_ops(udpif
, umap
, ops
, n_ops
);
2061 } else if (!flow_exists
) {
2062 ovs_mutex_lock(&umap
->mutex
);
2063 ukey_delete(umap
, ukey
);
2064 ovs_mutex_unlock(&umap
->mutex
);
2069 push_ukey_ops(udpif
, umap
, ops
, n_ops
);
2076 revalidator_sweep(struct revalidator
*revalidator
)
2078 revalidator_sweep__(revalidator
, false);
2082 revalidator_purge(struct revalidator
*revalidator
)
2084 revalidator_sweep__(revalidator
, true);
2088 upcall_unixctl_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2089 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
2091 struct ds ds
= DS_EMPTY_INITIALIZER
;
2092 struct udpif
*udpif
;
2094 LIST_FOR_EACH (udpif
, list_node
, &all_udpifs
) {
2095 unsigned int flow_limit
;
2099 atomic_read_relaxed(&udpif
->flow_limit
, &flow_limit
);
2100 ufid_enabled
= udpif_use_ufid(udpif
);
2102 ds_put_format(&ds
, "%s:\n", dpif_name(udpif
->dpif
));
2103 ds_put_format(&ds
, "\tflows : (current %lu)"
2104 " (avg %u) (max %u) (limit %u)\n", udpif_get_n_flows(udpif
),
2105 udpif
->avg_n_flows
, udpif
->max_n_flows
, flow_limit
);
2106 ds_put_format(&ds
, "\tdump duration : %lldms\n", udpif
->dump_duration
);
2107 ds_put_format(&ds
, "\tufid enabled : ");
2109 ds_put_format(&ds
, "true\n");
2111 ds_put_format(&ds
, "false\n");
2113 ds_put_char(&ds
, '\n');
2115 for (i
= 0; i
< n_revalidators
; i
++) {
2116 struct revalidator
*revalidator
= &udpif
->revalidators
[i
];
2117 int j
, elements
= 0;
2119 for (j
= i
; j
< N_UMAPS
; j
+= n_revalidators
) {
2120 elements
+= cmap_count(&udpif
->ukeys
[j
].cmap
);
2122 ds_put_format(&ds
, "\t%u: (keys %d)\n", revalidator
->id
, elements
);
2126 unixctl_command_reply(conn
, ds_cstr(&ds
));
2130 /* Disable using the megaflows.
2132 * This command is only needed for advanced debugging, so it's not
2133 * documented in the man page. */
2135 upcall_unixctl_disable_megaflows(struct unixctl_conn
*conn
,
2136 int argc OVS_UNUSED
,
2137 const char *argv
[] OVS_UNUSED
,
2138 void *aux OVS_UNUSED
)
2140 atomic_store_relaxed(&enable_megaflows
, false);
2141 udpif_flush_all_datapaths();
2142 unixctl_command_reply(conn
, "megaflows disabled");
2145 /* Re-enable using megaflows.
2147 * This command is only needed for advanced debugging, so it's not
2148 * documented in the man page. */
2150 upcall_unixctl_enable_megaflows(struct unixctl_conn
*conn
,
2151 int argc OVS_UNUSED
,
2152 const char *argv
[] OVS_UNUSED
,
2153 void *aux OVS_UNUSED
)
2155 atomic_store_relaxed(&enable_megaflows
, true);
2156 udpif_flush_all_datapaths();
2157 unixctl_command_reply(conn
, "megaflows enabled");
2160 /* Disable skipping flow attributes during flow dump.
2162 * This command is only needed for advanced debugging, so it's not
2163 * documented in the man page. */
2165 upcall_unixctl_disable_ufid(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2166 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
2168 atomic_store_relaxed(&enable_ufid
, false);
2169 unixctl_command_reply(conn
, "Datapath dumping tersely using UFID disabled");
2172 /* Re-enable skipping flow attributes during flow dump.
2174 * This command is only needed for advanced debugging, so it's not documented
2175 * in the man page. */
2177 upcall_unixctl_enable_ufid(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2178 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
2180 atomic_store_relaxed(&enable_ufid
, true);
2181 unixctl_command_reply(conn
, "Datapath dumping tersely using UFID enabled "
2182 "for supported datapaths");
2185 /* Set the flow limit.
2187 * This command is only needed for advanced debugging, so it's not
2188 * documented in the man page. */
2190 upcall_unixctl_set_flow_limit(struct unixctl_conn
*conn
,
2191 int argc OVS_UNUSED
,
2192 const char *argv
[] OVS_UNUSED
,
2193 void *aux OVS_UNUSED
)
2195 struct ds ds
= DS_EMPTY_INITIALIZER
;
2196 struct udpif
*udpif
;
2197 unsigned int flow_limit
= atoi(argv
[1]);
2199 LIST_FOR_EACH (udpif
, list_node
, &all_udpifs
) {
2200 atomic_store_relaxed(&udpif
->flow_limit
, flow_limit
);
2202 ds_put_format(&ds
, "set flow_limit to %u\n", flow_limit
);
2203 unixctl_command_reply(conn
, ds_cstr(&ds
));
2208 upcall_unixctl_dump_wait(struct unixctl_conn
*conn
,
2209 int argc OVS_UNUSED
,
2210 const char *argv
[] OVS_UNUSED
,
2211 void *aux OVS_UNUSED
)
2213 if (list_is_singleton(&all_udpifs
)) {
2214 struct udpif
*udpif
= NULL
;
2217 udpif
= OBJECT_CONTAINING(list_front(&all_udpifs
), udpif
, list_node
);
2218 len
= (udpif
->n_conns
+ 1) * sizeof *udpif
->conns
;
2219 udpif
->conn_seq
= seq_read(udpif
->dump_seq
);
2220 udpif
->conns
= xrealloc(udpif
->conns
, len
);
2221 udpif
->conns
[udpif
->n_conns
++] = conn
;
2223 unixctl_command_reply_error(conn
, "can't wait on multiple udpifs.");
2228 upcall_unixctl_purge(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2229 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
2231 struct udpif
*udpif
;
2233 LIST_FOR_EACH (udpif
, list_node
, &all_udpifs
) {
2236 for (n
= 0; n
< udpif
->n_revalidators
; n
++) {
2237 revalidator_purge(&udpif
->revalidators
[n
]);
2240 unixctl_command_reply(conn
, "");