2 * mactime.c - time-based src mac address filtration
4 * Copyright (c) 2018 Cisco and/or its affiliates.
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.
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <mactime/mactime.h>
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vpp/app/version.h>
26 /* define message IDs */
27 #include <mactime/mactime.api_enum.h>
28 #include <mactime/mactime.api_types.h>
30 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
32 #define REPLY_MSG_ID_BASE mm->msg_id_base
33 #include <vlibapi/api_helper_macros.h>
35 mactime_main_t mactime_main;
37 /** \file time-base src-mac filter device-input feature arc implementation
41 feature_init (mactime_main_t * mm)
43 if (mm->feature_initialized == 0)
45 /* Create the lookup table */
46 clib_bihash_init_8_8 (&mm->lookup_table, "mactime lookup table",
47 mm->lookup_table_num_buckets,
48 mm->lookup_table_memory_size);
49 clib_timebase_init (&mm->timebase, mm->timezone_offset,
50 CLIB_TIMEBASE_DAYLIGHT_USA);
51 mm->allow_counters.name = "allow";
52 mm->allow_counters.stat_segment_name = "/mactime/allow";
53 mm->drop_counters.name = "drop";
54 mm->drop_counters.stat_segment_name = "/mactime/drop";
55 mm->feature_initialized = 1;
59 /** Action function shared between message handler and debug CLI
62 mactime_enable_disable (mactime_main_t * mm, u32 sw_if_index,
65 vnet_sw_interface_t *sw;
67 static u8 url_init_done;
72 if (pool_is_free_index (mm->vnet_main->interface_main.sw_interfaces,
74 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
76 /* Not a physical port? */
77 sw = vnet_get_sw_interface (mm->vnet_main, sw_if_index);
78 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
79 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
81 vnet_feature_enable_disable ("device-input", "mactime",
82 sw_if_index, enable_disable, 0, 0);
83 vnet_feature_enable_disable ("interface-output", "mactime-tx",
84 sw_if_index, enable_disable, 0, 0);
85 if (url_init_done == 0)
87 mactime_url_init (mm->vlib_main);
95 mactime_enable_disable_command_fn (vlib_main_t * vm,
96 unformat_input_t * input,
97 vlib_cli_command_t * cmd)
99 mactime_main_t *mm = &mactime_main;
100 u32 sw_if_index = ~0;
101 int enable_disable = 1;
105 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
107 if (unformat (input, "disable"))
109 else if (unformat (input, "%U", unformat_vnet_sw_interface,
110 mm->vnet_main, &sw_if_index))
112 else if (unformat (input, "sw_if_index %d", &sw_if_index))
118 if (sw_if_index == ~0)
119 return clib_error_return (0, "Please specify an interface...");
121 rv = mactime_enable_disable (mm, sw_if_index, enable_disable);
128 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
129 return clib_error_return
130 (0, "Invalid interface, only works on physical ports");
134 return clib_error_return (0, "mactime_enable_disable returned %d", rv);
140 VLIB_CLI_COMMAND (mactime_enable_disable_command, static) =
142 .path = "mactime enable-disable",
144 "mactime enable-disable <interface-name> [disable]",
145 .function = mactime_enable_disable_command_fn,
150 /** Enable / disable time-base src mac filtration on an interface
153 static void vl_api_mactime_enable_disable_t_handler
154 (vl_api_mactime_enable_disable_t * mp)
156 vl_api_mactime_enable_disable_reply_t *rmp;
157 mactime_main_t *mm = &mactime_main;
160 VALIDATE_SW_IF_INDEX (mp);
162 rv = mactime_enable_disable (mm, ntohl (mp->sw_if_index),
163 (int) (mp->enable_disable));
164 BAD_SW_IF_INDEX_LABEL;
165 REPLY_MACRO (VL_API_MACTIME_ENABLE_DISABLE_REPLY);
169 vl_api_mactime_dump_t_handler (vl_api_mactime_dump_t * mp)
171 vl_api_mactime_details_t *ep;
172 vl_api_mactime_dump_reply_t *rmp;
173 mactime_device_t *dev;
174 mactime_main_t *mm = &mactime_main;
175 vl_api_registration_t *rp;
177 u32 his_table_epoch = clib_net_to_host_u32 (mp->my_table_epoch);
182 rp = vl_api_client_index_to_registration (mp->client_index);
186 if (his_table_epoch == mm->device_table_epoch)
188 rv = VNET_API_ERROR_NO_CHANGE;
193 pool_foreach (dev, mm->devices,
195 message_size = sizeof(*ep) + vec_len(dev->device_name) +
196 vec_len(dev->ranges) * sizeof(ep->ranges[0]);
198 ep = vl_msg_api_alloc (message_size);
199 memset (ep, 0, message_size);
200 ep->_vl_msg_id = clib_host_to_net_u16 (VL_API_MACTIME_DETAILS
202 ep->context = mp->context;
203 /* Index is the key for the stats segment combined counters */
204 ep->pool_index = clib_host_to_net_u32 (dev - mm->devices);
206 clib_memcpy_fast (ep->mac_address, dev->mac_address,
207 sizeof (ep->mac_address));
208 ep->data_quota = clib_host_to_net_u64 (dev->data_quota);
209 ep->data_used_in_range = clib_host_to_net_u64 (dev->data_used_in_range);
210 ep->flags = clib_host_to_net_u32 (dev->flags);
211 nranges = vec_len (dev->ranges);
212 ep->nranges = clib_host_to_net_u32 (nranges);
214 for (i = 0; i < vec_len (dev->ranges); i++)
216 ep->ranges[i].start = dev->ranges[i].start;
217 ep->ranges[i].end = dev->ranges[i].end;
220 name_len = vec_len (dev->device_name);
221 name_len = (name_len < ARRAY_LEN(ep->device_name)) ?
222 name_len : ARRAY_LEN(ep->device_name) - 1;
224 clib_memcpy_fast (ep->device_name, dev->device_name,
226 ep->device_name [ARRAY_LEN(ep->device_name) -1] = 0;
227 vl_api_send_msg (rp, (u8 *)ep);
233 REPLY_MACRO2 (VL_API_MACTIME_DUMP_REPLY,
235 rmp->table_epoch = clib_host_to_net_u32 (mm->device_table_epoch);
240 /** Create a lookup table entry for the indicated mac address
243 mactime_send_create_entry_message (u8 * mac_address)
245 mactime_main_t *mm = &mactime_main;
247 vl_shmem_hdr_t *shmem_hdr;
249 vl_api_mactime_add_del_range_t *mp;
252 shmem_hdr = am->shmem_hdr;
253 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
254 clib_memset (mp, 0, sizeof (*mp));
255 mp->_vl_msg_id = ntohs (VL_API_MACTIME_ADD_DEL_RANGE + mm->msg_id_base);
256 name = format (0, "mac-%U", format_mac_address, mac_address);
258 memcpy (mp->device_name, name, vec_len (name));
259 memcpy (mp->mac_address, mac_address, sizeof (mp->mac_address));
260 /* $$$ config: create allow / drop / range */
263 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
266 /** Add or delete static / dynamic accept/drop configuration for a src mac
269 static void vl_api_mactime_add_del_range_t_handler
270 (vl_api_mactime_add_del_range_t * mp)
272 mactime_main_t *mm = &mactime_main;
273 vl_api_mactime_add_del_range_reply_t *rmp;
274 mactime_device_t *dp;
275 clib_bihash_kv_8_8_t kv;
277 clib_bihash_8_8_t *lut = &mm->lookup_table;
284 * Change the table epoch. Skip 0 so clients can code my_table_epoch = 0
285 * to receive a full dump.
287 mm->device_table_epoch++;
288 if (PREDICT_FALSE (mm->device_table_epoch == 0))
289 mm->device_table_epoch++;
291 data_quota = clib_net_to_host_u64 (mp->data_quota);
293 clib_memset (&kv, 0, sizeof (kv));
294 memcpy (&kv.key, mp->mac_address, sizeof (mp->mac_address));
296 /* See if we have a lookup table entry for this src mac address */
297 if (clib_bihash_search_8_8 (lut, &kv, &kv) < 0)
303 /* Create the device entry? */
306 pool_get (mm->devices, dp);
307 clib_memset (dp, 0, sizeof (*dp));
308 vlib_validate_combined_counter (&mm->allow_counters,
310 vlib_zero_combined_counter (&mm->allow_counters, dp - mm->devices);
311 vlib_validate_combined_counter (&mm->drop_counters,
313 vlib_zero_combined_counter (&mm->drop_counters, dp - mm->devices);
314 mp->device_name[ARRAY_LEN (mp->device_name) - 1] = 0;
315 dp->device_name = format (0, "%s%c", mp->device_name, 0);
316 memcpy (dp->mac_address, mp->mac_address, sizeof (mp->mac_address));
317 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
319 clib_timebase_range_t _r, *r = &_r;
320 r->start = mp->ranges[i].start;
321 r->end = mp->ranges[i].end;
322 vec_add1 (dp->ranges, r[0]);
324 /* If we found some time ranges */
327 /* Set allow/drop based on msg flags */
329 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_DROP;
331 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW;
333 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA;
337 /* no ranges, it's a static allow/drop */
339 dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP;
341 dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW;
343 if (mp->no_udp_10001)
344 dp->flags |= MACTIME_DEVICE_FLAG_DROP_UDP_10001;
346 dp->data_quota = data_quota;
348 /* Add the hash table entry */
349 kv.value = dp - mm->devices;
350 clib_bihash_add_del_8_8 (lut, &kv, 1 /* is_add */ );
352 else /* add more ranges, flags, etc. */
354 dp = pool_elt_at_index (mm->devices, kv.value);
356 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
358 clib_timebase_range_t _r, *r = &_r;
359 r->start = mp->ranges[i].start;
360 r->end = mp->ranges[i].end;
361 vec_add1 (dp->ranges, r[0]);
364 if (vec_len (dp->ranges))
366 /* Set allow/drop based on msg flags */
368 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_DROP;
370 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW;
372 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA;
376 /* no ranges, it's a static allow/drop */
378 dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP;
380 dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW;
382 if (mp->no_udp_10001)
383 dp->flags |= MACTIME_DEVICE_FLAG_DROP_UDP_10001;
385 dp->data_quota = data_quota;
388 else /* delete case */
392 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
396 /* find the device entry */
397 dp = pool_elt_at_index (mm->devices, kv.value);
399 /* Remove it from the lookup table */
400 clib_bihash_add_del_8_8 (lut, &kv, 0 /* is_add */ );
401 vec_free (dp->ranges);
402 pool_put (mm->devices, dp);
406 REPLY_MACRO (VL_API_MACTIME_ADD_DEL_RANGE_REPLY);
409 #include <mactime/mactime.api.c>
410 static clib_error_t *
411 mactime_init (vlib_main_t * vm)
413 mactime_main_t *mm = &mactime_main;
416 mm->vnet_main = vnet_get_main ();
418 /* Ask for a correctly-sized block of API message decode slots */
419 mm->msg_id_base = setup_message_id_table ();
421 mm->lookup_table_num_buckets = MACTIME_NUM_BUCKETS;
422 mm->lookup_table_memory_size = MACTIME_MEMORY_SIZE;
423 mm->timezone_offset = -5; /* US EST / EDT */
427 VLIB_INIT_FUNCTION (mactime_init);
429 static clib_error_t *
430 mactime_config (vlib_main_t * vm, unformat_input_t * input)
432 mactime_main_t *mm = &mactime_main;
434 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
436 if (unformat (input, "lookup-table-buckets %u",
437 &mm->lookup_table_num_buckets))
439 else if (unformat (input, "lookup-table-memory %U",
440 unformat_memory_size, &mm->lookup_table_memory_size))
442 else if (unformat (input, "timezone_offset %d", &mm->timezone_offset))
446 return clib_error_return (0, "unknown input '%U'",
447 format_unformat_error, input);
453 VLIB_CONFIG_FUNCTION (mactime_config, "mactime");
456 VNET_FEATURE_INIT (mactime, static) =
458 .arc_name = "device-input",
459 .node_name = "mactime",
460 .runs_before = VNET_FEATURES ("ethernet-input"),
465 VNET_FEATURE_INIT (mactime_tx, static) =
467 .arc_name = "interface-output",
468 .node_name = "mactime-tx",
469 .runs_before = VNET_FEATURES ("interface-tx"),
474 VLIB_PLUGIN_REGISTER () =
476 .version = VPP_BUILD_VER,
477 .description = "Time-based MAC Source Address Filter",
482 format_bytes_with_width (u8 * s, va_list * va)
484 uword nbytes = va_arg (*va, u64);
485 int width = va_arg (*va, int);
491 fmt = format (0, "%%%d.3f%%s%c", width, 0);
493 fmt = format (0, "%%.3f%%s%c", 0);
495 if (nbytes > (1024ULL * 1024ULL * 1024ULL))
497 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0 * 1024.0);
500 else if (nbytes > (1024ULL * 1024ULL))
502 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0);
505 else if (nbytes > 1024ULL)
507 nbytes_f64 = ((f64) nbytes) / (1024.0);
512 nbytes_f64 = (f64) nbytes;
516 s = format (s, (char *) fmt, nbytes_f64, suffix);
521 static clib_error_t *
522 show_mactime_command_fn (vlib_main_t * vm,
523 unformat_input_t * input, vlib_cli_command_t * cmd)
525 mactime_main_t *mm = &mactime_main;
526 mactime_device_t *dp;
529 u32 *pool_indices = 0;
531 int current_status = 99;
534 vlib_counter_t allow, drop;
535 ethernet_arp_ip4_entry_t *n, *pool;
537 vec_reset_length (mm->arp_cache_copy);
538 pool = ip4_neighbors_pool ();
541 pool_foreach (n, pool,
543 vec_add1 (mm->arp_cache_copy, n[0]);
547 now = clib_timebase_now (&mm->timebase);
549 if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
550 mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
552 if (unformat (input, "verbose %d", &verbose))
555 if (unformat (input, "verbose"))
559 vlib_cli_output (vm, "Time now: %U", format_clib_timebase_time, now);
562 pool_foreach (dp, mm->devices,
564 vec_add1 (pool_indices, dp - mm->devices);
568 vlib_cli_output (vm, "%-15s %18s %14s %10s %11s %13s",
569 "Device Name", "Addresses", "Status",
570 "AllowPkt", "AllowByte", "DropPkt");
572 for (i = 0; i < vec_len (pool_indices); i++)
574 dp = pool_elt_at_index (mm->devices, pool_indices[i]);
576 /* Check dynamic ranges */
577 for (j = 0; j < vec_len (dp->ranges); j++)
579 clib_timebase_range_t *r = dp->ranges + j;
582 start0 = r->start + mm->sunday_midnight;
583 end0 = r->end + mm->sunday_midnight;
585 vlib_cli_output (vm, " Range %d: %U - %U", j,
586 format_clib_timebase_time, start0,
587 format_clib_timebase_time, end0);
589 if (now >= start0 && now <= end0)
591 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
593 else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
599 vlib_cli_output (vm, " Time in range %d:", j);
600 vlib_cli_output (vm, " %U - %U",
601 format_clib_timebase_time, start0,
602 format_clib_timebase_time, end0);
608 vlib_cli_output (vm, " No range match.");
609 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
611 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
613 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
615 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
617 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
621 vec_reset_length (macstring);
622 macstring = format (0, "%U", format_mac_address, dp->mac_address);
623 switch (current_status)
626 status_string = "static drop";
629 status_string = "static allow";
632 status_string = "dynamic drop";
635 status_string = "dynamic allow";
638 status_string = "d-quota inact";
641 status_string = "d-quota activ";
644 status_string = "code bug!";
647 vlib_get_combined_counter (&mm->allow_counters, dp - mm->devices,
649 vlib_get_combined_counter (&mm->drop_counters, dp - mm->devices, &drop);
650 vlib_cli_output (vm, "%-15s %18s %14s %10lld %U %13lld",
651 dp->device_name, macstring, status_string,
652 allow.packets, format_bytes_with_width, allow.bytes,
654 if (dp->data_quota > 0)
655 vlib_cli_output (vm, "%-54s %s%U %s%U", " ", "Quota ",
656 format_bytes_with_width, dp->data_quota, 10,
657 "Use ", format_bytes_with_width,
658 dp->data_used_in_range, 8);
659 /* This is really only good for small N... */
660 for (j = 0; j < vec_len (mm->arp_cache_copy); j++)
662 n = mm->arp_cache_copy + j;
663 if (!memcmp (dp->mac_address, n->mac.bytes, sizeof (n->mac)))
665 vlib_cli_output (vm, "%17s%U", " ", format_ip4_address,
670 vec_free (macstring);
671 vec_free (pool_indices);
677 VLIB_CLI_COMMAND (show_mactime_command, static) =
679 .path = "show mactime",
680 .short_help = "show mactime [verbose]",
681 .function = show_mactime_command_fn,
685 static clib_error_t *
686 clear_mactime_command_fn (vlib_main_t * vm,
687 unformat_input_t * input, vlib_cli_command_t * cmd)
689 mactime_main_t *mm = &mactime_main;
691 if (mm->feature_initialized == 0)
692 return clib_error_return (0, "feature not enabled");
694 vlib_clear_combined_counters (&mm->allow_counters);
695 vlib_clear_combined_counters (&mm->drop_counters);
696 vlib_cli_output (vm, "Mactime counters cleared...");
701 VLIB_CLI_COMMAND (clear_mactime_command, static) =
703 .path = "clear mactime",
704 .short_help = "clear mactime counters",
705 .function = clear_mactime_command_fn,
712 * fd.io coding-style-patch-verification: ON
715 * eval: (c-set-style "gnu")