]> git.proxmox.com Git - mirror_frr.git/blob - lib/srv6.c
lib: add block/node/arg len to SRv6 locator JSON
[mirror_frr.git] / lib / srv6.c
1 /*
2 * SRv6 definitions
3 * Copyright (C) 2020 Hiroki Shirokura, LINE Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "zebra.h"
21
22 #include "srv6.h"
23 #include "log.h"
24
25 DEFINE_QOBJ_TYPE(srv6_locator);
26 DEFINE_MTYPE_STATIC(LIB, SRV6_LOCATOR, "SRV6 locator");
27 DEFINE_MTYPE_STATIC(LIB, SRV6_LOCATOR_CHUNK, "SRV6 locator chunk");
28
29 const char *seg6local_action2str(uint32_t action)
30 {
31 switch (action) {
32 case ZEBRA_SEG6_LOCAL_ACTION_END:
33 return "End";
34 case ZEBRA_SEG6_LOCAL_ACTION_END_X:
35 return "End.X";
36 case ZEBRA_SEG6_LOCAL_ACTION_END_T:
37 return "End.T";
38 case ZEBRA_SEG6_LOCAL_ACTION_END_DX2:
39 return "End.DX2";
40 case ZEBRA_SEG6_LOCAL_ACTION_END_DX6:
41 return "End.DX6";
42 case ZEBRA_SEG6_LOCAL_ACTION_END_DX4:
43 return "End.DX4";
44 case ZEBRA_SEG6_LOCAL_ACTION_END_DT6:
45 return "End.DT6";
46 case ZEBRA_SEG6_LOCAL_ACTION_END_DT4:
47 return "End.DT4";
48 case ZEBRA_SEG6_LOCAL_ACTION_END_B6:
49 return "End.B6";
50 case ZEBRA_SEG6_LOCAL_ACTION_END_B6_ENCAP:
51 return "End.B6.Encap";
52 case ZEBRA_SEG6_LOCAL_ACTION_END_BM:
53 return "End.BM";
54 case ZEBRA_SEG6_LOCAL_ACTION_END_S:
55 return "End.S";
56 case ZEBRA_SEG6_LOCAL_ACTION_END_AS:
57 return "End.AS";
58 case ZEBRA_SEG6_LOCAL_ACTION_END_AM:
59 return "End.AM";
60 case ZEBRA_SEG6_LOCAL_ACTION_END_DT46:
61 return "End.DT46";
62 case ZEBRA_SEG6_LOCAL_ACTION_UNSPEC:
63 return "unspec";
64 default:
65 return "unknown";
66 }
67 }
68
69 int snprintf_seg6_segs(char *str,
70 size_t size, const struct seg6_segs *segs)
71 {
72 str[0] = '\0';
73 for (size_t i = 0; i < segs->num_segs; i++) {
74 char addr[INET6_ADDRSTRLEN];
75 bool not_last = (i + 1) < segs->num_segs;
76
77 inet_ntop(AF_INET6, &segs->segs[i], addr, sizeof(addr));
78 strlcat(str, addr, size);
79 strlcat(str, not_last ? "," : "", size);
80 }
81 return strlen(str);
82 }
83
84 const char *seg6local_context2str(char *str, size_t size,
85 const struct seg6local_context *ctx,
86 uint32_t action)
87 {
88 char b0[128];
89
90 switch (action) {
91
92 case ZEBRA_SEG6_LOCAL_ACTION_END:
93 snprintf(str, size, "USP");
94 return str;
95
96 case ZEBRA_SEG6_LOCAL_ACTION_END_X:
97 case ZEBRA_SEG6_LOCAL_ACTION_END_DX6:
98 inet_ntop(AF_INET6, &ctx->nh6, b0, 128);
99 snprintf(str, size, "nh6 %s", b0);
100 return str;
101
102 case ZEBRA_SEG6_LOCAL_ACTION_END_DX4:
103 inet_ntop(AF_INET, &ctx->nh4, b0, 128);
104 snprintf(str, size, "nh4 %s", b0);
105 return str;
106
107 case ZEBRA_SEG6_LOCAL_ACTION_END_T:
108 case ZEBRA_SEG6_LOCAL_ACTION_END_DT6:
109 case ZEBRA_SEG6_LOCAL_ACTION_END_DT4:
110 case ZEBRA_SEG6_LOCAL_ACTION_END_DT46:
111 snprintf(str, size, "table %u", ctx->table);
112 return str;
113
114 case ZEBRA_SEG6_LOCAL_ACTION_END_DX2:
115 case ZEBRA_SEG6_LOCAL_ACTION_END_B6:
116 case ZEBRA_SEG6_LOCAL_ACTION_END_B6_ENCAP:
117 case ZEBRA_SEG6_LOCAL_ACTION_END_BM:
118 case ZEBRA_SEG6_LOCAL_ACTION_END_S:
119 case ZEBRA_SEG6_LOCAL_ACTION_END_AS:
120 case ZEBRA_SEG6_LOCAL_ACTION_END_AM:
121 case ZEBRA_SEG6_LOCAL_ACTION_UNSPEC:
122 default:
123 snprintf(str, size, "unknown(%s)", __func__);
124 return str;
125 }
126 }
127
128 struct srv6_locator *srv6_locator_alloc(const char *name)
129 {
130 struct srv6_locator *locator = NULL;
131
132 locator = XCALLOC(MTYPE_SRV6_LOCATOR, sizeof(struct srv6_locator));
133 strlcpy(locator->name, name, sizeof(locator->name));
134 locator->chunks = list_new();
135 locator->chunks->del = (void (*)(void *))srv6_locator_chunk_free;
136
137 QOBJ_REG(locator, srv6_locator);
138 return locator;
139 }
140
141 struct srv6_locator_chunk *srv6_locator_chunk_alloc(void)
142 {
143 struct srv6_locator_chunk *chunk = NULL;
144
145 chunk = XCALLOC(MTYPE_SRV6_LOCATOR_CHUNK,
146 sizeof(struct srv6_locator_chunk));
147 return chunk;
148 }
149
150 void srv6_locator_free(struct srv6_locator *locator)
151 {
152 if (locator) {
153 QOBJ_UNREG(locator);
154 list_delete(&locator->chunks);
155
156 XFREE(MTYPE_SRV6_LOCATOR, locator);
157 }
158 }
159
160 void srv6_locator_chunk_free(struct srv6_locator_chunk *chunk)
161 {
162 XFREE(MTYPE_SRV6_LOCATOR_CHUNK, chunk);
163 }
164
165 json_object *srv6_locator_chunk_json(const struct srv6_locator_chunk *chunk)
166 {
167 json_object *jo_root = NULL;
168
169 jo_root = json_object_new_object();
170 json_object_string_addf(jo_root, "prefix", "%pFX", &chunk->prefix);
171 json_object_string_add(jo_root, "proto",
172 zebra_route_string(chunk->proto));
173
174 return jo_root;
175 }
176
177 json_object *
178 srv6_locator_chunk_detailed_json(const struct srv6_locator_chunk *chunk)
179 {
180 json_object *jo_root = NULL;
181
182 jo_root = json_object_new_object();
183
184 /* set prefix */
185 json_object_string_addf(jo_root, "prefix", "%pFX", &chunk->prefix);
186
187 /* set block_bits_length */
188 json_object_int_add(jo_root, "blockBitsLength",
189 chunk->block_bits_length);
190
191 /* set node_bits_length */
192 json_object_int_add(jo_root, "nodeBitsLength", chunk->node_bits_length);
193
194 /* set function_bits_length */
195 json_object_int_add(jo_root, "functionBitsLength",
196 chunk->function_bits_length);
197
198 /* set argument_bits_length */
199 json_object_int_add(jo_root, "argumentBitsLength",
200 chunk->argument_bits_length);
201
202 /* set keep */
203 json_object_int_add(jo_root, "keep", chunk->keep);
204
205 /* set proto */
206 json_object_string_add(jo_root, "proto",
207 zebra_route_string(chunk->proto));
208
209 /* set instance */
210 json_object_int_add(jo_root, "instance", chunk->instance);
211
212 /* set session_id */
213 json_object_int_add(jo_root, "sessionId", chunk->session_id);
214
215 return jo_root;
216 }
217
218 json_object *srv6_locator_json(const struct srv6_locator *loc)
219 {
220 struct listnode *node;
221 struct srv6_locator_chunk *chunk;
222 json_object *jo_root = NULL;
223 json_object *jo_chunk = NULL;
224 json_object *jo_chunks = NULL;
225
226 jo_root = json_object_new_object();
227
228 /* set name */
229 json_object_string_add(jo_root, "name", loc->name);
230
231 /* set prefix */
232 json_object_string_addf(jo_root, "prefix", "%pFX", &loc->prefix);
233
234 /* set block_bits_length */
235 json_object_int_add(jo_root, "blockBitsLength", loc->block_bits_length);
236
237 /* set node_bits_length */
238 json_object_int_add(jo_root, "nodeBitsLength", loc->node_bits_length);
239
240 /* set function_bits_length */
241 json_object_int_add(jo_root, "functionBitsLength",
242 loc->function_bits_length);
243
244 /* set argument_bits_length */
245 json_object_int_add(jo_root, "argumentBitsLength",
246 loc->argument_bits_length);
247
248 /* set status_up */
249 json_object_boolean_add(jo_root, "statusUp",
250 loc->status_up);
251
252 /* set chunks */
253 jo_chunks = json_object_new_array();
254 json_object_object_add(jo_root, "chunks", jo_chunks);
255 for (ALL_LIST_ELEMENTS_RO((struct list *)loc->chunks, node, chunk)) {
256 jo_chunk = srv6_locator_chunk_json(chunk);
257 json_object_array_add(jo_chunks, jo_chunk);
258 }
259
260 return jo_root;
261 }
262
263 json_object *srv6_locator_detailed_json(const struct srv6_locator *loc)
264 {
265 struct listnode *node;
266 struct srv6_locator_chunk *chunk;
267 json_object *jo_root = NULL;
268 json_object *jo_chunk = NULL;
269 json_object *jo_chunks = NULL;
270
271 jo_root = json_object_new_object();
272
273 /* set name */
274 json_object_string_add(jo_root, "name", loc->name);
275
276 /* set prefix */
277 json_object_string_addf(jo_root, "prefix", "%pFX", &loc->prefix);
278
279 /* set block_bits_length */
280 json_object_int_add(jo_root, "blockBitsLength", loc->block_bits_length);
281
282 /* set node_bits_length */
283 json_object_int_add(jo_root, "nodeBitsLength", loc->node_bits_length);
284
285 /* set function_bits_length */
286 json_object_int_add(jo_root, "functionBitsLength",
287 loc->function_bits_length);
288
289 /* set argument_bits_length */
290 json_object_int_add(jo_root, "argumentBitsLength",
291 loc->argument_bits_length);
292
293 /* set algonum */
294 json_object_int_add(jo_root, "algoNum", loc->algonum);
295
296 /* set status_up */
297 json_object_boolean_add(jo_root, "statusUp", loc->status_up);
298
299 /* set chunks */
300 jo_chunks = json_object_new_array();
301 json_object_object_add(jo_root, "chunks", jo_chunks);
302 for (ALL_LIST_ELEMENTS_RO((struct list *)loc->chunks, node, chunk)) {
303 jo_chunk = srv6_locator_chunk_detailed_json(chunk);
304 json_object_array_add(jo_chunks, jo_chunk);
305 }
306
307 return jo_root;
308 }