Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2018-2021 HiSilicon Limited.
3 : : */
4 : :
5 : : #include <rte_flow_driver.h>
6 : : #include <rte_io.h>
7 : : #include <rte_malloc.h>
8 : :
9 : : #include "hns3_ethdev.h"
10 : : #include "hns3_logs.h"
11 : : #include "hns3_flow.h"
12 : :
13 : : #define NEXT_ITEM_OF_ACTION(act, actions, index) \
14 : : do { \
15 : : (act) = (actions) + (index); \
16 : : while ((act)->type == RTE_FLOW_ACTION_TYPE_VOID) { \
17 : : (index)++; \
18 : : (act) = (actions) + (index); \
19 : : } \
20 : : } while (0)
21 : :
22 : : #define NEXT_ITEM_OF_PATTERN(item, pattern, index) \
23 : : do { \
24 : : (item) = (pattern) + (index); \
25 : : while ((item)->type == RTE_FLOW_ITEM_TYPE_VOID) { \
26 : : (index)++; \
27 : : (item) = (pattern) + (index); \
28 : : } \
29 : : } while (0)
30 : :
31 : : #define HNS3_HASH_HDR_ETH RTE_BIT64(0)
32 : : #define HNS3_HASH_HDR_IPV4 RTE_BIT64(1)
33 : : #define HNS3_HASH_HDR_IPV6 RTE_BIT64(2)
34 : : #define HNS3_HASH_HDR_TCP RTE_BIT64(3)
35 : : #define HNS3_HASH_HDR_UDP RTE_BIT64(4)
36 : : #define HNS3_HASH_HDR_SCTP RTE_BIT64(5)
37 : :
38 : : #define HNS3_HASH_VOID_NEXT_ALLOW BIT_ULL(RTE_FLOW_ITEM_TYPE_ETH)
39 : :
40 : : #define HNS3_HASH_ETH_NEXT_ALLOW (BIT_ULL(RTE_FLOW_ITEM_TYPE_IPV4) | \
41 : : BIT_ULL(RTE_FLOW_ITEM_TYPE_IPV6))
42 : :
43 : : #define HNS3_HASH_IP_NEXT_ALLOW (BIT_ULL(RTE_FLOW_ITEM_TYPE_TCP) | \
44 : : BIT_ULL(RTE_FLOW_ITEM_TYPE_UDP) | \
45 : : BIT_ULL(RTE_FLOW_ITEM_TYPE_SCTP))
46 : :
47 : : static const uint64_t hash_pattern_next_allow_items[] = {
48 : : [RTE_FLOW_ITEM_TYPE_VOID] = HNS3_HASH_VOID_NEXT_ALLOW,
49 : : [RTE_FLOW_ITEM_TYPE_ETH] = HNS3_HASH_ETH_NEXT_ALLOW,
50 : : [RTE_FLOW_ITEM_TYPE_IPV4] = HNS3_HASH_IP_NEXT_ALLOW,
51 : : [RTE_FLOW_ITEM_TYPE_IPV6] = HNS3_HASH_IP_NEXT_ALLOW,
52 : : };
53 : :
54 : : static const uint64_t hash_pattern_item_header[] = {
55 : : [RTE_FLOW_ITEM_TYPE_ETH] = HNS3_HASH_HDR_ETH,
56 : : [RTE_FLOW_ITEM_TYPE_IPV4] = HNS3_HASH_HDR_IPV4,
57 : : [RTE_FLOW_ITEM_TYPE_IPV6] = HNS3_HASH_HDR_IPV6,
58 : : [RTE_FLOW_ITEM_TYPE_TCP] = HNS3_HASH_HDR_TCP,
59 : : [RTE_FLOW_ITEM_TYPE_UDP] = HNS3_HASH_HDR_UDP,
60 : : [RTE_FLOW_ITEM_TYPE_SCTP] = HNS3_HASH_HDR_SCTP,
61 : : };
62 : :
63 : : #define HNS3_HASH_IPV4 (HNS3_HASH_HDR_ETH | HNS3_HASH_HDR_IPV4)
64 : : #define HNS3_HASH_IPV4_TCP (HNS3_HASH_HDR_ETH | \
65 : : HNS3_HASH_HDR_IPV4 | \
66 : : HNS3_HASH_HDR_TCP)
67 : : #define HNS3_HASH_IPV4_UDP (HNS3_HASH_HDR_ETH | \
68 : : HNS3_HASH_HDR_IPV4 | \
69 : : HNS3_HASH_HDR_UDP)
70 : : #define HNS3_HASH_IPV4_SCTP (HNS3_HASH_HDR_ETH | \
71 : : HNS3_HASH_HDR_IPV4 | \
72 : : HNS3_HASH_HDR_SCTP)
73 : : #define HNS3_HASH_IPV6 (HNS3_HASH_HDR_ETH | HNS3_HASH_HDR_IPV6)
74 : : #define HNS3_HASH_IPV6_TCP (HNS3_HASH_HDR_ETH | \
75 : : HNS3_HASH_HDR_IPV6 | \
76 : : HNS3_HASH_HDR_TCP)
77 : : #define HNS3_HASH_IPV6_UDP (HNS3_HASH_HDR_ETH | \
78 : : HNS3_HASH_HDR_IPV6 | \
79 : : HNS3_HASH_HDR_UDP)
80 : : #define HNS3_HASH_IPV6_SCTP (HNS3_HASH_HDR_ETH | \
81 : : HNS3_HASH_HDR_IPV6 | \
82 : : HNS3_HASH_HDR_SCTP)
83 : :
84 : : static const struct hns3_hash_map_info {
85 : : /* flow type specified, zero means action works for all flow types. */
86 : : uint64_t pattern_type;
87 : : uint64_t rss_pctype; /* packet type with prefix RTE_ETH_RSS_xxx */
88 : : uint64_t l3l4_types; /* Supported L3/L4 RSS types for this packet type */
89 : : uint64_t hw_pctype; /* packet type in driver */
90 : : uint64_t tuple_mask; /* full tuples of the hw_pctype */
91 : : } hash_map_table[] = {
92 : : /* IPV4 */
93 : : { HNS3_HASH_IPV4,
94 : : RTE_ETH_RSS_IPV4, HNS3_RSS_SUPPORT_L3_SRC_DST,
95 : : HNS3_RSS_PCTYPE_IPV4_NONF, HNS3_RSS_TUPLE_IPV4_NONF_M },
96 : : { HNS3_HASH_IPV4,
97 : : RTE_ETH_RSS_NONFRAG_IPV4_OTHER, HNS3_RSS_SUPPORT_L3_SRC_DST,
98 : : HNS3_RSS_PCTYPE_IPV4_NONF, HNS3_RSS_TUPLE_IPV4_NONF_M },
99 : : { HNS3_HASH_IPV4,
100 : : RTE_ETH_RSS_FRAG_IPV4, HNS3_RSS_SUPPORT_L3_SRC_DST,
101 : : HNS3_RSS_PCTYPE_IPV4_FLAG, HNS3_RSS_TUPLE_IPV4_FLAG_M },
102 : : { HNS3_HASH_IPV4_TCP,
103 : : RTE_ETH_RSS_NONFRAG_IPV4_TCP, HNS3_RSS_SUPPORT_L3L4,
104 : : HNS3_RSS_PCTYPE_IPV4_TCP, HNS3_RSS_TUPLE_IPV4_TCP_M },
105 : : { HNS3_HASH_IPV4_UDP,
106 : : RTE_ETH_RSS_NONFRAG_IPV4_UDP, HNS3_RSS_SUPPORT_L3L4,
107 : : HNS3_RSS_PCTYPE_IPV4_UDP, HNS3_RSS_TUPLE_IPV4_UDP_M },
108 : : { HNS3_HASH_IPV4_SCTP,
109 : : RTE_ETH_RSS_NONFRAG_IPV4_SCTP, HNS3_RSS_SUPPORT_L3L4,
110 : : HNS3_RSS_PCTYPE_IPV4_SCTP, HNS3_RSS_TUPLE_IPV4_SCTP_M },
111 : : /* IPV6 */
112 : : { HNS3_HASH_IPV6,
113 : : RTE_ETH_RSS_IPV6, HNS3_RSS_SUPPORT_L3_SRC_DST,
114 : : HNS3_RSS_PCTYPE_IPV6_NONF, HNS3_RSS_TUPLE_IPV6_NONF_M },
115 : : { HNS3_HASH_IPV6,
116 : : RTE_ETH_RSS_NONFRAG_IPV6_OTHER, HNS3_RSS_SUPPORT_L3_SRC_DST,
117 : : HNS3_RSS_PCTYPE_IPV6_NONF, HNS3_RSS_TUPLE_IPV6_NONF_M },
118 : : { HNS3_HASH_IPV6,
119 : : RTE_ETH_RSS_FRAG_IPV6, HNS3_RSS_SUPPORT_L3_SRC_DST,
120 : : HNS3_RSS_PCTYPE_IPV6_FLAG, HNS3_RSS_TUPLE_IPV6_FLAG_M },
121 : : { HNS3_HASH_IPV6_TCP,
122 : : RTE_ETH_RSS_NONFRAG_IPV6_TCP, HNS3_RSS_SUPPORT_L3L4,
123 : : HNS3_RSS_PCTYPE_IPV6_TCP, HNS3_RSS_TUPLE_IPV6_TCP_M },
124 : : { HNS3_HASH_IPV6_UDP,
125 : : RTE_ETH_RSS_NONFRAG_IPV6_UDP, HNS3_RSS_SUPPORT_L3L4,
126 : : HNS3_RSS_PCTYPE_IPV6_UDP, HNS3_RSS_TUPLE_IPV6_UDP_M },
127 : : { HNS3_HASH_IPV6_SCTP,
128 : : RTE_ETH_RSS_NONFRAG_IPV6_SCTP, HNS3_RSS_SUPPORT_L3L4,
129 : : HNS3_RSS_PCTYPE_IPV6_SCTP, HNS3_RSS_TUPLE_IPV6_SCTP_M },
130 : : };
131 : :
132 : : static const uint8_t full_mask[VNI_OR_TNI_LEN] = { 0xFF, 0xFF, 0xFF };
133 : : static const uint8_t zero_mask[VNI_OR_TNI_LEN] = { 0x00, 0x00, 0x00 };
134 : :
135 : : /* Special Filter id for non-specific packet flagging. Don't change value */
136 : : #define HNS3_MAX_FILTER_ID 0x0FFF
137 : :
138 : : #define ETHER_TYPE_MASK 0xFFFF
139 : : #define IPPROTO_MASK 0xFF
140 : : #define TUNNEL_TYPE_MASK 0xFFFF
141 : :
142 : : #define HNS3_TUNNEL_TYPE_VXLAN 0x12B5
143 : : #define HNS3_TUNNEL_TYPE_VXLAN_GPE 0x12B6
144 : : #define HNS3_TUNNEL_TYPE_GENEVE 0x17C1
145 : : #define HNS3_TUNNEL_TYPE_NVGRE 0x6558
146 : :
147 : : static enum rte_flow_item_type first_items[] = {
148 : : RTE_FLOW_ITEM_TYPE_ETH,
149 : : RTE_FLOW_ITEM_TYPE_IPV4,
150 : : RTE_FLOW_ITEM_TYPE_IPV6,
151 : : RTE_FLOW_ITEM_TYPE_TCP,
152 : : RTE_FLOW_ITEM_TYPE_UDP,
153 : : RTE_FLOW_ITEM_TYPE_SCTP,
154 : : RTE_FLOW_ITEM_TYPE_ICMP,
155 : : RTE_FLOW_ITEM_TYPE_NVGRE,
156 : : RTE_FLOW_ITEM_TYPE_VXLAN,
157 : : RTE_FLOW_ITEM_TYPE_GENEVE,
158 : : RTE_FLOW_ITEM_TYPE_VXLAN_GPE
159 : : };
160 : :
161 : : static enum rte_flow_item_type L2_next_items[] = {
162 : : RTE_FLOW_ITEM_TYPE_VLAN,
163 : : RTE_FLOW_ITEM_TYPE_IPV4,
164 : : RTE_FLOW_ITEM_TYPE_IPV6
165 : : };
166 : :
167 : : static enum rte_flow_item_type L3_next_items[] = {
168 : : RTE_FLOW_ITEM_TYPE_TCP,
169 : : RTE_FLOW_ITEM_TYPE_UDP,
170 : : RTE_FLOW_ITEM_TYPE_SCTP,
171 : : RTE_FLOW_ITEM_TYPE_NVGRE,
172 : : RTE_FLOW_ITEM_TYPE_ICMP
173 : : };
174 : :
175 : : static enum rte_flow_item_type L4_next_items[] = {
176 : : RTE_FLOW_ITEM_TYPE_VXLAN,
177 : : RTE_FLOW_ITEM_TYPE_GENEVE,
178 : : RTE_FLOW_ITEM_TYPE_VXLAN_GPE
179 : : };
180 : :
181 : : static enum rte_flow_item_type tunnel_next_items[] = {
182 : : RTE_FLOW_ITEM_TYPE_ETH,
183 : : RTE_FLOW_ITEM_TYPE_VLAN
184 : : };
185 : :
186 : : struct items_step_mngr {
187 : : enum rte_flow_item_type *items;
188 : : size_t count;
189 : : };
190 : :
191 : : static inline void
192 : : net_addr_to_host(uint32_t *dst, const rte_be32_t *src, size_t len)
193 : : {
194 : : size_t i;
195 : :
196 [ # # # # : 0 : for (i = 0; i < len; i++)
# # # # ]
197 [ # # # # : 0 : dst[i] = rte_be_to_cpu_32(src[i]);
# # # # ]
198 : : }
199 : :
200 : : /*
201 : : * This function is used to parse filter type.
202 : : * 1. As we know RSS is used to spread packets among several queues, the flow
203 : : * API provide the struct rte_flow_action_rss, user could config its field
204 : : * sush as: func/level/types/key/queue to control RSS function.
205 : : * 2. The flow API also supports queue region configuration for hns3. It was
206 : : * implemented by FDIR + RSS in hns3 hardware, user can create one FDIR rule
207 : : * which action is RSS queues region.
208 : : * 3. When action is RSS, we use the following rule to distinguish:
209 : : * Case 1: pattern has ETH and all fields in RSS action except 'queues' are
210 : : * zero or default, indicate it is queue region configuration.
211 : : * Case other: an rss general action.
212 : : */
213 : : static void
214 : 0 : hns3_parse_filter_type(const struct rte_flow_item pattern[],
215 : : const struct rte_flow_action actions[],
216 : : struct hns3_filter_info *filter_info)
217 : : {
218 : : const struct rte_flow_action_rss *rss_act;
219 : : const struct rte_flow_action *act = NULL;
220 : : bool only_has_queues = false;
221 : : bool have_eth = false;
222 : :
223 [ # # ]: 0 : for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
224 [ # # ]: 0 : if (actions->type == RTE_FLOW_ACTION_TYPE_RSS) {
225 : : act = actions;
226 : : break;
227 : : }
228 : : }
229 [ # # ]: 0 : if (act == NULL) {
230 : 0 : filter_info->type = RTE_ETH_FILTER_FDIR;
231 : 0 : return;
232 : : }
233 : :
234 [ # # ]: 0 : for (; pattern->type != RTE_FLOW_ITEM_TYPE_END; pattern++) {
235 [ # # ]: 0 : if (pattern->type == RTE_FLOW_ITEM_TYPE_ETH) {
236 : : have_eth = true;
237 : : break;
238 : : }
239 : : }
240 : :
241 : 0 : rss_act = act->conf;
242 [ # # ]: 0 : only_has_queues = (rss_act->queue_num > 0) &&
243 [ # # ]: 0 : (rss_act->func == RTE_ETH_HASH_FUNCTION_DEFAULT &&
244 [ # # # # ]: 0 : rss_act->types == 0 && rss_act->key_len == 0);
245 [ # # ]: 0 : if (have_eth && only_has_queues) {
246 : : /*
247 : : * Pattern has ETH and all fields in RSS action except 'queues'
248 : : * are zero or default, which indicates this is queue region
249 : : * configuration.
250 : : */
251 : 0 : filter_info->type = RTE_ETH_FILTER_FDIR;
252 : 0 : return;
253 : : }
254 : :
255 : 0 : filter_info->type = RTE_ETH_FILTER_HASH;
256 : : }
257 : :
258 : : static inline struct hns3_flow_counter *
259 : : hns3_counter_lookup(struct rte_eth_dev *dev, uint32_t id)
260 : : {
261 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
262 : : struct hns3_pf *pf = &hns->pf;
263 : : struct hns3_flow_counter *cnt;
264 : :
265 [ # # # # : 0 : LIST_FOREACH(cnt, &pf->flow_counters, next) {
# # # # #
# # # ]
266 [ # # # # : 0 : if (cnt->id == id)
# # # # #
# # # ]
267 : : return cnt;
268 : : }
269 : : return NULL;
270 : : }
271 : :
272 : : static int
273 : 0 : hns3_counter_new(struct rte_eth_dev *dev, uint32_t indirect, uint32_t id,
274 : : struct rte_flow_error *error)
275 : : {
276 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
277 : : struct hns3_pf *pf = &hns->pf;
278 : 0 : struct hns3_hw *hw = &hns->hw;
279 : : struct hns3_flow_counter *cnt;
280 : : uint64_t value;
281 : : int ret;
282 : :
283 : : cnt = hns3_counter_lookup(dev, id);
284 [ # # ]: 0 : if (cnt) {
285 [ # # # # ]: 0 : if (!cnt->indirect || cnt->indirect != indirect)
286 : 0 : return rte_flow_error_set(error, ENOTSUP,
287 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
288 : : cnt,
289 : : "Counter id is used, indirect flag not match");
290 : : /* Clear the indirect counter on first use. */
291 [ # # ]: 0 : if (cnt->indirect && cnt->ref_cnt == 1)
292 : 0 : (void)hns3_fd_get_count(hw, id, &value);
293 : 0 : cnt->ref_cnt++;
294 : 0 : return 0;
295 : : }
296 : :
297 : : /* Clear the counter by read ops because the counter is read-clear */
298 : 0 : ret = hns3_fd_get_count(hw, id, &value);
299 [ # # ]: 0 : if (ret)
300 : 0 : return rte_flow_error_set(error, EIO,
301 : : RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
302 : : "Clear counter failed!");
303 : :
304 : 0 : cnt = rte_zmalloc("hns3 counter", sizeof(*cnt), 0);
305 [ # # ]: 0 : if (cnt == NULL)
306 : 0 : return rte_flow_error_set(error, ENOMEM,
307 : : RTE_FLOW_ERROR_TYPE_HANDLE, cnt,
308 : : "Alloc mem for counter failed");
309 : 0 : cnt->id = id;
310 : 0 : cnt->indirect = indirect;
311 : 0 : cnt->ref_cnt = 1;
312 : 0 : cnt->hits = 0;
313 [ # # ]: 0 : LIST_INSERT_HEAD(&pf->flow_counters, cnt, next);
314 : 0 : return 0;
315 : : }
316 : :
317 : : static int
318 : 0 : hns3_counter_query(struct rte_eth_dev *dev, struct rte_flow *flow,
319 : : struct rte_flow_query_count *qc,
320 : : struct rte_flow_error *error)
321 : : {
322 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
323 : : struct hns3_flow_counter *cnt;
324 : : uint64_t value;
325 : : int ret;
326 : :
327 : : /* FDIR is available only in PF driver */
328 [ # # ]: 0 : if (hns->is_vf)
329 : 0 : return rte_flow_error_set(error, ENOTSUP,
330 : : RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
331 : : "Fdir is not supported in VF");
332 : 0 : cnt = hns3_counter_lookup(dev, flow->counter_id);
333 [ # # ]: 0 : if (cnt == NULL)
334 : 0 : return rte_flow_error_set(error, EINVAL,
335 : : RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
336 : : "Can't find counter id");
337 : :
338 : 0 : ret = hns3_fd_get_count(&hns->hw, flow->counter_id, &value);
339 [ # # ]: 0 : if (ret) {
340 : 0 : rte_flow_error_set(error, -ret, RTE_FLOW_ERROR_TYPE_HANDLE,
341 : : NULL, "Read counter fail.");
342 : 0 : return ret;
343 : : }
344 : 0 : qc->hits_set = 1;
345 : 0 : qc->hits = value;
346 : 0 : qc->bytes_set = 0;
347 : 0 : qc->bytes = 0;
348 : :
349 : 0 : return 0;
350 : : }
351 : :
352 : : static int
353 : 0 : hns3_counter_release(struct rte_eth_dev *dev, uint32_t id)
354 : : {
355 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
356 : : struct hns3_hw *hw = &hns->hw;
357 : : struct hns3_flow_counter *cnt;
358 : :
359 : : cnt = hns3_counter_lookup(dev, id);
360 [ # # ]: 0 : if (cnt == NULL) {
361 : 0 : hns3_err(hw, "Can't find available counter to release");
362 : 0 : return -EINVAL;
363 : : }
364 : 0 : cnt->ref_cnt--;
365 [ # # ]: 0 : if (cnt->ref_cnt == 0) {
366 [ # # ]: 0 : LIST_REMOVE(cnt, next);
367 : 0 : rte_free(cnt);
368 : : }
369 : : return 0;
370 : : }
371 : :
372 : : static void
373 : 0 : hns3_counter_flush(struct rte_eth_dev *dev)
374 : : {
375 : 0 : struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
376 : : LIST_HEAD(counters, hns3_flow_counter) indir_counters;
377 : : struct hns3_flow_counter *cnt_ptr;
378 : :
379 : 0 : LIST_INIT(&indir_counters);
380 : 0 : cnt_ptr = LIST_FIRST(&pf->flow_counters);
381 [ # # ]: 0 : while (cnt_ptr) {
382 [ # # ]: 0 : LIST_REMOVE(cnt_ptr, next);
383 [ # # ]: 0 : if (cnt_ptr->indirect)
384 [ # # ]: 0 : LIST_INSERT_HEAD(&indir_counters, cnt_ptr, next);
385 : : else
386 : 0 : rte_free(cnt_ptr);
387 : 0 : cnt_ptr = LIST_FIRST(&pf->flow_counters);
388 : : }
389 : :
390 : : /* Reset the indirect action and add to pf->flow_counters list. */
391 : 0 : cnt_ptr = LIST_FIRST(&indir_counters);
392 [ # # ]: 0 : while (cnt_ptr) {
393 [ # # ]: 0 : LIST_REMOVE(cnt_ptr, next);
394 : 0 : cnt_ptr->ref_cnt = 1;
395 : 0 : cnt_ptr->hits = 0;
396 [ # # ]: 0 : LIST_INSERT_HEAD(&pf->flow_counters, cnt_ptr, next);
397 : 0 : cnt_ptr = LIST_FIRST(&indir_counters);
398 : : }
399 : 0 : }
400 : :
401 : : static int
402 : 0 : hns3_handle_action_queue(struct rte_eth_dev *dev,
403 : : const struct rte_flow_action *action,
404 : : struct hns3_fdir_rule *rule,
405 : : struct rte_flow_error *error)
406 : : {
407 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
408 : : const struct rte_flow_action_queue *queue;
409 : : struct hns3_hw *hw = &hns->hw;
410 : :
411 : 0 : queue = (const struct rte_flow_action_queue *)action->conf;
412 [ # # ]: 0 : if (queue->index >= hw->data->nb_rx_queues) {
413 : 0 : hns3_err(hw, "queue ID(%u) is greater than number of available queue (%u) in driver.",
414 : : queue->index, hw->data->nb_rx_queues);
415 : 0 : return rte_flow_error_set(error, EINVAL,
416 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
417 : : action, "Invalid queue ID in PF");
418 : : }
419 : :
420 : 0 : rule->queue_id = queue->index;
421 : 0 : rule->nb_queues = 1;
422 : 0 : rule->action = HNS3_FD_ACTION_ACCEPT_PACKET;
423 : 0 : return 0;
424 : : }
425 : :
426 : : static int
427 : 0 : hns3_handle_action_queue_region(struct rte_eth_dev *dev,
428 : : const struct rte_flow_action *action,
429 : : struct hns3_fdir_rule *rule,
430 : : struct rte_flow_error *error)
431 : : {
432 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
433 : 0 : const struct rte_flow_action_rss *conf = action->conf;
434 : : struct hns3_hw *hw = &hns->hw;
435 : : uint16_t idx;
436 : :
437 [ # # ]: 0 : if (!hns3_dev_get_support(hw, FD_QUEUE_REGION))
438 : 0 : return rte_flow_error_set(error, ENOTSUP,
439 : : RTE_FLOW_ERROR_TYPE_ACTION, action,
440 : : "Not support config queue region!");
441 : :
442 [ # # ]: 0 : if ((!rte_is_power_of_2(conf->queue_num)) ||
443 [ # # ]: 0 : conf->queue_num > hw->rss_size_max ||
444 [ # # ]: 0 : conf->queue[0] >= hw->data->nb_rx_queues ||
445 [ # # ]: 0 : conf->queue[0] + conf->queue_num > hw->data->nb_rx_queues) {
446 : 0 : return rte_flow_error_set(error, EINVAL,
447 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF, action,
448 : : "Invalid start queue ID and queue num! the start queue "
449 : : "ID must valid, the queue num must be power of 2 and "
450 : : "<= rss_size_max.");
451 : : }
452 : :
453 [ # # ]: 0 : for (idx = 1; idx < conf->queue_num; idx++) {
454 [ # # ]: 0 : if (conf->queue[idx] != conf->queue[idx - 1] + 1)
455 : 0 : return rte_flow_error_set(error, EINVAL,
456 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF, action,
457 : : "Invalid queue ID sequence! the queue ID "
458 : : "must be continuous increment.");
459 : : }
460 : :
461 : 0 : rule->queue_id = conf->queue[0];
462 : 0 : rule->nb_queues = conf->queue_num;
463 : 0 : rule->action = HNS3_FD_ACTION_ACCEPT_PACKET;
464 : 0 : return 0;
465 : : }
466 : :
467 : : static int
468 : 0 : hns3_handle_action_indirect(struct rte_eth_dev *dev,
469 : : const struct rte_flow_action *action,
470 : : struct hns3_fdir_rule *rule,
471 : : struct rte_flow_error *error)
472 : : {
473 : 0 : const struct rte_flow_action_handle *indir = action->conf;
474 : :
475 [ # # ]: 0 : if (indir->indirect_type != HNS3_INDIRECT_ACTION_TYPE_COUNT)
476 : 0 : return rte_flow_error_set(error, EINVAL,
477 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
478 : : action, "Invalid indirect type");
479 : :
480 [ # # ]: 0 : if (hns3_counter_lookup(dev, indir->counter_id) == NULL)
481 : 0 : return rte_flow_error_set(error, EINVAL,
482 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
483 : : action, "Counter id not exist");
484 : :
485 : 0 : rule->act_cnt.id = indir->counter_id;
486 : 0 : rule->flags |= (HNS3_RULE_FLAG_COUNTER | HNS3_RULE_FLAG_COUNTER_INDIR);
487 : :
488 : 0 : return 0;
489 : : }
490 : :
491 : : /*
492 : : * Parse actions structure from the provided pattern.
493 : : * The pattern is validated as the items are copied.
494 : : *
495 : : * @param actions[in]
496 : : * @param rule[out]
497 : : * NIC specific actions derived from the actions.
498 : : * @param error[out]
499 : : */
500 : : static int
501 : 0 : hns3_handle_actions(struct rte_eth_dev *dev,
502 : : const struct rte_flow_action actions[],
503 : : struct hns3_fdir_rule *rule, struct rte_flow_error *error)
504 : : {
505 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
506 : : const struct rte_flow_action_count *act_count;
507 : : const struct rte_flow_action_mark *mark;
508 : : struct hns3_pf *pf = &hns->pf;
509 : : uint32_t counter_num;
510 : : int ret;
511 : :
512 [ # # ]: 0 : for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
513 [ # # # # : 0 : switch (actions->type) {
# # # #
# ]
514 : 0 : case RTE_FLOW_ACTION_TYPE_QUEUE:
515 : 0 : ret = hns3_handle_action_queue(dev, actions, rule,
516 : : error);
517 [ # # ]: 0 : if (ret)
518 : 0 : return ret;
519 : : break;
520 : 0 : case RTE_FLOW_ACTION_TYPE_DROP:
521 : 0 : rule->action = HNS3_FD_ACTION_DROP_PACKET;
522 : 0 : break;
523 : : /*
524 : : * Here RSS's real action is queue region.
525 : : * Queue region is implemented by FDIR + RSS in hns3 hardware,
526 : : * the FDIR's action is one queue region (start_queue_id and
527 : : * queue_num), then RSS spread packets to the queue region by
528 : : * RSS algorithm.
529 : : */
530 : 0 : case RTE_FLOW_ACTION_TYPE_RSS:
531 : 0 : ret = hns3_handle_action_queue_region(dev, actions,
532 : : rule, error);
533 [ # # ]: 0 : if (ret)
534 : 0 : return ret;
535 : : break;
536 : 0 : case RTE_FLOW_ACTION_TYPE_MARK:
537 : 0 : mark =
538 : : (const struct rte_flow_action_mark *)actions->conf;
539 [ # # ]: 0 : if (mark->id >= HNS3_MAX_FILTER_ID)
540 : 0 : return rte_flow_error_set(error, EINVAL,
541 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
542 : : actions,
543 : : "Invalid Mark ID");
544 : 0 : rule->fd_id = mark->id;
545 : 0 : rule->flags |= HNS3_RULE_FLAG_FDID;
546 : 0 : break;
547 : 0 : case RTE_FLOW_ACTION_TYPE_FLAG:
548 : 0 : rule->fd_id = HNS3_MAX_FILTER_ID;
549 : 0 : rule->flags |= HNS3_RULE_FLAG_FDID;
550 : 0 : break;
551 : 0 : case RTE_FLOW_ACTION_TYPE_COUNT:
552 : 0 : act_count =
553 : : (const struct rte_flow_action_count *)actions->conf;
554 : 0 : counter_num = pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_1];
555 [ # # ]: 0 : if (act_count->id >= counter_num)
556 : 0 : return rte_flow_error_set(error, EINVAL,
557 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
558 : : actions,
559 : : "Invalid counter id");
560 : 0 : rule->act_cnt = *act_count;
561 : 0 : rule->flags |= HNS3_RULE_FLAG_COUNTER;
562 : 0 : rule->flags &= ~HNS3_RULE_FLAG_COUNTER_INDIR;
563 : 0 : break;
564 : 0 : case RTE_FLOW_ACTION_TYPE_INDIRECT:
565 : 0 : ret = hns3_handle_action_indirect(dev, actions, rule,
566 : : error);
567 [ # # ]: 0 : if (ret)
568 : 0 : return ret;
569 : : break;
570 : : case RTE_FLOW_ACTION_TYPE_VOID:
571 : : break;
572 : 0 : default:
573 : 0 : return rte_flow_error_set(error, ENOTSUP,
574 : : RTE_FLOW_ERROR_TYPE_ACTION,
575 : : NULL, "Unsupported action");
576 : : }
577 : : }
578 : :
579 : : return 0;
580 : : }
581 : :
582 : : static int
583 : 0 : hns3_check_attr(const struct rte_flow_attr *attr, struct rte_flow_error *error)
584 : : {
585 [ # # ]: 0 : if (!attr->ingress)
586 : 0 : return rte_flow_error_set(error, EINVAL,
587 : : RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
588 : : attr, "Ingress can't be zero");
589 [ # # ]: 0 : if (attr->egress)
590 : 0 : return rte_flow_error_set(error, ENOTSUP,
591 : : RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
592 : : attr, "Not support egress");
593 [ # # ]: 0 : if (attr->transfer)
594 : 0 : return rte_flow_error_set(error, ENOTSUP,
595 : : RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
596 : : attr, "No support for transfer");
597 [ # # ]: 0 : if (attr->priority)
598 : 0 : return rte_flow_error_set(error, ENOTSUP,
599 : : RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
600 : : attr, "Not support priority");
601 [ # # ]: 0 : if (attr->group)
602 : 0 : return rte_flow_error_set(error, ENOTSUP,
603 : : RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
604 : : attr, "Not support group");
605 : : return 0;
606 : : }
607 : :
608 : : static int
609 : 0 : hns3_parse_eth(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
610 : : struct rte_flow_error *error __rte_unused)
611 : : {
612 : : const struct rte_flow_item_eth *eth_spec;
613 : : const struct rte_flow_item_eth *eth_mask;
614 : :
615 : : /* Only used to describe the protocol stack. */
616 [ # # # # ]: 0 : if (item->spec == NULL && item->mask == NULL)
617 : : return 0;
618 : :
619 [ # # ]: 0 : if (item->mask) {
620 : : eth_mask = item->mask;
621 [ # # ]: 0 : if (eth_mask->hdr.ether_type) {
622 : 0 : hns3_set_bit(rule->input_set, INNER_ETH_TYPE, 1);
623 : 0 : rule->key_conf.mask.ether_type =
624 [ # # ]: 0 : rte_be_to_cpu_16(eth_mask->hdr.ether_type);
625 : : }
626 [ # # ]: 0 : if (!rte_is_zero_ether_addr(ð_mask->hdr.src_addr)) {
627 : 0 : hns3_set_bit(rule->input_set, INNER_SRC_MAC, 1);
628 : 0 : memcpy(rule->key_conf.mask.src_mac,
629 : 0 : eth_mask->hdr.src_addr.addr_bytes, RTE_ETHER_ADDR_LEN);
630 : : }
631 [ # # ]: 0 : if (!rte_is_zero_ether_addr(ð_mask->hdr.dst_addr)) {
632 : 0 : hns3_set_bit(rule->input_set, INNER_DST_MAC, 1);
633 : 0 : memcpy(rule->key_conf.mask.dst_mac,
634 : 0 : eth_mask->hdr.dst_addr.addr_bytes, RTE_ETHER_ADDR_LEN);
635 : : }
636 : : }
637 : :
638 : 0 : eth_spec = item->spec;
639 [ # # ]: 0 : rule->key_conf.spec.ether_type = rte_be_to_cpu_16(eth_spec->hdr.ether_type);
640 : 0 : memcpy(rule->key_conf.spec.src_mac, eth_spec->hdr.src_addr.addr_bytes,
641 : : RTE_ETHER_ADDR_LEN);
642 : 0 : memcpy(rule->key_conf.spec.dst_mac, eth_spec->hdr.dst_addr.addr_bytes,
643 : : RTE_ETHER_ADDR_LEN);
644 : 0 : return 0;
645 : : }
646 : :
647 : : static int
648 : 0 : hns3_parse_vlan(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
649 : : struct rte_flow_error *error)
650 : : {
651 : : const struct rte_flow_item_vlan *vlan_spec;
652 : : const struct rte_flow_item_vlan *vlan_mask;
653 : :
654 : 0 : rule->key_conf.vlan_num++;
655 [ # # ]: 0 : if (rule->key_conf.vlan_num > VLAN_TAG_NUM_MAX)
656 : 0 : return rte_flow_error_set(error, EINVAL,
657 : : RTE_FLOW_ERROR_TYPE_ITEM, item,
658 : : "Vlan_num is more than 2");
659 : :
660 : : /* Only used to describe the protocol stack. */
661 [ # # # # ]: 0 : if (item->spec == NULL && item->mask == NULL)
662 : : return 0;
663 : :
664 [ # # ]: 0 : if (item->mask) {
665 : : vlan_mask = item->mask;
666 [ # # ]: 0 : if (vlan_mask->hdr.vlan_tci) {
667 [ # # ]: 0 : if (rule->key_conf.vlan_num == 1) {
668 : 0 : hns3_set_bit(rule->input_set, INNER_VLAN_TAG1,
669 : : 1);
670 : 0 : rule->key_conf.mask.vlan_tag1 =
671 [ # # ]: 0 : rte_be_to_cpu_16(vlan_mask->hdr.vlan_tci);
672 : : } else {
673 : 0 : hns3_set_bit(rule->input_set, INNER_VLAN_TAG2,
674 : : 1);
675 : 0 : rule->key_conf.mask.vlan_tag2 =
676 [ # # ]: 0 : rte_be_to_cpu_16(vlan_mask->hdr.vlan_tci);
677 : : }
678 : : }
679 : : }
680 : :
681 : : vlan_spec = item->spec;
682 [ # # ]: 0 : if (rule->key_conf.vlan_num == 1)
683 : 0 : rule->key_conf.spec.vlan_tag1 =
684 [ # # ]: 0 : rte_be_to_cpu_16(vlan_spec->hdr.vlan_tci);
685 : : else
686 : 0 : rule->key_conf.spec.vlan_tag2 =
687 [ # # ]: 0 : rte_be_to_cpu_16(vlan_spec->hdr.vlan_tci);
688 : : return 0;
689 : : }
690 : :
691 : : static bool
692 : : hns3_check_ipv4_mask_supported(const struct rte_flow_item_ipv4 *ipv4_mask)
693 : : {
694 [ # # # # ]: 0 : if (ipv4_mask->hdr.total_length || ipv4_mask->hdr.packet_id ||
695 [ # # # # ]: 0 : ipv4_mask->hdr.fragment_offset || ipv4_mask->hdr.time_to_live ||
696 [ # # ]: 0 : ipv4_mask->hdr.hdr_checksum)
697 : : return false;
698 : :
699 : : return true;
700 : : }
701 : :
702 : : static int
703 : 0 : hns3_parse_ipv4(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
704 : : struct rte_flow_error *error)
705 : : {
706 : : const struct rte_flow_item_ipv4 *ipv4_spec;
707 : : const struct rte_flow_item_ipv4 *ipv4_mask;
708 : :
709 : 0 : hns3_set_bit(rule->input_set, INNER_ETH_TYPE, 1);
710 : 0 : rule->key_conf.spec.ether_type = RTE_ETHER_TYPE_IPV4;
711 : 0 : rule->key_conf.mask.ether_type = ETHER_TYPE_MASK;
712 : :
713 : : /* Only used to describe the protocol stack. */
714 [ # # # # ]: 0 : if (item->spec == NULL && item->mask == NULL)
715 : : return 0;
716 : :
717 [ # # ]: 0 : if (item->mask) {
718 : : ipv4_mask = item->mask;
719 : : if (!hns3_check_ipv4_mask_supported(ipv4_mask)) {
720 : 0 : return rte_flow_error_set(error, EINVAL,
721 : : RTE_FLOW_ERROR_TYPE_ITEM_MASK,
722 : : item,
723 : : "Only support src & dst ip,tos,proto in IPV4");
724 : : }
725 : :
726 [ # # ]: 0 : if (ipv4_mask->hdr.src_addr) {
727 : 0 : hns3_set_bit(rule->input_set, INNER_SRC_IP, 1);
728 : 0 : rule->key_conf.mask.src_ip[IP_ADDR_KEY_ID] =
729 [ # # ]: 0 : rte_be_to_cpu_32(ipv4_mask->hdr.src_addr);
730 : : }
731 : :
732 [ # # ]: 0 : if (ipv4_mask->hdr.dst_addr) {
733 : 0 : hns3_set_bit(rule->input_set, INNER_DST_IP, 1);
734 : 0 : rule->key_conf.mask.dst_ip[IP_ADDR_KEY_ID] =
735 [ # # ]: 0 : rte_be_to_cpu_32(ipv4_mask->hdr.dst_addr);
736 : : }
737 : :
738 [ # # ]: 0 : if (ipv4_mask->hdr.type_of_service) {
739 : 0 : hns3_set_bit(rule->input_set, INNER_IP_TOS, 1);
740 : 0 : rule->key_conf.mask.ip_tos =
741 : : ipv4_mask->hdr.type_of_service;
742 : : }
743 : :
744 [ # # ]: 0 : if (ipv4_mask->hdr.next_proto_id) {
745 : 0 : hns3_set_bit(rule->input_set, INNER_IP_PROTO, 1);
746 : 0 : rule->key_conf.mask.ip_proto =
747 : : ipv4_mask->hdr.next_proto_id;
748 : : }
749 : : }
750 : :
751 : : ipv4_spec = item->spec;
752 : 0 : rule->key_conf.spec.src_ip[IP_ADDR_KEY_ID] =
753 [ # # ]: 0 : rte_be_to_cpu_32(ipv4_spec->hdr.src_addr);
754 : 0 : rule->key_conf.spec.dst_ip[IP_ADDR_KEY_ID] =
755 [ # # ]: 0 : rte_be_to_cpu_32(ipv4_spec->hdr.dst_addr);
756 : 0 : rule->key_conf.spec.ip_tos = ipv4_spec->hdr.type_of_service;
757 : 0 : rule->key_conf.spec.ip_proto = ipv4_spec->hdr.next_proto_id;
758 : 0 : return 0;
759 : : }
760 : :
761 : : static int
762 : 0 : hns3_parse_ipv6(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
763 : : struct rte_flow_error *error)
764 : : {
765 : : const struct rte_flow_item_ipv6 *ipv6_spec;
766 : : const struct rte_flow_item_ipv6 *ipv6_mask;
767 : :
768 : 0 : hns3_set_bit(rule->input_set, INNER_ETH_TYPE, 1);
769 : 0 : rule->key_conf.spec.ether_type = RTE_ETHER_TYPE_IPV6;
770 : 0 : rule->key_conf.mask.ether_type = ETHER_TYPE_MASK;
771 : :
772 : : /* Only used to describe the protocol stack. */
773 [ # # # # ]: 0 : if (item->spec == NULL && item->mask == NULL)
774 : : return 0;
775 : :
776 [ # # ]: 0 : if (item->mask) {
777 : : ipv6_mask = item->mask;
778 [ # # # # ]: 0 : if (ipv6_mask->hdr.vtc_flow || ipv6_mask->hdr.payload_len ||
779 : : ipv6_mask->hdr.hop_limits) {
780 : 0 : return rte_flow_error_set(error, EINVAL,
781 : : RTE_FLOW_ERROR_TYPE_ITEM_MASK,
782 : : item,
783 : : "Only support src & dst ip,proto in IPV6");
784 : : }
785 : 0 : net_addr_to_host(rule->key_conf.mask.src_ip,
786 : 0 : (const rte_be32_t *)ipv6_mask->hdr.src_addr,
787 : : IP_ADDR_LEN);
788 : 0 : net_addr_to_host(rule->key_conf.mask.dst_ip,
789 : 0 : (const rte_be32_t *)ipv6_mask->hdr.dst_addr,
790 : : IP_ADDR_LEN);
791 : 0 : rule->key_conf.mask.ip_proto = ipv6_mask->hdr.proto;
792 [ # # ]: 0 : if (rule->key_conf.mask.src_ip[IP_ADDR_KEY_ID])
793 : 0 : hns3_set_bit(rule->input_set, INNER_SRC_IP, 1);
794 [ # # ]: 0 : if (rule->key_conf.mask.dst_ip[IP_ADDR_KEY_ID])
795 : 0 : hns3_set_bit(rule->input_set, INNER_DST_IP, 1);
796 [ # # ]: 0 : if (ipv6_mask->hdr.proto)
797 : 0 : hns3_set_bit(rule->input_set, INNER_IP_PROTO, 1);
798 : : }
799 : :
800 : : ipv6_spec = item->spec;
801 : 0 : net_addr_to_host(rule->key_conf.spec.src_ip,
802 : 0 : (const rte_be32_t *)ipv6_spec->hdr.src_addr,
803 : : IP_ADDR_LEN);
804 : 0 : net_addr_to_host(rule->key_conf.spec.dst_ip,
805 : 0 : (const rte_be32_t *)ipv6_spec->hdr.dst_addr,
806 : : IP_ADDR_LEN);
807 : 0 : rule->key_conf.spec.ip_proto = ipv6_spec->hdr.proto;
808 : :
809 : 0 : return 0;
810 : : }
811 : :
812 : : static bool
813 : 0 : hns3_check_tcp_mask_supported(const struct rte_flow_item_tcp *tcp_mask)
814 : : {
815 [ # # # # ]: 0 : if (tcp_mask->hdr.sent_seq || tcp_mask->hdr.recv_ack ||
816 [ # # # # ]: 0 : tcp_mask->hdr.data_off || tcp_mask->hdr.tcp_flags ||
817 [ # # # # ]: 0 : tcp_mask->hdr.rx_win || tcp_mask->hdr.cksum ||
818 [ # # ]: 0 : tcp_mask->hdr.tcp_urp)
819 : 0 : return false;
820 : :
821 : : return true;
822 : : }
823 : :
824 : : static int
825 : 0 : hns3_parse_tcp(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
826 : : struct rte_flow_error *error)
827 : : {
828 : : const struct rte_flow_item_tcp *tcp_spec;
829 : : const struct rte_flow_item_tcp *tcp_mask;
830 : :
831 : 0 : hns3_set_bit(rule->input_set, INNER_IP_PROTO, 1);
832 : 0 : rule->key_conf.spec.ip_proto = IPPROTO_TCP;
833 : 0 : rule->key_conf.mask.ip_proto = IPPROTO_MASK;
834 : :
835 : : /* Only used to describe the protocol stack. */
836 [ # # # # ]: 0 : if (item->spec == NULL && item->mask == NULL)
837 : : return 0;
838 : :
839 [ # # ]: 0 : if (item->mask) {
840 : : tcp_mask = item->mask;
841 [ # # ]: 0 : if (!hns3_check_tcp_mask_supported(tcp_mask)) {
842 : 0 : return rte_flow_error_set(error, EINVAL,
843 : : RTE_FLOW_ERROR_TYPE_ITEM_MASK,
844 : : item,
845 : : "Only support src & dst port in TCP");
846 : : }
847 : :
848 [ # # ]: 0 : if (tcp_mask->hdr.src_port) {
849 : 0 : hns3_set_bit(rule->input_set, INNER_SRC_PORT, 1);
850 : 0 : rule->key_conf.mask.src_port =
851 [ # # ]: 0 : rte_be_to_cpu_16(tcp_mask->hdr.src_port);
852 : : }
853 [ # # ]: 0 : if (tcp_mask->hdr.dst_port) {
854 : 0 : hns3_set_bit(rule->input_set, INNER_DST_PORT, 1);
855 : 0 : rule->key_conf.mask.dst_port =
856 [ # # ]: 0 : rte_be_to_cpu_16(tcp_mask->hdr.dst_port);
857 : : }
858 : : }
859 : :
860 : : tcp_spec = item->spec;
861 [ # # ]: 0 : rule->key_conf.spec.src_port = rte_be_to_cpu_16(tcp_spec->hdr.src_port);
862 [ # # ]: 0 : rule->key_conf.spec.dst_port = rte_be_to_cpu_16(tcp_spec->hdr.dst_port);
863 : :
864 : 0 : return 0;
865 : : }
866 : :
867 : : static int
868 : 0 : hns3_parse_udp(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
869 : : struct rte_flow_error *error)
870 : : {
871 : : const struct rte_flow_item_udp *udp_spec;
872 : : const struct rte_flow_item_udp *udp_mask;
873 : :
874 : 0 : hns3_set_bit(rule->input_set, INNER_IP_PROTO, 1);
875 : 0 : rule->key_conf.spec.ip_proto = IPPROTO_UDP;
876 : 0 : rule->key_conf.mask.ip_proto = IPPROTO_MASK;
877 : :
878 : : /* Only used to describe the protocol stack. */
879 [ # # # # ]: 0 : if (item->spec == NULL && item->mask == NULL)
880 : : return 0;
881 : :
882 [ # # ]: 0 : if (item->mask) {
883 : : udp_mask = item->mask;
884 [ # # # # ]: 0 : if (udp_mask->hdr.dgram_len || udp_mask->hdr.dgram_cksum) {
885 : 0 : return rte_flow_error_set(error, EINVAL,
886 : : RTE_FLOW_ERROR_TYPE_ITEM_MASK,
887 : : item,
888 : : "Only support src & dst port in UDP");
889 : : }
890 [ # # ]: 0 : if (udp_mask->hdr.src_port) {
891 : 0 : hns3_set_bit(rule->input_set, INNER_SRC_PORT, 1);
892 : 0 : rule->key_conf.mask.src_port =
893 [ # # ]: 0 : rte_be_to_cpu_16(udp_mask->hdr.src_port);
894 : : }
895 [ # # ]: 0 : if (udp_mask->hdr.dst_port) {
896 : 0 : hns3_set_bit(rule->input_set, INNER_DST_PORT, 1);
897 : 0 : rule->key_conf.mask.dst_port =
898 [ # # ]: 0 : rte_be_to_cpu_16(udp_mask->hdr.dst_port);
899 : : }
900 : : }
901 : :
902 : : udp_spec = item->spec;
903 [ # # ]: 0 : rule->key_conf.spec.src_port = rte_be_to_cpu_16(udp_spec->hdr.src_port);
904 [ # # ]: 0 : rule->key_conf.spec.dst_port = rte_be_to_cpu_16(udp_spec->hdr.dst_port);
905 : :
906 : 0 : return 0;
907 : : }
908 : :
909 : : static int
910 : 0 : hns3_parse_sctp(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
911 : : struct rte_flow_error *error)
912 : : {
913 : : const struct rte_flow_item_sctp *sctp_spec;
914 : : const struct rte_flow_item_sctp *sctp_mask;
915 : :
916 : 0 : hns3_set_bit(rule->input_set, INNER_IP_PROTO, 1);
917 : 0 : rule->key_conf.spec.ip_proto = IPPROTO_SCTP;
918 : 0 : rule->key_conf.mask.ip_proto = IPPROTO_MASK;
919 : :
920 : : /* Only used to describe the protocol stack. */
921 [ # # # # ]: 0 : if (item->spec == NULL && item->mask == NULL)
922 : : return 0;
923 : :
924 [ # # ]: 0 : if (item->mask) {
925 : : sctp_mask = item->mask;
926 [ # # ]: 0 : if (sctp_mask->hdr.cksum)
927 : 0 : return rte_flow_error_set(error, EINVAL,
928 : : RTE_FLOW_ERROR_TYPE_ITEM_MASK,
929 : : item,
930 : : "Only support src & dst port & v-tag in SCTP");
931 [ # # ]: 0 : if (sctp_mask->hdr.src_port) {
932 : 0 : hns3_set_bit(rule->input_set, INNER_SRC_PORT, 1);
933 : 0 : rule->key_conf.mask.src_port =
934 [ # # ]: 0 : rte_be_to_cpu_16(sctp_mask->hdr.src_port);
935 : : }
936 [ # # ]: 0 : if (sctp_mask->hdr.dst_port) {
937 : 0 : hns3_set_bit(rule->input_set, INNER_DST_PORT, 1);
938 : 0 : rule->key_conf.mask.dst_port =
939 [ # # ]: 0 : rte_be_to_cpu_16(sctp_mask->hdr.dst_port);
940 : : }
941 [ # # ]: 0 : if (sctp_mask->hdr.tag) {
942 : 0 : hns3_set_bit(rule->input_set, INNER_SCTP_TAG, 1);
943 : 0 : rule->key_conf.mask.sctp_tag =
944 [ # # ]: 0 : rte_be_to_cpu_32(sctp_mask->hdr.tag);
945 : : }
946 : : }
947 : :
948 : : sctp_spec = item->spec;
949 : 0 : rule->key_conf.spec.src_port =
950 [ # # ]: 0 : rte_be_to_cpu_16(sctp_spec->hdr.src_port);
951 : 0 : rule->key_conf.spec.dst_port =
952 [ # # ]: 0 : rte_be_to_cpu_16(sctp_spec->hdr.dst_port);
953 [ # # ]: 0 : rule->key_conf.spec.sctp_tag = rte_be_to_cpu_32(sctp_spec->hdr.tag);
954 : :
955 : 0 : return 0;
956 : : }
957 : :
958 : : /*
959 : : * Check items before tunnel, save inner configs to outer configs, and clear
960 : : * inner configs.
961 : : * The key consists of two parts: meta_data and tuple keys.
962 : : * Meta data uses 15 bits, including vlan_num(2bit), des_port(12bit) and tunnel
963 : : * packet(1bit).
964 : : * Tuple keys uses 384bit, including ot_dst-mac(48bit), ot_dst-port(16bit),
965 : : * ot_tun_vni(24bit), ot_flow_id(8bit), src-mac(48bit), dst-mac(48bit),
966 : : * src-ip(32/128bit), dst-ip(32/128bit), src-port(16bit), dst-port(16bit),
967 : : * tos(8bit), ether-proto(16bit), ip-proto(8bit), vlantag1(16bit),
968 : : * Vlantag2(16bit) and sctp-tag(32bit).
969 : : */
970 : : static int
971 : 0 : hns3_handle_tunnel(const struct rte_flow_item *item,
972 : : struct hns3_fdir_rule *rule, struct rte_flow_error *error)
973 : : {
974 : : /* check eth config */
975 [ # # ]: 0 : if (rule->input_set & (BIT(INNER_SRC_MAC) | BIT(INNER_DST_MAC)))
976 : 0 : return rte_flow_error_set(error, EINVAL,
977 : : RTE_FLOW_ERROR_TYPE_ITEM,
978 : : item, "Outer eth mac is unsupported");
979 [ # # ]: 0 : if (rule->input_set & BIT(INNER_ETH_TYPE)) {
980 : 0 : hns3_set_bit(rule->input_set, OUTER_ETH_TYPE, 1);
981 : 0 : rule->key_conf.spec.outer_ether_type =
982 : 0 : rule->key_conf.spec.ether_type;
983 : 0 : rule->key_conf.mask.outer_ether_type =
984 : 0 : rule->key_conf.mask.ether_type;
985 : 0 : hns3_set_bit(rule->input_set, INNER_ETH_TYPE, 0);
986 : 0 : rule->key_conf.spec.ether_type = 0;
987 : 0 : rule->key_conf.mask.ether_type = 0;
988 : : }
989 : :
990 : : /* check vlan config */
991 [ # # ]: 0 : if (rule->input_set & (BIT(INNER_VLAN_TAG1) | BIT(INNER_VLAN_TAG2)))
992 : 0 : return rte_flow_error_set(error, EINVAL,
993 : : RTE_FLOW_ERROR_TYPE_ITEM,
994 : : item,
995 : : "Outer vlan tags is unsupported");
996 : :
997 : : /* clear vlan_num for inner vlan select */
998 : 0 : rule->key_conf.outer_vlan_num = rule->key_conf.vlan_num;
999 : 0 : rule->key_conf.vlan_num = 0;
1000 : :
1001 : : /* check L3 config */
1002 [ # # ]: 0 : if (rule->input_set &
1003 : : (BIT(INNER_SRC_IP) | BIT(INNER_DST_IP) | BIT(INNER_IP_TOS)))
1004 : 0 : return rte_flow_error_set(error, EINVAL,
1005 : : RTE_FLOW_ERROR_TYPE_ITEM,
1006 : : item, "Outer ip is unsupported");
1007 [ # # ]: 0 : if (rule->input_set & BIT(INNER_IP_PROTO)) {
1008 : 0 : hns3_set_bit(rule->input_set, OUTER_IP_PROTO, 1);
1009 : 0 : rule->key_conf.spec.outer_proto = rule->key_conf.spec.ip_proto;
1010 : 0 : rule->key_conf.mask.outer_proto = rule->key_conf.mask.ip_proto;
1011 : 0 : hns3_set_bit(rule->input_set, INNER_IP_PROTO, 0);
1012 : 0 : rule->key_conf.spec.ip_proto = 0;
1013 : 0 : rule->key_conf.mask.ip_proto = 0;
1014 : : }
1015 : :
1016 : : /* check L4 config */
1017 [ # # ]: 0 : if (rule->input_set & BIT(INNER_SCTP_TAG))
1018 : 0 : return rte_flow_error_set(error, EINVAL,
1019 : : RTE_FLOW_ERROR_TYPE_ITEM, item,
1020 : : "Outer sctp tag is unsupported");
1021 : :
1022 [ # # ]: 0 : if (rule->input_set & BIT(INNER_SRC_PORT)) {
1023 : 0 : hns3_set_bit(rule->input_set, OUTER_SRC_PORT, 1);
1024 : 0 : rule->key_conf.spec.outer_src_port =
1025 : 0 : rule->key_conf.spec.src_port;
1026 : 0 : rule->key_conf.mask.outer_src_port =
1027 : 0 : rule->key_conf.mask.src_port;
1028 : 0 : hns3_set_bit(rule->input_set, INNER_SRC_PORT, 0);
1029 : 0 : rule->key_conf.spec.src_port = 0;
1030 : 0 : rule->key_conf.mask.src_port = 0;
1031 : : }
1032 [ # # ]: 0 : if (rule->input_set & BIT(INNER_DST_PORT)) {
1033 : 0 : hns3_set_bit(rule->input_set, INNER_DST_PORT, 0);
1034 : 0 : rule->key_conf.spec.dst_port = 0;
1035 : 0 : rule->key_conf.mask.dst_port = 0;
1036 : : }
1037 : : return 0;
1038 : : }
1039 : :
1040 : : static int
1041 : 0 : hns3_parse_vxlan(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
1042 : : struct rte_flow_error *error)
1043 : : {
1044 : : const struct rte_flow_item_vxlan *vxlan_spec;
1045 : : const struct rte_flow_item_vxlan *vxlan_mask;
1046 : :
1047 : 0 : hns3_set_bit(rule->input_set, OUTER_DST_PORT, 1);
1048 : 0 : rule->key_conf.mask.tunnel_type = TUNNEL_TYPE_MASK;
1049 [ # # ]: 0 : if (item->type == RTE_FLOW_ITEM_TYPE_VXLAN)
1050 : 0 : rule->key_conf.spec.tunnel_type = HNS3_TUNNEL_TYPE_VXLAN;
1051 : : else
1052 : 0 : rule->key_conf.spec.tunnel_type = HNS3_TUNNEL_TYPE_VXLAN_GPE;
1053 : :
1054 : : /* Only used to describe the protocol stack. */
1055 [ # # # # ]: 0 : if (item->spec == NULL && item->mask == NULL)
1056 : : return 0;
1057 : :
1058 : 0 : vxlan_mask = item->mask;
1059 : : vxlan_spec = item->spec;
1060 : :
1061 [ # # ]: 0 : if (vxlan_mask->hdr.flags)
1062 : 0 : return rte_flow_error_set(error, EINVAL,
1063 : : RTE_FLOW_ERROR_TYPE_ITEM_MASK, item,
1064 : : "Flags is not supported in VxLAN");
1065 : :
1066 : : /* VNI must be totally masked or not. */
1067 [ # # ]: 0 : if (memcmp(vxlan_mask->hdr.vni, full_mask, VNI_OR_TNI_LEN) &&
1068 [ # # ]: 0 : memcmp(vxlan_mask->hdr.vni, zero_mask, VNI_OR_TNI_LEN))
1069 : 0 : return rte_flow_error_set(error, EINVAL,
1070 : : RTE_FLOW_ERROR_TYPE_ITEM_MASK, item,
1071 : : "VNI must be totally masked or not in VxLAN");
1072 [ # # ]: 0 : if (vxlan_mask->hdr.vni[0]) {
1073 : 0 : hns3_set_bit(rule->input_set, OUTER_TUN_VNI, 1);
1074 : 0 : memcpy(rule->key_conf.mask.outer_tun_vni, vxlan_mask->hdr.vni,
1075 : : VNI_OR_TNI_LEN);
1076 : : }
1077 : 0 : memcpy(rule->key_conf.spec.outer_tun_vni, vxlan_spec->hdr.vni,
1078 : : VNI_OR_TNI_LEN);
1079 : 0 : return 0;
1080 : : }
1081 : :
1082 : : static int
1083 : 0 : hns3_parse_nvgre(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
1084 : : struct rte_flow_error *error)
1085 : : {
1086 : : const struct rte_flow_item_nvgre *nvgre_spec;
1087 : : const struct rte_flow_item_nvgre *nvgre_mask;
1088 : :
1089 : 0 : hns3_set_bit(rule->input_set, OUTER_IP_PROTO, 1);
1090 : 0 : rule->key_conf.spec.outer_proto = IPPROTO_GRE;
1091 : 0 : rule->key_conf.mask.outer_proto = IPPROTO_MASK;
1092 : :
1093 : 0 : hns3_set_bit(rule->input_set, OUTER_DST_PORT, 1);
1094 : 0 : rule->key_conf.spec.tunnel_type = HNS3_TUNNEL_TYPE_NVGRE;
1095 : 0 : rule->key_conf.mask.tunnel_type = ~HNS3_TUNNEL_TYPE_NVGRE;
1096 : : /* Only used to describe the protocol stack. */
1097 [ # # # # ]: 0 : if (item->spec == NULL && item->mask == NULL)
1098 : : return 0;
1099 : :
1100 : 0 : nvgre_mask = item->mask;
1101 : : nvgre_spec = item->spec;
1102 : :
1103 [ # # # # ]: 0 : if (nvgre_mask->protocol || nvgre_mask->c_k_s_rsvd0_ver)
1104 : 0 : return rte_flow_error_set(error, EINVAL,
1105 : : RTE_FLOW_ERROR_TYPE_ITEM_MASK, item,
1106 : : "Ver/protocol is not supported in NVGRE");
1107 : :
1108 : : /* TNI must be totally masked or not. */
1109 [ # # ]: 0 : if (memcmp(nvgre_mask->tni, full_mask, VNI_OR_TNI_LEN) &&
1110 [ # # ]: 0 : memcmp(nvgre_mask->tni, zero_mask, VNI_OR_TNI_LEN))
1111 : 0 : return rte_flow_error_set(error, EINVAL,
1112 : : RTE_FLOW_ERROR_TYPE_ITEM_MASK, item,
1113 : : "TNI must be totally masked or not in NVGRE");
1114 : :
1115 [ # # ]: 0 : if (nvgre_mask->tni[0]) {
1116 : 0 : hns3_set_bit(rule->input_set, OUTER_TUN_VNI, 1);
1117 : 0 : memcpy(rule->key_conf.mask.outer_tun_vni, nvgre_mask->tni,
1118 : : VNI_OR_TNI_LEN);
1119 : : }
1120 [ # # ]: 0 : memcpy(rule->key_conf.spec.outer_tun_vni, nvgre_spec->tni,
1121 : : VNI_OR_TNI_LEN);
1122 : :
1123 [ # # ]: 0 : if (nvgre_mask->flow_id) {
1124 : 0 : hns3_set_bit(rule->input_set, OUTER_TUN_FLOW_ID, 1);
1125 : 0 : rule->key_conf.mask.outer_tun_flow_id = nvgre_mask->flow_id;
1126 : : }
1127 : 0 : rule->key_conf.spec.outer_tun_flow_id = nvgre_spec->flow_id;
1128 : 0 : return 0;
1129 : : }
1130 : :
1131 : : static int
1132 : 0 : hns3_parse_geneve(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
1133 : : struct rte_flow_error *error)
1134 : : {
1135 : : const struct rte_flow_item_geneve *geneve_spec;
1136 : : const struct rte_flow_item_geneve *geneve_mask;
1137 : :
1138 : 0 : hns3_set_bit(rule->input_set, OUTER_DST_PORT, 1);
1139 : 0 : rule->key_conf.spec.tunnel_type = HNS3_TUNNEL_TYPE_GENEVE;
1140 : 0 : rule->key_conf.mask.tunnel_type = TUNNEL_TYPE_MASK;
1141 : : /* Only used to describe the protocol stack. */
1142 [ # # # # ]: 0 : if (item->spec == NULL && item->mask == NULL)
1143 : : return 0;
1144 : :
1145 : 0 : geneve_mask = item->mask;
1146 : : geneve_spec = item->spec;
1147 : :
1148 [ # # # # ]: 0 : if (geneve_mask->ver_opt_len_o_c_rsvd0 || geneve_mask->protocol)
1149 : 0 : return rte_flow_error_set(error, EINVAL,
1150 : : RTE_FLOW_ERROR_TYPE_ITEM_MASK, item,
1151 : : "Ver/protocol is not supported in GENEVE");
1152 : : /* VNI must be totally masked or not. */
1153 [ # # ]: 0 : if (memcmp(geneve_mask->vni, full_mask, VNI_OR_TNI_LEN) &&
1154 [ # # ]: 0 : memcmp(geneve_mask->vni, zero_mask, VNI_OR_TNI_LEN))
1155 : 0 : return rte_flow_error_set(error, EINVAL,
1156 : : RTE_FLOW_ERROR_TYPE_ITEM_MASK, item,
1157 : : "VNI must be totally masked or not in GENEVE");
1158 [ # # ]: 0 : if (geneve_mask->vni[0]) {
1159 : 0 : hns3_set_bit(rule->input_set, OUTER_TUN_VNI, 1);
1160 : 0 : memcpy(rule->key_conf.mask.outer_tun_vni, geneve_mask->vni,
1161 : : VNI_OR_TNI_LEN);
1162 : : }
1163 : 0 : memcpy(rule->key_conf.spec.outer_tun_vni, geneve_spec->vni,
1164 : : VNI_OR_TNI_LEN);
1165 : 0 : return 0;
1166 : : }
1167 : :
1168 : : static int
1169 : 0 : hns3_parse_tunnel(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
1170 : : struct rte_flow_error *error)
1171 : : {
1172 : : int ret;
1173 : :
1174 [ # # # # ]: 0 : if (item->spec == NULL && item->mask)
1175 : 0 : return rte_flow_error_set(error, EINVAL,
1176 : : RTE_FLOW_ERROR_TYPE_ITEM, item,
1177 : : "Can't configure FDIR with mask "
1178 : : "but without spec");
1179 [ # # # # ]: 0 : else if (item->spec && (item->mask == NULL))
1180 : 0 : return rte_flow_error_set(error, EINVAL,
1181 : : RTE_FLOW_ERROR_TYPE_ITEM, item,
1182 : : "Tunnel packets must configure "
1183 : : "with mask");
1184 : :
1185 [ # # # # ]: 0 : switch (item->type) {
1186 : 0 : case RTE_FLOW_ITEM_TYPE_VXLAN:
1187 : : case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1188 : 0 : ret = hns3_parse_vxlan(item, rule, error);
1189 : 0 : break;
1190 : 0 : case RTE_FLOW_ITEM_TYPE_NVGRE:
1191 : 0 : ret = hns3_parse_nvgre(item, rule, error);
1192 : 0 : break;
1193 : 0 : case RTE_FLOW_ITEM_TYPE_GENEVE:
1194 : 0 : ret = hns3_parse_geneve(item, rule, error);
1195 : 0 : break;
1196 : 0 : default:
1197 : 0 : return rte_flow_error_set(error, ENOTSUP,
1198 : : RTE_FLOW_ERROR_TYPE_ITEM,
1199 : : NULL, "Unsupported tunnel type!");
1200 : : }
1201 [ # # ]: 0 : if (ret)
1202 : : return ret;
1203 : 0 : return hns3_handle_tunnel(item, rule, error);
1204 : : }
1205 : :
1206 : : static int
1207 : 0 : hns3_parse_normal(const struct rte_flow_item *item, struct hns3_fdir_rule *rule,
1208 : : struct items_step_mngr *step_mngr,
1209 : : struct rte_flow_error *error)
1210 : : {
1211 : : int ret;
1212 : :
1213 [ # # # # ]: 0 : if (item->spec == NULL && item->mask)
1214 : 0 : return rte_flow_error_set(error, EINVAL,
1215 : : RTE_FLOW_ERROR_TYPE_ITEM, item,
1216 : : "Can't configure FDIR with mask "
1217 : : "but without spec");
1218 : :
1219 [ # # # # : 0 : switch (item->type) {
# # # # ]
1220 : 0 : case RTE_FLOW_ITEM_TYPE_ETH:
1221 : 0 : ret = hns3_parse_eth(item, rule, error);
1222 : 0 : step_mngr->items = L2_next_items;
1223 : 0 : step_mngr->count = RTE_DIM(L2_next_items);
1224 : 0 : break;
1225 : 0 : case RTE_FLOW_ITEM_TYPE_VLAN:
1226 : 0 : ret = hns3_parse_vlan(item, rule, error);
1227 : 0 : step_mngr->items = L2_next_items;
1228 : 0 : step_mngr->count = RTE_DIM(L2_next_items);
1229 : 0 : break;
1230 : 0 : case RTE_FLOW_ITEM_TYPE_IPV4:
1231 : 0 : ret = hns3_parse_ipv4(item, rule, error);
1232 : 0 : step_mngr->items = L3_next_items;
1233 : 0 : step_mngr->count = RTE_DIM(L3_next_items);
1234 : 0 : break;
1235 : 0 : case RTE_FLOW_ITEM_TYPE_IPV6:
1236 : 0 : ret = hns3_parse_ipv6(item, rule, error);
1237 : 0 : step_mngr->items = L3_next_items;
1238 : 0 : step_mngr->count = RTE_DIM(L3_next_items);
1239 : 0 : break;
1240 : 0 : case RTE_FLOW_ITEM_TYPE_TCP:
1241 : 0 : ret = hns3_parse_tcp(item, rule, error);
1242 : 0 : step_mngr->items = L4_next_items;
1243 : 0 : step_mngr->count = RTE_DIM(L4_next_items);
1244 : 0 : break;
1245 : 0 : case RTE_FLOW_ITEM_TYPE_UDP:
1246 : 0 : ret = hns3_parse_udp(item, rule, error);
1247 : 0 : step_mngr->items = L4_next_items;
1248 : 0 : step_mngr->count = RTE_DIM(L4_next_items);
1249 : 0 : break;
1250 : 0 : case RTE_FLOW_ITEM_TYPE_SCTP:
1251 : 0 : ret = hns3_parse_sctp(item, rule, error);
1252 : 0 : step_mngr->items = L4_next_items;
1253 : 0 : step_mngr->count = RTE_DIM(L4_next_items);
1254 : 0 : break;
1255 : 0 : default:
1256 : 0 : return rte_flow_error_set(error, ENOTSUP,
1257 : : RTE_FLOW_ERROR_TYPE_ITEM,
1258 : : NULL, "Unsupported normal type!");
1259 : : }
1260 : :
1261 : : return ret;
1262 : : }
1263 : :
1264 : : static int
1265 : 0 : hns3_validate_item(const struct rte_flow_item *item,
1266 : : struct items_step_mngr step_mngr,
1267 : : struct rte_flow_error *error)
1268 : : {
1269 : : uint32_t i;
1270 : :
1271 [ # # ]: 0 : if (item->last)
1272 : 0 : return rte_flow_error_set(error, ENOTSUP,
1273 : : RTE_FLOW_ERROR_TYPE_ITEM_LAST, item,
1274 : : "Not supported last point for range");
1275 : :
1276 [ # # ]: 0 : for (i = 0; i < step_mngr.count; i++) {
1277 [ # # ]: 0 : if (item->type == step_mngr.items[i])
1278 : : break;
1279 : : }
1280 : :
1281 [ # # ]: 0 : if (i == step_mngr.count) {
1282 : 0 : return rte_flow_error_set(error, EINVAL,
1283 : : RTE_FLOW_ERROR_TYPE_ITEM,
1284 : : item, "Inval or missing item");
1285 : : }
1286 : : return 0;
1287 : : }
1288 : :
1289 : : static inline bool
1290 : : is_tunnel_packet(enum rte_flow_item_type type)
1291 : : {
1292 : : if (type == RTE_FLOW_ITEM_TYPE_VXLAN_GPE ||
1293 : : type == RTE_FLOW_ITEM_TYPE_VXLAN ||
1294 : : type == RTE_FLOW_ITEM_TYPE_NVGRE ||
1295 : : type == RTE_FLOW_ITEM_TYPE_GENEVE)
1296 : : return true;
1297 : : return false;
1298 : : }
1299 : :
1300 : : /*
1301 : : * Parse the flow director rule.
1302 : : * The supported PATTERN:
1303 : : * case: non-tunnel packet:
1304 : : * ETH : src-mac, dst-mac, ethertype
1305 : : * VLAN: tag1, tag2
1306 : : * IPv4: src-ip, dst-ip, tos, proto
1307 : : * IPv6: src-ip(last 32 bit addr), dst-ip(last 32 bit addr), proto
1308 : : * UDP : src-port, dst-port
1309 : : * TCP : src-port, dst-port
1310 : : * SCTP: src-port, dst-port, tag
1311 : : * case: tunnel packet:
1312 : : * OUTER-ETH: ethertype
1313 : : * OUTER-L3 : proto
1314 : : * OUTER-L4 : src-port, dst-port
1315 : : * TUNNEL : vni, flow-id(only valid when NVGRE)
1316 : : * INNER-ETH/VLAN/IPv4/IPv6/UDP/TCP/SCTP: same as non-tunnel packet
1317 : : * The supported ACTION:
1318 : : * QUEUE
1319 : : * DROP
1320 : : * COUNT
1321 : : * MARK: the id range [0, 4094]
1322 : : * FLAG
1323 : : * RSS: only valid if firmware support FD_QUEUE_REGION.
1324 : : */
1325 : : static int
1326 : 0 : hns3_parse_fdir_filter(struct rte_eth_dev *dev,
1327 : : const struct rte_flow_item pattern[],
1328 : : const struct rte_flow_action actions[],
1329 : : struct hns3_fdir_rule *rule,
1330 : : struct rte_flow_error *error)
1331 : : {
1332 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
1333 : : const struct rte_flow_item *item;
1334 : : struct items_step_mngr step_mngr;
1335 : : int ret;
1336 : :
1337 : : /* FDIR is available only in PF driver */
1338 [ # # ]: 0 : if (hns->is_vf)
1339 : 0 : return rte_flow_error_set(error, ENOTSUP,
1340 : : RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1341 : : "Fdir not supported in VF");
1342 : :
1343 : 0 : step_mngr.items = first_items;
1344 : 0 : step_mngr.count = RTE_DIM(first_items);
1345 [ # # ]: 0 : for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1346 [ # # ]: 0 : if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
1347 : 0 : continue;
1348 : :
1349 : 0 : ret = hns3_validate_item(item, step_mngr, error);
1350 [ # # ]: 0 : if (ret)
1351 : 0 : return ret;
1352 : :
1353 [ # # ]: 0 : if (is_tunnel_packet(item->type)) {
1354 : 0 : ret = hns3_parse_tunnel(item, rule, error);
1355 [ # # ]: 0 : if (ret)
1356 : 0 : return ret;
1357 : 0 : step_mngr.items = tunnel_next_items;
1358 : 0 : step_mngr.count = RTE_DIM(tunnel_next_items);
1359 : : } else {
1360 : 0 : ret = hns3_parse_normal(item, rule, &step_mngr, error);
1361 [ # # ]: 0 : if (ret)
1362 : 0 : return ret;
1363 : : }
1364 : : }
1365 : :
1366 : 0 : return hns3_handle_actions(dev, actions, rule, error);
1367 : : }
1368 : :
1369 : : static void
1370 : 0 : hns3_filterlist_flush(struct rte_eth_dev *dev)
1371 : : {
1372 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1373 : : struct hns3_fdir_rule_ele *fdir_rule_ptr;
1374 : : struct hns3_flow_mem *flow_node;
1375 : :
1376 : 0 : fdir_rule_ptr = TAILQ_FIRST(&hw->flow_fdir_list);
1377 [ # # ]: 0 : while (fdir_rule_ptr) {
1378 [ # # ]: 0 : TAILQ_REMOVE(&hw->flow_fdir_list, fdir_rule_ptr, entries);
1379 : 0 : rte_free(fdir_rule_ptr);
1380 : 0 : fdir_rule_ptr = TAILQ_FIRST(&hw->flow_fdir_list);
1381 : : }
1382 : :
1383 : 0 : flow_node = TAILQ_FIRST(&hw->flow_list);
1384 [ # # ]: 0 : while (flow_node) {
1385 [ # # ]: 0 : TAILQ_REMOVE(&hw->flow_list, flow_node, entries);
1386 : 0 : rte_free(flow_node->flow);
1387 : 0 : rte_free(flow_node);
1388 : 0 : flow_node = TAILQ_FIRST(&hw->flow_list);
1389 : : }
1390 : 0 : }
1391 : :
1392 : : static bool
1393 : 0 : hns3_flow_rule_key_same(const struct rte_flow_action_rss *comp,
1394 : : const struct rte_flow_action_rss *with)
1395 : : {
1396 [ # # ]: 0 : if (comp->key_len != with->key_len)
1397 : : return false;
1398 : :
1399 [ # # ]: 0 : if (with->key_len == 0)
1400 : : return true;
1401 : :
1402 [ # # # # ]: 0 : if (comp->key == NULL && with->key == NULL)
1403 : : return true;
1404 : :
1405 [ # # # # ]: 0 : if (!(comp->key != NULL && with->key != NULL))
1406 : : return false;
1407 : :
1408 : 0 : return !memcmp(comp->key, with->key, with->key_len);
1409 : : }
1410 : :
1411 : : static bool
1412 : 0 : hns3_flow_rule_queues_same(const struct rte_flow_action_rss *comp,
1413 : : const struct rte_flow_action_rss *with)
1414 : : {
1415 [ # # ]: 0 : if (comp->queue_num != with->queue_num)
1416 : : return false;
1417 : :
1418 [ # # ]: 0 : if (with->queue_num == 0)
1419 : : return true;
1420 : :
1421 [ # # # # ]: 0 : if (comp->queue == NULL && with->queue == NULL)
1422 : : return true;
1423 : :
1424 [ # # # # ]: 0 : if (!(comp->queue != NULL && with->queue != NULL))
1425 : : return false;
1426 : :
1427 : 0 : return !memcmp(comp->queue, with->queue, with->queue_num);
1428 : : }
1429 : :
1430 : : static bool
1431 : 0 : hns3_action_rss_same(const struct rte_flow_action_rss *comp,
1432 : : const struct rte_flow_action_rss *with)
1433 : : {
1434 : : bool same_level;
1435 : : bool same_types;
1436 : : bool same_func;
1437 : :
1438 : 0 : same_level = (comp->level == with->level);
1439 : 0 : same_types = (comp->types == with->types);
1440 : 0 : same_func = (comp->func == with->func);
1441 : :
1442 [ # # ]: 0 : return same_level && same_types && same_func &&
1443 [ # # # # ]: 0 : hns3_flow_rule_key_same(comp, with) &&
1444 [ # # ]: 0 : hns3_flow_rule_queues_same(comp, with);
1445 : : }
1446 : :
1447 : : static bool
1448 : : hns3_valid_ipv6_sctp_rss_types(struct hns3_hw *hw, uint64_t types)
1449 : : {
1450 : : /*
1451 : : * Some hardware don't support to use src/dst port fields to hash
1452 : : * for IPV6 SCTP packet type.
1453 : : */
1454 [ # # ]: 0 : if (types & RTE_ETH_RSS_NONFRAG_IPV6_SCTP &&
1455 [ # # ]: 0 : types & HNS3_RSS_SUPPORT_L4_SRC_DST &&
1456 [ # # ]: 0 : !hw->rss_info.ipv6_sctp_offload_supported)
1457 : : return false;
1458 : :
1459 : : return true;
1460 : : }
1461 : :
1462 : : static int
1463 : : hns3_flow_parse_hash_func(const struct rte_flow_action_rss *rss_act,
1464 : : struct hns3_flow_rss_conf *rss_conf,
1465 : : struct rte_flow_error *error)
1466 : : {
1467 : 0 : if (rss_act->func >= RTE_ETH_HASH_FUNCTION_MAX)
1468 : 0 : return rte_flow_error_set(error, ENOTSUP,
1469 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1470 : : NULL, "RSS hash func are not supported");
1471 : :
1472 : 0 : rss_conf->conf.func = rss_act->func;
1473 : : return 0;
1474 : : }
1475 : :
1476 : : static int
1477 : 0 : hns3_flow_parse_hash_key(struct hns3_hw *hw,
1478 : : const struct rte_flow_action_rss *rss_act,
1479 : : struct hns3_flow_rss_conf *rss_conf,
1480 : : struct rte_flow_error *error)
1481 : : {
1482 [ # # ]: 0 : if (rss_act->key_len != hw->rss_key_size)
1483 : 0 : return rte_flow_error_set(error, EINVAL,
1484 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1485 : : NULL, "invalid RSS key length");
1486 : :
1487 [ # # ]: 0 : if (rss_act->key != NULL)
1488 : 0 : memcpy(rss_conf->key, rss_act->key, rss_act->key_len);
1489 : : else
1490 : 0 : memcpy(rss_conf->key, hns3_hash_key,
1491 : 0 : RTE_MIN(sizeof(hns3_hash_key), rss_act->key_len));
1492 : : /* Need to record if user sets hash key. */
1493 : 0 : rss_conf->conf.key = rss_act->key;
1494 : 0 : rss_conf->conf.key_len = rss_act->key_len;
1495 : :
1496 : 0 : return 0;
1497 : : }
1498 : :
1499 : : static int
1500 : 0 : hns3_flow_parse_queues(struct hns3_hw *hw,
1501 : : const struct rte_flow_action_rss *rss_act,
1502 : : struct hns3_flow_rss_conf *rss_conf,
1503 : : struct rte_flow_error *error)
1504 : : {
1505 : : uint16_t i;
1506 : :
1507 [ # # ]: 0 : if (rss_act->queue_num > hw->rss_ind_tbl_size)
1508 : 0 : return rte_flow_error_set(error, ENOTSUP,
1509 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1510 : : NULL,
1511 : : "queue number can not exceed RSS indirection table.");
1512 : :
1513 [ # # ]: 0 : if (rss_act->queue_num > HNS3_RSS_QUEUES_BUFFER_NUM)
1514 : 0 : return rte_flow_error_set(error, ENOTSUP,
1515 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1516 : : NULL,
1517 : : "queue number configured exceeds queue buffer size driver supported");
1518 : :
1519 [ # # ]: 0 : for (i = 0; i < rss_act->queue_num; i++) {
1520 [ # # ]: 0 : if (rss_act->queue[i] >= hw->alloc_rss_size)
1521 : 0 : return rte_flow_error_set(error, EINVAL,
1522 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1523 : : NULL,
1524 : : "queue id must be less than queue number allocated to a TC");
1525 : : }
1526 : :
1527 : 0 : memcpy(rss_conf->queue, rss_act->queue,
1528 : 0 : rss_act->queue_num * sizeof(rss_conf->queue[0]));
1529 : 0 : rss_conf->conf.queue = rss_conf->queue;
1530 : 0 : rss_conf->conf.queue_num = rss_act->queue_num;
1531 : :
1532 : 0 : return 0;
1533 : : }
1534 : :
1535 : : static int
1536 : 0 : hns3_flow_get_hw_pctype(struct hns3_hw *hw,
1537 : : const struct rte_flow_action_rss *rss_act,
1538 : : const struct hns3_hash_map_info *map,
1539 : : struct hns3_flow_rss_conf *rss_conf,
1540 : : struct rte_flow_error *error)
1541 : : {
1542 : : uint64_t l3l4_src_dst, l3l4_refine, left_types;
1543 : :
1544 [ # # ]: 0 : if (rss_act->types == 0) {
1545 : : /* Disable RSS hash of this packet type if types is zero. */
1546 : 0 : rss_conf->hw_pctypes |= map->hw_pctype;
1547 : 0 : return 0;
1548 : : }
1549 : :
1550 : : /*
1551 : : * Can not have extra types except rss_pctype and l3l4_type in this map.
1552 : : */
1553 : 0 : left_types = ~map->rss_pctype & rss_act->types;
1554 [ # # ]: 0 : if (left_types & ~map->l3l4_types)
1555 : 0 : return rte_flow_error_set(error, EINVAL,
1556 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
1557 : : "cannot set extra types.");
1558 : :
1559 : : l3l4_src_dst = left_types;
1560 : : /* L3/L4 SRC and DST shouldn't be specified at the same time. */
1561 : : l3l4_refine = rte_eth_rss_hf_refine(l3l4_src_dst);
1562 [ # # ]: 0 : if (l3l4_refine != l3l4_src_dst)
1563 : 0 : return rte_flow_error_set(error, ENOTSUP,
1564 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
1565 : : "cannot specify L3_SRC/DST_ONLY or L4_SRC/DST_ONLY at the same.");
1566 : :
1567 : : if (!hns3_valid_ipv6_sctp_rss_types(hw, rss_act->types))
1568 : 0 : return rte_flow_error_set(error, ENOTSUP,
1569 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
1570 : : "hardware doesn't support to use L4 src/dst to hash for IPV6-SCTP.");
1571 : :
1572 : 0 : rss_conf->hw_pctypes |= map->hw_pctype;
1573 : :
1574 : 0 : return 0;
1575 : : }
1576 : :
1577 : : static int
1578 : 0 : hns3_flow_parse_rss_types_by_ptype(struct hns3_hw *hw,
1579 : : const struct rte_flow_action_rss *rss_act,
1580 : : uint64_t pattern_type,
1581 : : struct hns3_flow_rss_conf *rss_conf,
1582 : : struct rte_flow_error *error)
1583 : : {
1584 : : const struct hns3_hash_map_info *map;
1585 : : bool matched = false;
1586 : : uint16_t i;
1587 : : int ret;
1588 : :
1589 [ # # ]: 0 : for (i = 0; i < RTE_DIM(hash_map_table); i++) {
1590 : 0 : map = &hash_map_table[i];
1591 [ # # ]: 0 : if (map->pattern_type != pattern_type) {
1592 : : /*
1593 : : * If the target pattern type is already matched with
1594 : : * the one before this pattern in the hash map table,
1595 : : * no need to continue walk.
1596 : : */
1597 [ # # ]: 0 : if (matched)
1598 : : break;
1599 : 0 : continue;
1600 : : }
1601 : : matched = true;
1602 : :
1603 : : /*
1604 : : * If pattern type is matched and the 'types' is zero, all packet flow
1605 : : * types related to this pattern type disable RSS hash.
1606 : : * Otherwise, RSS types must match the pattern type and cannot have no
1607 : : * extra or unsupported types.
1608 : : */
1609 [ # # # # ]: 0 : if (rss_act->types != 0 && !(map->rss_pctype & rss_act->types))
1610 : 0 : continue;
1611 : :
1612 : 0 : ret = hns3_flow_get_hw_pctype(hw, rss_act, map, rss_conf, error);
1613 [ # # ]: 0 : if (ret != 0)
1614 : 0 : return ret;
1615 : : }
1616 : :
1617 [ # # ]: 0 : if (rss_conf->hw_pctypes != 0)
1618 : : return 0;
1619 : :
1620 [ # # ]: 0 : if (matched)
1621 : 0 : return rte_flow_error_set(error, ENOTSUP,
1622 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1623 : : NULL, "RSS types are unsupported");
1624 : :
1625 : 0 : return rte_flow_error_set(error, ENOTSUP,
1626 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1627 : : NULL, "Pattern specified is unsupported");
1628 : : }
1629 : :
1630 : : static uint64_t
1631 : : hns3_flow_get_all_hw_pctypes(uint64_t types)
1632 : : {
1633 : : uint64_t hw_pctypes = 0;
1634 : : uint16_t i;
1635 : :
1636 [ # # ]: 0 : for (i = 0; i < RTE_DIM(hash_map_table); i++) {
1637 [ # # ]: 0 : if (types & hash_map_table[i].rss_pctype)
1638 : 0 : hw_pctypes |= hash_map_table[i].hw_pctype;
1639 : : }
1640 : :
1641 : : return hw_pctypes;
1642 : : }
1643 : :
1644 : : static int
1645 : 0 : hns3_flow_parse_rss_types(struct hns3_hw *hw,
1646 : : const struct rte_flow_action_rss *rss_act,
1647 : : uint64_t pattern_type,
1648 : : struct hns3_flow_rss_conf *rss_conf,
1649 : : struct rte_flow_error *error)
1650 : : {
1651 : 0 : rss_conf->conf.types = rss_act->types;
1652 : :
1653 : : /* no pattern specified to set global RSS types. */
1654 [ # # ]: 0 : if (pattern_type == 0) {
1655 [ # # ]: 0 : if (!hns3_check_rss_types_valid(hw, rss_act->types))
1656 : 0 : return rte_flow_error_set(error, EINVAL,
1657 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1658 : : NULL, "RSS types is invalid.");
1659 : 0 : rss_conf->hw_pctypes =
1660 : 0 : hns3_flow_get_all_hw_pctypes(rss_act->types);
1661 : 0 : return 0;
1662 : : }
1663 : :
1664 : 0 : return hns3_flow_parse_rss_types_by_ptype(hw, rss_act, pattern_type,
1665 : : rss_conf, error);
1666 : : }
1667 : :
1668 : : static int
1669 : 0 : hns3_flow_parse_hash_global_conf(struct rte_eth_dev *dev,
1670 : : const struct rte_flow_action_rss *rss_act,
1671 : : struct hns3_flow_rss_conf *rss_conf,
1672 : : struct rte_flow_error *error)
1673 : : {
1674 [ # # ]: 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1675 : : int ret;
1676 : :
1677 : : ret = hns3_flow_parse_hash_func(rss_act, rss_conf, error);
1678 [ # # ]: 0 : if (ret != 0)
1679 : : return ret;
1680 : :
1681 [ # # ]: 0 : if (rss_act->queue_num > 0) {
1682 : 0 : ret = hns3_flow_parse_queues(hw, rss_act, rss_conf, error);
1683 [ # # ]: 0 : if (ret != 0)
1684 : : return ret;
1685 : : }
1686 : :
1687 [ # # ]: 0 : if (rss_act->key_len > 0) {
1688 : 0 : ret = hns3_flow_parse_hash_key(hw, rss_act, rss_conf, error);
1689 [ # # ]: 0 : if (ret != 0)
1690 : : return ret;
1691 : : }
1692 : :
1693 : 0 : return hns3_flow_parse_rss_types(hw, rss_act, rss_conf->pattern_type,
1694 : : rss_conf, error);
1695 : : }
1696 : :
1697 : : static int
1698 : 0 : hns3_flow_parse_pattern_type(const struct rte_flow_item pattern[],
1699 : : uint64_t *ptype, struct rte_flow_error *error)
1700 : : {
1701 : : enum rte_flow_item_type pre_type = RTE_FLOW_ITEM_TYPE_VOID;
1702 : : const char *message = "Pattern specified isn't supported";
1703 : : uint64_t item_hdr, pattern_hdrs = 0;
1704 : : enum rte_flow_item_type cur_type;
1705 : :
1706 [ # # ]: 0 : for (; pattern->type != RTE_FLOW_ITEM_TYPE_END; pattern++) {
1707 [ # # ]: 0 : if (pattern->type == RTE_FLOW_ITEM_TYPE_VOID)
1708 : 0 : continue;
1709 [ # # # # : 0 : if (pattern->mask || pattern->spec || pattern->last) {
# # ]
1710 : : message = "Header info shouldn't be specified";
1711 : 0 : goto unsup;
1712 : : }
1713 : :
1714 : : /* Check the sub-item allowed by the previous item . */
1715 [ # # ]: 0 : if (pre_type >= RTE_DIM(hash_pattern_next_allow_items) ||
1716 [ # # ]: 0 : !(hash_pattern_next_allow_items[pre_type] &
1717 : : BIT_ULL(pattern->type)))
1718 : 0 : goto unsup;
1719 : :
1720 : : cur_type = pattern->type;
1721 : : /* Unsupported for current type being greater than array size. */
1722 [ # # ]: 0 : if (cur_type >= RTE_DIM(hash_pattern_item_header))
1723 : 0 : goto unsup;
1724 : :
1725 : : /* The value is zero, which means unsupported current header. */
1726 : 0 : item_hdr = hash_pattern_item_header[cur_type];
1727 [ # # ]: 0 : if (item_hdr == 0)
1728 : 0 : goto unsup;
1729 : :
1730 : : /* Have duplicate pattern header. */
1731 [ # # ]: 0 : if (item_hdr & pattern_hdrs)
1732 : 0 : goto unsup;
1733 : : pre_type = cur_type;
1734 : 0 : pattern_hdrs |= item_hdr;
1735 : : }
1736 : :
1737 [ # # ]: 0 : if (pattern_hdrs != 0) {
1738 : 0 : *ptype = pattern_hdrs;
1739 : 0 : return 0;
1740 : : }
1741 : :
1742 : 0 : unsup:
1743 : 0 : return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
1744 : : pattern, message);
1745 : : }
1746 : :
1747 : : static int
1748 : 0 : hns3_flow_parse_pattern_act(struct rte_eth_dev *dev,
1749 : : const struct rte_flow_item pattern[],
1750 : : const struct rte_flow_action_rss *rss_act,
1751 : : struct hns3_flow_rss_conf *rss_conf,
1752 : : struct rte_flow_error *error)
1753 : : {
1754 [ # # ]: 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1755 : : int ret;
1756 : :
1757 : : ret = hns3_flow_parse_hash_func(rss_act, rss_conf, error);
1758 [ # # ]: 0 : if (ret != 0)
1759 : : return ret;
1760 : :
1761 [ # # ]: 0 : if (rss_act->key_len > 0) {
1762 : 0 : ret = hns3_flow_parse_hash_key(hw, rss_act, rss_conf, error);
1763 [ # # ]: 0 : if (ret != 0)
1764 : : return ret;
1765 : : }
1766 : :
1767 [ # # ]: 0 : if (rss_act->queue_num > 0) {
1768 : 0 : ret = hns3_flow_parse_queues(hw, rss_act, rss_conf, error);
1769 [ # # ]: 0 : if (ret != 0)
1770 : : return ret;
1771 : : }
1772 : :
1773 : 0 : ret = hns3_flow_parse_pattern_type(pattern, &rss_conf->pattern_type,
1774 : : error);
1775 [ # # ]: 0 : if (ret != 0)
1776 : : return ret;
1777 : :
1778 : 0 : ret = hns3_flow_parse_rss_types(hw, rss_act, rss_conf->pattern_type,
1779 : : rss_conf, error);
1780 [ # # ]: 0 : if (ret != 0)
1781 : : return ret;
1782 : :
1783 [ # # ]: 0 : if (rss_act->func != RTE_ETH_HASH_FUNCTION_DEFAULT ||
1784 [ # # ]: 0 : rss_act->key_len > 0 || rss_act->queue_num > 0)
1785 : 0 : hns3_warn(hw, "hash func, key and queues are global config, which work for all flow types. "
1786 : : "Recommend: don't set them together with pattern.");
1787 : :
1788 : : return 0;
1789 : : }
1790 : :
1791 : : static bool
1792 : 0 : hns3_rss_action_is_dup(struct hns3_hw *hw,
1793 : : const struct hns3_flow_rss_conf *conf)
1794 : : {
1795 : : struct hns3_rss_conf_ele *filter;
1796 : :
1797 [ # # ]: 0 : TAILQ_FOREACH(filter, &hw->flow_rss_list, entries) {
1798 [ # # ]: 0 : if (conf->pattern_type != filter->filter_info.pattern_type)
1799 : 0 : continue;
1800 : :
1801 [ # # ]: 0 : if (hns3_action_rss_same(&filter->filter_info.conf, &conf->conf))
1802 : : return true;
1803 : : }
1804 : :
1805 : : return false;
1806 : : }
1807 : :
1808 : : /*
1809 : : * This function is used to parse rss action validation.
1810 : : */
1811 : : static int
1812 : 0 : hns3_parse_rss_filter(struct rte_eth_dev *dev,
1813 : : const struct rte_flow_item pattern[],
1814 : : const struct rte_flow_action *actions,
1815 : : struct hns3_flow_rss_conf *rss_conf,
1816 : : struct rte_flow_error *error)
1817 : : {
1818 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
1819 : : const struct rte_flow_action_rss *rss_act;
1820 : : const struct rte_flow_action *act;
1821 : : const struct rte_flow_item *pat;
1822 : 0 : struct hns3_hw *hw = &hns->hw;
1823 : : uint32_t index = 0;
1824 : : int ret;
1825 : :
1826 [ # # ]: 0 : NEXT_ITEM_OF_ACTION(act, actions, index);
1827 [ # # ]: 0 : if (actions[1].type != RTE_FLOW_ACTION_TYPE_END)
1828 : 0 : return rte_flow_error_set(error, EINVAL,
1829 : : RTE_FLOW_ERROR_TYPE_ACTION,
1830 : 0 : &actions[1],
1831 : : "Only support one action for RSS.");
1832 : :
1833 : 0 : rss_act = (const struct rte_flow_action_rss *)act->conf;
1834 [ # # ]: 0 : if (rss_act == NULL) {
1835 : 0 : return rte_flow_error_set(error, EINVAL,
1836 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1837 : : act, "lost RSS action configuration");
1838 : : }
1839 : :
1840 [ # # ]: 0 : if (rss_act->level != 0)
1841 : 0 : return rte_flow_error_set(error, ENOTSUP,
1842 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1843 : : act,
1844 : : "RSS level is not supported");
1845 : :
1846 : : index = 0;
1847 [ # # ]: 0 : NEXT_ITEM_OF_PATTERN(pat, pattern, index);
1848 [ # # ]: 0 : if (pat[0].type == RTE_FLOW_ITEM_TYPE_END) {
1849 : 0 : rss_conf->pattern_type = 0;
1850 : 0 : ret = hns3_flow_parse_hash_global_conf(dev, rss_act,
1851 : : rss_conf, error);
1852 : : } else {
1853 : 0 : ret = hns3_flow_parse_pattern_act(dev, pat, rss_act,
1854 : : rss_conf, error);
1855 : : }
1856 [ # # ]: 0 : if (ret != 0)
1857 : : return ret;
1858 : :
1859 [ # # ]: 0 : if (hns3_rss_action_is_dup(hw, rss_conf))
1860 : 0 : return rte_flow_error_set(error, EINVAL,
1861 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1862 : : act, "duplicate RSS rule");
1863 : :
1864 : : return 0;
1865 : : }
1866 : :
1867 : : static int
1868 : 0 : hns3_update_indir_table(struct hns3_hw *hw,
1869 : : const struct rte_flow_action_rss *conf, uint16_t num)
1870 : : {
1871 : : uint16_t indir_tbl[HNS3_RSS_IND_TBL_SIZE_MAX];
1872 : : uint16_t j;
1873 : : uint32_t i;
1874 : :
1875 : : /* Fill in redirection table */
1876 [ # # ]: 0 : for (i = 0, j = 0; i < hw->rss_ind_tbl_size; i++, j++) {
1877 : 0 : j %= num;
1878 [ # # ]: 0 : if (conf->queue[j] >= hw->alloc_rss_size) {
1879 : 0 : hns3_err(hw, "queue id(%u) set to redirection table "
1880 : : "exceeds queue number(%u) allocated to a TC.",
1881 : : conf->queue[j], hw->alloc_rss_size);
1882 : 0 : return -EINVAL;
1883 : : }
1884 : 0 : indir_tbl[i] = conf->queue[j];
1885 : : }
1886 : :
1887 : 0 : return hns3_set_rss_indir_table(hw, indir_tbl, hw->rss_ind_tbl_size);
1888 : : }
1889 : :
1890 : : static uint64_t
1891 : : hns3_flow_get_pctype_tuple_mask(uint64_t hw_pctype)
1892 : : {
1893 : : uint64_t tuple_mask = 0;
1894 : : uint16_t i;
1895 : :
1896 [ # # ]: 0 : for (i = 0; i < RTE_DIM(hash_map_table); i++) {
1897 [ # # ]: 0 : if (hw_pctype == hash_map_table[i].hw_pctype) {
1898 : 0 : tuple_mask = hash_map_table[i].tuple_mask;
1899 : 0 : break;
1900 : : }
1901 : : }
1902 : :
1903 : : return tuple_mask;
1904 : : }
1905 : :
1906 : : static int
1907 : 0 : hns3_flow_set_rss_ptype_tuple(struct hns3_hw *hw,
1908 : : struct hns3_flow_rss_conf *rss_conf)
1909 : : {
1910 : : uint64_t old_tuple_fields, new_tuple_fields;
1911 : : uint64_t hw_pctypes, tuples, tuple_mask = 0;
1912 : : bool cfg_global_tuple;
1913 : : int ret;
1914 : :
1915 : 0 : cfg_global_tuple = (rss_conf->pattern_type == 0);
1916 [ # # ]: 0 : if (!cfg_global_tuple) {
1917 : : /*
1918 : : * To ensure that different packets do not affect each other,
1919 : : * we have to first read all tuple fields, and then only modify
1920 : : * the tuples for the specified packet type.
1921 : : */
1922 : 0 : ret = hns3_get_rss_tuple_field(hw, &old_tuple_fields);
1923 [ # # ]: 0 : if (ret != 0)
1924 : : return ret;
1925 : :
1926 : 0 : new_tuple_fields = old_tuple_fields;
1927 : 0 : hw_pctypes = rss_conf->hw_pctypes;
1928 [ # # ]: 0 : while (hw_pctypes > 0) {
1929 : : uint32_t idx = rte_bsf64(hw_pctypes);
1930 : 0 : uint64_t pctype = BIT_ULL(idx);
1931 : :
1932 : : tuple_mask = hns3_flow_get_pctype_tuple_mask(pctype);
1933 : 0 : tuples = hns3_rss_calc_tuple_filed(rss_conf->conf.types);
1934 : 0 : new_tuple_fields &= ~tuple_mask;
1935 : 0 : new_tuple_fields |= tuples;
1936 : 0 : hw_pctypes &= ~pctype;
1937 : : }
1938 : : } else {
1939 : : new_tuple_fields =
1940 : 0 : hns3_rss_calc_tuple_filed(rss_conf->conf.types);
1941 : : }
1942 : :
1943 : 0 : ret = hns3_set_rss_tuple_field(hw, new_tuple_fields);
1944 [ # # ]: 0 : if (ret != 0)
1945 : : return ret;
1946 : :
1947 [ # # ]: 0 : if (!cfg_global_tuple)
1948 : 0 : hns3_info(hw, "RSS tuple fields changed from 0x%" PRIx64 " to 0x%" PRIx64,
1949 : : old_tuple_fields, new_tuple_fields);
1950 : :
1951 : : return 0;
1952 : : }
1953 : :
1954 : : static int
1955 : 0 : hns3_config_rss_filter(struct hns3_hw *hw,
1956 : : struct hns3_flow_rss_conf *rss_conf)
1957 : : {
1958 : : struct rte_flow_action_rss *rss_act;
1959 : : int ret;
1960 : :
1961 : 0 : rss_act = &rss_conf->conf;
1962 [ # # ]: 0 : if (rss_act->queue_num > 0) {
1963 : 0 : ret = hns3_update_indir_table(hw, rss_act, rss_act->queue_num);
1964 [ # # ]: 0 : if (ret) {
1965 : 0 : hns3_err(hw, "set queues action failed, ret = %d", ret);
1966 : 0 : return ret;
1967 : : }
1968 : : }
1969 : :
1970 [ # # ]: 0 : if (rss_act->key_len > 0 ||
1971 [ # # ]: 0 : rss_act->func != RTE_ETH_HASH_FUNCTION_DEFAULT) {
1972 : 0 : ret = hns3_update_rss_algo_key(hw, rss_act->func, rss_conf->key,
1973 : : rss_act->key_len);
1974 [ # # ]: 0 : if (ret != 0) {
1975 : 0 : hns3_err(hw, "set func or hash key action failed, ret = %d",
1976 : : ret);
1977 : 0 : return ret;
1978 : : }
1979 : : }
1980 : :
1981 [ # # ]: 0 : if (rss_conf->hw_pctypes > 0) {
1982 : 0 : ret = hns3_flow_set_rss_ptype_tuple(hw, rss_conf);
1983 [ # # ]: 0 : if (ret != 0) {
1984 : 0 : hns3_err(hw, "set types action failed, ret = %d", ret);
1985 : 0 : return ret;
1986 : : }
1987 : : }
1988 : :
1989 : : return 0;
1990 : : }
1991 : :
1992 : : static int
1993 : 0 : hns3_clear_rss_filter(struct rte_eth_dev *dev)
1994 : : {
1995 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
1996 : : struct hns3_rss_conf_ele *rss_filter_ptr;
1997 : : struct hns3_hw *hw = &hns->hw;
1998 : :
1999 : 0 : rss_filter_ptr = TAILQ_FIRST(&hw->flow_rss_list);
2000 [ # # ]: 0 : while (rss_filter_ptr) {
2001 [ # # ]: 0 : TAILQ_REMOVE(&hw->flow_rss_list, rss_filter_ptr, entries);
2002 : 0 : rte_free(rss_filter_ptr);
2003 : 0 : rss_filter_ptr = TAILQ_FIRST(&hw->flow_rss_list);
2004 : : }
2005 : :
2006 : 0 : return hns3_config_rss(hns);
2007 : : }
2008 : :
2009 : : static int
2010 : 0 : hns3_reconfig_all_rss_filter(struct hns3_hw *hw)
2011 : : {
2012 : : struct hns3_rss_conf_ele *filter;
2013 : : uint32_t rule_no = 0;
2014 : : int ret;
2015 : :
2016 [ # # ]: 0 : TAILQ_FOREACH(filter, &hw->flow_rss_list, entries) {
2017 : 0 : ret = hns3_config_rss_filter(hw, &filter->filter_info);
2018 [ # # ]: 0 : if (ret != 0) {
2019 : 0 : hns3_err(hw, "config %uth RSS filter failed, ret = %d",
2020 : : rule_no, ret);
2021 : 0 : return ret;
2022 : : }
2023 : 0 : rule_no++;
2024 : : }
2025 : :
2026 : : return 0;
2027 : : }
2028 : :
2029 : : static int
2030 : 0 : hns3_restore_rss_filter(struct hns3_hw *hw)
2031 : : {
2032 : : int ret;
2033 : :
2034 : 0 : pthread_mutex_lock(&hw->flows_lock);
2035 : 0 : ret = hns3_reconfig_all_rss_filter(hw);
2036 : 0 : pthread_mutex_unlock(&hw->flows_lock);
2037 : :
2038 : 0 : return ret;
2039 : : }
2040 : :
2041 : : int
2042 : 0 : hns3_restore_filter(struct hns3_adapter *hns)
2043 : : {
2044 : 0 : struct hns3_hw *hw = &hns->hw;
2045 : : int ret;
2046 : :
2047 : 0 : ret = hns3_restore_all_fdir_filter(hns);
2048 [ # # ]: 0 : if (ret != 0)
2049 : : return ret;
2050 : :
2051 : 0 : return hns3_restore_rss_filter(hw);
2052 : : }
2053 : :
2054 : : static int
2055 : 0 : hns3_flow_args_check(const struct rte_flow_attr *attr,
2056 : : const struct rte_flow_item pattern[],
2057 : : const struct rte_flow_action actions[],
2058 : : struct rte_flow_error *error)
2059 : : {
2060 [ # # ]: 0 : if (pattern == NULL)
2061 : 0 : return rte_flow_error_set(error, EINVAL,
2062 : : RTE_FLOW_ERROR_TYPE_ITEM_NUM,
2063 : : NULL, "NULL pattern.");
2064 : :
2065 [ # # ]: 0 : if (actions == NULL)
2066 : 0 : return rte_flow_error_set(error, EINVAL,
2067 : : RTE_FLOW_ERROR_TYPE_ACTION_NUM,
2068 : : NULL, "NULL action.");
2069 : :
2070 [ # # ]: 0 : if (attr == NULL)
2071 : 0 : return rte_flow_error_set(error, EINVAL,
2072 : : RTE_FLOW_ERROR_TYPE_ATTR,
2073 : : NULL, "NULL attribute.");
2074 : :
2075 : 0 : return hns3_check_attr(attr, error);
2076 : : }
2077 : :
2078 : : /*
2079 : : * Check if the flow rule is supported by hns3.
2080 : : * It only checks the format. Don't guarantee the rule can be programmed into
2081 : : * the HW. Because there can be no enough room for the rule.
2082 : : */
2083 : : static int
2084 : 0 : hns3_flow_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
2085 : : const struct rte_flow_item pattern[],
2086 : : const struct rte_flow_action actions[],
2087 : : struct rte_flow_error *error,
2088 : : struct hns3_filter_info *filter_info)
2089 : : {
2090 : : union hns3_filter_conf *conf;
2091 : : int ret;
2092 : :
2093 : 0 : ret = hns3_flow_args_check(attr, pattern, actions, error);
2094 [ # # ]: 0 : if (ret)
2095 : : return ret;
2096 : :
2097 : 0 : hns3_parse_filter_type(pattern, actions, filter_info);
2098 : : conf = &filter_info->conf;
2099 [ # # ]: 0 : if (filter_info->type == RTE_ETH_FILTER_HASH)
2100 : 0 : return hns3_parse_rss_filter(dev, pattern, actions,
2101 : : &conf->rss_conf, error);
2102 : :
2103 : 0 : return hns3_parse_fdir_filter(dev, pattern, actions,
2104 : : &conf->fdir_conf, error);
2105 : : }
2106 : :
2107 : : static int
2108 : 0 : hns3_flow_rebuild_all_rss_filter(struct hns3_adapter *hns)
2109 : : {
2110 : 0 : struct hns3_hw *hw = &hns->hw;
2111 : : int ret;
2112 : :
2113 : 0 : ret = hns3_config_rss(hns);
2114 [ # # ]: 0 : if (ret != 0) {
2115 : 0 : hns3_err(hw, "restore original RSS configuration failed, ret = %d.",
2116 : : ret);
2117 : 0 : return ret;
2118 : : }
2119 : 0 : ret = hns3_reconfig_all_rss_filter(hw);
2120 [ # # ]: 0 : if (ret != 0)
2121 : 0 : hns3_err(hw, "rebuild all RSS filter failed, ret = %d.", ret);
2122 : :
2123 : : return ret;
2124 : : }
2125 : :
2126 : : static int
2127 : 0 : hns3_flow_create_rss_rule(struct rte_eth_dev *dev,
2128 : : struct hns3_flow_rss_conf *rss_conf,
2129 : : struct rte_flow *flow)
2130 : : {
2131 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2132 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2133 : : struct hns3_rss_conf_ele *rss_filter_ptr;
2134 : : struct hns3_flow_rss_conf *new_conf;
2135 : : struct rte_flow_action_rss *rss_act;
2136 : : int ret;
2137 : :
2138 : 0 : rss_filter_ptr = rte_zmalloc("hns3 rss filter",
2139 : : sizeof(struct hns3_rss_conf_ele), 0);
2140 [ # # ]: 0 : if (rss_filter_ptr == NULL) {
2141 : 0 : hns3_err(hw, "failed to allocate hns3_rss_filter memory");
2142 : 0 : return -ENOMEM;
2143 : : }
2144 : :
2145 [ # # ]: 0 : new_conf = &rss_filter_ptr->filter_info;
2146 : : memcpy(new_conf, rss_conf, sizeof(*new_conf));
2147 : : rss_act = &new_conf->conf;
2148 [ # # ]: 0 : if (rss_act->queue_num > 0)
2149 : 0 : new_conf->conf.queue = new_conf->queue;
2150 : : /*
2151 : : * There are two ways to deliver hash key action:
2152 : : * 1> 'key_len' is greater than zero and 'key' isn't NULL.
2153 : : * 2> 'key_len' is greater than zero, but 'key' is NULL.
2154 : : * For case 2, we need to keep 'key' of the new_conf is NULL so as to
2155 : : * inherit the configuration from user in case of failing to verify
2156 : : * duplicate rule later.
2157 : : */
2158 [ # # # # ]: 0 : if (rss_act->key_len > 0 && rss_act->key != NULL)
2159 : 0 : new_conf->conf.key = new_conf->key;
2160 : :
2161 : 0 : ret = hns3_config_rss_filter(hw, new_conf);
2162 [ # # ]: 0 : if (ret != 0) {
2163 : 0 : rte_free(rss_filter_ptr);
2164 : 0 : (void)hns3_flow_rebuild_all_rss_filter(hns);
2165 : 0 : return ret;
2166 : : }
2167 : :
2168 : 0 : TAILQ_INSERT_TAIL(&hw->flow_rss_list, rss_filter_ptr, entries);
2169 : 0 : flow->rule = rss_filter_ptr;
2170 : 0 : flow->filter_type = RTE_ETH_FILTER_HASH;
2171 : :
2172 : 0 : return 0;
2173 : : }
2174 : :
2175 : : static int
2176 : 0 : hns3_flow_create_fdir_rule(struct rte_eth_dev *dev,
2177 : : struct hns3_fdir_rule *fdir_rule,
2178 : : struct rte_flow_error *error,
2179 : : struct rte_flow *flow)
2180 : : {
2181 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2182 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2183 : : struct hns3_fdir_rule_ele *fdir_rule_ptr;
2184 : : bool indir;
2185 : : int ret;
2186 : :
2187 : 0 : indir = !!(fdir_rule->flags & HNS3_RULE_FLAG_COUNTER_INDIR);
2188 [ # # ]: 0 : if (fdir_rule->flags & HNS3_RULE_FLAG_COUNTER) {
2189 : 0 : ret = hns3_counter_new(dev, indir, fdir_rule->act_cnt.id,
2190 : : error);
2191 [ # # ]: 0 : if (ret != 0)
2192 : : return ret;
2193 : :
2194 : 0 : flow->counter_id = fdir_rule->act_cnt.id;
2195 : : }
2196 : :
2197 : 0 : fdir_rule_ptr = rte_zmalloc("hns3 fdir rule",
2198 : : sizeof(struct hns3_fdir_rule_ele), 0);
2199 [ # # ]: 0 : if (fdir_rule_ptr == NULL) {
2200 : 0 : hns3_err(hw, "failed to allocate fdir_rule memory.");
2201 : : ret = -ENOMEM;
2202 : 0 : goto err_malloc;
2203 : : }
2204 : :
2205 : : /*
2206 : : * After all the preceding tasks are successfully configured, configure
2207 : : * rules to the hardware to simplify the rollback of rules in the
2208 : : * hardware.
2209 : : */
2210 : 0 : ret = hns3_fdir_filter_program(hns, fdir_rule, false);
2211 [ # # ]: 0 : if (ret != 0)
2212 : 0 : goto err_fdir_filter;
2213 : :
2214 : 0 : memcpy(&fdir_rule_ptr->fdir_conf, fdir_rule,
2215 : : sizeof(struct hns3_fdir_rule));
2216 : 0 : TAILQ_INSERT_TAIL(&hw->flow_fdir_list, fdir_rule_ptr, entries);
2217 : 0 : flow->rule = fdir_rule_ptr;
2218 : 0 : flow->filter_type = RTE_ETH_FILTER_FDIR;
2219 : :
2220 : 0 : return 0;
2221 : :
2222 : : err_fdir_filter:
2223 : 0 : rte_free(fdir_rule_ptr);
2224 : 0 : err_malloc:
2225 [ # # ]: 0 : if (fdir_rule->flags & HNS3_RULE_FLAG_COUNTER)
2226 : 0 : hns3_counter_release(dev, fdir_rule->act_cnt.id);
2227 : :
2228 : : return ret;
2229 : : }
2230 : :
2231 : : /*
2232 : : * Create or destroy a flow rule.
2233 : : * Theorically one rule can match more than one filters.
2234 : : * We will let it use the filter which it hit first.
2235 : : * So, the sequence matters.
2236 : : */
2237 : : static struct rte_flow *
2238 : 0 : hns3_flow_create(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
2239 : : const struct rte_flow_item pattern[],
2240 : : const struct rte_flow_action actions[],
2241 : : struct rte_flow_error *error)
2242 : : {
2243 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
2244 : 0 : struct hns3_filter_info filter_info = {0};
2245 : : struct hns3_flow_mem *flow_node;
2246 : : struct hns3_hw *hw = &hns->hw;
2247 : : union hns3_filter_conf *conf;
2248 : : struct rte_flow *flow;
2249 : : int ret;
2250 : :
2251 : 0 : ret = hns3_flow_validate(dev, attr, pattern, actions, error,
2252 : : &filter_info);
2253 [ # # ]: 0 : if (ret)
2254 : : return NULL;
2255 : :
2256 : 0 : flow = rte_zmalloc("hns3 flow", sizeof(struct rte_flow), 0);
2257 [ # # ]: 0 : if (flow == NULL) {
2258 : 0 : rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
2259 : : NULL, "Failed to allocate flow memory");
2260 : 0 : return NULL;
2261 : : }
2262 : 0 : flow_node = rte_zmalloc("hns3 flow node",
2263 : : sizeof(struct hns3_flow_mem), 0);
2264 [ # # ]: 0 : if (flow_node == NULL) {
2265 : 0 : rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
2266 : : NULL, "Failed to allocate flow list memory");
2267 : 0 : rte_free(flow);
2268 : 0 : return NULL;
2269 : : }
2270 : :
2271 : 0 : flow_node->flow = flow;
2272 : : conf = &filter_info.conf;
2273 : 0 : TAILQ_INSERT_TAIL(&hw->flow_list, flow_node, entries);
2274 [ # # ]: 0 : if (filter_info.type == RTE_ETH_FILTER_HASH)
2275 : 0 : ret = hns3_flow_create_rss_rule(dev, &conf->rss_conf, flow);
2276 : : else
2277 : 0 : ret = hns3_flow_create_fdir_rule(dev, &conf->fdir_conf,
2278 : : error, flow);
2279 [ # # ]: 0 : if (ret == 0)
2280 : : return flow;
2281 : :
2282 : 0 : rte_flow_error_set(error, -ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2283 : : "Failed to create flow");
2284 [ # # ]: 0 : TAILQ_REMOVE(&hw->flow_list, flow_node, entries);
2285 : 0 : rte_free(flow_node);
2286 : 0 : rte_free(flow);
2287 : :
2288 : 0 : return NULL;
2289 : : }
2290 : :
2291 : : /* Destroy a flow rule on hns3. */
2292 : : static int
2293 : 0 : hns3_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
2294 : : struct rte_flow_error *error)
2295 : : {
2296 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
2297 : : struct hns3_fdir_rule_ele *fdir_rule_ptr;
2298 : : struct hns3_rss_conf_ele *rss_filter_ptr;
2299 : : struct hns3_flow_mem *flow_node;
2300 : : enum rte_filter_type filter_type;
2301 : : struct hns3_fdir_rule fdir_rule;
2302 : : struct hns3_hw *hw = &hns->hw;
2303 : : int ret;
2304 : :
2305 [ # # ]: 0 : if (flow == NULL)
2306 : 0 : return rte_flow_error_set(error, EINVAL,
2307 : : RTE_FLOW_ERROR_TYPE_HANDLE,
2308 : : flow, "Flow is NULL");
2309 : :
2310 : 0 : filter_type = flow->filter_type;
2311 [ # # # ]: 0 : switch (filter_type) {
2312 : 0 : case RTE_ETH_FILTER_FDIR:
2313 : 0 : fdir_rule_ptr = (struct hns3_fdir_rule_ele *)flow->rule;
2314 : 0 : memcpy(&fdir_rule, &fdir_rule_ptr->fdir_conf,
2315 : : sizeof(struct hns3_fdir_rule));
2316 : :
2317 : 0 : ret = hns3_fdir_filter_program(hns, &fdir_rule, true);
2318 [ # # ]: 0 : if (ret)
2319 : 0 : return rte_flow_error_set(error, EIO,
2320 : : RTE_FLOW_ERROR_TYPE_HANDLE,
2321 : : flow,
2322 : : "Destroy FDIR fail.Try again");
2323 [ # # ]: 0 : if (fdir_rule.flags & HNS3_RULE_FLAG_COUNTER)
2324 : 0 : hns3_counter_release(dev, fdir_rule.act_cnt.id);
2325 [ # # ]: 0 : TAILQ_REMOVE(&hw->flow_fdir_list, fdir_rule_ptr, entries);
2326 : 0 : rte_free(fdir_rule_ptr);
2327 : : fdir_rule_ptr = NULL;
2328 : 0 : break;
2329 : 0 : case RTE_ETH_FILTER_HASH:
2330 : 0 : rss_filter_ptr = (struct hns3_rss_conf_ele *)flow->rule;
2331 [ # # ]: 0 : TAILQ_REMOVE(&hw->flow_rss_list, rss_filter_ptr, entries);
2332 : 0 : rte_free(rss_filter_ptr);
2333 : : rss_filter_ptr = NULL;
2334 : 0 : (void)hns3_flow_rebuild_all_rss_filter(hns);
2335 : 0 : break;
2336 : 0 : default:
2337 : 0 : return rte_flow_error_set(error, EINVAL,
2338 : : RTE_FLOW_ERROR_TYPE_HANDLE, flow,
2339 : : "Unsupported filter type");
2340 : : }
2341 : :
2342 [ # # ]: 0 : TAILQ_FOREACH(flow_node, &hw->flow_list, entries) {
2343 [ # # ]: 0 : if (flow_node->flow == flow) {
2344 [ # # ]: 0 : TAILQ_REMOVE(&hw->flow_list, flow_node, entries);
2345 : 0 : rte_free(flow_node);
2346 : : flow_node = NULL;
2347 : 0 : break;
2348 : : }
2349 : : }
2350 : 0 : rte_free(flow);
2351 : :
2352 : 0 : return 0;
2353 : : }
2354 : :
2355 : : /* Destroy all flow rules associated with a port on hns3. */
2356 : : static int
2357 : 0 : hns3_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
2358 : : {
2359 : 0 : struct hns3_adapter *hns = dev->data->dev_private;
2360 : : int ret;
2361 : :
2362 : : /* FDIR is available only in PF driver */
2363 [ # # ]: 0 : if (!hns->is_vf) {
2364 : 0 : ret = hns3_clear_all_fdir_filter(hns);
2365 [ # # ]: 0 : if (ret) {
2366 : 0 : rte_flow_error_set(error, ret,
2367 : : RTE_FLOW_ERROR_TYPE_HANDLE,
2368 : : NULL, "Failed to flush rule");
2369 : 0 : return ret;
2370 : : }
2371 : 0 : hns3_counter_flush(dev);
2372 : : }
2373 : :
2374 : 0 : ret = hns3_clear_rss_filter(dev);
2375 [ # # ]: 0 : if (ret) {
2376 : 0 : rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
2377 : : NULL, "Failed to flush rss filter");
2378 : 0 : return ret;
2379 : : }
2380 : :
2381 : 0 : hns3_filterlist_flush(dev);
2382 : :
2383 : 0 : return 0;
2384 : : }
2385 : :
2386 : : /* Query an existing flow rule. */
2387 : : static int
2388 : 0 : hns3_flow_query(struct rte_eth_dev *dev, struct rte_flow *flow,
2389 : : const struct rte_flow_action *actions, void *data,
2390 : : struct rte_flow_error *error)
2391 : : {
2392 : : struct rte_flow_action_rss *rss_conf;
2393 : : struct hns3_rss_conf_ele *rss_rule;
2394 : : struct rte_flow_query_count *qc;
2395 : : int ret;
2396 : :
2397 [ # # ]: 0 : if (!flow->rule)
2398 : 0 : return rte_flow_error_set(error, EINVAL,
2399 : : RTE_FLOW_ERROR_TYPE_HANDLE, NULL, "invalid rule");
2400 : :
2401 [ # # ]: 0 : for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2402 [ # # # # ]: 0 : switch (actions->type) {
2403 : : case RTE_FLOW_ACTION_TYPE_VOID:
2404 : : break;
2405 : 0 : case RTE_FLOW_ACTION_TYPE_COUNT:
2406 : : qc = (struct rte_flow_query_count *)data;
2407 : 0 : ret = hns3_counter_query(dev, flow, qc, error);
2408 [ # # ]: 0 : if (ret)
2409 : 0 : return ret;
2410 : : break;
2411 : 0 : case RTE_FLOW_ACTION_TYPE_RSS:
2412 [ # # ]: 0 : if (flow->filter_type != RTE_ETH_FILTER_HASH) {
2413 : 0 : return rte_flow_error_set(error, ENOTSUP,
2414 : : RTE_FLOW_ERROR_TYPE_ACTION,
2415 : : actions, "action is not supported");
2416 : : }
2417 : : rss_conf = (struct rte_flow_action_rss *)data;
2418 : 0 : rss_rule = (struct hns3_rss_conf_ele *)flow->rule;
2419 [ # # ]: 0 : rte_memcpy(rss_conf, &rss_rule->filter_info.conf,
2420 : : sizeof(struct rte_flow_action_rss));
2421 : : break;
2422 : 0 : default:
2423 : 0 : return rte_flow_error_set(error, ENOTSUP,
2424 : : RTE_FLOW_ERROR_TYPE_ACTION,
2425 : : actions, "action is not supported");
2426 : : }
2427 : : }
2428 : :
2429 : : return 0;
2430 : : }
2431 : :
2432 : : static int
2433 : 0 : hns3_flow_validate_wrap(struct rte_eth_dev *dev,
2434 : : const struct rte_flow_attr *attr,
2435 : : const struct rte_flow_item pattern[],
2436 : : const struct rte_flow_action actions[],
2437 : : struct rte_flow_error *error)
2438 : : {
2439 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2440 : 0 : struct hns3_filter_info filter_info = {0};
2441 : : int ret;
2442 : :
2443 : 0 : pthread_mutex_lock(&hw->flows_lock);
2444 : 0 : ret = hns3_flow_validate(dev, attr, pattern, actions, error,
2445 : : &filter_info);
2446 : 0 : pthread_mutex_unlock(&hw->flows_lock);
2447 : :
2448 : 0 : return ret;
2449 : : }
2450 : :
2451 : : static struct rte_flow *
2452 : 0 : hns3_flow_create_wrap(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
2453 : : const struct rte_flow_item pattern[],
2454 : : const struct rte_flow_action actions[],
2455 : : struct rte_flow_error *error)
2456 : : {
2457 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2458 : : struct rte_flow *flow;
2459 : :
2460 : 0 : pthread_mutex_lock(&hw->flows_lock);
2461 : 0 : flow = hns3_flow_create(dev, attr, pattern, actions, error);
2462 : 0 : pthread_mutex_unlock(&hw->flows_lock);
2463 : :
2464 : 0 : return flow;
2465 : : }
2466 : :
2467 : : static int
2468 : 0 : hns3_flow_destroy_wrap(struct rte_eth_dev *dev, struct rte_flow *flow,
2469 : : struct rte_flow_error *error)
2470 : : {
2471 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2472 : : int ret;
2473 : :
2474 : 0 : pthread_mutex_lock(&hw->flows_lock);
2475 : 0 : ret = hns3_flow_destroy(dev, flow, error);
2476 : 0 : pthread_mutex_unlock(&hw->flows_lock);
2477 : :
2478 : 0 : return ret;
2479 : : }
2480 : :
2481 : : static int
2482 : 0 : hns3_flow_flush_wrap(struct rte_eth_dev *dev, struct rte_flow_error *error)
2483 : : {
2484 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2485 : : int ret;
2486 : :
2487 : 0 : pthread_mutex_lock(&hw->flows_lock);
2488 : 0 : ret = hns3_flow_flush(dev, error);
2489 : 0 : pthread_mutex_unlock(&hw->flows_lock);
2490 : :
2491 : 0 : return ret;
2492 : : }
2493 : :
2494 : : static int
2495 : 0 : hns3_flow_query_wrap(struct rte_eth_dev *dev, struct rte_flow *flow,
2496 : : const struct rte_flow_action *actions, void *data,
2497 : : struct rte_flow_error *error)
2498 : : {
2499 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2500 : : int ret;
2501 : :
2502 : 0 : pthread_mutex_lock(&hw->flows_lock);
2503 : 0 : ret = hns3_flow_query(dev, flow, actions, data, error);
2504 : 0 : pthread_mutex_unlock(&hw->flows_lock);
2505 : :
2506 : 0 : return ret;
2507 : : }
2508 : :
2509 : : static int
2510 : 0 : hns3_check_indir_action(const struct rte_flow_indir_action_conf *conf,
2511 : : const struct rte_flow_action *action,
2512 : : struct rte_flow_error *error)
2513 : : {
2514 [ # # ]: 0 : if (!conf->ingress)
2515 : 0 : return rte_flow_error_set(error, EINVAL,
2516 : : RTE_FLOW_ERROR_TYPE_ACTION,
2517 : : NULL, "Indir action ingress can't be zero");
2518 : :
2519 [ # # ]: 0 : if (conf->egress)
2520 : 0 : return rte_flow_error_set(error, EINVAL,
2521 : : RTE_FLOW_ERROR_TYPE_ACTION,
2522 : : NULL, "Indir action not support egress");
2523 : :
2524 [ # # ]: 0 : if (conf->transfer)
2525 : 0 : return rte_flow_error_set(error, EINVAL,
2526 : : RTE_FLOW_ERROR_TYPE_ACTION,
2527 : : NULL, "Indir action not support transfer");
2528 : :
2529 [ # # ]: 0 : if (action->type != RTE_FLOW_ACTION_TYPE_COUNT)
2530 : 0 : return rte_flow_error_set(error, EINVAL,
2531 : : RTE_FLOW_ERROR_TYPE_ACTION,
2532 : : NULL, "Indir action only support count");
2533 : :
2534 : : return 0;
2535 : : }
2536 : :
2537 : : static struct rte_flow_action_handle *
2538 : 0 : hns3_flow_action_create(struct rte_eth_dev *dev,
2539 : : const struct rte_flow_indir_action_conf *conf,
2540 : : const struct rte_flow_action *action,
2541 : : struct rte_flow_error *error)
2542 : : {
2543 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2544 : : struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2545 : : const struct rte_flow_action_count *act_count;
2546 : : struct rte_flow_action_handle *handle = NULL;
2547 : : struct hns3_flow_counter *counter;
2548 : :
2549 [ # # ]: 0 : if (hns3_check_indir_action(conf, action, error))
2550 : : return NULL;
2551 : :
2552 : 0 : handle = rte_zmalloc("hns3 action handle",
2553 : : sizeof(struct rte_flow_action_handle), 0);
2554 [ # # ]: 0 : if (handle == NULL) {
2555 : 0 : rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
2556 : : NULL, "Failed to allocate action memory");
2557 : 0 : return NULL;
2558 : : }
2559 : :
2560 : 0 : pthread_mutex_lock(&hw->flows_lock);
2561 : :
2562 : 0 : act_count = (const struct rte_flow_action_count *)action->conf;
2563 [ # # ]: 0 : if (act_count->id >= pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_1]) {
2564 : 0 : rte_flow_error_set(error, EINVAL,
2565 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2566 : : action, "Invalid counter id");
2567 : 0 : goto err_exit;
2568 : : }
2569 : :
2570 [ # # ]: 0 : if (hns3_counter_new(dev, false, act_count->id, error))
2571 : 0 : goto err_exit;
2572 : :
2573 : 0 : counter = hns3_counter_lookup(dev, act_count->id);
2574 [ # # ]: 0 : if (counter == NULL) {
2575 : 0 : rte_flow_error_set(error, EINVAL,
2576 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2577 : : action, "Counter id not found");
2578 : 0 : goto err_exit;
2579 : : }
2580 : :
2581 : 0 : counter->indirect = true;
2582 : 0 : handle->indirect_type = HNS3_INDIRECT_ACTION_TYPE_COUNT;
2583 : 0 : handle->counter_id = counter->id;
2584 : :
2585 : 0 : pthread_mutex_unlock(&hw->flows_lock);
2586 : 0 : return handle;
2587 : :
2588 : 0 : err_exit:
2589 : 0 : pthread_mutex_unlock(&hw->flows_lock);
2590 : 0 : rte_free(handle);
2591 : 0 : return NULL;
2592 : : }
2593 : :
2594 : : static int
2595 : 0 : hns3_flow_action_destroy(struct rte_eth_dev *dev,
2596 : : struct rte_flow_action_handle *handle,
2597 : : struct rte_flow_error *error)
2598 : : {
2599 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2600 : : struct hns3_flow_counter *counter;
2601 : :
2602 : 0 : pthread_mutex_lock(&hw->flows_lock);
2603 : :
2604 [ # # ]: 0 : if (handle->indirect_type != HNS3_INDIRECT_ACTION_TYPE_COUNT) {
2605 : 0 : pthread_mutex_unlock(&hw->flows_lock);
2606 : 0 : return rte_flow_error_set(error, EINVAL,
2607 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2608 : : handle, "Invalid indirect type");
2609 : : }
2610 : :
2611 : 0 : counter = hns3_counter_lookup(dev, handle->counter_id);
2612 [ # # ]: 0 : if (counter == NULL) {
2613 : 0 : pthread_mutex_unlock(&hw->flows_lock);
2614 : 0 : return rte_flow_error_set(error, EINVAL,
2615 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2616 : : handle, "Counter id not exist");
2617 : : }
2618 : :
2619 [ # # ]: 0 : if (counter->ref_cnt > 1) {
2620 : 0 : pthread_mutex_unlock(&hw->flows_lock);
2621 : 0 : return rte_flow_error_set(error, EBUSY,
2622 : : RTE_FLOW_ERROR_TYPE_HANDLE,
2623 : : handle, "Counter id in use");
2624 : : }
2625 : :
2626 : 0 : (void)hns3_counter_release(dev, handle->counter_id);
2627 : 0 : rte_free(handle);
2628 : :
2629 : 0 : pthread_mutex_unlock(&hw->flows_lock);
2630 : 0 : return 0;
2631 : : }
2632 : :
2633 : : static int
2634 : 0 : hns3_flow_action_query(struct rte_eth_dev *dev,
2635 : : const struct rte_flow_action_handle *handle,
2636 : : void *data,
2637 : : struct rte_flow_error *error)
2638 : : {
2639 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2640 : : struct rte_flow flow;
2641 : : int ret;
2642 : :
2643 : 0 : pthread_mutex_lock(&hw->flows_lock);
2644 : :
2645 [ # # ]: 0 : if (handle->indirect_type != HNS3_INDIRECT_ACTION_TYPE_COUNT) {
2646 : 0 : pthread_mutex_unlock(&hw->flows_lock);
2647 : 0 : return rte_flow_error_set(error, EINVAL,
2648 : : RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2649 : : handle, "Invalid indirect type");
2650 : : }
2651 : :
2652 : : memset(&flow, 0, sizeof(flow));
2653 : 0 : flow.counter_id = handle->counter_id;
2654 : 0 : ret = hns3_counter_query(dev, &flow,
2655 : : (struct rte_flow_query_count *)data, error);
2656 : 0 : pthread_mutex_unlock(&hw->flows_lock);
2657 : 0 : return ret;
2658 : : }
2659 : :
2660 : : static const struct rte_flow_ops hns3_flow_ops = {
2661 : : .validate = hns3_flow_validate_wrap,
2662 : : .create = hns3_flow_create_wrap,
2663 : : .destroy = hns3_flow_destroy_wrap,
2664 : : .flush = hns3_flow_flush_wrap,
2665 : : .query = hns3_flow_query_wrap,
2666 : : .isolate = NULL,
2667 : : .action_handle_create = hns3_flow_action_create,
2668 : : .action_handle_destroy = hns3_flow_action_destroy,
2669 : : .action_handle_query = hns3_flow_action_query,
2670 : : };
2671 : :
2672 : : int
2673 : 0 : hns3_dev_flow_ops_get(struct rte_eth_dev *dev,
2674 : : const struct rte_flow_ops **ops)
2675 : : {
2676 : : struct hns3_hw *hw;
2677 : :
2678 : 0 : hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2679 [ # # ]: 0 : if (hw->adapter_state >= HNS3_NIC_CLOSED)
2680 : : return -ENODEV;
2681 : :
2682 : 0 : *ops = &hns3_flow_ops;
2683 : 0 : return 0;
2684 : : }
2685 : :
2686 : : void
2687 : 0 : hns3_flow_init(struct rte_eth_dev *dev)
2688 : : {
2689 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2690 : : pthread_mutexattr_t attr;
2691 : :
2692 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2693 : 0 : return;
2694 : :
2695 : 0 : pthread_mutexattr_init(&attr);
2696 : 0 : pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
2697 : 0 : pthread_mutex_init(&hw->flows_lock, &attr);
2698 : 0 : dev->data->dev_flags |= RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE;
2699 : :
2700 : 0 : TAILQ_INIT(&hw->flow_fdir_list);
2701 : 0 : TAILQ_INIT(&hw->flow_rss_list);
2702 : 0 : TAILQ_INIT(&hw->flow_list);
2703 : : }
2704 : :
2705 : : void
2706 : 0 : hns3_flow_uninit(struct rte_eth_dev *dev)
2707 : : {
2708 : : struct rte_flow_error error;
2709 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2710 : 0 : hns3_flow_flush_wrap(dev, &error);
2711 : 0 : }
|