3 * Copyright (C) 2020 Hiroki Shirokura, LINE Corporation
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)
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
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
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");
29 const char *seg6local_action2str(uint32_t action
)
32 case ZEBRA_SEG6_LOCAL_ACTION_END
:
34 case ZEBRA_SEG6_LOCAL_ACTION_END_X
:
36 case ZEBRA_SEG6_LOCAL_ACTION_END_T
:
38 case ZEBRA_SEG6_LOCAL_ACTION_END_DX2
:
40 case ZEBRA_SEG6_LOCAL_ACTION_END_DX6
:
42 case ZEBRA_SEG6_LOCAL_ACTION_END_DX4
:
44 case ZEBRA_SEG6_LOCAL_ACTION_END_DT6
:
46 case ZEBRA_SEG6_LOCAL_ACTION_END_DT4
:
48 case ZEBRA_SEG6_LOCAL_ACTION_END_B6
:
50 case ZEBRA_SEG6_LOCAL_ACTION_END_B6_ENCAP
:
51 return "End.B6.Encap";
52 case ZEBRA_SEG6_LOCAL_ACTION_END_BM
:
54 case ZEBRA_SEG6_LOCAL_ACTION_END_S
:
56 case ZEBRA_SEG6_LOCAL_ACTION_END_AS
:
58 case ZEBRA_SEG6_LOCAL_ACTION_END_AM
:
60 case ZEBRA_SEG6_LOCAL_ACTION_END_DT46
:
62 case ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
:
69 int snprintf_seg6_segs(char *str
,
70 size_t size
, const struct seg6_segs
*segs
)
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
;
77 inet_ntop(AF_INET6
, &segs
->segs
[i
], addr
, sizeof(addr
));
78 strlcat(str
, addr
, size
);
79 strlcat(str
, not_last
? "," : "", size
);
84 const char *seg6local_context2str(char *str
, size_t size
,
85 const struct seg6local_context
*ctx
,
92 case ZEBRA_SEG6_LOCAL_ACTION_END
:
93 snprintf(str
, size
, "USP");
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
);
102 case ZEBRA_SEG6_LOCAL_ACTION_END_DX4
:
103 inet_ntop(AF_INET
, &ctx
->nh4
, b0
, 128);
104 snprintf(str
, size
, "nh4 %s", b0
);
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
);
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
:
123 snprintf(str
, size
, "unknown(%s)", __func__
);
128 struct srv6_locator
*srv6_locator_alloc(const char *name
)
130 struct srv6_locator
*locator
= NULL
;
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
;
137 QOBJ_REG(locator
, srv6_locator
);
141 struct srv6_locator_chunk
*srv6_locator_chunk_alloc(void)
143 struct srv6_locator_chunk
*chunk
= NULL
;
145 chunk
= XCALLOC(MTYPE_SRV6_LOCATOR_CHUNK
,
146 sizeof(struct srv6_locator_chunk
));
150 void srv6_locator_free(struct srv6_locator
*locator
)
154 list_delete(&locator
->chunks
);
156 XFREE(MTYPE_SRV6_LOCATOR
, locator
);
160 void srv6_locator_chunk_free(struct srv6_locator_chunk
*chunk
)
162 XFREE(MTYPE_SRV6_LOCATOR_CHUNK
, chunk
);
165 json_object
*srv6_locator_chunk_json(const struct srv6_locator_chunk
*chunk
)
167 json_object
*jo_root
= NULL
;
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
));
178 srv6_locator_chunk_detailed_json(const struct srv6_locator_chunk
*chunk
)
180 json_object
*jo_root
= NULL
;
182 jo_root
= json_object_new_object();
185 json_object_string_addf(jo_root
, "prefix", "%pFX", &chunk
->prefix
);
187 /* set block_bits_length */
188 json_object_int_add(jo_root
, "blockBitsLength",
189 chunk
->block_bits_length
);
191 /* set node_bits_length */
192 json_object_int_add(jo_root
, "nodeBitsLength", chunk
->node_bits_length
);
194 /* set function_bits_length */
195 json_object_int_add(jo_root
, "functionBitsLength",
196 chunk
->function_bits_length
);
198 /* set argument_bits_length */
199 json_object_int_add(jo_root
, "argumentBitsLength",
200 chunk
->argument_bits_length
);
203 json_object_int_add(jo_root
, "keep", chunk
->keep
);
206 json_object_string_add(jo_root
, "proto",
207 zebra_route_string(chunk
->proto
));
210 json_object_int_add(jo_root
, "instance", chunk
->instance
);
213 json_object_int_add(jo_root
, "sessionId", chunk
->session_id
);
218 json_object
*srv6_locator_json(const struct srv6_locator
*loc
)
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
;
226 jo_root
= json_object_new_object();
229 json_object_string_add(jo_root
, "name", loc
->name
);
232 json_object_string_addf(jo_root
, "prefix", "%pFX", &loc
->prefix
);
234 /* set block_bits_length */
235 json_object_int_add(jo_root
, "blockBitsLength", loc
->block_bits_length
);
237 /* set node_bits_length */
238 json_object_int_add(jo_root
, "nodeBitsLength", loc
->node_bits_length
);
240 /* set function_bits_length */
241 json_object_int_add(jo_root
, "functionBitsLength",
242 loc
->function_bits_length
);
244 /* set argument_bits_length */
245 json_object_int_add(jo_root
, "argumentBitsLength",
246 loc
->argument_bits_length
);
249 json_object_boolean_add(jo_root
, "statusUp",
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
);
263 json_object
*srv6_locator_detailed_json(const struct srv6_locator
*loc
)
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
;
271 jo_root
= json_object_new_object();
274 json_object_string_add(jo_root
, "name", loc
->name
);
277 json_object_string_addf(jo_root
, "prefix", "%pFX", &loc
->prefix
);
279 /* set block_bits_length */
280 json_object_int_add(jo_root
, "blockBitsLength", loc
->block_bits_length
);
282 /* set node_bits_length */
283 json_object_int_add(jo_root
, "nodeBitsLength", loc
->node_bits_length
);
285 /* set function_bits_length */
286 json_object_int_add(jo_root
, "functionBitsLength",
287 loc
->function_bits_length
);
289 /* set argument_bits_length */
290 json_object_int_add(jo_root
, "argumentBitsLength",
291 loc
->argument_bits_length
);
294 json_object_int_add(jo_root
, "algoNum", loc
->algonum
);
297 json_object_boolean_add(jo_root
, "statusUp", loc
->status_up
);
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
);