Skip to contentSkip to conversation

bpf: Allow error injection for update_socket_protocol

PATCH In review Gang Yan 3 participants 3 messages Aug 21, 2024

Subsystems (3)
lore
Share & tools
lore

Gang Yan

author
#0 lore raw

PATCH 1/1 bpf: Allow error injection for update_socket_protocol

From: Gang Yan <[email protected]>

The "update_socket_protocol" interface is designed to empower user space with the capability to customize and modify socket protocols leveraging BPF methods. Currently, it has only granted the fmod_ret permission, allowing for modifications to return values. We are extending the permissions further by 'ALLOW_ERROR_INJECTION', thereby facilitating the development of user-space programs with enhanced flexibility and convenience.

When we attempt to modify the return value of "update_socket_protocol" to "IPPROTO_MPTCP" using the below code based on the BCC tool:

'''
int kprobe__update_socket_protocol(void* ctx)

{
    ...
    bpf_override_return(ctx,IPPROTO_MPTCP);
    ...
}

'''

But an error occurs:

'''
ioctl(PERF_EVENT_IOC_SET_BPF): Invalid argument Traceback (most recent call last):
File "/media/yangang/work/Code/BCC/test.py", line 27, in <module>
b = BPF(text=prog)
^^^^^^^^^^^^^^

  File "/usr/lib/python3/dist-packages/bcc/__init__.py", line 487, \
                  in __init__ self._trace_autoload()
  File "/usr/lib/python3/dist-packages/bcc/__init__.py", line 1466, \
                  in _trace_autoload self.attach_kprobe(

File "/usr/lib/python3/dist-packages/bcc/__init__.py", line 855,\
in attach_kprobe
raise Exception("Failed to attach BPF program %s to kprobe %s"
Exception: Failed to attach BPF program b'kprobe__update_socket_protocol' \
to kprobe b'update_socket_protocol', it's not traceable \
(either non-existing, inlined, or marked as "notrace")
'''

This patch can fix the issue.

---
 net/socket.c | 1 +
 1 file changed, 1 insertion(+)
net/socket.c +1 -0
index fcbdd5bc47ac..63ce1caf75eb 100644
@@ -1695,6 +1695,7 @@ __weak noinline int update_socket_protocol(int family, int type, int protocol)
 {
 	return protocol;
 }
+ALLOW_ERROR_INJECTION(update_socket_protocol, ERRNO);
 
 __bpf_hook_end();
 
Signature · 1 line
2.43.0
#1 lore raw

Hi Gang,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal: Success! ✅
- KVM Validation: debug: Success! ✅
- KVM Validation: btf (only bpftest_all): Success! ✅
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/10482667406

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/0757fd384ece
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=881543

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)

maintainer
#2 lore raw
In reply to Gang Yan

Hi Gang, Geliang,

Quoted 34 lines · Gang Yan

From: Gang Yan <[email protected]>

The "update_socket_protocol" interface is designed to empower user space with the capability to customize and modify socket protocols leveraging BPF methods. Currently, it has only granted the fmod_ret permission, allowing for modifications to return values. We are extending the permissions further by 'ALLOW_ERROR_INJECTION', thereby facilitating the development of user-space programs with enhanced flexibility and convenience.

When we attempt to modify the return value of "update_socket_protocol" to "IPPROTO_MPTCP" using the below code based on the BCC tool:

'''
int kprobe__update_socket_protocol(void* ctx)

{
    ...
    bpf_override_return(ctx,IPPROTO_MPTCP);
    ...
}

'''

But an error occurs:

'''
ioctl(PERF_EVENT_IOC_SET_BPF): Invalid argument Traceback (most recent call last):
File "/media/yangang/work/Code/BCC/test.py", line 27, in <module>
b = BPF(text=prog)
^^^^^^^^^^^^^^

  File "/usr/lib/python3/dist-packages/bcc/__init__.py", line 487, \
                  in __init__ self._trace_autoload()
  File "/usr/lib/python3/dist-packages/bcc/__init__.py", line 1466, \
                  in _trace_autoload self.attach_kprobe(
Quoted 7 lines

File "/usr/lib/python3/dist-packages/bcc/__init__.py", line 855,\
in attach_kprobe
raise Exception("Failed to attach BPF program %s to kprobe %s"
Exception: Failed to attach BPF program b'kprobe__update_socket_protocol' \
to kprobe b'update_socket_protocol', it's not traceable \
(either non-existing, inlined, or marked as "notrace")
'''

Thank you for this patch and this explanation!

The modification looks good to me, but this should be accepted by BPF and netdev maintainers, more than us, I think.

Do you mind sending this directly to netdev with the BPF ML in CC please?

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.