|
| 1 | +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
| 2 | +// vim: ts=8 sw=2 smarttab |
| 3 | +/* |
| 4 | + * Ceph - scalable distributed file system |
| 5 | + * |
| 6 | + * Copyright (C) 2024 Clyso GmbH |
| 7 | + * |
| 8 | + * This is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License version 2.1, as published by the Free Software |
| 11 | + * Foundation. See file COPYING. |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +#include "common/tcp_info.h" |
| 16 | + |
| 17 | +#include "common/Formatter.h" |
| 18 | + |
| 19 | +namespace ceph { |
| 20 | + |
| 21 | +#ifdef _WIN32 |
| 22 | +struct tcp_info {}; |
| 23 | + |
| 24 | +bool tcp_info(int fd, struct tcp_info& info) { |
| 25 | + return false; |
| 26 | +} |
| 27 | +bool dump_tcp_info(int fd, Formatter* f) { |
| 28 | + return false; |
| 29 | +} |
| 30 | + |
| 31 | +#else |
| 32 | + |
| 33 | +bool tcp_info(int fd, struct tcp_info& info) { |
| 34 | + socklen_t info_len = sizeof(info); |
| 35 | + return (getsockopt(fd, SOL_TCP, TCP_INFO, &info, &info_len) == 0); |
| 36 | +} |
| 37 | + |
| 38 | +static const char* get_tcpi_state_name(uint8_t state) { |
| 39 | + switch (state) { |
| 40 | + case TCP_ESTABLISHED: |
| 41 | + return "established"; |
| 42 | + case TCP_SYN_SENT: |
| 43 | + return "syn sent"; |
| 44 | + case TCP_SYN_RECV: |
| 45 | + return "syn recv"; |
| 46 | + case TCP_FIN_WAIT1: |
| 47 | + return "fin wait1"; |
| 48 | + case TCP_FIN_WAIT2: |
| 49 | + return "fin wait2"; |
| 50 | + case TCP_TIME_WAIT: |
| 51 | + return "time wait"; |
| 52 | + case TCP_CLOSE: |
| 53 | + return "close"; |
| 54 | + case TCP_CLOSE_WAIT: |
| 55 | + return "close wait"; |
| 56 | + case TCP_LAST_ACK: |
| 57 | + return "last ack"; |
| 58 | + case TCP_LISTEN: |
| 59 | + return "listen"; |
| 60 | + case TCP_CLOSING: |
| 61 | + return "closing"; |
| 62 | + default: |
| 63 | + return "UNKNOWN"; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +bool dump_tcp_info(int fd, Formatter* f) { |
| 68 | + struct tcp_info info; |
| 69 | + if (!tcp_info(fd, info)) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + f->open_object_section("tcp_info"); |
| 74 | + f->dump_string("tcpi_state", get_tcpi_state_name(info.tcpi_state)); |
| 75 | + f->dump_unsigned("tcpi_retransmits", info.tcpi_retransmits); |
| 76 | + f->dump_unsigned("tcpi_probes", info.tcpi_probes); |
| 77 | + f->dump_unsigned("tcpi_backoff", info.tcpi_backoff); |
| 78 | + f->dump_unsigned("tcpi_rto_us", info.tcpi_rto); |
| 79 | + f->dump_unsigned("tcpi_ato_us", info.tcpi_ato); |
| 80 | + f->dump_unsigned("tcpi_snd_mss", info.tcpi_snd_mss); |
| 81 | + f->dump_unsigned("tcpi_rcv_mss", info.tcpi_rcv_mss); |
| 82 | + f->dump_unsigned("tcpi_unacked", info.tcpi_unacked); |
| 83 | + f->dump_unsigned("tcpi_lost", info.tcpi_lost); |
| 84 | + f->dump_unsigned("tcpi_retrans", info.tcpi_retrans); |
| 85 | + f->dump_unsigned("tcpi_pmtu", info.tcpi_pmtu); |
| 86 | + f->dump_unsigned("tcpi_rtt_us", info.tcpi_rtt); |
| 87 | + f->dump_unsigned("tcpi_rttvar_us", info.tcpi_rttvar); |
| 88 | + f->dump_unsigned("tcpi_total_retrans", info.tcpi_total_retrans); |
| 89 | + f->dump_unsigned("tcpi_last_data_sent_ms", info.tcpi_last_data_sent); |
| 90 | + f->dump_unsigned("tcpi_last_ack_sent_ms", info.tcpi_last_ack_sent); |
| 91 | + f->dump_unsigned("tcpi_last_data_recv_ms", info.tcpi_last_data_recv); |
| 92 | + f->dump_unsigned("tcpi_last_ack_recv_ms", info.tcpi_last_ack_recv); |
| 93 | + |
| 94 | + f->open_array_section("tcpi_options"); |
| 95 | + if (info.tcpi_options & TCPI_OPT_TIMESTAMPS) { |
| 96 | + f->dump_string("option", "timestamps"); |
| 97 | + } |
| 98 | + if (info.tcpi_options & TCPI_OPT_SACK) { |
| 99 | + f->dump_string("option", "sack"); |
| 100 | + } |
| 101 | + if (info.tcpi_options & TCPI_OPT_WSCALE) { |
| 102 | + f->dump_string("option", "wscale"); |
| 103 | + } |
| 104 | + if (info.tcpi_options & TCPI_OPT_ECN) { |
| 105 | + f->dump_string("option", "ecn"); |
| 106 | + } |
| 107 | + if (info.tcpi_options & TCPI_OPT_ECN_SEEN) { |
| 108 | + f->dump_string("option", "ecn seen"); |
| 109 | + } |
| 110 | + if (info.tcpi_options & TCPI_OPT_SYN_DATA) { |
| 111 | + f->dump_string("option", "syn data"); |
| 112 | + } |
| 113 | + f->close_section(); |
| 114 | + |
| 115 | + f->close_section(); |
| 116 | + return true; |
| 117 | +} |
| 118 | + |
| 119 | +#endif |
| 120 | + |
| 121 | +} // namespace ceph |
0 commit comments