Source VRF Select
[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
58 #include <vlibapi/api_helper_macros.h>
59
60 /* List of message types that this plugin understands */
61
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)           \
67   _(SVS_DUMP, svs_dump)
68
69 static void
70 vl_api_svs_plugin_get_version_t_handler (vl_api_svs_plugin_get_version_t * mp)
71 {
72   vl_api_svs_plugin_get_version_reply_t *rmp;
73   int msg_size = sizeof (*rmp);
74   unix_shared_memory_queue_t *q;
75
76   q = vl_api_client_index_to_input_queue (mp->client_index);
77   if (q == 0)
78     {
79       return;
80     }
81
82   rmp = vl_msg_api_alloc (msg_size);
83   memset (rmp, 0, msg_size);
84   rmp->_vl_msg_id =
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);
89
90   vl_msg_api_send_shmem (q, (u8 *) & rmp);
91 }
92
93 static void
94 vl_api_svs_table_add_del_t_handler (vl_api_svs_table_add_del_t * mp)
95 {
96   vl_api_svs_table_add_del_reply_t *rmp;
97   fib_protocol_t fproto;
98   int rv = 0;
99
100   fproto = fib_proto_from_api_address_family (mp->af);
101
102   if (mp->is_add)
103     {
104       svs_table_add (fproto, ntohl (mp->table_id));
105     }
106   else
107     {
108       svs_table_delete (fproto, ntohl (mp->table_id));
109     }
110
111   REPLY_MACRO (VL_API_SVS_TABLE_ADD_DEL_REPLY + svs_base_msg_id);
112 }
113
114 static void
115 vl_api_svs_route_add_del_t_handler (vl_api_svs_route_add_del_t * mp)
116 {
117   vl_api_svs_route_add_del_reply_t *rmp;
118   fib_prefix_t pfx;
119   int rv = 0;
120
121   ip_prefix_decode (&mp->prefix, &pfx);
122
123   if (mp->is_add)
124     {
125       rv =
126         svs_route_add (ntohl (mp->table_id), &pfx,
127                        ntohl (mp->source_table_id));
128     }
129   else
130     {
131       rv = svs_route_delete (ntohl (mp->table_id), &pfx);
132     }
133
134   REPLY_MACRO (VL_API_SVS_ROUTE_ADD_DEL_REPLY + svs_base_msg_id);
135 }
136
137 static void
138 vl_api_svs_enable_disable_t_handler (vl_api_svs_enable_disable_t * mp)
139 {
140   vl_api_svs_enable_disable_reply_t *rmp;
141   fib_protocol_t fproto;
142   int rv = 0;
143
144   VALIDATE_SW_IF_INDEX (mp);
145
146   fproto = fib_proto_from_api_address_family (mp->af);
147
148   if (mp->is_enable)
149     {
150       svs_enable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
151     }
152   else
153     {
154       svs_disable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
155     }
156
157   BAD_SW_IF_INDEX_LABEL;
158   REPLY_MACRO (VL_API_SVS_ENABLE_DISABLE_REPLY + svs_base_msg_id);
159 }
160
161 typedef struct svs_dump_walk_ctx_t_
162 {
163   unix_shared_memory_queue_t *q;
164   u32 context;
165 } svs_dump_walk_ctx_t;
166
167
168 static walk_rc_t
169 svs_send_details (fib_protocol_t fproto,
170                   u32 table_id, u32 sw_if_index, void *args)
171 {
172   vl_api_svs_details_t *mp;
173   svs_dump_walk_ctx_t *ctx;
174
175   ctx = args;
176
177   mp = vl_msg_api_alloc (sizeof (*mp));
178   mp->_vl_msg_id = ntohs (VL_API_SVS_DETAILS + svs_base_msg_id);
179
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);
184
185   vl_msg_api_send_shmem (ctx->q, (u8 *) & mp);
186
187   return (WALK_CONTINUE);
188 }
189
190 static void
191 vl_api_svs_dump_t_handler (vl_api_svs_dump_t * mp)
192 {
193   unix_shared_memory_queue_t *q;
194
195   q = vl_api_client_index_to_input_queue (mp->client_index);
196   if (q == 0)
197     {
198       return;
199     }
200
201   svs_dump_walk_ctx_t ctx = {
202     .q = q,
203     .context = mp->context,
204   };
205
206   svs_walk (svs_send_details, &ctx);
207 }
208
209 #define vl_msg_name_crc_list
210 #include <svs/svs_all_api_h.h>
211 #undef vl_msg_name_crc_list
212
213 /* Set up the API message handling tables */
214 static clib_error_t *
215 svs_plugin_api_hookup (vlib_main_t * vm)
216 {
217 #define _(N,n)                                                  \
218     vl_msg_api_set_handlers((VL_API_##N + svs_base_msg_id),     \
219                             #n,                                 \
220                             vl_api_##n##_t_handler,             \
221                             vl_noop_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;
226 #undef _
227
228   return 0;
229 }
230
231 static void
232 setup_message_id_table (api_main_t * apim)
233 {
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;
237 #undef _
238 }
239
240 static clib_error_t *
241 svs_api_init (vlib_main_t * vm)
242 {
243   clib_error_t *error = 0;
244
245   u8 *name = format (0, "svs_%08x%c", api_version, 0);
246
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);
250
251   error = svs_plugin_api_hookup (vm);
252
253   /* Add our API messages to the global name_crc hash table */
254   setup_message_id_table (&api_main);
255
256   vec_free (name);
257
258   return error;
259 }
260
261 VLIB_INIT_FUNCTION (svs_api_init);
262
263 /* *INDENT-OFF* */
264 VLIB_PLUGIN_REGISTER () = {
265     .version = VPP_BUILD_VER,
266     .description = "Source VRF Select",
267 };
268 /* *INDENT-ON* */
269
270 /*
271  * fd.io coding-style-patch-verification: ON
272  *
273  * Local Variables:
274  * eval: (c-set-style "gnu")
275  * End:
276  */