1b1b8bc3fSopenharmony_ci/* 2b1b8bc3fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License. 5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at 6b1b8bc3fSopenharmony_ci * 7b1b8bc3fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8b1b8bc3fSopenharmony_ci * 9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and 13b1b8bc3fSopenharmony_ci * limitations under the License. 14b1b8bc3fSopenharmony_ci */ 15b1b8bc3fSopenharmony_ci 16b1b8bc3fSopenharmony_ci#ifndef NETMANAGER_BASE_BPF_READER_H 17b1b8bc3fSopenharmony_ci#define NETMANAGER_BASE_BPF_READER_H 18b1b8bc3fSopenharmony_ci 19b1b8bc3fSopenharmony_ci#include <cerrno> 20b1b8bc3fSopenharmony_ci#include <functional> 21b1b8bc3fSopenharmony_ci 22b1b8bc3fSopenharmony_ci#include "bpf_syscall_wrapper.h" 23b1b8bc3fSopenharmony_ci 24b1b8bc3fSopenharmony_cinamespace OHOS { 25b1b8bc3fSopenharmony_cinamespace Bpf { 26b1b8bc3fSopenharmony_citemplate <class Key, class Value> class NetsysBpfMap { 27b1b8bc3fSopenharmony_cipublic: 28b1b8bc3fSopenharmony_ci NetsysBpfMap<Key, Value>() = default; 29b1b8bc3fSopenharmony_ci NetsysBpfMap<Key, Value>(const std::string &pathName, uint32_t flags); 30b1b8bc3fSopenharmony_ci NetsysBpfMap<Key, Value>(bpf_map_type mapType, uint32_t maxEntries, uint32_t mapFlags); 31b1b8bc3fSopenharmony_ci 32b1b8bc3fSopenharmony_ci /** 33b1b8bc3fSopenharmony_ci * Is has map fd 34b1b8bc3fSopenharmony_ci * 35b1b8bc3fSopenharmony_ci * @return bool true:has map fd false:not have 36b1b8bc3fSopenharmony_ci */ 37b1b8bc3fSopenharmony_ci bool IsValid() const; 38b1b8bc3fSopenharmony_ci 39b1b8bc3fSopenharmony_ci /** 40b1b8bc3fSopenharmony_ci * Read Value From Map 41b1b8bc3fSopenharmony_ci * 42b1b8bc3fSopenharmony_ci * @param key the key of map 43b1b8bc3fSopenharmony_ci * @return Value value corresponding to key 44b1b8bc3fSopenharmony_ci */ 45b1b8bc3fSopenharmony_ci Value ReadValueFromMap(const Key key) const; 46b1b8bc3fSopenharmony_ci 47b1b8bc3fSopenharmony_ci /** 48b1b8bc3fSopenharmony_ci * WriteValue 49b1b8bc3fSopenharmony_ci * 50b1b8bc3fSopenharmony_ci * @param key the key want to write 51b1b8bc3fSopenharmony_ci * @param value the value want to write 52b1b8bc3fSopenharmony_ci * @param flags map flag 53b1b8bc3fSopenharmony_ci * @return bool true:write success false:failure 54b1b8bc3fSopenharmony_ci */ 55b1b8bc3fSopenharmony_ci bool WriteValue(const Key &key, const Value &value, uint64_t flags) const; 56b1b8bc3fSopenharmony_ci 57b1b8bc3fSopenharmony_ci /** 58b1b8bc3fSopenharmony_ci * Iterate through each element in the map 59b1b8bc3fSopenharmony_ci * 60b1b8bc3fSopenharmony_ci * @param counter lambda expression to be executed 61b1b8bc3fSopenharmony_ci */ 62b1b8bc3fSopenharmony_ci void Iterate(const std::function<void(const Key &key, const NetsysBpfMap<Key, Value> &map)> &counter) const; 63b1b8bc3fSopenharmony_ci 64b1b8bc3fSopenharmony_ci /** 65b1b8bc3fSopenharmony_ci * Get the Next Key From Map 66b1b8bc3fSopenharmony_ci * 67b1b8bc3fSopenharmony_ci * @param curkey current key 68b1b8bc3fSopenharmony_ci * @return int next Key 69b1b8bc3fSopenharmony_ci */ 70b1b8bc3fSopenharmony_ci int GetNextKeyFromMap(Key &curkey) const; 71b1b8bc3fSopenharmony_ci 72b1b8bc3fSopenharmony_ci /** 73b1b8bc3fSopenharmony_ci * Get the Next Key From Stats Map 74b1b8bc3fSopenharmony_ci * 75b1b8bc3fSopenharmony_ci * @param curkey current key 76b1b8bc3fSopenharmony_ci * @param nextKey reference of next Key 77b1b8bc3fSopenharmony_ci * @return int next Key 78b1b8bc3fSopenharmony_ci */ 79b1b8bc3fSopenharmony_ci int GetNextKeyFromStatsMap(const Key &curkey, Key &nextKey) const; 80b1b8bc3fSopenharmony_ci 81b1b8bc3fSopenharmony_ci /** 82b1b8bc3fSopenharmony_ci * BpfMapFdPin 83b1b8bc3fSopenharmony_ci * 84b1b8bc3fSopenharmony_ci * @param pathName the path that map needs to pin to 85b1b8bc3fSopenharmony_ci * @return bool true:pin success false:failure 86b1b8bc3fSopenharmony_ci */ 87b1b8bc3fSopenharmony_ci bool BpfMapFdPin(const std::string &pathName) const; 88b1b8bc3fSopenharmony_ci 89b1b8bc3fSopenharmony_ci /** 90b1b8bc3fSopenharmony_ci * DeleteEntryFromMap 91b1b8bc3fSopenharmony_ci * 92b1b8bc3fSopenharmony_ci * @param deleteKey the key need to delete 93b1b8bc3fSopenharmony_ci * @return bool true:delete success false:failure 94b1b8bc3fSopenharmony_ci */ 95b1b8bc3fSopenharmony_ci bool DeleteEntryFromMap(const Key &deleteKey); 96b1b8bc3fSopenharmony_ci 97b1b8bc3fSopenharmony_ciprivate: 98b1b8bc3fSopenharmony_ci int32_t mapFd_ = 0; 99b1b8bc3fSopenharmony_ci}; 100b1b8bc3fSopenharmony_ci} // namespace Bpf 101b1b8bc3fSopenharmony_ci} // namespace OHOS 102b1b8bc3fSopenharmony_ci#endif // NETMANAGER_BASE_BPF_READER_H 103