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
281
282
|
extern "C" {
#include <assert.h>
#include <marsio.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <rte_ether.h>
#include <rte_ip.h>
#include <rte_udp.h>
#include <rte_version.h>
}
#include <algorithm>
static char appsym[64] = "smartoffload";
static char dev_symbol[64] = "vxlan_user";
uint64_t cpu_mask = 0x1;
unsigned int nr_thread = 1;
struct mr_instance * mr_instance = NULL;
struct mr_vdev * dev_handler = NULL;
struct mr_sendpath * sendpath = NULL;
#define BURST_MAX 64
unsigned int nr_burst = 1;
unsigned int self_loop = 0;
int __is_bfd_pkt_and_make_answer(char * pkt_ptr, unsigned int pkt_len, bool make_answer)
{
struct rte_ether_hdr * eth_hdr = (struct rte_ether_hdr *)pkt_ptr;
/* must be ipv4 pkt for vxlan overlay */
if (eth_hdr->ether_type != ntohs(RTE_ETHER_TYPE_IPV4))
{
return 0;
}
pkt_ptr += sizeof(struct rte_ether_hdr);
pkt_len -= sizeof(struct rte_ether_hdr);
/* ipv4 header */
struct rte_ipv4_hdr * ipv4_hdr = (struct rte_ipv4_hdr *)pkt_ptr;
if (ipv4_hdr->next_proto_id != IPPROTO_UDP)
{
return 0;
}
pkt_ptr += sizeof(struct rte_ipv4_hdr);
pkt_len -= sizeof(struct rte_ipv4_hdr);
struct rte_udp_hdr * udp_hdr = (struct rte_udp_hdr *)pkt_ptr;
if (udp_hdr->dst_port != ntohs(3784))
{
return 0;
}
if (make_answer)
{
/* at here, this pkt must be a vxlan pkt,
we need to swap the mac addresses and ipv4 addresses */
struct rte_ether_addr swap_tmp;
#if RTE_VERSION_NUM(21, 11, 0, 0) <= RTE_VERSION
rte_ether_addr_copy(ð_hdr->dst_addr, &swap_tmp);
rte_ether_addr_copy(ð_hdr->src_addr, ð_hdr->dst_addr);
rte_ether_addr_copy(&swap_tmp, ð_hdr->src_addr);
#else
rte_ether_addr_copy(ð_hdr->d_addr, &swap_tmp);
rte_ether_addr_copy(ð_hdr->s_addr, ð_hdr->d_addr);
rte_ether_addr_copy(&swap_tmp, ð_hdr->s_addr);
#endif
/* ipv4 src, dst and udp's ports must be swap */
rte_be32_t ipv4_addr_swap_tmp;
ipv4_addr_swap_tmp = ipv4_hdr->dst_addr;
ipv4_hdr->dst_addr = ipv4_hdr->src_addr;
ipv4_hdr->src_addr = ipv4_addr_swap_tmp;
rte_be16_t udp_port_swap_tmp;
udp_port_swap_tmp = udp_hdr->dst_port;
udp_hdr->dst_port = udp_hdr->src_port;
udp_hdr->src_port = udp_port_swap_tmp;
}
return 1;
}
int __is_g_vxlan_and_make_inject(char * pkt_ptr, unsigned int pkt_len, bool do_inject)
{
struct rte_ether_hdr * eth_hdr = (struct rte_ether_hdr *)pkt_ptr;
/* must be ipv4 pkt for vxlan overlay */
if (eth_hdr->ether_type != ntohs(RTE_ETHER_TYPE_IPV4))
{
return 0;
}
pkt_ptr += sizeof(struct rte_ether_hdr);
pkt_len -= sizeof(struct rte_ether_hdr);
/* ipv4 header */
struct rte_ipv4_hdr * ipv4_hdr = (struct rte_ipv4_hdr *)pkt_ptr;
if (ipv4_hdr->next_proto_id != IPPROTO_UDP)
{
return 0;
}
pkt_ptr += sizeof(struct rte_ipv4_hdr);
pkt_len -= sizeof(struct rte_ipv4_hdr);
struct rte_udp_hdr * udp_hdr = (struct rte_udp_hdr *)pkt_ptr;
if (udp_hdr->dst_port != ntohs(4789))
{
return 0;
}
/* at here, this pkt must be a vxlan pkt,
we need to swap the mac addresses and ipv4 addresses */
if (do_inject)
{
struct rte_ether_addr swap_tmp;
#if RTE_VERSION_NUM(21, 11, 0, 0) <= RTE_VERSION
rte_ether_addr_copy(ð_hdr->dst_addr, &swap_tmp);
rte_ether_addr_copy(ð_hdr->src_addr, ð_hdr->dst_addr);
rte_ether_addr_copy(&swap_tmp, ð_hdr->src_addr);
#else
rte_ether_addr_copy(ð_hdr->d_addr, &swap_tmp);
rte_ether_addr_copy(ð_hdr->s_addr, ð_hdr->d_addr);
rte_ether_addr_copy(&swap_tmp, ð_hdr->s_addr);
#endif
rte_be32_t ipv4_addr_swap_tmp;
ipv4_addr_swap_tmp = ipv4_hdr->dst_addr;
ipv4_hdr->dst_addr = ipv4_hdr->src_addr;
ipv4_hdr->src_addr = ipv4_addr_swap_tmp;
}
return 1;
}
void * txonly_loop(void * arg)
{
uintptr_t sid = (uintptr_t)arg;
marsio_buff_t * rx_buff[BURST_MAX];
marsio_thread_init(mr_instance);
for (;;)
{
int ret = marsio_recv_burst(dev_handler, sid, rx_buff, 1);
if (ret != 1)
continue;
marsio_buff_t * rx_buff_ptr = rx_buff[0];
char * pkt_ptr = marsio_buff_mtod(rx_buff_ptr);
unsigned int pkt_len = marsio_buff_datalen(rx_buff_ptr);
assert(pkt_ptr != NULL && pkt_len != 0);
bool should_inject = false;
if (__is_bfd_pkt_and_make_answer(pkt_ptr, pkt_len, 0) > 0)
{
__is_bfd_pkt_and_make_answer(pkt_ptr, pkt_len, 1);
should_inject = true;
}
else if (__is_g_vxlan_and_make_inject(pkt_ptr, pkt_len, 0) > 0)
{
/* for all vxlan pkts, create an offload request */
marsio_buff_t * offload_request_buf = marsio_buff_malloc_smartoffload(dev_handler, pkt_ptr, pkt_len);
assert(offload_request_buf != NULL);
__is_g_vxlan_and_make_inject(pkt_ptr, pkt_len, 1);
/* inject the offload request */
marsio_send_burst_with_options(sendpath, sid, &offload_request_buf, 1, MARSIO_SEND_OPT_CTRL);
should_inject = true;
}
if (should_inject)
{
marsio_send_burst_with_options(sendpath, sid, &rx_buff_ptr, 1, 0);
}
else
{
marsio_buff_free(mr_instance, rx_buff, 1, MARSIO_SOCKET_ID_ANY, MARSIO_LCORE_ID_ANY);
}
}
return (void *)NULL;
}
int help()
{
return 0;
}
int main(int argc, char * argv[])
{
int opt = 0;
while ((opt = getopt(argc, argv, "s:t:a:c:b:d:h?rl")) != -1)
{
char * endptr = NULL;
switch (opt)
{
case '?':
case 'h': {
help();
break;
}
case 'd': {
snprintf(dev_symbol, sizeof(dev_symbol), "%s", optarg);
break;
}
case 'a': {
snprintf(appsym, sizeof(appsym), "%s", optarg);
break;
}
case 'c': {
cpu_mask = strtoull(optarg, &endptr, 0);
if (cpu_mask == 0 && endptr == optarg)
help();
break;
}
case 'b': {
nr_burst = strtoull(optarg, &endptr, 0);
if (nr_burst == 0 && endptr == optarg)
help();
break;
}
case 'l': {
self_loop = 1;
break;
}
default:
help();
break;
}
}
mr_instance = marsio_create();
if (mr_instance == NULL)
{
fprintf(stderr, "Marsio instance create failed. ");
abort();
}
unsigned int opt_value = 1;
marsio_option_set(mr_instance, MARSIO_OPT_EXIT_WHEN_ERR, &opt_value, sizeof(opt_value));
marsio_option_set(mr_instance, MARSIO_OPT_THREAD_MASK, &cpu_mask, sizeof(cpu_mask));
marsio_init(mr_instance, appsym);
nr_thread = __builtin_popcountll(cpu_mask);
dev_handler = marsio_open_device(mr_instance, dev_symbol, nr_thread, nr_thread);
fprintf(stdout, "Thread Count = %d\n", nr_thread);
sendpath = marsio_sendpath_create_by_vdev(dev_handler);
assert(sendpath != NULL);
pthread_t __tmp_pid[nr_thread];
for (int i = 0; i < nr_thread; i++)
{
pthread_create(&__tmp_pid[i], NULL, txonly_loop, (void *)(uintptr_t)i);
}
for (int i = 0; i < nr_thread; i++)
{
pthread_join(__tmp_pid[i], NULL);
}
marsio_destory(mr_instance);
fprintf(stdout, "RXONLY is terminated. ");
return 0;
}
|