-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cc
200 lines (170 loc) · 5.29 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// Created by tanchao on 2022/6/30.
//
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <iostream>
#ifdef __cplusplus
extern "C" {
#endif
#include "base/rcu_map.h"
#include "base/debug.h"
#include "base/helper.h"
#ifdef __cplusplus
}
#endif
#include "base/logger.h"
#include "apis.h"
#include "dpthreads/dp_ctrl_thread.h"
extern int dp_data_add_tap(const char *netns, const char *iface, const char *ep_mac, int thr_id);
__thread int THREAD_ID; //线程局部存储
__thread char THREAD_NAME[32];
dp_thread_data_t g_dp_thread_data[MAX_DP_THREADS];
struct timeval g_now;
dp_mnt_shm_t *g_shm;
int g_dp_threads = 0;
int g_stats_slot = 0;
char *g_in_iface; //网络设备名字
io_config_t g_config;
rcu_map_t g_ep_map;
io_callback_t g_callback;
int g_running;
pthread_mutex_t g_debug_lock;
struct cds_list_head g_subnet4_list;
struct cds_list_head g_subnet6_list;
/* 中断dp的运行 */
static void dp_signal_exit(int num) {
g_running = false;
}
/* 创建共享内存区 */
template <typename T>
T *get_shm(size_t size) {
int fd;
void *ptr;
//创建共享内存文件(/dev/shm/dp_mnt.shm)
fd = shm_open(DP_MNT_SHM_NAME, O_CREAT | O_RDWR, S_IRWXU | S_IRWXG);
if (fd < 0) {
return NULL;
}
//将dp_mnt.shm共享内存文件映射到内存,MAP_SHARED建立进程间共享,用于进程间通信
ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED || ptr == NULL) {
close(fd);
return NULL;
}
close(fd);
return static_cast<T *>(ptr);
}
void init_dummy_ep(io_ep_t *ep);
static int net_run(const char *iface) {
pthread_t timer_thr;
pthread_t bld_dlp_thr;
pthread_t dp_thr[MAX_DP_THREADS];
int i, timer_thr_id, bld_dlp_thr_id, thr_id[MAX_DP_THREADS];
g_running = true;
// 发送中断信号
signal(SIGTERM, dp_signal_exit);
signal(SIGINT, dp_signal_exit);
signal(SIGQUIT, dp_signal_exit);
// 计算dp线程数
if (g_dp_threads == 0) {
g_dp_threads = count_cpu();
}
if (g_dp_threads > MAX_DP_THREADS) {
g_dp_threads = MAX_DP_THREADS;
}
DP_CTRL_Thread dpCtrlThread;
dpCtrlThread.Init(g_dp_thread_data);
pthread_create(&timer_thr, NULL, debug_timer_thr, &timer_thr_id);
// pthread_create(&bld_dlp_thr, NULL, &DP_CTRL_Thread::dp_bld_dlp_thr, &bld_dlp_thr_id);
for (i = 0; i < g_dp_threads; i++) {
thr_id[i] = i;
pthread_create(&dp_thr[i], NULL, dp_data_thr, &thr_id[i]);
}
//新建一个tap设备
if (iface != NULL) {
sleep(2);
dp_data_add_tap("/proc/1/ns/net", iface, "11:22:33:44:55:66", 0);
}
dpCtrlThread.dp_ctrl_loop();
pthread_join(timer_thr, NULL);
pthread_join(bld_dlp_thr, NULL);
for (i = 0; i < g_dp_threads; i++) {
pthread_join(dp_thr[i], NULL);
}
return 0;
}
static int dp_ep_match(struct cds_lfht_node *ht_node, const void *key) {
io_mac_t *ht_mac = STRUCT_OF(ht_node, io_mac_t, node);
const uint8_t *mac = (uint8_t *)key;
return memcmp(mac, &ht_mac->mac, sizeof(ht_mac->mac)) == 0 ? 1 : 0;
}
static uint32_t dp_ep_hash(const void *key) {
return sdbm_hash((uint8_t *)key, ETH_ALEN);
}
int main(int argc, char **argv) {
int arg = 0;
//是否传入PCAP文件
char *pcap = NULL;
//CPU限制
struct rlimit core_limits;
core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &core_limits); // 设置CPU使用限制
//清空一个g_config结构类型的变量, 对定义的字符串进行初始化为‘0’
memset(&g_config, 0, sizeof(g_config));
while (arg != -1) {
arg = getopt(argc, argv, "hcd:i:j:n:p:s");
switch (arg) {
case -1:
break;
case 'i':
g_in_iface = strdup(optarg);
g_config.promisc = true;
break;
case 'd':
if (strcasecmp(optarg, "none") == 0) {
g_debug_levels = 0;
} else if (optarg[0] == '-') {
g_debug_levels &= ~debug_name2level(optarg + 1);
} else {
g_debug_levels |= debug_name2level(optarg);
}
break;
default:
exit(-2);
}
}
setlinebuf(stdout);
pthread_mutex_init(&g_debug_lock, NULL);
rcu_map_init(&g_ep_map, 1, offsetof(io_mac_t, node), dp_ep_match, dp_ep_hash);
// 使用URCU数据结构创建2个list
CDS_INIT_LIST_HEAD(&g_subnet4_list);
CDS_INIT_LIST_HEAD(&g_subnet6_list);
// 初始化虚拟网络设备
init_dummy_ep(&g_config.dummy_ep);
g_config.dummy_mac.ep = &g_config.dummy_ep;
g_callback.debug = debug_stdout;
dpi_setup(&g_callback, &g_config);
// test_dpi_hs_search dpiHsSearch();
g_shm = get_shm<dp_mnt_shm_t>(sizeof(dp_mnt_shm_t));
if (g_shm == NULL) {
DEBUG_INIT("Unable to get shared memory.\n");
return -1;
}
int ret = net_run(g_in_iface);
munmap(g_shm, sizeof(dp_mnt_shm_t));
return ret;
}