Line data Source code
1 : /* SPDX-License-Identifier: Apache-2.0
2 : * Copyright(c) 2021 Cisco Systems, Inc.
3 : */
4 :
5 : #include <vppinfra/vec.h>
6 : #include <vppinfra/bitmap.h>
7 :
8 : /** unformat an any sized hexadecimal bitmask into a bitmap
9 :
10 : uword * bitmap;
11 : rv = unformat ("%U", unformat_bitmap_mask, &bitmap);
12 :
13 : Standard unformat_function_t arguments
14 :
15 : @param input - pointer an unformat_input_t
16 : @param va - varargs list comprising a single uword **
17 : @returns 1 on success, 0 on failure
18 : */
19 : __clib_export uword
20 0 : unformat_bitmap_mask (unformat_input_t *input, va_list *va)
21 : {
22 0 : u8 *v = 0; /* hexadecimal vector */
23 0 : uword **bitmap_return = va_arg (*va, uword **);
24 0 : uword *bitmap = 0;
25 :
26 0 : if (unformat (input, "%U", unformat_hex_string, &v))
27 : {
28 0 : int i, s = vec_len (v) - 1; /* 's' for significance or shift */
29 :
30 : /* v[0] holds the most significant byte */
31 0 : for (i = 0; s >= 0; i++, s--)
32 0 : bitmap = clib_bitmap_set_multiple (bitmap, s * BITS (v[i]), v[i],
33 : BITS (v[i]));
34 :
35 0 : vec_free (v);
36 0 : *bitmap_return = bitmap;
37 0 : return 1;
38 : }
39 :
40 0 : return 0;
41 : }
42 :
43 : /** unformat a list of bit ranges into a bitmap (eg "0-3,5-7,11" )
44 :
45 : uword * bitmap;
46 : rv = unformat ("%U", unformat_bitmap_list, &bitmap);
47 :
48 : Standard unformat_function_t arguments
49 :
50 : @param input - pointer an unformat_input_t
51 : @param va - varargs list comprising a single uword **
52 : @returns 1 on success, 0 on failure
53 : */
54 : __clib_export uword
55 14597 : unformat_bitmap_list (unformat_input_t *input, va_list *va)
56 : {
57 14597 : uword **bitmap_return = va_arg (*va, uword **);
58 14597 : uword *bitmap = 0;
59 :
60 : u32 a, b;
61 :
62 40227 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
63 : {
64 : int i;
65 25630 : if (unformat (input, "%u-%u,", &a, &b))
66 : ;
67 23220 : else if (unformat (input, "%u,", &a))
68 8643 : b = a;
69 14577 : else if (unformat (input, "%u-%u", &a, &b))
70 : ;
71 8662 : else if (unformat (input, "%u", &a))
72 8662 : b = a;
73 0 : else if (bitmap)
74 : {
75 0 : unformat_put_input (input);
76 0 : break;
77 : }
78 : else
79 0 : goto error;
80 :
81 25630 : if (b < a)
82 0 : goto error;
83 :
84 248155 : for (i = a; i <= b; i++)
85 222525 : bitmap = clib_bitmap_set (bitmap, i, 1);
86 : }
87 14597 : *bitmap_return = bitmap;
88 14597 : return 1;
89 0 : error:
90 0 : clib_bitmap_free (bitmap);
91 0 : return 0;
92 : }
93 :
94 : /** Format a bitmap as a string of hex bytes
95 :
96 : uword * bitmap;
97 : s = format ("%U", format_bitmap_hex, bitmap);
98 :
99 : Standard format_function_t arguments
100 :
101 : @param s - string under construction
102 : @param args - varargs list comprising a single uword *
103 : @returns string under construction
104 : */
105 :
106 : __clib_export u8 *
107 82 : format_bitmap_hex (u8 *s, va_list *args)
108 : {
109 82 : uword *bitmap = va_arg (*args, uword *);
110 82 : int i, is_trailing_zero = 1;
111 :
112 82 : if (!bitmap)
113 10 : return format (s, "0");
114 :
115 72 : i = vec_bytes (bitmap) * 2;
116 :
117 1032 : while (i > 0)
118 : {
119 960 : u8 x = clib_bitmap_get_multiple (bitmap, --i * 4, 4);
120 :
121 960 : if (x && is_trailing_zero)
122 32 : is_trailing_zero = 0;
123 :
124 960 : if (x || !is_trailing_zero)
125 32 : s = format (s, "%x", x);
126 : }
127 72 : return s;
128 : }
129 :
130 : /** Format a bitmap as a list
131 :
132 : uword * bitmap;
133 : s = format ("%U", format_bitmap_list, bitmap);
134 :
135 : Standard format_function_t arguments
136 :
137 : @param s - string under construction
138 : @param args - varargs list comprising a single uword *
139 : @returns string under construction
140 : */
141 :
142 : __clib_export u8 *
143 6 : format_bitmap_list (u8 *s, va_list *args)
144 : {
145 6 : uword *bitmap = va_arg (*args, uword *);
146 : uword fs, fc;
147 :
148 6 : if (!bitmap)
149 0 : return s;
150 :
151 6 : fs = clib_bitmap_first_set (bitmap);
152 6 : if (fs == ~0)
153 0 : return s;
154 :
155 : while (1)
156 : {
157 6 : fc = clib_bitmap_next_clear (bitmap, fs + 1);
158 6 : if (fc > fs + 1)
159 0 : s = format (s, "%lu-%lu", fs, fc - 1);
160 : else
161 6 : s = format (s, "%lu", fs);
162 :
163 6 : if ((fs = clib_bitmap_next_set (bitmap, fc)) == ~0)
164 6 : return s;
165 0 : s = format (s, ", ");
166 : }
167 : }
|