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