|
| 1 | +/* |
| 2 | + * This file is part of PowerDNS or dnsdist. |
| 3 | + * Copyright -- PowerDNS.COM B.V. and its contributors |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of version 2 of the GNU General Public License as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * In addition, for the avoidance of any doubt, permission is granted to |
| 10 | + * link this program with OpenSSL and to (re)distribute the binaries |
| 11 | + * produced as the result of such linking. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License |
| 19 | + * along with this program; if not, write to the Free Software |
| 20 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | + */ |
| 22 | + |
| 23 | +#include "dnsdist-udp.hh" |
| 24 | +#include "dolog.hh" |
| 25 | +#include "dnsdist-configuration.hh" |
| 26 | + |
| 27 | +namespace dnsdist::udp |
| 28 | +{ |
| 29 | +static std::string contextToStr(Context context) |
| 30 | +{ |
| 31 | + if (context == Context::Frontend) { |
| 32 | + return "frontend"; |
| 33 | + } |
| 34 | + if (context == Context::Backend) { |
| 35 | + return "backend"; |
| 36 | + } |
| 37 | + |
| 38 | + return ""; |
| 39 | +} |
| 40 | + |
| 41 | +void setUDPSocketBufferSizes(int socketDesc, const Logr::Logger& logger, Context context, const ComboAddress& addr) |
| 42 | +{ |
| 43 | + const auto& immutableConfig = dnsdist::configuration::getImmutableConfiguration(); |
| 44 | + if (immutableConfig.d_socketUDPSendBuffer > 0) { |
| 45 | + try { |
| 46 | + setSocketSendBuffer(socketDesc, immutableConfig.d_socketUDPSendBuffer); |
| 47 | + } |
| 48 | + catch (const std::exception& e) { |
| 49 | + if (context == Context::Frontend) { |
| 50 | + SLOG(warnlog(e.what()), |
| 51 | + logger.error(Logr::Warning, e.what(), "Failed to raise send buffer size on UDP socket", "frontend.address", Logging::Loggable(addr))); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + else { |
| 56 | + try { |
| 57 | + auto result = raiseSocketSendBufferToMax(socketDesc); |
| 58 | + if (result > 0 && context == Context::Frontend) { |
| 59 | + SLOG(infolog("Raised send buffer to %u for %s address '%s'", result, contextToStr(context), addr.toStringWithPort()), |
| 60 | + logger.info(Logr::Info, "Raised send buffer size", "frontend.address", Logging::Loggable(addr), "network.send_buffer_size", Logging::Loggable(result))); |
| 61 | + } |
| 62 | + } |
| 63 | + catch (const std::exception& e) { |
| 64 | + if (context == Context::Frontend) { |
| 65 | + SLOG(warnlog(e.what()), |
| 66 | + logger.error(Logr::Warning, e.what(), "Failed to raise send buffer size on UDP socket", "frontend.address", Logging::Loggable(addr))); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (immutableConfig.d_socketUDPRecvBuffer > 0) { |
| 72 | + try { |
| 73 | + setSocketReceiveBuffer(socketDesc, immutableConfig.d_socketUDPRecvBuffer); |
| 74 | + } |
| 75 | + catch (const std::exception& e) { |
| 76 | + if (context == Context::Frontend) { |
| 77 | + SLOG(warnlog(e.what()), |
| 78 | + logger.error(Logr::Warning, e.what(), "Failed to raise receive buffer size on UDP socket", "frontend.address", Logging::Loggable(addr))); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + else { |
| 83 | + try { |
| 84 | + auto result = raiseSocketReceiveBufferToMax(socketDesc); |
| 85 | + if (result > 0 && context == Context::Frontend) { |
| 86 | + SLOG(infolog("Raised receive buffer to %u for address '%s'", result, addr.toStringWithPort()), |
| 87 | + logger.info(Logr::Info, "Raised receive buffer size", "frontend.address", Logging::Loggable(addr), "buffer_size", Logging::Loggable(result))); |
| 88 | + } |
| 89 | + } |
| 90 | + catch (const std::exception& e) { |
| 91 | + if (context == Context::Frontend) { |
| 92 | + SLOG(warnlog(e.what()), |
| 93 | + logger.error(Logr::Warning, e.what(), "Failed to raise receive buffer size on UDP socket", "frontend.address", Logging::Loggable(addr))); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +} // dnsdist::udp |
0 commit comments