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