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_MAP_CREATOR_H 17 #define BPF_MAP_CREATOR_H 18 19 #ifdef ENABLE_ELFIO 20 #include "bpf_utils.h" 21 #include "elfio/elfio.hpp" 22 #endif 23 24 namespace OHOS { 25 namespace Bpf { 26 constexpr int32_t MAX_MAP_COUNT = 32; 27 28 class BpfMapCreator { 29 public: 30 /** 31 * Construct a new Bpf Map Creator object 32 * 33 */ 34 BpfMapCreator() = default; 35 36 #ifdef ENABLE_ELFIO 37 /** 38 * Create Map nodes and pin them to the file system. 39 * 40 * @param maps An array that stores Maps. 41 * @param len The count of the Maps. 42 * @return Returns true on success, false on failure. 43 */ 44 bool CreateMaps(BpfMapData *maps, int32_t len); 45 46 /** 47 * Load the maps Section. 48 * 49 * @param maps The attributes of the map. 50 * @param elfio The object of the elfio. 51 * @return Returns the number of maps. 52 */ 53 int32_t LoadElfMapsSection(BpfMapData *maps, const ELFIO::elfio &elfio) const; 54 55 private: 56 /** 57 * Create Map nodes. 58 * 59 * @param mapType The type of the Map. 60 * @param name The name of the Map. 61 * @param keySize The size of key. 62 * @param valueSize The size of value. 63 * @param maxEntries The max entries stored in the Map. 64 * @param mapFlags The flag of the Map. 65 * @param node It's valid only when BPF_F_NUMA_NODE FLAG is set. 66 * @return Returns fd of the Map that was created successfully. 67 */ 68 int32_t BpfCreateMapNode(bpf_map_type mapType, std::string &name, int32_t keySize, int32_t valueSize, 69 int32_t maxEntries, uint32_t mapFlags, int32_t node) const; 70 71 /** 72 * Construct the attributes that creates the Map. 73 * 74 * @param createAttr The attributes that creates the Map. 75 * @return Returns fd of the Map that was created successfully. 76 */ 77 int32_t BpfCreateMapXattr(const BpfCreateMapAttr *createAttr) const; 78 #endif 79 80 int32_t mapFd_[MAX_MAP_COUNT] = {}; 81 }; 82 } // namespace Bpf 83 } // namespace OHOS 84 #endif // BPF_MAP_CREATOR_H 85