1 From: Gregory CLEMENT <gregory.clement@free-electrons.com>
2 Date: Mon, 14 Mar 2016 09:39:04 +0100
3 Subject: [PATCH] net: add a hardware buffer management helper API
5 This basic implementation allows to share code between driver using
6 hardware buffer management. As the code is hardware agnostic, there is
7 few helpers, most of the optimization brought by the an HW BM has to be
10 Tested-by: Sebastian Careba <nitroshift@yahoo.com>
11 Signed-off-by: Gregory CLEMENT <gregory.clement@free-electrons.com>
12 Signed-off-by: David S. Miller <davem@davemloft.net>
14 create mode 100644 include/net/hwbm.h
15 create mode 100644 net/core/hwbm.c
18 +++ b/include/net/hwbm.h
24 + /* Capacity of the pool */
26 + /* Size of the buffers managed */
28 + /* Number of buffers currently used by this pool */
30 + /* constructor called during alocation */
31 + int (*construct)(struct hwbm_pool *bm_pool, void *buf);
32 + /* protect acces to the buffer counter*/
38 +void hwbm_buf_free(struct hwbm_pool *bm_pool, void *buf);
39 +int hwbm_pool_refill(struct hwbm_pool *bm_pool, gfp_t gfp);
40 +int hwbm_pool_add(struct hwbm_pool *bm_pool, unsigned int buf_num, gfp_t gfp);
42 +void hwbm_buf_free(struct hwbm_pool *bm_pool, void *buf) {}
43 +int hwbm_pool_refill(struct hwbm_pool *bm_pool, gfp_t gfp) { return 0; }
44 +int hwbm_pool_add(struct hwbm_pool *bm_pool, unsigned int buf_num, gfp_t gfp)
46 +#endif /* CONFIG_HWBM */
50 @@ -259,6 +259,9 @@ config XPS
57 config CGROUP_NET_PRIO
58 bool "Network priority cgroup"
60 --- a/net/core/Makefile
61 +++ b/net/core/Makefile
62 @@ -14,6 +14,7 @@ obj-y += dev.o ethtool.o dev_addr_
63 obj-$(CONFIG_SOCK_DIAG) += sock_diag.o
64 obj-$(CONFIG_XFRM) += flow.o
66 +obj-$(CONFIG_HWBM) += hwbm.o
67 obj-$(CONFIG_PROC_FS) += net-procfs.o
68 obj-$(CONFIG_NET_PKTGEN) += pktgen.o
69 obj-$(CONFIG_NETPOLL) += netpoll.o
73 +/* Support for hardware buffer manager.
75 + * Copyright (C) 2016 Marvell
77 + * Gregory CLEMENT <gregory.clement@free-electrons.com>
79 + * This program is free software; you can redistribute it and/or modify
80 + * it under the terms of the GNU General Public License as published by
81 + * the Free Software Foundation; either version 2 of the License, or
82 + * (at your option) any later version.
84 +#include <linux/kernel.h>
85 +#include <linux/printk.h>
86 +#include <linux/skbuff.h>
87 +#include <net/hwbm.h>
89 +void hwbm_buf_free(struct hwbm_pool *bm_pool, void *buf)
91 + if (likely(bm_pool->frag_size <= PAGE_SIZE))
96 +EXPORT_SYMBOL_GPL(hwbm_buf_free);
98 +/* Refill processing for HW buffer management */
99 +int hwbm_pool_refill(struct hwbm_pool *bm_pool, gfp_t gfp)
101 + int frag_size = bm_pool->frag_size;
104 + if (likely(frag_size <= PAGE_SIZE))
105 + buf = netdev_alloc_frag(frag_size);
107 + buf = kmalloc(frag_size, gfp);
112 + if (bm_pool->construct)
113 + if (bm_pool->construct(bm_pool, buf)) {
114 + hwbm_buf_free(bm_pool, buf);
120 +EXPORT_SYMBOL_GPL(hwbm_pool_refill);
122 +int hwbm_pool_add(struct hwbm_pool *bm_pool, unsigned int buf_num, gfp_t gfp)
125 + unsigned long flags;
127 + spin_lock_irqsave(&bm_pool->lock, flags);
128 + if (bm_pool->buf_num == bm_pool->size) {
129 + pr_warn("pool already filled\n");
130 + return bm_pool->buf_num;
133 + if (buf_num + bm_pool->buf_num > bm_pool->size) {
134 + pr_warn("cannot allocate %d buffers for pool\n",
139 + if ((buf_num + bm_pool->buf_num) < bm_pool->buf_num) {
140 + pr_warn("Adding %d buffers to the %d current buffers will overflow\n",
141 + buf_num, bm_pool->buf_num);
145 + for (i = 0; i < buf_num; i++) {
146 + err = hwbm_pool_refill(bm_pool, gfp);
151 + /* Update BM driver with number of buffers added to pool */
152 + bm_pool->buf_num += i;
154 + pr_debug("hwpm pool: %d of %d buffers added\n", i, buf_num);
155 + spin_unlock_irqrestore(&bm_pool->lock, flags);
159 +EXPORT_SYMBOL_GPL(hwbm_pool_add);