2 * Copyright (c) 2015 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.
15 #include <vppinfra/graph.h>
17 /* Set link distance, creating link if not found. */
19 graph_set_link (graph_t * g, u32 src, u32 dst, u32 distance)
21 graph_node_t *src_node, *dst_node;
25 /* The following validate will not work if src or dst are on the
27 if (src < vec_len (g->nodes))
28 ASSERT (!pool_is_free_index (g->nodes, src));
29 if (dst < vec_len (g->nodes))
30 ASSERT (!pool_is_free_index (g->nodes, dst));
32 /* Make new (empty) nodes to make src and dst valid. */
33 pool_validate_index (g->nodes, clib_max (src, dst));
35 src_node = pool_elt_at_index (g->nodes, src);
36 dst_node = pool_elt_at_index (g->nodes, dst);
38 l = graph_dir_get_link_to_node (&src_node->next, dst);
41 old_distance = l->distance;
42 l->distance = distance;
44 l = graph_dir_get_link_to_node (&dst_node->prev, src);
45 l->distance = distance;
49 uword li_next, li_prev;
53 li_next = graph_dir_add_link (&src_node->next, dst, distance);
54 li_prev = graph_dir_add_link (&dst_node->prev, src, distance);
56 l = vec_elt_at_index (src_node->next.links, li_next);
57 l->link_to_self_index = li_prev;
59 l = vec_elt_at_index (dst_node->prev.links, li_prev);
60 l->link_to_self_index = li_next;
67 graph_del_link (graph_t * g, u32 src, u32 dst)
69 graph_node_t *src_node, *dst_node;
71 src_node = pool_elt_at_index (g->nodes, src);
72 dst_node = pool_elt_at_index (g->nodes, dst);
74 graph_dir_del_link (&src_node->next, dst);
75 graph_dir_del_link (&dst_node->next, src);
78 /* Delete source node and all links from other nodes from/to source. */
80 graph_del_node (graph_t * g, u32 src)
82 graph_node_t *src_node, *n;
86 src_node = pool_elt_at_index (g->nodes, src);
88 vec_foreach (l, src_node->next.links)
90 n = pool_elt_at_index (g->nodes, l->node_index);
91 graph_dir_del_link (&n->prev, src);
94 vec_foreach (l, src_node->prev.links)
96 n = pool_elt_at_index (g->nodes, l->node_index);
97 graph_dir_del_link (&n->next, src);
100 graph_dir_free (&src_node->next);
101 graph_dir_free (&src_node->prev);
103 index = src_node - g->nodes;
104 pool_put (g->nodes, src_node);
105 clib_memset (src_node, ~0, sizeof (src_node[0]));
111 unformat_graph (unformat_input_t * input, va_list * args)
113 graph_t *g = va_arg (*args, graph_t *);
116 u32 src, dst, distance;
123 vec_add2 (links, l, 1);
124 if (!unformat (input, "%d%d%d", &l->src, &l->dst, &l->distance))
127 _vec_len (links) -= 1;
128 result = vec_len (links) > 0;
129 vec_foreach (l, links)
131 graph_set_link (g, l->src, l->dst, l->distance);
132 graph_set_link (g, l->dst, l->src, l->distance);
140 format_graph_node (u8 * s, va_list * args)
142 graph_t *g = va_arg (*args, graph_t *);
143 u32 node_index = va_arg (*args, u32);
146 s = format (s, "%U", g->format_node, g, node_index);
148 s = format (s, "%d", node_index);
154 format_graph (u8 * s, va_list * args)
156 graph_t *g = va_arg (*args, graph_t *);
159 u32 indent = format_get_indent (s);
161 s = format (s, "graph %d nodes", pool_elts (g->nodes));
163 pool_foreach (n, g->nodes, ({
164 s = format (s, "\n%U", format_white_space, indent + 2);
165 s = format (s, "%U -> ", format_graph_node, g, n - g->nodes);
166 vec_foreach (l, n->next.links)
167 s = format (s, "%U (%d), ",
168 format_graph_node, g, l->node_index,
177 * fd.io coding-style-patch-verification: ON
180 * eval: (c-set-style "gnu")