blob: 1e0d37d72204fb50de6338dc7ff22d32149d5202 [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);
148 tprintf("sendmmsg(1, {{{msg_name(0)=NULL, msg_iov(%u)=[{\"%s\", %u}"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +0000149 ", {\"%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. Levind44707d2016-01-20 04:56:25 +0000155 " | 00000 %-49s %-16s |\n"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +0000156 " * %u bytes in buffer 1\n"
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000157 " | 00000 %-49s %-16s |\n"
Dmitry V. Levin19d10f82016-01-14 00:06:20 +0000158 " = %u buffers in vector 1\n"
159 " * %u bytes in buffer 0\n"
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000160 " | 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 YAMATO993198d2014-11-07 01:23:27 +0900169
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000170 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. 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",
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 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