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