summaryrefslogtreecommitdiffstats
path: root/src/scripts/vnet/arp4
blob: 372d82a9ccdd31bbcd253c13d0f22e5bfc03bac6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
packet-generator new {
  name x
  limit 1
  node ip4-input
  size 64-64
  data {
    ICMP: 1.0.0.2 -> 2.0.0.2
    ICMP echo_request
    incrementing 100
  }
}

trace add pg-input 100
loop create
loop create
set int state loop0 up
set int state loop1 up

set int ip address loop0 1.0.0.1/24
set int ip address loop1 2.0.0.1/24
ial */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
#include <vnet/vnet.h>
#include <builtinurl/builtinurl.h>
#include <http_static/http_static.h>
#include <mactime/mactime.h>
#include <vlib/unix/plugin.h>
#include <vnet/ip-neighbor/ip_neighbor.h>

static walk_rc_t
mactime_ip_neighbor_copy (index_t ipni, void *ctx)
{
  mactime_main_t *mm = ctx;

  vec_add1 (mm->arp_cache_copy, ipni);

  return (WALK_CONTINUE);
}

static int
handle_get_mactime (http_builtin_method_type_t reqtype,
		    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;
  ip_neighbor_t *n;
  char *q = "\"";
  u8 *s = 0;
  int need_comma = 0;

  /* Walk all ip4 neighbours on all interfaces */
  vec_reset_length (mm->arp_cache_copy);
  ip_neighbor_walk (IP46_TYPE_IP4, ~0, mactime_ip_neighbor_copy, mm);

  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 = ip_neighbor_get (mm->arp_cache_copy[j]);
	  if (!memcmp (dp->mac_address,
		       ip_neighbor_get_mac (n), sizeof (mac_address_t)))
	    {
	      s = format (s, ", %sip4_address%s: %s%U%s", q, q,
			  q, format_ip46_address,
			  ip_neighbor_get_ip (n), IP46_TYPE_IP4, 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:
 */