Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2025 Intel Corporation
3 : : */
4 : :
5 : : #ifndef _COMMON_INTEL_FLOW_UTIL_H_
6 : : #define _COMMON_INTEL_FLOW_UTIL_H_
7 : :
8 : : #include <stdint.h>
9 : : #include <stdbool.h>
10 : : #include <string.h>
11 : :
12 : : #include <rte_debug.h>
13 : : #include <rte_flow.h>
14 : :
15 : : /*
16 : : * Utility functions primarily intended for flow parsers.
17 : : */
18 : :
19 : : /**
20 : : * Get the effective mask of a flow item.
21 : : *
22 : : * @param item
23 : : * Flow item.
24 : : * @return
25 : : * - NULL if spec is NULL
26 : : * - item->mask if mask is not NULL
27 : : * - default mask for the item type if mask is NULL
28 : : * - NULL if the item type has no default mask
29 : : */
30 : : static inline const void *
31 : 0 : ci_flow_item_mask(const struct rte_flow_item *item)
32 : : {
33 : : const void *mask;
34 : :
35 [ # # ]: 0 : if (item->spec == NULL)
36 : : return NULL;
37 [ # # ]: 0 : if (item->mask != NULL)
38 : : return item->mask;
39 [ # # ]: 0 : if (rte_flow_conv(RTE_FLOW_CONV_OP_ITEM_DEFAULT_MASK_PTR, &mask, sizeof(mask),
40 : 0 : (const void *)(uintptr_t)item->type, NULL) < 0)
41 : : return NULL;
42 : 0 : return mask;
43 : : }
44 : :
45 : : /*
46 : : * Simplistic usage counter. Not thread-safe, not atomic.
47 : : */
48 : : typedef uint32_t ci_usage_count_t;
49 : :
50 : : /**
51 : : * Claim a usage. Returns true if this is the first usage.
52 : : *
53 : : * @param counter
54 : : * Usage counter to claim.
55 : : * @return
56 : : * true if this is the first usage, false otherwise.
57 : : */
58 : : static inline bool
59 : : ci_usage_count_claim(ci_usage_count_t *counter)
60 : : {
61 : : #ifdef CI_DEBUG
62 : : if (*counter == UINT32_MAX)
63 : : rte_panic("ci usage counter overflow\n");
64 : : #endif
65 [ # # # # ]: 0 : return (*counter)++ == 0;
66 : : }
67 : :
68 : : /**
69 : : * Release a usage. Returns true if this was the last usage.
70 : : *
71 : : * @param counter
72 : : * Usage counter to release.
73 : : * @return
74 : : * true if this was the last usage, false otherwise.
75 : : */
76 : : static inline bool
77 : : ci_usage_count_release(ci_usage_count_t *counter)
78 : : {
79 : : #ifdef CI_DEBUG
80 : : if (*counter == 0)
81 : : rte_panic("ci usage counter underflow\n");
82 : : #endif
83 [ # # # # : 0 : return --(*counter) == 0;
# # # # ]
84 : : }
85 : :
86 : : /**
87 : : * Test whether the currently claimed usage is the last one.
88 : : *
89 : : * @param counter
90 : : * Usage counter to test.
91 : : * @return
92 : : * true if the current usage is the last one, false otherwise.
93 : : */
94 : : static inline bool
95 : : ci_usage_count_is_last(const ci_usage_count_t *counter)
96 : : {
97 [ # # # # : 0 : return *counter == 1;
# # # # #
# # # # #
# # ]
98 : : }
99 : :
100 : : /**
101 : : * Check if memory region is filled with a specific byte value.
102 : : *
103 : : * @param ptr
104 : : * Pointer to memory region.
105 : : * @param len
106 : : * Length in bytes.
107 : : * @param val
108 : : * Byte value to check (e.g. 0x00 or 0xFF).
109 : : * @return
110 : : * true if all bytes equal val, false otherwise.
111 : : */
112 : : static inline bool
113 : : ci_is_all_byte(const void *ptr, size_t len, uint8_t val)
114 : : {
115 : : const uint8_t *bytes = (const uint8_t *)ptr;
116 : 0 : const uint32_t pattern32 = 0x01010101U * val;
117 : : size_t i = 0;
118 : :
119 : : /* Process 4-byte chunks using memcpy */
120 [ # # # # : 0 : for (; i + 4 <= len; i += 4) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
121 : : uint32_t chunk;
122 : 0 : memcpy(&chunk, bytes + i, 4);
123 [ # # # # : 0 : if (chunk != pattern32)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
124 : : return false;
125 : : }
126 : :
127 : : /* Process remaining bytes */
128 [ # # # # : 0 : for (; i < len; i++) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
129 [ # # # # : 0 : if (bytes[i] != val)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
130 : : return false;
131 : : }
132 : :
133 : : return true;
134 : : }
135 : :
136 : : /**
137 : : * Check if bytes are all 0x00 OR all 0xFF.
138 : : *
139 : : * @param ptr
140 : : * Pointer to memory region.
141 : : * @param len
142 : : * Length in bytes.
143 : : * @return
144 : : * true if all bytes are 0x00 OR all bytes are 0xFF, false otherwise.
145 : : */
146 : : static inline bool
147 : 0 : ci_is_all_zero_or_masked(const void *ptr, size_t len)
148 : : {
149 : : const uint8_t *bytes = (const uint8_t *)ptr;
150 : : uint8_t first_val;
151 : :
152 : : /* zero length cannot be valid */
153 [ # # ]: 0 : if (len == 0)
154 : : return false;
155 : :
156 : 0 : first_val = bytes[0];
157 : :
158 [ # # ]: 0 : if (first_val != 0x00 && first_val != 0xFF)
159 : : return false;
160 : :
161 : : return ci_is_all_byte(ptr, len, first_val);
162 : : }
163 : :
164 : : /**
165 : : * Check if a value has no bits outside the mask, and within the mask is
166 : : * either all-zero or all-one.
167 : : *
168 : : * This is intended for bitfields e.g. VLAN_TCI. For byte-aligned fields,
169 : : * use CI_FIELD_IS_ZERO_OR_MASKED below.
170 : : *
171 : : * @param value
172 : : * Data value to check.
173 : : * @param mask
174 : : * Mask to compare against.
175 : : * @return
176 : : * true if the value is either all-zero or fully masked, false otherwise.
177 : : */
178 : : static inline bool
179 : : ci_is_zero_or_masked(uint64_t value, uint64_t mask)
180 : : {
181 [ # # ]: 0 : return value == 0 || value == mask;
182 : : }
183 : :
184 : : /**
185 : : * Check if a struct field is fully masked or unmasked.
186 : : *
187 : : * @param field_ptr
188 : : * Pointer to the mask field (e.g. ð_mask->hdr.src_addr).
189 : : */
190 : : #define CI_FIELD_IS_ZERO_OR_MASKED(field_ptr) \
191 : : ci_is_all_zero_or_masked((field_ptr), sizeof(*(field_ptr)))
192 : :
193 : : /**
194 : : * Check if a struct field is all 0x00.
195 : : *
196 : : * @param field_ptr
197 : : * Pointer to the mask field (e.g. ð_mask->hdr.src_addr).
198 : : */
199 : : #define CI_FIELD_IS_ZERO(field_ptr) \
200 : : ci_is_all_byte((field_ptr), sizeof(*(field_ptr)), 0x00)
201 : :
202 : : /**
203 : : * Check if a struct field is all 0xFF.
204 : : *
205 : : * @param field_ptr
206 : : * Pointer to the mask field (e.g. ð_mask->hdr.src_addr).
207 : : */
208 : : #define CI_FIELD_IS_MASKED(field_ptr) \
209 : : ci_is_all_byte((field_ptr), sizeof(*(field_ptr)), 0xFF)
210 : :
211 : : /**
212 : : * Convert 24-bit big-endian value to host byte order.
213 : : *
214 : : * Used to extract 24-bit big-endian values (e.g. VXLAN VNI).
215 : : *
216 : : * @param val
217 : : * Pointer to 3-byte big-endian value.
218 : : * @return
219 : : * Value in host byte order.
220 : : */
221 : : static inline uint32_t
222 : : ci_be24_to_cpu(const uint8_t val[3])
223 : : {
224 : 0 : return (val[0] << 16) | (val[1] << 8) | val[2];
225 : : }
226 : :
227 : : /**
228 : : * Check if a character is a valid hexadecimal digit.
229 : : *
230 : : * @param c
231 : : * Character to check.
232 : : * @return
233 : : * true if c is in [0-9a-fA-F], false otherwise.
234 : : */
235 : : static inline bool
236 : : ci_is_hex_char(unsigned char c)
237 : : {
238 : : return ((c >= '0' && c <= '9') ||
239 : : (c >= 'a' && c <= 'f') ||
240 : : (c >= 'A' && c <= 'F'));
241 : : }
242 : :
243 : : /**
244 : : * Convert hex character to 4-bit value.
245 : : *
246 : : * @param c
247 : : * Hex character ('0'-'9', 'a'-'f', 'A'-'F').
248 : : * @return
249 : : * Value 0-15, or 0 if invalid.
250 : : */
251 : : static inline unsigned char
252 : : ci_hex_char_to_nibble(unsigned char c)
253 : : {
254 : : if (c >= '0' && c <= '9')
255 : : return c - '0';
256 : : if (c >= 'a' && c <= 'f')
257 : : return c - 'a' + 10;
258 : : if (c >= 'A' && c <= 'F')
259 : : return c - 'A' + 10;
260 : : return 0;
261 : : }
262 : :
263 : : #endif /* _COMMON_INTEL_FLOW_UTIL_H_ */
|