Line data Source code
1 : #include <vnet/vnet.h>
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>
6 :
7 : static walk_rc_t
8 0 : mactime_ip_neighbor_copy (index_t ipni, void *ctx)
9 : {
10 0 : mactime_main_t *mm = ctx;
11 :
12 0 : vec_add1 (mm->arp_cache_copy, ipni);
13 :
14 0 : return (WALK_CONTINUE);
15 : }
16 :
17 : static hss_url_handler_rc_t
18 0 : handle_get_mactime (hss_url_handler_args_t *args)
19 : {
20 0 : mactime_main_t *mm = &mactime_main;
21 : mactime_device_t *dp;
22 0 : u8 *macstring = 0;
23 : char *status_string;
24 0 : u32 *pool_indices = 0;
25 0 : int current_status = 99;
26 : int i, j;
27 : f64 now;
28 : vlib_counter_t allow, drop;
29 : ip_neighbor_t *n;
30 0 : char *q = "\"";
31 0 : u8 *s = 0;
32 0 : int need_comma = 0;
33 :
34 : /* Walk all ip4 neighbours on all interfaces */
35 0 : vec_reset_length (mm->arp_cache_copy);
36 0 : ip_neighbor_walk (AF_IP4, ~0, mactime_ip_neighbor_copy, mm);
37 :
38 0 : now = clib_timebase_now (&mm->timebase);
39 :
40 0 : if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
41 0 : mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
42 :
43 0 : pool_foreach (dp, mm->devices)
44 : {
45 0 : vec_add1 (pool_indices, dp - mm->devices);
46 : }
47 :
48 0 : s = format (s, "{%smactime%s: [\n", q, q);
49 :
50 0 : for (i = 0; i < vec_len (pool_indices); i++)
51 : {
52 0 : dp = pool_elt_at_index (mm->devices, pool_indices[i]);
53 :
54 : /* Check dynamic ranges */
55 0 : for (j = 0; j < vec_len (dp->ranges); j++)
56 : {
57 0 : clib_timebase_range_t *r = dp->ranges + j;
58 : f64 start0, end0;
59 :
60 0 : start0 = r->start + mm->sunday_midnight;
61 0 : end0 = r->end + mm->sunday_midnight;
62 :
63 0 : if (now >= start0 && now <= end0)
64 : {
65 0 : if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
66 0 : current_status = 3;
67 0 : else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
68 0 : current_status = 5;
69 : else
70 0 : current_status = 2;
71 0 : goto print;
72 : }
73 : }
74 0 : if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
75 0 : current_status = 0;
76 0 : if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
77 0 : current_status = 1;
78 0 : if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
79 0 : current_status = 2;
80 0 : if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
81 0 : current_status = 3;
82 0 : if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
83 0 : current_status = 4;
84 :
85 0 : print:
86 0 : vec_reset_length (macstring);
87 :
88 0 : macstring = format (0, "%U", format_mac_address, dp->mac_address);
89 :
90 0 : if (need_comma)
91 0 : s = format (s, "},\n");
92 :
93 0 : need_comma = 1;
94 0 : s = format (s, "{%smac_address%s: %s%s%s, ", q, q, q, macstring, q);
95 :
96 0 : switch (current_status)
97 : {
98 0 : case 0:
99 0 : status_string = "static drop";
100 0 : break;
101 0 : case 1:
102 0 : status_string = "static allow";
103 0 : break;
104 0 : case 2:
105 0 : status_string = "dynamic drop";
106 0 : break;
107 0 : case 3:
108 0 : status_string = "dynamic allow";
109 0 : break;
110 0 : case 4:
111 0 : status_string = "d-quota inact";
112 0 : break;
113 0 : case 5:
114 0 : status_string = "d-quota activ";
115 0 : break;
116 0 : default:
117 0 : status_string = "code bug!";
118 0 : break;
119 : }
120 0 : vlib_get_combined_counter (&mm->allow_counters, dp - mm->devices,
121 : &allow);
122 0 : vlib_get_combined_counter (&mm->drop_counters, dp - mm->devices, &drop);
123 0 : 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 0 : s = format (s, "%sallow_pkts%s: %lld,", q, q, allow.packets);
126 0 : s = format (s, "%sallow_bytes%s: %lld,", q, q, allow.bytes);
127 0 : s = format (s, "%sdrop_pkts%s: %lld", q, q, drop.packets);
128 :
129 0 : for (j = 0; j < vec_len (mm->arp_cache_copy); j++)
130 : {
131 0 : n = ip_neighbor_get (mm->arp_cache_copy[j]);
132 0 : if (!memcmp (dp->mac_address,
133 0 : ip_neighbor_get_mac (n), sizeof (mac_address_t)))
134 : {
135 0 : 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);
138 0 : break;
139 : }
140 : }
141 : }
142 0 : if (need_comma)
143 0 : s = format (s, "}\n");
144 0 : s = format (s, "]}\n");
145 0 : vec_free (macstring);
146 0 : vec_free (pool_indices);
147 :
148 0 : args->data = s;
149 0 : args->data_len = vec_len (s);
150 0 : args->free_vec_data = 1;
151 0 : return HSS_URL_HANDLER_OK;
152 : }
153 :
154 : void
155 1 : mactime_url_init (vlib_main_t * vm)
156 : {
157 : hss_register_url_fn fp;
158 :
159 : /* Look up the builtin URL registration handler */
160 1 : fp = vlib_get_plugin_symbol ("http_static_plugin.so",
161 : "hss_register_url_handler");
162 :
163 1 : if (fp == 0)
164 : {
165 0 : clib_warning ("http_static_plugin.so not loaded...");
166 0 : return;
167 : }
168 :
169 1 : (*fp) (handle_get_mactime, "mactime.json", HTTP_REQ_GET);
170 : }
171 :
172 : /*
173 : * fd.io coding-style-patch-verification: ON
174 : *
175 : * Local Variables:
176 : * eval: (c-set-style "gnu")
177 : * End:
178 : */
|