blob: c8511c08eba3113fc946dfdd5ca59354fddcccad [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{
Benoît Gannea0f3f8c2021-10-01 19:20:02 +020068 ASSERT (FIB_API_PATH_NH_PROTO_IP4 == in->proto || FIB_API_PATH_NH_PROTO_IP6 == in->proto);
69 *out = to_ip46 (FIB_API_PATH_NH_PROTO_IP6 == in->proto, (void *)&in->nh.address);
Neale Ranns097fa662018-05-01 05:17:55 -070070}
71
Benoît Ganne83e73702021-08-10 16:23:36 +020072vl_api_fib_path_nh_proto_t
Neale Ranns097fa662018-05-01 05:17:55 -070073fib_api_path_dpo_proto_to_nh (dpo_proto_t dproto)
74{
75 switch (dproto)
76 {
77 case DPO_PROTO_IP4:
78 return (FIB_API_PATH_NH_PROTO_IP4);
79 case DPO_PROTO_IP6:
80 return (FIB_API_PATH_NH_PROTO_IP6);
81 case DPO_PROTO_MPLS:
82 return (FIB_API_PATH_NH_PROTO_MPLS);
83 case DPO_PROTO_BIER:
84 return (FIB_API_PATH_NH_PROTO_BIER);
85 case DPO_PROTO_ETHERNET:
86 return (FIB_API_PATH_NH_PROTO_ETHERNET);
87 case DPO_PROTO_NSH:
88 ASSERT(0);
89 break;
90 }
91 return (FIB_API_PATH_NH_PROTO_IP4);
92}
93
94
95static void
96fib_api_next_hop_encode (const fib_route_path_t *rpath,
97 vl_api_fib_path_t *fp)
98{
99 fp->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
100
101 if (rpath->frp_proto == DPO_PROTO_IP4)
Zhiyong Yangd3d7ef52020-01-09 04:20:57 -0500102 clib_memcpy (&fp->nh.address.ip4,
Neale Ranns097fa662018-05-01 05:17:55 -0700103 &rpath->frp_addr.ip4,
104 sizeof (rpath->frp_addr.ip4));
105 else if (rpath->frp_proto == DPO_PROTO_IP6)
Zhiyong Yangd3d7ef52020-01-09 04:20:57 -0500106 clib_memcpy (&fp->nh.address.ip6,
Neale Ranns097fa662018-05-01 05:17:55 -0700107 &rpath->frp_addr.ip6,
108 sizeof (rpath->frp_addr.ip6));
109}
110
Benoît Ganne83e73702021-08-10 16:23:36 +0200111int
Neale Ranns097fa662018-05-01 05:17:55 -0700112fib_api_path_nh_proto_to_dpo (vl_api_fib_path_nh_proto_t pp,
113 dpo_proto_t *dproto)
114{
115 switch (pp)
116 {
117 case FIB_API_PATH_NH_PROTO_IP4:
118 *dproto = DPO_PROTO_IP4;
119 break;
120 case FIB_API_PATH_NH_PROTO_IP6:
121 *dproto = DPO_PROTO_IP6;
122 break;
123 case FIB_API_PATH_NH_PROTO_MPLS:
124 *dproto = DPO_PROTO_MPLS;
125 break;
126 case FIB_API_PATH_NH_PROTO_BIER:
127 *dproto = DPO_PROTO_BIER;
128 break;
129 case FIB_API_PATH_NH_PROTO_ETHERNET:
130 *dproto = DPO_PROTO_ETHERNET;
131 break;
132 default:
133 return (-1);
134 }
135 return (0);
136}
137
138int
139fib_api_path_decode (vl_api_fib_path_t *in,
140 fib_route_path_t *out)
141{
142 vnet_classify_main_t *cm = &vnet_classify_main;
143 int rv = 0, n_labels;
144 vnet_main_t *vnm;
145 u8 ii;
146
147 vnm = vnet_get_main ();
148 clib_memset(&out->frp_dpo, 0, sizeof(out->frp_dpo));
149
150 /* enums are u32 */
151 in->flags = ntohl (in->flags);
152 in->type = ntohl (in->type);
153 in->proto = ntohl (in->proto);
154
155 /*
156 * attributes that apply to all path types
157 */
158 out->frp_flags = 0;
159 out->frp_weight = in->weight;
160 if (0 == out->frp_weight)
161 {
162 out->frp_weight = 1;
163 }
164 out->frp_preference = in->preference;
165
166 rv = fib_api_path_nh_proto_to_dpo(in->proto, &out->frp_proto);
167
168 if (0 != rv)
169 return (rv);
170
171 /*
172 * convert the flags and the AFI to determine the path type
173 */
174 if (in->flags & FIB_API_PATH_FLAG_RESOLVE_VIA_HOST)
175 out->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
176 if (in->flags & FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED)
177 out->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns1dbcf302019-07-19 11:44:53 +0000178 if (in->flags & FIB_API_PATH_FLAG_POP_PW_CW)
179 out->frp_flags |= FIB_ROUTE_PATH_POP_PW_CW;
Neale Ranns097fa662018-05-01 05:17:55 -0700180
181 switch (in->type)
182 {
183 case FIB_API_PATH_TYPE_DVR:
184 out->frp_sw_if_index = ntohl(in->sw_if_index);
185 out->frp_flags |= FIB_ROUTE_PATH_DVR;
186 break;
187 case FIB_API_PATH_TYPE_INTERFACE_RX:
188 out->frp_sw_if_index = ntohl(in->sw_if_index);
189 out->frp_flags |= FIB_ROUTE_PATH_INTF_RX;
190 break;
191 case FIB_API_PATH_TYPE_DROP:
192 out->frp_flags |= FIB_ROUTE_PATH_DROP;
193 break;
194 case FIB_API_PATH_TYPE_LOCAL:
195 out->frp_flags |= FIB_ROUTE_PATH_LOCAL;
196 out->frp_sw_if_index = ntohl(in->sw_if_index);
197 break;
198 case FIB_API_PATH_TYPE_ICMP_UNREACH:
199 out->frp_flags |= FIB_ROUTE_PATH_ICMP_UNREACH;
200 break;
201 case FIB_API_PATH_TYPE_ICMP_PROHIBIT:
202 out->frp_flags |= FIB_ROUTE_PATH_ICMP_PROHIBIT;
203 break;
204 case FIB_API_PATH_TYPE_CLASSIFY:
205 out->frp_flags |= FIB_ROUTE_PATH_CLASSIFY;
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100206
Neale Ranns097fa662018-05-01 05:17:55 -0700207 if (pool_is_free_index (cm->tables, ntohl (in->nh.classify_table_index)))
208 {
209 return VNET_API_ERROR_NO_SUCH_TABLE;
210 }
211 out->frp_classify_table_id = ntohl (in->nh.classify_table_index);
212 break;
213 case FIB_API_PATH_TYPE_UDP_ENCAP:
214 out->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
215 out->frp_udp_encap_id = ntohl(in->nh.obj_id);
216 break;
217 case FIB_API_PATH_TYPE_BIER_IMP:
218 out->frp_flags |= FIB_ROUTE_PATH_BIER_IMP;
219 out->frp_bier_imp = ntohl (in->nh.obj_id);
220 break;
221
222 case FIB_API_PATH_TYPE_SOURCE_LOOKUP:
223 out->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
224 /* fall through */
225 case FIB_API_PATH_TYPE_NORMAL:
226 switch (out->frp_proto)
227 {
228 case DPO_PROTO_IP4:
229 case DPO_PROTO_IP6:
230 fib_api_next_hop_decode(in, &out->frp_addr);
231 out->frp_sw_if_index = ntohl(in->sw_if_index);
232 out->frp_rpf_id = ntohl(in->rpf_id);
233
234 if (0 == out->frp_rpf_id)
235 {
236 /* allow 0 to be an unset value on the API */
237 out->frp_rpf_id = ~0;
238 }
239
240 if (~0 != out->frp_rpf_id)
241 {
242 out->frp_flags |= FIB_ROUTE_PATH_RPF_ID;
243 }
244
245 if (~0 == out->frp_sw_if_index)
246 {
247 /* recursive or deag, validate the next-hop FIB */
248 if (~0 != out->frp_rpf_id)
249 {
250 rv = fib_api_mtable_id_decode(
251 dpo_proto_to_fib(out->frp_proto),
252 ntohl(in->table_id),
253 &out->frp_fib_index);
254 }
255 else
256 {
257 rv = fib_api_table_id_decode(
258 dpo_proto_to_fib(out->frp_proto),
259 ntohl(in->table_id),
260 &out->frp_fib_index);
261 }
262 if (0 != rv)
263 {
264 return (rv);
265 }
266 }
267 else
268 {
269 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
270 out->frp_sw_if_index))
271 {
272 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
273 }
274 }
275
276 if (ip46_address_is_zero(&out->frp_addr))
277 {
278 if (~0 == out->frp_sw_if_index &&
279 ~0 != out->frp_fib_index)
280 {
281 out->frp_flags |= FIB_ROUTE_PATH_DEAG;
282 }
283 }
284
285 break;
286 case DPO_PROTO_MPLS:
287 out->frp_local_label = ntohl (in->nh.via_label);
288 out->frp_eos = MPLS_NON_EOS;
289 out->frp_sw_if_index = ~0;
290 break;
291 case DPO_PROTO_BIER:
292 out->frp_sw_if_index = ntohl(in->sw_if_index);
293 out->frp_rpf_id = ntohl(in->rpf_id);
294
295 if (!(out->frp_flags & FIB_ROUTE_PATH_BIER_IMP))
296 {
Neale Ranns3265ec82021-10-25 10:24:51 +0000297 index_t bdti;
Neale Ranns097fa662018-05-01 05:17:55 -0700298
Neale Ranns3265ec82021-10-25 10:24:51 +0000299 bdti = bier_disp_table_find(ntohl(in->table_id));
300
301 if (INDEX_INVALID != bdti)
Neale Ranns097fa662018-05-01 05:17:55 -0700302 {
Neale Ranns3265ec82021-10-25 10:24:51 +0000303 out->frp_fib_index = bdti;
304 }
305 else
306 {
307 return (VNET_API_ERROR_NO_SUCH_FIB);
Neale Ranns097fa662018-05-01 05:17:55 -0700308 }
309 }
310 break;
311 case DPO_PROTO_ETHERNET:
312 out->frp_sw_if_index = ntohl(in->sw_if_index);
313 break;
314 case DPO_PROTO_NSH:
315 break;
316 }
Neale Ranns81458422018-03-12 06:59:36 -0700317 }
318
319 n_labels = in->n_labels;
Neale Ranns097fa662018-05-01 05:17:55 -0700320 if (n_labels != 0)
Neale Ranns81458422018-03-12 06:59:36 -0700321 {
322 vec_validate (out->frp_label_stack, n_labels - 1);
323 for (ii = 0; ii < n_labels; ii++)
324 {
325 out->frp_label_stack[ii].fml_value =
326 ntohl(in->label_stack[ii].label);
327 out->frp_label_stack[ii].fml_ttl =
328 in->label_stack[ii].ttl;
329 out->frp_label_stack[ii].fml_exp =
330 in->label_stack[ii].exp;
331 out->frp_label_stack[ii].fml_mode =
332 (in->label_stack[ii].is_uniform ?
333 FIB_MPLS_LSP_MODE_UNIFORM :
334 FIB_MPLS_LSP_MODE_PIPE);
335 }
336 }
337
Neale Ranns097fa662018-05-01 05:17:55 -0700338 return (0);
Neale Ranns81458422018-03-12 06:59:36 -0700339}
340
341void
Neale Ranns097fa662018-05-01 05:17:55 -0700342fib_api_path_encode (const fib_route_path_t * rpath,
Neale Ranns81458422018-03-12 06:59:36 -0700343 vl_api_fib_path_t *out)
344{
Neale Ranns097fa662018-05-01 05:17:55 -0700345 memset (out, 0, sizeof (*out));
Neale Ranns775f73c2018-12-20 03:01:49 -0800346
Neale Ranns097fa662018-05-01 05:17:55 -0700347 out->weight = rpath->frp_weight;
348 out->preference = rpath->frp_preference;
349 out->sw_if_index = htonl (rpath->frp_sw_if_index);
350 out->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
351 out->rpf_id = rpath->frp_rpf_id;
352 fib_api_next_hop_encode (rpath, out);
353
354 if (0 != rpath->frp_fib_index)
Neale Ranns81458422018-03-12 06:59:36 -0700355 {
Neale Ranns097fa662018-05-01 05:17:55 -0700356 if ((DPO_PROTO_IP6 == rpath->frp_proto) ||
357 (DPO_PROTO_IP4 == rpath->frp_proto))
Neale Ranns81458422018-03-12 06:59:36 -0700358 {
Neale Ranns097fa662018-05-01 05:17:55 -0700359 if (rpath->frp_flags & FIB_ROUTE_PATH_RPF_ID)
360 {
361 out->table_id = htonl (mfib_table_get_table_id(
362 rpath->frp_fib_index,
363 dpo_proto_to_fib(rpath->frp_proto)));
364 }
365 else
366 {
367 out->table_id = htonl (fib_table_get_table_id(
368 rpath->frp_fib_index,
369 dpo_proto_to_fib(rpath->frp_proto)));
370 }
Neale Ranns81458422018-03-12 06:59:36 -0700371 }
Neale Ranns097fa662018-05-01 05:17:55 -0700372 }
373
374 if (rpath->frp_flags & FIB_ROUTE_PATH_DVR)
375 {
376 out->type = FIB_API_PATH_TYPE_DVR;
377 }
378 else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_UNREACH)
379 {
380 out->type = FIB_API_PATH_TYPE_ICMP_UNREACH;
381 }
382 else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_PROHIBIT)
383 {
384 out->type = FIB_API_PATH_TYPE_ICMP_PROHIBIT;
385 }
386 else if (rpath->frp_flags & FIB_ROUTE_PATH_LOCAL)
387 {
388 out->type = FIB_API_PATH_TYPE_LOCAL;
389 }
390 else if (rpath->frp_flags & FIB_ROUTE_PATH_DROP)
391 {
392 out->type = FIB_API_PATH_TYPE_DROP;
393 }
394 else if (rpath->frp_flags & FIB_ROUTE_PATH_UDP_ENCAP)
395 {
396 out->type = FIB_API_PATH_TYPE_UDP_ENCAP;
397 out->nh.obj_id = rpath->frp_udp_encap_id;
398 }
399 else if (rpath->frp_flags & FIB_ROUTE_PATH_BIER_IMP)
400 {
401 out->type = FIB_API_PATH_TYPE_BIER_IMP;
402 out->nh.obj_id = rpath->frp_bier_imp;
403 }
IJsbrand Wijnands79437c82020-03-05 06:25:32 -0800404 else if (rpath->frp_flags & FIB_ROUTE_PATH_INTF_RX)
405 {
406 out->type = FIB_API_PATH_TYPE_INTERFACE_RX;
407 }
Neale Ranns097fa662018-05-01 05:17:55 -0700408 else
409 {
410 out->type = FIB_API_PATH_TYPE_NORMAL;
411 }
412 if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_HOST)
413 {
414 out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_HOST;
415 }
416 if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED)
417 {
418 out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED;
419 }
420
421 out->flags = htonl (out->flags);
422 out->type = htonl (out->type);
423 out->proto = htonl (out->proto);
424
425 if (rpath->frp_label_stack)
426 {
427 int ii;
428
429 for (ii = 0; ii < vec_len(rpath->frp_label_stack); ii++)
430 {
431 out->label_stack[ii].label =
432 htonl(rpath->frp_label_stack[ii].fml_value);
433 out->label_stack[ii].ttl =
434 rpath->frp_label_stack[ii].fml_ttl;
435 out->label_stack[ii].exp =
436 rpath->frp_label_stack[ii].fml_exp;
437 }
438 out->n_labels = ii;
439 }
440}
441
Neale Rannsc2ac2352019-07-02 14:33:29 +0000442int
Neale Ranns097fa662018-05-01 05:17:55 -0700443fib_api_route_add_del (u8 is_add,
444 u8 is_multipath,
445 u32 fib_index,
446 const fib_prefix_t * prefix,
Neale Ranns976b2592019-12-04 06:11:00 +0000447 fib_source_t src,
Neale Ranns097fa662018-05-01 05:17:55 -0700448 fib_entry_flag_t entry_flags,
449 fib_route_path_t *rpaths)
450{
Neale Rannsc2ac2352019-07-02 14:33:29 +0000451 if (is_multipath)
Neale Ranns097fa662018-05-01 05:17:55 -0700452 {
Neale Rannsc2ac2352019-07-02 14:33:29 +0000453 if (vec_len(rpaths) == 0)
454 return (VNET_API_ERROR_NO_PATHS_IN_ROUTE);
455
456 /* Iterative path add/remove */
457 if (is_add)
458 fib_table_entry_path_add2 (fib_index,
459 prefix,
Neale Ranns976b2592019-12-04 06:11:00 +0000460 src,
Neale Rannsc2ac2352019-07-02 14:33:29 +0000461 entry_flags,
462 rpaths);
463 else
464 fib_table_entry_path_remove2 (fib_index,
465 prefix,
Neale Ranns976b2592019-12-04 06:11:00 +0000466 src,
Neale Rannsc2ac2352019-07-02 14:33:29 +0000467 rpaths);
Neale Ranns097fa662018-05-01 05:17:55 -0700468 }
Neale Rannsc2ac2352019-07-02 14:33:29 +0000469 else
Neale Ranns097fa662018-05-01 05:17:55 -0700470 {
Neale Rannsc2ac2352019-07-02 14:33:29 +0000471 if (is_add)
472 {
473 if (vec_len(rpaths) == 0)
474 return (VNET_API_ERROR_NO_PATHS_IN_ROUTE);
475
476 /* path replacement */
477 fib_table_entry_update (fib_index,
478 prefix,
Neale Ranns976b2592019-12-04 06:11:00 +0000479 src,
Neale Rannsc2ac2352019-07-02 14:33:29 +0000480 entry_flags,
481 rpaths);
482 }
483 else
484 /* entry delete */
485 fib_table_entry_delete (fib_index,
486 prefix,
Neale Ranns976b2592019-12-04 06:11:00 +0000487 src);
Neale Ranns097fa662018-05-01 05:17:55 -0700488 }
Neale Rannsc2ac2352019-07-02 14:33:29 +0000489
490 return (0);
Neale Ranns097fa662018-05-01 05:17:55 -0700491}
492
493u8*
494format_vl_api_fib_path (u8 * s, va_list * args)
495{
496 const vl_api_fib_path_t *path = va_arg (*args, vl_api_fib_path_t*);
497
498 s = format (s, "sw_if_index %d", ntohl (path->sw_if_index));
499 switch (clib_net_to_host_u32(path->proto))
500 {
501 case FIB_API_PATH_NH_PROTO_IP4:
502 s = format (s, " %U", format_vl_api_address_union,
503 &path->nh.address, ADDRESS_IP4);
504 break;
505 case FIB_API_PATH_NH_PROTO_IP6:
506 s = format (s, " %U", format_vl_api_address_union,
507 &path->nh.address, ADDRESS_IP6);
Neale Ranns81458422018-03-12 06:59:36 -0700508 break;
509 default:
510 break;
511 }
Neale Ranns097fa662018-05-01 05:17:55 -0700512 s = format (s, " weight %d", path->weight);
513 s = format (s, " preference %d", path->preference);
514 s = format (s, " type %d", ntohl(path->type));
515 s = format (s, " proto %d", ntohl(path->proto));
516 s = format (s, " flags %d", ntohl(path->flags));
517 s = format (s, " n_labels %d", ntohl(path->n_labels));
518 s = format (s, " table-id %d", ntohl(path->table_id));
519 s = format (s, " rpf-id %d", ntohl(path->rpf_id));
Neale Ranns81458422018-03-12 06:59:36 -0700520
Neale Ranns097fa662018-05-01 05:17:55 -0700521 return (s);
Neale Ranns81458422018-03-12 06:59:36 -0700522}
Neale Ranns2303cb12018-02-21 04:57:17 -0800523
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100524int
525fib_proto_from_api_address_family (vl_api_address_family_t af, fib_protocol_t * out)
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700526{
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100527 switch (af)
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700528 {
529 case ADDRESS_IP4:
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100530 *out = (FIB_PROTOCOL_IP4);
531 return (0);
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700532 case ADDRESS_IP6:
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100533 *out = (FIB_PROTOCOL_IP6);
534 return (0);
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700535 }
536
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100537 return (VNET_API_ERROR_INVALID_ADDRESS_FAMILY);
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700538}
539
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100540vl_api_address_family_t
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700541fib_proto_to_api_address_family (fib_protocol_t fproto)
542{
543 switch (fproto)
544 {
545 case FIB_PROTOCOL_IP4:
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100546 return (ADDRESS_IP4);
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700547 case FIB_PROTOCOL_IP6:
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100548 return (ADDRESS_IP6);
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700549 default:
550 break;
551 }
552
553 ASSERT(0);
Jakub Grajciar7dd63e52020-03-19 08:03:55 +0100554 return (ADDRESS_IP4);
Neale Rannsd1e68ab2018-10-01 01:42:13 -0700555}
Neale Ranns976b2592019-12-04 06:11:00 +0000556
557void
558vl_api_fib_source_add_t_handler (vl_api_fib_source_add_t * mp)
559{
560 vl_api_fib_source_add_reply_t *rmp;
561 fib_source_t src;
562 int rv = 0;
563 u8 *name;
564
565 name = format (0, "%s", mp->src.name);
566 vec_add1 (name, 0);
567
568 src = fib_source_allocate((const char *)name,
569 mp->src.priority,
570 FIB_SOURCE_BH_API);
571
572 vec_free(name);
573
574 REPLY_MACRO2 (VL_API_FIB_SOURCE_ADD_REPLY,
575 ({
576 rmp->id = src;
577 }));
578}
579
580typedef struct fib_source_dump_ctx_t_
581{
582 vl_api_registration_t * reg;
583 u32 context;
584} fib_source_dump_ctx_t;
585
586static walk_rc_t
587send_fib_source (fib_source_t id,
588 const char *name,
589 fib_source_priority_t prio,
590 fib_source_behaviour_t bh,
591 void *data)
592{
593 vl_api_fib_source_details_t *mp;
594 fib_source_dump_ctx_t *ctx;
595
596 ctx = data;
597 mp = vl_msg_api_alloc_zero (sizeof (*mp));
598 if (!mp)
599 return WALK_STOP;
600
601 mp->_vl_msg_id = ntohs (VL_API_FIB_SOURCE_DETAILS + REPLY_MSG_ID_BASE);
602 mp->context = ctx->context;
603
604 mp->src.priority = prio;
605 mp->src.id = id;
606 clib_memcpy(mp->src.name, name,
607 clib_min(strlen(name), ARRAY_LEN(mp->src.name)));
608
609 vl_api_send_msg (ctx->reg, (u8 *) mp);
610
611 return (WALK_CONTINUE);
612}
613
614void
615vl_api_fib_source_dump_t_handler (vl_api_fib_source_dump_t * mp)
616{
617 vl_api_registration_t *reg;
618
619 reg = vl_api_client_index_to_registration (mp->client_index);
620 if (!reg)
621 return;
622
623 fib_source_dump_ctx_t ctx = {
624 .reg = reg,
625 .context = mp->context,
626 };
627
628 fib_source_walk(send_fib_source, &ctx);
629}
630
631
632#include <vnet/fib/fib.api.c>
633
634static clib_error_t *
635fib_api_hookup (vlib_main_t * vm)
636{
637 /*
638 * Set up the (msg_name, crc, message-id) table
639 */
640 fib_base_msg_id = setup_message_id_table ();
641
642 return (NULL);
643}
644
645VLIB_API_INIT_FUNCTION (fib_api_hookup);