t_recvmmsg.c revision 1.3 1 1.3 christos /* $NetBSD: t_recvmmsg.c,v 1.3 2018/08/21 10:38:09 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.3 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.3 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.3 christos __RCSID("$NetBSD: t_recvmmsg.c,v 1.3 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/wait.h>
38 1.1 christos
39 1.1 christos #include <string.h>
40 1.1 christos #include <time.h>
41 1.1 christos #include <stdint.h>
42 1.1 christos #include <errno.h>
43 1.2 roy #include <signal.h>
44 1.1 christos #include <stdio.h>
45 1.1 christos #include <stdlib.h>
46 1.1 christos #include <unistd.h>
47 1.1 christos #include <sched.h>
48 1.1 christos
49 1.1 christos #define BUFSIZE 65536
50 1.1 christos #define NPKTS 50
51 1.1 christos
52 1.1 christos #define min(a, b) ((a) < (b) ? (a) : (b))
53 1.1 christos static int debug;
54 1.2 roy static volatile sig_atomic_t rdied;
55 1.1 christos
56 1.2 roy static void
57 1.2 roy handle_sigchld(__unused int pid)
58 1.2 roy {
59 1.2 roy
60 1.2 roy rdied = 1;
61 1.2 roy }
62 1.1 christos
63 1.1 christos ATF_TC(recvmmsg_basic);
64 1.1 christos ATF_TC_HEAD(recvmmsg_basic, tc)
65 1.1 christos {
66 1.1 christos atf_tc_set_md_var(tc, "descr", "A basic test of recvmmsg(2)");
67 1.1 christos }
68 1.1 christos
69 1.1 christos ATF_TC_BODY(recvmmsg_basic, tc)
70 1.1 christos {
71 1.1 christos int fd[2], error, i, cnt;
72 1.1 christos uint8_t *buf;
73 1.1 christos struct mmsghdr *mmsghdr;
74 1.1 christos struct iovec *iov;
75 1.1 christos unsigned int mmsgcnt, n;
76 1.1 christos int status;
77 1.1 christos off_t off;
78 1.1 christos uint8_t DGRAM[1316] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, };
79 1.2 roy struct sigaction sa;
80 1.2 roy ssize_t overf = 0;
81 1.2 roy
82 1.1 christos error = socketpair(AF_UNIX, SOCK_DGRAM, 0, fd);
83 1.1 christos ATF_REQUIRE_MSG(error != -1, "socketpair failed (%s)", strerror(errno));
84 1.1 christos
85 1.1 christos buf = malloc(BUFSIZE);
86 1.1 christos ATF_REQUIRE_MSG(buf != NULL, "malloc failed (%s)", strerror(errno));
87 1.1 christos
88 1.1 christos mmsgcnt = BUFSIZE / sizeof(DGRAM);
89 1.1 christos mmsghdr = malloc(sizeof(*mmsghdr) * mmsgcnt);
90 1.1 christos ATF_REQUIRE_MSG(mmsghdr != NULL, "malloc failed (%s)", strerror(errno));
91 1.1 christos iov = malloc(sizeof(*iov) * mmsgcnt);
92 1.1 christos ATF_REQUIRE_MSG(iov != NULL, "malloc failed (%s)", strerror(errno));
93 1.1 christos
94 1.1 christos for (off = 0, n = 0; n < mmsgcnt; n++) {
95 1.1 christos iov[n].iov_base = buf + off;
96 1.1 christos iov[n].iov_len = sizeof(DGRAM);
97 1.1 christos off += iov[n].iov_len;
98 1.1 christos mmsghdr[n].msg_hdr.msg_iov = &iov[n];
99 1.1 christos mmsghdr[n].msg_hdr.msg_iovlen = 1;
100 1.1 christos mmsghdr[n].msg_hdr.msg_name = NULL;
101 1.1 christos mmsghdr[n].msg_hdr.msg_namelen = 0;
102 1.1 christos }
103 1.1 christos
104 1.2 roy memset(&sa, 0, sizeof(sa));
105 1.2 roy sa.sa_flags = SA_RESTART;
106 1.2 roy sa.sa_handler = &handle_sigchld;
107 1.2 roy sigemptyset(&sa.sa_mask);
108 1.2 roy error = sigaction(SIGCHLD, &sa, 0);
109 1.2 roy ATF_REQUIRE_MSG(error != -1, "sigaction failed (%s)",
110 1.2 roy strerror(errno));
111 1.2 roy
112 1.1 christos switch (fork()) {
113 1.1 christos case -1:
114 1.1 christos ATF_REQUIRE_MSG(0, "fork failed (%s)", strerror(errno));
115 1.1 christos break;
116 1.1 christos
117 1.1 christos case 0:
118 1.1 christos n = NPKTS;
119 1.1 christos if (debug)
120 1.1 christos printf("waiting for %u messages (max %u per syscall)\n", n,
121 1.1 christos mmsgcnt);
122 1.1 christos while (n > 0) {
123 1.1 christos struct timespec ts = { 1, 0 };
124 1.1 christos cnt = recvmmsg(fd[1], mmsghdr, min(mmsgcnt, n),
125 1.1 christos MSG_WAITALL, &ts);
126 1.2 roy if (cnt == -1 && errno == ENOBUFS) {
127 1.2 roy overf++;
128 1.2 roy if (debug)
129 1.2 roy printf("receive buffer overflowed"
130 1.2 roy " (%zu)\n",overf);
131 1.2 roy continue;
132 1.2 roy }
133 1.1 christos ATF_REQUIRE_MSG(cnt != -1, "recvmmsg failed (%s)",
134 1.1 christos strerror(errno));
135 1.1 christos ATF_REQUIRE_MSG(cnt != 0, "recvmmsg timeout");
136 1.1 christos if (debug)
137 1.1 christos printf("recvmmsg: got %u messages\n", cnt);
138 1.1 christos for (i = 0; i < cnt; i++) {
139 1.1 christos ATF_CHECK_EQ_MSG(mmsghdr[i].msg_len,
140 1.1 christos sizeof(DGRAM), "packet length");
141 1.1 christos ATF_CHECK_EQ_MSG(
142 1.1 christos ((uint8_t *)iov[i].iov_base)[0],
143 1.1 christos NPKTS - n + i, "packet contents");
144 1.1 christos }
145 1.1 christos n -= cnt;
146 1.1 christos }
147 1.1 christos if (debug)
148 1.1 christos printf("done!\n");
149 1.1 christos exit(0);
150 1.1 christos /*NOTREACHED*/
151 1.1 christos default:
152 1.1 christos sched_yield();
153 1.1 christos
154 1.1 christos for (n = 0; n < NPKTS; n++) {
155 1.1 christos if (debug)
156 1.1 christos printf("sending packet %u/%u...\n", (n+1),
157 1.1 christos NPKTS);
158 1.1 christos do {
159 1.2 roy if (rdied)
160 1.2 roy break;
161 1.1 christos DGRAM[0] = n;
162 1.1 christos error = send(fd[0], DGRAM, sizeof(DGRAM), 0);
163 1.1 christos } while (error == -1 && errno == ENOBUFS);
164 1.2 roy ATF_REQUIRE_MSG(error != -1, "send failed (%s)",
165 1.2 roy strerror(errno));
166 1.1 christos }
167 1.1 christos error = wait(&status);
168 1.1 christos ATF_REQUIRE_MSG(error != -1, "wait failed (%s)",
169 1.1 christos strerror(errno));
170 1.2 roy ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0,
171 1.2 roy "receiver died");
172 1.1 christos break;
173 1.1 christos }
174 1.1 christos }
175 1.1 christos
176 1.1 christos ATF_TP_ADD_TCS(tp)
177 1.1 christos {
178 1.1 christos
179 1.1 christos ATF_TP_ADD_TC(tp, recvmmsg_basic);
180 1.1 christos
181 1.1 christos return atf_no_error();
182 1.1 christos }
183