1 /* $NetBSD: t_recvmmsg.c,v 1.1 2012/06/22 18:45:23 christos Exp $ */
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jared McNeill and Christos Zoulas.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: t_recvmmsg.c,v 1.1 2012/06/22 18:45:23 christos Exp $");
42 #include <sys/types.h>
43 #include <sys/socket.h>
58 #define min(a, b) ((a) < (b) ? (a) : (b))
62 ATF_TC(recvmmsg_basic
);
63 ATF_TC_HEAD(recvmmsg_basic
, tc
)
65 atf_tc_set_md_var(tc
, "descr", "A basic test of recvmmsg(2)");
68 ATF_TC_BODY(recvmmsg_basic
, tc
)
70 int fd
[2], error
, i
, cnt
;
72 struct mmsghdr
*mmsghdr
;
74 unsigned int mmsgcnt
, n
;
77 uint8_t DGRAM
[1316] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, };
79 error
= socketpair(AF_UNIX
, SOCK_DGRAM
, 0, fd
);
80 ATF_REQUIRE_MSG(error
!= -1, "socketpair failed (%s)", strerror(errno
));
82 buf
= malloc(BUFSIZE
);
83 ATF_REQUIRE_MSG(buf
!= NULL
, "malloc failed (%s)", strerror(errno
));
85 mmsgcnt
= BUFSIZE
/ sizeof(DGRAM
);
86 mmsghdr
= malloc(sizeof(*mmsghdr
) * mmsgcnt
);
87 ATF_REQUIRE_MSG(mmsghdr
!= NULL
, "malloc failed (%s)", strerror(errno
));
88 iov
= malloc(sizeof(*iov
) * mmsgcnt
);
89 ATF_REQUIRE_MSG(iov
!= NULL
, "malloc failed (%s)", strerror(errno
));
91 for (off
= 0, n
= 0; n
< mmsgcnt
; n
++) {
92 iov
[n
].iov_base
= buf
+ off
;
93 iov
[n
].iov_len
= sizeof(DGRAM
);
94 off
+= iov
[n
].iov_len
;
95 mmsghdr
[n
].msg_hdr
.msg_iov
= &iov
[n
];
96 mmsghdr
[n
].msg_hdr
.msg_iovlen
= 1;
97 mmsghdr
[n
].msg_hdr
.msg_name
= NULL
;
98 mmsghdr
[n
].msg_hdr
.msg_namelen
= 0;
103 ATF_REQUIRE_MSG(0, "fork failed (%s)", strerror(errno
));
109 printf("waiting for %u messages (max %u per syscall)\n", n
,
112 struct timespec ts
= { 1, 0 };
113 cnt
= recvmmsg(fd
[1], mmsghdr
, min(mmsgcnt
, n
),
115 ATF_REQUIRE_MSG(cnt
!= -1, "recvmmsg failed (%s)",
117 ATF_REQUIRE_MSG(cnt
!= 0, "recvmmsg timeout");
119 printf("recvmmsg: got %u messages\n", cnt
);
120 for (i
= 0; i
< cnt
; i
++) {
121 ATF_CHECK_EQ_MSG(mmsghdr
[i
].msg_len
,
122 sizeof(DGRAM
), "packet length");
124 ((uint8_t *)iov
[i
].iov_base
)[0],
125 NPKTS
- n
+ i
, "packet contents");
136 for (n
= 0; n
< NPKTS
; n
++) {
138 printf("sending packet %u/%u...\n", (n
+1),
142 error
= send(fd
[0], DGRAM
, sizeof(DGRAM
), 0);
143 } while (error
== -1 && errno
== ENOBUFS
);
145 ATF_REQUIRE_MSG(error
!= -1, "send failed (%s)",
148 error
= wait(&status
);
149 ATF_REQUIRE_MSG(error
!= -1, "wait failed (%s)",
158 ATF_TP_ADD_TC(tp
, recvmmsg_basic
);
160 return atf_no_error();