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>
19 vlib_log_class_t fib_walk_logger;
24 typedef enum fib_walk_flags_t_
28 * This walk will run to completion, i.e. visit ALL the children.
29 * It is a depth first traversal of the graph.
31 FIB_WALK_FLAG_SYNC = (1 << 0),
33 * An asynchronous walk.
34 * This walk will be scheduled to run in the background. It will thus visits
35 * the children at a later point in time.
36 * It is a depth first traversal of the graph.
38 FIB_WALK_FLAG_ASYNC = (1 << 1),
40 * An indication that the walk is currently executing.
42 FIB_WALK_FLAG_EXECUTING = (1 << 2),
46 * A representation of a graph walk from a parent object to its children
48 typedef struct fib_walk_t_
51 * FIB node linkage. This object is not in the FIB object graph,
52 * but it is present in other node's dependency lists, so it needs to
60 fib_walk_flags_t fw_flags;
63 * Sibling index in the dependency list
68 * Sibling index in the list of all walks
73 * Pointer to the node whose dependants this walk is walking
75 fib_node_ptr_t fw_parent;
78 * Number of nodes visited by this walk. saved for debugging purposes.
83 * Time the walk started
88 * The reasons this walk is occuring.
89 * This is a vector ordered in time. The reasons and the front were started
90 * first, and so should be acted first when a node is visisted.
92 fib_node_back_walk_ctx_t *fw_ctx;
96 * @brief The pool of all walk objects
98 static fib_walk_t *fib_walk_pool;
101 * Statistics maintained per-walk queue
103 typedef enum fib_walk_queue_stats_t_
107 } fib_walk_queue_stats_t;
108 #define FIB_WALK_QUEUE_STATS_NUM ((fib_walk_queue_stats_t)(FIB_WALK_COMPLETED+1))
110 #define FIB_WALK_QUEUE_STATS { \
111 [FIB_WALK_SCHEDULED] = "scheduled", \
112 [FIB_WALK_COMPLETED] = "completed", \
115 #define FOR_EACH_FIB_WALK_QUEUE_STATS(_wqs) \
116 for ((_wqs) = FIB_WALK_SCHEDULED; \
117 (_wqs) < FIB_WALK_QUEUE_STATS_NUM; \
121 * The names of the walk stats
123 static const char * const fib_walk_queue_stats_names[] = FIB_WALK_QUEUE_STATS;
125 * The names of the walk reasons
127 static const char * const fib_node_bw_reason_names[] = FIB_NODE_BW_REASONS;
130 * A represenation of one queue of walk
132 typedef struct fib_walk_queue_t_
137 u64 fwq_stats[FIB_WALK_QUEUE_STATS_NUM];
140 * The node list which acts as the queue
142 fib_node_list_t fwq_queue;
146 * A set of priority queues for outstanding walks
148 typedef struct fib_walk_queues_t_
150 fib_walk_queue_t fwqs_queues[FIB_WALK_PRIORITY_NUM];
154 * The global queues of outstanding walks
156 static fib_walk_queues_t fib_walk_queues;
159 * The names of the walk priorities
161 static const char * const fib_walk_priority_names[] = FIB_WALK_PRIORITIES;
164 * @brief Histogram stats on the lenths of each walk in elemenets visisted.
165 * Store upto 1<<23 elements in increments of 1<<10
167 #define HISTOGRAM_VISITS_PER_WALK_MAX (1<<23)
168 #define HISTOGRAM_VISITS_PER_WALK_INCR (1<<10)
169 #define HISTOGRAM_VISITS_PER_WALK_N_BUCKETS \
170 (HISTOGRAM_VISITS_PER_WALK_MAX/HISTOGRAM_VISITS_PER_WALK_INCR)
171 static u64 fib_walk_hist_vists_per_walk[HISTOGRAM_VISITS_PER_WALK_N_BUCKETS];
174 * @brief History of state for the last 128 walks
176 #define HISTORY_N_WALKS 128
177 #define MAX_HISTORY_REASONS 16
178 static u32 history_last_walk_pos;
179 typedef struct fib_walk_history_t_ {
183 fib_node_ptr_t fwh_parent;
184 fib_walk_flags_t fwh_flags;
185 fib_node_bw_reason_flag_t fwh_reason[MAX_HISTORY_REASONS];
186 } fib_walk_history_t;
187 static fib_walk_history_t fib_walk_history[HISTORY_N_WALKS];
189 static u8* format_fib_walk (u8* s, va_list *ap);
191 #define FIB_WALK_DBG(_walk, _fmt, _args...) \
193 vlib_log_debug(fib_walk_logger, \
196 fib_walk_get_index(_walk), \
201 format_fib_walk_priority (u8 *s, va_list *ap)
203 fib_walk_priority_t prio = va_arg(*ap, fib_walk_priority_t);
205 ASSERT(prio < FIB_WALK_PRIORITY_NUM);
207 return (format(s, "%s", fib_walk_priority_names[prio]));
210 format_fib_walk_queue_stats (u8 *s, va_list *ap)
212 fib_walk_queue_stats_t wqs = va_arg(*ap, fib_walk_queue_stats_t);
214 ASSERT(wqs < FIB_WALK_QUEUE_STATS_NUM);
216 return (format(s, "%s", fib_walk_queue_stats_names[wqs]));
220 fib_walk_get_index (fib_walk_t *fwalk)
222 return (fwalk - fib_walk_pool);
226 fib_walk_get (index_t fwi)
228 return (pool_elt_at_index(fib_walk_pool, fwi));
232 * not static so it can be used in the unit tests
235 fib_walk_queue_get_size (fib_walk_priority_t prio)
237 return (fib_node_list_get_size(fib_walk_queues.fwqs_queues[prio].fwq_queue));
240 static fib_node_index_t
241 fib_walk_queue_get_front (fib_walk_priority_t prio)
245 fib_node_list_get_front(fib_walk_queues.fwqs_queues[prio].fwq_queue, &wp);
247 return (wp.fnp_index);
251 fib_walk_destroy (index_t fwi)
256 fwalk = fib_walk_get(fwi);
258 if (FIB_NODE_INDEX_INVALID != fwalk->fw_prio_sibling)
260 fib_node_list_elt_remove(fwalk->fw_prio_sibling);
262 fib_node_child_remove(fwalk->fw_parent.fnp_type,
263 fwalk->fw_parent.fnp_index,
264 fwalk->fw_dep_sibling);
267 * refetch the walk object. More walks could have been spawned as a result
268 * of releasing the lock on the parent.
270 fwalk = fib_walk_get(fwi);
273 * add the stats to the continuous histogram collection.
275 bucket = (fwalk->fw_n_visits / HISTOGRAM_VISITS_PER_WALK_INCR);
276 bucket = (bucket >= HISTOGRAM_VISITS_PER_WALK_N_BUCKETS ?
277 HISTOGRAM_VISITS_PER_WALK_N_BUCKETS - 1 :
279 fib_walk_hist_vists_per_walk[bucket]++;
282 * save stats to the recent history
285 fib_walk_history[history_last_walk_pos].fwh_n_visits =
287 fib_walk_history[history_last_walk_pos].fwh_completed =
288 vlib_time_now(vlib_get_main());
289 fib_walk_history[history_last_walk_pos].fwh_duration =
290 fib_walk_history[history_last_walk_pos].fwh_completed -
291 fwalk->fw_start_time;
292 fib_walk_history[history_last_walk_pos].fwh_parent =
294 fib_walk_history[history_last_walk_pos].fwh_flags =
297 vec_foreach_index(ii, fwalk->fw_ctx)
299 if (ii < MAX_HISTORY_REASONS)
301 fib_walk_history[history_last_walk_pos].fwh_reason[ii] =
302 fwalk->fw_ctx[ii].fnbw_reason;
306 history_last_walk_pos = (history_last_walk_pos + 1) % HISTORY_N_WALKS;
308 fib_node_deinit(&fwalk->fw_node);
309 vec_free(fwalk->fw_ctx);
310 pool_put(fib_walk_pool, fwalk);
314 * return code when advancing a walk
316 typedef enum fib_walk_advance_rc_t_
319 * The walk is complete
321 FIB_WALK_ADVANCE_DONE,
323 * the walk has more work
325 FIB_WALK_ADVANCE_MORE,
327 * The walk merged with the one in front
329 FIB_WALK_ADVANCE_MERGE,
330 } fib_walk_advance_rc_t;
333 * @brief Advance the walk one element in its work list
335 static fib_walk_advance_rc_t
336 fib_walk_advance (fib_node_index_t fwi)
338 fib_node_back_walk_rc_t wrc;
339 fib_node_ptr_t sibling;
345 * this walk function is re-entrant - walks acan spawn walks.
346 * fib_walk_t objects come from a pool, so they can realloc. we need
347 * to retch from said pool at the appropriate times.
349 fwalk = fib_walk_get(fwi);
351 more_elts = fib_node_list_elt_get_next(fwalk->fw_dep_sibling, &sibling);
357 * loop through the backwalk contexts. This can grow in length
358 * as walks on the same object meet each other. Order is preserved so the
359 * most recently started walk as at the back of the vector.
362 n_ctxs = vec_len(fwalk->fw_ctx);
366 wrc = fib_node_back_walk_one(&sibling, &fwalk->fw_ctx[ii]);
369 fwalk = fib_walk_get(fwi);
370 fwalk->fw_n_visits++;
372 if (FIB_NODE_BACK_WALK_MERGE == wrc)
375 * this walk has merged with the one further along the node's
378 return (FIB_WALK_ADVANCE_MERGE);
382 * re-evaluate the number of backwalk contexts we need to process.
384 n_ctxs = vec_len(fwalk->fw_ctx);
387 * move foward to the next node to visit
389 more_elts = fib_node_list_advance(fwalk->fw_dep_sibling);
394 return (FIB_WALK_ADVANCE_MORE);
397 return (FIB_WALK_ADVANCE_DONE);
401 * @breif Enurmerate the times of sleep between walks
403 typedef enum fib_walk_sleep_type_t_
405 FIB_WALK_SHORT_SLEEP,
407 } fib_walk_sleep_type_t;
409 #define FIB_WALK_N_SLEEP (FIB_WALK_LONG_SLEEP+1)
412 * @brief Durations for the sleep types
414 static f64 fib_walk_sleep_duration[] = {
416 * Long sleep when there is no more work, i.e. the queues are empty.
417 * This is a sleep (as opposed to a wait for event) just to be sure we
418 * are not missing events by sleeping forever.
420 [FIB_WALK_LONG_SLEEP] = 2,
423 * Short sleep. There is work left in the queues. We are yielding the CPU
426 [FIB_WALK_SHORT_SLEEP] = 1e-8,
430 * @brief The time quota for a walk. When more than this amount of time is
431 * spent, the walk process will yield.
433 static f64 quota = 1e-4;
436 * Histogram on the amount of work done (in msecs) in each walk
438 #define N_TIME_BUCKETS 128
439 #define TIME_INCREMENTS (N_TIME_BUCKETS/2)
440 static u64 fib_walk_work_time_taken[N_TIME_BUCKETS];
443 * Histogram on the number of nodes visted in each quota
445 #define N_ELTS_BUCKETS 128
446 static u32 fib_walk_work_nodes_visisted_incr = 2;
447 static u64 fib_walk_work_nodes_visited[N_ELTS_BUCKETS];
450 * Histogram of the sleep lengths
452 static u64 fib_walk_sleep_lengths[2];
455 * @brief Service the queues
456 * This is not declared static so that it can be unit tested - i know i know...
459 fib_walk_process_queues (vlib_main_t * vm,
462 f64 start_time, consumed_time;
463 fib_walk_sleep_type_t sleep;
464 fib_walk_priority_t prio;
465 fib_walk_advance_rc_t rc;
466 fib_node_index_t fwi;
472 start_time = vlib_time_now(vm);
475 FOR_EACH_FIB_WALK_PRIORITY(prio)
477 while (0 != fib_walk_queue_get_size(prio))
479 fwi = fib_walk_queue_get_front(prio);
482 * set this walk as executing
484 fwalk = fib_walk_get(fwi);
485 fwalk->fw_flags |= FIB_WALK_FLAG_EXECUTING;
489 rc = fib_walk_advance(fwi);
491 consumed_time = (vlib_time_now(vm) - start_time);
492 } while ((consumed_time < quota) &&
493 (FIB_WALK_ADVANCE_MORE == rc));
496 * if this walk has no more work then pop it from the queue
497 * and move on to the next.
499 if (FIB_WALK_ADVANCE_MORE != rc)
501 fib_walk_destroy(fwi);
502 fib_walk_queues.fwqs_queues[prio].fwq_stats[FIB_WALK_COMPLETED]++;
507 * passed our work quota. sleep time.
509 fwalk = fib_walk_get(fwi);
510 fwalk->fw_flags &= ~FIB_WALK_FLAG_EXECUTING;
511 sleep = FIB_WALK_SHORT_SLEEP;
512 goto that_will_do_for_now;
517 * got to the end of all the work
519 sleep = FIB_WALK_LONG_SLEEP;
521 that_will_do_for_now:
525 * - for the number of nodes visisted we store 128 increments
526 * - for the time consumed we store quota/TIME_INCREMENTS increments.
528 bucket = ((n_elts/fib_walk_work_nodes_visisted_incr) > N_ELTS_BUCKETS ?
530 n_elts/fib_walk_work_nodes_visisted_incr);
531 ++fib_walk_work_nodes_visited[bucket];
533 bucket = (consumed_time - quota) / (quota / TIME_INCREMENTS);
534 bucket += N_TIME_BUCKETS/2;
535 bucket = (bucket < 0 ? 0 : bucket);
536 bucket = (bucket > N_TIME_BUCKETS-1 ? N_TIME_BUCKETS-1 : bucket);
537 ++fib_walk_work_time_taken[bucket];
539 ++fib_walk_sleep_lengths[sleep];
541 return (fib_walk_sleep_duration[sleep]);
545 * Events sent to the FIB walk process
547 typedef enum fib_walk_process_event_t_
549 FIB_WALK_PROCESS_EVENT_DATA,
550 FIB_WALK_PROCESS_EVENT_ENABLE,
551 FIB_WALK_PROCESS_EVENT_DISABLE,
552 } fib_walk_process_event;
555 * @brief The 'fib-walk' process's main loop.
558 fib_walk_process (vlib_main_t * vm,
559 vlib_node_runtime_t * node,
562 uword event_type, *event_data = 0;
567 sleep_time = fib_walk_sleep_duration[FIB_WALK_SHORT_SLEEP];
572 * the feature to disable/enable this walk process is only
573 * for testing purposes
577 vlib_process_wait_for_event_or_clock(vm, sleep_time);
581 vlib_process_wait_for_event(vm);
584 event_type = vlib_process_get_events(vm, &event_data);
585 vec_reset_length(event_data);
589 case FIB_WALK_PROCESS_EVENT_ENABLE:
592 case FIB_WALK_PROCESS_EVENT_DISABLE:
601 sleep_time = fib_walk_process_queues(vm, quota);
613 VLIB_REGISTER_NODE (fib_walk_process_node,static) = {
614 .function = fib_walk_process,
615 .type = VLIB_NODE_TYPE_PROCESS,
621 * @brief Allocate a new walk object
624 fib_walk_alloc (fib_node_type_t parent_type,
625 fib_node_index_t parent_index,
626 fib_walk_flags_t flags,
627 fib_node_back_walk_ctx_t *ctx)
631 pool_get(fib_walk_pool, fwalk);
633 fib_node_init(&fwalk->fw_node, FIB_NODE_TYPE_WALK);
635 fwalk->fw_flags = flags;
636 fwalk->fw_dep_sibling = FIB_NODE_INDEX_INVALID;
637 fwalk->fw_prio_sibling = FIB_NODE_INDEX_INVALID;
638 fwalk->fw_parent.fnp_index = parent_index;
639 fwalk->fw_parent.fnp_type = parent_type;
640 fwalk->fw_ctx = NULL;
641 fwalk->fw_start_time = vlib_time_now(vlib_get_main());
642 fwalk->fw_n_visits = 0;
645 * make a copy of the backwalk context so the depth count remains
646 * the same for each sibling visitsed. This is important in the case
647 * where a parent has a loop via one child, but all the others are not.
648 * if the looped child were visited first, the depth count would exceed, the
649 * max and the walk would terminate before it reached the other siblings.
651 vec_add1(fwalk->fw_ctx, *ctx);
657 * @brief Enqueue a walk onto the appropriate priority queue. Then signal
658 * the background process there is work to do.
661 fib_walk_prio_queue_enquue (fib_walk_priority_t prio,
666 sibling = fib_node_list_push_front(fib_walk_queues.fwqs_queues[prio].fwq_queue,
669 fib_walk_get_index(fwalk));
670 fib_walk_queues.fwqs_queues[prio].fwq_stats[FIB_WALK_SCHEDULED]++;
673 * poke the fib-walk process to perform the async walk.
674 * we are not passing it specific data, hence the last two args,
675 * the process will drain the queues
677 vlib_process_signal_event(vlib_get_main(),
678 fib_walk_process_node.index,
679 FIB_WALK_PROCESS_EVENT_DATA,
686 fib_walk_async (fib_node_type_t parent_type,
687 fib_node_index_t parent_index,
688 fib_walk_priority_t prio,
689 fib_node_back_walk_ctx_t *ctx)
693 if (FIB_NODE_GRAPH_MAX_DEPTH < ++ctx->fnbw_depth)
696 * The walk has reached the maximum depth. there is a loop in the graph.
701 if (0 == fib_node_get_n_children(parent_type,
705 * no children to walk - quit now
709 if (ctx->fnbw_flags & FIB_NODE_BW_FLAG_FORCE_SYNC)
712 * the originator of the walk wanted it to be synchronous, but the
713 * parent object chose async - denied.
715 return (fib_walk_sync(parent_type, parent_index, ctx));
719 fwalk = fib_walk_alloc(parent_type,
724 fwalk->fw_dep_sibling = fib_node_child_add(parent_type,
727 fib_walk_get_index(fwalk));
729 fwalk->fw_prio_sibling = fib_walk_prio_queue_enquue(prio, fwalk);
731 FIB_WALK_DBG(fwalk, "async-start: %U",
732 format_fib_node_bw_reason, ctx->fnbw_reason);
736 * @brief Back walk all the children of a FIB node.
738 * note this is a synchronous depth first walk. Children visited may propagate
739 * the walk to thier children. Other children node types may not propagate,
740 * synchronously but instead queue the walk for later async completion.
743 fib_walk_sync (fib_node_type_t parent_type,
744 fib_node_index_t parent_index,
745 fib_node_back_walk_ctx_t *ctx)
747 fib_walk_advance_rc_t rc;
748 fib_node_index_t fwi;
751 if (FIB_NODE_GRAPH_MAX_DEPTH < ++ctx->fnbw_depth)
754 * The walk has reached the maximum depth. there is a loop in the graph.
759 if (0 == fib_node_get_n_children(parent_type,
763 * no children to walk - quit now
768 fwalk = fib_walk_alloc(parent_type,
773 fwalk->fw_dep_sibling = fib_node_child_add(parent_type,
776 fib_walk_get_index(fwalk));
777 fwi = fib_walk_get_index(fwalk);
778 FIB_WALK_DBG(fwalk, "sync-start: %U",
779 format_fib_node_bw_reason, ctx->fnbw_reason);
784 * set this walk as executing
786 fwalk->fw_flags |= FIB_WALK_FLAG_EXECUTING;
790 rc = fib_walk_advance(fwi);
791 } while (FIB_WALK_ADVANCE_MORE == rc);
795 * this walk function is re-entrant - walks can spawn walks.
796 * fib_walk_t objects come from a pool, so they can realloc. we need
797 * to re-fetch from said pool at the appropriate times.
799 fwalk = fib_walk_get(fwi);
801 if (FIB_WALK_ADVANCE_MERGE == rc)
804 * this sync walk merged with an walk in front.
805 * by reqeusting a sync walk the client wanted all children walked,
806 * so we ditch the walk object in hand and continue with the one
809 fib_node_ptr_t merged_walk;
811 fib_node_list_elt_get_next(fwalk->fw_dep_sibling, &merged_walk);
813 ASSERT(FIB_NODE_INDEX_INVALID != merged_walk.fnp_index);
814 ASSERT(FIB_NODE_TYPE_WALK == merged_walk.fnp_type);
816 fib_walk_destroy(fwi);
818 fwi = merged_walk.fnp_index;
819 fwalk = fib_walk_get(fwi);
821 if (FIB_WALK_FLAG_EXECUTING & fwalk->fw_flags)
824 * we are executing a sync walk, and we have met with another
825 * walk that is also executing. since only one walk executs at once
826 * (there is no multi-threading) this implies we have met ourselves
827 * and hence the is a loop in the graph.
828 * This function is re-entrant, so the walk object we met is being
829 * acted on in a stack frame below this one. We must therefore not
830 * continue with it now, but let the stack unwind and along the
831 * appropriate frame to read the depth count and bail.
833 FIB_WALK_DBG(fwalk, "sync-stop: %U",
834 format_fib_node_bw_reason,
844 * the walk reached the end of the depdency list.
852 FIB_WALK_DBG(fwalk, "sync-stop: %U",
853 format_fib_node_bw_reason,
855 fib_walk_destroy(fwi);
860 fib_walk_get_node (fib_node_index_t index)
864 fwalk = fib_walk_get(index);
866 return (&(fwalk->fw_node));
870 * Walk objects are not parents, nor are they locked.
874 fib_walk_last_lock_gone (fib_node_t *node)
880 fib_walk_get_from_node (fib_node_t *node)
882 return ((fib_walk_t*)(((char*)node) -
883 STRUCT_OFFSET_OF(fib_walk_t, fw_node)));
887 * @brief Another back walk has reach this walk.
888 * Megre them so there is only one left. It is this node being
889 * visited that will remain, so copy or merge the context onto it.
891 static fib_node_back_walk_rc_t
892 fib_walk_back_walk_notify (fib_node_t *node,
893 fib_node_back_walk_ctx_t *ctx)
895 fib_node_back_walk_ctx_t *last;
898 fwalk = fib_walk_get_from_node(node);
901 * check whether the walk context can be merged with the most recent.
902 * the most recent was the one last added and is thus at the back of the vector.
903 * we can merge walks if the reason for the walk is the same.
905 last = vec_end(fwalk->fw_ctx) - 1;
907 if (last->fnbw_reason == ctx->fnbw_reason)
910 * copy the largest of the depth values. in the presence of a loop,
911 * the same walk will merge with itself. if we take the smaller depth
912 * then it will never end.
914 last->fnbw_depth = ((last->fnbw_depth >= ctx->fnbw_depth) ?
921 * walks could not be merged, this means that the walk infront needs to
922 * perform different action to this one that has caught up. the one in
923 * front was scheduled first so append the new walk context to the back
926 vec_add1(fwalk->fw_ctx, *ctx);
929 return (FIB_NODE_BACK_WALK_MERGE);
933 * The FIB walk's graph node virtual function table
935 static const fib_node_vft_t fib_walk_vft = {
936 .fnv_get = fib_walk_get_node,
937 .fnv_last_lock = fib_walk_last_lock_gone,
938 .fnv_back_walk = fib_walk_back_walk_notify,
942 fib_walk_module_init (void)
944 fib_walk_priority_t prio;
946 FOR_EACH_FIB_WALK_PRIORITY(prio)
948 fib_walk_queues.fwqs_queues[prio].fwq_queue = fib_node_list_create();
951 fib_node_register_type(FIB_NODE_TYPE_WALK, &fib_walk_vft);
952 fib_walk_logger = vlib_log_register_class("fib", "walk");
956 format_fib_walk (u8* s, va_list *ap)
958 fib_node_index_t fwi = va_arg(*ap, fib_node_index_t);
961 fwalk = fib_walk_get(fwi);
963 return (format(s, "[@%d] parent:{%s:%d} visits:%d flags:%d", fwi,
964 fib_node_type_get_name(fwalk->fw_parent.fnp_type),
965 fwalk->fw_parent.fnp_index,
971 format_fib_node_bw_reason (u8 *s, va_list *args)
973 fib_node_bw_reason_flag_t flag = va_arg (*args, int);
974 fib_node_back_walk_reason_t reason;
976 FOR_EACH_FIB_NODE_BW_REASON(reason) {
977 if ((1<<reason) & flag)
978 s = format(s, "%s", fib_node_bw_reason_names[reason]);
984 static clib_error_t *
985 fib_walk_show (vlib_main_t * vm,
986 unformat_input_t * input,
987 vlib_cli_command_t * cmd)
989 fib_walk_queue_stats_t wqs;
990 fib_walk_priority_t prio;
991 fib_node_ptr_t sibling;
992 fib_node_index_t fwi;
998 vlib_cli_output(vm, "FIB Walk Quota = %.2fusec:", quota * USEC);
999 vlib_cli_output(vm, "FIB Walk queues:");
1001 FOR_EACH_FIB_WALK_PRIORITY(prio)
1003 vlib_cli_output(vm, " %U priority queue:",
1004 format_fib_walk_priority, prio);
1005 vlib_cli_output(vm, " Stats: ");
1007 FOR_EACH_FIB_WALK_QUEUE_STATS(wqs)
1009 vlib_cli_output(vm, " %U:%d",
1010 format_fib_walk_queue_stats, wqs,
1011 fib_walk_queues.fwqs_queues[prio].fwq_stats[wqs]);
1013 vlib_cli_output(vm, " Occupancy:%d",
1014 fib_node_list_get_size(
1015 fib_walk_queues.fwqs_queues[prio].fwq_queue));
1017 more_elts = fib_node_list_get_front(
1018 fib_walk_queues.fwqs_queues[prio].fwq_queue,
1023 ASSERT(FIB_NODE_INDEX_INVALID != sibling.fnp_index);
1024 ASSERT(FIB_NODE_TYPE_WALK == sibling.fnp_type);
1026 fwi = sibling.fnp_index;
1027 fwalk = fib_walk_get(fwi);
1029 vlib_cli_output(vm, " %U", format_fib_walk, fwi);
1031 more_elts = fib_node_list_elt_get_next(fwalk->fw_prio_sibling,
1036 vlib_cli_output(vm, "Histogram Statistics:");
1037 vlib_cli_output(vm, " Number of Elements visit per-quota:");
1038 for (ii = 0; ii < N_ELTS_BUCKETS; ii++)
1040 if (0 != fib_walk_work_nodes_visited[ii])
1041 s = format(s, "%d:%d ",
1042 (ii * fib_walk_work_nodes_visisted_incr),
1043 fib_walk_work_nodes_visited[ii]);
1045 vlib_cli_output(vm, " %v", s);
1048 vlib_cli_output(vm, " Time consumed per-quota (Quota=%f usec):", quota*USEC);
1049 s = format(s, "0:%d ", fib_walk_work_time_taken[0]);
1050 for (ii = 1; ii < N_TIME_BUCKETS; ii++)
1052 if (0 != fib_walk_work_time_taken[ii])
1053 s = format(s, "%d:%d ", (u32)((((ii - N_TIME_BUCKETS/2) *
1054 (quota / TIME_INCREMENTS)) + quota) *
1056 fib_walk_work_time_taken[ii]);
1058 vlib_cli_output(vm, " %v", s);
1061 vlib_cli_output(vm, " Sleep Types:");
1062 vlib_cli_output(vm, " Short Long:");
1063 vlib_cli_output(vm, " %d %d:",
1064 fib_walk_sleep_lengths[FIB_WALK_SHORT_SLEEP],
1065 fib_walk_sleep_lengths[FIB_WALK_LONG_SLEEP]);
1067 vlib_cli_output(vm, " Number of Elements visited per-walk:");
1068 for (ii = 0; ii < HISTOGRAM_VISITS_PER_WALK_N_BUCKETS; ii++)
1070 if (0 != fib_walk_hist_vists_per_walk[ii])
1071 s = format(s, "%d:%d ",
1072 ii*HISTOGRAM_VISITS_PER_WALK_INCR,
1073 fib_walk_hist_vists_per_walk[ii]);
1075 vlib_cli_output(vm, " %v", s);
1079 vlib_cli_output(vm, "Brief History (last %d walks):", HISTORY_N_WALKS);
1080 ii = history_last_walk_pos - 1;
1082 ii = HISTORY_N_WALKS - 1;
1084 while (ii != history_last_walk_pos)
1086 if (0 != fib_walk_history[ii].fwh_reason[0])
1091 s = format(s, "[@%d]: %s:%d visits:%d duration:%.2f completed:%.2f ",
1092 ii, fib_node_type_get_name(fib_walk_history[ii].fwh_parent.fnp_type),
1093 fib_walk_history[ii].fwh_parent.fnp_index,
1094 fib_walk_history[ii].fwh_n_visits,
1095 fib_walk_history[ii].fwh_duration,
1096 fib_walk_history[ii].fwh_completed);
1097 if (FIB_WALK_FLAG_SYNC & fib_walk_history[ii].fwh_flags)
1098 s = format(s, "sync, ");
1099 if (FIB_WALK_FLAG_ASYNC & fib_walk_history[ii].fwh_flags)
1100 s = format(s, "async, ");
1102 s = format(s, "reason:");
1104 while (0 != fib_walk_history[ii].fwh_reason[jj])
1106 s = format (s, "%U,", format_fib_node_bw_reason,
1107 fib_walk_history[ii].fwh_reason[jj]);
1110 vlib_cli_output(vm, "%v", s);
1115 ii = HISTORY_N_WALKS - 1;
1121 VLIB_CLI_COMMAND (fib_walk_show_command, static) = {
1122 .path = "show fib walk",
1123 .short_help = "show fib walk",
1124 .function = fib_walk_show,
1127 static clib_error_t *
1128 fib_walk_set_quota (vlib_main_t * vm,
1129 unformat_input_t * input,
1130 vlib_cli_command_t * cmd)
1132 clib_error_t * error = NULL;
1135 if (unformat (input, "%f", &new_quota))
1141 error = clib_error_return(0 , "Pass a float value");
1147 VLIB_CLI_COMMAND (fib_walk_set_quota_command, static) = {
1148 .path = "set fib walk quota",
1149 .short_help = "set fib walk quota",
1150 .function = fib_walk_set_quota,
1153 static clib_error_t *
1154 fib_walk_set_histogram_elements_size (vlib_main_t * vm,
1155 unformat_input_t * input,
1156 vlib_cli_command_t * cmd)
1158 clib_error_t * error = NULL;
1161 if (unformat (input, "%d", &new))
1163 fib_walk_work_nodes_visisted_incr = new;
1167 error = clib_error_return(0 , "Pass an int value");
1173 VLIB_CLI_COMMAND (fib_walk_set_histogram_elements_size_command, static) = {
1174 .path = "set fib walk histogram elements size",
1175 .short_help = "set fib walk histogram elements size",
1176 .function = fib_walk_set_histogram_elements_size,
1179 static clib_error_t *
1180 fib_walk_clear (vlib_main_t * vm,
1181 unformat_input_t * input,
1182 vlib_cli_command_t * cmd)
1184 clib_memset(fib_walk_hist_vists_per_walk, 0, sizeof(fib_walk_hist_vists_per_walk));
1185 clib_memset(fib_walk_history, 0, sizeof(fib_walk_history));
1186 clib_memset(fib_walk_work_time_taken, 0, sizeof(fib_walk_work_time_taken));
1187 clib_memset(fib_walk_work_nodes_visited, 0, sizeof(fib_walk_work_nodes_visited));
1188 clib_memset(fib_walk_sleep_lengths, 0, sizeof(fib_walk_sleep_lengths));
1193 VLIB_CLI_COMMAND (fib_walk_clear_command, static) = {
1194 .path = "clear fib walk",
1195 .short_help = "clear fib walk",
1196 .function = fib_walk_clear,
1200 fib_walk_process_enable (void)
1202 vlib_process_signal_event(vlib_get_main(),
1203 fib_walk_process_node.index,
1204 FIB_WALK_PROCESS_EVENT_ENABLE,
1209 fib_walk_process_disable (void)
1211 vlib_process_signal_event(vlib_get_main(),
1212 fib_walk_process_node.index,
1213 FIB_WALK_PROCESS_EVENT_DISABLE,
1217 static clib_error_t *
1218 fib_walk_process_enable_disable (vlib_main_t * vm,
1219 unformat_input_t * input,
1220 vlib_cli_command_t * cmd)
1222 if (unformat (input, "enable"))
1224 fib_walk_process_enable();
1226 else if (unformat (input, "disable"))
1228 fib_walk_process_disable();
1232 return clib_error_return(0, "choose enable or disable");
1237 VLIB_CLI_COMMAND (fib_walk_process_command, static) = {
1238 .path = "test fib-walk-process",
1239 .short_help = "test fib-walk-process [enable|disable]",
1240 .function = fib_walk_process_enable_disable,