bpf.c (4cf23a3c6359556a1cca489cf2b901e2b904c4b0) | bpf.c (0ed08d6725b5116aaad7a0082d721286e0a43dca) |
---|---|
1// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2 3/* 4 * common eBPF ELF operations. 5 * 6 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> 7 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> 8 * Copyright (C) 2015 Huawei Inc. --- 1033 unchanged lines hidden (view full) --- 1042 memset(&attr, 0, sizeof(attr)); 1043 attr.raw_tracepoint.name = ptr_to_u64(name); 1044 attr.raw_tracepoint.prog_fd = prog_fd; 1045 1046 fd = sys_bpf_fd(BPF_RAW_TRACEPOINT_OPEN, &attr, sizeof(attr)); 1047 return libbpf_err_errno(fd); 1048} 1049 | 1// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2 3/* 4 * common eBPF ELF operations. 5 * 6 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> 7 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> 8 * Copyright (C) 2015 Huawei Inc. --- 1033 unchanged lines hidden (view full) --- 1042 memset(&attr, 0, sizeof(attr)); 1043 attr.raw_tracepoint.name = ptr_to_u64(name); 1044 attr.raw_tracepoint.prog_fd = prog_fd; 1045 1046 fd = sys_bpf_fd(BPF_RAW_TRACEPOINT_OPEN, &attr, sizeof(attr)); 1047 return libbpf_err_errno(fd); 1048} 1049 |
1050int bpf_load_btf(const void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size, 1051 bool do_log) | 1050int bpf_btf_load(const void *btf_data, size_t btf_size, const struct bpf_btf_load_opts *opts) |
1052{ | 1051{ |
1053 union bpf_attr attr = {}; | 1052 const size_t attr_sz = offsetofend(union bpf_attr, btf_log_level); 1053 union bpf_attr attr; 1054 char *log_buf; 1055 size_t log_size; 1056 __u32 log_level; |
1054 int fd; 1055 | 1057 int fd; 1058 |
1056 attr.btf = ptr_to_u64(btf); | 1059 memset(&attr, 0, attr_sz); 1060 1061 if (!OPTS_VALID(opts, bpf_btf_load_opts)) 1062 return libbpf_err(-EINVAL); 1063 1064 log_buf = OPTS_GET(opts, log_buf, NULL); 1065 log_size = OPTS_GET(opts, log_size, 0); 1066 log_level = OPTS_GET(opts, log_level, 0); 1067 1068 if (log_size > UINT_MAX) 1069 return libbpf_err(-EINVAL); 1070 if (log_size && !log_buf) 1071 return libbpf_err(-EINVAL); 1072 1073 attr.btf = ptr_to_u64(btf_data); |
1057 attr.btf_size = btf_size; | 1074 attr.btf_size = btf_size; |
1075 /* log_level == 0 and log_buf != NULL means "try loading without 1076 * log_buf, but retry with log_buf and log_level=1 on error", which is 1077 * consistent across low-level and high-level BTF and program loading 1078 * APIs within libbpf and provides a sensible behavior in practice 1079 */ 1080 if (log_level) { 1081 attr.btf_log_buf = ptr_to_u64(log_buf); 1082 attr.btf_log_size = (__u32)log_size; 1083 attr.btf_log_level = log_level; 1084 } |
|
1058 | 1085 |
1059retry: 1060 if (do_log && log_buf && log_buf_size) { 1061 attr.btf_log_level = 1; 1062 attr.btf_log_size = log_buf_size; | 1086 fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz); 1087 if (fd < 0 && log_buf && log_level == 0) { |
1063 attr.btf_log_buf = ptr_to_u64(log_buf); | 1088 attr.btf_log_buf = ptr_to_u64(log_buf); |
1089 attr.btf_log_size = (__u32)log_size; 1090 attr.btf_log_level = 1; 1091 fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz); |
|
1064 } | 1092 } |
1093 return libbpf_err_errno(fd); 1094} |
|
1065 | 1095 |
1066 fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, sizeof(attr)); | 1096int bpf_load_btf(const void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size, bool do_log) 1097{ 1098 LIBBPF_OPTS(bpf_btf_load_opts, opts); 1099 int fd; |
1067 | 1100 |
1101retry: 1102 if (do_log && log_buf && log_buf_size) { 1103 opts.log_buf = log_buf; 1104 opts.log_size = log_buf_size; 1105 opts.log_level = 1; 1106 } 1107 1108 fd = bpf_btf_load(btf, btf_size, &opts); |
|
1068 if (fd < 0 && !do_log && log_buf && log_buf_size) { 1069 do_log = true; 1070 goto retry; 1071 } 1072 1073 return libbpf_err_errno(fd); 1074} 1075 --- 53 unchanged lines hidden --- | 1109 if (fd < 0 && !do_log && log_buf && log_buf_size) { 1110 do_log = true; 1111 goto retry; 1112 } 1113 1114 return libbpf_err_errno(fd); 1115} 1116 --- 53 unchanged lines hidden --- |