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;
71 if (pool_is_free_index (mm->vnet_main->interface_main.sw_interfaces,
73 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
75 /* Not a physical port? */
76 sw = vnet_get_sw_interface (mm->vnet_main, sw_if_index);
77 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
78 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
80 vnet_feature_enable_disable ("device-input", "mactime",
81 sw_if_index, enable_disable, 0, 0);
82 vnet_feature_enable_disable ("interface-output", "mactime-tx",
83 sw_if_index, enable_disable, 0, 0);
88 mactime_enable_disable_command_fn (vlib_main_t * vm,
89 unformat_input_t * input,
90 vlib_cli_command_t * cmd)
92 mactime_main_t *mm = &mactime_main;
94 int enable_disable = 1;
98 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
100 if (unformat (input, "disable"))
102 else if (unformat (input, "%U", unformat_vnet_sw_interface,
103 mm->vnet_main, &sw_if_index))
105 else if (unformat (input, "sw_if_index %d", &sw_if_index))
111 if (sw_if_index == ~0)
112 return clib_error_return (0, "Please specify an interface...");
114 rv = mactime_enable_disable (mm, sw_if_index, enable_disable);
121 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
122 return clib_error_return
123 (0, "Invalid interface, only works on physical ports");
127 return clib_error_return (0, "mactime_enable_disable returned %d", rv);
133 VLIB_CLI_COMMAND (mactime_enable_disable_command, static) =
135 .path = "mactime enable-disable",
137 "mactime enable-disable <interface-name> [disable]",
138 .function = mactime_enable_disable_command_fn,
143 /** Enable / disable time-base src mac filtration on an interface
146 static void vl_api_mactime_enable_disable_t_handler
147 (vl_api_mactime_enable_disable_t * mp)
149 vl_api_mactime_enable_disable_reply_t *rmp;
150 mactime_main_t *mm = &mactime_main;
153 VALIDATE_SW_IF_INDEX (mp);
155 rv = mactime_enable_disable (mm, ntohl (mp->sw_if_index),
156 (int) (mp->enable_disable));
157 BAD_SW_IF_INDEX_LABEL;
158 REPLY_MACRO (VL_API_MACTIME_ENABLE_DISABLE_REPLY);
162 vl_api_mactime_dump_t_handler (vl_api_mactime_dump_t * mp)
164 vl_api_mactime_details_t *ep;
165 vl_api_mactime_dump_reply_t *rmp;
166 mactime_device_t *dev;
167 mactime_main_t *mm = &mactime_main;
168 vl_api_registration_t *rp;
170 u32 his_table_epoch = clib_net_to_host_u32 (mp->my_table_epoch);
175 rp = vl_api_client_index_to_registration (mp->client_index);
179 if (his_table_epoch == mm->device_table_epoch)
181 rv = VNET_API_ERROR_NO_CHANGE;
186 pool_foreach (dev, mm->devices,
188 message_size = sizeof(*ep) + vec_len(dev->device_name) +
189 vec_len(dev->ranges) * sizeof(ep->ranges[0]);
191 ep = vl_msg_api_alloc (message_size);
192 memset (ep, 0, message_size);
193 ep->_vl_msg_id = clib_host_to_net_u16 (VL_API_MACTIME_DETAILS
195 ep->context = mp->context;
196 /* Index is the key for the stats segment combined counters */
197 ep->pool_index = clib_host_to_net_u32 (dev - mm->devices);
199 clib_memcpy_fast (ep->mac_address, dev->mac_address,
200 sizeof (ep->mac_address));
201 ep->data_quota = clib_host_to_net_u64 (dev->data_quota);
202 ep->data_used_in_range = clib_host_to_net_u64 (dev->data_used_in_range);
203 ep->flags = clib_host_to_net_u32 (dev->flags);
204 nranges = vec_len (dev->ranges);
205 ep->nranges = clib_host_to_net_u32 (nranges);
207 for (i = 0; i < vec_len (dev->ranges); i++)
209 ep->ranges[i].start = dev->ranges[i].start;
210 ep->ranges[i].end = dev->ranges[i].end;
213 name_len = vec_len (dev->device_name);
214 name_len = (name_len < ARRAY_LEN(ep->device_name)) ?
215 name_len : ARRAY_LEN(ep->device_name) - 1;
217 clib_memcpy_fast (ep->device_name, dev->device_name,
219 ep->device_name [ARRAY_LEN(ep->device_name) -1] = 0;
220 vl_api_send_msg (rp, (u8 *)ep);
226 REPLY_MACRO2 (VL_API_MACTIME_DUMP_REPLY,
228 rmp->table_epoch = clib_host_to_net_u32 (mm->device_table_epoch);
233 /** Create a lookup table entry for the indicated mac address
236 mactime_send_create_entry_message (u8 * mac_address)
238 mactime_main_t *mm = &mactime_main;
240 vl_shmem_hdr_t *shmem_hdr;
242 vl_api_mactime_add_del_range_t *mp;
245 shmem_hdr = am->shmem_hdr;
246 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
247 clib_memset (mp, 0, sizeof (*mp));
248 mp->_vl_msg_id = ntohs (VL_API_MACTIME_ADD_DEL_RANGE + mm->msg_id_base);
249 name = format (0, "mac-%U", format_mac_address, mac_address);
251 memcpy (mp->device_name, name, vec_len (name));
252 memcpy (mp->mac_address, mac_address, sizeof (mp->mac_address));
253 /* $$$ config: create allow / drop / range */
256 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
259 /** Add or delete static / dynamic accept/drop configuration for a src mac
262 static void vl_api_mactime_add_del_range_t_handler
263 (vl_api_mactime_add_del_range_t * mp)
265 mactime_main_t *mm = &mactime_main;
266 vl_api_mactime_add_del_range_reply_t *rmp;
267 mactime_device_t *dp;
268 clib_bihash_kv_8_8_t kv;
270 clib_bihash_8_8_t *lut = &mm->lookup_table;
277 * Change the table epoch. Skip 0 so clients can code my_table_epoch = 0
278 * to receive a full dump.
280 mm->device_table_epoch++;
281 if (PREDICT_FALSE (mm->device_table_epoch == 0))
282 mm->device_table_epoch++;
284 data_quota = clib_net_to_host_u64 (mp->data_quota);
286 clib_memset (&kv, 0, sizeof (kv));
287 memcpy (&kv.key, mp->mac_address, sizeof (mp->mac_address));
289 /* See if we have a lookup table entry for this src mac address */
290 if (clib_bihash_search_8_8 (lut, &kv, &kv) < 0)
296 /* Create the device entry? */
299 pool_get (mm->devices, dp);
300 clib_memset (dp, 0, sizeof (*dp));
301 vlib_validate_combined_counter (&mm->allow_counters,
303 vlib_zero_combined_counter (&mm->allow_counters, dp - mm->devices);
304 vlib_validate_combined_counter (&mm->drop_counters,
306 vlib_zero_combined_counter (&mm->drop_counters, dp - mm->devices);
307 mp->device_name[ARRAY_LEN (mp->device_name) - 1] = 0;
308 dp->device_name = format (0, "%s%c", mp->device_name, 0);
309 memcpy (dp->mac_address, mp->mac_address, sizeof (mp->mac_address));
310 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
312 clib_timebase_range_t _r, *r = &_r;
313 r->start = mp->ranges[i].start;
314 r->end = mp->ranges[i].end;
315 vec_add1 (dp->ranges, r[0]);
317 /* If we found some time ranges */
320 /* Set allow/drop based on msg flags */
322 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_DROP;
324 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW;
326 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA;
330 /* no ranges, it's a static allow/drop */
332 dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP;
334 dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW;
336 if (mp->no_udp_10001)
337 dp->flags |= MACTIME_DEVICE_FLAG_DROP_UDP_10001;
339 dp->data_quota = data_quota;
341 /* Add the hash table entry */
342 kv.value = dp - mm->devices;
343 clib_bihash_add_del_8_8 (lut, &kv, 1 /* is_add */ );
345 else /* add more ranges, flags, etc. */
347 dp = pool_elt_at_index (mm->devices, kv.value);
349 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
351 clib_timebase_range_t _r, *r = &_r;
352 r->start = mp->ranges[i].start;
353 r->end = mp->ranges[i].end;
354 vec_add1 (dp->ranges, r[0]);
357 if (vec_len (dp->ranges))
359 /* Set allow/drop based on msg flags */
361 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_DROP;
363 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW;
365 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA;
369 /* no ranges, it's a static allow/drop */
371 dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP;
373 dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW;
375 if (mp->no_udp_10001)
376 dp->flags |= MACTIME_DEVICE_FLAG_DROP_UDP_10001;
378 dp->data_quota = data_quota;
381 else /* delete case */
385 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
389 /* find the device entry */
390 dp = pool_elt_at_index (mm->devices, kv.value);
392 /* Remove it from the lookup table */
393 clib_bihash_add_del_8_8 (lut, &kv, 0 /* is_add */ );
394 vec_free (dp->ranges);
395 pool_put (mm->devices, dp);
399 REPLY_MACRO (VL_API_MACTIME_ADD_DEL_RANGE_REPLY);
402 #include <mactime/mactime.api.c>
403 static clib_error_t *
404 mactime_init (vlib_main_t * vm)
406 mactime_main_t *mm = &mactime_main;
409 mm->vnet_main = vnet_get_main ();
411 /* Ask for a correctly-sized block of API message decode slots */
412 mm->msg_id_base = setup_message_id_table ();
414 mm->lookup_table_num_buckets = MACTIME_NUM_BUCKETS;
415 mm->lookup_table_memory_size = MACTIME_MEMORY_SIZE;
416 mm->timezone_offset = -5; /* US EST / EDT */
420 VLIB_INIT_FUNCTION (mactime_init);
422 static clib_error_t *
423 mactime_config (vlib_main_t * vm, unformat_input_t * input)
425 mactime_main_t *mm = &mactime_main;
427 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
429 if (unformat (input, "lookup-table-buckets %u",
430 &mm->lookup_table_num_buckets))
432 else if (unformat (input, "lookup-table-memory %U",
433 unformat_memory_size, &mm->lookup_table_memory_size))
435 else if (unformat (input, "timezone_offset %d", &mm->timezone_offset))
439 return clib_error_return (0, "unknown input '%U'",
440 format_unformat_error, input);
446 VLIB_CONFIG_FUNCTION (mactime_config, "mactime");
449 VNET_FEATURE_INIT (mactime, static) =
451 .arc_name = "device-input",
452 .node_name = "mactime",
453 .runs_before = VNET_FEATURES ("ethernet-input"),
458 VNET_FEATURE_INIT (mactime_tx, static) =
460 .arc_name = "interface-output",
461 .node_name = "mactime-tx",
462 .runs_before = VNET_FEATURES ("interface-tx"),
467 VLIB_PLUGIN_REGISTER () =
469 .version = VPP_BUILD_VER,
470 .description = "Time-based MAC Source Address Filter",
475 format_bytes_with_width (u8 * s, va_list * va)
477 uword nbytes = va_arg (*va, u64);
478 int width = va_arg (*va, int);
484 fmt = format (0, "%%%d.3f%%s%c", width, 0);
486 fmt = format (0, "%%.3f%%s%c", 0);
488 if (nbytes > (1024ULL * 1024ULL * 1024ULL))
490 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0 * 1024.0);
493 else if (nbytes > (1024ULL * 1024ULL))
495 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0);
498 else if (nbytes > 1024ULL)
500 nbytes_f64 = ((f64) nbytes) / (1024.0);
505 nbytes_f64 = (f64) nbytes;
509 s = format (s, (char *) fmt, nbytes_f64, suffix);
514 static clib_error_t *
515 show_mactime_command_fn (vlib_main_t * vm,
516 unformat_input_t * input, vlib_cli_command_t * cmd)
518 mactime_main_t *mm = &mactime_main;
519 mactime_device_t *dp;
522 u32 *pool_indices = 0;
524 int current_status = 99;
527 vlib_counter_t allow, drop;
528 ethernet_arp_ip4_entry_t *n, *pool;
530 vec_reset_length (mm->arp_cache_copy);
531 pool = ip4_neighbors_pool ();
534 pool_foreach (n, pool,
536 vec_add1 (mm->arp_cache_copy, n[0]);
540 now = clib_timebase_now (&mm->timebase);
542 if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
543 mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
545 if (unformat (input, "verbose %d", &verbose))
548 if (unformat (input, "verbose"))
552 vlib_cli_output (vm, "Time now: %U", format_clib_timebase_time, now);
555 pool_foreach (dp, mm->devices,
557 vec_add1 (pool_indices, dp - mm->devices);
561 vlib_cli_output (vm, "%-15s %18s %14s %10s %11s %13s",
562 "Device Name", "Addresses", "Status",
563 "AllowPkt", "AllowByte", "DropPkt");
565 for (i = 0; i < vec_len (pool_indices); i++)
567 dp = pool_elt_at_index (mm->devices, pool_indices[i]);
569 /* Check dynamic ranges */
570 for (j = 0; j < vec_len (dp->ranges); j++)
572 clib_timebase_range_t *r = dp->ranges + j;
575 start0 = r->start + mm->sunday_midnight;
576 end0 = r->end + mm->sunday_midnight;
578 vlib_cli_output (vm, " Range %d: %U - %U", j,
579 format_clib_timebase_time, start0,
580 format_clib_timebase_time, end0);
582 if (now >= start0 && now <= end0)
584 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
586 else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
592 vlib_cli_output (vm, " Time in range %d:", j);
593 vlib_cli_output (vm, " %U - %U",
594 format_clib_timebase_time, start0,
595 format_clib_timebase_time, end0);
601 vlib_cli_output (vm, " No range match.");
602 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
604 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
606 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
608 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
610 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
614 vec_reset_length (macstring);
615 macstring = format (0, "%U", format_mac_address, dp->mac_address);
616 switch (current_status)
619 status_string = "static drop";
622 status_string = "static allow";
625 status_string = "dynamic drop";
628 status_string = "dynamic allow";
631 status_string = "d-quota inact";
634 status_string = "d-quota activ";
637 status_string = "code bug!";
640 vlib_get_combined_counter (&mm->allow_counters, dp - mm->devices,
642 vlib_get_combined_counter (&mm->drop_counters, dp - mm->devices, &drop);
643 vlib_cli_output (vm, "%-15s %18s %14s %10lld %U %13lld",
644 dp->device_name, macstring, status_string,
645 allow.packets, format_bytes_with_width, allow.bytes,
647 if (dp->data_quota > 0)
648 vlib_cli_output (vm, "%-54s %s%U %s%U", " ", "Quota ",
649 format_bytes_with_width, dp->data_quota, 10,
650 "Use ", format_bytes_with_width,
651 dp->data_used_in_range, 8);
652 /* This is really only good for small N... */
653 for (j = 0; j < vec_len (mm->arp_cache_copy); j++)
655 n = mm->arp_cache_copy + j;
656 if (!memcmp (dp->mac_address, n->mac.bytes, sizeof (n->mac)))
658 vlib_cli_output (vm, "%17s%U", " ", format_ip4_address,
663 vec_free (macstring);
664 vec_free (pool_indices);
670 VLIB_CLI_COMMAND (show_mactime_command, static) =
672 .path = "show mactime",
673 .short_help = "show mactime [verbose]",
674 .function = show_mactime_command_fn,
678 static clib_error_t *
679 clear_mactime_command_fn (vlib_main_t * vm,
680 unformat_input_t * input, vlib_cli_command_t * cmd)
682 mactime_main_t *mm = &mactime_main;
684 if (mm->feature_initialized == 0)
685 return clib_error_return (0, "feature not enabled");
687 vlib_clear_combined_counters (&mm->allow_counters);
688 vlib_clear_combined_counters (&mm->drop_counters);
689 vlib_cli_output (vm, "Mactime counters cleared...");
694 VLIB_CLI_COMMAND (clear_mactime_command, static) =
696 .path = "clear mactime",
697 .short_help = "clear mactime counters",
698 .function = clear_mactime_command_fn,
705 * fd.io coding-style-patch-verification: ON
708 * eval: (c-set-style "gnu")