bpf_load

作者: grimlock44 | 来源:发表于2022-06-16 08:55 被阅读0次
    static int bpf_prog_load(union bpf_attr *attr)
    {
        enum bpf_prog_type type = attr->prog_type;
        struct bpf_prog *prog;
        int err;
        char license[128];
        bool is_gpl;
    
        if (CHECK_ATTR(BPF_PROG_LOAD))   //check attr中不用的字段是否全零
            return -EINVAL;
    
        if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)  //只支持BPF_F_STRICT_ALIGNMENT
            return -EINVAL;
    
        /* copy eBPF program license from user space */
        if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
                      sizeof(license) - 1) < 0)
            return -EFAULT;
        license[sizeof(license) - 1] = 0;   //从用户态内存copy license字符串
    
        /* eBPF programs must be GPL compatible to use GPL-ed functions */
        is_gpl = license_is_gpl_compatible(license);   //不是gpl 有些helper fn不能使用  verify时校验
    
        if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)  //指令数校验
            return -E2BIG;
    
        if (type == BPF_PROG_TYPE_KPROBE &&
            attr->kern_version != LINUX_VERSION_CODE)   //kprobe类型的  要和内核版本一致
            return -EINVAL;
    
        if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
            type != BPF_PROG_TYPE_CGROUP_SKB &&        //两种不查权限的类型
            !capable(CAP_SYS_ADMIN))
            return -EPERM;
    
        bpf_prog_load_fixup_attach_type(attr);   //加了expected_attach_type字段  向后兼容
        if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))  //check expected_attach_type
            return -EINVAL;
    
        /* plain bpf_prog allocation */
        prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);   //申请bpf_prog内存 用户空间内存 详解
        if (!prog)
            return -ENOMEM;
    
        prog->expected_attach_type = attr->expected_attach_type;
    
        prog->aux->offload_requested = !!attr->prog_ifindex;
    
        err = security_bpf_prog_alloc(prog->aux);  //LSM相关
        if (err)
            goto free_prog_nouncharge;
    
        err = bpf_prog_charge_memlock(prog);   //bpf_prog的内存锁定到当前用户
        if (err)
            goto free_prog_sec;
    
        prog->len = attr->insn_cnt;
    
        err = -EFAULT;
        if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
                   bpf_prog_insn_size(prog)) != 0)       //copy指令 用户态内存到内核态内存
            goto free_prog;
    
        prog->orig_prog = NULL;
        prog->jited = 0;
    
        atomic_set(&prog->aux->refcnt, 1);   //引用计数
        prog->gpl_compatible = is_gpl ? 1 : 0;
    
        if (bpf_prog_is_dev_bound(prog->aux)) {
            err = bpf_prog_offload_init(prog, attr);  //绑定接口的需要初始化offload
            if (err)
                goto free_prog;
        }
    
        /* find program type: socket_filter vs tracing_filter */
        err = find_prog_type(type, prog);  //按类型 绑定bpf_prog_ops
        if (err < 0)
            goto free_prog;
    
        prog->aux->load_time = ktime_get_boot_ns();
        err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
        if (err)
            goto free_prog;
    
        /* run eBPF verifier */
        err = bpf_check(&prog, attr);
        if (err < 0)
            goto free_used_maps;
    
        prog = bpf_prog_select_runtime(prog, &err);     //选择执行器
        if (err < 0)
            goto free_used_maps;
    
        err = bpf_prog_alloc_id(prog);              //分配id
        if (err)
            goto free_used_maps;
    
        err = bpf_prog_new_fd(prog);                   //分配fd
        if (err < 0) {
            /* failed to allocate fd.
             * bpf_prog_put() is needed because the above
             * bpf_prog_alloc_id() has published the prog
             * to the userspace and the userspace may
             * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
             */
            bpf_prog_put(prog);
            return err;
        }
    
        bpf_prog_kallsyms_add(prog);
        return err;
    
    free_used_maps:
        bpf_prog_kallsyms_del_subprogs(prog);
        free_used_maps(prog->aux);
    free_prog:
        bpf_prog_uncharge_memlock(prog);
    free_prog_sec:
        security_bpf_prog_free(prog->aux);
    free_prog_nouncharge:
        bpf_prog_free(prog);
        return err;
    }
    
    
    struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
    {
        gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
        struct bpf_prog_aux *aux;
        struct bpf_prog *fp;
    
        size = round_up(size, PAGE_SIZE);
        fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);  //页粒度 存size个指令
        if (fp == NULL)
            return NULL;
    
        aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);  //其他信息
        if (aux == NULL) {
            vfree(fp);
            return NULL;
        }
    
        fp->pages = size / PAGE_SIZE;    
        fp->aux = aux;
        fp->aux->prog = fp;
        fp->jit_requested = ebpf_jit_enabled();
    
        INIT_LIST_HEAD_RCU(&fp->aux->ksym_lnode);
    
        return fp;
    }
    
    

    相关文章

      网友评论

          本文标题:bpf_load

          本文链接:https://www.haomeiwen.com/subject/lwgfvrtx.html