Remove c-11 memcpy checks from perf-critical code
[vpp.git] / src / plugins / mactime / node.c
1 /*
2  * node.c - skeleton vpp engine plug-in dual-loop node skeleton
3  *
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:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
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
23 typedef struct
24 {
25   u32 next_index;
26   u32 device_index;
27   u8 src_mac[6];
28   u8 device_name[64];
29 } mactime_trace_t;
30
31 vlib_node_registration_t mactime_node;
32 vlib_node_registration_t mactime_tx_node;
33
34 #define foreach_mactime_error                   \
35 _(DROP, "Dropped packets")                      \
36 _(OK, "Permitted packets")
37
38 typedef enum
39 {
40 #define _(sym,str) MACTIME_ERROR_##sym,
41   foreach_mactime_error
42 #undef _
43     MACTIME_N_ERROR,
44 } mactime_error_t;
45
46 static char *mactime_error_strings[] = {
47 #define _(sym,string) string,
48   foreach_mactime_error
49 #undef _
50 };
51
52 typedef enum
53 {
54   MACTIME_NEXT_DROP,
55   MACTIME_NEXT_ETHERNET_INPUT,
56   MACTIME_N_NEXT,
57 } mactime_next_t;
58
59 /* packet trace format function */
60 static u8 *
61 format_mactime_trace (u8 * s, va_list * args)
62 {
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 *);
66
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");
71   return s;
72 }
73
74 static uword
75 mactime_node_inline (vlib_main_t * vm,
76                      vlib_node_runtime_t * node, vlib_frame_t * frame,
77                      int is_tx)
78 {
79   u32 n_left_from, *from, *to_next;
80   mactime_next_t next_index;
81   mactime_main_t *mm = &mactime_main;
82   mactime_device_t *dp;
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;
86   f64 now;
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;
92
93   if (is_tx)
94     fcm = vnet_feature_get_config_main (arc);
95
96   now = clib_timebase_now (&mm->timebase);
97
98   if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
99     mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
100
101   from = vlib_frame_vector_args (frame);
102   n_left_from = frame->n_vectors;
103   next_index = node->cached_next_index;
104
105   while (n_left_from > 0)
106     {
107       u32 n_left_to_next;
108
109       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
110
111       while (n_left_from > 0 && n_left_to_next > 0)
112         {
113           u32 bi0;
114           vlib_buffer_t *b0;
115           u32 next0;
116           u32 device_index0;
117           u32 len0;
118           ethernet_header_t *en0;
119           int i;
120
121           /* speculatively enqueue b0 to the current next frame */
122           bi0 = from[0];
123           to_next[0] = bi0;
124           from += 1;
125           to_next += 1;
126           n_left_from -= 1;
127           n_left_to_next -= 1;
128
129           b0 = vlib_get_buffer (vm, bi0);
130
131           /* Set next0 to e.g. interface-tx */
132           if (is_tx)
133             vnet_get_config_data (&fcm->config_main,
134                                   &b0->current_config_index, &next0,
135                                   /* # bytes of config data */ 0);
136           else
137             next0 = MACTIME_NEXT_ETHERNET_INPUT;
138
139           vlib_buffer_advance (b0, -(word) vnet_buffer (b0)->l2_hdr_offset);
140
141           len0 = vlib_buffer_length_in_chain (vm, b0);
142           en0 = vlib_buffer_get_current (b0);
143           kv.key = 0;
144           if (is_tx)
145             clib_memcpy_fast (&kv.key, en0->dst_address, 6);
146           else
147             clib_memcpy_fast (&kv.key, en0->src_address, 6);
148
149           /* Lookup the src/dst mac address */
150           if (clib_bihash_search_8_8 (lut, &kv, &kv) < 0)
151             {
152               /* Create a table entry... */
153               mactime_send_create_entry_message
154                 (is_tx ? en0->dst_address : en0->src_address);
155
156               /* and let this packet pass */
157               device_index0 = ~0;
158               dp = 0;
159               packets_ok++;
160               goto trace0;
161             }
162           else
163             device_index0 = kv.value;
164
165           dp = pool_elt_at_index (mm->devices, device_index0);
166
167           /* Static drop / allow? */
168           if (PREDICT_FALSE
169               (dp->flags &
170                (MACTIME_DEVICE_FLAG_STATIC_DROP
171                 | MACTIME_DEVICE_FLAG_STATIC_ALLOW)))
172             {
173               if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
174                 {
175                   next0 = MACTIME_NEXT_DROP;
176                   vlib_increment_combined_counter
177                     (&mm->drop_counters, thread_index, dp - mm->devices, 1,
178                      len0);
179                   packets_dropped++;
180                 }
181               else              /* note next0 set to allow */
182                 {
183                   vlib_increment_combined_counter
184                     (&mm->allow_counters, thread_index, dp - mm->devices, 1,
185                      len0);
186                   packets_ok++;
187                 }
188               goto trace0;
189             }
190
191           /* Known device, see if traffic allowed at the moment */
192           for (i = 0; i < vec_len (dp->ranges); i++)
193             {
194               clib_timebase_range_t *r = dp->ranges + i;
195               f64 start0, end0;
196
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)
201                 {
202                   /* And it's a drop range, drop it */
203                   if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
204                     {
205                       vlib_increment_combined_counter
206                         (&mm->drop_counters, thread_index,
207                          dp - mm->devices, 1, len0);
208                       packets_dropped++;
209                       next0 = MACTIME_NEXT_DROP;
210                     }
211                   else          /* it's an allow range, allow it */
212                     {
213                       vlib_increment_combined_counter
214                         (&mm->allow_counters, thread_index,
215                          dp - mm->devices, 1, len0);
216                       packets_ok++;
217                     }
218                   goto trace0;
219                 }
220             }
221           /*
222            * Didn't hit a range, so *drop* if allow configured, or
223            * *allow* if drop configured.
224            */
225           if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
226             {
227               next0 = MACTIME_NEXT_DROP;
228               vlib_increment_combined_counter
229                 (&mm->drop_counters, thread_index, dp - mm->devices, 1, len0);
230               packets_dropped++;
231             }
232           else
233             {
234               vlib_increment_combined_counter
235                 (&mm->allow_counters, thread_index, dp - mm->devices, 1,
236                  len0);
237               packets_ok++;
238             }
239
240         trace0:
241           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
242                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
243             {
244               mactime_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
245               clib_memcpy_fast (t->src_mac, en0->src_address,
246                                 sizeof (t->src_mac));
247
248               t->next_index = next0;
249               t->device_index = device_index0;
250
251               if (dp)
252                 {
253                   clib_memcpy_fast (t->device_name, dp->device_name,
254                                     ARRAY_LEN (t->device_name));
255                   t->device_name[ARRAY_LEN (t->device_name) - 1] = 0;
256                 }
257             }
258
259           /* verify speculative enqueue, maybe switch current next frame */
260           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
261                                            to_next, n_left_to_next,
262                                            bi0, next0);
263         }
264
265       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
266     }
267
268   vlib_node_increment_counter (vm, node->node_index,
269                                MACTIME_ERROR_DROP, packets_dropped);
270   vlib_node_increment_counter (vm, node->node_index,
271                                MACTIME_ERROR_OK, packets_ok);
272   return frame->n_vectors;
273 }
274
275 static uword
276 mactime_node_fn (vlib_main_t * vm,
277                  vlib_node_runtime_t * node, vlib_frame_t * frame)
278 {
279   return mactime_node_inline (vm, node, frame, 0 /* is_tx */ );
280 }
281
282 /* *INDENT-OFF* */
283 VLIB_REGISTER_NODE (mactime_node) =
284 {
285   .function = mactime_node_fn,
286   .name = "mactime",
287   .vector_size = sizeof (u32),
288   .format_trace = format_mactime_trace,
289   .type = VLIB_NODE_TYPE_INTERNAL,
290
291   .n_errors = ARRAY_LEN(mactime_error_strings),
292   .error_strings = mactime_error_strings,
293
294   .n_next_nodes = MACTIME_N_NEXT,
295
296   /* edit / add dispositions here */
297   .next_nodes =
298   {
299     [MACTIME_NEXT_ETHERNET_INPUT] = "ethernet-input",
300     [MACTIME_NEXT_DROP] = "error-drop",
301   },
302 };
303 /* *INDENT-ON* */
304
305 static uword
306 mactime_tx_node_fn (vlib_main_t * vm,
307                     vlib_node_runtime_t * node, vlib_frame_t * frame)
308 {
309   return mactime_node_inline (vm, node, frame, 1 /* is_tx */ );
310 }
311
312 /* *INDENT-OFF* */
313 VLIB_REGISTER_NODE (mactime_tx_node) =
314 {
315   .function = mactime_tx_node_fn,
316   .name = "mactime-tx",
317   .vector_size = sizeof (u32),
318   .format_trace = format_mactime_trace,
319   .type = VLIB_NODE_TYPE_INTERNAL,
320
321   .n_errors = ARRAY_LEN(mactime_error_strings),
322   .error_strings = mactime_error_strings,
323
324   .n_next_nodes = MACTIME_N_NEXT,
325
326   /* edit / add dispositions here */
327   .next_nodes =
328   {
329     [MACTIME_NEXT_DROP] = "error-drop",
330     [MACTIME_NEXT_ETHERNET_INPUT] = "ethernet-input", /* notused */
331   },
332 };
333 /* *INDENT-ON* */
334
335 /*
336  * fd.io coding-style-patch-verification: ON
337  *
338  * Local Variables:
339  * eval: (c-set-style "gnu")
340  * End:
341  */