gre: fix feature.yaml error
[vpp.git] / src / vnet / fib / fib_api.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/vnet.h>
17 #include <vlibmemory/api.h>
18 #include <vnet/fib/fib_api.h>
19 #include <vnet/ip/ip_types_api.h>
20 #include <vnet/fib/fib_table.h>
21 #include <vnet/mfib/mfib_table.h>
22 #include <vnet/bier/bier_disp_table.h>
23 #include <vpp/api/types.h>
24
25 #include <vnet/vnet_msg_enum.h>
26
27 #define vl_typedefs             /* define message structures */
28 #include <vnet/vnet_all_api_h.h>
29 #undef vl_typedefs
30
31 #define vl_endianfun            /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
33 #undef vl_endianfun
34
35 /* instantiate all the print functions we know about */
36 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
37 #define vl_printfun
38 #include <vnet/vnet_all_api_h.h>
39 #undef vl_printfun
40
41 #include <vlibapi/api_helper_macros.h>
42
43 int
44 fib_api_table_id_decode (fib_protocol_t fproto,
45                          u32 table_id,
46                          u32 *fib_index)
47 {
48     *fib_index = fib_table_find(fproto, table_id);
49
50     if (INDEX_INVALID == *fib_index)
51     {
52         return VNET_API_ERROR_NO_SUCH_FIB;
53     }
54
55     return (0);
56 }
57
58 int
59 fib_api_mtable_id_decode (fib_protocol_t fproto,
60                           u32 table_id,
61                           u32 *fib_index)
62 {
63     *fib_index = mfib_table_find(fproto, table_id);
64
65     if (~0 == *fib_index)
66     {
67         return VNET_API_ERROR_NO_SUCH_FIB;
68     }
69
70     return (0);
71 }
72
73 static void
74 fib_api_next_hop_decode (const vl_api_fib_path_t *in,
75                          ip46_address_t *out)
76 {
77     if (in->proto == FIB_API_PATH_NH_PROTO_IP4)
78         memcpy (&out->ip4, &in->nh.address.ip4, sizeof (out->ip4));
79     else if (in->proto == FIB_API_PATH_NH_PROTO_IP6)
80         memcpy (&out->ip6, &in->nh.address.ip6, sizeof (out->ip6));
81 }
82
83 static vl_api_fib_path_nh_proto_t
84 fib_api_path_dpo_proto_to_nh (dpo_proto_t dproto)
85 {
86     switch (dproto)
87     {
88     case DPO_PROTO_IP4:
89         return (FIB_API_PATH_NH_PROTO_IP4);
90     case DPO_PROTO_IP6:
91         return (FIB_API_PATH_NH_PROTO_IP6);
92     case DPO_PROTO_MPLS:
93         return (FIB_API_PATH_NH_PROTO_MPLS);
94     case DPO_PROTO_BIER:
95         return (FIB_API_PATH_NH_PROTO_BIER);
96     case DPO_PROTO_ETHERNET:
97         return (FIB_API_PATH_NH_PROTO_ETHERNET);
98     case DPO_PROTO_NSH:
99         ASSERT(0);
100         break;
101     }
102     return (FIB_API_PATH_NH_PROTO_IP4);
103 }
104
105
106 static void
107 fib_api_next_hop_encode (const fib_route_path_t *rpath,
108                          vl_api_fib_path_t *fp)
109 {
110     fp->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
111
112     if (rpath->frp_proto == DPO_PROTO_IP4)
113         memcpy (&fp->nh.address.ip4,
114                 &rpath->frp_addr.ip4,
115                 sizeof (rpath->frp_addr.ip4));
116     else if (rpath->frp_proto == DPO_PROTO_IP6)
117         memcpy (&fp->nh.address.ip6,
118                 &rpath->frp_addr.ip6,
119                 sizeof (rpath->frp_addr.ip6));
120 }
121
122 static int
123 fib_api_path_nh_proto_to_dpo (vl_api_fib_path_nh_proto_t pp,
124                               dpo_proto_t *dproto)
125 {
126     switch (pp)
127     {
128     case FIB_API_PATH_NH_PROTO_IP4:
129         *dproto = DPO_PROTO_IP4;
130         break;
131     case FIB_API_PATH_NH_PROTO_IP6:
132         *dproto = DPO_PROTO_IP6;
133         break;
134     case FIB_API_PATH_NH_PROTO_MPLS:
135         *dproto = DPO_PROTO_MPLS;
136         break;
137     case FIB_API_PATH_NH_PROTO_BIER:
138         *dproto = DPO_PROTO_BIER;
139         break;
140     case FIB_API_PATH_NH_PROTO_ETHERNET:
141         *dproto = DPO_PROTO_ETHERNET;
142         break;
143     default:
144         return (-1);
145     }
146     return (0);
147 }
148
149 int
150 fib_api_path_decode (vl_api_fib_path_t *in,
151                      fib_route_path_t *out)
152 {
153     vnet_classify_main_t *cm = &vnet_classify_main;
154     int rv = 0, n_labels;
155     vnet_main_t *vnm;
156     u8 ii;
157
158     vnm = vnet_get_main ();
159     clib_memset(&out->frp_dpo, 0, sizeof(out->frp_dpo));
160
161     /* enums are u32 */
162     in->flags = ntohl (in->flags);
163     in->type = ntohl (in->type);
164     in->proto = ntohl (in->proto);
165
166     /*
167      * attributes that apply to all path types
168      */
169     out->frp_flags = 0;
170     out->frp_weight = in->weight;
171     if (0 == out->frp_weight)
172     {
173         out->frp_weight = 1;
174     }
175     out->frp_preference = in->preference;
176
177     rv = fib_api_path_nh_proto_to_dpo(in->proto, &out->frp_proto);
178
179     if (0 != rv)
180         return (rv);
181
182     /*
183      * convert the flags and the AFI to determine the path type
184      */
185     if (in->flags & FIB_API_PATH_FLAG_RESOLVE_VIA_HOST)
186         out->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
187     if (in->flags & FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED)
188         out->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
189     if (in->flags & FIB_API_PATH_FLAG_POP_PW_CW)
190         out->frp_flags |= FIB_ROUTE_PATH_POP_PW_CW;
191
192     switch (in->type)
193     {
194     case FIB_API_PATH_TYPE_DVR:
195         out->frp_sw_if_index = ntohl(in->sw_if_index);
196         out->frp_flags |= FIB_ROUTE_PATH_DVR;
197         break;
198     case FIB_API_PATH_TYPE_INTERFACE_RX:
199         out->frp_sw_if_index = ntohl(in->sw_if_index);
200         out->frp_flags |= FIB_ROUTE_PATH_INTF_RX;
201         break;
202     case FIB_API_PATH_TYPE_DROP:
203         out->frp_flags |= FIB_ROUTE_PATH_DROP;
204         break;
205     case FIB_API_PATH_TYPE_LOCAL:
206         out->frp_flags |= FIB_ROUTE_PATH_LOCAL;
207         out->frp_sw_if_index = ntohl(in->sw_if_index);
208         break;
209     case FIB_API_PATH_TYPE_ICMP_UNREACH:
210         out->frp_flags |= FIB_ROUTE_PATH_ICMP_UNREACH;
211         break;
212     case FIB_API_PATH_TYPE_ICMP_PROHIBIT:
213         out->frp_flags |= FIB_ROUTE_PATH_ICMP_PROHIBIT;
214         break;
215     case FIB_API_PATH_TYPE_CLASSIFY:
216         out->frp_flags |= FIB_ROUTE_PATH_CLASSIFY;
217         
218         if (pool_is_free_index (cm->tables, ntohl (in->nh.classify_table_index)))
219         {
220             return VNET_API_ERROR_NO_SUCH_TABLE;
221         }
222         out->frp_classify_table_id = ntohl (in->nh.classify_table_index);
223         break;
224     case FIB_API_PATH_TYPE_UDP_ENCAP:
225         out->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
226         out->frp_udp_encap_id = ntohl(in->nh.obj_id);
227         break;
228     case FIB_API_PATH_TYPE_BIER_IMP:
229         out->frp_flags |= FIB_ROUTE_PATH_BIER_IMP;
230         out->frp_bier_imp = ntohl (in->nh.obj_id);
231         break;
232
233     case FIB_API_PATH_TYPE_SOURCE_LOOKUP:
234         out->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
235         /* fall through */
236     case FIB_API_PATH_TYPE_NORMAL:
237         switch (out->frp_proto)
238         {
239         case DPO_PROTO_IP4:
240         case DPO_PROTO_IP6:
241             fib_api_next_hop_decode(in, &out->frp_addr);
242             out->frp_sw_if_index = ntohl(in->sw_if_index);
243             out->frp_rpf_id = ntohl(in->rpf_id);
244
245             if (0 == out->frp_rpf_id)
246             {
247                 /* allow 0 to be an unset value on the API */
248                 out->frp_rpf_id = ~0;
249             }
250
251             if (~0 != out->frp_rpf_id)
252             {
253                 out->frp_flags |= FIB_ROUTE_PATH_RPF_ID;
254             }
255
256             if (~0 == out->frp_sw_if_index)
257             {
258                 /* recursive or deag, validate the next-hop FIB */
259                 if (~0 != out->frp_rpf_id)
260                 {
261                     rv = fib_api_mtable_id_decode(
262                         dpo_proto_to_fib(out->frp_proto),
263                         ntohl(in->table_id),
264                         &out->frp_fib_index);
265                 }
266                 else
267                 {
268                     rv = fib_api_table_id_decode(
269                         dpo_proto_to_fib(out->frp_proto),
270                         ntohl(in->table_id),
271                         &out->frp_fib_index);
272                 }
273                 if (0 != rv)
274                 {
275                     return (rv);
276                 }
277             }
278             else
279             {
280                 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
281                                         out->frp_sw_if_index))
282                 {
283                     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
284                 }
285             }
286
287             if (ip46_address_is_zero(&out->frp_addr))
288             {
289                 if (~0 == out->frp_sw_if_index &&
290                     ~0 != out->frp_fib_index)
291                 {
292                     out->frp_flags |= FIB_ROUTE_PATH_DEAG;
293                 }
294             }
295
296             break;
297         case DPO_PROTO_MPLS:
298             out->frp_local_label = ntohl (in->nh.via_label);
299             out->frp_eos = MPLS_NON_EOS;
300             out->frp_sw_if_index = ~0;
301             break;
302         case DPO_PROTO_BIER:
303             out->frp_sw_if_index = ntohl(in->sw_if_index);
304             out->frp_rpf_id = ntohl(in->rpf_id);
305
306             if (!(out->frp_flags & FIB_ROUTE_PATH_BIER_IMP))
307             {
308                 fib_api_next_hop_decode(in, &out->frp_addr);
309
310                 if (ip46_address_is_zero(&out->frp_addr))
311                 {
312                     index_t bdti;
313
314                     bdti = bier_disp_table_find(ntohl(in->table_id));
315
316                     if (INDEX_INVALID != bdti)
317                     {
318                         out->frp_fib_index = bdti;
319                     }
320                     else
321                     {
322                         return (VNET_API_ERROR_NO_SUCH_FIB);
323                     }
324                 }
325             }
326             break;
327         case DPO_PROTO_ETHERNET:
328             out->frp_sw_if_index = ntohl(in->sw_if_index);
329             break;
330         case DPO_PROTO_NSH:
331             break;
332         }
333     }
334
335     n_labels = in->n_labels;
336     if (n_labels != 0)
337     {
338         vec_validate (out->frp_label_stack, n_labels - 1);
339         for (ii = 0; ii < n_labels; ii++)
340         {
341             out->frp_label_stack[ii].fml_value =
342                 ntohl(in->label_stack[ii].label);
343             out->frp_label_stack[ii].fml_ttl =
344                 in->label_stack[ii].ttl;
345             out->frp_label_stack[ii].fml_exp =
346                 in->label_stack[ii].exp;
347             out->frp_label_stack[ii].fml_mode =
348                 (in->label_stack[ii].is_uniform ?
349                  FIB_MPLS_LSP_MODE_UNIFORM :
350                  FIB_MPLS_LSP_MODE_PIPE);
351         }
352     }
353
354     return (0);
355 }
356
357 void
358 fib_api_path_encode (const fib_route_path_t * rpath,
359                      vl_api_fib_path_t *out)
360 {
361     memset (out, 0, sizeof (*out));
362
363     out->weight = rpath->frp_weight;
364     out->preference = rpath->frp_preference;
365     out->sw_if_index = htonl (rpath->frp_sw_if_index);
366     out->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
367     out->rpf_id = rpath->frp_rpf_id;
368     fib_api_next_hop_encode (rpath, out);
369
370     if (0 != rpath->frp_fib_index)
371     {
372         if ((DPO_PROTO_IP6 == rpath->frp_proto) ||
373             (DPO_PROTO_IP4 == rpath->frp_proto))
374         {
375             if (rpath->frp_flags & FIB_ROUTE_PATH_RPF_ID)
376             {
377                 out->table_id = htonl (mfib_table_get_table_id(
378                                            rpath->frp_fib_index,
379                                            dpo_proto_to_fib(rpath->frp_proto)));
380             }
381             else
382             {
383                 out->table_id = htonl (fib_table_get_table_id(
384                                            rpath->frp_fib_index,
385                                            dpo_proto_to_fib(rpath->frp_proto)));
386             }
387         }
388     }
389
390     if (rpath->frp_flags & FIB_ROUTE_PATH_DVR)
391     {
392         out->type = FIB_API_PATH_TYPE_DVR;
393     }
394     else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_UNREACH)
395     {
396         out->type = FIB_API_PATH_TYPE_ICMP_UNREACH;
397     }
398     else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_PROHIBIT)
399     {
400         out->type = FIB_API_PATH_TYPE_ICMP_PROHIBIT;
401     }
402     else if (rpath->frp_flags & FIB_ROUTE_PATH_LOCAL)
403     {
404         out->type = FIB_API_PATH_TYPE_LOCAL;
405     }
406     else if (rpath->frp_flags & FIB_ROUTE_PATH_DROP)
407     {
408         out->type = FIB_API_PATH_TYPE_DROP;
409     }
410     else if (rpath->frp_flags & FIB_ROUTE_PATH_UDP_ENCAP)
411     {
412         out->type = FIB_API_PATH_TYPE_UDP_ENCAP;
413         out->nh.obj_id = rpath->frp_udp_encap_id;
414     }
415     else if (rpath->frp_flags & FIB_ROUTE_PATH_BIER_IMP)
416     {
417         out->type = FIB_API_PATH_TYPE_BIER_IMP;
418         out->nh.obj_id = rpath->frp_bier_imp;
419     }
420     else
421     {
422         out->type = FIB_API_PATH_TYPE_NORMAL;
423     }
424     if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_HOST)
425     {
426         out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_HOST;
427     }
428     if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED)
429     {
430         out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED;
431     }
432
433     out->flags = htonl (out->flags);
434     out->type = htonl (out->type);
435     out->proto = htonl (out->proto);
436
437     if (rpath->frp_label_stack)
438     {
439         int ii;
440
441         for (ii = 0; ii < vec_len(rpath->frp_label_stack); ii++)
442         {
443             out->label_stack[ii].label =
444                 htonl(rpath->frp_label_stack[ii].fml_value);
445             out->label_stack[ii].ttl =
446                 rpath->frp_label_stack[ii].fml_ttl;
447             out->label_stack[ii].exp =
448                 rpath->frp_label_stack[ii].fml_exp;
449         }
450         out->n_labels = ii;
451     }
452 }
453
454 int
455 fib_api_route_add_del (u8 is_add,
456                        u8 is_multipath,
457                        u32 fib_index,
458                        const fib_prefix_t * prefix,
459                        fib_entry_flag_t entry_flags,
460                        fib_route_path_t *rpaths)
461 {
462     if (is_multipath)
463     {
464         if (vec_len(rpaths) == 0)
465             return (VNET_API_ERROR_NO_PATHS_IN_ROUTE);
466
467         /* Iterative path add/remove */
468         if (is_add)
469             fib_table_entry_path_add2 (fib_index,
470                                        prefix,
471                                        FIB_SOURCE_API,
472                                        entry_flags,
473                                        rpaths);
474         else
475             fib_table_entry_path_remove2 (fib_index,
476                                           prefix,
477                                           FIB_SOURCE_API,
478                                           rpaths);
479     }
480     else
481     {
482         if (is_add)
483         {
484             if (vec_len(rpaths) == 0)
485                 return (VNET_API_ERROR_NO_PATHS_IN_ROUTE);
486
487             /* path replacement */
488             fib_table_entry_update (fib_index,
489                                     prefix,
490                                     FIB_SOURCE_API,
491                                     entry_flags,
492                                     rpaths);
493         }
494         else
495             /* entry delete */
496             fib_table_entry_delete (fib_index,
497                                     prefix,
498                                     FIB_SOURCE_API);
499     }
500
501     return (0);
502 }
503
504 u8*
505 format_vl_api_fib_path (u8 * s, va_list * args)
506 {
507     const vl_api_fib_path_t *path = va_arg (*args, vl_api_fib_path_t*);
508
509     s = format (s, "sw_if_index %d", ntohl (path->sw_if_index));
510     switch (clib_net_to_host_u32(path->proto))
511     {
512     case FIB_API_PATH_NH_PROTO_IP4:
513         s = format (s, " %U", format_vl_api_address_union,
514                     &path->nh.address, ADDRESS_IP4);
515         break;
516     case FIB_API_PATH_NH_PROTO_IP6:
517         s = format (s, " %U", format_vl_api_address_union,
518                     &path->nh.address, ADDRESS_IP6);
519         break;
520     default:
521         break;
522     }
523     s = format (s, " weight %d", path->weight);
524     s = format (s, " preference %d", path->preference);
525     s = format (s, " type %d", ntohl(path->type));
526     s = format (s, " proto %d", ntohl(path->proto));
527     s = format (s, " flags %d", ntohl(path->flags));
528     s = format (s, " n_labels %d", ntohl(path->n_labels));
529     s = format (s, " table-id %d", ntohl(path->table_id));
530     s = format (s, " rpf-id %d", ntohl(path->rpf_id));
531
532     return (s);
533 }
534
535 fib_protocol_t
536 fib_proto_from_api_address_family (int af)
537 {
538     switch (clib_net_to_host_u32 (af))
539     {
540     case ADDRESS_IP4:
541         return (FIB_PROTOCOL_IP4);
542     case ADDRESS_IP6:
543         return (FIB_PROTOCOL_IP6);
544     }
545
546     ASSERT(0);
547     return (FIB_PROTOCOL_IP4);
548 }
549
550 int
551 fib_proto_to_api_address_family (fib_protocol_t fproto)
552 {
553     switch (fproto)
554     {
555     case FIB_PROTOCOL_IP4:
556         return (clib_net_to_host_u32 (ADDRESS_IP4));
557     case FIB_PROTOCOL_IP6:
558         return (clib_net_to_host_u32 (ADDRESS_IP6));
559     default:
560         break;
561     }
562
563     ASSERT(0);
564     return (clib_net_to_host_u32 (ADDRESS_IP4));
565 }