1 /*
2  * Copyright (c) 2024 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_RING_BUFFER_H
17 #define BPF_RING_BUFFER_H
18 #include <iostream>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include <linux/bpf.h>
24 #include <sys/mman.h>
25 #include <sys/epoll.h>
26 #include <time.h>
27 #include <linux/if_ether.h>
28 #include <linux/unistd.h>
29 #include <string>
30 #include <functional>
31 
32 #include "bpf_path.h"
33 #include "bpf_def.h"
34 #include "bpf_stats.h"
35 #include "bpf_mapper.h"
36 #include "securec.h"
37 
38 namespace OHOS::NetManagerStandard {
39 
40 enum RingBufferError {
41     RING_BUFFER_ERR_NONE = 0,
42     RING_BUFFER_ERR_INTERNAL,
43 };
44 
45 class NetsysBpfRingBuffer {
46 public:
47     NetsysBpfRingBuffer() = default;
48     ~NetsysBpfRingBuffer() = default;
49 
50     static uint64_t BpfMapPathNameToU64(const std::string &pathName);
51     static int32_t BpfSyscall(int32_t cmd, const bpf_attr &attr);
52     static int GetRingbufFd(const std::string &path, uint32_t fileFlags);
53 
54     static int HandleNetworkPolicyEventCallback(void *ctx, void *data, size_t data_sz);
55     static void ListenRingBufferThread(void);
56     static void ListenNetworkAccessPolicyEvent();
57     static void ExistRingBufferPoll(void);
58 
59 private:
60     static bool existThread_;
61 };
62 } // namespace OHOS::NetManagerStandard
63 #endif // BPF_RING_BUFFER_H
64