2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <l3xc/l3xc.h>
21 #include <vnet/mpls/mpls_types.h>
22 #include <vnet/fib/fib_path_list.h>
23 #include <vnet/fib/fib_api.h>
25 #include <vpp/app/version.h>
27 #include <vlibapi/api.h>
28 #include <vlibmemory/api.h>
30 /* define message IDs */
31 #include <l3xc/l3xc_msg_enum.h>
33 /* define message structures */
35 #include <l3xc/l3xc_all_api_h.h>
38 /* define generated endian-swappers */
40 #include <l3xc/l3xc_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 <l3xc/l3xc_all_api_h.h>
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <l3xc/l3xc_all_api_h.h>
55 * Base message ID fot the plugin
57 static u32 l3xc_base_msg_id;
59 #include <vlibapi/api_helper_macros.h>
61 /* List of message types that this plugin understands */
63 #define foreach_l3xc_plugin_api_msg \
64 _(L3XC_PLUGIN_GET_VERSION, l3xc_plugin_get_version) \
65 _(L3XC_UPDATE, l3xc_update) \
66 _(L3XC_DEL, l3xc_del) \
67 _(L3XC_DUMP, l3xc_dump)
70 vl_api_l3xc_plugin_get_version_t_handler (vl_api_l3xc_plugin_get_version_t *
73 vl_api_l3xc_plugin_get_version_reply_t *rmp;
74 vl_api_registration_t *rp;
76 rp = vl_api_client_index_to_registration (mp->client_index);
80 rmp = vl_msg_api_alloc (sizeof (*rmp));
82 ntohs (VL_API_L3XC_PLUGIN_GET_VERSION_REPLY + l3xc_base_msg_id);
83 rmp->context = mp->context;
84 rmp->major = htonl (L3XC_PLUGIN_VERSION_MAJOR);
85 rmp->minor = htonl (L3XC_PLUGIN_VERSION_MINOR);
87 vl_api_send_msg (rp, (u8 *) rmp);
91 vl_api_l3xc_update_t_handler (vl_api_l3xc_update_t * mp)
93 vl_api_l3xc_update_reply_t *rmp;
94 fib_route_path_t *paths = NULL, *path;
98 VALIDATE_SW_IF_INDEX (&mp->l3xc);
100 if (0 == mp->l3xc.n_paths)
102 rv = VNET_API_ERROR_INVALID_VALUE;
106 vec_validate (paths, mp->l3xc.n_paths - 1);
108 for (pi = 0; pi < mp->l3xc.n_paths; pi++)
111 rv = fib_path_api_parse (&mp->l3xc.paths[pi], path);
119 rv = l3xc_update (ntohl (mp->l3xc.sw_if_index), mp->l3xc.is_ip6, paths);
124 BAD_SW_IF_INDEX_LABEL;
127 REPLY_MACRO2 (VL_API_L3XC_UPDATE_REPLY + l3xc_base_msg_id,
129 rmp->stats_index = 0;
135 vl_api_l3xc_del_t_handler (vl_api_l3xc_del_t * mp)
137 vl_api_l3xc_del_reply_t *rmp;
140 VALIDATE_SW_IF_INDEX (mp);
142 rv = l3xc_delete (ntohl (mp->sw_if_index), mp->is_ip6);
144 BAD_SW_IF_INDEX_LABEL;
146 REPLY_MACRO (VL_API_L3XC_DEL_REPLY + l3xc_base_msg_id);
149 typedef struct l3xc_dump_walk_ctx_t_
151 vl_api_registration_t *rp;
153 } l3xc_dump_walk_ctx_t;
156 l3xc_send_details (u32 l3xci, void *args)
158 fib_route_path_encode_t *api_rpaths = NULL, *api_rpath;
159 vl_api_l3xc_details_t *mp;
160 l3xc_dump_walk_ctx_t *ctx;
161 vl_api_fib_path_t *fp;
167 l3xc = l3xc_get (l3xci);
168 n_paths = fib_path_list_get_n_paths (l3xc->l3xc_pl);
169 msg_size = sizeof (*mp) + sizeof (mp->l3xc.paths[0]) * n_paths;
171 mp = vl_msg_api_alloc (msg_size);
172 clib_memset (mp, 0, msg_size);
173 mp->_vl_msg_id = ntohs (VL_API_L3XC_DETAILS + l3xc_base_msg_id);
175 /* fill in the message */
176 mp->context = ctx->context;
177 mp->l3xc.n_paths = n_paths;
178 mp->l3xc.sw_if_index = htonl (l3xc->l3xc_sw_if_index);
180 fib_path_list_walk_w_ext (l3xc->l3xc_pl, NULL, fib_path_encode,
184 vec_foreach (api_rpath, api_rpaths)
186 fib_api_path_encode (api_rpath, fp);
190 vl_api_send_msg (ctx->rp, (u8 *) mp);
196 vl_api_l3xc_dump_t_handler (vl_api_l3xc_dump_t * mp)
198 vl_api_registration_t *rp;
201 rp = vl_api_client_index_to_registration (mp->client_index);
205 l3xc_dump_walk_ctx_t ctx = {
207 .context = mp->context,
210 sw_if_index = ntohl (mp->sw_if_index);
212 if (~0 == sw_if_index)
213 l3xc_walk (l3xc_send_details, &ctx);
216 fib_protocol_t fproto;
219 FOR_EACH_FIB_IP_PROTOCOL (fproto)
221 l3xci = l3xc_find (sw_if_index, fproto);
223 if (INDEX_INVALID != l3xci)
224 l3xc_send_details (l3xci, &ctx);
229 #define vl_msg_name_crc_list
230 #include <l3xc/l3xc_all_api_h.h>
231 #undef vl_msg_name_crc_list
233 /* Set up the API message handling tables */
234 static clib_error_t *
235 l3xc_plugin_api_hookup (vlib_main_t * vm)
238 vl_msg_api_set_handlers((VL_API_##N + l3xc_base_msg_id), \
240 vl_api_##n##_t_handler, \
242 vl_api_##n##_t_endian, \
243 vl_api_##n##_t_print, \
244 sizeof(vl_api_##n##_t), 1);
245 foreach_l3xc_plugin_api_msg;
252 setup_message_id_table (api_main_t * apim)
254 #define _(id,n,crc) \
255 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + l3xc_base_msg_id);
256 foreach_vl_msg_name_crc_l3xc;
260 static clib_error_t *
261 l3xc_api_init (vlib_main_t * vm)
263 clib_error_t *error = 0;
265 u8 *name = format (0, "l3xc_%08x%c", api_version, 0);
267 /* Ask for a correctly-sized block of API message decode slots */
268 l3xc_base_msg_id = vl_msg_api_get_msg_ids ((char *) name,
269 VL_MSG_FIRST_AVAILABLE);
271 error = l3xc_plugin_api_hookup (vm);
273 /* Add our API messages to the global name_crc hash table */
274 setup_message_id_table (&api_main);
281 VLIB_INIT_FUNCTION (l3xc_api_init);
284 VLIB_PLUGIN_REGISTER () = {
285 .version = VPP_BUILD_VER,
286 .description = "L3 Cross-Connect (L3XC)",
291 * fd.io coding-style-patch-verification: ON
294 * eval: (c-set-style "gnu")