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>
13 #include <vnet/format_fns.h>
14 #include <mactime/mactime.api_types.h>
15 #include <mactime/mactime.api_enum.h>
20 uword *device_by_device_name;
21 mactime_device_t *devices;
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;
32 clib_time_t clib_time;
33 clib_timebase_t timebase;
37 /* API message-handling */
38 svm_queue_t *vl_input_queue;
41 volatile u32 result_ready;
47 /* Indispensable for debugging in gdb... */
55 #define foreach_mactime_api_msg \
56 _(MACTIME_DUMP_REPLY, mactime_dump_reply) \
57 _(MACTIME_DETAILS, mactime_details)
59 static void vl_api_mactime_dump_reply_t_handler
60 (vl_api_mactime_dump_reply_t * mp)
62 mt_main_t *mm = &mt_main;
63 i32 retval = clib_net_to_host_u32 (mp->retval);
70 vl_api_mactime_details_t_handler (vl_api_mactime_details_t * mp)
72 mt_main_t *mm = &mt_main;
73 mactime_device_t *dev;
75 clib_timebase_range_t *rp;
78 if (PREDICT_FALSE (mm->device_by_device_name == 0))
79 mm->device_by_device_name = hash_create_string (0, sizeof (uword));
81 p = hash_get_mem (mm->device_by_device_name, mp->device_name);
83 dev = pool_elt_at_index (mm->devices, p[0]);
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,
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++)
103 vec_add2 (dev->ranges, rp, 1);
104 rp->start = mp->ranges[i].start;
105 rp->end = mp->ranges[i].end;
109 #define vl_endianfun /* define message structures */
110 #include <mactime/mactime.api.h>
113 /* instantiate all the print functions we know about */
115 #include <mactime/mactime.api.h>
118 #define vl_api_version(n,v) static u32 api_version = v;
119 #include <mactime/mactime.api.h>
120 #undef vl_api_version
123 connect_to_vpp (char *name)
125 api_main_t *am = vlibapi_get_main ();
126 mt_main_t *mm = &mt_main;
127 u8 *msg_base_lookup_name;
129 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
132 mm->vl_input_queue = am->shmem_hdr->vl_input_queue;
133 mm->my_client_index = am->my_client_index;
135 msg_base_lookup_name = format (0, "mactime_%08x%c", api_version, 0);
137 mm->msg_id_base = vl_client_get_first_plugin_msg_id
138 ((char *) msg_base_lookup_name);
140 vec_free (msg_base_lookup_name);
142 if (mm->msg_id_base == (u16) ~ 0)
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;
157 dump_mactime_table (mt_main_t * mm)
159 vl_api_mactime_dump_t *mp;
160 u32 deadman_counter = 1000;
162 /* Send the dump request */
163 mp = vl_msg_api_alloc (sizeof (*mp));
164 memset (mp, 0, sizeof (*mp));
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);
171 /* Wait up to 1 second for vpp to reply */
172 while (deadman_counter-- && mm->result_ready == 0)
175 if (mm->retval && (mm->retval != VNET_API_ERROR_NO_CHANGE))
176 clib_warning ("dump reply %d", mm->retval);
181 scrape_stats_segment (mt_main_t * mm)
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;
193 vec_reset_length (pool_indices);
195 pool_foreach (dev, mm->devices)
197 vec_add1 (pool_indices, dev->pool_index);
201 /* Nothing to do... */
202 if (vec_len (pool_indices) == 0)
207 /* Has directory been updated? */
208 if (mm->ls_result1 == 0 || (sm->shared_header->epoch != sm->current_epoch))
211 vec_free (mm->ls_result1);
212 mm->ls_result1 = stat_segment_ls (mm->pattern1);
215 stat_segment_access_start (&sa, sm);
217 ep = vec_elt_at_index (sm->directory_vector, mm->ls_result1[0]);
218 counters_by_thread = stat_segment_adjust (sm, ep->data);
220 for (i = 0; i < vec_len (pool_indices); i++)
222 u32 index = pool_indices[i];
224 vec_validate (mm->allow_counters, index);
225 mm->allow_counters[index].packets = 0;
226 mm->allow_counters[index].bytes = 0;
228 for (j = 0; j < vec_len (counters_by_thread); j++)
230 counters = stat_segment_adjust (sm, counters_by_thread[j]);
231 mm->allow_counters[index].packets += counters[index].packets;
232 mm->allow_counters[index].bytes += counters[index].bytes;
236 /* Ugh, segment changed during access. Try again */
237 if (stat_segment_access_end (&sa, sm))
240 /* Has directory been updated? */
241 if (mm->ls_result2 == 0 || need_update2)
243 vec_free (mm->ls_result2);
244 mm->ls_result2 = stat_segment_ls (mm->pattern2);
248 stat_segment_access_start (&sa, sm);
250 ep = vec_elt_at_index (sm->directory_vector, mm->ls_result2[0]);
251 counters_by_thread = stat_segment_adjust (sm, ep->data);
253 for (i = 0; i < vec_len (pool_indices); i++)
255 u32 index = pool_indices[i];
257 vec_validate (mm->drop_counters, index);
258 mm->drop_counters[index].packets = 0;
259 mm->drop_counters[index].bytes = 0;
261 for (j = 0; j < vec_len (counters_by_thread); j++)
263 counters = stat_segment_adjust (sm, counters_by_thread[j]);
264 mm->drop_counters[index].packets += counters[index].packets;
265 mm->drop_counters[index].bytes += counters[index].bytes;
268 /* Ugh, segment changed during access. Try again */
269 if (stat_segment_access_end (&sa, sm))
274 format_mac_address (u8 * s, va_list * args)
276 u8 *a = va_arg (*args, u8 *);
278 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
279 a[0], a[1], a[2], a[3], a[4], a[5]);
283 format_bytes_with_width (u8 * s, va_list * va)
285 uword nbytes = va_arg (*va, u64);
286 int width = va_arg (*va, int);
292 fmt = format (0, "%%%d.3f%%s%c", width, 0);
294 fmt = format (0, "%%.3f%%s%c", 0);
296 if (nbytes > (1024ULL * 1024ULL * 1024ULL))
298 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0 * 1024.0);
301 else if (nbytes > (1024ULL * 1024ULL))
303 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0);
306 else if (nbytes > 1024ULL)
308 nbytes_f64 = ((f64) nbytes) / (1024.0);
313 nbytes_f64 = (f64) nbytes;
317 s = format (s, (char *) fmt, nbytes_f64, suffix);
323 format_device (u8 * s, va_list * args)
325 mactime_device_t *dp = va_arg (*args, mactime_device_t *);
326 mt_main_t *mm = &mt_main;
327 int verbose = va_arg (*args, int);
328 int current_status = 99;
336 s = format (s, "%-15s %5s %18s %14s %10s %11s %13s",
337 "Device Name", "Index", "Addresses", "Status",
338 "AllowPkt", "AllowByte", "DropPkt");
343 now = clib_timebase_now (&mm->timebase);
345 if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
346 mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
348 /* Check dynamic ranges */
349 for (j = 0; j < vec_len (dp->ranges); j++)
351 clib_timebase_range_t *r = dp->ranges + j;
354 start0 = r->start + mm->sunday_midnight;
355 end0 = r->end + mm->sunday_midnight;
357 s = format (s, " Range %d: %U - %U\n", j,
358 format_clib_timebase_time, start0,
359 format_clib_timebase_time, end0);
361 if (now >= start0 && now <= end0)
363 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
365 else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
371 s = format (s, " Time in range %d:", j);
372 s = format (s, " %U - %U\n",
373 format_clib_timebase_time, start0,
374 format_clib_timebase_time, end0);
380 s = format (s, " No range match.\n");
381 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
383 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
385 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
387 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
389 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
393 macstring = format (0, "%U", format_mac_address, dp->mac_address);
394 switch (current_status)
397 status_string = "static drop";
400 status_string = "static allow";
403 status_string = "dynamic drop";
406 status_string = "dynamic allow";
409 status_string = "d-quota inact";
412 status_string = "d-quota activ";
415 status_string = "code bug!";
419 s = format (s, "%-15s %5d %18s %14s %10lld %U %13lld\n",
420 dp->device_name, dp->pool_index, macstring, status_string,
421 mm->allow_counters[dp->pool_index].packets,
422 format_bytes_with_width,
423 mm->allow_counters[dp->pool_index].bytes, 10,
424 mm->drop_counters[dp->pool_index].packets);
425 vec_free (macstring);
427 if (dp->data_quota > 0)
429 s = format (s, "%-59s %s%U %s%U", " ", "Quota ",
430 format_bytes_with_width, dp->data_quota, 10,
431 "Use ", format_bytes_with_width, dp->data_used_in_range, 8);
438 print_device_table (mt_main_t * mm)
440 mactime_device_t *dev;
442 fformat (stdout, "%U", format_device, NULL /* header */, 0 /* verbose */);
444 pool_foreach (dev, mm->devices)
446 fformat (stdout, "%U", format_device, dev, 0 /* verbose */);
452 main (int argc, char **argv)
454 mt_main_t *mm = &mt_main;
455 extern stat_client_main_t stat_client_main;
457 clib_mem_init (0, 64 << 20);
459 if (connect_to_vpp ("mactime_top") < 0)
461 fformat (stderr, "vpp api client connect error\n");
465 if (stat_segment_connect (argv[1]) < 0)
467 fformat (stderr, "stat segment connect error");
471 mm->stat_client_main = (stat_client_main_t *) & stat_client_main;
473 /* US EDT - $$$ FIXME */
474 clib_time_init (&mm->clib_time);
475 mm->timezone_offset = -5.0;
476 clib_timebase_init (&mm->timebase, mm->timezone_offset,
477 CLIB_TIMEBASE_DAYLIGHT_USA,
478 0 /* allocate a clib_time_t */ );
480 vec_add1 (mm->pattern1, (u8 *) "^/mactime/allow");
481 vec_add1 (mm->pattern2, (u8 *) "^/mactime/drop");
485 dump_mactime_table (mm);
486 scrape_stats_segment (mm);
487 print_device_table (mm);
495 * fd.io coding-style-patch-verification: ON
498 * eval: (c-set-style "gnu")