1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2008-2017 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
6 #include <rte_ethdev_driver.h>
7 #include <rte_malloc.h>
9 #include <rte_byteorder.h>
14 #include <rte_eth_ctrl.h>
16 #include "enic_compat.h"
18 #include "wq_enet_desc.h"
19 #include "rq_enet_desc.h"
20 #include "cq_enet_desc.h"
21 #include "vnic_enet.h"
26 #include "vnic_intr.h"
30 #include <rte_hash_crc.h>
31 #define DEFAULT_HASH_FUNC rte_hash_crc
33 #include <rte_jhash.h>
34 #define DEFAULT_HASH_FUNC rte_jhash
37 #define ENICPMD_CLSF_HASH_ENTRIES ENICPMD_FDIR_MAX
39 static void copy_fltr_v1(struct filter_v2 *fltr,
40 const struct rte_eth_fdir_input *input,
41 const struct rte_eth_fdir_masks *masks);
42 static void copy_fltr_v2(struct filter_v2 *fltr,
43 const struct rte_eth_fdir_input *input,
44 const struct rte_eth_fdir_masks *masks);
46 void enic_fdir_stats_get(struct enic *enic, struct rte_eth_fdir_stats *stats)
48 *stats = enic->fdir.stats;
51 void enic_fdir_info_get(struct enic *enic, struct rte_eth_fdir_info *info)
53 info->mode = (enum rte_fdir_mode)enic->fdir.modes;
54 info->flow_types_mask[0] = enic->fdir.types_mask;
57 void enic_fdir_info(struct enic *enic)
59 enic->fdir.modes = (u32)RTE_FDIR_MODE_PERFECT;
60 enic->fdir.types_mask = 1 << RTE_ETH_FLOW_NONFRAG_IPV4_UDP |
61 1 << RTE_ETH_FLOW_NONFRAG_IPV4_TCP;
62 if (enic->adv_filters) {
63 enic->fdir.types_mask |= 1 << RTE_ETH_FLOW_NONFRAG_IPV4_OTHER |
64 1 << RTE_ETH_FLOW_NONFRAG_IPV4_SCTP |
65 1 << RTE_ETH_FLOW_NONFRAG_IPV6_UDP |
66 1 << RTE_ETH_FLOW_NONFRAG_IPV6_TCP |
67 1 << RTE_ETH_FLOW_NONFRAG_IPV6_SCTP |
68 1 << RTE_ETH_FLOW_NONFRAG_IPV6_OTHER;
69 enic->fdir.copy_fltr_fn = copy_fltr_v2;
71 enic->fdir.copy_fltr_fn = copy_fltr_v1;
76 enic_set_layer(struct filter_generic_1 *gp, unsigned int flag,
77 enum filter_generic_1_layer layer, void *mask, void *val,
80 gp->mask_flags |= flag;
81 gp->val_flags |= gp->mask_flags;
82 memcpy(gp->layer[layer].mask, mask, len);
83 memcpy(gp->layer[layer].val, val, len);
86 /* Copy Flow Director filter to a VIC ipv4 filter (for Cisco VICs
87 * without advanced filter support.
90 copy_fltr_v1(struct filter_v2 *fltr, const struct rte_eth_fdir_input *input,
91 __rte_unused const struct rte_eth_fdir_masks *masks)
93 fltr->type = FILTER_IPV4_5TUPLE;
94 fltr->u.ipv4.src_addr = rte_be_to_cpu_32(
95 input->flow.ip4_flow.src_ip);
96 fltr->u.ipv4.dst_addr = rte_be_to_cpu_32(
97 input->flow.ip4_flow.dst_ip);
98 fltr->u.ipv4.src_port = rte_be_to_cpu_16(
99 input->flow.udp4_flow.src_port);
100 fltr->u.ipv4.dst_port = rte_be_to_cpu_16(
101 input->flow.udp4_flow.dst_port);
103 if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_TCP)
104 fltr->u.ipv4.protocol = PROTO_TCP;
106 fltr->u.ipv4.protocol = PROTO_UDP;
108 fltr->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
111 /* Copy Flow Director filter to a VIC generic filter (requires advanced
115 copy_fltr_v2(struct filter_v2 *fltr, const struct rte_eth_fdir_input *input,
116 const struct rte_eth_fdir_masks *masks)
118 struct filter_generic_1 *gp = &fltr->u.generic_1;
120 fltr->type = FILTER_DPDK_1;
121 memset(gp, 0, sizeof(*gp));
123 if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_UDP) {
124 struct udp_hdr udp_mask, udp_val;
125 memset(&udp_mask, 0, sizeof(udp_mask));
126 memset(&udp_val, 0, sizeof(udp_val));
128 if (input->flow.udp4_flow.src_port) {
129 udp_mask.src_port = masks->src_port_mask;
130 udp_val.src_port = input->flow.udp4_flow.src_port;
132 if (input->flow.udp4_flow.dst_port) {
133 udp_mask.dst_port = masks->dst_port_mask;
134 udp_val.dst_port = input->flow.udp4_flow.dst_port;
137 enic_set_layer(gp, FILTER_GENERIC_1_UDP, FILTER_GENERIC_1_L4,
138 &udp_mask, &udp_val, sizeof(struct udp_hdr));
139 } else if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_TCP) {
140 struct tcp_hdr tcp_mask, tcp_val;
141 memset(&tcp_mask, 0, sizeof(tcp_mask));
142 memset(&tcp_val, 0, sizeof(tcp_val));
144 if (input->flow.tcp4_flow.src_port) {
145 tcp_mask.src_port = masks->src_port_mask;
146 tcp_val.src_port = input->flow.tcp4_flow.src_port;
148 if (input->flow.tcp4_flow.dst_port) {
149 tcp_mask.dst_port = masks->dst_port_mask;
150 tcp_val.dst_port = input->flow.tcp4_flow.dst_port;
153 enic_set_layer(gp, FILTER_GENERIC_1_TCP, FILTER_GENERIC_1_L4,
154 &tcp_mask, &tcp_val, sizeof(struct tcp_hdr));
155 } else if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_SCTP) {
156 struct sctp_hdr sctp_mask, sctp_val;
157 memset(&sctp_mask, 0, sizeof(sctp_mask));
158 memset(&sctp_val, 0, sizeof(sctp_val));
160 if (input->flow.sctp4_flow.src_port) {
161 sctp_mask.src_port = masks->src_port_mask;
162 sctp_val.src_port = input->flow.sctp4_flow.src_port;
164 if (input->flow.sctp4_flow.dst_port) {
165 sctp_mask.dst_port = masks->dst_port_mask;
166 sctp_val.dst_port = input->flow.sctp4_flow.dst_port;
168 if (input->flow.sctp4_flow.verify_tag) {
169 sctp_mask.tag = 0xffffffff;
170 sctp_val.tag = input->flow.sctp4_flow.verify_tag;
174 * Unlike UDP/TCP (FILTER_GENERIC_1_{UDP,TCP}), the firmware
175 * has no "packet is SCTP" flag. Use flag=0 (generic L4) and
176 * manually set proto_id=sctp below.
178 enic_set_layer(gp, 0, FILTER_GENERIC_1_L4, &sctp_mask,
179 &sctp_val, sizeof(struct sctp_hdr));
182 if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_UDP ||
183 input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_TCP ||
184 input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_SCTP ||
185 input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_OTHER) {
186 struct ipv4_hdr ip4_mask, ip4_val;
187 memset(&ip4_mask, 0, sizeof(struct ipv4_hdr));
188 memset(&ip4_val, 0, sizeof(struct ipv4_hdr));
190 if (input->flow.ip4_flow.tos) {
191 ip4_mask.type_of_service = masks->ipv4_mask.tos;
192 ip4_val.type_of_service = input->flow.ip4_flow.tos;
194 if (input->flow.ip4_flow.ttl) {
195 ip4_mask.time_to_live = masks->ipv4_mask.ttl;
196 ip4_val.time_to_live = input->flow.ip4_flow.ttl;
198 if (input->flow.ip4_flow.proto) {
199 ip4_mask.next_proto_id = masks->ipv4_mask.proto;
200 ip4_val.next_proto_id = input->flow.ip4_flow.proto;
201 } else if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_SCTP) {
202 /* Explicitly match the SCTP protocol number */
203 ip4_mask.next_proto_id = 0xff;
204 ip4_val.next_proto_id = IPPROTO_SCTP;
206 if (input->flow.ip4_flow.src_ip) {
207 ip4_mask.src_addr = masks->ipv4_mask.src_ip;
208 ip4_val.src_addr = input->flow.ip4_flow.src_ip;
210 if (input->flow.ip4_flow.dst_ip) {
211 ip4_mask.dst_addr = masks->ipv4_mask.dst_ip;
212 ip4_val.dst_addr = input->flow.ip4_flow.dst_ip;
215 enic_set_layer(gp, FILTER_GENERIC_1_IPV4, FILTER_GENERIC_1_L3,
216 &ip4_mask, &ip4_val, sizeof(struct ipv4_hdr));
219 if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV6_UDP) {
220 struct udp_hdr udp_mask, udp_val;
221 memset(&udp_mask, 0, sizeof(udp_mask));
222 memset(&udp_val, 0, sizeof(udp_val));
224 if (input->flow.udp6_flow.src_port) {
225 udp_mask.src_port = masks->src_port_mask;
226 udp_val.src_port = input->flow.udp6_flow.src_port;
228 if (input->flow.udp6_flow.dst_port) {
229 udp_mask.dst_port = masks->dst_port_mask;
230 udp_val.dst_port = input->flow.udp6_flow.dst_port;
232 enic_set_layer(gp, FILTER_GENERIC_1_UDP, FILTER_GENERIC_1_L4,
233 &udp_mask, &udp_val, sizeof(struct udp_hdr));
234 } else if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV6_TCP) {
235 struct tcp_hdr tcp_mask, tcp_val;
236 memset(&tcp_mask, 0, sizeof(tcp_mask));
237 memset(&tcp_val, 0, sizeof(tcp_val));
239 if (input->flow.tcp6_flow.src_port) {
240 tcp_mask.src_port = masks->src_port_mask;
241 tcp_val.src_port = input->flow.tcp6_flow.src_port;
243 if (input->flow.tcp6_flow.dst_port) {
244 tcp_mask.dst_port = masks->dst_port_mask;
245 tcp_val.dst_port = input->flow.tcp6_flow.dst_port;
247 enic_set_layer(gp, FILTER_GENERIC_1_TCP, FILTER_GENERIC_1_L4,
248 &tcp_mask, &tcp_val, sizeof(struct tcp_hdr));
249 } else if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV6_SCTP) {
250 struct sctp_hdr sctp_mask, sctp_val;
251 memset(&sctp_mask, 0, sizeof(sctp_mask));
252 memset(&sctp_val, 0, sizeof(sctp_val));
254 if (input->flow.sctp6_flow.src_port) {
255 sctp_mask.src_port = masks->src_port_mask;
256 sctp_val.src_port = input->flow.sctp6_flow.src_port;
258 if (input->flow.sctp6_flow.dst_port) {
259 sctp_mask.dst_port = masks->dst_port_mask;
260 sctp_val.dst_port = input->flow.sctp6_flow.dst_port;
262 if (input->flow.sctp6_flow.verify_tag) {
263 sctp_mask.tag = 0xffffffff;
264 sctp_val.tag = input->flow.sctp6_flow.verify_tag;
267 enic_set_layer(gp, 0, FILTER_GENERIC_1_L4, &sctp_mask,
268 &sctp_val, sizeof(struct sctp_hdr));
271 if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV6_UDP ||
272 input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV6_TCP ||
273 input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV6_SCTP ||
274 input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV6_OTHER) {
275 struct ipv6_hdr ipv6_mask, ipv6_val;
276 memset(&ipv6_mask, 0, sizeof(struct ipv6_hdr));
277 memset(&ipv6_val, 0, sizeof(struct ipv6_hdr));
279 if (input->flow.ipv6_flow.proto) {
280 ipv6_mask.proto = masks->ipv6_mask.proto;
281 ipv6_val.proto = input->flow.ipv6_flow.proto;
282 } else if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV6_SCTP) {
283 /* See comments for IPv4 SCTP above. */
284 ipv6_mask.proto = 0xff;
285 ipv6_val.proto = IPPROTO_SCTP;
287 memcpy(ipv6_mask.src_addr, masks->ipv6_mask.src_ip,
288 sizeof(ipv6_mask.src_addr));
289 memcpy(ipv6_val.src_addr, input->flow.ipv6_flow.src_ip,
290 sizeof(ipv6_val.src_addr));
291 memcpy(ipv6_mask.dst_addr, masks->ipv6_mask.dst_ip,
292 sizeof(ipv6_mask.dst_addr));
293 memcpy(ipv6_val.dst_addr, input->flow.ipv6_flow.dst_ip,
294 sizeof(ipv6_val.dst_addr));
295 if (input->flow.ipv6_flow.tc) {
296 ipv6_mask.vtc_flow = masks->ipv6_mask.tc << 12;
297 ipv6_val.vtc_flow = input->flow.ipv6_flow.tc << 12;
299 if (input->flow.ipv6_flow.hop_limits) {
300 ipv6_mask.hop_limits = masks->ipv6_mask.hop_limits;
301 ipv6_val.hop_limits = input->flow.ipv6_flow.hop_limits;
304 enic_set_layer(gp, FILTER_GENERIC_1_IPV6, FILTER_GENERIC_1_L3,
305 &ipv6_mask, &ipv6_val, sizeof(struct ipv6_hdr));
309 int enic_fdir_del_fltr(struct enic *enic, struct rte_eth_fdir_filter *params)
312 struct enic_fdir_node *key;
313 /* See if the key is in the table */
314 pos = rte_hash_del_key(enic->fdir.hash, params);
318 enic->fdir.stats.f_remove++;
321 /* The entry is present in the table */
322 key = enic->fdir.nodes[pos];
324 /* Delete the filter */
325 vnic_dev_classifier(enic->vdev, CLSF_DEL,
326 &key->fltr_id, NULL, NULL);
328 enic->fdir.nodes[pos] = NULL;
329 enic->fdir.stats.free++;
330 enic->fdir.stats.remove++;
336 int enic_fdir_add_fltr(struct enic *enic, struct rte_eth_fdir_filter *params)
338 struct enic_fdir_node *key;
339 struct filter_v2 fltr;
343 u32 flowtype_supported;
346 struct filter_action_v2 action;
348 memset(&fltr, 0, sizeof(fltr));
349 memset(&action, 0, sizeof(action));
350 flowtype_supported = enic->fdir.types_mask
351 & (1 << params->input.flow_type);
353 flex_bytes = ((params->input.flow_ext.flexbytes[1] << 8 & 0xFF00) |
354 (params->input.flow_ext.flexbytes[0] & 0xFF));
356 if (!enic->fdir.hash ||
357 (params->input.flow_ext.vlan_tci & 0xFFF) ||
358 !flowtype_supported || flex_bytes ||
359 params->action.behavior /* drop */) {
360 enic->fdir.stats.f_add++;
364 /* Get the enicpmd RQ from the DPDK Rx queue */
365 queue = enic_rte_rq_idx_to_sop_idx(params->action.rx_queue);
367 if (!enic->rq[queue].in_use)
370 /* See if the key is already there in the table */
371 pos = rte_hash_del_key(enic->fdir.hash, params);
374 enic->fdir.stats.f_add++;
377 /* Add a new classifier entry */
378 if (!enic->fdir.stats.free) {
379 enic->fdir.stats.f_add++;
382 key = rte_zmalloc("enic_fdir_node",
383 sizeof(struct enic_fdir_node), 0);
385 enic->fdir.stats.f_add++;
390 /* The entry is already present in the table.
391 * Check if there is a change in queue
393 key = enic->fdir.nodes[pos];
394 enic->fdir.nodes[pos] = NULL;
395 if (unlikely(key->rq_index == queue)) {
396 /* Nothing to be done */
397 enic->fdir.stats.f_add++;
398 pos = rte_hash_add_key(enic->fdir.hash, params);
400 dev_err(enic, "Add hash key failed\n");
403 enic->fdir.nodes[pos] = key;
405 "FDIR rule is already present\n");
409 if (likely(enic->fdir.stats.free)) {
410 /* Add the filter and then delete the old one.
411 * This is to avoid packets from going into the
412 * default queue during the window between
416 old_fltr_id = key->fltr_id;
418 /* No free slots in the classifier.
419 * Delete the filter and add the modified one later
421 vnic_dev_classifier(enic->vdev, CLSF_DEL,
422 &key->fltr_id, NULL, NULL);
423 enic->fdir.stats.free++;
429 key->filter = *params;
430 key->rq_index = queue;
432 enic->fdir.copy_fltr_fn(&fltr, ¶ms->input,
433 &enic->rte_dev->data->dev_conf.fdir_conf.mask);
434 action.type = FILTER_ACTION_RQ_STEERING;
435 action.rq_idx = queue;
437 if (!vnic_dev_classifier(enic->vdev, CLSF_ADD, &queue, &fltr,
439 key->fltr_id = queue;
441 dev_err(enic, "Add classifier entry failed\n");
442 enic->fdir.stats.f_add++;
448 vnic_dev_classifier(enic->vdev, CLSF_DEL, &old_fltr_id, NULL,
451 enic->fdir.stats.free--;
452 enic->fdir.stats.add++;
455 pos = rte_hash_add_key(enic->fdir.hash, params);
457 enic->fdir.stats.f_add++;
458 dev_err(enic, "Add hash key failed\n");
462 enic->fdir.nodes[pos] = key;
466 void enic_clsf_destroy(struct enic *enic)
469 struct enic_fdir_node *key;
470 /* delete classifier entries */
471 for (index = 0; index < ENICPMD_FDIR_MAX; index++) {
472 key = enic->fdir.nodes[index];
474 vnic_dev_classifier(enic->vdev, CLSF_DEL,
475 &key->fltr_id, NULL, NULL);
477 enic->fdir.nodes[index] = NULL;
481 if (enic->fdir.hash) {
482 rte_hash_free(enic->fdir.hash);
483 enic->fdir.hash = NULL;
487 int enic_clsf_init(struct enic *enic)
489 char clsf_name[RTE_HASH_NAMESIZE];
490 struct rte_hash_parameters hash_params = {
492 .entries = ENICPMD_CLSF_HASH_ENTRIES,
493 .key_len = sizeof(struct rte_eth_fdir_filter),
494 .hash_func = DEFAULT_HASH_FUNC,
495 .hash_func_init_val = 0,
496 .socket_id = SOCKET_ID_ANY,
498 snprintf(clsf_name, RTE_HASH_NAMESIZE, "enic_clsf_%s", enic->bdf_name);
499 enic->fdir.hash = rte_hash_create(&hash_params);
500 memset(&enic->fdir.stats, 0, sizeof(enic->fdir.stats));
501 enic->fdir.stats.free = ENICPMD_FDIR_MAX;
502 return NULL == enic->fdir.hash;