blob: 3f8a3e8423ece841cd5af3424debea7423d0a935 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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:
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#include <vppinfra/graph.h>
16
17/* Set link distance, creating link if not found. */
Dave Barachc3799992016-08-15 11:12:27 -040018u32
19graph_set_link (graph_t * g, u32 src, u32 dst, u32 distance)
Ed Warnickecb9cada2015-12-08 15:45:58 -070020{
Dave Barachc3799992016-08-15 11:12:27 -040021 graph_node_t *src_node, *dst_node;
22 graph_link_t *l;
Ed Warnickecb9cada2015-12-08 15:45:58 -070023 u32 old_distance;
Dave Barachc3799992016-08-15 11:12:27 -040024
Ed Warnickecb9cada2015-12-08 15:45:58 -070025 /* The following validate will not work if src or dst are on the
26 pool free list. */
27 if (src < vec_len (g->nodes))
Dave Barachc3799992016-08-15 11:12:27 -040028 ASSERT (!pool_is_free_index (g->nodes, src));
Ed Warnickecb9cada2015-12-08 15:45:58 -070029 if (dst < vec_len (g->nodes))
Dave Barachc3799992016-08-15 11:12:27 -040030 ASSERT (!pool_is_free_index (g->nodes, dst));
Ed Warnickecb9cada2015-12-08 15:45:58 -070031
32 /* Make new (empty) nodes to make src and dst valid. */
33 pool_validate_index (g->nodes, clib_max (src, dst));
34
35 src_node = pool_elt_at_index (g->nodes, src);
36 dst_node = pool_elt_at_index (g->nodes, dst);
37
38 l = graph_dir_get_link_to_node (&src_node->next, dst);
39 if (l)
40 {
41 old_distance = l->distance;
42 l->distance = distance;
43
44 l = graph_dir_get_link_to_node (&dst_node->prev, src);
45 l->distance = distance;
46 }
47 else
48 {
49 uword li_next, li_prev;
50
51 old_distance = ~0;
52
53 li_next = graph_dir_add_link (&src_node->next, dst, distance);
54 li_prev = graph_dir_add_link (&dst_node->prev, src, distance);
55
56 l = vec_elt_at_index (src_node->next.links, li_next);
57 l->link_to_self_index = li_prev;
58
59 l = vec_elt_at_index (dst_node->prev.links, li_prev);
60 l->link_to_self_index = li_next;
61 }
62
63 return old_distance;
64}
65
Dave Barachc3799992016-08-15 11:12:27 -040066void
67graph_del_link (graph_t * g, u32 src, u32 dst)
Ed Warnickecb9cada2015-12-08 15:45:58 -070068{
Dave Barachc3799992016-08-15 11:12:27 -040069 graph_node_t *src_node, *dst_node;
70
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 src_node = pool_elt_at_index (g->nodes, src);
72 dst_node = pool_elt_at_index (g->nodes, dst);
73
74 graph_dir_del_link (&src_node->next, dst);
75 graph_dir_del_link (&dst_node->next, src);
76}
77
78/* Delete source node and all links from other nodes from/to source. */
Dave Barachc3799992016-08-15 11:12:27 -040079uword
80graph_del_node (graph_t * g, u32 src)
Ed Warnickecb9cada2015-12-08 15:45:58 -070081{
Dave Barachc3799992016-08-15 11:12:27 -040082 graph_node_t *src_node, *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -070083 uword index;
Dave Barachc3799992016-08-15 11:12:27 -040084 graph_link_t *l;
Ed Warnickecb9cada2015-12-08 15:45:58 -070085
86 src_node = pool_elt_at_index (g->nodes, src);
87
88 vec_foreach (l, src_node->next.links)
Dave Barachc3799992016-08-15 11:12:27 -040089 {
90 n = pool_elt_at_index (g->nodes, l->node_index);
91 graph_dir_del_link (&n->prev, src);
92 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
94 vec_foreach (l, src_node->prev.links)
Dave Barachc3799992016-08-15 11:12:27 -040095 {
96 n = pool_elt_at_index (g->nodes, l->node_index);
97 graph_dir_del_link (&n->next, src);
98 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
100 graph_dir_free (&src_node->next);
101 graph_dir_free (&src_node->prev);
102
103 index = src_node - g->nodes;
104 pool_put (g->nodes, src_node);
Dave Barachb7b92992018-10-17 10:38:51 -0400105 clib_memset (src_node, ~0, sizeof (src_node[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
107 return index;
108}
109
Dave Barachc3799992016-08-15 11:12:27 -0400110uword
111unformat_graph (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112{
Dave Barachc3799992016-08-15 11:12:27 -0400113 graph_t *g = va_arg (*args, graph_t *);
114 typedef struct
115 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 u32 src, dst, distance;
117 } T;
Dave Barachc3799992016-08-15 11:12:27 -0400118 T *links = 0, *l;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119 uword result;
120
121 while (1)
122 {
123 vec_add2 (links, l, 1);
Dave Barachc3799992016-08-15 11:12:27 -0400124 if (!unformat (input, "%d%d%d", &l->src, &l->dst, &l->distance))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 break;
126 }
127 _vec_len (links) -= 1;
128 result = vec_len (links) > 0;
129 vec_foreach (l, links)
Dave Barachc3799992016-08-15 11:12:27 -0400130 {
131 graph_set_link (g, l->src, l->dst, l->distance);
132 graph_set_link (g, l->dst, l->src, l->distance);
133 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
135 vec_free (links);
136 return result;
137}
138
Dave Barachc3799992016-08-15 11:12:27 -0400139u8 *
140format_graph_node (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141{
Dave Barachc3799992016-08-15 11:12:27 -0400142 graph_t *g = va_arg (*args, graph_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 u32 node_index = va_arg (*args, u32);
144
145 if (g->format_node)
146 s = format (s, "%U", g->format_node, g, node_index);
147 else
148 s = format (s, "%d", node_index);
149
150 return s;
151}
152
Dave Barachc3799992016-08-15 11:12:27 -0400153u8 *
154format_graph (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155{
Dave Barachc3799992016-08-15 11:12:27 -0400156 graph_t *g = va_arg (*args, graph_t *);
157 graph_node_t *n;
158 graph_link_t *l;
Christophe Fontained3c008d2017-10-02 18:10:54 +0200159 u32 indent = format_get_indent (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160
161 s = format (s, "graph %d nodes", pool_elts (g->nodes));
Dave Barachc3799992016-08-15 11:12:27 -0400162 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 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,
169 l->distance);
170 }));
Dave Barachc3799992016-08-15 11:12:27 -0400171 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172
173 return s;
174}
Dave Barachc3799992016-08-15 11:12:27 -0400175
176/*
177 * fd.io coding-style-patch-verification: ON
178 *
179 * Local Variables:
180 * eval: (c-set-style "gnu")
181 * End:
182 */