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 <mactime/mactime.api_enum.h>
28 #include <mactime/mactime.api_types.h>
33 mactime_device_t *devices;
34 uword *device_by_device_name;
37 /* time range setup */
39 clib_timebase_t timebase;
42 /* API message ID base */
45 } mactime_test_main_t;
47 mactime_test_main_t mactime_test_main;
49 #define __plugin_msg_base mactime_test_main.msg_id_base
50 #include <vlibapi/vat_helper_macros.h>
53 api_mactime_enable_disable (vat_main_t * vam)
55 unformat_input_t *i = vam->input;
56 int enable_disable = 1;
58 vl_api_mactime_enable_disable_t *mp;
61 /* Parse args required to build the message */
62 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
64 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
66 else if (unformat (i, "sw_if_index %d", &sw_if_index))
68 else if (unformat (i, "disable"))
74 if (sw_if_index == ~0)
76 errmsg ("missing interface name / explicit sw_if_index number \n");
80 /* Construct the API message */
81 M (MACTIME_ENABLE_DISABLE, mp);
82 mp->sw_if_index = ntohl (sw_if_index);
83 mp->enable_disable = enable_disable;
88 /* Wait for a reply... */
93 #if VPP_API_TEST_BUILTIN
94 extern u8 *format_bytes_with_width (u8 * s, va_list * va);
97 format_bytes_with_width (u8 * s, va_list * va)
99 uword nbytes = va_arg (*va, u64);
100 int width = va_arg (*va, int);
106 fmt = format (0, "%%%d.3f%%s%c", width, 0);
108 fmt = format (0, "%%.3f%%s%c", 0);
110 if (nbytes > (1024ULL * 1024ULL * 1024ULL))
112 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0 * 1024.0);
115 else if (nbytes > (1024ULL * 1024ULL))
117 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0);
120 else if (nbytes > 1024ULL)
122 nbytes_f64 = ((f64) nbytes) / (1024.0);
127 nbytes_f64 = (f64) nbytes;
131 s = format (s, (char *) fmt, nbytes_f64, suffix);
138 format_device (u8 * s, va_list * args)
140 mactime_device_t *dp = va_arg (*args, mactime_device_t *);
141 mactime_test_main_t *mm = &mactime_test_main;
142 int verbose = va_arg (*args, int);
143 int current_status = 99;
151 s = format (s, "%-15s %5s %18s %14s %10s %11s %13s",
152 "Device Name", "Index", "Addresses", "Status",
153 "AllowPkt", "AllowByte", "DropPkt");
158 now = clib_timebase_now (&mm->timebase);
160 /* Check dynamic ranges */
161 for (j = 0; j < vec_len (dp->ranges); j++)
163 clib_timebase_range_t *r = dp->ranges + j;
166 start0 = r->start + mm->sunday_midnight;
167 end0 = r->end + mm->sunday_midnight;
169 s = format (s, " Range %d: %U - %U\n", j,
170 format_clib_timebase_time, start0,
171 format_clib_timebase_time, end0);
173 if (now >= start0 && now <= end0)
175 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
177 else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
183 s = format (s, " Time in range %d:", j);
184 s = format (s, " %U - %U\n",
185 format_clib_timebase_time, start0,
186 format_clib_timebase_time, end0);
192 s = format (s, " No range match.\n");
193 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
195 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
197 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
199 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
201 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
205 macstring = format (0, "%U", format_mac_address, dp->mac_address);
206 switch (current_status)
209 status_string = "static drop";
212 status_string = "static allow";
215 status_string = "dynamic drop";
218 status_string = "dynamic allow";
221 status_string = "d-quota inact";
224 status_string = "d-quota activ";
227 status_string = "code bug!";
231 s = format (s, "%-15s %5d %18s %14s\n",
232 dp->device_name, dp->pool_index, macstring, status_string);
233 vec_free (macstring);
235 if (dp->data_quota > 0)
237 s = format (s, "%-59s %s%U %s%U", " ", "Quota ",
238 format_bytes_with_width, dp->data_quota, 10,
239 "Use ", format_bytes_with_width, dp->data_used_in_range, 8);
246 api_mactime_dump (vat_main_t * vam)
248 mactime_test_main_t *tm = &mactime_test_main;
249 unformat_input_t *i = vam->input;
250 vl_api_mactime_dump_t *mp;
254 mactime_device_t *dev;
256 now = clib_timebase_now (&tm->timebase);
258 if (PREDICT_FALSE ((now - tm->sunday_midnight) > 86400.0 * 7.0))
259 tm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
261 /* Parse args required to build the message */
262 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
264 if (unformat (i, "force"))
265 tm->vpp_table_epoch = 0;
266 else if (unformat (i, "verbose"))
272 /* Construct the API message */
273 M (MACTIME_DUMP, mp);
274 mp->my_table_epoch = clib_host_to_net_u32 (tm->vpp_table_epoch);
279 /* Wait for a reply... */
282 fformat (vam->ofp, "%U", format_device, 0 /* header */ , 0 /* verbose */ );
284 pool_foreach (dev, tm->devices,
286 fformat (vam->ofp, "%U", format_device, dev, verbose);
293 /* These two ought to be in a library somewhere but they aren't */
295 my_unformat_mac_address (unformat_input_t * input, va_list * args)
297 u8 *a = va_arg (*args, u8 *);
298 return unformat (input, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3],
303 my_format_mac_address (u8 * s, va_list * args)
305 u8 *a = va_arg (*args, u8 *);
306 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
307 a[0], a[1], a[2], a[3], a[4], a[5]);
311 api_mactime_add_del_range (vat_main_t * vam)
313 unformat_input_t *i = vam->input;
314 vl_api_mactime_add_del_range_t *mp;
317 clib_timebase_range_t *rp = 0;
329 /* Parse args required to build the message */
330 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
332 if (unformat (i, "name %s", &device_name))
334 vec_add1 (device_name, 0);
337 else if (unformat (i, "allow-range %U",
338 unformat_clib_timebase_range_vector, &rp))
340 else if (unformat (i, "allow-quota-range %U",
341 unformat_clib_timebase_range_vector, &rp))
343 else if (unformat (i, "drop-range %U",
344 unformat_clib_timebase_range_vector, &rp))
346 else if (unformat (i, "allow-static"))
348 else if (unformat (i, "drop-static"))
350 else if (unformat (i, "no-udp-10001"))
352 else if (unformat (i, "mac %U", my_unformat_mac_address, mac_address))
354 else if (unformat (i, "del"))
356 else if (unformat (i, "data-quota %lldM", &data_quota))
358 else if (unformat (i, "data-quota %lldG", &data_quota))
368 vec_free (device_name);
369 errmsg ("mac address required, not set\n");
373 /* allow-range / drop-range parse errors cause this condition */
374 if (is_add && allow == 0 && drop == 0 && allow_quota == 0)
377 vec_free (device_name);
378 errmsg ("parse error...\n");
382 /* Unlikely, but check anyhow */
383 if (vec_len (device_name) > ARRAY_LEN (mp->device_name))
386 vec_free (device_name);
387 errmsg ("device name too long, max %d\n", ARRAY_LEN (mp->device_name));
391 /* Cough up a device name if none set */
394 device_name = format (0, "mac %U%c", my_format_mac_address,
398 /* Construct the API message */
399 M2 (MACTIME_ADD_DEL_RANGE, mp, sizeof (rp[0]) * vec_len (rp));
403 mp->allow_quota = allow_quota;
404 mp->no_udp_10001 = no_udp_10001;
405 mp->data_quota = clib_host_to_net_u64 (data_quota);
406 memcpy (mp->mac_address, mac_address, sizeof (mp->mac_address));
407 memcpy (mp->device_name, device_name, vec_len (device_name));
408 mp->count = clib_host_to_net_u32 (vec_len (rp));
410 for (ii = 0; ii < vec_len (rp); ii++)
412 mp->ranges[ii].start = rp[ii].start;
413 mp->ranges[ii].end = rp[ii].end;
417 vec_free (device_name);
422 /* Wait for a reply... */
427 /* We shouldn't get these... */
429 vl_api_mactime_details_t_handler (vl_api_mactime_details_t * mp)
431 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")