205098574910878729ee6bcd437b49c2326b3910
[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
35 #include <libgen.h>
36
37 #include <rte_ethdev.h>
38 #include <rte_malloc.h>
39 #include <rte_hash.h>
40 #include <rte_byteorder.h>
41
42 #include "enic_compat.h"
43 #include "enic.h"
44 #include "wq_enet_desc.h"
45 #include "rq_enet_desc.h"
46 #include "cq_enet_desc.h"
47 #include "vnic_enet.h"
48 #include "vnic_dev.h"
49 #include "vnic_wq.h"
50 #include "vnic_rq.h"
51 #include "vnic_cq.h"
52 #include "vnic_intr.h"
53 #include "vnic_nic.h"
54
55 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
56 #include <rte_hash_crc.h>
57 #define DEFAULT_HASH_FUNC       rte_hash_crc
58 #else
59 #include <rte_jhash.h>
60 #define DEFAULT_HASH_FUNC       rte_jhash
61 #endif
62
63 #define SOCKET_0                0
64 #define ENICPMD_CLSF_HASH_ENTRIES       ENICPMD_FDIR_MAX
65
66 void enic_fdir_stats_get(struct enic *enic, struct rte_eth_fdir_stats *stats)
67 {
68         *stats = enic->fdir.stats;
69 }
70
71 int enic_fdir_del_fltr(struct enic *enic, struct rte_eth_fdir_filter *params)
72 {
73         int32_t pos;
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);
77         switch (pos) {
78         case -EINVAL:
79         case -ENOENT:
80                 enic->fdir.stats.f_remove++;
81                 return -EINVAL;
82         default:
83                 /* The entry is present in the table */
84                 key = enic->fdir.nodes[pos];
85
86                 /* Delete the filter */
87                 vnic_dev_classifier(enic->vdev, CLSF_DEL,
88                         &key->fltr_id, NULL);
89                 rte_free(key);
90                 enic->fdir.nodes[pos] = NULL;
91                 enic->fdir.stats.free++;
92                 enic->fdir.stats.remove++;
93                 break;
94         }
95         return 0;
96 }
97
98 int enic_fdir_add_fltr(struct enic *enic, struct rte_eth_fdir_filter *params)
99 {
100         struct enic_fdir_node *key;
101         struct filter fltr = {0};
102         int32_t pos;
103         u8 do_free = 0;
104         u16 old_fltr_id = 0;
105         u32 flowtype_supported;
106         u16 flex_bytes;
107         u16 queue;
108
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));
112
113         flex_bytes = ((params->input.flow_ext.flexbytes[1] << 8 & 0xFF00) |
114                 (params->input.flow_ext.flexbytes[0] & 0xFF));
115
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++;
121                 return -ENOTSUP;
122         }
123
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);
127         switch (pos) {
128         case -EINVAL:
129                 enic->fdir.stats.f_add++;
130                 return -EINVAL;
131         case -ENOENT:
132                 /* Add a new classifier entry */
133                 if (!enic->fdir.stats.free) {
134                         enic->fdir.stats.f_add++;
135                         return -ENOSPC;
136                 }
137                 key = rte_zmalloc("enic_fdir_node",
138                                   sizeof(struct enic_fdir_node), 0);
139                 if (!key) {
140                         enic->fdir.stats.f_add++;
141                         return -ENOMEM;
142                 }
143                 break;
144         default:
145                 /* The entry is already present in the table.
146                  * Check if there is a change in queue
147                  */
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++;
155                         dev_warning(enic,
156                                 "FDIR rule is already present\n");
157                         return 0;
158                 }
159
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
164                          * delete and add
165                          */
166                         do_free = 1;
167                         old_fltr_id = key->fltr_id;
168                 } else {
169                         /* No free slots in the classifier.
170                          * Delete the filter and add the modified one later
171                          */
172                         vnic_dev_classifier(enic->vdev, CLSF_DEL,
173                                 &key->fltr_id, NULL);
174                         enic->fdir.stats.free++;
175                 }
176
177                 break;
178         }
179
180         key->filter = *params;
181         key->rq_index = queue;
182
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);
192
193         if (RTE_ETH_FLOW_NONFRAG_IPV4_TCP == params->input.flow_type)
194                 fltr.u.ipv4.protocol = PROTO_TCP;
195         else
196                 fltr.u.ipv4.protocol = PROTO_UDP;
197
198         fltr.u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
199
200         if (!vnic_dev_classifier(enic->vdev, CLSF_ADD, &queue, &fltr)) {
201                 key->fltr_id = queue;
202         } else {
203                 dev_err(enic, "Add classifier entry failed\n");
204                 enic->fdir.stats.f_add++;
205                 rte_free(key);
206                 return -1;
207         }
208
209         if (do_free)
210                 vnic_dev_classifier(enic->vdev, CLSF_DEL, &old_fltr_id, NULL);
211         else{
212                 enic->fdir.stats.free--;
213                 enic->fdir.stats.add++;
214         }
215
216         pos = rte_hash_add_key(enic->fdir.hash, params);
217         enic->fdir.nodes[pos] = key;
218         return 0;
219 }
220
221 void enic_clsf_destroy(struct enic *enic)
222 {
223         u32 index;
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];
228                 if (key) {
229                         vnic_dev_classifier(enic->vdev, CLSF_DEL,
230                                 &key->fltr_id, NULL);
231                         rte_free(key);
232                 }
233         }
234
235         if (enic->fdir.hash) {
236                 rte_hash_free(enic->fdir.hash);
237                 enic->fdir.hash = NULL;
238         }
239 }
240
241 int enic_clsf_init(struct enic *enic)
242 {
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,
250         };
251
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);
256 }