Skip to content

Commit 4d2dba4

Browse files
wedsonafSven Van Asbroeck
authored and
Sven Van Asbroeck
committed
WIP: Add ping test.
Just a preview of the Binder ping test.
1 parent b4b5942 commit 4d2dba4

File tree

4 files changed

+259
-0
lines changed

4 files changed

+259
-0
lines changed

tools/testing/selftests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0
22
TARGETS = arm64
3+
TARGETS += binder
34
TARGETS += bpf
45
TARGETS += breakpoints
56
TARGETS += capabilities
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
CFLAGS += -I../../../../usr/include/
4+
CFLAGS += -g
5+
CFLAGS += -D_GNU_SOURCE
6+
CFLAGS += -Wall
7+
CFLAGS += -pthread
8+
9+
TEST_GEN_PROGS := ping_server ping_client
10+
11+
include ../lib.mk
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include <stdio.h>
2+
#include <stdint.h>
3+
#include <string.h>
4+
#include <stdalign.h>
5+
#include <time.h>
6+
7+
#include <unistd.h>
8+
#include <sys/types.h>
9+
#include <sys/stat.h>
10+
#include <sys/ioctl.h>
11+
#include <sys/mman.h>
12+
#include <fcntl.h>
13+
#include <linux/ioctl.h>
14+
#include <linux/android/binder.h>
15+
16+
static size_t handle_read(int fd, struct binder_write_read *b)
17+
{
18+
char *ptr = (char *)b->read_buffer;
19+
char *limit = ptr + b->read_consumed;
20+
struct binder_transaction_data *tr;
21+
size_t ret = 0;
22+
23+
while (limit - ptr >= sizeof(uint32_t)) {
24+
uint32_t v = *(uint32_t *)ptr;
25+
ptr += sizeof(uint32_t);
26+
switch (v) {
27+
case BR_REPLY:
28+
tr = (struct binder_transaction_data *)ptr;
29+
ptr += sizeof(*tr);
30+
if (ptr > limit) {
31+
printf("Truncated transaction.\n");
32+
break;
33+
}
34+
ret = tr->data.ptr.buffer;
35+
break;
36+
37+
case BR_NOOP:
38+
break;
39+
40+
case BR_TRANSACTION_COMPLETE:
41+
break;
42+
43+
default:
44+
printf("Unknown reply: 0x%x\n", v);
45+
ptr = limit;
46+
}
47+
}
48+
49+
return ret;
50+
}
51+
52+
int main(int argc, char **argv)
53+
{
54+
int ret;
55+
size_t i;
56+
struct timespec begin, end;
57+
58+
if (argc != 2) {
59+
fprintf(stderr, "Usage: %s <binder-file-name>\n", argv[0]);
60+
return 1;
61+
}
62+
63+
int fd = open(argv[1], O_RDWR);
64+
if (fd == -1) {
65+
perror("open");
66+
return 1;
67+
}
68+
69+
void *shared_ptr = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE, fd, 0);
70+
if (shared_ptr == MAP_FAILED) {
71+
perror("mmap");
72+
return 1;
73+
}
74+
75+
uint32_t buf[512];
76+
static struct __attribute__((__packed__)) {
77+
uint32_t cmd0;
78+
size_t addr;
79+
uint32_t cmd1;
80+
struct binder_transaction_data tr;
81+
} to_write = {
82+
.cmd0 = BC_FREE_BUFFER,
83+
.addr = 0,
84+
.cmd1 = BC_TRANSACTION,
85+
.tr.target.handle = 0,
86+
.tr.code = 0,
87+
};
88+
struct binder_write_read b = {
89+
.write_buffer = (size_t)&to_write,
90+
.write_size = sizeof(to_write),
91+
.read_buffer = (size_t)&buf[0],
92+
.read_size = sizeof(buf),
93+
};
94+
ret = ioctl(fd, BINDER_WRITE_READ, &b);
95+
if (ret == -1) {
96+
perror("ioctl");
97+
return 1;
98+
}
99+
100+
to_write.addr = handle_read(fd, &b);
101+
102+
clock_gettime(CLOCK_MONOTONIC, &begin);
103+
for (i = 0; i < 1000000; i++) {
104+
b.read_consumed = 0;
105+
b.write_consumed = 0;
106+
ret = ioctl(fd, BINDER_WRITE_READ, &b);
107+
if (ret == -1) {
108+
perror("ioctl");
109+
return 1;
110+
}
111+
to_write.addr = handle_read(fd, &b);
112+
}
113+
clock_gettime(CLOCK_MONOTONIC, &end);
114+
115+
end.tv_sec -= begin.tv_sec;
116+
if (begin.tv_nsec > end.tv_nsec) {
117+
end.tv_sec--;
118+
end.tv_nsec += 1000000000;
119+
}
120+
end.tv_nsec -= begin.tv_nsec;
121+
printf("Total time: %lld.%.9ld\n", (long long)end.tv_sec, end.tv_nsec);
122+
123+
close(fd);
124+
125+
return 0;
126+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include <stdio.h>
2+
#include <stdint.h>
3+
#include <string.h>
4+
#include <stdalign.h>
5+
6+
#include <unistd.h>
7+
#include <sys/types.h>
8+
#include <sys/stat.h>
9+
#include <sys/ioctl.h>
10+
#include <sys/mman.h>
11+
#include <fcntl.h>
12+
#include <linux/ioctl.h>
13+
#include <linux/android/binder.h>
14+
15+
static void handle_read(int fd, struct binder_write_read* b)
16+
{
17+
static struct __attribute__((__packed__)) {
18+
uint32_t cmd0;
19+
size_t ptr;
20+
uint32_t cmd1;
21+
struct binder_transaction_data tr;
22+
} to_write = {
23+
.cmd0 = BC_FREE_BUFFER,
24+
.cmd1 = BC_REPLY,
25+
.tr.target.handle = 0,
26+
.tr.code = 0,
27+
};
28+
char *ptr = (char *)b->read_buffer;
29+
char *limit = ptr + b->read_consumed;
30+
struct binder_transaction_data *tr;
31+
32+
while (limit - ptr >= sizeof(uint32_t)) {
33+
uint32_t v = *(uint32_t *)ptr;
34+
ptr += sizeof(uint32_t);
35+
switch (v) {
36+
case BR_TRANSACTION:
37+
tr = (struct binder_transaction_data *)ptr;
38+
ptr += sizeof(*tr);
39+
if (ptr > limit) {
40+
printf("Truncated transaction.\n");
41+
break;
42+
}
43+
44+
to_write.ptr = tr->data.ptr.buffer;
45+
b->write_buffer = (size_t)&to_write;
46+
b->write_size = sizeof(to_write);
47+
b->write_consumed = 0;
48+
break;
49+
50+
case BR_NOOP:
51+
break;
52+
53+
case BR_TRANSACTION_COMPLETE:
54+
break;
55+
56+
default:
57+
printf("Unknown reply: 0x%x\n", v);
58+
ptr = limit;
59+
}
60+
}
61+
}
62+
63+
int main(int argc, char **argv)
64+
{
65+
int ret;
66+
67+
if (argc != 2) {
68+
fprintf(stderr, "Usage: %s <binder-file-name>\n", argv[0]);
69+
return 1;
70+
}
71+
72+
int fd = open(argv[1], O_RDWR);
73+
if (fd == -1) {
74+
perror("open");
75+
return 1;
76+
}
77+
78+
void *shared_ptr = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE, fd, 0);
79+
if (shared_ptr == MAP_FAILED) {
80+
perror("mmap");
81+
return 1;
82+
}
83+
84+
ret = ioctl(fd, BINDER_SET_CONTEXT_MGR, NULL);
85+
if (ret == -1) {
86+
perror("ioctl");
87+
return 1;
88+
}
89+
90+
{
91+
uint32_t cmd = BC_ENTER_LOOPER;
92+
struct binder_write_read b = {
93+
.write_buffer = (size_t)&cmd,
94+
.write_size = sizeof(cmd),
95+
};
96+
int ret = ioctl(fd, BINDER_WRITE_READ, &b);
97+
if (ret == -1) {
98+
perror("ioctl(BC_ENTER_LOOPER)");
99+
}
100+
}
101+
102+
uint32_t buf[512];
103+
struct binder_write_read b = {
104+
.read_buffer = (size_t)&buf[0],
105+
.read_size = sizeof(buf),
106+
};
107+
for (;;) {
108+
b.read_consumed = 0;
109+
ret = ioctl(fd, BINDER_WRITE_READ, &b);
110+
if (ret == -1) {
111+
perror("ioctl");
112+
continue;
113+
}
114+
115+
handle_read(fd, &b);
116+
}
117+
118+
close(fd);
119+
120+
return 0;
121+
}

0 commit comments

Comments
 (0)