From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stefano Brivio To: passt-dev@passt.top Subject: [PATCH 07/22] tcp, netlink, HAS{BYTES_ACKED,MIN_RTT,GETRANDOM} and NETLINK_GET_STRICT_CHK Date: Fri, 28 Jan 2022 19:33:42 +0100 Message-ID: <20220128183357.3407606-8-sbrivio@redhat.com> In-Reply-To: <20220128183357.3407606-1-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============7197972160134538807==" --===============7197972160134538807== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable tcpi_bytes_acked and tcpi_min_rtt are only available on recent kernel versions: provide fall-back paths (incurring some grade of performance penalty). Support for getrandom() was introduced in Linux 3.17 and glibc 2.25: provide an alternate mechanism for that as well, reading from /dev/random. Also check if NETLINK_GET_STRICT_CHK is defined before using it: it's not strictly needed, we'll filter out irrelevant results from netlink anyway. Signed-off-by: Stefano Brivio --- Makefile | 15 +++++++++++++++ netlink.c | 9 +++++++-- tcp.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 4647210..443c39d 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,21 @@ ifeq ($(shell printf "$(C)" | $(CC) -S -xc - -o - >/dev/nu= ll 2>&1; echo $$?),0) CFLAGS +=3D -DHAS_SND_WND endif =20 +C :=3D \#include \nstruct tcp_info x =3D { .tcpi_bytes_acked = =3D 0 }; +ifeq ($(shell printf "$(C)" | $(CC) -S -xc - -o - >/dev/null 2>&1; echo $$?)= ,0) + CFLAGS +=3D -DHAS_BYTES_ACKED +endif + +C :=3D \#include \nstruct tcp_info x =3D { .tcpi_min_rtt =3D 0 = }; +ifeq ($(shell printf "$(C)" | $(CC) -S -xc - -o - >/dev/null 2>&1; echo $$?)= ,0) + CFLAGS +=3D -DHAS_MIN_RTT +endif + +C :=3D \#include \nint main(){int a=3Dgetrandom(0, 0, 0);} +ifeq ($(shell printf "$(C)" | $(CC) -S -xc - -o - >/dev/null 2>&1; echo $$?)= ,0) + CFLAGS +=3D -DHAS_GETRANDOM +endif + prefix ?=3D /usr/local =20 all: passt pasta passt4netns qrap diff --git a/netlink.c b/netlink.c index 0948f45..3ba5f05 100644 --- a/netlink.c +++ b/netlink.c @@ -46,7 +46,10 @@ static int nl_seq; static int nl_sock_init_do(void *arg) { struct sockaddr_nl addr =3D { .nl_family =3D AF_NETLINK, }; - int *s =3D &nl_sock, v =3D 1; + int *s =3D &nl_sock; +#ifdef NETLINK_GET_STRICT_CHK + int y =3D 1; +#endif =20 ns: if (((*s) =3D socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0 || @@ -56,7 +59,9 @@ ns: if (*s =3D=3D -1 || !arg || s =3D=3D &nl_sock_ns) return 0; =20 - setsockopt(*s, SOL_NETLINK, NETLINK_GET_STRICT_CHK, &v, sizeof(v)); +#ifdef NETLINK_GET_STRICT_CHK + setsockopt(*s, SOL_NETLINK, NETLINK_GET_STRICT_CHK, &y, sizeof(y)); +#endif =20 ns_enter((struct ctx *)arg); s =3D &nl_sock_ns; diff --git a/tcp.c b/tcp.c index 96d462f..839bf30 100644 --- a/tcp.c +++ b/tcp.c @@ -321,7 +321,9 @@ #include #include #include +#ifdef HAS_GETRANDOM #include +#endif #include #include #include @@ -760,6 +762,7 @@ static int tcp_rtt_dst_low(struct tcp_tap_conn *conn) */ static void tcp_rtt_dst_check(struct tcp_tap_conn *conn, struct tcp_info *ti= nfo) { +#ifdef HAS_MIN_RTT int i, hole =3D -1; =20 if (!tinfo->tcpi_min_rtt || @@ -777,6 +780,10 @@ static void tcp_rtt_dst_check(struct tcp_tap_conn *conn,= struct tcp_info *tinfo) if (hole =3D=3D LOW_RTT_TABLE_SIZE) hole =3D 0; memcpy(low_rtt_dst + hole, &in6addr_any, sizeof(conn->a.a6)); +#else + (void)conn; + (void)tinfo; +#endif /* HAS_MIN_RTT */ } =20 /** @@ -1552,6 +1559,13 @@ static int tcp_update_seqack_wnd(struct ctx *c, struct= tcp_tap_conn *conn, struct tcp_info tinfo_new; int s =3D conn->sock; =20 +#ifndef HAS_BYTES_ACKED + (void)flags; + + conn->seq_ack_to_tap =3D conn->seq_from_tap; + if (SEQ_LT(conn->seq_ack_to_tap, prev_ack_to_tap)) + conn->seq_ack_to_tap =3D prev_ack_to_tap; +#else if (conn->state > ESTABLISHED || (flags & (DUP_ACK | FORCE_ACK)) || conn->local || tcp_rtt_dst_low(conn) || conn->snd_buf < SNDBUF_SMALL) { @@ -1569,6 +1583,7 @@ static int tcp_update_seqack_wnd(struct ctx *c, struct = tcp_tap_conn *conn, if (SEQ_LT(conn->seq_ack_to_tap, prev_ack_to_tap)) conn->seq_ack_to_tap =3D prev_ack_to_tap; } +#endif /* !HAS_BYTES_ACKED */ =20 if (!KERNEL_REPORTS_SND_WND(c)) { tcp_get_sndbuf(conn); @@ -3586,9 +3601,30 @@ int tcp_sock_init(struct ctx *c, struct timespec *now) { struct tcp_sock_refill_arg refill_arg =3D { c, 0 }; int i, port; +#ifndef HAS_GETRANDOM + int dev_random =3D open("/dev/random", O_RDONLY); + unsigned int random_read =3D 0; + + while (dev_random && random_read < sizeof(c->tcp.hash_secret)) { + int ret =3D read(dev_random, + (uint8_t *)&c->tcp.hash_secret + random_read, + sizeof(c->tcp.hash_secret) - random_read); =20 + if (ret =3D=3D -1 && errno =3D=3D EINTR) + continue; + + if (ret <=3D 0) + break; + + random_read +=3D ret; + } + if (dev_random >=3D 0) + close(dev_random); + if (random_read < sizeof(c->tcp.hash_secret)) { +#else if (getrandom(&c->tcp.hash_secret, sizeof(c->tcp.hash_secret), GRND_RANDOM) < 0) { +#endif /* !HAS_GETRANDOM */ perror("TCP initial sequence getrandom"); exit(EXIT_FAILURE); } --=20 2.33.0 --===============7197972160134538807==--