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