1 /* Copyright libuv contributors. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22 #include "uv.h"
23 #include "task.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #define CHECK_HANDLE(handle) \
30 ASSERT_NE((uv_udp_t*)(handle) == &recver || (uv_udp_t*)(handle) == &sender, 0)
31
32 #define BUFFER_MULTIPLIER 20
33 #define MAX_DGRAM_SIZE (64 * 1024)
34 #define NUM_SENDS 40
35
36 static uv_udp_t recver;
37 static uv_udp_t sender;
38 static int recv_cb_called;
39 static int received_datagrams;
40 static int read_bytes;
41 static int close_cb_called;
42 static int alloc_cb_called;
43
44
alloc_cb(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)45 static void alloc_cb(uv_handle_t* handle,
46 size_t suggested_size,
47 uv_buf_t* buf) {
48 size_t buffer_size;
49 CHECK_HANDLE(handle);
50
51 /* Only allocate enough room for multiple dgrams if we can actually recv them */
52 buffer_size = MAX_DGRAM_SIZE;
53 if (uv_udp_using_recvmmsg((uv_udp_t*)handle))
54 buffer_size *= BUFFER_MULTIPLIER;
55
56 /* Actually malloc to exercise free'ing the buffer later */
57 buf->base = malloc(buffer_size);
58 ASSERT_NOT_NULL(buf->base);
59 buf->len = buffer_size;
60 alloc_cb_called++;
61 }
62
63
close_cb(uv_handle_t * handle)64 static void close_cb(uv_handle_t* handle) {
65 CHECK_HANDLE(handle);
66 ASSERT(uv_is_closing(handle));
67 close_cb_called++;
68 }
69
70
recv_cb(uv_udp_t * handle,ssize_t nread,const uv_buf_t * rcvbuf,const struct sockaddr * addr,unsigned flags)71 static void recv_cb(uv_udp_t* handle,
72 ssize_t nread,
73 const uv_buf_t* rcvbuf,
74 const struct sockaddr* addr,
75 unsigned flags) {
76 ASSERT_GE(nread, 0);
77 read_bytes += nread;
78
79 /* free and return if this is a mmsg free-only callback invocation */
80 if (flags & UV_UDP_MMSG_FREE) {
81 ASSERT_OK(nread);
82 ASSERT_NULL(addr);
83 free(rcvbuf->base);
84 return;
85 }
86
87 if (nread == 0) {
88 /* There can be no more available data for the time being. */
89 ASSERT_NULL(addr);
90 } else {
91 ASSERT_EQ(4, nread);
92 ASSERT_NOT_NULL(addr);
93 ASSERT_MEM_EQ("PING", rcvbuf->base, nread);
94 received_datagrams++;
95 }
96
97 recv_cb_called++;
98 if (received_datagrams == NUM_SENDS) {
99 uv_close((uv_handle_t*) handle, close_cb);
100 uv_close((uv_handle_t*) &sender, close_cb);
101 }
102
103 /* Don't free if the buffer could be reused via mmsg */
104 if (rcvbuf && !(flags & UV_UDP_MMSG_CHUNK))
105 free(rcvbuf->base);
106 }
107
108
TEST_IMPL(udp_mmsg)109 TEST_IMPL(udp_mmsg) {
110 struct sockaddr_in addr;
111 uv_buf_t buf;
112 int i;
113
114 ASSERT_OK(uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
115
116 ASSERT_OK(uv_udp_init_ex(uv_default_loop(), &recver,
117 AF_UNSPEC | UV_UDP_RECVMMSG));
118
119 ASSERT_OK(uv_udp_bind(&recver, (const struct sockaddr*) &addr, 0));
120
121 ASSERT_OK(uv_udp_recv_start(&recver, alloc_cb, recv_cb));
122
123 ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
124
125 ASSERT_OK(uv_udp_init(uv_default_loop(), &sender));
126
127 buf = uv_buf_init("PING", 4);
128 for (i = 0; i < NUM_SENDS; i++) {
129 ASSERT_EQ(4, uv_udp_try_send(&sender, &buf, 1, (const struct sockaddr*) &addr));
130 }
131
132 ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
133
134 ASSERT_EQ(2, close_cb_called);
135 ASSERT_EQ(received_datagrams, NUM_SENDS);
136
137 ASSERT_OK(sender.send_queue_size);
138 ASSERT_OK(recver.send_queue_size);
139
140 printf("%d allocs for %d recvs\n", alloc_cb_called, recv_cb_called);
141
142 /* On platforms that don't support mmsg, each recv gets its own alloc */
143 if (uv_udp_using_recvmmsg(&recver))
144 ASSERT_EQ(read_bytes, NUM_SENDS * 4); /* we're sending 4 bytes per datagram */
145 else
146 ASSERT_EQ(alloc_cb_called, recv_cb_called);
147
148 MAKE_VALGRIND_HAPPY(uv_default_loop());
149 return 0;
150 }
151