Commit ea9d5616 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Martin KaFai Lau

selftests/bpf: convert bpf_rdonly_cast() uses to bpf_core_cast() macro

Use more ergonomic bpf_core_cast() macro instead of bpf_rdonly_cast() in
selftests code.
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20240130212023.183765-3-andrii@kernel.orgSigned-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
parent 20d59ee5
...@@ -28,8 +28,7 @@ int connect_unix_prog(struct bpf_sock_addr *ctx) ...@@ -28,8 +28,7 @@ int connect_unix_prog(struct bpf_sock_addr *ctx)
if (sa_kern->uaddrlen != unaddrlen) if (sa_kern->uaddrlen != unaddrlen)
return 0; return 0;
sa_kern_unaddr = bpf_rdonly_cast(sa_kern->uaddr, sa_kern_unaddr = bpf_core_cast(sa_kern->uaddr, struct sockaddr_un);
bpf_core_type_id_kernel(struct sockaddr_un));
if (memcmp(sa_kern_unaddr->sun_path, SERVUN_REWRITE_ADDRESS, if (memcmp(sa_kern_unaddr->sun_path, SERVUN_REWRITE_ADDRESS,
sizeof(SERVUN_REWRITE_ADDRESS) - 1) != 0) sizeof(SERVUN_REWRITE_ADDRESS) - 1) != 0)
return 0; return 0;
......
...@@ -27,8 +27,7 @@ int getpeername_unix_prog(struct bpf_sock_addr *ctx) ...@@ -27,8 +27,7 @@ int getpeername_unix_prog(struct bpf_sock_addr *ctx)
if (sa_kern->uaddrlen != unaddrlen) if (sa_kern->uaddrlen != unaddrlen)
return 1; return 1;
sa_kern_unaddr = bpf_rdonly_cast(sa_kern->uaddr, sa_kern_unaddr = bpf_core_cast(sa_kern->uaddr, struct sockaddr_un);
bpf_core_type_id_kernel(struct sockaddr_un));
if (memcmp(sa_kern_unaddr->sun_path, SERVUN_REWRITE_ADDRESS, if (memcmp(sa_kern_unaddr->sun_path, SERVUN_REWRITE_ADDRESS,
sizeof(SERVUN_REWRITE_ADDRESS) - 1) != 0) sizeof(SERVUN_REWRITE_ADDRESS) - 1) != 0)
return 1; return 1;
......
...@@ -27,8 +27,7 @@ int getsockname_unix_prog(struct bpf_sock_addr *ctx) ...@@ -27,8 +27,7 @@ int getsockname_unix_prog(struct bpf_sock_addr *ctx)
if (sa_kern->uaddrlen != unaddrlen) if (sa_kern->uaddrlen != unaddrlen)
return 1; return 1;
sa_kern_unaddr = bpf_rdonly_cast(sa_kern->uaddr, sa_kern_unaddr = bpf_core_cast(sa_kern->uaddr, struct sockaddr_un);
bpf_core_type_id_kernel(struct sockaddr_un));
if (memcmp(sa_kern_unaddr->sun_path, SERVUN_REWRITE_ADDRESS, if (memcmp(sa_kern_unaddr->sun_path, SERVUN_REWRITE_ADDRESS,
sizeof(SERVUN_REWRITE_ADDRESS) - 1) != 0) sizeof(SERVUN_REWRITE_ADDRESS) - 1) != 0)
return 1; return 1;
......
...@@ -27,8 +27,7 @@ int recvmsg_unix_prog(struct bpf_sock_addr *ctx) ...@@ -27,8 +27,7 @@ int recvmsg_unix_prog(struct bpf_sock_addr *ctx)
if (sa_kern->uaddrlen != unaddrlen) if (sa_kern->uaddrlen != unaddrlen)
return 1; return 1;
sa_kern_unaddr = bpf_rdonly_cast(sa_kern->uaddr, sa_kern_unaddr = bpf_core_cast(sa_kern->uaddr, struct sockaddr_un);
bpf_core_type_id_kernel(struct sockaddr_un));
if (memcmp(sa_kern_unaddr->sun_path, SERVUN_ADDRESS, if (memcmp(sa_kern_unaddr->sun_path, SERVUN_ADDRESS,
sizeof(SERVUN_ADDRESS) - 1) != 0) sizeof(SERVUN_ADDRESS) - 1) != 0)
return 1; return 1;
......
...@@ -28,8 +28,7 @@ int sendmsg_unix_prog(struct bpf_sock_addr *ctx) ...@@ -28,8 +28,7 @@ int sendmsg_unix_prog(struct bpf_sock_addr *ctx)
if (sa_kern->uaddrlen != unaddrlen) if (sa_kern->uaddrlen != unaddrlen)
return 0; return 0;
sa_kern_unaddr = bpf_rdonly_cast(sa_kern->uaddr, sa_kern_unaddr = bpf_core_cast(sa_kern->uaddr, struct sockaddr_un);
bpf_core_type_id_kernel(struct sockaddr_un));
if (memcmp(sa_kern_unaddr->sun_path, SERVUN_REWRITE_ADDRESS, if (memcmp(sa_kern_unaddr->sun_path, SERVUN_REWRITE_ADDRESS,
sizeof(SERVUN_REWRITE_ADDRESS) - 1) != 0) sizeof(SERVUN_REWRITE_ADDRESS) - 1) != 0)
return 0; return 0;
......
...@@ -27,7 +27,7 @@ int BPF_PROG(bpf_local_storage_destroy, struct bpf_local_storage *local_storage) ...@@ -27,7 +27,7 @@ int BPF_PROG(bpf_local_storage_destroy, struct bpf_local_storage *local_storage)
if (local_storage_ptr != local_storage) if (local_storage_ptr != local_storage)
return 0; return 0;
sk = bpf_rdonly_cast(sk_ptr, bpf_core_type_id_kernel(struct sock)); sk = bpf_core_cast(sk_ptr, struct sock);
if (sk->sk_cookie.counter != cookie) if (sk->sk_cookie.counter != cookie)
return 0; return 0;
......
...@@ -32,7 +32,7 @@ int iter_tcp_soreuse(struct bpf_iter__tcp *ctx) ...@@ -32,7 +32,7 @@ int iter_tcp_soreuse(struct bpf_iter__tcp *ctx)
if (!sk) if (!sk)
return 0; return 0;
sk = bpf_rdonly_cast(sk, bpf_core_type_id_kernel(struct sock)); sk = bpf_core_cast(sk, struct sock);
if (sk->sk_family != AF_INET6 || if (sk->sk_family != AF_INET6 ||
sk->sk_state != TCP_LISTEN || sk->sk_state != TCP_LISTEN ||
!ipv6_addr_loopback(&sk->sk_v6_rcv_saddr)) !ipv6_addr_loopback(&sk->sk_v6_rcv_saddr))
...@@ -68,7 +68,7 @@ int iter_udp_soreuse(struct bpf_iter__udp *ctx) ...@@ -68,7 +68,7 @@ int iter_udp_soreuse(struct bpf_iter__udp *ctx)
if (!sk) if (!sk)
return 0; return 0;
sk = bpf_rdonly_cast(sk, bpf_core_type_id_kernel(struct sock)); sk = bpf_core_cast(sk, struct sock);
if (sk->sk_family != AF_INET6 || if (sk->sk_family != AF_INET6 ||
!ipv6_addr_loopback(&sk->sk_v6_rcv_saddr)) !ipv6_addr_loopback(&sk->sk_v6_rcv_saddr))
return 0; return 0;
......
...@@ -46,13 +46,12 @@ int md_skb(struct __sk_buff *skb) ...@@ -46,13 +46,12 @@ int md_skb(struct __sk_buff *skb)
/* Simulate the following kernel macro: /* Simulate the following kernel macro:
* #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB))) * #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
*/ */
shared_info = bpf_rdonly_cast(kskb->head + kskb->end, shared_info = bpf_core_cast(kskb->head + kskb->end, struct skb_shared_info);
bpf_core_type_id_kernel(struct skb_shared_info));
meta_len = shared_info->meta_len; meta_len = shared_info->meta_len;
frag0_len = shared_info->frag_list->len; frag0_len = shared_info->frag_list->len;
/* kskb2 should be equal to kskb */ /* kskb2 should be equal to kskb */
kskb2 = bpf_rdonly_cast(kskb, bpf_core_type_id_kernel(struct sk_buff)); kskb2 = bpf_core_cast(kskb, typeof(*kskb2));
kskb2_len = kskb2->len; kskb2_len = kskb2->len;
return 0; return 0;
} }
...@@ -63,7 +62,7 @@ int BPF_PROG(untrusted_ptr, struct pt_regs *regs, long id) ...@@ -63,7 +62,7 @@ int BPF_PROG(untrusted_ptr, struct pt_regs *regs, long id)
struct task_struct *task, *task_dup; struct task_struct *task, *task_dup;
task = bpf_get_current_task_btf(); task = bpf_get_current_task_btf();
task_dup = bpf_rdonly_cast(task, bpf_core_type_id_kernel(struct task_struct)); task_dup = bpf_core_cast(task, struct task_struct);
(void)bpf_task_storage_get(&enter_id, task_dup, 0, 0); (void)bpf_task_storage_get(&enter_id, task_dup, 0, 0);
return 0; return 0;
} }
...@@ -71,7 +70,7 @@ int BPF_PROG(untrusted_ptr, struct pt_regs *regs, long id) ...@@ -71,7 +70,7 @@ int BPF_PROG(untrusted_ptr, struct pt_regs *regs, long id)
SEC("?tracepoint/syscalls/sys_enter_nanosleep") SEC("?tracepoint/syscalls/sys_enter_nanosleep")
int kctx_u64(void *ctx) int kctx_u64(void *ctx)
{ {
u64 *kctx = bpf_rdonly_cast(ctx, bpf_core_type_id_kernel(u64)); u64 *kctx = bpf_core_cast(ctx, u64);
(void)kctx; (void)kctx;
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment