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;
28 static const char* fib_mpls_lsp_mode_names[] = FIB_MPLS_LSP_MODES;
31 format_fib_protocol (u8 * s, va_list * ap)
33 fib_protocol_t proto = va_arg(*ap, int); // fib_protocol_t promotion
35 return (format (s, "%s", fib_protocol_names[proto]));
39 format_vnet_link (u8 * s, va_list * ap)
41 vnet_link_t link = va_arg(*ap, int); // vnet_link_t promotion
43 return (format (s, "%s", vnet_link_names[link]));
47 format_fib_forw_chain_type (u8 * s, va_list * args)
49 fib_forward_chain_type_t fct = va_arg(*args, int);
51 return (format (s, "%s", fib_forw_chain_names[fct]));
55 format_fib_mpls_lsp_mode(u8 *s, va_list *ap)
57 fib_mpls_lsp_mode_t mode = va_arg(*ap, int);
59 return (format (s, "%s", fib_mpls_lsp_mode_names[mode]));
63 format_fib_mpls_label (u8 *s, va_list *ap)
65 fib_mpls_label_t *label = va_arg(*ap, fib_mpls_label_t *);
67 s = format(s, "%U %U ttl:%d exp:%d",
68 format_mpls_unicast_label,
70 format_fib_mpls_lsp_mode,
79 fib_prefix_from_ip46_addr (const ip46_address_t *addr,
82 ASSERT(!ip46_address_is_zero(addr));
84 pfx->fp_proto = ((ip46_address_is_ip4(addr) ?
87 pfx->fp_len = ((ip46_address_is_ip4(addr) ?
93 fib_prefix_from_mpls_label (mpls_label_t label,
97 pfx->fp_proto = FIB_PROTOCOL_MPLS;
99 pfx->fp_label = label;
104 fib_prefix_cmp (const fib_prefix_t *p1,
105 const fib_prefix_t *p2)
109 res = (p1->fp_proto - p2->fp_proto);
113 switch (p1->fp_proto)
115 case FIB_PROTOCOL_IP4:
116 case FIB_PROTOCOL_IP6:
117 res = (p1->fp_len - p2->fp_len);
121 res = ip46_address_cmp(&p1->fp_addr, &p2->fp_addr);
124 case FIB_PROTOCOL_MPLS:
125 res = (p1->fp_label - p2->fp_label);
129 res = (p1->fp_eos - p2->fp_eos);
139 fib_prefix_is_cover (const fib_prefix_t *p1,
140 const fib_prefix_t *p2)
142 switch (p1->fp_proto)
144 case FIB_PROTOCOL_IP4:
145 return (ip4_destination_matches_route(&ip4_main,
149 case FIB_PROTOCOL_IP6:
150 return (ip6_destination_matches_route(&ip6_main,
154 case FIB_PROTOCOL_MPLS:
161 fib_prefix_is_host (const fib_prefix_t *prefix)
163 switch (prefix->fp_proto)
165 case FIB_PROTOCOL_IP4:
166 return (prefix->fp_len == 32);
167 case FIB_PROTOCOL_IP6:
168 return (prefix->fp_len == 128);
169 case FIB_PROTOCOL_MPLS:
176 format_fib_prefix (u8 * s, va_list * args)
178 fib_prefix_t *fp = va_arg (*args, fib_prefix_t *);
181 * protocol specific so it prints ::/0 correctly.
183 switch (fp->fp_proto)
185 case FIB_PROTOCOL_IP6:
187 ip6_address_t p6 = fp->fp_addr.ip6;
189 ip6_address_mask(&p6, &(ip6_main.fib_masks[fp->fp_len]));
190 s = format (s, "%U", format_ip6_address, &p6);
193 case FIB_PROTOCOL_IP4:
195 ip4_address_t p4 = fp->fp_addr.ip4;
196 p4.as_u32 &= ip4_main.fib_masks[fp->fp_len];
198 s = format (s, "%U", format_ip4_address, &p4);
201 case FIB_PROTOCOL_MPLS:
202 s = format (s, "%U:%U",
203 format_mpls_unicast_label, fp->fp_label,
204 format_mpls_eos_bit, fp->fp_eos);
207 s = format (s, "/%d", fp->fp_len);
213 fib_route_path_cmp (const fib_route_path_t *rpath1,
214 const fib_route_path_t *rpath2)
218 res = ip46_address_cmp(&rpath1->frp_addr,
221 if (0 != res) return (res);
223 res = (rpath1->frp_sw_if_index - rpath2->frp_sw_if_index);
225 if (0 != res) return (res);
227 if (ip46_address_is_zero(&rpath1->frp_addr))
229 res = rpath1->frp_fib_index - rpath2->frp_fib_index;
236 fib_proto_to_dpo (fib_protocol_t fib_proto)
240 case FIB_PROTOCOL_IP6:
241 return (DPO_PROTO_IP6);
242 case FIB_PROTOCOL_IP4:
243 return (DPO_PROTO_IP4);
244 case FIB_PROTOCOL_MPLS:
245 return (DPO_PROTO_MPLS);
252 dpo_proto_to_fib (dpo_proto_t dpo_proto)
257 return (FIB_PROTOCOL_IP6);
259 return (FIB_PROTOCOL_IP4);
261 return (FIB_PROTOCOL_MPLS);
270 fib_proto_to_link (fib_protocol_t proto)
274 case FIB_PROTOCOL_IP4:
275 return (VNET_LINK_IP4);
276 case FIB_PROTOCOL_IP6:
277 return (VNET_LINK_IP6);
278 case FIB_PROTOCOL_MPLS:
279 return (VNET_LINK_MPLS);
285 fib_forward_chain_type_t
286 fib_forw_chain_type_from_dpo_proto (dpo_proto_t proto)
291 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
293 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
295 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
296 case DPO_PROTO_ETHERNET:
297 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
299 return (FIB_FORW_CHAIN_TYPE_NSH);
301 return (FIB_FORW_CHAIN_TYPE_BIER);
304 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
308 fib_forw_chain_type_to_link_type (fib_forward_chain_type_t fct)
312 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
313 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
314 return (VNET_LINK_IP4);
315 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
316 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
317 return (VNET_LINK_IP6);
318 case FIB_FORW_CHAIN_TYPE_ETHERNET:
319 return (VNET_LINK_ETHERNET);
320 case FIB_FORW_CHAIN_TYPE_NSH:
321 return (VNET_LINK_NSH);
322 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
323 case FIB_FORW_CHAIN_TYPE_BIER:
325 * insufficient information to to convert
329 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
330 return (VNET_LINK_MPLS);
332 return (VNET_LINK_IP4);
335 fib_forward_chain_type_t
336 fib_forw_chain_type_from_link_type (vnet_link_t link_type)
341 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
343 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
345 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
346 case VNET_LINK_ETHERNET:
347 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
349 return (FIB_FORW_CHAIN_TYPE_NSH);
355 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
359 fib_forw_chain_type_to_dpo_proto (fib_forward_chain_type_t fct)
363 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
364 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
365 return (DPO_PROTO_IP4);
366 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
367 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
368 return (DPO_PROTO_IP6);
369 case FIB_FORW_CHAIN_TYPE_ETHERNET:
370 return (DPO_PROTO_ETHERNET);
371 case FIB_FORW_CHAIN_TYPE_NSH:
372 return (DPO_PROTO_NSH);
373 case FIB_FORW_CHAIN_TYPE_BIER:
374 return (DPO_PROTO_BIER);
375 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
376 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
377 return (DPO_PROTO_MPLS);
379 return (DPO_PROTO_IP4);
383 unformat_fib_route_path (unformat_input_t * input, va_list * args)
385 fib_route_path_t *rpath = va_arg (*args, fib_route_path_t *);
386 u32 *payload_proto = va_arg (*args, u32*);
387 u32 weight, preference, udp_encap_id;
388 mpls_label_t out_label;
391 vnm = vnet_get_main ();
392 memset(rpath, 0, sizeof(*rpath));
393 rpath->frp_weight = 1;
394 rpath->frp_sw_if_index = ~0;
396 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
398 if (unformat (input, "%U %U",
399 unformat_ip4_address,
400 &rpath->frp_addr.ip4,
401 unformat_vnet_sw_interface, vnm,
402 &rpath->frp_sw_if_index))
404 rpath->frp_proto = DPO_PROTO_IP4;
406 else if (unformat (input, "%U %U",
407 unformat_ip6_address,
408 &rpath->frp_addr.ip6,
409 unformat_vnet_sw_interface, vnm,
410 &rpath->frp_sw_if_index))
412 rpath->frp_proto = DPO_PROTO_IP6;
414 else if (unformat (input, "weight %u", &weight))
416 rpath->frp_weight = weight;
418 else if (unformat (input, "preference %u", &preference))
420 rpath->frp_preference = preference;
422 else if (unformat (input, "%U next-hop-table %d",
423 unformat_ip4_address,
424 &rpath->frp_addr.ip4,
425 &rpath->frp_fib_index))
427 rpath->frp_sw_if_index = ~0;
428 rpath->frp_proto = DPO_PROTO_IP4;
430 else if (unformat (input, "%U next-hop-table %d",
431 unformat_ip6_address,
432 &rpath->frp_addr.ip6,
433 &rpath->frp_fib_index))
435 rpath->frp_sw_if_index = ~0;
436 rpath->frp_proto = DPO_PROTO_IP6;
438 else if (unformat (input, "%U",
439 unformat_ip4_address,
440 &rpath->frp_addr.ip4))
443 * the recursive next-hops are by default in the default table
445 rpath->frp_fib_index = 0;
446 rpath->frp_sw_if_index = ~0;
447 rpath->frp_proto = DPO_PROTO_IP4;
449 else if (unformat (input, "%U",
450 unformat_ip6_address,
451 &rpath->frp_addr.ip6))
453 rpath->frp_fib_index = 0;
454 rpath->frp_sw_if_index = ~0;
455 rpath->frp_proto = DPO_PROTO_IP6;
457 else if (unformat (input, "udp-encap %d", &udp_encap_id))
459 rpath->frp_udp_encap_id = udp_encap_id;
460 rpath->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
461 rpath->frp_proto = *payload_proto;
463 else if (unformat (input, "lookup in table %d", &rpath->frp_fib_index))
465 rpath->frp_proto = *payload_proto;
466 rpath->frp_sw_if_index = ~0;
467 rpath->frp_flags |= FIB_ROUTE_PATH_DEAG;
469 else if (unformat (input, "resolve-via-host"))
471 rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
473 else if (unformat (input, "resolve-via-attached"))
475 rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
477 else if (unformat (input,
478 "ip4-lookup-in-table %d",
479 &rpath->frp_fib_index))
481 rpath->frp_proto = DPO_PROTO_IP4;
482 *payload_proto = DPO_PROTO_IP4;
484 else if (unformat (input,
485 "ip6-lookup-in-table %d",
486 &rpath->frp_fib_index))
488 rpath->frp_proto = DPO_PROTO_IP6;
489 *payload_proto = DPO_PROTO_IP6;
491 else if (unformat (input,
492 "mpls-lookup-in-table %d",
493 &rpath->frp_fib_index))
495 rpath->frp_proto = DPO_PROTO_MPLS;
496 *payload_proto = DPO_PROTO_MPLS;
498 else if (unformat (input,
500 unformat_vnet_sw_interface, vnm,
501 &rpath->frp_sw_if_index))
503 rpath->frp_proto = DPO_PROTO_ETHERNET;
504 *payload_proto = DPO_PROTO_ETHERNET;
506 else if (unformat (input, "via-label %U",
507 unformat_mpls_unicast_label,
508 &rpath->frp_local_label))
510 rpath->frp_eos = MPLS_NON_EOS;
511 rpath->frp_proto = DPO_PROTO_MPLS;
512 rpath->frp_sw_if_index = ~0;
514 else if (unformat (input, "rx-ip4 %U",
515 unformat_vnet_sw_interface, vnm,
516 &rpath->frp_sw_if_index))
518 rpath->frp_proto = DPO_PROTO_IP4;
519 rpath->frp_flags = FIB_ROUTE_PATH_INTF_RX;
521 else if (unformat (input, "out-labels"))
523 while (unformat (input, "%U",
524 unformat_mpls_unicast_label, &out_label))
526 fib_mpls_label_t fml = {
527 .fml_value = out_label,
529 vec_add1(rpath->frp_label_stack, fml);
532 else if (unformat (input, "%U",
533 unformat_vnet_sw_interface, vnm,
534 &rpath->frp_sw_if_index))
536 rpath->frp_proto = *payload_proto;