rogue
Loading...
Searching...
No Matches
Server.cpp
Go to the documentation of this file.
1
17#include "rogue/Directives.h"
18
20
21#include <inttypes.h>
22#include <arpa/inet.h>
23#include <unistd.h>
24
25#include <cerrno>
26#include <chrono>
27#include <cstdlib>
28#include <cstring>
29#include <iostream>
30#include <memory>
31#include <thread>
32
33#include "rogue/GeneralError.h"
34#include "rogue/GilRelease.h"
35#include "rogue/Logging.h"
41
42namespace rpu = rogue::protocols::udp;
44
45#ifndef NO_PYTHON
46 #include <boost/python.hpp>
47namespace bp = boost::python;
48#endif
49
51rpu::ServerPtr rpu::Server::create(uint16_t port, bool jumbo) {
52 rpu::ServerPtr r = std::make_shared<rpu::Server>(port, jumbo);
53 return (r);
54}
55
57rpu::Server::Server(uint16_t port, bool jumbo) : rpu::Core(jumbo) {
58 uint32_t len;
59 uint32_t size;
60
61 port_ = port;
62 udpLog_ = rogue::Logging::create("udp.Server");
63
64 // Create a shared pointer to use as a lock for runThread()
65 std::shared_ptr<int> scopePtr = std::make_shared<int>(0);
66
67 // Create socket
68 if ((fd_ = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
69 throw(rogue::GeneralError::create("Server::Server", "Failed to create socket for port %" PRIu16, port_));
70
71 // Setup Remote Address
72 memset(&locAddr_, 0, sizeof(struct sockaddr_in));
73 locAddr_.sin_family = AF_INET;
74 locAddr_.sin_addr.s_addr = htonl(INADDR_ANY);
75 locAddr_.sin_port = htons(port_);
76
77 memset(&remAddr_, 0, sizeof(struct sockaddr_in));
78
79 // Intentionally no SO_REUSEADDR / SO_REUSEPORT here: the kernel default
80 // EADDRINUSE on duplicate bind is the contract we want, so two Servers
81 // on the same port can't silently coexist with non-deterministic packet
82 // routing. Drafted then reverted in a7d5bc533 — see PR #1193 "Reverted".
83 if (bind(fd_, (struct sockaddr*)&locAddr_, sizeof(locAddr_)) < 0) {
84 ::close(fd_);
85 fd_ = -1;
86 throw(rogue::GeneralError::create("Server::Server",
87 "Failed to bind to local port %" PRIu16 ". Another process may be using it",
88 port_));
89 }
90
91 // Kernel assigns port
92 if (port_ == 0) {
93 len = sizeof(locAddr_);
94 if (getsockname(fd_, (struct sockaddr*)&locAddr_, &len) < 0) {
95 ::close(fd_);
96 fd_ = -1;
97 throw(rogue::GeneralError::create("Server::Server", "Failed to dynamically assign local port"));
98 }
99 port_ = ntohs(locAddr_.sin_port);
100 }
101
102 // Fixed size buffer pool
104 setPoolSize(10000); // Initial value, 10K frames
105
106 udpLog_->debug("UDP server ready. localPort=%" PRIu16 ", maxPayload=%" PRIu32, port_, maxPayload());
107
108 threadEn_ = true;
109 try {
110 thread_ = new std::thread(&rpu::Server::runThread, this, std::weak_ptr<int>(scopePtr));
111 } catch (...) {
112 threadEn_ = false;
113 ::close(fd_);
114 fd_ = -1;
115 throw;
116 }
117
118 // Set a thread name
119#ifndef __MACH__
120 pthread_setname_np(thread_->native_handle(), "UdpServer");
121#endif
122}
123
125rpu::Server::~Server() {
126 this->stop();
127}
128
129void rpu::Server::stop() {
130 if (threadEn_) {
131 threadEn_ = false;
132 // The worker never blocks for more than one select() timeout, so it
133 // can be joined before the socket is closed; closing first would let
134 // a reused descriptor number reach the worker's final syscalls.
135 rogue::GilRelease noGil;
136 thread_->join();
137 delete thread_;
138 thread_ = nullptr;
139 ::close(fd_);
140 fd_ = -1;
141 udpLog_->debug("Stopping UDP server on local port %" PRIu16, port_);
142 }
143}
144
146uint32_t rpu::Server::getPort() {
147 return (port_);
148}
149
151void rpu::Server::acceptFrame(ris::FramePtr frame) {
152 ris::Frame::BufferIterator it;
153 int32_t res;
154 fd_set fds;
155 struct timeval tout;
156 uint32_t x;
157 struct msghdr msg;
158 struct iovec msg_iov[1];
159
160 rogue::GilRelease noGil;
161 ris::FrameLockPtr frLock = frame->lock();
162 std::lock_guard<std::mutex> lock(udpMtx_);
163
164 // Drop errored frames
165 if (frame->getError()) {
166 udpLog_->warning("Dropping errored outbound frame on local port %" PRIu16 ", error=0x%" PRIx8,
167 port_,
168 frame->getError());
169 return;
170 }
171
172 // Setup message header
173 msg.msg_name = &remAddr_;
174 msg.msg_namelen = sizeof(struct sockaddr_in);
175 msg.msg_iov = msg_iov;
176 msg.msg_iovlen = 1;
177 msg.msg_control = NULL;
178 msg.msg_controllen = 0;
179 msg.msg_flags = 0;
180
181 // Go through each buffer in the frame
182 for (it = frame->beginBuffer(); it != frame->endBuffer(); ++it) {
183 if ((*it)->getPayload() == 0) break;
184
185 // Setup IOVs
186 msg_iov[0].iov_base = (*it)->begin();
187 msg_iov[0].iov_len = (*it)->getPayload();
188
189 // Keep trying since select call can fire
190 // but write fails because we did not win the buffer lock
191 do {
192 // Setup fds for select call
193 FD_ZERO(&fds);
194 FD_SET(fd_, &fds);
195
196 // Setup select timeout
197 tout = timeout_;
198
199 if (select(fd_ + 1, NULL, &fds, NULL, &tout) <= 0) {
200 udpLog_->critical("Server::acceptFrame: Timeout waiting for outbound transmit after %" PRIuLEAST32
201 ".%" PRIuLEAST32 " seconds! May be caused by outbound backpressure.",
202 timeout_.tv_sec,
203 timeout_.tv_usec);
204 res = 0;
205 } else if ((res = sendmsg(fd_, &msg, 0)) < 0) {
206 udpLog_->warning("UDP write call failed on server port %" PRIu16 ": %s",
207 port_,
208 std::strerror(errno));
209 }
210 } while (res == 0); // Continue while write result was zero
211 }
212}
213
215void rpu::Server::runThread(std::weak_ptr<int> lockPtr) {
216 ris::BufferPtr buff;
217 ris::FramePtr frame;
218 fd_set fds;
219 int32_t res;
220 struct timeval tout;
221 struct sockaddr_in tmpAddr;
222 uint32_t tmpLen;
223 uint32_t avail;
224
225 // Wait until constructor completes
226 while (!lockPtr.expired()) continue;
227
228 udpLog_->logThreadId();
229
230 // The inherited Pool needs shared_from_this() to allocate, and the
231 // constructor starts this thread before a shared_ptr owns the endpoint.
232 // Datagrams arriving meanwhile queue in the socket.
233 while (threadEn_) {
234 try {
235 ris::Pool::shared_from_this();
236 break;
237 } catch (const std::bad_weak_ptr&) {
238 std::this_thread::sleep_for(std::chrono::milliseconds(1));
239 }
240 }
241 if (!threadEn_) return;
242
243 // Preallocate frame
244 frame = reqLocalFrame(maxPayload(), false);
245
246 while (threadEn_) {
247 // Attempt receive
248 buff = *(frame->beginBuffer());
249 avail = buff->getAvailable();
250 tmpLen = sizeof(struct sockaddr_in);
251 res = recvfrom(fd_, buff->begin(), avail, MSG_TRUNC | MSG_DONTWAIT, (struct sockaddr*)&tmpAddr, &tmpLen);
252
253 if (res > 0) {
254 // Learn the peer before delivering the frame, so a slave that
255 // replies from within sendFrame(), or a sender that waits for the
256 // frame to arrive, reaches the sender of this datagram.
257 if (memcmp(&remAddr_, &tmpAddr, sizeof(remAddr_)) != 0) {
258 std::lock_guard<std::mutex> lock(udpMtx_);
259 char tmpIp[INET_ADDRSTRLEN];
260 if (inet_ntop(AF_INET, &(tmpAddr.sin_addr), tmpIp, sizeof(tmpIp)) != NULL) {
261 udpLog_->debug("UDP server peer updated on local port %" PRIu16 " to %s:%" PRIu16,
262 port_,
263 tmpIp,
264 ntohs(tmpAddr.sin_port));
265 } else {
266 udpLog_->debug("UDP server peer updated on local port %" PRIu16, port_);
267 }
268 remAddr_ = tmpAddr;
269 }
270
271 // Message was too big
272 if (res > avail) {
273 udpLog_->warning("Receive data was too large on local port %" PRIu16 ". rx=%i, avail=%" PRIu32
274 ". Dropping.",
275 port_,
276 res,
277 avail);
278 } else {
279 buff->setPayload(res);
280 sendFrame(frame);
281 }
282
283 // Get new frame
284 frame = reqLocalFrame(maxPayload(), false);
285 } else {
286 // No datagram pending. Wait for one with a bounded timeout so the
287 // loop re-checks threadEn_ and stop() can join without traffic.
288 FD_ZERO(&fds);
289 FD_SET(fd_, &fds);
290
291 tout.tv_sec = 0;
292 tout.tv_usec = 100000;
293
294 select(fd_ + 1, &fds, NULL, NULL, &tout);
295 }
296 }
297}
298
299void rpu::Server::setup_python() {
300#ifndef NO_PYTHON
301
302 bp::class_<rpu::Server, rpu::ServerPtr, bp::bases<rpu::Core, ris::Master, ris::Slave>, boost::noncopyable>(
303 "Server",
304 bp::init<uint16_t, bool>())
305 .def("getPort", &rpu::Server::getPort);
306
307 bp::implicitly_convertible<rpu::ServerPtr, rpu::CorePtr>();
308 bp::implicitly_convertible<rpu::ServerPtr, ris::MasterPtr>();
309 bp::implicitly_convertible<rpu::ServerPtr, ris::SlavePtr>();
310#endif
311}
static GeneralError create(std::string src, const char *fmt,...)
Creates a formatted error instance.
RAII helper that releases the Python GIL for a scope.
Definition GilRelease.h:36
static std::shared_ptr< rogue::Logging > create(const std::string &name, bool quiet=false)
Creates a logger instance.
Definition Logging.cpp:95
void setFixedSize(uint32_t size)
Sets fixed-size mode.
Definition Pool.cpp:116
void setPoolSize(uint32_t size)
Sets buffer pool size.
Definition Pool.cpp:137
Shared UDP transport base for stream client/server endpoints.
Definition Core.h:64
std::atomic< bool > threadEn_
Definition Core.h:81
std::shared_ptr< rogue::Logging > udpLog_
Definition Core.h:66
struct sockaddr_in remAddr_
Definition Core.h:75
uint32_t maxPayload()
Returns maximum UDP payload size in bytes.
Definition Core.cpp:45
std::thread * thread_
Definition Core.h:80
std::shared_ptr< rogue::interfaces::stream::Buffer > BufferPtr
Shared pointer alias for Buffer.
Definition Buffer.h:270
std::shared_ptr< rogue::interfaces::stream::Frame > FramePtr
Shared pointer alias for Frame.
Definition Frame.h:549
std::shared_ptr< rogue::interfaces::stream::FrameLock > FrameLockPtr
Shared pointer alias for FrameLock.
Definition FrameLock.h:110
std::shared_ptr< rogue::protocols::udp::Server > ServerPtr
Definition Server.h:130