MPLS infrastructure improvments
[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* vnet_link_names[] = VNET_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_vnet_link (u8 * s, va_list ap)
39 {
40     vnet_link_t link = va_arg(ap, int); // vnet_link_t promotion
41
42     return (format (s, "%s", vnet_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 void
68 fib_prefix_from_mpls_label (mpls_label_t label,
69                             fib_prefix_t *pfx)
70 {
71     pfx->fp_proto = FIB_PROTOCOL_MPLS;
72     pfx->fp_len = 21;
73     pfx->fp_label = label;
74     pfx->fp_eos = MPLS_NON_EOS;
75 }
76
77 int
78 fib_prefix_cmp (const fib_prefix_t *p1,
79                 const fib_prefix_t *p2)
80 {
81     int res;
82
83     res = (p1->fp_proto - p2->fp_proto);
84
85     if (0 == res)
86     {
87         switch (p1->fp_proto)
88         {
89         case FIB_PROTOCOL_IP4:
90         case FIB_PROTOCOL_IP6:
91             res = (p1->fp_len - p2->fp_len);
92
93             if (0 == res)
94             {
95                 res = ip46_address_cmp(&p1->fp_addr, &p2->fp_addr);
96             }
97             break;
98         case FIB_PROTOCOL_MPLS:
99             res = (p1->fp_label - p2->fp_label);
100
101             if (0 == res)
102             {
103                 res = (p1->fp_eos - p2->fp_eos);
104             }
105             break;
106         }
107     }
108
109     return (res);
110 }
111
112 int
113 fib_prefix_is_cover (const fib_prefix_t *p1,
114                      const fib_prefix_t *p2)
115 {
116     switch (p1->fp_proto)
117     {
118     case FIB_PROTOCOL_IP4:
119         return (ip4_destination_matches_route(&ip4_main,
120                                               &p1->fp_addr.ip4,
121                                               &p2->fp_addr.ip4,
122                                               p1->fp_len));
123     case FIB_PROTOCOL_IP6:
124         return (ip6_destination_matches_route(&ip6_main,
125                                               &p1->fp_addr.ip6,
126                                               &p2->fp_addr.ip6,
127                                               p1->fp_len));
128     case FIB_PROTOCOL_MPLS:
129         break;
130     }
131     return (0);
132 }
133
134 int
135 fib_prefix_is_host (const fib_prefix_t *prefix)
136 {
137     switch (prefix->fp_proto)
138     {
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:
144         return (!0);
145     }
146     return (0);
147 }
148
149 u8 *
150 format_fib_prefix (u8 * s, va_list * args)
151 {
152     fib_prefix_t *fp = va_arg (*args, fib_prefix_t *);
153
154     /*
155      * protocol specific so it prints ::/0 correctly.
156      */
157     switch (fp->fp_proto)
158     {
159     case FIB_PROTOCOL_IP6:
160     {
161         ip6_address_t p6 = fp->fp_addr.ip6;
162
163         ip6_address_mask(&p6, &(ip6_main.fib_masks[fp->fp_len]));
164         s = format (s, "%U", format_ip6_address, &p6);
165         break;
166     }
167     case FIB_PROTOCOL_IP4:
168     {
169         ip4_address_t p4 = fp->fp_addr.ip4;
170         p4.as_u32 &= ip4_main.fib_masks[fp->fp_len];
171
172         s = format (s, "%U", format_ip4_address, &p4);
173         break;
174     }
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);
179         break;
180     }
181     s = format (s, "/%d", fp->fp_len);
182
183     return (s);
184 }
185
186 int
187 fib_route_path_cmp (const fib_route_path_t *rpath1,
188                     const fib_route_path_t *rpath2)
189 {
190     int res;
191
192     res = ip46_address_cmp(&rpath1->frp_addr,
193                            &rpath2->frp_addr);
194
195     if (0 != res) return (res);
196
197     res = vnet_sw_interface_compare(vnet_get_main(),
198                                     rpath1->frp_sw_if_index,
199                                     rpath2->frp_sw_if_index);
200
201     if (0 != res) return (res);
202
203     if (ip46_address_is_zero(&rpath1->frp_addr))
204     {
205         res = rpath1->frp_fib_index - rpath2->frp_fib_index;
206     }
207
208     return (res);
209 }
210
211 dpo_proto_t
212 fib_proto_to_dpo (fib_protocol_t fib_proto)
213 {
214     switch (fib_proto)
215     {
216     case FIB_PROTOCOL_IP6:
217         return (DPO_PROTO_IP6);
218     case FIB_PROTOCOL_IP4:
219         return (DPO_PROTO_IP4);
220     case FIB_PROTOCOL_MPLS:
221         return (DPO_PROTO_MPLS);
222     }
223     ASSERT(0);
224     return (0);
225 }
226
227 fib_protocol_t
228 dpo_proto_to_fib (dpo_proto_t dpo_proto)
229 {
230     switch (dpo_proto)
231     {
232     case DPO_PROTO_IP6:
233         return (FIB_PROTOCOL_IP6);
234     case DPO_PROTO_IP4:
235         return (FIB_PROTOCOL_IP4);
236     case DPO_PROTO_MPLS:
237         return (FIB_PROTOCOL_MPLS);
238     default:
239         break;
240     }
241     ASSERT(0);
242     return (0);
243 }
244
245 vnet_link_t
246 fib_proto_to_link (fib_protocol_t proto)
247 {
248     switch (proto)
249     {
250     case FIB_PROTOCOL_IP4:
251         return (VNET_LINK_IP4);
252     case FIB_PROTOCOL_IP6:
253         return (VNET_LINK_IP6);
254     case FIB_PROTOCOL_MPLS:
255         return (VNET_LINK_MPLS);
256     }
257     ASSERT(0);
258     return (0);
259 }
260
261 fib_forward_chain_type_t
262 fib_forw_chain_type_from_dpo_proto (dpo_proto_t proto)
263 {
264     switch (proto)
265     {
266     case DPO_PROTO_IP4:
267         return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
268     case DPO_PROTO_IP6:
269         return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
270     case DPO_PROTO_MPLS:
271         return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
272     case DPO_PROTO_ETHERNET:
273         return (FIB_FORW_CHAIN_TYPE_ETHERNET);
274     }
275     ASSERT(0);
276     return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
277 }
278
279 vnet_link_t
280 fib_forw_chain_type_to_link_type (fib_forward_chain_type_t fct)
281 {
282     switch (fct)
283     {
284     case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
285         return (VNET_LINK_IP4);
286     case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
287         return (VNET_LINK_IP6);
288     case FIB_FORW_CHAIN_TYPE_ETHERNET:
289         return (VNET_LINK_ETHERNET);
290     case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
291         /*
292          * insufficient information to to convert
293          */
294         ASSERT(0);
295         break;
296     case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
297         return (VNET_LINK_MPLS);
298     }
299     return (VNET_LINK_IP4);
300 }
301
302 dpo_proto_t
303 fib_forw_chain_type_to_dpo_proto (fib_forward_chain_type_t fct)
304 {
305     switch (fct)
306     {
307     case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
308         return (DPO_PROTO_IP4);
309     case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
310         return (DPO_PROTO_IP6);
311     case FIB_FORW_CHAIN_TYPE_ETHERNET:
312         return (DPO_PROTO_ETHERNET);
313     case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
314     case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
315         return (DPO_PROTO_MPLS);
316     }
317     return (DPO_PROTO_IP4);
318 }