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