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;
58 #include <vlibapi/api_helper_macros.h>
60 /* List of message types that this plugin understands */
62 #define foreach_svs_plugin_api_msg \
63 _(SVS_PLUGIN_GET_VERSION, svs_plugin_get_version) \
64 _(SVS_TABLE_ADD_DEL, svs_table_add_del) \
65 _(SVS_ROUTE_ADD_DEL, svs_route_add_del) \
66 _(SVS_ENABLE_DISABLE, svs_enable_disable) \
70 vl_api_svs_plugin_get_version_t_handler (vl_api_svs_plugin_get_version_t * mp)
72 vl_api_svs_plugin_get_version_reply_t *rmp;
73 int msg_size = sizeof (*rmp);
74 unix_shared_memory_queue_t *q;
76 q = vl_api_client_index_to_input_queue (mp->client_index);
82 rmp = vl_msg_api_alloc (msg_size);
83 memset (rmp, 0, msg_size);
85 ntohs (VL_API_SVS_PLUGIN_GET_VERSION_REPLY + svs_base_msg_id);
86 rmp->context = mp->context;
87 rmp->major = htonl (SVS_PLUGIN_VERSION_MAJOR);
88 rmp->minor = htonl (SVS_PLUGIN_VERSION_MINOR);
90 vl_msg_api_send_shmem (q, (u8 *) & rmp);
94 vl_api_svs_table_add_del_t_handler (vl_api_svs_table_add_del_t * mp)
96 vl_api_svs_table_add_del_reply_t *rmp;
97 fib_protocol_t fproto;
100 fproto = fib_proto_from_api_address_family (mp->af);
104 svs_table_add (fproto, ntohl (mp->table_id));
108 svs_table_delete (fproto, ntohl (mp->table_id));
111 REPLY_MACRO (VL_API_SVS_TABLE_ADD_DEL_REPLY + svs_base_msg_id);
115 vl_api_svs_route_add_del_t_handler (vl_api_svs_route_add_del_t * mp)
117 vl_api_svs_route_add_del_reply_t *rmp;
121 ip_prefix_decode (&mp->prefix, &pfx);
126 svs_route_add (ntohl (mp->table_id), &pfx,
127 ntohl (mp->source_table_id));
131 rv = svs_route_delete (ntohl (mp->table_id), &pfx);
134 REPLY_MACRO (VL_API_SVS_ROUTE_ADD_DEL_REPLY + svs_base_msg_id);
138 vl_api_svs_enable_disable_t_handler (vl_api_svs_enable_disable_t * mp)
140 vl_api_svs_enable_disable_reply_t *rmp;
141 fib_protocol_t fproto;
144 VALIDATE_SW_IF_INDEX (mp);
146 fproto = fib_proto_from_api_address_family (mp->af);
150 svs_enable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
154 svs_disable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
157 BAD_SW_IF_INDEX_LABEL;
158 REPLY_MACRO (VL_API_SVS_ENABLE_DISABLE_REPLY + svs_base_msg_id);
161 typedef struct svs_dump_walk_ctx_t_
163 unix_shared_memory_queue_t *q;
165 } svs_dump_walk_ctx_t;
169 svs_send_details (fib_protocol_t fproto,
170 u32 table_id, u32 sw_if_index, void *args)
172 vl_api_svs_details_t *mp;
173 svs_dump_walk_ctx_t *ctx;
177 mp = vl_msg_api_alloc (sizeof (*mp));
178 mp->_vl_msg_id = ntohs (VL_API_SVS_DETAILS + svs_base_msg_id);
180 mp->context = ctx->context;
181 mp->sw_if_index = htonl (sw_if_index);
182 mp->table_id = htonl (table_id);
183 mp->af = fib_proto_to_api_address_family (fproto);
185 vl_msg_api_send_shmem (ctx->q, (u8 *) & mp);
187 return (WALK_CONTINUE);
191 vl_api_svs_dump_t_handler (vl_api_svs_dump_t * mp)
193 unix_shared_memory_queue_t *q;
195 q = vl_api_client_index_to_input_queue (mp->client_index);
201 svs_dump_walk_ctx_t ctx = {
203 .context = mp->context,
206 svs_walk (svs_send_details, &ctx);
209 #define vl_msg_name_crc_list
210 #include <svs/svs_all_api_h.h>
211 #undef vl_msg_name_crc_list
213 /* Set up the API message handling tables */
214 static clib_error_t *
215 svs_plugin_api_hookup (vlib_main_t * vm)
218 vl_msg_api_set_handlers((VL_API_##N + svs_base_msg_id), \
220 vl_api_##n##_t_handler, \
222 vl_api_##n##_t_endian, \
223 vl_api_##n##_t_print, \
224 sizeof(vl_api_##n##_t), 1);
225 foreach_svs_plugin_api_msg;
232 setup_message_id_table (api_main_t * apim)
234 #define _(id,n,crc) \
235 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + svs_base_msg_id);
236 foreach_vl_msg_name_crc_svs;
240 static clib_error_t *
241 svs_api_init (vlib_main_t * vm)
243 clib_error_t *error = 0;
245 u8 *name = format (0, "svs_%08x%c", api_version, 0);
247 /* Ask for a correctly-sized block of API message decode slots */
248 svs_base_msg_id = vl_msg_api_get_msg_ids ((char *) name,
249 VL_MSG_FIRST_AVAILABLE);
251 error = svs_plugin_api_hookup (vm);
253 /* Add our API messages to the global name_crc hash table */
254 setup_message_id_table (&api_main);
261 VLIB_INIT_FUNCTION (svs_api_init);
264 VLIB_PLUGIN_REGISTER () = {
265 .version = VPP_BUILD_VER,
266 .description = "Source VRF Select",
271 * fd.io coding-style-patch-verification: ON
274 * eval: (c-set-style "gnu")