blob: 00e1ef33ac4de2a85ce6027332b9070297cd63ae [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
2 * Copyright (c) 2014 Masatake YAMATO <yamato@redhat.com>
Dmitry V. Levin4e666722016-01-06 11:43:08 +00003 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00004 * 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. Levin0c8853c2016-01-02 13:28:43 +000029#include "tests.h"
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000030# include <sys/syscall.h>
Dmitry V. Levin4e666722016-01-06 11:43:08 +000031
Dmitry V. Levineb4649a2016-01-12 04:37:06 +000032#if (defined __NR_sendmmsg || defined HAVE_SENDMMSG) \
33 && (defined __NR_recvmmsg || defined HAVE_RECVMMSG)
Dmitry V. Levin4e666722016-01-06 11:43:08 +000034
35# include <assert.h>
36# include <errno.h>
Dmitry V. Levin19d10f82016-01-14 00:06:20 +000037# include <stdio.h>
Dmitry V. Levin4e666722016-01-06 11:43:08 +000038# include <unistd.h>
Masatake YAMATO993198d2014-11-07 01:23:27 +090039
Dmitry V. Levin4b38ce92016-06-27 00:02:41 +000040# include "msghdr.h"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +000041
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000042static int
43send_mmsg(int fd, struct mmsghdr *vec, unsigned int vlen, unsigned int flags)
44{
45 int rc;
46#ifdef __NR_sendmmsg
47 rc = syscall(__NR_sendmmsg, (long) fd, vec, (unsigned long) vlen,
48 (unsigned long) flags);
49 if (rc >= 0 || ENOSYS != errno)
50 return rc;
Dmitry V. Levin536a0352016-01-20 02:06:59 +000051 tprintf("sendmmsg(%d, %p, %u, MSG_DONTROUTE|MSG_NOSIGNAL)"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +000052 " = -1 ENOSYS (%m)\n", fd, vec, vlen);
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000053#endif
54#ifdef HAVE_SENDMMSG
55 rc = sendmmsg(fd, vec, vlen, flags);
56#endif
57 return rc;
58}
59
60static int
61recv_mmsg(int fd, struct mmsghdr *vec, unsigned int vlen, unsigned int flags,
62 struct timespec *timeout)
63{
64 int rc;
Dmitry V. Levineb4649a2016-01-12 04:37:06 +000065#ifdef __NR_recvmmsg
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000066 rc = syscall(__NR_recvmmsg, (long) fd, vec, (unsigned long) vlen,
67 (unsigned long) flags, timeout);
68 if (rc >= 0 || ENOSYS != errno)
69 return rc;
Dmitry V. Levin536a0352016-01-20 02:06:59 +000070 tprintf("recvmmsg(%d, %p, %u, MSG_DONTWAIT, NULL)"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +000071 " = -1 ENOSYS (%m)\n", fd, vec, vlen);
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000072#endif
Dmitry V. Levineb4649a2016-01-12 04:37:06 +000073#ifdef HAVE_RECVMMSG
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000074 rc = recvmmsg(fd, vec, vlen, flags, timeout);
75#endif
76 return rc;
77}
78
Masatake YAMATO993198d2014-11-07 01:23:27 +090079int
80main(void)
81{
Dmitry V. Levin536a0352016-01-20 02:06:59 +000082 tprintf("%s", "");
83
Dmitry V. Levind44707d2016-01-20 04:56:25 +000084 int fds[2];
85 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds))
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +000086 perror_msg_and_skip("socketpair");
Dmitry V. Levind44707d2016-01-20 04:56:25 +000087 assert(0 == fds[0]);
88 assert(1 == fds[1]);
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +000089
Dmitry V. Levind44707d2016-01-20 04:56:25 +000090 static const char w0_c[] = "012";
91 const char *w0_d = hexdump_strdup(w0_c);
92 void *w0 = tail_memdup(w0_c, LENGTH_OF(w0_c));
Dmitry V. Levin19d10f82016-01-14 00:06:20 +000093
Dmitry V. Levind44707d2016-01-20 04:56:25 +000094 static const char w1_c[] = "34567";
95 const char *w1_d = hexdump_strdup(w1_c);
96 void *w1 = tail_memdup(w1_c, LENGTH_OF(w1_c));
Masatake YAMATO993198d2014-11-07 01:23:27 +090097
Dmitry V. Levind44707d2016-01-20 04:56:25 +000098 static const char w2_c[] = "89abcde";
99 const char *w2_d = hexdump_strdup(w2_c);
100 void *w2 = tail_memdup(w2_c, LENGTH_OF(w2_c));
101
102 const struct iovec w0_iov_[] = {
Masatake YAMATO993198d2014-11-07 01:23:27 +0900103 {
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000104 .iov_base = w0,
105 .iov_len = LENGTH_OF(w0_c)
Masatake YAMATO993198d2014-11-07 01:23:27 +0900106 }, {
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000107 .iov_base = w1,
108 .iov_len = LENGTH_OF(w1_c)
Masatake YAMATO993198d2014-11-07 01:23:27 +0900109 }
110 };
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000111 struct iovec *w0_iov = tail_memdup(w0_iov_, sizeof(w0_iov_));
Masatake YAMATO993198d2014-11-07 01:23:27 +0900112
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000113 const struct iovec w1_iov_[] = {
114 {
115 .iov_base = w2,
116 .iov_len = LENGTH_OF(w2_c)
117 }
118 };
119 struct iovec *w1_iov = tail_memdup(w1_iov_, sizeof(w1_iov_));
120
121 const struct mmsghdr w_mmh_[] = {
Masatake YAMATO993198d2014-11-07 01:23:27 +0900122 {
123 .msg_hdr = {
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000124 .msg_iov = w0_iov,
125 .msg_iovlen = ARRAY_SIZE(w0_iov_),
Masatake YAMATO993198d2014-11-07 01:23:27 +0900126 }
127 }, {
128 .msg_hdr = {
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000129 .msg_iov = w1_iov,
130 .msg_iovlen = ARRAY_SIZE(w1_iov_),
Masatake YAMATO993198d2014-11-07 01:23:27 +0900131 }
132 }
133 };
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000134 void *w_mmh = tail_memdup(w_mmh_, sizeof(w_mmh_));
135 const unsigned int n_w_mmh = ARRAY_SIZE(w_mmh_);
Masatake YAMATO993198d2014-11-07 01:23:27 +0900136
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000137 int r = send_mmsg(1, w_mmh, n_w_mmh, MSG_DONTROUTE | MSG_NOSIGNAL);
Dmitry V. Levin95cbf6e2015-01-14 12:47:45 +0000138 if (r < 0 && errno == ENOSYS)
Dmitry V. Levin4e666722016-01-06 11:43:08 +0000139 perror_msg_and_skip("sendmmsg");
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000140 assert(r == (int) n_w_mmh);
141 assert(close(1) == 0);
Dmitry V. Levin26f90af2016-06-26 23:57:39 +0000142 tprintf("sendmmsg(1, {{{msg_name=NULL, msg_namelen=0"
143 ", msg_iov=[{\"%s\", %u}, {\"%s\", %u}], msg_iovlen=%u"
144 ", msg_controllen=0, msg_flags=0}, %u}"
145 ", {{msg_name=NULL, msg_namelen=0"
146 ", msg_iov=[{\"%s\", %u}], msg_iovlen=%u"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +0000147 ", msg_controllen=0, msg_flags=0}, %u}}, %u"
148 ", MSG_DONTROUTE|MSG_NOSIGNAL) = %d\n"
149 " = %u buffers in vector 0\n"
150 " * %u bytes in buffer 0\n"
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000151 " | 00000 %-49s %-16s |\n"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +0000152 " * %u bytes in buffer 1\n"
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000153 " | 00000 %-49s %-16s |\n"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +0000154 " = %u buffers in vector 1\n"
155 " * %u bytes in buffer 0\n"
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000156 " | 00000 %-49s %-16s |\n",
Dmitry V. Levin26f90af2016-06-26 23:57:39 +0000157 w0_c, LENGTH_OF(w0_c),
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000158 w1_c, LENGTH_OF(w1_c),
Dmitry V. Levin26f90af2016-06-26 23:57:39 +0000159 ARRAY_SIZE(w0_iov_),
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000160 LENGTH_OF(w0_c) + LENGTH_OF(w1_c),
Dmitry V. Levin26f90af2016-06-26 23:57:39 +0000161 w2_c, LENGTH_OF(w2_c), ARRAY_SIZE(w1_iov_),
162 LENGTH_OF(w2_c),
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000163 n_w_mmh, r,
164 ARRAY_SIZE(w0_iov_), LENGTH_OF(w0_c), w0_d, w0_c,
165 LENGTH_OF(w1_c), w1_d, w1_c,
166 ARRAY_SIZE(w1_iov_), LENGTH_OF(w2_c), w2_d, w2_c);
Masatake YAMATO993198d2014-11-07 01:23:27 +0900167
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000168 const unsigned int w_len =
169 LENGTH_OF(w0_c) + LENGTH_OF(w1_c) + LENGTH_OF(w2_c);
170 const unsigned int r_len = (w_len + 1) / 2;
171 void *r0 = tail_alloc(r_len);
172 void *r1 = tail_alloc(r_len);
173 void *r2 = tail_alloc(r_len);
174 const struct iovec r0_iov_[] = {
175 {
176 .iov_base = r0,
177 .iov_len = r_len
178 }
179 };
180 struct iovec *r0_iov = tail_memdup(r0_iov_, sizeof(r0_iov_));
181 const struct iovec r1_iov_[] = {
182 {
183 .iov_base = r1,
184 .iov_len = r_len
185 },
186 {
187 .iov_base = r2,
188 .iov_len = r_len
189 }
190 };
191 struct iovec *r1_iov = tail_memdup(r1_iov_, sizeof(r1_iov_));
192
193 const struct mmsghdr r_mmh_[] = {
194 {
195 .msg_hdr = {
196 .msg_iov = r0_iov,
197 .msg_iovlen = ARRAY_SIZE(r0_iov_),
198 }
199 }, {
200 .msg_hdr = {
201 .msg_iov = r1_iov,
202 .msg_iovlen = ARRAY_SIZE(r1_iov_),
203 }
204 }
205 };
206 void *r_mmh = tail_memdup(r_mmh_, sizeof(r_mmh_));
207 const unsigned int n_r_mmh = ARRAY_SIZE(r_mmh_);
208
209 static const char r0_c[] = "01234567";
210 const char *r0_d = hexdump_strdup(r0_c);
211 static const char r1_c[] = "89abcde";
212 const char *r1_d = hexdump_strdup(r1_c);
213
214 assert(recv_mmsg(0, r_mmh, n_r_mmh, MSG_DONTWAIT, NULL) == (int) n_r_mmh);
215 assert(close(0) == 0);
Dmitry V. Levin26f90af2016-06-26 23:57:39 +0000216 tprintf("recvmmsg(0, {{{msg_name=NULL, msg_namelen=0"
217 ", msg_iov=[{\"%s\", %u}], msg_iovlen=%u"
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000218 ", msg_controllen=0, msg_flags=0}, %u}"
Dmitry V. Levin26f90af2016-06-26 23:57:39 +0000219 ", {{msg_name=NULL, msg_namelen=0"
220 ", msg_iov=[{\"%s\", %u}, {\"\", %u}], msg_iovlen=%u"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +0000221 ", 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. Levind44707d2016-01-20 04:56:25 +0000225 " | 00000 %-49s %-16s |\n"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +0000226 " = %u buffers in vector 1\n"
227 " * %u bytes in buffer 0\n"
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000228 " | 00000 %-49s %-16s |\n",
Dmitry V. Levin26f90af2016-06-26 23:57:39 +0000229 r0_c, r_len, ARRAY_SIZE(r0_iov_), LENGTH_OF(r0_c),
230 r1_c, r_len, r_len, ARRAY_SIZE(r1_iov_), LENGTH_OF(r1_c),
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000231 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 YAMATO993198d2014-11-07 01:23:27 +0900234
Dmitry V. Levin536a0352016-01-20 02:06:59 +0000235 tprintf("+++ exited with 0 +++\n");
Masatake YAMATO993198d2014-11-07 01:23:27 +0900236 return 0;
Masatake YAMATO993198d2014-11-07 01:23:27 +0900237}
Dmitry V. Levin4e666722016-01-06 11:43:08 +0000238
239#else
240
Dmitry V. Levineb4649a2016-01-12 04:37:06 +0000241SKIP_MAIN_UNDEFINED("(__NR_sendmmsg || HAVE_SENDMMSG) && (__NR_recvmmsg || HAVE_RECVMMSG)")
Dmitry V. Levin4e666722016-01-06 11:43:08 +0000242
243#endif