2 * node.c - skeleton vpp engine plug-in dual-loop node skeleton
4 * Copyright (c) <current-year> <your-organization>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vnet/pg/pg.h>
20 #include <vppinfra/error.h>
21 #include <mactime/mactime.h>
22 #include <vnet/ip/ip4.h>
32 vlib_node_registration_t mactime_node;
33 vlib_node_registration_t mactime_tx_node;
35 #define foreach_mactime_error \
36 _(OK, "Permitted packets") \
37 _(STATIC_DROP, "Static drop packets") \
38 _(RANGE_DROP, "Range drop packets") \
39 _(QUOTA_DROP, "Data quota drop packets") \
40 _(DROP_10001, "Dropped UDP DST-port 10001")
44 #define _(sym,str) MACTIME_ERROR_##sym,
50 static char *mactime_error_strings[] = {
51 #define _(sym,string) string,
59 MACTIME_NEXT_ETHERNET_INPUT,
63 /* packet trace format function */
65 format_mactime_trace (u8 * s, va_list * args)
67 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
68 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
69 mactime_trace_t *t = va_arg (*args, mactime_trace_t *);
71 s = format (s, "MACTIME: src mac %U device %s result %s\n",
72 format_mac_address, t->src_mac,
73 (t->device_index != ~0) ? t->device_name : (u8 *) "unknown",
74 t->next_index == MACTIME_NEXT_DROP ? "drop" : "pass");
79 mactime_node_inline (vlib_main_t * vm,
80 vlib_node_runtime_t * node, vlib_frame_t * frame,
83 u32 n_left_from, *from, *to_next;
84 mactime_next_t next_index;
85 mactime_main_t *mm = &mactime_main;
87 clib_bihash_kv_8_8_t kv;
88 clib_bihash_8_8_t *lut = &mm->lookup_table;
91 u32 thread_index = vm->thread_index;
92 vnet_main_t *vnm = vnet_get_main ();
93 vnet_interface_main_t *im = &vnm->interface_main;
94 u8 arc = im->output_feature_arc_index;
95 vnet_feature_config_main_t *fcm;
98 fcm = vnet_feature_get_config_main (arc);
100 now = clib_timebase_now (&mm->timebase);
102 if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
103 mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
105 from = vlib_frame_vector_args (frame);
106 n_left_from = frame->n_vectors;
107 next_index = node->cached_next_index;
109 while (n_left_from > 0)
113 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
115 while (n_left_from > 0 && n_left_to_next > 0)
122 ethernet_header_t *en0;
123 int has_dynamic_range_allow = 0;
126 /* speculatively enqueue b0 to the current next frame */
134 b0 = vlib_get_buffer (vm, bi0);
136 /* Set next0 to e.g. interface-tx */
138 vnet_get_config_data (&fcm->config_main,
139 &b0->current_config_index, &next0,
140 /* # bytes of config data */ 0);
142 next0 = MACTIME_NEXT_ETHERNET_INPUT;
144 vlib_buffer_advance (b0, -(word) vnet_buffer (b0)->l2_hdr_offset);
146 len0 = vlib_buffer_length_in_chain (vm, b0);
147 en0 = vlib_buffer_get_current (b0);
150 clib_memcpy_fast (&kv.key, en0->dst_address, 6);
152 clib_memcpy_fast (&kv.key, en0->src_address, 6);
154 /* Lookup the src/dst mac address */
155 if (clib_bihash_search_8_8 (lut, &kv, &kv) < 0)
157 /* Create a table entry... */
158 mactime_send_create_entry_message
159 (is_tx ? en0->dst_address : en0->src_address);
161 /* and let this packet pass */
168 device_index0 = kv.value;
170 dp = pool_elt_at_index (mm->devices, device_index0);
172 /* Known device, check for an always-on traffic quota */
173 if ((dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
174 && PREDICT_FALSE (dp->data_quota))
176 vlib_counter_t device_current_count;
177 vlib_get_combined_counter (&mm->allow_counters,
179 &device_current_count);
180 if (device_current_count.bytes >= dp->data_quota)
182 next0 = MACTIME_NEXT_DROP;
183 b0->error = node->errors[MACTIME_ERROR_QUOTA_DROP];
184 vlib_increment_combined_counter
185 (&mm->drop_counters, thread_index, dp - mm->devices, 1,
191 /* Static drop / allow? */
194 (MACTIME_DEVICE_FLAG_STATIC_DROP
195 | MACTIME_DEVICE_FLAG_STATIC_ALLOW)))
197 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
199 next0 = MACTIME_NEXT_DROP;
200 b0->error = node->errors[MACTIME_ERROR_STATIC_DROP];
201 vlib_increment_combined_counter
202 (&mm->drop_counters, thread_index, dp - mm->devices, 1,
205 else /* note next0 set to allow */
208 * Special-case mini-ACL for a certain species of
209 * home security DVR which likes to "call home."
212 (dp->flags & MACTIME_DEVICE_FLAG_DROP_UDP_10001))
214 ip4_header_t *ip = (void *) (((u8 *) en0) + 14);
215 udp_header_t *udp = (udp_header_t *) (ip + 1);
216 if (ip->protocol != IP_PROTOCOL_UDP)
218 if (clib_net_to_host_u16 (udp->dst_port) == 10001 ||
219 clib_net_to_host_u16 (udp->dst_port) == 9603)
221 next0 = MACTIME_NEXT_DROP;
222 b0->error = node->errors[MACTIME_ERROR_DROP_10001];
230 vlib_increment_combined_counter
231 (&mm->allow_counters, thread_index, dp - mm->devices,
239 /* Known device, see if traffic allowed at the moment */
240 for (i = 0; i < vec_len (dp->ranges); i++)
242 clib_timebase_range_t *r = dp->ranges + i;
245 start0 = r->start + mm->sunday_midnight;
246 end0 = r->end + mm->sunday_midnight;
247 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
248 has_dynamic_range_allow = 1;
250 /* Packet within time range */
251 if (now >= start0 && now <= end0)
253 /* And it's a drop range, drop it */
254 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
256 vlib_increment_combined_counter
257 (&mm->drop_counters, thread_index,
258 dp - mm->devices, 1, len0);
259 next0 = MACTIME_NEXT_DROP;
260 b0->error = node->errors[MACTIME_ERROR_RANGE_DROP];
263 /* Quota-check allow range? */
264 else if (has_dynamic_range_allow)
266 if (dp->data_used_in_range + len0 >= dp->data_quota)
268 next0 = MACTIME_NEXT_DROP;
269 b0->error = node->errors[MACTIME_ERROR_QUOTA_DROP];
270 vlib_increment_combined_counter
271 (&mm->drop_counters, thread_index,
272 dp - mm->devices, 1, len0);
277 dp->data_used_in_range += len0;
282 { /* it's an allow range, allow it */
284 vlib_increment_combined_counter
285 (&mm->allow_counters, thread_index,
286 dp - mm->devices, 1, len0);
293 * Didn't hit a range, so *drop* if allow configured, or
294 * *allow* if drop configured.
296 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
298 next0 = MACTIME_NEXT_DROP;
299 b0->error = node->errors[MACTIME_ERROR_STATIC_DROP];
300 vlib_increment_combined_counter
301 (&mm->drop_counters, thread_index, dp - mm->devices, 1, len0);
303 else /* DYNAMIC_DROP, DYNAMIC_RANGE_ALLOW_QUOTA */
305 vlib_increment_combined_counter
306 (&mm->allow_counters, thread_index, dp - mm->devices, 1,
308 /* Clear the data quota accumulater */
309 dp->data_used_in_range = 0;
314 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
315 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
317 mactime_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
318 clib_memcpy_fast (t->src_mac, en0->src_address,
319 sizeof (t->src_mac));
321 t->next_index = next0;
322 t->device_index = device_index0;
326 clib_memcpy_fast (t->device_name, dp->device_name,
327 ARRAY_LEN (t->device_name));
328 t->device_name[ARRAY_LEN (t->device_name) - 1] = 0;
332 /* verify speculative enqueue, maybe switch current next frame */
333 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
334 to_next, n_left_to_next,
338 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
341 vlib_node_increment_counter (vm, node->node_index,
342 MACTIME_ERROR_OK, packets_ok);
343 return frame->n_vectors;
347 mactime_node_fn (vlib_main_t * vm,
348 vlib_node_runtime_t * node, vlib_frame_t * frame)
350 return mactime_node_inline (vm, node, frame, 0 /* is_tx */ );
354 VLIB_REGISTER_NODE (mactime_node) =
356 .function = mactime_node_fn,
358 .vector_size = sizeof (u32),
359 .format_trace = format_mactime_trace,
360 .type = VLIB_NODE_TYPE_INTERNAL,
362 .n_errors = ARRAY_LEN(mactime_error_strings),
363 .error_strings = mactime_error_strings,
365 .n_next_nodes = MACTIME_N_NEXT,
367 /* edit / add dispositions here */
370 [MACTIME_NEXT_ETHERNET_INPUT] = "ethernet-input",
371 [MACTIME_NEXT_DROP] = "error-drop",
377 mactime_tx_node_fn (vlib_main_t * vm,
378 vlib_node_runtime_t * node, vlib_frame_t * frame)
380 return mactime_node_inline (vm, node, frame, 1 /* is_tx */ );
384 VLIB_REGISTER_NODE (mactime_tx_node) =
386 .function = mactime_tx_node_fn,
387 .name = "mactime-tx",
388 .vector_size = sizeof (u32),
389 .format_trace = format_mactime_trace,
390 .type = VLIB_NODE_TYPE_INTERNAL,
392 .n_errors = ARRAY_LEN(mactime_error_strings),
393 .error_strings = mactime_error_strings,
395 .n_next_nodes = MACTIME_N_NEXT,
397 /* edit / add dispositions here */
400 [MACTIME_NEXT_DROP] = "error-drop",
401 [MACTIME_NEXT_ETHERNET_INPUT] = "ethernet-input", /* notused */
407 * fd.io coding-style-patch-verification: ON
410 * eval: (c-set-style "gnu")