426161b067065fa5a7d50e0716959b29fd20767a
[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 #include <vnet/classify/vnet_classify.h>
25 #include <vnet/ip/ip_format_fns.h>
26
27 #include <vnet/fib/fib.api_enum.h>
28 #include <vnet/fib/fib.api_types.h>
29
30 static u16 fib_base_msg_id;
31 #define REPLY_MSG_ID_BASE fib_base_msg_id
32 #include <vlibapi/api_helper_macros.h>
33
34 int
35 fib_api_table_id_decode (fib_protocol_t fproto,
36                          u32 table_id,
37                          u32 *fib_index)
38 {
39     *fib_index = fib_table_find(fproto, table_id);
40
41     if (INDEX_INVALID == *fib_index)
42     {
43         return VNET_API_ERROR_NO_SUCH_FIB;
44     }
45
46     return (0);
47 }
48
49 int
50 fib_api_mtable_id_decode (fib_protocol_t fproto,
51                           u32 table_id,
52                           u32 *fib_index)
53 {
54     *fib_index = mfib_table_find(fproto, table_id);
55
56     if (~0 == *fib_index)
57     {
58         return VNET_API_ERROR_NO_SUCH_FIB;
59     }
60
61     return (0);
62 }
63
64 static void
65 fib_api_next_hop_decode (const vl_api_fib_path_t *in,
66                          ip46_address_t *out)
67 {
68     ASSERT (FIB_API_PATH_NH_PROTO_IP4 == in->proto || FIB_API_PATH_NH_PROTO_IP6 == in->proto);
69     *out = to_ip46 (FIB_API_PATH_NH_PROTO_IP6 == in->proto, (void *)&in->nh.address);
70 }
71
72 vl_api_fib_path_nh_proto_t
73 fib_api_path_dpo_proto_to_nh (dpo_proto_t dproto)
74 {
75     switch (dproto)
76     {
77     case DPO_PROTO_IP4:
78         return (FIB_API_PATH_NH_PROTO_IP4);
79     case DPO_PROTO_IP6:
80         return (FIB_API_PATH_NH_PROTO_IP6);
81     case DPO_PROTO_MPLS:
82         return (FIB_API_PATH_NH_PROTO_MPLS);
83     case DPO_PROTO_BIER:
84         return (FIB_API_PATH_NH_PROTO_BIER);
85     case DPO_PROTO_ETHERNET:
86         return (FIB_API_PATH_NH_PROTO_ETHERNET);
87     case DPO_PROTO_NSH:
88         ASSERT(0);
89         break;
90     }
91     return (FIB_API_PATH_NH_PROTO_IP4);
92 }
93
94
95 static void
96 fib_api_next_hop_encode (const fib_route_path_t *rpath,
97                          vl_api_fib_path_t *fp)
98 {
99     fp->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
100
101     if (rpath->frp_proto == DPO_PROTO_IP4)
102         clib_memcpy (&fp->nh.address.ip4,
103                 &rpath->frp_addr.ip4,
104                 sizeof (rpath->frp_addr.ip4));
105     else if (rpath->frp_proto == DPO_PROTO_IP6)
106         clib_memcpy (&fp->nh.address.ip6,
107                 &rpath->frp_addr.ip6,
108                 sizeof (rpath->frp_addr.ip6));
109 }
110
111 int
112 fib_api_path_nh_proto_to_dpo (vl_api_fib_path_nh_proto_t pp,
113                               dpo_proto_t *dproto)
114 {
115     switch (pp)
116     {
117     case FIB_API_PATH_NH_PROTO_IP4:
118         *dproto = DPO_PROTO_IP4;
119         break;
120     case FIB_API_PATH_NH_PROTO_IP6:
121         *dproto = DPO_PROTO_IP6;
122         break;
123     case FIB_API_PATH_NH_PROTO_MPLS:
124         *dproto = DPO_PROTO_MPLS;
125         break;
126     case FIB_API_PATH_NH_PROTO_BIER:
127         *dproto = DPO_PROTO_BIER;
128         break;
129     case FIB_API_PATH_NH_PROTO_ETHERNET:
130         *dproto = DPO_PROTO_ETHERNET;
131         break;
132     default:
133         return (-1);
134     }
135     return (0);
136 }
137
138 int
139 fib_api_path_decode (vl_api_fib_path_t *in,
140                      fib_route_path_t *out)
141 {
142     vnet_classify_main_t *cm = &vnet_classify_main;
143     int rv = 0, n_labels;
144     vnet_main_t *vnm;
145     u8 ii;
146
147     vnm = vnet_get_main ();
148     clib_memset(&out->frp_dpo, 0, sizeof(out->frp_dpo));
149
150     /* enums are u32 */
151     in->flags = ntohl (in->flags);
152     in->type = ntohl (in->type);
153     in->proto = ntohl (in->proto);
154
155     /*
156      * attributes that apply to all path types
157      */
158     out->frp_flags = 0;
159     out->frp_weight = in->weight;
160     if (0 == out->frp_weight)
161     {
162         out->frp_weight = 1;
163     }
164     out->frp_preference = in->preference;
165
166     rv = fib_api_path_nh_proto_to_dpo(in->proto, &out->frp_proto);
167
168     if (0 != rv)
169         return (rv);
170
171     /*
172      * convert the flags and the AFI to determine the path type
173      */
174     if (in->flags & FIB_API_PATH_FLAG_RESOLVE_VIA_HOST)
175         out->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
176     if (in->flags & FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED)
177         out->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
178     if (in->flags & FIB_API_PATH_FLAG_POP_PW_CW)
179         out->frp_flags |= FIB_ROUTE_PATH_POP_PW_CW;
180
181     switch (in->type)
182     {
183     case FIB_API_PATH_TYPE_DVR:
184         out->frp_sw_if_index = ntohl(in->sw_if_index);
185         out->frp_flags |= FIB_ROUTE_PATH_DVR;
186         break;
187     case FIB_API_PATH_TYPE_INTERFACE_RX:
188         out->frp_sw_if_index = ntohl(in->sw_if_index);
189         out->frp_flags |= FIB_ROUTE_PATH_INTF_RX;
190         break;
191     case FIB_API_PATH_TYPE_DROP:
192         out->frp_flags |= FIB_ROUTE_PATH_DROP;
193         out->frp_sw_if_index = ntohl(in->sw_if_index);
194         break;
195     case FIB_API_PATH_TYPE_LOCAL:
196         out->frp_flags |= FIB_ROUTE_PATH_LOCAL;
197         out->frp_sw_if_index = ntohl(in->sw_if_index);
198         break;
199     case FIB_API_PATH_TYPE_ICMP_UNREACH:
200         out->frp_flags |= FIB_ROUTE_PATH_ICMP_UNREACH;
201         break;
202     case FIB_API_PATH_TYPE_ICMP_PROHIBIT:
203         out->frp_flags |= FIB_ROUTE_PATH_ICMP_PROHIBIT;
204         break;
205     case FIB_API_PATH_TYPE_CLASSIFY:
206         out->frp_flags |= FIB_ROUTE_PATH_CLASSIFY;
207
208         if (pool_is_free_index (cm->tables, ntohl (in->nh.classify_table_index)))
209         {
210             return VNET_API_ERROR_NO_SUCH_TABLE;
211         }
212         out->frp_classify_table_id = ntohl (in->nh.classify_table_index);
213         break;
214     case FIB_API_PATH_TYPE_UDP_ENCAP:
215         out->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
216         out->frp_udp_encap_id = ntohl(in->nh.obj_id);
217         break;
218     case FIB_API_PATH_TYPE_BIER_IMP:
219         out->frp_flags |= FIB_ROUTE_PATH_BIER_IMP;
220         out->frp_bier_imp = ntohl (in->nh.obj_id);
221         break;
222
223     case FIB_API_PATH_TYPE_SOURCE_LOOKUP:
224         out->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
225         /* fall through */
226     case FIB_API_PATH_TYPE_NORMAL:
227         switch (out->frp_proto)
228         {
229         case DPO_PROTO_IP4:
230         case DPO_PROTO_IP6:
231             fib_api_next_hop_decode(in, &out->frp_addr);
232             out->frp_sw_if_index = ntohl(in->sw_if_index);
233             out->frp_rpf_id = ntohl(in->rpf_id);
234
235             if (0 == out->frp_rpf_id)
236             {
237                 /* allow 0 to be an unset value on the API */
238                 out->frp_rpf_id = ~0;
239             }
240
241             if (~0 != out->frp_rpf_id)
242             {
243                 out->frp_flags |= FIB_ROUTE_PATH_RPF_ID;
244             }
245
246             if (~0 == out->frp_sw_if_index)
247             {
248                 /* recursive or deag, validate the next-hop FIB */
249                 if (~0 != out->frp_rpf_id)
250                 {
251                     rv = fib_api_mtable_id_decode(
252                         dpo_proto_to_fib(out->frp_proto),
253                         ntohl(in->table_id),
254                         &out->frp_fib_index);
255                 }
256                 else
257                 {
258                     rv = fib_api_table_id_decode(
259                         dpo_proto_to_fib(out->frp_proto),
260                         ntohl(in->table_id),
261                         &out->frp_fib_index);
262                 }
263                 if (0 != rv)
264                 {
265                     return (rv);
266                 }
267             }
268             else
269             {
270                 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
271                                         out->frp_sw_if_index))
272                 {
273                     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
274                 }
275             }
276
277             if (ip46_address_is_zero(&out->frp_addr))
278             {
279                 if (~0 == out->frp_sw_if_index &&
280                     ~0 != out->frp_fib_index)
281                 {
282                     out->frp_flags |= FIB_ROUTE_PATH_DEAG;
283                 }
284             }
285
286             break;
287         case DPO_PROTO_MPLS:
288             out->frp_local_label = ntohl (in->nh.via_label);
289             out->frp_eos = MPLS_NON_EOS;
290             out->frp_sw_if_index = ~0;
291             break;
292         case DPO_PROTO_BIER:
293             out->frp_sw_if_index = ntohl(in->sw_if_index);
294             out->frp_rpf_id = ntohl(in->rpf_id);
295
296             if (!(out->frp_flags & FIB_ROUTE_PATH_BIER_IMP))
297             {
298                 index_t bdti;
299
300                 bdti = bier_disp_table_find(ntohl(in->table_id));
301
302                 if (INDEX_INVALID != bdti)
303                 {
304                     out->frp_fib_index = bdti;
305                 }
306                 else
307                 {
308                     return (VNET_API_ERROR_NO_SUCH_FIB);
309                 }
310             }
311             break;
312         case DPO_PROTO_ETHERNET:
313             out->frp_sw_if_index = ntohl(in->sw_if_index);
314             break;
315         case DPO_PROTO_NSH:
316             break;
317         }
318     }
319
320     n_labels = in->n_labels;
321     if (n_labels != 0)
322     {
323         vec_validate (out->frp_label_stack, n_labels - 1);
324         for (ii = 0; ii < n_labels; ii++)
325         {
326             out->frp_label_stack[ii].fml_value =
327                 ntohl(in->label_stack[ii].label);
328             out->frp_label_stack[ii].fml_ttl =
329                 in->label_stack[ii].ttl;
330             out->frp_label_stack[ii].fml_exp =
331                 in->label_stack[ii].exp;
332             out->frp_label_stack[ii].fml_mode =
333                 (in->label_stack[ii].is_uniform ?
334                  FIB_MPLS_LSP_MODE_UNIFORM :
335                  FIB_MPLS_LSP_MODE_PIPE);
336         }
337     }
338
339     return (0);
340 }
341
342 void
343 fib_api_path_encode (const fib_route_path_t * rpath,
344                      vl_api_fib_path_t *out)
345 {
346     memset (out, 0, sizeof (*out));
347
348     out->weight = rpath->frp_weight;
349     out->preference = rpath->frp_preference;
350     out->sw_if_index = htonl (rpath->frp_sw_if_index);
351     out->proto = fib_api_path_dpo_proto_to_nh(rpath->frp_proto);
352     out->rpf_id = rpath->frp_rpf_id;
353     fib_api_next_hop_encode (rpath, out);
354
355     if (0 != rpath->frp_fib_index)
356     {
357         if ((DPO_PROTO_IP6 == rpath->frp_proto) ||
358             (DPO_PROTO_IP4 == rpath->frp_proto))
359         {
360             if (rpath->frp_flags & FIB_ROUTE_PATH_RPF_ID)
361             {
362                 out->table_id = htonl (mfib_table_get_table_id(
363                                            rpath->frp_fib_index,
364                                            dpo_proto_to_fib(rpath->frp_proto)));
365             }
366             else
367             {
368                 out->table_id = htonl (fib_table_get_table_id(
369                                            rpath->frp_fib_index,
370                                            dpo_proto_to_fib(rpath->frp_proto)));
371             }
372         }
373     }
374
375     if (rpath->frp_flags & FIB_ROUTE_PATH_DVR)
376     {
377         out->type = FIB_API_PATH_TYPE_DVR;
378     }
379     else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_UNREACH)
380     {
381         out->type = FIB_API_PATH_TYPE_ICMP_UNREACH;
382     }
383     else if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_PROHIBIT)
384     {
385         out->type = FIB_API_PATH_TYPE_ICMP_PROHIBIT;
386     }
387     else if (rpath->frp_flags & FIB_ROUTE_PATH_LOCAL)
388     {
389         out->type = FIB_API_PATH_TYPE_LOCAL;
390     }
391     else if (rpath->frp_flags & FIB_ROUTE_PATH_DROP)
392     {
393         out->type = FIB_API_PATH_TYPE_DROP;
394     }
395     else if (rpath->frp_flags & FIB_ROUTE_PATH_UDP_ENCAP)
396     {
397         out->type = FIB_API_PATH_TYPE_UDP_ENCAP;
398         out->nh.obj_id = rpath->frp_udp_encap_id;
399     }
400     else if (rpath->frp_flags & FIB_ROUTE_PATH_BIER_IMP)
401     {
402         out->type = FIB_API_PATH_TYPE_BIER_IMP;
403         out->nh.obj_id = rpath->frp_bier_imp;
404     }
405     else if (rpath->frp_flags & FIB_ROUTE_PATH_INTF_RX)
406     {
407         out->type = FIB_API_PATH_TYPE_INTERFACE_RX;
408     }
409     else
410     {
411         out->type = FIB_API_PATH_TYPE_NORMAL;
412     }
413     if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_HOST)
414     {
415         out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_HOST;
416     }
417     if (rpath->frp_flags & FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED)
418     {
419         out->flags |= FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED;
420     }
421
422     out->flags = htonl (out->flags);
423     out->type = htonl (out->type);
424     out->proto = htonl (out->proto);
425
426     if (rpath->frp_label_stack)
427     {
428         int ii;
429
430         for (ii = 0; ii < vec_len(rpath->frp_label_stack); ii++)
431         {
432             out->label_stack[ii].label =
433                 htonl(rpath->frp_label_stack[ii].fml_value);
434             out->label_stack[ii].ttl =
435                 rpath->frp_label_stack[ii].fml_ttl;
436             out->label_stack[ii].exp =
437                 rpath->frp_label_stack[ii].fml_exp;
438         }
439         out->n_labels = ii;
440     }
441 }
442
443 int
444 fib_api_route_add_del (u8 is_add,
445                        u8 is_multipath,
446                        u32 fib_index,
447                        const fib_prefix_t * prefix,
448                        fib_source_t src,
449                        fib_entry_flag_t entry_flags,
450                        fib_route_path_t *rpaths)
451 {
452     if (!fib_prefix_validate(prefix)) {
453           return (VNET_API_ERROR_INVALID_PREFIX_LENGTH);
454     }
455     if (is_multipath)
456     {
457         if (vec_len(rpaths) == 0)
458             return (VNET_API_ERROR_NO_PATHS_IN_ROUTE);
459
460         /* Iterative path add/remove */
461         if (is_add)
462             fib_table_entry_path_add2 (fib_index,
463                                        prefix,
464                                        src,
465                                        entry_flags,
466                                        rpaths);
467         else
468             fib_table_entry_path_remove2 (fib_index,
469                                           prefix,
470                                           src,
471                                           rpaths);
472     }
473     else
474     {
475         if (is_add)
476         {
477             if (vec_len(rpaths) == 0)
478                 return (VNET_API_ERROR_NO_PATHS_IN_ROUTE);
479
480             /* path replacement */
481             fib_table_entry_update (fib_index,
482                                     prefix,
483                                     src,
484                                     entry_flags,
485                                     rpaths);
486         }
487         else
488             /* entry delete */
489             fib_table_entry_delete (fib_index,
490                                     prefix,
491                                     src);
492     }
493
494     return (0);
495 }
496
497 u8 *
498 format_vl_api_address_union (u8 * s, va_list * args)
499 {
500   const vl_api_address_union_t *addr =
501     va_arg (*args, vl_api_address_union_t *);
502   vl_api_address_family_t af = va_arg (*args, int);
503
504   if (ADDRESS_IP6 == af)
505     s = format (s, "%U", format_ip6_address, addr->ip6);
506   else
507     s = format (s, "%U", format_ip4_address, addr->ip4);
508
509   return s;
510 }
511
512 u8*
513 format_vl_api_fib_path (u8 * s, va_list * args)
514 {
515     const vl_api_fib_path_t *path = va_arg (*args, vl_api_fib_path_t*);
516
517     s = format (s, "sw_if_index %d", ntohl (path->sw_if_index));
518     switch (clib_net_to_host_u32(path->proto))
519     {
520     case FIB_API_PATH_NH_PROTO_IP4:
521         s = format (s, " %U", format_vl_api_address_union,
522                     &path->nh.address, ADDRESS_IP4);
523         break;
524     case FIB_API_PATH_NH_PROTO_IP6:
525         s = format (s, " %U", format_vl_api_address_union,
526                     &path->nh.address, ADDRESS_IP6);
527         break;
528     default:
529         break;
530     }
531     s = format (s, " weight %d", path->weight);
532     s = format (s, " preference %d", path->preference);
533     s = format (s, " type %d", ntohl(path->type));
534     s = format (s, " proto %d", ntohl(path->proto));
535     s = format (s, " flags %d", ntohl(path->flags));
536     s = format (s, " n_labels %d", ntohl(path->n_labels));
537     s = format (s, " table-id %d", ntohl(path->table_id));
538     s = format (s, " rpf-id %d", ntohl(path->rpf_id));
539
540     return (s);
541 }
542
543 int
544 fib_proto_from_api_address_family (vl_api_address_family_t af, fib_protocol_t * out)
545 {
546     switch (af)
547     {
548     case ADDRESS_IP4:
549         *out = (FIB_PROTOCOL_IP4);
550         return (0);
551     case ADDRESS_IP6:
552         *out = (FIB_PROTOCOL_IP6);
553         return (0);
554     }
555
556     return (VNET_API_ERROR_INVALID_ADDRESS_FAMILY);
557 }
558
559 vl_api_address_family_t
560 fib_proto_to_api_address_family (fib_protocol_t fproto)
561 {
562     switch (fproto)
563     {
564     case FIB_PROTOCOL_IP4:
565         return (ADDRESS_IP4);
566     case FIB_PROTOCOL_IP6:
567         return (ADDRESS_IP6);
568     default:
569         break;
570     }
571
572     ASSERT(0);
573     return (ADDRESS_IP4);
574 }
575
576 void
577 vl_api_fib_source_add_t_handler (vl_api_fib_source_add_t * mp)
578 {
579     vl_api_fib_source_add_reply_t *rmp;
580     fib_source_t src;
581     int rv = 0;
582     u8 *name;
583
584     name = format (0, "%s", mp->src.name);
585     vec_add1 (name, 0);
586
587     src = fib_source_allocate((const char *)name,
588                               mp->src.priority,
589                               FIB_SOURCE_BH_API);
590
591     vec_free(name);
592
593     REPLY_MACRO2 (VL_API_FIB_SOURCE_ADD_REPLY,
594     ({
595         rmp->id = src;
596     }));
597 }
598
599 typedef struct fib_source_dump_ctx_t_
600 {
601     vl_api_registration_t * reg;
602     u32 context;
603 } fib_source_dump_ctx_t;
604
605 static walk_rc_t
606 send_fib_source (fib_source_t id,
607                  const char *name,
608                  fib_source_priority_t prio,
609                  fib_source_behaviour_t bh,
610                  void *data)
611 {
612     vl_api_fib_source_details_t *mp;
613     fib_source_dump_ctx_t *ctx;
614
615     ctx = data;
616     mp = vl_msg_api_alloc_zero (sizeof (*mp));
617     if (!mp)
618         return WALK_STOP;
619
620     mp->_vl_msg_id = ntohs (VL_API_FIB_SOURCE_DETAILS + REPLY_MSG_ID_BASE);
621     mp->context = ctx->context;
622
623     mp->src.priority = prio;
624     mp->src.id = id;
625     clib_memcpy(mp->src.name, name,
626                 clib_min(strlen(name), ARRAY_LEN(mp->src.name)));
627
628     vl_api_send_msg (ctx->reg, (u8 *) mp);
629
630     return (WALK_CONTINUE);
631 }
632
633 void
634 vl_api_fib_source_dump_t_handler (vl_api_fib_source_dump_t * mp)
635 {
636     vl_api_registration_t *reg;
637
638     reg = vl_api_client_index_to_registration (mp->client_index);
639     if (!reg)
640         return;
641
642     fib_source_dump_ctx_t ctx = {
643         .reg = reg,
644         .context = mp->context,
645     };
646
647     fib_source_walk(send_fib_source, &ctx);
648 }
649
650
651 #include <vnet/fib/fib.api.c>
652
653 static clib_error_t *
654 fib_api_hookup (vlib_main_t * vm)
655 {
656   /*
657    * Set up the (msg_name, crc, message-id) table
658    */
659   fib_base_msg_id = setup_message_id_table ();
660
661   return (NULL);
662 }
663
664 VLIB_API_INIT_FUNCTION (fib_api_hookup);