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
273
274
275
276
277
278
279
280
|
#include <sched.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <netinet/ether.h>
#include <MESA/MESA_prof_load.h>
#include "log.h"
#include "packet_io.h"
#define MAX_RX_BURST 128
#define LOG_PKTIO "PACKET_IO"
#define MIN(a, b) ((a) > (b) ? (b) : (a))
struct config
{
int thread_num;
int cpu_mask[MAX_THREAD_NUM];
int rx_burst_max;
int bypass_traffic;
char app_symbol[256];
char app_device[256];
};
struct packet_io
{
struct config config;
struct mr_vdev *mr_dev;
struct mr_sendpath *mr_path;
struct mr_instance *mr_instance;
void *cb_args;
packet_handle_cb *callback;
};
static int packet_io_config(const char *profile, struct config *config)
{
MESA_load_profile_int_def(profile, "PACKET_IO", "thread_num", (int *)&(config->thread_num), 1);
MESA_load_profile_uint_range(profile, "PACKET_IO", "cpu_mask", MAX_THREAD_NUM, (unsigned int *)config->cpu_mask);
MESA_load_profile_int_def(profile, "PACKET_IO", "rx_burst_max", (int *)&(config->rx_burst_max), 1);
MESA_load_profile_int_def(profile, "PACKET_IO", "bypass_traffic", (int *)&(config->bypass_traffic), 0);
MESA_load_profile_string_nodef(profile, "PACKET_IO", "app_symbol", config->app_symbol, sizeof(config->app_symbol));
MESA_load_profile_string_nodef(profile, "PACKET_IO", "app_device", config->app_device, sizeof(config->app_device));
config->thread_num = MIN(config->thread_num, MAX_THREAD_NUM);
if (config->rx_burst_max > MAX_RX_BURST)
{
LOG_ERROR("%s: invalid rx_burst_max, exceeds limit %d", LOG_PKTIO, MAX_RX_BURST);
return -1;
}
if (strlen(config->app_symbol) == 0)
{
LOG_ERROR("%s: invalid app_symbol in %s", LOG_PKTIO, profile);
return -1;
}
if (strlen(config->app_device) == 0)
{
LOG_ERROR("%s: invalid app_device in %s", LOG_PKTIO, profile);
return -1;
}
LOG_DEBUG("%s: PACKET_IO->thread_num : %d", LOG_PKTIO, config->thread_num);
LOG_DEBUG("%s: PACKET_IO->rx_burst_max : %d", LOG_PKTIO, config->rx_burst_max);
LOG_DEBUG("%s: PACKET_IO->bypass_traffic : %d", LOG_PKTIO, config->bypass_traffic);
LOG_DEBUG("%s: PACKET_IO->app_symbol : %s", LOG_PKTIO, config->app_symbol);
LOG_DEBUG("%s: PACKET_IO->app_device : %s", LOG_PKTIO, config->app_device);
return 0;
}
static int marsio_buff_is_keepalive(char *raw_data, int raw_len)
{
if (raw_data == NULL || raw_len < (int)(sizeof(struct ethhdr)))
{
return 0;
}
struct ethhdr *eth_hdr = (struct ethhdr *)raw_data;
if (eth_hdr->h_proto == 0xAAAA)
{
return 1;
}
else
{
return 0;
}
}
struct packet_io *packet_io_create(const char *profile)
{
int opt = 1;
cpu_set_t coremask;
struct packet_io *handle = (struct packet_io *)calloc(1, sizeof(struct packet_io));
assert(handle != NULL);
if (packet_io_config(profile, &(handle->config)) != 0)
{
goto error_out;
}
CPU_ZERO(&coremask);
for (int i = 0; i < handle->config.thread_num; i++)
{
CPU_SET(handle->config.cpu_mask[i], &coremask);
}
handle->mr_instance = marsio_create();
if (handle->mr_instance == NULL)
{
LOG_ERROR("%s: unable to create marsio instance", LOG_PKTIO);
goto error_out;
}
if (marsio_option_set(handle->mr_instance, MARSIO_OPT_THREAD_MASK_IN_CPUSET, &coremask, sizeof(cpu_set_t)) != 0)
{
LOG_ERROR("%s: unable to set MARSIO_OPT_EXIT_WHEN_ERR option for marsio instance", LOG_PKTIO);
goto error_out;
}
if (marsio_option_set(handle->mr_instance, MARSIO_OPT_EXIT_WHEN_ERR, &opt, sizeof(opt)) != 0)
{
LOG_ERROR("%s: unable to set MARSIO_OPT_EXIT_WHEN_ERR option for marsio instance", LOG_PKTIO);
goto error_out;
}
if (marsio_init(handle->mr_instance, handle->config.app_symbol) != 0)
{
LOG_ERROR("%s: unable to initialize marsio instance", LOG_PKTIO);
goto error_out;
}
handle->mr_dev = marsio_open_device(handle->mr_instance, handle->config.app_device, handle->config.thread_num, handle->config.thread_num);
if (handle->mr_dev == NULL)
{
LOG_ERROR("%s: unable to open device %s", LOG_PKTIO, handle->config.app_device);
goto error_out;
}
handle->mr_path = marsio_sendpath_create_by_vdev(handle->mr_dev);
if (handle->mr_path == NULL)
{
LOG_ERROR("%s: unable to create sendpath for device %s", LOG_PKTIO, handle->config.app_device);
goto error_out;
}
return handle;
error_out:
packet_io_destory(handle);
return NULL;
}
void packet_io_destory(struct packet_io *handle)
{
if (handle)
{
if (handle->mr_path)
{
marsio_sendpath_destory(handle->mr_path);
handle->mr_path = NULL;
}
if (handle->mr_dev)
{
marsio_close_device(handle->mr_dev);
handle->mr_dev = NULL;
}
if (handle->mr_instance)
{
marsio_destory(handle->mr_instance);
handle->mr_instance = NULL;
}
free(handle);
handle = NULL;
}
}
void packet_io_set_callback(struct packet_io *handle, packet_handle_cb *cb, void *args)
{
handle->callback = cb;
handle->cb_args = args;
}
int packet_io_thread_init(struct packet_io *handle, int thread_index)
{
if (marsio_thread_init(handle->mr_instance) != 0)
{
LOG_ERROR("%s: unable to init marsio thread %d", LOG_PKTIO, thread_index);
return -1;
}
return 0;
}
void packet_io_thread_wait(struct packet_io *handle, int thread_index, int timeout_ms)
{
struct mr_vdev *vdevs[] = {
handle->mr_dev,
};
marsio_poll_wait(handle->mr_instance, vdevs, 1, thread_index, timeout_ms);
}
int packet_io_thread_polling(struct packet_io *handle, int thread_index)
{
int raw_len;
char *raw_data;
marsio_buff_t *rx_buff;
marsio_buff_t *rx_buffs[MAX_RX_BURST];
int nr_recv = marsio_recv_burst(handle->mr_dev, thread_index, rx_buffs, handle->config.rx_burst_max);
if (nr_recv <= 0)
{
return 0;
}
if (handle->config.bypass_traffic == 1)
{
marsio_send_burst(handle->mr_path, thread_index, rx_buffs, nr_recv);
return nr_recv;
}
for (int j = 0; j < nr_recv; j++)
{
rx_buff = rx_buffs[j];
raw_len = marsio_buff_datalen(rx_buff);
raw_data = marsio_buff_mtod(rx_buff);
if (marsio_buff_is_keepalive(raw_data, raw_len))
{
marsio_send_burst(handle->mr_path, thread_index, &rx_buff, 1);
}
else if (marsio_buff_is_ctrlbuf(rx_buff))
{
marsio_send_burst(handle->mr_path, thread_index, &rx_buff, 1);
}
else
{
if (handle->callback)
{
if (handle->callback(raw_data, raw_len, handle->cb_args) == ACTION_BYPASS)
{
marsio_send_burst(handle->mr_path, thread_index, &rx_buff, 1);
}
else
{
marsio_buff_free(handle->mr_instance, &rx_buff, 1, 0, thread_index);
}
}
else
{
marsio_send_burst(handle->mr_path, thread_index, &rx_buff, 1);
}
}
}
return nr_recv;
}
int packet_io_thread_number(struct packet_io *handle)
{
if (handle)
{
return handle->config.thread_num;
}
else
{
return 0;
}
}
|