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>
25 #include <vnet/ip/ip_format_fns.h>
27 #include <vnet/fib/fib.api_enum.h>
28 #include <vnet/fib/fib.api_types.h>
30 static u16 fib_base_msg_id;
31 #define REPLY_MSG_ID_BASE fib_base_msg_id
32 #include <vlibapi/api_helper_macros.h>
35 fib_api_table_id_decode (fib_protocol_t fproto,
39 *fib_index = fib_table_find(fproto, table_id);
41 if (INDEX_INVALID == *fib_index)
43 return VNET_API_ERROR_NO_SUCH_FIB;
50 fib_api_mtable_id_decode (fib_protocol_t fproto,
54 *fib_index = mfib_table_find(fproto, table_id);
58 return VNET_API_ERROR_NO_SUCH_FIB;
65 fib_api_next_hop_decode (const vl_api_fib_path_t *in,
68 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);
72 static vl_api_fib_path_nh_proto_t
73 fib_api_path_dpo_proto_to_nh (dpo_proto_t dproto)
78 return (FIB_API_PATH_NH_PROTO_IP4);
80 return (FIB_API_PATH_NH_PROTO_IP6);
82 return (FIB_API_PATH_NH_PROTO_MPLS);
84 return (FIB_API_PATH_NH_PROTO_BIER);
85 case DPO_PROTO_ETHERNET:
86 return (FIB_API_PATH_NH_PROTO_ETHERNET);
91 return (FIB_API_PATH_NH_PROTO_IP4);
96 fib_api_next_hop_encode (const fib_route_path_t *rpath,
97 vl_api_fib_path_t *fp)
99 fp->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
101 if (rpath->frp_proto == DPO_PROTO_IP4)
102 clib_memcpy (&fp->nh.address.ip4,
103 &rpath->frp_addr.ip4,
104 sizeof (rpath->frp_addr.ip4));
105 else if (rpath->frp_proto == DPO_PROTO_IP6)
106 clib_memcpy (&fp->nh.address.ip6,
107 &rpath->frp_addr.ip6,
108 sizeof (rpath->frp_addr.ip6));
112 fib_api_path_nh_proto_to_dpo (vl_api_fib_path_nh_proto_t pp,
117 case FIB_API_PATH_NH_PROTO_IP4:
118 *dproto = DPO_PROTO_IP4;
120 case FIB_API_PATH_NH_PROTO_IP6:
121 *dproto = DPO_PROTO_IP6;
123 case FIB_API_PATH_NH_PROTO_MPLS:
124 *dproto = DPO_PROTO_MPLS;
126 case FIB_API_PATH_NH_PROTO_BIER:
127 *dproto = DPO_PROTO_BIER;
129 case FIB_API_PATH_NH_PROTO_ETHERNET:
130 *dproto = DPO_PROTO_ETHERNET;
139 fib_api_path_decode (vl_api_fib_path_t *in,
140 fib_route_path_t *out)
142 vnet_classify_main_t *cm = &vnet_classify_main;
143 int rv = 0, n_labels;
147 vnm = vnet_get_main ();
148 clib_memset(&out->frp_dpo, 0, sizeof(out->frp_dpo));
151 in->flags = ntohl (in->flags);
152 in->type = ntohl (in->type);
153 in->proto = ntohl (in->proto);
156 * attributes that apply to all path types
159 out->frp_weight = in->weight;
160 if (0 == out->frp_weight)
164 out->frp_preference = in->preference;
166 rv = fib_api_path_nh_proto_to_dpo(in->proto, &out->frp_proto);
172 * convert the flags and the AFI to determine the path type
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;
178 if (in->flags & FIB_API_PATH_FLAG_POP_PW_CW)
179 out->frp_flags |= FIB_ROUTE_PATH_POP_PW_CW;
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;
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;
191 case FIB_API_PATH_TYPE_DROP:
192 out->frp_flags |= FIB_ROUTE_PATH_DROP;
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);
198 case FIB_API_PATH_TYPE_ICMP_UNREACH:
199 out->frp_flags |= FIB_ROUTE_PATH_ICMP_UNREACH;
201 case FIB_API_PATH_TYPE_ICMP_PROHIBIT:
202 out->frp_flags |= FIB_ROUTE_PATH_ICMP_PROHIBIT;
204 case FIB_API_PATH_TYPE_CLASSIFY:
205 out->frp_flags |= FIB_ROUTE_PATH_CLASSIFY;
207 if (pool_is_free_index (cm->tables, ntohl (in->nh.classify_table_index)))
209 return VNET_API_ERROR_NO_SUCH_TABLE;
211 out->frp_classify_table_id = ntohl (in->nh.classify_table_index);
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);
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);
222 case FIB_API_PATH_TYPE_SOURCE_LOOKUP:
223 out->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
225 case FIB_API_PATH_TYPE_NORMAL:
226 switch (out->frp_proto)
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);
234 if (0 == out->frp_rpf_id)
236 /* allow 0 to be an unset value on the API */
237 out->frp_rpf_id = ~0;
240 if (~0 != out->frp_rpf_id)
242 out->frp_flags |= FIB_ROUTE_PATH_RPF_ID;
245 if (~0 == out->frp_sw_if_index)
247 /* recursive or deag, validate the next-hop FIB */
248 if (~0 != out->frp_rpf_id)
250 rv = fib_api_mtable_id_decode(
251 dpo_proto_to_fib(out->frp_proto),
253 &out->frp_fib_index);
257 rv = fib_api_table_id_decode(
258 dpo_proto_to_fib(out->frp_proto),
260 &out->frp_fib_index);
269 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
270 out->frp_sw_if_index))
272 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
276 if (ip46_address_is_zero(&out->frp_addr))
278 if (~0 == out->frp_sw_if_index &&
279 ~0 != out->frp_fib_index)
281 out->frp_flags |= FIB_ROUTE_PATH_DEAG;
287 out->frp_local_label = ntohl (in->nh.via_label);
288 out->frp_eos = MPLS_NON_EOS;
289 out->frp_sw_if_index = ~0;
292 out->frp_sw_if_index = ntohl(in->sw_if_index);
293 out->frp_rpf_id = ntohl(in->rpf_id);
295 if (!(out->frp_flags & FIB_ROUTE_PATH_BIER_IMP))
297 fib_api_next_hop_decode(in, &out->frp_addr);
299 if (ip46_address_is_zero(&out->frp_addr))
303 bdti = bier_disp_table_find(ntohl(in->table_id));
305 if (INDEX_INVALID != bdti)
307 out->frp_fib_index = bdti;
311 return (VNET_API_ERROR_NO_SUCH_FIB);
316 case DPO_PROTO_ETHERNET:
317 out->frp_sw_if_index = ntohl(in->sw_if_index);
324 n_labels = in->n_labels;
327 vec_validate (out->frp_label_stack, n_labels - 1);
328 for (ii = 0; ii < n_labels; ii++)
330 out->frp_label_stack[ii].fml_value =
331 ntohl(in->label_stack[ii].label);
332 out->frp_label_stack[ii].fml_ttl =
333 in->label_stack[ii].ttl;
334 out->frp_label_stack[ii].fml_exp =
335 in->label_stack[ii].exp;
336 out->frp_label_stack[ii].fml_mode =
337 (in->label_stack[ii].is_uniform ?
338 FIB_MPLS_LSP_MODE_UNIFORM :
339 FIB_MPLS_LSP_MODE_PIPE);
347 fib_api_path_encode (const fib_route_path_t * rpath,
348 vl_api_fib_path_t *out)
350 memset (out, 0, sizeof (*out));
352 out->weight = rpath->frp_weight;
353 out->preference = rpath->frp_preference;
354 out->sw_if_index = htonl (rpath->frp_sw_if_index);
355 out->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
356 out->rpf_id = rpath->frp_rpf_id;
357 fib_api_next_hop_encode (rpath, out);
359 if (0 != rpath->frp_fib_index)
361 if ((DPO_PROTO_IP6 == rpath->frp_proto) ||
362 (DPO_PROTO_IP4 == rpath->frp_proto))
364 if (rpath->frp_flags & FIB_ROUTE_PATH_RPF_ID)
366 out->table_id = htonl (mfib_table_get_table_id(
367 rpath->frp_fib_index,
368 dpo_proto_to_fib(rpath->frp_proto)));
372 out->table_id = htonl (fib_table_get_table_id(
373 rpath->frp_fib_index,
374 dpo_proto_to_fib(rpath->frp_proto)));
379 if (rpath->frp_flags & FIB_ROUTE_PATH_DVR)
381 out->type = FIB_API_PATH_TYPE_DVR;
383 else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_UNREACH)
385 out->type = FIB_API_PATH_TYPE_ICMP_UNREACH;
387 else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_PROHIBIT)
389 out->type = FIB_API_PATH_TYPE_ICMP_PROHIBIT;
391 else if (rpath->frp_flags & FIB_ROUTE_PATH_LOCAL)
393 out->type = FIB_API_PATH_TYPE_LOCAL;
395 else if (rpath->frp_flags & FIB_ROUTE_PATH_DROP)
397 out->type = FIB_API_PATH_TYPE_DROP;
399 else if (rpath->frp_flags & FIB_ROUTE_PATH_UDP_ENCAP)
401 out->type = FIB_API_PATH_TYPE_UDP_ENCAP;
402 out->nh.obj_id = rpath->frp_udp_encap_id;
404 else if (rpath->frp_flags & FIB_ROUTE_PATH_BIER_IMP)
406 out->type = FIB_API_PATH_TYPE_BIER_IMP;
407 out->nh.obj_id = rpath->frp_bier_imp;
409 else if (rpath->frp_flags & FIB_ROUTE_PATH_INTF_RX)
411 out->type = FIB_API_PATH_TYPE_INTERFACE_RX;
415 out->type = FIB_API_PATH_TYPE_NORMAL;
417 if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_HOST)
419 out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_HOST;
421 if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED)
423 out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED;
426 out->flags = htonl (out->flags);
427 out->type = htonl (out->type);
428 out->proto = htonl (out->proto);
430 if (rpath->frp_label_stack)
434 for (ii = 0; ii < vec_len(rpath->frp_label_stack); ii++)
436 out->label_stack[ii].label =
437 htonl(rpath->frp_label_stack[ii].fml_value);
438 out->label_stack[ii].ttl =
439 rpath->frp_label_stack[ii].fml_ttl;
440 out->label_stack[ii].exp =
441 rpath->frp_label_stack[ii].fml_exp;
448 fib_api_route_add_del (u8 is_add,
451 const fib_prefix_t * prefix,
453 fib_entry_flag_t entry_flags,
454 fib_route_path_t *rpaths)
458 if (vec_len(rpaths) == 0)
459 return (VNET_API_ERROR_NO_PATHS_IN_ROUTE);
461 /* Iterative path add/remove */
463 fib_table_entry_path_add2 (fib_index,
469 fib_table_entry_path_remove2 (fib_index,
478 if (vec_len(rpaths) == 0)
479 return (VNET_API_ERROR_NO_PATHS_IN_ROUTE);
481 /* path replacement */
482 fib_table_entry_update (fib_index,
490 fib_table_entry_delete (fib_index,
499 format_vl_api_fib_path (u8 * s, va_list * args)
501 const vl_api_fib_path_t *path = va_arg (*args, vl_api_fib_path_t*);
503 s = format (s, "sw_if_index %d", ntohl (path->sw_if_index));
504 switch (clib_net_to_host_u32(path->proto))
506 case FIB_API_PATH_NH_PROTO_IP4:
507 s = format (s, " %U", format_vl_api_address_union,
508 &path->nh.address, ADDRESS_IP4);
510 case FIB_API_PATH_NH_PROTO_IP6:
511 s = format (s, " %U", format_vl_api_address_union,
512 &path->nh.address, ADDRESS_IP6);
517 s = format (s, " weight %d", path->weight);
518 s = format (s, " preference %d", path->preference);
519 s = format (s, " type %d", ntohl(path->type));
520 s = format (s, " proto %d", ntohl(path->proto));
521 s = format (s, " flags %d", ntohl(path->flags));
522 s = format (s, " n_labels %d", ntohl(path->n_labels));
523 s = format (s, " table-id %d", ntohl(path->table_id));
524 s = format (s, " rpf-id %d", ntohl(path->rpf_id));
530 fib_proto_from_api_address_family (vl_api_address_family_t af, fib_protocol_t * out)
535 *out = (FIB_PROTOCOL_IP4);
538 *out = (FIB_PROTOCOL_IP6);
542 return (VNET_API_ERROR_INVALID_ADDRESS_FAMILY);
545 vl_api_address_family_t
546 fib_proto_to_api_address_family (fib_protocol_t fproto)
550 case FIB_PROTOCOL_IP4:
551 return (ADDRESS_IP4);
552 case FIB_PROTOCOL_IP6:
553 return (ADDRESS_IP6);
559 return (ADDRESS_IP4);
563 vl_api_fib_source_add_t_handler (vl_api_fib_source_add_t * mp)
565 vl_api_fib_source_add_reply_t *rmp;
570 name = format (0, "%s", mp->src.name);
573 src = fib_source_allocate((const char *)name,
579 REPLY_MACRO2 (VL_API_FIB_SOURCE_ADD_REPLY,
585 typedef struct fib_source_dump_ctx_t_
587 vl_api_registration_t * reg;
589 } fib_source_dump_ctx_t;
592 send_fib_source (fib_source_t id,
594 fib_source_priority_t prio,
595 fib_source_behaviour_t bh,
598 vl_api_fib_source_details_t *mp;
599 fib_source_dump_ctx_t *ctx;
602 mp = vl_msg_api_alloc_zero (sizeof (*mp));
606 mp->_vl_msg_id = ntohs (VL_API_FIB_SOURCE_DETAILS + REPLY_MSG_ID_BASE);
607 mp->context = ctx->context;
609 mp->src.priority = prio;
611 clib_memcpy(mp->src.name, name,
612 clib_min(strlen(name), ARRAY_LEN(mp->src.name)));
614 vl_api_send_msg (ctx->reg, (u8 *) mp);
616 return (WALK_CONTINUE);
620 vl_api_fib_source_dump_t_handler (vl_api_fib_source_dump_t * mp)
622 vl_api_registration_t *reg;
624 reg = vl_api_client_index_to_registration (mp->client_index);
628 fib_source_dump_ctx_t ctx = {
630 .context = mp->context,
633 fib_source_walk(send_fib_source, &ctx);
637 #include <vnet/fib/fib.api.c>
639 static clib_error_t *
640 fib_api_hookup (vlib_main_t * vm)
643 * Set up the (msg_name, crc, message-id) table
645 fib_base_msg_id = setup_message_id_table ();
650 VLIB_API_INIT_FUNCTION (fib_api_hookup);