Skip to content

Commit 3aa93e0

Browse files
committed
tgupdate: merge t/DO-NOT-MERGE-mptcp-enabled-by-default into t/upstream base
2 parents 862e6a3 + 445cb12 commit 3aa93e0

File tree

3 files changed

+299
-1
lines changed

3 files changed

+299
-1
lines changed

tools/testing/selftests/net/mptcp/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CFLAGS += -Wall -Wl,--no-as-needed -O2 -g -I$(top_srcdir)/usr/include $(KHDR_INC
77
TEST_PROGS := mptcp_connect.sh pm_netlink.sh mptcp_join.sh diag.sh \
88
simult_flows.sh mptcp_sockopt.sh userspace_pm.sh
99

10-
TEST_GEN_FILES = mptcp_connect pm_nl_ctl mptcp_sockopt mptcp_inq
10+
TEST_GEN_FILES = mptcp_connect pm_nl_ctl mptcp_sockopt mptcp_inq mptcp_diag
1111

1212
TEST_FILES := mptcp_lib.sh settings
1313

tools/testing/selftests/net/mptcp/diag.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,32 @@ chk_msk_cestab()
200200
"${expected}" "${msg}" ""
201201
}
202202

203+
chk_dump_one()
204+
{
205+
local ss_token
206+
local token
207+
local msg
208+
209+
ss_token="$(ss -inmHMN $ns | grep 'token:' |\
210+
head -n 1 |\
211+
sed 's/.*token:\([0-9a-f]*\).*/\1/')"
212+
213+
token="$(ip netns exec $ns ./mptcp_diag -t $ss_token |\
214+
awk -F':[ \t]+' '/^token/ {print $2}')"
215+
216+
msg="....chk dump_one"
217+
218+
mptcp_lib_print_title "$msg"
219+
if [ -n "$ss_token" ] && [ "$ss_token" = "$token" ]; then
220+
mptcp_lib_pr_ok
221+
mptcp_lib_result_pass "${msg}"
222+
else
223+
mptcp_lib_pr_fail "expected $ss_token found $token"
224+
mptcp_lib_result_fail "${msg}"
225+
ret=${KSFT_FAIL}
226+
fi
227+
}
228+
203229
msk_info_get_value()
204230
{
205231
local port="${1}"
@@ -290,6 +316,7 @@ chk_msk_remote_key_nr 2 "....chk remote_key"
290316
chk_msk_fallback_nr 0 "....chk no fallback"
291317
chk_msk_inuse 2
292318
chk_msk_cestab 2
319+
chk_dump_one
293320
flush_pids
294321

295322
chk_msk_inuse 0 "2->0"
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025, Kylin Software */
3+
4+
#include <linux/sock_diag.h>
5+
#include <linux/rtnetlink.h>
6+
#include <linux/inet_diag.h>
7+
#include <linux/netlink.h>
8+
#include <sys/socket.h>
9+
#include <netinet/in.h>
10+
#include <linux/tcp.h>
11+
12+
#include <unistd.h>
13+
#include <stdlib.h>
14+
#include <string.h>
15+
#include <errno.h>
16+
#include <stdio.h>
17+
18+
#ifndef IPPROTO_MPTCP
19+
#define IPPROTO_MPTCP 262
20+
#endif
21+
22+
struct mptcp_info {
23+
__u8 mptcpi_subflows;
24+
__u8 mptcpi_add_addr_signal;
25+
__u8 mptcpi_add_addr_accepted;
26+
__u8 mptcpi_subflows_max;
27+
__u8 mptcpi_add_addr_signal_max;
28+
__u8 mptcpi_add_addr_accepted_max;
29+
__u32 mptcpi_flags;
30+
__u32 mptcpi_token;
31+
__u64 mptcpi_write_seq;
32+
__u64 mptcpi_snd_una;
33+
__u64 mptcpi_rcv_nxt;
34+
__u8 mptcpi_local_addr_used;
35+
__u8 mptcpi_local_addr_max;
36+
__u8 mptcpi_csum_enabled;
37+
__u32 mptcpi_retransmits;
38+
__u64 mptcpi_bytes_retrans;
39+
__u64 mptcpi_bytes_sent;
40+
__u64 mptcpi_bytes_received;
41+
__u64 mptcpi_bytes_acked;
42+
__u8 mptcpi_subflows_total;
43+
__u8 reserved[3];
44+
__u32 mptcpi_last_data_sent;
45+
__u32 mptcpi_last_data_recv;
46+
__u32 mptcpi_last_ack_recv;
47+
};
48+
49+
static void die_perror(const char *msg)
50+
{
51+
perror(msg);
52+
exit(1);
53+
}
54+
55+
static void die_usage(int r)
56+
{
57+
fprintf(stderr, "Usage: mptcp_diag -t\n");
58+
exit(r);
59+
}
60+
61+
static void send_query(int fd, __u32 token)
62+
{
63+
struct sockaddr_nl nladdr = {
64+
.nl_family = AF_NETLINK
65+
};
66+
struct {
67+
struct nlmsghdr nlh;
68+
struct inet_diag_req_v2 r;
69+
} req = {
70+
.nlh = {
71+
.nlmsg_len = sizeof(req),
72+
.nlmsg_type = SOCK_DIAG_BY_FAMILY,
73+
.nlmsg_flags = NLM_F_REQUEST
74+
},
75+
.r = {
76+
.sdiag_family = AF_INET,
77+
.sdiag_protocol = IPPROTO_MPTCP,
78+
.id.idiag_cookie[0] = token,
79+
}
80+
};
81+
struct rtattr rta_proto;
82+
struct iovec iov[6];
83+
int iovlen = 1;
84+
__u32 proto;
85+
86+
req.r.idiag_ext |= (1 << (INET_DIAG_INFO - 1));
87+
proto = IPPROTO_MPTCP;
88+
rta_proto.rta_type = INET_DIAG_REQ_PROTOCOL;
89+
rta_proto.rta_len = RTA_LENGTH(sizeof(proto));
90+
91+
iov[0] = (struct iovec) {
92+
.iov_base = &req,
93+
.iov_len = sizeof(req)
94+
};
95+
iov[iovlen] = (struct iovec){ &rta_proto, sizeof(rta_proto)};
96+
iov[iovlen + 1] = (struct iovec){ &proto, sizeof(proto)};
97+
req.nlh.nlmsg_len += RTA_LENGTH(sizeof(proto));
98+
iovlen += 2;
99+
struct msghdr msg = {
100+
.msg_name = &nladdr,
101+
.msg_namelen = sizeof(nladdr),
102+
.msg_iov = iov,
103+
.msg_iovlen = iovlen
104+
};
105+
106+
for (;;) {
107+
if (sendmsg(fd, &msg, 0) < 0) {
108+
if (errno == EINTR)
109+
continue;
110+
die_perror("sendmsg");
111+
}
112+
break;
113+
}
114+
}
115+
116+
static void parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
117+
int len, unsigned short flags)
118+
{
119+
unsigned short type;
120+
121+
memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
122+
while (RTA_OK(rta, len)) {
123+
type = rta->rta_type & ~flags;
124+
if (type <= max && !tb[type])
125+
tb[type] = rta;
126+
rta = RTA_NEXT(rta, len);
127+
}
128+
}
129+
130+
static void print_info_msg(struct mptcp_info *info)
131+
{
132+
printf("Token & Flags\n");
133+
printf("token: %x\n", info->mptcpi_token);
134+
printf("flags: %x\n", info->mptcpi_flags);
135+
printf("csum_enabled: %u\n", info->mptcpi_csum_enabled);
136+
137+
printf("\nBasic Info\n");
138+
printf("subflows: %u\n", info->mptcpi_subflows);
139+
printf("subflows_max: %u\n", info->mptcpi_subflows_max);
140+
printf("subflows_total: %u\n", info->mptcpi_subflows_total);
141+
printf("local_addr_used: %u\n", info->mptcpi_local_addr_used);
142+
printf("local_addr_max: %u\n", info->mptcpi_local_addr_max);
143+
printf("add_addr_signal: %u\n", info->mptcpi_add_addr_signal);
144+
printf("add_addr_accepted: %u\n", info->mptcpi_add_addr_accepted);
145+
printf("add_addr_signal_max: %u\n", info->mptcpi_add_addr_signal_max);
146+
printf("add_addr_accepted_max: %u\n", info->mptcpi_add_addr_accepted_max);
147+
148+
printf("\nTransmission Info\n");
149+
printf("write_seq: %llx\n", info->mptcpi_write_seq);
150+
printf("snd_una: %llx\n", info->mptcpi_snd_una);
151+
printf("rcv_nxt: %llx\n", info->mptcpi_rcv_nxt);
152+
printf("last_data_sent: %x\n", info->mptcpi_last_data_sent);
153+
printf("last_data_recv: %x\n", info->mptcpi_last_data_recv);
154+
printf("last_ack_recv: %x\n", info->mptcpi_last_ack_recv);
155+
printf("retransmits: %u\n", info->mptcpi_retransmits);
156+
printf("retransmit bytes: %llu\n", info->mptcpi_bytes_retrans);
157+
printf("bytes_sent: %llu\n", info->mptcpi_bytes_sent);
158+
printf("bytes_received: %llu\n", info->mptcpi_bytes_received);
159+
printf("bytes_acked: %llu\n", info->mptcpi_bytes_acked);
160+
}
161+
162+
static void parse_nlmsg(struct nlmsghdr *nlh)
163+
{
164+
struct inet_diag_msg *r = NLMSG_DATA(nlh);
165+
struct rtattr *tb[INET_DIAG_MAX + 1];
166+
167+
parse_rtattr_flags(tb, INET_DIAG_MAX, (struct rtattr *)(r + 1),
168+
nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)),
169+
NLA_F_NESTED);
170+
171+
if (tb[INET_DIAG_INFO]) {
172+
int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]);
173+
struct mptcp_info *info;
174+
175+
/* workaround fort older kernels with less fields */
176+
if (len < sizeof(*info)) {
177+
info = alloca(sizeof(*info));
178+
memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len);
179+
memset((char *)info + len, 0, sizeof(*info) - len);
180+
} else {
181+
info = RTA_DATA(tb[INET_DIAG_INFO]);
182+
}
183+
print_info_msg(info);
184+
}
185+
}
186+
187+
static void recv_nlmsg(int fd, struct nlmsghdr *nlh)
188+
{
189+
char rcv_buff[8192];
190+
struct sockaddr_nl rcv_nladdr = {
191+
.nl_family = AF_NETLINK
192+
};
193+
struct iovec rcv_iov = {
194+
.iov_base = rcv_buff,
195+
.iov_len = sizeof(rcv_buff)
196+
};
197+
struct msghdr rcv_msg = {
198+
.msg_name = &rcv_nladdr,
199+
.msg_namelen = sizeof(rcv_nladdr),
200+
.msg_iov = &rcv_iov,
201+
.msg_iovlen = 1
202+
};
203+
int len;
204+
205+
len = recvmsg(fd, &rcv_msg, 0);
206+
nlh = (struct nlmsghdr *)rcv_buff;
207+
208+
while (NLMSG_OK(nlh, len)) {
209+
if (nlh->nlmsg_type == NLMSG_DONE) {
210+
printf("NLMSG_DONE\n");
211+
break;
212+
} else if (nlh->nlmsg_type == NLMSG_ERROR) {
213+
struct nlmsgerr *err;
214+
215+
err = (struct nlmsgerr *)NLMSG_DATA(nlh);
216+
printf("Error %d:%s\n",
217+
-(err->error), strerror(-(err->error)));
218+
break;
219+
}
220+
parse_nlmsg(nlh);
221+
nlh = NLMSG_NEXT(nlh, len);
222+
}
223+
}
224+
225+
static void get_mptcpinfo(__u32 token)
226+
{
227+
struct nlmsghdr *nlh = NULL;
228+
int fd;
229+
230+
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
231+
if (fd < 0)
232+
die_perror("Netlink socket");
233+
234+
send_query(fd, token);
235+
recv_nlmsg(fd, nlh);
236+
237+
close(fd);
238+
}
239+
240+
static void parse_opts(int argc, char **argv, __u32 *target_token)
241+
{
242+
int c;
243+
244+
if (argc < 2)
245+
die_usage(1);
246+
247+
while ((c = getopt(argc, argv, "ht:")) != -1) {
248+
switch (c) {
249+
case 'h':
250+
die_usage(0);
251+
break;
252+
case 't':
253+
sscanf(optarg, "%x", target_token);
254+
break;
255+
default:
256+
die_usage(1);
257+
break;
258+
}
259+
}
260+
}
261+
262+
int main(int argc, char *argv[])
263+
{
264+
__u32 target_token;
265+
266+
parse_opts(argc, argv, &target_token);
267+
get_mptcpinfo(target_token);
268+
269+
return 0;
270+
}
271+

0 commit comments

Comments
 (0)