mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-12-27 12:21:22 -05:00
selftests: mptcp: add helpers to get subflow_info
This patch adds 'get_subflow_info' in 'mptcp_diag', which can check whether a TCP connection is an MPTCP subflow based on the "INET_ULP_INFO_MPTCP" with tcp_diag method. The helper 'print_subflow_info' in 'mptcp_diag' can print the subflow_filed of an MPTCP subflow for further checking the 'subflow_info' through inet_diag method. The example of the whole output should be: $ ./mptcp_diag -s "127.0.0.1:10000 127.0.0.1:38984" 127.0.0.1:10000 -> 127.0.0.1:38984 It's a mptcp subflow, the subflow info: flags:Mec token:0000(id:0)/4278e77e(id:0) seq:9288466187236176036 \ sfseq:1 ssnoff:2317083055 maplen:215 Co-developed-by: Geliang Tang <geliang@kernel.org> Signed-off-by: Geliang Tang <geliang@kernel.org> Signed-off-by: Gang Yan <yangang@kylinos.cn> Reviewed-by: Matthieu Baerts (NGI0) <matttbe@kernel.org> Signed-off-by: Matthieu Baerts (NGI0) <matttbe@kernel.org> Link: https://patch.msgid.link/20250502-net-next-mptcp-sft-inc-cover-v1-6-68eec95898fb@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <linux/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
@@ -19,8 +20,13 @@
|
||||
#define IPPROTO_MPTCP 262
|
||||
#endif
|
||||
|
||||
#define parse_rtattr_nested(tb, max, rta) \
|
||||
(parse_rtattr_flags((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta), \
|
||||
NLA_F_NESTED))
|
||||
|
||||
struct params {
|
||||
__u32 target_token;
|
||||
char subflow_addrs[1024];
|
||||
};
|
||||
|
||||
struct mptcp_info {
|
||||
@@ -50,6 +56,37 @@ struct mptcp_info {
|
||||
__u32 mptcpi_last_ack_recv;
|
||||
};
|
||||
|
||||
enum {
|
||||
MPTCP_SUBFLOW_ATTR_UNSPEC,
|
||||
MPTCP_SUBFLOW_ATTR_TOKEN_REM,
|
||||
MPTCP_SUBFLOW_ATTR_TOKEN_LOC,
|
||||
MPTCP_SUBFLOW_ATTR_RELWRITE_SEQ,
|
||||
MPTCP_SUBFLOW_ATTR_MAP_SEQ,
|
||||
MPTCP_SUBFLOW_ATTR_MAP_SFSEQ,
|
||||
MPTCP_SUBFLOW_ATTR_SSN_OFFSET,
|
||||
MPTCP_SUBFLOW_ATTR_MAP_DATALEN,
|
||||
MPTCP_SUBFLOW_ATTR_FLAGS,
|
||||
MPTCP_SUBFLOW_ATTR_ID_REM,
|
||||
MPTCP_SUBFLOW_ATTR_ID_LOC,
|
||||
MPTCP_SUBFLOW_ATTR_PAD,
|
||||
|
||||
__MPTCP_SUBFLOW_ATTR_MAX
|
||||
};
|
||||
|
||||
#define MPTCP_SUBFLOW_ATTR_MAX (__MPTCP_SUBFLOW_ATTR_MAX - 1)
|
||||
|
||||
#define MPTCP_SUBFLOW_FLAG_MCAP_REM _BITUL(0)
|
||||
#define MPTCP_SUBFLOW_FLAG_MCAP_LOC _BITUL(1)
|
||||
#define MPTCP_SUBFLOW_FLAG_JOIN_REM _BITUL(2)
|
||||
#define MPTCP_SUBFLOW_FLAG_JOIN_LOC _BITUL(3)
|
||||
#define MPTCP_SUBFLOW_FLAG_BKUP_REM _BITUL(4)
|
||||
#define MPTCP_SUBFLOW_FLAG_BKUP_LOC _BITUL(5)
|
||||
#define MPTCP_SUBFLOW_FLAG_FULLY_ESTABLISHED _BITUL(6)
|
||||
#define MPTCP_SUBFLOW_FLAG_CONNECTED _BITUL(7)
|
||||
#define MPTCP_SUBFLOW_FLAG_MAPVALID _BITUL(8)
|
||||
|
||||
#define rta_getattr(type, value) (*(type *)RTA_DATA(value))
|
||||
|
||||
static void die_perror(const char *msg)
|
||||
{
|
||||
perror(msg);
|
||||
@@ -58,7 +95,9 @@ static void die_perror(const char *msg)
|
||||
|
||||
static void die_usage(int r)
|
||||
{
|
||||
fprintf(stderr, "Usage: mptcp_diag -t\n");
|
||||
fprintf(stderr, "Usage:\n"
|
||||
"mptcp_diag -t <token>\n"
|
||||
"mptcp_diag -s \"<saddr>:<sport> <daddr>:<dport>\"\n");
|
||||
exit(r);
|
||||
}
|
||||
|
||||
@@ -159,6 +198,66 @@ static void print_info_msg(struct mptcp_info *info)
|
||||
printf("bytes_acked: %llu\n", info->mptcpi_bytes_acked);
|
||||
}
|
||||
|
||||
/*
|
||||
* 'print_subflow_info' is from 'mptcp_subflow_info'
|
||||
* which is a function in 'misc/ss.c' of iproute2.
|
||||
*/
|
||||
static void print_subflow_info(struct rtattr *tb[])
|
||||
{
|
||||
u_int32_t flags = 0;
|
||||
|
||||
printf("It's a mptcp subflow, the subflow info:\n");
|
||||
if (tb[MPTCP_SUBFLOW_ATTR_FLAGS]) {
|
||||
char caps[32 + 1] = { 0 }, *cap = &caps[0];
|
||||
|
||||
flags = rta_getattr(__u32, tb[MPTCP_SUBFLOW_ATTR_FLAGS]);
|
||||
|
||||
if (flags & MPTCP_SUBFLOW_FLAG_MCAP_REM)
|
||||
*cap++ = 'M';
|
||||
if (flags & MPTCP_SUBFLOW_FLAG_MCAP_LOC)
|
||||
*cap++ = 'm';
|
||||
if (flags & MPTCP_SUBFLOW_FLAG_JOIN_REM)
|
||||
*cap++ = 'J';
|
||||
if (flags & MPTCP_SUBFLOW_FLAG_JOIN_LOC)
|
||||
*cap++ = 'j';
|
||||
if (flags & MPTCP_SUBFLOW_FLAG_BKUP_REM)
|
||||
*cap++ = 'B';
|
||||
if (flags & MPTCP_SUBFLOW_FLAG_BKUP_LOC)
|
||||
*cap++ = 'b';
|
||||
if (flags & MPTCP_SUBFLOW_FLAG_FULLY_ESTABLISHED)
|
||||
*cap++ = 'e';
|
||||
if (flags & MPTCP_SUBFLOW_FLAG_CONNECTED)
|
||||
*cap++ = 'c';
|
||||
if (flags & MPTCP_SUBFLOW_FLAG_MAPVALID)
|
||||
*cap++ = 'v';
|
||||
|
||||
if (flags)
|
||||
printf(" flags:%s", caps);
|
||||
}
|
||||
if (tb[MPTCP_SUBFLOW_ATTR_TOKEN_REM] &&
|
||||
tb[MPTCP_SUBFLOW_ATTR_TOKEN_LOC] &&
|
||||
tb[MPTCP_SUBFLOW_ATTR_ID_REM] &&
|
||||
tb[MPTCP_SUBFLOW_ATTR_ID_LOC])
|
||||
printf(" token:%04x(id:%u)/%04x(id:%u)",
|
||||
rta_getattr(__u32, tb[MPTCP_SUBFLOW_ATTR_TOKEN_REM]),
|
||||
rta_getattr(__u8, tb[MPTCP_SUBFLOW_ATTR_ID_REM]),
|
||||
rta_getattr(__u32, tb[MPTCP_SUBFLOW_ATTR_TOKEN_LOC]),
|
||||
rta_getattr(__u8, tb[MPTCP_SUBFLOW_ATTR_ID_LOC]));
|
||||
if (tb[MPTCP_SUBFLOW_ATTR_MAP_SEQ])
|
||||
printf(" seq:%llu",
|
||||
rta_getattr(__u64, tb[MPTCP_SUBFLOW_ATTR_MAP_SEQ]));
|
||||
if (tb[MPTCP_SUBFLOW_ATTR_MAP_SFSEQ])
|
||||
printf(" sfseq:%u",
|
||||
rta_getattr(__u32, tb[MPTCP_SUBFLOW_ATTR_MAP_SFSEQ]));
|
||||
if (tb[MPTCP_SUBFLOW_ATTR_SSN_OFFSET])
|
||||
printf(" ssnoff:%u",
|
||||
rta_getattr(__u32, tb[MPTCP_SUBFLOW_ATTR_SSN_OFFSET]));
|
||||
if (tb[MPTCP_SUBFLOW_ATTR_MAP_DATALEN])
|
||||
printf(" maplen:%u",
|
||||
rta_getattr(__u32, tb[MPTCP_SUBFLOW_ATTR_MAP_DATALEN]));
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void parse_nlmsg(struct nlmsghdr *nlh, __u32 proto)
|
||||
{
|
||||
struct inet_diag_msg *r = NLMSG_DATA(nlh);
|
||||
@@ -182,6 +281,22 @@ static void parse_nlmsg(struct nlmsghdr *nlh, __u32 proto)
|
||||
}
|
||||
print_info_msg(info);
|
||||
}
|
||||
if (proto == IPPROTO_TCP && tb[INET_DIAG_ULP_INFO]) {
|
||||
struct rtattr *ulpinfo[INET_ULP_INFO_MAX + 1] = { 0 };
|
||||
|
||||
parse_rtattr_nested(ulpinfo, INET_ULP_INFO_MAX,
|
||||
tb[INET_DIAG_ULP_INFO]);
|
||||
|
||||
if (ulpinfo[INET_ULP_INFO_MPTCP]) {
|
||||
struct rtattr *sfinfo[MPTCP_SUBFLOW_ATTR_MAX + 1] = { 0 };
|
||||
|
||||
parse_rtattr_nested(sfinfo, MPTCP_SUBFLOW_ATTR_MAX,
|
||||
ulpinfo[INET_ULP_INFO_MPTCP]);
|
||||
print_subflow_info(sfinfo);
|
||||
} else {
|
||||
printf("It's a normal TCP!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void recv_nlmsg(int fd, __u32 proto)
|
||||
@@ -244,6 +359,39 @@ static void get_mptcpinfo(__u32 token)
|
||||
close(fd);
|
||||
}
|
||||
|
||||
static void get_subflow_info(char *subflow_addrs)
|
||||
{
|
||||
struct inet_diag_req_v2 r = {
|
||||
.sdiag_family = AF_INET,
|
||||
.sdiag_protocol = IPPROTO_TCP,
|
||||
.idiag_ext = 1 << (INET_DIAG_INFO - 1),
|
||||
.id.idiag_cookie[0] = INET_DIAG_NOCOOKIE,
|
||||
.id.idiag_cookie[1] = INET_DIAG_NOCOOKIE,
|
||||
};
|
||||
char saddr[64], daddr[64];
|
||||
int sport, dport;
|
||||
int ret;
|
||||
int fd;
|
||||
|
||||
ret = sscanf(subflow_addrs, "%[^:]:%d %[^:]:%d", saddr, &sport, daddr, &dport);
|
||||
if (ret != 4)
|
||||
die_perror("IP PORT Pairs has style problems!");
|
||||
|
||||
printf("%s:%d -> %s:%d\n", saddr, sport, daddr, dport);
|
||||
|
||||
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
|
||||
if (fd < 0)
|
||||
die_perror("Netlink socket");
|
||||
|
||||
r.id.idiag_sport = htons(sport);
|
||||
r.id.idiag_dport = htons(dport);
|
||||
|
||||
inet_pton(AF_INET, saddr, &r.id.idiag_src);
|
||||
inet_pton(AF_INET, daddr, &r.id.idiag_dst);
|
||||
send_query(fd, &r, IPPROTO_TCP);
|
||||
recv_nlmsg(fd, IPPROTO_TCP);
|
||||
}
|
||||
|
||||
static void parse_opts(int argc, char **argv, struct params *p)
|
||||
{
|
||||
int c;
|
||||
@@ -251,7 +399,7 @@ static void parse_opts(int argc, char **argv, struct params *p)
|
||||
if (argc < 2)
|
||||
die_usage(1);
|
||||
|
||||
while ((c = getopt(argc, argv, "ht:")) != -1) {
|
||||
while ((c = getopt(argc, argv, "ht:s:")) != -1) {
|
||||
switch (c) {
|
||||
case 'h':
|
||||
die_usage(0);
|
||||
@@ -259,6 +407,10 @@ static void parse_opts(int argc, char **argv, struct params *p)
|
||||
case 't':
|
||||
sscanf(optarg, "%x", &p->target_token);
|
||||
break;
|
||||
case 's':
|
||||
strncpy(p->subflow_addrs, optarg,
|
||||
sizeof(p->subflow_addrs) - 1);
|
||||
break;
|
||||
default:
|
||||
die_usage(1);
|
||||
break;
|
||||
@@ -275,6 +427,9 @@ int main(int argc, char *argv[])
|
||||
if (p.target_token)
|
||||
get_mptcpinfo(p.target_token);
|
||||
|
||||
if (p.subflow_addrs[0] != '\0')
|
||||
get_subflow_info(p.subflow_addrs);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user