2 #include <builtinurl/builtinurl.h>
3 #include <http_static/http_static.h>
4 #include <mactime/mactime.h>
5 #include <vlib/unix/plugin.h>
8 handle_get_mactime (u8 * request, http_session_t * hs)
10 mactime_main_t *mm = &mactime_main;
14 u32 *pool_indices = 0;
15 int current_status = 99;
18 vlib_counter_t allow, drop;
19 ethernet_arp_ip4_entry_t *n, *pool;
24 vec_reset_length (mm->arp_cache_copy);
25 pool = ip4_neighbors_pool ();
27 pool_foreach (n, pool, (
29 vec_add1 (mm->arp_cache_copy, n[0]);}));
31 now = clib_timebase_now (&mm->timebase);
33 if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
34 mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
37 pool_foreach (dp, mm->devices,
39 vec_add1 (pool_indices, dp - mm->devices);
43 s = format (s, "{%smactime%s: [\n", q, q);
45 for (i = 0; i < vec_len (pool_indices); i++)
47 dp = pool_elt_at_index (mm->devices, pool_indices[i]);
49 /* Check dynamic ranges */
50 for (j = 0; j < vec_len (dp->ranges); j++)
52 clib_timebase_range_t *r = dp->ranges + j;
55 start0 = r->start + mm->sunday_midnight;
56 end0 = r->end + mm->sunday_midnight;
58 if (now >= start0 && now <= end0)
60 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
62 else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
69 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
71 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
73 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
75 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
77 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
81 vec_reset_length (macstring);
83 macstring = format (0, "%U", format_mac_address, dp->mac_address);
86 s = format (s, "},\n");
89 s = format (s, "{%smac_address%s: %s%s%s, ", q, q, q, macstring, q);
91 switch (current_status)
94 status_string = "static drop";
97 status_string = "static allow";
100 status_string = "dynamic drop";
103 status_string = "dynamic allow";
106 status_string = "d-quota inact";
109 status_string = "d-quota activ";
112 status_string = "code bug!";
115 vlib_get_combined_counter (&mm->allow_counters, dp - mm->devices,
117 vlib_get_combined_counter (&mm->drop_counters, dp - mm->devices, &drop);
118 s = format (s, "%sname%s: %s%s%s, %sstatus%s: %s%s%s,",
119 q, q, q, dp->device_name, q, q, q, q, status_string, q);
120 s = format (s, "%sallow_pkts%s: %lld,", q, q, allow.packets);
121 s = format (s, "%sallow_bytes%s: %lld,", q, q, allow.bytes);
122 s = format (s, "%sdrop_pkts%s: %lld", q, q, drop.packets);
124 for (j = 0; j < vec_len (mm->arp_cache_copy); j++)
126 n = mm->arp_cache_copy + j;
127 if (!memcmp (dp->mac_address, n->mac.bytes, sizeof (n->mac)))
129 s = format (s, ", %sip4_address%s: %s%U%s", q, q,
130 q, format_ip4_address, &n->ip4_address, q);
136 s = format (s, "}\n");
137 s = format (s, "]}\n");
138 vec_free (macstring);
139 vec_free (pool_indices);
143 hs->cache_pool_index = ~0;
149 mactime_url_init (vlib_main_t * vm)
151 void (*fp) (void *, char *, int);
153 /* Look up the builtin URL registration handler */
154 fp = vlib_get_plugin_symbol ("http_static_plugin.so",
155 "http_static_server_register_builtin_handler");
159 clib_warning ("http_static_plugin.so not loaded...");
163 (*fp) (handle_get_mactime, "mactime.json", HTTP_BUILTIN_METHOD_GET);
167 * fd.io coding-style-patch-verification: ON
170 * eval: (c-set-style "gnu")