Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2021 Facebook */
0003 #include <vmlinux.h>
0004 #include <bpf/bpf_helpers.h>
0005 
0006 extern int bpf_kfunc_call_test2(struct sock *sk, __u32 a, __u32 b) __ksym;
0007 extern __u64 bpf_kfunc_call_test1(struct sock *sk, __u32 a, __u64 b,
0008                   __u32 c, __u64 d) __ksym;
0009 
0010 extern struct prog_test_ref_kfunc *bpf_kfunc_call_test_acquire(unsigned long *sp) __ksym;
0011 extern void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) __ksym;
0012 extern void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) __ksym;
0013 extern void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) __ksym;
0014 extern void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) __ksym;
0015 extern void bpf_kfunc_call_test_mem_len_pass1(void *mem, int len) __ksym;
0016 extern void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym;
0017 
0018 SEC("tc")
0019 int kfunc_call_test2(struct __sk_buff *skb)
0020 {
0021     struct bpf_sock *sk = skb->sk;
0022 
0023     if (!sk)
0024         return -1;
0025 
0026     sk = bpf_sk_fullsock(sk);
0027     if (!sk)
0028         return -1;
0029 
0030     return bpf_kfunc_call_test2((struct sock *)sk, 1, 2);
0031 }
0032 
0033 SEC("tc")
0034 int kfunc_call_test1(struct __sk_buff *skb)
0035 {
0036     struct bpf_sock *sk = skb->sk;
0037     __u64 a = 1ULL << 32;
0038     __u32 ret;
0039 
0040     if (!sk)
0041         return -1;
0042 
0043     sk = bpf_sk_fullsock(sk);
0044     if (!sk)
0045         return -1;
0046 
0047     a = bpf_kfunc_call_test1((struct sock *)sk, 1, a | 2, 3, a | 4);
0048     ret = a >> 32;   /* ret should be 2 */
0049     ret += (__u32)a; /* ret should be 12 */
0050 
0051     return ret;
0052 }
0053 
0054 SEC("tc")
0055 int kfunc_call_test_ref_btf_id(struct __sk_buff *skb)
0056 {
0057     struct prog_test_ref_kfunc *pt;
0058     unsigned long s = 0;
0059     int ret = 0;
0060 
0061     pt = bpf_kfunc_call_test_acquire(&s);
0062     if (pt) {
0063         if (pt->a != 42 || pt->b != 108)
0064             ret = -1;
0065         bpf_kfunc_call_test_release(pt);
0066     }
0067     return ret;
0068 }
0069 
0070 SEC("tc")
0071 int kfunc_call_test_pass(struct __sk_buff *skb)
0072 {
0073     struct prog_test_pass1 p1 = {};
0074     struct prog_test_pass2 p2 = {};
0075     short a = 0;
0076     __u64 b = 0;
0077     long c = 0;
0078     char d = 0;
0079     int e = 0;
0080 
0081     bpf_kfunc_call_test_pass_ctx(skb);
0082     bpf_kfunc_call_test_pass1(&p1);
0083     bpf_kfunc_call_test_pass2(&p2);
0084 
0085     bpf_kfunc_call_test_mem_len_pass1(&a, sizeof(a));
0086     bpf_kfunc_call_test_mem_len_pass1(&b, sizeof(b));
0087     bpf_kfunc_call_test_mem_len_pass1(&c, sizeof(c));
0088     bpf_kfunc_call_test_mem_len_pass1(&d, sizeof(d));
0089     bpf_kfunc_call_test_mem_len_pass1(&e, sizeof(e));
0090     bpf_kfunc_call_test_mem_len_fail2(&b, -1);
0091 
0092     return 0;
0093 }
0094 
0095 char _license[] SEC("license") = "GPL";