blob: eeab2fef9645c2639a882b079a44544ba92a36f6 [file] [log] [blame]
Neale Rannsd792d9c2017-10-21 10:53:20 -07001/*
2 *------------------------------------------------------------------
3 * bier_api.c - vnet BIER api
4 *
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <vnet/vnet.h>
21#include <vlibmemory/api.h>
22
23#include <vnet/api_errno.h>
24#include <vnet/bier/bier_table.h>
25#include <vnet/bier/bier_imp.h>
26#include <vnet/bier/bier_disp_table.h>
27#include <vnet/bier/bier_disp_entry.h>
28#include <vnet/bier/bier_bit_string.h>
29#include <vnet/bier/bier_hdr_inlines.h>
30#include <vnet/fib/fib_path_list.h>
31#include <vnet/fib/fib_api.h>
32#include <vnet/fib/fib_table.h>
33#include <vnet/mfib/mfib_table.h>
34
Filip Tehlar52d80c52021-06-17 01:28:22 +000035#include <vnet/format_fns.h>
36#include <bier/bier.api_enum.h>
37#include <bier/bier.api_types.h>
Neale Rannsd792d9c2017-10-21 10:53:20 -070038
Filip Tehlar52d80c52021-06-17 01:28:22 +000039#define REPLY_MSG_ID_BASE bier_main.msg_id_base
Neale Rannsd792d9c2017-10-21 10:53:20 -070040#include <vlibapi/api_helper_macros.h>
41
Filip Tehlar52d80c52021-06-17 01:28:22 +000042typedef struct
43{
44 u16 msg_id_base;
45} bier_main_t;
46
47bier_main_t bier_main;
Neale Rannsd792d9c2017-10-21 10:53:20 -070048
49static void
50vl_api_bier_table_add_del_t_handler (vl_api_bier_table_add_del_t * mp)
51{
52 vl_api_bier_table_add_del_reply_t *rmp;
53 vnet_main_t *vnm;
54 int rv;
55
56 vnm = vnet_get_main ();
57 vnm->api_errno = 0;
58
Neale Rannsf0510722018-01-31 11:35:41 -080059 if (mp->bt_tbl_id.bt_hdr_len_id >= BIER_HDR_LEN_2048)
Neale Rannsd792d9c2017-10-21 10:53:20 -070060 {
Neale Rannsf0510722018-01-31 11:35:41 -080061 rv = VNET_API_ERROR_BIER_BSL_UNSUP;
Neale Rannsd792d9c2017-10-21 10:53:20 -070062 }
63 else
64 {
Neale Rannsf0510722018-01-31 11:35:41 -080065 bier_table_id_t bti = {
66 .bti_set = mp->bt_tbl_id.bt_set,
67 .bti_sub_domain = mp->bt_tbl_id.bt_sub_domain,
68 .bti_hdr_len = mp->bt_tbl_id.bt_hdr_len_id,
69 .bti_type = BIER_TABLE_MPLS_SPF,
70 .bti_ecmp = BIER_ECMP_TABLE_ID_MAIN,
71 };
Neale Rannsd792d9c2017-10-21 10:53:20 -070072
Neale Rannsf0510722018-01-31 11:35:41 -080073 if (mp->bt_is_add)
74 {
75 mpls_label_t label = ntohl(mp->bt_label);
76
77 /*
78 * convert acceptable 'don't want a label' values from
79 * the API to the correct internal INVLID value
80 */
81 if ((0 == label) || (~0 == label))
82 {
83 label = MPLS_LABEL_INVALID;
84 }
85 bier_table_add_or_lock(&bti, label);
86 }
87 else
88 {
89 bier_table_unlock(&bti);
90 }
91
92 rv = vnm->api_errno;
93 }
Neale Rannsd792d9c2017-10-21 10:53:20 -070094
95 REPLY_MACRO (VL_API_BIER_TABLE_ADD_DEL_REPLY);
96}
97
98static void
Florin Coras6c4dae22018-01-09 06:39:23 -080099send_bier_table_details (vl_api_registration_t * reg,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700100 u32 context,
101 const bier_table_t *bt)
102{
103 vl_api_bier_table_details_t *mp;
104
105 mp = vl_msg_api_alloc(sizeof(*mp));
106 if (!mp)
107 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400108 clib_memset(mp, 0, sizeof(*mp));
Filip Tehlar52d80c52021-06-17 01:28:22 +0000109 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_BIER_TABLE_DETAILS);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700110 mp->context = context;
111
112 mp->bt_label = bt->bt_ll;
113 mp->bt_tbl_id.bt_set = bt->bt_id.bti_set;
114 mp->bt_tbl_id.bt_sub_domain = bt->bt_id.bti_sub_domain;
115 mp->bt_tbl_id.bt_hdr_len_id = bt->bt_id.bti_hdr_len;
116
Florin Coras6c4dae22018-01-09 06:39:23 -0800117 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700118}
119
120static void
121vl_api_bier_table_dump_t_handler (vl_api_bier_table_dump_t * mp)
122{
Florin Coras6c4dae22018-01-09 06:39:23 -0800123 vl_api_registration_t *reg;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700124 bier_table_t *bt;
125
Florin Coras6c4dae22018-01-09 06:39:23 -0800126 reg = vl_api_client_index_to_registration (mp->client_index);
127 if (!reg)
128 return;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700129
Damjan Marionb2c31b62020-12-13 21:47:40 +0100130 pool_foreach (bt, bier_table_pool)
131 {
Neale Rannsd792d9c2017-10-21 10:53:20 -0700132 /*
133 * skip the ecmp tables.
134 */
135 if (bier_table_is_main(bt))
136 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800137 send_bier_table_details(reg, mp->context, bt);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700138 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100139 }
Neale Rannsd792d9c2017-10-21 10:53:20 -0700140}
141
142static void
143vl_api_bier_route_add_del_t_handler (vl_api_bier_route_add_del_t * mp)
144{
145 vl_api_bier_route_add_del_reply_t *rmp;
146 fib_route_path_t *brpaths, *brpath;
147 vnet_main_t *vnm;
148 bier_bp_t bp;
149 int rv = 0;
Neale Ranns2303cb12018-02-21 04:57:17 -0800150 u8 ii;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700151
152 vnm = vnet_get_main ();
153 vnm->api_errno = 0;
Neale Ranns097fa662018-05-01 05:17:55 -0700154 bp = ntohl(mp->br_route.br_bp);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700155 brpaths = NULL;
156
Neale Ranns097fa662018-05-01 05:17:55 -0700157 if (mp->br_route.br_tbl_id.bt_hdr_len_id >= BIER_HDR_LEN_2048)
Neale Rannsf0510722018-01-31 11:35:41 -0800158 {
159 rv = VNET_API_ERROR_BIER_BSL_UNSUP;
160 goto done;
161 }
Neale Rannsbe302d72017-12-19 01:34:30 -0800162 if (0 == bp || bp > BIER_BP_MAX)
Neale Rannsd792d9c2017-10-21 10:53:20 -0700163 {
164 rv = -1;
165 goto done;
166 }
167
168 bier_table_id_t bti = {
Neale Ranns097fa662018-05-01 05:17:55 -0700169 .bti_set = mp->br_route.br_tbl_id.bt_set,
170 .bti_sub_domain = mp->br_route.br_tbl_id.bt_sub_domain,
171 .bti_hdr_len = mp->br_route.br_tbl_id.bt_hdr_len_id,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700172 .bti_type = BIER_TABLE_MPLS_SPF,
173 .bti_ecmp = BIER_ECMP_TABLE_ID_MAIN,
174 };
175
Neale Ranns097fa662018-05-01 05:17:55 -0700176 vec_validate(brpaths, mp->br_route.br_n_paths - 1);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700177
178 vec_foreach_index(ii, brpaths)
179 {
180 brpath = &brpaths[ii];
Neale Ranns097fa662018-05-01 05:17:55 -0700181 rv = fib_api_path_decode(&mp->br_route.br_paths[ii], brpath);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700182
Neale Ranns2303cb12018-02-21 04:57:17 -0800183 if (0 != rv)
Neale Rannsd792d9c2017-10-21 10:53:20 -0700184 {
Neale Ranns2303cb12018-02-21 04:57:17 -0800185 goto done;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700186 }
187 }
188
Neale Rannsef90ed02018-09-13 08:45:12 -0700189 if (mp->br_is_replace)
Neale Rannsd792d9c2017-10-21 10:53:20 -0700190 {
Neale Rannsef90ed02018-09-13 08:45:12 -0700191 if (0 == vec_len(brpaths))
192 {
193 bier_table_route_delete(&bti, bp);
194 }
195 else
196 {
197 bier_table_route_path_update(&bti, bp, brpaths);
198 }
199 }
200 else if (mp->br_is_add)
201 {
202 bier_table_route_path_add(&bti, bp, brpaths);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700203 }
204 else
205 {
Neale Rannsef90ed02018-09-13 08:45:12 -0700206 bier_table_route_path_remove(&bti, bp, brpaths);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700207 }
Neale Rannsf0510722018-01-31 11:35:41 -0800208 vec_free(brpaths);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700209
210done:
Neale Rannsd792d9c2017-10-21 10:53:20 -0700211 rv = (rv == 0) ? vnm->api_errno : rv;
212
213 REPLY_MACRO (VL_API_BIER_ROUTE_ADD_DEL_REPLY);
214}
215
216typedef struct bier_route_details_walk_t_
217{
Florin Coras6c4dae22018-01-09 06:39:23 -0800218 vl_api_registration_t * reg;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700219 u32 context;
220} bier_route_details_walk_t;
221
222static void
223send_bier_route_details (const bier_table_t *bt,
224 const bier_entry_t *be,
225 void *args)
226{
Neale Rannsd792d9c2017-10-21 10:53:20 -0700227 bier_route_details_walk_t *ctx = args;
228 vl_api_bier_route_details_t *mp;
Neale Ranns097fa662018-05-01 05:17:55 -0700229 fib_path_encode_ctx_t path_ctx = {
230 .rpaths = NULL,
231 };
232 fib_route_path_t *rpath;
Neale Ranns31ed7442018-02-23 05:29:09 -0800233 vl_api_fib_path_t *fp;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700234 u32 n_paths, m_size;
235
236 n_paths = fib_path_list_get_n_paths(be->be_path_list);
Neale Ranns31ed7442018-02-23 05:29:09 -0800237 m_size = sizeof(*mp) + (n_paths * sizeof(vl_api_fib_path_t));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700238 mp = vl_msg_api_alloc(m_size);
239 if (!mp)
240 return;
241
Dave Barachb7b92992018-10-17 10:38:51 -0400242 clib_memset(mp, 0, m_size);
Filip Tehlar52d80c52021-06-17 01:28:22 +0000243 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_BIER_ROUTE_DETAILS);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700244 mp->context = ctx->context;
245
Neale Ranns097fa662018-05-01 05:17:55 -0700246 mp->br_route.br_tbl_id.bt_set = bt->bt_id.bti_set;
247 mp->br_route.br_tbl_id.bt_sub_domain = bt->bt_id.bti_sub_domain;
248 mp->br_route.br_tbl_id.bt_hdr_len_id = bt->bt_id.bti_hdr_len;
249 mp->br_route.br_bp = htonl(be->be_bp);
250 mp->br_route.br_n_paths = htonl(n_paths);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700251
Neale Ranns775f73c2018-12-20 03:01:49 -0800252 fib_path_list_walk_w_ext(be->be_path_list,
253 NULL,
254 fib_path_encode,
Neale Ranns097fa662018-05-01 05:17:55 -0700255 &path_ctx);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700256
Neale Ranns097fa662018-05-01 05:17:55 -0700257 fp = mp->br_route.br_paths;
258 vec_foreach (rpath, path_ctx.rpaths)
Neale Rannsd792d9c2017-10-21 10:53:20 -0700259 {
Neale Ranns097fa662018-05-01 05:17:55 -0700260 fib_api_path_encode(rpath, fp);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700261 fp++;
262 }
263
Neale Ranns097fa662018-05-01 05:17:55 -0700264 vec_free(path_ctx.rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800265 vl_api_send_msg (ctx->reg, (u8 *) mp);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700266}
267
268static void
269vl_api_bier_route_dump_t_handler (vl_api_bier_route_dump_t * mp)
270{
Florin Coras6c4dae22018-01-09 06:39:23 -0800271 vl_api_registration_t *reg;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700272
Florin Coras6c4dae22018-01-09 06:39:23 -0800273 reg = vl_api_client_index_to_registration (mp->client_index);
274 if (!reg)
275 return;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700276
277 bier_table_id_t bti = {
278 .bti_set = mp->br_tbl_id.bt_set,
279 .bti_sub_domain = mp->br_tbl_id.bt_sub_domain,
280 .bti_hdr_len = mp->br_tbl_id.bt_hdr_len_id,
281 .bti_type = BIER_TABLE_MPLS_SPF,
282 .bti_ecmp = BIER_ECMP_TABLE_ID_MAIN,
283 };
284 bier_route_details_walk_t ctx = {
Florin Coras6c4dae22018-01-09 06:39:23 -0800285 .reg = reg,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700286 .context = mp->context,
287 };
288 bier_table_walk(&bti, send_bier_route_details, &ctx);
289}
290
291static void
292vl_api_bier_imp_add_t_handler (vl_api_bier_imp_add_t * mp)
293{
294 vl_api_bier_imp_add_reply_t *rmp;
295 vnet_main_t *vnm;
Neale Ranns18075a02018-04-05 02:24:52 -0700296 index_t bii = ~0;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700297 int rv = 0;
298
299 vnm = vnet_get_main ();
300 vnm->api_errno = 0;
301
Neale Rannsf0510722018-01-31 11:35:41 -0800302 /*
303 * The BSL support by VPP is limited to the size of the
304 * available space in the vlib_buffer_t
305 */
306 if (mp->bi_tbl_id.bt_hdr_len_id >= BIER_HDR_LEN_2048)
307 {
308 rv = VNET_API_ERROR_BIER_BSL_UNSUP;
309 }
310 else
311 {
312 bier_table_id_t bti = {
313 .bti_set = mp->bi_tbl_id.bt_set,
314 .bti_sub_domain = mp->bi_tbl_id.bt_sub_domain,
315 .bti_hdr_len = mp->bi_tbl_id.bt_hdr_len_id,
316 .bti_type = BIER_TABLE_MPLS_SPF,
317 .bti_ecmp = BIER_ECMP_TABLE_ID_MAIN,
318 };
319 bier_bit_string_t bs = {
320 .bbs_len = mp->bi_n_bytes,
321 .bbs_buckets = mp->bi_bytes,
322 };
Neale Rannsd792d9c2017-10-21 10:53:20 -0700323
Neale Rannsf0510722018-01-31 11:35:41 -0800324 bii = bier_imp_add_or_lock(&bti, ntohs(mp->bi_src), &bs);
325 }
Neale Rannsd792d9c2017-10-21 10:53:20 -0700326
Neale Rannsd792d9c2017-10-21 10:53:20 -0700327 REPLY_MACRO2 (VL_API_BIER_IMP_ADD_REPLY,
328 ({
Neale Rannseea537a2018-01-09 04:11:28 -0800329 rmp->bi_index = ntohl (bii);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700330 }));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700331}
332
333static void
334vl_api_bier_imp_del_t_handler (vl_api_bier_imp_del_t * mp)
335{
336 vl_api_bier_imp_del_reply_t *rmp;
337 vnet_main_t *vnm;
338 int rv = 0;
339
340 vnm = vnet_get_main ();
341 vnm->api_errno = 0;
342
343 bier_imp_unlock(ntohl(mp->bi_index));
344
345 REPLY_MACRO(VL_API_BIER_IMP_DEL_REPLY);
346}
347
348static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800349send_bier_imp_details (vl_api_registration_t * reg,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700350 u32 context,
351 const bier_imp_t *bi)
352{
353 vl_api_bier_imp_details_t *mp;
354 bier_hdr_t copy;
355 u8 n_bytes;
356
357 copy = bi->bi_hdr;
358 bier_hdr_ntoh(&copy);
359
360 n_bytes = bier_hdr_len_id_to_num_bytes(
361 bier_hdr_get_len_id(&copy));
362 mp = vl_msg_api_alloc(sizeof(*mp) + n_bytes);
363 if (!mp)
364 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400365 clib_memset(mp, 0, sizeof(*mp)+n_bytes);
Filip Tehlar52d80c52021-06-17 01:28:22 +0000366 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_BIER_IMP_DETAILS);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700367 mp->context = context;
368
369 mp->bi_tbl_id.bt_set = bi->bi_tbl.bti_set;
370 mp->bi_tbl_id.bt_sub_domain = bi->bi_tbl.bti_sub_domain;
371 mp->bi_tbl_id.bt_hdr_len_id = bi->bi_tbl.bti_hdr_len;
372
Neale Rannsd792d9c2017-10-21 10:53:20 -0700373 mp->bi_src = htons(bier_hdr_get_src_id(&copy));
374 mp->bi_n_bytes = n_bytes;
Neale Rannsf0510722018-01-31 11:35:41 -0800375 memcpy(mp->bi_bytes, bi->bi_bits, n_bytes);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700376
Florin Coras6c4dae22018-01-09 06:39:23 -0800377 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700378}
379
380static void
381vl_api_bier_imp_dump_t_handler (vl_api_bier_imp_dump_t * mp)
382{
Florin Coras6c4dae22018-01-09 06:39:23 -0800383 vl_api_registration_t *reg;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700384 bier_imp_t *bi;
385
Florin Coras6c4dae22018-01-09 06:39:23 -0800386 reg = vl_api_client_index_to_registration (mp->client_index);
387 if (!reg)
388 return;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700389
Damjan Marionb2c31b62020-12-13 21:47:40 +0100390 pool_foreach (bi, bier_imp_pool)
391 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800392 send_bier_imp_details(reg, mp->context, bi);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100393 }
Neale Rannsd792d9c2017-10-21 10:53:20 -0700394}
395
396static void
397vl_api_bier_disp_table_add_del_t_handler (vl_api_bier_disp_table_add_del_t * mp)
398{
399 vl_api_bier_disp_table_add_del_reply_t *rmp;
400 vnet_main_t *vnm;
401 u32 table_id;
402 int rv;
403
404 vnm = vnet_get_main ();
405 vnm->api_errno = 0;
406 table_id = ntohl(mp->bdt_tbl_id);
407
408 if (mp->bdt_is_add)
409 {
410 bier_disp_table_add_or_lock(table_id);
411 }
412 else
413 {
414 bier_disp_table_unlock_w_table_id(table_id);
415 }
416
417 rv = vnm->api_errno;
418
419 REPLY_MACRO (VL_API_BIER_DISP_TABLE_ADD_DEL_REPLY);
420}
421
422static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800423send_bier_disp_table_details (vl_api_registration_t * reg,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700424 u32 context,
425 const bier_disp_table_t *bdt)
426{
427 vl_api_bier_disp_table_details_t *mp;
428
429 mp = vl_msg_api_alloc(sizeof(*mp));
430 if (!mp)
431 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400432 clib_memset(mp, 0, sizeof(*mp));
Filip Tehlar52d80c52021-06-17 01:28:22 +0000433 mp->_vl_msg_id =
434 ntohs (REPLY_MSG_ID_BASE + VL_API_BIER_DISP_TABLE_DETAILS);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700435 mp->context = context;
436
437 mp->bdt_tbl_id = htonl(bdt->bdt_table_id);
438
Florin Coras6c4dae22018-01-09 06:39:23 -0800439 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700440}
441
442static void
443vl_api_bier_disp_table_dump_t_handler (vl_api_bier_disp_table_dump_t * mp)
444{
Florin Coras6c4dae22018-01-09 06:39:23 -0800445 vl_api_registration_t *reg;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700446 bier_disp_table_t *bdt;
447
Florin Coras6c4dae22018-01-09 06:39:23 -0800448 reg = vl_api_client_index_to_registration (mp->client_index);
449 if (!reg)
450 return;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700451
Damjan Marionb2c31b62020-12-13 21:47:40 +0100452 pool_foreach (bdt, bier_disp_table_pool)
453 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800454 send_bier_disp_table_details(reg, mp->context, bdt);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100455 }
Neale Rannsd792d9c2017-10-21 10:53:20 -0700456}
457
458static void
459vl_api_bier_disp_entry_add_del_t_handler (vl_api_bier_disp_entry_add_del_t * mp)
460{
461 vl_api_bier_disp_entry_add_del_reply_t *rmp;
462 fib_route_path_t *brps = NULL, *brp;
463 vnet_main_t *vnm;
464 bier_bp_t bp;
465 u32 table_id;
466 int rv = 0;
467 u32 ii;
468
469 vnm = vnet_get_main ();
470 vnm->api_errno = 0;
471 table_id = ntohl(mp->bde_tbl_id);
472 bp = ntohs(mp->bde_bp);
473
Neale Rannsceb4d052017-12-13 09:13:41 -0800474 /*
475 * BP=0 is the default route
476 */
477 if (bp > 0xffff)
Neale Rannsd792d9c2017-10-21 10:53:20 -0700478 {
479 rv = -1;
480 goto done;
481 }
482
483 vec_validate(brps, mp->bde_n_paths - 1);
484 vec_foreach_index(ii, brps)
485 {
486 brp = &brps[ii];
487 brp->frp_fib_index = ntohl(mp->bde_paths[ii].table_id);
488 brp->frp_sw_if_index = ntohl(mp->bde_paths[ii].sw_if_index);
489
490 if (~0 != ntohl(mp->bde_paths[ii].rpf_id))
491 {
492 brp->frp_flags = FIB_ROUTE_PATH_RPF_ID;
493 brp->frp_rpf_id = ntohl(mp->bde_paths[ii].rpf_id);
494 }
495
Neale Ranns097fa662018-05-01 05:17:55 -0700496 if (FIB_API_PATH_NH_PROTO_IP4 == mp->bde_paths[ii].proto)
Neale Rannsd792d9c2017-10-21 10:53:20 -0700497 {
Neale Ranns097fa662018-05-01 05:17:55 -0700498 clib_memcpy (&brp->frp_addr.ip4,
499 &mp->bde_paths[ii].nh.address.ip4,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700500 sizeof (brp->frp_addr.ip4));
501 }
Neale Ranns097fa662018-05-01 05:17:55 -0700502 else if (FIB_API_PATH_NH_PROTO_IP6 == mp->bde_paths[ii].proto)
Neale Rannsd792d9c2017-10-21 10:53:20 -0700503 {
Neale Ranns097fa662018-05-01 05:17:55 -0700504 clib_memcpy (&brp->frp_addr.ip6,
505 &mp->bde_paths[ii].nh.address.ip6,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700506 sizeof (brp->frp_addr.ip6));
507 }
508 if (ip46_address_is_zero(&brp->frp_addr))
509 {
510 index_t fti;
511
512 switch (mp->bde_payload_proto)
513 {
514 case BIER_HDR_PROTO_INVALID:
515 case BIER_HDR_PROTO_MPLS_DOWN_STREAM:
516 case BIER_HDR_PROTO_MPLS_UP_STREAM:
517 case BIER_HDR_PROTO_ETHERNET:
518 case BIER_HDR_PROTO_VXLAN:
519 case BIER_HDR_PROTO_CTRL:
520 case BIER_HDR_PROTO_OAM:
521 rv = VNET_API_ERROR_UNSUPPORTED;
522 goto done;
523 break;
524 case BIER_HDR_PROTO_IPV4:
525 case BIER_HDR_PROTO_IPV6:
526 {
527 fib_protocol_t fproto;
528
529 fproto = (mp->bde_payload_proto == BIER_HDR_PROTO_IPV4 ?
530 FIB_PROTOCOL_IP4 :
531 FIB_PROTOCOL_IP6);
532
533 if (brp->frp_flags & FIB_ROUTE_PATH_RPF_ID)
534 {
535 fti = mfib_table_find (fproto,
536 ntohl (mp->bde_paths[ii].table_id));
537 }
538 else
539 {
540 fti = fib_table_find (fproto,
541 ntohl (mp->bde_paths[ii].table_id));
542 }
543
544 if (INDEX_INVALID != fti)
545 {
546 brp->frp_fib_index = fti;
547 }
548 else
549 {
550 rv = VNET_API_ERROR_NO_SUCH_FIB;
551 goto done;
552 }
553 break;
554 }
555 }
556 }
557 }
558
559 if (mp->bde_is_add)
560 {
561 bier_disp_table_entry_path_add(table_id, bp,
562 mp->bde_payload_proto,
563 brps);
564 }
565 else
566 {
567 bier_disp_table_entry_path_remove(table_id, bp,
568 mp->bde_payload_proto,
569 brps);
570 }
571
572done:
573 vec_free(brps);
574 rv = (rv == 0) ? vnm->api_errno : rv;
575
576 REPLY_MACRO (VL_API_BIER_DISP_ENTRY_ADD_DEL_REPLY);
577}
578
579typedef struct bier_disp_entry_details_walk_t_
580{
Florin Coras6c4dae22018-01-09 06:39:23 -0800581 vl_api_registration_t * reg;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700582 u32 context;
583} bier_disp_entry_details_walk_t;
584
585static void
586send_bier_disp_entry_details (const bier_disp_table_t *bdt,
587 const bier_disp_entry_t *bde,
588 u16 bp,
589 void *args)
590{
Neale Rannsd792d9c2017-10-21 10:53:20 -0700591 bier_disp_entry_details_walk_t *ctx = args;
592 vl_api_bier_disp_entry_details_t *mp;
593 bier_hdr_proto_id_t pproto;
Neale Ranns31ed7442018-02-23 05:29:09 -0800594 vl_api_fib_path_t *fp;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700595 u32 n_paths, m_size;
596
597 FOR_EACH_BIER_HDR_PROTO(pproto)
598 {
599 fib_node_index_t pl = bde->bde_pl[pproto];
Neale Ranns097fa662018-05-01 05:17:55 -0700600
Neale Rannsd792d9c2017-10-21 10:53:20 -0700601 if (INDEX_INVALID != pl)
602 {
Neale Ranns097fa662018-05-01 05:17:55 -0700603 fib_path_encode_ctx_t path_ctx = {
604 .rpaths = NULL,
605 };
606 fib_route_path_t *rpath;
607
Neale Rannsd792d9c2017-10-21 10:53:20 -0700608 n_paths = fib_path_list_get_n_paths(pl);
Neale Ranns31ed7442018-02-23 05:29:09 -0800609 m_size = sizeof(*mp) + (n_paths * sizeof(vl_api_fib_path_t));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700610 mp = vl_msg_api_alloc(m_size);
611 if (!mp)
612 return;
613
Dave Barachb7b92992018-10-17 10:38:51 -0400614 clib_memset(mp, 0, m_size);
Filip Tehlar52d80c52021-06-17 01:28:22 +0000615 mp->_vl_msg_id =
616 ntohs (REPLY_MSG_ID_BASE + VL_API_BIER_DISP_ENTRY_DETAILS);
617 mp->context = ctx->context;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700618
Filip Tehlar52d80c52021-06-17 01:28:22 +0000619 mp->bde_tbl_id = htonl (bdt->bdt_table_id);
620 mp->bde_n_paths = htonl (n_paths);
621 mp->bde_payload_proto = pproto;
622 mp->bde_bp = htons (bp);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700623
Filip Tehlar52d80c52021-06-17 01:28:22 +0000624 fib_path_list_walk_w_ext (pl, NULL, fib_path_encode, &path_ctx);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700625
Filip Tehlar52d80c52021-06-17 01:28:22 +0000626 fp = mp->bde_paths;
627 vec_foreach (rpath, path_ctx.rpaths)
628 {
629 fib_api_path_encode (rpath, fp);
630 fp++;
631 }
Neale Rannsd792d9c2017-10-21 10:53:20 -0700632
Filip Tehlar52d80c52021-06-17 01:28:22 +0000633 vl_api_send_msg (ctx->reg, (u8 *) mp);
634 vec_free (path_ctx.rpaths);
635 }
Neale Rannsd792d9c2017-10-21 10:53:20 -0700636 }
637}
638
639static void
640vl_api_bier_disp_entry_dump_t_handler (vl_api_bier_disp_entry_dump_t * mp)
641{
Florin Coras6c4dae22018-01-09 06:39:23 -0800642 vl_api_registration_t *reg;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700643
Florin Coras6c4dae22018-01-09 06:39:23 -0800644 reg = vl_api_client_index_to_registration (mp->client_index);
645 if (!reg)
646 return;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700647
648 bier_disp_entry_details_walk_t ctx = {
Florin Coras6c4dae22018-01-09 06:39:23 -0800649 .reg = reg,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700650 .context = mp->context,
651 };
652 bier_disp_table_walk(ntohl(mp->bde_tbl_id),
653 send_bier_disp_entry_details,
654 &ctx);
655}
656
Filip Tehlar52d80c52021-06-17 01:28:22 +0000657#include <bier/bier.api.c>
Neale Rannsd792d9c2017-10-21 10:53:20 -0700658
659static clib_error_t *
660bier_api_hookup (vlib_main_t * vm)
661{
Filip Tehlar52d80c52021-06-17 01:28:22 +0000662 bier_main.msg_id_base = setup_message_id_table ();
Neale Rannsd792d9c2017-10-21 10:53:20 -0700663
Filip Tehlar52d80c52021-06-17 01:28:22 +0000664 return 0;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700665}
666
667VLIB_API_INIT_FUNCTION (bier_api_hookup);