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/ip/ip.h>
18 #include <vnet/fib/fib_types.h>
19 #include <vnet/fib/fib_internal.h>
20 #include <vnet/mpls/mpls.h>
23 * arrays of protocol and link names
25 static const char* fib_protocol_names[] = FIB_PROTOCOLS;
26 static const char* vnet_link_names[] = VNET_LINKS;
27 static const char* fib_forw_chain_names[] = FIB_FORW_CHAINS;
30 format_fib_protocol (u8 * s, va_list * ap)
32 fib_protocol_t proto = va_arg(*ap, int); // fib_protocol_t promotion
34 return (format (s, "%s", fib_protocol_names[proto]));
38 format_vnet_link (u8 * s, va_list * ap)
40 vnet_link_t link = va_arg(*ap, int); // vnet_link_t promotion
42 return (format (s, "%s", vnet_link_names[link]));
46 format_fib_forw_chain_type (u8 * s, va_list * args)
48 fib_forward_chain_type_t fct = va_arg(*args, int);
50 return (format (s, "%s", fib_forw_chain_names[fct]));
54 fib_prefix_from_ip46_addr (const ip46_address_t *addr,
57 ASSERT(!ip46_address_is_zero(addr));
59 pfx->fp_proto = ((ip46_address_is_ip4(addr) ?
62 pfx->fp_len = ((ip46_address_is_ip4(addr) ?
68 fib_prefix_from_mpls_label (mpls_label_t label,
72 pfx->fp_proto = FIB_PROTOCOL_MPLS;
74 pfx->fp_label = label;
79 fib_prefix_cmp (const fib_prefix_t *p1,
80 const fib_prefix_t *p2)
84 res = (p1->fp_proto - p2->fp_proto);
90 case FIB_PROTOCOL_IP4:
91 case FIB_PROTOCOL_IP6:
92 res = (p1->fp_len - p2->fp_len);
96 res = ip46_address_cmp(&p1->fp_addr, &p2->fp_addr);
99 case FIB_PROTOCOL_MPLS:
100 res = (p1->fp_label - p2->fp_label);
104 res = (p1->fp_eos - p2->fp_eos);
114 fib_prefix_is_cover (const fib_prefix_t *p1,
115 const fib_prefix_t *p2)
117 switch (p1->fp_proto)
119 case FIB_PROTOCOL_IP4:
120 return (ip4_destination_matches_route(&ip4_main,
124 case FIB_PROTOCOL_IP6:
125 return (ip6_destination_matches_route(&ip6_main,
129 case FIB_PROTOCOL_MPLS:
136 fib_prefix_is_host (const fib_prefix_t *prefix)
138 switch (prefix->fp_proto)
140 case FIB_PROTOCOL_IP4:
141 return (prefix->fp_len == 32);
142 case FIB_PROTOCOL_IP6:
143 return (prefix->fp_len == 128);
144 case FIB_PROTOCOL_MPLS:
151 format_fib_prefix (u8 * s, va_list * args)
153 fib_prefix_t *fp = va_arg (*args, fib_prefix_t *);
156 * protocol specific so it prints ::/0 correctly.
158 switch (fp->fp_proto)
160 case FIB_PROTOCOL_IP6:
162 ip6_address_t p6 = fp->fp_addr.ip6;
164 ip6_address_mask(&p6, &(ip6_main.fib_masks[fp->fp_len]));
165 s = format (s, "%U", format_ip6_address, &p6);
168 case FIB_PROTOCOL_IP4:
170 ip4_address_t p4 = fp->fp_addr.ip4;
171 p4.as_u32 &= ip4_main.fib_masks[fp->fp_len];
173 s = format (s, "%U", format_ip4_address, &p4);
176 case FIB_PROTOCOL_MPLS:
177 s = format (s, "%U:%U",
178 format_mpls_unicast_label, fp->fp_label,
179 format_mpls_eos_bit, fp->fp_eos);
182 s = format (s, "/%d", fp->fp_len);
188 fib_route_path_cmp (const fib_route_path_t *rpath1,
189 const fib_route_path_t *rpath2)
193 res = ip46_address_cmp(&rpath1->frp_addr,
196 if (0 != res) return (res);
198 res = (rpath1->frp_sw_if_index - rpath2->frp_sw_if_index);
200 if (0 != res) return (res);
202 if (ip46_address_is_zero(&rpath1->frp_addr))
204 res = rpath1->frp_fib_index - rpath2->frp_fib_index;
211 fib_proto_to_dpo (fib_protocol_t fib_proto)
215 case FIB_PROTOCOL_IP6:
216 return (DPO_PROTO_IP6);
217 case FIB_PROTOCOL_IP4:
218 return (DPO_PROTO_IP4);
219 case FIB_PROTOCOL_MPLS:
220 return (DPO_PROTO_MPLS);
227 dpo_proto_to_fib (dpo_proto_t dpo_proto)
232 return (FIB_PROTOCOL_IP6);
234 return (FIB_PROTOCOL_IP4);
236 return (FIB_PROTOCOL_MPLS);
245 fib_proto_to_link (fib_protocol_t proto)
249 case FIB_PROTOCOL_IP4:
250 return (VNET_LINK_IP4);
251 case FIB_PROTOCOL_IP6:
252 return (VNET_LINK_IP6);
253 case FIB_PROTOCOL_MPLS:
254 return (VNET_LINK_MPLS);
260 fib_forward_chain_type_t
261 fib_forw_chain_type_from_dpo_proto (dpo_proto_t proto)
266 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
268 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
270 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
271 case DPO_PROTO_ETHERNET:
272 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
274 return (FIB_FORW_CHAIN_TYPE_NSH);
276 return (FIB_FORW_CHAIN_TYPE_BIER);
279 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
283 fib_forw_chain_type_to_link_type (fib_forward_chain_type_t fct)
287 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
288 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
289 return (VNET_LINK_IP4);
290 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
291 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
292 return (VNET_LINK_IP6);
293 case FIB_FORW_CHAIN_TYPE_ETHERNET:
294 return (VNET_LINK_ETHERNET);
295 case FIB_FORW_CHAIN_TYPE_NSH:
296 return (VNET_LINK_NSH);
297 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
298 case FIB_FORW_CHAIN_TYPE_BIER:
300 * insufficient information to to convert
304 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
305 return (VNET_LINK_MPLS);
307 return (VNET_LINK_IP4);
311 fib_forw_chain_type_to_dpo_proto (fib_forward_chain_type_t fct)
315 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
316 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
317 return (DPO_PROTO_IP4);
318 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
319 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
320 return (DPO_PROTO_IP6);
321 case FIB_FORW_CHAIN_TYPE_ETHERNET:
322 return (DPO_PROTO_ETHERNET);
323 case FIB_FORW_CHAIN_TYPE_NSH:
324 return (DPO_PROTO_NSH);
325 case FIB_FORW_CHAIN_TYPE_BIER:
326 return (DPO_PROTO_BIER);
327 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
328 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
329 return (DPO_PROTO_MPLS);
331 return (DPO_PROTO_IP4);
335 unformat_fib_route_path (unformat_input_t * input, va_list * args)
337 fib_route_path_t *rpath = va_arg (*args, fib_route_path_t *);
338 u32 *payload_proto = va_arg (*args, u32*);
339 u32 weight, preference, udp_encap_id;
340 mpls_label_t out_label;
343 vnm = vnet_get_main ();
344 memset(rpath, 0, sizeof(*rpath));
345 rpath->frp_weight = 1;
346 rpath->frp_sw_if_index = ~0;
348 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
350 if (unformat (input, "%U %U",
351 unformat_ip4_address,
352 &rpath->frp_addr.ip4,
353 unformat_vnet_sw_interface, vnm,
354 &rpath->frp_sw_if_index))
356 rpath->frp_proto = DPO_PROTO_IP4;
358 else if (unformat (input, "%U %U",
359 unformat_ip6_address,
360 &rpath->frp_addr.ip6,
361 unformat_vnet_sw_interface, vnm,
362 &rpath->frp_sw_if_index))
364 rpath->frp_proto = DPO_PROTO_IP6;
366 else if (unformat (input, "weight %u", &weight))
368 rpath->frp_weight = weight;
370 else if (unformat (input, "preference %u", &preference))
372 rpath->frp_preference = preference;
374 else if (unformat (input, "%U next-hop-table %d",
375 unformat_ip4_address,
376 &rpath->frp_addr.ip4,
377 &rpath->frp_fib_index))
379 rpath->frp_sw_if_index = ~0;
380 rpath->frp_proto = DPO_PROTO_IP4;
382 else if (unformat (input, "%U next-hop-table %d",
383 unformat_ip6_address,
384 &rpath->frp_addr.ip6,
385 &rpath->frp_fib_index))
387 rpath->frp_sw_if_index = ~0;
388 rpath->frp_proto = DPO_PROTO_IP6;
390 else if (unformat (input, "%U",
391 unformat_ip4_address,
392 &rpath->frp_addr.ip4))
395 * the recursive next-hops are by default in the default table
397 rpath->frp_fib_index = 0;
398 rpath->frp_sw_if_index = ~0;
399 rpath->frp_proto = DPO_PROTO_IP4;
401 else if (unformat (input, "%U",
402 unformat_ip6_address,
403 &rpath->frp_addr.ip6))
405 rpath->frp_fib_index = 0;
406 rpath->frp_sw_if_index = ~0;
407 rpath->frp_proto = DPO_PROTO_IP6;
409 else if (unformat (input, "udp-encap %d", &udp_encap_id))
411 rpath->frp_udp_encap_id = udp_encap_id;
412 rpath->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
413 rpath->frp_proto = *payload_proto;
415 else if (unformat (input, "lookup in table %d", &rpath->frp_fib_index))
417 rpath->frp_proto = *payload_proto;
418 rpath->frp_sw_if_index = ~0;
419 rpath->frp_flags |= FIB_ROUTE_PATH_DEAG;
421 else if (unformat (input, "resolve-via-host"))
423 rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
425 else if (unformat (input, "resolve-via-attached"))
427 rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
429 else if (unformat (input,
430 "ip4-lookup-in-table %d",
431 &rpath->frp_fib_index))
433 rpath->frp_proto = DPO_PROTO_IP4;
434 *payload_proto = DPO_PROTO_IP4;
436 else if (unformat (input,
437 "ip6-lookup-in-table %d",
438 &rpath->frp_fib_index))
440 rpath->frp_proto = DPO_PROTO_IP6;
441 *payload_proto = DPO_PROTO_IP6;
443 else if (unformat (input,
444 "mpls-lookup-in-table %d",
445 &rpath->frp_fib_index))
447 rpath->frp_proto = DPO_PROTO_MPLS;
448 *payload_proto = DPO_PROTO_MPLS;
450 else if (unformat (input,
452 unformat_vnet_sw_interface, vnm,
453 &rpath->frp_sw_if_index))
455 rpath->frp_proto = DPO_PROTO_ETHERNET;
456 *payload_proto = DPO_PROTO_ETHERNET;
458 else if (unformat (input, "via-label %U",
459 unformat_mpls_unicast_label,
460 &rpath->frp_local_label))
462 rpath->frp_eos = MPLS_NON_EOS;
463 rpath->frp_proto = DPO_PROTO_MPLS;
464 rpath->frp_sw_if_index = ~0;
466 else if (unformat (input, "rx-ip4 %U",
467 unformat_vnet_sw_interface, vnm,
468 &rpath->frp_sw_if_index))
470 rpath->frp_proto = DPO_PROTO_IP4;
471 rpath->frp_flags = FIB_ROUTE_PATH_INTF_RX;
473 else if (unformat (input, "out-labels"))
475 while (unformat (input, "%U",
476 unformat_mpls_unicast_label, &out_label))
478 vec_add1(rpath->frp_label_stack, out_label);
481 else if (unformat (input, "%U",
482 unformat_vnet_sw_interface, vnm,
483 &rpath->frp_sw_if_index))
485 rpath->frp_proto = *payload_proto;