2 *------------------------------------------------------------------
3 * mpls_api.c - mpls api
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
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 #include <vnet/ip/ip_types_api.h>
33 #include <vnet/vnet_msg_enum.h>
35 #define vl_typedefs /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
39 #define vl_endianfun /* define message structures */
40 #include <vnet/vnet_all_api_h.h>
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #include <vnet/vnet_all_api_h.h>
49 #include <vlibapi/api_helper_macros.h>
51 #define foreach_vpe_api_msg \
52 _(MPLS_IP_BIND_UNBIND, mpls_ip_bind_unbind) \
53 _(MPLS_ROUTE_ADD_DEL, mpls_route_add_del) \
54 _(MPLS_TABLE_ADD_DEL, mpls_table_add_del) \
55 _(MPLS_TUNNEL_ADD_DEL, mpls_tunnel_add_del) \
56 _(MPLS_TUNNEL_DUMP, mpls_tunnel_dump) \
57 _(SW_INTERFACE_SET_MPLS_ENABLE, sw_interface_set_mpls_enable) \
58 _(MPLS_TABLE_DUMP, mpls_table_dump) \
59 _(MPLS_ROUTE_DUMP, mpls_route_dump)
62 mpls_table_delete (u32 table_id, u8 is_api)
67 * The MPLS defult table must also be explicitly created via the API.
68 * So in contrast to IP, it gets no special treatment here.
70 * The API holds only one lock on the table.
71 * i.e. it can be added many times via the API but needs to be
74 fib_index = fib_table_find (FIB_PROTOCOL_MPLS, table_id);
78 fib_table_unlock (fib_index,
80 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
85 vl_api_mpls_table_add_del_t_handler (vl_api_mpls_table_add_del_t * mp)
87 vl_api_mpls_table_add_del_reply_t *rmp;
91 vnm = vnet_get_main ();
95 mpls_table_create (ntohl (mp->mt_table.mt_table_id),
96 1, mp->mt_table.mt_name);
98 mpls_table_delete (ntohl (mp->mt_table.mt_table_id), 1);
100 // NB: Nothing sets rv; none of the above returns an error
102 REPLY_MACRO (VL_API_MPLS_TABLE_ADD_DEL_REPLY);
106 mpls_ip_bind_unbind_handler (vnet_main_t * vnm,
107 vl_api_mpls_ip_bind_unbind_t * mp)
109 u32 mpls_fib_index, ip_fib_index;
113 fib_table_find (FIB_PROTOCOL_MPLS, ntohl (mp->mb_mpls_table_id));
115 if (~0 == mpls_fib_index)
117 return VNET_API_ERROR_NO_SUCH_FIB;
120 ip_prefix_decode (&mp->mb_prefix, &pfx);
122 ip_fib_index = fib_table_find (pfx.fp_proto, ntohl (mp->mb_ip_table_id));
123 if (~0 == ip_fib_index)
124 return VNET_API_ERROR_NO_SUCH_FIB;
127 fib_table_entry_local_label_add (ip_fib_index, &pfx,
128 ntohl (mp->mb_label));
130 fib_table_entry_local_label_remove (ip_fib_index, &pfx,
131 ntohl (mp->mb_label));
137 vl_api_mpls_ip_bind_unbind_t_handler (vl_api_mpls_ip_bind_unbind_t * mp)
139 vl_api_mpls_ip_bind_unbind_reply_t *rmp;
143 vnm = vnet_get_main ();
146 rv = mpls_ip_bind_unbind_handler (vnm, mp);
147 rv = (rv == 0) ? vnm->api_errno : rv;
149 REPLY_MACRO (VL_API_MPLS_IP_BIND_UNBIND_REPLY);
153 mpls_route_add_del_t_handler (vnet_main_t * vnm,
154 vl_api_mpls_route_add_del_t * mp,
157 fib_route_path_t *rpaths = NULL, *rpath;
158 vl_api_fib_path_t *apath;
164 .fp_proto = FIB_PROTOCOL_MPLS,
165 .fp_eos = mp->mr_route.mr_eos,
166 .fp_label = ntohl (mp->mr_route.mr_label),
170 pfx.fp_payload_proto = mp->mr_route.mr_eos_proto;
174 pfx.fp_payload_proto = DPO_PROTO_MPLS;
177 rv = fib_api_table_id_decode (FIB_PROTOCOL_MPLS,
178 ntohl (mp->mr_route.mr_table_id), &fib_index);
182 vec_validate (rpaths, mp->mr_route.mr_n_paths - 1);
184 for (ii = 0; ii < mp->mr_route.mr_n_paths; ii++)
186 apath = &mp->mr_route.mr_paths[ii];
189 rv = fib_api_path_decode (apath, rpath);
195 rv = fib_api_route_add_del (mp->mr_is_add,
199 (mp->mr_route.mr_is_multicast ?
200 FIB_ENTRY_FLAG_MULTICAST :
201 FIB_ENTRY_FLAG_NONE), rpaths);
203 if (mp->mr_is_add && 0 == rv)
204 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
213 vl_api_mpls_route_add_del_t_handler (vl_api_mpls_route_add_del_t * mp)
215 vl_api_mpls_route_add_del_reply_t *rmp;
220 vnm = vnet_get_main ();
223 rv = mpls_route_add_del_t_handler (vnm, mp, &stats_index);
226 REPLY_MACRO2 (VL_API_MPLS_ROUTE_ADD_DEL_REPLY,
228 rmp->stats_index = htonl (stats_index);
234 mpls_table_create (u32 table_id, u8 is_api, const u8 * name)
239 * The MPLS defult table must also be explicitly created via the API.
240 * So in contrast to IP, it gets no special treatment here.
244 * The API holds only one lock on the table.
245 * i.e. it can be added many times via the API but needs to be
248 fib_index = fib_table_find (FIB_PROTOCOL_MPLS, table_id);
252 fib_table_find_or_create_and_lock_w_name (FIB_PROTOCOL_MPLS,
256 FIB_SOURCE_CLI), name);
261 vl_api_mpls_tunnel_add_del_t_handler (vl_api_mpls_tunnel_add_del_t * mp)
263 u32 tunnel_sw_if_index = ~0, tunnel_index = ~0;
264 vl_api_mpls_tunnel_add_del_reply_t *rmp;
265 fib_route_path_t *rpath, *rpaths = NULL;
268 vec_validate (rpaths, mp->mt_tunnel.mt_n_paths - 1);
270 for (ii = 0; ii < mp->mt_tunnel.mt_n_paths; ii++)
274 rv = fib_api_path_decode (&mp->mt_tunnel.mt_paths[ii], rpath);
279 tunnel_sw_if_index = ntohl (mp->mt_tunnel.mt_sw_if_index);
283 if (~0 == tunnel_sw_if_index)
285 vnet_mpls_tunnel_create (mp->mt_tunnel.mt_l2_only,
286 mp->mt_tunnel.mt_is_multicast);
287 vnet_mpls_tunnel_path_add (tunnel_sw_if_index, rpaths);
289 tunnel_index = vnet_mpls_tunnel_get_index (tunnel_sw_if_index);
293 tunnel_index = vnet_mpls_tunnel_get_index (tunnel_sw_if_index);
294 tunnel_sw_if_index = ntohl (mp->mt_tunnel.mt_sw_if_index);
295 if (!vnet_mpls_tunnel_path_remove (tunnel_sw_if_index, rpaths))
296 vnet_mpls_tunnel_del (tunnel_sw_if_index);
303 REPLY_MACRO2(VL_API_MPLS_TUNNEL_ADD_DEL_REPLY,
305 rmp->sw_if_index = ntohl(tunnel_sw_if_index);
306 rmp->tunnel_index = ntohl(tunnel_index);
312 vl_api_sw_interface_set_mpls_enable_t_handler
313 (vl_api_sw_interface_set_mpls_enable_t * mp)
315 vl_api_sw_interface_set_mpls_enable_reply_t *rmp;
318 VALIDATE_SW_IF_INDEX (mp);
320 rv = mpls_sw_interface_enable_disable (&mpls_main,
321 ntohl (mp->sw_if_index),
324 BAD_SW_IF_INDEX_LABEL;
325 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MPLS_ENABLE_REPLY);
328 typedef struct mpls_tunnel_send_walk_ctx_t_
330 vl_api_registration_t *reg;
333 } mpls_tunnel_send_walk_ctx_t;
336 send_mpls_tunnel_entry (u32 mti, void *arg)
338 mpls_tunnel_send_walk_ctx_t *ctx;
339 vl_api_mpls_tunnel_details_t *mp;
340 fib_path_encode_ctx_t path_ctx = {
343 const mpls_tunnel_t *mt;
344 fib_route_path_t *rpath;
345 vl_api_fib_path_t *fp;
350 mt = mpls_tunnel_get (mti);
352 if (~0 != ctx->sw_if_index && mt->mt_sw_if_index != ctx->sw_if_index)
355 n = fib_path_list_get_n_paths (mt->mt_path_list);
357 mp = vl_msg_api_alloc (sizeof (*mp) + n * sizeof (vl_api_fib_path_t));
358 clib_memset (mp, 0, sizeof (*mp) + n * sizeof (vl_api_fib_path_t));
360 mp->_vl_msg_id = ntohs (VL_API_MPLS_TUNNEL_DETAILS);
361 mp->context = ctx->context;
363 mp->mt_tunnel.mt_n_paths = n;
364 mp->mt_tunnel.mt_sw_if_index = ntohl (mt->mt_sw_if_index);
365 mp->mt_tunnel.mt_tunnel_index = ntohl (mti);
366 mp->mt_tunnel.mt_l2_only = ! !(MPLS_TUNNEL_FLAG_L2 & mt->mt_flags);
367 mp->mt_tunnel.mt_is_multicast = ! !(MPLS_TUNNEL_FLAG_MCAST & mt->mt_flags);
369 fib_path_list_walk_w_ext (mt->mt_path_list,
370 &mt->mt_path_exts, fib_path_encode, &path_ctx);
372 fp = mp->mt_tunnel.mt_paths;
373 vec_foreach (rpath, path_ctx.rpaths)
375 fib_api_path_encode (rpath, fp);
379 vl_api_send_msg (ctx->reg, (u8 *) mp);
381 vec_free (path_ctx.rpaths);
385 vl_api_mpls_tunnel_dump_t_handler (vl_api_mpls_tunnel_dump_t * mp)
387 vl_api_registration_t *reg;
389 reg = vl_api_client_index_to_registration (mp->client_index);
393 mpls_tunnel_send_walk_ctx_t ctx = {
395 .sw_if_index = ntohl (mp->sw_if_index),
396 .context = mp->context,
398 mpls_tunnel_walk (send_mpls_tunnel_entry, &ctx);
402 send_mpls_table_details (vpe_api_main_t * am,
403 vl_api_registration_t * reg,
404 u32 context, const fib_table_t * table)
406 vl_api_mpls_table_details_t *mp;
408 mp = vl_msg_api_alloc (sizeof (*mp));
409 memset (mp, 0, sizeof (*mp));
410 mp->_vl_msg_id = ntohs (VL_API_MPLS_TABLE_DETAILS);
411 mp->context = context;
413 mp->mt_table.mt_table_id = htonl (table->ft_table_id);
414 memcpy (mp->mt_table.mt_name,
416 clib_min (vec_len (table->ft_desc), sizeof (mp->mt_table.mt_name)));
418 vl_api_send_msg (reg, (u8 *) mp);
422 vl_api_mpls_table_dump_t_handler (vl_api_mpls_table_dump_t * mp)
424 vpe_api_main_t *am = &vpe_api_main;
425 vl_api_registration_t *reg;
426 mpls_main_t *mm = &mpls_main;
427 fib_table_t *fib_table;
429 reg = vl_api_client_index_to_registration (mp->client_index);
434 pool_foreach (fib_table, mm->fibs,
436 send_mpls_table_details(am, reg, mp->context, fib_table);
442 send_mpls_route_details (vpe_api_main_t * am,
443 vl_api_registration_t * reg,
444 u32 context, fib_node_index_t fib_entry_index)
446 fib_route_path_t *rpaths, *rpath;
447 vl_api_mpls_route_details_t *mp;
448 const fib_prefix_t *pfx;
449 vl_api_fib_path_t *fp;
452 rpaths = fib_entry_encode (fib_entry_index);
453 pfx = fib_entry_get_prefix (fib_entry_index);
455 path_count = vec_len (rpaths);
456 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
459 clib_memset (mp, 0, sizeof (*mp));
460 mp->_vl_msg_id = ntohs (VL_API_MPLS_ROUTE_DETAILS);
461 mp->context = context;
463 mp->mr_route.mr_table_id =
464 htonl (fib_table_get_table_id
465 (fib_entry_get_fib_index (fib_entry_index), pfx->fp_proto));
466 mp->mr_route.mr_eos = pfx->fp_eos;
467 mp->mr_route.mr_eos_proto = pfx->fp_payload_proto;
468 mp->mr_route.mr_label = htonl (pfx->fp_label);
470 mp->mr_route.mr_n_paths = path_count;
471 fp = mp->mr_route.mr_paths;
472 vec_foreach (rpath, rpaths)
474 fib_api_path_encode (rpath, fp);
479 vl_api_send_msg (reg, (u8 *) mp);
482 typedef struct vl_api_mpls_route_dump_table_walk_ctx_t_
484 fib_node_index_t *lfeis;
485 } vl_api_mpls_route_dump_table_walk_ctx_t;
487 static fib_table_walk_rc_t
488 vl_api_mpls_route_dump_table_walk (fib_node_index_t fei, void *arg)
490 vl_api_mpls_route_dump_table_walk_ctx_t *ctx = arg;
492 vec_add1 (ctx->lfeis, fei);
494 return (FIB_TABLE_WALK_CONTINUE);
498 vl_api_mpls_route_dump_t_handler (vl_api_mpls_route_dump_t * mp)
500 vpe_api_main_t *am = &vpe_api_main;
501 vl_api_registration_t *reg;
502 fib_node_index_t *lfeip = NULL;
503 vl_api_mpls_route_dump_table_walk_ctx_t ctx = {
508 reg = vl_api_client_index_to_registration (mp->client_index);
512 fib_index = fib_table_find (FIB_PROTOCOL_MPLS,
513 ntohl (mp->table.mt_table_id));
515 if (INDEX_INVALID != fib_index)
517 fib_table_walk (fib_index,
519 vl_api_mpls_route_dump_table_walk, &ctx);
520 vec_sort_with_function (ctx.lfeis, fib_entry_cmp_for_sort);
522 vec_foreach (lfeip, ctx.lfeis)
524 send_mpls_route_details (am, reg, mp->context, *lfeip);
527 vec_free (ctx.lfeis);
533 * Add vpe's API message handlers to the table.
534 * vlib has already mapped shared memory and
535 * added the client registration handlers.
536 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
538 #define vl_msg_name_crc_list
539 #include <vnet/vnet_all_api_h.h>
540 #undef vl_msg_name_crc_list
543 setup_message_id_table (api_main_t * am)
545 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
546 foreach_vl_msg_name_crc_mpls;
550 static clib_error_t *
551 mpls_api_hookup (vlib_main_t * vm)
553 api_main_t *am = vlibapi_get_main ();
556 vl_msg_api_set_handlers(VL_API_##N, #n, \
557 vl_api_##n##_t_handler, \
559 vl_api_##n##_t_endian, \
560 vl_api_##n##_t_print, \
561 sizeof(vl_api_##n##_t), 1);
566 * Trace space for 8 MPLS encap labels
568 am->api_trace_cfg[VL_API_MPLS_TUNNEL_ADD_DEL].size += 8 * sizeof (u32);
571 * Set up the (msg_name, crc, message-id) table
573 setup_message_id_table (am);
578 VLIB_API_INIT_FUNCTION (mpls_api_hookup);
581 * fd.io coding-style-patch-verification: ON
584 * eval: (c-set-style "gnu")