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