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,
71 pfx->fp_proto = FIB_PROTOCOL_MPLS;
73 pfx->fp_label = label;
74 pfx->fp_eos = MPLS_NON_EOS;
78 fib_prefix_cmp (const fib_prefix_t *p1,
79 const fib_prefix_t *p2)
83 res = (p1->fp_proto - p2->fp_proto);
89 case FIB_PROTOCOL_IP4:
90 case FIB_PROTOCOL_IP6:
91 res = (p1->fp_len - p2->fp_len);
95 res = ip46_address_cmp(&p1->fp_addr, &p2->fp_addr);
98 case FIB_PROTOCOL_MPLS:
99 res = (p1->fp_label - p2->fp_label);
103 res = (p1->fp_eos - p2->fp_eos);
113 fib_prefix_is_cover (const fib_prefix_t *p1,
114 const fib_prefix_t *p2)
116 switch (p1->fp_proto)
118 case FIB_PROTOCOL_IP4:
119 return (ip4_destination_matches_route(&ip4_main,
123 case FIB_PROTOCOL_IP6:
124 return (ip6_destination_matches_route(&ip6_main,
128 case FIB_PROTOCOL_MPLS:
135 fib_prefix_is_host (const fib_prefix_t *prefix)
137 switch (prefix->fp_proto)
139 case FIB_PROTOCOL_IP4:
140 return (prefix->fp_len == 32);
141 case FIB_PROTOCOL_IP6:
142 return (prefix->fp_len == 128);
143 case FIB_PROTOCOL_MPLS:
150 format_fib_prefix (u8 * s, va_list * args)
152 fib_prefix_t *fp = va_arg (*args, fib_prefix_t *);
155 * protocol specific so it prints ::/0 correctly.
157 switch (fp->fp_proto)
159 case FIB_PROTOCOL_IP6:
161 ip6_address_t p6 = fp->fp_addr.ip6;
163 ip6_address_mask(&p6, &(ip6_main.fib_masks[fp->fp_len]));
164 s = format (s, "%U", format_ip6_address, &p6);
167 case FIB_PROTOCOL_IP4:
169 ip4_address_t p4 = fp->fp_addr.ip4;
170 p4.as_u32 &= ip4_main.fib_masks[fp->fp_len];
172 s = format (s, "%U", format_ip4_address, &p4);
175 case FIB_PROTOCOL_MPLS:
176 s = format (s, "%U:%U",
177 format_mpls_unicast_label, fp->fp_label,
178 format_mpls_eos_bit, fp->fp_eos);
181 s = format (s, "/%d", fp->fp_len);
187 fib_route_path_cmp (const fib_route_path_t *rpath1,
188 const fib_route_path_t *rpath2)
192 res = ip46_address_cmp(&rpath1->frp_addr,
195 if (0 != res) return (res);
197 if (~0 != rpath1->frp_sw_if_index &&
198 ~0 != rpath2->frp_sw_if_index)
200 res = vnet_sw_interface_compare(vnet_get_main(),
201 rpath1->frp_sw_if_index,
202 rpath2->frp_sw_if_index);
206 res = rpath1->frp_sw_if_index - rpath2->frp_sw_if_index;
209 if (0 != res) return (res);
211 if (ip46_address_is_zero(&rpath1->frp_addr))
213 res = rpath1->frp_fib_index - rpath2->frp_fib_index;
220 fib_proto_to_dpo (fib_protocol_t fib_proto)
224 case FIB_PROTOCOL_IP6:
225 return (DPO_PROTO_IP6);
226 case FIB_PROTOCOL_IP4:
227 return (DPO_PROTO_IP4);
228 case FIB_PROTOCOL_MPLS:
229 return (DPO_PROTO_MPLS);
236 dpo_proto_to_fib (dpo_proto_t dpo_proto)
241 return (FIB_PROTOCOL_IP6);
243 return (FIB_PROTOCOL_IP4);
245 return (FIB_PROTOCOL_MPLS);
254 fib_proto_to_link (fib_protocol_t proto)
258 case FIB_PROTOCOL_IP4:
259 return (VNET_LINK_IP4);
260 case FIB_PROTOCOL_IP6:
261 return (VNET_LINK_IP6);
262 case FIB_PROTOCOL_MPLS:
263 return (VNET_LINK_MPLS);
269 fib_forward_chain_type_t
270 fib_forw_chain_type_from_dpo_proto (dpo_proto_t proto)
275 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
277 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
279 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
280 case DPO_PROTO_ETHERNET:
281 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
284 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
288 fib_forw_chain_type_to_link_type (fib_forward_chain_type_t fct)
292 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
293 return (VNET_LINK_IP4);
294 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
295 return (VNET_LINK_IP6);
296 case FIB_FORW_CHAIN_TYPE_ETHERNET:
297 return (VNET_LINK_ETHERNET);
298 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
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 return (DPO_PROTO_IP4);
317 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
318 return (DPO_PROTO_IP6);
319 case FIB_FORW_CHAIN_TYPE_ETHERNET:
320 return (DPO_PROTO_ETHERNET);
321 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
322 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
323 return (DPO_PROTO_MPLS);
325 return (DPO_PROTO_IP4);