2 * Copyright (c) 2018 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>
21 #include <vnet/fib/fib_api.h>
22 #include <vnet/ip/ip_types_api.h>
24 #include <vpp/app/version.h>
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
29 /* define message IDs */
30 #include <svs/svs_msg_enum.h>
32 /* define message structures */
34 #include <svs/svs_all_api_h.h>
37 /* define generated endian-swappers */
39 #include <svs/svs_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 <svs/svs_all_api_h.h>
48 /* Get the API version number */
49 #define vl_api_version(n,v) static u32 api_version=(v);
50 #include <svs/svs_all_api_h.h>
54 * Base message ID fot the plugin
56 static u32 svs_base_msg_id;
57 #include <vlibapi/api_helper_macros.h>
59 /* List of message types that this plugin understands */
61 #define foreach_svs_plugin_api_msg \
62 _(SVS_PLUGIN_GET_VERSION, svs_plugin_get_version) \
63 _(SVS_TABLE_ADD_DEL, svs_table_add_del) \
64 _(SVS_ROUTE_ADD_DEL, svs_route_add_del) \
65 _(SVS_ENABLE_DISABLE, svs_enable_disable) \
69 vl_api_svs_plugin_get_version_t_handler (vl_api_svs_plugin_get_version_t * mp)
71 vl_api_svs_plugin_get_version_reply_t *rmp;
72 int msg_size = sizeof (*rmp);
73 vl_api_registration_t *rp;
75 rp = vl_api_client_index_to_registration (mp->client_index);
79 rmp = vl_msg_api_alloc (msg_size);
80 clib_memset (rmp, 0, msg_size);
82 ntohs (VL_API_SVS_PLUGIN_GET_VERSION_REPLY + svs_base_msg_id);
83 rmp->context = mp->context;
84 rmp->major = htonl (SVS_PLUGIN_VERSION_MAJOR);
85 rmp->minor = htonl (SVS_PLUGIN_VERSION_MINOR);
87 vl_api_send_msg (rp, (u8 *) rmp);
91 vl_api_svs_table_add_del_t_handler (vl_api_svs_table_add_del_t * mp)
93 vl_api_svs_table_add_del_reply_t *rmp;
94 fib_protocol_t fproto;
97 fproto = fib_proto_from_api_address_family (mp->af);
101 rv = svs_table_add (fproto, ntohl (mp->table_id));
105 rv = svs_table_delete (fproto, ntohl (mp->table_id));
108 REPLY_MACRO (VL_API_SVS_TABLE_ADD_DEL_REPLY + svs_base_msg_id);
112 vl_api_svs_route_add_del_t_handler (vl_api_svs_route_add_del_t * mp)
114 vl_api_svs_route_add_del_reply_t *rmp;
118 ip_prefix_decode (&mp->prefix, &pfx);
122 rv = svs_route_add (ntohl (mp->table_id), &pfx,
123 ntohl (mp->source_table_id));
127 rv = svs_route_delete (ntohl (mp->table_id), &pfx);
130 REPLY_MACRO (VL_API_SVS_ROUTE_ADD_DEL_REPLY + svs_base_msg_id);
134 vl_api_svs_enable_disable_t_handler (vl_api_svs_enable_disable_t * mp)
136 vl_api_svs_enable_disable_reply_t *rmp;
137 fib_protocol_t fproto;
140 VALIDATE_SW_IF_INDEX (mp);
142 fproto = fib_proto_from_api_address_family (mp->af);
146 rv = svs_enable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
151 svs_disable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
154 BAD_SW_IF_INDEX_LABEL;
155 REPLY_MACRO (VL_API_SVS_ENABLE_DISABLE_REPLY + svs_base_msg_id);
158 typedef struct svs_dump_walk_ctx_t_
160 vl_api_registration_t *rp;
162 } svs_dump_walk_ctx_t;
166 svs_send_details (fib_protocol_t fproto,
167 u32 table_id, u32 sw_if_index, void *args)
169 vl_api_svs_details_t *mp;
170 svs_dump_walk_ctx_t *ctx;
174 mp = vl_msg_api_alloc (sizeof (*mp));
175 mp->_vl_msg_id = ntohs (VL_API_SVS_DETAILS + svs_base_msg_id);
177 mp->context = ctx->context;
178 mp->sw_if_index = htonl (sw_if_index);
179 mp->table_id = htonl (table_id);
180 mp->af = fib_proto_to_api_address_family (fproto);
182 vl_api_send_msg (ctx->rp, (u8 *) mp);
184 return (WALK_CONTINUE);
188 vl_api_svs_dump_t_handler (vl_api_svs_dump_t * mp)
190 vl_api_registration_t *rp;
192 rp = vl_api_client_index_to_registration (mp->client_index);
196 svs_dump_walk_ctx_t ctx = {
198 .context = mp->context,
201 svs_walk (svs_send_details, &ctx);
204 #define vl_msg_name_crc_list
205 #include <svs/svs_all_api_h.h>
206 #undef vl_msg_name_crc_list
208 /* Set up the API message handling tables */
209 static clib_error_t *
210 svs_plugin_api_hookup (vlib_main_t * vm)
213 vl_msg_api_set_handlers((VL_API_##N + svs_base_msg_id), \
215 vl_api_##n##_t_handler, \
217 vl_api_##n##_t_endian, \
218 vl_api_##n##_t_print, \
219 sizeof(vl_api_##n##_t), 1);
220 foreach_svs_plugin_api_msg;
227 setup_message_id_table (api_main_t * apim)
229 #define _(id,n,crc) \
230 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + svs_base_msg_id);
231 foreach_vl_msg_name_crc_svs;
235 static clib_error_t *
236 svs_api_init (vlib_main_t * vm)
238 clib_error_t *error = 0;
240 u8 *name = format (0, "svs_%08x%c", api_version, 0);
242 /* Ask for a correctly-sized block of API message decode slots */
243 svs_base_msg_id = vl_msg_api_get_msg_ids ((char *) name,
244 VL_MSG_FIRST_AVAILABLE);
246 error = svs_plugin_api_hookup (vm);
248 /* Add our API messages to the global name_crc hash table */
249 setup_message_id_table (&api_main);
256 VLIB_INIT_FUNCTION (svs_api_init);
259 VLIB_PLUGIN_REGISTER () = {
260 .version = VPP_BUILD_VER,
261 .description = "Source Virtual Routing and Fowarding (VRF) Select",
266 * fd.io coding-style-patch-verification: ON
269 * eval: (c-set-style "gnu")