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_UNSPEC
:
67 int snprintf_seg6_segs(char *str
,
68 size_t size
, const struct seg6_segs
*segs
)
71 for (size_t i
= 0; i
< segs
->num_segs
; i
++) {
72 char addr
[INET6_ADDRSTRLEN
];
73 bool not_last
= (i
+ 1) < segs
->num_segs
;
75 inet_ntop(AF_INET6
, &segs
->segs
[i
], addr
, sizeof(addr
));
76 strlcat(str
, addr
, size
);
77 strlcat(str
, not_last
? "," : "", size
);
82 const char *seg6local_context2str(char *str
, size_t size
,
83 const struct seg6local_context
*ctx
,
90 case ZEBRA_SEG6_LOCAL_ACTION_END
:
91 snprintf(str
, size
, "USP");
94 case ZEBRA_SEG6_LOCAL_ACTION_END_X
:
95 case ZEBRA_SEG6_LOCAL_ACTION_END_DX6
:
96 inet_ntop(AF_INET6
, &ctx
->nh6
, b0
, 128);
97 snprintf(str
, size
, "nh6 %s", b0
);
100 case ZEBRA_SEG6_LOCAL_ACTION_END_DX4
:
101 inet_ntop(AF_INET
, &ctx
->nh4
, b0
, 128);
102 snprintf(str
, size
, "nh4 %s", b0
);
105 case ZEBRA_SEG6_LOCAL_ACTION_END_T
:
106 case ZEBRA_SEG6_LOCAL_ACTION_END_DT6
:
107 case ZEBRA_SEG6_LOCAL_ACTION_END_DT4
:
108 snprintf(str
, size
, "table %u", ctx
->table
);
111 case ZEBRA_SEG6_LOCAL_ACTION_END_DX2
:
112 case ZEBRA_SEG6_LOCAL_ACTION_END_B6
:
113 case ZEBRA_SEG6_LOCAL_ACTION_END_B6_ENCAP
:
114 case ZEBRA_SEG6_LOCAL_ACTION_END_BM
:
115 case ZEBRA_SEG6_LOCAL_ACTION_END_S
:
116 case ZEBRA_SEG6_LOCAL_ACTION_END_AS
:
117 case ZEBRA_SEG6_LOCAL_ACTION_END_AM
:
118 case ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
:
120 snprintf(str
, size
, "unknown(%s)", __func__
);
125 struct srv6_locator
*srv6_locator_alloc(const char *name
)
127 struct srv6_locator
*locator
= NULL
;
129 locator
= XCALLOC(MTYPE_SRV6_LOCATOR
, sizeof(struct srv6_locator
));
130 strlcpy(locator
->name
, name
, sizeof(locator
->name
));
131 locator
->chunks
= list_new();
132 locator
->chunks
->del
= (void (*)(void *))srv6_locator_chunk_free
;
134 QOBJ_REG(locator
, srv6_locator
);
138 struct srv6_locator_chunk
*srv6_locator_chunk_alloc(void)
140 struct srv6_locator_chunk
*chunk
= NULL
;
142 chunk
= XCALLOC(MTYPE_SRV6_LOCATOR_CHUNK
,
143 sizeof(struct srv6_locator_chunk
));
147 void srv6_locator_free(struct srv6_locator
*locator
)
151 list_delete(&locator
->chunks
);
153 XFREE(MTYPE_SRV6_LOCATOR
, locator
);
157 void srv6_locator_chunk_free(struct srv6_locator_chunk
*chunk
)
159 XFREE(MTYPE_SRV6_LOCATOR_CHUNK
, chunk
);
162 json_object
*srv6_locator_chunk_json(const struct srv6_locator_chunk
*chunk
)
164 json_object
*jo_root
= NULL
;
166 jo_root
= json_object_new_object();
167 json_object_string_addf(jo_root
, "prefix", "%pFX", &chunk
->prefix
);
168 json_object_string_add(jo_root
, "proto",
169 zebra_route_string(chunk
->proto
));
175 srv6_locator_chunk_detailed_json(const struct srv6_locator_chunk
*chunk
)
177 json_object
*jo_root
= NULL
;
179 jo_root
= json_object_new_object();
182 json_object_string_addf(jo_root
, "prefix", "%pFX", &chunk
->prefix
);
184 /* set block_bits_length */
185 json_object_int_add(jo_root
, "blockBitsLength",
186 chunk
->block_bits_length
);
188 /* set node_bits_length */
189 json_object_int_add(jo_root
, "nodeBitsLength", chunk
->node_bits_length
);
191 /* set function_bits_length */
192 json_object_int_add(jo_root
, "functionBitsLength",
193 chunk
->function_bits_length
);
195 /* set argument_bits_length */
196 json_object_int_add(jo_root
, "argumentBitsLength",
197 chunk
->argument_bits_length
);
200 json_object_int_add(jo_root
, "keep", chunk
->keep
);
203 json_object_string_add(jo_root
, "proto",
204 zebra_route_string(chunk
->proto
));
207 json_object_int_add(jo_root
, "instance", chunk
->instance
);
210 json_object_int_add(jo_root
, "sessionId", chunk
->session_id
);
215 json_object
*srv6_locator_json(const struct srv6_locator
*loc
)
217 struct listnode
*node
;
218 struct srv6_locator_chunk
*chunk
;
219 json_object
*jo_root
= NULL
;
220 json_object
*jo_chunk
= NULL
;
221 json_object
*jo_chunks
= NULL
;
223 jo_root
= json_object_new_object();
226 json_object_string_add(jo_root
, "name", loc
->name
);
229 json_object_string_addf(jo_root
, "prefix", "%pFX", &loc
->prefix
);
231 /* set function_bits_length */
232 json_object_int_add(jo_root
, "functionBitsLength",
233 loc
->function_bits_length
);
236 json_object_boolean_add(jo_root
, "statusUp",
240 jo_chunks
= json_object_new_array();
241 json_object_object_add(jo_root
, "chunks", jo_chunks
);
242 for (ALL_LIST_ELEMENTS_RO((struct list
*)loc
->chunks
, node
, chunk
)) {
243 jo_chunk
= srv6_locator_chunk_json(chunk
);
244 json_object_array_add(jo_chunks
, jo_chunk
);
250 json_object
*srv6_locator_detailed_json(const struct srv6_locator
*loc
)
252 struct listnode
*node
;
253 struct srv6_locator_chunk
*chunk
;
254 json_object
*jo_root
= NULL
;
255 json_object
*jo_chunk
= NULL
;
256 json_object
*jo_chunks
= NULL
;
258 jo_root
= json_object_new_object();
261 json_object_string_add(jo_root
, "name", loc
->name
);
264 json_object_string_addf(jo_root
, "prefix", "%pFX", &loc
->prefix
);
266 /* set block_bits_length */
267 json_object_int_add(jo_root
, "blockBitsLength", loc
->block_bits_length
);
269 /* set node_bits_length */
270 json_object_int_add(jo_root
, "nodeBitsLength", loc
->node_bits_length
);
272 /* set function_bits_length */
273 json_object_int_add(jo_root
, "functionBitsLength",
274 loc
->function_bits_length
);
276 /* set argument_bits_length */
277 json_object_int_add(jo_root
, "argumentBitsLength",
278 loc
->argument_bits_length
);
281 json_object_int_add(jo_root
, "algoNum", loc
->algonum
);
284 json_object_boolean_add(jo_root
, "statusUp", loc
->status_up
);
287 jo_chunks
= json_object_new_array();
288 json_object_object_add(jo_root
, "chunks", jo_chunks
);
289 for (ALL_LIST_ELEMENTS_RO((struct list
*)loc
->chunks
, node
, chunk
)) {
290 jo_chunk
= srv6_locator_chunk_detailed_json(chunk
);
291 json_object_array_add(jo_chunks
, jo_chunk
);