52434dae8aa2d8ffbab61b067cff94020a0a2ee6
[vpp.git] / src / vnet / mpls / mpls_api.c
1 /*
2  *------------------------------------------------------------------
3  * mpls_api.c - mpls api
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/mpls/mpls.h>
26 #include <vnet/mpls/mpls_tunnel.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vnet/fib/fib_api.h>
29 #include <vnet/fib/mpls_fib.h>
30 #include <vnet/fib/fib_path_list.h>
31
32 #include <vnet/vnet_msg_enum.h>
33
34 #define vl_typedefs             /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
36 #undef vl_typedefs
37
38 #define vl_endianfun            /* define message structures */
39 #include <vnet/vnet_all_api_h.h>
40 #undef vl_endianfun
41
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #define vl_printfun
45 #include <vnet/vnet_all_api_h.h>
46 #undef vl_printfun
47
48 #include <vlibapi/api_helper_macros.h>
49
50 #define foreach_vpe_api_msg                                 \
51 _(MPLS_IP_BIND_UNBIND, mpls_ip_bind_unbind)                 \
52 _(MPLS_ROUTE_ADD_DEL, mpls_route_add_del)                   \
53 _(MPLS_TABLE_ADD_DEL, mpls_table_add_del)                   \
54 _(MPLS_TUNNEL_ADD_DEL, mpls_tunnel_add_del)                 \
55 _(MPLS_TUNNEL_DUMP, mpls_tunnel_dump)                       \
56 _(SW_INTERFACE_SET_MPLS_ENABLE, sw_interface_set_mpls_enable) \
57 _(MPLS_FIB_DUMP, mpls_fib_dump)
58
59 void
60 mpls_table_delete (u32 table_id, u8 is_api)
61 {
62   u32 fib_index;
63
64   /*
65    * The MPLS defult table must also be explicitly created via the API.
66    * So in contrast to IP, it gets no special treatment here.
67    *
68    * The API holds only one lock on the table.
69    * i.e. it can be added many times via the API but needs to be
70    * deleted only once.
71    */
72   fib_index = fib_table_find (FIB_PROTOCOL_MPLS, table_id);
73
74   if (~0 != fib_index)
75     {
76       fib_table_unlock (fib_index,
77                         FIB_PROTOCOL_MPLS,
78                         (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
79     }
80 }
81
82 void
83 vl_api_mpls_table_add_del_t_handler (vl_api_mpls_table_add_del_t * mp)
84 {
85   vl_api_mpls_table_add_del_reply_t *rmp;
86   vnet_main_t *vnm;
87   int rv = 0;
88
89   vnm = vnet_get_main ();
90   vnm->api_errno = 0;
91
92   if (mp->mt_is_add)
93     mpls_table_create (ntohl (mp->mt_table_id), 1, mp->mt_name);
94   else
95     mpls_table_delete (ntohl (mp->mt_table_id), 1);
96
97   // NB: Nothing sets rv; none of the above returns an error
98
99   REPLY_MACRO (VL_API_MPLS_TABLE_ADD_DEL_REPLY);
100 }
101
102 static int
103 mpls_ip_bind_unbind_handler (vnet_main_t * vnm,
104                              vl_api_mpls_ip_bind_unbind_t * mp)
105 {
106   u32 mpls_fib_index, ip_fib_index;
107
108   mpls_fib_index =
109     fib_table_find (FIB_PROTOCOL_MPLS, ntohl (mp->mb_mpls_table_id));
110
111   if (~0 == mpls_fib_index)
112     {
113       return VNET_API_ERROR_NO_SUCH_FIB;
114     }
115
116   ip_fib_index = fib_table_find ((mp->mb_is_ip4 ?
117                                   FIB_PROTOCOL_IP4 :
118                                   FIB_PROTOCOL_IP6),
119                                  ntohl (mp->mb_ip_table_id));
120   if (~0 == ip_fib_index)
121     return VNET_API_ERROR_NO_SUCH_FIB;
122
123   fib_prefix_t pfx = {
124     .fp_len = mp->mb_address_length,
125   };
126
127   if (mp->mb_is_ip4)
128     {
129       pfx.fp_proto = FIB_PROTOCOL_IP4;
130       clib_memcpy (&pfx.fp_addr.ip4, mp->mb_address,
131                    sizeof (pfx.fp_addr.ip4));
132     }
133   else
134     {
135       pfx.fp_proto = FIB_PROTOCOL_IP6;
136       clib_memcpy (&pfx.fp_addr.ip6, mp->mb_address,
137                    sizeof (pfx.fp_addr.ip6));
138     }
139
140   if (mp->mb_is_bind)
141     fib_table_entry_local_label_add (ip_fib_index, &pfx,
142                                      ntohl (mp->mb_label));
143   else
144     fib_table_entry_local_label_remove (ip_fib_index, &pfx,
145                                         ntohl (mp->mb_label));
146
147   return (0);
148 }
149
150 void
151 vl_api_mpls_ip_bind_unbind_t_handler (vl_api_mpls_ip_bind_unbind_t * mp)
152 {
153   vl_api_mpls_ip_bind_unbind_reply_t *rmp;
154   vnet_main_t *vnm;
155   int rv;
156
157   vnm = vnet_get_main ();
158   vnm->api_errno = 0;
159
160   rv = mpls_ip_bind_unbind_handler (vnm, mp);
161   rv = (rv == 0) ? vnm->api_errno : rv;
162
163   REPLY_MACRO (VL_API_MPLS_IP_BIND_UNBIND_REPLY);
164 }
165
166 static int
167 mpls_route_add_del_t_handler (vnet_main_t * vnm,
168                               vl_api_mpls_route_add_del_t * mp,
169                               u32 * stats_index)
170 {
171   fib_mpls_label_t *label_stack = NULL;
172   u32 fib_index, next_hop_fib_index;
173   int rv, ii, n_labels;;
174
175   fib_prefix_t pfx = {
176     .fp_len = 21,
177     .fp_proto = FIB_PROTOCOL_MPLS,
178     .fp_eos = mp->mr_eos,
179     .fp_label = ntohl (mp->mr_label),
180   };
181   if (pfx.fp_eos)
182     {
183       pfx.fp_payload_proto = mp->mr_next_hop_proto;
184     }
185   else
186     {
187       pfx.fp_payload_proto = DPO_PROTO_MPLS;
188     }
189
190   rv = add_del_route_check (FIB_PROTOCOL_MPLS,
191                             mp->mr_table_id,
192                             mp->mr_next_hop_sw_if_index,
193                             pfx.fp_payload_proto,
194                             mp->mr_next_hop_table_id,
195                             mp->mr_is_rpf_id,
196                             &fib_index, &next_hop_fib_index);
197
198   if (0 != rv)
199     return (rv);
200
201   ip46_address_t nh;
202   clib_memset (&nh, 0, sizeof (nh));
203
204   if (DPO_PROTO_IP4 == mp->mr_next_hop_proto)
205     memcpy (&nh.ip4, mp->mr_next_hop, sizeof (nh.ip4));
206   else if (DPO_PROTO_IP6 == mp->mr_next_hop_proto)
207     memcpy (&nh.ip6, mp->mr_next_hop, sizeof (nh.ip6));
208
209   n_labels = mp->mr_next_hop_n_out_labels;
210   if (n_labels == 0)
211     ;
212   else
213     {
214       vec_validate (label_stack, n_labels - 1);
215       for (ii = 0; ii < n_labels; ii++)
216         {
217           label_stack[ii].fml_value =
218             ntohl (mp->mr_next_hop_out_label_stack[ii].label);
219           label_stack[ii].fml_ttl = mp->mr_next_hop_out_label_stack[ii].ttl;
220           label_stack[ii].fml_exp = mp->mr_next_hop_out_label_stack[ii].exp;
221           label_stack[ii].fml_mode =
222             (mp->mr_next_hop_out_label_stack[ii].is_uniform ?
223              FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
224         }
225     }
226
227   /* *INDENT-OFF* */
228   rv = add_del_route_t_handler (mp->mr_is_multipath, mp->mr_is_add,
229                                 0,      // mp->is_drop,
230                                 0,      // mp->is_unreach,
231                                 0,      // mp->is_prohibit,
232                                 0,      // mp->is_local,
233                                 mp->mr_is_multicast,
234                                 mp->mr_is_classify,
235                                 mp->mr_classify_table_index,
236                                 mp->mr_is_resolve_host,
237                                 mp->mr_is_resolve_attached,
238                                 mp->mr_is_interface_rx,
239                                 mp->mr_is_rpf_id,
240                                 0,      // l2_bridged
241                                 0,   // is source_lookup
242                                 0,   // is_udp_encap
243                                    fib_index, &pfx,
244                                 mp->mr_next_hop_proto,
245                                 &nh, ~0, // next_hop_id
246                                 ntohl (mp->mr_next_hop_sw_if_index),
247                                 next_hop_fib_index,
248                                 mp->mr_next_hop_weight,
249                                 mp->mr_next_hop_preference,
250                                 ntohl (mp->mr_next_hop_via_label),
251                                 label_stack);
252   /* *INDENT-ON* */
253
254   if (mp->mr_is_add && 0 == rv)
255     *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
256
257   return (rv);
258 }
259
260 void
261 vl_api_mpls_route_add_del_t_handler (vl_api_mpls_route_add_del_t * mp)
262 {
263   vl_api_mpls_route_add_del_reply_t *rmp;
264   vnet_main_t *vnm;
265   u32 stats_index;
266   int rv;
267
268   vnm = vnet_get_main ();
269   stats_index = ~0;
270
271   rv = mpls_route_add_del_t_handler (vnm, mp, &stats_index);
272
273   /* *INDENT-OFF* */
274   REPLY_MACRO2 (VL_API_MPLS_ROUTE_ADD_DEL_REPLY,
275   ({
276     rmp->stats_index = htonl (stats_index);
277   }));
278   /* *INDENT-ON* */
279 }
280
281 void
282 mpls_table_create (u32 table_id, u8 is_api, const u8 * name)
283 {
284   u32 fib_index;
285
286   /*
287    * The MPLS defult table must also be explicitly created via the API.
288    * So in contrast to IP, it gets no special treatment here.
289    */
290
291   /*
292    * The API holds only one lock on the table.
293    * i.e. it can be added many times via the API but needs to be
294    * deleted only once.
295    */
296   fib_index = fib_table_find (FIB_PROTOCOL_MPLS, table_id);
297
298   if (~0 == fib_index)
299     {
300       fib_table_find_or_create_and_lock_w_name (FIB_PROTOCOL_MPLS,
301                                                 table_id,
302                                                 (is_api ?
303                                                  FIB_SOURCE_API :
304                                                  FIB_SOURCE_CLI), name);
305     }
306 }
307
308 static void
309 vl_api_mpls_tunnel_add_del_t_handler (vl_api_mpls_tunnel_add_del_t * mp)
310 {
311   u32 tunnel_sw_if_index = ~0, tunnel_index = ~0, next_hop_via_label;
312   vl_api_mpls_tunnel_add_del_reply_t *rmp;
313   fib_route_path_t rpath, *rpaths = NULL;
314   int ii, rv = 0;
315
316   clib_memset (&rpath, 0, sizeof (rpath));
317
318   if (mp->mt_next_hop_proto_is_ip4)
319     {
320       rpath.frp_proto = DPO_PROTO_IP4;
321       clib_memcpy (&rpath.frp_addr.ip4,
322                    mp->mt_next_hop, sizeof (rpath.frp_addr.ip4));
323     }
324   else
325     {
326       rpath.frp_proto = DPO_PROTO_IP6;
327       clib_memcpy (&rpath.frp_addr.ip6,
328                    mp->mt_next_hop, sizeof (rpath.frp_addr.ip6));
329     }
330   rpath.frp_sw_if_index = ntohl (mp->mt_next_hop_sw_if_index);
331   rpath.frp_weight = mp->mt_next_hop_weight;
332   rpath.frp_preference = mp->mt_next_hop_preference;
333
334   next_hop_via_label = ntohl (mp->mt_next_hop_via_label);
335   if ((MPLS_LABEL_INVALID != next_hop_via_label) && (0 != next_hop_via_label))
336     {
337       rpath.frp_proto = DPO_PROTO_MPLS;
338       rpath.frp_local_label = next_hop_via_label;
339       rpath.frp_eos = MPLS_NON_EOS;
340     }
341
342   if (rpath.frp_sw_if_index == ~0)
343     {                           /* recursive path, set fib index */
344       rpath.frp_fib_index =
345         fib_table_find (dpo_proto_to_fib (rpath.frp_proto),
346                         ntohl (mp->mt_next_hop_table_id));
347       if (rpath.frp_fib_index == ~0)
348         {
349           rv = VNET_API_ERROR_NO_SUCH_FIB;
350           goto out;
351         }
352     }
353
354   if (mp->mt_is_add)
355     {
356       for (ii = 0; ii < mp->mt_next_hop_n_out_labels; ii++)
357         {
358           fib_mpls_label_t fml = {
359             .fml_value = ntohl (mp->mt_next_hop_out_label_stack[ii].label),
360             .fml_ttl = mp->mt_next_hop_out_label_stack[ii].ttl,
361             .fml_exp = mp->mt_next_hop_out_label_stack[ii].exp,
362             .fml_mode = (mp->mt_next_hop_out_label_stack[ii].is_uniform ?
363                          FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE),
364           };
365           vec_add1 (rpath.frp_label_stack, fml);
366         }
367     }
368
369   vec_add1 (rpaths, rpath);
370
371   tunnel_sw_if_index = ntohl (mp->mt_sw_if_index);
372
373   if (mp->mt_is_add)
374     {
375       if (~0 == tunnel_sw_if_index)
376         tunnel_sw_if_index = vnet_mpls_tunnel_create (mp->mt_l2_only,
377                                                       mp->mt_is_multicast);
378       vnet_mpls_tunnel_path_add (tunnel_sw_if_index, rpaths);
379
380       tunnel_index = vnet_mpls_tunnel_get_index (tunnel_sw_if_index);
381     }
382   else
383     {
384       tunnel_index = vnet_mpls_tunnel_get_index (tunnel_sw_if_index);
385       tunnel_sw_if_index = ntohl (mp->mt_sw_if_index);
386       if (!vnet_mpls_tunnel_path_remove (tunnel_sw_if_index, rpaths))
387         vnet_mpls_tunnel_del (tunnel_sw_if_index);
388     }
389
390   vec_free (rpaths);
391
392 out:
393   /* *INDENT-OFF* */
394   REPLY_MACRO2(VL_API_MPLS_TUNNEL_ADD_DEL_REPLY,
395   ({
396     rmp->sw_if_index = ntohl(tunnel_sw_if_index);
397     rmp->tunnel_index = ntohl(tunnel_index);
398   }));
399   /* *INDENT-ON* */
400 }
401
402 static void
403   vl_api_sw_interface_set_mpls_enable_t_handler
404   (vl_api_sw_interface_set_mpls_enable_t * mp)
405 {
406   vl_api_sw_interface_set_mpls_enable_reply_t *rmp;
407   int rv = 0;
408
409   VALIDATE_SW_IF_INDEX (mp);
410
411   rv = mpls_sw_interface_enable_disable (&mpls_main,
412                                          ntohl (mp->sw_if_index),
413                                          mp->enable, 1);
414
415   BAD_SW_IF_INDEX_LABEL;
416   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MPLS_ENABLE_REPLY);
417 }
418
419 typedef struct mpls_tunnel_send_walk_ctx_t_
420 {
421   vl_api_registration_t *reg;
422   u32 sw_if_index;
423   u32 context;
424 } mpls_tunnel_send_walk_ctx_t;
425
426 static void
427 send_mpls_tunnel_entry (u32 mti, void *arg)
428 {
429   fib_route_path_encode_t *api_rpaths = NULL, *api_rpath;
430   mpls_tunnel_send_walk_ctx_t *ctx;
431   vl_api_mpls_tunnel_details_t *mp;
432   const mpls_tunnel_t *mt;
433   vl_api_fib_path_t *fp;
434   u32 n;
435
436   ctx = arg;
437
438   mt = mpls_tunnel_get (mti);
439
440   if (~0 != ctx->sw_if_index && mt->mt_sw_if_index != ctx->sw_if_index)
441     return;
442
443   n = fib_path_list_get_n_paths (mt->mt_path_list);
444
445   mp = vl_msg_api_alloc (sizeof (*mp) + n * sizeof (vl_api_fib_path_t));
446   clib_memset (mp, 0, sizeof (*mp) + n * sizeof (vl_api_fib_path_t));
447
448   mp->_vl_msg_id = ntohs (VL_API_MPLS_TUNNEL_DETAILS);
449   mp->context = ctx->context;
450
451   mp->mt_tunnel_index = ntohl (mti);
452   mp->mt_sw_if_index = ntohl (mt->mt_sw_if_index);
453   mp->mt_count = ntohl (n);
454
455   fib_path_list_walk_w_ext (mt->mt_path_list,
456                             &mt->mt_path_exts, fib_path_encode, &api_rpaths);
457
458   fp = mp->mt_paths;
459   vec_foreach (api_rpath, api_rpaths)
460   {
461     fib_api_path_encode (api_rpath, fp);
462     fp++;
463   }
464
465   vl_api_send_msg (ctx->reg, (u8 *) mp);
466 }
467
468 static void
469 vl_api_mpls_tunnel_dump_t_handler (vl_api_mpls_tunnel_dump_t * mp)
470 {
471   vl_api_registration_t *reg;
472
473   reg = vl_api_client_index_to_registration (mp->client_index);
474   if (!reg)
475     return;
476
477   mpls_tunnel_send_walk_ctx_t ctx = {
478     .reg = reg,
479     .sw_if_index = ntohl (mp->sw_if_index),
480     .context = mp->context,
481   };
482   mpls_tunnel_walk (send_mpls_tunnel_entry, &ctx);
483 }
484
485 static void
486 send_mpls_fib_details (vpe_api_main_t * am,
487                        vl_api_registration_t * reg,
488                        const fib_table_t * table,
489                        const fib_prefix_t * pfx,
490                        fib_route_path_encode_t * api_rpaths, u32 context)
491 {
492   vl_api_mpls_fib_details_t *mp;
493   fib_route_path_encode_t *api_rpath;
494   vl_api_fib_path_t *fp;
495   int path_count;
496
497   path_count = vec_len (api_rpaths);
498   mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
499   if (!mp)
500     return;
501   clib_memset (mp, 0, sizeof (*mp));
502   mp->_vl_msg_id = ntohs (VL_API_MPLS_FIB_DETAILS);
503   mp->context = context;
504
505   mp->table_id = htonl (table->ft_table_id);
506   memcpy (mp->table_name, table->ft_desc,
507           clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
508   mp->eos_bit = pfx->fp_eos;
509   mp->label = htonl (pfx->fp_label);
510
511   mp->count = htonl (path_count);
512   fp = mp->path;
513   vec_foreach (api_rpath, api_rpaths)
514   {
515     fib_api_path_encode (api_rpath, fp);
516     fp++;
517   }
518
519   vl_api_send_msg (reg, (u8 *) mp);
520 }
521
522 typedef struct vl_api_mpls_fib_dump_table_walk_ctx_t_
523 {
524   fib_node_index_t *lfeis;
525 } vl_api_mpls_fib_dump_table_walk_ctx_t;
526
527 static fib_table_walk_rc_t
528 vl_api_mpls_fib_dump_table_walk (fib_node_index_t fei, void *arg)
529 {
530   vl_api_mpls_fib_dump_table_walk_ctx_t *ctx = arg;
531
532   vec_add1 (ctx->lfeis, fei);
533
534   return (FIB_TABLE_WALK_CONTINUE);
535 }
536
537 static void
538 vl_api_mpls_fib_dump_t_handler (vl_api_mpls_fib_dump_t * mp)
539 {
540   vpe_api_main_t *am = &vpe_api_main;
541   vl_api_registration_t *reg;
542   mpls_main_t *mm = &mpls_main;
543   fib_table_t *fib_table;
544   mpls_fib_t *mpls_fib;
545   fib_node_index_t *lfeip = NULL;
546   const fib_prefix_t *pfx;
547   u32 fib_index;
548   fib_route_path_encode_t *api_rpaths;
549   vl_api_mpls_fib_dump_table_walk_ctx_t ctx = {
550     .lfeis = NULL,
551   };
552
553   reg = vl_api_client_index_to_registration (mp->client_index);
554   if (!reg)
555     return;
556
557   /* *INDENT-OFF* */
558   pool_foreach (mpls_fib, mm->mpls_fibs,
559   ({
560     mpls_fib_table_walk (mpls_fib,
561                          vl_api_mpls_fib_dump_table_walk,
562                          &ctx);
563   }));
564   /* *INDENT-ON* */
565   vec_sort_with_function (ctx.lfeis, fib_entry_cmp_for_sort);
566
567   vec_foreach (lfeip, ctx.lfeis)
568   {
569     pfx = fib_entry_get_prefix (*lfeip);
570     fib_index = fib_entry_get_fib_index (*lfeip);
571     fib_table = fib_table_get (fib_index, pfx->fp_proto);
572     api_rpaths = NULL;
573     fib_entry_encode (*lfeip, &api_rpaths);
574     send_mpls_fib_details (am, reg, fib_table, pfx, api_rpaths, mp->context);
575     vec_free (api_rpaths);
576   }
577
578   vec_free (ctx.lfeis);
579 }
580
581 /*
582  * mpls_api_hookup
583  * Add vpe's API message handlers to the table.
584  * vlib has already mapped shared memory and
585  * added the client registration handlers.
586  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
587  */
588 #define vl_msg_name_crc_list
589 #include <vnet/vnet_all_api_h.h>
590 #undef vl_msg_name_crc_list
591
592 static void
593 setup_message_id_table (api_main_t * am)
594 {
595 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
596   foreach_vl_msg_name_crc_mpls;
597 #undef _
598 }
599
600 static clib_error_t *
601 mpls_api_hookup (vlib_main_t * vm)
602 {
603   api_main_t *am = &api_main;
604
605 #define _(N,n)                                                  \
606     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
607                            vl_api_##n##_t_handler,              \
608                            vl_noop_handler,                     \
609                            vl_api_##n##_t_endian,               \
610                            vl_api_##n##_t_print,                \
611                            sizeof(vl_api_##n##_t), 1);
612   foreach_vpe_api_msg;
613 #undef _
614
615   /*
616    * Trace space for 8 MPLS encap labels
617    */
618   am->api_trace_cfg[VL_API_MPLS_TUNNEL_ADD_DEL].size += 8 * sizeof (u32);
619
620   /*
621    * Set up the (msg_name, crc, message-id) table
622    */
623   setup_message_id_table (am);
624
625   return 0;
626 }
627
628 VLIB_API_INIT_FUNCTION (mpls_api_hookup);
629
630 /*
631  * fd.io coding-style-patch-verification: ON
632  *
633  * Local Variables:
634  * eval: (c-set-style "gnu")
635  * End:
636  */