interface: refactor interface capabilities code
[vpp.git] / src / vnet / interface / caps.c
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright(c) 2021 Cisco Systems, Inc.
3  */
4
5 #include <vlib/vlib.h>
6 #include <vnet/vnet.h>
7 #include <vnet/interface.h>
8
9 VLIB_REGISTER_LOG_CLASS (if_caps_log, static) = {
10   .class_name = "interface",
11   .subclass_name = "caps",
12 };
13
14 #define log_debug(fmt, ...)                                                   \
15   vlib_log_debug (if_caps_log.class, fmt, __VA_ARGS__)
16
17 format_function_t format_vnet_hw_if_caps;
18
19 void
20 vnet_hw_if_change_caps (vnet_main_t *vnm, u32 hw_if_index,
21                         vnet_hw_if_caps_change_t *caps)
22 {
23   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
24   vnet_hw_if_caps_t old = hi->caps;
25
26   hi->caps = (hi->caps & ~caps->mask) | caps->val;
27
28   log_debug ("change: interface %U, set: %U, cleared: %U",
29              format_vnet_hw_if_index_name, vnm, hw_if_index,
30              format_vnet_hw_if_caps, (old ^ hi->caps) & caps->val,
31              format_vnet_hw_if_caps, (old ^ hi->caps) & ~caps->val);
32 }
33
34 u8 *
35 format_vnet_hw_if_caps (u8 *s, va_list *va)
36 {
37   vnet_hw_if_caps_t caps = va_arg (*va, vnet_hw_if_caps_t);
38
39   const char *strings[sizeof (vnet_hw_if_caps_t) * 8] = {
40 #define _(bit, sfx, str) [bit] = (str),
41     foreach_vnet_hw_if_caps
42 #undef _
43   };
44
45   if (caps == 0)
46     return format (s, "none");
47
48   while (caps)
49     {
50       int bit = get_lowest_set_bit_index (caps);
51
52       if (strings[bit])
53         s = format (s, "%s", strings[bit]);
54       else
55         s = format (s, "unknown-%u", bit);
56
57       caps = reset_lowest_set_bit (caps);
58       if (caps)
59         vec_add1 (s, ' ');
60     }
61
62   return s;
63 }