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 /* Index is the key for the stats segment combined counters */
196 ep->pool_index = clib_host_to_net_u32 (dev - mm->devices);
198 clib_memcpy_fast (ep->mac_address, dev->mac_address,
199 sizeof (ep->mac_address));
200 ep->data_quota = clib_host_to_net_u64 (dev->data_quota);
201 ep->data_used_in_range = clib_host_to_net_u64 (dev->data_used_in_range);
202 ep->flags = clib_host_to_net_u32 (dev->flags);
203 nranges = vec_len (dev->ranges);
204 ep->nranges = clib_host_to_net_u32 (nranges);
206 for (i = 0; i < vec_len (dev->ranges); i++)
208 ep->ranges[i].start = dev->ranges[i].start;
209 ep->ranges[i].end = dev->ranges[i].end;
212 name_len = vec_len (dev->device_name);
213 name_len = (name_len < ARRAY_LEN(ep->device_name)) ?
214 name_len : ARRAY_LEN(ep->device_name) - 1;
216 clib_memcpy_fast (ep->device_name, dev->device_name,
218 ep->device_name [ARRAY_LEN(ep->device_name) -1] = 0;
219 vl_api_send_msg (rp, (u8 *)ep);
225 REPLY_MACRO2 (VL_API_MACTIME_DUMP_REPLY,
227 rmp->table_epoch = clib_host_to_net_u32 (mm->device_table_epoch);
232 /** Create a lookup table entry for the indicated mac address
235 mactime_send_create_entry_message (u8 * mac_address)
237 mactime_main_t *mm = &mactime_main;
239 vl_shmem_hdr_t *shmem_hdr;
241 vl_api_mactime_add_del_range_t *mp;
244 shmem_hdr = am->shmem_hdr;
245 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
246 clib_memset (mp, 0, sizeof (*mp));
247 mp->_vl_msg_id = ntohs (VL_API_MACTIME_ADD_DEL_RANGE + mm->msg_id_base);
248 name = format (0, "mac-%U", format_mac_address, mac_address);
250 memcpy (mp->device_name, name, vec_len (name));
251 memcpy (mp->mac_address, mac_address, sizeof (mp->mac_address));
252 /* $$$ config: create allow / drop / range */
255 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
258 /** Add or delete static / dynamic accept/drop configuration for a src mac
261 static void vl_api_mactime_add_del_range_t_handler
262 (vl_api_mactime_add_del_range_t * mp)
264 mactime_main_t *mm = &mactime_main;
265 vl_api_mactime_add_del_range_reply_t *rmp;
266 mactime_device_t *dp;
267 clib_bihash_kv_8_8_t kv;
269 clib_bihash_8_8_t *lut = &mm->lookup_table;
276 * Change the table epoch. Skip 0 so clients can code my_table_epoch = 0
277 * to receive a full dump.
279 mm->device_table_epoch++;
280 if (PREDICT_FALSE (mm->device_table_epoch == 0))
281 mm->device_table_epoch++;
283 data_quota = clib_net_to_host_u64 (mp->data_quota);
285 clib_memset (&kv, 0, sizeof (kv));
286 memcpy (&kv.key, mp->mac_address, sizeof (mp->mac_address));
288 /* See if we have a lookup table entry for this src mac address */
289 if (clib_bihash_search_8_8 (lut, &kv, &kv) < 0)
295 /* Create the device entry? */
298 pool_get (mm->devices, dp);
299 clib_memset (dp, 0, sizeof (*dp));
300 vlib_validate_combined_counter (&mm->allow_counters,
302 vlib_zero_combined_counter (&mm->allow_counters, dp - mm->devices);
303 vlib_validate_combined_counter (&mm->drop_counters,
305 vlib_zero_combined_counter (&mm->drop_counters, dp - mm->devices);
306 mp->device_name[ARRAY_LEN (mp->device_name) - 1] = 0;
307 dp->device_name = format (0, "%s%c", mp->device_name, 0);
308 memcpy (dp->mac_address, mp->mac_address, sizeof (mp->mac_address));
309 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
311 clib_timebase_range_t _r, *r = &_r;
312 r->start = mp->ranges[i].start;
313 r->end = mp->ranges[i].end;
314 vec_add1 (dp->ranges, r[0]);
316 /* If we found some time ranges */
319 /* Set allow/drop based on msg flags */
321 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_DROP;
323 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW;
325 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA;
329 /* no ranges, it's a static allow/drop */
331 dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP;
333 dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW;
335 if (mp->no_udp_10001)
336 dp->flags |= MACTIME_DEVICE_FLAG_DROP_UDP_10001;
338 dp->data_quota = data_quota;
340 /* Add the hash table entry */
341 kv.value = dp - mm->devices;
342 clib_bihash_add_del_8_8 (lut, &kv, 1 /* is_add */ );
344 else /* add more ranges, flags, etc. */
346 dp = pool_elt_at_index (mm->devices, kv.value);
348 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
350 clib_timebase_range_t _r, *r = &_r;
351 r->start = mp->ranges[i].start;
352 r->end = mp->ranges[i].end;
353 vec_add1 (dp->ranges, r[0]);
356 if (vec_len (dp->ranges))
358 /* Set allow/drop based on msg flags */
360 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_DROP;
362 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW;
364 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA;
368 /* no ranges, it's a static allow/drop */
370 dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP;
372 dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW;
374 if (mp->no_udp_10001)
375 dp->flags |= MACTIME_DEVICE_FLAG_DROP_UDP_10001;
377 dp->data_quota = data_quota;
380 else /* delete case */
384 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
388 /* find the device entry */
389 dp = pool_elt_at_index (mm->devices, kv.value);
391 /* Remove it from the lookup table */
392 clib_bihash_add_del_8_8 (lut, &kv, 0 /* is_add */ );
393 vec_free (dp->ranges);
394 pool_put (mm->devices, dp);
398 REPLY_MACRO (VL_API_MACTIME_ADD_DEL_RANGE_REPLY);
401 #include <mactime/mactime.api.c>
402 static clib_error_t *
403 mactime_init (vlib_main_t * vm)
405 mactime_main_t *mm = &mactime_main;
408 mm->vnet_main = vnet_get_main ();
410 /* Ask for a correctly-sized block of API message decode slots */
411 mm->msg_id_base = setup_message_id_table ();
413 mm->lookup_table_num_buckets = MACTIME_NUM_BUCKETS;
414 mm->lookup_table_memory_size = MACTIME_MEMORY_SIZE;
415 mm->timezone_offset = -5; /* US EST / EDT */
419 VLIB_INIT_FUNCTION (mactime_init);
421 static clib_error_t *
422 mactime_config (vlib_main_t * vm, unformat_input_t * input)
424 mactime_main_t *mm = &mactime_main;
426 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
428 if (unformat (input, "lookup-table-buckets %u",
429 &mm->lookup_table_num_buckets))
431 else if (unformat (input, "lookup-table-memory %U",
432 unformat_memory_size, &mm->lookup_table_memory_size))
434 else if (unformat (input, "timezone_offset %d", &mm->timezone_offset))
438 return clib_error_return (0, "unknown input '%U'",
439 format_unformat_error, input);
445 VLIB_CONFIG_FUNCTION (mactime_config, "mactime");
448 VNET_FEATURE_INIT (mactime, static) =
450 .arc_name = "device-input",
451 .node_name = "mactime",
452 .runs_before = VNET_FEATURES ("ethernet-input"),
457 VNET_FEATURE_INIT (mactime_tx, static) =
459 .arc_name = "interface-output",
460 .node_name = "mactime-tx",
461 .runs_before = VNET_FEATURES ("interface-tx"),
466 VLIB_PLUGIN_REGISTER () =
468 .version = VPP_BUILD_VER,
469 .description = "Time-based MAC Source Address Filter",
474 format_bytes_with_width (u8 * s, va_list * va)
476 uword nbytes = va_arg (*va, u64);
477 int width = va_arg (*va, int);
483 fmt = format (0, "%%%d.3f%%s%c", width, 0);
485 fmt = format (0, "%%.3f%%s%c", 0);
487 if (nbytes > (1024ULL * 1024ULL * 1024ULL))
489 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0 * 1024.0);
492 else if (nbytes > (1024ULL * 1024ULL))
494 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0);
497 else if (nbytes > 1024ULL)
499 nbytes_f64 = ((f64) nbytes) / (1024.0);
504 nbytes_f64 = (f64) nbytes;
508 s = format (s, (char *) fmt, nbytes_f64, suffix);
513 static clib_error_t *
514 show_mactime_command_fn (vlib_main_t * vm,
515 unformat_input_t * input, vlib_cli_command_t * cmd)
517 mactime_main_t *mm = &mactime_main;
518 mactime_device_t *dp;
521 u32 *pool_indices = 0;
523 int current_status = 99;
526 vlib_counter_t allow, drop;
527 ethernet_arp_ip4_entry_t *n, *pool;
529 vec_reset_length (mm->arp_cache_copy);
530 pool = ip4_neighbors_pool ();
533 pool_foreach (n, pool,
535 vec_add1 (mm->arp_cache_copy, n[0]);
539 now = clib_timebase_now (&mm->timebase);
541 if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
542 mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
544 if (unformat (input, "verbose %d", &verbose))
547 if (unformat (input, "verbose"))
551 vlib_cli_output (vm, "Time now: %U", format_clib_timebase_time, now);
554 pool_foreach (dp, mm->devices,
556 vec_add1 (pool_indices, dp - mm->devices);
560 vlib_cli_output (vm, "%-15s %18s %14s %10s %11s %13s",
561 "Device Name", "Addresses", "Status",
562 "AllowPkt", "AllowByte", "DropPkt");
564 for (i = 0; i < vec_len (pool_indices); i++)
566 dp = pool_elt_at_index (mm->devices, pool_indices[i]);
568 /* Check dynamic ranges */
569 for (j = 0; j < vec_len (dp->ranges); j++)
571 clib_timebase_range_t *r = dp->ranges + j;
574 start0 = r->start + mm->sunday_midnight;
575 end0 = r->end + mm->sunday_midnight;
577 vlib_cli_output (vm, " Range %d: %U - %U", j,
578 format_clib_timebase_time, start0,
579 format_clib_timebase_time, end0);
581 if (now >= start0 && now <= end0)
583 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
585 else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
591 vlib_cli_output (vm, " Time in range %d:", j);
592 vlib_cli_output (vm, " %U - %U",
593 format_clib_timebase_time, start0,
594 format_clib_timebase_time, end0);
600 vlib_cli_output (vm, " No range match.");
601 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
603 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
605 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
607 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
609 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
613 vec_reset_length (macstring);
614 macstring = format (0, "%U", format_mac_address, dp->mac_address);
615 switch (current_status)
618 status_string = "static drop";
621 status_string = "static allow";
624 status_string = "dynamic drop";
627 status_string = "dynamic allow";
630 status_string = "d-quota inact";
633 status_string = "d-quota activ";
636 status_string = "code bug!";
639 vlib_get_combined_counter (&mm->allow_counters, dp - mm->devices,
641 vlib_get_combined_counter (&mm->drop_counters, dp - mm->devices, &drop);
642 vlib_cli_output (vm, "%-15s %18s %14s %10lld %U %13lld",
643 dp->device_name, macstring, status_string,
644 allow.packets, format_bytes_with_width, allow.bytes,
646 if (dp->data_quota > 0)
647 vlib_cli_output (vm, "%-54s %s%U %s%U", " ", "Quota ",
648 format_bytes_with_width, dp->data_quota, 10,
649 "Use ", format_bytes_with_width,
650 dp->data_used_in_range, 8);
651 /* This is really only good for small N... */
652 for (j = 0; j < vec_len (mm->arp_cache_copy); j++)
654 n = mm->arp_cache_copy + j;
655 if (!memcmp (dp->mac_address, n->mac.bytes, sizeof (n->mac)))
657 vlib_cli_output (vm, "%17s%U", " ", format_ip4_address,
662 vec_free (macstring);
663 vec_free (pool_indices);
669 VLIB_CLI_COMMAND (show_mactime_command, static) =
671 .path = "show mactime",
672 .short_help = "show mactime [verbose]",
673 .function = show_mactime_command_fn,
677 static clib_error_t *
678 clear_mactime_command_fn (vlib_main_t * vm,
679 unformat_input_t * input, vlib_cli_command_t * cmd)
681 mactime_main_t *mm = &mactime_main;
683 if (mm->feature_initialized == 0)
684 return clib_error_return (0, "feature not enabled");
686 vlib_clear_combined_counters (&mm->allow_counters);
687 vlib_clear_combined_counters (&mm->drop_counters);
688 vlib_cli_output (vm, "Mactime counters cleared...");
693 VLIB_CLI_COMMAND (clear_mactime_command, static) =
695 .path = "clear mactime",
696 .short_help = "clear mactime counters",
697 .function = clear_mactime_command_fn,
704 * fd.io coding-style-patch-verification: ON
707 * eval: (c-set-style "gnu")