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