Line data Source code
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 : #include <mactime/mactime_device.h>
24 : #include <vpp-api/client/stat_client.h>
25 :
26 : /* Declare message IDs */
27 : #include <vnet/format_fns.h>
28 : #include <mactime/mactime.api_enum.h>
29 : #include <mactime/mactime.api_types.h>
30 :
31 : typedef struct
32 : {
33 : /* device table */
34 : mactime_device_t *devices;
35 : uword *device_by_device_name;
36 : u32 vpp_table_epoch;
37 :
38 : /* time range setup */
39 : f64 sunday_midnight;
40 : clib_timebase_t timebase;
41 : f64 timezone_offset;
42 :
43 : /* API message ID base */
44 : u16 msg_id_base;
45 : vat_main_t *vat_main;
46 : } mactime_test_main_t;
47 :
48 : mactime_test_main_t mactime_test_main;
49 :
50 : #define __plugin_msg_base mactime_test_main.msg_id_base
51 : #include <vlibapi/vat_helper_macros.h>
52 :
53 : static int
54 0 : api_mactime_enable_disable (vat_main_t * vam)
55 : {
56 0 : unformat_input_t *i = vam->input;
57 0 : int enable_disable = 1;
58 0 : u32 sw_if_index = ~0;
59 : vl_api_mactime_enable_disable_t *mp;
60 : int ret;
61 :
62 : /* Parse args required to build the message */
63 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
64 : {
65 0 : if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
66 : ;
67 0 : else if (unformat (i, "sw_if_index %d", &sw_if_index))
68 : ;
69 0 : else if (unformat (i, "disable"))
70 0 : enable_disable = 0;
71 : else
72 0 : break;
73 : }
74 :
75 0 : if (sw_if_index == ~0)
76 : {
77 0 : errmsg ("missing interface name / explicit sw_if_index number \n");
78 0 : return -99;
79 : }
80 :
81 : /* Construct the API message */
82 0 : M (MACTIME_ENABLE_DISABLE, mp);
83 0 : mp->sw_if_index = ntohl (sw_if_index);
84 0 : mp->enable_disable = enable_disable;
85 :
86 : /* send it... */
87 0 : S (mp);
88 :
89 : /* Wait for a reply... */
90 0 : W (ret);
91 0 : return ret;
92 : }
93 :
94 : #if VPP_API_TEST_BUILTIN
95 : extern u8 *format_bytes_with_width (u8 * s, va_list * va);
96 : #else
97 : u8 *
98 0 : format_bytes_with_width (u8 * s, va_list * va)
99 : {
100 0 : uword nbytes = va_arg (*va, u64);
101 0 : int width = va_arg (*va, int);
102 : f64 nbytes_f64;
103 : u8 *fmt;
104 0 : char *suffix = "";
105 :
106 0 : if (width > 0)
107 0 : fmt = format (0, "%%%d.3f%%s%c", width, 0);
108 : else
109 0 : fmt = format (0, "%%.3f%%s%c", 0);
110 :
111 0 : if (nbytes > (1024ULL * 1024ULL * 1024ULL))
112 : {
113 0 : nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0 * 1024.0);
114 0 : suffix = "G";
115 : }
116 0 : else if (nbytes > (1024ULL * 1024ULL))
117 : {
118 0 : nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0);
119 0 : suffix = "M";
120 : }
121 0 : else if (nbytes > 1024ULL)
122 : {
123 0 : nbytes_f64 = ((f64) nbytes) / (1024.0);
124 0 : suffix = "K";
125 : }
126 : else
127 : {
128 0 : nbytes_f64 = (f64) nbytes;
129 0 : suffix = "B";
130 : }
131 :
132 0 : s = format (s, (char *) fmt, nbytes_f64, suffix);
133 0 : vec_free (fmt);
134 0 : return s;
135 : }
136 : #endif
137 :
138 : static u8 *
139 0 : format_device (u8 * s, va_list * args)
140 : {
141 0 : mactime_device_t *dp = va_arg (*args, mactime_device_t *);
142 0 : mactime_test_main_t *mm = &mactime_test_main;
143 0 : int verbose = va_arg (*args, int);
144 0 : int current_status = 99;
145 : char *status_string;
146 0 : u8 *macstring = 0;
147 : f64 now;
148 : int j;
149 :
150 0 : if (dp == 0)
151 : {
152 0 : s = format (s, "%-15s %5s %18s %14s %10s %11s %13s",
153 : "Device Name", "Index", "Addresses", "Status",
154 : "AllowPkt", "AllowByte", "DropPkt");
155 0 : vec_add1 (s, '\n');
156 0 : return s;
157 : }
158 :
159 0 : now = clib_timebase_now (&mm->timebase);
160 :
161 : /* Check dynamic ranges */
162 0 : for (j = 0; j < vec_len (dp->ranges); j++)
163 : {
164 0 : clib_timebase_range_t *r = dp->ranges + j;
165 : f64 start0, end0;
166 :
167 0 : start0 = r->start + mm->sunday_midnight;
168 0 : end0 = r->end + mm->sunday_midnight;
169 0 : if (verbose)
170 0 : s = format (s, " Range %d: %U - %U\n", j,
171 : format_clib_timebase_time, start0,
172 : format_clib_timebase_time, end0);
173 :
174 0 : if (now >= start0 && now <= end0)
175 : {
176 0 : if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
177 0 : current_status = 3;
178 0 : else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
179 0 : current_status = 5;
180 : else
181 0 : current_status = 2;
182 0 : if (verbose)
183 : {
184 0 : s = format (s, " Time in range %d:", j);
185 0 : s = format (s, " %U - %U\n",
186 : format_clib_timebase_time, start0,
187 : format_clib_timebase_time, end0);
188 : }
189 0 : goto print;
190 : }
191 : }
192 0 : if (verbose && j)
193 0 : s = format (s, " No range match.\n");
194 0 : if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
195 0 : current_status = 0;
196 0 : if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
197 0 : current_status = 1;
198 0 : if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
199 0 : current_status = 2;
200 0 : if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
201 0 : current_status = 3;
202 0 : if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
203 0 : current_status = 4;
204 :
205 0 : print:
206 0 : macstring = format (0, "%U", format_vl_api_mac_address_t, dp->mac_address);
207 0 : switch (current_status)
208 : {
209 0 : case 0:
210 0 : status_string = "static drop";
211 0 : break;
212 0 : case 1:
213 0 : status_string = "static allow";
214 0 : break;
215 0 : case 2:
216 0 : status_string = "dynamic drop";
217 0 : break;
218 0 : case 3:
219 0 : status_string = "dynamic allow";
220 0 : break;
221 0 : case 4:
222 0 : status_string = "d-quota inact";
223 0 : break;
224 0 : case 5:
225 0 : status_string = "d-quota activ";
226 0 : break;
227 0 : default:
228 0 : status_string = "code bug!";
229 0 : break;
230 : }
231 :
232 0 : s = format (s, "%-15s %5d %18s %14s\n",
233 : dp->device_name, dp->pool_index, macstring, status_string);
234 0 : vec_free (macstring);
235 :
236 0 : if (dp->data_quota > 0)
237 : {
238 0 : 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);
241 0 : vec_add1 (s, '\n');
242 : }
243 0 : return s;
244 : }
245 :
246 : static int
247 0 : api_mactime_dump (vat_main_t * vam)
248 : {
249 0 : mactime_test_main_t *tm = &mactime_test_main;
250 0 : unformat_input_t *i = vam->input;
251 : vl_api_mactime_dump_t *mp;
252 0 : int verbose = 0;
253 : int ret;
254 : f64 now;
255 : mactime_device_t *dev;
256 :
257 0 : now = clib_timebase_now (&tm->timebase);
258 :
259 0 : if (PREDICT_FALSE ((now - tm->sunday_midnight) > 86400.0 * 7.0))
260 0 : tm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
261 :
262 : /* Parse args required to build the message */
263 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
264 : {
265 0 : if (unformat (i, "force"))
266 0 : tm->vpp_table_epoch = 0;
267 0 : else if (unformat (i, "verbose"))
268 0 : verbose = 1;
269 : else
270 0 : break;
271 : }
272 :
273 : /* Construct the API message */
274 0 : M (MACTIME_DUMP, mp);
275 0 : mp->my_table_epoch = clib_host_to_net_u32 (tm->vpp_table_epoch);
276 :
277 : /* send it... */
278 0 : S (mp);
279 :
280 : /* Wait for a reply... */
281 0 : W (ret);
282 :
283 0 : fformat (vam->ofp, "%U", format_device, 0 /* header */ , 0 /* verbose */ );
284 : /* *INDENT-OFF* */
285 0 : pool_foreach (dev, tm->devices)
286 : {
287 0 : fformat (vam->ofp, "%U", format_device, dev, verbose);
288 : }
289 : /* *INDENT-ON* */
290 :
291 0 : return ret;
292 : }
293 :
294 : /* These two ought to be in a library somewhere but they aren't */
295 : static uword
296 0 : my_unformat_mac_address (unformat_input_t * input, va_list * args)
297 : {
298 0 : u8 *a = va_arg (*args, u8 *);
299 0 : return unformat (input, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3],
300 : &a[4], &a[5]);
301 : }
302 :
303 : static u8 *
304 0 : my_format_mac_address (u8 * s, va_list * args)
305 : {
306 0 : u8 *a = va_arg (*args, u8 *);
307 0 : return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
308 0 : a[0], a[1], a[2], a[3], a[4], a[5]);
309 : }
310 :
311 : static int
312 0 : api_mactime_add_del_range (vat_main_t * vam)
313 : {
314 0 : unformat_input_t *i = vam->input;
315 : vl_api_mactime_add_del_range_t *mp;
316 : u8 mac_address[8];
317 0 : u8 *device_name = 0;
318 0 : clib_timebase_range_t *rp = 0;
319 0 : int name_set = 0;
320 0 : int mac_set = 0;
321 0 : u8 is_add = 1;
322 0 : u8 allow = 0;
323 0 : u8 allow_quota = 0;
324 0 : u8 drop = 0;
325 0 : u8 no_udp_10001 = 0;
326 0 : u64 data_quota = 0;
327 : int ret;
328 : int ii;
329 :
330 : /* Parse args required to build the message */
331 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
332 : {
333 0 : if (unformat (i, "name %s", &device_name))
334 : {
335 0 : vec_add1 (device_name, 0);
336 0 : name_set = 1;
337 : }
338 0 : else if (unformat (i, "allow-range %U",
339 : unformat_clib_timebase_range_vector, &rp))
340 0 : allow = 1;
341 0 : else if (unformat (i, "allow-quota-range %U",
342 : unformat_clib_timebase_range_vector, &rp))
343 0 : allow_quota = 1;
344 0 : else if (unformat (i, "drop-range %U",
345 : unformat_clib_timebase_range_vector, &rp))
346 0 : drop = 1;
347 0 : else if (unformat (i, "allow-static"))
348 0 : allow = 1;
349 0 : else if (unformat (i, "drop-static"))
350 0 : drop = 1;
351 0 : else if (unformat (i, "no-udp-10001"))
352 0 : no_udp_10001 = 1;
353 0 : else if (unformat (i, "mac %U", my_unformat_mac_address, mac_address))
354 0 : mac_set = 1;
355 0 : else if (unformat (i, "del"))
356 0 : is_add = 0;
357 0 : else if (unformat (i, "data-quota %lldM", &data_quota))
358 0 : data_quota <<= 20;
359 0 : else if (unformat (i, "data-quota %lldG", &data_quota))
360 0 : data_quota <<= 30;
361 : else
362 0 : break;
363 : }
364 :
365 : /* Sanity checks */
366 0 : if (mac_set == 0)
367 : {
368 0 : vec_free (rp);
369 0 : vec_free (device_name);
370 0 : errmsg ("mac address required, not set\n");
371 0 : return -99;
372 : }
373 :
374 : /* allow-range / drop-range parse errors cause this condition */
375 0 : if (is_add && allow == 0 && drop == 0 && allow_quota == 0)
376 : {
377 0 : vec_free (rp);
378 0 : vec_free (device_name);
379 0 : errmsg ("parse error...\n");
380 0 : return -99;
381 : }
382 :
383 : /* Unlikely, but check anyhow */
384 0 : if (vec_len (device_name) > ARRAY_LEN (mp->device_name))
385 : {
386 0 : vec_free (rp);
387 0 : vec_free (device_name);
388 0 : errmsg ("device name too long, max %d\n", ARRAY_LEN (mp->device_name));
389 0 : return -99;
390 : }
391 :
392 : /* Cough up a device name if none set */
393 0 : if (name_set == 0)
394 : {
395 0 : device_name = format (0, "mac %U%c", my_format_mac_address,
396 : mac_address, 0);
397 : }
398 :
399 : /* Construct the API message */
400 0 : M2 (MACTIME_ADD_DEL_RANGE, mp, sizeof (rp[0]) * vec_len (rp));
401 0 : mp->is_add = is_add;
402 0 : mp->drop = drop;
403 0 : mp->allow = allow;
404 0 : mp->allow_quota = allow_quota;
405 0 : mp->no_udp_10001 = no_udp_10001;
406 0 : mp->data_quota = clib_host_to_net_u64 (data_quota);
407 0 : memcpy (mp->mac_address, mac_address, sizeof (mp->mac_address));
408 0 : memcpy (mp->device_name, device_name, vec_len (device_name));
409 0 : mp->count = clib_host_to_net_u32 (vec_len (rp));
410 :
411 0 : for (ii = 0; ii < vec_len (rp); ii++)
412 : {
413 0 : mp->ranges[ii].start = rp[ii].start;
414 0 : mp->ranges[ii].end = rp[ii].end;
415 : }
416 :
417 0 : vec_free (rp);
418 0 : vec_free (device_name);
419 :
420 : /* send it... */
421 0 : S (mp);
422 :
423 : /* Wait for a reply... */
424 0 : W (ret);
425 0 : return ret;
426 : }
427 :
428 : /* We shouldn't get these... */
429 : static void
430 0 : vl_api_mactime_details_t_handler (vl_api_mactime_details_t * mp)
431 : {
432 0 : clib_warning ("WARNING: stub called...");
433 0 : }
434 :
435 : #include <mactime/mactime.api_test.c>
436 :
437 : /*
438 : * fd.io coding-style-patch-verification: ON
439 : *
440 : * Local Variables:
441 : * eval: (c-set-style "gnu")
442 : * End:
443 : */
|