b61d6254b0458a3a34e6fabe871a6f23ac867048
[dpdk.git] / lib / librte_pmd_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 #define ENICPMD_CLSF_BUCKET_ENTRIES     4
67
68 int enic_fdir_del_fltr(struct enic *enic, struct rte_fdir_filter *params)
69 {
70         int32_t pos;
71         struct enic_fdir_node *key;
72         /* See if the key is in the table */
73         pos = rte_hash_del_key(enic->fdir.hash, params);
74         switch (pos) {
75         case -EINVAL:
76         case -ENOENT:
77                 enic->fdir.stats.f_remove++;
78                 return -EINVAL;
79         default:
80                 /* The entry is present in the table */
81                 key = enic->fdir.nodes[pos];
82
83                 /* Delete the filter */
84                 vnic_dev_classifier(enic->vdev, CLSF_DEL,
85                         &key->fltr_id, NULL);
86                 rte_free(key);
87                 enic->fdir.nodes[pos] = NULL;
88                 enic->fdir.stats.free++;
89                 enic->fdir.stats.remove++;
90                 break;
91         }
92         return 0;
93 }
94
95 int enic_fdir_add_fltr(struct enic *enic, struct rte_fdir_filter *params,
96         u16 queue, u8 drop)
97 {
98         struct enic_fdir_node *key;
99         struct filter fltr = {.type = 0};
100         int32_t pos;
101         u8 do_free = 0;
102         u16 old_fltr_id = 0;
103
104         if (!enic->fdir.hash || params->vlan_id || !params->l4type ||
105                 (RTE_FDIR_IPTYPE_IPV6 == params->iptype) ||
106                 (RTE_FDIR_L4TYPE_SCTP == params->l4type) ||
107                 params->flex_bytes || drop) {
108                 enic->fdir.stats.f_add++;
109                 return -ENOTSUP;
110         }
111
112         /* See if the key is already there in the table */
113         pos = rte_hash_del_key(enic->fdir.hash, params);
114         switch (pos) {
115         case -EINVAL:
116                 enic->fdir.stats.f_add++;
117                 return -EINVAL;
118         case -ENOENT:
119                 /* Add a new classifier entry */
120                 if (!enic->fdir.stats.free) {
121                         enic->fdir.stats.f_add++;
122                         return -ENOSPC;
123                 }
124                 key = rte_zmalloc("enic_fdir_node",
125                                   sizeof(struct enic_fdir_node), 0);
126                 if (!key) {
127                         enic->fdir.stats.f_add++;
128                         return -ENOMEM;
129                 }
130                 break;
131         default:
132                 /* The entry is already present in the table.
133                  * Check if there is a change in queue
134                  */
135                 key = enic->fdir.nodes[pos];
136                 enic->fdir.nodes[pos] = NULL;
137                 if (unlikely(key->rq_index == queue)) {
138                         /* Nothing to be done */
139                         pos = rte_hash_add_key(enic->fdir.hash, params);
140                         enic->fdir.nodes[pos] = key;
141                         enic->fdir.stats.f_add++;
142                         dev_warning(enic,
143                                 "FDIR rule is already present\n");
144                         return 0;
145                 }
146
147                 if (likely(enic->fdir.stats.free)) {
148                         /* Add the filter and then delete the old one.
149                          * This is to avoid packets from going into the
150                          * default queue during the window between
151                          * delete and add
152                          */
153                         do_free = 1;
154                         old_fltr_id = key->fltr_id;
155                 } else {
156                         /* No free slots in the classifier.
157                          * Delete the filter and add the modified one later
158                          */
159                         vnic_dev_classifier(enic->vdev, CLSF_DEL,
160                                 &key->fltr_id, NULL);
161                         enic->fdir.stats.free++;
162                 }
163
164                 break;
165         }
166
167         key->filter = *params;
168         key->rq_index = queue;
169
170         fltr.type = FILTER_IPV4_5TUPLE;
171         fltr.u.ipv4.src_addr = rte_be_to_cpu_32(params->ip_src.ipv4_addr);
172         fltr.u.ipv4.dst_addr = rte_be_to_cpu_32(params->ip_dst.ipv4_addr);
173         fltr.u.ipv4.src_port = rte_be_to_cpu_16(params->port_src);
174         fltr.u.ipv4.dst_port = rte_be_to_cpu_16(params->port_dst);
175
176         if (RTE_FDIR_L4TYPE_TCP == params->l4type)
177                 fltr.u.ipv4.protocol = PROTO_TCP;
178         else
179                 fltr.u.ipv4.protocol = PROTO_UDP;
180
181         fltr.u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
182
183         if (!vnic_dev_classifier(enic->vdev, CLSF_ADD, &queue, &fltr)) {
184                 key->fltr_id = queue;
185         } else {
186                 dev_err(enic, "Add classifier entry failed\n");
187                 enic->fdir.stats.f_add++;
188                 rte_free(key);
189                 return -1;
190         }
191
192         if (do_free)
193                 vnic_dev_classifier(enic->vdev, CLSF_DEL, &old_fltr_id, NULL);
194         else{
195                 enic->fdir.stats.free--;
196                 enic->fdir.stats.add++;
197         }
198
199         pos = rte_hash_add_key(enic->fdir.hash, (void *)key);
200         enic->fdir.nodes[pos] = key;
201         return 0;
202 }
203
204 void enic_clsf_destroy(struct enic *enic)
205 {
206         u32 index;
207         struct enic_fdir_node *key;
208         /* delete classifier entries */
209         for (index = 0; index < ENICPMD_FDIR_MAX; index++) {
210                 key = enic->fdir.nodes[index];
211                 if (key) {
212                         vnic_dev_classifier(enic->vdev, CLSF_DEL,
213                                 &key->fltr_id, NULL);
214                         rte_free(key);
215                 }
216         }
217
218         if (enic->fdir.hash) {
219                 rte_hash_free(enic->fdir.hash);
220                 enic->fdir.hash = NULL;
221         }
222 }
223
224 int enic_clsf_init(struct enic *enic)
225 {
226         struct rte_hash_parameters hash_params = {
227                 .name = "enicpmd_clsf_hash",
228                 .entries = ENICPMD_CLSF_HASH_ENTRIES,
229                 .bucket_entries = ENICPMD_CLSF_BUCKET_ENTRIES,
230                 .key_len = sizeof(struct rte_fdir_filter),
231                 .hash_func = DEFAULT_HASH_FUNC,
232                 .hash_func_init_val = 0,
233                 .socket_id = SOCKET_0,
234         };
235
236         enic->fdir.hash = rte_hash_create(&hash_params);
237         memset(&enic->fdir.stats, 0, sizeof(enic->fdir.stats));
238         enic->fdir.stats.free = ENICPMD_FDIR_MAX;
239         return (NULL == enic->fdir.hash);
240 }