Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Masatake YAMATO <yamato@redhat.com> |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Dmitry V. Levin | 0c8853c | 2016-01-02 13:28:43 +0000 | [diff] [blame] | 29 | #include "tests.h" |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 30 | # include <sys/syscall.h> |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 31 | |
Dmitry V. Levin | eb4649a | 2016-01-12 04:37:06 +0000 | [diff] [blame] | 32 | #if (defined __NR_sendmmsg || defined HAVE_SENDMMSG) \ |
| 33 | && (defined __NR_recvmmsg || defined HAVE_RECVMMSG) |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 34 | |
| 35 | # include <assert.h> |
| 36 | # include <errno.h> |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 37 | # include <stdio.h> |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 38 | # include <unistd.h> |
| 39 | # include <sys/socket.h> |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 40 | |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 41 | # ifndef HAVE_STRUCT_MMSGHDR |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 42 | struct mmsghdr { |
| 43 | struct msghdr msg_hdr; |
| 44 | unsigned msg_len; |
| 45 | }; |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 46 | # endif |
| 47 | |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 48 | static int |
| 49 | send_mmsg(int fd, struct mmsghdr *vec, unsigned int vlen, unsigned int flags) |
| 50 | { |
| 51 | int rc; |
| 52 | #ifdef __NR_sendmmsg |
| 53 | rc = syscall(__NR_sendmmsg, (long) fd, vec, (unsigned long) vlen, |
| 54 | (unsigned long) flags); |
| 55 | if (rc >= 0 || ENOSYS != errno) |
| 56 | return rc; |
Dmitry V. Levin | 536a035 | 2016-01-20 02:06:59 +0000 | [diff] [blame] | 57 | tprintf("sendmmsg(%d, %p, %u, MSG_DONTROUTE|MSG_NOSIGNAL)" |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 58 | " = -1 ENOSYS (%m)\n", fd, vec, vlen); |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 59 | #endif |
| 60 | #ifdef HAVE_SENDMMSG |
| 61 | rc = sendmmsg(fd, vec, vlen, flags); |
| 62 | #endif |
| 63 | return rc; |
| 64 | } |
| 65 | |
| 66 | static int |
| 67 | recv_mmsg(int fd, struct mmsghdr *vec, unsigned int vlen, unsigned int flags, |
| 68 | struct timespec *timeout) |
| 69 | { |
| 70 | int rc; |
Dmitry V. Levin | eb4649a | 2016-01-12 04:37:06 +0000 | [diff] [blame] | 71 | #ifdef __NR_recvmmsg |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 72 | rc = syscall(__NR_recvmmsg, (long) fd, vec, (unsigned long) vlen, |
| 73 | (unsigned long) flags, timeout); |
| 74 | if (rc >= 0 || ENOSYS != errno) |
| 75 | return rc; |
Dmitry V. Levin | 536a035 | 2016-01-20 02:06:59 +0000 | [diff] [blame] | 76 | tprintf("recvmmsg(%d, %p, %u, MSG_DONTWAIT, NULL)" |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 77 | " = -1 ENOSYS (%m)\n", fd, vec, vlen); |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 78 | #endif |
Dmitry V. Levin | eb4649a | 2016-01-12 04:37:06 +0000 | [diff] [blame] | 79 | #ifdef HAVE_RECVMMSG |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 80 | rc = recvmmsg(fd, vec, vlen, flags, timeout); |
| 81 | #endif |
| 82 | return rc; |
| 83 | } |
| 84 | |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 85 | int |
| 86 | main(void) |
| 87 | { |
Dmitry V. Levin | 536a035 | 2016-01-20 02:06:59 +0000 | [diff] [blame] | 88 | tprintf("%s", ""); |
| 89 | |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 90 | int fds[2]; |
| 91 | if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 92 | perror_msg_and_skip("socketpair"); |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 93 | assert(0 == fds[0]); |
| 94 | assert(1 == fds[1]); |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 95 | |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 96 | static const char w0_c[] = "012"; |
| 97 | const char *w0_d = hexdump_strdup(w0_c); |
| 98 | void *w0 = tail_memdup(w0_c, LENGTH_OF(w0_c)); |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 99 | |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 100 | static const char w1_c[] = "34567"; |
| 101 | const char *w1_d = hexdump_strdup(w1_c); |
| 102 | void *w1 = tail_memdup(w1_c, LENGTH_OF(w1_c)); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 103 | |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 104 | static const char w2_c[] = "89abcde"; |
| 105 | const char *w2_d = hexdump_strdup(w2_c); |
| 106 | void *w2 = tail_memdup(w2_c, LENGTH_OF(w2_c)); |
| 107 | |
| 108 | const struct iovec w0_iov_[] = { |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 109 | { |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 110 | .iov_base = w0, |
| 111 | .iov_len = LENGTH_OF(w0_c) |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 112 | }, { |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 113 | .iov_base = w1, |
| 114 | .iov_len = LENGTH_OF(w1_c) |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 115 | } |
| 116 | }; |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 117 | struct iovec *w0_iov = tail_memdup(w0_iov_, sizeof(w0_iov_)); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 118 | |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 119 | const struct iovec w1_iov_[] = { |
| 120 | { |
| 121 | .iov_base = w2, |
| 122 | .iov_len = LENGTH_OF(w2_c) |
| 123 | } |
| 124 | }; |
| 125 | struct iovec *w1_iov = tail_memdup(w1_iov_, sizeof(w1_iov_)); |
| 126 | |
| 127 | const struct mmsghdr w_mmh_[] = { |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 128 | { |
| 129 | .msg_hdr = { |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 130 | .msg_iov = w0_iov, |
| 131 | .msg_iovlen = ARRAY_SIZE(w0_iov_), |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 132 | } |
| 133 | }, { |
| 134 | .msg_hdr = { |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 135 | .msg_iov = w1_iov, |
| 136 | .msg_iovlen = ARRAY_SIZE(w1_iov_), |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | }; |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 140 | void *w_mmh = tail_memdup(w_mmh_, sizeof(w_mmh_)); |
| 141 | const unsigned int n_w_mmh = ARRAY_SIZE(w_mmh_); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 142 | |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 143 | int r = send_mmsg(1, w_mmh, n_w_mmh, MSG_DONTROUTE | MSG_NOSIGNAL); |
Dmitry V. Levin | 95cbf6e | 2015-01-14 12:47:45 +0000 | [diff] [blame] | 144 | if (r < 0 && errno == ENOSYS) |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 145 | perror_msg_and_skip("sendmmsg"); |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 146 | assert(r == (int) n_w_mmh); |
| 147 | assert(close(1) == 0); |
| 148 | tprintf("sendmmsg(1, {{{msg_name(0)=NULL, msg_iov(%u)=[{\"%s\", %u}" |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 149 | ", {\"%s\", %u}], msg_controllen=0, msg_flags=0}, %u}" |
| 150 | ", {{msg_name(0)=NULL, msg_iov(%u)=[{\"%s\", %u}]" |
| 151 | ", msg_controllen=0, msg_flags=0}, %u}}, %u" |
| 152 | ", MSG_DONTROUTE|MSG_NOSIGNAL) = %d\n" |
| 153 | " = %u buffers in vector 0\n" |
| 154 | " * %u bytes in buffer 0\n" |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 155 | " | 00000 %-49s %-16s |\n" |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 156 | " * %u bytes in buffer 1\n" |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 157 | " | 00000 %-49s %-16s |\n" |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 158 | " = %u buffers in vector 1\n" |
| 159 | " * %u bytes in buffer 0\n" |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 160 | " | 00000 %-49s %-16s |\n", |
| 161 | ARRAY_SIZE(w0_iov_), w0_c, LENGTH_OF(w0_c), |
| 162 | w1_c, LENGTH_OF(w1_c), |
| 163 | LENGTH_OF(w0_c) + LENGTH_OF(w1_c), |
| 164 | ARRAY_SIZE(w1_iov_), w2_c, LENGTH_OF(w2_c), LENGTH_OF(w2_c), |
| 165 | n_w_mmh, r, |
| 166 | ARRAY_SIZE(w0_iov_), LENGTH_OF(w0_c), w0_d, w0_c, |
| 167 | LENGTH_OF(w1_c), w1_d, w1_c, |
| 168 | ARRAY_SIZE(w1_iov_), LENGTH_OF(w2_c), w2_d, w2_c); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 169 | |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 170 | const unsigned int w_len = |
| 171 | LENGTH_OF(w0_c) + LENGTH_OF(w1_c) + LENGTH_OF(w2_c); |
| 172 | const unsigned int r_len = (w_len + 1) / 2; |
| 173 | void *r0 = tail_alloc(r_len); |
| 174 | void *r1 = tail_alloc(r_len); |
| 175 | void *r2 = tail_alloc(r_len); |
| 176 | const struct iovec r0_iov_[] = { |
| 177 | { |
| 178 | .iov_base = r0, |
| 179 | .iov_len = r_len |
| 180 | } |
| 181 | }; |
| 182 | struct iovec *r0_iov = tail_memdup(r0_iov_, sizeof(r0_iov_)); |
| 183 | const struct iovec r1_iov_[] = { |
| 184 | { |
| 185 | .iov_base = r1, |
| 186 | .iov_len = r_len |
| 187 | }, |
| 188 | { |
| 189 | .iov_base = r2, |
| 190 | .iov_len = r_len |
| 191 | } |
| 192 | }; |
| 193 | struct iovec *r1_iov = tail_memdup(r1_iov_, sizeof(r1_iov_)); |
| 194 | |
| 195 | const struct mmsghdr r_mmh_[] = { |
| 196 | { |
| 197 | .msg_hdr = { |
| 198 | .msg_iov = r0_iov, |
| 199 | .msg_iovlen = ARRAY_SIZE(r0_iov_), |
| 200 | } |
| 201 | }, { |
| 202 | .msg_hdr = { |
| 203 | .msg_iov = r1_iov, |
| 204 | .msg_iovlen = ARRAY_SIZE(r1_iov_), |
| 205 | } |
| 206 | } |
| 207 | }; |
| 208 | void *r_mmh = tail_memdup(r_mmh_, sizeof(r_mmh_)); |
| 209 | const unsigned int n_r_mmh = ARRAY_SIZE(r_mmh_); |
| 210 | |
| 211 | static const char r0_c[] = "01234567"; |
| 212 | const char *r0_d = hexdump_strdup(r0_c); |
| 213 | static const char r1_c[] = "89abcde"; |
| 214 | const char *r1_d = hexdump_strdup(r1_c); |
| 215 | |
| 216 | assert(recv_mmsg(0, r_mmh, n_r_mmh, MSG_DONTWAIT, NULL) == (int) n_r_mmh); |
| 217 | assert(close(0) == 0); |
| 218 | tprintf("recvmmsg(0, {{{msg_name(0)=NULL, msg_iov(%u)=[{\"%s\", %u}]" |
| 219 | ", msg_controllen=0, msg_flags=0}, %u}" |
| 220 | ", {{msg_name(0)=NULL, msg_iov(%u)=[{\"%s\", %u}, {\"\", %u}]" |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 221 | ", msg_controllen=0, msg_flags=0}, %u}}, %u" |
| 222 | ", MSG_DONTWAIT, NULL) = %d (left NULL)\n" |
| 223 | " = %u buffers in vector 0\n" |
| 224 | " * %u bytes in buffer 0\n" |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 225 | " | 00000 %-49s %-16s |\n" |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 226 | " = %u buffers in vector 1\n" |
| 227 | " * %u bytes in buffer 0\n" |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 228 | " | 00000 %-49s %-16s |\n", |
| 229 | ARRAY_SIZE(r0_iov_), r0_c, r_len, LENGTH_OF(r0_c), |
| 230 | ARRAY_SIZE(r1_iov_), r1_c, r_len, r_len, LENGTH_OF(r1_c), |
| 231 | n_r_mmh, r, |
| 232 | ARRAY_SIZE(r0_iov_), LENGTH_OF(r0_c), r0_d, r0_c, |
| 233 | ARRAY_SIZE(r1_iov_), LENGTH_OF(r1_c), r1_d, r1_c); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 234 | |
Dmitry V. Levin | 536a035 | 2016-01-20 02:06:59 +0000 | [diff] [blame] | 235 | tprintf("+++ exited with 0 +++\n"); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 236 | return 0; |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 237 | } |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 238 | |
| 239 | #else |
| 240 | |
Dmitry V. Levin | eb4649a | 2016-01-12 04:37:06 +0000 | [diff] [blame] | 241 | SKIP_MAIN_UNDEFINED("(__NR_sendmmsg || HAVE_SENDMMSG) && (__NR_recvmmsg || HAVE_RECVMMSG)") |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 242 | |
| 243 | #endif |