-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.cpp
More file actions
395 lines (294 loc) · 8.63 KB
/
main.cpp
File metadata and controls
395 lines (294 loc) · 8.63 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
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
#include <stdio.h>
#include <string.h>
#include <pcap.h>
#include <time.h>
#include <stdlib.h>
#include "common.h"
//#define DEBUG
#ifdef DEBUG
#define debug printf
#else
#define debug
#endif
#define PACKET_SIZE 100
#define PORT_0 0
#define PORT_1 1
#define PORT_2 2
#define HANDLE_NUM 10
unsigned long long repeat_num_g = 10000;
time_t start, end;
void start_timer()
{
time (&start);
}
void stop_timer()
{
time (&end);
}
void print_rates(unsigned long long pktCount, unsigned long long pktSize)
{
double diff = difftime (end, start);
unsigned long long byteCount = pktCount * pktSize * 8;
printf ("Sent/received %llu packets or %llu bytes per %.2lf seconds\n", pktCount, byteCount, diff);
printf ("Rate is %.2lf pps or %.2lf bps\n", pktCount/diff, byteCount/diff);
}
void my_callback(u_char *useless, const struct pcap_pkthdr* pkthdr, const u_char*packet)
{
debug("Received a packet of length %d \n", pkthdr->len);
}
int createPacket(u_char* packet)
{
int i = 0;
packet[0]=1;
packet[1]=1;
packet[2]=1;
packet[3]=1;
packet[4]=1;
packet[5]=1;
packet[6]=2;
packet[7]=2;
packet[8]=2;
packet[9]=2;
packet[10]=2;
packet[11]=2;
packet[12]=0x88;
packet[13]=0x80;
for(i = 14; i < PACKET_SIZE; i++)
{
packet[i] = i%256;
}
return 0;
}
int test1()
{
pcap_t *handles[HANDLE_NUM];
pcap_pkthdr *header = NULL;
const u_char *pktdata = NULL;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t *deviceList;
pcap_if_t *device;
u_char packet[PACKET_SIZE];
int i = 0;
int status = 0;
dpdkpcap_stats_t rxPackets;
dpdkpcap_stats_t txPackets;
memset(handles, 0, sizeof(handles));
createPacket(packet);
debug("Retrieving the device list from the local machine\n");
if(pcap_findalldevs(&deviceList, errbuf) < 0)
{
printf("Error in pcap_findalldevs_ex: %s\n", errbuf);
return -1;
}
i = 0;
for(device = deviceList; device != NULL; device = device->next)
{
debug("%s\n", device->name);
handles[i] = pcap_open_live(device->name, BUFSIZ, 1, 1000, errbuf);
if (handles[i] == NULL)
{
printf("Couldn't open device %s: %s\n", device->name, errbuf);
return -1;
}
status = linkStatusGet(device->name);
if (status)
printf("Link is UP on device %s\n", device->name);
else
printf("Link is DOWN on device %s\n", device->name);
i++;
}
start_timer();
for (i = 0; i < repeat_num_g; i++)
{
debug("Sending a packet %d\n", i + 1);
if (pcap_sendpacket(handles[PORT_0], packet, sizeof(packet)) < 0)
{
printf("pcap_sendpacket failed : %s\n", pcap_geterr(handles[PORT_0]));
return -1;
}
/* debug("Receiving a packet %d\n", i + 1);
if (pcap_next_ex(handles[RX_HANDLE], &header, &pktdata) < 0)
{
printf("pcap_next_ex failed : %s\n", pcap_geterr(handles[RX_HANDLE]));
continue;
}
debug("Received a buffer of length %d\n", header->len);*/
}
stop_timer();
print_rates(repeat_num_g, PACKET_SIZE);
for(i = 0; i < HANDLE_NUM; i++)
{
if (handles[i] == 0)
continue;
rxPackets = rxStatsGet(handles[i]);
txPackets = txStatsGet(handles[i]);
printf("%d : RX : %lld, RX errors: %lld \n", i, rxPackets.packets, rxPackets.errors);
printf("%d : TX : %lld, TX errors : %lld \n", i, txPackets.packets, txPackets.errors);
pcap_close(handles[i]);
}
pcap_freealldevs(deviceList);
return 0;
}
int test2()
{
pcap_t *handles[HANDLE_NUM];
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t *deviceList;
pcap_if_t *device;
u_char packet[PACKET_SIZE];
int i = 0;
memset(handles, 0, sizeof(handles));
createPacket(packet);
debug("Retrieving the device list from the local machine\n");
if(pcap_findalldevs(&deviceList, errbuf) < 0)
{
printf("Error in pcap_findalldevs_ex: %s\n", errbuf);
return -1;
}
i = 0;
for(device = deviceList; device != NULL; device = device->next)
{
debug("%s\n", device->name);
handles[i] = pcap_open_live(device->name, BUFSIZ, 1, 1000, errbuf);
if (handles[i] == NULL)
{
printf("Couldn't open device %s: %s\n", device->name, errbuf);
return -1;
}
i++;
}
for (i = 0; i < repeat_num_g; i++)
{
debug("Sending a packet %d\n", i + 1);
if (pcap_sendpacket(handles[PORT_0], packet, sizeof(packet)) < 0)
{
printf("pcap_sendpacket failed : %s\n", pcap_geterr(handles[PORT_0]));
return -1;
}
debug("Receiving a packet %d\n", i + 1);
if (pcap_loop(handles[PORT_1], 1, my_callback, NULL) < 0)
{
printf("pcap_loop failed\n");
continue;
}
}
for(i = 0; i < HANDLE_NUM; i++)
{
if (handles[i] == 0)
continue;
pcap_close(handles[i]);
}
pcap_freealldevs(deviceList);
return 0;
}
int test3()
{
char errbuf[PCAP_ERRBUF_SIZE];
char *deviceName = NULL;
pcap_t *handle = NULL;
u_char packet[PACKET_SIZE];
int packets_number = repeat_num_g;
createPacket(packet);
if ( (deviceName = pcap_lookupdev(errbuf)) == NULL)
{
return -1;
}
handle = pcap_open_live(deviceName, BUFSIZ, 1, 1000, errbuf);
if (handle == NULL)
{
printf("Couldn't open device %s: %s\n", deviceName, errbuf);
return -1;
}
printf("Sending %u packets\n", packets_number);
start_timer();
if (dpdpcap_transmit_in_loop(handle, packet, sizeof(packet), packets_number) < 0)
{
printf("dpdpcap_transmit_in_loop failed : %s\n", pcap_geterr(handle));
return -1;
}
stop_timer();
print_rates(packets_number, PACKET_SIZE);
pcap_close(handle);
return 0;
}
int test4()
{
pcap_t *handles[HANDLE_NUM];
pcap_pkthdr *header = NULL;
const u_char *pktdata = NULL;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t *deviceList;
pcap_if_t *device;
u_char packet[PACKET_SIZE];
int i = 0;
int status = 0;
dpdkpcap_stats_t rxPackets;
dpdkpcap_stats_t txPackets;
memset(handles, 0, sizeof(handles));
createPacket(packet);
debug("Retrieving the device list from the local machine\n");
if(pcap_findalldevs(&deviceList, errbuf) < 0)
{
printf("Error in pcap_findalldevs_ex: %s\n", errbuf);
return -1;
}
i = 0;
for(device = deviceList; device != NULL; device = device->next)
{
debug("%s\n", device->name);
handles[i] = pcap_open_live(device->name, BUFSIZ, 1, 1000, errbuf);
if (handles[i] == NULL)
{
printf("Couldn't open device %s: %s\n", device->name, errbuf);
return -1;
}
status = linkStatusGet(device->name);
if (status)
printf("Link is UP on device %s\n", device->name);
else
printf("Link is DOWN on device %s\n", device->name);
i++;
}
start_timer();
for (i = 0; i < repeat_num_g; i++)
{
debug("Sending a packet %d\n to %d port", i + 1, PORT_0);
if (pcap_sendpacket(handles[PORT_0], packet, sizeof(packet)) < 0)
{
printf("pcap_sendpacket failed : %s\n", pcap_geterr(handles[PORT_0]));
return -1;
}
debug("Sending a packet %d\n to %d port", i + 1, PORT_1);
if (pcap_sendpacket(handles[PORT_1], packet, sizeof(packet)) < 0)
{
printf("pcap_sendpacket failed : %s\n", pcap_geterr(handles[PORT_1]));
return -1;
}
}
stop_timer();
print_rates(repeat_num_g, PACKET_SIZE);
for(i = 0; i < HANDLE_NUM; i++)
{
if (handles[i] == 0)
continue;
rxPackets = rxStatsGet(handles[i]);
txPackets = txStatsGet(handles[i]);
printf("%d : RX : %lld, RX errors: %lld \n", i, rxPackets.packets, rxPackets.errors);
printf("%d : TX : %lld, TX errors : %lld \n", i, txPackets.packets, txPackets.errors);
pcap_close(handles[i]);
}
pcap_freealldevs(deviceList);
return 0;
}
int main(int argc, char *argv[])
{
if (argc > 1)
{
repeat_num_g = atol(argv[1]);
}
(test1() == 0) ? printf("Test 1 - OK\n") : printf("Test 1 - FAILED\n");
//(test2() == 0) ? printf("Test 2 - OK\n") : printf("Test 2 - FAILED\n");
//(test3() == 0) ? printf("Test 3 - OK\n") : printf("Test 3 - FAILED\n");
(test4() == 0) ? printf("Test 4 - OK\n") : printf("Test 4 - FAILED\n");
return(0);
}