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