Skip to content

Commit eb39b19

Browse files
Dwyane-Yanintel-lab-lkp
authored andcommitted
selftests: mptcp: Add a tool to get specific msk_info
This patch enables the retrieval of the mptcp_info structure corresponding to a specified MPTCP socket (msk). When multiple MPTCP connections are present, specific information can be obtained for a given connection through the 'mptcp_diag_dump_one' by using the 'token' associated with the msk. Signed-off-by: Gang Yan <[email protected]>
1 parent bacd459 commit eb39b19

File tree

2 files changed

+236
-1
lines changed

2 files changed

+236
-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

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
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+
#ifndef MPTCP_INFO
23+
struct mptcp_info {
24+
__u8 mptcpi_subflows;
25+
__u8 mptcpi_add_addr_signal;
26+
__u8 mptcpi_add_addr_accepted;
27+
__u8 mptcpi_subflows_max;
28+
__u8 mptcpi_add_addr_signal_max;
29+
__u8 mptcpi_add_addr_accepted_max;
30+
__u32 mptcpi_flags;
31+
__u32 mptcpi_token;
32+
__u64 mptcpi_write_seq;
33+
__u64 mptcpi_snd_una;
34+
__u64 mptcpi_rcv_nxt;
35+
__u8 mptcpi_local_addr_used;
36+
__u8 mptcpi_local_addr_max;
37+
__u8 mptcpi_csum_enabled;
38+
__u32 mptcpi_retransmits;
39+
__u64 mptcpi_bytes_retrans;
40+
__u64 mptcpi_bytes_sent;
41+
__u64 mptcpi_bytes_received;
42+
__u64 mptcpi_bytes_acked;
43+
};
44+
45+
#define MPTCP_INFO 1
46+
#endif
47+
48+
static void die_perror(const char *msg)
49+
{
50+
perror(msg);
51+
exit(1);
52+
}
53+
54+
static void die_usage(int r)
55+
{
56+
fprintf(stderr, "Usage: mptcp_diag -t\n");
57+
exit(r);
58+
}
59+
60+
static void send_query(int fd, __u32 token)
61+
{
62+
struct sockaddr_nl nladdr = {
63+
.nl_family = AF_NETLINK
64+
};
65+
struct {
66+
struct nlmsghdr nlh;
67+
struct inet_diag_req_v2 r;
68+
} req = {
69+
.nlh = {
70+
.nlmsg_len = sizeof(req),
71+
.nlmsg_type = SOCK_DIAG_BY_FAMILY,
72+
.nlmsg_flags = NLM_F_REQUEST
73+
},
74+
.r = {
75+
.sdiag_family = AF_INET,
76+
.sdiag_protocol = IPPROTO_MPTCP,
77+
.id.idiag_cookie[0] = token,
78+
}
79+
};
80+
struct rtattr rta_proto;
81+
struct iovec iov[6];
82+
int iovlen = 1;
83+
__u32 proto;
84+
85+
req.r.idiag_ext |= (1 << (INET_DIAG_INFO - 1));
86+
proto = IPPROTO_MPTCP;
87+
rta_proto.rta_type = INET_DIAG_REQ_PROTOCOL;
88+
rta_proto.rta_len = RTA_LENGTH(sizeof(proto));
89+
90+
iov[0] = (struct iovec) {
91+
.iov_base = &req,
92+
.iov_len = sizeof(req)
93+
};
94+
iov[iovlen] = (struct iovec){ &rta_proto, sizeof(rta_proto)};
95+
iov[iovlen + 1] = (struct iovec){ &proto, sizeof(proto)};
96+
req.nlh.nlmsg_len += RTA_LENGTH(sizeof(proto));
97+
iovlen += 2;
98+
struct msghdr msg = {
99+
.msg_name = &nladdr,
100+
.msg_namelen = sizeof(nladdr),
101+
.msg_iov = iov,
102+
.msg_iovlen = iovlen
103+
};
104+
105+
for (;;) {
106+
if (sendmsg(fd, &msg, 0) < 0) {
107+
if (errno == EINTR)
108+
continue;
109+
die_perror("sendmsg");
110+
}
111+
break;
112+
}
113+
}
114+
115+
static void parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
116+
int len, unsigned short flags)
117+
{
118+
unsigned short type;
119+
120+
memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
121+
while (RTA_OK(rta, len)) {
122+
type = rta->rta_type & ~flags;
123+
if ((type <= max) && (!tb[type]))
124+
tb[type] = rta;
125+
rta = RTA_NEXT(rta, len);
126+
}
127+
}
128+
129+
static struct mptcp_info *parse_nlmsg(struct nlmsghdr *nlh)
130+
{
131+
struct inet_diag_msg *r = NLMSG_DATA(nlh);
132+
struct rtattr *tb[INET_DIAG_MAX+1];
133+
struct mptcp_info *info;
134+
135+
parse_rtattr_flags(tb, INET_DIAG_MAX, (struct rtattr *)(r+1),
136+
nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)),
137+
NLA_F_NESTED);
138+
139+
if (tb[INET_DIAG_INFO]) {
140+
info = RTA_DATA(tb[INET_DIAG_INFO]);
141+
return info;
142+
}
143+
144+
die_perror("Has no INET_DIAG_INFO");
145+
return info;
146+
}
147+
148+
static struct mptcp_info *recv_nlmsg(int fd, struct nlmsghdr *nlh)
149+
{
150+
char rcv_buff[8192];
151+
struct sockaddr_nl rcv_nladdr = {
152+
.nl_family = AF_NETLINK
153+
};
154+
struct iovec rcv_iov = {
155+
.iov_base = rcv_buff,
156+
.iov_len = sizeof(rcv_buff)
157+
};
158+
struct msghdr rcv_msg = {
159+
.msg_name = &rcv_nladdr,
160+
.msg_namelen = sizeof(rcv_nladdr),
161+
.msg_iov = &rcv_iov,
162+
.msg_iovlen = 1
163+
};
164+
struct mptcp_info *info = NULL;
165+
int len;
166+
167+
len = recvmsg(fd, &rcv_msg, 0);
168+
nlh = (struct nlmsghdr *)rcv_buff;
169+
170+
while (NLMSG_OK(nlh, len)) {
171+
if (nlh->nlmsg_type == NLMSG_DONE) {
172+
printf("NLMSG_DONE\n");
173+
break;
174+
} else if (nlh->nlmsg_type == NLMSG_ERROR) {
175+
struct nlmsgerr *err;
176+
177+
err = (struct nlmsgerr *)NLMSG_DATA(nlh);
178+
printf("Error %d:%s\n", -(err->error), strerror(-(err->error)));
179+
break;
180+
}
181+
info = parse_nlmsg(nlh);
182+
nlh = NLMSG_NEXT(nlh, len);
183+
}
184+
close(fd);
185+
return info;
186+
}
187+
188+
static struct mptcp_info *get_mptcpinfo(__u32 token)
189+
{
190+
struct nlmsghdr *nlh = NULL;
191+
int fd;
192+
193+
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
194+
if (fd < 0)
195+
die_perror("Netlink socket");
196+
197+
send_query(fd, token);
198+
return recv_nlmsg(fd, nlh);
199+
}
200+
201+
static void parse_opts(int argc, char **argv, __u32 *target_token)
202+
{
203+
int c;
204+
205+
while ((c = getopt(argc, argv, "ht:")) != -1) {
206+
switch (c) {
207+
case 'h':
208+
die_usage(1);
209+
break;
210+
case 't':
211+
sscanf(optarg, "%x", target_token);
212+
break;
213+
default:
214+
die_usage(1);
215+
break;
216+
}
217+
}
218+
}
219+
220+
int main(int argc, char *argv[])
221+
{
222+
struct mptcp_info *info = NULL;
223+
__u32 target_token;
224+
225+
parse_opts(argc, argv, &target_token);
226+
info = get_mptcpinfo(target_token);
227+
228+
if (info)
229+
printf("token:%x\n", info->mptcpi_token);
230+
else
231+
perror("No Such msk using this token!\n");
232+
233+
return 0;
234+
}
235+

0 commit comments

Comments
 (0)