api: refactor vlibmemory
[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 extern void stats_dslock_with_hint (int hint, int tag);
60 extern void stats_dsunlock (void);
61
62 void
63 mpls_table_delete (u32 table_id, u8 is_api)
64 {
65   u32 fib_index;
66
67   /*
68    * The MPLS defult table must also be explicitly created via the API.
69    * So in contrast to IP, it gets no special treatment here.
70    *
71    * The API holds only one lock on the table.
72    * i.e. it can be added many times via the API but needs to be
73    * deleted only once.
74    */
75   fib_index = fib_table_find (FIB_PROTOCOL_MPLS, table_id);
76
77   if (~0 != fib_index)
78     {
79       fib_table_unlock (fib_index,
80                         FIB_PROTOCOL_MPLS,
81                         (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
82     }
83 }
84
85 void
86 vl_api_mpls_table_add_del_t_handler (vl_api_mpls_table_add_del_t * mp)
87 {
88   vl_api_mpls_table_add_del_reply_t *rmp;
89   vnet_main_t *vnm;
90   int rv = 0;
91
92   vnm = vnet_get_main ();
93   vnm->api_errno = 0;
94
95   if (mp->mt_is_add)
96     mpls_table_create (ntohl (mp->mt_table_id), 1, mp->mt_name);
97   else
98     mpls_table_delete (ntohl (mp->mt_table_id), 1);
99
100   // NB: Nothing sets rv; none of the above returns an error
101
102   REPLY_MACRO (VL_API_MPLS_TABLE_ADD_DEL_REPLY);
103 }
104
105 static int
106 mpls_ip_bind_unbind_handler (vnet_main_t * vnm,
107                              vl_api_mpls_ip_bind_unbind_t * mp)
108 {
109   u32 mpls_fib_index, ip_fib_index;
110
111   mpls_fib_index =
112     fib_table_find (FIB_PROTOCOL_MPLS, ntohl (mp->mb_mpls_table_id));
113
114   if (~0 == mpls_fib_index)
115     {
116       return VNET_API_ERROR_NO_SUCH_FIB;
117     }
118
119   ip_fib_index = fib_table_find ((mp->mb_is_ip4 ?
120                                   FIB_PROTOCOL_IP4 :
121                                   FIB_PROTOCOL_IP6),
122                                  ntohl (mp->mb_ip_table_id));
123   if (~0 == ip_fib_index)
124     return VNET_API_ERROR_NO_SUCH_FIB;
125
126   fib_prefix_t pfx = {
127     .fp_len = mp->mb_address_length,
128   };
129
130   if (mp->mb_is_ip4)
131     {
132       pfx.fp_proto = FIB_PROTOCOL_IP4;
133       clib_memcpy (&pfx.fp_addr.ip4, mp->mb_address,
134                    sizeof (pfx.fp_addr.ip4));
135     }
136   else
137     {
138       pfx.fp_proto = FIB_PROTOCOL_IP6;
139       clib_memcpy (&pfx.fp_addr.ip6, mp->mb_address,
140                    sizeof (pfx.fp_addr.ip6));
141     }
142
143   if (mp->mb_is_bind)
144     fib_table_entry_local_label_add (ip_fib_index, &pfx,
145                                      ntohl (mp->mb_label));
146   else
147     fib_table_entry_local_label_remove (ip_fib_index, &pfx,
148                                         ntohl (mp->mb_label));
149
150   return (0);
151 }
152
153 void
154 vl_api_mpls_ip_bind_unbind_t_handler (vl_api_mpls_ip_bind_unbind_t * mp)
155 {
156   vl_api_mpls_ip_bind_unbind_reply_t *rmp;
157   vnet_main_t *vnm;
158   int rv;
159
160   vnm = vnet_get_main ();
161   vnm->api_errno = 0;
162
163   rv = mpls_ip_bind_unbind_handler (vnm, mp);
164   rv = (rv == 0) ? vnm->api_errno : rv;
165
166   REPLY_MACRO (VL_API_MPLS_IP_BIND_UNBIND_REPLY);
167 }
168
169 static int
170 mpls_route_add_del_t_handler (vnet_main_t * vnm,
171                               vl_api_mpls_route_add_del_t * mp)
172 {
173   u32 fib_index, next_hop_fib_index;
174   mpls_label_t *label_stack = NULL;
175   int rv, ii, n_labels;;
176
177   fib_prefix_t pfx = {
178     .fp_len = 21,
179     .fp_proto = FIB_PROTOCOL_MPLS,
180     .fp_eos = mp->mr_eos,
181     .fp_label = ntohl (mp->mr_label),
182   };
183   if (pfx.fp_eos)
184     {
185       pfx.fp_payload_proto = mp->mr_next_hop_proto;
186     }
187   else
188     {
189       pfx.fp_payload_proto = DPO_PROTO_MPLS;
190     }
191
192   rv = add_del_route_check (FIB_PROTOCOL_MPLS,
193                             mp->mr_table_id,
194                             mp->mr_next_hop_sw_if_index,
195                             pfx.fp_payload_proto,
196                             mp->mr_next_hop_table_id,
197                             mp->mr_is_rpf_id,
198                             &fib_index, &next_hop_fib_index);
199
200   if (0 != rv)
201     return (rv);
202
203   ip46_address_t nh;
204   memset (&nh, 0, sizeof (nh));
205
206   if (DPO_PROTO_IP4 == mp->mr_next_hop_proto)
207     memcpy (&nh.ip4, mp->mr_next_hop, sizeof (nh.ip4));
208   else if (DPO_PROTO_IP6 == mp->mr_next_hop_proto)
209     memcpy (&nh.ip6, mp->mr_next_hop, sizeof (nh.ip6));
210
211   n_labels = mp->mr_next_hop_n_out_labels;
212   if (n_labels == 0)
213     ;
214   else if (1 == n_labels)
215     vec_add1 (label_stack, ntohl (mp->mr_next_hop_out_label_stack[0]));
216   else
217     {
218       vec_validate (label_stack, n_labels - 1);
219       for (ii = 0; ii < n_labels; ii++)
220         label_stack[ii] = ntohl (mp->mr_next_hop_out_label_stack[ii]);
221     }
222
223   /* *INDENT-OFF* */
224   return (add_del_route_t_handler (mp->mr_is_multipath, mp->mr_is_add,
225                                    0,   // mp->is_drop,
226                                    0,   // mp->is_unreach,
227                                    0,   // mp->is_prohibit,
228                                    0,   // mp->is_local,
229                                    mp->mr_is_multicast,
230                                    mp->mr_is_classify,
231                                    mp->mr_classify_table_index,
232                                    mp->mr_is_resolve_host,
233                                    mp->mr_is_resolve_attached,
234                                    mp->mr_is_interface_rx,
235                                    mp->mr_is_rpf_id,
236                                    0,   // l2_bridged
237                                    0,   // is source_lookup
238                                    0,   // is_udp_encap
239                                    fib_index, &pfx,
240                                    mp->mr_next_hop_proto,
241                                    &nh, ~0, // next_hop_id
242                                    ntohl (mp->mr_next_hop_sw_if_index),
243                                    next_hop_fib_index,
244                                    mp->mr_next_hop_weight,
245                                    mp->mr_next_hop_preference,
246                                    ntohl (mp->mr_next_hop_via_label),
247                                    label_stack));
248   /* *INDENT-ON* */
249 }
250
251 void
252 vl_api_mpls_route_add_del_t_handler (vl_api_mpls_route_add_del_t * mp)
253 {
254   vl_api_mpls_route_add_del_reply_t *rmp;
255   vnet_main_t *vnm;
256   int rv;
257
258   vnm = vnet_get_main ();
259   vnm->api_errno = 0;
260
261   rv = mpls_route_add_del_t_handler (vnm, mp);
262
263   rv = (rv == 0) ? vnm->api_errno : rv;
264
265   REPLY_MACRO (VL_API_MPLS_ROUTE_ADD_DEL_REPLY);
266 }
267
268 void
269 mpls_table_create (u32 table_id, u8 is_api, const u8 * name)
270 {
271   u32 fib_index;
272
273   /*
274    * The MPLS defult table must also be explicitly created via the API.
275    * So in contrast to IP, it gets no special treatment here.
276    */
277
278   /*
279    * The API holds only one lock on the table.
280    * i.e. it can be added many times via the API but needs to be
281    * deleted only once.
282    */
283   fib_index = fib_table_find (FIB_PROTOCOL_MPLS, table_id);
284
285   if (~0 == fib_index)
286     {
287       fib_table_find_or_create_and_lock_w_name (FIB_PROTOCOL_MPLS,
288                                                 table_id,
289                                                 (is_api ?
290                                                  FIB_SOURCE_API :
291                                                  FIB_SOURCE_CLI), name);
292     }
293 }
294
295 static void
296 vl_api_mpls_tunnel_add_del_t_handler (vl_api_mpls_tunnel_add_del_t * mp)
297 {
298   vl_api_mpls_tunnel_add_del_reply_t *rmp;
299   int rv = 0;
300   u32 tunnel_sw_if_index;
301   int ii;
302   fib_route_path_t rpath, *rpaths = NULL;
303
304   memset (&rpath, 0, sizeof (rpath));
305
306   stats_dslock_with_hint (1 /* release hint */ , 5 /* tag */ );
307
308   if (mp->mt_next_hop_proto_is_ip4)
309     {
310       rpath.frp_proto = DPO_PROTO_IP4;
311       clib_memcpy (&rpath.frp_addr.ip4,
312                    mp->mt_next_hop, sizeof (rpath.frp_addr.ip4));
313     }
314   else
315     {
316       rpath.frp_proto = DPO_PROTO_IP6;
317       clib_memcpy (&rpath.frp_addr.ip6,
318                    mp->mt_next_hop, sizeof (rpath.frp_addr.ip6));
319     }
320   rpath.frp_sw_if_index = ntohl (mp->mt_next_hop_sw_if_index);
321   rpath.frp_weight = 1;
322
323   if (mp->mt_is_add)
324     {
325       for (ii = 0; ii < mp->mt_next_hop_n_out_labels; ii++)
326         vec_add1 (rpath.frp_label_stack,
327                   ntohl (mp->mt_next_hop_out_label_stack[ii]));
328     }
329
330   vec_add1 (rpaths, rpath);
331
332   tunnel_sw_if_index = ntohl (mp->mt_sw_if_index);
333
334   if (mp->mt_is_add)
335     {
336       if (~0 == tunnel_sw_if_index)
337         tunnel_sw_if_index = vnet_mpls_tunnel_create (mp->mt_l2_only,
338                                                       mp->mt_is_multicast);
339       vnet_mpls_tunnel_path_add (tunnel_sw_if_index, rpaths);
340     }
341   else
342     {
343       tunnel_sw_if_index = ntohl (mp->mt_sw_if_index);
344       if (!vnet_mpls_tunnel_path_remove (tunnel_sw_if_index, rpaths))
345         vnet_mpls_tunnel_del (tunnel_sw_if_index);
346     }
347
348   vec_free (rpaths);
349
350   stats_dsunlock ();
351
352   /* *INDENT-OFF* */
353   REPLY_MACRO2(VL_API_MPLS_TUNNEL_ADD_DEL_REPLY,
354   ({
355     rmp->sw_if_index = ntohl(tunnel_sw_if_index);
356   }));
357   /* *INDENT-ON* */
358 }
359
360 static void
361   vl_api_sw_interface_set_mpls_enable_t_handler
362   (vl_api_sw_interface_set_mpls_enable_t * mp)
363 {
364   vl_api_sw_interface_set_mpls_enable_reply_t *rmp;
365   int rv = 0;
366
367   VALIDATE_SW_IF_INDEX (mp);
368
369   rv = mpls_sw_interface_enable_disable (&mpls_main,
370                                          ntohl (mp->sw_if_index),
371                                          mp->enable, 1);
372
373   BAD_SW_IF_INDEX_LABEL;
374   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MPLS_ENABLE_REPLY);
375 }
376
377 typedef struct mpls_tunnel_send_walk_ctx_t_
378 {
379   svm_queue_t *q;
380   u32 index;
381   u32 context;
382 } mpls_tunnel_send_walk_ctx_t;
383
384 static void
385 send_mpls_tunnel_entry (u32 mti, void *arg)
386 {
387   fib_route_path_encode_t *api_rpaths = NULL, *api_rpath;
388   mpls_tunnel_send_walk_ctx_t *ctx;
389   vl_api_mpls_tunnel_details_t *mp;
390   const mpls_tunnel_t *mt;
391   vl_api_fib_path2_t *fp;
392   u32 n;
393
394   ctx = arg;
395
396   if (~0 != ctx->index && mti != ctx->index)
397     return;
398
399   mt = mpls_tunnel_get (mti);
400   n = fib_path_list_get_n_paths (mt->mt_path_list);
401
402   mp = vl_msg_api_alloc (sizeof (*mp) + n * sizeof (vl_api_fib_path2_t));
403   memset (mp, 0, sizeof (*mp) + n * sizeof (vl_api_fib_path2_t));
404
405   mp->_vl_msg_id = ntohs (VL_API_MPLS_TUNNEL_DETAILS);
406   mp->context = ctx->context;
407
408   mp->mt_tunnel_index = ntohl (mti);
409   mp->mt_count = ntohl (n);
410
411   fib_path_list_walk (mt->mt_path_list, fib_path_encode, &api_rpaths);
412
413   fp = mp->mt_paths;
414   vec_foreach (api_rpath, api_rpaths)
415   {
416     memset (fp, 0, sizeof (*fp));
417
418     fp->weight = api_rpath->rpath.frp_weight;
419     fp->preference = api_rpath->rpath.frp_preference;
420     fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
421     copy_fib_next_hop (api_rpath, fp);
422     fp++;
423   }
424
425   // FIXME
426   // memcpy (mp->mt_next_hop_out_labels,
427   //   mt->mt_label_stack, nlabels * sizeof (u32));
428
429
430   vl_msg_api_send_shmem (ctx->q, (u8 *) & mp);
431 }
432
433 static void
434 vl_api_mpls_tunnel_dump_t_handler (vl_api_mpls_tunnel_dump_t * mp)
435 {
436   svm_queue_t *q;
437
438   q = vl_api_client_index_to_input_queue (mp->client_index);
439   if (q == 0)
440     return;
441
442   mpls_tunnel_send_walk_ctx_t ctx = {
443     .q = q,
444     .index = ntohl (mp->tunnel_index),
445     .context = mp->context,
446   };
447   mpls_tunnel_walk (send_mpls_tunnel_entry, &ctx);
448 }
449
450 static void
451 send_mpls_fib_details (vpe_api_main_t * am,
452                        svm_queue_t * q,
453                        const fib_table_t * table,
454                        u32 label, u32 eos,
455                        fib_route_path_encode_t * api_rpaths, u32 context)
456 {
457   vl_api_mpls_fib_details_t *mp;
458   fib_route_path_encode_t *api_rpath;
459   vl_api_fib_path2_t *fp;
460   int path_count;
461
462   path_count = vec_len (api_rpaths);
463   mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
464   if (!mp)
465     return;
466   memset (mp, 0, sizeof (*mp));
467   mp->_vl_msg_id = ntohs (VL_API_MPLS_FIB_DETAILS);
468   mp->context = context;
469
470   mp->table_id = htonl (table->ft_table_id);
471   memcpy (mp->table_name, table->ft_desc,
472           clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
473   mp->eos_bit = eos;
474   mp->label = htonl (label);
475
476   mp->count = htonl (path_count);
477   fp = mp->path;
478   vec_foreach (api_rpath, api_rpaths)
479   {
480     memset (fp, 0, sizeof (*fp));
481     fp->weight = api_rpath->rpath.frp_weight;
482     fp->preference = api_rpath->rpath.frp_preference;
483     fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
484     copy_fib_next_hop (api_rpath, fp);
485     fp++;
486   }
487
488   vl_msg_api_send_shmem (q, (u8 *) & mp);
489 }
490
491 typedef struct vl_api_mpls_fib_dump_table_walk_ctx_t_
492 {
493   fib_node_index_t *lfeis;
494 } vl_api_mpls_fib_dump_table_walk_ctx_t;
495
496 static int
497 vl_api_mpls_fib_dump_table_walk (fib_node_index_t fei, void *arg)
498 {
499   vl_api_mpls_fib_dump_table_walk_ctx_t *ctx = arg;
500
501   vec_add1 (ctx->lfeis, fei);
502
503   return (1);
504 }
505
506 static void
507 vl_api_mpls_fib_dump_t_handler (vl_api_mpls_fib_dump_t * mp)
508 {
509   vpe_api_main_t *am = &vpe_api_main;
510   svm_queue_t *q;
511   mpls_main_t *mm = &mpls_main;
512   fib_table_t *fib_table;
513   mpls_fib_t *mpls_fib;
514   fib_node_index_t *lfeip = NULL;
515   fib_prefix_t pfx;
516   u32 fib_index;
517   fib_route_path_encode_t *api_rpaths;
518   vl_api_mpls_fib_dump_table_walk_ctx_t ctx = {
519     .lfeis = NULL,
520   };
521
522   q = vl_api_client_index_to_input_queue (mp->client_index);
523   if (q == 0)
524     return;
525
526   /* *INDENT-OFF* */
527   pool_foreach (mpls_fib, mm->mpls_fibs,
528   ({
529     mpls_fib_table_walk (mpls_fib,
530                          vl_api_mpls_fib_dump_table_walk,
531                          &ctx);
532   }));
533   /* *INDENT-ON* */
534   vec_sort_with_function (ctx.lfeis, fib_entry_cmp_for_sort);
535
536   vec_foreach (lfeip, ctx.lfeis)
537   {
538     fib_entry_get_prefix (*lfeip, &pfx);
539     fib_index = fib_entry_get_fib_index (*lfeip);
540     fib_table = fib_table_get (fib_index, pfx.fp_proto);
541     api_rpaths = NULL;
542     fib_entry_encode (*lfeip, &api_rpaths);
543     send_mpls_fib_details (am, q,
544                            fib_table, pfx.fp_label,
545                            pfx.fp_eos, api_rpaths, mp->context);
546     vec_free (api_rpaths);
547   }
548
549   vec_free (ctx.lfeis);
550 }
551
552 /*
553  * mpls_api_hookup
554  * Add vpe's API message handlers to the table.
555  * vlib has alread mapped shared memory and
556  * added the client registration handlers.
557  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
558  */
559 #define vl_msg_name_crc_list
560 #include <vnet/vnet_all_api_h.h>
561 #undef vl_msg_name_crc_list
562
563 static void
564 setup_message_id_table (api_main_t * am)
565 {
566 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
567   foreach_vl_msg_name_crc_mpls;
568 #undef _
569 }
570
571 static clib_error_t *
572 mpls_api_hookup (vlib_main_t * vm)
573 {
574   api_main_t *am = &api_main;
575
576 #define _(N,n)                                                  \
577     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
578                            vl_api_##n##_t_handler,              \
579                            vl_noop_handler,                     \
580                            vl_api_##n##_t_endian,               \
581                            vl_api_##n##_t_print,                \
582                            sizeof(vl_api_##n##_t), 1);
583   foreach_vpe_api_msg;
584 #undef _
585
586   /*
587    * Trace space for 8 MPLS encap labels
588    */
589   am->api_trace_cfg[VL_API_MPLS_TUNNEL_ADD_DEL].size += 8 * sizeof (u32);
590
591   /*
592    * Set up the (msg_name, crc, message-id) table
593    */
594   setup_message_id_table (am);
595
596   return 0;
597 }
598
599 VLIB_API_INIT_FUNCTION (mpls_api_hookup);
600
601 /*
602  * fd.io coding-style-patch-verification: ON
603  *
604  * Local Variables:
605  * eval: (c-set-style "gnu")
606  * End:
607  */