Skip to content

Commit 1c66bba

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 1c66bba

File tree

2 files changed

+259
-1
lines changed

2 files changed

+259
-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: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
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 void print_info_msg(struct mptcp_info *info)
130+
{
131+
printf("Token & Flags\n");
132+
printf("token: %x\n", info->mptcpi_token);
133+
printf("flags: %x\n", info->mptcpi_flags);
134+
printf("csum_enabled: %u\n", info->mptcpi_csum_enabled);
135+
136+
printf("\nBasic Info\n");
137+
printf("subflows: %-5u subflows_max: %u\n",
138+
info->mptcpi_subflows,
139+
info->mptcpi_subflows_max);
140+
printf("local_addr_used: %-5u local_addr_max: %u\n",
141+
info->mptcpi_local_addr_used,
142+
info->mptcpi_local_addr_max);
143+
printf("add_addr_signal: %-5u add_addr_accepted: %u\n",
144+
info->mptcpi_add_addr_signal,
145+
info->mptcpi_add_addr_accepted);
146+
printf("add_addr_signal_max: %-5u add_addr_accepted_max: %u\n",
147+
info->mptcpi_add_addr_signal_max,
148+
info->mptcpi_add_addr_accepted_max);
149+
150+
printf("\nTransmission Info\n");
151+
printf("write_seq: %llx\n", info->mptcpi_write_seq);
152+
printf("snd_una: %llx\n", info->mptcpi_snd_una);
153+
printf("rcv_nxt: %llx\n", info->mptcpi_rcv_nxt);
154+
printf("retransmits: %u\n", info->mptcpi_retransmits);
155+
printf("retransmit bytes: %llu\n", info->mptcpi_bytes_retrans);
156+
printf("bytes_sent: %llu\n", info->mptcpi_bytes_sent);
157+
printf("bytes_received: %llu\n", info->mptcpi_bytes_received);
158+
printf("bytes_acked: %llu\n", info->mptcpi_bytes_acked);
159+
}
160+
161+
static void parse_nlmsg(struct nlmsghdr *nlh)
162+
{
163+
struct inet_diag_msg *r = NLMSG_DATA(nlh);
164+
struct rtattr *tb[INET_DIAG_MAX + 1];
165+
struct mptcp_info *info;
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+
info = RTA_DATA(tb[INET_DIAG_INFO]);
173+
print_info_msg(info);
174+
}
175+
}
176+
177+
static void recv_nlmsg(int fd, struct nlmsghdr *nlh)
178+
{
179+
char rcv_buff[8192];
180+
struct sockaddr_nl rcv_nladdr = {
181+
.nl_family = AF_NETLINK
182+
};
183+
struct iovec rcv_iov = {
184+
.iov_base = rcv_buff,
185+
.iov_len = sizeof(rcv_buff)
186+
};
187+
struct msghdr rcv_msg = {
188+
.msg_name = &rcv_nladdr,
189+
.msg_namelen = sizeof(rcv_nladdr),
190+
.msg_iov = &rcv_iov,
191+
.msg_iovlen = 1
192+
};
193+
int len;
194+
195+
len = recvmsg(fd, &rcv_msg, 0);
196+
nlh = (struct nlmsghdr *)rcv_buff;
197+
198+
while (NLMSG_OK(nlh, len)) {
199+
if (nlh->nlmsg_type == NLMSG_DONE) {
200+
printf("NLMSG_DONE\n");
201+
break;
202+
} else if (nlh->nlmsg_type == NLMSG_ERROR) {
203+
struct nlmsgerr *err;
204+
205+
err = (struct nlmsgerr *)NLMSG_DATA(nlh);
206+
printf("Error %d:%s\n",
207+
-(err->error), strerror(-(err->error)));
208+
break;
209+
}
210+
parse_nlmsg(nlh);
211+
nlh = NLMSG_NEXT(nlh, len);
212+
}
213+
}
214+
215+
static void get_mptcpinfo(__u32 token)
216+
{
217+
struct nlmsghdr *nlh = NULL;
218+
int fd;
219+
220+
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
221+
if (fd < 0)
222+
die_perror("Netlink socket");
223+
224+
send_query(fd, token);
225+
recv_nlmsg(fd, nlh);
226+
227+
close(fd);
228+
}
229+
230+
static void parse_opts(int argc, char **argv, __u32 *target_token)
231+
{
232+
int c;
233+
234+
while ((c = getopt(argc, argv, "ht:")) != -1) {
235+
switch (c) {
236+
case 'h':
237+
die_usage(0);
238+
break;
239+
case 't':
240+
sscanf(optarg, "%x", target_token);
241+
break;
242+
default:
243+
die_usage(1);
244+
break;
245+
}
246+
}
247+
}
248+
249+
int main(int argc, char *argv[])
250+
{
251+
__u32 target_token;
252+
253+
parse_opts(argc, argv, &target_token);
254+
get_mptcpinfo(target_token);
255+
256+
return 0;
257+
}
258+

0 commit comments

Comments
 (0)