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 NETMANAGER_BASE_BPF_SYSCALL_WRAPPER_H
17 #define NETMANAGER_BASE_BPF_SYSCALL_WRAPPER_H
18 
19 #include <cerrno>
20 #include <linux/bpf.h>
21 #include <linux/unistd.h>
22 #include <stdint.h>
23 #include <string>
24 #include <unistd.h>
25 
26 #include "securec.h"
27 
28 namespace OHOS {
29 namespace Bpf {
30 template <class Key, class Value> class BpfSyscallWrapper {
31 public:
32     BpfSyscallWrapper<Key, Value>() = default;
33 
34     /**
35      * Bpf Syscall
36      *
37      * @param cmd which command need to execute
38      * @param attr union consists of various anonymous structures
39      * @return int return the result of executing the command
40      */
41     static inline int BpfSyscall(int cmd, const bpf_attr &attr);
42 
43     /**
44      * Create A Bpf Map but for test only
45      *
46      * @param mapType map type
47      * @param keySize key size in bytes
48      * @param valueSize value size in bytes
49      * @param maxEntries maximum number of elements
50      * @param mapFlags map flag
51      * @return int return a map file descriptor
52      */
53     static int CreateMap(bpf_map_type mapType, uint32_t keySize, uint32_t valueSize, uint32_t maxEntries,
54                          uint32_t mapFlags);
55 
56     /**
57      * Write Value To Bpf Map
58      *
59      * @param mapFd map fd
60      * @param key the key of Bpf Map
61      * @param value the value of Bpf Map
62      * @param flags map flag
63      * @return int true:write success false:failure
64      */
65     static int WriteValueToMap(const int mapFd, const Key &key, const Value &value, uint64_t flags);
66 
67     /**
68      * LookUp Elem From Map
69      *
70      * @param mapFd map fd
71      * @param key the key of Bpf Map
72      * @param value the value of Bpf Map
73      * @return int true:find success false:failure
74      */
75     static int LookUpElem(const int mapFd, const Key &key, const Value &value);
76 
77     /**
78      * Delete Elem From Map
79      *
80      * @param mapFd map fd
81      * @param key the key of Bpf Map
82      * @return int true:delete success false:failure
83      */
84     static int DeleteElem(const int mapFd, const Key &key);
85 
86     /**
87      * Get the Next Key From Map
88      *
89      * @param mapFd map fd
90      * @param key the key of Bpf Map
91      * @param next_key the key of Bpf Map
92      * @return int return next key
93      */
94     static int GetNextKey(const int mapFd, const Key &key, Key &next_key);
95 
96     /**
97      * Get the First Key From Map
98      *
99      * @param mapFd map fd
100      * @param firstKey the first key of Bpf Map
101      * @return int return first key
102      */
103     static int GetFirstKey(const int mapFd, Key &key);
104 
105     /**
106      * Attach Program To Map
107      *
108      * @param type bpf attach type
109      * @param progFd eBPF program to attach
110      * @param cgFd container object to attach to
111      * @return int true:attach success false:failure
112      */
113     static int AttachProgram(bpf_attach_type type, const int progFd, const int cgFd);
114 
115     /**
116      * Detach Program From Map
117      *
118      * @param type bpf detach type
119      * @param cgFd container object to detach to
120      * @return int true:detach success false:failure
121      */
122     static int DetachProgram(bpf_attach_type type, const int cgFd);
123 
124     /**
125      * Pin Bpf Object To File node
126      *
127      * @param pathName path the bpf map pinned
128      * @param bfdFd bfd fd
129      * @return int true:pin success false:failure
130      */
131     static int BpfObjPin(const std::string &pathName, int bfdFd);
132 
133     /**
134      * Get Bpf Object By PathName
135      *
136      * @param pathName bpf map path
137      * @param fileFlags file flags
138      * @return int return map file descriptor
139      */
140     static int BpfObjGet(const std::string &pathName, uint32_t fileFlags);
141 
142     /**
143      * Get the Map Fd
144      *
145      * @param pathName bpf map path
146      * @param objFlags obj flags
147      * @return int return map file descriptor
148      */
149     static int GetMap(const std::string &pathName, uint32_t objFlags);
150 
151     /**
152      * Get the Map Fd
153      *
154      * @param pathName bpf map path
155      * @return int return map file descriptor
156      */
157     static int GetRWMap(const std::string &pathName);
158 
159     /**
160      * Get the Read—Only Map Fd
161      *
162      * @param pathName bpf map path
163      * @return int return map file descriptor
164      */
165     static int GetROMap(const std::string &pathName);
166 
167     /**
168      * Get the Write—Only Map Fd
169      *
170      * @param pathName bpf map path
171      * @return int return map file descriptor
172      */
173     static int GetWOMap(const std::string &pathName);
174 
175 private:
176     static inline uint32_t BpfFdToU32(const int mapFd);
177 
178     static inline uint64_t BpfMapPathNameToU64(const std::string &pathName);
179 
180     static inline uint64_t BpfMapKeyToU64(const Key &key);
181 
182     static inline uint64_t BpfMapValueToU64(const Value &value);
183 };
184 } // namespace Bpf
185 } // namespace OHOS
186 #endif // NETMANAGER_BASE_BPF_SYSCALL_WRAPPER_H
187