2 * mactime.c - skeleton vpp-api-test plug-in
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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 #include <mactime/mactime_device.h>
24 #include <vpp-api/client/stat_client.h>
26 /* Declare message IDs */
27 #include <vnet/format_fns.h>
28 #include <mactime/mactime.api_enum.h>
29 #include <mactime/mactime.api_types.h>
34 mactime_device_t *devices;
35 uword *device_by_device_name;
38 /* time range setup */
40 clib_timebase_t timebase;
43 /* API message ID base */
46 } mactime_test_main_t;
48 mactime_test_main_t mactime_test_main;
50 #define __plugin_msg_base mactime_test_main.msg_id_base
51 #include <vlibapi/vat_helper_macros.h>
54 api_mactime_enable_disable (vat_main_t * vam)
56 unformat_input_t *i = vam->input;
57 int enable_disable = 1;
59 vl_api_mactime_enable_disable_t *mp;
62 /* Parse args required to build the message */
63 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
65 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
67 else if (unformat (i, "sw_if_index %d", &sw_if_index))
69 else if (unformat (i, "disable"))
75 if (sw_if_index == ~0)
77 errmsg ("missing interface name / explicit sw_if_index number \n");
81 /* Construct the API message */
82 M (MACTIME_ENABLE_DISABLE, mp);
83 mp->sw_if_index = ntohl (sw_if_index);
84 mp->enable_disable = enable_disable;
89 /* Wait for a reply... */
94 #if VPP_API_TEST_BUILTIN
95 extern u8 *format_bytes_with_width (u8 * s, va_list * va);
98 format_bytes_with_width (u8 * s, va_list * va)
100 uword nbytes = va_arg (*va, u64);
101 int width = va_arg (*va, int);
107 fmt = format (0, "%%%d.3f%%s%c", width, 0);
109 fmt = format (0, "%%.3f%%s%c", 0);
111 if (nbytes > (1024ULL * 1024ULL * 1024ULL))
113 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0 * 1024.0);
116 else if (nbytes > (1024ULL * 1024ULL))
118 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0);
121 else if (nbytes > 1024ULL)
123 nbytes_f64 = ((f64) nbytes) / (1024.0);
128 nbytes_f64 = (f64) nbytes;
132 s = format (s, (char *) fmt, nbytes_f64, suffix);
139 format_device (u8 * s, va_list * args)
141 mactime_device_t *dp = va_arg (*args, mactime_device_t *);
142 mactime_test_main_t *mm = &mactime_test_main;
143 int verbose = va_arg (*args, int);
144 int current_status = 99;
152 s = format (s, "%-15s %5s %18s %14s %10s %11s %13s",
153 "Device Name", "Index", "Addresses", "Status",
154 "AllowPkt", "AllowByte", "DropPkt");
159 now = clib_timebase_now (&mm->timebase);
161 /* Check dynamic ranges */
162 for (j = 0; j < vec_len (dp->ranges); j++)
164 clib_timebase_range_t *r = dp->ranges + j;
167 start0 = r->start + mm->sunday_midnight;
168 end0 = r->end + mm->sunday_midnight;
170 s = format (s, " Range %d: %U - %U\n", j,
171 format_clib_timebase_time, start0,
172 format_clib_timebase_time, end0);
174 if (now >= start0 && now <= end0)
176 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
178 else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
184 s = format (s, " Time in range %d:", j);
185 s = format (s, " %U - %U\n",
186 format_clib_timebase_time, start0,
187 format_clib_timebase_time, end0);
193 s = format (s, " No range match.\n");
194 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
196 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
198 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
200 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
202 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
206 macstring = format (0, "%U", format_vl_api_mac_address_t, dp->mac_address);
207 switch (current_status)
210 status_string = "static drop";
213 status_string = "static allow";
216 status_string = "dynamic drop";
219 status_string = "dynamic allow";
222 status_string = "d-quota inact";
225 status_string = "d-quota activ";
228 status_string = "code bug!";
232 s = format (s, "%-15s %5d %18s %14s\n",
233 dp->device_name, dp->pool_index, macstring, status_string);
234 vec_free (macstring);
236 if (dp->data_quota > 0)
238 s = format (s, "%-59s %s%U %s%U", " ", "Quota ",
239 format_bytes_with_width, dp->data_quota, 10,
240 "Use ", format_bytes_with_width, dp->data_used_in_range, 8);
247 api_mactime_dump (vat_main_t * vam)
249 mactime_test_main_t *tm = &mactime_test_main;
250 unformat_input_t *i = vam->input;
251 vl_api_mactime_dump_t *mp;
255 mactime_device_t *dev;
257 now = clib_timebase_now (&tm->timebase);
259 if (PREDICT_FALSE ((now - tm->sunday_midnight) > 86400.0 * 7.0))
260 tm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
262 /* Parse args required to build the message */
263 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
265 if (unformat (i, "force"))
266 tm->vpp_table_epoch = 0;
267 else if (unformat (i, "verbose"))
273 /* Construct the API message */
274 M (MACTIME_DUMP, mp);
275 mp->my_table_epoch = clib_host_to_net_u32 (tm->vpp_table_epoch);
280 /* Wait for a reply... */
283 fformat (vam->ofp, "%U", format_device, 0 /* header */ , 0 /* verbose */ );
285 pool_foreach (dev, tm->devices,
287 fformat (vam->ofp, "%U", format_device, dev, verbose);
294 /* These two ought to be in a library somewhere but they aren't */
296 my_unformat_mac_address (unformat_input_t * input, va_list * args)
298 u8 *a = va_arg (*args, u8 *);
299 return unformat (input, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3],
304 my_format_mac_address (u8 * s, va_list * args)
306 u8 *a = va_arg (*args, u8 *);
307 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
308 a[0], a[1], a[2], a[3], a[4], a[5]);
312 api_mactime_add_del_range (vat_main_t * vam)
314 unformat_input_t *i = vam->input;
315 vl_api_mactime_add_del_range_t *mp;
318 clib_timebase_range_t *rp = 0;
330 /* Parse args required to build the message */
331 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
333 if (unformat (i, "name %s", &device_name))
335 vec_add1 (device_name, 0);
338 else if (unformat (i, "allow-range %U",
339 unformat_clib_timebase_range_vector, &rp))
341 else if (unformat (i, "allow-quota-range %U",
342 unformat_clib_timebase_range_vector, &rp))
344 else if (unformat (i, "drop-range %U",
345 unformat_clib_timebase_range_vector, &rp))
347 else if (unformat (i, "allow-static"))
349 else if (unformat (i, "drop-static"))
351 else if (unformat (i, "no-udp-10001"))
353 else if (unformat (i, "mac %U", my_unformat_mac_address, mac_address))
355 else if (unformat (i, "del"))
357 else if (unformat (i, "data-quota %lldM", &data_quota))
359 else if (unformat (i, "data-quota %lldG", &data_quota))
369 vec_free (device_name);
370 errmsg ("mac address required, not set\n");
374 /* allow-range / drop-range parse errors cause this condition */
375 if (is_add && allow == 0 && drop == 0 && allow_quota == 0)
378 vec_free (device_name);
379 errmsg ("parse error...\n");
383 /* Unlikely, but check anyhow */
384 if (vec_len (device_name) > ARRAY_LEN (mp->device_name))
387 vec_free (device_name);
388 errmsg ("device name too long, max %d\n", ARRAY_LEN (mp->device_name));
392 /* Cough up a device name if none set */
395 device_name = format (0, "mac %U%c", my_format_mac_address,
399 /* Construct the API message */
400 M2 (MACTIME_ADD_DEL_RANGE, mp, sizeof (rp[0]) * vec_len (rp));
404 mp->allow_quota = allow_quota;
405 mp->no_udp_10001 = no_udp_10001;
406 mp->data_quota = clib_host_to_net_u64 (data_quota);
407 memcpy (mp->mac_address, mac_address, sizeof (mp->mac_address));
408 memcpy (mp->device_name, device_name, vec_len (device_name));
409 mp->count = clib_host_to_net_u32 (vec_len (rp));
411 for (ii = 0; ii < vec_len (rp); ii++)
413 mp->ranges[ii].start = rp[ii].start;
414 mp->ranges[ii].end = rp[ii].end;
418 vec_free (device_name);
423 /* Wait for a reply... */
428 /* We shouldn't get these... */
430 vl_api_mactime_details_t_handler (vl_api_mactime_details_t * mp)
432 clib_warning ("WARNING: stub called...");
435 #include <mactime/mactime.api_test.c>
438 * fd.io coding-style-patch-verification: ON
441 * eval: (c-set-style "gnu")