Skip to content

bpf, sockmap: Introduce tracing capability for sockmap #8763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -4429,6 +4429,7 @@ L: [email protected]
L: [email protected]
S: Maintained
F: include/linux/skmsg.h
F: include/trace/events/sockmap.h
F: net/core/skmsg.c
F: net/core/sock_map.c
F: net/ipv4/tcp_bpf.c
Expand Down
1 change: 1 addition & 0 deletions include/linux/bpf_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
#define __LINUX_BPF_TRACE_H__

#include <trace/events/xdp.h>
#include <trace/events/sockmap.h>

#endif /* __LINUX_BPF_TRACE_H__ */
158 changes: 158 additions & 0 deletions include/trace/events/sockmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/* SPDX-License-Identifier: GPL-2.0 */
#undef TRACE_SYSTEM
#define TRACE_SYSTEM sockmap

#if !defined(_TRACE_SOCKMAP_H) || defined(TRACE_HEADER_MULTI_READ)
#define _TRACE_SOCKMAP_H

#include <linux/tracepoint.h>
#include <linux/bpf.h>
#include <linux/skmsg.h>

#ifndef __TRACE_SOCKMAP_HELPER_ONCE_ONLY
#define __TRACE_SOCKMAP_HELPER_ONCE_ONLY

enum sockmap_direct_type {
SOCKMAP_REDIR_NONE = 0,
SOCKMAP_REDIR_INGRESS,
SOCKMAP_REDIR_EGRESS,
};

enum sockmap_data_type {
SOCKMAP_MSG = 0,
SOCKMAP_SKB,
};

#endif /* end __TRACE_SOCKMAP_HELPER_ONCE_ONLY */

TRACE_DEFINE_ENUM(SOCKMAP_MSG);
TRACE_DEFINE_ENUM(SOCKMAP_SKB);
TRACE_DEFINE_ENUM(SOCKMAP_REDIR_NONE);
TRACE_DEFINE_ENUM(SOCKMAP_REDIR_INGRESS);
TRACE_DEFINE_ENUM(SOCKMAP_REDIR_EGRESS);

TRACE_DEFINE_ENUM(__SK_DROP);
TRACE_DEFINE_ENUM(__SK_PASS);
TRACE_DEFINE_ENUM(__SK_REDIRECT);
TRACE_DEFINE_ENUM(__SK_NONE);

#define show_redirect_type(x) \
__print_symbolic(x, \
{ SOCKMAP_REDIR_NONE, "none" }, \
{ SOCKMAP_REDIR_INGRESS, "ingress" }, \
{ SOCKMAP_REDIR_EGRESS, "egress" })

#define show_act(x) \
__print_symbolic(x, \
{ __SK_DROP, "DROP" }, \
{ __SK_PASS, "PASS" }, \
{ __SK_REDIRECT, "REDIRECT" }, \
{ __SK_NONE, "NONE" })

#define show_data_type(x) \
__print_symbolic(x, \
{ SOCKMAP_MSG, "msg" }, \
{ SOCKMAP_SKB, "skb" })

#define trace_sockmap_skmsg_redirect(sk, prog, msg, act) \
trace_sockmap_redirect((sk), SOCKMAP_MSG, (prog), \
(msg)->sg.size, (act), \
sk_msg_to_ingress(msg))

#define trace_sockmap_skb_redirect(sk, prog, skb, act) \
trace_sockmap_redirect((sk), SOCKMAP_SKB, (prog), \
(skb)->len, (act), \
skb_bpf_ingress(skb))

#define trace_sockmap_skb_strp_parse(sk, prog, skb, ret) \
trace_sockmap_strparser((sk), (prog), (skb)->len, (ret))

TRACE_EVENT(sockmap_redirect,

TP_PROTO(const struct sock *sk, enum sockmap_data_type type,
const struct bpf_prog *prog, int len, int act,
bool ingress),

TP_ARGS(sk, type, prog, len, act, ingress),

TP_STRUCT__entry(
__field(const void *, sk)
__field(unsigned long, ino)
__field(unsigned int, netns_ino)
__field(__u16, family)
__field(__u16, protocol)
__field(int, prog_id)
__field(int, len)
__field(int, act)
__field(enum sockmap_data_type, type)
__field(enum sockmap_direct_type, redir)
),

TP_fast_assign(
/* 'redir' is undefined if action is not REDIRECT */
enum sockmap_direct_type redir = SOCKMAP_REDIR_NONE;

if (act == __SK_REDIRECT) {
if (ingress)
redir = SOCKMAP_REDIR_INGRESS;
else
redir = SOCKMAP_REDIR_EGRESS;
}
__entry->sk = sk;
__entry->ino = sock_i_ino((struct sock *)sk);
__entry->netns_ino = sock_net(sk)->ns.inum;
__entry->type = type;
__entry->family = sk->sk_family;
__entry->protocol = sk->sk_protocol;
__entry->prog_id = prog->aux->id;
__entry->len = len;
__entry->act = act;
__entry->redir = redir;
),

TP_printk("sk=%p, netns=%u, inode=%lu, family=%u, protocol=%u,"
" prog_id=%d, len=%d, type=%s, action=%s, redirect_type=%s",
__entry->sk, __entry->netns_ino, __entry->ino,
__entry->family, __entry->protocol, __entry->prog_id,
__entry->len, show_data_type(__entry->type),
show_act(__entry->act), show_redirect_type(__entry->redir))
);

TRACE_EVENT(sockmap_strparser,

TP_PROTO(const struct sock *sk, const struct bpf_prog *prog,
int in_len, int full_len),

TP_ARGS(sk, prog, in_len, full_len),

TP_STRUCT__entry(
__field(const void *, sk)
__field(unsigned long, ino)
__field(unsigned int, netns_ino)
__field(__u16, family)
__field(__u16, protocol)
__field(int, prog_id)
__field(int, in_len)
__field(int, full_len)
),

TP_fast_assign(
__entry->sk = sk;
__entry->ino = sock_i_ino((struct sock *)sk);
__entry->netns_ino = sock_net(sk)->ns.inum;
__entry->family = sk->sk_family;
__entry->protocol = sk->sk_protocol;
__entry->prog_id = prog->aux->id;
__entry->in_len = in_len;
__entry->full_len = full_len;
),

TP_printk("sk=%p, netns=%u, inode=%lu, family=%u, protocol=%u,"
" prog_id=%d, in_len=%d, full_len=%d",
__entry->sk, __entry->netns_ino, __entry->ino,
__entry->family, __entry->protocol, __entry->prog_id,
__entry->in_len, __entry->full_len)
);
#endif /* _TRACE_SOCKMAP_H */

#include <trace/define_trace.h>
7 changes: 0 additions & 7 deletions kernel/bpf/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -3180,10 +3180,3 @@ late_initcall(bpf_global_ma_init);

DEFINE_STATIC_KEY_FALSE(bpf_stats_enabled_key);
EXPORT_SYMBOL(bpf_stats_enabled_key);

/* All definitions of tracepoints related to BPF. */
#define CREATE_TRACE_POINTS
#include <linux/bpf_trace.h>

EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_bulk_tx);
1 change: 1 addition & 0 deletions net/bpf/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-$(CONFIG_BPF_SYSCALL) := test_run.o
obj-$(CONFIG_BPF_SYSCALL) += bpf_net_trace.o
ifeq ($(CONFIG_BPF_JIT),y)
obj-$(CONFIG_BPF_SYSCALL) += bpf_dummy_struct_ops.o
endif
8 changes: 8 additions & 0 deletions net/bpf/bpf_net_trace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: GPL-2.0

/* All definitions of net tracepoints related to BPF. */
#define CREATE_TRACE_POINTS
#include <linux/bpf_trace.h>

EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_bulk_tx);
6 changes: 6 additions & 0 deletions net/core/skmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <net/tcp.h>
#include <net/tls.h>
#include <trace/events/sock.h>
#include <trace/events/sockmap.h>

static bool sk_msg_try_coalesce_ok(struct sk_msg *msg, int elem_first_coalesce)
{
Expand Down Expand Up @@ -910,6 +911,7 @@ int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock,
sock_hold(psock->sk_redir);
}
out:
trace_sockmap_skmsg_redirect(sk, prog, msg, ret);
rcu_read_unlock();
return ret;
}
Expand Down Expand Up @@ -981,6 +983,7 @@ int sk_psock_tls_strp_read(struct sk_psock *psock, struct sk_buff *skb)
ret = bpf_prog_run_pin_on_cpu(prog, skb);
ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
skb->sk = NULL;
trace_sockmap_skb_redirect(psock->sk, prog, skb, ret);
}
sk_psock_tls_verdict_apply(skb, psock, ret);
rcu_read_unlock();
Expand Down Expand Up @@ -1090,6 +1093,7 @@ static void sk_psock_strp_read(struct strparser *strp, struct sk_buff *skb)
skb_bpf_set_strparser(skb);
ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
skb->sk = NULL;
trace_sockmap_skb_redirect(sk, prog, skb, ret);
}
sk_psock_verdict_apply(psock, skb, ret);
out:
Expand All @@ -1113,6 +1117,7 @@ static int sk_psock_strp_parse(struct strparser *strp, struct sk_buff *skb)
skb->sk = psock->sk;
ret = bpf_prog_run_pin_on_cpu(prog, skb);
skb->sk = NULL;
trace_sockmap_skb_strp_parse(psock->sk, prog, skb, ret);
}
rcu_read_unlock();
return ret;
Expand Down Expand Up @@ -1217,6 +1222,7 @@ static int sk_psock_verdict_recv(struct sock *sk, struct sk_buff *skb)
skb_bpf_redirect_clear(skb);
ret = bpf_prog_run_pin_on_cpu(prog, skb);
ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
trace_sockmap_skb_redirect(psock->sk, prog, skb, ret);
}
ret = sk_psock_verdict_apply(psock, skb, ret);
if (ret < 0)
Expand Down
Loading