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
63 #define ENICPMD_CLSF_HASH_ENTRIES ENICPMD_FDIR_MAX
65 void enic_fdir_stats_get(struct enic *enic, struct rte_eth_fdir_stats *stats)
67 *stats = enic->fdir.stats;
70 int enic_fdir_del_fltr(struct enic *enic, struct rte_eth_fdir_filter *params)
73 struct enic_fdir_node *key;
74 /* See if the key is in the table */
75 pos = rte_hash_del_key(enic->fdir.hash, params);
79 enic->fdir.stats.f_remove++;
82 /* The entry is present in the table */
83 key = enic->fdir.nodes[pos];
85 /* Delete the filter */
86 vnic_dev_classifier(enic->vdev, CLSF_DEL,
89 enic->fdir.nodes[pos] = NULL;
90 enic->fdir.stats.free++;
91 enic->fdir.stats.remove++;
97 int enic_fdir_add_fltr(struct enic *enic, struct rte_eth_fdir_filter *params)
99 struct enic_fdir_node *key;
100 struct filter fltr = {0};
104 u32 flowtype_supported;
108 flowtype_supported = (
109 (RTE_ETH_FLOW_NONFRAG_IPV4_TCP == params->input.flow_type) ||
110 (RTE_ETH_FLOW_NONFRAG_IPV4_UDP == params->input.flow_type));
112 flex_bytes = ((params->input.flow_ext.flexbytes[1] << 8 & 0xFF00) |
113 (params->input.flow_ext.flexbytes[0] & 0xFF));
115 if (!enic->fdir.hash ||
116 (params->input.flow_ext.vlan_tci & 0xFFF) ||
117 !flowtype_supported || flex_bytes ||
118 params->action.behavior /* drop */) {
119 enic->fdir.stats.f_add++;
123 queue = params->action.rx_queue;
124 /* See if the key is already there in the table */
125 pos = rte_hash_del_key(enic->fdir.hash, params);
128 enic->fdir.stats.f_add++;
131 /* Add a new classifier entry */
132 if (!enic->fdir.stats.free) {
133 enic->fdir.stats.f_add++;
136 key = rte_zmalloc("enic_fdir_node",
137 sizeof(struct enic_fdir_node), 0);
139 enic->fdir.stats.f_add++;
144 /* The entry is already present in the table.
145 * Check if there is a change in queue
147 key = enic->fdir.nodes[pos];
148 enic->fdir.nodes[pos] = NULL;
149 if (unlikely(key->rq_index == queue)) {
150 /* Nothing to be done */
151 pos = rte_hash_add_key(enic->fdir.hash, params);
152 enic->fdir.nodes[pos] = key;
153 enic->fdir.stats.f_add++;
155 "FDIR rule is already present\n");
159 if (likely(enic->fdir.stats.free)) {
160 /* Add the filter and then delete the old one.
161 * This is to avoid packets from going into the
162 * default queue during the window between
166 old_fltr_id = key->fltr_id;
168 /* No free slots in the classifier.
169 * Delete the filter and add the modified one later
171 vnic_dev_classifier(enic->vdev, CLSF_DEL,
172 &key->fltr_id, NULL);
173 enic->fdir.stats.free++;
179 key->filter = *params;
180 key->rq_index = queue;
182 fltr.type = FILTER_IPV4_5TUPLE;
183 fltr.u.ipv4.src_addr = rte_be_to_cpu_32(
184 params->input.flow.ip4_flow.src_ip);
185 fltr.u.ipv4.dst_addr = rte_be_to_cpu_32(
186 params->input.flow.ip4_flow.dst_ip);
187 fltr.u.ipv4.src_port = rte_be_to_cpu_16(
188 params->input.flow.udp4_flow.src_port);
189 fltr.u.ipv4.dst_port = rte_be_to_cpu_16(
190 params->input.flow.udp4_flow.dst_port);
192 if (RTE_ETH_FLOW_NONFRAG_IPV4_TCP == params->input.flow_type)
193 fltr.u.ipv4.protocol = PROTO_TCP;
195 fltr.u.ipv4.protocol = PROTO_UDP;
197 fltr.u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
199 if (!vnic_dev_classifier(enic->vdev, CLSF_ADD, &queue, &fltr)) {
200 key->fltr_id = queue;
202 dev_err(enic, "Add classifier entry failed\n");
203 enic->fdir.stats.f_add++;
209 vnic_dev_classifier(enic->vdev, CLSF_DEL, &old_fltr_id, NULL);
211 enic->fdir.stats.free--;
212 enic->fdir.stats.add++;
215 pos = rte_hash_add_key(enic->fdir.hash, params);
216 enic->fdir.nodes[pos] = key;
220 void enic_clsf_destroy(struct enic *enic)
223 struct enic_fdir_node *key;
224 /* delete classifier entries */
225 for (index = 0; index < ENICPMD_FDIR_MAX; index++) {
226 key = enic->fdir.nodes[index];
228 vnic_dev_classifier(enic->vdev, CLSF_DEL,
229 &key->fltr_id, NULL);
234 if (enic->fdir.hash) {
235 rte_hash_free(enic->fdir.hash);
236 enic->fdir.hash = NULL;
240 int enic_clsf_init(struct enic *enic)
242 struct rte_hash_parameters hash_params = {
243 .name = "enicpmd_clsf_hash",
244 .entries = ENICPMD_CLSF_HASH_ENTRIES,
245 .key_len = sizeof(struct rte_eth_fdir_filter),
246 .hash_func = DEFAULT_HASH_FUNC,
247 .hash_func_init_val = 0,
248 .socket_id = SOCKET_ID_ANY,
251 enic->fdir.hash = rte_hash_create(&hash_params);
252 memset(&enic->fdir.stats, 0, sizeof(enic->fdir.stats));
253 enic->fdir.stats.free = ENICPMD_FDIR_MAX;
254 return NULL == enic->fdir.hash;