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>
32 #include <vnet/vnet_msg_enum.h>
34 #define vl_typedefs /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
38 #define vl_endianfun /* define message structures */
39 #include <vnet/vnet_all_api_h.h>
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #include <vnet/vnet_all_api_h.h>
48 #include <vlibapi/api_helper_macros.h>
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)
60 mpls_table_delete (u32 table_id, u8 is_api)
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.
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
72 fib_index = fib_table_find (FIB_PROTOCOL_MPLS, table_id);
76 fib_table_unlock (fib_index,
78 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
83 vl_api_mpls_table_add_del_t_handler (vl_api_mpls_table_add_del_t * mp)
85 vl_api_mpls_table_add_del_reply_t *rmp;
89 vnm = vnet_get_main ();
93 mpls_table_create (ntohl (mp->mt_table_id), 1, mp->mt_name);
95 mpls_table_delete (ntohl (mp->mt_table_id), 1);
97 // NB: Nothing sets rv; none of the above returns an error
99 REPLY_MACRO (VL_API_MPLS_TABLE_ADD_DEL_REPLY);
103 mpls_ip_bind_unbind_handler (vnet_main_t * vnm,
104 vl_api_mpls_ip_bind_unbind_t * mp)
106 u32 mpls_fib_index, ip_fib_index;
109 fib_table_find (FIB_PROTOCOL_MPLS, ntohl (mp->mb_mpls_table_id));
111 if (~0 == mpls_fib_index)
113 return VNET_API_ERROR_NO_SUCH_FIB;
116 ip_fib_index = fib_table_find ((mp->mb_is_ip4 ?
119 ntohl (mp->mb_ip_table_id));
120 if (~0 == ip_fib_index)
121 return VNET_API_ERROR_NO_SUCH_FIB;
124 .fp_len = mp->mb_address_length,
129 pfx.fp_proto = FIB_PROTOCOL_IP4;
130 clib_memcpy (&pfx.fp_addr.ip4, mp->mb_address,
131 sizeof (pfx.fp_addr.ip4));
135 pfx.fp_proto = FIB_PROTOCOL_IP6;
136 clib_memcpy (&pfx.fp_addr.ip6, mp->mb_address,
137 sizeof (pfx.fp_addr.ip6));
141 fib_table_entry_local_label_add (ip_fib_index, &pfx,
142 ntohl (mp->mb_label));
144 fib_table_entry_local_label_remove (ip_fib_index, &pfx,
145 ntohl (mp->mb_label));
151 vl_api_mpls_ip_bind_unbind_t_handler (vl_api_mpls_ip_bind_unbind_t * mp)
153 vl_api_mpls_ip_bind_unbind_reply_t *rmp;
157 vnm = vnet_get_main ();
160 rv = mpls_ip_bind_unbind_handler (vnm, mp);
161 rv = (rv == 0) ? vnm->api_errno : rv;
163 REPLY_MACRO (VL_API_MPLS_IP_BIND_UNBIND_REPLY);
167 mpls_route_add_del_t_handler (vnet_main_t * vnm,
168 vl_api_mpls_route_add_del_t * mp,
171 fib_mpls_label_t *label_stack = NULL;
172 u32 fib_index, next_hop_fib_index;
173 int rv, ii, n_labels;;
177 .fp_proto = FIB_PROTOCOL_MPLS,
178 .fp_eos = mp->mr_eos,
179 .fp_label = ntohl (mp->mr_label),
183 pfx.fp_payload_proto = mp->mr_next_hop_proto;
187 pfx.fp_payload_proto = DPO_PROTO_MPLS;
190 rv = add_del_route_check (FIB_PROTOCOL_MPLS,
192 mp->mr_next_hop_sw_if_index,
193 pfx.fp_payload_proto,
194 mp->mr_next_hop_table_id,
196 &fib_index, &next_hop_fib_index);
202 clib_memset (&nh, 0, sizeof (nh));
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));
209 n_labels = mp->mr_next_hop_n_out_labels;
214 vec_validate (label_stack, n_labels - 1);
215 for (ii = 0; ii < n_labels; ii++)
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);
228 rv = add_del_route_t_handler (mp->mr_is_multipath, mp->mr_is_add,
230 0, // mp->is_unreach,
231 0, // mp->is_prohibit,
235 mp->mr_classify_table_index,
236 mp->mr_is_resolve_host,
237 mp->mr_is_resolve_attached,
238 mp->mr_is_interface_rx,
241 0, // is source_lookup
244 mp->mr_next_hop_proto,
245 &nh, ~0, // next_hop_id
246 ntohl (mp->mr_next_hop_sw_if_index),
248 mp->mr_next_hop_weight,
249 mp->mr_next_hop_preference,
250 ntohl (mp->mr_next_hop_via_label),
254 if (mp->mr_is_add && 0 == rv)
255 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
261 vl_api_mpls_route_add_del_t_handler (vl_api_mpls_route_add_del_t * mp)
263 vl_api_mpls_route_add_del_reply_t *rmp;
268 vnm = vnet_get_main ();
271 rv = mpls_route_add_del_t_handler (vnm, mp, &stats_index);
274 REPLY_MACRO2 (VL_API_MPLS_ROUTE_ADD_DEL_REPLY,
276 rmp->stats_index = htonl (stats_index);
282 mpls_table_create (u32 table_id, u8 is_api, const u8 * name)
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.
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
296 fib_index = fib_table_find (FIB_PROTOCOL_MPLS, table_id);
300 fib_table_find_or_create_and_lock_w_name (FIB_PROTOCOL_MPLS,
304 FIB_SOURCE_CLI), name);
309 vl_api_mpls_tunnel_add_del_t_handler (vl_api_mpls_tunnel_add_del_t * mp)
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;
316 clib_memset (&rpath, 0, sizeof (rpath));
318 if (mp->mt_next_hop_proto_is_ip4)
320 rpath.frp_proto = DPO_PROTO_IP4;
321 clib_memcpy (&rpath.frp_addr.ip4,
322 mp->mt_next_hop, sizeof (rpath.frp_addr.ip4));
326 rpath.frp_proto = DPO_PROTO_IP6;
327 clib_memcpy (&rpath.frp_addr.ip6,
328 mp->mt_next_hop, sizeof (rpath.frp_addr.ip6));
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;
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))
337 rpath.frp_proto = DPO_PROTO_MPLS;
338 rpath.frp_local_label = next_hop_via_label;
339 rpath.frp_eos = MPLS_NON_EOS;
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)
349 rv = VNET_API_ERROR_NO_SUCH_FIB;
356 for (ii = 0; ii < mp->mt_next_hop_n_out_labels; ii++)
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),
365 vec_add1 (rpath.frp_label_stack, fml);
369 vec_add1 (rpaths, rpath);
371 tunnel_sw_if_index = ntohl (mp->mt_sw_if_index);
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);
380 tunnel_index = vnet_mpls_tunnel_get_index (tunnel_sw_if_index);
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);
394 REPLY_MACRO2(VL_API_MPLS_TUNNEL_ADD_DEL_REPLY,
396 rmp->sw_if_index = ntohl(tunnel_sw_if_index);
397 rmp->tunnel_index = ntohl(tunnel_index);
403 vl_api_sw_interface_set_mpls_enable_t_handler
404 (vl_api_sw_interface_set_mpls_enable_t * mp)
406 vl_api_sw_interface_set_mpls_enable_reply_t *rmp;
409 VALIDATE_SW_IF_INDEX (mp);
411 rv = mpls_sw_interface_enable_disable (&mpls_main,
412 ntohl (mp->sw_if_index),
415 BAD_SW_IF_INDEX_LABEL;
416 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MPLS_ENABLE_REPLY);
419 typedef struct mpls_tunnel_send_walk_ctx_t_
421 vl_api_registration_t *reg;
424 } mpls_tunnel_send_walk_ctx_t;
427 send_mpls_tunnel_entry (u32 mti, void *arg)
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;
438 mt = mpls_tunnel_get (mti);
440 if (~0 != ctx->sw_if_index && mt->mt_sw_if_index != ctx->sw_if_index)
443 n = fib_path_list_get_n_paths (mt->mt_path_list);
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));
448 mp->_vl_msg_id = ntohs (VL_API_MPLS_TUNNEL_DETAILS);
449 mp->context = ctx->context;
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);
455 fib_path_list_walk_w_ext (mt->mt_path_list,
456 &mt->mt_path_exts, fib_path_encode, &api_rpaths);
459 vec_foreach (api_rpath, api_rpaths)
461 fib_api_path_encode (api_rpath, fp);
465 vl_api_send_msg (ctx->reg, (u8 *) mp);
469 vl_api_mpls_tunnel_dump_t_handler (vl_api_mpls_tunnel_dump_t * mp)
471 vl_api_registration_t *reg;
473 reg = vl_api_client_index_to_registration (mp->client_index);
477 mpls_tunnel_send_walk_ctx_t ctx = {
479 .sw_if_index = ntohl (mp->sw_if_index),
480 .context = mp->context,
482 mpls_tunnel_walk (send_mpls_tunnel_entry, &ctx);
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)
492 vl_api_mpls_fib_details_t *mp;
493 fib_route_path_encode_t *api_rpath;
494 vl_api_fib_path_t *fp;
497 path_count = vec_len (api_rpaths);
498 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
501 clib_memset (mp, 0, sizeof (*mp));
502 mp->_vl_msg_id = ntohs (VL_API_MPLS_FIB_DETAILS);
503 mp->context = context;
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);
511 mp->count = htonl (path_count);
513 vec_foreach (api_rpath, api_rpaths)
515 fib_api_path_encode (api_rpath, fp);
519 vl_api_send_msg (reg, (u8 *) mp);
522 typedef struct vl_api_mpls_fib_dump_table_walk_ctx_t_
524 fib_node_index_t *lfeis;
525 } vl_api_mpls_fib_dump_table_walk_ctx_t;
527 static fib_table_walk_rc_t
528 vl_api_mpls_fib_dump_table_walk (fib_node_index_t fei, void *arg)
530 vl_api_mpls_fib_dump_table_walk_ctx_t *ctx = arg;
532 vec_add1 (ctx->lfeis, fei);
534 return (FIB_TABLE_WALK_CONTINUE);
538 vl_api_mpls_fib_dump_t_handler (vl_api_mpls_fib_dump_t * mp)
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;
548 fib_route_path_encode_t *api_rpaths;
549 vl_api_mpls_fib_dump_table_walk_ctx_t ctx = {
553 reg = vl_api_client_index_to_registration (mp->client_index);
558 pool_foreach (mpls_fib, mm->mpls_fibs,
560 mpls_fib_table_walk (mpls_fib,
561 vl_api_mpls_fib_dump_table_walk,
565 vec_sort_with_function (ctx.lfeis, fib_entry_cmp_for_sort);
567 vec_foreach (lfeip, ctx.lfeis)
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);
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);
578 vec_free (ctx.lfeis);
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()
588 #define vl_msg_name_crc_list
589 #include <vnet/vnet_all_api_h.h>
590 #undef vl_msg_name_crc_list
593 setup_message_id_table (api_main_t * am)
595 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
596 foreach_vl_msg_name_crc_mpls;
600 static clib_error_t *
601 mpls_api_hookup (vlib_main_t * vm)
603 api_main_t *am = &api_main;
606 vl_msg_api_set_handlers(VL_API_##N, #n, \
607 vl_api_##n##_t_handler, \
609 vl_api_##n##_t_endian, \
610 vl_api_##n##_t_print, \
611 sizeof(vl_api_##n##_t), 1);
616 * Trace space for 8 MPLS encap labels
618 am->api_trace_cfg[VL_API_MPLS_TUNNEL_ADD_DEL].size += 8 * sizeof (u32);
621 * Set up the (msg_name, crc, message-id) table
623 setup_message_id_table (am);
628 VLIB_API_INIT_FUNCTION (mpls_api_hookup);
631 * fd.io coding-style-patch-verification: ON
634 * eval: (c-set-style "gnu")