GNU Linux-libre 4.14.295-gnu1
[releases.git] / drivers / net / ethernet / netronome / nfp / flower / metadata.c
1 /*
2  * Copyright (C) 2017 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #include <linux/hash.h>
35 #include <linux/hashtable.h>
36 #include <linux/jhash.h>
37 #include <linux/vmalloc.h>
38 #include <net/pkt_cls.h>
39
40 #include "cmsg.h"
41 #include "main.h"
42 #include "../nfp_app.h"
43
44 struct nfp_mask_id_table {
45         struct hlist_node link;
46         u32 hash_key;
47         u32 ref_cnt;
48         u8 mask_id;
49 };
50
51 static int nfp_release_stats_entry(struct nfp_app *app, u32 stats_context_id)
52 {
53         struct nfp_flower_priv *priv = app->priv;
54         struct circ_buf *ring;
55
56         ring = &priv->stats_ids.free_list;
57         /* Check if buffer is full. */
58         if (!CIRC_SPACE(ring->head, ring->tail, NFP_FL_STATS_ENTRY_RS *
59                         NFP_FL_STATS_ELEM_RS -
60                         NFP_FL_STATS_ELEM_RS + 1))
61                 return -ENOBUFS;
62
63         memcpy(&ring->buf[ring->head], &stats_context_id, NFP_FL_STATS_ELEM_RS);
64         ring->head = (ring->head + NFP_FL_STATS_ELEM_RS) %
65                      (NFP_FL_STATS_ENTRY_RS * NFP_FL_STATS_ELEM_RS);
66
67         return 0;
68 }
69
70 static int nfp_get_stats_entry(struct nfp_app *app, u32 *stats_context_id)
71 {
72         struct nfp_flower_priv *priv = app->priv;
73         u32 freed_stats_id, temp_stats_id;
74         struct circ_buf *ring;
75
76         ring = &priv->stats_ids.free_list;
77         freed_stats_id = NFP_FL_STATS_ENTRY_RS;
78         /* Check for unallocated entries first. */
79         if (priv->stats_ids.init_unalloc > 0) {
80                 *stats_context_id = priv->stats_ids.init_unalloc - 1;
81                 priv->stats_ids.init_unalloc--;
82                 return 0;
83         }
84
85         /* Check if buffer is empty. */
86         if (ring->head == ring->tail) {
87                 *stats_context_id = freed_stats_id;
88                 return -ENOENT;
89         }
90
91         memcpy(&temp_stats_id, &ring->buf[ring->tail], NFP_FL_STATS_ELEM_RS);
92         *stats_context_id = temp_stats_id;
93         memcpy(&ring->buf[ring->tail], &freed_stats_id, NFP_FL_STATS_ELEM_RS);
94         ring->tail = (ring->tail + NFP_FL_STATS_ELEM_RS) %
95                      (NFP_FL_STATS_ENTRY_RS * NFP_FL_STATS_ELEM_RS);
96
97         return 0;
98 }
99
100 /* Must be called with either RTNL or rcu_read_lock */
101 struct nfp_fl_payload *
102 nfp_flower_search_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie)
103 {
104         struct nfp_flower_priv *priv = app->priv;
105         struct nfp_fl_payload *flower_entry;
106
107         hash_for_each_possible_rcu(priv->flow_table, flower_entry, link,
108                                    tc_flower_cookie)
109                 if (flower_entry->tc_flower_cookie == tc_flower_cookie)
110                         return flower_entry;
111
112         return NULL;
113 }
114
115 static void
116 nfp_flower_update_stats(struct nfp_app *app, struct nfp_fl_stats_frame *stats)
117 {
118         struct nfp_fl_payload *nfp_flow;
119         unsigned long flower_cookie;
120
121         flower_cookie = be64_to_cpu(stats->stats_cookie);
122
123         rcu_read_lock();
124         nfp_flow = nfp_flower_search_fl_table(app, flower_cookie);
125         if (!nfp_flow)
126                 goto exit_rcu_unlock;
127
128         if (nfp_flow->meta.host_ctx_id != stats->stats_con_id)
129                 goto exit_rcu_unlock;
130
131         spin_lock(&nfp_flow->lock);
132         nfp_flow->stats.pkts += be32_to_cpu(stats->pkt_count);
133         nfp_flow->stats.bytes += be64_to_cpu(stats->byte_count);
134         nfp_flow->stats.used = jiffies;
135         spin_unlock(&nfp_flow->lock);
136
137 exit_rcu_unlock:
138         rcu_read_unlock();
139 }
140
141 void nfp_flower_rx_flow_stats(struct nfp_app *app, struct sk_buff *skb)
142 {
143         unsigned int msg_len = skb->len - NFP_FLOWER_CMSG_HLEN;
144         struct nfp_fl_stats_frame *stats_frame;
145         unsigned char *msg;
146         int i;
147
148         msg = nfp_flower_cmsg_get_data(skb);
149
150         stats_frame = (struct nfp_fl_stats_frame *)msg;
151         for (i = 0; i < msg_len / sizeof(*stats_frame); i++)
152                 nfp_flower_update_stats(app, stats_frame + i);
153 }
154
155 static int nfp_release_mask_id(struct nfp_app *app, u8 mask_id)
156 {
157         struct nfp_flower_priv *priv = app->priv;
158         struct circ_buf *ring;
159         struct timespec64 now;
160
161         ring = &priv->mask_ids.mask_id_free_list;
162         /* Checking if buffer is full. */
163         if (CIRC_SPACE(ring->head, ring->tail, NFP_FLOWER_MASK_ENTRY_RS) == 0)
164                 return -ENOBUFS;
165
166         memcpy(&ring->buf[ring->head], &mask_id, NFP_FLOWER_MASK_ELEMENT_RS);
167         ring->head = (ring->head + NFP_FLOWER_MASK_ELEMENT_RS) %
168                      (NFP_FLOWER_MASK_ENTRY_RS * NFP_FLOWER_MASK_ELEMENT_RS);
169
170         getnstimeofday64(&now);
171         priv->mask_ids.last_used[mask_id] = now;
172
173         return 0;
174 }
175
176 static int nfp_mask_alloc(struct nfp_app *app, u8 *mask_id)
177 {
178         struct nfp_flower_priv *priv = app->priv;
179         struct timespec64 delta, now;
180         struct circ_buf *ring;
181         u8 temp_id, freed_id;
182
183         ring = &priv->mask_ids.mask_id_free_list;
184         freed_id = NFP_FLOWER_MASK_ENTRY_RS - 1;
185         /* Checking for unallocated entries first. */
186         if (priv->mask_ids.init_unallocated > 0) {
187                 *mask_id = priv->mask_ids.init_unallocated;
188                 priv->mask_ids.init_unallocated--;
189                 return 0;
190         }
191
192         /* Checking if buffer is empty. */
193         if (ring->head == ring->tail)
194                 goto err_not_found;
195
196         memcpy(&temp_id, &ring->buf[ring->tail], NFP_FLOWER_MASK_ELEMENT_RS);
197         *mask_id = temp_id;
198
199         getnstimeofday64(&now);
200         delta = timespec64_sub(now, priv->mask_ids.last_used[*mask_id]);
201
202         if (timespec64_to_ns(&delta) < NFP_FL_MASK_REUSE_TIME_NS)
203                 goto err_not_found;
204
205         memcpy(&ring->buf[ring->tail], &freed_id, NFP_FLOWER_MASK_ELEMENT_RS);
206         ring->tail = (ring->tail + NFP_FLOWER_MASK_ELEMENT_RS) %
207                      (NFP_FLOWER_MASK_ENTRY_RS * NFP_FLOWER_MASK_ELEMENT_RS);
208
209         return 0;
210
211 err_not_found:
212         *mask_id = freed_id;
213         return -ENOENT;
214 }
215
216 static int
217 nfp_add_mask_table(struct nfp_app *app, char *mask_data, u32 mask_len)
218 {
219         struct nfp_flower_priv *priv = app->priv;
220         struct nfp_mask_id_table *mask_entry;
221         unsigned long hash_key;
222         u8 mask_id;
223
224         if (nfp_mask_alloc(app, &mask_id))
225                 return -ENOENT;
226
227         mask_entry = kmalloc(sizeof(*mask_entry), GFP_KERNEL);
228         if (!mask_entry) {
229                 nfp_release_mask_id(app, mask_id);
230                 return -ENOMEM;
231         }
232
233         INIT_HLIST_NODE(&mask_entry->link);
234         mask_entry->mask_id = mask_id;
235         hash_key = jhash(mask_data, mask_len, priv->mask_id_seed);
236         mask_entry->hash_key = hash_key;
237         mask_entry->ref_cnt = 1;
238         hash_add(priv->mask_table, &mask_entry->link, hash_key);
239
240         return mask_id;
241 }
242
243 static struct nfp_mask_id_table *
244 nfp_search_mask_table(struct nfp_app *app, char *mask_data, u32 mask_len)
245 {
246         struct nfp_flower_priv *priv = app->priv;
247         struct nfp_mask_id_table *mask_entry;
248         unsigned long hash_key;
249
250         hash_key = jhash(mask_data, mask_len, priv->mask_id_seed);
251
252         hash_for_each_possible(priv->mask_table, mask_entry, link, hash_key)
253                 if (mask_entry->hash_key == hash_key)
254                         return mask_entry;
255
256         return NULL;
257 }
258
259 static int
260 nfp_find_in_mask_table(struct nfp_app *app, char *mask_data, u32 mask_len)
261 {
262         struct nfp_mask_id_table *mask_entry;
263
264         mask_entry = nfp_search_mask_table(app, mask_data, mask_len);
265         if (!mask_entry)
266                 return -ENOENT;
267
268         mask_entry->ref_cnt++;
269
270         /* Casting u8 to int for later use. */
271         return mask_entry->mask_id;
272 }
273
274 static bool
275 nfp_check_mask_add(struct nfp_app *app, char *mask_data, u32 mask_len,
276                    u8 *meta_flags, u8 *mask_id)
277 {
278         int id;
279
280         id = nfp_find_in_mask_table(app, mask_data, mask_len);
281         if (id < 0) {
282                 id = nfp_add_mask_table(app, mask_data, mask_len);
283                 if (id < 0)
284                         return false;
285                 *meta_flags |= NFP_FL_META_FLAG_MANAGE_MASK;
286         }
287         *mask_id = id;
288
289         return true;
290 }
291
292 static bool
293 nfp_check_mask_remove(struct nfp_app *app, char *mask_data, u32 mask_len,
294                       u8 *meta_flags, u8 *mask_id)
295 {
296         struct nfp_mask_id_table *mask_entry;
297
298         mask_entry = nfp_search_mask_table(app, mask_data, mask_len);
299         if (!mask_entry)
300                 return false;
301
302         if (meta_flags)
303                 *meta_flags &= ~NFP_FL_META_FLAG_MANAGE_MASK;
304
305         *mask_id = mask_entry->mask_id;
306         mask_entry->ref_cnt--;
307         if (!mask_entry->ref_cnt) {
308                 hash_del(&mask_entry->link);
309                 nfp_release_mask_id(app, *mask_id);
310                 kfree(mask_entry);
311                 if (meta_flags)
312                         *meta_flags |= NFP_FL_META_FLAG_MANAGE_MASK;
313         }
314
315         return true;
316 }
317
318 int nfp_compile_flow_metadata(struct nfp_app *app,
319                               struct tc_cls_flower_offload *flow,
320                               struct nfp_fl_payload *nfp_flow)
321 {
322         struct nfp_flower_priv *priv = app->priv;
323         struct nfp_fl_payload *check_entry;
324         u8 new_mask_id;
325         u32 stats_cxt;
326
327         if (nfp_get_stats_entry(app, &stats_cxt))
328                 return -ENOENT;
329
330         nfp_flow->meta.host_ctx_id = cpu_to_be32(stats_cxt);
331         nfp_flow->meta.host_cookie = cpu_to_be64(flow->cookie);
332
333         new_mask_id = 0;
334         if (!nfp_check_mask_add(app, nfp_flow->mask_data,
335                                 nfp_flow->meta.mask_len,
336                                 &nfp_flow->meta.flags, &new_mask_id)) {
337                 if (nfp_release_stats_entry(app, stats_cxt))
338                         return -EINVAL;
339                 return -ENOENT;
340         }
341
342         nfp_flow->meta.flow_version = cpu_to_be64(priv->flower_version);
343         priv->flower_version++;
344
345         /* Update flow payload with mask ids. */
346         nfp_flow->unmasked_data[NFP_FL_MASK_ID_LOCATION] = new_mask_id;
347         nfp_flow->stats.pkts = 0;
348         nfp_flow->stats.bytes = 0;
349         nfp_flow->stats.used = jiffies;
350
351         check_entry = nfp_flower_search_fl_table(app, flow->cookie);
352         if (check_entry) {
353                 if (nfp_release_stats_entry(app, stats_cxt))
354                         return -EINVAL;
355
356                 if (!nfp_check_mask_remove(app, nfp_flow->mask_data,
357                                            nfp_flow->meta.mask_len,
358                                            NULL, &new_mask_id))
359                         return -EINVAL;
360
361                 return -EEXIST;
362         }
363
364         return 0;
365 }
366
367 int nfp_modify_flow_metadata(struct nfp_app *app,
368                              struct nfp_fl_payload *nfp_flow)
369 {
370         struct nfp_flower_priv *priv = app->priv;
371         u8 new_mask_id = 0;
372         u32 temp_ctx_id;
373
374         nfp_check_mask_remove(app, nfp_flow->mask_data,
375                               nfp_flow->meta.mask_len, &nfp_flow->meta.flags,
376                               &new_mask_id);
377
378         nfp_flow->meta.flow_version = cpu_to_be64(priv->flower_version);
379         priv->flower_version++;
380
381         /* Update flow payload with mask ids. */
382         nfp_flow->unmasked_data[NFP_FL_MASK_ID_LOCATION] = new_mask_id;
383
384         /* Release the stats ctx id. */
385         temp_ctx_id = be32_to_cpu(nfp_flow->meta.host_ctx_id);
386
387         return nfp_release_stats_entry(app, temp_ctx_id);
388 }
389
390 int nfp_flower_metadata_init(struct nfp_app *app)
391 {
392         struct nfp_flower_priv *priv = app->priv;
393
394         hash_init(priv->mask_table);
395         hash_init(priv->flow_table);
396         get_random_bytes(&priv->mask_id_seed, sizeof(priv->mask_id_seed));
397
398         /* Init ring buffer and unallocated mask_ids. */
399         priv->mask_ids.mask_id_free_list.buf =
400                 kmalloc_array(NFP_FLOWER_MASK_ENTRY_RS,
401                               NFP_FLOWER_MASK_ELEMENT_RS, GFP_KERNEL);
402         if (!priv->mask_ids.mask_id_free_list.buf)
403                 return -ENOMEM;
404
405         priv->mask_ids.init_unallocated = NFP_FLOWER_MASK_ENTRY_RS - 1;
406
407         /* Init timestamps for mask id*/
408         priv->mask_ids.last_used =
409                 kmalloc_array(NFP_FLOWER_MASK_ENTRY_RS,
410                               sizeof(*priv->mask_ids.last_used), GFP_KERNEL);
411         if (!priv->mask_ids.last_used)
412                 goto err_free_mask_id;
413
414         /* Init ring buffer and unallocated stats_ids. */
415         priv->stats_ids.free_list.buf =
416                 vmalloc(NFP_FL_STATS_ENTRY_RS * NFP_FL_STATS_ELEM_RS);
417         if (!priv->stats_ids.free_list.buf)
418                 goto err_free_last_used;
419
420         priv->stats_ids.init_unalloc = NFP_FL_REPEATED_HASH_MAX;
421
422         return 0;
423
424 err_free_last_used:
425         kfree(priv->mask_ids.last_used);
426 err_free_mask_id:
427         kfree(priv->mask_ids.mask_id_free_list.buf);
428         return -ENOMEM;
429 }
430
431 void nfp_flower_metadata_cleanup(struct nfp_app *app)
432 {
433         struct nfp_flower_priv *priv = app->priv;
434
435         if (!priv)
436                 return;
437
438         kfree(priv->mask_ids.mask_id_free_list.buf);
439         kfree(priv->mask_ids.last_used);
440         vfree(priv->stats_ids.free_list.buf);
441 }