blob: 97928a744d8d976360c8d53d39fc8f5cc64a3bff [file] [log] [blame]
Pavel Kotucek0f971d82017-01-03 10:48:54 +01001/*
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>
Neale Rannsa3af3372017-03-28 03:49:52 -070029#include <vnet/fib/mpls_fib.h>
Neale Ranns0f26c5a2017-03-01 15:12:11 -080030#include <vnet/fib/fib_path_list.h>
Neale Ranns097fa662018-05-01 05:17:55 -070031#include <vnet/ip/ip_types_api.h>
Pavel Kotucek0f971d82017-01-03 10:48:54 +010032
33#include <vnet/vnet_msg_enum.h>
34
35#define vl_typedefs /* define message structures */
36#include <vnet/vnet_all_api_h.h>
37#undef vl_typedefs
38
39#define vl_endianfun /* define message structures */
40#include <vnet/vnet_all_api_h.h>
41#undef vl_endianfun
42
43/* instantiate all the print functions we know about */
44#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45#define vl_printfun
46#include <vnet/vnet_all_api_h.h>
47#undef vl_printfun
48
49#include <vlibapi/api_helper_macros.h>
50
51#define foreach_vpe_api_msg \
52_(MPLS_IP_BIND_UNBIND, mpls_ip_bind_unbind) \
53_(MPLS_ROUTE_ADD_DEL, mpls_route_add_del) \
Neale Ranns28ab9cc2017-08-14 07:18:42 -070054_(MPLS_TABLE_ADD_DEL, mpls_table_add_del) \
Pavel Kotucek0f971d82017-01-03 10:48:54 +010055_(MPLS_TUNNEL_ADD_DEL, mpls_tunnel_add_del) \
56_(MPLS_TUNNEL_DUMP, mpls_tunnel_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -080057_(SW_INTERFACE_SET_MPLS_ENABLE, sw_interface_set_mpls_enable) \
Neale Ranns097fa662018-05-01 05:17:55 -070058_(MPLS_TABLE_DUMP, mpls_table_dump) \
59_(MPLS_ROUTE_DUMP, mpls_route_dump)
Pavel Kotucek0f971d82017-01-03 10:48:54 +010060
Neale Ranns28ab9cc2017-08-14 07:18:42 -070061void
Neale Ranns15002542017-09-10 04:39:11 -070062mpls_table_delete (u32 table_id, u8 is_api)
63{
64 u32 fib_index;
65
66 /*
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.
69 *
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
72 * deleted only once.
73 */
74 fib_index = fib_table_find (FIB_PROTOCOL_MPLS, table_id);
75
76 if (~0 != fib_index)
77 {
78 fib_table_unlock (fib_index,
79 FIB_PROTOCOL_MPLS,
80 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
81 }
82}
83
84void
Neale Ranns28ab9cc2017-08-14 07:18:42 -070085vl_api_mpls_table_add_del_t_handler (vl_api_mpls_table_add_del_t * mp)
86{
87 vl_api_mpls_table_add_del_reply_t *rmp;
88 vnet_main_t *vnm;
89 int rv = 0;
90
91 vnm = vnet_get_main ();
92 vnm->api_errno = 0;
93
Neale Ranns15002542017-09-10 04:39:11 -070094 if (mp->mt_is_add)
Neale Ranns097fa662018-05-01 05:17:55 -070095 mpls_table_create (ntohl (mp->mt_table.mt_table_id),
96 1, mp->mt_table.mt_name);
Neale Ranns15002542017-09-10 04:39:11 -070097 else
Neale Ranns097fa662018-05-01 05:17:55 -070098 mpls_table_delete (ntohl (mp->mt_table.mt_table_id), 1);
Neale Ranns15002542017-09-10 04:39:11 -070099
Chris Lukeb2bcad62017-09-18 08:51:22 -0400100 // NB: Nothing sets rv; none of the above returns an error
Neale Ranns15002542017-09-10 04:39:11 -0700101
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700102 REPLY_MACRO (VL_API_MPLS_TABLE_ADD_DEL_REPLY);
103}
104
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100105static int
106mpls_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;
Neale Ranns097fa662018-05-01 05:17:55 -0700110 fib_prefix_t pfx;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100111
112 mpls_fib_index =
113 fib_table_find (FIB_PROTOCOL_MPLS, ntohl (mp->mb_mpls_table_id));
114
115 if (~0 == mpls_fib_index)
116 {
Neale Ranns15002542017-09-10 04:39:11 -0700117 return VNET_API_ERROR_NO_SUCH_FIB;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100118 }
119
Neale Ranns097fa662018-05-01 05:17:55 -0700120 ip_prefix_decode (&mp->mb_prefix, &pfx);
121
122 ip_fib_index = fib_table_find (pfx.fp_proto, ntohl (mp->mb_ip_table_id));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100123 if (~0 == ip_fib_index)
124 return VNET_API_ERROR_NO_SUCH_FIB;
125
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100126 if (mp->mb_is_bind)
127 fib_table_entry_local_label_add (ip_fib_index, &pfx,
128 ntohl (mp->mb_label));
129 else
130 fib_table_entry_local_label_remove (ip_fib_index, &pfx,
131 ntohl (mp->mb_label));
132
133 return (0);
134}
135
136void
137vl_api_mpls_ip_bind_unbind_t_handler (vl_api_mpls_ip_bind_unbind_t * mp)
138{
139 vl_api_mpls_ip_bind_unbind_reply_t *rmp;
140 vnet_main_t *vnm;
141 int rv;
142
143 vnm = vnet_get_main ();
144 vnm->api_errno = 0;
145
146 rv = mpls_ip_bind_unbind_handler (vnm, mp);
147 rv = (rv == 0) ? vnm->api_errno : rv;
148
149 REPLY_MACRO (VL_API_MPLS_IP_BIND_UNBIND_REPLY);
150}
151
152static int
153mpls_route_add_del_t_handler (vnet_main_t * vnm,
Neale Ranns008dbe12018-09-07 09:32:36 -0700154 vl_api_mpls_route_add_del_t * mp,
155 u32 * stats_index)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100156{
Neale Ranns097fa662018-05-01 05:17:55 -0700157 fib_route_path_t *rpaths = NULL, *rpath;
158 vl_api_fib_path_t *apath;
159 u32 fib_index;
160 int rv, ii;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100161
162 fib_prefix_t pfx = {
163 .fp_len = 21,
164 .fp_proto = FIB_PROTOCOL_MPLS,
Neale Ranns097fa662018-05-01 05:17:55 -0700165 .fp_eos = mp->mr_route.mr_eos,
166 .fp_label = ntohl (mp->mr_route.mr_label),
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100167 };
168 if (pfx.fp_eos)
169 {
Neale Ranns097fa662018-05-01 05:17:55 -0700170 pfx.fp_payload_proto = mp->mr_route.mr_eos_proto;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100171 }
172 else
173 {
174 pfx.fp_payload_proto = DPO_PROTO_MPLS;
175 }
176
Neale Ranns097fa662018-05-01 05:17:55 -0700177 rv = fib_api_table_id_decode (FIB_PROTOCOL_MPLS,
178 ntohl (mp->mr_route.mr_table_id), &fib_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100179 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -0700180 goto out;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100181
Neale Ranns097fa662018-05-01 05:17:55 -0700182 vec_validate (rpaths, mp->mr_route.mr_n_paths - 1);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100183
Neale Ranns097fa662018-05-01 05:17:55 -0700184 for (ii = 0; ii < mp->mr_route.mr_n_paths; ii++)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100185 {
Neale Ranns097fa662018-05-01 05:17:55 -0700186 apath = &mp->mr_route.mr_paths[ii];
187 rpath = &rpaths[ii];
188
189 rv = fib_api_path_decode (apath, rpath);
190
191 if (0 != rv)
192 goto out;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100193 }
194
Neale Ranns976b2592019-12-04 06:11:00 +0000195 rv = fib_api_route_add_del (
196 mp->mr_is_add, mp->mr_is_multipath, fib_index, &pfx, FIB_SOURCE_API,
197 (mp->mr_route.mr_is_multicast ? FIB_ENTRY_FLAG_MULTICAST :
198 FIB_ENTRY_FLAG_NONE),
199 rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700200
201 if (mp->mr_is_add && 0 == rv)
202 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
203
Neale Ranns097fa662018-05-01 05:17:55 -0700204out:
205 vec_free (rpaths);
206
Neale Ranns008dbe12018-09-07 09:32:36 -0700207 return (rv);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100208}
209
210void
211vl_api_mpls_route_add_del_t_handler (vl_api_mpls_route_add_del_t * mp)
212{
213 vl_api_mpls_route_add_del_reply_t *rmp;
214 vnet_main_t *vnm;
Neale Ranns008dbe12018-09-07 09:32:36 -0700215 u32 stats_index;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100216 int rv;
217
218 vnm = vnet_get_main ();
Neale Ranns008dbe12018-09-07 09:32:36 -0700219 stats_index = ~0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100220
Neale Ranns008dbe12018-09-07 09:32:36 -0700221 rv = mpls_route_add_del_t_handler (vnm, mp, &stats_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100222
Neale Ranns008dbe12018-09-07 09:32:36 -0700223 /* *INDENT-OFF* */
224 REPLY_MACRO2 (VL_API_MPLS_ROUTE_ADD_DEL_REPLY,
225 ({
226 rmp->stats_index = htonl (stats_index);
227 }));
228 /* *INDENT-ON* */
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100229}
230
Neale Ranns15002542017-09-10 04:39:11 -0700231void
Neale Ranns2297af02017-09-12 09:45:04 -0700232mpls_table_create (u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -0700233{
234 u32 fib_index;
235
236 /*
237 * The MPLS defult table must also be explicitly created via the API.
238 * So in contrast to IP, it gets no special treatment here.
239 */
240
241 /*
242 * The API holds only one lock on the table.
243 * i.e. it can be added many times via the API but needs to be
244 * deleted only once.
245 */
246 fib_index = fib_table_find (FIB_PROTOCOL_MPLS, table_id);
247
248 if (~0 == fib_index)
249 {
Neale Ranns2297af02017-09-12 09:45:04 -0700250 fib_table_find_or_create_and_lock_w_name (FIB_PROTOCOL_MPLS,
251 table_id,
252 (is_api ?
253 FIB_SOURCE_API :
254 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -0700255 }
256}
257
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100258static void
259vl_api_mpls_tunnel_add_del_t_handler (vl_api_mpls_tunnel_add_del_t * mp)
260{
Neale Ranns097fa662018-05-01 05:17:55 -0700261 u32 tunnel_sw_if_index = ~0, tunnel_index = ~0;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100262 vl_api_mpls_tunnel_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -0700263 fib_route_path_t *rpath, *rpaths = NULL;
Neale Rannsf5fa5ae2018-09-26 05:07:25 -0700264 int ii, rv = 0;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800265
Neale Ranns097fa662018-05-01 05:17:55 -0700266 vec_validate (rpaths, mp->mt_tunnel.mt_n_paths - 1);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100267
Neale Ranns097fa662018-05-01 05:17:55 -0700268 for (ii = 0; ii < mp->mt_tunnel.mt_n_paths; ii++)
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800269 {
Neale Ranns097fa662018-05-01 05:17:55 -0700270 rpath = &rpaths[ii];
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800271
Neale Ranns097fa662018-05-01 05:17:55 -0700272 rv = fib_api_path_decode (&mp->mt_tunnel.mt_paths[ii], rpath);
273
274 if (0 != rv)
275 goto out;
Neale Ranns7c922dc2018-08-30 06:12:27 -0700276 }
Neale Ranns097fa662018-05-01 05:17:55 -0700277 tunnel_sw_if_index = ntohl (mp->mt_tunnel.mt_sw_if_index);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800278
279 if (mp->mt_is_add)
280 {
281 if (~0 == tunnel_sw_if_index)
Neale Ranns097fa662018-05-01 05:17:55 -0700282 tunnel_sw_if_index =
283 vnet_mpls_tunnel_create (mp->mt_tunnel.mt_l2_only,
IJsbrand Wijnands39ae0a02020-03-05 10:56:26 -0800284 mp->mt_tunnel.mt_is_multicast,
285 mp->mt_tunnel.mt_tag);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800286 vnet_mpls_tunnel_path_add (tunnel_sw_if_index, rpaths);
Neale Rannsf5fa5ae2018-09-26 05:07:25 -0700287
288 tunnel_index = vnet_mpls_tunnel_get_index (tunnel_sw_if_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100289 }
290 else
291 {
Neale Rannsf5fa5ae2018-09-26 05:07:25 -0700292 tunnel_index = vnet_mpls_tunnel_get_index (tunnel_sw_if_index);
Neale Ranns097fa662018-05-01 05:17:55 -0700293 tunnel_sw_if_index = ntohl (mp->mt_tunnel.mt_sw_if_index);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800294 if (!vnet_mpls_tunnel_path_remove (tunnel_sw_if_index, rpaths))
295 vnet_mpls_tunnel_del (tunnel_sw_if_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100296 }
297
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800298 vec_free (rpaths);
299
John Lo06fda9c2018-10-03 16:32:44 -0400300out:
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100301 /* *INDENT-OFF* */
302 REPLY_MACRO2(VL_API_MPLS_TUNNEL_ADD_DEL_REPLY,
303 ({
304 rmp->sw_if_index = ntohl(tunnel_sw_if_index);
Neale Rannsf5fa5ae2018-09-26 05:07:25 -0700305 rmp->tunnel_index = ntohl(tunnel_index);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100306 }));
307 /* *INDENT-ON* */
308}
309
Neale Rannsb8d44812017-11-10 06:53:54 -0800310static void
311 vl_api_sw_interface_set_mpls_enable_t_handler
312 (vl_api_sw_interface_set_mpls_enable_t * mp)
313{
314 vl_api_sw_interface_set_mpls_enable_reply_t *rmp;
315 int rv = 0;
316
317 VALIDATE_SW_IF_INDEX (mp);
318
319 rv = mpls_sw_interface_enable_disable (&mpls_main,
320 ntohl (mp->sw_if_index),
321 mp->enable, 1);
322
323 BAD_SW_IF_INDEX_LABEL;
324 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MPLS_ENABLE_REPLY);
325}
326
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100327typedef struct mpls_tunnel_send_walk_ctx_t_
328{
Florin Coras6c4dae22018-01-09 06:39:23 -0800329 vl_api_registration_t *reg;
Neale Rannsf5fa5ae2018-09-26 05:07:25 -0700330 u32 sw_if_index;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100331 u32 context;
332} mpls_tunnel_send_walk_ctx_t;
333
334static void
335send_mpls_tunnel_entry (u32 mti, void *arg)
336{
337 mpls_tunnel_send_walk_ctx_t *ctx;
338 vl_api_mpls_tunnel_details_t *mp;
Neale Ranns097fa662018-05-01 05:17:55 -0700339 fib_path_encode_ctx_t path_ctx = {
340 .rpaths = NULL,
341 };
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100342 const mpls_tunnel_t *mt;
Neale Ranns097fa662018-05-01 05:17:55 -0700343 fib_route_path_t *rpath;
Neale Ranns31ed7442018-02-23 05:29:09 -0800344 vl_api_fib_path_t *fp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800345 u32 n;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100346
347 ctx = arg;
348
Neale Rannsf5fa5ae2018-09-26 05:07:25 -0700349 mt = mpls_tunnel_get (mti);
350
351 if (~0 != ctx->sw_if_index && mt->mt_sw_if_index != ctx->sw_if_index)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100352 return;
353
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800354 n = fib_path_list_get_n_paths (mt->mt_path_list);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100355
Neale Ranns31ed7442018-02-23 05:29:09 -0800356 mp = vl_msg_api_alloc (sizeof (*mp) + n * sizeof (vl_api_fib_path_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400357 clib_memset (mp, 0, sizeof (*mp) + n * sizeof (vl_api_fib_path_t));
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800358
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100359 mp->_vl_msg_id = ntohs (VL_API_MPLS_TUNNEL_DETAILS);
360 mp->context = ctx->context;
361
IJsbrand Wijnandsbf103d92019-10-28 17:19:27 +0100362 mp->mt_tunnel.mt_n_paths = n;
Neale Ranns097fa662018-05-01 05:17:55 -0700363 mp->mt_tunnel.mt_sw_if_index = ntohl (mt->mt_sw_if_index);
364 mp->mt_tunnel.mt_tunnel_index = ntohl (mti);
365 mp->mt_tunnel.mt_l2_only = ! !(MPLS_TUNNEL_FLAG_L2 & mt->mt_flags);
366 mp->mt_tunnel.mt_is_multicast = ! !(MPLS_TUNNEL_FLAG_MCAST & mt->mt_flags);
IJsbrand Wijnands39ae0a02020-03-05 10:56:26 -0800367 memcpy (mp->mt_tunnel.mt_tag, mt->mt_tag, sizeof (mp->mt_tunnel.mt_tag));
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800368
Neale Ranns775f73c2018-12-20 03:01:49 -0800369 fib_path_list_walk_w_ext (mt->mt_path_list,
Neale Ranns097fa662018-05-01 05:17:55 -0700370 &mt->mt_path_exts, fib_path_encode, &path_ctx);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800371
Neale Ranns097fa662018-05-01 05:17:55 -0700372 fp = mp->mt_tunnel.mt_paths;
373 vec_foreach (rpath, path_ctx.rpaths)
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800374 {
Neale Ranns097fa662018-05-01 05:17:55 -0700375 fib_api_path_encode (rpath, fp);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800376 fp++;
377 }
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100378
Florin Coras6c4dae22018-01-09 06:39:23 -0800379 vl_api_send_msg (ctx->reg, (u8 *) mp);
Neale Ranns097fa662018-05-01 05:17:55 -0700380
381 vec_free (path_ctx.rpaths);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100382}
383
384static void
385vl_api_mpls_tunnel_dump_t_handler (vl_api_mpls_tunnel_dump_t * mp)
386{
Florin Coras6c4dae22018-01-09 06:39:23 -0800387 vl_api_registration_t *reg;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100388
Florin Coras6c4dae22018-01-09 06:39:23 -0800389 reg = vl_api_client_index_to_registration (mp->client_index);
390 if (!reg)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100391 return;
392
393 mpls_tunnel_send_walk_ctx_t ctx = {
Florin Coras6c4dae22018-01-09 06:39:23 -0800394 .reg = reg,
Neale Rannsf5fa5ae2018-09-26 05:07:25 -0700395 .sw_if_index = ntohl (mp->sw_if_index),
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100396 .context = mp->context,
397 };
398 mpls_tunnel_walk (send_mpls_tunnel_entry, &ctx);
399}
400
401static void
Neale Ranns097fa662018-05-01 05:17:55 -0700402send_mpls_table_details (vpe_api_main_t * am,
403 vl_api_registration_t * reg,
404 u32 context, const fib_table_t * table)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100405{
Neale Ranns097fa662018-05-01 05:17:55 -0700406 vl_api_mpls_table_details_t *mp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100407
Neale Ranns097fa662018-05-01 05:17:55 -0700408 mp = vl_msg_api_alloc (sizeof (*mp));
409 memset (mp, 0, sizeof (*mp));
410 mp->_vl_msg_id = ntohs (VL_API_MPLS_TABLE_DETAILS);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100411 mp->context = context;
412
Neale Ranns097fa662018-05-01 05:17:55 -0700413 mp->mt_table.mt_table_id = htonl (table->ft_table_id);
414 memcpy (mp->mt_table.mt_name,
415 table->ft_desc,
416 clib_min (vec_len (table->ft_desc), sizeof (mp->mt_table.mt_name)));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100417
Florin Coras6c4dae22018-01-09 06:39:23 -0800418 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100419}
420
Neale Ranns097fa662018-05-01 05:17:55 -0700421static void
422vl_api_mpls_table_dump_t_handler (vl_api_mpls_table_dump_t * mp)
423{
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;
428
429 reg = vl_api_client_index_to_registration (mp->client_index);
430 if (!reg)
431 return;
432
433 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100434 pool_foreach (fib_table, mm->fibs)
435 {
Neale Ranns097fa662018-05-01 05:17:55 -0700436 send_mpls_table_details(am, reg, mp->context, fib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100437 }
Neale Ranns097fa662018-05-01 05:17:55 -0700438 /* *INDENT-ON* */
439}
440
441static void
442send_mpls_route_details (vpe_api_main_t * am,
443 vl_api_registration_t * reg,
444 u32 context, fib_node_index_t fib_entry_index)
445{
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;
450 int path_count;
451
452 rpaths = fib_entry_encode (fib_entry_index);
453 pfx = fib_entry_get_prefix (fib_entry_index);
454
455 path_count = vec_len (rpaths);
456 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
457 if (!mp)
458 return;
459 clib_memset (mp, 0, sizeof (*mp));
460 mp->_vl_msg_id = ntohs (VL_API_MPLS_ROUTE_DETAILS);
461 mp->context = context;
462
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);
469
470 mp->mr_route.mr_n_paths = path_count;
471 fp = mp->mr_route.mr_paths;
472 vec_foreach (rpath, rpaths)
473 {
474 fib_api_path_encode (rpath, fp);
475 fp++;
476 }
477
478 vec_free (rpaths);
479 vl_api_send_msg (reg, (u8 *) mp);
480}
481
482typedef struct vl_api_mpls_route_dump_table_walk_ctx_t_
Neale Rannsa3af3372017-03-28 03:49:52 -0700483{
484 fib_node_index_t *lfeis;
Neale Ranns097fa662018-05-01 05:17:55 -0700485} vl_api_mpls_route_dump_table_walk_ctx_t;
Neale Rannsa3af3372017-03-28 03:49:52 -0700486
Neale Ranns89541992017-04-06 04:41:02 -0700487static fib_table_walk_rc_t
Neale Ranns097fa662018-05-01 05:17:55 -0700488vl_api_mpls_route_dump_table_walk (fib_node_index_t fei, void *arg)
Neale Rannsa3af3372017-03-28 03:49:52 -0700489{
Neale Ranns097fa662018-05-01 05:17:55 -0700490 vl_api_mpls_route_dump_table_walk_ctx_t *ctx = arg;
Neale Rannsa3af3372017-03-28 03:49:52 -0700491
492 vec_add1 (ctx->lfeis, fei);
493
Neale Ranns89541992017-04-06 04:41:02 -0700494 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700495}
496
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100497static void
Neale Ranns097fa662018-05-01 05:17:55 -0700498vl_api_mpls_route_dump_t_handler (vl_api_mpls_route_dump_t * mp)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100499{
500 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800501 vl_api_registration_t *reg;
Neale Rannsa3af3372017-03-28 03:49:52 -0700502 fib_node_index_t *lfeip = NULL;
Neale Ranns097fa662018-05-01 05:17:55 -0700503 vl_api_mpls_route_dump_table_walk_ctx_t ctx = {
Neale Rannsa3af3372017-03-28 03:49:52 -0700504 .lfeis = NULL,
505 };
Neale Ranns097fa662018-05-01 05:17:55 -0700506 u32 fib_index;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100507
Florin Coras6c4dae22018-01-09 06:39:23 -0800508 reg = vl_api_client_index_to_registration (mp->client_index);
509 if (!reg)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100510 return;
511
Neale Ranns097fa662018-05-01 05:17:55 -0700512 fib_index = fib_table_find (FIB_PROTOCOL_MPLS,
513 ntohl (mp->table.mt_table_id));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100514
Neale Ranns097fa662018-05-01 05:17:55 -0700515 if (INDEX_INVALID != fib_index)
516 {
517 fib_table_walk (fib_index,
518 FIB_PROTOCOL_MPLS,
519 vl_api_mpls_route_dump_table_walk, &ctx);
520 vec_sort_with_function (ctx.lfeis, fib_entry_cmp_for_sort);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100521
Neale Ranns097fa662018-05-01 05:17:55 -0700522 vec_foreach (lfeip, ctx.lfeis)
523 {
524 send_mpls_route_details (am, reg, mp->context, *lfeip);
525 }
526
527 vec_free (ctx.lfeis);
528 }
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100529}
530
531/*
532 * mpls_api_hookup
533 * Add vpe's API message handlers to the table.
Jim Thompsonf324dec2019-04-08 03:22:21 -0500534 * vlib has already mapped shared memory and
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100535 * added the client registration handlers.
536 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
537 */
538#define vl_msg_name_crc_list
539#include <vnet/vnet_all_api_h.h>
540#undef vl_msg_name_crc_list
541
542static void
543setup_message_id_table (api_main_t * am)
544{
545#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
546 foreach_vl_msg_name_crc_mpls;
547#undef _
548}
549
550static clib_error_t *
551mpls_api_hookup (vlib_main_t * vm)
552{
Dave Barach39d69112019-11-27 11:42:13 -0500553 api_main_t *am = vlibapi_get_main ();
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100554
555#define _(N,n) \
556 vl_msg_api_set_handlers(VL_API_##N, #n, \
557 vl_api_##n##_t_handler, \
558 vl_noop_handler, \
559 vl_api_##n##_t_endian, \
560 vl_api_##n##_t_print, \
561 sizeof(vl_api_##n##_t), 1);
562 foreach_vpe_api_msg;
563#undef _
564
565 /*
566 * Trace space for 8 MPLS encap labels
567 */
568 am->api_trace_cfg[VL_API_MPLS_TUNNEL_ADD_DEL].size += 8 * sizeof (u32);
569
570 /*
571 * Set up the (msg_name, crc, message-id) table
572 */
573 setup_message_id_table (am);
574
575 return 0;
576}
577
578VLIB_API_INIT_FUNCTION (mpls_api_hookup);
579
580/*
581 * fd.io coding-style-patch-verification: ON
582 *
583 * Local Variables:
584 * eval: (c-set-style "gnu")
585 * End:
586 */