aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/mactime/builtins.c
blob: e90ac661a13a981d5d5815d4ba59a8a4d164668c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#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:
 */
f->data_len); if (*l == 0) { printf("Unregistered API message: %d\n", msg_id); return (-1); } *p = (char *)msg; } else { printf("Read failed with %d\n", rv); } return (rv); } /* * XXX: Makes the assumption that client_index is the first member */ typedef VL_API_PACKED(struct _vl_api_header { u16 _vl_msg_id; u32 client_index; }) vl_api_header_t; static unsigned int pneum_client_index (void) { return (api_main.my_client_index); } int pneum_write (char *p, int l) { int rv = -1; api_main_t *am = &api_main; vl_api_header_t *mp = vl_msg_api_alloc(l); unix_shared_memory_queue_t *q; pneum_main_t *pm = &pneum_main; if (!pm->connected_to_vlib) return -1; if (!mp) return (-1); memcpy(mp, p, l); mp->client_index = pneum_client_index(); q = am->shmem_hdr->vl_input_queue; rv = unix_shared_memory_queue_add(q, (u8 *)&mp, 0); if (rv != 0) { printf("vpe_api_write fails: %d\n", rv); /* Clear message */ pneum_free(mp); } return (rv); } uint32_t pneum_get_msg_index (unsigned char * name) { return vl_api_get_msg_index (name); }