2 #include <builtinurl/builtinurl.h>
3 #include <http_static/http_static.h>
4 #include <mactime/mactime.h>
5 #include <vlib/unix/plugin.h>
6 #include <vnet/ip-neighbor/ip_neighbor.h>
9 mactime_ip_neighbor_copy (index_t ipni, void *ctx)
11 mactime_main_t *mm = ctx;
13 vec_add1 (mm->arp_cache_copy, ipni);
15 return (WALK_CONTINUE);
19 handle_get_mactime (http_builtin_method_type_t reqtype,
20 u8 * request, http_session_t * hs)
22 mactime_main_t *mm = &mactime_main;
26 u32 *pool_indices = 0;
27 int current_status = 99;
30 vlib_counter_t allow, drop;
36 /* Walk all ip4 neighbours on all interfaces */
37 vec_reset_length (mm->arp_cache_copy);
38 ip_neighbor_walk (AF_IP4, ~0, mactime_ip_neighbor_copy, mm);
40 now = clib_timebase_now (&mm->timebase);
42 if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
43 mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
45 pool_foreach (dp, mm->devices)
47 vec_add1 (pool_indices, dp - mm->devices);
50 s = format (s, "{%smactime%s: [\n", q, q);
52 for (i = 0; i < vec_len (pool_indices); i++)
54 dp = pool_elt_at_index (mm->devices, pool_indices[i]);
56 /* Check dynamic ranges */
57 for (j = 0; j < vec_len (dp->ranges); j++)
59 clib_timebase_range_t *r = dp->ranges + j;
62 start0 = r->start + mm->sunday_midnight;
63 end0 = r->end + mm->sunday_midnight;
65 if (now >= start0 && now <= end0)
67 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
69 else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
76 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
78 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
80 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
82 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
84 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
88 vec_reset_length (macstring);
90 macstring = format (0, "%U", format_mac_address, dp->mac_address);
93 s = format (s, "},\n");
96 s = format (s, "{%smac_address%s: %s%s%s, ", q, q, q, macstring, q);
98 switch (current_status)
101 status_string = "static drop";
104 status_string = "static allow";
107 status_string = "dynamic drop";
110 status_string = "dynamic allow";
113 status_string = "d-quota inact";
116 status_string = "d-quota activ";
119 status_string = "code bug!";
122 vlib_get_combined_counter (&mm->allow_counters, dp - mm->devices,
124 vlib_get_combined_counter (&mm->drop_counters, dp - mm->devices, &drop);
125 s = format (s, "%sname%s: %s%s%s, %sstatus%s: %s%s%s,",
126 q, q, q, dp->device_name, q, q, q, q, status_string, q);
127 s = format (s, "%sallow_pkts%s: %lld,", q, q, allow.packets);
128 s = format (s, "%sallow_bytes%s: %lld,", q, q, allow.bytes);
129 s = format (s, "%sdrop_pkts%s: %lld", q, q, drop.packets);
131 for (j = 0; j < vec_len (mm->arp_cache_copy); j++)
133 n = ip_neighbor_get (mm->arp_cache_copy[j]);
134 if (!memcmp (dp->mac_address,
135 ip_neighbor_get_mac (n), sizeof (mac_address_t)))
137 s = format (s, ", %sip4_address%s: %s%U%s", q, q,
138 q, format_ip46_address,
139 ip_neighbor_get_ip (n), IP46_TYPE_IP4, q);
145 s = format (s, "}\n");
146 s = format (s, "]}\n");
147 vec_free (macstring);
148 vec_free (pool_indices);
152 hs->cache_pool_index = ~0;
158 mactime_url_init (vlib_main_t * vm)
160 void (*fp) (void *, char *, int);
162 /* Look up the builtin URL registration handler */
163 fp = vlib_get_plugin_symbol ("http_static_plugin.so",
164 "http_static_server_register_builtin_handler");
168 clib_warning ("http_static_plugin.so not loaded...");
172 (*fp) (handle_get_mactime, "mactime.json", HTTP_BUILTIN_METHOD_GET);
176 * fd.io coding-style-patch-verification: ON
179 * eval: (c-set-style "gnu")