From nobody Sat Apr 20 11:20:22 2024 Delivered-To: wpasupplicant.patchew@gmail.com Received: by 2002:a02:cbb9:0:0:0:0:0 with SMTP id v25csp7705243jap; Tue, 16 Nov 2021 05:59:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJwDgnMjBCjbIN1HWp7ouJZZYmUQZ72UjiRPXByR/nVq+eF7cFpj1TNK7QiqC1RyJBIXc74w X-Received: by 2002:a17:90a:b105:: with SMTP id z5mr72773933pjq.64.1637071173667; Tue, 16 Nov 2021 05:59:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637071173; cv=none; d=google.com; s=arc-20160816; b=Xzn7g31Zf18el0MNFvNMX7yKwau5n1u2pPpqaxzOB7FMlC03TIdaw+kom/Xs9jZYnz udGOZxfzvjnFN4aAa3rhl5oFWkAiiW2WVCs2bIEI80U+1VkpkhUj4dufvUzUNNZmmv3E 5ZnHhx9JenlPHBlsmbT5ZTl8whmKNVs7G7CnemFWjt8UIHI2v8X9xkuRsJ6Irtqj9/3j /2hKbu7AU+cYiK/06Psnf36wewU2Q59L/T5sXLi6apj8NIrS9r7SUgaYWy0y8wI6T7Fu TEo859xyCy3BS6Ox58sWnF1dMF6dBSEq1Oxe5OgOLIaSv5zsma6VSpf3bQBNyCT9flNy effw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=FkTL4Q4rIZqyLdM6Vz0MT/bGnxTA5TgEF8NjM3J7Vlc=; b=vMqo9uzeLSS5CWXAvJjP3ONGrU1yPyzpcd+KrAe4lo3PG2z/lxYTghdYawEU4H15eu fh63ueWV0pr2QkgtK8DKUddXLFsxmVqCK9v2l9++DwSs6O6ZFEGV0brlBvdMl0hXpMKi Jn4zePsMrOFRtGwwee8auxWTx3VLauKdL+ottEkT9JhLggJQwYizcL5BTi5t0OppZ53X 46vcDQNnkbIb+j+bqABy7jEy1wAjA3U8PF8knT9WXoYOEe1HinyPKMcPRHeXF+y2OaEA jphmA6UswM2bJXCLR4UW0IREtcfDj5jpjP0vLfKjcDspC7TaXE5vWMefMt4fSw1he6lY JU+A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of mptcp+bounces-2448-wpasupplicant.patchew=gmail.com@lists.linux.dev designates 147.75.197.195 as permitted sender) smtp.mailfrom="mptcp+bounces-2448-wpasupplicant.patchew=gmail.com@lists.linux.dev" Return-Path: Received: from ewr.edge.kernel.org (ewr.edge.kernel.org. [147.75.197.195]) by mx.google.com with ESMTPS id u3si38313372pfk.211.2021.11.16.05.59.33 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Nov 2021 05:59:33 -0800 (PST) Received-SPF: pass (google.com: domain of mptcp+bounces-2448-wpasupplicant.patchew=gmail.com@lists.linux.dev designates 147.75.197.195 as permitted sender) client-ip=147.75.197.195; Authentication-Results: mx.google.com; spf=pass (google.com: domain of mptcp+bounces-2448-wpasupplicant.patchew=gmail.com@lists.linux.dev designates 147.75.197.195 as permitted sender) smtp.mailfrom="mptcp+bounces-2448-wpasupplicant.patchew=gmail.com@lists.linux.dev" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ewr.edge.kernel.org (Postfix) with ESMTPS id A425D1C0B91 for ; Tue, 16 Nov 2021 13:59:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 35C852C88; Tue, 16 Nov 2021 13:59:31 +0000 (UTC) X-Original-To: mptcp@lists.linux.dev Received: from Chamillionaire.breakpoint.cc (Chamillionaire.breakpoint.cc [193.142.43.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E67EE2C85 for ; Tue, 16 Nov 2021 13:59:29 +0000 (UTC) Received: from fw by Chamillionaire.breakpoint.cc with local (Exim 4.92) (envelope-from ) id 1mmyza-0003TT-4g; Tue, 16 Nov 2021 14:59:22 +0100 From: Florian Westphal To: Cc: Florian Westphal Subject: [PATCH mptcp-next v3 1/4] mptcp: add TCP_INQ cmsg support Date: Tue, 16 Nov 2021 14:48:01 +0100 Message-Id: <20211116134804.30090-2-fw@strlen.de> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211116134804.30090-1-fw@strlen.de> References: <20211116134804.30090-1-fw@strlen.de> Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Support the TCP_INQ setsockopt. This is a boolean that tells recvmsg path to include the remaining in-sequence bytes in the cmsg data. v2: do not use CB(skb)->offset, increment map_seq instead (Paolo Abeni) v3: adjust CB(skb)->map_seq when taking skb from ofo queue (Paolo Abeni) Closes: https://github.com/multipath-tcp/mptcp_net-next/issues/224 Signed-off-by: Florian Westphal Reviewed-by: Mat Martineau --- net/mptcp/protocol.c | 36 +++++++++++++++++++++++++++++++++++- net/mptcp/protocol.h | 1 + net/mptcp/sockopt.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c index b0bfe20d6bb0..4aaa458d846c 100644 --- a/net/mptcp/protocol.c +++ b/net/mptcp/protocol.c @@ -46,6 +46,7 @@ struct mptcp_skb_cb { =20 enum { MPTCP_CMSG_TS =3D BIT(0), + MPTCP_CMSG_INQ =3D BIT(1), }; =20 static struct percpu_counter mptcp_sockets_allocated; @@ -738,6 +739,7 @@ static bool __mptcp_ofo_queue(struct mptcp_sock *msk) MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq, delta); MPTCP_SKB_CB(skb)->offset +=3D delta; + MPTCP_SKB_CB(skb)->map_seq +=3D delta; __skb_queue_tail(&sk->sk_receive_queue, skb); } msk->ack_seq =3D end_seq; @@ -1825,8 +1827,10 @@ static int __mptcp_recvmsg_mskq(struct mptcp_sock *m= sk, copied +=3D count; =20 if (count < data_len) { - if (!(flags & MSG_PEEK)) + if (!(flags & MSG_PEEK)) { MPTCP_SKB_CB(skb)->offset +=3D count; + MPTCP_SKB_CB(skb)->map_seq +=3D count; + } break; } =20 @@ -2006,6 +2010,27 @@ static bool __mptcp_move_skbs(struct mptcp_sock *msk) return !skb_queue_empty(&msk->receive_queue); } =20 +static unsigned int mptcp_inq_hint(const struct sock *sk) +{ + const struct mptcp_sock *msk =3D mptcp_sk(sk); + const struct sk_buff *skb; + + skb =3D skb_peek(&msk->receive_queue); + if (skb) { + u64 hint_val =3D msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq; + + if (hint_val >=3D INT_MAX) + return INT_MAX; + + return (unsigned int)hint_val; + } + + if (sk->sk_state =3D=3D TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)) + return 1; + + return 0; +} + static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, int flags, int *addr_len) { @@ -2030,6 +2055,9 @@ static int mptcp_recvmsg(struct sock *sk, struct msgh= dr *msg, size_t len, len =3D min_t(size_t, len, INT_MAX); target =3D sock_rcvlowat(sk, flags & MSG_WAITALL, len); =20 + if (unlikely(msk->recvmsg_inq)) + cmsg_flags =3D MPTCP_CMSG_INQ; + while (copied < len) { int bytes_read; =20 @@ -2103,6 +2131,12 @@ static int mptcp_recvmsg(struct sock *sk, struct msg= hdr *msg, size_t len, if (cmsg_flags && copied >=3D 0) { if (cmsg_flags & MPTCP_CMSG_TS) tcp_recv_timestamp(msg, sk, &tss); + + if (cmsg_flags & MPTCP_CMSG_INQ) { + unsigned int inq =3D mptcp_inq_hint(sk); + + put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq); + } } =20 pr_debug("msk=3D%p rx queue empty=3D%d:%d copied=3D%d", diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h index 906509c6cde5..e77de7662df0 100644 --- a/net/mptcp/protocol.h +++ b/net/mptcp/protocol.h @@ -250,6 +250,7 @@ struct mptcp_sock { bool use_64bit_ack; /* Set when we received a 64-bit DSN */ bool csum_enabled; bool allow_infinite_fallback; + u8 recvmsg_inq:1; spinlock_t join_list_lock; struct work_struct work; struct sk_buff *ooo_last_skb; diff --git a/net/mptcp/sockopt.c b/net/mptcp/sockopt.c index fb43e145cb57..0295d6d9207e 100644 --- a/net/mptcp/sockopt.c +++ b/net/mptcp/sockopt.c @@ -557,6 +557,7 @@ static bool mptcp_supported_sockopt(int level, int optn= ame) case TCP_TIMESTAMP: case TCP_NOTSENT_LOWAT: case TCP_TX_DELAY: + case TCP_INQ: return true; } =20 @@ -698,7 +699,21 @@ static int mptcp_setsockopt_v4(struct mptcp_sock *msk,= int optname, static int mptcp_setsockopt_sol_tcp(struct mptcp_sock *msk, int optname, sockptr_t optval, unsigned int optlen) { + struct sock *sk =3D (void *)msk; + int ret, val; + switch (optname) { + case TCP_INQ: + ret =3D mptcp_get_int_option(msk, optval, optlen, &val); + if (ret) + return ret; + if (val < 0 || val > 1) + return -EINVAL; + + lock_sock(sk); + msk->recvmsg_inq =3D !!val; + release_sock(sk); + return 0; case TCP_ULP: return -EOPNOTSUPP; case TCP_CONGESTION: @@ -1032,6 +1047,26 @@ static int mptcp_getsockopt_subflow_addrs(struct mpt= cp_sock *msk, char __user *o return 0; } =20 +static int mptcp_put_int_option(struct mptcp_sock *msk, char __user *optva= l, + int __user *optlen, int val) +{ + int len; + + if (get_user(len, optlen)) + return -EFAULT; + + len =3D min_t(unsigned int, len, sizeof(int)); + if (len < 0) + return -EINVAL; + + if (put_user(len, optlen)) + return -EFAULT; + if (copy_to_user(optval, &val, len)) + return -EFAULT; + + return 0; +} + static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname, char __user *optval, int __user *optlen) { @@ -1042,6 +1077,8 @@ static int mptcp_getsockopt_sol_tcp(struct mptcp_sock= *msk, int optname, case TCP_CC_INFO: return mptcp_getsockopt_first_sf_only(msk, SOL_TCP, optname, optval, optlen); + case TCP_INQ: + return mptcp_put_int_option(msk, optval, optlen, msk->recvmsg_inq); } return -EOPNOTSUPP; } --=20 2.32.0 From nobody Sat Apr 20 11:20:22 2024 Delivered-To: wpasupplicant.patchew@gmail.com Received: by 2002:a02:cbb9:0:0:0:0:0 with SMTP id v25csp7705157jap; Tue, 16 Nov 2021 05:59:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJxLiC6WZQM9d8nJ/a/BOjdSrGxnpGCx3Ea0dipz9uLjnjO5IbYBRhdX3YcBtVNynRQl4scj X-Received: by 2002:aa7:83cd:0:b0:480:9e79:fae5 with SMTP id j13-20020aa783cd000000b004809e79fae5mr40529579pfn.42.1637071170152; Tue, 16 Nov 2021 05:59:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637071170; cv=none; d=google.com; s=arc-20160816; b=oW4uw9Gk5YFV8lwg2ZBfdRd4KpFwUSZAJ/p6iPYqulikWT/HskVcuK/sdPIJFkBLv4 Vc8JKdx7PahbkDwalZkBmZAhSwYjBf/mXI2XZjMuoLIaySLjNyjoJGnkDWYSl8XU2X46 5cRQzTFBuH2QI6KvSKU/x4KxZxCW+DzIlyQh+1IG5fD1VpaPplwOlj/mY396WMfvwGW7 H2JXhxllODDx5tlNpb5uPqPYwm3Vj3GvpOf8vvklKJbzcouXBygF+rF7ElKvWZxOivKR 9pWdD+OxknJcznwGIpQJfWx+Tr/FLhyokIoJuDqIXCOqb7ERNQN/ekhcBqFib9N/Ej6r JovA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=qgkT64WZVTuVA59luxJdqTNCqDHNJebqupWXp7xOfos=; b=FftTr98hk2pH4gu1bHGs7U8Quw657w1H83+nxnw7u/mEQ/glLNeMMn5Plv+i+xGsJ7 n1MyR9QlIdK4wRCDCNELTLnmWl2oFe9KXo0IoZxOqUunQt3VqPY0NyZdfZAK1SgLtepF h33Up2etKHCdfnWIqdaFnRdMceWUMKQlJV7dNcEBnM4kzT/FcBDRdUgx8KLFdsTVgkbq nIUbongU8z44P2dBAVIp013vgIIagx6oxpOxywdQ3dkyOq6WUt6orGr1Gfbn909wcYL9 Fu1GHxsdkvHg/y0W/R7uAUbi+VnUcHKBc+4t9GRmYGD5v0Cs1TU/UsbxbdfT13OMFg43 ZHgw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of mptcp+bounces-2447-wpasupplicant.patchew=gmail.com@lists.linux.dev designates 2604:1380:1000:8100::1 as permitted sender) smtp.mailfrom="mptcp+bounces-2447-wpasupplicant.patchew=gmail.com@lists.linux.dev" Return-Path: Received: from sjc.edge.kernel.org (sjc.edge.kernel.org. [2604:1380:1000:8100::1]) by mx.google.com with ESMTPS id t13si104212plg.85.2021.11.16.05.59.30 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Nov 2021 05:59:30 -0800 (PST) Received-SPF: pass (google.com: domain of mptcp+bounces-2447-wpasupplicant.patchew=gmail.com@lists.linux.dev designates 2604:1380:1000:8100::1 as permitted sender) client-ip=2604:1380:1000:8100::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of mptcp+bounces-2447-wpasupplicant.patchew=gmail.com@lists.linux.dev designates 2604:1380:1000:8100::1 as permitted sender) smtp.mailfrom="mptcp+bounces-2447-wpasupplicant.patchew=gmail.com@lists.linux.dev" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sjc.edge.kernel.org (Postfix) with ESMTPS id D9AA93E0F0C for ; Tue, 16 Nov 2021 13:59:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6EADF2C96; Tue, 16 Nov 2021 13:59:29 +0000 (UTC) X-Original-To: mptcp@lists.linux.dev Received: from Chamillionaire.breakpoint.cc (Chamillionaire.breakpoint.cc [193.142.43.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0E082C85 for ; Tue, 16 Nov 2021 13:59:27 +0000 (UTC) Received: from fw by Chamillionaire.breakpoint.cc with local (Exim 4.92) (envelope-from ) id 1mmyze-0003Tg-9U; Tue, 16 Nov 2021 14:59:26 +0100 From: Florian Westphal To: Cc: Florian Westphal Subject: [PATCH mptcp-next v3 2/4] selftests: mptcp: add TCP_INQ support Date: Tue, 16 Nov 2021 14:48:02 +0100 Message-Id: <20211116134804.30090-3-fw@strlen.de> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211116134804.30090-1-fw@strlen.de> References: <20211116134804.30090-1-fw@strlen.de> Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Do checks on the returned inq counter. Fail on: 1. Huge value (> 1 kbyte, test case files are 1 kb) 2. last hint larger than returned bytes when read was short 3. erronenous indication of EOF. 3) happens when a hint of X bytes reads X-1 on next call but next recvmsg returns more data (instead of EOF). Signed-off-by: Florian Westphal Reviewed-by: Mat Martineau --- .../selftests/net/mptcp/mptcp_connect.c | 58 ++++++++++++++++++- .../selftests/net/mptcp/mptcp_sockopt.sh | 4 +- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/net/mptcp/mptcp_connect.c b/tools/test= ing/selftests/net/mptcp/mptcp_connect.c index ada9b80774d4..0e396af4545a 100644 --- a/tools/testing/selftests/net/mptcp/mptcp_connect.c +++ b/tools/testing/selftests/net/mptcp/mptcp_connect.c @@ -73,12 +73,20 @@ static uint32_t cfg_mark; struct cfg_cmsg_types { unsigned int cmsg_enabled:1; unsigned int timestampns:1; + unsigned int tcp_inq:1; }; =20 struct cfg_sockopt_types { unsigned int transparent:1; }; =20 +struct tcp_inq_state { + unsigned int last; + bool expect_eof; +}; + +static struct tcp_inq_state tcp_inq; + static struct cfg_cmsg_types cfg_cmsg_types; static struct cfg_sockopt_types cfg_sockopt_types; =20 @@ -389,7 +397,9 @@ static size_t do_write(const int fd, char *buf, const s= ize_t len) static void process_cmsg(struct msghdr *msgh) { struct __kernel_timespec ts; + bool inq_found =3D false; bool ts_found =3D false; + unsigned int inq =3D 0; struct cmsghdr *cmsg; =20 for (cmsg =3D CMSG_FIRSTHDR(msgh); cmsg ; cmsg =3D CMSG_NXTHDR(msgh, cmsg= )) { @@ -398,12 +408,27 @@ static void process_cmsg(struct msghdr *msgh) ts_found =3D true; continue; } + if (cmsg->cmsg_level =3D=3D IPPROTO_TCP && cmsg->cmsg_type =3D=3D TCP_CM= _INQ) { + memcpy(&inq, CMSG_DATA(cmsg), sizeof(inq)); + inq_found =3D true; + continue; + } + } =20 if (cfg_cmsg_types.timestampns) { if (!ts_found) xerror("TIMESTAMPNS not present\n"); } + + if (cfg_cmsg_types.tcp_inq) { + if (!inq_found) + xerror("TCP_INQ not present\n"); + + if (inq > 1024) + xerror("tcp_inq %u is larger than one kbyte\n", inq); + tcp_inq.last =3D inq; + } } =20 static ssize_t do_recvmsg_cmsg(const int fd, char *buf, const size_t len) @@ -420,10 +445,23 @@ static ssize_t do_recvmsg_cmsg(const int fd, char *bu= f, const size_t len) .msg_controllen =3D sizeof(msg_buf), }; int flags =3D 0; + unsigned int last_hint =3D tcp_inq.last; int ret =3D recvmsg(fd, &msg, flags); =20 - if (ret <=3D 0) + if (ret <=3D 0) { + if (ret =3D=3D 0 && tcp_inq.expect_eof) + return ret; + + if (ret =3D=3D 0 && cfg_cmsg_types.tcp_inq) + if (last_hint !=3D 1 && last_hint !=3D 0) + xerror("EOF but last tcp_inq hint was %u\n", last_hint); + return ret; + } + + if (tcp_inq.expect_eof) + xerror("expected EOF, last_hint %u, now %u\n", + last_hint, tcp_inq.last); =20 if (msg.msg_controllen && !cfg_cmsg_types.cmsg_enabled) xerror("got %lu bytes of cmsg data, expected 0\n", @@ -435,6 +473,17 @@ static ssize_t do_recvmsg_cmsg(const int fd, char *buf= , const size_t len) if (msg.msg_controllen) process_cmsg(&msg); =20 + if (cfg_cmsg_types.tcp_inq) { + if ((size_t)ret < len && last_hint > (unsigned int)ret) { + if (ret + 1 !=3D (int)last_hint) { + int next =3D read(fd, msg_buf, sizeof(msg_buf)); + + xerror("read %u of %u, last_hint was %u tcp_inq hint now %u next_read = returned %d/%m\n", ret, (unsigned int)len, last_hint, tcp_inq.last, next); + } else + tcp_inq.expect_eof =3D true; + } + } + return ret; } =20 @@ -944,6 +993,8 @@ static void apply_cmsg_types(int fd, const struct cfg_c= msg_types *cmsg) =20 if (cmsg->timestampns) xsetsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS_NEW, &on, sizeof(on)); + if (cmsg->tcp_inq) + xsetsockopt(fd, IPPROTO_TCP, TCP_INQ, &on, sizeof(on)); } =20 static void parse_cmsg_types(const char *type) @@ -965,6 +1016,11 @@ static void parse_cmsg_types(const char *type) return; } =20 + if (strncmp(type, "TCPINQ", len) =3D=3D 0) { + cfg_cmsg_types.tcp_inq =3D 1; + return; + } + fprintf(stderr, "Unrecognized cmsg option %s\n", type); exit(1); } diff --git a/tools/testing/selftests/net/mptcp/mptcp_sockopt.sh b/tools/tes= ting/selftests/net/mptcp/mptcp_sockopt.sh index 41de643788b8..c8c364369599 100755 --- a/tools/testing/selftests/net/mptcp/mptcp_sockopt.sh +++ b/tools/testing/selftests/net/mptcp/mptcp_sockopt.sh @@ -178,7 +178,7 @@ do_transfer() =20 timeout ${timeout_test} \ ip netns exec ${listener_ns} \ - $mptcp_connect -t ${timeout_poll} -l -M 1 -p $port -s ${srv_proto} -c T= IMESTAMPNS \ + $mptcp_connect -t ${timeout_poll} -l -M 1 -p $port -s ${srv_proto} -c T= IMESTAMPNS,TCPINQ \ ${local_addr} < "$sin" > "$sout" & spid=3D$! =20 @@ -186,7 +186,7 @@ do_transfer() =20 timeout ${timeout_test} \ ip netns exec ${connector_ns} \ - $mptcp_connect -t ${timeout_poll} -M 2 -p $port -s ${cl_proto} -c TIMES= TAMPNS \ + $mptcp_connect -t ${timeout_poll} -M 2 -p $port -s ${cl_proto} -c TIMES= TAMPNS,TCPINQ \ $connect_addr < "$cin" > "$cout" & =20 cpid=3D$! --=20 2.32.0 From nobody Sat Apr 20 11:20:22 2024 Delivered-To: wpasupplicant.patchew@gmail.com Received: by 2002:a02:cbb9:0:0:0:0:0 with SMTP id v25csp7705266jap; Tue, 16 Nov 2021 05:59:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJyX8UgOXKGTfwIBSBebRt3fFgc3IJE1R6pksIcjeWX6SXFpkIpLG46Yl9Z740CaT6kRF9Zn X-Received: by 2002:a17:902:b588:b0:143:b732:834 with SMTP id a8-20020a170902b58800b00143b7320834mr31663945pls.22.1637071174876; Tue, 16 Nov 2021 05:59:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637071174; cv=none; d=google.com; s=arc-20160816; b=ue8fvreEmYcXz71n8ZDBUvZBTgbgHT+/YmsBJxztNS7dywpRrbFd2HFX0YQoS0wD3l CK4pjwrS3/6fuLGWKTmgaqVvBS/0/c3roonLzjecd5jHC//e48tnXQrFUkEJiBGnYxhP aVXSxY5skEHJ0bH5pA2yxXc8LqxAtqNgDy2Mz3/4XD+RGtmATB1vvtwDLQuaRueZvRT/ csP0NMKpzalVwXuoR9x/7zAE1K6qQ1mgKXe8CcUPLx+rXd+li38xU/PRREUFs9BGrWsd 0U2IQX4ivrlk/BVRvbo1Z0LdSeFF967LG0ezQWk4Q1sE+y5iBZzfkZmFDO9vAt6WPwk/ l8iw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=WZ2iK+2TQsbAa8fzWbhzHileWHjxK3oIDWVH502VOpY=; b=hCM8G4dtnNdeGzBAzjKtmbcTUavriNd0mAQbRBSO7NiKouR5hOIVzAI45pn5Voc5Ft 2g1fL6Lqw1RyoxUhnPhi6zfGGmjLpICcJUK2ytTGfkZkqEixSjKnfwYPQS4sTl9lIShV oLYlBbv/VAzK6WFA58Oj8ILJJhbpPr1aTpV1m2vevT64N1PXsqN8voIIA/dGSdLBtwjS 9u1557wdLIYq3tB6Atv8svGVHl15SYnb77o8HhAnJlwaYWonMbh5ofTxdVmudAqDYHjX 56WAjj9tadR/cedL/8hSdR3p6CwPPlsKogigK7u4XJ1Fe2JzDQOX28vBMijmmiqWiPa3 /olg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of mptcp+bounces-2449-wpasupplicant.patchew=gmail.com@lists.linux.dev designates 147.75.69.165 as permitted sender) smtp.mailfrom="mptcp+bounces-2449-wpasupplicant.patchew=gmail.com@lists.linux.dev" Return-Path: Received: from sjc.edge.kernel.org (sjc.edge.kernel.org. [147.75.69.165]) by mx.google.com with ESMTPS id 193si29696907pgc.15.2021.11.16.05.59.34 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Nov 2021 05:59:34 -0800 (PST) Received-SPF: pass (google.com: domain of mptcp+bounces-2449-wpasupplicant.patchew=gmail.com@lists.linux.dev designates 147.75.69.165 as permitted sender) client-ip=147.75.69.165; Authentication-Results: mx.google.com; spf=pass (google.com: domain of mptcp+bounces-2449-wpasupplicant.patchew=gmail.com@lists.linux.dev designates 147.75.69.165 as permitted sender) smtp.mailfrom="mptcp+bounces-2449-wpasupplicant.patchew=gmail.com@lists.linux.dev" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sjc.edge.kernel.org (Postfix) with ESMTPS id 9AE7A3E1020 for ; Tue, 16 Nov 2021 13:59:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 64D0C2C88; Tue, 16 Nov 2021 13:59:33 +0000 (UTC) X-Original-To: mptcp@lists.linux.dev Received: from Chamillionaire.breakpoint.cc (Chamillionaire.breakpoint.cc [193.142.43.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 415802C85 for ; Tue, 16 Nov 2021 13:59:32 +0000 (UTC) Received: from fw by Chamillionaire.breakpoint.cc with local (Exim 4.92) (envelope-from ) id 1mmyzi-0003Tq-EL; Tue, 16 Nov 2021 14:59:30 +0100 From: Florian Westphal To: Cc: Florian Westphal Subject: [PATCH mptcp-next v3 3/4] mptcp: add SIOCINQ, OUTQ and OUTQNSD ioctls Date: Tue, 16 Nov 2021 14:48:03 +0100 Message-Id: <20211116134804.30090-4-fw@strlen.de> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211116134804.30090-1-fw@strlen.de> References: <20211116134804.30090-1-fw@strlen.de> Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allows to query in-sequence data ready for read(), total bytes in write queue and total bytes in write queue that have not yet been sent. v2: remove unneeded READ_ONCE() (Paolo Abeni) v3: check for new data unconditionally in SIOCINQ ioctl (Mat Martineau) Signed-off-by: Florian Westphal Reviewed-by: Mat Martineau --- net/mptcp/protocol.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c index 4aaa458d846c..3ab6628ea2b2 100644 --- a/net/mptcp/protocol.c +++ b/net/mptcp/protocol.c @@ -22,6 +22,7 @@ #endif #include #include +#include #include "protocol.h" #include "mib.h" =20 @@ -3261,6 +3262,57 @@ static int mptcp_forward_alloc_get(const struct sock= *sk) return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc; } =20 +static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v) +{ + const struct sock *sk =3D (void *)msk; + u64 delta; + + if (sk->sk_state =3D=3D TCP_LISTEN) + return -EINVAL; + + if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) + return 0; + + delta =3D msk->write_seq - v; + if (delta > INT_MAX) + delta =3D INT_MAX; + + return (int)delta; +} + +static int mptcp_ioctl(struct sock *sk, int cmd, unsigned long arg) +{ + struct mptcp_sock *msk =3D mptcp_sk(sk); + bool slow; + int answ; + + switch (cmd) { + case SIOCINQ: + if (sk->sk_state =3D=3D TCP_LISTEN) + return -EINVAL; + + lock_sock(sk); + __mptcp_move_skbs(msk); + answ =3D mptcp_inq_hint(sk); + release_sock(sk); + break; + case SIOCOUTQ: + slow =3D lock_sock_fast(sk); + answ =3D mptcp_ioctl_outq(msk, READ_ONCE(msk->snd_una)); + unlock_sock_fast(sk, slow); + break; + case SIOCOUTQNSD: + slow =3D lock_sock_fast(sk); + answ =3D mptcp_ioctl_outq(msk, msk->snd_nxt); + unlock_sock_fast(sk, slow); + break; + default: + return -ENOIOCTLCMD; + } + + return put_user(answ, (int __user *)arg); +} + static struct proto mptcp_prot =3D { .name =3D "MPTCP", .owner =3D THIS_MODULE, @@ -3273,6 +3325,7 @@ static struct proto mptcp_prot =3D { .shutdown =3D mptcp_shutdown, .destroy =3D mptcp_destroy, .sendmsg =3D mptcp_sendmsg, + .ioctl =3D mptcp_ioctl, .recvmsg =3D mptcp_recvmsg, .release_cb =3D mptcp_release_cb, .hash =3D mptcp_hash, --=20 2.32.0 From nobody Sat Apr 20 11:20:22 2024 Delivered-To: wpasupplicant.patchew@gmail.com Received: by 2002:a02:cbb9:0:0:0:0:0 with SMTP id v25csp7705408jap; Tue, 16 Nov 2021 05:59:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJyTiSjQFGvQCRxgB5bXU+98cJJxzYkqcgrlc5W6XGG9syM7qPc466em8b5y2Hq7IfVRfFwS X-Received: by 2002:a17:90a:d3c3:: with SMTP id d3mr75796728pjw.209.1637071180675; Tue, 16 Nov 2021 05:59:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637071180; cv=none; d=google.com; s=arc-20160816; b=IJ/8Prhyp5cuhrgyojzn3BrnM9A6lBs9tQjeNJ7+TEbT7C3J701m1qhG5Rjwxy71u1 wF4pZWKxaULUjaEaDSVgtTCVS/6uPkJK669YBei+/jeoVHn5eyDvc5YVf9/2NZyAP6W4 s1Ktwriy+AqCjzqc/wzuWwXBphBtXrsuhk/anFXsYbb6CG4009uU4IuIUNc27H7s9GKQ Xh/y/u1tLZkRf1h9p4EE3oSH72p/MQfjPZKQYq78UwMBMNmMPLpmYaaY4j9g9YHwfZpf nVFi0ucepPLd93lRzuj3xKtW2RPR4l4Ih3CCJJbBJkxfJ5uHWH4WO0H4hdtossJST5to 63nw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=sfi55wqswJL4Muq4xM0zM8uK3jJndb3IXc13YTMr7Xc=; b=eFoa7n68LJsEZD5zXRJeg6L+2XCJfcmfnTgfdal2HQMlJ55ilPrYTS0zbSWhbXboWK RpHXXzT2zwGPVMNCE0eoXcnlbISZsnO8281PtRB4xxn2rj1AP1x3Vtn3e77O0fihAlOR 5n3l0360piNIZSHnJ9R3tXpKLoo/JqJT6TOPpa/Vo2uUEkdTaFWxtOm5aXkaGRqM1joG Vn22WvvqydDL5vctQA2105KxleJyetWK3n3alfyvYbQmNcJNQuZ9yRifrRMqHPxhE5Iq BqultWU4+osZXOgPULoQ5uhyezBHo+LsPPfZzD4n2n2qkt+dJBM+5I1pj6xurm1qO5gT e8WA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of mptcp+bounces-2450-wpasupplicant.patchew=gmail.com@lists.linux.dev designates 147.75.197.195 as permitted sender) smtp.mailfrom="mptcp+bounces-2450-wpasupplicant.patchew=gmail.com@lists.linux.dev" Return-Path: Received: from ewr.edge.kernel.org (ewr.edge.kernel.org. [147.75.197.195]) by mx.google.com with ESMTPS id s7si4549004pjp.159.2021.11.16.05.59.40 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Nov 2021 05:59:40 -0800 (PST) Received-SPF: pass (google.com: domain of mptcp+bounces-2450-wpasupplicant.patchew=gmail.com@lists.linux.dev designates 147.75.197.195 as permitted sender) client-ip=147.75.197.195; Authentication-Results: mx.google.com; spf=pass (google.com: domain of mptcp+bounces-2450-wpasupplicant.patchew=gmail.com@lists.linux.dev designates 147.75.197.195 as permitted sender) smtp.mailfrom="mptcp+bounces-2450-wpasupplicant.patchew=gmail.com@lists.linux.dev" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ewr.edge.kernel.org (Postfix) with ESMTPS id DEBBD1C0BA1 for ; Tue, 16 Nov 2021 13:59:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 671E62C88; Tue, 16 Nov 2021 13:59:38 +0000 (UTC) X-Original-To: mptcp@lists.linux.dev Received: from Chamillionaire.breakpoint.cc (Chamillionaire.breakpoint.cc [193.142.43.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C125D2C85 for ; Tue, 16 Nov 2021 13:59:36 +0000 (UTC) Received: from fw by Chamillionaire.breakpoint.cc with local (Exim 4.92) (envelope-from ) id 1mmyzm-0003UD-Mx; Tue, 16 Nov 2021 14:59:34 +0100 From: Florian Westphal To: Cc: Florian Westphal , Mat Martineau Subject: [PATCH mptcp-next v3 4/4] selftests: mptcp: add inq test case Date: Tue, 16 Nov 2021 14:48:04 +0100 Message-Id: <20211116134804.30090-5-fw@strlen.de> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211116134804.30090-1-fw@strlen.de> References: <20211116134804.30090-1-fw@strlen.de> Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" client & server use a unix socket connection to communicate outside of the mptcp connection. This allows the consumer to know in advance how many bytes have been (or will be) sent by the peer. This allows stricter checks on the bytecounts reported by TCP_INQ cmsg. Suggested-by: Mat Martineau Signed-off-by: Florian Westphal Reviewed-by: Mat Martineau --- tools/testing/selftests/net/mptcp/.gitignore | 1 + tools/testing/selftests/net/mptcp/Makefile | 2 +- tools/testing/selftests/net/mptcp/mptcp_inq.c | 603 ++++++++++++++++++ .../selftests/net/mptcp/mptcp_sockopt.sh | 40 ++ 4 files changed, 645 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/net/mptcp/mptcp_inq.c diff --git a/tools/testing/selftests/net/mptcp/.gitignore b/tools/testing/s= elftests/net/mptcp/.gitignore index 7569d892967a..49daae73c41e 100644 --- a/tools/testing/selftests/net/mptcp/.gitignore +++ b/tools/testing/selftests/net/mptcp/.gitignore @@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only mptcp_connect +mptcp_inq mptcp_sockopt pm_nl_ctl *.pcap diff --git a/tools/testing/selftests/net/mptcp/Makefile b/tools/testing/sel= ftests/net/mptcp/Makefile index bbf4e448bad9..0356c4501c99 100644 --- a/tools/testing/selftests/net/mptcp/Makefile +++ b/tools/testing/selftests/net/mptcp/Makefile @@ -8,7 +8,7 @@ CFLAGS =3D -Wall -Wl,--no-as-needed -O2 -g -I$(top_srcdir= )/usr/include TEST_PROGS :=3D mptcp_connect.sh pm_netlink.sh mptcp_join.sh diag.sh \ simult_flows.sh mptcp_sockopt.sh =20 -TEST_GEN_FILES =3D mptcp_connect pm_nl_ctl mptcp_sockopt +TEST_GEN_FILES =3D mptcp_connect pm_nl_ctl mptcp_sockopt mptcp_inq =20 TEST_FILES :=3D settings =20 diff --git a/tools/testing/selftests/net/mptcp/mptcp_inq.c b/tools/testing/= selftests/net/mptcp/mptcp_inq.c new file mode 100644 index 000000000000..62ee2dd63b5f --- /dev/null +++ b/tools/testing/selftests/net/mptcp/mptcp_inq.c @@ -0,0 +1,603 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#ifndef IPPROTO_MPTCP +#define IPPROTO_MPTCP 262 +#endif +#ifndef SOL_MPTCP +#define SOL_MPTCP 284 +#endif + +static int pf =3D AF_INET; +static int proto_tx =3D IPPROTO_MPTCP; +static int proto_rx =3D IPPROTO_MPTCP; + +static void die_perror(const char *msg) +{ + perror(msg); + exit(1); +} + +static void die_usage(int r) +{ + fprintf(stderr, "Usage: mptcp_inq [-6] [ -t tcp|mptcp ] [ -r tcp|mptcp]\n= "); + exit(r); +} + +static void xerror(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fputc('\n', stderr); + exit(1); +} + +static const char *getxinfo_strerr(int err) +{ + if (err =3D=3D EAI_SYSTEM) + return strerror(errno); + + return gai_strerror(err); +} + +static void xgetaddrinfo(const char *node, const char *service, + const struct addrinfo *hints, + struct addrinfo **res) +{ + int err =3D getaddrinfo(node, service, hints, res); + + if (err) { + const char *errstr =3D getxinfo_strerr(err); + + fprintf(stderr, "Fatal: getaddrinfo(%s:%s): %s\n", + node ? node : "", service ? service : "", errstr); + exit(1); + } +} + +static int sock_listen_mptcp(const char * const listenaddr, + const char * const port) +{ + int sock; + struct addrinfo hints =3D { + .ai_protocol =3D IPPROTO_TCP, + .ai_socktype =3D SOCK_STREAM, + .ai_flags =3D AI_PASSIVE | AI_NUMERICHOST + }; + + hints.ai_family =3D pf; + + struct addrinfo *a, *addr; + int one =3D 1; + + xgetaddrinfo(listenaddr, port, &hints, &addr); + hints.ai_family =3D pf; + + for (a =3D addr; a; a =3D a->ai_next) { + sock =3D socket(a->ai_family, a->ai_socktype, proto_rx); + if (sock < 0) + continue; + + if (-1 =3D=3D setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, + sizeof(one))) + perror("setsockopt"); + + if (bind(sock, a->ai_addr, a->ai_addrlen) =3D=3D 0) + break; /* success */ + + perror("bind"); + close(sock); + sock =3D -1; + } + + freeaddrinfo(addr); + + if (sock < 0) + xerror("could not create listen socket"); + + if (listen(sock, 20)) + die_perror("listen"); + + return sock; +} + +static int sock_connect_mptcp(const char * const remoteaddr, + const char * const port, int proto) +{ + struct addrinfo hints =3D { + .ai_protocol =3D IPPROTO_TCP, + .ai_socktype =3D SOCK_STREAM, + }; + struct addrinfo *a, *addr; + int sock =3D -1; + + hints.ai_family =3D pf; + + xgetaddrinfo(remoteaddr, port, &hints, &addr); + for (a =3D addr; a; a =3D a->ai_next) { + sock =3D socket(a->ai_family, a->ai_socktype, proto); + if (sock < 0) + continue; + + if (connect(sock, a->ai_addr, a->ai_addrlen) =3D=3D 0) + break; /* success */ + + die_perror("connect"); + } + + if (sock < 0) + xerror("could not create connect socket"); + + freeaddrinfo(addr); + return sock; +} + +static int protostr_to_num(const char *s) +{ + if (strcasecmp(s, "tcp") =3D=3D 0) + return IPPROTO_TCP; + if (strcasecmp(s, "mptcp") =3D=3D 0) + return IPPROTO_MPTCP; + + die_usage(1); + return 0; +} + +static void parse_opts(int argc, char **argv) +{ + int c; + + while ((c =3D getopt(argc, argv, "h6t:r:")) !=3D -1) { + switch (c) { + case 'h': + die_usage(0); + break; + case '6': + pf =3D AF_INET6; + break; + case 't': + proto_tx =3D protostr_to_num(optarg); + break; + case 'r': + proto_rx =3D protostr_to_num(optarg); + break; + default: + die_usage(1); + break; + } + } +} + +/* wait up to timeout milliseconds */ +static void wait_for_ack(int fd, int timeout, size_t total) +{ + int i; + + for (i =3D 0; i < timeout; i++) { + int nsd, ret, queued =3D -1; + struct timespec req; + + ret =3D ioctl(fd, TIOCOUTQ, &queued); + if (ret < 0) + die_perror("TIOCOUTQ"); + + ret =3D ioctl(fd, SIOCOUTQNSD, &nsd); + if (ret < 0) + die_perror("SIOCOUTQNSD"); + + if ((size_t)queued > total) + xerror("TIOCOUTQ %u, but only %zu expected\n", queued, total); + assert(nsd <=3D queued); + + if (queued =3D=3D 0) + return; + + /* wait for peer to ack rx of all data */ + req.tv_sec =3D 0; + req.tv_nsec =3D 1 * 1000 * 1000ul; /* 1ms */ + nanosleep(&req, NULL); + } + + xerror("still tx data queued after %u ms\n", timeout); +} + +static void connect_one_server(int fd, int unixfd) +{ + size_t len, i, total, sent; + char buf[4096], buf2[4096]; + int queued, on =3D 1; + ssize_t ret; + + len =3D rand() % (sizeof(buf) - 1); + + if (len < 128) + len =3D 128; + + for (i =3D 0; i < len ; i++) { + buf[i] =3D rand() % 26; + buf[i] +=3D 'A'; + } + + buf[i] =3D '\n'; + + /* un-block server */ + ret =3D read(unixfd, buf2, 4); + assert(ret =3D=3D 4); + + assert(strncmp(buf2, "xmit", 4) =3D=3D 0); + + ret =3D write(unixfd, &len, sizeof(len)); + assert(ret =3D=3D (ssize_t)sizeof(len)); + + ret =3D write(fd, buf, len); + if (ret < 0) + die_perror("write"); + + if (ret !=3D (ssize_t)len) + xerror("short write"); + + ret =3D read(unixfd, buf2, 4); + assert(strncmp(buf2, "huge", 4) =3D=3D 0); + + total =3D rand() % (16 * 1024 * 1024); + total +=3D (1 * 1024 * 1024); + sent =3D total; + + ret =3D write(unixfd, &total, sizeof(total)); + assert(ret =3D=3D (ssize_t)sizeof(total)); + + wait_for_ack(fd, 5000, len); + + while (total > 0) { + if (total > sizeof(buf)) + len =3D sizeof(buf); + else + len =3D total; + + ret =3D write(fd, buf, len); + if (ret < 0) + die_perror("write"); + total -=3D ret; + + /* we don't have to care about buf content, only + * number of total bytes sent + */ + } + + ret =3D read(unixfd, buf2, 4); + assert(ret =3D=3D 4); + assert(strncmp(buf2, "shut", 4) =3D=3D 0); + + wait_for_ack(fd, 5000, sent); + + write(fd, buf, 1); + close(fd); + ret =3D write(unixfd, "closed", 6); + assert(ret =3D=3D 6); + + close(unixfd); +} + +static void get_tcp_inq(struct msghdr *msgh, unsigned int *inqv) +{ + struct cmsghdr *cmsg; + + for (cmsg =3D CMSG_FIRSTHDR(msgh); cmsg ; cmsg =3D CMSG_NXTHDR(msgh, cmsg= )) { + if (cmsg->cmsg_level =3D=3D IPPROTO_TCP && cmsg->cmsg_type =3D=3D TCP_CM= _INQ) { + memcpy(inqv, CMSG_DATA(cmsg), sizeof(*inqv)); + return; + } + } + + xerror("could not find TCP_CM_INQ cmsg type"); +} + +static void process_one_client(int fd, int unixfd) +{ + unsigned int tcp_inq; + size_t expect_len; + char msg_buf[4096]; + char buf[4096]; + char tmp[16]; + struct iovec iov =3D { + .iov_base =3D buf, + .iov_len =3D 1, + }; + struct msghdr msg =3D { + .msg_iov =3D &iov, + .msg_iovlen =3D 1, + .msg_control =3D msg_buf, + .msg_controllen =3D sizeof(msg_buf), + }; + ssize_t ret, tot; + + ret =3D write(unixfd, "xmit", 4); + assert(ret =3D=3D 4); + + ret =3D read(unixfd, &expect_len, sizeof(expect_len)); + assert(ret =3D=3D (ssize_t)sizeof(expect_len)); + + if (expect_len > sizeof(buf)) + xerror("expect len %zu exceeds buffer size", expect_len); + + for (;;) { + struct timespec req; + unsigned int queued; + + ret =3D ioctl(fd, FIONREAD, &queued); + if (ret < 0) + die_perror("FIONREAD"); + if (queued > expect_len) + xerror("FIONREAD returned %u, but only %zu expected\n", + queued, expect_len); + if (queued =3D=3D expect_len) + break; + + req.tv_sec =3D 0; + req.tv_nsec =3D 1000 * 1000ul; + nanosleep(&req, NULL); + } + + /* read one byte, expect cmsg to return expected - 1 */ + ret =3D recvmsg(fd, &msg, 0); + if (ret < 0) + die_perror("recvmsg"); + + if (msg.msg_controllen =3D=3D 0) + xerror("msg_controllen is 0"); + + get_tcp_inq(&msg, &tcp_inq); + + assert((size_t)tcp_inq =3D=3D (expect_len - 1)); + + iov.iov_len =3D sizeof(buf); + ret =3D recvmsg(fd, &msg, 0); + if (ret < 0) + die_perror("recvmsg"); + + /* should have gotten exact remainder of all pending data */ + assert(ret =3D=3D (ssize_t)tcp_inq); + + /* should be 0, all drained */ + get_tcp_inq(&msg, &tcp_inq); + assert(tcp_inq =3D=3D 0); + + /* request a large swath of data. */ + ret =3D write(unixfd, "huge", 4); + assert(ret =3D=3D 4); + + ret =3D read(unixfd, &expect_len, sizeof(expect_len)); + assert(ret =3D=3D (ssize_t)sizeof(expect_len)); + + /* peer should send us a few mb of data */ + if (expect_len <=3D sizeof(buf)) + xerror("expect len %zu too small\n", expect_len); + + tot =3D 0; + do { + iov.iov_len =3D sizeof(buf); + ret =3D recvmsg(fd, &msg, 0); + if (ret < 0) + die_perror("recvmsg"); + + tot +=3D ret; + + get_tcp_inq(&msg, &tcp_inq); + + if (tcp_inq > expect_len - tot) + xerror("inq %d, remaining %d total_len %d\n", + tcp_inq, expect_len - tot, (int)expect_len); + + assert(tcp_inq <=3D expect_len - tot); + } while ((size_t)tot < expect_len); + + ret =3D write(unixfd, "shut", 4); + assert(ret =3D=3D 4); + + /* wait for hangup. Should have received one more byte of data. */ + ret =3D read(unixfd, tmp, sizeof(tmp)); + assert(ret =3D=3D 6); + assert(strncmp(tmp, "closed", 6) =3D=3D 0); + + sleep(1); + + iov.iov_len =3D 1; + ret =3D recvmsg(fd, &msg, 0); + if (ret < 0) + die_perror("recvmsg"); + assert(ret =3D=3D 1); + + get_tcp_inq(&msg, &tcp_inq); + + /* tcp_inq should be 1 due to received fin. */ + assert(tcp_inq =3D=3D 1); + + iov.iov_len =3D 1; + ret =3D recvmsg(fd, &msg, 0); + if (ret < 0) + die_perror("recvmsg"); + + /* expect EOF */ + assert(ret =3D=3D 0); + get_tcp_inq(&msg, &tcp_inq); + assert(tcp_inq =3D=3D 1); + + close(fd); +} + +static int xaccept(int s) +{ + int fd =3D accept(s, NULL, 0); + + if (fd < 0) + die_perror("accept"); + + return fd; +} + +static int server(int unixfd) +{ + int fd =3D -1, r, on =3D 1; + + switch (pf) { + case AF_INET: + fd =3D sock_listen_mptcp("127.0.0.1", "15432"); + break; + case AF_INET6: + fd =3D sock_listen_mptcp("::1", "15432"); + break; + default: + xerror("Unknown pf %d\n", pf); + break; + } + + r =3D write(unixfd, "conn", 4); + assert(r =3D=3D 4); + + alarm(15); + r =3D xaccept(fd); + + if (-1 =3D=3D setsockopt(r, IPPROTO_TCP, TCP_INQ, &on, sizeof(on))) + die_perror("setsockopt"); + + process_one_client(r, unixfd); + + return 0; +} + +static int client(int unixfd) +{ + int fd =3D -1; + + alarm(15); + + switch (pf) { + case AF_INET: + fd =3D sock_connect_mptcp("127.0.0.1", "15432", proto_tx); + break; + case AF_INET6: + fd =3D sock_connect_mptcp("::1", "15432", proto_tx); + break; + default: + xerror("Unknown pf %d\n", pf); + } + + connect_one_server(fd, unixfd); + + return 0; +} + +static void init_rng(void) +{ + int fd =3D open("/dev/urandom", O_RDONLY); + unsigned int foo; + + if (fd > 0) { + int ret =3D read(fd, &foo, sizeof(foo)); + + if (ret < 0) + srand(fd + foo); + close(fd); + } + + srand(foo); +} + +static pid_t xfork(void) +{ + pid_t p =3D fork(); + + if (p < 0) + die_perror("fork"); + else if (p =3D=3D 0) + init_rng(); + + return p; +} + +static int rcheck(int wstatus, const char *what) +{ + if (WIFEXITED(wstatus)) { + if (WEXITSTATUS(wstatus) =3D=3D 0) + return 0; + fprintf(stderr, "%s exited, status=3D%d\n", what, WEXITSTATUS(wstatus)); + return WEXITSTATUS(wstatus); + } else if (WIFSIGNALED(wstatus)) { + xerror("%s killed by signal %d\n", what, WTERMSIG(wstatus)); + } else if (WIFSTOPPED(wstatus)) { + xerror("%s stopped by signal %d\n", what, WSTOPSIG(wstatus)); + } + + return 111; +} + +int main(int argc, char *argv[]) +{ + int e1, e2, wstatus; + pid_t s, c, ret; + int unixfds[2]; + + parse_opts(argc, argv); + + e1 =3D socketpair(AF_UNIX, SOCK_DGRAM, 0, unixfds); + if (e1 < 0) + die_perror("pipe"); + + s =3D xfork(); + if (s =3D=3D 0) + return server(unixfds[1]); + + close(unixfds[1]); + + /* wait until server bound a socket */ + e1 =3D read(unixfds[0], &e1, 4); + assert(e1 =3D=3D 4); + + c =3D xfork(); + if (c =3D=3D 0) + return client(unixfds[0]); + + close(unixfds[0]); + + ret =3D waitpid(s, &wstatus, 0); + if (ret =3D=3D -1) + die_perror("waitpid"); + e1 =3D rcheck(wstatus, "server"); + ret =3D waitpid(c, &wstatus, 0); + if (ret =3D=3D -1) + die_perror("waitpid"); + e2 =3D rcheck(wstatus, "client"); + + return e1 ? e1 : e2; +} diff --git a/tools/testing/selftests/net/mptcp/mptcp_sockopt.sh b/tools/tes= ting/selftests/net/mptcp/mptcp_sockopt.sh index c8c364369599..0879da915014 100755 --- a/tools/testing/selftests/net/mptcp/mptcp_sockopt.sh +++ b/tools/testing/selftests/net/mptcp/mptcp_sockopt.sh @@ -279,6 +279,45 @@ run_tests() fi } =20 +do_tcpinq_test() +{ + ip netns exec "$ns1" ./mptcp_inq "$@" + lret=3D$? + if [ $lret -ne 0 ];then + ret=3D$lret + echo "FAIL: mptcp_inq $@" 1>&2 + return $lret + fi + + echo "PASS: TCP_INQ cmsg/ioctl $@" + return $lret +} + +do_tcpinq_tests() +{ + local lret=3D0 + + ip netns exec "$ns1" iptables -F + ip netns exec "$ns1" ip6tables -F + + for args in "-t tcp" "-r tcp"; do + do_tcpinq_test $args + lret=3D$? + if [ $lret -ne 0 ] ; then + return $lret + fi + do_tcpinq_test -6 $args + lret=3D$? + if [ $lret -ne 0 ] ; then + return $lret + fi + done + + do_tcpinq_test -r tcp -t tcp + + return $? +} + sin=3D$(mktemp) sout=3D$(mktemp) cin=3D$(mktemp) @@ -300,4 +339,5 @@ if [ $ret -eq 0 ];then echo "PASS: SOL_MPTCP getsockopt has expected information" fi =20 +do_tcpinq_tests exit $ret --=20 2.32.0