Tune the ethernet input untagged l2-bridging case
[vpp.git] / vnet / vnet / fib / fib_walk.c
1 /*
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  */
22 typedef 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  */
46 typedef 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     /**
81      * Time the walk started
82      */
83     f64 fw_start_time;
84
85     /**
86      * 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  */
96 static 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  */
106 typedef 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_queue_stats_t)(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;         \
120          (_wqs) < FIB_WALK_QUEUE_STATS_NUM;   \
121          (_wqs)++)
122
123 /**
124  * The names of the walk stats
125  */
126 static const char * const fib_walk_queue_stats_names[] = FIB_WALK_QUEUE_STATS;
127 /**
128  * The names of the walk reasons
129  */
130 static const char * const fib_node_bw_reason_names[] = FIB_NODE_BW_REASONS;
131
132 /**
133  * A represenation of one queue of walk
134  */
135 typedef struct fib_walk_queue_t_
136 {
137     /**
138      * Qeuee stats
139      */
140     u64 fwq_stats[FIB_WALK_QUEUE_STATS_NUM];
141
142     /**
143      * The node list which acts as the queue
144      */
145     fib_node_list_t fwq_queue;
146 } fib_walk_queue_t;
147
148 /**
149  * A set of priority queues for outstanding walks
150  */
151 typedef struct fib_walk_queues_t_
152 {
153     fib_walk_queue_t fwqs_queues[FIB_WALK_PRIORITY_NUM];
154 } fib_walk_queues_t;
155
156 /**
157  * The global queues of outstanding walks
158  */
159 static fib_walk_queues_t fib_walk_queues;
160
161 /**
162  * The names of the walk priorities
163  */
164 static const char * const fib_walk_priority_names[] = FIB_WALK_PRIORITIES;
165
166 /**
167  * @brief Histogram stats on the lenths of each walk in elemenets visisted.
168  * Store upto 1<<23 elements in increments of 1<<10
169  */
170 #define HISTOGRAM_VISITS_PER_WALK_MAX (1<<23)
171 #define HISTOGRAM_VISITS_PER_WALK_INCR (1<<10)
172 #define HISTOGRAM_VISITS_PER_WALK_N_BUCKETS \
173     (HISTOGRAM_VISITS_PER_WALK_MAX/HISTOGRAM_VISITS_PER_WALK_INCR)
174 static u64 fib_walk_hist_vists_per_walk[HISTOGRAM_VISITS_PER_WALK_N_BUCKETS];
175
176 /**
177  * @brief History of state for the last 128 walks
178  */
179 #define HISTORY_N_WALKS 128
180 static u32 history_last_walk_pos;
181 typedef struct fib_walk_history_t_ {
182     u32 fwh_n_visits;
183     f64 fwh_duration;
184     fib_node_ptr_t fwh_parent;
185     fib_walk_flags_t fwh_flags;
186     fib_node_bw_reason_flag_t fwh_reason;
187 } fib_walk_history_t;
188 static fib_walk_history_t fib_walk_history[HISTORY_N_WALKS];
189
190 u8*
191 format_fib_walk_priority (u8 *s, va_list ap)
192 {
193     fib_walk_priority_t prio = va_arg(ap, fib_walk_priority_t);
194
195     ASSERT(prio < FIB_WALK_PRIORITY_NUM);
196
197     return (format(s, "%s", fib_walk_priority_names[prio]));
198 }
199 static u8*
200 format_fib_walk_queue_stats (u8 *s, va_list ap)
201 {
202     fib_walk_queue_stats_t wqs = va_arg(ap, fib_walk_queue_stats_t);
203
204     ASSERT(wqs < FIB_WALK_QUEUE_STATS_NUM);
205
206     return (format(s, "%s", fib_walk_queue_stats_names[wqs]));
207 }
208
209 static index_t
210 fib_walk_get_index (fib_walk_t *fwalk)
211 {
212     return (fwalk - fib_walk_pool);
213 }
214
215 static fib_walk_t *
216 fib_walk_get (index_t fwi)
217 {
218     return (pool_elt_at_index(fib_walk_pool, fwi));
219 }
220
221 /*
222  * not static so it can be used in the unit tests
223  */
224 u32
225 fib_walk_queue_get_size (fib_walk_priority_t prio)
226 {
227     return (fib_node_list_get_size(fib_walk_queues.fwqs_queues[prio].fwq_queue));
228 }
229
230 static fib_node_index_t
231 fib_walk_queue_get_front (fib_walk_priority_t prio)
232 {
233     fib_node_ptr_t wp;
234
235     fib_node_list_get_front(fib_walk_queues.fwqs_queues[prio].fwq_queue, &wp);
236
237     return (wp.fnp_index);
238 }
239
240 static void
241 fib_walk_destroy (fib_walk_t *fwalk)
242 {
243     fib_node_back_walk_ctx_t *ctx;
244     u32 bucket;
245
246     if (FIB_NODE_INDEX_INVALID != fwalk->fw_prio_sibling)
247     {
248         fib_node_list_elt_remove(fwalk->fw_prio_sibling);
249     }
250     fib_node_child_remove(fwalk->fw_parent.fnp_type,
251                           fwalk->fw_parent.fnp_index,
252                           fwalk->fw_dep_sibling);
253
254     /*
255      * add the stats to the continuous histogram collection.
256      */
257     bucket = (fwalk->fw_n_visits / HISTOGRAM_VISITS_PER_WALK_INCR);
258     bucket = (bucket >= HISTOGRAM_VISITS_PER_WALK_N_BUCKETS ?
259               HISTOGRAM_VISITS_PER_WALK_N_BUCKETS - 1 :
260               bucket);
261     fib_walk_hist_vists_per_walk[bucket]++;
262
263     /*
264      * save stats to the recent history
265      */
266
267     fib_walk_history[history_last_walk_pos].fwh_n_visits =
268         fwalk->fw_n_visits;
269     fib_walk_history[history_last_walk_pos].fwh_duration =
270         vlib_time_now(vlib_get_main()) - fwalk->fw_start_time;
271     fib_walk_history[history_last_walk_pos].fwh_parent =
272         fwalk->fw_parent;
273     fib_walk_history[history_last_walk_pos].fwh_flags =
274         fwalk->fw_flags;
275
276     fib_walk_history[history_last_walk_pos].fwh_reason = 0;
277     vec_foreach(ctx, fwalk->fw_ctx)
278     {
279         fib_walk_history[history_last_walk_pos].fwh_reason |=
280             ctx->fnbw_reason;
281     }
282
283     history_last_walk_pos = (history_last_walk_pos + 1) % HISTORY_N_WALKS;
284
285     fib_node_deinit(&fwalk->fw_node);
286     pool_put(fib_walk_pool, fwalk);
287 }
288
289 /**
290  * return code when advancing a walk
291  */
292 typedef enum fib_walk_advance_rc_t_
293 {
294     /**
295      * The walk is complete
296      */
297     FIB_WALK_ADVANCE_DONE,
298     /**
299      * the walk has more work
300      */
301     FIB_WALK_ADVANCE_MORE,
302     /**
303      * The walk merged with the one in front
304      */
305     FIB_WALK_ADVANCE_MERGE,
306 } fib_walk_advance_rc_t;
307
308 /**
309  * @brief Advance the walk one element in its work list
310  */
311 static fib_walk_advance_rc_t
312 fib_walk_advance (fib_node_index_t fwi)
313 {
314     fib_node_back_walk_ctx_t *ctx;
315     fib_node_back_walk_rc_t wrc;
316     fib_node_ptr_t sibling;
317     fib_walk_t *fwalk;
318     int more_elts;
319
320     /*
321      * this walk function is re-entrant - walks acan spawn walks.
322      * fib_walk_t objects come from a pool, so they can realloc. we need
323      * to retch from said pool at the appropriate times.
324      */
325     fwalk = fib_walk_get(fwi);
326
327     more_elts = fib_node_list_elt_get_next(fwalk->fw_dep_sibling, &sibling);
328
329     if (more_elts)
330     {
331         vec_foreach(ctx, fwalk->fw_ctx)
332         {
333             wrc = fib_node_back_walk_one(&sibling, ctx);
334
335             fwalk = fib_walk_get(fwi);
336             fwalk->fw_n_visits++;
337
338             if (FIB_NODE_BACK_WALK_MERGE == wrc)
339             {
340                 /*
341                  * this walk has merged with the one further along the node's
342                  * dependecy list.
343                  */
344                 return (FIB_WALK_ADVANCE_MERGE);
345             }
346         }
347         /*
348          * move foward to the next node to visit
349          */
350         more_elts = fib_node_list_advance(fwalk->fw_dep_sibling);
351     }
352
353     if (more_elts)
354     {
355         return (FIB_WALK_ADVANCE_MORE);
356     }
357
358     return (FIB_WALK_ADVANCE_DONE);
359 }
360
361 /**
362  * @breif Enurmerate the times of sleep between walks
363  */
364 typedef enum fib_walk_sleep_type_t_
365 {
366     FIB_WALK_SHORT_SLEEP,
367     FIB_WALK_LONG_SLEEP,
368 } fib_walk_sleep_type_t;
369
370 #define FIB_WALK_N_SLEEP (FIB_WALK_LONG_SLEEP+1)
371
372 /**
373  * @brief Durations for the sleep types
374  */
375 static f64 fib_walk_sleep_duration[] = {
376     [FIB_WALK_LONG_SLEEP] = 1e-3,
377     [FIB_WALK_SHORT_SLEEP] = 1e-8,
378 };
379
380 /**
381  * @brief The time quota for a walk. When more than this amount of time is
382  * spent, the walk process will yield.
383  */
384 static f64 quota = 1e-4;
385
386 /**
387  * Histogram on the amount of work done (in msecs) in each walk
388  */
389 #define N_TIME_BUCKETS 128
390 #define TIME_INCREMENTS (N_TIME_BUCKETS/2)
391 static u64 fib_walk_work_time_taken[N_TIME_BUCKETS];
392
393 /**
394  * Histogram on the number of nodes visted in each quota
395  */
396 #define N_ELTS_BUCKETS 128
397 static u32 fib_walk_work_nodes_visisted_incr = 2;
398 static u64 fib_walk_work_nodes_visited[N_ELTS_BUCKETS];
399
400 /**
401  * Histogram of the sleep lengths
402  */
403 static u64 fib_walk_sleep_lengths[2];
404
405 /**
406  * @brief Service the queues
407  * This is not declared static so that it can be unit tested - i know i know...
408  */
409 f64
410 fib_walk_process_queues (vlib_main_t * vm,
411                          const f64 quota)
412 {
413     f64 start_time, consumed_time;
414     fib_walk_sleep_type_t sleep;
415     fib_walk_priority_t prio;
416     fib_walk_advance_rc_t rc;
417     fib_node_index_t fwi;
418     fib_walk_t *fwalk;
419     u32 n_elts;
420     i32 bucket;
421
422     consumed_time = 0;
423     start_time = vlib_time_now(vm);
424     n_elts = 0;
425
426     FOR_EACH_FIB_WALK_PRIORITY(prio)
427     {
428         while (0 != fib_walk_queue_get_size(prio))
429         {
430             fwi = fib_walk_queue_get_front(prio);
431
432             /*
433              * set this walk as executing
434              */
435             fwalk = fib_walk_get(fwi);
436             fwalk->fw_flags |= FIB_WALK_FLAG_EXECUTING;
437
438             do
439             {
440                 rc = fib_walk_advance(fwi);
441                 n_elts++;
442                 consumed_time = (vlib_time_now(vm) - start_time);
443             } while ((consumed_time < quota) &&
444                      (FIB_WALK_ADVANCE_MORE == rc));
445
446             /*
447              * if this walk has no more work then pop it from the queue
448              * and move on to the next.
449              */
450             if (FIB_WALK_ADVANCE_MORE != rc)
451             {
452                 fwalk = fib_walk_get(fwi);
453                 fib_walk_destroy(fwalk);
454                 fib_walk_queues.fwqs_queues[prio].fwq_stats[FIB_WALK_COMPLETED]++;
455             }
456             else
457             {
458                 /*
459                  * passed our work quota. sleep time.
460                  */
461                 fwalk = fib_walk_get(fwi);
462                 fwalk->fw_flags &= ~FIB_WALK_FLAG_EXECUTING;
463                 sleep = FIB_WALK_SHORT_SLEEP;
464                 goto that_will_do_for_now;
465             }
466         }
467     }
468     /*
469      * got to the end of all the work
470      */
471     sleep = FIB_WALK_LONG_SLEEP;
472
473 that_will_do_for_now:
474
475     /*
476      * collect the stats:
477      *  - for the number of nodes visisted we store 128 increments
478      *  - for the time consumed we store quota/TIME_INCREMENTS increments.
479      */
480     bucket = ((n_elts/fib_walk_work_nodes_visisted_incr) > N_ELTS_BUCKETS ?
481               N_ELTS_BUCKETS-1 :
482               n_elts/fib_walk_work_nodes_visisted_incr);
483     ++fib_walk_work_nodes_visited[bucket];
484
485     bucket = (consumed_time - quota) / (quota / TIME_INCREMENTS);
486     bucket += N_TIME_BUCKETS/2;
487     bucket = (bucket < 0 ? 0 : bucket);
488     bucket = (bucket > N_TIME_BUCKETS-1 ? N_TIME_BUCKETS-1 : bucket);
489     ++fib_walk_work_time_taken[bucket];
490
491     ++fib_walk_sleep_lengths[sleep];
492
493     return (fib_walk_sleep_duration[sleep]);
494 }
495
496 /**
497  * @brief The 'fib-walk' process's main loop.
498  */
499 static uword
500 fib_walk_process (vlib_main_t * vm,
501                   vlib_node_runtime_t * node,
502                   vlib_frame_t * f)
503 {
504     f64 sleep_time;
505
506     sleep_time = fib_walk_sleep_duration[FIB_WALK_SHORT_SLEEP];
507
508     while (1)
509     {
510         vlib_process_wait_for_event_or_clock(vm, sleep_time);
511
512         /*
513          * there may be lots of event queued between the processes,
514          * but the walks we want to schedule are in the priority queues,
515          * so we ignore the process events.
516          */
517         vlib_process_get_events(vm, NULL);
518
519         sleep_time = fib_walk_process_queues(vm, quota);
520     }
521
522     /*
523      * Unreached
524      */
525     ASSERT(!"WTF");
526     return 0;
527 }
528
529 /* *INDENT-OFF* */
530 VLIB_REGISTER_NODE (fib_walk_process_node,static) = {
531     .function = fib_walk_process,
532     .type = VLIB_NODE_TYPE_PROCESS,
533     .name = "fib-walk",
534 };
535 /* *INDENT-ON* */
536
537 /**
538  * @brief Allocate a new walk object
539  */
540 static fib_walk_t *
541 fib_walk_alloc (fib_node_type_t parent_type,
542                 fib_node_index_t parent_index,
543                 fib_walk_flags_t flags,
544                 fib_node_back_walk_ctx_t *ctx)
545 {
546     fib_walk_t *fwalk;
547
548     pool_get(fib_walk_pool, fwalk);
549
550     fib_node_init(&fwalk->fw_node, FIB_NODE_TYPE_WALK);
551
552     fwalk->fw_flags = flags;
553     fwalk->fw_dep_sibling  = FIB_NODE_INDEX_INVALID;
554     fwalk->fw_prio_sibling = FIB_NODE_INDEX_INVALID;
555     fwalk->fw_parent.fnp_index = parent_index;
556     fwalk->fw_parent.fnp_type = parent_type;
557     fwalk->fw_ctx = NULL;
558     fwalk->fw_start_time = vlib_time_now(vlib_get_main());
559     fwalk->fw_n_visits = 0;
560
561     /*
562      * make a copy of the backwalk context so the depth count remains
563      * the same for each sibling visitsed. This is important in the case
564      * where a parents has a loop via one child, but all the others are not.
565      * if the looped child were visited first, the depth count would exceed, the
566      * max and the walk would terminate before it reached the other siblings.
567      */
568     vec_add1(fwalk->fw_ctx, *ctx);
569
570     return (fwalk);
571 }
572
573 /**
574  * @brief Enqueue a walk onto the appropriate priority queue. Then signal
575  * the background process there is work to do.
576  */
577 static index_t
578 fib_walk_prio_queue_enquue (fib_walk_priority_t prio,
579                             fib_walk_t *fwalk)
580 {
581     index_t sibling;
582
583     sibling = fib_node_list_push_front(fib_walk_queues.fwqs_queues[prio].fwq_queue,
584                                        0,
585                                        FIB_NODE_TYPE_WALK,
586                                        fib_walk_get_index(fwalk));
587     fib_walk_queues.fwqs_queues[prio].fwq_stats[FIB_WALK_SCHEDULED]++;
588
589     /*
590      * poke the fib-walk process to perform the async walk.
591      * we are not passing it specific data, hence the last two args,
592      * the process will drain the queues
593      */
594     vlib_process_signal_event(vlib_get_main(),
595                               fib_walk_process_node.index,
596                               FIB_WALK_EVENT,
597                               FIB_WALK_EVENT);
598
599     return (sibling);
600 }
601
602 void
603 fib_walk_async (fib_node_type_t parent_type,
604                 fib_node_index_t parent_index,
605                 fib_walk_priority_t prio,
606                 fib_node_back_walk_ctx_t *ctx)
607 {
608     fib_walk_t *fwalk;
609
610     if (FIB_NODE_GRAPH_MAX_DEPTH < ++ctx->fnbw_depth)
611     {
612         /*
613          * The walk has reached the maximum depth. there is a loop in the graph.
614          * bail.
615          */
616         return;
617     }
618     if (ctx->fnbw_flags & FIB_NODE_BW_FLAG_FORCE_SYNC)
619     {
620         /*
621          * the originator of the walk wanted it to be synchronous, but the
622          * parent object chose async - denied.
623          */
624         return (fib_walk_sync(parent_type, parent_index, ctx));
625     }
626
627
628     fwalk = fib_walk_alloc(parent_type,
629                            parent_index,
630                            FIB_WALK_FLAG_ASYNC,
631                            ctx);
632
633     fwalk->fw_dep_sibling = fib_node_child_add(parent_type,
634                                                parent_index,
635                                                FIB_NODE_TYPE_WALK,
636                                                fib_walk_get_index(fwalk));
637
638     fwalk->fw_prio_sibling = fib_walk_prio_queue_enquue(prio, fwalk);
639 }
640
641 /**
642  * @brief Back walk all the children of a FIB node.
643  *
644  * note this is a synchronous depth first walk. Children visited may propagate
645  * the walk to thier children. Other children node types may not propagate,
646  * synchronously but instead queue the walk for later async completion.
647  */
648 void
649 fib_walk_sync (fib_node_type_t parent_type,
650                fib_node_index_t parent_index,
651                fib_node_back_walk_ctx_t *ctx)
652 {
653     fib_walk_advance_rc_t rc;
654     fib_node_index_t fwi;
655     fib_walk_t *fwalk;
656
657     if (FIB_NODE_GRAPH_MAX_DEPTH < ++ctx->fnbw_depth)
658     {
659         /*
660          * The walk has reached the maximum depth. there is a loop in the graph.
661          * bail.
662          */
663         return;
664     }
665
666     fwalk = fib_walk_alloc(parent_type,
667                            parent_index,
668                            FIB_WALK_FLAG_SYNC,
669                            ctx);
670
671     fwalk->fw_dep_sibling = fib_node_child_add(parent_type,
672                                                parent_index,
673                                                FIB_NODE_TYPE_WALK,
674                                                fib_walk_get_index(fwalk));
675     fwi = fib_walk_get_index(fwalk);
676
677     while (1)
678     {
679         /*
680          * set this walk as executing
681          */
682         fwalk->fw_flags |= FIB_WALK_FLAG_EXECUTING;
683
684         do
685         {
686             rc = fib_walk_advance(fwi);
687         } while (FIB_WALK_ADVANCE_MORE == rc);
688
689
690         /*
691          * this walk function is re-entrant - walks can spawn walks.
692          * fib_walk_t objects come from a pool, so they can realloc. we need
693          * to re-fetch from said pool at the appropriate times.
694          */
695         fwalk = fib_walk_get(fwi);
696
697         if (FIB_WALK_ADVANCE_MERGE == rc)
698         {
699             /*
700              * this sync walk merged with an walk in front.
701              * by reqeusting a sync walk the client wanted all children walked,
702              * so we ditch the walk object in hand and continue with the one
703              * we merged into
704              */
705             fib_node_ptr_t merged_walk;
706
707             fib_node_list_elt_get_next(fwalk->fw_dep_sibling, &merged_walk);
708
709             ASSERT(FIB_NODE_INDEX_INVALID != merged_walk.fnp_index);
710             ASSERT(FIB_NODE_TYPE_WALK == merged_walk.fnp_type);
711
712             fib_walk_destroy(fwalk);
713
714             fwi = merged_walk.fnp_index;
715             fwalk = fib_walk_get(fwi);
716
717             if (FIB_WALK_FLAG_EXECUTING & fwalk->fw_flags)
718             {
719                 /*
720                  * we are executing a sync walk, and we have met with another
721                  * walk that is also executing. since only one walk executs at once
722                  * (there is no multi-threading) this implies we have met ourselves
723                  * and hence the is a loop in the graph.
724                  * This function is re-entrant, so the walk object we met is being
725                  * acted on in a stack frame below this one. We must therefore not
726                  * continue with it now, but let the stack unwind and along the
727                  * appropriate frame to read the depth count and bail.
728                  */
729                 fwalk = NULL;
730                 break;
731             }
732         }
733         else
734         {
735             /*
736              * the walk reached the end of the depdency list.
737              */
738             break;
739         }
740     }
741
742     if (NULL != fwalk)
743     {
744         fib_walk_destroy(fwalk);
745     }
746 }
747
748 static fib_node_t *
749 fib_walk_get_node (fib_node_index_t index)
750 {
751     fib_walk_t *fwalk;
752
753     fwalk = fib_walk_get(index);
754
755     return (&(fwalk->fw_node));
756 }
757
758 /**
759  * Walk objects are not parents, nor are they locked.
760  * are no-ops
761  */
762 static void
763 fib_walk_last_lock_gone (fib_node_t *node)
764 {
765     ASSERT(0);
766 }
767
768 static fib_walk_t*
769 fib_walk_get_from_node (fib_node_t *node)
770 {
771     return ((fib_walk_t*)(((char*)node) -
772                           STRUCT_OFFSET_OF(fib_walk_t, fw_node)));
773 }
774
775 /**
776  * @brief Another back walk has reach this walk.
777  * Megre them so there is only one left. It is this node being
778  * visited that will remain, so copy or merge the context onto it.
779  */
780 static fib_node_back_walk_rc_t
781 fib_walk_back_walk_notify (fib_node_t *node,
782                            fib_node_back_walk_ctx_t *ctx)
783 {
784     fib_node_back_walk_ctx_t *old;
785     fib_walk_t *fwalk;
786
787     fwalk = fib_walk_get_from_node(node);
788
789     /*
790      * check whether the walk context can be merge with another,
791      * or whether it needs to be appended.
792      */
793     vec_foreach(old, fwalk->fw_ctx)
794     {
795         /*
796          * we can merge walks if the reason for the walk is the same.
797          */
798         if (old->fnbw_reason == ctx->fnbw_reason)
799         {
800             /*
801              * copy the largest of the depth values. in the presence of a loop,
802              * the same walk will merge with itself. if we take the smaller depth
803              * then it will never end.
804              */
805             old->fnbw_depth = ((old->fnbw_depth >= ctx->fnbw_depth) ?
806                                 old->fnbw_depth :
807                                 ctx->fnbw_depth);
808             goto out;
809         }
810     }
811
812     /*
813      * walks could not be merged, this means that the walk infront needs to
814      * perform different action to this one that has caught up. the one in front
815      * was scheduled first so append the new walk context to the back of the list.
816      */
817     vec_add1(fwalk->fw_ctx, *ctx);
818
819 out:
820     return (FIB_NODE_BACK_WALK_MERGE);
821 }
822
823 /**
824  * The FIB walk's graph node virtual function table
825  */
826 static const fib_node_vft_t fib_walk_vft = {
827     .fnv_get = fib_walk_get_node,
828     .fnv_last_lock = fib_walk_last_lock_gone,
829     .fnv_back_walk = fib_walk_back_walk_notify,
830 };
831
832 void
833 fib_walk_module_init (void)
834 {
835     fib_walk_priority_t prio;
836
837     FOR_EACH_FIB_WALK_PRIORITY(prio)
838     {
839         fib_walk_queues.fwqs_queues[prio].fwq_queue = fib_node_list_create();
840     }
841
842     fib_node_register_type(FIB_NODE_TYPE_WALK, &fib_walk_vft);
843 }
844
845 static u8*
846 format_fib_walk (u8* s, va_list ap)
847 {
848     fib_node_index_t fwi = va_arg(ap, fib_node_index_t);
849     fib_walk_t *fwalk;
850
851     fwalk = fib_walk_get(fwi);
852
853     return (format(s, "  parent:{%s:%d} visits:%d flags:%d",
854                    fib_node_type_get_name(fwalk->fw_parent.fnp_type),
855                    fwalk->fw_parent.fnp_index,
856                    fwalk->fw_n_visits,
857                    fwalk->fw_flags));
858 }
859
860 static clib_error_t *
861 fib_walk_show (vlib_main_t * vm,
862                unformat_input_t * input,
863                vlib_cli_command_t * cmd)
864 {
865     fib_walk_queue_stats_t wqs;
866     fib_walk_priority_t prio;
867     fib_node_ptr_t sibling;
868     fib_node_index_t fwi;
869     fib_walk_t *fwalk;
870     int more_elts, ii;
871     u8 *s = NULL;
872
873 #define USEC 1000000
874     vlib_cli_output(vm, "FIB Walk Quota = %.2fusec:", quota * USEC);
875     vlib_cli_output(vm, "FIB Walk queues:");
876
877     FOR_EACH_FIB_WALK_PRIORITY(prio)
878     {
879         vlib_cli_output(vm, " %U priority queue:",
880                         format_fib_walk_priority, prio);
881         vlib_cli_output(vm, "  Stats: ");
882
883         FOR_EACH_FIB_WALK_QUEUE_STATS(wqs)
884         {
885             vlib_cli_output(vm, "    %U:%d",
886                             format_fib_walk_queue_stats, wqs,
887                             fib_walk_queues.fwqs_queues[prio].fwq_stats[wqs]);
888         }
889         vlib_cli_output(vm, "  Occupancy:%d",
890                         fib_node_list_get_size(
891                             fib_walk_queues.fwqs_queues[prio].fwq_queue));
892
893         more_elts = fib_node_list_get_front(
894                         fib_walk_queues.fwqs_queues[prio].fwq_queue,
895                         &sibling);
896
897         while (more_elts)
898         {
899             ASSERT(FIB_NODE_INDEX_INVALID != sibling.fnp_index);
900             ASSERT(FIB_NODE_TYPE_WALK == sibling.fnp_type);
901
902             fwi = sibling.fnp_index;
903             fwalk = fib_walk_get(fwi);
904
905             vlib_cli_output(vm, "  %U", format_fib_walk, fwi);
906
907             more_elts = fib_node_list_elt_get_next(fwalk->fw_prio_sibling,
908                                                    &sibling);
909         }
910     }
911
912     vlib_cli_output(vm, "Histogram Statistics:");
913     vlib_cli_output(vm, " Number of Elements visit per-quota:");
914     for (ii = 0; ii < N_ELTS_BUCKETS; ii++)
915     {
916         if (0 != fib_walk_work_nodes_visited[ii])
917             s = format(s, "%d:%d ",
918                        (ii * fib_walk_work_nodes_visisted_incr),
919                        fib_walk_work_nodes_visited[ii]);
920     }
921     vlib_cli_output(vm, "  %v", s);
922     vec_free(s);
923
924     vlib_cli_output(vm, " Time consumed per-quota (Quota=%f usec):", quota*USEC);
925     s = format(s, "0:%d ", fib_walk_work_time_taken[0]);
926     for (ii = 1; ii < N_TIME_BUCKETS; ii++)
927     {
928         if (0 != fib_walk_work_time_taken[ii])
929             s = format(s, "%d:%d ", (u32)((((ii - N_TIME_BUCKETS/2) *
930                                            (quota / TIME_INCREMENTS)) + quota) *
931                                          USEC),
932                        fib_walk_work_time_taken[ii]);
933     }
934     vlib_cli_output(vm, "  %v", s);
935     vec_free(s);
936
937     vlib_cli_output(vm, " Sleep Types:");
938     vlib_cli_output(vm, "  Short  Long:");
939     vlib_cli_output(vm, "  %d %d:",
940                     fib_walk_sleep_lengths[FIB_WALK_SHORT_SLEEP],
941                     fib_walk_sleep_lengths[FIB_WALK_LONG_SLEEP]);
942
943     vlib_cli_output(vm, " Number of Elements visited per-walk:");
944     for (ii = 0; ii < HISTOGRAM_VISITS_PER_WALK_N_BUCKETS; ii++)
945     {
946         if (0 != fib_walk_hist_vists_per_walk[ii])
947             s = format(s, "%d:%d ",
948                        ii*HISTOGRAM_VISITS_PER_WALK_INCR,
949                        fib_walk_hist_vists_per_walk[ii]);
950     }
951     vlib_cli_output(vm, "  %v", s);
952     vec_free(s);
953
954
955     vlib_cli_output(vm, "Brief History (last %d walks):", HISTORY_N_WALKS);
956     ii = history_last_walk_pos - 1;
957     if (ii < 0)
958         ii = HISTORY_N_WALKS - 1;
959
960     while (ii != history_last_walk_pos)
961     {
962         if (0 != fib_walk_history[ii].fwh_n_visits)
963         {
964             fib_node_back_walk_reason_t reason;
965             u8 *s = NULL;
966
967             s = format(s, " %s:%d visits:%d duration:%.2f ",
968                        fib_node_type_get_name(fib_walk_history[ii].fwh_parent.fnp_type),
969                        fib_walk_history[ii].fwh_parent.fnp_index,
970                        fib_walk_history[ii].fwh_n_visits,
971                        fib_walk_history[ii].fwh_duration);
972             if (FIB_WALK_FLAG_SYNC & fib_walk_history[ii].fwh_flags)
973                 s = format(s, "sync, ");
974             if (FIB_WALK_FLAG_ASYNC & fib_walk_history[ii].fwh_flags)
975                 s = format(s, "async, ");
976
977             s = format(s, "reason:");
978             FOR_EACH_FIB_NODE_BW_REASON(reason) {
979                 if ((1<<reason) & fib_walk_history[ii].fwh_reason) {
980                     s = format (s, "%s,", fib_node_bw_reason_names[reason]);
981                 }
982             }
983             vlib_cli_output(vm, "%v", s);
984         }
985
986         ii--;
987         if (ii < 0)
988             ii = HISTORY_N_WALKS - 1;
989     }
990
991     return (NULL);
992 }
993
994 VLIB_CLI_COMMAND (fib_walk_show_command, static) = {
995     .path = "show fib walk",
996     .short_help = "show fib walk",
997     .function = fib_walk_show,
998 };
999
1000 static clib_error_t *
1001 fib_walk_set_quota (vlib_main_t * vm,
1002                     unformat_input_t * input,
1003                     vlib_cli_command_t * cmd)
1004 {
1005     clib_error_t * error = NULL;
1006     f64 new_quota;
1007
1008     if (unformat (input, "%f", &new_quota))
1009     {
1010         quota = new_quota;
1011     }
1012     else
1013     {
1014         error = clib_error_return(0 , "Pass a float value");
1015     }
1016
1017     return (error);
1018 }
1019
1020 VLIB_CLI_COMMAND (fib_walk_set_quota_command, static) = {
1021     .path = "set fib walk quota",
1022     .short_help = "set fib walk quota",
1023     .function = fib_walk_set_quota,
1024 };
1025
1026 static clib_error_t *
1027 fib_walk_set_histogram_elements_size (vlib_main_t * vm,
1028                                       unformat_input_t * input,
1029                                       vlib_cli_command_t * cmd)
1030 {
1031     clib_error_t * error = NULL;
1032     u32 new;
1033
1034     if (unformat (input, "%d", &new))
1035     {
1036         fib_walk_work_nodes_visisted_incr = new;
1037     }
1038     else
1039     {
1040         error = clib_error_return(0 , "Pass an int value");
1041     }
1042
1043     return (error);
1044 }
1045
1046 VLIB_CLI_COMMAND (fib_walk_set_histogram_elements_size_command, static) = {
1047     .path = "set fib walk histogram elements size",
1048     .short_help = "set fib walk histogram elements size",
1049     .function = fib_walk_set_histogram_elements_size,
1050 };
1051
1052 static clib_error_t *
1053 fib_walk_clear (vlib_main_t * vm,
1054                 unformat_input_t * input,
1055                 vlib_cli_command_t * cmd)
1056 {
1057     memset(fib_walk_hist_vists_per_walk, 0, sizeof(fib_walk_hist_vists_per_walk));
1058     memset(fib_walk_history, 0, sizeof(fib_walk_history));
1059     memset(fib_walk_work_time_taken, 0, sizeof(fib_walk_work_time_taken));
1060     memset(fib_walk_work_nodes_visited, 0, sizeof(fib_walk_work_nodes_visited));
1061     memset(fib_walk_sleep_lengths, 0, sizeof(fib_walk_sleep_lengths));
1062
1063     return (NULL);
1064 }
1065
1066 VLIB_CLI_COMMAND (fib_walk_clear_command, static) = {
1067     .path = "clear fib walk",
1068     .short_help = "clear fib walk",
1069     .function = fib_walk_clear,
1070 };