blob: 0254c551411ab0184e79c974217737c6060d9b0b [file] [log] [blame]
Neale Ranns81458422018-03-12 06:59:36 -07001/*
2 * Copyright (c) 2016 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
16#include <vnet/vnet.h>
17#include <vlibmemory/api.h>
18#include <vnet/fib/fib_api.h>
Neale Ranns097fa662018-05-01 05:17:55 -070019#include <vnet/ip/ip_types_api.h>
Neale Ranns81458422018-03-12 06:59:36 -070020#include <vnet/fib/fib_table.h>
Neale Ranns775f73c2018-12-20 03:01:49 -080021#include <vnet/mfib/mfib_table.h>
Neale Ranns81458422018-03-12 06:59:36 -070022#include <vnet/bier/bier_disp_table.h>
Neale Ranns097fa662018-05-01 05:17:55 -070023#include <vpp/api/types.h>
Neale Rannsba4a5bf2020-01-09 06:43:14 +000024#include <vnet/classify/vnet_classify.h>
Neale Ranns976b2592019-12-04 06:11:00 +000025#include <vnet/ip/ip_format_fns.h>
Neale Ranns81458422018-03-12 06:59:36 -070026
Neale Ranns976b2592019-12-04 06:11:00 +000027#include <vnet/fib/fib.api_enum.h>
28#include <vnet/fib/fib.api_types.h>
Neale Ranns81458422018-03-12 06:59:36 -070029
Neale Ranns976b2592019-12-04 06:11:00 +000030static u16 fib_base_msg_id;
31#define REPLY_MSG_ID_BASE fib_base_msg_id
Neale Ranns81458422018-03-12 06:59:36 -070032#include <vlibapi/api_helper_macros.h>
33
34int
Neale Ranns097fa662018-05-01 05:17:55 -070035fib_api_table_id_decode (fib_protocol_t fproto,
36 u32 table_id,
37 u32 *fib_index)
Neale Ranns81458422018-03-12 06:59:36 -070038{
Neale Ranns097fa662018-05-01 05:17:55 -070039 *fib_index = fib_table_find(fproto, table_id);
Neale Ranns81458422018-03-12 06:59:36 -070040
Neale Ranns097fa662018-05-01 05:17:55 -070041 if (INDEX_INVALID == *fib_index)
Neale Rannsf726f532019-03-11 05:34:50 -070042 {
Neale Ranns097fa662018-05-01 05:17:55 -070043 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Rannsf726f532019-03-11 05:34:50 -070044 }
45
Neale Ranns097fa662018-05-01 05:17:55 -070046 return (0);
47}
48
49int
50fib_api_mtable_id_decode (fib_protocol_t fproto,
51 u32 table_id,
52 u32 *fib_index)
53{
54 *fib_index = mfib_table_find(fproto, table_id);
55
56 if (~0 == *fib_index)
Neale Ranns81458422018-03-12 06:59:36 -070057 {
Neale Ranns097fa662018-05-01 05:17:55 -070058 return VNET_API_ERROR_NO_SUCH_FIB;
59 }
60
61 return (0);
62}
63
64static void
65fib_api_next_hop_decode (const vl_api_fib_path_t *in,
66 ip46_address_t *out)
67{
68 if (in->proto == FIB_API_PATH_NH_PROTO_IP4)
Zhiyong Yangd3d7ef52020-01-09 04:20:57 -050069 clib_memcpy (&out->ip4, &in->nh.address.ip4, sizeof (out->ip4));
Neale Ranns097fa662018-05-01 05:17:55 -070070 else if (in->proto == FIB_API_PATH_NH_PROTO_IP6)
Zhiyong Yangd3d7ef52020-01-09 04:20:57 -050071 clib_memcpy (&out->ip6, &in->nh.address.ip6, sizeof (out->ip6));
Neale Ranns097fa662018-05-01 05:17:55 -070072}
73
74static vl_api_fib_path_nh_proto_t
75fib_api_path_dpo_proto_to_nh (dpo_proto_t dproto)
76{
77 switch (dproto)
78 {
79 case DPO_PROTO_IP4:
80 return (FIB_API_PATH_NH_PROTO_IP4);
81 case DPO_PROTO_IP6:
82 return (FIB_API_PATH_NH_PROTO_IP6);
83 case DPO_PROTO_MPLS:
84 return (FIB_API_PATH_NH_PROTO_MPLS);
85 case DPO_PROTO_BIER:
86 return (FIB_API_PATH_NH_PROTO_BIER);
87 case DPO_PROTO_ETHERNET:
88 return (FIB_API_PATH_NH_PROTO_ETHERNET);
89 case DPO_PROTO_NSH:
90 ASSERT(0);
91 break;
92 }
93 return (FIB_API_PATH_NH_PROTO_IP4);
94}
95
96
97static void
98fib_api_next_hop_encode (const fib_route_path_t *rpath,
99 vl_api_fib_path_t *fp)
100{
101 fp->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
102
103 if (rpath->frp_proto == DPO_PROTO_IP4)
Zhiyong Yangd3d7ef52020-01-09 04:20:57 -0500104 clib_memcpy (&fp->nh.address.ip4,
Neale Ranns097fa662018-05-01 05:17:55 -0700105 &rpath->frp_addr.ip4,
106 sizeof (rpath->frp_addr.ip4));
107 else if (rpath->frp_proto == DPO_PROTO_IP6)
Zhiyong Yangd3d7ef52020-01-09 04:20:57 -0500108 clib_memcpy (&fp->nh.address.ip6,
Neale Ranns097fa662018-05-01 05:17:55 -0700109 &rpath->frp_addr.ip6,
110 sizeof (rpath->frp_addr.ip6));
111}
112
113static int
114fib_api_path_nh_proto_to_dpo (vl_api_fib_path_nh_proto_t pp,
115 dpo_proto_t *dproto)
116{
117 switch (pp)
118 {
119 case FIB_API_PATH_NH_PROTO_IP4:
120 *dproto = DPO_PROTO_IP4;
121 break;
122 case FIB_API_PATH_NH_PROTO_IP6:
123 *dproto = DPO_PROTO_IP6;
124 break;
125 case FIB_API_PATH_NH_PROTO_MPLS:
126 *dproto = DPO_PROTO_MPLS;
127 break;
128 case FIB_API_PATH_NH_PROTO_BIER:
129 *dproto = DPO_PROTO_BIER;
130 break;
131 case FIB_API_PATH_NH_PROTO_ETHERNET:
132 *dproto = DPO_PROTO_ETHERNET;
133 break;
134 default:
135 return (-1);
136 }
137 return (0);
138}
139
140int
141fib_api_path_decode (vl_api_fib_path_t *in,
142 fib_route_path_t *out)
143{
144 vnet_classify_main_t *cm = &vnet_classify_main;
145 int rv = 0, n_labels;
146 vnet_main_t *vnm;
147 u8 ii;
148
149 vnm = vnet_get_main ();
150 clib_memset(&out->frp_dpo, 0, sizeof(out->frp_dpo));
151
152 /* enums are u32 */
153 in->flags = ntohl (in->flags);
154 in->type = ntohl (in->type);
155 in->proto = ntohl (in->proto);
156
157 /*
158 * attributes that apply to all path types
159 */
160 out->frp_flags = 0;
161 out->frp_weight = in->weight;
162 if (0 == out->frp_weight)
163 {
164 out->frp_weight = 1;
165 }
166 out->frp_preference = in->preference;
167
168 rv = fib_api_path_nh_proto_to_dpo(in->proto, &out->frp_proto);
169
170 if (0 != rv)
171 return (rv);
172
173 /*
174 * convert the flags and the AFI to determine the path type
175 */
176 if (in->flags & FIB_API_PATH_FLAG_RESOLVE_VIA_HOST)
177 out->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
178 if (in->flags & FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED)
179 out->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns1dbcf302019-07-19 11:44:53 +0000180 if (in->flags & FIB_API_PATH_FLAG_POP_PW_CW)
181 out->frp_flags |= FIB_ROUTE_PATH_POP_PW_CW;
Neale Ranns097fa662018-05-01 05:17:55 -0700182
183 switch (in->type)
184 {
185 case FIB_API_PATH_TYPE_DVR:
186 out->frp_sw_if_index = ntohl(in->sw_if_index);
187 out->frp_flags |= FIB_ROUTE_PATH_DVR;
188 break;
189 case FIB_API_PATH_TYPE_INTERFACE_RX:
190 out->frp_sw_if_index = ntohl(in->sw_if_index);
191 out->frp_flags |= FIB_ROUTE_PATH_INTF_RX;
192 break;
193 case FIB_API_PATH_TYPE_DROP:
194 out->frp_flags |= FIB_ROUTE_PATH_DROP;
195 break;
196 case FIB_API_PATH_TYPE_LOCAL:
197 out->frp_flags |= FIB_ROUTE_PATH_LOCAL;
198 out->frp_sw_if_index = ntohl(in->sw_if_index);
199 break;
200 case FIB_API_PATH_TYPE_ICMP_UNREACH:
201 out->frp_flags |= FIB_ROUTE_PATH_ICMP_UNREACH;
202 break;
203 case FIB_API_PATH_TYPE_ICMP_PROHIBIT:
204 out->frp_flags |= FIB_ROUTE_PATH_ICMP_PROHIBIT;
205 break;
206 case FIB_API_PATH_TYPE_CLASSIFY:
207 out->frp_flags |= FIB_ROUTE_PATH_CLASSIFY;
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100208
Neale Ranns097fa662018-05-01 05:17:55 -0700209 if (pool_is_free_index (cm->tables, ntohl (in->nh.classify_table_index)))
210 {
211 return VNET_API_ERROR_NO_SUCH_TABLE;
212 }
213 out->frp_classify_table_id = ntohl (in->nh.classify_table_index);
214 break;
215 case FIB_API_PATH_TYPE_UDP_ENCAP:
216 out->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
217 out->frp_udp_encap_id = ntohl(in->nh.obj_id);
218 break;
219 case FIB_API_PATH_TYPE_BIER_IMP:
220 out->frp_flags |= FIB_ROUTE_PATH_BIER_IMP;
221 out->frp_bier_imp = ntohl (in->nh.obj_id);
222 break;
223
224 case FIB_API_PATH_TYPE_SOURCE_LOOKUP:
225 out->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
226 /* fall through */
227 case FIB_API_PATH_TYPE_NORMAL:
228 switch (out->frp_proto)
229 {
230 case DPO_PROTO_IP4:
231 case DPO_PROTO_IP6:
232 fib_api_next_hop_decode(in, &out->frp_addr);
233 out->frp_sw_if_index = ntohl(in->sw_if_index);
234 out->frp_rpf_id = ntohl(in->rpf_id);
235
236 if (0 == out->frp_rpf_id)
237 {
238 /* allow 0 to be an unset value on the API */
239 out->frp_rpf_id = ~0;
240 }
241
242 if (~0 != out->frp_rpf_id)
243 {
244 out->frp_flags |= FIB_ROUTE_PATH_RPF_ID;
245 }
246
247 if (~0 == out->frp_sw_if_index)
248 {
249 /* recursive or deag, validate the next-hop FIB */
250 if (~0 != out->frp_rpf_id)
251 {
252 rv = fib_api_mtable_id_decode(
253 dpo_proto_to_fib(out->frp_proto),
254 ntohl(in->table_id),
255 &out->frp_fib_index);
256 }
257 else
258 {
259 rv = fib_api_table_id_decode(
260 dpo_proto_to_fib(out->frp_proto),
261 ntohl(in->table_id),
262 &out->frp_fib_index);
263 }
264 if (0 != rv)
265 {
266 return (rv);
267 }
268 }
269 else
270 {
271 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
272 out->frp_sw_if_index))
273 {
274 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
275 }
276 }
277
278 if (ip46_address_is_zero(&out->frp_addr))
279 {
280 if (~0 == out->frp_sw_if_index &&
281 ~0 != out->frp_fib_index)
282 {
283 out->frp_flags |= FIB_ROUTE_PATH_DEAG;
284 }
285 }
286
287 break;
288 case DPO_PROTO_MPLS:
289 out->frp_local_label = ntohl (in->nh.via_label);
290 out->frp_eos = MPLS_NON_EOS;
291 out->frp_sw_if_index = ~0;
292 break;
293 case DPO_PROTO_BIER:
294 out->frp_sw_if_index = ntohl(in->sw_if_index);
295 out->frp_rpf_id = ntohl(in->rpf_id);
296
297 if (!(out->frp_flags & FIB_ROUTE_PATH_BIER_IMP))
298 {
299 fib_api_next_hop_decode(in, &out->frp_addr);
300
301 if (ip46_address_is_zero(&out->frp_addr))
302 {
303 index_t bdti;
304
305 bdti = bier_disp_table_find(ntohl(in->table_id));
306
307 if (INDEX_INVALID != bdti)
308 {
309 out->frp_fib_index = bdti;
310 }
311 else
312 {
313 return (VNET_API_ERROR_NO_SUCH_FIB);
314 }
315 }
316 }
317 break;
318 case DPO_PROTO_ETHERNET:
319 out->frp_sw_if_index = ntohl(in->sw_if_index);
320 break;
321 case DPO_PROTO_NSH:
322 break;
323 }
Neale Ranns81458422018-03-12 06:59:36 -0700324 }
325
326 n_labels = in->n_labels;
Neale Ranns097fa662018-05-01 05:17:55 -0700327 if (n_labels != 0)
Neale Ranns81458422018-03-12 06:59:36 -0700328 {
329 vec_validate (out->frp_label_stack, n_labels - 1);
330 for (ii = 0; ii < n_labels; ii++)
331 {
332 out->frp_label_stack[ii].fml_value =
333 ntohl(in->label_stack[ii].label);
334 out->frp_label_stack[ii].fml_ttl =
335 in->label_stack[ii].ttl;
336 out->frp_label_stack[ii].fml_exp =
337 in->label_stack[ii].exp;
338 out->frp_label_stack[ii].fml_mode =
339 (in->label_stack[ii].is_uniform ?
340 FIB_MPLS_LSP_MODE_UNIFORM :
341 FIB_MPLS_LSP_MODE_PIPE);
342 }
343 }
344
Neale Ranns097fa662018-05-01 05:17:55 -0700345 return (0);
Neale Ranns81458422018-03-12 06:59:36 -0700346}
347
348void
Neale Ranns097fa662018-05-01 05:17:55 -0700349fib_api_path_encode (const fib_route_path_t * rpath,
Neale Ranns81458422018-03-12 06:59:36 -0700350 vl_api_fib_path_t *out)
351{
Neale Ranns097fa662018-05-01 05:17:55 -0700352 memset (out, 0, sizeof (*out));
Neale Ranns775f73c2018-12-20 03:01:49 -0800353
Neale Ranns097fa662018-05-01 05:17:55 -0700354 out->weight = rpath->frp_weight;
355 out->preference = rpath->frp_preference;
356 out->sw_if_index = htonl (rpath->frp_sw_if_index);
357 out->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
358 out->rpf_id = rpath->frp_rpf_id;
359 fib_api_next_hop_encode (rpath, out);
360
361 if (0 != rpath->frp_fib_index)
Neale Ranns81458422018-03-12 06:59:36 -0700362 {
Neale Ranns097fa662018-05-01 05:17:55 -0700363 if ((DPO_PROTO_IP6 == rpath->frp_proto) ||
364 (DPO_PROTO_IP4 == rpath->frp_proto))
Neale Ranns81458422018-03-12 06:59:36 -0700365 {
Neale Ranns097fa662018-05-01 05:17:55 -0700366 if (rpath->frp_flags & FIB_ROUTE_PATH_RPF_ID)
367 {
368 out->table_id = htonl (mfib_table_get_table_id(
369 rpath->frp_fib_index,
370 dpo_proto_to_fib(rpath->frp_proto)));
371 }
372 else
373 {
374 out->table_id = htonl (fib_table_get_table_id(
375 rpath->frp_fib_index,
376 dpo_proto_to_fib(rpath->frp_proto)));
377 }
Neale Ranns81458422018-03-12 06:59:36 -0700378 }
Neale Ranns097fa662018-05-01 05:17:55 -0700379 }
380
381 if (rpath->frp_flags & FIB_ROUTE_PATH_DVR)
382 {
383 out->type = FIB_API_PATH_TYPE_DVR;
384 }
385 else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_UNREACH)
386 {
387 out->type = FIB_API_PATH_TYPE_ICMP_UNREACH;
388 }
389 else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_PROHIBIT)
390 {
391 out->type = FIB_API_PATH_TYPE_ICMP_PROHIBIT;
392 }
393 else if (rpath->frp_flags & FIB_ROUTE_PATH_LOCAL)
394 {
395 out->type = FIB_API_PATH_TYPE_LOCAL;
396 }
397 else if (rpath->frp_flags & FIB_ROUTE_PATH_DROP)
398 {
399 out->type = FIB_API_PATH_TYPE_DROP;
400 }
401 else if (rpath->frp_flags & FIB_ROUTE_PATH_UDP_ENCAP)
402 {
403 out->type = FIB_API_PATH_TYPE_UDP_ENCAP;
404 out->nh.obj_id = rpath->frp_udp_encap_id;
405 }
406 else if (rpath->frp_flags & FIB_ROUTE_PATH_BIER_IMP)
407 {
408 out->type = FIB_API_PATH_TYPE_BIER_IMP;
409 out->nh.obj_id = rpath->frp_bier_imp;
410 }
IJsbrand Wijnands79437c82020-03-05 06:25:32 -0800411 else if (rpath->frp_flags & FIB_ROUTE_PATH_INTF_RX)
412 {
413 out->type = FIB_API_PATH_TYPE_INTERFACE_RX;
414 }
Neale Ranns097fa662018-05-01 05:17:55 -0700415 else
416 {
417 out->type = FIB_API_PATH_TYPE_NORMAL;
418 }
419 if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_HOST)
420 {
421 out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_HOST;
422 }
423 if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED)
424 {
425 out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED;
426 }
427
428 out->flags = htonl (out->flags);
429 out->type = htonl (out->type);
430 out->proto = htonl (out->proto);
431
432 if (rpath->frp_label_stack)
433 {
434 int ii;
435
436 for (ii = 0; ii < vec_len(rpath->frp_label_stack); ii++)
437 {
438 out->label_stack[ii].label =
439 htonl(rpath->frp_label_stack[ii].fml_value);
440 out->label_stack[ii].ttl =
441 rpath->frp_label_stack[ii].fml_ttl;
442 out->label_stack[ii].exp =
443 rpath->frp_label_stack[ii].fml_exp;
444 }
445 out->n_labels = ii;
446 }
447}
448
Neale Rannsc2ac2352019-07-02 14:33:29 +0000449int
Neale Ranns097fa662018-05-01 05:17:55 -0700450fib_api_route_add_del (u8 is_add,
451 u8 is_multipath,
452 u32 fib_index,
453 const fib_prefix_t * prefix,
Neale Ranns976b2592019-12-04 06:11:00 +0000454 fib_source_t src,
Neale Ranns097fa662018-05-01 05:17:55 -0700455 fib_entry_flag_t entry_flags,
456 fib_route_path_t *rpaths)
457{
Neale Rannsc2ac2352019-07-02 14:33:29 +0000458 if (is_multipath)
Neale Ranns097fa662018-05-01 05:17:55 -0700459 {
Neale Rannsc2ac2352019-07-02 14:33:29 +0000460 if (vec_len(rpaths) == 0)
461 return (VNET_API_ERROR_NO_PATHS_IN_ROUTE);
462
463 /* Iterative path add/remove */
464 if (is_add)
465 fib_table_entry_path_add2 (fib_index,
466 prefix,
Neale Ranns976b2592019-12-04 06:11:00 +0000467 src,
Neale Rannsc2ac2352019-07-02 14:33:29 +0000468 entry_flags,
469 rpaths);
470 else
471 fib_table_entry_path_remove2 (fib_index,
472 prefix,
Neale Ranns976b2592019-12-04 06:11:00 +0000473 src,
Neale Rannsc2ac2352019-07-02 14:33:29 +0000474 rpaths);
Neale Ranns097fa662018-05-01 05:17:55 -0700475 }
Neale Rannsc2ac2352019-07-02 14:33:29 +0000476 else
Neale Ranns097fa662018-05-01 05:17:55 -0700477 {
Neale Rannsc2ac2352019-07-02 14:33:29 +0000478 if (is_add)
479 {
480 if (vec_len(rpaths) == 0)
481 return (VNET_API_ERROR_NO_PATHS_IN_ROUTE);
482
483 /* path replacement */
484 fib_table_entry_update (fib_index,
485 prefix,
Neale Ranns976b2592019-12-04 06:11:00 +0000486 src,
Neale Rannsc2ac2352019-07-02 14:33:29 +0000487 entry_flags,
488 rpaths);
489 }
490 else
491 /* entry delete */
492 fib_table_entry_delete (fib_index,
493 prefix,
Neale Ranns976b2592019-12-04 06:11:00 +0000494 src);
Neale Ranns097fa662018-05-01 05:17:55 -0700495 }
Neale Rannsc2ac2352019-07-02 14:33:29 +0000496
497 return (0);
Neale Ranns097fa662018-05-01 05:17:55 -0700498}
499
500u8*
501format_vl_api_fib_path (u8 * s, va_list * args)
502{
503 const vl_api_fib_path_t *path = va_arg (*args, vl_api_fib_path_t*);
504
505 s = format (s, "sw_if_index %d", ntohl (path->sw_if_index));
506 switch (clib_net_to_host_u32(path->proto))
507 {
508 case FIB_API_PATH_NH_PROTO_IP4:
509 s = format (s, " %U", format_vl_api_address_union,
510 &path->nh.address, ADDRESS_IP4);
511 break;
512 case FIB_API_PATH_NH_PROTO_IP6:
513 s = format (s, " %U", format_vl_api_address_union,
514 &path->nh.address, ADDRESS_IP6);
Neale Ranns81458422018-03-12 06:59:36 -0700515 break;
516 default:
517 break;
518 }
Neale Ranns097fa662018-05-01 05:17:55 -0700519 s = format (s, " weight %d", path->weight);
520 s = format (s, " preference %d", path->preference);
521 s = format (s, " type %d", ntohl(path->type));
522 s = format (s, " proto %d", ntohl(path->proto));
523 s = format (s, " flags %d", ntohl(path->flags));
524 s = format (s, " n_labels %d", ntohl(path->n_labels));
525 s = format (s, " table-id %d", ntohl(path->table_id));
526 s = format (s, " rpf-id %d", ntohl(path->rpf_id));
Neale Ranns81458422018-03-12 06:59:36 -0700527
Neale Ranns097fa662018-05-01 05:17:55 -0700528 return (s);
Neale Ranns81458422018-03-12 06:59:36 -0700529}
Neale Ranns2303cb12018-02-21 04:57:17 -0800530
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100531int
532fib_proto_from_api_address_family (vl_api_address_family_t af, fib_protocol_t * out)
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700533{
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100534 switch (af)
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700535 {
536 case ADDRESS_IP4:
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100537 *out = (FIB_PROTOCOL_IP4);
538 return (0);
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700539 case ADDRESS_IP6:
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100540 *out = (FIB_PROTOCOL_IP6);
541 return (0);
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700542 }
543
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100544 return (VNET_API_ERROR_INVALID_ADDRESS_FAMILY);
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700545}
546
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100547vl_api_address_family_t
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700548fib_proto_to_api_address_family (fib_protocol_t fproto)
549{
550 switch (fproto)
551 {
552 case FIB_PROTOCOL_IP4:
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100553 return (ADDRESS_IP4);
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700554 case FIB_PROTOCOL_IP6:
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100555 return (ADDRESS_IP6);
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700556 default:
557 break;
558 }
559
560 ASSERT(0);
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100561 return (ADDRESS_IP4);
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700562}
Neale Ranns976b2592019-12-04 06:11:00 +0000563
564void
565vl_api_fib_source_add_t_handler (vl_api_fib_source_add_t * mp)
566{
567 vl_api_fib_source_add_reply_t *rmp;
568 fib_source_t src;
569 int rv = 0;
570 u8 *name;
571
572 name = format (0, "%s", mp->src.name);
573 vec_add1 (name, 0);
574
575 src = fib_source_allocate((const char *)name,
576 mp->src.priority,
577 FIB_SOURCE_BH_API);
578
579 vec_free(name);
580
581 REPLY_MACRO2 (VL_API_FIB_SOURCE_ADD_REPLY,
582 ({
583 rmp->id = src;
584 }));
585}
586
587typedef struct fib_source_dump_ctx_t_
588{
589 vl_api_registration_t * reg;
590 u32 context;
591} fib_source_dump_ctx_t;
592
593static walk_rc_t
594send_fib_source (fib_source_t id,
595 const char *name,
596 fib_source_priority_t prio,
597 fib_source_behaviour_t bh,
598 void *data)
599{
600 vl_api_fib_source_details_t *mp;
601 fib_source_dump_ctx_t *ctx;
602
603 ctx = data;
604 mp = vl_msg_api_alloc_zero (sizeof (*mp));
605 if (!mp)
606 return WALK_STOP;
607
608 mp->_vl_msg_id = ntohs (VL_API_FIB_SOURCE_DETAILS + REPLY_MSG_ID_BASE);
609 mp->context = ctx->context;
610
611 mp->src.priority = prio;
612 mp->src.id = id;
613 clib_memcpy(mp->src.name, name,
614 clib_min(strlen(name), ARRAY_LEN(mp->src.name)));
615
616 vl_api_send_msg (ctx->reg, (u8 *) mp);
617
618 return (WALK_CONTINUE);
619}
620
621void
622vl_api_fib_source_dump_t_handler (vl_api_fib_source_dump_t * mp)
623{
624 vl_api_registration_t *reg;
625
626 reg = vl_api_client_index_to_registration (mp->client_index);
627 if (!reg)
628 return;
629
630 fib_source_dump_ctx_t ctx = {
631 .reg = reg,
632 .context = mp->context,
633 };
634
635 fib_source_walk(send_fib_source, &ctx);
636}
637
638
639#include <vnet/fib/fib.api.c>
640
641static clib_error_t *
642fib_api_hookup (vlib_main_t * vm)
643{
644 /*
645 * Set up the (msg_name, crc, message-id) table
646 */
647 fib_base_msg_id = setup_message_id_table ();
648
649 return (NULL);
650}
651
652VLIB_API_INIT_FUNCTION (fib_api_hookup);