xref: /netbsd-src/tests/lib/libc/sys/t_recvmmsg.c (revision 1d33257c91e3183bff73ce4a2810f533c59f0238)
1*1d33257cSchristos /*	$NetBSD: t_recvmmsg.c,v 1.4 2018/08/21 10:39:21 christos Exp $	*/
2291dafdaSchristos 
3291dafdaSchristos /*-
4*1d33257cSchristos  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5291dafdaSchristos  * All rights reserved.
6291dafdaSchristos  *
7291dafdaSchristos  * This code is derived from software contributed to The NetBSD Foundation
8*1d33257cSchristos  * by Jared McNeill and Christos Zoulas.
9291dafdaSchristos  *
10291dafdaSchristos  * Redistribution and use in source and binary forms, with or without
11291dafdaSchristos  * modification, are permitted provided that the following conditions
12291dafdaSchristos  * are met:
13291dafdaSchristos  * 1. Redistributions of source code must retain the above copyright
14291dafdaSchristos  *    notice, this list of conditions and the following disclaimer.
15291dafdaSchristos  * 2. Redistributions in binary form must reproduce the above copyright
16291dafdaSchristos  *    notice, this list of conditions and the following disclaimer in the
17291dafdaSchristos  *    documentation and/or other materials provided with the distribution.
18*1d33257cSchristos  * 3. All advertising materials mentioning features or use of this software
19*1d33257cSchristos  *    must display the following acknowledgement:
20*1d33257cSchristos  *        This product includes software developed by the NetBSD
21*1d33257cSchristos  *        Foundation, Inc. and its contributors.
22*1d33257cSchristos  * 4. Neither the name of The NetBSD Foundation nor the names of its
23*1d33257cSchristos  *    contributors may be used to endorse or promote products derived
24*1d33257cSchristos  *    from this software without specific prior written permission.
25291dafdaSchristos  *
26291dafdaSchristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27291dafdaSchristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28291dafdaSchristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29291dafdaSchristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30291dafdaSchristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31291dafdaSchristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32291dafdaSchristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33291dafdaSchristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34291dafdaSchristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35291dafdaSchristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36291dafdaSchristos  * POSSIBILITY OF SUCH DAMAGE.
37291dafdaSchristos  */
38291dafdaSchristos #include <sys/cdefs.h>
39*1d33257cSchristos __RCSID("$NetBSD: t_recvmmsg.c,v 1.4 2018/08/21 10:39:21 christos Exp $");
40291dafdaSchristos 
41291dafdaSchristos #include <atf-c.h>
42291dafdaSchristos #include <sys/types.h>
43291dafdaSchristos #include <sys/socket.h>
44291dafdaSchristos #include <sys/wait.h>
45291dafdaSchristos 
46291dafdaSchristos #include <string.h>
47291dafdaSchristos #include <time.h>
48291dafdaSchristos #include <stdint.h>
49291dafdaSchristos #include <errno.h>
50d96ab75cSroy #include <signal.h>
51291dafdaSchristos #include <stdio.h>
52291dafdaSchristos #include <stdlib.h>
53291dafdaSchristos #include <unistd.h>
54291dafdaSchristos #include <sched.h>
55291dafdaSchristos 
56291dafdaSchristos #define BUFSIZE	65536
57291dafdaSchristos #define NPKTS	50
58291dafdaSchristos 
59291dafdaSchristos #define min(a, b) ((a) < (b) ? (a) : (b))
60291dafdaSchristos static int debug;
61d96ab75cSroy static volatile sig_atomic_t rdied;
62291dafdaSchristos 
63d96ab75cSroy static void
handle_sigchld(__unused int pid)64d96ab75cSroy handle_sigchld(__unused int pid)
65d96ab75cSroy {
66d96ab75cSroy 
67d96ab75cSroy 	rdied = 1;
68d96ab75cSroy }
69291dafdaSchristos 
70291dafdaSchristos ATF_TC(recvmmsg_basic);
ATF_TC_HEAD(recvmmsg_basic,tc)71291dafdaSchristos ATF_TC_HEAD(recvmmsg_basic, tc)
72291dafdaSchristos {
73291dafdaSchristos 	atf_tc_set_md_var(tc, "descr", "A basic test of recvmmsg(2)");
74291dafdaSchristos }
75291dafdaSchristos 
ATF_TC_BODY(recvmmsg_basic,tc)76291dafdaSchristos ATF_TC_BODY(recvmmsg_basic, tc)
77291dafdaSchristos {
78291dafdaSchristos 	int fd[2], error, i, cnt;
79291dafdaSchristos 	uint8_t *buf;
80291dafdaSchristos 	struct mmsghdr *mmsghdr;
81291dafdaSchristos 	struct iovec *iov;
82291dafdaSchristos 	unsigned int mmsgcnt, n;
83291dafdaSchristos 	int status;
84291dafdaSchristos 	off_t off;
85291dafdaSchristos 	uint8_t DGRAM[1316] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, };
86d96ab75cSroy 	struct sigaction sa;
87d96ab75cSroy 	ssize_t overf = 0;
88291dafdaSchristos 
89291dafdaSchristos 	error = socketpair(AF_UNIX, SOCK_DGRAM, 0, fd);
90291dafdaSchristos 	ATF_REQUIRE_MSG(error != -1, "socketpair failed (%s)", strerror(errno));
91291dafdaSchristos 
92291dafdaSchristos 	buf = malloc(BUFSIZE);
93291dafdaSchristos 	ATF_REQUIRE_MSG(buf != NULL, "malloc failed (%s)", strerror(errno));
94291dafdaSchristos 
95291dafdaSchristos 	mmsgcnt = BUFSIZE / sizeof(DGRAM);
96291dafdaSchristos 	mmsghdr = malloc(sizeof(*mmsghdr) * mmsgcnt);
97291dafdaSchristos 	ATF_REQUIRE_MSG(mmsghdr != NULL, "malloc failed (%s)", strerror(errno));
98291dafdaSchristos 	iov = malloc(sizeof(*iov) * mmsgcnt);
99291dafdaSchristos 	ATF_REQUIRE_MSG(iov != NULL, "malloc failed (%s)", strerror(errno));
100291dafdaSchristos 
101291dafdaSchristos 	for (off = 0, n = 0; n < mmsgcnt; n++) {
102291dafdaSchristos 		iov[n].iov_base = buf + off;
103291dafdaSchristos 		iov[n].iov_len = sizeof(DGRAM);
104291dafdaSchristos 		off += iov[n].iov_len;
105291dafdaSchristos 		mmsghdr[n].msg_hdr.msg_iov = &iov[n];
106291dafdaSchristos 		mmsghdr[n].msg_hdr.msg_iovlen = 1;
107291dafdaSchristos 		mmsghdr[n].msg_hdr.msg_name = NULL;
108291dafdaSchristos 		mmsghdr[n].msg_hdr.msg_namelen = 0;
109291dafdaSchristos 	}
110291dafdaSchristos 
111d96ab75cSroy 	memset(&sa, 0, sizeof(sa));
112d96ab75cSroy 	sa.sa_flags = SA_RESTART;
113d96ab75cSroy 	sa.sa_handler = &handle_sigchld;
114d96ab75cSroy 	sigemptyset(&sa.sa_mask);
115d96ab75cSroy 	error = sigaction(SIGCHLD, &sa, 0);
116d96ab75cSroy 	ATF_REQUIRE_MSG(error != -1, "sigaction failed (%s)",
117d96ab75cSroy 	    strerror(errno));
118d96ab75cSroy 
119291dafdaSchristos 	switch (fork()) {
120291dafdaSchristos 	case -1:
121291dafdaSchristos 		ATF_REQUIRE_MSG(0, "fork failed (%s)", strerror(errno));
122291dafdaSchristos 		break;
123291dafdaSchristos 
124291dafdaSchristos 	case 0:
125291dafdaSchristos 		n = NPKTS;
126291dafdaSchristos 		if (debug)
127291dafdaSchristos 		    printf("waiting for %u messages (max %u per syscall)\n", n,
128291dafdaSchristos 			mmsgcnt);
129291dafdaSchristos 		while (n > 0) {
130291dafdaSchristos 			struct timespec ts = { 1, 0 };
131291dafdaSchristos 			cnt = recvmmsg(fd[1], mmsghdr, min(mmsgcnt, n),
132291dafdaSchristos 			    MSG_WAITALL, &ts);
133d96ab75cSroy 			if (cnt == -1 && errno == ENOBUFS) {
134d96ab75cSroy 				overf++;
135d96ab75cSroy 				if (debug)
136d96ab75cSroy 					printf("receive buffer overflowed"
137d96ab75cSroy 					    " (%zu)\n",overf);
138d96ab75cSroy 				continue;
139d96ab75cSroy 			}
140291dafdaSchristos 			ATF_REQUIRE_MSG(cnt != -1, "recvmmsg failed (%s)",
141291dafdaSchristos 			    strerror(errno));
142291dafdaSchristos 			ATF_REQUIRE_MSG(cnt != 0, "recvmmsg timeout");
143291dafdaSchristos 			if (debug)
144291dafdaSchristos 				printf("recvmmsg: got %u messages\n", cnt);
145291dafdaSchristos 			for (i = 0; i < cnt; i++) {
146291dafdaSchristos 				ATF_CHECK_EQ_MSG(mmsghdr[i].msg_len,
147291dafdaSchristos 				    sizeof(DGRAM), "packet length");
148291dafdaSchristos 				ATF_CHECK_EQ_MSG(
149291dafdaSchristos 				    ((uint8_t *)iov[i].iov_base)[0],
150291dafdaSchristos 				    NPKTS - n + i, "packet contents");
151291dafdaSchristos 			}
152291dafdaSchristos 			n -= cnt;
153291dafdaSchristos 		}
154291dafdaSchristos 		if (debug)
155291dafdaSchristos 			printf("done!\n");
156291dafdaSchristos 		exit(0);
157291dafdaSchristos 		/*NOTREACHED*/
158291dafdaSchristos 	default:
159291dafdaSchristos 		sched_yield();
160291dafdaSchristos 
161291dafdaSchristos 		for (n = 0; n < NPKTS; n++) {
162291dafdaSchristos 			if (debug)
163291dafdaSchristos 				printf("sending packet %u/%u...\n", (n+1),
164291dafdaSchristos 				    NPKTS);
165291dafdaSchristos 			do {
166d96ab75cSroy 				if (rdied)
167d96ab75cSroy 					break;
168291dafdaSchristos 				DGRAM[0] = n;
169291dafdaSchristos 				error = send(fd[0], DGRAM, sizeof(DGRAM), 0);
170291dafdaSchristos 			} while (error == -1 && errno == ENOBUFS);
171291dafdaSchristos 			ATF_REQUIRE_MSG(error != -1, "send failed (%s)",
172291dafdaSchristos 			    strerror(errno));
173291dafdaSchristos 		}
174291dafdaSchristos 		error = wait(&status);
175291dafdaSchristos 		ATF_REQUIRE_MSG(error != -1, "wait failed (%s)",
176291dafdaSchristos 		    strerror(errno));
177d96ab75cSroy 		ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0,
178d96ab75cSroy 		    "receiver died");
179291dafdaSchristos 		break;
180291dafdaSchristos 	}
181291dafdaSchristos }
182291dafdaSchristos 
ATF_TP_ADD_TCS(tp)183291dafdaSchristos ATF_TP_ADD_TCS(tp)
184291dafdaSchristos {
185291dafdaSchristos 
186291dafdaSchristos 	ATF_TP_ADD_TC(tp, recvmmsg_basic);
187291dafdaSchristos 
188291dafdaSchristos 	return atf_no_error();
189291dafdaSchristos }
190