-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbase.c
More file actions
272 lines (239 loc) · 5.34 KB
/
base.c
File metadata and controls
272 lines (239 loc) · 5.34 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* virtual net card.
*
* (C) 2019.05.15 <buddy.zhang@aliyun.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <malloc.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <net/route.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
/* queue header file */
#include <queue.h>
#include <base.h>
#ifdef CONFIG_HOST
struct vc_node Defnode = {
.name = "X64 Virtual Card",
.ip = "10.10.10.1",
};
#elif defined CONFIG_FPGA
struct vc_node Defnode = {
.name = "FPGA Virtual Card",
.ip = "10.10.10.5",
};
#endif
/* Virtual Card init-route */
struct vc_node *vc_init(void)
{
struct vc_node *vc;
int fd;
vc = vc_root();
/* Initialize queue */
vc->queue = queue_init();
/* Initialize tun */
vc->tun_fd = tun_create(vc->tun_name, IFF_TUN | IFF_NO_PI, vc->ip);
if (vc->tun_fd < 0) {
printf("ERROR: Can't establish TUN\n");
goto err_tun;
}
#ifdef CONFIG_MSG_PARSE
vc->perf_fd = open("./Perf_Winfo.info", O_RDWR | O_CREAT);
if (vc->perf_fd < 0) {
printf("Can't open pref_Winfo.info file\n");
goto err_file;
}
vc->perf_fd2 = open("./Perf_Rinfo.info", O_RDWR | O_CREAT);
if (vc->perf_fd2 < 0) {
printf("Can't open pref_Rinfo.info file\n");
goto err_file2;
}
#endif
/* Tap/Tun Write Buffer */
vc->WBuf = (char *)malloc(BUFFER_SIZE);
if (!vc->WBuf) {
printf("ERROR: No enough memory for WBuf\n");
goto err_WBuf;
}
memset(vc->WBuf, 0, BUFFER_SIZE);
/* Tap/Tun Read Buffer */
vc->RBuf = (char *)malloc(BUFFER_SIZE);
if (!vc->RBuf) {
printf("ERROR: No enough memory for RBuf.\n");
goto err_RBuf;
}
memset(vc->RBuf, 0, BUFFER_SIZE);
#ifdef CONFIG_HOST
/* DMA */
vc->Xdma = xdma_open();
if (vc->Xdma == NULL) {
printf("ERROR: Unable to open DMA Channel!\n");
goto err_dma;
}
#endif
/* Write RingBuf */
#ifdef CONFIG_HOST
vc->RingBuf = (unsigned char *)align_alloc(RINGBUF_SIZE);
#else /* FPGA */
fd = open("/dev/mem", O_RDWR | O_SYNC);
if (fd < 0)
goto err_share_mem;
vc->RingBuf = (unsigned char *)mmap(0, RINGBUF_SIZE,
PROT_READ | PROT_WRITE, MAP_SHARED, fd,
(off_t)READ_PHY_ADDR);
#endif
if (!vc->RingBuf) {
printf("ERROR: Unable to alloc RingBuf.\n");
goto err_ringbuf;
}
vc->ring_index = 0;
vc->ring_count = 0;
vc->pos = 0;
/* Read RingBuf */
#ifdef CONFIG_HOST
vc->RingBuf2 = (unsigned char *)align_alloc(RINGBUF_SIZE);
#else /* FPGA */
vc->RingBuf2 = (unsigned char *)mmap(0, RINGBUF_SIZE,
PROT_READ | PROT_WRITE, MAP_SHARED, fd,
(off_t)WRITE_PHY_ADDR);
close(fd); /* pre-relase mmap-handle */
#endif
if (!vc->RingBuf2) {
printf("ERROR: Unable to alloc RingBuf2.\n");
goto err_ringbuf2;
}
vc->ring_index2 = 0;
vc->ring_count2 = 0;
vc->pos2 = 0;
/* Signal */
signal_init();
/* Thread flags */
vc->flags = 1;
return vc;
#ifdef CONFIG_HOST
err_ringbuf2:
free(vc->RingBuf);
err_ringbuf:
xdma_close(vc->Xdma);
err_dma:
free(vc->RBuf);
#endif
#ifdef CONFIG_FPGA
err_ringbuf2:
munmap((void *)vc->RingBuf, RINGBUF_SIZE);
err_ringbuf:
close(fd);
err_share_mem:
free(vc->RBuf);
#endif
err_RBuf:
free(vc->WBuf);
err_WBuf:
#ifdef CONFIG_MSG_PARSE
close(vc->perf_fd2);
err_file2:
close(vc->perf_fd);
err_file:
#endif
close(vc->tun_fd);
err_tun:
queue_exit(vc->queue);
err:
return NULL;
}
/* Virtual Card exit-route */
void vc_exit(struct vc_node *vc)
{
#ifdef CONFIG_HOST
free(vc->RingBuf2);
free(vc->RingBuf);
/* Exit dma */
xdma_close(vc->Xdma);
#else
munmap((void *)vc->RingBuf2, RINGBUF_SIZE);
munmap((void *)vc->RingBuf, RINGBUF_SIZE);
#endif
/* Free buffer */
free(vc->RBuf);
free(vc->WBuf);
#ifdef CONFIG_MSG_PARSE
close(vc->perf_fd2);
close(vc->perf_fd);
#endif
/* Remove TUN */
close(vc->tun_fd);
/* Remove queue */
queue_exit(vc->queue);
printf("\nSafe exit virtual card!\n");
}
/* debug helper */
void debug_dump_socket_frame(const char *buf, unsigned long count,
const char *msg)
{
int i, j = 0;
printf("\n*************%s-%ld*************\n", msg, count);
for (i = 0; i < count; i++, j++) {
if (j > 15) {
printf("\n");
j = 0;
}
printf("%#hhx ", buf[i]);
}
printf("\n\n");
}
long perf_time(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000 + tv.tv_usec;
}
void perf_speed(long start, long end)
{
long time = end - start;
printf("Speed: %f (start: %ld end: %ld)\n", (float)1 / (float)time,
start, end);
}
#ifdef CONFIG_MSG_PARSE
static void msg_parse(const char *buf, char *msg, unsigned long offset, int len)
{
memcpy(msg, (char *)((unsigned long)buf + offset), len);
}
void msg_parse_context(const char *buf, char *msg)
{
msg_parse(buf, msg, MSG_CONTEXT_OFF, MSG_CONTEXT_LEN);
}
void msg_store(struct vc_node *vc, struct msg_context *array,
int limit, int w, char *str)
{
char bufx[120];
int i;
for (i = 0; i < limit; i++) {
sprintf(bufx, "%s---%ld\t\t%ld\n", str, array[i].id,
array[i].time);
if (w)
write(vc->perf_fd, bufx, strlen(bufx));
else
write(vc->perf_fd2, bufx, strlen(bufx));
}
}
#endif