[libc-commits] [libc] [llvm] [libc] Add TCP_INFO and struct tcp_info to <netinet/tcp.h> (PR #243858)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Wed Nov 18 05:57:07 PST 2026


https://github.com/labath updated https://github.com/llvm/llvm-project/pull/243858

>From b3adb33dd0571581fa44c89c216b3c9f5d0ffee8 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Wed, 18 Nov 2026 13:46:28 +0000
Subject: [PATCH] [libc] Add TCP_INFO and struct tcp_info to <netinet/tcp.h>

This patch adds the TCP_INFO socket option, the Linux definition of
struct tcp_info and the TCP_ESTABLISHED..TCP_CLOSING constants which
describe the values of its tcpi_state field.

The struct definition is based on the kernel's uapi/linux/tcp.h as of
Linux 7.3.5. The kernel only ever appends fields to the end of this
struct, and the getsockopt call reports how much of it was filled in,
so it's safe to use with older or newer kernels as well.

I've also added a layout test for the struct and a test which reads
the state of a freshly created TCP socket.

Assisted-by: Claude
---
 libc/hdr/types/CMakeLists.txt                 |   8 ++
 libc/hdr/types/struct_tcp_info.h              |  27 ++++
 libc/include/CMakeLists.txt                   |   1 +
 .../linux/netinet-tcp-macros.h                |  14 +++
 libc/include/llvm-libc-types/CMakeLists.txt   |   5 +
 .../llvm-libc-types/linux/CMakeLists.txt      |   8 ++
 .../llvm-libc-types/linux/struct_tcp_info.h   | 116 ++++++++++++++++++
 .../include/llvm-libc-types/struct_tcp_info.h |  21 ++++
 libc/include/netinet/tcp.yaml                 |   3 +-
 libc/test/src/netinet/linux/CMakeLists.txt    |  10 ++
 libc/test/src/netinet/linux/tcp_test.cpp      |  43 +++++++
 libc/test/src/sys/socket/linux/CMakeLists.txt |   1 +
 .../src/sys/socket/linux/socketopt_test.cpp   |  19 +++
 .../libc/hdr/types/BUILD.bazel                |   5 +
 .../libc/test/src/sys/socket/BUILD.bazel      |   1 +
 15 files changed, 281 insertions(+), 1 deletion(-)
 create mode 100644 libc/hdr/types/struct_tcp_info.h
 create mode 100644 libc/include/llvm-libc-types/linux/struct_tcp_info.h
 create mode 100644 libc/include/llvm-libc-types/struct_tcp_info.h
 create mode 100644 libc/test/src/netinet/linux/tcp_test.cpp

diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index e920b0b138ea85..32e8aa331d6f29 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -754,6 +754,14 @@ add_proxy_header_library(
     libc.include.llvm-libc-types.struct_iphdr
 )
 
+add_proxy_header_library(
+  struct_tcp_info
+  HDRS
+    struct_tcp_info.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.struct_tcp_info
+)
+
 add_proxy_header_library(
   struct_udphdr
   HDRS
diff --git a/libc/hdr/types/struct_tcp_info.h b/libc/hdr/types/struct_tcp_info.h
new file mode 100644
index 00000000000000..a1c2cf4066f5f7
--- /dev/null
+++ b/libc/hdr/types/struct_tcp_info.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Proxy header for struct tcp_info.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_TCP_INFO_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_TCP_INFO_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_tcp_info.h"
+
+#else
+
+#include <netinet/tcp.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_TCP_INFO_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 001893f983701b..1eb3dca6ba48e1 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -1268,6 +1268,7 @@ add_header_macro(
   netinet/tcp.h
   DEPENDS
     .llvm-libc-macros.netinet_tcp_macros
+    .llvm-libc-types.struct_tcp_info
     .llvm_libc_common_h
 )
 
diff --git a/libc/include/llvm-libc-macros/linux/netinet-tcp-macros.h b/libc/include/llvm-libc-macros/linux/netinet-tcp-macros.h
index 3da802947f7e8a..f26f5be580b0c5 100644
--- a/libc/include/llvm-libc-macros/linux/netinet-tcp-macros.h
+++ b/libc/include/llvm-libc-macros/linux/netinet-tcp-macros.h
@@ -28,6 +28,7 @@
 #define TCP_LINGER2 8
 #define TCP_DEFER_ACCEPT 9
 #define TCP_WINDOW_CLAMP 10
+#define TCP_INFO 11 // struct tcp_info
 #define TCP_QUICKACK 12
 #define TCP_CONGESTION 13 // char[]
 #define TCP_THIN_LINEAR_TIMEOUTS 16
@@ -64,4 +65,17 @@
 #define TCP_SEND_QUEUE 2
 #define TCP_QUEUES_NR 3 // Number of queues, not a valid value.
 
+// TCP connection states, as reported in tcp_info::tcpi_state.
+#define TCP_ESTABLISHED 1
+#define TCP_SYN_SENT 2
+#define TCP_SYN_RECV 3
+#define TCP_FIN_WAIT1 4
+#define TCP_FIN_WAIT2 5
+#define TCP_TIME_WAIT 6
+#define TCP_CLOSE 7
+#define TCP_CLOSE_WAIT 8
+#define TCP_LAST_ACK 9
+#define TCP_LISTEN 10
+#define TCP_CLOSING 11
+
 #endif // LLVM_LIBC_MACROS_LINUX_NETINET_TCP_MACROS_H
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 545bd26cdb7189..ff3b23a987d103 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -326,6 +326,11 @@ add_header(struct_cmsghdr HDR struct_cmsghdr.h DEPENDS .size_t)
 add_header(struct_msghdr HDR struct_msghdr.h DEPENDS .size_t .socklen_t .struct_iovec)
 add_header(struct_mmsghdr HDR struct_mmsghdr.h DEPENDS .struct_msghdr)
 add_header(struct_iphdr HDR struct_iphdr.h DEPENDS libc.include.llvm-libc-macros.stdint_macros)
+add_type_header(
+  struct_tcp_info
+  HDR
+    struct_tcp_info.h
+)
 add_header(struct_udphdr HDR struct_udphdr.h DEPENDS libc.include.llvm-libc-macros.stdint_macros)
 add_header(struct_ether_addr HDR struct_ether_addr.h DEPENDS libc.include.llvm-libc-macros.stdint_macros)
 add_header(ACTION HDR ACTION.h)
diff --git a/libc/include/llvm-libc-types/linux/CMakeLists.txt b/libc/include/llvm-libc-types/linux/CMakeLists.txt
index 3796f597596d3a..0dbf4f575dc7fc 100644
--- a/libc/include/llvm-libc-types/linux/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/linux/CMakeLists.txt
@@ -10,6 +10,14 @@ add_header(
     struct_sysinfo.h
 )
 
+add_header(
+  struct_tcp_info
+  HDR
+    struct_tcp_info.h
+  DEPENDS
+    libc.include.llvm-libc-macros.stdint_macros
+)
+
 add_header(
   struct_ifmap
   HDR
diff --git a/libc/include/llvm-libc-types/linux/struct_tcp_info.h b/libc/include/llvm-libc-types/linux/struct_tcp_info.h
new file mode 100644
index 00000000000000..cbd4c6c6220fcc
--- /dev/null
+++ b/libc/include/llvm-libc-types/linux/struct_tcp_info.h
@@ -0,0 +1,116 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Definition of struct tcp_info for Linux.
+///
+/// The kernel extends this struct by appending new fields to the end. When
+/// reading it with getsockopt(TCP_INFO), the returned option length indicates
+/// which fields were filled in by the kernel.
+///
+/// This definition is based on the kernel's include/uapi/linux/tcp.h as of
+/// Linux 7.3.5.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TYPES_LINUX_STRUCT_TCP_INFO_H
+#define LLVM_LIBC_TYPES_LINUX_STRUCT_TCP_INFO_H
+
+#include "../../llvm-libc-macros/stdint-macros.h"
+
+struct tcp_info {
+  uint8_t tcpi_state; // TCP_ESTABLISHED, etc.
+  uint8_t tcpi_ca_state;
+  uint8_t tcpi_retransmits;
+  uint8_t tcpi_probes;
+  uint8_t tcpi_backoff;
+  uint8_t tcpi_options;
+  uint8_t tcpi_snd_wscale : 4, tcpi_rcv_wscale : 4;
+  uint8_t tcpi_delivery_rate_app_limited : 1, tcpi_fastopen_client_fail : 2;
+
+  uint32_t tcpi_rto;
+  uint32_t tcpi_ato;
+  uint32_t tcpi_snd_mss;
+  uint32_t tcpi_rcv_mss;
+
+  uint32_t tcpi_unacked;
+  uint32_t tcpi_sacked;
+  uint32_t tcpi_lost;
+  uint32_t tcpi_retrans;
+  uint32_t tcpi_fackets;
+
+  // Times.
+  uint32_t tcpi_last_data_sent;
+  uint32_t tcpi_last_ack_sent;
+  uint32_t tcpi_last_data_recv;
+  uint32_t tcpi_last_ack_recv;
+
+  // Metrics.
+  uint32_t tcpi_pmtu;
+  uint32_t tcpi_rcv_ssthresh;
+  uint32_t tcpi_rtt;
+  uint32_t tcpi_rttvar;
+  uint32_t tcpi_snd_ssthresh;
+  uint32_t tcpi_snd_cwnd;
+  uint32_t tcpi_advmss;
+  uint32_t tcpi_reordering;
+
+  uint32_t tcpi_rcv_rtt;
+  uint32_t tcpi_rcv_space;
+
+  uint32_t tcpi_total_retrans;
+
+  uint64_t tcpi_pacing_rate;
+  uint64_t tcpi_max_pacing_rate;
+  uint64_t tcpi_bytes_acked;
+  uint64_t tcpi_bytes_received;
+  uint32_t tcpi_segs_out;
+  uint32_t tcpi_segs_in;
+
+  uint32_t tcpi_notsent_bytes;
+  uint32_t tcpi_min_rtt;
+  uint32_t tcpi_data_segs_in;
+  uint32_t tcpi_data_segs_out;
+
+  uint64_t tcpi_delivery_rate;
+
+  uint64_t tcpi_busy_time;
+  uint64_t tcpi_rwnd_limited;
+  uint64_t tcpi_sndbuf_limited;
+
+  uint32_t tcpi_delivered;
+  uint32_t tcpi_delivered_ce;
+
+  uint64_t tcpi_bytes_sent;
+  uint64_t tcpi_bytes_retrans;
+  uint32_t tcpi_dsack_dups;
+  uint32_t tcpi_reord_seen;
+
+  uint32_t tcpi_rcv_ooopack;
+
+  uint32_t tcpi_snd_wnd;
+  uint32_t tcpi_rcv_wnd;
+
+  uint32_t tcpi_rehash;
+
+  uint16_t tcpi_total_rto;
+  uint16_t tcpi_total_rto_recoveries;
+  uint32_t tcpi_total_rto_time;
+
+  uint32_t tcpi_received_ce;
+  uint32_t tcpi_delivered_e1_bytes;
+  uint32_t tcpi_delivered_e0_bytes;
+  uint32_t tcpi_delivered_ce_bytes;
+  uint32_t tcpi_received_e1_bytes;
+  uint32_t tcpi_received_e0_bytes;
+  uint32_t tcpi_received_ce_bytes;
+  uint32_t tcpi_ecn_mode : 2, tcpi_accecn_opt_seen : 2,
+      tcpi_accecn_fail_mode : 4, tcpi_options2 : 24;
+};
+
+#endif // LLVM_LIBC_TYPES_LINUX_STRUCT_TCP_INFO_H
diff --git a/libc/include/llvm-libc-types/struct_tcp_info.h b/libc/include/llvm-libc-types/struct_tcp_info.h
new file mode 100644
index 00000000000000..ec9bae20a7dc73
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_tcp_info.h
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Definition of struct tcp_info.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TYPES_STRUCT_TCP_INFO_H
+#define LLVM_LIBC_TYPES_STRUCT_TCP_INFO_H
+
+#if defined(__linux__)
+#include "linux/struct_tcp_info.h"
+#endif
+
+#endif // LLVM_LIBC_TYPES_STRUCT_TCP_INFO_H
diff --git a/libc/include/netinet/tcp.yaml b/libc/include/netinet/tcp.yaml
index 8b31fd48a3b675..e31c92b1612e89 100644
--- a/libc/include/netinet/tcp.yaml
+++ b/libc/include/netinet/tcp.yaml
@@ -4,7 +4,8 @@ standards:
 macros:
   - macro_name: TCP_NODELAY
     macro_header: netinet-tcp-macros.h
-types: []
+types:
+  - type_name: struct_tcp_info
 enums: []
 objects: []
 functions: []
diff --git a/libc/test/src/netinet/linux/CMakeLists.txt b/libc/test/src/netinet/linux/CMakeLists.txt
index 4316564b9a7f1e..574b2523c9d863 100644
--- a/libc/test/src/netinet/linux/CMakeLists.txt
+++ b/libc/test/src/netinet/linux/CMakeLists.txt
@@ -9,3 +9,13 @@ add_libc_test(
     libc.src.__support.net.byte_order
     libc.src.string.memcmp
 )
+
+add_libc_test(
+  tcp_test
+  SUITE
+    libc-netinet-tests
+  SRCS
+    tcp_test.cpp
+  DEPENDS
+    libc.hdr.types.struct_tcp_info
+)
diff --git a/libc/test/src/netinet/linux/tcp_test.cpp b/libc/test/src/netinet/linux/tcp_test.cpp
new file mode 100644
index 00000000000000..a766f7cbd3deb4
--- /dev/null
+++ b/libc/test/src/netinet/linux/tcp_test.cpp
@@ -0,0 +1,43 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for netinet/tcp.h.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/types/struct_tcp_info.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcNetinetTcpTest, StructTcpInfoLayout) {
+  // These fields have been around forever, so they should be present in any
+  // version of the struct.
+  EXPECT_EQ(offsetof(struct tcp_info, tcpi_state), static_cast<size_t>(0));
+  EXPECT_EQ(offsetof(struct tcp_info, tcpi_options), static_cast<size_t>(5));
+  EXPECT_EQ(offsetof(struct tcp_info, tcpi_rto), static_cast<size_t>(8));
+  EXPECT_EQ(offsetof(struct tcp_info, tcpi_total_retrans),
+            static_cast<size_t>(100));
+
+#ifdef LIBC_FULL_BUILD
+  // In overlay mode, the system headers may contain an older version of the
+  // struct, so only check our own definition.
+  EXPECT_EQ(offsetof(struct tcp_info, tcpi_pacing_rate),
+            static_cast<size_t>(104));
+  EXPECT_EQ(offsetof(struct tcp_info, tcpi_delivery_rate),
+            static_cast<size_t>(160));
+  EXPECT_EQ(offsetof(struct tcp_info, tcpi_bytes_sent),
+            static_cast<size_t>(200));
+  EXPECT_EQ(offsetof(struct tcp_info, tcpi_total_rto),
+            static_cast<size_t>(240));
+  EXPECT_EQ(offsetof(struct tcp_info, tcpi_received_ce),
+            static_cast<size_t>(248));
+  EXPECT_EQ(offsetof(struct tcp_info, tcpi_received_ce_bytes),
+            static_cast<size_t>(272));
+  EXPECT_EQ(sizeof(struct tcp_info), static_cast<size_t>(280));
+#endif
+}
diff --git a/libc/test/src/sys/socket/linux/CMakeLists.txt b/libc/test/src/sys/socket/linux/CMakeLists.txt
index ca49e10b5f6887..fe069cccffd2d7 100644
--- a/libc/test/src/sys/socket/linux/CMakeLists.txt
+++ b/libc/test/src/sys/socket/linux/CMakeLists.txt
@@ -123,6 +123,7 @@ add_libc_test(
     libc.hdr.sys_socket_macros
     libc.hdr.types.socklen_t
     libc.hdr.types.struct_linger
+    libc.hdr.types.struct_tcp_info
     libc.hdr.time_macros
     libc.hdr.types.struct_timespec
     libc.hdr.types.struct_timeval
diff --git a/libc/test/src/sys/socket/linux/socketopt_test.cpp b/libc/test/src/sys/socket/linux/socketopt_test.cpp
index b7dbb0f5d1b78b..5c9c5078b83f42 100644
--- a/libc/test/src/sys/socket/linux/socketopt_test.cpp
+++ b/libc/test/src/sys/socket/linux/socketopt_test.cpp
@@ -11,6 +11,7 @@
 #include "hdr/sys_socket_macros.h"
 #include "hdr/time_macros.h"
 #include "hdr/types/struct_linger.h"
+#include "hdr/types/struct_tcp_info.h"
 #include "hdr/types/struct_timespec.h"
 #include "hdr/types/struct_timeval.h"
 #include "src/__support/time/clock_gettime.h"
@@ -121,6 +122,24 @@ TEST_F(LlvmLibcSocketOptTest, TcpSocketOpt) {
   ASSERT_EQ(optlen, INT_OPTLEN);
 }
 
+TEST_F(LlvmLibcSocketOptTest, TcpInfo) {
+  int sock = LIBC_NAMESPACE::socket(AF_INET, SOCK_STREAM, 0);
+  ASSERT_GE(sock, 0);
+  scope_exit close_sock(
+      [&] { ASSERT_THAT(LIBC_NAMESPACE::close(sock), Succeeds(0)); });
+
+  struct tcp_info info = {};
+  socklen_t optlen = sizeof(info);
+  ASSERT_THAT(
+      LIBC_NAMESPACE::getsockopt(sock, SOL_TCP, TCP_INFO, &info, &optlen),
+      Succeeds(0));
+  // The kernel's version of the struct can be smaller or larger than ours,
+  // but it will always contain the tcpi_state field.
+  ASSERT_GT(optlen, static_cast<socklen_t>(0));
+  ASSERT_LE(optlen, static_cast<socklen_t>(sizeof(info)));
+  EXPECT_EQ(static_cast<int>(info.tcpi_state), static_cast<int>(TCP_CLOSE));
+}
+
 TEST_F(LlvmLibcSocketOptTest, NotASocket) {
   int fds[2];
   ASSERT_THAT(LIBC_NAMESPACE::pipe(fds), Succeeds(0));
diff --git a/utils/bazel/llvm-project-overlay/libc/hdr/types/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/hdr/types/BUILD.bazel
index eeb8b13509fbbd..641aaffa7a8a07 100644
--- a/utils/bazel/llvm-project-overlay/libc/hdr/types/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/hdr/types/BUILD.bazel
@@ -285,6 +285,11 @@ libc_support_library(
     hdrs = ["struct_sysinfo.h"],
 )
 
+libc_support_library(
+    name = "struct_tcp_info",
+    hdrs = ["struct_tcp_info.h"],
+)
+
 libc_support_library(
     name = "struct_utsname",
     hdrs = ["struct_utsname.h"],
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/sys/socket/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/sys/socket/BUILD.bazel
index b70b02eb300131..3ae739b4e96a0d 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/sys/socket/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/sys/socket/BUILD.bazel
@@ -204,6 +204,7 @@ libc_test(
         "//libc/hdr:time_macros",
         "//libc/hdr/types:socklen_t",
         "//libc/hdr/types:struct_linger",
+        "//libc/hdr/types:struct_tcp_info",
         "//libc/hdr/types:struct_timespec",
         "//libc/hdr/types:struct_timeval",
         "//libc/src/__support/CPP:scope",



More information about the libc-commits mailing list