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 #define EXPECTED_MMSG_ALLOCS (NUM_SENDS / BUFFER_MULTIPLIER)
36
37 static uv_udp_t recver;
38 static uv_udp_t sender;
39 static int recv_cb_called;
40 static int received_datagrams;
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
78 /* free and return if this is a mmsg free-only callback invocation */
79 if (flags & UV_UDP_MMSG_FREE) {
80 ASSERT_OK(nread);
81 ASSERT_NULL(addr);
82 free(rcvbuf->base);
83 return;
84 }
85
86 if (nread == 0) {
87 /* There can be no more available data for the time being. */
88 ASSERT_NULL(addr);
89 } else {
90 ASSERT_EQ(4, nread);
91 ASSERT_NOT_NULL(addr);
92 ASSERT_MEM_EQ("PING", rcvbuf->base, nread);
93 received_datagrams++;
94 }
95
96 recv_cb_called++;
97 if (received_datagrams == NUM_SENDS) {
98 uv_close((uv_handle_t*) handle, close_cb);
99 uv_close((uv_handle_t*) &sender, close_cb);
100 }
101
102 /* Don't free if the buffer could be reused via mmsg */
103 if (rcvbuf && !(flags & UV_UDP_MMSG_CHUNK))
104 free(rcvbuf->base);
105 }
106
107
TEST_IMPL(udp_mmsg)108 TEST_IMPL(udp_mmsg) {
109 struct sockaddr_in addr;
110 uv_buf_t buf;
111 int i;
112
113 ASSERT_OK(uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
114
115 ASSERT_OK(uv_udp_init_ex(uv_default_loop(), &recver,
116 AF_UNSPEC | UV_UDP_RECVMMSG));
117
118 ASSERT_OK(uv_udp_bind(&recver, (const struct sockaddr*) &addr, 0));
119
120 ASSERT_OK(uv_udp_recv_start(&recver, alloc_cb, recv_cb));
121
122 ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
123
124 ASSERT_OK(uv_udp_init(uv_default_loop(), &sender));
125
126 buf = uv_buf_init("PING", 4);
127 for (i = 0; i < NUM_SENDS; i++) {
128 ASSERT_EQ(4, uv_udp_try_send(&sender, &buf, 1, (const struct sockaddr*) &addr));
129 }
130
131 ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
132
133 ASSERT_EQ(2, close_cb_called);
134 ASSERT_EQ(received_datagrams, NUM_SENDS);
135
136 ASSERT_OK(sender.send_queue_size);
137 ASSERT_OK(recver.send_queue_size);
138
139 printf("%d allocs for %d recvs\n", alloc_cb_called, recv_cb_called);
140
141 /* On platforms that don't support mmsg, each recv gets its own alloc */
142 if (uv_udp_using_recvmmsg(&recver))
143 ASSERT_EQ(alloc_cb_called, EXPECTED_MMSG_ALLOCS);
144 else
145 ASSERT_EQ(alloc_cb_called, recv_cb_called);
146
147 MAKE_VALGRIND_HAPPY(uv_default_loop());
148 return 0;
149 }
150