Skip to content

Commit 956af6e

Browse files
mrpreKernel Patches Daemon
authored andcommitted
selftest/bpf/benchs: Add cpu-affinity for sockmap bench
Add cpu-affinity for sockmap bench. Also add no-verify args to avoid validating data for performance enhancements. Signed-off-by: Jiayuan Chen <[email protected]>
1 parent da166b9 commit 956af6e

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

tools/testing/selftests/bpf/benchs/bench_sockmap.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ enum SOCKMAP_ARG_FLAG {
4343
ARG_FW_TX_VERDICT_INGRESS,
4444
ARG_FW_TX_VERDICT_EGRESS,
4545
ARG_CTL_RX_STRP,
46+
ARG_CTL_CPU_AFFINITY,
47+
ARG_CTL_NO_VERIFY,
4648
ARG_CONSUMER_DELAY_TIME,
4749
ARG_PRODUCER_DURATION,
4850
};
@@ -109,6 +111,8 @@ static struct socmap_ctx {
109111
int delay_consumer;
110112
int prod_run_time;
111113
int strp_size;
114+
int cpu_affinity;
115+
int skip_verify;
112116
} ctx = {
113117
.prod_send = 0,
114118
.user_read = 0,
@@ -118,6 +122,8 @@ static struct socmap_ctx {
118122
.delay_consumer = 0,
119123
.prod_run_time = 0,
120124
.strp_size = 0,
125+
.cpu_affinity = 0,
126+
.skip_verify = 0,
121127
};
122128

123129
static void bench_sockmap_prog_destroy(void)
@@ -235,11 +241,18 @@ static int create_sockets(void)
235241
static void validate(void)
236242
{
237243
if (env.consumer_cnt != 2 || env.producer_cnt != 1 ||
238-
!env.affinity)
244+
!env.affinity) {
245+
fprintf(stderr, "argument '-c 2 -p 1 -a' is necessary\n");
239246
goto err;
247+
}
248+
249+
if (!ctx.cpu_affinity && env.nr_cpus < 4) {
250+
fprintf(stderr, "4 CPU are needed to test cpu-affinity\n");
251+
goto err;
252+
}
253+
240254
return;
241255
err:
242-
fprintf(stderr, "argument '-c 2 -p 1 -a' is necessary");
243256
exit(1);
244257
}
245258

@@ -327,6 +340,9 @@ static void setup(void)
327340
exit(1);
328341
}
329342

343+
if (ctx.cpu_affinity)
344+
ctx.skel->data->redir_cpu = 3;
345+
330346
if (create_sockets()) {
331347
fprintf(stderr, "create_net_mode error\n");
332348
goto err;
@@ -367,9 +383,12 @@ static void measure(struct bench_res *res)
367383

368384
static void verify_data(int *check_pos, char *buf, int rcv)
369385
{
386+
if (ctx.skip_verify)
387+
return;
388+
370389
for (int i = 0 ; i < rcv; i++) {
371390
if (buf[i] != snd_data[(*check_pos) % DATA_REPEAT_SIZE]) {
372-
fprintf(stderr, "verify data fail");
391+
fprintf(stderr, "verify data fail\n");
373392
exit(1);
374393
}
375394
(*check_pos)++;
@@ -553,6 +572,10 @@ static const struct argp_option opts[] = {
553572
"delay consumer start"},
554573
{ "producer-duration", ARG_PRODUCER_DURATION, "SEC", 0,
555574
"producer duration"},
575+
{ "cpu-affinity", ARG_CTL_CPU_AFFINITY, NULL, 0,
576+
"set cpu-affinity for sockmap backlog thread"},
577+
{ "no-verify", ARG_CTL_NO_VERIFY, NULL, 0,
578+
"skip data validation for performance enhancements"},
556579
{},
557580
};
558581

@@ -571,6 +594,12 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
571594
case ARG_CTL_RX_STRP:
572595
ctx.strp_size = strtol(arg, NULL, 10);
573596
break;
597+
case ARG_CTL_CPU_AFFINITY:
598+
ctx.cpu_affinity = 1;
599+
break;
600+
case ARG_CTL_NO_VERIFY:
601+
ctx.skip_verify = 1;
602+
break;
574603
default:
575604
return ARGP_ERR_UNKNOWN;
576605
}

tools/testing/selftests/bpf/bpf_kfuncs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,10 @@ extern int bpf_set_dentry_xattr(struct dentry *dentry, const char *name__str,
9292
const struct bpf_dynptr *value_p, int flags) __ksym __weak;
9393
extern int bpf_remove_dentry_xattr(struct dentry *dentry, const char *name__str) __ksym __weak;
9494

95+
/* Description
96+
* Set sockmap redir cpu
97+
* Returns
98+
* Error code
99+
*/
100+
extern int bpf_sk_skb_set_redirect_cpu(struct __sk_buff *skb, int redir_cpu) __ksym;
95101
#endif

tools/testing/selftests/bpf/progs/bench_sockmap_prog.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
#include <linux/bpf.h>
33
#include <bpf/bpf_helpers.h>
44
#include <bpf/bpf_endian.h>
5+
#include <stdbool.h>
6+
#include "bpf_kfuncs.h"
57

68
long process_byte = 0;
79
int verdict_dir = 0;
810
int dropped = 0;
911
int pkt_size = 0;
12+
int redir_cpu = -1;
13+
1014
struct {
1115
__uint(type, BPF_MAP_TYPE_SOCKMAP);
1216
__uint(max_entries, 20);
@@ -33,6 +37,9 @@ int prog_skb_verdict(struct __sk_buff *skb)
3337
int one = 1;
3438
int ret = bpf_sk_redirect_map(skb, &sock_map_rx, one, verdict_dir);
3539

40+
if (redir_cpu != -1)
41+
bpf_sk_skb_set_redirect_cpu(skb, redir_cpu);
42+
3643
if (ret == SK_DROP)
3744
dropped++;
3845
__sync_fetch_and_add(&process_byte, skb->len);

0 commit comments

Comments
 (0)