Skip to content

Commit

Permalink
bpf_skc_to_ssk_sock
Browse files Browse the repository at this point in the history
Signed-off-by: Geliang Tang <[email protected]>
  • Loading branch information
Geliang Tang committed Nov 22, 2024
1 parent 7b097bc commit 7feec72
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 17 deletions.
1 change: 1 addition & 0 deletions include/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -3259,6 +3259,7 @@ extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
extern const struct bpf_func_proto bpf_skc_to_unix_sock_proto;
extern const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto;
extern const struct bpf_func_proto bpf_skc_to_ssk_sock_proto;
extern const struct bpf_func_proto bpf_copy_from_user_proto;
extern const struct bpf_func_proto bpf_snprintf_btf_proto;
extern const struct bpf_func_proto bpf_snprintf_proto;
Expand Down
7 changes: 7 additions & 0 deletions include/uapi/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -5796,6 +5796,12 @@ union bpf_attr {
* 0 on success.
*
* **-ENOENT** if the bpf_local_storage cannot be found.
*
* void *bpf_skc_to_ssk_sock(void *sk)
* Description
* Dynamically cast a *sk* pointer to a *sock* pointer.
* Return
* *sk* if casting is valid, or **NULL** otherwise.
*/
#define ___BPF_FUNC_MAPPER(FN, ctx...) \
FN(unspec, 0, ##ctx) \
Expand Down Expand Up @@ -6010,6 +6016,7 @@ union bpf_attr {
FN(user_ringbuf_drain, 209, ##ctx) \
FN(cgrp_storage_get, 210, ##ctx) \
FN(cgrp_storage_delete, 211, ##ctx) \
FN(skc_to_ssk_sock, 212, ##ctx) \
/* */

/* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
Expand Down
17 changes: 17 additions & 0 deletions net/core/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -11844,6 +11844,20 @@ const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = {
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_MPTCP],
};

BPF_CALL_1(bpf_skc_to_ssk_sock, struct sock *, sk)
{
BTF_TYPE_EMIT(struct sock);
return (unsigned long)sk;
}

const struct bpf_func_proto bpf_skc_to_ssk_sock_proto = {
.func = bpf_skc_to_ssk_sock,
.gpl_only = false,
.ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
.arg1_type = ARG_ANYTHING,
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK],
};

BPF_CALL_1(bpf_sock_from_file, struct file *, file)
{
return (unsigned long)sock_from_file(file);
Expand Down Expand Up @@ -11889,6 +11903,9 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
case BPF_FUNC_skc_to_mptcp_sock:
func = &bpf_skc_to_mptcp_sock_proto;
break;
case BPF_FUNC_skc_to_ssk_sock:
func = &bpf_skc_to_ssk_sock_proto;
break;
case BPF_FUNC_ktime_get_coarse_ns:
return &bpf_ktime_get_coarse_ns_proto;
default:
Expand Down
2 changes: 2 additions & 0 deletions net/mptcp/bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ bpf_mptcp_sched_get_func_proto(enum bpf_func_id func_id,
return &bpf_skc_to_tcp6_sock_proto;
case BPF_FUNC_skc_to_tcp_sock:
return &bpf_skc_to_tcp_sock_proto;
case BPF_FUNC_skc_to_ssk_sock:
return &bpf_skc_to_ssk_sock_proto;
default:
return bpf_base_func_proto(func_id, prog);
}
Expand Down
7 changes: 7 additions & 0 deletions tools/include/uapi/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -5796,6 +5796,12 @@ union bpf_attr {
* 0 on success.
*
* **-ENOENT** if the bpf_local_storage cannot be found.
*
* void *bpf_skc_to_ssk_sock(void *sk)
* Description
* Dynamically cast a *sk* pointer to a *sock* pointer.
* Return
* *sk* if casting is valid, or **NULL** otherwise.
*/
#define ___BPF_FUNC_MAPPER(FN, ctx...) \
FN(unspec, 0, ##ctx) \
Expand Down Expand Up @@ -6010,6 +6016,7 @@ union bpf_attr {
FN(user_ringbuf_drain, 209, ##ctx) \
FN(cgrp_storage_get, 210, ##ctx) \
FN(cgrp_storage_delete, 211, ##ctx) \
FN(skc_to_ssk_sock, 212, ##ctx) \
/* */

/* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
Expand Down
25 changes: 9 additions & 16 deletions tools/testing/selftests/bpf/progs/mptcp_bpf_burst.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ char _license[] SEC("license") = "GPL";

#define min(a, b) ((a) < (b) ? (a) : (b))

struct bpf_subflow_send_info {
struct mptcp_subflow_context *subflow;
__u64 linger_time;
};

extern bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) __ksym;
extern void mptcp_set_timeout(struct sock *sk) __ksym;
extern __u64 mptcp_wnd_end(const struct mptcp_sock *msk) __ksym;
Expand Down Expand Up @@ -70,7 +65,7 @@ void BPF_PROG(mptcp_sched_burst_release, struct mptcp_sock *msk)
SEC("struct_ops")
int BPF_PROG(bpf_burst_get_send, struct mptcp_sock *msk)
{
struct bpf_subflow_send_info send_info[SSK_MODE_MAX];
struct subflow_send_info send_info[SSK_MODE_MAX];
struct mptcp_subflow_context *subflow;
struct sock *sk = (struct sock *)msk;
__u32 pace, burst, wmem;
Expand All @@ -80,7 +75,7 @@ int BPF_PROG(bpf_burst_get_send, struct mptcp_sock *msk)

/* pick the subflow with the lower wmem/wspace ratio */
for (i = 0; i < SSK_MODE_MAX; ++i) {
send_info[i].subflow = NULL;
send_info[i].ssk = NULL;
send_info[i].linger_time = -1;
}

Expand All @@ -103,24 +98,22 @@ int BPF_PROG(bpf_burst_get_send, struct mptcp_sock *msk)

linger_time = div_u64((__u64)ssk->sk_wmem_queued << 32, pace);
if (linger_time < send_info[backup].linger_time) {
if (!sk_stream_memory_free(ssk))
continue;
send_info[backup].subflow = subflow;
send_info[backup].ssk = ssk;
send_info[backup].linger_time = linger_time;
}
}
mptcp_set_timeout(sk);

/* pick the best backup if no other subflow is active */
if (!nr_active)
send_info[SSK_MODE_ACTIVE].subflow = send_info[SSK_MODE_BACKUP].subflow;
send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk;

subflow = bpf_core_cast(send_info[SSK_MODE_ACTIVE].subflow,
struct mptcp_subflow_context);
if (!subflow)
ssk = bpf_skc_to_ssk_sock(send_info[SSK_MODE_ACTIVE].ssk);
if (!ssk || !sk_stream_memory_free(ssk))
return -1;
ssk = mptcp_subflow_tcp_sock(subflow);
if (!ssk)

subflow = bpf_mptcp_subflow_ctx(ssk);
if (!subflow)
return -1;

burst = min(MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt);
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/bpf/progs/mptcp_bpf_rr.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ static int bpf_rr_get_subflow(struct mptcp_sock *msk)
}

out:
next = bpf_core_cast(next, struct mptcp_subflow_context);
mptcp_subflow_set_scheduled(next, true);
//bpf_printk("rr subflow=%u/%u", next->subflow_id, msk->pm.subflows + 1);
ptr->last_snd = mptcp_subflow_tcp_sock(next);
Expand Down

0 comments on commit 7feec72

Please sign in to comment.