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