blob: 701801370b19bec4494b81b66487a09c2ce878ed [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <vnet/fib/fib_walk.h>
17#include <vnet/fib/fib_node_list.h>
18
19/**
20 * The flags on a walk
21 */
22typedef enum fib_walk_flags_t_
23{
24 /**
25 * A synchronous walk.
26 * This walk will run to completion, i.e. visit ALL the children.
27 * It is a depth first traversal of the graph.
28 */
29 FIB_WALK_FLAG_SYNC = (1 << 0),
30 /**
31 * An asynchronous walk.
32 * This walk will be scheduled to run in the background. It will thus visits
33 * the children at a later point in time.
34 * It is a depth first traversal of the graph.
35 */
36 FIB_WALK_FLAG_ASYNC = (1 << 1),
37 /**
38 * An indication that the walk is currently executing.
39 */
40 FIB_WALK_FLAG_EXECUTING = (1 << 2),
41} fib_walk_flags_t;
42
43/**
44 * A representation of a graph walk from a parent object to its children
45 */
46typedef struct fib_walk_t_
47{
48 /**
49 * FIB node linkage. This object is not in the FIB object graph,
50 * but it is present in other node's dependency lists, so it needs to
51 * be pointerable to.
52 */
53 fib_node_t fw_node;
54
55 /**
56 * the walk's flags
57 */
58 fib_walk_flags_t fw_flags;
59
60 /**
61 * Sibling index in the dependency list
62 */
63 u32 fw_dep_sibling;
64
65 /**
66 * Sibling index in the list of all walks
67 */
68 u32 fw_prio_sibling;
69
70 /**
71 * Pointer to the node whose dependants this walk is walking
72 */
73 fib_node_ptr_t fw_parent;
74
75 /**
76 * Number of nodes visited by this walk. saved for debugging purposes.
77 */
78 u32 fw_n_visits;
79
80 /**
Neale Ranns33a7dd52016-10-07 15:14:33 +010081 * Time the walk started
82 */
83 f64 fw_start_time;
84
85 /**
Neale Ranns0bfe5d82016-08-25 15:29:12 +010086 * The reasons this walk is occuring.
87 * This is a vector ordered in time. The reasons and the front were started
88 * first, and so should be acted first when a node is visisted.
89 */
90 fib_node_back_walk_ctx_t *fw_ctx;
91} fib_walk_t;
92
93/**
94 * @brief The pool of all walk objects
95 */
96static fib_walk_t *fib_walk_pool;
97
98/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +010099 * Statistics maintained per-walk queue
100 */
101typedef enum fib_walk_queue_stats_t_
102{
103 FIB_WALK_SCHEDULED,
104 FIB_WALK_COMPLETED,
105} fib_walk_queue_stats_t;
Neale Ranns450cd302016-11-09 17:49:42 +0000106#define FIB_WALK_QUEUE_STATS_NUM ((fib_walk_queue_stats_t)(FIB_WALK_COMPLETED+1))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100107
108#define FIB_WALK_QUEUE_STATS { \
109 [FIB_WALK_SCHEDULED] = "scheduled", \
110 [FIB_WALK_COMPLETED] = "completed", \
111}
112
113#define FOR_EACH_FIB_WALK_QUEUE_STATS(_wqs) \
114 for ((_wqs) = FIB_WALK_SCHEDULED; \
Neale Ranns33a7dd52016-10-07 15:14:33 +0100115 (_wqs) < FIB_WALK_QUEUE_STATS_NUM; \
116 (_wqs)++)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100117
118/**
119 * The names of the walk stats
120 */
121static const char * const fib_walk_queue_stats_names[] = FIB_WALK_QUEUE_STATS;
Neale Rannsad95b5d2016-11-10 20:35:14 +0000122/**
123 * The names of the walk reasons
124 */
125static const char * const fib_node_bw_reason_names[] = FIB_NODE_BW_REASONS;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100126
127/**
128 * A represenation of one queue of walk
129 */
130typedef struct fib_walk_queue_t_
131{
132 /**
133 * Qeuee stats
134 */
135 u64 fwq_stats[FIB_WALK_QUEUE_STATS_NUM];
136
137 /**
138 * The node list which acts as the queue
139 */
140 fib_node_list_t fwq_queue;
141} fib_walk_queue_t;
142
143/**
144 * A set of priority queues for outstanding walks
145 */
146typedef struct fib_walk_queues_t_
147{
148 fib_walk_queue_t fwqs_queues[FIB_WALK_PRIORITY_NUM];
149} fib_walk_queues_t;
150
151/**
152 * The global queues of outstanding walks
153 */
154static fib_walk_queues_t fib_walk_queues;
155
156/**
157 * The names of the walk priorities
158 */
159static const char * const fib_walk_priority_names[] = FIB_WALK_PRIORITIES;
160
Neale Ranns33a7dd52016-10-07 15:14:33 +0100161/**
162 * @brief Histogram stats on the lenths of each walk in elemenets visisted.
163 * Store upto 1<<23 elements in increments of 1<<10
164 */
165#define HISTOGRAM_VISITS_PER_WALK_MAX (1<<23)
166#define HISTOGRAM_VISITS_PER_WALK_INCR (1<<10)
167#define HISTOGRAM_VISITS_PER_WALK_N_BUCKETS \
168 (HISTOGRAM_VISITS_PER_WALK_MAX/HISTOGRAM_VISITS_PER_WALK_INCR)
169static u64 fib_walk_hist_vists_per_walk[HISTOGRAM_VISITS_PER_WALK_N_BUCKETS];
170
171/**
172 * @brief History of state for the last 128 walks
173 */
174#define HISTORY_N_WALKS 128
Neale Ranns19c68d22016-12-07 15:38:14 +0000175#define MAX_HISTORY_REASONS 16
Neale Ranns33a7dd52016-10-07 15:14:33 +0100176static u32 history_last_walk_pos;
177typedef struct fib_walk_history_t_ {
178 u32 fwh_n_visits;
179 f64 fwh_duration;
Neale Ranns8b37b872016-11-21 12:25:22 +0000180 f64 fwh_completed;
Neale Ranns33a7dd52016-10-07 15:14:33 +0100181 fib_node_ptr_t fwh_parent;
Neale Rannsad95b5d2016-11-10 20:35:14 +0000182 fib_walk_flags_t fwh_flags;
Neale Ranns19c68d22016-12-07 15:38:14 +0000183 fib_node_bw_reason_flag_t fwh_reason[MAX_HISTORY_REASONS];
Neale Ranns33a7dd52016-10-07 15:14:33 +0100184} fib_walk_history_t;
185static fib_walk_history_t fib_walk_history[HISTORY_N_WALKS];
186
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100187u8*
188format_fib_walk_priority (u8 *s, va_list ap)
189{
190 fib_walk_priority_t prio = va_arg(ap, fib_walk_priority_t);
191
192 ASSERT(prio < FIB_WALK_PRIORITY_NUM);
193
194 return (format(s, "%s", fib_walk_priority_names[prio]));
195}
196static u8*
197format_fib_walk_queue_stats (u8 *s, va_list ap)
198{
199 fib_walk_queue_stats_t wqs = va_arg(ap, fib_walk_queue_stats_t);
200
201 ASSERT(wqs < FIB_WALK_QUEUE_STATS_NUM);
202
203 return (format(s, "%s", fib_walk_queue_stats_names[wqs]));
204}
205
206static index_t
207fib_walk_get_index (fib_walk_t *fwalk)
208{
209 return (fwalk - fib_walk_pool);
210}
211
212static fib_walk_t *
213fib_walk_get (index_t fwi)
214{
215 return (pool_elt_at_index(fib_walk_pool, fwi));
216}
217
218/*
219 * not static so it can be used in the unit tests
220 */
221u32
222fib_walk_queue_get_size (fib_walk_priority_t prio)
223{
224 return (fib_node_list_get_size(fib_walk_queues.fwqs_queues[prio].fwq_queue));
225}
226
227static fib_node_index_t
228fib_walk_queue_get_front (fib_walk_priority_t prio)
229{
230 fib_node_ptr_t wp;
231
232 fib_node_list_get_front(fib_walk_queues.fwqs_queues[prio].fwq_queue, &wp);
233
234 return (wp.fnp_index);
235}
236
237static void
Neale Rannsf12a83f2017-04-18 09:09:40 -0700238fib_walk_destroy (index_t fwi)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100239{
Neale Rannsf12a83f2017-04-18 09:09:40 -0700240 fib_walk_t *fwalk;
Neale Ranns19c68d22016-12-07 15:38:14 +0000241 u32 bucket, ii;
Neale Ranns33a7dd52016-10-07 15:14:33 +0100242
Neale Rannsf12a83f2017-04-18 09:09:40 -0700243 fwalk = fib_walk_get(fwi);
244
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100245 if (FIB_NODE_INDEX_INVALID != fwalk->fw_prio_sibling)
246 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100247 fib_node_list_elt_remove(fwalk->fw_prio_sibling);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100248 }
249 fib_node_child_remove(fwalk->fw_parent.fnp_type,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100250 fwalk->fw_parent.fnp_index,
251 fwalk->fw_dep_sibling);
252
253 /*
Neale Rannsf12a83f2017-04-18 09:09:40 -0700254 * refetch the walk object. More walks could have been spawned as a result
255 * of releasing the lock on the parent.
256 */
257 fwalk = fib_walk_get(fwi);
258
259 /*
Neale Ranns33a7dd52016-10-07 15:14:33 +0100260 * add the stats to the continuous histogram collection.
261 */
262 bucket = (fwalk->fw_n_visits / HISTOGRAM_VISITS_PER_WALK_INCR);
Neale Ranns924d03a2016-10-19 08:25:46 +0100263 bucket = (bucket >= HISTOGRAM_VISITS_PER_WALK_N_BUCKETS ?
Neale Ranns5899fde2016-10-12 13:51:05 +0100264 HISTOGRAM_VISITS_PER_WALK_N_BUCKETS - 1 :
Neale Ranns33a7dd52016-10-07 15:14:33 +0100265 bucket);
266 fib_walk_hist_vists_per_walk[bucket]++;
267
268 /*
269 * save stats to the recent history
270 */
271
272 fib_walk_history[history_last_walk_pos].fwh_n_visits =
273 fwalk->fw_n_visits;
Neale Ranns8b37b872016-11-21 12:25:22 +0000274 fib_walk_history[history_last_walk_pos].fwh_completed =
275 vlib_time_now(vlib_get_main());
Neale Ranns33a7dd52016-10-07 15:14:33 +0100276 fib_walk_history[history_last_walk_pos].fwh_duration =
Neale Ranns8b37b872016-11-21 12:25:22 +0000277 fib_walk_history[history_last_walk_pos].fwh_completed -
278 fwalk->fw_start_time;
Neale Ranns33a7dd52016-10-07 15:14:33 +0100279 fib_walk_history[history_last_walk_pos].fwh_parent =
280 fwalk->fw_parent;
Neale Rannsad95b5d2016-11-10 20:35:14 +0000281 fib_walk_history[history_last_walk_pos].fwh_flags =
282 fwalk->fw_flags;
283
Neale Ranns19c68d22016-12-07 15:38:14 +0000284 vec_foreach_index(ii, fwalk->fw_ctx)
Neale Rannsad95b5d2016-11-10 20:35:14 +0000285 {
Neale Ranns19c68d22016-12-07 15:38:14 +0000286 if (ii < MAX_HISTORY_REASONS)
287 {
288 fib_walk_history[history_last_walk_pos].fwh_reason[ii] =
289 fwalk->fw_ctx[ii].fnbw_reason;
290 }
Neale Rannsad95b5d2016-11-10 20:35:14 +0000291 }
Neale Ranns33a7dd52016-10-07 15:14:33 +0100292
293 history_last_walk_pos = (history_last_walk_pos + 1) % HISTORY_N_WALKS;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100294
295 fib_node_deinit(&fwalk->fw_node);
Neale Ranns19c68d22016-12-07 15:38:14 +0000296 vec_free(fwalk->fw_ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100297 pool_put(fib_walk_pool, fwalk);
298}
299
300/**
301 * return code when advancing a walk
302 */
303typedef enum fib_walk_advance_rc_t_
304{
305 /**
306 * The walk is complete
307 */
308 FIB_WALK_ADVANCE_DONE,
309 /**
310 * the walk has more work
311 */
312 FIB_WALK_ADVANCE_MORE,
313 /**
314 * The walk merged with the one in front
315 */
316 FIB_WALK_ADVANCE_MERGE,
317} fib_walk_advance_rc_t;
318
319/**
320 * @brief Advance the walk one element in its work list
321 */
322static fib_walk_advance_rc_t
323fib_walk_advance (fib_node_index_t fwi)
324{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100325 fib_node_back_walk_rc_t wrc;
326 fib_node_ptr_t sibling;
327 fib_walk_t *fwalk;
Neale Ranns8c4611b2017-05-23 03:43:47 -0700328 uint n_ctxs, ii;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100329 int more_elts;
330
331 /*
332 * this walk function is re-entrant - walks acan spawn walks.
Neale Ranns33a7dd52016-10-07 15:14:33 +0100333 * fib_walk_t objects come from a pool, so they can realloc. we need
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100334 * to retch from said pool at the appropriate times.
335 */
336 fwalk = fib_walk_get(fwi);
337
338 more_elts = fib_node_list_elt_get_next(fwalk->fw_dep_sibling, &sibling);
339
340 if (more_elts)
341 {
Neale Ranns19c68d22016-12-07 15:38:14 +0000342
Neale Ranns8c4611b2017-05-23 03:43:47 -0700343 /*
344 * loop through the backwalk contexts. This can grow in length
345 * as walks on the same object meet each other. Order is preserved so the
346 * most recently started walk as at the back of the vector.
347 */
348 ii = 0;
349 n_ctxs = vec_len(fwalk->fw_ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100350
Neale Ranns8c4611b2017-05-23 03:43:47 -0700351 while (ii < n_ctxs)
352 {
353 wrc = fib_node_back_walk_one(&sibling, &fwalk->fw_ctx[ii]);
354
355 ii++;
Neale Ranns33a7dd52016-10-07 15:14:33 +0100356 fwalk = fib_walk_get(fwi);
357 fwalk->fw_n_visits++;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100358
Neale Ranns33a7dd52016-10-07 15:14:33 +0100359 if (FIB_NODE_BACK_WALK_MERGE == wrc)
360 {
361 /*
362 * this walk has merged with the one further along the node's
363 * dependecy list.
364 */
365 return (FIB_WALK_ADVANCE_MERGE);
366 }
Neale Ranns8c4611b2017-05-23 03:43:47 -0700367
368 /*
369 * re-evaluate the number of backwalk contexts we need to process.
370 */
371 n_ctxs = vec_len(fwalk->fw_ctx);
Neale Ranns33a7dd52016-10-07 15:14:33 +0100372 }
373 /*
374 * move foward to the next node to visit
375 */
376 more_elts = fib_node_list_advance(fwalk->fw_dep_sibling);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100377 }
378
379 if (more_elts)
380 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100381 return (FIB_WALK_ADVANCE_MORE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100382 }
383
384 return (FIB_WALK_ADVANCE_DONE);
385}
386
387/**
Neale Ranns33a7dd52016-10-07 15:14:33 +0100388 * @breif Enurmerate the times of sleep between walks
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100389 */
Neale Ranns33a7dd52016-10-07 15:14:33 +0100390typedef enum fib_walk_sleep_type_t_
391{
392 FIB_WALK_SHORT_SLEEP,
393 FIB_WALK_LONG_SLEEP,
394} fib_walk_sleep_type_t;
395
396#define FIB_WALK_N_SLEEP (FIB_WALK_LONG_SLEEP+1)
397
398/**
399 * @brief Durations for the sleep types
400 */
401static f64 fib_walk_sleep_duration[] = {
402 [FIB_WALK_LONG_SLEEP] = 1e-3,
403 [FIB_WALK_SHORT_SLEEP] = 1e-8,
404};
405
406/**
407 * @brief The time quota for a walk. When more than this amount of time is
408 * spent, the walk process will yield.
409 */
410static f64 quota = 1e-4;
411
412/**
413 * Histogram on the amount of work done (in msecs) in each walk
414 */
415#define N_TIME_BUCKETS 128
416#define TIME_INCREMENTS (N_TIME_BUCKETS/2)
417static u64 fib_walk_work_time_taken[N_TIME_BUCKETS];
418
419/**
420 * Histogram on the number of nodes visted in each quota
421 */
422#define N_ELTS_BUCKETS 128
423static u32 fib_walk_work_nodes_visisted_incr = 2;
424static u64 fib_walk_work_nodes_visited[N_ELTS_BUCKETS];
425
426/**
427 * Histogram of the sleep lengths
428 */
429static u64 fib_walk_sleep_lengths[2];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100430
431/**
432 * @brief Service the queues
433 * This is not declared static so that it can be unit tested - i know i know...
434 */
435f64
436fib_walk_process_queues (vlib_main_t * vm,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100437 const f64 quota)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100438{
Neale Ranns33a7dd52016-10-07 15:14:33 +0100439 f64 start_time, consumed_time;
440 fib_walk_sleep_type_t sleep;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100441 fib_walk_priority_t prio;
442 fib_walk_advance_rc_t rc;
443 fib_node_index_t fwi;
444 fib_walk_t *fwalk;
Neale Ranns33a7dd52016-10-07 15:14:33 +0100445 u32 n_elts;
446 i32 bucket;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100447
Neale Ranns33a7dd52016-10-07 15:14:33 +0100448 consumed_time = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100449 start_time = vlib_time_now(vm);
Neale Ranns33a7dd52016-10-07 15:14:33 +0100450 n_elts = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100451
452 FOR_EACH_FIB_WALK_PRIORITY(prio)
453 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100454 while (0 != fib_walk_queue_get_size(prio))
455 {
456 fwi = fib_walk_queue_get_front(prio);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100457
Neale Ranns33a7dd52016-10-07 15:14:33 +0100458 /*
459 * set this walk as executing
460 */
461 fwalk = fib_walk_get(fwi);
462 fwalk->fw_flags |= FIB_WALK_FLAG_EXECUTING;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100463
Neale Ranns33a7dd52016-10-07 15:14:33 +0100464 do
465 {
466 rc = fib_walk_advance(fwi);
467 n_elts++;
468 consumed_time = (vlib_time_now(vm) - start_time);
469 } while ((consumed_time < quota) &&
470 (FIB_WALK_ADVANCE_MORE == rc));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100471
Neale Ranns33a7dd52016-10-07 15:14:33 +0100472 /*
473 * if this walk has no more work then pop it from the queue
474 * and move on to the next.
475 */
476 if (FIB_WALK_ADVANCE_MORE != rc)
477 {
Neale Rannsf12a83f2017-04-18 09:09:40 -0700478 fib_walk_destroy(fwi);
Neale Ranns33a7dd52016-10-07 15:14:33 +0100479 fib_walk_queues.fwqs_queues[prio].fwq_stats[FIB_WALK_COMPLETED]++;
480 }
481 else
482 {
483 /*
484 * passed our work quota. sleep time.
485 */
486 fwalk = fib_walk_get(fwi);
487 fwalk->fw_flags &= ~FIB_WALK_FLAG_EXECUTING;
488 sleep = FIB_WALK_SHORT_SLEEP;
489 goto that_will_do_for_now;
490 }
491 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100492 }
493 /*
494 * got to the end of all the work
495 */
Neale Ranns33a7dd52016-10-07 15:14:33 +0100496 sleep = FIB_WALK_LONG_SLEEP;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100497
498that_will_do_for_now:
Neale Ranns33a7dd52016-10-07 15:14:33 +0100499
500 /*
501 * collect the stats:
502 * - for the number of nodes visisted we store 128 increments
503 * - for the time consumed we store quota/TIME_INCREMENTS increments.
504 */
505 bucket = ((n_elts/fib_walk_work_nodes_visisted_incr) > N_ELTS_BUCKETS ?
506 N_ELTS_BUCKETS-1 :
507 n_elts/fib_walk_work_nodes_visisted_incr);
508 ++fib_walk_work_nodes_visited[bucket];
509
510 bucket = (consumed_time - quota) / (quota / TIME_INCREMENTS);
511 bucket += N_TIME_BUCKETS/2;
512 bucket = (bucket < 0 ? 0 : bucket);
513 bucket = (bucket > N_TIME_BUCKETS-1 ? N_TIME_BUCKETS-1 : bucket);
514 ++fib_walk_work_time_taken[bucket];
515
516 ++fib_walk_sleep_lengths[sleep];
517
518 return (fib_walk_sleep_duration[sleep]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100519}
520
521/**
Neale Rannsf12a83f2017-04-18 09:09:40 -0700522 * Events sent to the FIB walk process
523 */
524typedef enum fib_walk_process_event_t_
525{
526 FIB_WALK_PROCESS_EVENT_DATA,
527 FIB_WALK_PROCESS_EVENT_ENABLE,
528 FIB_WALK_PROCESS_EVENT_DISABLE,
529} fib_walk_process_event;
530
531/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100532 * @brief The 'fib-walk' process's main loop.
533 */
534static uword
535fib_walk_process (vlib_main_t * vm,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100536 vlib_node_runtime_t * node,
537 vlib_frame_t * f)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100538{
Neale Rannsf12a83f2017-04-18 09:09:40 -0700539 uword event_type, *event_data = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100540 f64 sleep_time;
Neale Rannsf12a83f2017-04-18 09:09:40 -0700541 int enabled;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100542
Neale Rannsf12a83f2017-04-18 09:09:40 -0700543 enabled = 1;
Neale Ranns33a7dd52016-10-07 15:14:33 +0100544 sleep_time = fib_walk_sleep_duration[FIB_WALK_SHORT_SLEEP];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100545
546 while (1)
547 {
Neale Rannsf12a83f2017-04-18 09:09:40 -0700548 /*
549 * the feature to disable/enable this walk process is only
550 * for testing purposes
551 */
552 if (enabled)
553 {
554 vlib_process_wait_for_event_or_clock(vm, sleep_time);
555 }
556 else
557 {
558 vlib_process_wait_for_event(vm);
559 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100560
Neale Rannsf12a83f2017-04-18 09:09:40 -0700561 event_type = vlib_process_get_events(vm, &event_data);
562 vec_reset_length(event_data);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100563
Neale Rannsf12a83f2017-04-18 09:09:40 -0700564 switch (event_type)
565 {
566 case FIB_WALK_PROCESS_EVENT_ENABLE:
567 enabled = 1;
568 break;
569 case FIB_WALK_PROCESS_EVENT_DISABLE:
570 enabled = 0;
571 break;
572 default:
573 break;
574 }
575
576 if (enabled)
577 {
578 sleep_time = fib_walk_process_queues(vm, quota);
579 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100580 }
581
582 /*
583 * Unreached
584 */
585 ASSERT(!"WTF");
586 return 0;
587}
588
589/* *INDENT-OFF* */
590VLIB_REGISTER_NODE (fib_walk_process_node,static) = {
591 .function = fib_walk_process,
592 .type = VLIB_NODE_TYPE_PROCESS,
593 .name = "fib-walk",
594};
595/* *INDENT-ON* */
596
597/**
598 * @brief Allocate a new walk object
Neale Ranns33a7dd52016-10-07 15:14:33 +0100599 */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100600static fib_walk_t *
601fib_walk_alloc (fib_node_type_t parent_type,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100602 fib_node_index_t parent_index,
603 fib_walk_flags_t flags,
604 fib_node_back_walk_ctx_t *ctx)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100605{
606 fib_walk_t *fwalk;
607
608 pool_get(fib_walk_pool, fwalk);
609
610 fib_node_init(&fwalk->fw_node, FIB_NODE_TYPE_WALK);
611
612 fwalk->fw_flags = flags;
613 fwalk->fw_dep_sibling = FIB_NODE_INDEX_INVALID;
614 fwalk->fw_prio_sibling = FIB_NODE_INDEX_INVALID;
615 fwalk->fw_parent.fnp_index = parent_index;
616 fwalk->fw_parent.fnp_type = parent_type;
617 fwalk->fw_ctx = NULL;
Neale Ranns33a7dd52016-10-07 15:14:33 +0100618 fwalk->fw_start_time = vlib_time_now(vlib_get_main());
619 fwalk->fw_n_visits = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100620
621 /*
622 * make a copy of the backwalk context so the depth count remains
623 * the same for each sibling visitsed. This is important in the case
Neale Ranns19c68d22016-12-07 15:38:14 +0000624 * where a parent has a loop via one child, but all the others are not.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100625 * if the looped child were visited first, the depth count would exceed, the
626 * max and the walk would terminate before it reached the other siblings.
627 */
628 vec_add1(fwalk->fw_ctx, *ctx);
629
630 return (fwalk);
631}
632
633/**
634 * @brief Enqueue a walk onto the appropriate priority queue. Then signal
635 * the background process there is work to do.
636 */
637static index_t
638fib_walk_prio_queue_enquue (fib_walk_priority_t prio,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100639 fib_walk_t *fwalk)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100640{
641 index_t sibling;
642
643 sibling = fib_node_list_push_front(fib_walk_queues.fwqs_queues[prio].fwq_queue,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100644 0,
645 FIB_NODE_TYPE_WALK,
646 fib_walk_get_index(fwalk));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100647 fib_walk_queues.fwqs_queues[prio].fwq_stats[FIB_WALK_SCHEDULED]++;
648
649 /*
650 * poke the fib-walk process to perform the async walk.
651 * we are not passing it specific data, hence the last two args,
652 * the process will drain the queues
653 */
654 vlib_process_signal_event(vlib_get_main(),
Neale Ranns33a7dd52016-10-07 15:14:33 +0100655 fib_walk_process_node.index,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700656 FIB_WALK_PROCESS_EVENT_DATA,
657 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100658
659 return (sibling);
660}
661
662void
663fib_walk_async (fib_node_type_t parent_type,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100664 fib_node_index_t parent_index,
665 fib_walk_priority_t prio,
666 fib_node_back_walk_ctx_t *ctx)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100667{
668 fib_walk_t *fwalk;
669
670 if (FIB_NODE_GRAPH_MAX_DEPTH < ++ctx->fnbw_depth)
671 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100672 /*
673 * The walk has reached the maximum depth. there is a loop in the graph.
674 * bail.
675 */
676 return;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100677 }
Neale Rannsad422ed2016-11-02 14:20:04 +0000678 if (0 == fib_node_get_n_children(parent_type,
679 parent_index))
Neale Ranns8b37b872016-11-21 12:25:22 +0000680 {
681 /*
682 * no children to walk - quit now
683 */
684 return;
685 }
Neale Rannsad95b5d2016-11-10 20:35:14 +0000686 if (ctx->fnbw_flags & FIB_NODE_BW_FLAG_FORCE_SYNC)
687 {
688 /*
689 * the originator of the walk wanted it to be synchronous, but the
690 * parent object chose async - denied.
691 */
692 return (fib_walk_sync(parent_type, parent_index, ctx));
693 }
694
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100695
696 fwalk = fib_walk_alloc(parent_type,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100697 parent_index,
698 FIB_WALK_FLAG_ASYNC,
699 ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100700
701 fwalk->fw_dep_sibling = fib_node_child_add(parent_type,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100702 parent_index,
703 FIB_NODE_TYPE_WALK,
704 fib_walk_get_index(fwalk));
705
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100706 fwalk->fw_prio_sibling = fib_walk_prio_queue_enquue(prio, fwalk);
707}
708
709/**
710 * @brief Back walk all the children of a FIB node.
711 *
712 * note this is a synchronous depth first walk. Children visited may propagate
713 * the walk to thier children. Other children node types may not propagate,
714 * synchronously but instead queue the walk for later async completion.
715 */
716void
717fib_walk_sync (fib_node_type_t parent_type,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100718 fib_node_index_t parent_index,
719 fib_node_back_walk_ctx_t *ctx)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100720{
721 fib_walk_advance_rc_t rc;
722 fib_node_index_t fwi;
723 fib_walk_t *fwalk;
724
725 if (FIB_NODE_GRAPH_MAX_DEPTH < ++ctx->fnbw_depth)
726 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100727 /*
728 * The walk has reached the maximum depth. there is a loop in the graph.
729 * bail.
730 */
731 return;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100732 }
Neale Rannsad422ed2016-11-02 14:20:04 +0000733 if (0 == fib_node_get_n_children(parent_type,
734 parent_index))
Neale Ranns8b37b872016-11-21 12:25:22 +0000735 {
736 /*
737 * no children to walk - quit now
738 */
739 return;
740 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100741
742 fwalk = fib_walk_alloc(parent_type,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100743 parent_index,
744 FIB_WALK_FLAG_SYNC,
745 ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100746
747 fwalk->fw_dep_sibling = fib_node_child_add(parent_type,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100748 parent_index,
749 FIB_NODE_TYPE_WALK,
750 fib_walk_get_index(fwalk));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100751 fwi = fib_walk_get_index(fwalk);
752
753 while (1)
754 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100755 /*
756 * set this walk as executing
757 */
758 fwalk->fw_flags |= FIB_WALK_FLAG_EXECUTING;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100759
Neale Ranns33a7dd52016-10-07 15:14:33 +0100760 do
761 {
762 rc = fib_walk_advance(fwi);
763 } while (FIB_WALK_ADVANCE_MORE == rc);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100764
765
Neale Ranns33a7dd52016-10-07 15:14:33 +0100766 /*
767 * this walk function is re-entrant - walks can spawn walks.
768 * fib_walk_t objects come from a pool, so they can realloc. we need
769 * to re-fetch from said pool at the appropriate times.
770 */
771 fwalk = fib_walk_get(fwi);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100772
Neale Ranns33a7dd52016-10-07 15:14:33 +0100773 if (FIB_WALK_ADVANCE_MERGE == rc)
774 {
775 /*
776 * this sync walk merged with an walk in front.
777 * by reqeusting a sync walk the client wanted all children walked,
778 * so we ditch the walk object in hand and continue with the one
779 * we merged into
780 */
781 fib_node_ptr_t merged_walk;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100782
Neale Ranns33a7dd52016-10-07 15:14:33 +0100783 fib_node_list_elt_get_next(fwalk->fw_dep_sibling, &merged_walk);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100784
Neale Ranns33a7dd52016-10-07 15:14:33 +0100785 ASSERT(FIB_NODE_INDEX_INVALID != merged_walk.fnp_index);
786 ASSERT(FIB_NODE_TYPE_WALK == merged_walk.fnp_type);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100787
Neale Rannsf12a83f2017-04-18 09:09:40 -0700788 fib_walk_destroy(fwi);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100789
Neale Ranns33a7dd52016-10-07 15:14:33 +0100790 fwi = merged_walk.fnp_index;
791 fwalk = fib_walk_get(fwi);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100792
Neale Ranns33a7dd52016-10-07 15:14:33 +0100793 if (FIB_WALK_FLAG_EXECUTING & fwalk->fw_flags)
794 {
795 /*
796 * we are executing a sync walk, and we have met with another
797 * walk that is also executing. since only one walk executs at once
798 * (there is no multi-threading) this implies we have met ourselves
799 * and hence the is a loop in the graph.
800 * This function is re-entrant, so the walk object we met is being
801 * acted on in a stack frame below this one. We must therefore not
802 * continue with it now, but let the stack unwind and along the
803 * appropriate frame to read the depth count and bail.
804 */
805 fwalk = NULL;
806 break;
807 }
808 }
809 else
810 {
811 /*
812 * the walk reached the end of the depdency list.
813 */
814 break;
815 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100816 }
817
818 if (NULL != fwalk)
819 {
Neale Rannsf12a83f2017-04-18 09:09:40 -0700820 fib_walk_destroy(fwi);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100821 }
822}
823
824static fib_node_t *
825fib_walk_get_node (fib_node_index_t index)
826{
827 fib_walk_t *fwalk;
828
829 fwalk = fib_walk_get(index);
830
831 return (&(fwalk->fw_node));
832}
833
834/**
835 * Walk objects are not parents, nor are they locked.
836 * are no-ops
837 */
838static void
839fib_walk_last_lock_gone (fib_node_t *node)
840{
841 ASSERT(0);
842}
843
844static fib_walk_t*
845fib_walk_get_from_node (fib_node_t *node)
846{
847 return ((fib_walk_t*)(((char*)node) -
Neale Ranns33a7dd52016-10-07 15:14:33 +0100848 STRUCT_OFFSET_OF(fib_walk_t, fw_node)));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100849}
850
851/**
852 * @brief Another back walk has reach this walk.
853 * Megre them so there is only one left. It is this node being
854 * visited that will remain, so copy or merge the context onto it.
855 */
856static fib_node_back_walk_rc_t
857fib_walk_back_walk_notify (fib_node_t *node,
858 fib_node_back_walk_ctx_t *ctx)
859{
Neale Ranns19c68d22016-12-07 15:38:14 +0000860 fib_node_back_walk_ctx_t *last;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100861 fib_walk_t *fwalk;
862
863 fwalk = fib_walk_get_from_node(node);
864
865 /*
Neale Ranns19c68d22016-12-07 15:38:14 +0000866 * check whether the walk context can be merged with the most recent.
867 * the most recent was the one last added and is thus at the back of the vector.
868 * we can merge walks if the reason for the walk is the same.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100869 */
Neale Ranns19c68d22016-12-07 15:38:14 +0000870 last = vec_end(fwalk->fw_ctx) - 1;
871
872 if (last->fnbw_reason == ctx->fnbw_reason)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100873 {
Neale Ranns19c68d22016-12-07 15:38:14 +0000874 /*
875 * copy the largest of the depth values. in the presence of a loop,
876 * the same walk will merge with itself. if we take the smaller depth
877 * then it will never end.
878 */
879 last->fnbw_depth = ((last->fnbw_depth >= ctx->fnbw_depth) ?
880 last->fnbw_depth :
881 ctx->fnbw_depth);
882 }
883 else
884 {
885 /*
886 * walks could not be merged, this means that the walk infront needs to
887 * perform different action to this one that has caught up. the one in
888 * front was scheduled first so append the new walk context to the back
889 * of the list.
890 */
891 vec_add1(fwalk->fw_ctx, *ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100892 }
893
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100894 return (FIB_NODE_BACK_WALK_MERGE);
895}
896
897/**
898 * The FIB walk's graph node virtual function table
899 */
900static const fib_node_vft_t fib_walk_vft = {
901 .fnv_get = fib_walk_get_node,
902 .fnv_last_lock = fib_walk_last_lock_gone,
903 .fnv_back_walk = fib_walk_back_walk_notify,
904};
905
906void
907fib_walk_module_init (void)
908{
909 fib_walk_priority_t prio;
910
911 FOR_EACH_FIB_WALK_PRIORITY(prio)
912 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100913 fib_walk_queues.fwqs_queues[prio].fwq_queue = fib_node_list_create();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100914 }
915
916 fib_node_register_type(FIB_NODE_TYPE_WALK, &fib_walk_vft);
917}
918
919static u8*
920format_fib_walk (u8* s, va_list ap)
921{
922 fib_node_index_t fwi = va_arg(ap, fib_node_index_t);
923 fib_walk_t *fwalk;
924
925 fwalk = fib_walk_get(fwi);
926
927 return (format(s, " parent:{%s:%d} visits:%d flags:%d",
Neale Ranns33a7dd52016-10-07 15:14:33 +0100928 fib_node_type_get_name(fwalk->fw_parent.fnp_type),
929 fwalk->fw_parent.fnp_index,
930 fwalk->fw_n_visits,
931 fwalk->fw_flags));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100932}
933
934static clib_error_t *
935fib_walk_show (vlib_main_t * vm,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100936 unformat_input_t * input,
937 vlib_cli_command_t * cmd)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100938{
939 fib_walk_queue_stats_t wqs;
940 fib_walk_priority_t prio;
941 fib_node_ptr_t sibling;
942 fib_node_index_t fwi;
943 fib_walk_t *fwalk;
Neale Ranns33a7dd52016-10-07 15:14:33 +0100944 int more_elts, ii;
945 u8 *s = NULL;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100946
Neale Ranns33a7dd52016-10-07 15:14:33 +0100947#define USEC 1000000
948 vlib_cli_output(vm, "FIB Walk Quota = %.2fusec:", quota * USEC);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100949 vlib_cli_output(vm, "FIB Walk queues:");
950
951 FOR_EACH_FIB_WALK_PRIORITY(prio)
952 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100953 vlib_cli_output(vm, " %U priority queue:",
954 format_fib_walk_priority, prio);
955 vlib_cli_output(vm, " Stats: ");
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100956
Neale Ranns33a7dd52016-10-07 15:14:33 +0100957 FOR_EACH_FIB_WALK_QUEUE_STATS(wqs)
958 {
959 vlib_cli_output(vm, " %U:%d",
960 format_fib_walk_queue_stats, wqs,
961 fib_walk_queues.fwqs_queues[prio].fwq_stats[wqs]);
962 }
963 vlib_cli_output(vm, " Occupancy:%d",
964 fib_node_list_get_size(
965 fib_walk_queues.fwqs_queues[prio].fwq_queue));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100966
Neale Ranns33a7dd52016-10-07 15:14:33 +0100967 more_elts = fib_node_list_get_front(
968 fib_walk_queues.fwqs_queues[prio].fwq_queue,
969 &sibling);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100970
Neale Ranns33a7dd52016-10-07 15:14:33 +0100971 while (more_elts)
972 {
973 ASSERT(FIB_NODE_INDEX_INVALID != sibling.fnp_index);
974 ASSERT(FIB_NODE_TYPE_WALK == sibling.fnp_type);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100975
Neale Ranns33a7dd52016-10-07 15:14:33 +0100976 fwi = sibling.fnp_index;
977 fwalk = fib_walk_get(fwi);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100978
Neale Ranns33a7dd52016-10-07 15:14:33 +0100979 vlib_cli_output(vm, " %U", format_fib_walk, fwi);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100980
Neale Ranns33a7dd52016-10-07 15:14:33 +0100981 more_elts = fib_node_list_elt_get_next(fwalk->fw_prio_sibling,
982 &sibling);
983 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100984 }
Neale Ranns33a7dd52016-10-07 15:14:33 +0100985
986 vlib_cli_output(vm, "Histogram Statistics:");
987 vlib_cli_output(vm, " Number of Elements visit per-quota:");
988 for (ii = 0; ii < N_ELTS_BUCKETS; ii++)
989 {
990 if (0 != fib_walk_work_nodes_visited[ii])
991 s = format(s, "%d:%d ",
992 (ii * fib_walk_work_nodes_visisted_incr),
993 fib_walk_work_nodes_visited[ii]);
994 }
995 vlib_cli_output(vm, " %v", s);
996 vec_free(s);
997
998 vlib_cli_output(vm, " Time consumed per-quota (Quota=%f usec):", quota*USEC);
999 s = format(s, "0:%d ", fib_walk_work_time_taken[0]);
1000 for (ii = 1; ii < N_TIME_BUCKETS; ii++)
1001 {
1002 if (0 != fib_walk_work_time_taken[ii])
1003 s = format(s, "%d:%d ", (u32)((((ii - N_TIME_BUCKETS/2) *
1004 (quota / TIME_INCREMENTS)) + quota) *
1005 USEC),
1006 fib_walk_work_time_taken[ii]);
1007 }
1008 vlib_cli_output(vm, " %v", s);
1009 vec_free(s);
1010
1011 vlib_cli_output(vm, " Sleep Types:");
1012 vlib_cli_output(vm, " Short Long:");
1013 vlib_cli_output(vm, " %d %d:",
1014 fib_walk_sleep_lengths[FIB_WALK_SHORT_SLEEP],
1015 fib_walk_sleep_lengths[FIB_WALK_LONG_SLEEP]);
1016
1017 vlib_cli_output(vm, " Number of Elements visited per-walk:");
1018 for (ii = 0; ii < HISTOGRAM_VISITS_PER_WALK_N_BUCKETS; ii++)
1019 {
1020 if (0 != fib_walk_hist_vists_per_walk[ii])
1021 s = format(s, "%d:%d ",
1022 ii*HISTOGRAM_VISITS_PER_WALK_INCR,
1023 fib_walk_hist_vists_per_walk[ii]);
1024 }
1025 vlib_cli_output(vm, " %v", s);
1026 vec_free(s);
1027
1028
1029 vlib_cli_output(vm, "Brief History (last %d walks):", HISTORY_N_WALKS);
Neale Rannsad95b5d2016-11-10 20:35:14 +00001030 ii = history_last_walk_pos - 1;
1031 if (ii < 0)
1032 ii = HISTORY_N_WALKS - 1;
1033
1034 while (ii != history_last_walk_pos)
Neale Ranns33a7dd52016-10-07 15:14:33 +01001035 {
Neale Ranns19c68d22016-12-07 15:38:14 +00001036 if (0 != fib_walk_history[ii].fwh_reason[0])
Neale Ranns33a7dd52016-10-07 15:14:33 +01001037 {
Neale Rannsad95b5d2016-11-10 20:35:14 +00001038 fib_node_back_walk_reason_t reason;
1039 u8 *s = NULL;
Neale Ranns19c68d22016-12-07 15:38:14 +00001040 u32 jj;
Neale Rannsad95b5d2016-11-10 20:35:14 +00001041
Neale Ranns8b37b872016-11-21 12:25:22 +00001042 s = format(s, "[@%d]: %s:%d visits:%d duration:%.2f completed:%.2f ",
1043 ii, fib_node_type_get_name(fib_walk_history[ii].fwh_parent.fnp_type),
Neale Rannsad95b5d2016-11-10 20:35:14 +00001044 fib_walk_history[ii].fwh_parent.fnp_index,
1045 fib_walk_history[ii].fwh_n_visits,
Neale Ranns8b37b872016-11-21 12:25:22 +00001046 fib_walk_history[ii].fwh_duration,
1047 fib_walk_history[ii].fwh_completed);
Neale Rannsad95b5d2016-11-10 20:35:14 +00001048 if (FIB_WALK_FLAG_SYNC & fib_walk_history[ii].fwh_flags)
1049 s = format(s, "sync, ");
1050 if (FIB_WALK_FLAG_ASYNC & fib_walk_history[ii].fwh_flags)
1051 s = format(s, "async, ");
1052
1053 s = format(s, "reason:");
Neale Ranns19c68d22016-12-07 15:38:14 +00001054 jj = 0;
1055 while (0 != fib_walk_history[ii].fwh_reason[jj])
1056 {
1057 FOR_EACH_FIB_NODE_BW_REASON(reason) {
1058 if ((1<<reason) & fib_walk_history[ii].fwh_reason[jj]) {
1059 s = format (s, "%s,", fib_node_bw_reason_names[reason]);
1060 }
Neale Rannsad95b5d2016-11-10 20:35:14 +00001061 }
Neale Ranns19c68d22016-12-07 15:38:14 +00001062 jj++;
Neale Rannsad95b5d2016-11-10 20:35:14 +00001063 }
1064 vlib_cli_output(vm, "%v", s);
Neale Ranns33a7dd52016-10-07 15:14:33 +01001065 }
1066
Neale Rannsad95b5d2016-11-10 20:35:14 +00001067 ii--;
1068 if (ii < 0)
1069 ii = HISTORY_N_WALKS - 1;
1070 }
Neale Ranns33a7dd52016-10-07 15:14:33 +01001071
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001072 return (NULL);
1073}
1074
1075VLIB_CLI_COMMAND (fib_walk_show_command, static) = {
1076 .path = "show fib walk",
1077 .short_help = "show fib walk",
1078 .function = fib_walk_show,
1079};
Neale Ranns33a7dd52016-10-07 15:14:33 +01001080
1081static clib_error_t *
1082fib_walk_set_quota (vlib_main_t * vm,
1083 unformat_input_t * input,
1084 vlib_cli_command_t * cmd)
1085{
1086 clib_error_t * error = NULL;
1087 f64 new_quota;
1088
1089 if (unformat (input, "%f", &new_quota))
1090 {
1091 quota = new_quota;
1092 }
1093 else
1094 {
1095 error = clib_error_return(0 , "Pass a float value");
1096 }
1097
1098 return (error);
1099}
1100
1101VLIB_CLI_COMMAND (fib_walk_set_quota_command, static) = {
1102 .path = "set fib walk quota",
1103 .short_help = "set fib walk quota",
1104 .function = fib_walk_set_quota,
1105};
1106
1107static clib_error_t *
1108fib_walk_set_histogram_elements_size (vlib_main_t * vm,
1109 unformat_input_t * input,
1110 vlib_cli_command_t * cmd)
1111{
1112 clib_error_t * error = NULL;
1113 u32 new;
1114
1115 if (unformat (input, "%d", &new))
1116 {
1117 fib_walk_work_nodes_visisted_incr = new;
1118 }
1119 else
1120 {
1121 error = clib_error_return(0 , "Pass an int value");
1122 }
1123
1124 return (error);
1125}
1126
1127VLIB_CLI_COMMAND (fib_walk_set_histogram_elements_size_command, static) = {
1128 .path = "set fib walk histogram elements size",
1129 .short_help = "set fib walk histogram elements size",
1130 .function = fib_walk_set_histogram_elements_size,
1131};
1132
1133static clib_error_t *
1134fib_walk_clear (vlib_main_t * vm,
1135 unformat_input_t * input,
1136 vlib_cli_command_t * cmd)
1137{
1138 memset(fib_walk_hist_vists_per_walk, 0, sizeof(fib_walk_hist_vists_per_walk));
1139 memset(fib_walk_history, 0, sizeof(fib_walk_history));
1140 memset(fib_walk_work_time_taken, 0, sizeof(fib_walk_work_time_taken));
1141 memset(fib_walk_work_nodes_visited, 0, sizeof(fib_walk_work_nodes_visited));
1142 memset(fib_walk_sleep_lengths, 0, sizeof(fib_walk_sleep_lengths));
1143
1144 return (NULL);
1145}
1146
1147VLIB_CLI_COMMAND (fib_walk_clear_command, static) = {
1148 .path = "clear fib walk",
1149 .short_help = "clear fib walk",
1150 .function = fib_walk_clear,
1151};
Neale Rannsf12a83f2017-04-18 09:09:40 -07001152
1153void
1154fib_walk_process_enable (void)
1155{
1156 vlib_process_signal_event(vlib_get_main(),
1157 fib_walk_process_node.index,
1158 FIB_WALK_PROCESS_EVENT_ENABLE,
1159 0);
1160}
1161
1162void
1163fib_walk_process_disable (void)
1164{
1165 vlib_process_signal_event(vlib_get_main(),
1166 fib_walk_process_node.index,
1167 FIB_WALK_PROCESS_EVENT_DISABLE,
1168 0);
1169}
1170
1171static clib_error_t *
1172fib_walk_process_enable_disable (vlib_main_t * vm,
1173 unformat_input_t * input,
1174 vlib_cli_command_t * cmd)
1175{
1176 if (unformat (input, "enable"))
1177 {
1178 fib_walk_process_enable();
1179 }
1180 else if (unformat (input, "disable"))
1181 {
1182 fib_walk_process_disable();
1183 }
1184 else
1185 {
1186 return clib_error_return(0, "choose enable or disable");
1187 }
1188 return (NULL);
1189}
1190
1191VLIB_CLI_COMMAND (fib_walk_process_command, static) = {
1192 .path = "test fib-walk-process",
1193 .short_help = "test fib-walk-process [enable|disable]",
1194 .function = fib_walk_process_enable_disable,
1195};