A Protocol Independent Hierarchical FIB (VPP-352)
[vpp.git] / vnet / vnet / fib / fib_types.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vnet/ip/ip.h>
17
18 #include <vnet/fib/fib_types.h>
19 #include <vnet/fib/fib_internal.h>
20 #include <vnet/mpls/mpls.h>
21
22 /*
23  * arrays of protocol and link names
24  */
25 static const char* fib_protocol_names[] = FIB_PROTOCOLS;
26 static const char* fib_link_names[] = FIB_LINKS;
27 static const char* fib_forw_chain_names[] = FIB_FORW_CHAINS;
28
29 u8 *
30 format_fib_protocol (u8 * s, va_list ap)
31 {
32     fib_protocol_t proto = va_arg(ap, int); // fib_protocol_t promotion
33
34     return (format (s, "%s", fib_protocol_names[proto]));
35 }
36
37 u8 *
38 format_fib_link (u8 * s, va_list ap)
39 {
40     fib_link_t link = va_arg(ap, int); // fib_link_t promotion
41
42     return (format (s, "%s", fib_link_names[link]));
43 }
44
45 u8 *
46 format_fib_forw_chain_type (u8 * s, va_list * args)
47 {
48     fib_forward_chain_type_t fct = va_arg(*args, int);
49
50     return (format (s, "%s", fib_forw_chain_names[fct]));
51 }
52
53 void
54 fib_prefix_from_ip46_addr (const ip46_address_t *addr,
55                            fib_prefix_t *pfx)
56 {
57     ASSERT(!ip46_address_is_zero(addr));
58
59     pfx->fp_proto = ((ip46_address_is_ip4(addr) ?
60                       FIB_PROTOCOL_IP4 :
61                       FIB_PROTOCOL_IP6));
62     pfx->fp_len = ((ip46_address_is_ip4(addr) ?
63                     32 : 128));
64     pfx->fp_addr = *addr;
65 }
66
67 int
68 fib_prefix_cmp (const fib_prefix_t *p1,
69                 const fib_prefix_t *p2)
70 {
71     int res;
72
73     res = (p1->fp_proto - p2->fp_proto);
74
75     if (0 == res)
76     {
77         switch (p1->fp_proto)
78         {
79         case FIB_PROTOCOL_IP4:
80         case FIB_PROTOCOL_IP6:
81             res = (p1->fp_len - p2->fp_len);
82
83             if (0 == res)
84             {
85                 res = ip46_address_cmp(&p1->fp_addr, &p2->fp_addr);
86             }
87             break;
88         case FIB_PROTOCOL_MPLS:
89             res = (p1->fp_label - p2->fp_label);
90
91             if (0 == res)
92             {
93                 res = (p1->fp_eos - p2->fp_eos);
94             }
95             break;
96         }
97     }
98
99     return (res);
100 }
101
102 int
103 fib_prefix_is_cover (const fib_prefix_t *p1,
104                      const fib_prefix_t *p2)
105 {
106     switch (p1->fp_proto)
107     {
108     case FIB_PROTOCOL_IP4:
109         return (ip4_destination_matches_route(&ip4_main,
110                                               &p1->fp_addr.ip4,
111                                               &p2->fp_addr.ip4,
112                                               p1->fp_len));
113     case FIB_PROTOCOL_IP6:
114         return (ip6_destination_matches_route(&ip6_main,
115                                               &p1->fp_addr.ip6,
116                                               &p2->fp_addr.ip6,
117                                               p1->fp_len));
118     case FIB_PROTOCOL_MPLS:
119         break;
120     }
121     return (0);
122 }
123
124 int
125 fib_prefix_is_host (const fib_prefix_t *prefix)
126 {
127     switch (prefix->fp_proto)
128     {
129     case FIB_PROTOCOL_IP4:
130         return (prefix->fp_len == 32);
131     case FIB_PROTOCOL_IP6:
132         return (prefix->fp_len == 128);
133     case FIB_PROTOCOL_MPLS:
134         return (!0);
135     }
136     return (0);
137 }
138
139 u8 *
140 format_fib_prefix (u8 * s, va_list * args)
141 {
142     fib_prefix_t *fp = va_arg (*args, fib_prefix_t *);
143
144     /*
145      * protocol specific so it prints ::/0 correctly.
146      */
147     switch (fp->fp_proto)
148     {
149     case FIB_PROTOCOL_IP6:
150     {
151         ip6_address_t p6 = fp->fp_addr.ip6;
152
153         ip6_address_mask(&p6, &(ip6_main.fib_masks[fp->fp_len]));
154         s = format (s, "%U", format_ip6_address, &p6);
155         break;
156     }
157     case FIB_PROTOCOL_IP4:
158     {
159         ip4_address_t p4 = fp->fp_addr.ip4;
160         p4.as_u32 &= ip4_main.fib_masks[fp->fp_len];
161
162         s = format (s, "%U", format_ip4_address, &p4);
163         break;
164     }
165     case FIB_PROTOCOL_MPLS:
166         s = format (s, "%U:%U",
167                     format_mpls_unicast_label, fp->fp_label,
168                     format_mpls_eos_bit, fp->fp_eos);
169         break;
170     }
171     s = format (s, "/%d", fp->fp_len);
172
173     return (s);
174 }
175
176 int
177 fib_route_path_cmp (const fib_route_path_t *rpath1,
178                     const fib_route_path_t *rpath2)
179 {
180     int res;
181
182     res = ip46_address_cmp(&rpath1->frp_addr,
183                            &rpath2->frp_addr);
184
185     if (0 != res) return (res);
186
187     res = vnet_sw_interface_compare(vnet_get_main(),
188                                     rpath1->frp_sw_if_index,
189                                     rpath2->frp_sw_if_index);
190
191     if (0 != res) return (res);
192
193     if (ip46_address_is_zero(&rpath1->frp_addr))
194     {
195         res = rpath1->frp_fib_index - rpath2->frp_fib_index;
196     }
197
198     return (res);
199 }
200
201 dpo_proto_t
202 fib_proto_to_dpo (fib_protocol_t fib_proto)
203 {
204     switch (fib_proto)
205     {
206     case FIB_PROTOCOL_IP6:
207         return (DPO_PROTO_IP6);
208     case FIB_PROTOCOL_IP4:
209         return (DPO_PROTO_IP4);
210     case FIB_PROTOCOL_MPLS:
211         return (DPO_PROTO_MPLS);
212     }
213     ASSERT(0);
214     return (0);
215 }
216
217 fib_protocol_t
218 dpo_proto_to_fib (dpo_proto_t dpo_proto)
219 {
220     switch (dpo_proto)
221     {
222     case DPO_PROTO_IP6:
223         return (FIB_PROTOCOL_IP6);
224     case DPO_PROTO_IP4:
225         return (FIB_PROTOCOL_IP4);
226     case DPO_PROTO_MPLS:
227         return (FIB_PROTOCOL_MPLS);
228     }
229     ASSERT(0);
230     return (0);
231 }
232
233 fib_link_t
234 fib_proto_to_link (fib_protocol_t proto)
235 {
236     switch (proto)
237     {
238     case FIB_PROTOCOL_IP4:
239         return (FIB_LINK_IP4);
240     case FIB_PROTOCOL_IP6:
241         return (FIB_LINK_IP6);
242     case FIB_PROTOCOL_MPLS:
243         return (FIB_LINK_MPLS);
244     }
245     ASSERT(0);
246     return (0);
247 }
248
249 fib_forward_chain_type_t
250 fib_proto_to_forw_chain_type (fib_protocol_t proto)
251 {
252     switch (proto)
253     {
254     case FIB_PROTOCOL_IP4:
255         return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
256     case FIB_PROTOCOL_IP6:
257         return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
258     case FIB_PROTOCOL_MPLS:
259         return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
260     }
261     ASSERT(0);
262     return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
263 }
264
265 fib_link_t
266 fib_forw_chain_type_to_link_type (fib_forward_chain_type_t fct)
267 {
268     switch (fct)
269     {
270     case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
271         return (FIB_LINK_IP4);
272     case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
273         return (FIB_LINK_IP6);
274     case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
275         /*
276          * insufficient information to to convert
277          */
278         ASSERT(0);
279         break;
280     case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
281         return (FIB_LINK_MPLS);
282     }
283     return (FIB_LINK_IP4);
284 }
285
286 dpo_proto_t
287 fib_forw_chain_type_to_dpo_proto (fib_forward_chain_type_t fct)
288 {
289     switch (fct)
290     {
291     case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
292         return (DPO_PROTO_IP4);
293     case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
294         return (DPO_PROTO_IP6);
295     case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
296         /*
297          * insufficient information to to convert
298          */
299         ASSERT(0);
300         break;
301     case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
302         return (DPO_PROTO_MPLS);
303     }
304     return (FIB_LINK_IP4);
305 }