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