http: fix client parse error handling
[vpp.git] / src / plugins / mactime / mactime_top.c
1 #include <vppinfra/time.h>
2 #include <vppinfra/hash.h>
3 #include <vppinfra/pool.h>
4 #include <vpp-api/client/stat_client.h>
5 #include <vppinfra/vec.h>
6 #include <mactime/mactime_device.h>
7 #include <vlibapi/api_common.h>
8 #include <vlibmemory/memory_client.h>
9 #include <vlibmemory/api.h>
10 #include <vnet/api_errno.h>
11 #include <svm/queue.h>
12
13 #include <vnet/format_fns.h>
14 #include <mactime/mactime.api_types.h>
15 #include <mactime/mactime.api_enum.h>
16
17 typedef struct
18 {
19   /* device database */
20   uword *device_by_device_name;
21   mactime_device_t *devices;
22   u32 my_table_epoch;
23
24   /* Stat segment variables */
25   stat_client_main_t *stat_client_main;
26   u8 **pattern1, **pattern2;
27   u32 *ls_result1, *ls_result2;
28   vlib_counter_t *allow_counters;
29   vlib_counter_t *drop_counters;
30
31   /* Timebase */
32   clib_time_t clib_time;
33   clib_timebase_t timebase;
34   f64 timezone_offset;
35   f64 sunday_midnight;
36
37   /* API message-handling */
38   svm_queue_t *vl_input_queue;
39   u32 my_client_index;
40   u16 msg_id_base;
41   volatile u32 result_ready;
42   volatile i32 retval;
43 } mt_main_t;
44
45 mt_main_t mt_main;
46
47 /* Indispensable for debugging in gdb... */
48
49 u32
50 vl (void *x)
51 {
52   return vec_len (x);
53 }
54
55 #define foreach_mactime_api_msg                 \
56 _(MACTIME_DUMP_REPLY, mactime_dump_reply)       \
57 _(MACTIME_DETAILS, mactime_details)
58
59 static void vl_api_mactime_dump_reply_t_handler
60   (vl_api_mactime_dump_reply_t * mp)
61 {
62   mt_main_t *mm = &mt_main;
63   i32 retval = clib_net_to_host_u32 (mp->retval);
64
65   mm->retval = retval;
66   mm->result_ready = 1;
67 }
68
69 static void
70 vl_api_mactime_details_t_handler (vl_api_mactime_details_t * mp)
71 {
72   mt_main_t *mm = &mt_main;
73   mactime_device_t *dev;
74   int i;
75   clib_timebase_range_t *rp;
76   uword *p;
77
78   if (PREDICT_FALSE (mm->device_by_device_name == 0))
79     mm->device_by_device_name = hash_create_string (0, sizeof (uword));
80
81   p = hash_get_mem (mm->device_by_device_name, mp->device_name);
82   if (p)
83     dev = pool_elt_at_index (mm->devices, p[0]);
84   else
85     {
86       u8 *hash_name_copy = format (0, "%s%c", mp->device_name, 0);
87       pool_get (mm->devices, dev);
88       memset (dev, 0, sizeof (*dev));
89       dev->device_name = vec_dup (hash_name_copy);
90       hash_set_mem (mm->device_by_device_name, hash_name_copy,
91                     dev - mm->devices);
92     }
93
94   clib_memcpy_fast (dev->mac_address, mp->mac_address,
95                     sizeof (dev->mac_address));
96   dev->data_quota = clib_net_to_host_u64 (mp->data_quota);
97   dev->data_used_in_range = clib_net_to_host_u64 (mp->data_used_in_range);
98   dev->flags = clib_net_to_host_u32 (mp->flags);
99   dev->pool_index = clib_net_to_host_u32 (mp->pool_index);
100   vec_reset_length (dev->ranges);
101   for (i = 0; i < clib_net_to_host_u32 (mp->nranges); i++)
102     {
103       vec_add2 (dev->ranges, rp, 1);
104       rp->start = mp->ranges[i].start;
105       rp->end = mp->ranges[i].end;
106     }
107 }
108
109 #define vl_endianfun            /* define message structures */
110 #include <mactime/mactime.api.h>
111 #undef vl_endianfun
112
113 /* instantiate all the print functions we know about */
114 #define vl_printfun
115 #include <mactime/mactime.api.h>
116 #undef vl_printfun
117
118 #define vl_api_version(n,v) static u32 api_version = v;
119 #include <mactime/mactime.api.h>
120 #undef vl_api_version
121
122 static int
123 connect_to_vpp (char *name)
124 {
125   api_main_t *am = vlibapi_get_main ();
126   mt_main_t *mm = &mt_main;
127   u8 *msg_base_lookup_name;
128
129   if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
130     return -1;
131
132   mm->vl_input_queue = am->shmem_hdr->vl_input_queue;
133   mm->my_client_index = am->my_client_index;
134
135   msg_base_lookup_name = format (0, "mactime_%08x%c", api_version, 0);
136
137   mm->msg_id_base = vl_client_get_first_plugin_msg_id
138     ((char *) msg_base_lookup_name);
139
140   vec_free (msg_base_lookup_name);
141
142   if (mm->msg_id_base == (u16) ~ 0)
143     return -1;
144
145 #define _(N, n)                                                               \
146   vl_msg_api_set_handlers ((VL_API_##N + mm->msg_id_base), #n,                \
147                            vl_api_##n##_t_handler, vl_api_##n##_t_endian,     \
148                            vl_api_##n##_t_format, sizeof (vl_api_##n##_t), 1, \
149                            vl_api_##n##_t_tojson, vl_api_##n##_t_fromjson);
150   foreach_mactime_api_msg;
151 #undef _
152
153   return 0;
154 }
155
156 static void
157 dump_mactime_table (mt_main_t * mm)
158 {
159   vl_api_mactime_dump_t *mp;
160   u32 deadman_counter = 1000;
161
162   /* Send the dump request */
163   mp = vl_msg_api_alloc (sizeof (*mp));
164   memset (mp, 0, sizeof (*mp));
165   mp->_vl_msg_id =
166     clib_host_to_net_u16 (VL_API_MACTIME_DUMP + mm->msg_id_base);
167   mp->client_index = mm->my_client_index;
168   mp->my_table_epoch = mm->my_table_epoch;
169   vl_msg_api_send_shmem (mm->vl_input_queue, (u8 *) & mp);
170
171   /* Wait up to 1 second for vpp to reply */
172   while (deadman_counter-- && mm->result_ready == 0)
173     unix_sleep (1e-3);
174
175   if (mm->retval && (mm->retval != VNET_API_ERROR_NO_CHANGE))
176     clib_warning ("dump reply %d", mm->retval);
177
178 }
179
180 static void
181 scrape_stats_segment (mt_main_t * mm)
182 {
183   vlib_counter_t **counters_by_thread;
184   vlib_counter_t *counters;
185   mactime_device_t *dev;
186   stat_segment_access_t sa;
187   stat_client_main_t *sm = mm->stat_client_main;
188   vlib_stats_entry_t *ep;
189   int need_update2 = 0;
190   static u32 *pool_indices;
191   int i, j;
192
193   vec_reset_length (pool_indices);
194   pool_foreach (dev, mm->devices)
195    {
196     vec_add1 (pool_indices, dev->pool_index);
197   }
198
199   /* Nothing to do... */
200   if (vec_len (pool_indices) == 0)
201     return;
202
203 again1:
204
205   /* Has directory been updated? */
206   if (mm->ls_result1 == 0 || (sm->shared_header->epoch != sm->current_epoch))
207     {
208       need_update2 = 1;
209       vec_free (mm->ls_result1);
210       mm->ls_result1 = stat_segment_ls (mm->pattern1);
211     }
212
213   stat_segment_access_start (&sa, sm);
214
215   ep = vec_elt_at_index (sm->directory_vector, mm->ls_result1[0]);
216   counters_by_thread = stat_segment_adjust (sm, ep->data);
217
218   for (i = 0; i < vec_len (pool_indices); i++)
219     {
220       u32 index = pool_indices[i];
221
222       vec_validate (mm->allow_counters, index);
223       mm->allow_counters[index].packets = 0;
224       mm->allow_counters[index].bytes = 0;
225
226       for (j = 0; j < vec_len (counters_by_thread); j++)
227         {
228           counters = stat_segment_adjust (sm, counters_by_thread[j]);
229           mm->allow_counters[index].packets += counters[index].packets;
230           mm->allow_counters[index].bytes += counters[index].bytes;
231         }
232     }
233
234   /* Ugh, segment changed during access. Try again */
235   if (stat_segment_access_end (&sa, sm))
236     goto again1;
237
238   /* Has directory been updated? */
239   if (mm->ls_result2 == 0 || need_update2)
240     {
241       vec_free (mm->ls_result2);
242       mm->ls_result2 = stat_segment_ls (mm->pattern2);
243     }
244
245 again2:
246   stat_segment_access_start (&sa, sm);
247
248   ep = vec_elt_at_index (sm->directory_vector, mm->ls_result2[0]);
249   counters_by_thread = stat_segment_adjust (sm, ep->data);
250
251   for (i = 0; i < vec_len (pool_indices); i++)
252     {
253       u32 index = pool_indices[i];
254
255       vec_validate (mm->drop_counters, index);
256       mm->drop_counters[index].packets = 0;
257       mm->drop_counters[index].bytes = 0;
258
259       for (j = 0; j < vec_len (counters_by_thread); j++)
260         {
261           counters = stat_segment_adjust (sm, counters_by_thread[j]);
262           mm->drop_counters[index].packets += counters[index].packets;
263           mm->drop_counters[index].bytes += counters[index].bytes;
264         }
265     }
266   /* Ugh, segment changed during access. Try again */
267   if (stat_segment_access_end (&sa, sm))
268     goto again2;
269 }
270
271 static u8 *
272 format_mac_address (u8 * s, va_list * args)
273 {
274   u8 *a = va_arg (*args, u8 *);
275
276   return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
277                  a[0], a[1], a[2], a[3], a[4], a[5]);
278 }
279
280 static u8 *
281 format_bytes_with_width (u8 * s, va_list * va)
282 {
283   uword nbytes = va_arg (*va, u64);
284   int width = va_arg (*va, int);
285   f64 nbytes_f64;
286   u8 *fmt;
287   char *suffix = "";
288
289   if (width > 0)
290     fmt = format (0, "%%%d.3f%%s%c", width, 0);
291   else
292     fmt = format (0, "%%.3f%%s%c", 0);
293
294   if (nbytes > (1024ULL * 1024ULL * 1024ULL))
295     {
296       nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0 * 1024.0);
297       suffix = "G";
298     }
299   else if (nbytes > (1024ULL * 1024ULL))
300     {
301       nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0);
302       suffix = "M";
303     }
304   else if (nbytes > 1024ULL)
305     {
306       nbytes_f64 = ((f64) nbytes) / (1024.0);
307       suffix = "K";
308     }
309   else
310     {
311       nbytes_f64 = (f64) nbytes;
312       suffix = "B";
313     }
314
315   s = format (s, (char *) fmt, nbytes_f64, suffix);
316   vec_free (fmt);
317   return s;
318 }
319
320 static u8 *
321 format_device (u8 * s, va_list * args)
322 {
323   mactime_device_t *dp = va_arg (*args, mactime_device_t *);
324   mt_main_t *mm = &mt_main;
325   int verbose = va_arg (*args, int);
326   int current_status = 99;
327   char *status_string;
328   u8 *macstring = 0;
329   f64 now;
330   int j;
331
332   if (dp == 0)
333     {
334       s = format (s, "%-15s %5s %18s %14s %10s %11s %13s",
335                   "Device Name", "Index", "Addresses", "Status",
336                   "AllowPkt", "AllowByte", "DropPkt");
337       vec_add1 (s, '\n');
338       return s;
339     }
340
341   now = clib_timebase_now (&mm->timebase);
342
343   if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
344     mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
345
346   /* Check dynamic ranges */
347   for (j = 0; j < vec_len (dp->ranges); j++)
348     {
349       clib_timebase_range_t *r = dp->ranges + j;
350       f64 start0, end0;
351
352       start0 = r->start + mm->sunday_midnight;
353       end0 = r->end + mm->sunday_midnight;
354       if (verbose)
355         s = format (s, "  Range %d: %U - %U\n", j,
356                     format_clib_timebase_time, start0,
357                     format_clib_timebase_time, end0);
358
359       if (now >= start0 && now <= end0)
360         {
361           if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
362             current_status = 3;
363           else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
364             current_status = 5;
365           else
366             current_status = 2;
367           if (verbose)
368             {
369               s = format (s, "  Time in range %d:", j);
370               s = format (s, "     %U - %U\n",
371                           format_clib_timebase_time, start0,
372                           format_clib_timebase_time, end0);
373             }
374           goto print;
375         }
376     }
377   if (verbose && j)
378     s = format (s, "  No range match.\n");
379   if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
380     current_status = 0;
381   if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
382     current_status = 1;
383   if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
384     current_status = 2;
385   if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
386     current_status = 3;
387   if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
388     current_status = 4;
389
390 print:
391   macstring = format (0, "%U", format_mac_address, dp->mac_address);
392   switch (current_status)
393     {
394     case 0:
395       status_string = "static drop";
396       break;
397     case 1:
398       status_string = "static allow";
399       break;
400     case 2:
401       status_string = "dynamic drop";
402       break;
403     case 3:
404       status_string = "dynamic allow";
405       break;
406     case 4:
407       status_string = "d-quota inact";
408       break;
409     case 5:
410       status_string = "d-quota activ";
411       break;
412     default:
413       status_string = "code bug!";
414       break;
415     }
416
417   s = format (s, "%-15s %5d %18s %14s %10lld %U %13lld\n",
418               dp->device_name, dp->pool_index, macstring, status_string,
419               mm->allow_counters[dp->pool_index].packets,
420               format_bytes_with_width,
421               mm->allow_counters[dp->pool_index].bytes, 10,
422               mm->drop_counters[dp->pool_index].packets);
423   vec_free (macstring);
424
425   if (dp->data_quota > 0)
426     {
427       s = format (s, "%-59s %s%U %s%U", " ", "Quota ",
428                   format_bytes_with_width, dp->data_quota, 10,
429                   "Use ", format_bytes_with_width, dp->data_used_in_range, 8);
430       vec_add1 (s, '\n');
431     }
432   return s;
433 }
434
435 static void
436 print_device_table (mt_main_t * mm)
437 {
438   mactime_device_t *dev;
439
440   fformat (stdout, "%U", format_device, NULL /* header */, 0 /* verbose */);
441   pool_foreach (dev, mm->devices)
442    {
443     fformat (stdout, "%U", format_device, dev, 0 /* verbose */);
444   }
445 }
446
447 int
448 main (int argc, char **argv)
449 {
450   mt_main_t *mm = &mt_main;
451   extern stat_client_main_t stat_client_main;
452
453   clib_mem_init (0, 64 << 20);
454
455   if (connect_to_vpp ("mactime_top") < 0)
456     {
457       fformat (stderr, "vpp api client connect error\n");
458       exit (1);
459     }
460
461   if (stat_segment_connect (argv[1]) < 0)
462     {
463       fformat (stderr, "stat segment connect error");
464       exit (1);
465     }
466
467   mm->stat_client_main = (stat_client_main_t *) & stat_client_main;
468
469   /* US EDT - $$$ FIXME */
470   clib_time_init (&mm->clib_time);
471   mm->timezone_offset = -5.0;
472   clib_timebase_init (&mm->timebase, mm->timezone_offset,
473                       CLIB_TIMEBASE_DAYLIGHT_USA,
474                       0 /* allocate a clib_time_t */ );
475
476   vec_add1 (mm->pattern1, (u8 *) "^/mactime/allow");
477   vec_add1 (mm->pattern2, (u8 *) "^/mactime/drop");
478
479   while (1)
480     {
481       dump_mactime_table (mm);
482       scrape_stats_segment (mm);
483       print_device_table (mm);
484       unix_sleep (5.0);
485     }
486   return 0;
487 }
488
489
490 /*
491  * fd.io coding-style-patch-verification: ON
492  *
493  * Local Variables:
494  * eval: (c-set-style "gnu")
495  * End:
496  */