-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecv_send.c
408 lines (358 loc) · 12.3 KB
/
recv_send.c
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <stdint.h>
#include <inttypes.h>
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_cycles.h>
#include <rte_lcore.h>
#include <rte_mbuf.h>
#include <rte_ether.h>
#include <rte_arp.h>
#include <rte_icmp.h>
#include <rte_ip.h>
#include <rte_udp.h>
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include "recv_send.h"
#define RX_RING_SIZE 128
#define TX_RING_SIZE 512
#define NUM_MBUFS 8191
#define MBUF_CACHE_SIZE 250
#define BURST_SIZE 32
int receive_send_pkt(struct rte_mempool *mbuf_pool);
void PRINT_MESSAGE(unsigned char *msg, int len);
static const struct rte_eth_conf port_conf_default = {
.rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN, },
.txmode = { .offloads = DEV_TX_OFFLOAD_IPV4_CKSUM ,}
};
FILE *fp;
void PRINT_MESSAGE(unsigned char *msg, int len)
{
int row_cnt,rows,rest_bytes,hex_cnt,ch_cnt,cnt,xi,ci;
if (NULL == msg){
printf("PRINT_MESSAGE(): NULL message ?\n");
return;
}
/*if ((len*5) > 2048){ // 5 format bytes for one raw data byte
printf("Too large[len(%d) > max(%d)] to print out!\n",len,2048);
return;
}*/
rest_bytes = len % 16;
rows = len / 16;
ci = xi = 0;
for(row_cnt=0; row_cnt<rows; row_cnt++){
/*------------- print label for each row --------------*/
printf("%04x: ",(row_cnt+1)<<4);
/*------------- print hex-part --------------*/
for(hex_cnt=1; hex_cnt<=8; hex_cnt++){
if (hex_cnt < 8)
printf("%02x ",msg[xi++]); /* Must be unsigned, otherwise garbage displayed */
else
printf("%02x",msg[xi++]); /* Must be unsigned, otherwise garbage displayed */
}
/* delimiters space for each 8's Hex char */
printf(" ");
for(hex_cnt=9; hex_cnt<=16; hex_cnt++){
if (hex_cnt < 16)
printf("%02x ",msg[xi++]);
else
printf("%02x",msg[xi++]);
}
/* delimiters space bet. Hex and Character row */
printf(" ");
/*------------- print character-part --------------*/
for(ch_cnt=1; ch_cnt<=16; ch_cnt++,ci++){
if (msg[ci]>0x20 && msg[ci]<=0x7e){
printf("%c",msg[ci]);
}
else{
printf(".");
}
}
printf("\n");
} //for
/*================ print the rest bytes(hex & char) ==================*/
if (rest_bytes == 0) {
printf("\n");
return;
}
/*------------- print label for last row --------------*/
printf("%04x: ",(row_cnt+1)<<4);
/*------------- print hex-part(rest) --------------*/
if (rest_bytes < 8){
for(hex_cnt=1; hex_cnt<=rest_bytes; hex_cnt++){
printf("%02x ",msg[xi++]);
}
/* fill in the space for 16's Hex-part alignment */
for(cnt=rest_bytes+1; cnt<=8; cnt++){ /* from rest_bytes+1 to 8 */
if (cnt < 8)
printf(" ");
else
printf(" ");
}
/* delimiters bet. hex and char */
printf(" ");
for(cnt=9; cnt<=16; cnt++){
if (cnt < 16)
printf(" ");
else
printf(" ");
}
printf(" ");
}
else if (rest_bytes == 8){
for(hex_cnt=1; hex_cnt<=rest_bytes; hex_cnt++){
if (hex_cnt < 8)
printf("%02x ",msg[xi++]);
else
printf("%02x",msg[xi++]);
}
printf(" ");
for(cnt=9; cnt<=16; cnt++){
if (cnt < 16)
printf(" ");
else
printf(" ");
}
printf(" ");
}
else{ /* rest_bytes > 8 */
for(hex_cnt=1; hex_cnt<=8; hex_cnt++){
if (hex_cnt < 8)
printf("%02x ",msg[xi++]);
else
printf("%02x",msg[xi++]);
}
/* delimiters space for each 8's Hex char */
printf(" ");
for(hex_cnt=9; hex_cnt<=rest_bytes; hex_cnt++){ /* 9 - rest_bytes */
if (hex_cnt < 16)
printf("%02x ",msg[xi++]);
else
printf("%02x",msg[xi++]);
}
for(cnt=rest_bytes+1; cnt<=16; cnt++){
if (cnt < 16)
printf(" ");
else
printf(" ");
}
/* delimiters space bet. Hex and Character row */
printf(" ");
} /* else */
/*------------- print character-part --------------*/
for(ch_cnt=1; ch_cnt<=rest_bytes; ch_cnt++,ci++){
if (msg[ci]>0x20 && msg[ci]<=0x7e){
printf("%c",msg[ci]);
}
else
printf(".");
}
printf("\n");
}
static inline int
port_init(uint16_t port, struct rte_mempool *mbuf_pool)
{
struct rte_eth_conf port_conf = port_conf_default;
struct rte_eth_dev_info dev_info;
const uint16_t rx_rings = 1, tx_rings = 1;
int retval;
uint16_t q;
if (!rte_eth_dev_is_valid_port(port))
return -1;
rte_eth_dev_info_get(port, &dev_info);
if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
port_conf.txmode.offloads |=
DEV_TX_OFFLOAD_MBUF_FAST_FREE;
retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
if (retval != 0)
return retval;
/* Allocate and set up 1 RX queue per Ethernet port. */
for (q = 0; q < rx_rings; q++) {
retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
rte_eth_dev_socket_id(port), NULL, mbuf_pool);
if (retval < 0)
return retval;
}
/* Allocate and set up 1 TX queue per Ethernet port. */
for (q = 0; q < tx_rings; q++) {
retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,
rte_eth_dev_socket_id(port), NULL);
if (retval < 0)
return retval;
}
/* Start the Ethernet port. */
retval = rte_eth_dev_start(port);
if (retval < 0)
return retval;
//rte_eth_promiscuous_enable(port);
return 0;
}
int receive_send_pkt(struct rte_mempool *mbuf_pool)
{
uint64_t total_tx;
uint64_t recv_size = 0;
struct rte_mbuf *single_pkt;
unsigned char mac_addr[6];// = {0x76,0xfb,0xc5,0x78,0x6e,0xd5};
rte_eth_macaddr_get(0,(struct ether_addr *)mac_addr);
printf("mac = %x:%x:%x:%x:%x:%x\n", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
uint32_t ip_addr = htonl(0xc0a80166);
uint64_t cur_tsc;
uint64_t prev_tsc = 0;
float cur_time = 0;
uint64_t cur_clock = 0;
struct ether_hdr *eth_hdr;
struct icmp_hdr *icmphdr;
struct udp_hdr udphdr;
struct arp_hdr *arphdr;
struct rte_mbuf *pkt[BURST_SIZE];
for(;;) {
total_tx = 0;
/*for(i=0;i<BURST_SIZE;i++) {
pkt[i] = rte_pktmbuf_alloc(mbuf_pool);
if (pkt[i] != 0)
total_addr++;
//printf("pkt addr = %x\n", pkt[i]);
}*/
//printf("in core %u\n", rte_lcore_id());
/*uint16_t port;
RTE_ETH_FOREACH_DEV(port) {
printf("port id = %u\n", port);
}*/
uint16_t nb_rx = rte_eth_rx_burst(0, 0,pkt,BURST_SIZE);
//printf("nb_rx = %x\n", nb_rx);
//printf("total addr = %lu\n", total_addr);
if(nb_rx == 0) {
//for(i=0;i<BURST_SIZE;i++)
//rte_pktmbuf_free(pkt[i]);
continue;
}
//printf("nb_rx = %x\n", nb_rx);
//fp = fopen("test.txt","a");
//fprintf(fp, "nb rx = %x\n", nb_rx);
for(int i=0;i<nb_rx;i++) {
single_pkt = pkt[i];
rte_prefetch0(rte_pktmbuf_mtod(single_pkt, void *));
//printf("pkt->pkt_len = %x data_len = %x\n", pkt[i]->pkt_len, pkt[i]->data_len);
//fprintf(fp, "%d %d %x %x %x %x %c", i, nb_rx, pkt[i]->pkt_len, pkt[i]->data_off, pkt[i]->buf_addr, rte_pktmbuf_mtod(pkt[i],struct ether_hdr*), '\n');
//printf("i = %d nb_rx = %d\n", i, nb_rx);
//printf("mbuf addr = %x\n", (uint32_t *)(pkt[i]->buf_addr));
eth_hdr = rte_pktmbuf_mtod(single_pkt,struct ether_hdr*);
//printf("ether type = %x\n", eth_hdr->ether_type);
if (eth_hdr->ether_type == htons(0x0806)) {
//PRINT_MESSAGE((char *)eth_hdr,pkt[i]->data_len);
memcpy(eth_hdr->d_addr.addr_bytes,eth_hdr->s_addr.addr_bytes,6);
memcpy(eth_hdr->s_addr.addr_bytes,mac_addr,6);
arphdr = (struct arp_hdr *)(rte_pktmbuf_mtod(single_pkt, unsigned char *) + sizeof(struct ether_hdr));
//fprintf(fp, "arp src ip = %x op code = %x\n", arphdr->arp_data.arp_sip, arphdr->arp_op);
if (arphdr->arp_op == htons(0x0001) && arphdr->arp_data.arp_tip == ip_addr) {
//printf("<%d\n", __LINE__);
memcpy(arphdr->arp_data.arp_tha.addr_bytes,arphdr->arp_data.arp_sha.addr_bytes,6);
memcpy(arphdr->arp_data.arp_sha.addr_bytes,mac_addr,6);
arphdr->arp_data.arp_tip = arphdr->arp_data.arp_sip;
arphdr->arp_data.arp_sip = ip_addr;
arphdr->arp_op = htons(0x0002);
}
pkt[total_tx++] = single_pkt;
//fprintf(fp, "src mac = %x:%x:%x:%x:%x:%x dst mac = %x:%x:%x:%x:%x:%x\n", arphdr->arp_data.arp_sha.addr_bytes[0], arphdr->arp_data.arp_sha.addr_bytes[1], arphdr->arp_data.arp_sha.addr_bytes[2], arphdr->arp_data.arp_sha.addr_bytes[3], arphdr->arp_data.arp_sha.addr_bytes[4], arphdr->arp_data.arp_sha.addr_bytes[5], arphdr->arp_data.arp_tha.addr_bytes[0], arphdr->arp_data.arp_tha.addr_bytes[1], arphdr->arp_data.arp_tha.addr_bytes[2], arphdr->arp_data.arp_tha.addr_bytes[3], arphdr->arp_data.arp_tha.addr_bytes[4], arphdr->arp_data.arp_tha.addr_bytes[5]);
}
/*printf("recv packet from: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
" %02" PRIx8 " %02" PRIx8 " %02" PRIx8 " : ",
eth_hdr->s_addr.addr_bytes[0],eth_hdr->s_addr.addr_bytes[1],
eth_hdr->s_addr.addr_bytes[2],eth_hdr->s_addr.addr_bytes[3],
eth_hdr->s_addr.addr_bytes[4],eth_hdr->s_addr.addr_bytes[5]);*/
else {
//unsigned char dst_mac[6] = {0x7a,0x1f,0x95,0x13,0x8d,0x38};
//unsigned char src_mac[6] = {0xbe,0xc2,0x46,0xe9,0x1a,0x24};
memcpy(eth_hdr->d_addr.addr_bytes,eth_hdr->s_addr.addr_bytes,6);
memcpy(eth_hdr->s_addr.addr_bytes,mac_addr,6);
struct ipv4_hdr *ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(single_pkt, unsigned char *) + sizeof(struct ether_hdr));
ip_hdr->dst_addr = ip_hdr->src_addr;
ip_hdr->src_addr = ip_addr;
single_pkt->ol_flags |= PKT_TX_IPV4 | PKT_TX_IP_CKSUM;
single_pkt->l2_len = sizeof(struct ether_hdr);
single_pkt->l3_len = sizeof(struct ipv4_hdr);
ip_hdr->hdr_checksum = 0;
//printf("%lu\n", rte_rdtsc());
switch (ip_hdr->next_proto_id) {
case IPV4_ICMP:
icmphdr = (struct icmp_hdr *)(rte_pktmbuf_mtod(single_pkt, unsigned char *) + sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr));
icmphdr->icmp_type = 0;
uint32_t cksum = ~icmphdr->icmp_cksum & 0xffff;
cksum += ~htons(8 << 8) & 0xffff;
cksum += htons(0 << 8);
cksum = (cksum & 0xffff) + (cksum >> 16);
cksum = (cksum & 0xffff) + (cksum >> 16);
icmphdr->icmp_cksum = ~cksum;
pkt[total_tx++] = single_pkt;
break;
case IPV4_UDP:
//udphdr = (struct udp_hdr *)(rte_pktmbuf_mtod(pkt[i], unsigned char *) + sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr));
recv_size += single_pkt->data_len;
cur_tsc = rte_rdtsc();
if (likely(prev_tsc != 0)) {
cur_clock = rte_get_tsc_hz();
cur_time += (float)(cur_tsc - prev_tsc) / (float)cur_clock;
//printf("diff_tsc = %lu, cur_clock = %lu\n", cur_tsc - prev_tsc, cur_clock);
//printf("%.9f\n", cur_time);
if (unlikely(cur_time >= 1)) {
printf("tx rate = %lu Mb/s\n", recv_size/131072);
cur_time = 0;
recv_size = 0;
}
}
rte_pktmbuf_free(single_pkt);
prev_tsc = cur_tsc;
continue;
break;
default:
rte_pktmbuf_free(single_pkt);
;
}
}
}
//printf("pkt addr = %x, nb_rx = %d\n", pkt, nb_rx);
//fclose(fp);
if (total_tx > 0) {
uint16_t nb_tx = rte_eth_tx_burst(0, 0,pkt, total_tx);
//fprintf(fp, "nb_tx = %x\n", nb_tx);
/*for(i=0;i<BURST_SIZE;i++)
rte_pktmbuf_free(pkt[i]);*/
if (unlikely(nb_tx < total_tx)) {
uint16_t buf;
for(buf = nb_tx; buf < total_tx; buf++)
rte_pktmbuf_free(pkt[buf]);
}
}
//printf("<%d\n", __LINE__);
}
return 0;
}
int main(int argc, char *argv[])
{
struct rte_mempool *mbuf_pool;
uint16_t portid;
int ret = rte_eal_init(argc, argv);
if (ret < 0)
rte_exit(EXIT_FAILURE, "initlize fail!");
argc -= ret;
argv += ret;
if (rte_lcore_count() != 2)
rte_exit(EXIT_FAILURE, "We only need 2 cores\n");
/* Creates a new mempool in memory to hold the mbufs. */
mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS,
MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
if (mbuf_pool == NULL)
rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
/* Initialize all ports. */
RTE_ETH_FOREACH_DEV(portid) {
if (port_init(portid,mbuf_pool) != 0)
rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n",portid);
}
unsigned lcore_id;
RTE_LCORE_FOREACH_SLAVE(lcore_id) {
rte_eal_remote_launch((void *)receive_send_pkt, mbuf_pool, lcore_id);
}
rte_eal_mp_wait_lcore();
return 0;
}