Skip to content

Commit b6ca132

Browse files
Improve xdplua_user's packet counting
1 parent 50e056a commit b6ca132

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

samples/bpf/xdplua_user.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ static void usage(const char *prog) {
3838
" -p eBPF program path\n"
3939
" -i iface\n"
4040
" -m monitor\n"
41-
" -s lua script\n",
41+
" -s lua script\n"
42+
" -I Interval\n"
43+
" -D Duration\n",
4244
prog);
4345
}
4446

@@ -103,13 +105,19 @@ static int do_detach(int idx, const char *name)
103105
return err;
104106
}
105107

106-
static void poll(int map_fd, int interval) {
107-
long cnt;
108+
static void poll(int map_fd, int interval, int duration) {
109+
unsigned int nr_cpus = bpf_num_possible_cpus();
110+
long cnts[nr_cpus];
108111
unsigned int key = 0;
109112

110-
while(1) {
111-
bpf_map_lookup_elem(map_fd, &key, &cnt);
112-
printf("pkt count: %lu\n", cnt);
113+
for (int i = 0; i < duration; i++) {
114+
unsigned long cnt = 0;
115+
int i;
116+
bpf_map_lookup_elem(map_fd, &key, cnts);
117+
for (i = 0; i < nr_cpus; ++i) {
118+
cnt += cnts[i];
119+
}
120+
printf("%lu\n", cnt);
113121
sleep(interval);
114122
}
115123
}
@@ -123,9 +131,11 @@ int main(int argc, char *argv[])
123131
struct bpf_object *obj;
124132
int opt, prog_fd;
125133
int rx_cnt_map_fd;
126-
int detach = 0, attach_lua = 0, attach_ebpf = 0, monitor = 0, attach_script = 0;
134+
int detach = 0, attach_lua = 0, attach_ebpf = 0, monitor = 0, attach_script = 0,
135+
interval = 1, duration = 1;
136+
127137
char *lua_prog = NULL;
128-
const char *optstr = "f:p:i:dms:";
138+
const char *optstr = "f:p:i:dms:I:D:";
129139
struct bpf_prog_load_attr prog_load_attr = {
130140
.prog_type = BPF_PROG_TYPE_XDP,
131141
};
@@ -158,12 +168,19 @@ int main(int argc, char *argv[])
158168
"%s", optarg);
159169
attach_script = 1;
160170
break;
171+
case 'I':
172+
interval = atoi(optarg);
173+
break;
174+
case 'D':
175+
duration = atoi(optarg);
176+
break;
161177
default:
162178
usage(basename(argv[0]));
163179
return 1;
164180
}
165181
}
166182

183+
167184
if (attach_ebpf || detach) {
168185
if (!ifindex) {
169186
printf("ERROR: invalid interface name");
@@ -219,7 +236,7 @@ int main(int argc, char *argv[])
219236
if (monitor) {
220237
rx_cnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rx_cnt");
221238

222-
poll(rx_cnt_map_fd, 1);
239+
poll(rx_cnt_map_fd, interval, duration);
223240
}
224241
return 0;
225242
}

0 commit comments

Comments
 (0)