xref: /netbsd-src/tests/lib/libc/sys/t_sendmmsg.c (revision 97775c96b8a910ee9bbd213bf5e3750070dbed6e)
1*97775c96Schristos /*	$NetBSD: t_sendmmsg.c,v 1.3 2019/03/16 21:46:43 christos Exp $	*/
2a80aa007Schristos 
3a80aa007Schristos /*-
4a80aa007Schristos  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5a80aa007Schristos  * All rights reserved.
6a80aa007Schristos  *
7a80aa007Schristos  * This code is derived from software contributed to The NetBSD Foundation
8a80aa007Schristos  * by Christos Zoulas.
9a80aa007Schristos  *
10a80aa007Schristos  * Redistribution and use in source and binary forms, with or without
11a80aa007Schristos  * modification, are permitted provided that the following conditions
12a80aa007Schristos  * are met:
13a80aa007Schristos  * 1. Redistributions of source code must retain the above copyright
14a80aa007Schristos  *    notice, this list of conditions and the following disclaimer.
15a80aa007Schristos  * 2. Redistributions in binary form must reproduce the above copyright
16a80aa007Schristos  *    notice, this list of conditions and the following disclaimer in the
17a80aa007Schristos  *    documentation and/or other materials provided with the distribution.
18a80aa007Schristos  *
19a80aa007Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20a80aa007Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21a80aa007Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a80aa007Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23a80aa007Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24a80aa007Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25a80aa007Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26a80aa007Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27a80aa007Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a80aa007Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29a80aa007Schristos  * POSSIBILITY OF SUCH DAMAGE.
30a80aa007Schristos  */
31a80aa007Schristos #include <sys/cdefs.h>
32*97775c96Schristos __RCSID("$NetBSD: t_sendmmsg.c,v 1.3 2019/03/16 21:46:43 christos Exp $");
33a80aa007Schristos 
34a80aa007Schristos #include <atf-c.h>
35a80aa007Schristos #include <sys/types.h>
36a80aa007Schristos #include <sys/socket.h>
37a80aa007Schristos #include <sys/ioctl.h>
38a80aa007Schristos #include <sys/wait.h>
39a80aa007Schristos 
40a80aa007Schristos #include <string.h>
41a80aa007Schristos #include <time.h>
42a80aa007Schristos #include <stdint.h>
43a80aa007Schristos #include <errno.h>
44a80aa007Schristos #include <signal.h>
45a80aa007Schristos #include <stdio.h>
46a80aa007Schristos #include <stdlib.h>
47a80aa007Schristos #include <unistd.h>
48a80aa007Schristos #include <sched.h>
49a80aa007Schristos 
50a80aa007Schristos #define BUFSIZE	65536
51a80aa007Schristos 
52a80aa007Schristos #define min(a, b) ((a) < (b) ? (a) : (b))
53a80aa007Schristos static int debug = 1;
54a80aa007Schristos static volatile sig_atomic_t rdied;
55a80aa007Schristos 
56a80aa007Schristos static void
handle_sigchld(__unused int pid)57a80aa007Schristos handle_sigchld(__unused int pid)
58a80aa007Schristos {
59a80aa007Schristos 
60a80aa007Schristos 	rdied = 1;
61a80aa007Schristos }
62a80aa007Schristos 
63a80aa007Schristos ATF_TC(sendmmsg_basic);
ATF_TC_HEAD(sendmmsg_basic,tc)64a80aa007Schristos ATF_TC_HEAD(sendmmsg_basic, tc)
65a80aa007Schristos {
66a80aa007Schristos 	atf_tc_set_md_var(tc, "descr", "A basic test of sendmmsg(2)");
67a80aa007Schristos }
68a80aa007Schristos 
69a80aa007Schristos static void
setsock(int fd,int type)70a80aa007Schristos setsock(int fd, int type)
71a80aa007Schristos {
72a80aa007Schristos 	int buflen = BUFSIZE;
73a80aa007Schristos 	socklen_t socklen = sizeof(buflen);
74a80aa007Schristos 
75a80aa007Schristos 	ATF_REQUIRE_MSG(setsockopt(fd, SOL_SOCKET, type,
76a80aa007Schristos 	    &buflen, socklen) != -1, "%s (%s)",
77a80aa007Schristos 	    type == SO_RCVBUF ? "rcv" : "snd", strerror(errno));
78a80aa007Schristos }
79a80aa007Schristos 
ATF_TC_BODY(sendmmsg_basic,tc)80a80aa007Schristos ATF_TC_BODY(sendmmsg_basic, tc)
81a80aa007Schristos {
82a80aa007Schristos 	int fd[2], error, cnt;
83a80aa007Schristos 	uint8_t *buf;
84a80aa007Schristos 	struct mmsghdr *mmsghdr;
85a80aa007Schristos 	struct iovec *iov;
86a80aa007Schristos 	unsigned int mmsgcnt, n;
87a80aa007Schristos 	int status;
88a80aa007Schristos 	off_t off;
89a80aa007Schristos 	uint8_t DGRAM[1316] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, };
90a80aa007Schristos 	uint8_t rgram[sizeof(DGRAM)];
91a80aa007Schristos 	struct sigaction sa;
92a80aa007Schristos 	ssize_t overf = 0;
93a80aa007Schristos 
94a80aa007Schristos 	error = socketpair(AF_UNIX, SOCK_DGRAM, 0, fd);
95a80aa007Schristos 	ATF_REQUIRE_MSG(error != -1, "socketpair failed (%s)", strerror(errno));
96a80aa007Schristos 
97a80aa007Schristos 	buf = malloc(BUFSIZE);
98a80aa007Schristos 	ATF_REQUIRE_MSG(buf != NULL, "malloc failed (%s)", strerror(errno));
99a80aa007Schristos 
100a80aa007Schristos 	setsock(fd[1], SO_SNDBUF);
101a80aa007Schristos //	setsock(fd[0], SO_RCVBUF);
102a80aa007Schristos 
103a80aa007Schristos 	mmsgcnt = BUFSIZE / sizeof(DGRAM);
104*97775c96Schristos 	mmsghdr = calloc(mmsgcnt, sizeof(*mmsghdr));
105a80aa007Schristos 	ATF_REQUIRE_MSG(mmsghdr != NULL, "malloc failed (%s)", strerror(errno));
106a80aa007Schristos 	iov = malloc(sizeof(*iov) * mmsgcnt);
107a80aa007Schristos 	ATF_REQUIRE_MSG(iov != NULL, "malloc failed (%s)", strerror(errno));
108a80aa007Schristos 
109a80aa007Schristos 	for (off = 0, n = 0; n < mmsgcnt; n++) {
110a80aa007Schristos 		iov[n].iov_base = buf + off;
111a80aa007Schristos 		memcpy(iov[n].iov_base, DGRAM, sizeof(DGRAM));
112a80aa007Schristos 		*(buf + off) = n;
113a80aa007Schristos 		iov[n].iov_len = sizeof(DGRAM);
114a80aa007Schristos 		off += iov[n].iov_len;
115a80aa007Schristos 		mmsghdr[n].msg_hdr.msg_iov = &iov[n];
116a80aa007Schristos 		mmsghdr[n].msg_hdr.msg_iovlen = 1;
117a80aa007Schristos 		mmsghdr[n].msg_hdr.msg_name = NULL;
118a80aa007Schristos 		mmsghdr[n].msg_hdr.msg_namelen = 0;
119a80aa007Schristos 	}
120a80aa007Schristos 
121a80aa007Schristos 	memset(&sa, 0, sizeof(sa));
122a80aa007Schristos 	sa.sa_flags = SA_RESTART;
123a80aa007Schristos 	sa.sa_handler = &handle_sigchld;
124a80aa007Schristos 	sigemptyset(&sa.sa_mask);
125a80aa007Schristos 	error = sigaction(SIGCHLD, &sa, 0);
126a80aa007Schristos 	ATF_REQUIRE_MSG(error != -1, "sigaction failed (%s)",
127a80aa007Schristos 	    strerror(errno));
128a80aa007Schristos 
129a80aa007Schristos 	switch (fork()) {
130a80aa007Schristos 	case -1:
131a80aa007Schristos 		ATF_REQUIRE_MSG(0, "fork failed (%s)", strerror(errno));
132a80aa007Schristos 		break;
133a80aa007Schristos 	case 0:
134a80aa007Schristos 		sched_yield();
135a80aa007Schristos 		if (debug)
136a80aa007Schristos 		    printf("sending %u messages (max %u per syscall)\n", n,
137a80aa007Schristos 			mmsgcnt);
138a80aa007Schristos 		for (n = 0; n < mmsgcnt;) {
139a80aa007Schristos 			if (debug)
140a80aa007Schristos 				printf("sending packet %u/%u...\n", n,
141a80aa007Schristos 				    mmsgcnt);
142a80aa007Schristos 			// XXX: ENOBUFS bug, on the receive side!!!
143a80aa007Schristos 			// in npkt = min(mmsgsize, mmsgcnt - n);
144a80aa007Schristos 			int npkt = min(3, mmsgcnt - n), a;
145a80aa007Schristos 			do {
146a80aa007Schristos 				a = 0;
147a80aa007Schristos 				ATF_REQUIRE(ioctl(fd[1], FIONSPACE, &a) != -1);
148a80aa007Schristos 				printf("1 %d\n", a);
149a80aa007Schristos 				ATF_REQUIRE(ioctl(fd[0], FIONSPACE, &a) != -1);
150a80aa007Schristos 				printf("0 %d\n", a);
151a80aa007Schristos 			} while ((size_t)a < sizeof(DGRAM));
152a80aa007Schristos 			cnt = sendmmsg(fd[1], mmsghdr + n, npkt, 0);
153a80aa007Schristos 			if (cnt == -1 && errno == ENOBUFS) {
154a80aa007Schristos 				overf++;
155a80aa007Schristos 				if (debug)
156a80aa007Schristos 					printf("send buffer overflowed"
157a80aa007Schristos 					    " (%zu)\n",overf);
158a80aa007Schristos 				if (overf > 100)
159a80aa007Schristos 					exit(1);
160a80aa007Schristos 				sched_yield();
161a80aa007Schristos 				sched_yield();
162a80aa007Schristos 				sched_yield();
163a80aa007Schristos 				continue;
164a80aa007Schristos 			}
165*97775c96Schristos 			ATF_REQUIRE_MSG(cnt != -1, "sendmmsg %u failed (%s)",
166*97775c96Schristos 			    n, strerror(errno));
167a80aa007Schristos 			if (debug)
168a80aa007Schristos 				printf("sendmmsg: sent %u messages\n", cnt);
169a80aa007Schristos 			n += cnt;
170a80aa007Schristos 			sched_yield();
171a80aa007Schristos 			sched_yield();
172a80aa007Schristos 			sched_yield();
173a80aa007Schristos 		}
174a80aa007Schristos 		if (debug)
175a80aa007Schristos 			printf("done!\n");
176a80aa007Schristos 		exit(0);
177a80aa007Schristos 		/*NOTREACHED*/
178a80aa007Schristos 	default:
179a80aa007Schristos 		for (n = 0; n < mmsgcnt; n++) {
180a80aa007Schristos 			if (debug)
181a80aa007Schristos 				printf("receiving packet %u/%u...\n", n,
182a80aa007Schristos 				    mmsgcnt);
183a80aa007Schristos 			do {
184a80aa007Schristos 				if (rdied)
185a80aa007Schristos 					break;
186a80aa007Schristos 				cnt = recv(fd[0], rgram, sizeof(rgram), 0);
1874c5df848Sroy 				ATF_REQUIRE_MSG(cnt != -1 || errno != ENOBUFS,
1884c5df848Sroy 				    "recv failed (%s)", strerror(errno));
189a80aa007Schristos 				ATF_CHECK_EQ_MSG(cnt, sizeof(rgram),
190a80aa007Schristos 				    "packet length");
191a80aa007Schristos 				ATF_CHECK_EQ_MSG(rgram[0], n,
192a80aa007Schristos 				    "number %u != %u", rgram[0], n);
193a80aa007Schristos 				ATF_REQUIRE_MSG(memcmp(rgram + 1, DGRAM + 1,
194a80aa007Schristos 				    sizeof(rgram) - 1) == 0, "bad data");
195a80aa007Schristos 			} while (cnt == -1 && errno == ENOBUFS);
196a80aa007Schristos 		}
197a80aa007Schristos 		error = wait(&status);
198a80aa007Schristos 		ATF_REQUIRE_MSG(error != -1, "wait failed (%s)",
199a80aa007Schristos 		    strerror(errno));
200a80aa007Schristos 		ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0,
201a80aa007Schristos 		    "receiver died");
202a80aa007Schristos 		break;
203a80aa007Schristos 	}
204a80aa007Schristos }
205a80aa007Schristos 
ATF_TP_ADD_TCS(tp)206a80aa007Schristos ATF_TP_ADD_TCS(tp)
207a80aa007Schristos {
208a80aa007Schristos 
209a80aa007Schristos 	ATF_TP_ADD_TC(tp, sendmmsg_basic);
210a80aa007Schristos 
211a80aa007Schristos 	return atf_no_error();
212a80aa007Schristos }
213