• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/iomgr/port.h"
22 
23 #ifdef GRPC_UV
24 #include <limits.h>
25 #include <string.h>
26 
27 #include <grpc/slice_buffer.h>
28 
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
31 #include <grpc/support/string_util.h>
32 
33 #include "src/core/lib/gpr/string.h"
34 #include "src/core/lib/iomgr/error.h"
35 #include "src/core/lib/iomgr/iomgr_custom.h"
36 #include "src/core/lib/iomgr/resolve_address_custom.h"
37 #include "src/core/lib/iomgr/resource_quota.h"
38 #include "src/core/lib/iomgr/tcp_custom.h"
39 #include "src/core/lib/slice/slice_internal.h"
40 #include "src/core/lib/slice/slice_string_helpers.h"
41 
42 #include <uv.h>
43 
44 #define IGNORE_CONST(addr) ((grpc_sockaddr*)(uintptr_t)(addr))
45 
46 typedef struct uv_socket_t {
47   uv_connect_t connect_req;
48   uv_write_t write_req;
49   uv_shutdown_t shutdown_req;
50   uv_tcp_t* handle;
51   uv_buf_t* write_buffers;
52 
53   char* read_buf;
54   size_t read_len;
55 
56   int pending_connections;
57   grpc_custom_socket* accept_socket;
58   grpc_error* accept_error;
59 
60   grpc_custom_connect_callback connect_cb;
61   grpc_custom_write_callback write_cb;
62   grpc_custom_read_callback read_cb;
63   grpc_custom_accept_callback accept_cb;
64   grpc_custom_close_callback close_cb;
65 
66 } uv_socket_t;
67 
tcp_error_create(const char * desc,int status)68 static grpc_error* tcp_error_create(const char* desc, int status) {
69   if (status == 0) {
70     return GRPC_ERROR_NONE;
71   }
72   grpc_error* error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc);
73   /* All tcp errors are marked with UNAVAILABLE so that application may
74    * choose to retry. */
75   error = grpc_error_set_int(error, GRPC_ERROR_INT_GRPC_STATUS,
76                              GRPC_STATUS_UNAVAILABLE);
77   return grpc_error_set_str(error, GRPC_ERROR_STR_OS_ERROR,
78                             grpc_slice_from_static_string(uv_strerror(status)));
79 }
80 
uv_socket_destroy(grpc_custom_socket * socket)81 static void uv_socket_destroy(grpc_custom_socket* socket) {
82   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
83   gpr_free(uv_socket->handle);
84   gpr_free(uv_socket);
85 }
86 
alloc_uv_buf(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)87 static void alloc_uv_buf(uv_handle_t* handle, size_t suggested_size,
88                          uv_buf_t* buf) {
89   uv_socket_t* uv_socket =
90       (uv_socket_t*)((grpc_custom_socket*)handle->data)->impl;
91   (void)suggested_size;
92   buf->base = uv_socket->read_buf;
93   buf->len = uv_socket->read_len;
94 }
95 
uv_read_callback(uv_stream_t * stream,ssize_t nread,const uv_buf_t * buf)96 static void uv_read_callback(uv_stream_t* stream, ssize_t nread,
97                              const uv_buf_t* buf) {
98   grpc_error* error = GRPC_ERROR_NONE;
99   if (nread == 0) {
100     // Nothing happened. Wait for the next callback
101     return;
102   }
103   // TODO(murgatroid99): figure out what the return value here means
104   uv_read_stop(stream);
105   if (nread == UV_EOF) {
106     error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("EOF");
107   } else if (nread < 0) {
108     error = tcp_error_create("TCP Read failed", nread);
109   }
110   grpc_custom_socket* socket = (grpc_custom_socket*)stream->data;
111   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
112   uv_socket->read_cb(socket, (size_t)nread, error);
113 }
114 
uv_close_callback(uv_handle_t * handle)115 static void uv_close_callback(uv_handle_t* handle) {
116   grpc_custom_socket* socket = (grpc_custom_socket*)handle->data;
117   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
118   if (uv_socket->accept_socket) {
119     uv_socket->accept_cb(socket, uv_socket->accept_socket,
120                          GRPC_ERROR_CREATE_FROM_STATIC_STRING("socket closed"));
121   }
122   uv_socket->close_cb(socket);
123 }
124 
uv_socket_read(grpc_custom_socket * socket,char * buffer,size_t length,grpc_custom_read_callback read_cb)125 static void uv_socket_read(grpc_custom_socket* socket, char* buffer,
126                            size_t length, grpc_custom_read_callback read_cb) {
127   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
128   int status;
129   grpc_error* error;
130   uv_socket->read_cb = read_cb;
131   uv_socket->read_buf = buffer;
132   uv_socket->read_len = length;
133   // TODO(murgatroid99): figure out what the return value here means
134   status =
135       uv_read_start((uv_stream_t*)uv_socket->handle, (uv_alloc_cb)alloc_uv_buf,
136                     (uv_read_cb)uv_read_callback);
137   if (status != 0) {
138     error = tcp_error_create("TCP Read failed at start", status);
139     uv_socket->read_cb(socket, 0, error);
140   }
141 }
142 
uv_write_callback(uv_write_t * req,int status)143 static void uv_write_callback(uv_write_t* req, int status) {
144   grpc_custom_socket* socket = (grpc_custom_socket*)req->data;
145   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
146   gpr_free(uv_socket->write_buffers);
147   uv_socket->write_cb(socket, tcp_error_create("TCP Write failed", status));
148 }
149 
uv_socket_write(grpc_custom_socket * socket,grpc_slice_buffer * write_slices,grpc_custom_write_callback write_cb)150 void uv_socket_write(grpc_custom_socket* socket,
151                      grpc_slice_buffer* write_slices,
152                      grpc_custom_write_callback write_cb) {
153   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
154   uv_socket->write_cb = write_cb;
155   uv_buf_t* uv_buffers;
156   uv_write_t* write_req;
157 
158   uv_buffers = (uv_buf_t*)gpr_malloc(sizeof(uv_buf_t) * write_slices->count);
159   for (size_t i = 0; i < write_slices->count; i++) {
160     uv_buffers[i].base = (char*)GRPC_SLICE_START_PTR(write_slices->slices[i]);
161     uv_buffers[i].len = GRPC_SLICE_LENGTH(write_slices->slices[i]);
162   }
163 
164   uv_socket->write_buffers = uv_buffers;
165   write_req = &uv_socket->write_req;
166   write_req->data = socket;
167   // TODO(murgatroid99): figure out what the return value here means
168   uv_write(write_req, (uv_stream_t*)uv_socket->handle, uv_buffers,
169            write_slices->count, uv_write_callback);
170 }
171 
shutdown_callback(uv_shutdown_t * req,int status)172 static void shutdown_callback(uv_shutdown_t* req, int status) {}
173 
uv_socket_shutdown(grpc_custom_socket * socket)174 static void uv_socket_shutdown(grpc_custom_socket* socket) {
175   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
176   uv_shutdown_t* req = &uv_socket->shutdown_req;
177   uv_shutdown(req, (uv_stream_t*)uv_socket->handle, shutdown_callback);
178 }
179 
uv_socket_close(grpc_custom_socket * socket,grpc_custom_close_callback close_cb)180 static void uv_socket_close(grpc_custom_socket* socket,
181                             grpc_custom_close_callback close_cb) {
182   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
183   uv_socket->close_cb = close_cb;
184   uv_close((uv_handle_t*)uv_socket->handle, uv_close_callback);
185 }
186 
uv_socket_init_helper(uv_socket_t * uv_socket,int domain)187 static grpc_error* uv_socket_init_helper(uv_socket_t* uv_socket, int domain) {
188   uv_tcp_t* tcp = (uv_tcp_t*)gpr_malloc(sizeof(uv_tcp_t));
189   uv_socket->handle = tcp;
190   int status = uv_tcp_init_ex(uv_default_loop(), tcp, (unsigned int)domain);
191   if (status != 0) {
192     return tcp_error_create("Failed to initialize UV tcp handle", status);
193   }
194 #if defined(GPR_LINUX) && defined(SO_REUSEPORT)
195   if (domain == AF_INET || domain == AF_INET6) {
196     int enable = 1;
197     int fd;
198     uv_fileno((uv_handle_t*)tcp, &fd);
199     // TODO Handle error here.
200     setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(enable));
201   }
202 #endif
203   uv_socket->write_buffers = nullptr;
204   uv_socket->read_len = 0;
205   uv_tcp_nodelay(uv_socket->handle, 1);
206   // Node uses a garbage collector to call destructors, so we don't
207   // want to hold the uv loop open with active gRPC objects.
208   uv_unref((uv_handle_t*)uv_socket->handle);
209   uv_socket->pending_connections = 0;
210   uv_socket->accept_socket = nullptr;
211   uv_socket->accept_error = GRPC_ERROR_NONE;
212   return GRPC_ERROR_NONE;
213 }
214 
uv_socket_init(grpc_custom_socket * socket,int domain)215 static grpc_error* uv_socket_init(grpc_custom_socket* socket, int domain) {
216   uv_socket_t* uv_socket = (uv_socket_t*)gpr_malloc(sizeof(uv_socket_t));
217   grpc_error* error = uv_socket_init_helper(uv_socket, domain);
218   if (error != GRPC_ERROR_NONE) {
219     return error;
220   }
221   uv_socket->handle->data = socket;
222   socket->impl = uv_socket;
223   return GRPC_ERROR_NONE;
224 }
225 
uv_socket_getpeername(grpc_custom_socket * socket,const grpc_sockaddr * addr,int * addr_len)226 static grpc_error* uv_socket_getpeername(grpc_custom_socket* socket,
227                                          const grpc_sockaddr* addr,
228                                          int* addr_len) {
229   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
230   int err = uv_tcp_getpeername(uv_socket->handle,
231                                (struct sockaddr*)IGNORE_CONST(addr), addr_len);
232   return tcp_error_create("getpeername failed", err);
233 }
234 
uv_socket_getsockname(grpc_custom_socket * socket,const grpc_sockaddr * addr,int * addr_len)235 static grpc_error* uv_socket_getsockname(grpc_custom_socket* socket,
236                                          const grpc_sockaddr* addr,
237                                          int* addr_len) {
238   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
239   int err = uv_tcp_getsockname(uv_socket->handle,
240                                (struct sockaddr*)IGNORE_CONST(addr), addr_len);
241   return tcp_error_create("getsockname failed", err);
242 }
243 
accept_new_connection(grpc_custom_socket * socket)244 static void accept_new_connection(grpc_custom_socket* socket) {
245   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
246   if (uv_socket->pending_connections == 0 || !uv_socket->accept_socket) {
247     return;
248   }
249   grpc_custom_socket* new_socket = uv_socket->accept_socket;
250   grpc_error* error = uv_socket->accept_error;
251   uv_socket->accept_socket = nullptr;
252   uv_socket->accept_error = GRPC_ERROR_NONE;
253   uv_socket->pending_connections -= 1;
254   if (uv_socket->accept_error != GRPC_ERROR_NONE) {
255     uv_stream_t dummy_handle;
256     uv_accept((uv_stream_t*)uv_socket->handle, &dummy_handle);
257     uv_socket->accept_cb(socket, new_socket, error);
258   } else {
259     uv_socket_t* uv_new_socket = (uv_socket_t*)gpr_malloc(sizeof(uv_socket_t));
260     uv_socket_init_helper(uv_new_socket, AF_UNSPEC);
261     // UV documentation says this is guaranteed to succeed
262     GPR_ASSERT(uv_accept((uv_stream_t*)uv_socket->handle,
263                          (uv_stream_t*)uv_new_socket->handle) == 0);
264     new_socket->impl = uv_new_socket;
265     uv_new_socket->handle->data = new_socket;
266     uv_socket->accept_cb(socket, new_socket, error);
267   }
268 }
269 
uv_on_connect(uv_stream_t * server,int status)270 static void uv_on_connect(uv_stream_t* server, int status) {
271   grpc_custom_socket* socket = (grpc_custom_socket*)server->data;
272   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
273   if (status < 0) {
274     switch (status) {
275       case UV_EINTR:
276       case UV_EAGAIN:
277         return;
278       default:
279         uv_socket->accept_error = tcp_error_create("accept failed", status);
280     }
281   }
282   uv_socket->pending_connections += 1;
283   accept_new_connection(socket);
284 }
285 
uv_socket_accept(grpc_custom_socket * socket,grpc_custom_socket * new_socket,grpc_custom_accept_callback accept_cb)286 void uv_socket_accept(grpc_custom_socket* socket,
287                       grpc_custom_socket* new_socket,
288                       grpc_custom_accept_callback accept_cb) {
289   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
290   uv_socket->accept_cb = accept_cb;
291   GPR_ASSERT(uv_socket->accept_socket == nullptr);
292   uv_socket->accept_socket = new_socket;
293   accept_new_connection(socket);
294 }
295 
uv_socket_bind(grpc_custom_socket * socket,const grpc_sockaddr * addr,size_t len,int flags)296 static grpc_error* uv_socket_bind(grpc_custom_socket* socket,
297                                   const grpc_sockaddr* addr, size_t len,
298                                   int flags) {
299   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
300   int status =
301       uv_tcp_bind((uv_tcp_t*)uv_socket->handle, (struct sockaddr*)addr, 0);
302   return tcp_error_create("Failed to bind to port", status);
303 }
304 
uv_socket_listen(grpc_custom_socket * socket)305 static grpc_error* uv_socket_listen(grpc_custom_socket* socket) {
306   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
307   int status =
308       uv_listen((uv_stream_t*)uv_socket->handle, SOMAXCONN, uv_on_connect);
309   return tcp_error_create("Failed to listen to port", status);
310 }
311 
uv_tc_on_connect(uv_connect_t * req,int status)312 static void uv_tc_on_connect(uv_connect_t* req, int status) {
313   grpc_custom_socket* socket = (grpc_custom_socket*)req->data;
314   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
315   grpc_error* error;
316   if (status == UV_ECANCELED) {
317     // This should only happen if the handle is already closed
318     error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Timeout occurred");
319   } else {
320     error = tcp_error_create("Failed to connect to remote host", status);
321   }
322   uv_socket->connect_cb(socket, error);
323 }
324 
uv_socket_connect(grpc_custom_socket * socket,const grpc_sockaddr * addr,size_t len,grpc_custom_connect_callback connect_cb)325 static void uv_socket_connect(grpc_custom_socket* socket,
326                               const grpc_sockaddr* addr, size_t len,
327                               grpc_custom_connect_callback connect_cb) {
328   uv_socket_t* uv_socket = (uv_socket_t*)socket->impl;
329   uv_socket->connect_cb = connect_cb;
330   uv_socket->connect_req.data = socket;
331   int status = uv_tcp_connect(&uv_socket->connect_req, uv_socket->handle,
332                               (struct sockaddr*)addr, uv_tc_on_connect);
333   if (status != 0) {
334     // The callback will not be called
335     uv_socket->connect_cb(socket, tcp_error_create("connect failed", status));
336   }
337 }
338 
handle_addrinfo_result(struct addrinfo * result)339 static grpc_resolved_addresses* handle_addrinfo_result(
340     struct addrinfo* result) {
341   struct addrinfo* resp;
342   size_t i;
343   grpc_resolved_addresses* addresses =
344       (grpc_resolved_addresses*)gpr_malloc(sizeof(grpc_resolved_addresses));
345   addresses->naddrs = 0;
346   for (resp = result; resp != nullptr; resp = resp->ai_next) {
347     addresses->naddrs++;
348   }
349   addresses->addrs = (grpc_resolved_address*)gpr_malloc(
350       sizeof(grpc_resolved_address) * addresses->naddrs);
351   for (resp = result, i = 0; resp != nullptr; resp = resp->ai_next, i++) {
352     memcpy(&addresses->addrs[i].addr, resp->ai_addr, resp->ai_addrlen);
353     addresses->addrs[i].len = resp->ai_addrlen;
354   }
355   // addrinfo objects are allocated by libuv (e.g. in uv_getaddrinfo)
356   // and not by gpr_malloc
357   uv_freeaddrinfo(result);
358   return addresses;
359 }
360 
uv_resolve_callback(uv_getaddrinfo_t * req,int status,struct addrinfo * res)361 static void uv_resolve_callback(uv_getaddrinfo_t* req, int status,
362                                 struct addrinfo* res) {
363   grpc_custom_resolver* r = (grpc_custom_resolver*)req->data;
364   gpr_free(req);
365   grpc_resolved_addresses* result = nullptr;
366   if (status == 0) {
367     result = handle_addrinfo_result(res);
368   }
369   grpc_custom_resolve_callback(r, result,
370                                tcp_error_create("getaddrinfo failed", status));
371 }
372 
uv_resolve(const char * host,const char * port,grpc_resolved_addresses ** result)373 static grpc_error* uv_resolve(const char* host, const char* port,
374                               grpc_resolved_addresses** result) {
375   int status;
376   uv_getaddrinfo_t req;
377   struct addrinfo hints;
378   memset(&hints, 0, sizeof(struct addrinfo));
379   hints.ai_family = AF_UNSPEC;     /* ipv4 or ipv6 */
380   hints.ai_socktype = SOCK_STREAM; /* stream socket */
381   hints.ai_flags = AI_PASSIVE;     /* for wildcard IP address */
382   status = uv_getaddrinfo(uv_default_loop(), &req, NULL, host, port, &hints);
383   if (status != 0) {
384     *result = nullptr;
385   } else {
386     *result = handle_addrinfo_result(req.addrinfo);
387   }
388   return tcp_error_create("getaddrinfo failed", status);
389 }
390 
uv_resolve_async(grpc_custom_resolver * r,const char * host,const char * port)391 static void uv_resolve_async(grpc_custom_resolver* r, const char* host,
392                              const char* port) {
393   int status;
394   uv_getaddrinfo_t* req =
395       (uv_getaddrinfo_t*)gpr_malloc(sizeof(uv_getaddrinfo_t));
396   req->data = r;
397   struct addrinfo hints;
398   memset(&hints, 0, sizeof(struct addrinfo));
399   hints.ai_family = GRPC_AF_UNSPEC;     /* ipv4 or ipv6 */
400   hints.ai_socktype = GRPC_SOCK_STREAM; /* stream socket */
401   hints.ai_flags = GRPC_AI_PASSIVE;     /* for wildcard IP address */
402   status = uv_getaddrinfo(uv_default_loop(), req, uv_resolve_callback, host,
403                           port, &hints);
404   if (status != 0) {
405     gpr_free(req);
406     grpc_error* error = tcp_error_create("getaddrinfo failed", status);
407     grpc_custom_resolve_callback(r, NULL, error);
408   }
409 }
410 
411 grpc_custom_resolver_vtable uv_resolver_vtable = {uv_resolve, uv_resolve_async};
412 
413 grpc_socket_vtable grpc_uv_socket_vtable = {
414     uv_socket_init,     uv_socket_connect,     uv_socket_destroy,
415     uv_socket_shutdown, uv_socket_close,       uv_socket_write,
416     uv_socket_read,     uv_socket_getpeername, uv_socket_getsockname,
417     uv_socket_bind,     uv_socket_listen,      uv_socket_accept};
418 
419 #endif
420