-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpkt_gen.c
130 lines (107 loc) · 2.77 KB
/
pkt_gen.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
/*
* Copyright (C) 2018
* Authors: Ivan Khoronzhuk <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation version 2.
*
* This program is distributed "as is" WITHOUT ANY WARRANTY of any
* kind, whether express or implied; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <poll.h>
#include <stdio.h>
#include "pkt_gen.h"
#include <unistd.h>
#include <errno.h>
#define MAX_LATENCY 5000
static int fast_pktgen(void)
{
struct sockaddr *addr = (struct sockaddr *)&plget->sk_addr;
int dsize = plget->sk_payload_size;
int sid = plget->stream_id;
char *packet = plget->pkt;
int sfd = plget->sfd;
int ret;
plget->inum = plget->pkt_num ? plget->pkt_num : ~0;
for (plget->icnt = 0; plget->icnt < plget->inum; plget->icnt++) {
if (plget->flags & PLF_PTP)
sid_wr(htons((plget->icnt & SEQ_ID_MASK) | sid));
tid_wr(plget->icnt);
ret = sendto(sfd, packet, dsize, 0, addr,
sizeof(plget->sk_addr));
if (ret != dsize) {
if (ret < 0)
perror("sendto");
else
perror("cannot send whole packet\n");
break;
}
}
plget->pkt_num = plget->icnt;
return !(plget->icnt == plget->inum);
}
int pktgen_proc(void)
{
struct sockaddr *addr = (struct sockaddr *)&plget->sk_addr;
int dsize = plget->sk_payload_size;
int sid = plget->stream_id;
char *packet = plget->pkt;
int sfd = plget->sfd;
struct pollfd fds[1];
uint64_t exps;
int ret;
ret = plget_start_timer();
if (ret)
return ret;
fds[0].fd = plget->timer_fd;
fds[0].events = POLLIN;
plget->inum = plget->pkt_num ? plget->pkt_num : ~0;
tid_wr(0);
for (plget->icnt = 0; plget->icnt < plget->inum;) {
ret = poll(fds, 1, MAX_LATENCY);
if (ret <= 0) {
if (!ret) {
printf("Timed out\n");
break;
}
perror("Some error on poll()");
break;
}
/* time to send new packet */
if (fds[0].revents & POLLIN) {
ret = read(plget->timer_fd, &exps, sizeof(exps));
if (ret < 0)
return perror("Couldn't read timerfd"), -errno;
ret = sendto(sfd, packet, dsize, 0, addr,
sizeof(plget->sk_addr));
if (ret != dsize) {
if (ret < 0)
perror("sendto");
else
perror("cannot send whole packet\n");
break;
}
if (plget->flags & PLF_PTP)
sid_wr(htons((++plget->icnt & SEQ_ID_MASK) |
sid));
tid_wr(plget->icnt);
}
}
plget->pkt_num = plget->icnt;
return !(plget->icnt == plget->inum);
}
int pktgen(void)
{
int ret;
if (!ts_correct(&plget->interval))
return fast_pktgen();
ret = plget_create_timer();
if (ret)
return ret;
ret = pktgen_proc();
close(plget->timer_fd);
return ret;
}