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