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>
25 #include <vnet/vnet_msg_enum.h>
27 #define vl_typedefs /* define message structures */
28 #include <vnet/vnet_all_api_h.h>
31 #define vl_endianfun /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
35 /* instantiate all the print functions we know about */
36 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
38 #include <vnet/vnet_all_api_h.h>
41 #include <vlibapi/api_helper_macros.h>
44 fib_api_table_id_decode (fib_protocol_t fproto,
48 *fib_index = fib_table_find(fproto, table_id);
50 if (INDEX_INVALID == *fib_index)
52 return VNET_API_ERROR_NO_SUCH_FIB;
59 fib_api_mtable_id_decode (fib_protocol_t fproto,
63 *fib_index = mfib_table_find(fproto, table_id);
67 return VNET_API_ERROR_NO_SUCH_FIB;
74 fib_api_next_hop_decode (const vl_api_fib_path_t *in,
77 if (in->proto == FIB_API_PATH_NH_PROTO_IP4)
78 memcpy (&out->ip4, &in->nh.address.ip4, sizeof (out->ip4));
79 else if (in->proto == FIB_API_PATH_NH_PROTO_IP6)
80 memcpy (&out->ip6, &in->nh.address.ip6, sizeof (out->ip6));
83 static vl_api_fib_path_nh_proto_t
84 fib_api_path_dpo_proto_to_nh (dpo_proto_t dproto)
89 return (FIB_API_PATH_NH_PROTO_IP4);
91 return (FIB_API_PATH_NH_PROTO_IP6);
93 return (FIB_API_PATH_NH_PROTO_MPLS);
95 return (FIB_API_PATH_NH_PROTO_BIER);
96 case DPO_PROTO_ETHERNET:
97 return (FIB_API_PATH_NH_PROTO_ETHERNET);
102 return (FIB_API_PATH_NH_PROTO_IP4);
107 fib_api_next_hop_encode (const fib_route_path_t *rpath,
108 vl_api_fib_path_t *fp)
110 fp->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
112 if (rpath->frp_proto == DPO_PROTO_IP4)
113 memcpy (&fp->nh.address.ip4,
114 &rpath->frp_addr.ip4,
115 sizeof (rpath->frp_addr.ip4));
116 else if (rpath->frp_proto == DPO_PROTO_IP6)
117 memcpy (&fp->nh.address.ip6,
118 &rpath->frp_addr.ip6,
119 sizeof (rpath->frp_addr.ip6));
123 fib_api_path_nh_proto_to_dpo (vl_api_fib_path_nh_proto_t pp,
128 case FIB_API_PATH_NH_PROTO_IP4:
129 *dproto = DPO_PROTO_IP4;
131 case FIB_API_PATH_NH_PROTO_IP6:
132 *dproto = DPO_PROTO_IP6;
134 case FIB_API_PATH_NH_PROTO_MPLS:
135 *dproto = DPO_PROTO_MPLS;
137 case FIB_API_PATH_NH_PROTO_BIER:
138 *dproto = DPO_PROTO_BIER;
140 case FIB_API_PATH_NH_PROTO_ETHERNET:
141 *dproto = DPO_PROTO_ETHERNET;
150 fib_api_path_decode (vl_api_fib_path_t *in,
151 fib_route_path_t *out)
153 vnet_classify_main_t *cm = &vnet_classify_main;
154 int rv = 0, n_labels;
158 vnm = vnet_get_main ();
159 clib_memset(&out->frp_dpo, 0, sizeof(out->frp_dpo));
162 in->flags = ntohl (in->flags);
163 in->type = ntohl (in->type);
164 in->proto = ntohl (in->proto);
167 * attributes that apply to all path types
170 out->frp_weight = in->weight;
171 if (0 == out->frp_weight)
175 out->frp_preference = in->preference;
177 rv = fib_api_path_nh_proto_to_dpo(in->proto, &out->frp_proto);
183 * convert the flags and the AFI to determine the path type
185 if (in->flags & FIB_API_PATH_FLAG_RESOLVE_VIA_HOST)
186 out->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
187 if (in->flags & FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED)
188 out->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
189 if (in->flags & FIB_API_PATH_FLAG_POP_PW_CW)
190 out->frp_flags |= FIB_ROUTE_PATH_POP_PW_CW;
194 case FIB_API_PATH_TYPE_DVR:
195 out->frp_sw_if_index = ntohl(in->sw_if_index);
196 out->frp_flags |= FIB_ROUTE_PATH_DVR;
198 case FIB_API_PATH_TYPE_INTERFACE_RX:
199 out->frp_sw_if_index = ntohl(in->sw_if_index);
200 out->frp_flags |= FIB_ROUTE_PATH_INTF_RX;
202 case FIB_API_PATH_TYPE_DROP:
203 out->frp_flags |= FIB_ROUTE_PATH_DROP;
205 case FIB_API_PATH_TYPE_LOCAL:
206 out->frp_flags |= FIB_ROUTE_PATH_LOCAL;
207 out->frp_sw_if_index = ntohl(in->sw_if_index);
209 case FIB_API_PATH_TYPE_ICMP_UNREACH:
210 out->frp_flags |= FIB_ROUTE_PATH_ICMP_UNREACH;
212 case FIB_API_PATH_TYPE_ICMP_PROHIBIT:
213 out->frp_flags |= FIB_ROUTE_PATH_ICMP_PROHIBIT;
215 case FIB_API_PATH_TYPE_CLASSIFY:
216 out->frp_flags |= FIB_ROUTE_PATH_CLASSIFY;
218 if (pool_is_free_index (cm->tables, ntohl (in->nh.classify_table_index)))
220 return VNET_API_ERROR_NO_SUCH_TABLE;
222 out->frp_classify_table_id = ntohl (in->nh.classify_table_index);
224 case FIB_API_PATH_TYPE_UDP_ENCAP:
225 out->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
226 out->frp_udp_encap_id = ntohl(in->nh.obj_id);
228 case FIB_API_PATH_TYPE_BIER_IMP:
229 out->frp_flags |= FIB_ROUTE_PATH_BIER_IMP;
230 out->frp_bier_imp = ntohl (in->nh.obj_id);
233 case FIB_API_PATH_TYPE_SOURCE_LOOKUP:
234 out->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
236 case FIB_API_PATH_TYPE_NORMAL:
237 switch (out->frp_proto)
241 fib_api_next_hop_decode(in, &out->frp_addr);
242 out->frp_sw_if_index = ntohl(in->sw_if_index);
243 out->frp_rpf_id = ntohl(in->rpf_id);
245 if (0 == out->frp_rpf_id)
247 /* allow 0 to be an unset value on the API */
248 out->frp_rpf_id = ~0;
251 if (~0 != out->frp_rpf_id)
253 out->frp_flags |= FIB_ROUTE_PATH_RPF_ID;
256 if (~0 == out->frp_sw_if_index)
258 /* recursive or deag, validate the next-hop FIB */
259 if (~0 != out->frp_rpf_id)
261 rv = fib_api_mtable_id_decode(
262 dpo_proto_to_fib(out->frp_proto),
264 &out->frp_fib_index);
268 rv = fib_api_table_id_decode(
269 dpo_proto_to_fib(out->frp_proto),
271 &out->frp_fib_index);
280 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
281 out->frp_sw_if_index))
283 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
287 if (ip46_address_is_zero(&out->frp_addr))
289 if (~0 == out->frp_sw_if_index &&
290 ~0 != out->frp_fib_index)
292 out->frp_flags |= FIB_ROUTE_PATH_DEAG;
298 out->frp_local_label = ntohl (in->nh.via_label);
299 out->frp_eos = MPLS_NON_EOS;
300 out->frp_sw_if_index = ~0;
303 out->frp_sw_if_index = ntohl(in->sw_if_index);
304 out->frp_rpf_id = ntohl(in->rpf_id);
306 if (!(out->frp_flags & FIB_ROUTE_PATH_BIER_IMP))
308 fib_api_next_hop_decode(in, &out->frp_addr);
310 if (ip46_address_is_zero(&out->frp_addr))
314 bdti = bier_disp_table_find(ntohl(in->table_id));
316 if (INDEX_INVALID != bdti)
318 out->frp_fib_index = bdti;
322 return (VNET_API_ERROR_NO_SUCH_FIB);
327 case DPO_PROTO_ETHERNET:
328 out->frp_sw_if_index = ntohl(in->sw_if_index);
335 n_labels = in->n_labels;
338 vec_validate (out->frp_label_stack, n_labels - 1);
339 for (ii = 0; ii < n_labels; ii++)
341 out->frp_label_stack[ii].fml_value =
342 ntohl(in->label_stack[ii].label);
343 out->frp_label_stack[ii].fml_ttl =
344 in->label_stack[ii].ttl;
345 out->frp_label_stack[ii].fml_exp =
346 in->label_stack[ii].exp;
347 out->frp_label_stack[ii].fml_mode =
348 (in->label_stack[ii].is_uniform ?
349 FIB_MPLS_LSP_MODE_UNIFORM :
350 FIB_MPLS_LSP_MODE_PIPE);
358 fib_api_path_encode (const fib_route_path_t * rpath,
359 vl_api_fib_path_t *out)
361 memset (out, 0, sizeof (*out));
363 out->weight = rpath->frp_weight;
364 out->preference = rpath->frp_preference;
365 out->sw_if_index = htonl (rpath->frp_sw_if_index);
366 out->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
367 out->rpf_id = rpath->frp_rpf_id;
368 fib_api_next_hop_encode (rpath, out);
370 if (0 != rpath->frp_fib_index)
372 if ((DPO_PROTO_IP6 == rpath->frp_proto) ||
373 (DPO_PROTO_IP4 == rpath->frp_proto))
375 if (rpath->frp_flags & FIB_ROUTE_PATH_RPF_ID)
377 out->table_id = htonl (mfib_table_get_table_id(
378 rpath->frp_fib_index,
379 dpo_proto_to_fib(rpath->frp_proto)));
383 out->table_id = htonl (fib_table_get_table_id(
384 rpath->frp_fib_index,
385 dpo_proto_to_fib(rpath->frp_proto)));
390 if (rpath->frp_flags & FIB_ROUTE_PATH_DVR)
392 out->type = FIB_API_PATH_TYPE_DVR;
394 else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_UNREACH)
396 out->type = FIB_API_PATH_TYPE_ICMP_UNREACH;
398 else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_PROHIBIT)
400 out->type = FIB_API_PATH_TYPE_ICMP_PROHIBIT;
402 else if (rpath->frp_flags & FIB_ROUTE_PATH_LOCAL)
404 out->type = FIB_API_PATH_TYPE_LOCAL;
406 else if (rpath->frp_flags & FIB_ROUTE_PATH_DROP)
408 out->type = FIB_API_PATH_TYPE_DROP;
410 else if (rpath->frp_flags & FIB_ROUTE_PATH_UDP_ENCAP)
412 out->type = FIB_API_PATH_TYPE_UDP_ENCAP;
413 out->nh.obj_id = rpath->frp_udp_encap_id;
415 else if (rpath->frp_flags & FIB_ROUTE_PATH_BIER_IMP)
417 out->type = FIB_API_PATH_TYPE_BIER_IMP;
418 out->nh.obj_id = rpath->frp_bier_imp;
422 out->type = FIB_API_PATH_TYPE_NORMAL;
424 if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_HOST)
426 out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_HOST;
428 if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED)
430 out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED;
433 out->flags = htonl (out->flags);
434 out->type = htonl (out->type);
435 out->proto = htonl (out->proto);
437 if (rpath->frp_label_stack)
441 for (ii = 0; ii < vec_len(rpath->frp_label_stack); ii++)
443 out->label_stack[ii].label =
444 htonl(rpath->frp_label_stack[ii].fml_value);
445 out->label_stack[ii].ttl =
446 rpath->frp_label_stack[ii].fml_ttl;
447 out->label_stack[ii].exp =
448 rpath->frp_label_stack[ii].fml_exp;
455 fib_api_route_add_del (u8 is_add,
458 const fib_prefix_t * prefix,
459 fib_entry_flag_t entry_flags,
460 fib_route_path_t *rpaths)
464 if (vec_len(rpaths) == 0)
465 return (VNET_API_ERROR_NO_PATHS_IN_ROUTE);
467 /* Iterative path add/remove */
469 fib_table_entry_path_add2 (fib_index,
475 fib_table_entry_path_remove2 (fib_index,
484 if (vec_len(rpaths) == 0)
485 return (VNET_API_ERROR_NO_PATHS_IN_ROUTE);
487 /* path replacement */
488 fib_table_entry_update (fib_index,
496 fib_table_entry_delete (fib_index,
505 format_vl_api_fib_path (u8 * s, va_list * args)
507 const vl_api_fib_path_t *path = va_arg (*args, vl_api_fib_path_t*);
509 s = format (s, "sw_if_index %d", ntohl (path->sw_if_index));
510 switch (clib_net_to_host_u32(path->proto))
512 case FIB_API_PATH_NH_PROTO_IP4:
513 s = format (s, " %U", format_vl_api_address_union,
514 &path->nh.address, ADDRESS_IP4);
516 case FIB_API_PATH_NH_PROTO_IP6:
517 s = format (s, " %U", format_vl_api_address_union,
518 &path->nh.address, ADDRESS_IP6);
523 s = format (s, " weight %d", path->weight);
524 s = format (s, " preference %d", path->preference);
525 s = format (s, " type %d", ntohl(path->type));
526 s = format (s, " proto %d", ntohl(path->proto));
527 s = format (s, " flags %d", ntohl(path->flags));
528 s = format (s, " n_labels %d", ntohl(path->n_labels));
529 s = format (s, " table-id %d", ntohl(path->table_id));
530 s = format (s, " rpf-id %d", ntohl(path->rpf_id));
536 fib_proto_from_api_address_family (int af)
538 switch (clib_net_to_host_u32 (af))
541 return (FIB_PROTOCOL_IP4);
543 return (FIB_PROTOCOL_IP6);
547 return (FIB_PROTOCOL_IP4);
551 fib_proto_to_api_address_family (fib_protocol_t fproto)
555 case FIB_PROTOCOL_IP4:
556 return (clib_net_to_host_u32 (ADDRESS_IP4));
557 case FIB_PROTOCOL_IP6:
558 return (clib_net_to_host_u32 (ADDRESS_IP6));
564 return (clib_net_to_host_u32 (ADDRESS_IP4));