1*57718be8SEnji Cooper /* $NetBSD: t_recvmmsg.c,v 1.1 2012/06/22 18:45:23 christos Exp $ */
2*57718be8SEnji Cooper
3*57718be8SEnji Cooper /*-
4*57718be8SEnji Cooper * Copyright (c) 2012 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper * All rights reserved.
6*57718be8SEnji Cooper *
7*57718be8SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation
8*57718be8SEnji Cooper * by Jared McNeill and Christos Zoulas.
9*57718be8SEnji Cooper *
10*57718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
11*57718be8SEnji Cooper * modification, are permitted provided that the following conditions
12*57718be8SEnji Cooper * are met:
13*57718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
14*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
15*57718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
16*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
17*57718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
18*57718be8SEnji Cooper * 3. All advertising materials mentioning features or use of this software
19*57718be8SEnji Cooper * must display the following acknowledgement:
20*57718be8SEnji Cooper * This product includes software developed by the NetBSD
21*57718be8SEnji Cooper * Foundation, Inc. and its contributors.
22*57718be8SEnji Cooper * 4. Neither the name of The NetBSD Foundation nor the names of its
23*57718be8SEnji Cooper * contributors may be used to endorse or promote products derived
24*57718be8SEnji Cooper * from this software without specific prior written permission.
25*57718be8SEnji Cooper *
26*57718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27*57718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28*57718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29*57718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30*57718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31*57718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32*57718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33*57718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34*57718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35*57718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36*57718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
37*57718be8SEnji Cooper */
38*57718be8SEnji Cooper #include <sys/cdefs.h>
39*57718be8SEnji Cooper __RCSID("$NetBSD: t_recvmmsg.c,v 1.1 2012/06/22 18:45:23 christos Exp $");
40*57718be8SEnji Cooper
41*57718be8SEnji Cooper #include <atf-c.h>
42*57718be8SEnji Cooper #include <sys/types.h>
43*57718be8SEnji Cooper #include <sys/socket.h>
44*57718be8SEnji Cooper #include <sys/wait.h>
45*57718be8SEnji Cooper
46*57718be8SEnji Cooper #include <string.h>
47*57718be8SEnji Cooper #include <time.h>
48*57718be8SEnji Cooper #include <stdint.h>
49*57718be8SEnji Cooper #include <errno.h>
50*57718be8SEnji Cooper #include <stdio.h>
51*57718be8SEnji Cooper #include <stdlib.h>
52*57718be8SEnji Cooper #include <unistd.h>
53*57718be8SEnji Cooper #include <sched.h>
54*57718be8SEnji Cooper
55*57718be8SEnji Cooper #define BUFSIZE 65536
56*57718be8SEnji Cooper #define NPKTS 50
57*57718be8SEnji Cooper
58*57718be8SEnji Cooper #define min(a, b) ((a) < (b) ? (a) : (b))
59*57718be8SEnji Cooper static int debug;
60*57718be8SEnji Cooper
61*57718be8SEnji Cooper
62*57718be8SEnji Cooper ATF_TC(recvmmsg_basic);
ATF_TC_HEAD(recvmmsg_basic,tc)63*57718be8SEnji Cooper ATF_TC_HEAD(recvmmsg_basic, tc)
64*57718be8SEnji Cooper {
65*57718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "A basic test of recvmmsg(2)");
66*57718be8SEnji Cooper }
67*57718be8SEnji Cooper
ATF_TC_BODY(recvmmsg_basic,tc)68*57718be8SEnji Cooper ATF_TC_BODY(recvmmsg_basic, tc)
69*57718be8SEnji Cooper {
70*57718be8SEnji Cooper int fd[2], error, i, cnt;
71*57718be8SEnji Cooper uint8_t *buf;
72*57718be8SEnji Cooper struct mmsghdr *mmsghdr;
73*57718be8SEnji Cooper struct iovec *iov;
74*57718be8SEnji Cooper unsigned int mmsgcnt, n;
75*57718be8SEnji Cooper int status;
76*57718be8SEnji Cooper off_t off;
77*57718be8SEnji Cooper uint8_t DGRAM[1316] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, };
78*57718be8SEnji Cooper
79*57718be8SEnji Cooper error = socketpair(AF_UNIX, SOCK_DGRAM, 0, fd);
80*57718be8SEnji Cooper ATF_REQUIRE_MSG(error != -1, "socketpair failed (%s)", strerror(errno));
81*57718be8SEnji Cooper
82*57718be8SEnji Cooper buf = malloc(BUFSIZE);
83*57718be8SEnji Cooper ATF_REQUIRE_MSG(buf != NULL, "malloc failed (%s)", strerror(errno));
84*57718be8SEnji Cooper
85*57718be8SEnji Cooper mmsgcnt = BUFSIZE / sizeof(DGRAM);
86*57718be8SEnji Cooper mmsghdr = malloc(sizeof(*mmsghdr) * mmsgcnt);
87*57718be8SEnji Cooper ATF_REQUIRE_MSG(mmsghdr != NULL, "malloc failed (%s)", strerror(errno));
88*57718be8SEnji Cooper iov = malloc(sizeof(*iov) * mmsgcnt);
89*57718be8SEnji Cooper ATF_REQUIRE_MSG(iov != NULL, "malloc failed (%s)", strerror(errno));
90*57718be8SEnji Cooper
91*57718be8SEnji Cooper for (off = 0, n = 0; n < mmsgcnt; n++) {
92*57718be8SEnji Cooper iov[n].iov_base = buf + off;
93*57718be8SEnji Cooper iov[n].iov_len = sizeof(DGRAM);
94*57718be8SEnji Cooper off += iov[n].iov_len;
95*57718be8SEnji Cooper mmsghdr[n].msg_hdr.msg_iov = &iov[n];
96*57718be8SEnji Cooper mmsghdr[n].msg_hdr.msg_iovlen = 1;
97*57718be8SEnji Cooper mmsghdr[n].msg_hdr.msg_name = NULL;
98*57718be8SEnji Cooper mmsghdr[n].msg_hdr.msg_namelen = 0;
99*57718be8SEnji Cooper }
100*57718be8SEnji Cooper
101*57718be8SEnji Cooper switch (fork()) {
102*57718be8SEnji Cooper case -1:
103*57718be8SEnji Cooper ATF_REQUIRE_MSG(0, "fork failed (%s)", strerror(errno));
104*57718be8SEnji Cooper break;
105*57718be8SEnji Cooper
106*57718be8SEnji Cooper case 0:
107*57718be8SEnji Cooper n = NPKTS;
108*57718be8SEnji Cooper if (debug)
109*57718be8SEnji Cooper printf("waiting for %u messages (max %u per syscall)\n", n,
110*57718be8SEnji Cooper mmsgcnt);
111*57718be8SEnji Cooper while (n > 0) {
112*57718be8SEnji Cooper struct timespec ts = { 1, 0 };
113*57718be8SEnji Cooper cnt = recvmmsg(fd[1], mmsghdr, min(mmsgcnt, n),
114*57718be8SEnji Cooper MSG_WAITALL, &ts);
115*57718be8SEnji Cooper ATF_REQUIRE_MSG(cnt != -1, "recvmmsg failed (%s)",
116*57718be8SEnji Cooper strerror(errno));
117*57718be8SEnji Cooper ATF_REQUIRE_MSG(cnt != 0, "recvmmsg timeout");
118*57718be8SEnji Cooper if (debug)
119*57718be8SEnji Cooper printf("recvmmsg: got %u messages\n", cnt);
120*57718be8SEnji Cooper for (i = 0; i < cnt; i++) {
121*57718be8SEnji Cooper ATF_CHECK_EQ_MSG(mmsghdr[i].msg_len,
122*57718be8SEnji Cooper sizeof(DGRAM), "packet length");
123*57718be8SEnji Cooper ATF_CHECK_EQ_MSG(
124*57718be8SEnji Cooper ((uint8_t *)iov[i].iov_base)[0],
125*57718be8SEnji Cooper NPKTS - n + i, "packet contents");
126*57718be8SEnji Cooper }
127*57718be8SEnji Cooper n -= cnt;
128*57718be8SEnji Cooper }
129*57718be8SEnji Cooper if (debug)
130*57718be8SEnji Cooper printf("done!\n");
131*57718be8SEnji Cooper exit(0);
132*57718be8SEnji Cooper /*NOTREACHED*/
133*57718be8SEnji Cooper default:
134*57718be8SEnji Cooper sched_yield();
135*57718be8SEnji Cooper
136*57718be8SEnji Cooper for (n = 0; n < NPKTS; n++) {
137*57718be8SEnji Cooper if (debug)
138*57718be8SEnji Cooper printf("sending packet %u/%u...\n", (n+1),
139*57718be8SEnji Cooper NPKTS);
140*57718be8SEnji Cooper do {
141*57718be8SEnji Cooper DGRAM[0] = n;
142*57718be8SEnji Cooper error = send(fd[0], DGRAM, sizeof(DGRAM), 0);
143*57718be8SEnji Cooper } while (error == -1 && errno == ENOBUFS);
144*57718be8SEnji Cooper if (error == -1)
145*57718be8SEnji Cooper ATF_REQUIRE_MSG(error != -1, "send failed (%s)",
146*57718be8SEnji Cooper strerror(errno));
147*57718be8SEnji Cooper }
148*57718be8SEnji Cooper error = wait(&status);
149*57718be8SEnji Cooper ATF_REQUIRE_MSG(error != -1, "wait failed (%s)",
150*57718be8SEnji Cooper strerror(errno));
151*57718be8SEnji Cooper break;
152*57718be8SEnji Cooper }
153*57718be8SEnji Cooper }
154*57718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)155*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
156*57718be8SEnji Cooper {
157*57718be8SEnji Cooper
158*57718be8SEnji Cooper ATF_TP_ADD_TC(tp, recvmmsg_basic);
159*57718be8SEnji Cooper
160*57718be8SEnji Cooper return atf_no_error();
161*57718be8SEnji Cooper }
162