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:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/fib/fib_walk.h>
17 #include <vnet/fib/fib_node_list.h>
22 typedef enum fib_walk_flags_t_
26 * This walk will run to completion, i.e. visit ALL the children.
27 * It is a depth first traversal of the graph.
29 FIB_WALK_FLAG_SYNC = (1 << 0),
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.
36 FIB_WALK_FLAG_ASYNC = (1 << 1),
38 * An indication that the walk is currently executing.
40 FIB_WALK_FLAG_EXECUTING = (1 << 2),
44 * A representation of a graph walk from a parent object to its children
46 typedef struct fib_walk_t_
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
58 fib_walk_flags_t fw_flags;
61 * Sibling index in the dependency list
66 * Sibling index in the list of all walks
71 * Pointer to the node whose dependants this walk is walking
73 fib_node_ptr_t fw_parent;
76 * Number of nodes visited by this walk. saved for debugging purposes.
81 * Time the walk started
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.
90 fib_node_back_walk_ctx_t *fw_ctx;
94 * @brief The pool of all walk objects
96 static fib_walk_t *fib_walk_pool;
99 * Statistics maintained per-walk queue
101 typedef enum fib_walk_queue_stats_t_
105 } fib_walk_queue_stats_t;
106 #define FIB_WALK_QUEUE_STATS_NUM ((fib_walk_queue_stats_t)(FIB_WALK_COMPLETED+1))
108 #define FIB_WALK_QUEUE_STATS { \
109 [FIB_WALK_SCHEDULED] = "scheduled", \
110 [FIB_WALK_COMPLETED] = "completed", \
113 #define FOR_EACH_FIB_WALK_QUEUE_STATS(_wqs) \
114 for ((_wqs) = FIB_WALK_SCHEDULED; \
115 (_wqs) < FIB_WALK_QUEUE_STATS_NUM; \
119 * The names of the walk stats
121 static const char * const fib_walk_queue_stats_names[] = FIB_WALK_QUEUE_STATS;
123 * The names of the walk reasons
125 static const char * const fib_node_bw_reason_names[] = FIB_NODE_BW_REASONS;
128 * A represenation of one queue of walk
130 typedef struct fib_walk_queue_t_
135 u64 fwq_stats[FIB_WALK_QUEUE_STATS_NUM];
138 * The node list which acts as the queue
140 fib_node_list_t fwq_queue;
144 * A set of priority queues for outstanding walks
146 typedef struct fib_walk_queues_t_
148 fib_walk_queue_t fwqs_queues[FIB_WALK_PRIORITY_NUM];
152 * The global queues of outstanding walks
154 static fib_walk_queues_t fib_walk_queues;
157 * The names of the walk priorities
159 static const char * const fib_walk_priority_names[] = FIB_WALK_PRIORITIES;
162 * @brief Histogram stats on the lenths of each walk in elemenets visisted.
163 * Store upto 1<<23 elements in increments of 1<<10
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)
169 static u64 fib_walk_hist_vists_per_walk[HISTOGRAM_VISITS_PER_WALK_N_BUCKETS];
172 * @brief History of state for the last 128 walks
174 #define HISTORY_N_WALKS 128
175 #define MAX_HISTORY_REASONS 16
176 static u32 history_last_walk_pos;
177 typedef struct fib_walk_history_t_ {
181 fib_node_ptr_t fwh_parent;
182 fib_walk_flags_t fwh_flags;
183 fib_node_bw_reason_flag_t fwh_reason[MAX_HISTORY_REASONS];
184 } fib_walk_history_t;
185 static fib_walk_history_t fib_walk_history[HISTORY_N_WALKS];
188 format_fib_walk_priority (u8 *s, va_list ap)
190 fib_walk_priority_t prio = va_arg(ap, fib_walk_priority_t);
192 ASSERT(prio < FIB_WALK_PRIORITY_NUM);
194 return (format(s, "%s", fib_walk_priority_names[prio]));
197 format_fib_walk_queue_stats (u8 *s, va_list ap)
199 fib_walk_queue_stats_t wqs = va_arg(ap, fib_walk_queue_stats_t);
201 ASSERT(wqs < FIB_WALK_QUEUE_STATS_NUM);
203 return (format(s, "%s", fib_walk_queue_stats_names[wqs]));
207 fib_walk_get_index (fib_walk_t *fwalk)
209 return (fwalk - fib_walk_pool);
213 fib_walk_get (index_t fwi)
215 return (pool_elt_at_index(fib_walk_pool, fwi));
219 * not static so it can be used in the unit tests
222 fib_walk_queue_get_size (fib_walk_priority_t prio)
224 return (fib_node_list_get_size(fib_walk_queues.fwqs_queues[prio].fwq_queue));
227 static fib_node_index_t
228 fib_walk_queue_get_front (fib_walk_priority_t prio)
232 fib_node_list_get_front(fib_walk_queues.fwqs_queues[prio].fwq_queue, &wp);
234 return (wp.fnp_index);
238 fib_walk_destroy (index_t fwi)
243 fwalk = fib_walk_get(fwi);
245 if (FIB_NODE_INDEX_INVALID != fwalk->fw_prio_sibling)
247 fib_node_list_elt_remove(fwalk->fw_prio_sibling);
249 fib_node_child_remove(fwalk->fw_parent.fnp_type,
250 fwalk->fw_parent.fnp_index,
251 fwalk->fw_dep_sibling);
254 * refetch the walk object. More walks could have been spawned as a result
255 * of releasing the lock on the parent.
257 fwalk = fib_walk_get(fwi);
260 * add the stats to the continuous histogram collection.
262 bucket = (fwalk->fw_n_visits / HISTOGRAM_VISITS_PER_WALK_INCR);
263 bucket = (bucket >= HISTOGRAM_VISITS_PER_WALK_N_BUCKETS ?
264 HISTOGRAM_VISITS_PER_WALK_N_BUCKETS - 1 :
266 fib_walk_hist_vists_per_walk[bucket]++;
269 * save stats to the recent history
272 fib_walk_history[history_last_walk_pos].fwh_n_visits =
274 fib_walk_history[history_last_walk_pos].fwh_completed =
275 vlib_time_now(vlib_get_main());
276 fib_walk_history[history_last_walk_pos].fwh_duration =
277 fib_walk_history[history_last_walk_pos].fwh_completed -
278 fwalk->fw_start_time;
279 fib_walk_history[history_last_walk_pos].fwh_parent =
281 fib_walk_history[history_last_walk_pos].fwh_flags =
284 vec_foreach_index(ii, fwalk->fw_ctx)
286 if (ii < MAX_HISTORY_REASONS)
288 fib_walk_history[history_last_walk_pos].fwh_reason[ii] =
289 fwalk->fw_ctx[ii].fnbw_reason;
293 history_last_walk_pos = (history_last_walk_pos + 1) % HISTORY_N_WALKS;
295 fib_node_deinit(&fwalk->fw_node);
296 vec_free(fwalk->fw_ctx);
297 pool_put(fib_walk_pool, fwalk);
301 * return code when advancing a walk
303 typedef enum fib_walk_advance_rc_t_
306 * The walk is complete
308 FIB_WALK_ADVANCE_DONE,
310 * the walk has more work
312 FIB_WALK_ADVANCE_MORE,
314 * The walk merged with the one in front
316 FIB_WALK_ADVANCE_MERGE,
317 } fib_walk_advance_rc_t;
320 * @brief Advance the walk one element in its work list
322 static fib_walk_advance_rc_t
323 fib_walk_advance (fib_node_index_t fwi)
325 fib_node_back_walk_rc_t wrc;
326 fib_node_ptr_t sibling;
332 * this walk function is re-entrant - walks acan spawn walks.
333 * fib_walk_t objects come from a pool, so they can realloc. we need
334 * to retch from said pool at the appropriate times.
336 fwalk = fib_walk_get(fwi);
338 more_elts = fib_node_list_elt_get_next(fwalk->fw_dep_sibling, &sibling);
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.
349 n_ctxs = vec_len(fwalk->fw_ctx);
353 wrc = fib_node_back_walk_one(&sibling, &fwalk->fw_ctx[ii]);
356 fwalk = fib_walk_get(fwi);
357 fwalk->fw_n_visits++;
359 if (FIB_NODE_BACK_WALK_MERGE == wrc)
362 * this walk has merged with the one further along the node's
365 return (FIB_WALK_ADVANCE_MERGE);
369 * re-evaluate the number of backwalk contexts we need to process.
371 n_ctxs = vec_len(fwalk->fw_ctx);
374 * move foward to the next node to visit
376 more_elts = fib_node_list_advance(fwalk->fw_dep_sibling);
381 return (FIB_WALK_ADVANCE_MORE);
384 return (FIB_WALK_ADVANCE_DONE);
388 * @breif Enurmerate the times of sleep between walks
390 typedef enum fib_walk_sleep_type_t_
392 FIB_WALK_SHORT_SLEEP,
394 } fib_walk_sleep_type_t;
396 #define FIB_WALK_N_SLEEP (FIB_WALK_LONG_SLEEP+1)
399 * @brief Durations for the sleep types
401 static f64 fib_walk_sleep_duration[] = {
403 * Long sleep when there is no more work, i.e. the queues are empty.
404 * This is a sleep (as opposed to a wait for event) just to be sure we
405 * are not missing events by sleeping forever.
407 [FIB_WALK_LONG_SLEEP] = 2,
410 * Short sleep. There is work left in the queues. We are yielding the CPU
413 [FIB_WALK_SHORT_SLEEP] = 1e-8,
417 * @brief The time quota for a walk. When more than this amount of time is
418 * spent, the walk process will yield.
420 static f64 quota = 1e-4;
423 * Histogram on the amount of work done (in msecs) in each walk
425 #define N_TIME_BUCKETS 128
426 #define TIME_INCREMENTS (N_TIME_BUCKETS/2)
427 static u64 fib_walk_work_time_taken[N_TIME_BUCKETS];
430 * Histogram on the number of nodes visted in each quota
432 #define N_ELTS_BUCKETS 128
433 static u32 fib_walk_work_nodes_visisted_incr = 2;
434 static u64 fib_walk_work_nodes_visited[N_ELTS_BUCKETS];
437 * Histogram of the sleep lengths
439 static u64 fib_walk_sleep_lengths[2];
442 * @brief Service the queues
443 * This is not declared static so that it can be unit tested - i know i know...
446 fib_walk_process_queues (vlib_main_t * vm,
449 f64 start_time, consumed_time;
450 fib_walk_sleep_type_t sleep;
451 fib_walk_priority_t prio;
452 fib_walk_advance_rc_t rc;
453 fib_node_index_t fwi;
459 start_time = vlib_time_now(vm);
462 FOR_EACH_FIB_WALK_PRIORITY(prio)
464 while (0 != fib_walk_queue_get_size(prio))
466 fwi = fib_walk_queue_get_front(prio);
469 * set this walk as executing
471 fwalk = fib_walk_get(fwi);
472 fwalk->fw_flags |= FIB_WALK_FLAG_EXECUTING;
476 rc = fib_walk_advance(fwi);
478 consumed_time = (vlib_time_now(vm) - start_time);
479 } while ((consumed_time < quota) &&
480 (FIB_WALK_ADVANCE_MORE == rc));
483 * if this walk has no more work then pop it from the queue
484 * and move on to the next.
486 if (FIB_WALK_ADVANCE_MORE != rc)
488 fib_walk_destroy(fwi);
489 fib_walk_queues.fwqs_queues[prio].fwq_stats[FIB_WALK_COMPLETED]++;
494 * passed our work quota. sleep time.
496 fwalk = fib_walk_get(fwi);
497 fwalk->fw_flags &= ~FIB_WALK_FLAG_EXECUTING;
498 sleep = FIB_WALK_SHORT_SLEEP;
499 goto that_will_do_for_now;
504 * got to the end of all the work
506 sleep = FIB_WALK_LONG_SLEEP;
508 that_will_do_for_now:
512 * - for the number of nodes visisted we store 128 increments
513 * - for the time consumed we store quota/TIME_INCREMENTS increments.
515 bucket = ((n_elts/fib_walk_work_nodes_visisted_incr) > N_ELTS_BUCKETS ?
517 n_elts/fib_walk_work_nodes_visisted_incr);
518 ++fib_walk_work_nodes_visited[bucket];
520 bucket = (consumed_time - quota) / (quota / TIME_INCREMENTS);
521 bucket += N_TIME_BUCKETS/2;
522 bucket = (bucket < 0 ? 0 : bucket);
523 bucket = (bucket > N_TIME_BUCKETS-1 ? N_TIME_BUCKETS-1 : bucket);
524 ++fib_walk_work_time_taken[bucket];
526 ++fib_walk_sleep_lengths[sleep];
528 return (fib_walk_sleep_duration[sleep]);
532 * Events sent to the FIB walk process
534 typedef enum fib_walk_process_event_t_
536 FIB_WALK_PROCESS_EVENT_DATA,
537 FIB_WALK_PROCESS_EVENT_ENABLE,
538 FIB_WALK_PROCESS_EVENT_DISABLE,
539 } fib_walk_process_event;
542 * @brief The 'fib-walk' process's main loop.
545 fib_walk_process (vlib_main_t * vm,
546 vlib_node_runtime_t * node,
549 uword event_type, *event_data = 0;
554 sleep_time = fib_walk_sleep_duration[FIB_WALK_SHORT_SLEEP];
559 * the feature to disable/enable this walk process is only
560 * for testing purposes
564 vlib_process_wait_for_event_or_clock(vm, sleep_time);
568 vlib_process_wait_for_event(vm);
571 event_type = vlib_process_get_events(vm, &event_data);
572 vec_reset_length(event_data);
576 case FIB_WALK_PROCESS_EVENT_ENABLE:
579 case FIB_WALK_PROCESS_EVENT_DISABLE:
588 sleep_time = fib_walk_process_queues(vm, quota);
600 VLIB_REGISTER_NODE (fib_walk_process_node,static) = {
601 .function = fib_walk_process,
602 .type = VLIB_NODE_TYPE_PROCESS,
608 * @brief Allocate a new walk object
611 fib_walk_alloc (fib_node_type_t parent_type,
612 fib_node_index_t parent_index,
613 fib_walk_flags_t flags,
614 fib_node_back_walk_ctx_t *ctx)
618 pool_get(fib_walk_pool, fwalk);
620 fib_node_init(&fwalk->fw_node, FIB_NODE_TYPE_WALK);
622 fwalk->fw_flags = flags;
623 fwalk->fw_dep_sibling = FIB_NODE_INDEX_INVALID;
624 fwalk->fw_prio_sibling = FIB_NODE_INDEX_INVALID;
625 fwalk->fw_parent.fnp_index = parent_index;
626 fwalk->fw_parent.fnp_type = parent_type;
627 fwalk->fw_ctx = NULL;
628 fwalk->fw_start_time = vlib_time_now(vlib_get_main());
629 fwalk->fw_n_visits = 0;
632 * make a copy of the backwalk context so the depth count remains
633 * the same for each sibling visitsed. This is important in the case
634 * where a parent has a loop via one child, but all the others are not.
635 * if the looped child were visited first, the depth count would exceed, the
636 * max and the walk would terminate before it reached the other siblings.
638 vec_add1(fwalk->fw_ctx, *ctx);
644 * @brief Enqueue a walk onto the appropriate priority queue. Then signal
645 * the background process there is work to do.
648 fib_walk_prio_queue_enquue (fib_walk_priority_t prio,
653 sibling = fib_node_list_push_front(fib_walk_queues.fwqs_queues[prio].fwq_queue,
656 fib_walk_get_index(fwalk));
657 fib_walk_queues.fwqs_queues[prio].fwq_stats[FIB_WALK_SCHEDULED]++;
660 * poke the fib-walk process to perform the async walk.
661 * we are not passing it specific data, hence the last two args,
662 * the process will drain the queues
664 vlib_process_signal_event(vlib_get_main(),
665 fib_walk_process_node.index,
666 FIB_WALK_PROCESS_EVENT_DATA,
673 fib_walk_async (fib_node_type_t parent_type,
674 fib_node_index_t parent_index,
675 fib_walk_priority_t prio,
676 fib_node_back_walk_ctx_t *ctx)
680 if (FIB_NODE_GRAPH_MAX_DEPTH < ++ctx->fnbw_depth)
683 * The walk has reached the maximum depth. there is a loop in the graph.
688 if (0 == fib_node_get_n_children(parent_type,
692 * no children to walk - quit now
696 if (ctx->fnbw_flags & FIB_NODE_BW_FLAG_FORCE_SYNC)
699 * the originator of the walk wanted it to be synchronous, but the
700 * parent object chose async - denied.
702 return (fib_walk_sync(parent_type, parent_index, ctx));
706 fwalk = fib_walk_alloc(parent_type,
711 fwalk->fw_dep_sibling = fib_node_child_add(parent_type,
714 fib_walk_get_index(fwalk));
716 fwalk->fw_prio_sibling = fib_walk_prio_queue_enquue(prio, fwalk);
720 * @brief Back walk all the children of a FIB node.
722 * note this is a synchronous depth first walk. Children visited may propagate
723 * the walk to thier children. Other children node types may not propagate,
724 * synchronously but instead queue the walk for later async completion.
727 fib_walk_sync (fib_node_type_t parent_type,
728 fib_node_index_t parent_index,
729 fib_node_back_walk_ctx_t *ctx)
731 fib_walk_advance_rc_t rc;
732 fib_node_index_t fwi;
735 if (FIB_NODE_GRAPH_MAX_DEPTH < ++ctx->fnbw_depth)
738 * The walk has reached the maximum depth. there is a loop in the graph.
743 if (0 == fib_node_get_n_children(parent_type,
747 * no children to walk - quit now
752 fwalk = fib_walk_alloc(parent_type,
757 fwalk->fw_dep_sibling = fib_node_child_add(parent_type,
760 fib_walk_get_index(fwalk));
761 fwi = fib_walk_get_index(fwalk);
766 * set this walk as executing
768 fwalk->fw_flags |= FIB_WALK_FLAG_EXECUTING;
772 rc = fib_walk_advance(fwi);
773 } while (FIB_WALK_ADVANCE_MORE == rc);
777 * this walk function is re-entrant - walks can spawn walks.
778 * fib_walk_t objects come from a pool, so they can realloc. we need
779 * to re-fetch from said pool at the appropriate times.
781 fwalk = fib_walk_get(fwi);
783 if (FIB_WALK_ADVANCE_MERGE == rc)
786 * this sync walk merged with an walk in front.
787 * by reqeusting a sync walk the client wanted all children walked,
788 * so we ditch the walk object in hand and continue with the one
791 fib_node_ptr_t merged_walk;
793 fib_node_list_elt_get_next(fwalk->fw_dep_sibling, &merged_walk);
795 ASSERT(FIB_NODE_INDEX_INVALID != merged_walk.fnp_index);
796 ASSERT(FIB_NODE_TYPE_WALK == merged_walk.fnp_type);
798 fib_walk_destroy(fwi);
800 fwi = merged_walk.fnp_index;
801 fwalk = fib_walk_get(fwi);
803 if (FIB_WALK_FLAG_EXECUTING & fwalk->fw_flags)
806 * we are executing a sync walk, and we have met with another
807 * walk that is also executing. since only one walk executs at once
808 * (there is no multi-threading) this implies we have met ourselves
809 * and hence the is a loop in the graph.
810 * This function is re-entrant, so the walk object we met is being
811 * acted on in a stack frame below this one. We must therefore not
812 * continue with it now, but let the stack unwind and along the
813 * appropriate frame to read the depth count and bail.
822 * the walk reached the end of the depdency list.
830 fib_walk_destroy(fwi);
835 fib_walk_get_node (fib_node_index_t index)
839 fwalk = fib_walk_get(index);
841 return (&(fwalk->fw_node));
845 * Walk objects are not parents, nor are they locked.
849 fib_walk_last_lock_gone (fib_node_t *node)
855 fib_walk_get_from_node (fib_node_t *node)
857 return ((fib_walk_t*)(((char*)node) -
858 STRUCT_OFFSET_OF(fib_walk_t, fw_node)));
862 * @brief Another back walk has reach this walk.
863 * Megre them so there is only one left. It is this node being
864 * visited that will remain, so copy or merge the context onto it.
866 static fib_node_back_walk_rc_t
867 fib_walk_back_walk_notify (fib_node_t *node,
868 fib_node_back_walk_ctx_t *ctx)
870 fib_node_back_walk_ctx_t *last;
873 fwalk = fib_walk_get_from_node(node);
876 * check whether the walk context can be merged with the most recent.
877 * the most recent was the one last added and is thus at the back of the vector.
878 * we can merge walks if the reason for the walk is the same.
880 last = vec_end(fwalk->fw_ctx) - 1;
882 if (last->fnbw_reason == ctx->fnbw_reason)
885 * copy the largest of the depth values. in the presence of a loop,
886 * the same walk will merge with itself. if we take the smaller depth
887 * then it will never end.
889 last->fnbw_depth = ((last->fnbw_depth >= ctx->fnbw_depth) ?
896 * walks could not be merged, this means that the walk infront needs to
897 * perform different action to this one that has caught up. the one in
898 * front was scheduled first so append the new walk context to the back
901 vec_add1(fwalk->fw_ctx, *ctx);
904 return (FIB_NODE_BACK_WALK_MERGE);
908 * The FIB walk's graph node virtual function table
910 static const fib_node_vft_t fib_walk_vft = {
911 .fnv_get = fib_walk_get_node,
912 .fnv_last_lock = fib_walk_last_lock_gone,
913 .fnv_back_walk = fib_walk_back_walk_notify,
917 fib_walk_module_init (void)
919 fib_walk_priority_t prio;
921 FOR_EACH_FIB_WALK_PRIORITY(prio)
923 fib_walk_queues.fwqs_queues[prio].fwq_queue = fib_node_list_create();
926 fib_node_register_type(FIB_NODE_TYPE_WALK, &fib_walk_vft);
930 format_fib_walk (u8* s, va_list ap)
932 fib_node_index_t fwi = va_arg(ap, fib_node_index_t);
935 fwalk = fib_walk_get(fwi);
937 return (format(s, " parent:{%s:%d} visits:%d flags:%d",
938 fib_node_type_get_name(fwalk->fw_parent.fnp_type),
939 fwalk->fw_parent.fnp_index,
944 static clib_error_t *
945 fib_walk_show (vlib_main_t * vm,
946 unformat_input_t * input,
947 vlib_cli_command_t * cmd)
949 fib_walk_queue_stats_t wqs;
950 fib_walk_priority_t prio;
951 fib_node_ptr_t sibling;
952 fib_node_index_t fwi;
958 vlib_cli_output(vm, "FIB Walk Quota = %.2fusec:", quota * USEC);
959 vlib_cli_output(vm, "FIB Walk queues:");
961 FOR_EACH_FIB_WALK_PRIORITY(prio)
963 vlib_cli_output(vm, " %U priority queue:",
964 format_fib_walk_priority, prio);
965 vlib_cli_output(vm, " Stats: ");
967 FOR_EACH_FIB_WALK_QUEUE_STATS(wqs)
969 vlib_cli_output(vm, " %U:%d",
970 format_fib_walk_queue_stats, wqs,
971 fib_walk_queues.fwqs_queues[prio].fwq_stats[wqs]);
973 vlib_cli_output(vm, " Occupancy:%d",
974 fib_node_list_get_size(
975 fib_walk_queues.fwqs_queues[prio].fwq_queue));
977 more_elts = fib_node_list_get_front(
978 fib_walk_queues.fwqs_queues[prio].fwq_queue,
983 ASSERT(FIB_NODE_INDEX_INVALID != sibling.fnp_index);
984 ASSERT(FIB_NODE_TYPE_WALK == sibling.fnp_type);
986 fwi = sibling.fnp_index;
987 fwalk = fib_walk_get(fwi);
989 vlib_cli_output(vm, " %U", format_fib_walk, fwi);
991 more_elts = fib_node_list_elt_get_next(fwalk->fw_prio_sibling,
996 vlib_cli_output(vm, "Histogram Statistics:");
997 vlib_cli_output(vm, " Number of Elements visit per-quota:");
998 for (ii = 0; ii < N_ELTS_BUCKETS; ii++)
1000 if (0 != fib_walk_work_nodes_visited[ii])
1001 s = format(s, "%d:%d ",
1002 (ii * fib_walk_work_nodes_visisted_incr),
1003 fib_walk_work_nodes_visited[ii]);
1005 vlib_cli_output(vm, " %v", s);
1008 vlib_cli_output(vm, " Time consumed per-quota (Quota=%f usec):", quota*USEC);
1009 s = format(s, "0:%d ", fib_walk_work_time_taken[0]);
1010 for (ii = 1; ii < N_TIME_BUCKETS; ii++)
1012 if (0 != fib_walk_work_time_taken[ii])
1013 s = format(s, "%d:%d ", (u32)((((ii - N_TIME_BUCKETS/2) *
1014 (quota / TIME_INCREMENTS)) + quota) *
1016 fib_walk_work_time_taken[ii]);
1018 vlib_cli_output(vm, " %v", s);
1021 vlib_cli_output(vm, " Sleep Types:");
1022 vlib_cli_output(vm, " Short Long:");
1023 vlib_cli_output(vm, " %d %d:",
1024 fib_walk_sleep_lengths[FIB_WALK_SHORT_SLEEP],
1025 fib_walk_sleep_lengths[FIB_WALK_LONG_SLEEP]);
1027 vlib_cli_output(vm, " Number of Elements visited per-walk:");
1028 for (ii = 0; ii < HISTOGRAM_VISITS_PER_WALK_N_BUCKETS; ii++)
1030 if (0 != fib_walk_hist_vists_per_walk[ii])
1031 s = format(s, "%d:%d ",
1032 ii*HISTOGRAM_VISITS_PER_WALK_INCR,
1033 fib_walk_hist_vists_per_walk[ii]);
1035 vlib_cli_output(vm, " %v", s);
1039 vlib_cli_output(vm, "Brief History (last %d walks):", HISTORY_N_WALKS);
1040 ii = history_last_walk_pos - 1;
1042 ii = HISTORY_N_WALKS - 1;
1044 while (ii != history_last_walk_pos)
1046 if (0 != fib_walk_history[ii].fwh_reason[0])
1048 fib_node_back_walk_reason_t reason;
1052 s = format(s, "[@%d]: %s:%d visits:%d duration:%.2f completed:%.2f ",
1053 ii, fib_node_type_get_name(fib_walk_history[ii].fwh_parent.fnp_type),
1054 fib_walk_history[ii].fwh_parent.fnp_index,
1055 fib_walk_history[ii].fwh_n_visits,
1056 fib_walk_history[ii].fwh_duration,
1057 fib_walk_history[ii].fwh_completed);
1058 if (FIB_WALK_FLAG_SYNC & fib_walk_history[ii].fwh_flags)
1059 s = format(s, "sync, ");
1060 if (FIB_WALK_FLAG_ASYNC & fib_walk_history[ii].fwh_flags)
1061 s = format(s, "async, ");
1063 s = format(s, "reason:");
1065 while (0 != fib_walk_history[ii].fwh_reason[jj])
1067 FOR_EACH_FIB_NODE_BW_REASON(reason) {
1068 if ((1<<reason) & fib_walk_history[ii].fwh_reason[jj]) {
1069 s = format (s, "%s,", fib_node_bw_reason_names[reason]);
1074 vlib_cli_output(vm, "%v", s);
1079 ii = HISTORY_N_WALKS - 1;
1085 VLIB_CLI_COMMAND (fib_walk_show_command, static) = {
1086 .path = "show fib walk",
1087 .short_help = "show fib walk",
1088 .function = fib_walk_show,
1091 static clib_error_t *
1092 fib_walk_set_quota (vlib_main_t * vm,
1093 unformat_input_t * input,
1094 vlib_cli_command_t * cmd)
1096 clib_error_t * error = NULL;
1099 if (unformat (input, "%f", &new_quota))
1105 error = clib_error_return(0 , "Pass a float value");
1111 VLIB_CLI_COMMAND (fib_walk_set_quota_command, static) = {
1112 .path = "set fib walk quota",
1113 .short_help = "set fib walk quota",
1114 .function = fib_walk_set_quota,
1117 static clib_error_t *
1118 fib_walk_set_histogram_elements_size (vlib_main_t * vm,
1119 unformat_input_t * input,
1120 vlib_cli_command_t * cmd)
1122 clib_error_t * error = NULL;
1125 if (unformat (input, "%d", &new))
1127 fib_walk_work_nodes_visisted_incr = new;
1131 error = clib_error_return(0 , "Pass an int value");
1137 VLIB_CLI_COMMAND (fib_walk_set_histogram_elements_size_command, static) = {
1138 .path = "set fib walk histogram elements size",
1139 .short_help = "set fib walk histogram elements size",
1140 .function = fib_walk_set_histogram_elements_size,
1143 static clib_error_t *
1144 fib_walk_clear (vlib_main_t * vm,
1145 unformat_input_t * input,
1146 vlib_cli_command_t * cmd)
1148 memset(fib_walk_hist_vists_per_walk, 0, sizeof(fib_walk_hist_vists_per_walk));
1149 memset(fib_walk_history, 0, sizeof(fib_walk_history));
1150 memset(fib_walk_work_time_taken, 0, sizeof(fib_walk_work_time_taken));
1151 memset(fib_walk_work_nodes_visited, 0, sizeof(fib_walk_work_nodes_visited));
1152 memset(fib_walk_sleep_lengths, 0, sizeof(fib_walk_sleep_lengths));
1157 VLIB_CLI_COMMAND (fib_walk_clear_command, static) = {
1158 .path = "clear fib walk",
1159 .short_help = "clear fib walk",
1160 .function = fib_walk_clear,
1164 fib_walk_process_enable (void)
1166 vlib_process_signal_event(vlib_get_main(),
1167 fib_walk_process_node.index,
1168 FIB_WALK_PROCESS_EVENT_ENABLE,
1173 fib_walk_process_disable (void)
1175 vlib_process_signal_event(vlib_get_main(),
1176 fib_walk_process_node.index,
1177 FIB_WALK_PROCESS_EVENT_DISABLE,
1181 static clib_error_t *
1182 fib_walk_process_enable_disable (vlib_main_t * vm,
1183 unformat_input_t * input,
1184 vlib_cli_command_t * cmd)
1186 if (unformat (input, "enable"))
1188 fib_walk_process_enable();
1190 else if (unformat (input, "disable"))
1192 fib_walk_process_disable();
1196 return clib_error_return(0, "choose enable or disable");
1201 VLIB_CLI_COMMAND (fib_walk_process_command, static) = {
1202 .path = "test fib-walk-process",
1203 .short_help = "test fib-walk-process [enable|disable]",
1204 .function = fib_walk_process_enable_disable,