On 6/12/23 10:23, Stanislav Fomichev wrote: ..... cut .....
+ +__diag_push(); +__diag_ignore_all("-Wmissing-prototypes", + "Global functions as their definitions will be in vmlinux BTF"); + +/** + * bpf_devtx_sb_attach - Attach devtx 'packet submit' program + * @ifindex: netdev interface index. + * @prog_fd: BPF program file descriptor. + * + * Return: + * * Returns 0 on success or ``-errno`` on error. + */ +__bpf_kfunc int bpf_devtx_sb_attach(int ifindex, int prog_fd) +{ + struct net_device *netdev; + int ret; + + netdev = dev_get_by_index(current->nsproxy->net_ns, ifindex); + if (!netdev) + return -EINVAL; + + mutex_lock(&devtx_attach_lock); + ret = __bpf_devtx_attach(netdev, prog_fd, "devtx_sb", &netdev->devtx_sb); + mutex_unlock(&devtx_attach_lock); + + dev_put(netdev); + + return ret; +}
How about adding another detach kfunc instead of overloading this one? It is easier to understand.
+ +/** + * bpf_devtx_cp_attach - Attach devtx 'packet complete' program + * @ifindex: netdev interface index. + * @prog_fd: BPF program file descriptor. + * + * Return: + * * Returns 0 on success or ``-errno`` on error. + */ +__bpf_kfunc int bpf_devtx_cp_attach(int ifindex, int prog_fd) +{ + struct net_device *netdev; + int ret; + + netdev = dev_get_by_index(current->nsproxy->net_ns, ifindex); + if (!netdev) + return -EINVAL; + + mutex_lock(&devtx_attach_lock); + ret = __bpf_devtx_attach(netdev, prog_fd, "devtx_cp", &netdev->devtx_cp); + mutex_unlock(&devtx_attach_lock); + + dev_put(netdev); + + return ret; +} + +__diag_pop(); + +bool is_devtx_kfunc(u32 kfunc_id) +{ + return !!btf_id_set8_contains(&bpf_devtx_hook_ids, kfunc_id); +} + +void devtx_shutdown(struct net_device *netdev) +{ + mutex_lock(&devtx_attach_lock); + __bpf_devtx_detach(netdev, &netdev->devtx_sb); + __bpf_devtx_detach(netdev, &netdev->devtx_cp); + mutex_unlock(&devtx_attach_lock); +} + +BTF_SET8_START(bpf_devtx_syscall_kfunc_ids) +BTF_ID_FLAGS(func, bpf_devtx_sb_attach) +BTF_ID_FLAGS(func, bpf_devtx_cp_attach) +BTF_SET8_END(bpf_devtx_syscall_kfunc_ids) + +static const struct btf_kfunc_id_set bpf_devtx_syscall_kfunc_set = { + .owner = THIS_MODULE, + .set = &bpf_devtx_syscall_kfunc_ids, +}; + +static int __init devtx_init(void) +{ + int ret; + + ret = register_btf_fmodret_id_set(&bpf_devtx_hook_set); + if (ret) { + pr_warn("failed to register devtx hooks: %d", ret); + return ret; + } + + ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_devtx_syscall_kfunc_set); + if (ret) { + pr_warn("failed to register syscall kfuncs: %d", ret); + return ret; + } + + return 0; +} +late_initcall(devtx_init);