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 (http_builtin_method_type_t reqtype,
9 u8 * request, http_session_t * hs)
11 mactime_main_t *mm = &mactime_main;
15 u32 *pool_indices = 0;
16 int current_status = 99;
19 vlib_counter_t allow, drop;
20 ethernet_arp_ip4_entry_t *n, *pool;
25 vec_reset_length (mm->arp_cache_copy);
26 pool = ip4_neighbors_pool ();
29 pool_foreach (n, pool, ({ vec_add1 (mm->arp_cache_copy, n[0]);}));
32 now = clib_timebase_now (&mm->timebase);
34 if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
35 mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
38 pool_foreach (dp, mm->devices,
40 vec_add1 (pool_indices, dp - mm->devices);
44 s = format (s, "{%smactime%s: [\n", q, q);
46 for (i = 0; i < vec_len (pool_indices); i++)
48 dp = pool_elt_at_index (mm->devices, pool_indices[i]);
50 /* Check dynamic ranges */
51 for (j = 0; j < vec_len (dp->ranges); j++)
53 clib_timebase_range_t *r = dp->ranges + j;
56 start0 = r->start + mm->sunday_midnight;
57 end0 = r->end + mm->sunday_midnight;
59 if (now >= start0 && now <= end0)
61 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
63 else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
70 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
72 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
74 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
76 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
78 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
82 vec_reset_length (macstring);
84 macstring = format (0, "%U", format_mac_address, dp->mac_address);
87 s = format (s, "},\n");
90 s = format (s, "{%smac_address%s: %s%s%s, ", q, q, q, macstring, q);
92 switch (current_status)
95 status_string = "static drop";
98 status_string = "static allow";
101 status_string = "dynamic drop";
104 status_string = "dynamic allow";
107 status_string = "d-quota inact";
110 status_string = "d-quota activ";
113 status_string = "code bug!";
116 vlib_get_combined_counter (&mm->allow_counters, dp - mm->devices,
118 vlib_get_combined_counter (&mm->drop_counters, dp - mm->devices, &drop);
119 s = format (s, "%sname%s: %s%s%s, %sstatus%s: %s%s%s,",
120 q, q, q, dp->device_name, q, q, q, q, status_string, q);
121 s = format (s, "%sallow_pkts%s: %lld,", q, q, allow.packets);
122 s = format (s, "%sallow_bytes%s: %lld,", q, q, allow.bytes);
123 s = format (s, "%sdrop_pkts%s: %lld", q, q, drop.packets);
125 for (j = 0; j < vec_len (mm->arp_cache_copy); j++)
127 n = mm->arp_cache_copy + j;
128 if (!memcmp (dp->mac_address, n->mac.bytes, sizeof (n->mac)))
130 s = format (s, ", %sip4_address%s: %s%U%s", q, q,
131 q, format_ip4_address, &n->ip4_address, q);
137 s = format (s, "}\n");
138 s = format (s, "]}\n");
139 vec_free (macstring);
140 vec_free (pool_indices);
144 hs->cache_pool_index = ~0;
150 mactime_url_init (vlib_main_t * vm)
152 void (*fp) (void *, char *, int);
154 /* Look up the builtin URL registration handler */
155 fp = vlib_get_plugin_symbol ("http_static_plugin.so",
156 "http_static_server_register_builtin_handler");
160 clib_warning ("http_static_plugin.so not loaded...");
164 (*fp) (handle_get_mactime, "mactime.json", HTTP_BUILTIN_METHOD_GET);
168 * fd.io coding-style-patch-verification: ON
171 * eval: (c-set-style "gnu")