-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestNIC.cpp
More file actions
212 lines (166 loc) · 6.36 KB
/
TestNIC.cpp
File metadata and controls
212 lines (166 loc) · 6.36 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
#include "TestNIC.h"
namespace TestNIC
{
void TestNIC::EnableTimestamping(int sock)
{
int flags =
SOF_TIMESTAMPING_RX_HARDWARE
| SOF_TIMESTAMPING_TX_HARDWARE
| SOF_TIMESTAMPING_RAW_HARDWARE;
// | SOF_TIMESTAMPING_SYS_HARDWARE; // Apparently this option is deprecated?
std::cout << "Selecting hardware timestamping mode 0x";
std::cout << std::hex << flags << std::dec << " [fd = " << sock << "]" << std::endl;
int rc = setsockopt(sock, SOL_SOCKET, SO_TIMESTAMPING, &flags, sizeof(flags));
std::cout << "Return code from setsockopt() is [rc = " << rc << "]" << std::endl;
if (true)
{
int enable = 1;
rc = setsockopt(sock, SOL_SOCKET, SO_TIMESTAMP, &enable, sizeof(enable));
std::cout << "Return code of SO_TIMESTAMP [rc = " << rc << "]" << std::endl;
}
if (true)
{
int enable = 1;
rc = setsockopt(sock, SOL_SOCKET, SO_TIMESTAMPNS, &enable, sizeof(enable));
std::cout << "Return code of SO_TIMESTAMPNS [rc = " << rc << "]" << std::endl;
}
}
void TestNIC::ReadPacket(int sock)
{
struct msghdr msg;
struct sockaddr_in host_address;
struct iovec iov;
int flags = 0;
char control[1024]; // Buffer for CMSGs
char buffer[4000]; // Data Buffer
host_address.sin_family = AF_INET;
host_address.sin_port = htons(15516);
host_address.sin_addr.s_addr = INADDR_ANY;
msg.msg_namelen = sizeof(struct sockaddr_in); // Needs to be set each call
msg.msg_name = &host_address;
msg.msg_iov = &iov;
msg.msg_iovlen = 1; // One buffer
iov.iov_base = buffer;
iov.iov_len = 4000;
msg.msg_control = control;
msg.msg_controllen = 1024; // Needs to be set each call
// Do a recvmsg()
ssize_t bytes_rcvd = recvmsg(sock, &msg, flags);
std::cout << "Bytes received = " << bytes_rcvd << std::endl;
std::cout << "Length of CMSGs = " << msg.msg_controllen << std::endl;
std::cout << "Message flags: " << msg.msg_flags << std::endl;
std::cout << "Attempting to extract timestamps ..." << std::endl;
struct cmsghdr* cmsg;
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
{
std::cout << "CMSG level = " << cmsg->cmsg_level << std::endl;
std::cout << "CMSG type = " << cmsg->cmsg_type << std::endl;
if (cmsg->cmsg_type == 35) // SO_TIMESTAMPNS
{
// SO_TIMESTAMPNS
struct timespec *ts = (struct timespec *)CMSG_DATA(cmsg);
std::cout << "SO_TIMESTAMPNS: [sec = " << ts->tv_sec << "]";
std::cout << "[nsec = " << ts->tv_nsec << "]" << std::endl;
}
if (cmsg->cmsg_type == 37) // SO_TIMESTAMPING
{
struct timespec *ts = (struct timespec *)CMSG_DATA(cmsg);
std::cout << "SO_TIMESTAMPING:" << std::endl;
std::cout << "SW Timespec [sec = " << ts[0].tv_sec << "]";
std::cout << "[nsec = " << ts[0].tv_nsec << "]" << std::endl; // 0 = Software
std::cout << "HW Timespec [sec = " << ts[1].tv_sec << "]";
std::cout << "[nsec = " << ts[1].tv_nsec << "]" << std::endl; // 1 = HW transformed
std::cout << "RAW Timespec [sec = " << ts[2].tv_sec << "]";
std::cout << "[nsec = " << ts[2].tv_nsec << "]" << std::endl; // 2 = HW Raw
}
}
}
void TestNIC::ListenMulticast(std::string local_ip)
{
// Create the buffer
read_buffer = new char[1000];
const std::string group_ip = "233.37.54.162";
const int port = 15516;
// Create a UDP socket and listen for packets
m_udp_feed = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (m_udp_feed < 0)
{
std::cout << "socket creation failed" << std::endl;
throw new std::runtime_error("KRX socket creation failed");
}
std::cout << "Created socket [fd = " << m_udp_feed << "]" << std::endl;
// Enable SO_REUSEADDR to allow multiple instances of this
// application to receive copies of the multicast datagrams.
if (true)
{
int reuse = 1;
setsockopt(m_udp_feed, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse));
std::cout << "Reuse address" << std::endl;
}
// http://stackoverflow.com/questions/9243292/subscribing-to-multiple-multicast-groups-on-one-socket-linux-c
// Bind to the multicast port
struct sockaddr_in host_address;
memset(&host_address, 0, sizeof(host_address));
host_address.sin_family = AF_INET;
// Bind to the *GROUP* address in the case of multicast UDP.
host_address.sin_addr.s_addr = inet_addr(group_ip.c_str()); // Linux
host_address.sin_port = htons(port);
// Using bind from the global namespace, not std::bind
int rc = ::bind(m_udp_feed, (struct sockaddr*)&host_address, sizeof(host_address));
if (rc < 0)
{
std::cout << "Socket binding failed" << std::endl;
throw new std::runtime_error("Socket binding failed");
}
std::cout << "Bound" << std::endl;
// Join the Multicast Group
// For the machine to receive multicast packets, I needed to allow IP forwarding
// and disable rp_filter, both in /etc/sysctl.conf
ip_mreq request;
request.imr_multiaddr.s_addr = inet_addr(group_ip.c_str());
request.imr_interface.s_addr = inet_addr(local_ip.c_str());
std::cout << "About to join Multicast group [group = " << group_ip << "]" << std::endl;
int am = setsockopt(m_udp_feed, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&request, sizeof(request));
if (am < 0)
{
std::cout << "Failed to join Multicast group :: "
<< group_ip << " on " << local_ip;
throw new std::runtime_error("Failed to join Multicast group");
}
std::cout << "Return code to join multicastgroup [rc = " << rc << "]" << std::endl;
// Success!
std::cout << "Socket created, listening on " << group_ip << ":" << port;
std::cout << " [fd = " << m_udp_feed << "]" << std::endl;
std::cout << "Trying to enable timestamps ..." << std::endl;
EnableTimestamping(m_udp_feed);
}
void TestNIC::DoEventLoop()
{
struct epoll_event evt;
struct epoll_event *evts;
int epfd = epoll_create(1);
if (epfd == -1)
std::cout << "epoll_create() failed";
std::cout << "epoll created [epfd = " << epfd << "]" << std::endl;
evt.data.fd = m_udp_feed;
evt.events = EPOLLIN | EPOLLET;
int rc = epoll_ctl(epfd, EPOLL_CTL_ADD, m_udp_feed, &evt);
std::cout << "Added socket to epoll" << std::endl;
while (true)
{
int rc = epoll_wait(epfd, &evt, 1, 5000 /* milliseconds */);
if (rc > 0)
{
std::cout << "Packet received" << std::endl;
ReadPacket(m_udp_feed);
}
if (rc == 0)
std::cout << "epoll_wait() timed out" << std::endl;
if (rc == -1)
{
std::cout << "epoll_wait() failed [error = " << errno << "]";
return;
}
} // while (true)
}
}