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