Driver level time-based src mac filter
[vpp.git] / src / plugins / mactime / mactime_test.c
1 /*
2  * mactime.c - skeleton vpp-api-test plug-in
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <vat/vat.h>
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vppinfra/error.h>
21 #include <vppinfra/time_range.h>
22
23 uword vat_unformat_sw_if_index (unformat_input_t * input, va_list * args);
24
25 /* Declare message IDs */
26 #include <mactime/mactime_msg_enum.h>
27
28 /* define message structures */
29 #define vl_typedefs
30 #include <mactime/mactime_all_api_h.h>
31 #undef vl_typedefs
32
33 /* declare message handlers for each api */
34
35 #define vl_endianfun            /* define message structures */
36 #include <mactime/mactime_all_api_h.h>
37 #undef vl_endianfun
38
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...)
41 #define vl_printfun
42 #include <mactime/mactime_all_api_h.h>
43 #undef vl_printfun
44
45 /* Get the API version number. */
46 #define vl_api_version(n,v) static u32 api_version=(v);
47 #include <mactime/mactime_all_api_h.h>
48 #undef vl_api_version
49
50 typedef struct
51 {
52   /* API message ID base */
53   u16 msg_id_base;
54   vat_main_t *vat_main;
55 } mactime_test_main_t;
56
57 mactime_test_main_t mactime_test_main;
58
59 #define __plugin_msg_base mactime_test_main.msg_id_base
60 #include <vlibapi/vat_helper_macros.h>
61
62 #define foreach_standard_reply_retval_handler   \
63 _(mactime_enable_disable_reply)                 \
64 _(mactime_add_del_range_reply)
65
66 #define _(n)                                            \
67     static void vl_api_##n##_t_handler                  \
68     (vl_api_##n##_t * mp)                               \
69     {                                                   \
70         vat_main_t * vam = mactime_test_main.vat_main;   \
71         i32 retval = ntohl(mp->retval);                 \
72         if (vam->async_mode) {                          \
73             vam->async_errors += (retval < 0);          \
74         } else {                                        \
75             vam->retval = retval;                       \
76             vam->result_ready = 1;                      \
77         }                                               \
78     }
79 foreach_standard_reply_retval_handler;
80 #undef _
81
82 static u8 *
83 format_mac_address (u8 * s, va_list * args)
84 {
85   u8 *a = va_arg (*args, u8 *);
86   return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
87                  a[0], a[1], a[2], a[3], a[4], a[5]);
88 }
89
90 static uword
91 unformat_mac_address (unformat_input_t * input, va_list * args)
92 {
93   u8 *a = va_arg (*args, u8 *);
94   return unformat (input, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3],
95                    &a[4], &a[5]);
96 }
97
98 /*
99  * Table of message reply handlers, must include boilerplate handlers
100  * we just generated
101  */
102 #define foreach_vpe_api_reply_msg                               \
103 _(MACTIME_ENABLE_DISABLE_REPLY, mactime_enable_disable_reply)   \
104 _(MACTIME_ADD_DEL_RANGE_REPLY, mactime_add_del_range_reply)
105
106 static int
107 api_mactime_enable_disable (vat_main_t * vam)
108 {
109   unformat_input_t *i = vam->input;
110   int enable_disable = 1;
111   u32 sw_if_index = ~0;
112   vl_api_mactime_enable_disable_t *mp;
113   int ret;
114
115   /* Parse args required to build the message */
116   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
117     {
118       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
119         ;
120       else if (unformat (i, "sw_if_index %d", &sw_if_index))
121         ;
122       else if (unformat (i, "disable"))
123         enable_disable = 0;
124       else
125         break;
126     }
127
128   if (sw_if_index == ~0)
129     {
130       errmsg ("missing interface name / explicit sw_if_index number \n");
131       return -99;
132     }
133
134   /* Construct the API message */
135   M (MACTIME_ENABLE_DISABLE, mp);
136   mp->sw_if_index = ntohl (sw_if_index);
137   mp->enable_disable = enable_disable;
138
139   /* send it... */
140   S (mp);
141
142   /* Wait for a reply... */
143   W (ret);
144   return ret;
145 }
146
147 static int
148 api_mactime_add_del_range (vat_main_t * vam)
149 {
150   unformat_input_t *i = vam->input;
151   vl_api_mactime_add_del_range_t *mp;
152   u8 mac_address[8];
153   u8 *device_name = 0;
154   clib_timebase_range_t *rp = 0;
155   int name_set = 0;
156   int mac_set = 0;
157   u8 is_add = 1;
158   u8 allow = 0;
159   u8 drop = 0;
160   int ret;
161   int ii;
162
163   /* Parse args required to build the message */
164   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
165     {
166       if (unformat (i, "name %s", &device_name))
167         {
168           vec_add1 (device_name, 0);
169           name_set = 1;
170         }
171       else if (unformat (i, "allow-range %U",
172                          unformat_clib_timebase_range_vector, &rp))
173         allow = 1;
174       else if (unformat (i, "drop-range %U",
175                          unformat_clib_timebase_range_vector, &rp))
176         drop = 1;
177       else if (unformat (i, "allow-static"))
178         allow = 1;
179       else if (unformat (i, "drop-static"))
180         drop = 1;
181       else if (unformat (i, "mac %U", unformat_mac_address, mac_address))
182         mac_set = 1;
183       else if (unformat (i, "del"))
184         is_add = 0;
185       else
186         break;
187     }
188
189   /* Sanity checks */
190   if (mac_set == 0)
191     {
192       vec_free (rp);
193       vec_free (device_name);
194       errmsg ("mac address required, not set\n");
195       return -99;
196     }
197
198   /* allow-range / drop-range parse errors cause this condition */
199   if (is_add && allow == 0 && drop == 0)
200     {
201       vec_free (rp);
202       vec_free (device_name);
203       errmsg ("neither allow nor drop set, parse error...\n");
204     }
205
206   /* Unlikely, but check anyhow */
207   if (vec_len (device_name) > ARRAY_LEN (mp->device_name))
208     {
209       vec_free (rp);
210       vec_free (device_name);
211       errmsg ("device name too long, max %d\n", ARRAY_LEN (mp->device_name));
212       return -99;
213     }
214
215   /* Cough up a device name if none set */
216   if (name_set == 0)
217     {
218       device_name = format (0, "mac %U%c", format_mac_address,
219                             mac_address, 0);
220     }
221
222   /* Construct the API message */
223   M2 (MACTIME_ADD_DEL_RANGE, mp, sizeof (rp[0]) * vec_len (rp));
224   mp->is_add = is_add;
225   mp->drop = drop;
226   mp->allow = allow;
227   memcpy (mp->mac_address, mac_address, sizeof (mp->mac_address));
228   memcpy (mp->device_name, device_name, vec_len (device_name));
229   mp->count = clib_host_to_net_u32 (vec_len (rp));
230
231   for (ii = 0; ii < vec_len (rp); ii++)
232     {
233       mp->ranges[ii].start = rp[ii].start;
234       mp->ranges[ii].end = rp[ii].end;
235     }
236
237   vec_free (rp);
238   vec_free (device_name);
239
240   /* send it... */
241   S (mp);
242
243   /* Wait for a reply... */
244   W (ret);
245   return ret;
246 }
247
248 /*
249  * List of messages that the api test plugin sends,
250  * and that the data plane plugin processes
251  */
252 #define foreach_vpe_api_msg                     \
253 _(mactime_enable_disable, "<intfc> [disable]")  \
254 _(mactime_add_del_range,                        \
255   "name <devname> mac <mac-addr> allow drop\n"  \
256   "allow-range Mon - Fri 9:00 - 17:00")
257
258 static void
259 mactime_api_hookup (vat_main_t * vam)
260 {
261   mactime_test_main_t *sm = &mactime_test_main;
262   /* Hook up handlers for replies from the data plane plug-in */
263 #define _(N,n)                                                  \
264     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
265                            #n,                                  \
266                            vl_api_##n##_t_handler,              \
267                            vl_noop_handler,                     \
268                            vl_api_##n##_t_endian,               \
269                            vl_api_##n##_t_print,                \
270                            sizeof(vl_api_##n##_t), 1);
271   foreach_vpe_api_reply_msg;
272 #undef _
273
274   /* API messages we can send */
275 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
276   foreach_vpe_api_msg;
277 #undef _
278
279   /* Help strings */
280 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
281   foreach_vpe_api_msg;
282 #undef _
283 }
284
285 clib_error_t *
286 vat_plugin_register (vat_main_t * vam)
287 {
288   mactime_test_main_t *sm = &mactime_test_main;
289   u8 *name;
290
291   sm->vat_main = vam;
292
293   /* Ask the vpp engine for the first assigned message-id */
294   name = format (0, "mactime_%08x%c", api_version, 0);
295   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
296
297   if (sm->msg_id_base != (u16) ~ 0)
298     mactime_api_hookup (vam);
299
300   vec_free (name);
301
302   return 0;
303 }
304
305 /*
306  * fd.io coding-style-patch-verification: ON
307  *
308  * Local Variables:
309  * eval: (c-set-style "gnu")
310  * End:
311  */