From 8f54229a1eed38d3038d580d3e504b5f092a55b3 Mon Sep 17 00:00:00 2001 From: Dave Watson Date: Wed, 18 Feb 2015 08:12:06 -0800 Subject: [PATCH] Use folly's async udp socket Summary: They are functionaly equivalent, no need to have more than one Test Plan: fbconfig -r realtime/voip; fbmake runtests Reviewed By: naizhi@fb.com Subscribers: trunkagent, doug, ps, bmatheny, folly-diffs@, yfeldblum FB internal diff: D1828044 Tasks: 6154007 Signature: t1:1828044:1423165354:f71d2fd28ca76a8f67a597c747f8578d2909823c --- folly/io/async/AsyncUDPSocket.cpp | 22 ++++++++++++++++++++++ folly/io/async/AsyncUDPSocket.h | 9 +++++++++ 2 files changed, 31 insertions(+) diff --git a/folly/io/async/AsyncUDPSocket.cpp b/folly/io/async/AsyncUDPSocket.cpp index 979c2b75..0337501f 100644 --- a/folly/io/async/AsyncUDPSocket.cpp +++ b/folly/io/async/AsyncUDPSocket.cpp @@ -22,6 +22,12 @@ #include #include +// Due to the way kernel headers are included, this may or may not be defined. +// Number pulled from 3.10 kernel headers. +#ifndef SO_REUSEPORT +#define SO_REUSEPORT 15 +#endif + namespace folly { AsyncUDPSocket::AsyncUDPSocket(EventBase* evb) @@ -68,6 +74,22 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) { errno); } + if (reusePort_) { + // put the socket in port reuse mode + int value = 1; + if (setsockopt(socket, + SOL_SOCKET, + SO_REUSEPORT, + &value, + sizeof(value)) != 0) { + ::close(socket); + throw AsyncSocketException(AsyncSocketException::NOT_OPEN, + "failed to put socket in reuse_port mode", + errno); + + } + } + // bind to the address sockaddr_storage addrStorage; address.getAddress(&addrStorage); diff --git a/folly/io/async/AsyncUDPSocket.h b/folly/io/async/AsyncUDPSocket.h index 1c5d3f0a..39300105 100644 --- a/folly/io/async/AsyncUDPSocket.h +++ b/folly/io/async/AsyncUDPSocket.h @@ -136,6 +136,13 @@ class AsyncUDPSocket : public EventHandler { CHECK_NE(-1, fd_) << "Need to bind before getting FD out"; return fd_; } + + /** + * Set reuse port mode to call bind() on the same address multiple times + */ + void setReusePort(bool reusePort) { + reusePort_ = reusePort; + } private: AsyncUDPSocket(const AsyncUDPSocket&) = delete; AsyncUDPSocket& operator=(const AsyncUDPSocket&) = delete; @@ -157,6 +164,8 @@ class AsyncUDPSocket : public EventHandler { // Non-null only when we are reading ReadCallback* readCallback_; + + bool reusePort_{false}; }; } // Namespace -- 2.34.1