Skip to content

Commit 193d000

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bulk-cpumap-redirect'
Jesper Dangaard Brouer says: ==================== This patchset utilize a number of different kernel bulk APIs for optimizing the performance for the XDP cpumap redirect feature. Benchmark details are available here: https://github.com/xdp-project/xdp-project/blob/master/areas/cpumap/cpumap03-optimizations.org Performance measurements can be considered micro benchmarks, as they measure dropping packets at different stages in the network stack. Summary based on above: Baseline benchmarks - baseline-redirect: UdpNoPorts: 3,180,074 - baseline-redirect: iptables-raw drop: 6,193,534 Patch1: bpf: cpumap use ptr_ring_consume_batched - redirect: UdpNoPorts: 3,327,729 - redirect: iptables-raw drop: 6,321,540 Patch2: net: core: introduce build_skb_around - redirect: UdpNoPorts: 3,221,303 - redirect: iptables-raw drop: 6,320,066 Patch3: bpf: cpumap do bulk allocation of SKBs - redirect: UdpNoPorts: 3,290,563 - redirect: iptables-raw drop: 6,650,112 Patch4: bpf: cpumap memory prefetchw optimizations for struct page - redirect: UdpNoPorts: 3,520,250 - redirect: iptables-raw drop: 7,649,604 In this V2 submission I have chosen drop the SKB-list patch using netif_receive_skb_list() as it was not showing a performance improvement for these micro benchmarks. ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 00967e8 + 86d2314 commit 193d000

3 files changed

Lines changed: 91 additions & 35 deletions

File tree

‎include/linux/skbuff.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,8 @@ struct sk_buff *__alloc_skb(unsigned int size, gfp_t priority, int flags,
10421042
int node);
10431043
struct sk_buff *__build_skb(void *data, unsigned int frag_size);
10441044
struct sk_buff *build_skb(void *data, unsigned int frag_size);
1045+
struct sk_buff *build_skb_around(struct sk_buff *skb,
1046+
void *data, unsigned int frag_size);
10451047

10461048
/**
10471049
* alloc_skb - allocate a network buffer

‎kernel/bpf/cpumap.c‎

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ static void cpu_map_kthread_stop(struct work_struct *work)
160160
}
161161

162162
static struct sk_buff *cpu_map_build_skb(struct bpf_cpu_map_entry *rcpu,
163-
struct xdp_frame *xdpf)
163+
struct xdp_frame *xdpf,
164+
struct sk_buff *skb)
164165
{
165166
unsigned int hard_start_headroom;
166167
unsigned int frame_size;
167168
void *pkt_data_start;
168-
struct sk_buff *skb;
169169

170170
/* Part of headroom was reserved to xdpf */
171171
hard_start_headroom = sizeof(struct xdp_frame) + xdpf->headroom;
@@ -191,8 +191,8 @@ static struct sk_buff *cpu_map_build_skb(struct bpf_cpu_map_entry *rcpu,
191191
SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
192192

193193
pkt_data_start = xdpf->data - hard_start_headroom;
194-
skb = build_skb(pkt_data_start, frame_size);
195-
if (!skb)
194+
skb = build_skb_around(skb, pkt_data_start, frame_size);
195+
if (unlikely(!skb))
196196
return NULL;
197197

198198
skb_reserve(skb, hard_start_headroom);
@@ -240,6 +240,8 @@ static void put_cpu_map_entry(struct bpf_cpu_map_entry *rcpu)
240240
}
241241
}
242242

243+
#define CPUMAP_BATCH 8
244+
243245
static int cpu_map_kthread_run(void *data)
244246
{
245247
struct bpf_cpu_map_entry *rcpu = data;
@@ -252,8 +254,11 @@ static int cpu_map_kthread_run(void *data)
252254
* kthread_stop signal until queue is empty.
253255
*/
254256
while (!kthread_should_stop() || !__ptr_ring_empty(rcpu->queue)) {
255-
unsigned int processed = 0, drops = 0, sched = 0;
256-
struct xdp_frame *xdpf;
257+
unsigned int drops = 0, sched = 0;
258+
void *frames[CPUMAP_BATCH];
259+
void *skbs[CPUMAP_BATCH];
260+
gfp_t gfp = __GFP_ZERO | GFP_ATOMIC;
261+
int i, n, m;
257262

258263
/* Release CPU reschedule checks */
259264
if (__ptr_ring_empty(rcpu->queue)) {
@@ -269,18 +274,38 @@ static int cpu_map_kthread_run(void *data)
269274
sched = cond_resched();
270275
}
271276

272-
/* Process packets in rcpu->queue */
273-
local_bh_disable();
274277
/*
275278
* The bpf_cpu_map_entry is single consumer, with this
276279
* kthread CPU pinned. Lockless access to ptr_ring
277280
* consume side valid as no-resize allowed of queue.
278281
*/
279-
while ((xdpf = __ptr_ring_consume(rcpu->queue))) {
280-
struct sk_buff *skb;
282+
n = ptr_ring_consume_batched(rcpu->queue, frames, CPUMAP_BATCH);
283+
284+
for (i = 0; i < n; i++) {
285+
void *f = frames[i];
286+
struct page *page = virt_to_page(f);
287+
288+
/* Bring struct page memory area to curr CPU. Read by
289+
* build_skb_around via page_is_pfmemalloc(), and when
290+
* freed written by page_frag_free call.
291+
*/
292+
prefetchw(page);
293+
}
294+
295+
m = kmem_cache_alloc_bulk(skbuff_head_cache, gfp, n, skbs);
296+
if (unlikely(m == 0)) {
297+
for (i = 0; i < n; i++)
298+
skbs[i] = NULL; /* effect: xdp_return_frame */
299+
drops = n;
300+
}
301+
302+
local_bh_disable();
303+
for (i = 0; i < n; i++) {
304+
struct xdp_frame *xdpf = frames[i];
305+
struct sk_buff *skb = skbs[i];
281306
int ret;
282307

283-
skb = cpu_map_build_skb(rcpu, xdpf);
308+
skb = cpu_map_build_skb(rcpu, xdpf, skb);
284309
if (!skb) {
285310
xdp_return_frame(xdpf);
286311
continue;
@@ -290,13 +315,9 @@ static int cpu_map_kthread_run(void *data)
290315
ret = netif_receive_skb_core(skb);
291316
if (ret == NET_RX_DROP)
292317
drops++;
293-
294-
/* Limit BH-disable period */
295-
if (++processed == 8)
296-
break;
297318
}
298319
/* Feedback loop via tracepoint */
299-
trace_xdp_cpumap_kthread(rcpu->map_id, processed, drops, sched);
320+
trace_xdp_cpumap_kthread(rcpu->map_id, n, drops, sched);
300321

301322
local_bh_enable(); /* resched point, may call do_softirq() */
302323
}

‎net/core/skbuff.c‎

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,33 @@ struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
258258
}
259259
EXPORT_SYMBOL(__alloc_skb);
260260

261+
/* Caller must provide SKB that is memset cleared */
262+
static struct sk_buff *__build_skb_around(struct sk_buff *skb,
263+
void *data, unsigned int frag_size)
264+
{
265+
struct skb_shared_info *shinfo;
266+
unsigned int size = frag_size ? : ksize(data);
267+
268+
size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
269+
270+
/* Assumes caller memset cleared SKB */
271+
skb->truesize = SKB_TRUESIZE(size);
272+
refcount_set(&skb->users, 1);
273+
skb->head = data;
274+
skb->data = data;
275+
skb_reset_tail_pointer(skb);
276+
skb->end = skb->tail + size;
277+
skb->mac_header = (typeof(skb->mac_header))~0U;
278+
skb->transport_header = (typeof(skb->transport_header))~0U;
279+
280+
/* make sure we initialize shinfo sequentially */
281+
shinfo = skb_shinfo(skb);
282+
memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
283+
atomic_set(&shinfo->dataref, 1);
284+
285+
return skb;
286+
}
287+
261288
/**
262289
* __build_skb - build a network buffer
263290
* @data: data buffer provided by caller
@@ -279,32 +306,15 @@ EXPORT_SYMBOL(__alloc_skb);
279306
*/
280307
struct sk_buff *__build_skb(void *data, unsigned int frag_size)
281308
{
282-
struct skb_shared_info *shinfo;
283309
struct sk_buff *skb;
284-
unsigned int size = frag_size ? : ksize(data);
285310

286311
skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
287-
if (!skb)
312+
if (unlikely(!skb))
288313
return NULL;
289314

290-
size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
291-
292315
memset(skb, 0, offsetof(struct sk_buff, tail));
293-
skb->truesize = SKB_TRUESIZE(size);
294-
refcount_set(&skb->users, 1);
295-
skb->head = data;
296-
skb->data = data;
297-
skb_reset_tail_pointer(skb);
298-
skb->end = skb->tail + size;
299-
skb->mac_header = (typeof(skb->mac_header))~0U;
300-
skb->transport_header = (typeof(skb->transport_header))~0U;
301316

302-
/* make sure we initialize shinfo sequentially */
303-
shinfo = skb_shinfo(skb);
304-
memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
305-
atomic_set(&shinfo->dataref, 1);
306-
307-
return skb;
317+
return __build_skb_around(skb, data, frag_size);
308318
}
309319

310320
/* build_skb() is wrapper over __build_skb(), that specifically
@@ -325,6 +335,29 @@ struct sk_buff *build_skb(void *data, unsigned int frag_size)
325335
}
326336
EXPORT_SYMBOL(build_skb);
327337

338+
/**
339+
* build_skb_around - build a network buffer around provided skb
340+
* @skb: sk_buff provide by caller, must be memset cleared
341+
* @data: data buffer provided by caller
342+
* @frag_size: size of data, or 0 if head was kmalloced
343+
*/
344+
struct sk_buff *build_skb_around(struct sk_buff *skb,
345+
void *data, unsigned int frag_size)
346+
{
347+
if (unlikely(!skb))
348+
return NULL;
349+
350+
skb = __build_skb_around(skb, data, frag_size);
351+
352+
if (skb && frag_size) {
353+
skb->head_frag = 1;
354+
if (page_is_pfmemalloc(virt_to_head_page(data)))
355+
skb->pfmemalloc = 1;
356+
}
357+
return skb;
358+
}
359+
EXPORT_SYMBOL(build_skb_around);
360+
328361
#define NAPI_SKB_CACHE_SIZE 64
329362

330363
struct napi_alloc_cache {

0 commit comments

Comments
 (0)