From 6f1037e22c2a304795895498cdc955e0ef80f8e3 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 26 Apr 2017 10:47:29 +0800 Subject: [PATCH] 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 --- src/msg/async/net_handler.cc | 65 ++++++++++++++++++------------------ src/msg/simple/Pipe.cc | 9 +++-- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/msg/async/net_handler.cc b/src/msg/async/net_handler.cc index 059f9220948aa..d43e6a45a01bf 100644 --- a/src/msg/async/net_handler.cc +++ b/src/msg/async/net_handler.cc @@ -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 SO_PRIORITY + if (prio < 0) { + return; + } #ifdef IPTOS_CLASS_CS6 - int iptos = 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 (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; + if (r < 0) { + r = errno; + ldout(cct,0) << "couldn't set IP_TOS to " << iptos + << ": " << cpp_strerror(r) << 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 + } else if (domain == AF_INET6) { + r = ::setsockopt(sd, IPPROTO_IPV6, IPV6_TCLASS, &iptos, sizeof(iptos)); if (r < 0) { - ldout(cct, 0) << __func__ << " couldn't set SO_PRIORITY to " << prio - << ": " << cpp_strerror(errno) << dendl; + r = errno; + ldout(cct,0) << "couldn't set IPV6_TCLASS to " << iptos + << ": " << cpp_strerror(r) << dendl; } -#endif + } 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) diff --git a/src/msg/simple/Pipe.cc b/src/msg/simple/Pipe.cc index fa5df0a184fbc..b1ec5e533ad76 100644 --- a/src/msg/simple/Pipe.cc +++ b/src/msg/simple/Pipe.cc @@ -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 -- 2.39.5