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>
31 vlib_node_registration_t mactime_node;
32 vlib_node_registration_t mactime_tx_node;
34 #define foreach_mactime_error \
35 _(DROP, "Dropped packets") \
36 _(OK, "Permitted packets")
40 #define _(sym,str) MACTIME_ERROR_##sym,
46 static char *mactime_error_strings[] = {
47 #define _(sym,string) string,
55 MACTIME_NEXT_ETHERNET_INPUT,
59 /* packet trace format function */
61 format_mactime_trace (u8 * s, va_list * args)
63 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
64 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
65 mactime_trace_t *t = va_arg (*args, mactime_trace_t *);
67 s = format (s, "MACTIME: src mac %U device %s result %s\n",
68 format_mac_address, t->src_mac,
69 (t->device_index != ~0) ? t->device_name : (u8 *) "unknown",
70 t->next_index == MACTIME_NEXT_DROP ? "drop" : "pass");
75 mactime_node_inline (vlib_main_t * vm,
76 vlib_node_runtime_t * node, vlib_frame_t * frame,
79 u32 n_left_from, *from, *to_next;
80 mactime_next_t next_index;
81 mactime_main_t *mm = &mactime_main;
83 clib_bihash_kv_8_8_t kv;
84 clib_bihash_8_8_t *lut = &mm->lookup_table;
85 u32 packets_ok = 0, packets_dropped = 0;
87 u32 thread_index = vm->thread_index;
88 vnet_main_t *vnm = vnet_get_main ();
89 vnet_interface_main_t *im = &vnm->interface_main;
90 u8 arc = im->output_feature_arc_index;
91 vnet_feature_config_main_t *fcm;
94 fcm = vnet_feature_get_config_main (arc);
96 now = clib_timebase_now (&mm->timebase);
98 if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
99 mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
101 from = vlib_frame_vector_args (frame);
102 n_left_from = frame->n_vectors;
103 next_index = node->cached_next_index;
105 while (n_left_from > 0)
109 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
111 while (n_left_from > 0 && n_left_to_next > 0)
118 ethernet_header_t *en0;
121 /* speculatively enqueue b0 to the current next frame */
129 b0 = vlib_get_buffer (vm, bi0);
131 /* Set next0 to e.g. interface-tx */
133 vnet_get_config_data (&fcm->config_main,
134 &b0->current_config_index, &next0,
135 /* # bytes of config data */ 0);
137 next0 = MACTIME_NEXT_ETHERNET_INPUT;
139 vlib_buffer_advance (b0, -(word) vnet_buffer (b0)->l2_hdr_offset);
141 len0 = vlib_buffer_length_in_chain (vm, b0);
142 en0 = vlib_buffer_get_current (b0);
145 clib_memcpy (&kv.key, en0->dst_address, 6);
147 clib_memcpy (&kv.key, en0->src_address, 6);
149 /* Lookup the src/dst mac address */
150 if (clib_bihash_search_8_8 (lut, &kv, &kv) < 0)
152 /* Create a table entry... */
153 mactime_send_create_entry_message
154 (is_tx ? en0->dst_address : en0->src_address);
156 /* and let this packet pass */
163 device_index0 = kv.value;
165 dp = pool_elt_at_index (mm->devices, device_index0);
167 /* Static drop / allow? */
170 (MACTIME_DEVICE_FLAG_STATIC_DROP
171 | MACTIME_DEVICE_FLAG_STATIC_ALLOW)))
173 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
175 next0 = MACTIME_NEXT_DROP;
176 vlib_increment_combined_counter
177 (&mm->drop_counters, thread_index, dp - mm->devices, 1,
181 else /* note next0 set to allow */
183 vlib_increment_combined_counter
184 (&mm->allow_counters, thread_index, dp - mm->devices, 1,
191 /* Known device, see if traffic allowed at the moment */
192 for (i = 0; i < vec_len (dp->ranges); i++)
194 clib_timebase_range_t *r = dp->ranges + i;
197 start0 = r->start + mm->sunday_midnight;
198 end0 = r->end + mm->sunday_midnight;
199 /* Packet within time range */
200 if (now >= start0 && now <= end0)
202 /* And it's a drop range, drop it */
203 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
205 vlib_increment_combined_counter
206 (&mm->drop_counters, thread_index,
207 dp - mm->devices, 1, len0);
209 next0 = MACTIME_NEXT_DROP;
211 else /* it's an allow range, allow it */
213 vlib_increment_combined_counter
214 (&mm->allow_counters, thread_index,
215 dp - mm->devices, 1, len0);
222 * Didn't hit a range, so *drop* if allow configured, or
223 * *allow* if drop configured.
225 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
227 next0 = MACTIME_NEXT_DROP;
228 vlib_increment_combined_counter
229 (&mm->drop_counters, thread_index, dp - mm->devices, 1, len0);
234 vlib_increment_combined_counter
235 (&mm->allow_counters, thread_index, dp - mm->devices, 1,
241 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
242 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
244 mactime_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
245 clib_memcpy (t->src_mac, en0->src_address, sizeof (t->src_mac));
247 t->next_index = next0;
248 t->device_index = device_index0;
252 clib_memcpy (t->device_name, dp->device_name,
253 ARRAY_LEN (t->device_name));
254 t->device_name[ARRAY_LEN (t->device_name) - 1] = 0;
258 /* verify speculative enqueue, maybe switch current next frame */
259 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
260 to_next, n_left_to_next,
264 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
267 vlib_node_increment_counter (vm, node->node_index,
268 MACTIME_ERROR_DROP, packets_dropped);
269 vlib_node_increment_counter (vm, node->node_index,
270 MACTIME_ERROR_OK, packets_ok);
271 return frame->n_vectors;
275 mactime_node_fn (vlib_main_t * vm,
276 vlib_node_runtime_t * node, vlib_frame_t * frame)
278 return mactime_node_inline (vm, node, frame, 0 /* is_tx */ );
282 VLIB_REGISTER_NODE (mactime_node) =
284 .function = mactime_node_fn,
286 .vector_size = sizeof (u32),
287 .format_trace = format_mactime_trace,
288 .type = VLIB_NODE_TYPE_INTERNAL,
290 .n_errors = ARRAY_LEN(mactime_error_strings),
291 .error_strings = mactime_error_strings,
293 .n_next_nodes = MACTIME_N_NEXT,
295 /* edit / add dispositions here */
298 [MACTIME_NEXT_ETHERNET_INPUT] = "ethernet-input",
299 [MACTIME_NEXT_DROP] = "error-drop",
305 mactime_tx_node_fn (vlib_main_t * vm,
306 vlib_node_runtime_t * node, vlib_frame_t * frame)
308 return mactime_node_inline (vm, node, frame, 1 /* is_tx */ );
312 VLIB_REGISTER_NODE (mactime_tx_node) =
314 .function = mactime_tx_node_fn,
315 .name = "mactime-tx",
316 .vector_size = sizeof (u32),
317 .format_trace = format_mactime_trace,
318 .type = VLIB_NODE_TYPE_INTERNAL,
320 .n_errors = ARRAY_LEN(mactime_error_strings),
321 .error_strings = mactime_error_strings,
323 .n_next_nodes = MACTIME_N_NEXT,
325 /* edit / add dispositions here */
328 [MACTIME_NEXT_DROP] = "error-drop",
329 [MACTIME_NEXT_ETHERNET_INPUT] = "ethernet-input", /* notused */
335 * fd.io coding-style-patch-verification: ON
338 * eval: (c-set-style "gnu")