hash: rename unused field
[dpdk.git] / drivers / net / enic / enic_clsf.c
1 /*
2  * Copyright 2008-2014 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
5  * Copyright (c) 2014, Cisco Systems, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
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
18  * distribution.
19  *
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.
32  *
33  */
34 #ident "$Id$"
35
36 #include <libgen.h>
37
38 #include <rte_ethdev.h>
39 #include <rte_malloc.h>
40 #include <rte_hash.h>
41 #include <rte_byteorder.h>
42
43 #include "enic_compat.h"
44 #include "enic.h"
45 #include "wq_enet_desc.h"
46 #include "rq_enet_desc.h"
47 #include "cq_enet_desc.h"
48 #include "vnic_enet.h"
49 #include "vnic_dev.h"
50 #include "vnic_wq.h"
51 #include "vnic_rq.h"
52 #include "vnic_cq.h"
53 #include "vnic_intr.h"
54 #include "vnic_nic.h"
55
56 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
57 #include <rte_hash_crc.h>
58 #define DEFAULT_HASH_FUNC       rte_hash_crc
59 #else
60 #include <rte_jhash.h>
61 #define DEFAULT_HASH_FUNC       rte_jhash
62 #endif
63
64 #define SOCKET_0                0
65 #define ENICPMD_CLSF_HASH_ENTRIES       ENICPMD_FDIR_MAX
66
67 void enic_fdir_stats_get(struct enic *enic, struct rte_eth_fdir_stats *stats)
68 {
69         *stats = enic->fdir.stats;
70 }
71
72 int enic_fdir_del_fltr(struct enic *enic, struct rte_eth_fdir_filter *params)
73 {
74         int32_t pos;
75         struct enic_fdir_node *key;
76         /* See if the key is in the table */
77         pos = rte_hash_del_key(enic->fdir.hash, params);
78         switch (pos) {
79         case -EINVAL:
80         case -ENOENT:
81                 enic->fdir.stats.f_remove++;
82                 return -EINVAL;
83         default:
84                 /* The entry is present in the table */
85                 key = enic->fdir.nodes[pos];
86
87                 /* Delete the filter */
88                 vnic_dev_classifier(enic->vdev, CLSF_DEL,
89                         &key->fltr_id, NULL);
90                 rte_free(key);
91                 enic->fdir.nodes[pos] = NULL;
92                 enic->fdir.stats.free++;
93                 enic->fdir.stats.remove++;
94                 break;
95         }
96         return 0;
97 }
98
99 int enic_fdir_add_fltr(struct enic *enic, struct rte_eth_fdir_filter *params)
100 {
101         struct enic_fdir_node *key;
102         struct filter fltr = {0};
103         int32_t pos;
104         u8 do_free = 0;
105         u16 old_fltr_id = 0;
106         u32 flowtype_supported;
107         u16 flex_bytes;
108         u16 queue;
109
110         flowtype_supported = (
111                 (RTE_ETH_FLOW_NONFRAG_IPV4_TCP == params->input.flow_type) ||
112                 (RTE_ETH_FLOW_NONFRAG_IPV4_UDP == params->input.flow_type));
113
114         flex_bytes = ((params->input.flow_ext.flexbytes[1] << 8 & 0xFF00) |
115                 (params->input.flow_ext.flexbytes[0] & 0xFF));
116
117         if (!enic->fdir.hash ||
118                 (params->input.flow_ext.vlan_tci & 0xFFF) ||
119                 !flowtype_supported || flex_bytes ||
120                 params->action.behavior /* drop */) {
121                 enic->fdir.stats.f_add++;
122                 return -ENOTSUP;
123         }
124
125         queue = params->action.rx_queue;
126         /* See if the key is already there in the table */
127         pos = rte_hash_del_key(enic->fdir.hash, params);
128         switch (pos) {
129         case -EINVAL:
130                 enic->fdir.stats.f_add++;
131                 return -EINVAL;
132         case -ENOENT:
133                 /* Add a new classifier entry */
134                 if (!enic->fdir.stats.free) {
135                         enic->fdir.stats.f_add++;
136                         return -ENOSPC;
137                 }
138                 key = rte_zmalloc("enic_fdir_node",
139                                   sizeof(struct enic_fdir_node), 0);
140                 if (!key) {
141                         enic->fdir.stats.f_add++;
142                         return -ENOMEM;
143                 }
144                 break;
145         default:
146                 /* The entry is already present in the table.
147                  * Check if there is a change in queue
148                  */
149                 key = enic->fdir.nodes[pos];
150                 enic->fdir.nodes[pos] = NULL;
151                 if (unlikely(key->rq_index == queue)) {
152                         /* Nothing to be done */
153                         pos = rte_hash_add_key(enic->fdir.hash, params);
154                         enic->fdir.nodes[pos] = key;
155                         enic->fdir.stats.f_add++;
156                         dev_warning(enic,
157                                 "FDIR rule is already present\n");
158                         return 0;
159                 }
160
161                 if (likely(enic->fdir.stats.free)) {
162                         /* Add the filter and then delete the old one.
163                          * This is to avoid packets from going into the
164                          * default queue during the window between
165                          * delete and add
166                          */
167                         do_free = 1;
168                         old_fltr_id = key->fltr_id;
169                 } else {
170                         /* No free slots in the classifier.
171                          * Delete the filter and add the modified one later
172                          */
173                         vnic_dev_classifier(enic->vdev, CLSF_DEL,
174                                 &key->fltr_id, NULL);
175                         enic->fdir.stats.free++;
176                 }
177
178                 break;
179         }
180
181         key->filter = *params;
182         key->rq_index = queue;
183
184         fltr.type = FILTER_IPV4_5TUPLE;
185         fltr.u.ipv4.src_addr = rte_be_to_cpu_32(
186                 params->input.flow.ip4_flow.src_ip);
187         fltr.u.ipv4.dst_addr = rte_be_to_cpu_32(
188                 params->input.flow.ip4_flow.dst_ip);
189         fltr.u.ipv4.src_port = rte_be_to_cpu_16(
190                 params->input.flow.udp4_flow.src_port);
191         fltr.u.ipv4.dst_port = rte_be_to_cpu_16(
192                 params->input.flow.udp4_flow.dst_port);
193
194         if (RTE_ETH_FLOW_NONFRAG_IPV4_TCP == params->input.flow_type)
195                 fltr.u.ipv4.protocol = PROTO_TCP;
196         else
197                 fltr.u.ipv4.protocol = PROTO_UDP;
198
199         fltr.u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
200
201         if (!vnic_dev_classifier(enic->vdev, CLSF_ADD, &queue, &fltr)) {
202                 key->fltr_id = queue;
203         } else {
204                 dev_err(enic, "Add classifier entry failed\n");
205                 enic->fdir.stats.f_add++;
206                 rte_free(key);
207                 return -1;
208         }
209
210         if (do_free)
211                 vnic_dev_classifier(enic->vdev, CLSF_DEL, &old_fltr_id, NULL);
212         else{
213                 enic->fdir.stats.free--;
214                 enic->fdir.stats.add++;
215         }
216
217         pos = rte_hash_add_key(enic->fdir.hash, (void *)key);
218         enic->fdir.nodes[pos] = key;
219         return 0;
220 }
221
222 void enic_clsf_destroy(struct enic *enic)
223 {
224         u32 index;
225         struct enic_fdir_node *key;
226         /* delete classifier entries */
227         for (index = 0; index < ENICPMD_FDIR_MAX; index++) {
228                 key = enic->fdir.nodes[index];
229                 if (key) {
230                         vnic_dev_classifier(enic->vdev, CLSF_DEL,
231                                 &key->fltr_id, NULL);
232                         rte_free(key);
233                 }
234         }
235
236         if (enic->fdir.hash) {
237                 rte_hash_free(enic->fdir.hash);
238                 enic->fdir.hash = NULL;
239         }
240 }
241
242 int enic_clsf_init(struct enic *enic)
243 {
244         struct rte_hash_parameters hash_params = {
245                 .name = "enicpmd_clsf_hash",
246                 .entries = ENICPMD_CLSF_HASH_ENTRIES,
247                 .key_len = RTE_HASH_KEY_LENGTH_MAX,
248                 .hash_func = DEFAULT_HASH_FUNC,
249                 .hash_func_init_val = 0,
250                 .socket_id = SOCKET_0,
251         };
252
253         enic->fdir.hash = rte_hash_create(&hash_params);
254         memset(&enic->fdir.stats, 0, sizeof(enic->fdir.stats));
255         enic->fdir.stats.free = ENICPMD_FDIR_MAX;
256         return (NULL == enic->fdir.hash);
257 }