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