From cda71d1b25ea84f9b146a0eac0c8fb793c6e1bae Mon Sep 17 00:00:00 2001 From: Sam Kumar Date: Thu, 7 Apr 2022 10:42:17 -0700 Subject: [PATCH] [tcp] remove extraneous #define and restrict inclusion of TCPlp headers (#7570) * remove extraneous #define * restrict inclusion of `third_party/tcplp/tcplp.h` --- src/core/net/tcp6.cpp | 59 ++++++++++++++++++++++++++-- src/core/net/tcp6.hpp | 47 ++++++++++------------ third_party/tcplp/bsdtcp/tcp.h | 2 - third_party/tcplp/bsdtcp/tcp_input.c | 6 +-- third_party/tcplp/bsdtcp/tcp_reass.c | 2 +- third_party/tcplp/bsdtcp/tcp_var.h | 6 +-- third_party/tcplp/tcplp.h | 2 +- 7 files changed, 84 insertions(+), 40 deletions(-) diff --git a/src/core/net/tcp6.cpp b/src/core/net/tcp6.cpp index c033bd094..6a6d5b4e8 100644 --- a/src/core/net/tcp6.cpp +++ b/src/core/net/tcp6.cpp @@ -57,6 +57,17 @@ using ot::Encoding::BigEndian::HostSwap32; RegisterLogModule("Tcp"); +static_assert(sizeof(struct tcpcb) == sizeof(Tcp::Endpoint::mTcb), "mTcb field in otTcpEndpoint is sized incorrectly"); +static_assert(alignof(struct tcpcb) == alignof(decltype(Tcp::Endpoint::mTcb)), + "mTcb field in otTcpEndpoint is aligned incorrectly"); +static_assert(offsetof(Tcp::Endpoint, mTcb) == 0, "mTcb field in otTcpEndpoint has nonzero offset"); + +static_assert(sizeof(struct tcpcb_listen) == sizeof(Tcp::Listener::mTcbListen), + "mTcbListen field in otTcpListener is sized incorrectly"); +static_assert(alignof(struct tcpcb_listen) == alignof(decltype(Tcp::Listener::mTcbListen)), + "mTcbListen field in otTcpListener is aligned incorrectly"); +static_assert(offsetof(Tcp::Listener, mTcbListen) == 0, "mTcbListen field in otTcpEndpoint has nonzero offset"); + Tcp::Tcp(Instance &aInstance) : InstanceLocator(aInstance) , mTimer(aInstance, Tcp::HandleTimer) @@ -259,6 +270,11 @@ exit: return error; } +bool Tcp::Endpoint::IsClosed(void) const +{ + return GetTcb().t_state == TCP6S_CLOSED; +} + uint8_t Tcp::Endpoint::TimerFlagToIndex(uint8_t aTimerFlag) { uint8_t timerIndex = 0; @@ -412,6 +428,26 @@ exit: return calledUserCallback; } +Address &Tcp::Endpoint::GetLocalIp6Address(void) +{ + return *reinterpret_cast
(&GetTcb().laddr); +} + +const Address &Tcp::Endpoint::GetLocalIp6Address(void) const +{ + return *reinterpret_cast(&GetTcb().laddr); +} + +Address &Tcp::Endpoint::GetForeignIp6Address(void) +{ + return *reinterpret_cast
(&GetTcb().faddr); +} + +const Address &Tcp::Endpoint::GetForeignIp6Address(void) const +{ + return *reinterpret_cast(&GetTcb().faddr); +} + bool Tcp::Endpoint::Matches(const MessageInfo &aMessageInfo) const { bool matches = false; @@ -494,6 +530,21 @@ exit: return error; } +bool Tcp::Listener::IsClosed(void) const +{ + return GetTcbListen().t_state == TCP6S_CLOSED; +} + +Address &Tcp::Listener::GetLocalIp6Address(void) +{ + return *reinterpret_cast
(&GetTcbListen().laddr); +} + +const Address &Tcp::Listener::GetLocalIp6Address(void) const +{ + return *reinterpret_cast(&GetTcbListen().laddr); +} + bool Tcp::Listener::Matches(const MessageInfo &aMessageInfo) const { bool matches = false; @@ -551,9 +602,9 @@ Error Tcp::HandleMessage(ot::Ip6::Header &aIp6Header, Message &aMessage, Message endpoint = mEndpoints.FindMatching(aMessageInfo, endpointPrev); if (endpoint != nullptr) { - struct signals sig; - int nextAction; - struct tcpcb * tp = &endpoint->GetTcb(); + struct tcplp_signals sig; + int nextAction; + struct tcpcb * tp = &endpoint->GetTcb(); otLinkedBuffer *priorHead = lbuf_head(&tp->sendbuf); @@ -583,7 +634,7 @@ exit: return error; } -void Tcp::ProcessSignals(Endpoint &aEndpoint, otLinkedBuffer *aPriorHead, struct signals &aSignals) +void Tcp::ProcessSignals(Endpoint &aEndpoint, otLinkedBuffer *aPriorHead, struct tcplp_signals &aSignals) { VerifyOrExit(IsInitialized(aEndpoint) && !aEndpoint.IsClosed()); if (aEndpoint.mSendDoneCallback != nullptr) diff --git a/src/core/net/tcp6.hpp b/src/core/net/tcp6.hpp index 37f8104d8..d446cfdd7 100644 --- a/src/core/net/tcp6.hpp +++ b/src/core/net/tcp6.hpp @@ -47,7 +47,17 @@ #include "net/ip6_headers.hpp" #include "net/socket.hpp" -#include "../../third_party/tcplp/tcplp.h" +/* + * These structures and functions are forward-declared here to avoid + * #includ'ing third_party/tcplp/tcplp.h in this header file. + */ +extern "C" { +struct tcpcb; +struct tcpcb_listen; +struct tcplp_signals; +void tcplp_sys_set_timer(struct tcpcb *aTcb, uint8_t aTimerFlag, uint32_t aDelay); +void tcplp_sys_stop_timer(struct tcpcb *aTcb, uint8_t aTimerFlag); +} namespace ot { namespace Ip6 { @@ -352,12 +362,11 @@ public: /** * Checks if this Endpoint is in the closed state. */ - bool IsClosed(void) const { return GetTcb().t_state == TCP6S_CLOSED; } + bool IsClosed(void) const; private: friend void ::tcplp_sys_set_timer(struct tcpcb *aTcb, uint8_t aTimerFlag, uint32_t aDelay); friend void ::tcplp_sys_stop_timer(struct tcpcb *aTcb, uint8_t aTimerFlag); - friend void ::tcplp_sys_connection_lost(struct tcpcb *aTcb, uint8_t aErrNum); enum : uint8_t { @@ -375,18 +384,13 @@ public: void CancelTimer(uint8_t aTimerFlag); bool FirePendingTimers(TimeMilli aNow, bool &aHasFutureTimer, TimeMilli &aEarliestFutureExpiry); - Address & GetLocalIp6Address(void) { return *reinterpret_cast
(&GetTcb().laddr); } - const Address &GetLocalIp6Address(void) const { return *reinterpret_cast(&GetTcb().laddr); } - Address & GetForeignIp6Address(void) { return *reinterpret_cast
(&GetTcb().faddr); } - const Address &GetForeignIp6Address(void) const { return *reinterpret_cast(&GetTcb().faddr); } + Address & GetLocalIp6Address(void); + const Address &GetLocalIp6Address(void) const; + Address & GetForeignIp6Address(void); + const Address &GetForeignIp6Address(void) const; bool Matches(const MessageInfo &aMessageInfo) const; }; - static_assert(sizeof(struct tcpcb) == sizeof(Endpoint::mTcb), "mTcb field in otTcpEndpoint is sized incorrectly"); - static_assert(alignof(struct tcpcb) == alignof(decltype(Endpoint::mTcb)), - "mTcb field in otTcpEndpoint is aligned incorrectly"); - static_assert(offsetof(Endpoint, mTcb) == 0, "mTcb field in otTcpEndpoint has nonzero offset"); - /** * This class represents a TCP/IPv6 listener. * @@ -506,23 +510,14 @@ public: /** * Checks if this Listener is in the closed state. */ - bool IsClosed(void) const { return GetTcbListen().t_state == TCP6S_CLOSED; } + bool IsClosed(void) const; private: - Address & GetLocalIp6Address(void) { return *reinterpret_cast
(&GetTcbListen().laddr); } - const Address &GetLocalIp6Address(void) const - { - return *reinterpret_cast(&GetTcbListen().laddr); - } - bool Matches(const MessageInfo &aMessageInfo) const; + Address & GetLocalIp6Address(void); + const Address &GetLocalIp6Address(void) const; + bool Matches(const MessageInfo &aMessageInfo) const; }; - static_assert(sizeof(struct tcpcb_listen) == sizeof(Listener::mTcbListen), - "mTcbListen field in otTcpListener is sized incorrectly"); - static_assert(alignof(struct tcpcb_listen) == alignof(decltype(Listener::mTcbListen)), - "mTcbListen field in otTcpListener is aligned incorrectly"); - static_assert(offsetof(Listener, mTcbListen) == 0, "mTcbListen field in otTcpEndpoint has nonzero offset"); - /** * This class implements TCP header parsing. * @@ -661,7 +656,7 @@ private: kDynamicPortMax = 65535, ///< Service Name and Transport Protocol Port Number Registry }; - void ProcessSignals(Endpoint &aEndpoint, otLinkedBuffer *aPriorHead, struct signals &aSignals); + void ProcessSignals(Endpoint &aEndpoint, otLinkedBuffer *aPriorHead, struct tcplp_signals &aSignals); static Error BsdErrorToOtError(int aBsdError); bool CanBind(const SockAddr &aSockName); diff --git a/third_party/tcplp/bsdtcp/tcp.h b/third_party/tcplp/bsdtcp/tcp.h index 472d842e4..1faf353d4 100644 --- a/third_party/tcplp/bsdtcp/tcp.h +++ b/third_party/tcplp/bsdtcp/tcp.h @@ -43,8 +43,6 @@ #include #include -#define __func__ "BSD TCP function" - #define KASSERT(COND, MSG) if (!(COND)) tcplp_sys_panic MSG typedef uint32_t tcp_seq; diff --git a/third_party/tcplp/bsdtcp/tcp_input.c b/third_party/tcplp/bsdtcp/tcp_input.c index 0071e3bcc..67147c8a8 100644 --- a/third_party/tcplp/bsdtcp/tcp_input.c +++ b/third_party/tcplp/bsdtcp/tcp_input.c @@ -103,7 +103,7 @@ static void tcp_dooptions(struct tcpopt *, uint8_t *, int, int); static void tcp_do_segment(struct ip6_hdr* ip6, struct tcphdr *th, otMessage* msg, struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, - struct signals* sig); + struct tcplp_signals* sig); static void tcp_xmit_timer(struct tcpcb *, int); void tcp_hc_get(/*struct in_conninfo *inc*/ struct tcpcb* tp, struct hc_metrics_lite *hc_metrics_lite); static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); @@ -431,7 +431,7 @@ tcp_dropwithreset(struct ip6_hdr* ip6, struct tcphdr *th, struct tcpcb *tp, otIn /* NOTE: tcp_fields_to_host(th) must be called before this function is called. */ int tcp_input(struct ip6_hdr* ip6, struct tcphdr* th, otMessage* msg, struct tcpcb* tp, struct tcpcb_listen* tpl, - struct signals* sig) + struct tcplp_signals* sig) { /* * samkumar: I significantly modified this function, compared to the @@ -948,7 +948,7 @@ drop: static void tcp_do_segment(struct ip6_hdr* ip6, struct tcphdr *th, otMessage* msg, struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, - struct signals* sig) + struct tcplp_signals* sig) { /* * samkumar: All code pertaining to locks, stats, and debug has been diff --git a/third_party/tcplp/bsdtcp/tcp_reass.c b/third_party/tcplp/bsdtcp/tcp_reass.c index af0372a49..28c1dcc2e 100644 --- a/third_party/tcplp/bsdtcp/tcp_reass.c +++ b/third_party/tcplp/bsdtcp/tcp_reass.c @@ -51,7 +51,7 @@ * not need to update it if only part of the segment is trimmed off. */ int -tcp_reass(struct tcpcb* tp, struct tcphdr* th, int* tlenp, otMessage* data, off_t data_offset, struct signals* sig) +tcp_reass(struct tcpcb* tp, struct tcphdr* th, int* tlenp, otMessage* data, off_t data_offset, struct tcplp_signals* sig) { size_t mergeable, written; size_t offset; diff --git a/third_party/tcplp/bsdtcp/tcp_var.h b/third_party/tcplp/bsdtcp/tcp_var.h index 56890ff92..c21b827a7 100644 --- a/third_party/tcplp/bsdtcp/tcp_var.h +++ b/third_party/tcplp/bsdtcp/tcp_var.h @@ -161,7 +161,7 @@ struct tcpcb_listen { #define SACKHOLE_POOL_SIZE MAX_SACKHOLES #define SACKHOLE_BMP_SIZE BITS_TO_BYTES(SACKHOLE_POOL_SIZE) -struct signals; +struct tcplp_signals; /* * Tcp control block, one per tcp; fields: @@ -577,14 +577,14 @@ int tcp_twcheck(struct tcpcb*, struct tcphdr *, int); void tcp_dropwithreset(struct ip6_hdr* ip6, struct tcphdr *th, struct tcpcb *tp, otInstance* instance, int tlen, int rstreason); int tcp_input(struct ip6_hdr* ip6, struct tcphdr* th, otMessage* msg, struct tcpcb* tp, struct tcpcb_listen* tpl, - struct signals* sig); + struct tcplp_signals* sig); int tcp_output(struct tcpcb *); void tcpip_maketemplate(struct tcpcb *, struct tcptemp*); void tcpip_fillheaders(struct tcpcb *, otMessageInfo *, void *); uint64_t tcp_maxmtu6(struct tcpcb*, struct tcp_ifcap *); int tcp_addoptions(struct tcpopt *, uint8_t *); int tcp_mssopt(struct tcpcb*); -int tcp_reass(struct tcpcb *, struct tcphdr *, int *, otMessage *, off_t, struct signals*); +int tcp_reass(struct tcpcb *, struct tcphdr *, int *, otMessage *, off_t, struct tcplp_signals*); void tcp_sack_init(struct tcpcb *); // Sam: new function that I added void tcp_sack_doack(struct tcpcb *, struct tcpopt *, tcp_seq); void tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_laststart, tcp_seq rcv_lastend); diff --git a/third_party/tcplp/tcplp.h b/third_party/tcplp/tcplp.h index 0ddb6d0d4..dd04a3bee 100644 --- a/third_party/tcplp/tcplp.h +++ b/third_party/tcplp/tcplp.h @@ -53,7 +53,7 @@ extern "C" { #define RELOOKUP_REQUIRED -1 #define CONN_LOST_NORMAL 0 -struct signals { +struct tcplp_signals { int links_popped; bool conn_established; bool recvbuf_notempty;