• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*\
3  * Basic sendmmsg() test that sends and receives messages.
4  *
5  * This test is based on source contained in the man pages for sendmmsg and
6  * recvmmsg in release 4.15 of the Linux man-pages project.
7  */
8 
9 #define _GNU_SOURCE
10 #include "sendmmsg.h"
11 
12 #define BUFSIZE 16
13 #define VLEN 2
14 
15 static int send_sockfd;
16 static int receive_sockfd;
17 static struct mmsghdr *snd_msg, *rcv_msg;
18 static struct iovec *snd1, *snd2, *rcv1, *rcv2;
19 
run(void)20 static void run(void)
21 {
22 	struct time64_variants *tv = &variants[tst_variant];
23 	struct tst_ts timeout;
24 	int retval;
25 
26 	retval = tv->sendmmsg(send_sockfd, snd_msg, VLEN, 0);
27 	if (retval < 0 || snd_msg[0].msg_len != 6 || snd_msg[1].msg_len != 6) {
28 		tst_res(TFAIL | TERRNO, "sendmmsg() failed");
29 		return;
30 	}
31 
32 	memset(rcv1->iov_base, 0, rcv1->iov_len);
33 	memset(rcv2->iov_base, 0, rcv2->iov_len);
34 
35 	timeout.type = tv->ts_type;
36 	tst_ts_set_sec(&timeout, 1);
37 	tst_ts_set_nsec(&timeout, 0);
38 
39 	retval = tv->recvmmsg(receive_sockfd, rcv_msg, VLEN, 0, tst_ts_get(&timeout));
40 
41 	if (retval == -1) {
42 		tst_res(TFAIL | TERRNO, "recvmmsg() failed");
43 		return;
44 	}
45 	if (retval != 2) {
46 		tst_res(TFAIL, "Received unexpected number of messages (%d)",
47 			retval);
48 		return;
49 	}
50 
51 	if (memcmp(rcv1->iov_base, "onetwo", 6))
52 		tst_res(TFAIL, "Error in first received message");
53 	else
54 		tst_res(TPASS, "First message received successfully");
55 
56 	if (memcmp(rcv2->iov_base, "three", 5))
57 		tst_res(TFAIL, "Error in second received message");
58 	else
59 		tst_res(TPASS, "Second message received successfully");
60 }
61 
setup(void)62 static void setup(void)
63 {
64 	struct sockaddr_in addr;
65 	unsigned int port = TST_GET_UNUSED_PORT(AF_INET, SOCK_DGRAM);
66 
67 	send_sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
68 	receive_sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
69 
70 	addr.sin_family = AF_INET;
71 	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
72 	addr.sin_port = port;
73 
74 	SAFE_BIND(receive_sockfd, (struct sockaddr *)&addr, sizeof(addr));
75 	SAFE_CONNECT(send_sockfd, (struct sockaddr *)&addr, sizeof(addr));
76 
77 	memcpy(snd1[0].iov_base, "one", snd1[0].iov_len);
78 	memcpy(snd1[1].iov_base, "two", snd1[1].iov_len);
79 	memcpy(snd2->iov_base, "three3", snd2->iov_len);
80 
81 	memset(snd_msg, 0, VLEN * sizeof(*snd_msg));
82 	snd_msg[0].msg_hdr.msg_iov = snd1;
83 	snd_msg[0].msg_hdr.msg_iovlen = 2;
84 	snd_msg[1].msg_hdr.msg_iov = snd2;
85 	snd_msg[1].msg_hdr.msg_iovlen = 1;
86 
87 	memset(rcv_msg, 0, VLEN * sizeof(*rcv_msg));
88 	rcv_msg[0].msg_hdr.msg_iov = rcv1;
89 	rcv_msg[0].msg_hdr.msg_iovlen = 1;
90 	rcv_msg[1].msg_hdr.msg_iov = rcv2;
91 	rcv_msg[1].msg_hdr.msg_iovlen = 1;
92 
93 	tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
94 }
95 
cleanup(void)96 static void cleanup(void)
97 {
98 	if (send_sockfd > 0)
99 		SAFE_CLOSE(send_sockfd);
100 	if (receive_sockfd > 0)
101 		SAFE_CLOSE(receive_sockfd);
102 }
103 
104 static struct tst_test test = {
105 	.test_all = run,
106 	.setup = setup,
107 	.cleanup = cleanup,
108 	.test_variants = ARRAY_SIZE(variants),
109 	.bufs = (struct tst_buffers []) {
110 		{&snd1, .iov_sizes = (int[]){3, 3, -1}},
111 		{&snd2, .iov_sizes = (int[]){6, -1}},
112 		{&rcv1, .iov_sizes = (int[]){6, -1}},
113 		{&rcv2, .iov_sizes = (int[]){5, -1}},
114 		{&snd_msg, .size = VLEN * sizeof(*snd_msg)},
115 		{&rcv_msg, .size = VLEN * sizeof(*rcv_msg)},
116 		{},
117 	}
118 };
119