1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef BPF_PROG_LOADER_H 17 #define BPF_PROG_LOADER_H 18 19 #ifdef ENABLE_ELFIO 20 #include "bpf_utils.h" 21 #endif 22 23 namespace OHOS { 24 namespace Bpf { 25 class BpfProgLoader { 26 public: 27 /** 28 * Construct a new Bpf Prog Loader object 29 * 30 */ 31 BpfProgLoader() = default; 32 33 #ifdef ENABLE_ELFIO 34 /** 35 * Load the bpf progs and pin them to file system. 36 * 37 * @param event The type of prog. 38 * @param prog EBPF bytecodes compiled by LLVM/Clang. 39 * @param size The data size of section. 40 * @return Returns true on success, false on failure. 41 */ 42 bool LoadAndAttach(const std::string &event, const bpf_insn *prog, int32_t size); 43 44 private: 45 int32_t BpfLoadProg(bpf_prog_type type, const bpf_insn *insns, size_t insnsCnt, std::string &license, 46 uint32_t kernVersion, std::string &logBuf, size_t logBufSz) const; 47 void InitProgAttr(const BpfLoadProgAttr *loadAttr, bpf_attr &attr, const std::string &logBuf, 48 size_t logBufSz) const; 49 int32_t BpfLoadProgXattr(const BpfLoadProgAttr *loadAttr, std::string &logBuf, size_t logBufSz) const; 50 51 std::string bpfLogBuf_ = {}; 52 #endif 53 }; 54 } // namespace Bpf 55 } // namespace OHOS 56 #endif // BPF_PROG_LOADER_H 57