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