Skip to content

Commit 3084887

Browse files
4astdavem330
authored andcommitted
tools/lib/bpf: add support for BPF_PROG_TEST_RUN command
add support for BPF_PROG_TEST_RUN command to libbpf.a Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Acked-by: Wang Nan <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1cf1cae commit 3084887

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

tools/include/uapi/linux/bpf.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ enum bpf_cmd {
8181
BPF_OBJ_GET,
8282
BPF_PROG_ATTACH,
8383
BPF_PROG_DETACH,
84+
BPF_PROG_TEST_RUN,
8485
};
8586

8687
enum bpf_map_type {
@@ -189,6 +190,17 @@ union bpf_attr {
189190
__u32 attach_type;
190191
__u32 attach_flags;
191192
};
193+
194+
struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
195+
__u32 prog_fd;
196+
__u32 retval;
197+
__u32 data_size_in;
198+
__u32 data_size_out;
199+
__aligned_u64 data_in;
200+
__aligned_u64 data_out;
201+
__u32 repeat;
202+
__u32 duration;
203+
} test;
192204
} __attribute__((aligned(8)));
193205

194206
/* BPF helper function descriptions:
@@ -459,6 +471,18 @@ union bpf_attr {
459471
* Return:
460472
* > 0 length of the string including the trailing NUL on success
461473
* < 0 error
474+
*
475+
* u64 bpf_bpf_get_socket_cookie(skb)
476+
* Get the cookie for the socket stored inside sk_buff.
477+
* @skb: pointer to skb
478+
* Return: 8 Bytes non-decreasing number on success or 0 if the socket
479+
* field is missing inside sk_buff
480+
*
481+
* u32 bpf_get_socket_uid(skb)
482+
* Get the owner uid of the socket stored inside sk_buff.
483+
* @skb: pointer to skb
484+
* Return: uid of the socket owner on success or 0 if the socket pointer
485+
* inside sk_buff is NULL
462486
*/
463487
#define __BPF_FUNC_MAPPER(FN) \
464488
FN(unspec), \

tools/lib/bpf/bpf.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,27 @@ int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
209209

210210
return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
211211
}
212+
213+
int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
214+
void *data_out, __u32 *size_out, __u32 *retval,
215+
__u32 *duration)
216+
{
217+
union bpf_attr attr;
218+
int ret;
219+
220+
bzero(&attr, sizeof(attr));
221+
attr.test.prog_fd = prog_fd;
222+
attr.test.data_in = ptr_to_u64(data);
223+
attr.test.data_out = ptr_to_u64(data_out);
224+
attr.test.data_size_in = size;
225+
attr.test.repeat = repeat;
226+
227+
ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
228+
if (size_out)
229+
*size_out = attr.test.data_size_out;
230+
if (retval)
231+
*retval = attr.test.retval;
232+
if (duration)
233+
*duration = attr.test.duration;
234+
return ret;
235+
}

tools/lib/bpf/bpf.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ int bpf_obj_get(const char *pathname);
4747
int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type,
4848
unsigned int flags);
4949
int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
50-
50+
int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
51+
void *data_out, __u32 *size_out, __u32 *retval,
52+
__u32 *duration);
5153

5254
#endif

0 commit comments

Comments
 (0)