Skip to content

Commit 9f89bc1

Browse files
maskitzwoop
authored andcommitted
Open UDP ports on traffic_manager if ports are configured for QUIC (#6808)
(cherry picked from commit f1ef5ee)
1 parent bcfbd0c commit 9f89bc1

File tree

7 files changed

+90
-21
lines changed

7 files changed

+90
-21
lines changed

iocore/net/I_UDPNet.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class UDPNetProcessor : public Processor
6363
@param c Continuation that is called back with newly created
6464
socket.
6565
@param addr Address to bind (includes port)
66+
@param fd File descriptor to use (if exists)
6667
@param send_bufsize (optional) Socket buffer size for sending.
6768
Limits how much outstanding data to OS before it is able to send
6869
to the NIC.
@@ -71,7 +72,7 @@ class UDPNetProcessor : public Processor
7172
@return Action* Always returns ACTION_RESULT_DONE if socket was
7273
created successfully, or ACTION_IO_ERROR if not.
7374
*/
74-
inkcoreapi Action *UDPBind(Continuation *c, sockaddr const *addr, int send_bufsize = 0, int recv_bufsize = 0);
75+
inkcoreapi Action *UDPBind(Continuation *c, sockaddr const *addr, int fd = -1, int send_bufsize = 0, int recv_bufsize = 0);
7576

7677
// Regarding sendto_re, sendmsg_re, recvfrom_re:
7778
// * You may be called back on 'c' with completion or error status.

iocore/net/QUICNetProcessor.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ QUICNetProcessor::main_accept(Continuation *cont, SOCKET fd, AcceptOptions const
215215
na->init_accept();
216216

217217
SCOPED_MUTEX_LOCK(lock, na->mutex, this_ethread());
218-
udpNet.UDPBind((Continuation *)na, &na->server.accept_addr.sa, 1048576, 1048576);
218+
udpNet.UDPBind((Continuation *)na, &na->server.accept_addr.sa, fd, 1048576, 1048576);
219219

220220
return na->action_.get();
221221
}

iocore/net/UnixUDPNet.cc

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -735,20 +735,24 @@ UDPNetProcessor::CreateUDPSocket(int *resfd, sockaddr const *remote_addr, Action
735735
}
736736

737737
Action *
738-
UDPNetProcessor::UDPBind(Continuation *cont, sockaddr const *addr, int send_bufsize, int recv_bufsize)
738+
UDPNetProcessor::UDPBind(Continuation *cont, sockaddr const *addr, int fd, int send_bufsize, int recv_bufsize)
739739
{
740740
int res = 0;
741-
int fd = -1;
742741
UnixUDPConnection *n = nullptr;
743742
IpEndpoint myaddr;
744743
int myaddr_len = sizeof(myaddr);
745744
PollCont *pc = nullptr;
746745
PollDescriptor *pd = nullptr;
746+
bool need_bind = true;
747747

748-
if ((res = socketManager.socket(addr->sa_family, SOCK_DGRAM, 0)) < 0) {
749-
goto Lerror;
748+
if (fd == -1) {
749+
if ((res = socketManager.socket(addr->sa_family, SOCK_DGRAM, 0)) < 0) {
750+
goto Lerror;
751+
}
752+
fd = res;
753+
} else {
754+
need_bind = false;
750755
}
751-
fd = res;
752756
if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
753757
goto Lerror;
754758
}
@@ -798,11 +802,12 @@ UDPNetProcessor::UDPBind(Continuation *cont, sockaddr const *addr, int send_bufs
798802
}
799803
}
800804

801-
if (ats_is_ip6(addr) && safe_setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, SOCKOPT_ON, sizeof(int)) < 0) {
805+
if (need_bind && ats_is_ip6(addr) && safe_setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, SOCKOPT_ON, sizeof(int)) < 0) {
802806
goto Lerror;
803807
}
804808

805-
if (socketManager.ink_bind(fd, addr, ats_ip_size(addr)) < 0) {
809+
if (need_bind && (socketManager.ink_bind(fd, addr, ats_ip_size(addr)) < 0)) {
810+
Debug("udpnet", "ink_bind failed");
806811
goto Lerror;
807812
}
808813

iocore/net/test_I_UDPNet.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ EchoServer::start()
6161
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
6262
addr.sin_port = 0;
6363

64-
udpNet.UDPBind(static_cast<Continuation *>(this), reinterpret_cast<sockaddr const *>(&addr), 1048576, 1048576);
64+
udpNet.UDPBind(static_cast<Continuation *>(this), reinterpret_cast<sockaddr const *>(&addr), -1, 1048576, 1048576);
6565

6666
return true;
6767
}

mgmt/LocalManager.cc

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -941,29 +941,91 @@ LocalManager::listenForProxy()
941941
// We are not already bound, bind the port
942942
for (auto &p : lmgmt->m_proxy_ports) {
943943
if (ts::NO_FD == p.m_fd) {
944-
this->bindProxyPort(p);
944+
// Check the protocol (TCP or UDP) and create an appropriate socket
945+
if (p.isQUIC()) {
946+
this->bindUdpProxyPort(p);
947+
} else {
948+
this->bindTcpProxyPort(p);
949+
}
945950
}
946951

947-
// read backlog configuration value and overwrite the default value if found
948-
bool found;
949952
std::string_view fam{ats_ip_family_name(p.m_family)};
950-
RecInt backlog = REC_readInteger("proxy.config.net.listen_backlog", &found);
951-
backlog = (found && backlog >= 0) ? backlog : ats_tcp_somaxconn();
953+
if (p.isQUIC()) {
954+
// Can we do something like listen backlog for QUIC(UDP) ??
955+
// Do nothing for now
956+
} else {
957+
// read backlog configuration value and overwrite the default value if found
958+
bool found;
959+
RecInt backlog = REC_readInteger("proxy.config.net.listen_backlog", &found);
960+
backlog = (found && backlog >= 0) ? backlog : ats_tcp_somaxconn();
952961

953-
if ((listen(p.m_fd, backlog)) < 0) {
954-
mgmt_fatal(errno, "[LocalManager::listenForProxy] Unable to listen on port: %d (%.*s)\n", p.m_port, fam.size(), fam.data());
962+
if ((listen(p.m_fd, backlog)) < 0) {
963+
mgmt_fatal(errno, "[LocalManager::listenForProxy] Unable to listen on port: %d (%.*s)\n", p.m_port, fam.size(), fam.data());
964+
}
955965
}
966+
956967
mgmt_log("[LocalManager::listenForProxy] Listening on port: %d (%.*s)\n", p.m_port, fam.size(), fam.data());
957968
}
958969
return;
959970
}
960971

961972
/*
962-
* bindProxyPort()
973+
* bindUdpProxyPort()
974+
* Function binds the accept port of the proxy
975+
*/
976+
void
977+
LocalManager::bindUdpProxyPort(HttpProxyPort &port)
978+
{
979+
int one = 1;
980+
int priv = (port.m_port < 1024 && 0 != geteuid()) ? ElevateAccess::LOW_PORT_PRIVILEGE : 0;
981+
982+
ElevateAccess access(priv);
983+
984+
if ((port.m_fd = socket(port.m_family, SOCK_DGRAM, 0)) < 0) {
985+
mgmt_fatal(0, "[bindProxyPort] Unable to create socket : %s\n", strerror(errno));
986+
}
987+
988+
if (port.m_family == AF_INET6) {
989+
if (setsockopt(port.m_fd, IPPROTO_IPV6, IPV6_V6ONLY, SOCKOPT_ON, sizeof(int)) < 0) {
990+
mgmt_log("[bindProxyPort] Unable to set socket options: %d : %s\n", port.m_port, strerror(errno));
991+
}
992+
}
993+
if (setsockopt(port.m_fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char *>(&one), sizeof(int)) < 0) {
994+
mgmt_fatal(0, "[bindProxyPort] Unable to set socket options: %d : %s\n", port.m_port, strerror(errno));
995+
}
996+
997+
IpEndpoint ip;
998+
if (port.m_inbound_ip.isValid()) {
999+
ip.assign(port.m_inbound_ip);
1000+
} else if (AF_INET6 == port.m_family) {
1001+
if (m_inbound_ip6.isValid()) {
1002+
ip.assign(m_inbound_ip6);
1003+
} else {
1004+
ip.setToAnyAddr(AF_INET6);
1005+
}
1006+
} else if (AF_INET == port.m_family) {
1007+
if (m_inbound_ip4.isValid()) {
1008+
ip.assign(m_inbound_ip4);
1009+
} else {
1010+
ip.setToAnyAddr(AF_INET);
1011+
}
1012+
} else {
1013+
mgmt_fatal(0, "[bindProxyPort] Proxy port with invalid address type %d\n", port.m_family);
1014+
}
1015+
ip.port() = htons(port.m_port);
1016+
if (bind(port.m_fd, &ip.sa, ats_ip_size(&ip)) < 0) {
1017+
mgmt_fatal(0, "[bindProxyPort] Unable to bind socket: %d : %s\n", port.m_port, strerror(errno));
1018+
}
1019+
1020+
Debug("lm", "[bindProxyPort] Successfully bound proxy port %d", port.m_port);
1021+
}
1022+
1023+
/*
1024+
* bindTcpProxyPort()
9631025
* Function binds the accept port of the proxy
9641026
*/
9651027
void
966-
LocalManager::bindProxyPort(HttpProxyPort &port)
1028+
LocalManager::bindTcpProxyPort(HttpProxyPort &port)
9671029
{
9681030
int one = 1;
9691031
int priv = (port.m_port < 1024 && 0 != geteuid()) ? ElevateAccess::LOW_PORT_PRIVILEGE : 0;

mgmt/LocalManager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class LocalManager : public BaseManager
7676
void processEventQueue();
7777
bool startProxy(const char *onetime_options);
7878
void listenForProxy();
79-
void bindProxyPort(HttpProxyPort &);
79+
void bindUdpProxyPort(HttpProxyPort &);
80+
void bindTcpProxyPort(HttpProxyPort &);
8081
void closeProxyPorts();
8182

8283
void mgmtCleanup();

src/traffic_server/InkIOCoreAPI.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ INKUDPBind(TSCont contp, unsigned int ip, int port)
443443
ats_ip4_set(&addr, ip, htons(port));
444444

445445
return reinterpret_cast<TSAction>(
446-
udpNet.UDPBind((Continuation *)contp, ats_ip_sa_cast(&addr), INK_ETHERNET_MTU_SIZE, INK_ETHERNET_MTU_SIZE));
446+
udpNet.UDPBind((Continuation *)contp, ats_ip_sa_cast(&addr), -1, INK_ETHERNET_MTU_SIZE, INK_ETHERNET_MTU_SIZE));
447447
}
448448

449449
TSAction

0 commit comments

Comments
 (0)