blob: 1771047a6b4017d5889faeca77d544bc0e1f7aa3 [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/**
99 * @brief There's only one event type sent to the walk process
100 */
101#define FIB_WALK_EVENT 0
102
103/**
104 * Statistics maintained per-walk queue
105 */
106typedef enum fib_walk_queue_stats_t_
107{
108 FIB_WALK_SCHEDULED,
109 FIB_WALK_COMPLETED,
110} fib_walk_queue_stats_t;
111#define FIB_WALK_QUEUE_STATS_NUM (FIB_WALK_COMPLETED+1)
112
113#define FIB_WALK_QUEUE_STATS { \
114 [FIB_WALK_SCHEDULED] = "scheduled", \
115 [FIB_WALK_COMPLETED] = "completed", \
116}
117
118#define FOR_EACH_FIB_WALK_QUEUE_STATS(_wqs) \
119 for ((_wqs) = FIB_WALK_SCHEDULED; \
Neale Ranns33a7dd52016-10-07 15:14:33 +0100120 (_wqs) < FIB_WALK_QUEUE_STATS_NUM; \
121 (_wqs)++)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100122
123/**
124 * The names of the walk stats
125 */
126static const char * const fib_walk_queue_stats_names[] = FIB_WALK_QUEUE_STATS;
127
128/**
129 * A represenation of one queue of walk
130 */
131typedef struct fib_walk_queue_t_
132{
133 /**
134 * Qeuee stats
135 */
136 u64 fwq_stats[FIB_WALK_QUEUE_STATS_NUM];
137
138 /**
139 * The node list which acts as the queue
140 */
141 fib_node_list_t fwq_queue;
142} fib_walk_queue_t;
143
144/**
145 * A set of priority queues for outstanding walks
146 */
147typedef struct fib_walk_queues_t_
148{
149 fib_walk_queue_t fwqs_queues[FIB_WALK_PRIORITY_NUM];
150} fib_walk_queues_t;
151
152/**
153 * The global queues of outstanding walks
154 */
155static fib_walk_queues_t fib_walk_queues;
156
157/**
158 * The names of the walk priorities
159 */
160static const char * const fib_walk_priority_names[] = FIB_WALK_PRIORITIES;
161
Neale Ranns33a7dd52016-10-07 15:14:33 +0100162/**
163 * @brief Histogram stats on the lenths of each walk in elemenets visisted.
164 * Store upto 1<<23 elements in increments of 1<<10
165 */
166#define HISTOGRAM_VISITS_PER_WALK_MAX (1<<23)
167#define HISTOGRAM_VISITS_PER_WALK_INCR (1<<10)
168#define HISTOGRAM_VISITS_PER_WALK_N_BUCKETS \
169 (HISTOGRAM_VISITS_PER_WALK_MAX/HISTOGRAM_VISITS_PER_WALK_INCR)
170static u64 fib_walk_hist_vists_per_walk[HISTOGRAM_VISITS_PER_WALK_N_BUCKETS];
171
172/**
173 * @brief History of state for the last 128 walks
174 */
175#define HISTORY_N_WALKS 128
176static u32 history_last_walk_pos;
177typedef struct fib_walk_history_t_ {
178 u32 fwh_n_visits;
179 f64 fwh_duration;
180 fib_node_ptr_t fwh_parent;
181} fib_walk_history_t;
182static fib_walk_history_t fib_walk_history[HISTORY_N_WALKS];
183
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100184u8*
185format_fib_walk_priority (u8 *s, va_list ap)
186{
187 fib_walk_priority_t prio = va_arg(ap, fib_walk_priority_t);
188
189 ASSERT(prio < FIB_WALK_PRIORITY_NUM);
190
191 return (format(s, "%s", fib_walk_priority_names[prio]));
192}
193static u8*
194format_fib_walk_queue_stats (u8 *s, va_list ap)
195{
196 fib_walk_queue_stats_t wqs = va_arg(ap, fib_walk_queue_stats_t);
197
198 ASSERT(wqs < FIB_WALK_QUEUE_STATS_NUM);
199
200 return (format(s, "%s", fib_walk_queue_stats_names[wqs]));
201}
202
203static index_t
204fib_walk_get_index (fib_walk_t *fwalk)
205{
206 return (fwalk - fib_walk_pool);
207}
208
209static fib_walk_t *
210fib_walk_get (index_t fwi)
211{
212 return (pool_elt_at_index(fib_walk_pool, fwi));
213}
214
215/*
216 * not static so it can be used in the unit tests
217 */
218u32
219fib_walk_queue_get_size (fib_walk_priority_t prio)
220{
221 return (fib_node_list_get_size(fib_walk_queues.fwqs_queues[prio].fwq_queue));
222}
223
224static fib_node_index_t
225fib_walk_queue_get_front (fib_walk_priority_t prio)
226{
227 fib_node_ptr_t wp;
228
229 fib_node_list_get_front(fib_walk_queues.fwqs_queues[prio].fwq_queue, &wp);
230
231 return (wp.fnp_index);
232}
233
234static void
235fib_walk_destroy (fib_walk_t *fwalk)
236{
Neale Ranns33a7dd52016-10-07 15:14:33 +0100237 u32 bucket;
238
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100239 if (FIB_NODE_INDEX_INVALID != fwalk->fw_prio_sibling)
240 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100241 fib_node_list_elt_remove(fwalk->fw_prio_sibling);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100242 }
243 fib_node_child_remove(fwalk->fw_parent.fnp_type,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100244 fwalk->fw_parent.fnp_index,
245 fwalk->fw_dep_sibling);
246
247 /*
248 * add the stats to the continuous histogram collection.
249 */
250 bucket = (fwalk->fw_n_visits / HISTOGRAM_VISITS_PER_WALK_INCR);
Neale Ranns5899fde2016-10-12 13:51:05 +0100251 bucket = (bucket > HISTOGRAM_VISITS_PER_WALK_N_BUCKETS ?
252 HISTOGRAM_VISITS_PER_WALK_N_BUCKETS - 1 :
Neale Ranns33a7dd52016-10-07 15:14:33 +0100253 bucket);
254 fib_walk_hist_vists_per_walk[bucket]++;
255
256 /*
257 * save stats to the recent history
258 */
259
260 fib_walk_history[history_last_walk_pos].fwh_n_visits =
261 fwalk->fw_n_visits;
262 fib_walk_history[history_last_walk_pos].fwh_duration =
263 vlib_time_now(vlib_get_main()) - fwalk->fw_start_time;
264 fib_walk_history[history_last_walk_pos].fwh_parent =
265 fwalk->fw_parent;
266
267 history_last_walk_pos = (history_last_walk_pos + 1) % HISTORY_N_WALKS;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100268
269 fib_node_deinit(&fwalk->fw_node);
270 pool_put(fib_walk_pool, fwalk);
271}
272
273/**
274 * return code when advancing a walk
275 */
276typedef enum fib_walk_advance_rc_t_
277{
278 /**
279 * The walk is complete
280 */
281 FIB_WALK_ADVANCE_DONE,
282 /**
283 * the walk has more work
284 */
285 FIB_WALK_ADVANCE_MORE,
286 /**
287 * The walk merged with the one in front
288 */
289 FIB_WALK_ADVANCE_MERGE,
290} fib_walk_advance_rc_t;
291
292/**
293 * @brief Advance the walk one element in its work list
294 */
295static fib_walk_advance_rc_t
296fib_walk_advance (fib_node_index_t fwi)
297{
298 fib_node_back_walk_ctx_t *ctx;
299 fib_node_back_walk_rc_t wrc;
300 fib_node_ptr_t sibling;
301 fib_walk_t *fwalk;
302 int more_elts;
303
304 /*
305 * this walk function is re-entrant - walks acan spawn walks.
Neale Ranns33a7dd52016-10-07 15:14:33 +0100306 * fib_walk_t objects come from a pool, so they can realloc. we need
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100307 * to retch from said pool at the appropriate times.
308 */
309 fwalk = fib_walk_get(fwi);
310
311 more_elts = fib_node_list_elt_get_next(fwalk->fw_dep_sibling, &sibling);
312
313 if (more_elts)
314 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100315 vec_foreach(ctx, fwalk->fw_ctx)
316 {
317 wrc = fib_node_back_walk_one(&sibling, ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100318
Neale Ranns33a7dd52016-10-07 15:14:33 +0100319 fwalk = fib_walk_get(fwi);
320 fwalk->fw_n_visits++;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100321
Neale Ranns33a7dd52016-10-07 15:14:33 +0100322 if (FIB_NODE_BACK_WALK_MERGE == wrc)
323 {
324 /*
325 * this walk has merged with the one further along the node's
326 * dependecy list.
327 */
328 return (FIB_WALK_ADVANCE_MERGE);
329 }
330 }
331 /*
332 * move foward to the next node to visit
333 */
334 more_elts = fib_node_list_advance(fwalk->fw_dep_sibling);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100335 }
336
337 if (more_elts)
338 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100339 return (FIB_WALK_ADVANCE_MORE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100340 }
341
342 return (FIB_WALK_ADVANCE_DONE);
343}
344
345/**
Neale Ranns33a7dd52016-10-07 15:14:33 +0100346 * @breif Enurmerate the times of sleep between walks
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100347 */
Neale Ranns33a7dd52016-10-07 15:14:33 +0100348typedef enum fib_walk_sleep_type_t_
349{
350 FIB_WALK_SHORT_SLEEP,
351 FIB_WALK_LONG_SLEEP,
352} fib_walk_sleep_type_t;
353
354#define FIB_WALK_N_SLEEP (FIB_WALK_LONG_SLEEP+1)
355
356/**
357 * @brief Durations for the sleep types
358 */
359static f64 fib_walk_sleep_duration[] = {
360 [FIB_WALK_LONG_SLEEP] = 1e-3,
361 [FIB_WALK_SHORT_SLEEP] = 1e-8,
362};
363
364/**
365 * @brief The time quota for a walk. When more than this amount of time is
366 * spent, the walk process will yield.
367 */
368static f64 quota = 1e-4;
369
370/**
371 * Histogram on the amount of work done (in msecs) in each walk
372 */
373#define N_TIME_BUCKETS 128
374#define TIME_INCREMENTS (N_TIME_BUCKETS/2)
375static u64 fib_walk_work_time_taken[N_TIME_BUCKETS];
376
377/**
378 * Histogram on the number of nodes visted in each quota
379 */
380#define N_ELTS_BUCKETS 128
381static u32 fib_walk_work_nodes_visisted_incr = 2;
382static u64 fib_walk_work_nodes_visited[N_ELTS_BUCKETS];
383
384/**
385 * Histogram of the sleep lengths
386 */
387static u64 fib_walk_sleep_lengths[2];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100388
389/**
390 * @brief Service the queues
391 * This is not declared static so that it can be unit tested - i know i know...
392 */
393f64
394fib_walk_process_queues (vlib_main_t * vm,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100395 const f64 quota)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100396{
Neale Ranns33a7dd52016-10-07 15:14:33 +0100397 f64 start_time, consumed_time;
398 fib_walk_sleep_type_t sleep;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100399 fib_walk_priority_t prio;
400 fib_walk_advance_rc_t rc;
401 fib_node_index_t fwi;
402 fib_walk_t *fwalk;
Neale Ranns33a7dd52016-10-07 15:14:33 +0100403 u32 n_elts;
404 i32 bucket;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100405
Neale Ranns33a7dd52016-10-07 15:14:33 +0100406 consumed_time = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100407 start_time = vlib_time_now(vm);
Neale Ranns33a7dd52016-10-07 15:14:33 +0100408 n_elts = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100409
410 FOR_EACH_FIB_WALK_PRIORITY(prio)
411 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100412 while (0 != fib_walk_queue_get_size(prio))
413 {
414 fwi = fib_walk_queue_get_front(prio);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100415
Neale Ranns33a7dd52016-10-07 15:14:33 +0100416 /*
417 * set this walk as executing
418 */
419 fwalk = fib_walk_get(fwi);
420 fwalk->fw_flags |= FIB_WALK_FLAG_EXECUTING;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100421
Neale Ranns33a7dd52016-10-07 15:14:33 +0100422 do
423 {
424 rc = fib_walk_advance(fwi);
425 n_elts++;
426 consumed_time = (vlib_time_now(vm) - start_time);
427 } while ((consumed_time < quota) &&
428 (FIB_WALK_ADVANCE_MORE == rc));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100429
Neale Ranns33a7dd52016-10-07 15:14:33 +0100430 /*
431 * if this walk has no more work then pop it from the queue
432 * and move on to the next.
433 */
434 if (FIB_WALK_ADVANCE_MORE != rc)
435 {
436 fwalk = fib_walk_get(fwi);
437 fib_walk_destroy(fwalk);
438 fib_walk_queues.fwqs_queues[prio].fwq_stats[FIB_WALK_COMPLETED]++;
439 }
440 else
441 {
442 /*
443 * passed our work quota. sleep time.
444 */
445 fwalk = fib_walk_get(fwi);
446 fwalk->fw_flags &= ~FIB_WALK_FLAG_EXECUTING;
447 sleep = FIB_WALK_SHORT_SLEEP;
448 goto that_will_do_for_now;
449 }
450 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100451 }
452 /*
453 * got to the end of all the work
454 */
Neale Ranns33a7dd52016-10-07 15:14:33 +0100455 sleep = FIB_WALK_LONG_SLEEP;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100456
457that_will_do_for_now:
Neale Ranns33a7dd52016-10-07 15:14:33 +0100458
459 /*
460 * collect the stats:
461 * - for the number of nodes visisted we store 128 increments
462 * - for the time consumed we store quota/TIME_INCREMENTS increments.
463 */
464 bucket = ((n_elts/fib_walk_work_nodes_visisted_incr) > N_ELTS_BUCKETS ?
465 N_ELTS_BUCKETS-1 :
466 n_elts/fib_walk_work_nodes_visisted_incr);
467 ++fib_walk_work_nodes_visited[bucket];
468
469 bucket = (consumed_time - quota) / (quota / TIME_INCREMENTS);
470 bucket += N_TIME_BUCKETS/2;
471 bucket = (bucket < 0 ? 0 : bucket);
472 bucket = (bucket > N_TIME_BUCKETS-1 ? N_TIME_BUCKETS-1 : bucket);
473 ++fib_walk_work_time_taken[bucket];
474
475 ++fib_walk_sleep_lengths[sleep];
476
477 return (fib_walk_sleep_duration[sleep]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100478}
479
480/**
481 * @brief The 'fib-walk' process's main loop.
482 */
483static uword
484fib_walk_process (vlib_main_t * vm,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100485 vlib_node_runtime_t * node,
486 vlib_frame_t * f)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100487{
488 f64 sleep_time;
489
Neale Ranns33a7dd52016-10-07 15:14:33 +0100490 sleep_time = fib_walk_sleep_duration[FIB_WALK_SHORT_SLEEP];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100491
492 while (1)
493 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100494 vlib_process_wait_for_event_or_clock(vm, sleep_time);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100495
Neale Ranns33a7dd52016-10-07 15:14:33 +0100496 /*
497 * there may be lots of event queued between the processes,
498 * but the walks we want to schedule are in the priority queues,
499 * so we ignore the process events.
500 */
501 vlib_process_get_events(vm, NULL);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100502
Neale Ranns33a7dd52016-10-07 15:14:33 +0100503 sleep_time = fib_walk_process_queues(vm, quota);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100504 }
505
506 /*
507 * Unreached
508 */
509 ASSERT(!"WTF");
510 return 0;
511}
512
513/* *INDENT-OFF* */
514VLIB_REGISTER_NODE (fib_walk_process_node,static) = {
515 .function = fib_walk_process,
516 .type = VLIB_NODE_TYPE_PROCESS,
517 .name = "fib-walk",
518};
519/* *INDENT-ON* */
520
521/**
522 * @brief Allocate a new walk object
Neale Ranns33a7dd52016-10-07 15:14:33 +0100523 */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100524static fib_walk_t *
525fib_walk_alloc (fib_node_type_t parent_type,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100526 fib_node_index_t parent_index,
527 fib_walk_flags_t flags,
528 fib_node_back_walk_ctx_t *ctx)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100529{
530 fib_walk_t *fwalk;
531
532 pool_get(fib_walk_pool, fwalk);
533
534 fib_node_init(&fwalk->fw_node, FIB_NODE_TYPE_WALK);
535
536 fwalk->fw_flags = flags;
537 fwalk->fw_dep_sibling = FIB_NODE_INDEX_INVALID;
538 fwalk->fw_prio_sibling = FIB_NODE_INDEX_INVALID;
539 fwalk->fw_parent.fnp_index = parent_index;
540 fwalk->fw_parent.fnp_type = parent_type;
541 fwalk->fw_ctx = NULL;
Neale Ranns33a7dd52016-10-07 15:14:33 +0100542 fwalk->fw_start_time = vlib_time_now(vlib_get_main());
543 fwalk->fw_n_visits = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100544
545 /*
546 * make a copy of the backwalk context so the depth count remains
547 * the same for each sibling visitsed. This is important in the case
548 * where a parents has a loop via one child, but all the others are not.
549 * if the looped child were visited first, the depth count would exceed, the
550 * max and the walk would terminate before it reached the other siblings.
551 */
552 vec_add1(fwalk->fw_ctx, *ctx);
553
554 return (fwalk);
555}
556
557/**
558 * @brief Enqueue a walk onto the appropriate priority queue. Then signal
559 * the background process there is work to do.
560 */
561static index_t
562fib_walk_prio_queue_enquue (fib_walk_priority_t prio,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100563 fib_walk_t *fwalk)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100564{
565 index_t sibling;
566
567 sibling = fib_node_list_push_front(fib_walk_queues.fwqs_queues[prio].fwq_queue,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100568 0,
569 FIB_NODE_TYPE_WALK,
570 fib_walk_get_index(fwalk));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100571 fib_walk_queues.fwqs_queues[prio].fwq_stats[FIB_WALK_SCHEDULED]++;
572
573 /*
574 * poke the fib-walk process to perform the async walk.
575 * we are not passing it specific data, hence the last two args,
576 * the process will drain the queues
577 */
578 vlib_process_signal_event(vlib_get_main(),
Neale Ranns33a7dd52016-10-07 15:14:33 +0100579 fib_walk_process_node.index,
580 FIB_WALK_EVENT,
581 FIB_WALK_EVENT);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100582
583 return (sibling);
584}
585
586void
587fib_walk_async (fib_node_type_t parent_type,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100588 fib_node_index_t parent_index,
589 fib_walk_priority_t prio,
590 fib_node_back_walk_ctx_t *ctx)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100591{
592 fib_walk_t *fwalk;
593
594 if (FIB_NODE_GRAPH_MAX_DEPTH < ++ctx->fnbw_depth)
595 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100596 /*
597 * The walk has reached the maximum depth. there is a loop in the graph.
598 * bail.
599 */
600 return;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100601 }
602
603 fwalk = fib_walk_alloc(parent_type,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100604 parent_index,
605 FIB_WALK_FLAG_ASYNC,
606 ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100607
608 fwalk->fw_dep_sibling = fib_node_child_add(parent_type,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100609 parent_index,
610 FIB_NODE_TYPE_WALK,
611 fib_walk_get_index(fwalk));
612
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100613 fwalk->fw_prio_sibling = fib_walk_prio_queue_enquue(prio, fwalk);
614}
615
616/**
617 * @brief Back walk all the children of a FIB node.
618 *
619 * note this is a synchronous depth first walk. Children visited may propagate
620 * the walk to thier children. Other children node types may not propagate,
621 * synchronously but instead queue the walk for later async completion.
622 */
623void
624fib_walk_sync (fib_node_type_t parent_type,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100625 fib_node_index_t parent_index,
626 fib_node_back_walk_ctx_t *ctx)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100627{
628 fib_walk_advance_rc_t rc;
629 fib_node_index_t fwi;
630 fib_walk_t *fwalk;
631
632 if (FIB_NODE_GRAPH_MAX_DEPTH < ++ctx->fnbw_depth)
633 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100634 /*
635 * The walk has reached the maximum depth. there is a loop in the graph.
636 * bail.
637 */
638 return;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100639 }
640
641 fwalk = fib_walk_alloc(parent_type,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100642 parent_index,
643 FIB_WALK_FLAG_SYNC,
644 ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100645
646 fwalk->fw_dep_sibling = fib_node_child_add(parent_type,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100647 parent_index,
648 FIB_NODE_TYPE_WALK,
649 fib_walk_get_index(fwalk));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100650 fwi = fib_walk_get_index(fwalk);
651
652 while (1)
653 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100654 /*
655 * set this walk as executing
656 */
657 fwalk->fw_flags |= FIB_WALK_FLAG_EXECUTING;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100658
Neale Ranns33a7dd52016-10-07 15:14:33 +0100659 do
660 {
661 rc = fib_walk_advance(fwi);
662 } while (FIB_WALK_ADVANCE_MORE == rc);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100663
664
Neale Ranns33a7dd52016-10-07 15:14:33 +0100665 /*
666 * this walk function is re-entrant - walks can spawn walks.
667 * fib_walk_t objects come from a pool, so they can realloc. we need
668 * to re-fetch from said pool at the appropriate times.
669 */
670 fwalk = fib_walk_get(fwi);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100671
Neale Ranns33a7dd52016-10-07 15:14:33 +0100672 if (FIB_WALK_ADVANCE_MERGE == rc)
673 {
674 /*
675 * this sync walk merged with an walk in front.
676 * by reqeusting a sync walk the client wanted all children walked,
677 * so we ditch the walk object in hand and continue with the one
678 * we merged into
679 */
680 fib_node_ptr_t merged_walk;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100681
Neale Ranns33a7dd52016-10-07 15:14:33 +0100682 fib_node_list_elt_get_next(fwalk->fw_dep_sibling, &merged_walk);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100683
Neale Ranns33a7dd52016-10-07 15:14:33 +0100684 ASSERT(FIB_NODE_INDEX_INVALID != merged_walk.fnp_index);
685 ASSERT(FIB_NODE_TYPE_WALK == merged_walk.fnp_type);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100686
Neale Ranns33a7dd52016-10-07 15:14:33 +0100687 fib_walk_destroy(fwalk);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100688
Neale Ranns33a7dd52016-10-07 15:14:33 +0100689 fwi = merged_walk.fnp_index;
690 fwalk = fib_walk_get(fwi);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100691
Neale Ranns33a7dd52016-10-07 15:14:33 +0100692 if (FIB_WALK_FLAG_EXECUTING & fwalk->fw_flags)
693 {
694 /*
695 * we are executing a sync walk, and we have met with another
696 * walk that is also executing. since only one walk executs at once
697 * (there is no multi-threading) this implies we have met ourselves
698 * and hence the is a loop in the graph.
699 * This function is re-entrant, so the walk object we met is being
700 * acted on in a stack frame below this one. We must therefore not
701 * continue with it now, but let the stack unwind and along the
702 * appropriate frame to read the depth count and bail.
703 */
704 fwalk = NULL;
705 break;
706 }
707 }
708 else
709 {
710 /*
711 * the walk reached the end of the depdency list.
712 */
713 break;
714 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100715 }
716
717 if (NULL != fwalk)
718 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100719 fib_walk_destroy(fwalk);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100720 }
721}
722
723static fib_node_t *
724fib_walk_get_node (fib_node_index_t index)
725{
726 fib_walk_t *fwalk;
727
728 fwalk = fib_walk_get(index);
729
730 return (&(fwalk->fw_node));
731}
732
733/**
734 * Walk objects are not parents, nor are they locked.
735 * are no-ops
736 */
737static void
738fib_walk_last_lock_gone (fib_node_t *node)
739{
740 ASSERT(0);
741}
742
743static fib_walk_t*
744fib_walk_get_from_node (fib_node_t *node)
745{
746 return ((fib_walk_t*)(((char*)node) -
Neale Ranns33a7dd52016-10-07 15:14:33 +0100747 STRUCT_OFFSET_OF(fib_walk_t, fw_node)));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100748}
749
750/**
751 * @brief Another back walk has reach this walk.
752 * Megre them so there is only one left. It is this node being
753 * visited that will remain, so copy or merge the context onto it.
754 */
755static fib_node_back_walk_rc_t
756fib_walk_back_walk_notify (fib_node_t *node,
757 fib_node_back_walk_ctx_t *ctx)
758{
759 fib_node_back_walk_ctx_t *old;
760 fib_walk_t *fwalk;
761
762 fwalk = fib_walk_get_from_node(node);
763
764 /*
765 * check whether the walk context can be merge with another,
766 * or whether it needs to be appended.
767 */
768 vec_foreach(old, fwalk->fw_ctx)
769 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100770 /*
771 * we can merge walks if the reason for the walk is the same.
772 */
773 if (old->fnbw_reason == ctx->fnbw_reason)
774 {
775 /*
776 * copy the largest of the depth values. in the presence of a loop,
777 * the same walk will merge with itself. if we take the smaller depth
778 * then it will never end.
779 */
780 old->fnbw_depth = ((old->fnbw_depth >= ctx->fnbw_depth) ?
781 old->fnbw_depth :
782 ctx->fnbw_depth);
783 goto out;
784 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100785 }
786
787 /*
788 * walks could not be merged, this means that the walk infront needs to
789 * perform different action to this one that has caught up. the one in front
790 * was scheduled first so append the new walk context to the back of the list.
791 */
792 vec_add1(fwalk->fw_ctx, *ctx);
793
794out:
795 return (FIB_NODE_BACK_WALK_MERGE);
796}
797
798/**
799 * The FIB walk's graph node virtual function table
800 */
801static const fib_node_vft_t fib_walk_vft = {
802 .fnv_get = fib_walk_get_node,
803 .fnv_last_lock = fib_walk_last_lock_gone,
804 .fnv_back_walk = fib_walk_back_walk_notify,
805};
806
807void
808fib_walk_module_init (void)
809{
810 fib_walk_priority_t prio;
811
812 FOR_EACH_FIB_WALK_PRIORITY(prio)
813 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100814 fib_walk_queues.fwqs_queues[prio].fwq_queue = fib_node_list_create();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100815 }
816
817 fib_node_register_type(FIB_NODE_TYPE_WALK, &fib_walk_vft);
818}
819
820static u8*
821format_fib_walk (u8* s, va_list ap)
822{
823 fib_node_index_t fwi = va_arg(ap, fib_node_index_t);
824 fib_walk_t *fwalk;
825
826 fwalk = fib_walk_get(fwi);
827
828 return (format(s, " parent:{%s:%d} visits:%d flags:%d",
Neale Ranns33a7dd52016-10-07 15:14:33 +0100829 fib_node_type_get_name(fwalk->fw_parent.fnp_type),
830 fwalk->fw_parent.fnp_index,
831 fwalk->fw_n_visits,
832 fwalk->fw_flags));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100833}
834
835static clib_error_t *
836fib_walk_show (vlib_main_t * vm,
Neale Ranns33a7dd52016-10-07 15:14:33 +0100837 unformat_input_t * input,
838 vlib_cli_command_t * cmd)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100839{
840 fib_walk_queue_stats_t wqs;
841 fib_walk_priority_t prio;
842 fib_node_ptr_t sibling;
843 fib_node_index_t fwi;
844 fib_walk_t *fwalk;
Neale Ranns33a7dd52016-10-07 15:14:33 +0100845 int more_elts, ii;
846 u8 *s = NULL;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100847
Neale Ranns33a7dd52016-10-07 15:14:33 +0100848#define USEC 1000000
849 vlib_cli_output(vm, "FIB Walk Quota = %.2fusec:", quota * USEC);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100850 vlib_cli_output(vm, "FIB Walk queues:");
851
852 FOR_EACH_FIB_WALK_PRIORITY(prio)
853 {
Neale Ranns33a7dd52016-10-07 15:14:33 +0100854 vlib_cli_output(vm, " %U priority queue:",
855 format_fib_walk_priority, prio);
856 vlib_cli_output(vm, " Stats: ");
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100857
Neale Ranns33a7dd52016-10-07 15:14:33 +0100858 FOR_EACH_FIB_WALK_QUEUE_STATS(wqs)
859 {
860 vlib_cli_output(vm, " %U:%d",
861 format_fib_walk_queue_stats, wqs,
862 fib_walk_queues.fwqs_queues[prio].fwq_stats[wqs]);
863 }
864 vlib_cli_output(vm, " Occupancy:%d",
865 fib_node_list_get_size(
866 fib_walk_queues.fwqs_queues[prio].fwq_queue));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100867
Neale Ranns33a7dd52016-10-07 15:14:33 +0100868 more_elts = fib_node_list_get_front(
869 fib_walk_queues.fwqs_queues[prio].fwq_queue,
870 &sibling);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100871
Neale Ranns33a7dd52016-10-07 15:14:33 +0100872 while (more_elts)
873 {
874 ASSERT(FIB_NODE_INDEX_INVALID != sibling.fnp_index);
875 ASSERT(FIB_NODE_TYPE_WALK == sibling.fnp_type);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100876
Neale Ranns33a7dd52016-10-07 15:14:33 +0100877 fwi = sibling.fnp_index;
878 fwalk = fib_walk_get(fwi);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100879
Neale Ranns33a7dd52016-10-07 15:14:33 +0100880 vlib_cli_output(vm, " %U", format_fib_walk, fwi);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100881
Neale Ranns33a7dd52016-10-07 15:14:33 +0100882 more_elts = fib_node_list_elt_get_next(fwalk->fw_prio_sibling,
883 &sibling);
884 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100885 }
Neale Ranns33a7dd52016-10-07 15:14:33 +0100886
887 vlib_cli_output(vm, "Histogram Statistics:");
888 vlib_cli_output(vm, " Number of Elements visit per-quota:");
889 for (ii = 0; ii < N_ELTS_BUCKETS; ii++)
890 {
891 if (0 != fib_walk_work_nodes_visited[ii])
892 s = format(s, "%d:%d ",
893 (ii * fib_walk_work_nodes_visisted_incr),
894 fib_walk_work_nodes_visited[ii]);
895 }
896 vlib_cli_output(vm, " %v", s);
897 vec_free(s);
898
899 vlib_cli_output(vm, " Time consumed per-quota (Quota=%f usec):", quota*USEC);
900 s = format(s, "0:%d ", fib_walk_work_time_taken[0]);
901 for (ii = 1; ii < N_TIME_BUCKETS; ii++)
902 {
903 if (0 != fib_walk_work_time_taken[ii])
904 s = format(s, "%d:%d ", (u32)((((ii - N_TIME_BUCKETS/2) *
905 (quota / TIME_INCREMENTS)) + quota) *
906 USEC),
907 fib_walk_work_time_taken[ii]);
908 }
909 vlib_cli_output(vm, " %v", s);
910 vec_free(s);
911
912 vlib_cli_output(vm, " Sleep Types:");
913 vlib_cli_output(vm, " Short Long:");
914 vlib_cli_output(vm, " %d %d:",
915 fib_walk_sleep_lengths[FIB_WALK_SHORT_SLEEP],
916 fib_walk_sleep_lengths[FIB_WALK_LONG_SLEEP]);
917
918 vlib_cli_output(vm, " Number of Elements visited per-walk:");
919 for (ii = 0; ii < HISTOGRAM_VISITS_PER_WALK_N_BUCKETS; ii++)
920 {
921 if (0 != fib_walk_hist_vists_per_walk[ii])
922 s = format(s, "%d:%d ",
923 ii*HISTOGRAM_VISITS_PER_WALK_INCR,
924 fib_walk_hist_vists_per_walk[ii]);
925 }
926 vlib_cli_output(vm, " %v", s);
927 vec_free(s);
928
929
930 vlib_cli_output(vm, "Brief History (last %d walks):", HISTORY_N_WALKS);
931 ii = history_last_walk_pos;
932 do
933 {
934 if (0 != fib_walk_history[ii].fwh_n_visits)
935 {
936 vlib_cli_output(
937 vm, " %s:%d visits:%d duration:%.2f ",
938 fib_node_type_get_name(fib_walk_history[ii].fwh_parent.fnp_type),
939 fib_walk_history[ii].fwh_parent.fnp_index,
940 fib_walk_history[ii].fwh_n_visits,
941 fib_walk_history[ii].fwh_duration);
942 }
943
944 ii = (ii + 1) % HISTORY_N_WALKS;
945 } while (ii != history_last_walk_pos);
946
947
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100948 return (NULL);
949}
950
951VLIB_CLI_COMMAND (fib_walk_show_command, static) = {
952 .path = "show fib walk",
953 .short_help = "show fib walk",
954 .function = fib_walk_show,
955};
Neale Ranns33a7dd52016-10-07 15:14:33 +0100956
957static clib_error_t *
958fib_walk_set_quota (vlib_main_t * vm,
959 unformat_input_t * input,
960 vlib_cli_command_t * cmd)
961{
962 clib_error_t * error = NULL;
963 f64 new_quota;
964
965 if (unformat (input, "%f", &new_quota))
966 {
967 quota = new_quota;
968 }
969 else
970 {
971 error = clib_error_return(0 , "Pass a float value");
972 }
973
974 return (error);
975}
976
977VLIB_CLI_COMMAND (fib_walk_set_quota_command, static) = {
978 .path = "set fib walk quota",
979 .short_help = "set fib walk quota",
980 .function = fib_walk_set_quota,
981};
982
983static clib_error_t *
984fib_walk_set_histogram_elements_size (vlib_main_t * vm,
985 unformat_input_t * input,
986 vlib_cli_command_t * cmd)
987{
988 clib_error_t * error = NULL;
989 u32 new;
990
991 if (unformat (input, "%d", &new))
992 {
993 fib_walk_work_nodes_visisted_incr = new;
994 }
995 else
996 {
997 error = clib_error_return(0 , "Pass an int value");
998 }
999
1000 return (error);
1001}
1002
1003VLIB_CLI_COMMAND (fib_walk_set_histogram_elements_size_command, static) = {
1004 .path = "set fib walk histogram elements size",
1005 .short_help = "set fib walk histogram elements size",
1006 .function = fib_walk_set_histogram_elements_size,
1007};
1008
1009static clib_error_t *
1010fib_walk_clear (vlib_main_t * vm,
1011 unformat_input_t * input,
1012 vlib_cli_command_t * cmd)
1013{
1014 memset(fib_walk_hist_vists_per_walk, 0, sizeof(fib_walk_hist_vists_per_walk));
1015 memset(fib_walk_history, 0, sizeof(fib_walk_history));
1016 memset(fib_walk_work_time_taken, 0, sizeof(fib_walk_work_time_taken));
1017 memset(fib_walk_work_nodes_visited, 0, sizeof(fib_walk_work_nodes_visited));
1018 memset(fib_walk_sleep_lengths, 0, sizeof(fib_walk_sleep_lengths));
1019
1020 return (NULL);
1021}
1022
1023VLIB_CLI_COMMAND (fib_walk_clear_command, static) = {
1024 .path = "clear fib walk",
1025 .short_help = "clear fib walk",
1026 .function = fib_walk_clear,
1027};