2 * Copyright 2008-2014 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
5 * Copyright (c) 2014, Cisco Systems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
37 #include <rte_ethdev.h>
38 #include <rte_malloc.h>
40 #include <rte_byteorder.h>
42 #include "enic_compat.h"
44 #include "wq_enet_desc.h"
45 #include "rq_enet_desc.h"
46 #include "cq_enet_desc.h"
47 #include "vnic_enet.h"
52 #include "vnic_intr.h"
55 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
56 #include <rte_hash_crc.h>
57 #define DEFAULT_HASH_FUNC rte_hash_crc
59 #include <rte_jhash.h>
60 #define DEFAULT_HASH_FUNC rte_jhash
64 #define ENICPMD_CLSF_HASH_ENTRIES ENICPMD_FDIR_MAX
66 void enic_fdir_stats_get(struct enic *enic, struct rte_eth_fdir_stats *stats)
68 *stats = enic->fdir.stats;
71 int enic_fdir_del_fltr(struct enic *enic, struct rte_eth_fdir_filter *params)
74 struct enic_fdir_node *key;
75 /* See if the key is in the table */
76 pos = rte_hash_del_key(enic->fdir.hash, params);
80 enic->fdir.stats.f_remove++;
83 /* The entry is present in the table */
84 key = enic->fdir.nodes[pos];
86 /* Delete the filter */
87 vnic_dev_classifier(enic->vdev, CLSF_DEL,
90 enic->fdir.nodes[pos] = NULL;
91 enic->fdir.stats.free++;
92 enic->fdir.stats.remove++;
98 int enic_fdir_add_fltr(struct enic *enic, struct rte_eth_fdir_filter *params)
100 struct enic_fdir_node *key;
101 struct filter fltr = {0};
105 u32 flowtype_supported;
109 flowtype_supported = (
110 (RTE_ETH_FLOW_NONFRAG_IPV4_TCP == params->input.flow_type) ||
111 (RTE_ETH_FLOW_NONFRAG_IPV4_UDP == params->input.flow_type));
113 flex_bytes = ((params->input.flow_ext.flexbytes[1] << 8 & 0xFF00) |
114 (params->input.flow_ext.flexbytes[0] & 0xFF));
116 if (!enic->fdir.hash ||
117 (params->input.flow_ext.vlan_tci & 0xFFF) ||
118 !flowtype_supported || flex_bytes ||
119 params->action.behavior /* drop */) {
120 enic->fdir.stats.f_add++;
124 queue = params->action.rx_queue;
125 /* See if the key is already there in the table */
126 pos = rte_hash_del_key(enic->fdir.hash, params);
129 enic->fdir.stats.f_add++;
132 /* Add a new classifier entry */
133 if (!enic->fdir.stats.free) {
134 enic->fdir.stats.f_add++;
137 key = rte_zmalloc("enic_fdir_node",
138 sizeof(struct enic_fdir_node), 0);
140 enic->fdir.stats.f_add++;
145 /* The entry is already present in the table.
146 * Check if there is a change in queue
148 key = enic->fdir.nodes[pos];
149 enic->fdir.nodes[pos] = NULL;
150 if (unlikely(key->rq_index == queue)) {
151 /* Nothing to be done */
152 pos = rte_hash_add_key(enic->fdir.hash, params);
153 enic->fdir.nodes[pos] = key;
154 enic->fdir.stats.f_add++;
156 "FDIR rule is already present\n");
160 if (likely(enic->fdir.stats.free)) {
161 /* Add the filter and then delete the old one.
162 * This is to avoid packets from going into the
163 * default queue during the window between
167 old_fltr_id = key->fltr_id;
169 /* No free slots in the classifier.
170 * Delete the filter and add the modified one later
172 vnic_dev_classifier(enic->vdev, CLSF_DEL,
173 &key->fltr_id, NULL);
174 enic->fdir.stats.free++;
180 key->filter = *params;
181 key->rq_index = queue;
183 fltr.type = FILTER_IPV4_5TUPLE;
184 fltr.u.ipv4.src_addr = rte_be_to_cpu_32(
185 params->input.flow.ip4_flow.src_ip);
186 fltr.u.ipv4.dst_addr = rte_be_to_cpu_32(
187 params->input.flow.ip4_flow.dst_ip);
188 fltr.u.ipv4.src_port = rte_be_to_cpu_16(
189 params->input.flow.udp4_flow.src_port);
190 fltr.u.ipv4.dst_port = rte_be_to_cpu_16(
191 params->input.flow.udp4_flow.dst_port);
193 if (RTE_ETH_FLOW_NONFRAG_IPV4_TCP == params->input.flow_type)
194 fltr.u.ipv4.protocol = PROTO_TCP;
196 fltr.u.ipv4.protocol = PROTO_UDP;
198 fltr.u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
200 if (!vnic_dev_classifier(enic->vdev, CLSF_ADD, &queue, &fltr)) {
201 key->fltr_id = queue;
203 dev_err(enic, "Add classifier entry failed\n");
204 enic->fdir.stats.f_add++;
210 vnic_dev_classifier(enic->vdev, CLSF_DEL, &old_fltr_id, NULL);
212 enic->fdir.stats.free--;
213 enic->fdir.stats.add++;
216 pos = rte_hash_add_key(enic->fdir.hash, params);
217 enic->fdir.nodes[pos] = key;
221 void enic_clsf_destroy(struct enic *enic)
224 struct enic_fdir_node *key;
225 /* delete classifier entries */
226 for (index = 0; index < ENICPMD_FDIR_MAX; index++) {
227 key = enic->fdir.nodes[index];
229 vnic_dev_classifier(enic->vdev, CLSF_DEL,
230 &key->fltr_id, NULL);
235 if (enic->fdir.hash) {
236 rte_hash_free(enic->fdir.hash);
237 enic->fdir.hash = NULL;
241 int enic_clsf_init(struct enic *enic)
243 struct rte_hash_parameters hash_params = {
244 .name = "enicpmd_clsf_hash",
245 .entries = ENICPMD_CLSF_HASH_ENTRIES,
246 .key_len = sizeof(struct rte_eth_fdir_filter),
247 .hash_func = DEFAULT_HASH_FUNC,
248 .hash_func_init_val = 0,
249 .socket_id = SOCKET_0,
252 enic->fdir.hash = rte_hash_create(&hash_params);
253 memset(&enic->fdir.stats, 0, sizeof(enic->fdir.stats));
254 enic->fdir.stats.free = ENICPMD_FDIR_MAX;
255 return (NULL == enic->fdir.hash);