fib: fib api updates
[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
190     switch (in->type)
191     {
192     case FIB_API_PATH_TYPE_DVR:
193         out->frp_sw_if_index = ntohl(in->sw_if_index);
194         out->frp_flags |= FIB_ROUTE_PATH_DVR;
195         break;
196     case FIB_API_PATH_TYPE_INTERFACE_RX:
197         out->frp_sw_if_index = ntohl(in->sw_if_index);
198         out->frp_flags |= FIB_ROUTE_PATH_INTF_RX;
199         break;
200     case FIB_API_PATH_TYPE_DROP:
201         out->frp_flags |= FIB_ROUTE_PATH_DROP;
202         break;
203     case FIB_API_PATH_TYPE_LOCAL:
204         out->frp_flags |= FIB_ROUTE_PATH_LOCAL;
205         out->frp_sw_if_index = ntohl(in->sw_if_index);
206         break;
207     case FIB_API_PATH_TYPE_ICMP_UNREACH:
208         out->frp_flags |= FIB_ROUTE_PATH_ICMP_UNREACH;
209         break;
210     case FIB_API_PATH_TYPE_ICMP_PROHIBIT:
211         out->frp_flags |= FIB_ROUTE_PATH_ICMP_PROHIBIT;
212         break;
213     case FIB_API_PATH_TYPE_CLASSIFY:
214         out->frp_flags |= FIB_ROUTE_PATH_CLASSIFY;
215         
216         if (pool_is_free_index (cm->tables, ntohl (in->nh.classify_table_index)))
217         {
218             return VNET_API_ERROR_NO_SUCH_TABLE;
219         }
220         out->frp_classify_table_id = ntohl (in->nh.classify_table_index);
221         break;
222     case FIB_API_PATH_TYPE_UDP_ENCAP:
223         out->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
224         out->frp_udp_encap_id = ntohl(in->nh.obj_id);
225         break;
226     case FIB_API_PATH_TYPE_BIER_IMP:
227         out->frp_flags |= FIB_ROUTE_PATH_BIER_IMP;
228         out->frp_bier_imp = ntohl (in->nh.obj_id);
229         break;
230
231     case FIB_API_PATH_TYPE_SOURCE_LOOKUP:
232         out->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
233         /* fall through */
234     case FIB_API_PATH_TYPE_NORMAL:
235         switch (out->frp_proto)
236         {
237         case DPO_PROTO_IP4:
238         case DPO_PROTO_IP6:
239             fib_api_next_hop_decode(in, &out->frp_addr);
240             out->frp_sw_if_index = ntohl(in->sw_if_index);
241             out->frp_rpf_id = ntohl(in->rpf_id);
242
243             if (0 == out->frp_rpf_id)
244             {
245                 /* allow 0 to be an unset value on the API */
246                 out->frp_rpf_id = ~0;
247             }
248
249             if (~0 != out->frp_rpf_id)
250             {
251                 out->frp_flags |= FIB_ROUTE_PATH_RPF_ID;
252             }
253
254             if (~0 == out->frp_sw_if_index)
255             {
256                 /* recursive or deag, validate the next-hop FIB */
257                 if (~0 != out->frp_rpf_id)
258                 {
259                     rv = fib_api_mtable_id_decode(
260                         dpo_proto_to_fib(out->frp_proto),
261                         ntohl(in->table_id),
262                         &out->frp_fib_index);
263                 }
264                 else
265                 {
266                     rv = fib_api_table_id_decode(
267                         dpo_proto_to_fib(out->frp_proto),
268                         ntohl(in->table_id),
269                         &out->frp_fib_index);
270                 }
271                 if (0 != rv)
272                 {
273                     return (rv);
274                 }
275             }
276             else
277             {
278                 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
279                                         out->frp_sw_if_index))
280                 {
281                     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
282                 }
283             }
284
285             if (ip46_address_is_zero(&out->frp_addr))
286             {
287                 if (~0 == out->frp_sw_if_index &&
288                     ~0 != out->frp_fib_index)
289                 {
290                     out->frp_flags |= FIB_ROUTE_PATH_DEAG;
291                 }
292             }
293
294             break;
295         case DPO_PROTO_MPLS:
296             out->frp_local_label = ntohl (in->nh.via_label);
297             out->frp_eos = MPLS_NON_EOS;
298             out->frp_sw_if_index = ~0;
299             break;
300         case DPO_PROTO_BIER:
301             out->frp_sw_if_index = ntohl(in->sw_if_index);
302             out->frp_rpf_id = ntohl(in->rpf_id);
303
304             if (!(out->frp_flags & FIB_ROUTE_PATH_BIER_IMP))
305             {
306                 fib_api_next_hop_decode(in, &out->frp_addr);
307
308                 if (ip46_address_is_zero(&out->frp_addr))
309                 {
310                     index_t bdti;
311
312                     bdti = bier_disp_table_find(ntohl(in->table_id));
313
314                     if (INDEX_INVALID != bdti)
315                     {
316                         out->frp_fib_index = bdti;
317                     }
318                     else
319                     {
320                         return (VNET_API_ERROR_NO_SUCH_FIB);
321                     }
322                 }
323             }
324             break;
325         case DPO_PROTO_ETHERNET:
326             out->frp_sw_if_index = ntohl(in->sw_if_index);
327             break;
328         case DPO_PROTO_NSH:
329             break;
330         }
331     }
332
333     n_labels = in->n_labels;
334     if (n_labels != 0)
335     {
336         vec_validate (out->frp_label_stack, n_labels - 1);
337         for (ii = 0; ii < n_labels; ii++)
338         {
339             out->frp_label_stack[ii].fml_value =
340                 ntohl(in->label_stack[ii].label);
341             out->frp_label_stack[ii].fml_ttl =
342                 in->label_stack[ii].ttl;
343             out->frp_label_stack[ii].fml_exp =
344                 in->label_stack[ii].exp;
345             out->frp_label_stack[ii].fml_mode =
346                 (in->label_stack[ii].is_uniform ?
347                  FIB_MPLS_LSP_MODE_UNIFORM :
348                  FIB_MPLS_LSP_MODE_PIPE);
349         }
350     }
351
352     return (0);
353 }
354
355 void
356 fib_api_path_encode (const fib_route_path_t * rpath,
357                      vl_api_fib_path_t *out)
358 {
359     memset (out, 0, sizeof (*out));
360
361     out->weight = rpath->frp_weight;
362     out->preference = rpath->frp_preference;
363     out->sw_if_index = htonl (rpath->frp_sw_if_index);
364     out->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
365     out->rpf_id = rpath->frp_rpf_id;
366     fib_api_next_hop_encode (rpath, out);
367
368     if (0 != rpath->frp_fib_index)
369     {
370         if ((DPO_PROTO_IP6 == rpath->frp_proto) ||
371             (DPO_PROTO_IP4 == rpath->frp_proto))
372         {
373             if (rpath->frp_flags & FIB_ROUTE_PATH_RPF_ID)
374             {
375                 out->table_id = htonl (mfib_table_get_table_id(
376                                            rpath->frp_fib_index,
377                                            dpo_proto_to_fib(rpath->frp_proto)));
378             }
379             else
380             {
381                 out->table_id = htonl (fib_table_get_table_id(
382                                            rpath->frp_fib_index,
383                                            dpo_proto_to_fib(rpath->frp_proto)));
384             }
385         }
386     }
387
388     if (rpath->frp_flags & FIB_ROUTE_PATH_DVR)
389     {
390         out->type = FIB_API_PATH_TYPE_DVR;
391     }
392     else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_UNREACH)
393     {
394         out->type = FIB_API_PATH_TYPE_ICMP_UNREACH;
395     }
396     else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_PROHIBIT)
397     {
398         out->type = FIB_API_PATH_TYPE_ICMP_PROHIBIT;
399     }
400     else if (rpath->frp_flags & FIB_ROUTE_PATH_LOCAL)
401     {
402         out->type = FIB_API_PATH_TYPE_LOCAL;
403     }
404     else if (rpath->frp_flags & FIB_ROUTE_PATH_DROP)
405     {
406         out->type = FIB_API_PATH_TYPE_DROP;
407     }
408     else if (rpath->frp_flags & FIB_ROUTE_PATH_UDP_ENCAP)
409     {
410         out->type = FIB_API_PATH_TYPE_UDP_ENCAP;
411         out->nh.obj_id = rpath->frp_udp_encap_id;
412     }
413     else if (rpath->frp_flags & FIB_ROUTE_PATH_BIER_IMP)
414     {
415         out->type = FIB_API_PATH_TYPE_BIER_IMP;
416         out->nh.obj_id = rpath->frp_bier_imp;
417     }
418     else
419     {
420         out->type = FIB_API_PATH_TYPE_NORMAL;
421     }
422     if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_HOST)
423     {
424         out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_HOST;
425     }
426     if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED)
427     {
428         out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED;
429     }
430
431     out->flags = htonl (out->flags);
432     out->type = htonl (out->type);
433     out->proto = htonl (out->proto);
434
435     if (rpath->frp_label_stack)
436     {
437         int ii;
438
439         for (ii = 0; ii < vec_len(rpath->frp_label_stack); ii++)
440         {
441             out->label_stack[ii].label =
442                 htonl(rpath->frp_label_stack[ii].fml_value);
443             out->label_stack[ii].ttl =
444                 rpath->frp_label_stack[ii].fml_ttl;
445             out->label_stack[ii].exp =
446                 rpath->frp_label_stack[ii].fml_exp;
447         }
448         out->n_labels = ii;
449     }
450 }
451
452 void
453 fib_api_route_add_del (u8 is_add,
454                        u8 is_multipath,
455                        u32 fib_index,
456                        const fib_prefix_t * prefix,
457                        fib_entry_flag_t entry_flags,
458                        fib_route_path_t *rpaths)
459 {
460   if (is_multipath)
461     {
462       /* Iterative path add/remove */
463       if (is_add)
464         fib_table_entry_path_add2 (fib_index,
465                                    prefix,
466                                    FIB_SOURCE_API,
467                                    entry_flags,
468                                    rpaths);
469       else
470         fib_table_entry_path_remove2 (fib_index,
471                                       prefix,
472                                       FIB_SOURCE_API,
473                                       rpaths);
474     }
475   else
476     {
477       if (is_add)
478         /* path replacement */
479         fib_table_entry_update (fib_index,
480                                 prefix,
481                                 FIB_SOURCE_API,
482                                 entry_flags,
483                                 rpaths);
484       else
485         /* entry delete */
486         fib_table_entry_delete (fib_index,
487                                 prefix,
488                                 FIB_SOURCE_API);
489     }
490 }
491
492 u8*
493 format_vl_api_fib_path (u8 * s, va_list * args)
494 {
495     const vl_api_fib_path_t *path = va_arg (*args, vl_api_fib_path_t*);
496
497     s = format (s, "sw_if_index %d", ntohl (path->sw_if_index));
498     switch (clib_net_to_host_u32(path->proto))
499     {
500     case FIB_API_PATH_NH_PROTO_IP4:
501         s = format (s, " %U", format_vl_api_address_union,
502                     &path->nh.address, ADDRESS_IP4);
503         break;
504     case FIB_API_PATH_NH_PROTO_IP6:
505         s = format (s, " %U", format_vl_api_address_union,
506                     &path->nh.address, ADDRESS_IP6);
507         break;
508     default:
509         break;
510     }
511     s = format (s, " weight %d", path->weight);
512     s = format (s, " preference %d", path->preference);
513     s = format (s, " type %d", ntohl(path->type));
514     s = format (s, " proto %d", ntohl(path->proto));
515     s = format (s, " flags %d", ntohl(path->flags));
516     s = format (s, " n_labels %d", ntohl(path->n_labels));
517     s = format (s, " table-id %d", ntohl(path->table_id));
518     s = format (s, " rpf-id %d", ntohl(path->rpf_id));
519
520     return (s);
521 }
522
523 fib_protocol_t
524 fib_proto_from_api_address_family (int af)
525 {
526     switch (clib_net_to_host_u32 (af))
527     {
528     case ADDRESS_IP4:
529         return (FIB_PROTOCOL_IP4);
530     case ADDRESS_IP6:
531         return (FIB_PROTOCOL_IP6);
532     }
533
534     ASSERT(0);
535     return (FIB_PROTOCOL_IP4);
536 }
537
538 int
539 fib_proto_to_api_address_family (fib_protocol_t fproto)
540 {
541     switch (fproto)
542     {
543     case FIB_PROTOCOL_IP4:
544         return (clib_net_to_host_u32 (ADDRESS_IP4));
545     case FIB_PROTOCOL_IP6:
546         return (clib_net_to_host_u32 (ADDRESS_IP6));
547     default:
548         break;
549     }
550
551     ASSERT(0);
552     return (clib_net_to_host_u32 (ADDRESS_IP4));
553 }