API: Fix shared memory only action handlers.
[vpp.git] / src / plugins / svs / svs_api.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <stddef.h>
17
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <svs/svs.h>
21 #include <vnet/fib/fib_api.h>
22 #include <vnet/ip/ip_types_api.h>
23
24 #include <vpp/app/version.h>
25
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28
29 /* define message IDs */
30 #include <svs/svs_msg_enum.h>
31
32 /* define message structures */
33 #define vl_typedefs
34 #include <svs/svs_all_api_h.h>
35 #undef vl_typedefs
36
37 /* define generated endian-swappers */
38 #define vl_endianfun
39 #include <svs/svs_all_api_h.h>
40 #undef vl_endianfun
41
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #define vl_printfun
45 #include <svs/svs_all_api_h.h>
46 #undef vl_printfun
47
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>
51 #undef vl_api_version
52
53 /**
54  * Base message ID fot the plugin
55  */
56 static u32 svs_base_msg_id;
57 #include <vlibapi/api_helper_macros.h>
58
59 /* List of message types that this plugin understands */
60
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)           \
66   _(SVS_DUMP, svs_dump)
67
68 static void
69 vl_api_svs_plugin_get_version_t_handler (vl_api_svs_plugin_get_version_t * mp)
70 {
71   vl_api_svs_plugin_get_version_reply_t *rmp;
72   int msg_size = sizeof (*rmp);
73   vl_api_registration_t *rp;
74
75   rp = vl_api_client_index_to_registration (mp->client_index);
76   if (rp == 0)
77     return;
78
79   rmp = vl_msg_api_alloc (msg_size);
80   clib_memset (rmp, 0, msg_size);
81   rmp->_vl_msg_id =
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);
86
87   vl_api_send_msg (rp, (u8 *) rmp);
88 }
89
90 static void
91 vl_api_svs_table_add_del_t_handler (vl_api_svs_table_add_del_t * mp)
92 {
93   vl_api_svs_table_add_del_reply_t *rmp;
94   fib_protocol_t fproto;
95   int rv = 0;
96
97   fproto = fib_proto_from_api_address_family (mp->af);
98
99   if (mp->is_add)
100     {
101       rv = svs_table_add (fproto, ntohl (mp->table_id));
102     }
103   else
104     {
105       rv = svs_table_delete (fproto, ntohl (mp->table_id));
106     }
107
108   REPLY_MACRO (VL_API_SVS_TABLE_ADD_DEL_REPLY + svs_base_msg_id);
109 }
110
111 static void
112 vl_api_svs_route_add_del_t_handler (vl_api_svs_route_add_del_t * mp)
113 {
114   vl_api_svs_route_add_del_reply_t *rmp;
115   fib_prefix_t pfx;
116   int rv = 0;
117
118   ip_prefix_decode (&mp->prefix, &pfx);
119
120   if (mp->is_add)
121     {
122       rv = svs_route_add (ntohl (mp->table_id), &pfx,
123                           ntohl (mp->source_table_id));
124     }
125   else
126     {
127       rv = svs_route_delete (ntohl (mp->table_id), &pfx);
128     }
129
130   REPLY_MACRO (VL_API_SVS_ROUTE_ADD_DEL_REPLY + svs_base_msg_id);
131 }
132
133 static void
134 vl_api_svs_enable_disable_t_handler (vl_api_svs_enable_disable_t * mp)
135 {
136   vl_api_svs_enable_disable_reply_t *rmp;
137   fib_protocol_t fproto;
138   int rv = 0;
139
140   VALIDATE_SW_IF_INDEX (mp);
141
142   fproto = fib_proto_from_api_address_family (mp->af);
143
144   if (mp->is_enable)
145     {
146       rv = svs_enable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
147     }
148   else
149     {
150       rv =
151         svs_disable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
152     }
153
154   BAD_SW_IF_INDEX_LABEL;
155   REPLY_MACRO (VL_API_SVS_ENABLE_DISABLE_REPLY + svs_base_msg_id);
156 }
157
158 typedef struct svs_dump_walk_ctx_t_
159 {
160   vl_api_registration_t *rp;
161   u32 context;
162 } svs_dump_walk_ctx_t;
163
164
165 static walk_rc_t
166 svs_send_details (fib_protocol_t fproto,
167                   u32 table_id, u32 sw_if_index, void *args)
168 {
169   vl_api_svs_details_t *mp;
170   svs_dump_walk_ctx_t *ctx;
171
172   ctx = args;
173
174   mp = vl_msg_api_alloc (sizeof (*mp));
175   mp->_vl_msg_id = ntohs (VL_API_SVS_DETAILS + svs_base_msg_id);
176
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);
181
182   vl_api_send_msg (ctx->rp, (u8 *) mp);
183
184   return (WALK_CONTINUE);
185 }
186
187 static void
188 vl_api_svs_dump_t_handler (vl_api_svs_dump_t * mp)
189 {
190   vl_api_registration_t *rp;
191
192   rp = vl_api_client_index_to_registration (mp->client_index);
193   if (rp == 0)
194     return;
195
196   svs_dump_walk_ctx_t ctx = {
197     .rp = rp,
198     .context = mp->context,
199   };
200
201   svs_walk (svs_send_details, &ctx);
202 }
203
204 #define vl_msg_name_crc_list
205 #include <svs/svs_all_api_h.h>
206 #undef vl_msg_name_crc_list
207
208 /* Set up the API message handling tables */
209 static clib_error_t *
210 svs_plugin_api_hookup (vlib_main_t * vm)
211 {
212 #define _(N,n)                                                  \
213     vl_msg_api_set_handlers((VL_API_##N + svs_base_msg_id),     \
214                             #n,                                 \
215                             vl_api_##n##_t_handler,             \
216                             vl_noop_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;
221 #undef _
222
223   return 0;
224 }
225
226 static void
227 setup_message_id_table (api_main_t * apim)
228 {
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;
232 #undef _
233 }
234
235 static clib_error_t *
236 svs_api_init (vlib_main_t * vm)
237 {
238   clib_error_t *error = 0;
239
240   u8 *name = format (0, "svs_%08x%c", api_version, 0);
241
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);
245
246   error = svs_plugin_api_hookup (vm);
247
248   /* Add our API messages to the global name_crc hash table */
249   setup_message_id_table (&api_main);
250
251   vec_free (name);
252
253   return error;
254 }
255
256 VLIB_INIT_FUNCTION (svs_api_init);
257
258 /* *INDENT-OFF* */
259 VLIB_PLUGIN_REGISTER () = {
260     .version = VPP_BUILD_VER,
261     .description = "Source VRF Select",
262 };
263 /* *INDENT-ON* */
264
265 /*
266  * fd.io coding-style-patch-verification: ON
267  *
268  * Local Variables:
269  * eval: (c-set-style "gnu")
270  * End:
271  */