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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/vnet.h>
17 #include <vlibmemory/api.h>
18 #include <vnet/fib/fib_api.h>
19 #include <vnet/ip/ip_types_api.h>
20 #include <vnet/fib/fib_table.h>
21 #include <vnet/mfib/mfib_table.h>
22 #include <vnet/bier/bier_disp_table.h>
23 #include <vpp/api/types.h>
24 #include <vnet/classify/vnet_classify.h>
26 #include <vnet/vnet_msg_enum.h>
28 #define vl_typedefs /* define message structures */
29 #include <vnet/vnet_all_api_h.h>
32 #define vl_endianfun /* define message structures */
33 #include <vnet/vnet_all_api_h.h>
36 /* instantiate all the print functions we know about */
37 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
39 #include <vnet/vnet_all_api_h.h>
42 #include <vlibapi/api_helper_macros.h>
45 fib_api_table_id_decode (fib_protocol_t fproto,
49 *fib_index = fib_table_find(fproto, table_id);
51 if (INDEX_INVALID == *fib_index)
53 return VNET_API_ERROR_NO_SUCH_FIB;
60 fib_api_mtable_id_decode (fib_protocol_t fproto,
64 *fib_index = mfib_table_find(fproto, table_id);
68 return VNET_API_ERROR_NO_SUCH_FIB;
75 fib_api_next_hop_decode (const vl_api_fib_path_t *in,
78 if (in->proto == FIB_API_PATH_NH_PROTO_IP4)
79 clib_memcpy (&out->ip4, &in->nh.address.ip4, sizeof (out->ip4));
80 else if (in->proto == FIB_API_PATH_NH_PROTO_IP6)
81 clib_memcpy (&out->ip6, &in->nh.address.ip6, sizeof (out->ip6));
84 static vl_api_fib_path_nh_proto_t
85 fib_api_path_dpo_proto_to_nh (dpo_proto_t dproto)
90 return (FIB_API_PATH_NH_PROTO_IP4);
92 return (FIB_API_PATH_NH_PROTO_IP6);
94 return (FIB_API_PATH_NH_PROTO_MPLS);
96 return (FIB_API_PATH_NH_PROTO_BIER);
97 case DPO_PROTO_ETHERNET:
98 return (FIB_API_PATH_NH_PROTO_ETHERNET);
103 return (FIB_API_PATH_NH_PROTO_IP4);
108 fib_api_next_hop_encode (const fib_route_path_t *rpath,
109 vl_api_fib_path_t *fp)
111 fp->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
113 if (rpath->frp_proto == DPO_PROTO_IP4)
114 clib_memcpy (&fp->nh.address.ip4,
115 &rpath->frp_addr.ip4,
116 sizeof (rpath->frp_addr.ip4));
117 else if (rpath->frp_proto == DPO_PROTO_IP6)
118 clib_memcpy (&fp->nh.address.ip6,
119 &rpath->frp_addr.ip6,
120 sizeof (rpath->frp_addr.ip6));
124 fib_api_path_nh_proto_to_dpo (vl_api_fib_path_nh_proto_t pp,
129 case FIB_API_PATH_NH_PROTO_IP4:
130 *dproto = DPO_PROTO_IP4;
132 case FIB_API_PATH_NH_PROTO_IP6:
133 *dproto = DPO_PROTO_IP6;
135 case FIB_API_PATH_NH_PROTO_MPLS:
136 *dproto = DPO_PROTO_MPLS;
138 case FIB_API_PATH_NH_PROTO_BIER:
139 *dproto = DPO_PROTO_BIER;
141 case FIB_API_PATH_NH_PROTO_ETHERNET:
142 *dproto = DPO_PROTO_ETHERNET;
151 fib_api_path_decode (vl_api_fib_path_t *in,
152 fib_route_path_t *out)
154 vnet_classify_main_t *cm = &vnet_classify_main;
155 int rv = 0, n_labels;
159 vnm = vnet_get_main ();
160 clib_memset(&out->frp_dpo, 0, sizeof(out->frp_dpo));
163 in->flags = ntohl (in->flags);
164 in->type = ntohl (in->type);
165 in->proto = ntohl (in->proto);
168 * attributes that apply to all path types
171 out->frp_weight = in->weight;
172 if (0 == out->frp_weight)
176 out->frp_preference = in->preference;
178 rv = fib_api_path_nh_proto_to_dpo(in->proto, &out->frp_proto);
184 * convert the flags and the AFI to determine the path type
186 if (in->flags & FIB_API_PATH_FLAG_RESOLVE_VIA_HOST)
187 out->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
188 if (in->flags & FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED)
189 out->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
190 if (in->flags & FIB_API_PATH_FLAG_POP_PW_CW)
191 out->frp_flags |= FIB_ROUTE_PATH_POP_PW_CW;
195 case FIB_API_PATH_TYPE_DVR:
196 out->frp_sw_if_index = ntohl(in->sw_if_index);
197 out->frp_flags |= FIB_ROUTE_PATH_DVR;
199 case FIB_API_PATH_TYPE_INTERFACE_RX:
200 out->frp_sw_if_index = ntohl(in->sw_if_index);
201 out->frp_flags |= FIB_ROUTE_PATH_INTF_RX;
203 case FIB_API_PATH_TYPE_DROP:
204 out->frp_flags |= FIB_ROUTE_PATH_DROP;
206 case FIB_API_PATH_TYPE_LOCAL:
207 out->frp_flags |= FIB_ROUTE_PATH_LOCAL;
208 out->frp_sw_if_index = ntohl(in->sw_if_index);
210 case FIB_API_PATH_TYPE_ICMP_UNREACH:
211 out->frp_flags |= FIB_ROUTE_PATH_ICMP_UNREACH;
213 case FIB_API_PATH_TYPE_ICMP_PROHIBIT:
214 out->frp_flags |= FIB_ROUTE_PATH_ICMP_PROHIBIT;
216 case FIB_API_PATH_TYPE_CLASSIFY:
217 out->frp_flags |= FIB_ROUTE_PATH_CLASSIFY;
219 if (pool_is_free_index (cm->tables, ntohl (in->nh.classify_table_index)))
221 return VNET_API_ERROR_NO_SUCH_TABLE;
223 out->frp_classify_table_id = ntohl (in->nh.classify_table_index);
225 case FIB_API_PATH_TYPE_UDP_ENCAP:
226 out->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
227 out->frp_udp_encap_id = ntohl(in->nh.obj_id);
229 case FIB_API_PATH_TYPE_BIER_IMP:
230 out->frp_flags |= FIB_ROUTE_PATH_BIER_IMP;
231 out->frp_bier_imp = ntohl (in->nh.obj_id);
234 case FIB_API_PATH_TYPE_SOURCE_LOOKUP:
235 out->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
237 case FIB_API_PATH_TYPE_NORMAL:
238 switch (out->frp_proto)
242 fib_api_next_hop_decode(in, &out->frp_addr);
243 out->frp_sw_if_index = ntohl(in->sw_if_index);
244 out->frp_rpf_id = ntohl(in->rpf_id);
246 if (0 == out->frp_rpf_id)
248 /* allow 0 to be an unset value on the API */
249 out->frp_rpf_id = ~0;
252 if (~0 != out->frp_rpf_id)
254 out->frp_flags |= FIB_ROUTE_PATH_RPF_ID;
257 if (~0 == out->frp_sw_if_index)
259 /* recursive or deag, validate the next-hop FIB */
260 if (~0 != out->frp_rpf_id)
262 rv = fib_api_mtable_id_decode(
263 dpo_proto_to_fib(out->frp_proto),
265 &out->frp_fib_index);
269 rv = fib_api_table_id_decode(
270 dpo_proto_to_fib(out->frp_proto),
272 &out->frp_fib_index);
281 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
282 out->frp_sw_if_index))
284 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
288 if (ip46_address_is_zero(&out->frp_addr))
290 if (~0 == out->frp_sw_if_index &&
291 ~0 != out->frp_fib_index)
293 out->frp_flags |= FIB_ROUTE_PATH_DEAG;
299 out->frp_local_label = ntohl (in->nh.via_label);
300 out->frp_eos = MPLS_NON_EOS;
301 out->frp_sw_if_index = ~0;
304 out->frp_sw_if_index = ntohl(in->sw_if_index);
305 out->frp_rpf_id = ntohl(in->rpf_id);
307 if (!(out->frp_flags & FIB_ROUTE_PATH_BIER_IMP))
309 fib_api_next_hop_decode(in, &out->frp_addr);
311 if (ip46_address_is_zero(&out->frp_addr))
315 bdti = bier_disp_table_find(ntohl(in->table_id));
317 if (INDEX_INVALID != bdti)
319 out->frp_fib_index = bdti;
323 return (VNET_API_ERROR_NO_SUCH_FIB);
328 case DPO_PROTO_ETHERNET:
329 out->frp_sw_if_index = ntohl(in->sw_if_index);
336 n_labels = in->n_labels;
339 vec_validate (out->frp_label_stack, n_labels - 1);
340 for (ii = 0; ii < n_labels; ii++)
342 out->frp_label_stack[ii].fml_value =
343 ntohl(in->label_stack[ii].label);
344 out->frp_label_stack[ii].fml_ttl =
345 in->label_stack[ii].ttl;
346 out->frp_label_stack[ii].fml_exp =
347 in->label_stack[ii].exp;
348 out->frp_label_stack[ii].fml_mode =
349 (in->label_stack[ii].is_uniform ?
350 FIB_MPLS_LSP_MODE_UNIFORM :
351 FIB_MPLS_LSP_MODE_PIPE);
359 fib_api_path_encode (const fib_route_path_t * rpath,
360 vl_api_fib_path_t *out)
362 memset (out, 0, sizeof (*out));
364 out->weight = rpath->frp_weight;
365 out->preference = rpath->frp_preference;
366 out->sw_if_index = htonl (rpath->frp_sw_if_index);
367 out->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
368 out->rpf_id = rpath->frp_rpf_id;
369 fib_api_next_hop_encode (rpath, out);
371 if (0 != rpath->frp_fib_index)
373 if ((DPO_PROTO_IP6 == rpath->frp_proto) ||
374 (DPO_PROTO_IP4 == rpath->frp_proto))
376 if (rpath->frp_flags & FIB_ROUTE_PATH_RPF_ID)
378 out->table_id = htonl (mfib_table_get_table_id(
379 rpath->frp_fib_index,
380 dpo_proto_to_fib(rpath->frp_proto)));
384 out->table_id = htonl (fib_table_get_table_id(
385 rpath->frp_fib_index,
386 dpo_proto_to_fib(rpath->frp_proto)));
391 if (rpath->frp_flags & FIB_ROUTE_PATH_DVR)
393 out->type = FIB_API_PATH_TYPE_DVR;
395 else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_UNREACH)
397 out->type = FIB_API_PATH_TYPE_ICMP_UNREACH;
399 else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_PROHIBIT)
401 out->type = FIB_API_PATH_TYPE_ICMP_PROHIBIT;
403 else if (rpath->frp_flags & FIB_ROUTE_PATH_LOCAL)
405 out->type = FIB_API_PATH_TYPE_LOCAL;
407 else if (rpath->frp_flags & FIB_ROUTE_PATH_DROP)
409 out->type = FIB_API_PATH_TYPE_DROP;
411 else if (rpath->frp_flags & FIB_ROUTE_PATH_UDP_ENCAP)
413 out->type = FIB_API_PATH_TYPE_UDP_ENCAP;
414 out->nh.obj_id = rpath->frp_udp_encap_id;
416 else if (rpath->frp_flags & FIB_ROUTE_PATH_BIER_IMP)
418 out->type = FIB_API_PATH_TYPE_BIER_IMP;
419 out->nh.obj_id = rpath->frp_bier_imp;
423 out->type = FIB_API_PATH_TYPE_NORMAL;
425 if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_HOST)
427 out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_HOST;
429 if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED)
431 out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED;
434 out->flags = htonl (out->flags);
435 out->type = htonl (out->type);
436 out->proto = htonl (out->proto);
438 if (rpath->frp_label_stack)
442 for (ii = 0; ii < vec_len(rpath->frp_label_stack); ii++)
444 out->label_stack[ii].label =
445 htonl(rpath->frp_label_stack[ii].fml_value);
446 out->label_stack[ii].ttl =
447 rpath->frp_label_stack[ii].fml_ttl;
448 out->label_stack[ii].exp =
449 rpath->frp_label_stack[ii].fml_exp;
456 fib_api_route_add_del (u8 is_add,
459 const fib_prefix_t * prefix,
460 fib_entry_flag_t entry_flags,
461 fib_route_path_t *rpaths)
465 if (vec_len(rpaths) == 0)
466 return (VNET_API_ERROR_NO_PATHS_IN_ROUTE);
468 /* Iterative path add/remove */
470 fib_table_entry_path_add2 (fib_index,
476 fib_table_entry_path_remove2 (fib_index,
485 if (vec_len(rpaths) == 0)
486 return (VNET_API_ERROR_NO_PATHS_IN_ROUTE);
488 /* path replacement */
489 fib_table_entry_update (fib_index,
497 fib_table_entry_delete (fib_index,
506 format_vl_api_fib_path (u8 * s, va_list * args)
508 const vl_api_fib_path_t *path = va_arg (*args, vl_api_fib_path_t*);
510 s = format (s, "sw_if_index %d", ntohl (path->sw_if_index));
511 switch (clib_net_to_host_u32(path->proto))
513 case FIB_API_PATH_NH_PROTO_IP4:
514 s = format (s, " %U", format_vl_api_address_union,
515 &path->nh.address, ADDRESS_IP4);
517 case FIB_API_PATH_NH_PROTO_IP6:
518 s = format (s, " %U", format_vl_api_address_union,
519 &path->nh.address, ADDRESS_IP6);
524 s = format (s, " weight %d", path->weight);
525 s = format (s, " preference %d", path->preference);
526 s = format (s, " type %d", ntohl(path->type));
527 s = format (s, " proto %d", ntohl(path->proto));
528 s = format (s, " flags %d", ntohl(path->flags));
529 s = format (s, " n_labels %d", ntohl(path->n_labels));
530 s = format (s, " table-id %d", ntohl(path->table_id));
531 s = format (s, " rpf-id %d", ntohl(path->rpf_id));
537 fib_proto_from_api_address_family (int af)
539 switch (clib_net_to_host_u32 (af))
542 return (FIB_PROTOCOL_IP4);
544 return (FIB_PROTOCOL_IP6);
548 return (FIB_PROTOCOL_IP4);
552 fib_proto_to_api_address_family (fib_protocol_t fproto)
556 case FIB_PROTOCOL_IP4:
557 return (clib_net_to_host_u32 (ADDRESS_IP4));
558 case FIB_PROTOCOL_IP6:
559 return (clib_net_to_host_u32 (ADDRESS_IP6));
565 return (clib_net_to_host_u32 (ADDRESS_IP4));