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