mactime: add the "mactime.json" builtin URL 21/22721/2
authorDave Barach <dave@barachs.net>
Mon, 14 Oct 2019 15:26:15 +0000 (11:26 -0400)
committerDamjan Marion <dmarion@me.com>
Mon, 14 Oct 2019 22:42:49 +0000 (22:42 +0000)
If the http static server plugin is enabled, register the name
"mactime.json" with the server. Visiting <web-root>/mactime.json
produces a json brain-dump of the mactime table.

Type: feature

Signed-off-by: Dave Barach <dave@barachs.net>
Change-Id: Ie39b0c776675864a85251b8c07fbf719d399f6de

src/plugins/mactime/CMakeLists.txt
src/plugins/mactime/builtins.c [new file with mode: 0644]
src/plugins/mactime/mactime.c
src/plugins/mactime/mactime.h
src/plugins/mactime/mactime_test.c

index 5477674..debf033 100644 (file)
@@ -14,6 +14,7 @@
 add_vpp_plugin(mactime
   SOURCES
   mactime.c
+  builtins.c
   node.c
 
   API_FILES
diff --git a/src/plugins/mactime/builtins.c b/src/plugins/mactime/builtins.c
new file mode 100644 (file)
index 0000000..a0d7b5f
--- /dev/null
@@ -0,0 +1,172 @@
+#include <vnet/vnet.h>
+#include <builtinurl/builtinurl.h>
+#include <http_static/http_static.h>
+#include <mactime/mactime.h>
+#include <vlib/unix/plugin.h>
+
+static int
+handle_get_mactime (u8 * request, http_session_t * hs)
+{
+  mactime_main_t *mm = &mactime_main;
+  mactime_device_t *dp;
+  u8 *macstring = 0;
+  char *status_string;
+  u32 *pool_indices = 0;
+  int current_status = 99;
+  int i, j;
+  f64 now;
+  vlib_counter_t allow, drop;
+  ethernet_arp_ip4_entry_t *n, *pool;
+  char *q = "\"";
+  u8 *s = 0;
+  int need_comma = 0;
+
+  vec_reset_length (mm->arp_cache_copy);
+  pool = ip4_neighbors_pool ();
+
+  pool_foreach (n, pool, (
+                          {
+                          vec_add1 (mm->arp_cache_copy, n[0]);}));
+
+  now = clib_timebase_now (&mm->timebase);
+
+  if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
+    mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
+
+    /* *INDENT-OFF* */
+    pool_foreach (dp, mm->devices,
+    ({
+        vec_add1 (pool_indices, dp - mm->devices);
+    }));
+    /* *INDENT-ON* */
+
+  s = format (s, "{%smactime%s: [\n", q, q);
+
+  for (i = 0; i < vec_len (pool_indices); i++)
+    {
+      dp = pool_elt_at_index (mm->devices, pool_indices[i]);
+
+      /* Check dynamic ranges */
+      for (j = 0; j < vec_len (dp->ranges); j++)
+       {
+         clib_timebase_range_t *r = dp->ranges + j;
+         f64 start0, end0;
+
+         start0 = r->start + mm->sunday_midnight;
+         end0 = r->end + mm->sunday_midnight;
+
+         if (now >= start0 && now <= end0)
+           {
+             if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
+               current_status = 3;
+             else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
+               current_status = 5;
+             else
+               current_status = 2;
+             goto print;
+           }
+       }
+      if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
+       current_status = 0;
+      if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
+       current_status = 1;
+      if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
+       current_status = 2;
+      if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
+       current_status = 3;
+      if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
+       current_status = 4;
+
+    print:
+      vec_reset_length (macstring);
+
+      macstring = format (0, "%U", format_mac_address, dp->mac_address);
+
+      if (need_comma)
+       s = format (s, "},\n");
+
+      need_comma = 1;
+      s = format (s, "{%smac_address%s: %s%s%s, ", q, q, q, macstring, q);
+
+      switch (current_status)
+       {
+       case 0:
+         status_string = "static drop";
+         break;
+       case 1:
+         status_string = "static allow";
+         break;
+       case 2:
+         status_string = "dynamic drop";
+         break;
+       case 3:
+         status_string = "dynamic allow";
+         break;
+       case 4:
+         status_string = "d-quota inact";
+         break;
+       case 5:
+         status_string = "d-quota activ";
+         break;
+       default:
+         status_string = "code bug!";
+         break;
+       }
+      vlib_get_combined_counter (&mm->allow_counters, dp - mm->devices,
+                                &allow);
+      vlib_get_combined_counter (&mm->drop_counters, dp - mm->devices, &drop);
+      s = format (s, "%sname%s: %s%s%s, %sstatus%s: %s%s%s,",
+                 q, q, q, dp->device_name, q, q, q, q, status_string, q);
+      s = format (s, "%sallow_pkts%s: %lld,", q, q, allow.packets);
+      s = format (s, "%sallow_bytes%s: %lld,", q, q, allow.bytes);
+      s = format (s, "%sdrop_pkts%s: %lld", q, q, drop.packets);
+
+      for (j = 0; j < vec_len (mm->arp_cache_copy); j++)
+       {
+         n = mm->arp_cache_copy + j;
+         if (!memcmp (dp->mac_address, n->mac.bytes, sizeof (n->mac)))
+           {
+             s = format (s, ", %sip4_address%s: %s%U%s", q, q,
+                         q, format_ip4_address, &n->ip4_address, q);
+             break;
+           }
+       }
+    }
+  if (need_comma)
+    s = format (s, "}\n");
+  s = format (s, "]}\n");
+  vec_free (macstring);
+  vec_free (pool_indices);
+
+  hs->data = s;
+  hs->data_offset = 0;
+  hs->cache_pool_index = ~0;
+  hs->free_data = 1;
+  return 0;
+}
+
+void
+mactime_url_init (vlib_main_t * vm)
+{
+  void (*fp) (void *, char *, int);
+
+  /* Look up the builtin URL registration handler */
+  fp = vlib_get_plugin_symbol ("http_static_plugin.so",
+                              "http_static_server_register_builtin_handler");
+
+  if (fp == 0)
+    {
+      clib_warning ("http_static_plugin.so not loaded...");
+      return;
+    }
+
+  (*fp) (handle_get_mactime, "mactime.json", HTTP_BUILTIN_METHOD_GET);
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
index 06fa708..cd2f509 100644 (file)
@@ -64,6 +64,7 @@ mactime_enable_disable (mactime_main_t * mm, u32 sw_if_index,
 {
   vnet_sw_interface_t *sw;
   int rv = 0;
+  static u8 url_init_done;
 
   feature_init (mm);
 
@@ -81,6 +82,12 @@ mactime_enable_disable (mactime_main_t * mm, u32 sw_if_index,
                               sw_if_index, enable_disable, 0, 0);
   vnet_feature_enable_disable ("interface-output", "mactime-tx",
                               sw_if_index, enable_disable, 0, 0);
+  if (url_init_done == 0)
+    {
+      mactime_url_init (mm->vlib_main);
+      url_init_done = 1;
+    }
+
   return rv;
 }
 
index 2ce1cf9..61160f8 100644 (file)
@@ -89,6 +89,7 @@ extern vlib_node_registration_t mactime_node;
 extern vlib_node_registration_t mactime_tx_node;
 
 void mactime_send_create_entry_message (u8 * mac_address);
+void mactime_url_init (vlib_main_t * vm);
 
 /* Periodic function events */
 #define MACTIME_EVENT1 1
index 40a9da0..b47cc21 100644 (file)
@@ -432,7 +432,6 @@ vl_api_mactime_details_t_handler (vl_api_mactime_details_t * mp)
   clib_warning ("WARNING: stub called...");
 }
 
-
 #include <mactime/mactime.api_test.c>
 
 /*