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 OHOS_NETMANAGERSTANDARD_NERSYSBPFMAP_H 17 #define OHOS_NETMANAGERSTANDARD_NERSYSBPFMAP_H 18 19 #include <errno.h> 20 #include <functional> 21 22 #include "bpf_wrappers.h" 23 #include "netnative_log_wrapper.h" 24 #include "unique_fd.h" 25 26 namespace OHOS { 27 namespace NetManagerStandard { 28 template <class Key, class Value> class NetsysBpfMap { 29 public: 30 NetsysBpfMap<Key, Value>() = default; 31 NetsysBpfMap<Key, Value>(const std::string &pathName, uint32_t flags) 32 { 33 int mapFd = BpfWrappers<Key, Value>::GetMap(pathName, flags); 34 if (mapFd >= 0) { 35 mapFd_ = mapFd; 36 } 37 } 38 39 NetsysBpfMap<Key, Value>(bpf_map_type mapType, uint32_t maxEntries, uint32_t mapFlags) 40 { 41 int mapFd = BpfWrappers<Key, Value>::CreateMap(mapType, sizeof(Key), sizeof(Value), maxEntries, mapFlags); 42 if (IsValid()) { 43 mapFd_ = mapFd; 44 } 45 } 46 47 /** 48 * Is has map fd 49 * 50 * @return bool true:has map fd false:not have 51 */ IsValid()52 bool IsValid() const 53 { 54 return mapFd_ >= 0; 55 } 56 57 /** 58 * Read Value From Map 59 * 60 * @param key the key of map 61 * @return Value value corresponding to key 62 */ ReadValueFromMap(const Key key)63 Value ReadValueFromMap(const Key key) const 64 { 65 Value value; 66 if (BpfWrappers<Key, Value>::LookUpElem(mapFd_, key, value)) { 67 NETNATIVE_LOGE("Read Value failed: errno = %{public}d,failed : %{public}s", errno, strerror(errno)); 68 } 69 return value; 70 } 71 72 /** 73 * WriteValue 74 * 75 * @param key the key want to write 76 * @param value the value want to write 77 * @param flags map flag 78 * @return bool true:write success false:failure 79 */ WriteValue(const Key & key,const Value & value,uint64_t flags)80 bool WriteValue(const Key &key, const Value &value, uint64_t flags) const 81 { 82 return BpfWrappers<Key, Value>::WriteValueToMap(mapFd_, key, value, flags) == 0; 83 } 84 85 /** 86 * Iterate through each element in the map 87 * 88 * @param counter lambda expression to be executed 89 */ Iterate(const std::function<void (const Key & key,const NetsysBpfMap<Key,Value> & map)> & counter)90 void Iterate(const std::function<void(const Key &key, const NetsysBpfMap<Key, Value> &map)> &counter) const 91 { 92 Key curKey; 93 if (BpfWrappers<Key, Value>::GetFirstKey(mapFd_, curKey)) { 94 NETNATIVE_LOGE("Get FirstKey failed and curKey is %{public}u", static_cast<uint32_t>(curKey)); 95 return; 96 } 97 Key nextKey; 98 while (BpfWrappers<Key, Value>::GetNextKey(mapFd_, curKey, nextKey) == 0) { 99 counter(curKey, *this); 100 curKey = nextKey; 101 } 102 if (BpfWrappers<Key, Value>::GetNextKey(mapFd_, curKey, nextKey) < 0 && (errno == ENOENT)) { 103 counter(curKey, *this); 104 } 105 } 106 107 /** 108 * Get the Next Key From Map 109 * 110 * @param curkey current key 111 * @return int next Key 112 */ GetNextKeyFromMap(Key & curkey)113 int GetNextKeyFromMap(Key &curkey) const 114 { 115 Key nextKey; 116 if (BpfWrappers<Key, Value>::GetNextKey(mapFd_, &curkey, &nextKey)) { 117 NETNATIVE_LOGE("Get Next Key failed: errno = %{public}d,failed : %{public}s", errno, strerror(errno)); 118 return errno; 119 } 120 return nextKey; 121 } 122 123 /** 124 * Get the Next Key From Stats Map 125 * 126 * @param curkey current key 127 * @param nextKey reference of next Key 128 * @return int next Key 129 */ GetNextKeyFromStatsMap(const Key & curkey,Key & nextKey)130 int GetNextKeyFromStatsMap(const Key &curkey, Key &nextKey) const 131 { 132 return BpfWrappers<Key, Value>::GetNextKey(mapFd_, curkey, nextKey); 133 } 134 135 /** 136 * BpfMapFdPin 137 * 138 * @param pathName the path that map needs to pin to 139 * @return bool true:pin success false:failure 140 */ BpfMapFdPin(const std::string & pathName)141 bool BpfMapFdPin(const std::string &pathName) const 142 { 143 return BpfWrappers<Key, Value>::BpfObjPin(pathName, mapFd_) == 0; 144 } 145 146 /** 147 * DeleteEntryFromMap 148 * 149 * @param deleteKey the key need to delete 150 * @return bool true:delete success false:failure 151 */ DeleteEntryFromMap(const Key & deleteKey)152 bool DeleteEntryFromMap(const Key &deleteKey) 153 { 154 return BpfWrappers<Key, Value>::DeleteElem(mapFd_, deleteKey) == 0; 155 } 156 157 private: 158 int32_t mapFd_ = 0; 159 }; 160 } // namespace NetManagerStandard 161 } // namespace OHOS 162 #endif // OHOS_NETMANAGERSTANDARD_NERSYSBPFMAP_H 163