blob: 85e61d5c2ab864c0325119b2bdafc58598134f1a [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>
39# include <sys/socket.h>
Masatake YAMATO993198d2014-11-07 01:23:27 +090040
Dmitry V. Levin19d10f82016-01-14 00:06:20 +000041# ifndef HAVE_STRUCT_MMSGHDR
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000042struct mmsghdr {
43 struct msghdr msg_hdr;
44 unsigned msg_len;
45};
Dmitry V. Levin19d10f82016-01-14 00:06:20 +000046# endif
47
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000048static int
49send_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. Levin536a0352016-01-20 02:06:59 +000057 tprintf("sendmmsg(%d, %p, %u, MSG_DONTROUTE|MSG_NOSIGNAL)"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +000058 " = -1 ENOSYS (%m)\n", fd, vec, vlen);
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000059#endif
60#ifdef HAVE_SENDMMSG
61 rc = sendmmsg(fd, vec, vlen, flags);
62#endif
63 return rc;
64}
65
66static int
67recv_mmsg(int fd, struct mmsghdr *vec, unsigned int vlen, unsigned int flags,
68 struct timespec *timeout)
69{
70 int rc;
Dmitry V. Levineb4649a2016-01-12 04:37:06 +000071#ifdef __NR_recvmmsg
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000072 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. Levin536a0352016-01-20 02:06:59 +000076 tprintf("recvmmsg(%d, %p, %u, MSG_DONTWAIT, NULL)"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +000077 " = -1 ENOSYS (%m)\n", fd, vec, vlen);
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000078#endif
Dmitry V. Levineb4649a2016-01-12 04:37:06 +000079#ifdef HAVE_RECVMMSG
Dmitry V. Levin6e815ce2016-01-11 00:16:39 +000080 rc = recvmmsg(fd, vec, vlen, flags, timeout);
81#endif
82 return rc;
83}
84
Masatake YAMATO993198d2014-11-07 01:23:27 +090085int
86main(void)
87{
Dmitry V. Levin536a0352016-01-20 02:06:59 +000088 tprintf("%s", "");
89
Dmitry V. Levind44707d2016-01-20 04:56:25 +000090 int fds[2];
91 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds))
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +000092 perror_msg_and_skip("socketpair");
Dmitry V. Levind44707d2016-01-20 04:56:25 +000093 assert(0 == fds[0]);
94 assert(1 == fds[1]);
Dmitry V. Levin35eb03f2016-01-11 00:17:36 +000095
Dmitry V. Levind44707d2016-01-20 04:56:25 +000096 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. Levin19d10f82016-01-14 00:06:20 +000099
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000100 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 YAMATO993198d2014-11-07 01:23:27 +0900103
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000104 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 YAMATO993198d2014-11-07 01:23:27 +0900109 {
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000110 .iov_base = w0,
111 .iov_len = LENGTH_OF(w0_c)
Masatake YAMATO993198d2014-11-07 01:23:27 +0900112 }, {
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000113 .iov_base = w1,
114 .iov_len = LENGTH_OF(w1_c)
Masatake YAMATO993198d2014-11-07 01:23:27 +0900115 }
116 };
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000117 struct iovec *w0_iov = tail_memdup(w0_iov_, sizeof(w0_iov_));
Masatake YAMATO993198d2014-11-07 01:23:27 +0900118
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000119 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 YAMATO993198d2014-11-07 01:23:27 +0900128 {
129 .msg_hdr = {
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000130 .msg_iov = w0_iov,
131 .msg_iovlen = ARRAY_SIZE(w0_iov_),
Masatake YAMATO993198d2014-11-07 01:23:27 +0900132 }
133 }, {
134 .msg_hdr = {
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000135 .msg_iov = w1_iov,
136 .msg_iovlen = ARRAY_SIZE(w1_iov_),
Masatake YAMATO993198d2014-11-07 01:23:27 +0900137 }
138 }
139 };
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000140 void *w_mmh = tail_memdup(w_mmh_, sizeof(w_mmh_));
141 const unsigned int n_w_mmh = ARRAY_SIZE(w_mmh_);
Masatake YAMATO993198d2014-11-07 01:23:27 +0900142
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000143 int r = send_mmsg(1, w_mmh, n_w_mmh, MSG_DONTROUTE | MSG_NOSIGNAL);
Dmitry V. Levin95cbf6e2015-01-14 12:47:45 +0000144 if (r < 0 && errno == ENOSYS)
Dmitry V. Levin4e666722016-01-06 11:43:08 +0000145 perror_msg_and_skip("sendmmsg");
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000146 assert(r == (int) n_w_mmh);
147 assert(close(1) == 0);
Dmitry V. Levin26f90af2016-06-26 23:57:39 +0000148 tprintf("sendmmsg(1, {{{msg_name=NULL, msg_namelen=0"
149 ", msg_iov=[{\"%s\", %u}, {\"%s\", %u}], msg_iovlen=%u"
150 ", msg_controllen=0, msg_flags=0}, %u}"
151 ", {{msg_name=NULL, msg_namelen=0"
152 ", msg_iov=[{\"%s\", %u}], msg_iovlen=%u"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +0000153 ", msg_controllen=0, msg_flags=0}, %u}}, %u"
154 ", MSG_DONTROUTE|MSG_NOSIGNAL) = %d\n"
155 " = %u buffers in vector 0\n"
156 " * %u bytes in buffer 0\n"
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000157 " | 00000 %-49s %-16s |\n"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +0000158 " * %u bytes in buffer 1\n"
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000159 " | 00000 %-49s %-16s |\n"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +0000160 " = %u buffers in vector 1\n"
161 " * %u bytes in buffer 0\n"
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000162 " | 00000 %-49s %-16s |\n",
Dmitry V. Levin26f90af2016-06-26 23:57:39 +0000163 w0_c, LENGTH_OF(w0_c),
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000164 w1_c, LENGTH_OF(w1_c),
Dmitry V. Levin26f90af2016-06-26 23:57:39 +0000165 ARRAY_SIZE(w0_iov_),
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000166 LENGTH_OF(w0_c) + LENGTH_OF(w1_c),
Dmitry V. Levin26f90af2016-06-26 23:57:39 +0000167 w2_c, LENGTH_OF(w2_c), ARRAY_SIZE(w1_iov_),
168 LENGTH_OF(w2_c),
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000169 n_w_mmh, r,
170 ARRAY_SIZE(w0_iov_), LENGTH_OF(w0_c), w0_d, w0_c,
171 LENGTH_OF(w1_c), w1_d, w1_c,
172 ARRAY_SIZE(w1_iov_), LENGTH_OF(w2_c), w2_d, w2_c);
Masatake YAMATO993198d2014-11-07 01:23:27 +0900173
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000174 const unsigned int w_len =
175 LENGTH_OF(w0_c) + LENGTH_OF(w1_c) + LENGTH_OF(w2_c);
176 const unsigned int r_len = (w_len + 1) / 2;
177 void *r0 = tail_alloc(r_len);
178 void *r1 = tail_alloc(r_len);
179 void *r2 = tail_alloc(r_len);
180 const struct iovec r0_iov_[] = {
181 {
182 .iov_base = r0,
183 .iov_len = r_len
184 }
185 };
186 struct iovec *r0_iov = tail_memdup(r0_iov_, sizeof(r0_iov_));
187 const struct iovec r1_iov_[] = {
188 {
189 .iov_base = r1,
190 .iov_len = r_len
191 },
192 {
193 .iov_base = r2,
194 .iov_len = r_len
195 }
196 };
197 struct iovec *r1_iov = tail_memdup(r1_iov_, sizeof(r1_iov_));
198
199 const struct mmsghdr r_mmh_[] = {
200 {
201 .msg_hdr = {
202 .msg_iov = r0_iov,
203 .msg_iovlen = ARRAY_SIZE(r0_iov_),
204 }
205 }, {
206 .msg_hdr = {
207 .msg_iov = r1_iov,
208 .msg_iovlen = ARRAY_SIZE(r1_iov_),
209 }
210 }
211 };
212 void *r_mmh = tail_memdup(r_mmh_, sizeof(r_mmh_));
213 const unsigned int n_r_mmh = ARRAY_SIZE(r_mmh_);
214
215 static const char r0_c[] = "01234567";
216 const char *r0_d = hexdump_strdup(r0_c);
217 static const char r1_c[] = "89abcde";
218 const char *r1_d = hexdump_strdup(r1_c);
219
220 assert(recv_mmsg(0, r_mmh, n_r_mmh, MSG_DONTWAIT, NULL) == (int) n_r_mmh);
221 assert(close(0) == 0);
Dmitry V. Levin26f90af2016-06-26 23:57:39 +0000222 tprintf("recvmmsg(0, {{{msg_name=NULL, msg_namelen=0"
223 ", msg_iov=[{\"%s\", %u}], msg_iovlen=%u"
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000224 ", msg_controllen=0, msg_flags=0}, %u}"
Dmitry V. Levin26f90af2016-06-26 23:57:39 +0000225 ", {{msg_name=NULL, msg_namelen=0"
226 ", msg_iov=[{\"%s\", %u}, {\"\", %u}], msg_iovlen=%u"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +0000227 ", msg_controllen=0, msg_flags=0}, %u}}, %u"
228 ", MSG_DONTWAIT, NULL) = %d (left NULL)\n"
229 " = %u buffers in vector 0\n"
230 " * %u bytes in buffer 0\n"
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000231 " | 00000 %-49s %-16s |\n"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +0000232 " = %u buffers in vector 1\n"
233 " * %u bytes in buffer 0\n"
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000234 " | 00000 %-49s %-16s |\n",
Dmitry V. Levin26f90af2016-06-26 23:57:39 +0000235 r0_c, r_len, ARRAY_SIZE(r0_iov_), LENGTH_OF(r0_c),
236 r1_c, r_len, r_len, ARRAY_SIZE(r1_iov_), LENGTH_OF(r1_c),
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000237 n_r_mmh, r,
238 ARRAY_SIZE(r0_iov_), LENGTH_OF(r0_c), r0_d, r0_c,
239 ARRAY_SIZE(r1_iov_), LENGTH_OF(r1_c), r1_d, r1_c);
Masatake YAMATO993198d2014-11-07 01:23:27 +0900240
Dmitry V. Levin536a0352016-01-20 02:06:59 +0000241 tprintf("+++ exited with 0 +++\n");
Masatake YAMATO993198d2014-11-07 01:23:27 +0900242 return 0;
Masatake YAMATO993198d2014-11-07 01:23:27 +0900243}
Dmitry V. Levin4e666722016-01-06 11:43:08 +0000244
245#else
246
Dmitry V. Levineb4649a2016-01-12 04:37:06 +0000247SKIP_MAIN_UNDEFINED("(__NR_sendmmsg || HAVE_SENDMMSG) && (__NR_recvmmsg || HAVE_RECVMMSG)")
Dmitry V. Levin4e666722016-01-06 11:43:08 +0000248
249#endif