2 #include <http_static/http_static.h>
3 #include <mactime/mactime.h>
4 #include <vlib/unix/plugin.h>
5 #include <vnet/ip-neighbor/ip_neighbor.h>
8 mactime_ip_neighbor_copy (index_t ipni, void *ctx)
10 mactime_main_t *mm = ctx;
12 vec_add1 (mm->arp_cache_copy, ipni);
14 return (WALK_CONTINUE);
18 handle_get_mactime (http_req_method_t reqtype, u8 *request, hss_session_t *hs)
20 mactime_main_t *mm = &mactime_main;
24 u32 *pool_indices = 0;
25 int current_status = 99;
28 vlib_counter_t allow, drop;
34 /* Walk all ip4 neighbours on all interfaces */
35 vec_reset_length (mm->arp_cache_copy);
36 ip_neighbor_walk (AF_IP4, ~0, mactime_ip_neighbor_copy, mm);
38 now = clib_timebase_now (&mm->timebase);
40 if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
41 mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
43 pool_foreach (dp, mm->devices)
45 vec_add1 (pool_indices, dp - mm->devices);
48 s = format (s, "{%smactime%s: [\n", q, q);
50 for (i = 0; i < vec_len (pool_indices); i++)
52 dp = pool_elt_at_index (mm->devices, pool_indices[i]);
54 /* Check dynamic ranges */
55 for (j = 0; j < vec_len (dp->ranges); j++)
57 clib_timebase_range_t *r = dp->ranges + j;
60 start0 = r->start + mm->sunday_midnight;
61 end0 = r->end + mm->sunday_midnight;
63 if (now >= start0 && now <= end0)
65 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
67 else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
74 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
76 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
78 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
80 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
82 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
86 vec_reset_length (macstring);
88 macstring = format (0, "%U", format_mac_address, dp->mac_address);
91 s = format (s, "},\n");
94 s = format (s, "{%smac_address%s: %s%s%s, ", q, q, q, macstring, q);
96 switch (current_status)
99 status_string = "static drop";
102 status_string = "static allow";
105 status_string = "dynamic drop";
108 status_string = "dynamic allow";
111 status_string = "d-quota inact";
114 status_string = "d-quota activ";
117 status_string = "code bug!";
120 vlib_get_combined_counter (&mm->allow_counters, dp - mm->devices,
122 vlib_get_combined_counter (&mm->drop_counters, dp - mm->devices, &drop);
123 s = format (s, "%sname%s: %s%s%s, %sstatus%s: %s%s%s,",
124 q, q, q, dp->device_name, q, q, q, q, status_string, q);
125 s = format (s, "%sallow_pkts%s: %lld,", q, q, allow.packets);
126 s = format (s, "%sallow_bytes%s: %lld,", q, q, allow.bytes);
127 s = format (s, "%sdrop_pkts%s: %lld", q, q, drop.packets);
129 for (j = 0; j < vec_len (mm->arp_cache_copy); j++)
131 n = ip_neighbor_get (mm->arp_cache_copy[j]);
132 if (!memcmp (dp->mac_address,
133 ip_neighbor_get_mac (n), sizeof (mac_address_t)))
135 s = format (s, ", %sip4_address%s: %s%U%s", q, q,
136 q, format_ip46_address,
137 ip_neighbor_get_ip (n), IP46_TYPE_IP4, q);
143 s = format (s, "}\n");
144 s = format (s, "]}\n");
145 vec_free (macstring);
146 vec_free (pool_indices);
150 hs->cache_pool_index = ~0;
156 mactime_url_init (vlib_main_t * vm)
158 void (*fp) (void *, char *, int);
160 /* Look up the builtin URL registration handler */
161 fp = vlib_get_plugin_symbol ("http_static_plugin.so",
162 "hss_register_url_handler");
166 clib_warning ("http_static_plugin.so not loaded...");
170 (*fp) (handle_get_mactime, "mactime.json", HTTP_REQ_GET);
174 * fd.io coding-style-patch-verification: ON
177 * eval: (c-set-style "gnu")