msg/async: return right away in NetHandler::set_priority() if not supported

* SO_PRIORITY is linux specific, so no need to check __linux__
* early return if priority is less than 0 (maybe we should also return if
  it's higher than 6?), the less indent.
* store errno if fails to set SO_PRIORITY before printing log messages.
* guard the whole function with '#ifdef SO_PRIORITY' so on platforms
  where this option is not supported, this function will be a no-op.

Signed-off-by: Kefu Chai <kchai@redhat.com>
This commit is contained in:
Kefu Chai 2017-04-26 10:47:29 +08:00
parent 024314e912
commit 6f1037e22c
2 changed files with 42 additions and 42 deletions

View File

@ -125,44 +125,45 @@ int NetHandler::set_socket_options(int sd, bool nodelay, int size)
void NetHandler::set_priority(int sd, int prio, int domain)
{
if (prio >= 0) {
int r = -1;
#ifdef IPTOS_CLASS_CS6
int iptos = IPTOS_CLASS_CS6;
r = ::setsockopt(sd, IPPROTO_IP, IP_TOS, &iptos, sizeof(iptos));
if (domain == AF_INET) {
r = ::setsockopt(sd, IPPROTO_IP, IP_TOS, &iptos, sizeof(iptos));
r = -errno;
if (r < 0) {
ldout(cct,0) << "couldn't set IP_TOS to " << iptos
<< ": " << cpp_strerror(r) << dendl;
}
} else if (domain == AF_INET6) {
r = ::setsockopt(sd, IPPROTO_IPV6, IPV6_TCLASS, &iptos, sizeof(iptos));
if (r)
r = -errno;
if (r < 0) {
ldout(cct,0) << "couldn't set IPV6_TCLASS to " << iptos
<< ": " << cpp_strerror(r) << dendl;
}
} else {
ldout(cct,0) << "couldn't set ToS of unknown family to " << iptos
<< dendl;
}
#endif
#if defined(SO_PRIORITY)
// setsockopt(IPTOS_CLASS_CS6) sets the priority of the socket as 0.
// See http://goo.gl/QWhvsD and http://goo.gl/laTbjT
// We need to call setsockopt(SO_PRIORITY) after it.
#if defined(__linux__)
r = ::setsockopt(sd, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio));
#endif
if (r < 0) {
ldout(cct, 0) << __func__ << " couldn't set SO_PRIORITY to " << prio
<< ": " << cpp_strerror(errno) << dendl;
}
#endif
#ifdef SO_PRIORITY
if (prio < 0) {
return;
}
#ifdef IPTOS_CLASS_CS6
int iptos = IPTOS_CLASS_CS6;
int r = -1;
if (domain == AF_INET) {
r = ::setsockopt(sd, IPPROTO_IP, IP_TOS, &iptos, sizeof(iptos));
if (r < 0) {
r = errno;
ldout(cct,0) << "couldn't set IP_TOS to " << iptos
<< ": " << cpp_strerror(r) << dendl;
}
} else if (domain == AF_INET6) {
r = ::setsockopt(sd, IPPROTO_IPV6, IPV6_TCLASS, &iptos, sizeof(iptos));
if (r < 0) {
r = errno;
ldout(cct,0) << "couldn't set IPV6_TCLASS to " << iptos
<< ": " << cpp_strerror(r) << dendl;
}
} else {
lderr(cct) << "couldn't set ToS of unknown family (" << domain << ")"
<< " to " << iptos << dendl;
return;
}
#endif // IPTOS_CLASS_CS6
// setsockopt(IPTOS_CLASS_CS6) sets the priority of the socket as 0.
// See http://goo.gl/QWhvsD and http://goo.gl/laTbjT
// We need to call setsockopt(SO_PRIORITY) after it.
r = ::setsockopt(sd, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio));
if (r < 0) {
r = errno;
ldout(cct, 0) << __func__ << " couldn't set SO_PRIORITY to " << prio
<< ": " << cpp_strerror(r) << dendl;
}
#else
return;
#endif // SO_PRIORITY
}
int NetHandler::generic_connect(const entity_addr_t& addr, const entity_addr_t &bind_addr, bool nonblock)

View File

@ -917,6 +917,7 @@ void Pipe::set_socket_options()
}
#endif
#ifdef SO_PRIORITY
int prio = msgr->get_socket_priority();
if (prio >= 0) {
int r = -1;
@ -938,17 +939,15 @@ void Pipe::set_socket_options()
<< ": " << cpp_strerror(r) << dendl;
}
} else {
ldout(msgr->cct,0) << "couldn't set ToS of unknown family to " << iptos
<< dendl;
lderr(msgr->cct) << "couldn't set ToS of unknown family ("
<< peer_addr.get_family() << ")"
<< " to " << iptos << dendl;
}
#endif
#if defined(SO_PRIORITY)
// setsockopt(IPTOS_CLASS_CS6) sets the priority of the socket as 0.
// See http://goo.gl/QWhvsD and http://goo.gl/laTbjT
// We need to call setsockopt(SO_PRIORITY) after it.
#if defined(__linux__)
r = ::setsockopt(sd, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio));
#endif
if (r < 0) {
r = -errno;
ldout(msgr->cct,0) << "couldn't set SO_PRIORITY to " << prio