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.
8 #include <rte_ethdev_driver.h>
9 #include <rte_malloc.h>
11 #include <rte_byteorder.h>
16 #include <rte_eth_ctrl.h>
18 #include "enic_compat.h"
20 #include "wq_enet_desc.h"
21 #include "rq_enet_desc.h"
22 #include "cq_enet_desc.h"
23 #include "vnic_enet.h"
28 #include "vnic_intr.h"
32 #include <rte_hash_crc.h>
33 #define DEFAULT_HASH_FUNC rte_hash_crc
35 #include <rte_jhash.h>
36 #define DEFAULT_HASH_FUNC rte_jhash
39 #define ENICPMD_CLSF_HASH_ENTRIES ENICPMD_FDIR_MAX
41 void enic_fdir_stats_get(struct enic *enic, struct rte_eth_fdir_stats *stats)
43 *stats = enic->fdir.stats;
46 void enic_fdir_info_get(struct enic *enic, struct rte_eth_fdir_info *info)
48 info->mode = (enum rte_fdir_mode)enic->fdir.modes;
49 info->flow_types_mask[0] = enic->fdir.types_mask;
52 void enic_fdir_info(struct enic *enic)
54 enic->fdir.modes = (u32)RTE_FDIR_MODE_PERFECT;
55 enic->fdir.types_mask = 1 << RTE_ETH_FLOW_NONFRAG_IPV4_UDP |
56 1 << RTE_ETH_FLOW_NONFRAG_IPV4_TCP;
57 if (enic->adv_filters) {
58 enic->fdir.types_mask |= 1 << RTE_ETH_FLOW_NONFRAG_IPV4_OTHER |
59 1 << RTE_ETH_FLOW_NONFRAG_IPV4_SCTP |
60 1 << RTE_ETH_FLOW_NONFRAG_IPV6_UDP |
61 1 << RTE_ETH_FLOW_NONFRAG_IPV6_TCP |
62 1 << RTE_ETH_FLOW_NONFRAG_IPV6_SCTP |
63 1 << RTE_ETH_FLOW_NONFRAG_IPV6_OTHER;
64 enic->fdir.copy_fltr_fn = copy_fltr_v2;
66 enic->fdir.copy_fltr_fn = copy_fltr_v1;
71 enic_set_layer(struct filter_generic_1 *gp, unsigned int flag,
72 enum filter_generic_1_layer layer, void *mask, void *val,
75 gp->mask_flags |= flag;
76 gp->val_flags |= gp->mask_flags;
77 memcpy(gp->layer[layer].mask, mask, len);
78 memcpy(gp->layer[layer].val, val, len);
81 /* Copy Flow Director filter to a VIC ipv4 filter (for Cisco VICs
82 * without advanced filter support.
85 copy_fltr_v1(struct filter_v2 *fltr, struct rte_eth_fdir_input *input,
86 __rte_unused struct rte_eth_fdir_masks *masks)
88 fltr->type = FILTER_IPV4_5TUPLE;
89 fltr->u.ipv4.src_addr = rte_be_to_cpu_32(
90 input->flow.ip4_flow.src_ip);
91 fltr->u.ipv4.dst_addr = rte_be_to_cpu_32(
92 input->flow.ip4_flow.dst_ip);
93 fltr->u.ipv4.src_port = rte_be_to_cpu_16(
94 input->flow.udp4_flow.src_port);
95 fltr->u.ipv4.dst_port = rte_be_to_cpu_16(
96 input->flow.udp4_flow.dst_port);
98 if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_TCP)
99 fltr->u.ipv4.protocol = PROTO_TCP;
101 fltr->u.ipv4.protocol = PROTO_UDP;
103 fltr->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
106 /* Copy Flow Director filter to a VIC generic filter (requires advanced
110 copy_fltr_v2(struct filter_v2 *fltr, struct rte_eth_fdir_input *input,
111 struct rte_eth_fdir_masks *masks)
113 struct filter_generic_1 *gp = &fltr->u.generic_1;
116 fltr->type = FILTER_DPDK_1;
117 memset(gp, 0, sizeof(*gp));
119 if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_UDP) {
120 struct udp_hdr udp_mask, udp_val;
121 memset(&udp_mask, 0, sizeof(udp_mask));
122 memset(&udp_val, 0, sizeof(udp_val));
124 if (input->flow.udp4_flow.src_port) {
125 udp_mask.src_port = masks->src_port_mask;
126 udp_val.src_port = input->flow.udp4_flow.src_port;
128 if (input->flow.udp4_flow.dst_port) {
129 udp_mask.dst_port = masks->dst_port_mask;
130 udp_val.dst_port = input->flow.udp4_flow.dst_port;
133 enic_set_layer(gp, FILTER_GENERIC_1_UDP, FILTER_GENERIC_1_L4,
134 &udp_mask, &udp_val, sizeof(struct udp_hdr));
135 } else if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_TCP) {
136 struct tcp_hdr tcp_mask, tcp_val;
137 memset(&tcp_mask, 0, sizeof(tcp_mask));
138 memset(&tcp_val, 0, sizeof(tcp_val));
140 if (input->flow.tcp4_flow.src_port) {
141 tcp_mask.src_port = masks->src_port_mask;
142 tcp_val.src_port = input->flow.tcp4_flow.src_port;
144 if (input->flow.tcp4_flow.dst_port) {
145 tcp_mask.dst_port = masks->dst_port_mask;
146 tcp_val.dst_port = input->flow.tcp4_flow.dst_port;
149 enic_set_layer(gp, FILTER_GENERIC_1_TCP, FILTER_GENERIC_1_L4,
150 &tcp_mask, &tcp_val, sizeof(struct tcp_hdr));
151 } else if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_SCTP) {
152 struct sctp_hdr sctp_mask, sctp_val;
153 memset(&sctp_mask, 0, sizeof(sctp_mask));
154 memset(&sctp_val, 0, sizeof(sctp_val));
156 if (input->flow.sctp4_flow.src_port) {
157 sctp_mask.src_port = masks->src_port_mask;
158 sctp_val.src_port = input->flow.sctp4_flow.src_port;
160 if (input->flow.sctp4_flow.dst_port) {
161 sctp_mask.dst_port = masks->dst_port_mask;
162 sctp_val.dst_port = input->flow.sctp4_flow.dst_port;
164 if (input->flow.sctp4_flow.verify_tag) {
165 sctp_mask.tag = 0xffffffff;
166 sctp_val.tag = input->flow.sctp4_flow.verify_tag;
169 /* v4 proto should be 132, override ip4_flow.proto */
170 input->flow.ip4_flow.proto = 132;
172 enic_set_layer(gp, 0, FILTER_GENERIC_1_L4, &sctp_mask,
173 &sctp_val, sizeof(struct sctp_hdr));
176 if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_UDP ||
177 input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_TCP ||
178 input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_SCTP ||
179 input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV4_OTHER) {
180 struct ipv4_hdr ip4_mask, ip4_val;
181 memset(&ip4_mask, 0, sizeof(struct ipv4_hdr));
182 memset(&ip4_val, 0, sizeof(struct ipv4_hdr));
184 if (input->flow.ip4_flow.tos) {
185 ip4_mask.type_of_service = masks->ipv4_mask.tos;
186 ip4_val.type_of_service = input->flow.ip4_flow.tos;
188 if (input->flow.ip4_flow.ttl) {
189 ip4_mask.time_to_live = masks->ipv4_mask.ttl;
190 ip4_val.time_to_live = input->flow.ip4_flow.ttl;
192 if (input->flow.ip4_flow.proto) {
193 ip4_mask.next_proto_id = masks->ipv4_mask.proto;
194 ip4_val.next_proto_id = input->flow.ip4_flow.proto;
196 if (input->flow.ip4_flow.src_ip) {
197 ip4_mask.src_addr = masks->ipv4_mask.src_ip;
198 ip4_val.src_addr = input->flow.ip4_flow.src_ip;
200 if (input->flow.ip4_flow.dst_ip) {
201 ip4_mask.dst_addr = masks->ipv4_mask.dst_ip;
202 ip4_val.dst_addr = input->flow.ip4_flow.dst_ip;
205 enic_set_layer(gp, FILTER_GENERIC_1_IPV4, FILTER_GENERIC_1_L3,
206 &ip4_mask, &ip4_val, sizeof(struct ipv4_hdr));
209 if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV6_UDP) {
210 struct udp_hdr udp_mask, udp_val;
211 memset(&udp_mask, 0, sizeof(udp_mask));
212 memset(&udp_val, 0, sizeof(udp_val));
214 if (input->flow.udp6_flow.src_port) {
215 udp_mask.src_port = masks->src_port_mask;
216 udp_val.src_port = input->flow.udp6_flow.src_port;
218 if (input->flow.udp6_flow.dst_port) {
219 udp_mask.dst_port = masks->dst_port_mask;
220 udp_val.dst_port = input->flow.udp6_flow.dst_port;
222 enic_set_layer(gp, FILTER_GENERIC_1_UDP, FILTER_GENERIC_1_L4,
223 &udp_mask, &udp_val, sizeof(struct udp_hdr));
224 } else if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV6_TCP) {
225 struct tcp_hdr tcp_mask, tcp_val;
226 memset(&tcp_mask, 0, sizeof(tcp_mask));
227 memset(&tcp_val, 0, sizeof(tcp_val));
229 if (input->flow.tcp6_flow.src_port) {
230 tcp_mask.src_port = masks->src_port_mask;
231 tcp_val.src_port = input->flow.tcp6_flow.src_port;
233 if (input->flow.tcp6_flow.dst_port) {
234 tcp_mask.dst_port = masks->dst_port_mask;
235 tcp_val.dst_port = input->flow.tcp6_flow.dst_port;
237 enic_set_layer(gp, FILTER_GENERIC_1_TCP, FILTER_GENERIC_1_L4,
238 &tcp_mask, &tcp_val, sizeof(struct tcp_hdr));
239 } else if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV6_SCTP) {
240 struct sctp_hdr sctp_mask, sctp_val;
241 memset(&sctp_mask, 0, sizeof(sctp_mask));
242 memset(&sctp_val, 0, sizeof(sctp_val));
244 if (input->flow.sctp6_flow.src_port) {
245 sctp_mask.src_port = masks->src_port_mask;
246 sctp_val.src_port = input->flow.sctp6_flow.src_port;
248 if (input->flow.sctp6_flow.dst_port) {
249 sctp_mask.dst_port = masks->dst_port_mask;
250 sctp_val.dst_port = input->flow.sctp6_flow.dst_port;
252 if (input->flow.sctp6_flow.verify_tag) {
253 sctp_mask.tag = 0xffffffff;
254 sctp_val.tag = input->flow.sctp6_flow.verify_tag;
257 /* v4 proto should be 132, override ipv6_flow.proto */
258 input->flow.ipv6_flow.proto = 132;
260 enic_set_layer(gp, 0, FILTER_GENERIC_1_L4, &sctp_mask,
261 &sctp_val, sizeof(struct sctp_hdr));
264 if (input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV6_UDP ||
265 input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV6_TCP ||
266 input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV6_SCTP ||
267 input->flow_type == RTE_ETH_FLOW_NONFRAG_IPV6_OTHER) {
268 struct ipv6_hdr ipv6_mask, ipv6_val;
269 memset(&ipv6_mask, 0, sizeof(struct ipv6_hdr));
270 memset(&ipv6_val, 0, sizeof(struct ipv6_hdr));
272 if (input->flow.ipv6_flow.proto) {
273 ipv6_mask.proto = masks->ipv6_mask.proto;
274 ipv6_val.proto = input->flow.ipv6_flow.proto;
276 for (i = 0; i < 4; i++) {
277 *(uint32_t *)&ipv6_mask.src_addr[i * 4] =
278 masks->ipv6_mask.src_ip[i];
279 *(uint32_t *)&ipv6_val.src_addr[i * 4] =
280 input->flow.ipv6_flow.src_ip[i];
282 for (i = 0; i < 4; i++) {
283 *(uint32_t *)&ipv6_mask.dst_addr[i * 4] =
284 masks->ipv6_mask.src_ip[i];
285 *(uint32_t *)&ipv6_val.dst_addr[i * 4] =
286 input->flow.ipv6_flow.dst_ip[i];
288 if (input->flow.ipv6_flow.tc) {
289 ipv6_mask.vtc_flow = masks->ipv6_mask.tc << 12;
290 ipv6_val.vtc_flow = input->flow.ipv6_flow.tc << 12;
292 if (input->flow.ipv6_flow.hop_limits) {
293 ipv6_mask.hop_limits = masks->ipv6_mask.hop_limits;
294 ipv6_val.hop_limits = input->flow.ipv6_flow.hop_limits;
297 enic_set_layer(gp, FILTER_GENERIC_1_IPV6, FILTER_GENERIC_1_L3,
298 &ipv6_mask, &ipv6_val, sizeof(struct ipv6_hdr));
302 int enic_fdir_del_fltr(struct enic *enic, struct rte_eth_fdir_filter *params)
305 struct enic_fdir_node *key;
306 /* See if the key is in the table */
307 pos = rte_hash_del_key(enic->fdir.hash, params);
311 enic->fdir.stats.f_remove++;
314 /* The entry is present in the table */
315 key = enic->fdir.nodes[pos];
317 /* Delete the filter */
318 vnic_dev_classifier(enic->vdev, CLSF_DEL,
319 &key->fltr_id, NULL, NULL);
321 enic->fdir.nodes[pos] = NULL;
322 enic->fdir.stats.free++;
323 enic->fdir.stats.remove++;
329 int enic_fdir_add_fltr(struct enic *enic, struct rte_eth_fdir_filter *params)
331 struct enic_fdir_node *key;
332 struct filter_v2 fltr;
336 u32 flowtype_supported;
339 struct filter_action_v2 action;
341 memset(&fltr, 0, sizeof(fltr));
342 memset(&action, 0, sizeof(action));
343 flowtype_supported = enic->fdir.types_mask
344 & (1 << params->input.flow_type);
346 flex_bytes = ((params->input.flow_ext.flexbytes[1] << 8 & 0xFF00) |
347 (params->input.flow_ext.flexbytes[0] & 0xFF));
349 if (!enic->fdir.hash ||
350 (params->input.flow_ext.vlan_tci & 0xFFF) ||
351 !flowtype_supported || flex_bytes ||
352 params->action.behavior /* drop */) {
353 enic->fdir.stats.f_add++;
357 /* Get the enicpmd RQ from the DPDK Rx queue */
358 queue = enic_rte_rq_idx_to_sop_idx(params->action.rx_queue);
360 if (!enic->rq[queue].in_use)
363 /* See if the key is already there in the table */
364 pos = rte_hash_del_key(enic->fdir.hash, params);
367 enic->fdir.stats.f_add++;
370 /* Add a new classifier entry */
371 if (!enic->fdir.stats.free) {
372 enic->fdir.stats.f_add++;
375 key = rte_zmalloc("enic_fdir_node",
376 sizeof(struct enic_fdir_node), 0);
378 enic->fdir.stats.f_add++;
383 /* The entry is already present in the table.
384 * Check if there is a change in queue
386 key = enic->fdir.nodes[pos];
387 enic->fdir.nodes[pos] = NULL;
388 if (unlikely(key->rq_index == queue)) {
389 /* Nothing to be done */
390 enic->fdir.stats.f_add++;
391 pos = rte_hash_add_key(enic->fdir.hash, params);
393 dev_err(enic, "Add hash key failed\n");
396 enic->fdir.nodes[pos] = key;
398 "FDIR rule is already present\n");
402 if (likely(enic->fdir.stats.free)) {
403 /* Add the filter and then delete the old one.
404 * This is to avoid packets from going into the
405 * default queue during the window between
409 old_fltr_id = key->fltr_id;
411 /* No free slots in the classifier.
412 * Delete the filter and add the modified one later
414 vnic_dev_classifier(enic->vdev, CLSF_DEL,
415 &key->fltr_id, NULL, NULL);
416 enic->fdir.stats.free++;
422 key->filter = *params;
423 key->rq_index = queue;
425 enic->fdir.copy_fltr_fn(&fltr, ¶ms->input,
426 &enic->rte_dev->data->dev_conf.fdir_conf.mask);
427 action.type = FILTER_ACTION_RQ_STEERING;
428 action.rq_idx = queue;
430 if (!vnic_dev_classifier(enic->vdev, CLSF_ADD, &queue, &fltr,
432 key->fltr_id = queue;
434 dev_err(enic, "Add classifier entry failed\n");
435 enic->fdir.stats.f_add++;
441 vnic_dev_classifier(enic->vdev, CLSF_DEL, &old_fltr_id, NULL,
444 enic->fdir.stats.free--;
445 enic->fdir.stats.add++;
448 pos = rte_hash_add_key(enic->fdir.hash, params);
450 enic->fdir.stats.f_add++;
451 dev_err(enic, "Add hash key failed\n");
455 enic->fdir.nodes[pos] = key;
459 void enic_clsf_destroy(struct enic *enic)
462 struct enic_fdir_node *key;
463 /* delete classifier entries */
464 for (index = 0; index < ENICPMD_FDIR_MAX; index++) {
465 key = enic->fdir.nodes[index];
467 vnic_dev_classifier(enic->vdev, CLSF_DEL,
468 &key->fltr_id, NULL, NULL);
470 enic->fdir.nodes[index] = NULL;
474 if (enic->fdir.hash) {
475 rte_hash_free(enic->fdir.hash);
476 enic->fdir.hash = NULL;
480 int enic_clsf_init(struct enic *enic)
482 char clsf_name[RTE_HASH_NAMESIZE];
483 struct rte_hash_parameters hash_params = {
485 .entries = ENICPMD_CLSF_HASH_ENTRIES,
486 .key_len = sizeof(struct rte_eth_fdir_filter),
487 .hash_func = DEFAULT_HASH_FUNC,
488 .hash_func_init_val = 0,
489 .socket_id = SOCKET_ID_ANY,
491 snprintf(clsf_name, RTE_HASH_NAMESIZE, "enic_clsf_%s", enic->bdf_name);
492 enic->fdir.hash = rte_hash_create(&hash_params);
493 memset(&enic->fdir.stats, 0, sizeof(enic->fdir.stats));
494 enic->fdir.stats.free = ENICPMD_FDIR_MAX;
495 return NULL == enic->fdir.hash;