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