FD.io VPP  v21.01.1
Vector Packet Processing
builtins.c
Go to the documentation of this file.
1 #include <vnet/vnet.h>
4 #include <mactime/mactime.h>
5 #include <vlib/unix/plugin.h>
7 
8 static walk_rc_t
10 {
11  mactime_main_t *mm = ctx;
12 
13  vec_add1 (mm->arp_cache_copy, ipni);
14 
15  return (WALK_CONTINUE);
16 }
17 
18 static int
20  u8 * request, http_session_t * hs)
21 {
23  mactime_device_t *dp;
24  u8 *macstring = 0;
25  char *status_string;
26  u32 *pool_indices = 0;
27  int current_status = 99;
28  int i, j;
29  f64 now;
30  vlib_counter_t allow, drop;
31  ip_neighbor_t *n;
32  char *q = "\"";
33  u8 *s = 0;
34  int need_comma = 0;
35 
36  /* Walk all ip4 neighbours on all interfaces */
39 
40  now = clib_timebase_now (&mm->timebase);
41 
42  if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
44 
45  /* *INDENT-OFF* */
46  pool_foreach (dp, mm->devices)
47  {
48  vec_add1 (pool_indices, dp - mm->devices);
49  }
50  /* *INDENT-ON* */
51 
52  s = format (s, "{%smactime%s: [\n", q, q);
53 
54  for (i = 0; i < vec_len (pool_indices); i++)
55  {
56  dp = pool_elt_at_index (mm->devices, pool_indices[i]);
57 
58  /* Check dynamic ranges */
59  for (j = 0; j < vec_len (dp->ranges); j++)
60  {
61  clib_timebase_range_t *r = dp->ranges + j;
62  f64 start0, end0;
63 
64  start0 = r->start + mm->sunday_midnight;
65  end0 = r->end + mm->sunday_midnight;
66 
67  if (now >= start0 && now <= end0)
68  {
70  current_status = 3;
72  current_status = 5;
73  else
74  current_status = 2;
75  goto print;
76  }
77  }
79  current_status = 0;
81  current_status = 1;
83  current_status = 2;
85  current_status = 3;
87  current_status = 4;
88 
89  print:
90  vec_reset_length (macstring);
91 
92  macstring = format (0, "%U", format_mac_address, dp->mac_address);
93 
94  if (need_comma)
95  s = format (s, "},\n");
96 
97  need_comma = 1;
98  s = format (s, "{%smac_address%s: %s%s%s, ", q, q, q, macstring, q);
99 
100  switch (current_status)
101  {
102  case 0:
103  status_string = "static drop";
104  break;
105  case 1:
106  status_string = "static allow";
107  break;
108  case 2:
109  status_string = "dynamic drop";
110  break;
111  case 3:
112  status_string = "dynamic allow";
113  break;
114  case 4:
115  status_string = "d-quota inact";
116  break;
117  case 5:
118  status_string = "d-quota activ";
119  break;
120  default:
121  status_string = "code bug!";
122  break;
123  }
125  &allow);
126  vlib_get_combined_counter (&mm->drop_counters, dp - mm->devices, &drop);
127  s = format (s, "%sname%s: %s%s%s, %sstatus%s: %s%s%s,",
128  q, q, q, dp->device_name, q, q, q, q, status_string, q);
129  s = format (s, "%sallow_pkts%s: %lld,", q, q, allow.packets);
130  s = format (s, "%sallow_bytes%s: %lld,", q, q, allow.bytes);
131  s = format (s, "%sdrop_pkts%s: %lld", q, q, drop.packets);
132 
133  for (j = 0; j < vec_len (mm->arp_cache_copy); j++)
134  {
135  n = ip_neighbor_get (mm->arp_cache_copy[j]);
136  if (!memcmp (dp->mac_address,
137  ip_neighbor_get_mac (n), sizeof (mac_address_t)))
138  {
139  s = format (s, ", %sip4_address%s: %s%U%s", q, q,
142  break;
143  }
144  }
145  }
146  if (need_comma)
147  s = format (s, "}\n");
148  s = format (s, "]}\n");
149  vec_free (macstring);
150  vec_free (pool_indices);
151 
152  hs->data = s;
153  hs->data_offset = 0;
154  hs->cache_pool_index = ~0;
155  hs->free_data = 1;
156  return 0;
157 }
158 
159 void
161 {
162  void (*fp) (void *, char *, int);
163 
164  /* Look up the builtin URL registration handler */
165  fp = vlib_get_plugin_symbol ("http_static_plugin.so",
166  "http_static_server_register_builtin_handler");
167 
168  if (fp == 0)
169  {
170  clib_warning ("http_static_plugin.so not loaded...");
171  return;
172  }
173 
174  (*fp) (handle_get_mactime, "mactime.json", HTTP_BUILTIN_METHOD_GET);
175 }
176 
177 /*
178  * fd.io coding-style-patch-verification: ON
179  *
180  * Local Variables:
181  * eval: (c-set-style "gnu")
182  * End:
183  */
#define MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA
vlib_combined_counter_main_t drop_counters
Definition: mactime.h:63
const mac_address_t * ip_neighbor_get_mac(const ip_neighbor_t *ipn)
Definition: ip_neighbor.c:127
static int handle_get_mactime(http_builtin_method_type_t reqtype, u8 *request, http_session_t *hs)
Definition: builtins.c:19
Static http server definitions.
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:527
clib_timebase_t timebase
Definition: mactime.h:49
u32 data_offset
Current data send offset.
Definition: http_static.h:104
#define MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:41
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
Combined counter to hold both packets and byte differences.
Definition: counter_types.h:26
vlib_main_t * vm
Definition: in2out_ed.c:1580
unsigned char u8
Definition: types.h:56
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
enum walk_rc_t_ walk_rc_t
Walk return code.
clib_timebase_range_t * ranges
static f64 clib_timebase_now(clib_timebase_t *tb)
Definition: time_range.h:88
description fragment has unexpected format
Definition: map.api:433
A representation of an IP neighbour/peer.
const ip_address_t * ip_neighbor_get_ip(const ip_neighbor_t *ipn)
Definition: ip_neighbor.c:115
unsigned int u32
Definition: types.h:88
#define MACTIME_DEVICE_FLAG_STATIC_ALLOW
int free_data
Need to free data in detach_cache_entry.
Definition: http_static.h:106
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:546
mactime_main_t mactime_main
Definition: mactime.c:38
counter_t packets
packet counter
Definition: counter_types.h:28
u8 * data
File data, a vector.
Definition: http_static.h:102
vlib_combined_counter_main_t allow_counters
Definition: mactime.h:62
long ctx[MAX_CONNS]
Definition: main.c:144
#define MACTIME_DEVICE_FLAG_DYNAMIC_DROP
#define PREDICT_FALSE(x)
Definition: clib.h:121
Application session.
Definition: http_server.c:40
format_function_t format_ip46_address
Definition: ip46_address.h:50
static void vlib_get_combined_counter(const vlib_combined_counter_main_t *cm, u32 index, vlib_counter_t *result)
Get the value of a combined counter, never called in the speed path Scrapes the entire set of per-thr...
Definition: counter.h:278
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
f64 sunday_midnight
Definition: mactime.h:52
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
#define clib_warning(format, args...)
Definition: error.h:59
void mactime_url_init(vlib_main_t *vm)
Definition: builtins.c:160
mactime_device_t * devices
Definition: mactime.h:58
u32 cache_pool_index
File cache pool index.
Definition: http_static.h:109
void ip_neighbor_walk(ip_address_family_t af, u32 sw_if_index, ip_neighbor_walk_cb_t cb, void *ctx)
Definition: ip_neighbor.c:1040
static walk_rc_t mactime_ip_neighbor_copy(index_t ipni, void *ctx)
Definition: builtins.c:9
#define MACTIME_DEVICE_FLAG_STATIC_DROP
Always drop packets from this device.
void * vlib_get_plugin_symbol(char *plugin_name, char *symbol_name)
Definition: plugin.c:38
http_builtin_method_type_t
Definition: http_static.h:74
counter_t bytes
byte counter
Definition: counter_types.h:29
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
vhost_user_req_t request
Definition: vhost_user.h:104
u8 * format_mac_address(u8 *s, va_list *args)
Definition: format.c:58
index_t * arp_cache_copy
Definition: mactime.h:74
ip_neighbor_t * ip_neighbor_get(index_t ipni)
Definition: ip_neighbor.c:88
static unsigned char * print(const cJSON *const item, cJSON_bool format, const internal_hooks *const hooks)
Definition: cJSON.c:1181
__clib_export f64 clib_timebase_find_sunday_midnight(f64 start_time)
Definition: time_range.c:221