t_recvmmsg.c revision 1.1.4.2 1 1.1.4.2 yamt /* $NetBSD: t_recvmmsg.c,v 1.1.4.2 2012/10/30 19:00:02 yamt Exp $ */
2 1.1.4.2 yamt
3 1.1.4.2 yamt /*-
4 1.1.4.2 yamt * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 1.1.4.2 yamt * All rights reserved.
6 1.1.4.2 yamt *
7 1.1.4.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.1.4.2 yamt * by Jared McNeill and Christos Zoulas.
9 1.1.4.2 yamt *
10 1.1.4.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.1.4.2 yamt * modification, are permitted provided that the following conditions
12 1.1.4.2 yamt * are met:
13 1.1.4.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.1.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.1.4.2 yamt * documentation and/or other materials provided with the distribution.
18 1.1.4.2 yamt * 3. All advertising materials mentioning features or use of this software
19 1.1.4.2 yamt * must display the following acknowledgement:
20 1.1.4.2 yamt * This product includes software developed by the NetBSD
21 1.1.4.2 yamt * Foundation, Inc. and its contributors.
22 1.1.4.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.4.2 yamt * contributors may be used to endorse or promote products derived
24 1.1.4.2 yamt * from this software without specific prior written permission.
25 1.1.4.2 yamt *
26 1.1.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.4.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.4.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.4.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.4.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.4.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.4.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.4.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.4.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.4.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.4.2 yamt * POSSIBILITY OF SUCH DAMAGE.
37 1.1.4.2 yamt */
38 1.1.4.2 yamt #include <sys/cdefs.h>
39 1.1.4.2 yamt __RCSID("$NetBSD: t_recvmmsg.c,v 1.1.4.2 2012/10/30 19:00:02 yamt Exp $");
40 1.1.4.2 yamt
41 1.1.4.2 yamt #include <atf-c.h>
42 1.1.4.2 yamt #include <sys/types.h>
43 1.1.4.2 yamt #include <sys/socket.h>
44 1.1.4.2 yamt #include <sys/wait.h>
45 1.1.4.2 yamt
46 1.1.4.2 yamt #include <string.h>
47 1.1.4.2 yamt #include <time.h>
48 1.1.4.2 yamt #include <stdint.h>
49 1.1.4.2 yamt #include <errno.h>
50 1.1.4.2 yamt #include <stdio.h>
51 1.1.4.2 yamt #include <stdlib.h>
52 1.1.4.2 yamt #include <unistd.h>
53 1.1.4.2 yamt #include <sched.h>
54 1.1.4.2 yamt
55 1.1.4.2 yamt #define BUFSIZE 65536
56 1.1.4.2 yamt #define NPKTS 50
57 1.1.4.2 yamt
58 1.1.4.2 yamt #define min(a, b) ((a) < (b) ? (a) : (b))
59 1.1.4.2 yamt static int debug;
60 1.1.4.2 yamt
61 1.1.4.2 yamt
62 1.1.4.2 yamt ATF_TC(recvmmsg_basic);
63 1.1.4.2 yamt ATF_TC_HEAD(recvmmsg_basic, tc)
64 1.1.4.2 yamt {
65 1.1.4.2 yamt atf_tc_set_md_var(tc, "descr", "A basic test of recvmmsg(2)");
66 1.1.4.2 yamt }
67 1.1.4.2 yamt
68 1.1.4.2 yamt ATF_TC_BODY(recvmmsg_basic, tc)
69 1.1.4.2 yamt {
70 1.1.4.2 yamt int fd[2], error, i, cnt;
71 1.1.4.2 yamt uint8_t *buf;
72 1.1.4.2 yamt struct mmsghdr *mmsghdr;
73 1.1.4.2 yamt struct iovec *iov;
74 1.1.4.2 yamt unsigned int mmsgcnt, n;
75 1.1.4.2 yamt int status;
76 1.1.4.2 yamt off_t off;
77 1.1.4.2 yamt uint8_t DGRAM[1316] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, };
78 1.1.4.2 yamt
79 1.1.4.2 yamt error = socketpair(AF_UNIX, SOCK_DGRAM, 0, fd);
80 1.1.4.2 yamt ATF_REQUIRE_MSG(error != -1, "socketpair failed (%s)", strerror(errno));
81 1.1.4.2 yamt
82 1.1.4.2 yamt buf = malloc(BUFSIZE);
83 1.1.4.2 yamt ATF_REQUIRE_MSG(buf != NULL, "malloc failed (%s)", strerror(errno));
84 1.1.4.2 yamt
85 1.1.4.2 yamt mmsgcnt = BUFSIZE / sizeof(DGRAM);
86 1.1.4.2 yamt mmsghdr = malloc(sizeof(*mmsghdr) * mmsgcnt);
87 1.1.4.2 yamt ATF_REQUIRE_MSG(mmsghdr != NULL, "malloc failed (%s)", strerror(errno));
88 1.1.4.2 yamt iov = malloc(sizeof(*iov) * mmsgcnt);
89 1.1.4.2 yamt ATF_REQUIRE_MSG(iov != NULL, "malloc failed (%s)", strerror(errno));
90 1.1.4.2 yamt
91 1.1.4.2 yamt for (off = 0, n = 0; n < mmsgcnt; n++) {
92 1.1.4.2 yamt iov[n].iov_base = buf + off;
93 1.1.4.2 yamt iov[n].iov_len = sizeof(DGRAM);
94 1.1.4.2 yamt off += iov[n].iov_len;
95 1.1.4.2 yamt mmsghdr[n].msg_hdr.msg_iov = &iov[n];
96 1.1.4.2 yamt mmsghdr[n].msg_hdr.msg_iovlen = 1;
97 1.1.4.2 yamt mmsghdr[n].msg_hdr.msg_name = NULL;
98 1.1.4.2 yamt mmsghdr[n].msg_hdr.msg_namelen = 0;
99 1.1.4.2 yamt }
100 1.1.4.2 yamt
101 1.1.4.2 yamt switch (fork()) {
102 1.1.4.2 yamt case -1:
103 1.1.4.2 yamt ATF_REQUIRE_MSG(0, "fork failed (%s)", strerror(errno));
104 1.1.4.2 yamt break;
105 1.1.4.2 yamt
106 1.1.4.2 yamt case 0:
107 1.1.4.2 yamt n = NPKTS;
108 1.1.4.2 yamt if (debug)
109 1.1.4.2 yamt printf("waiting for %u messages (max %u per syscall)\n", n,
110 1.1.4.2 yamt mmsgcnt);
111 1.1.4.2 yamt while (n > 0) {
112 1.1.4.2 yamt struct timespec ts = { 1, 0 };
113 1.1.4.2 yamt cnt = recvmmsg(fd[1], mmsghdr, min(mmsgcnt, n),
114 1.1.4.2 yamt MSG_WAITALL, &ts);
115 1.1.4.2 yamt ATF_REQUIRE_MSG(cnt != -1, "recvmmsg failed (%s)",
116 1.1.4.2 yamt strerror(errno));
117 1.1.4.2 yamt ATF_REQUIRE_MSG(cnt != 0, "recvmmsg timeout");
118 1.1.4.2 yamt if (debug)
119 1.1.4.2 yamt printf("recvmmsg: got %u messages\n", cnt);
120 1.1.4.2 yamt for (i = 0; i < cnt; i++) {
121 1.1.4.2 yamt ATF_CHECK_EQ_MSG(mmsghdr[i].msg_len,
122 1.1.4.2 yamt sizeof(DGRAM), "packet length");
123 1.1.4.2 yamt ATF_CHECK_EQ_MSG(
124 1.1.4.2 yamt ((uint8_t *)iov[i].iov_base)[0],
125 1.1.4.2 yamt NPKTS - n + i, "packet contents");
126 1.1.4.2 yamt }
127 1.1.4.2 yamt n -= cnt;
128 1.1.4.2 yamt }
129 1.1.4.2 yamt if (debug)
130 1.1.4.2 yamt printf("done!\n");
131 1.1.4.2 yamt exit(0);
132 1.1.4.2 yamt /*NOTREACHED*/
133 1.1.4.2 yamt default:
134 1.1.4.2 yamt sched_yield();
135 1.1.4.2 yamt
136 1.1.4.2 yamt for (n = 0; n < NPKTS; n++) {
137 1.1.4.2 yamt if (debug)
138 1.1.4.2 yamt printf("sending packet %u/%u...\n", (n+1),
139 1.1.4.2 yamt NPKTS);
140 1.1.4.2 yamt do {
141 1.1.4.2 yamt DGRAM[0] = n;
142 1.1.4.2 yamt error = send(fd[0], DGRAM, sizeof(DGRAM), 0);
143 1.1.4.2 yamt } while (error == -1 && errno == ENOBUFS);
144 1.1.4.2 yamt if (error == -1)
145 1.1.4.2 yamt ATF_REQUIRE_MSG(error != -1, "send failed (%s)",
146 1.1.4.2 yamt strerror(errno));
147 1.1.4.2 yamt }
148 1.1.4.2 yamt error = wait(&status);
149 1.1.4.2 yamt ATF_REQUIRE_MSG(error != -1, "wait failed (%s)",
150 1.1.4.2 yamt strerror(errno));
151 1.1.4.2 yamt break;
152 1.1.4.2 yamt }
153 1.1.4.2 yamt }
154 1.1.4.2 yamt
155 1.1.4.2 yamt ATF_TP_ADD_TCS(tp)
156 1.1.4.2 yamt {
157 1.1.4.2 yamt
158 1.1.4.2 yamt ATF_TP_ADD_TC(tp, recvmmsg_basic);
159 1.1.4.2 yamt
160 1.1.4.2 yamt return atf_no_error();
161 1.1.4.2 yamt }
162