Skip to content

Commit

Permalink
selftests: mptcp: cover 'mptcp_diag_dump_one' in mptcp_sockopt
Browse files Browse the repository at this point in the history
Through code coverage analysis, it has been identified that the
'mptcp_diag_dump_one' function lacks test coverage on the testing front.

This patch introduces a function in mptcp_sockopt.c, which is built upon
the 'inet_diag' module, and integrates it into the 'mptcp_sockopt.sh' to
execute the 'mptcp_diag_dump_one' function when server bound a socket.

Closes: #524
Signed-off-by: Gang Yan <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
Gang Yan authored and Patchew Applier committed Jan 17, 2025
1 parent 49838af commit ee9495d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
76 changes: 75 additions & 1 deletion tools/testing/selftests/net/mptcp/mptcp_sockopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include <netinet/in.h>

#include <linux/tcp.h>
#include <linux/netlink.h>
#include <linux/inet_diag.h>
#include <linux/rtnetlink.h>
#include <linux/sock_diag.h>

static int pf = AF_INET;

Expand Down Expand Up @@ -250,18 +254,88 @@ static int sock_connect_mptcp(const char * const remoteaddr,
return sock;
}

static void send_query(int fd)
{
struct sockaddr_nl nladdr = {
.nl_family = AF_NETLINK
};
struct {
struct nlmsghdr nlh;
struct inet_diag_req_v2 r;
} req = {
.nlh = {
.nlmsg_len = sizeof(req),
.nlmsg_type = SOCK_DIAG_BY_FAMILY,
.nlmsg_flags = NLM_F_REQUEST | NLM_F_ATOMIC
},
.r = {
.sdiag_family = AF_INET,
.sdiag_protocol = IPPROTO_MPTCP,
}
};
struct rtattr rta_proto;
struct iovec iov[6];
int iovlen = 1;
__u32 proto;

proto = IPPROTO_MPTCP;
rta_proto.rta_type = INET_DIAG_REQ_PROTOCOL;
rta_proto.rta_len = RTA_LENGTH(sizeof(proto));

iov[0] = (struct iovec) {
.iov_base = &req,
.iov_len = sizeof(req)
};
iov[iovlen] = (struct iovec){ &rta_proto, sizeof(rta_proto)};
iov[iovlen+1] = (struct iovec){ &proto, sizeof(proto)};
req.nlh.nlmsg_len += RTA_LENGTH(sizeof(proto));
iovlen += 2;

struct msghdr msg = {
.msg_name = &nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = iov,
.msg_iovlen = iovlen
};

for (;;) {
if (sendmsg(fd, &msg, 0) < 0) {
if (errno == EINTR)
continue;
die_perror("sendmsg");
}
return;
}
}

void diag(void)
{
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);

if (fd < 0)
die_perror("Netlink socket");

send_query(fd);

close(fd);
}

static void parse_opts(int argc, char **argv)
{
int c;

while ((c = getopt(argc, argv, "h6")) != -1) {
while ((c = getopt(argc, argv, "h6d")) != -1) {
switch (c) {
case 'h':
die_usage(0);
break;
case '6':
pf = AF_INET6;
break;
case 'd':
diag();
exit(0);
break;
default:
die_usage(1);
break;
Expand Down
6 changes: 6 additions & 0 deletions tools/testing/selftests/net/mptcp/mptcp_sockopt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ do_mptcp_sockopt_tests()
mptcp_lib_result_pass "sockopt v6"
}

do_diag_tests()
{
ip netns exec "$ns_sbox" ./mptcp_sockopt -d
}

run_tests()
{
local listener_ns="$1"
Expand Down Expand Up @@ -363,6 +368,7 @@ run_tests $ns1 $ns2 dead:beef:1::1

do_mptcp_sockopt_tests
do_tcpinq_tests
do_diag_tests

mptcp_lib_result_print_all_tap
exit $ret

0 comments on commit ee9495d

Please sign in to comment.