tcp_server_uv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "src/core/lib/iomgr/port.h"
  34. #ifdef GRPC_UV
  35. #include <string.h>
  36. #include <grpc/support/alloc.h>
  37. #include <grpc/support/log.h>
  38. #include "src/core/lib/iomgr/error.h"
  39. #include "src/core/lib/iomgr/exec_ctx.h"
  40. #include "src/core/lib/iomgr/sockaddr.h"
  41. #include "src/core/lib/iomgr/sockaddr_utils.h"
  42. #include "src/core/lib/iomgr/tcp_server.h"
  43. #include "src/core/lib/iomgr/tcp_uv.h"
  44. /* one listening port */
  45. typedef struct grpc_tcp_listener grpc_tcp_listener;
  46. struct grpc_tcp_listener {
  47. uv_tcp_t *handle;
  48. grpc_tcp_server *server;
  49. unsigned port_index;
  50. int port;
  51. /* linked list */
  52. struct grpc_tcp_listener *next;
  53. };
  54. struct grpc_tcp_server {
  55. gpr_refcount refs;
  56. /* Called whenever accept() succeeds on a server port. */
  57. grpc_tcp_server_cb on_accept_cb;
  58. void *on_accept_cb_arg;
  59. int open_ports;
  60. /* linked list of server ports */
  61. grpc_tcp_listener *head;
  62. grpc_tcp_listener *tail;
  63. /* List of closures passed to shutdown_starting_add(). */
  64. grpc_closure_list shutdown_starting;
  65. /* shutdown callback */
  66. grpc_closure *shutdown_complete;
  67. };
  68. grpc_error *grpc_tcp_server_create(grpc_closure *shutdown_complete,
  69. const grpc_channel_args *args,
  70. grpc_tcp_server **server) {
  71. grpc_tcp_server *s = gpr_malloc(sizeof(grpc_tcp_server));
  72. (void)args;
  73. gpr_ref_init(&s->refs, 1);
  74. s->on_accept_cb = NULL;
  75. s->on_accept_cb_arg = NULL;
  76. s->open_ports = 0;
  77. s->head = NULL;
  78. s->tail = NULL;
  79. s->shutdown_starting.head = NULL;
  80. s->shutdown_starting.tail = NULL;
  81. s->shutdown_complete = shutdown_complete;
  82. *server = s;
  83. return GRPC_ERROR_NONE;
  84. }
  85. grpc_tcp_server *grpc_tcp_server_ref(grpc_tcp_server *s) {
  86. gpr_ref(&s->refs);
  87. return s;
  88. }
  89. void grpc_tcp_server_shutdown_starting_add(grpc_tcp_server *s,
  90. grpc_closure *shutdown_starting) {
  91. grpc_closure_list_append(&s->shutdown_starting, shutdown_starting,
  92. GRPC_ERROR_NONE);
  93. }
  94. static void finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s) {
  95. if (s->shutdown_complete != NULL) {
  96. grpc_exec_ctx_sched(exec_ctx, s->shutdown_complete, GRPC_ERROR_NONE, NULL);
  97. }
  98. while (s->head) {
  99. grpc_tcp_listener *sp = s->head;
  100. s->head = sp->next;
  101. sp->next = NULL;
  102. gpr_free(sp->handle);
  103. gpr_free(sp);
  104. }
  105. gpr_free(s);
  106. }
  107. static void handle_close_callback(uv_handle_t *handle) {
  108. grpc_tcp_listener *sp = (grpc_tcp_listener *)handle->data;
  109. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  110. sp->server->open_ports--;
  111. if (sp->server->open_ports == 0) {
  112. finish_shutdown(&exec_ctx, sp->server);
  113. }
  114. grpc_exec_ctx_finish(&exec_ctx);
  115. }
  116. static void tcp_server_destroy(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s) {
  117. int immediately_done = 0;
  118. grpc_tcp_listener *sp;
  119. if (s->open_ports == 0) {
  120. immediately_done = 1;
  121. }
  122. for (sp = s->head; sp; sp = sp->next) {
  123. uv_close((uv_handle_t *)sp->handle, handle_close_callback);
  124. }
  125. if (immediately_done) {
  126. finish_shutdown(exec_ctx, s);
  127. }
  128. }
  129. void grpc_tcp_server_unref(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s) {
  130. if (gpr_unref(&s->refs)) {
  131. /* Complete shutdown_starting work before destroying. */
  132. grpc_exec_ctx local_exec_ctx = GRPC_EXEC_CTX_INIT;
  133. grpc_exec_ctx_enqueue_list(&local_exec_ctx, &s->shutdown_starting, NULL);
  134. if (exec_ctx == NULL) {
  135. grpc_exec_ctx_flush(&local_exec_ctx);
  136. tcp_server_destroy(&local_exec_ctx, s);
  137. grpc_exec_ctx_finish(&local_exec_ctx);
  138. } else {
  139. grpc_exec_ctx_finish(&local_exec_ctx);
  140. tcp_server_destroy(exec_ctx, s);
  141. }
  142. }
  143. }
  144. static void accepted_connection_close_cb(uv_handle_t *handle) {
  145. gpr_free(handle);
  146. }
  147. static void on_connect(uv_stream_t *server, int status) {
  148. grpc_tcp_listener *sp = (grpc_tcp_listener *)server->data;
  149. grpc_tcp_server_acceptor acceptor = {sp->server, sp->port_index, 0};
  150. uv_tcp_t *client;
  151. grpc_endpoint *ep = NULL;
  152. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  153. grpc_resolved_address peer_name;
  154. char *peer_name_string;
  155. int err;
  156. if (status < 0) {
  157. gpr_log(GPR_INFO, "Skipping on_accept due to error: %s",
  158. uv_strerror(status));
  159. return;
  160. }
  161. client = gpr_malloc(sizeof(uv_tcp_t));
  162. uv_tcp_init(uv_default_loop(), client);
  163. // UV documentation says this is guaranteed to succeed
  164. uv_accept((uv_stream_t *)server, (uv_stream_t *)client);
  165. // If the server has not been started, we discard incoming connections
  166. if (sp->server->on_accept_cb == NULL) {
  167. uv_close((uv_handle_t *)client, accepted_connection_close_cb);
  168. } else {
  169. peer_name_string = NULL;
  170. memset(&peer_name, 0, sizeof(grpc_resolved_address));
  171. peer_name.len = sizeof(struct sockaddr_storage);
  172. err = uv_tcp_getpeername(client, (struct sockaddr *)&peer_name.addr,
  173. (int *)&peer_name.len);
  174. if (err == 0) {
  175. peer_name_string = grpc_sockaddr_to_uri(&peer_name);
  176. } else {
  177. gpr_log(GPR_INFO, "uv_tcp_getpeername error: %s", uv_strerror(status));
  178. }
  179. ep = grpc_tcp_create(client, peer_name_string);
  180. sp->server->on_accept_cb(&exec_ctx, sp->server->on_accept_cb_arg, ep, NULL,
  181. &acceptor);
  182. grpc_exec_ctx_finish(&exec_ctx);
  183. }
  184. }
  185. static grpc_error *add_socket_to_server(grpc_tcp_server *s, uv_tcp_t *handle,
  186. const grpc_resolved_address *addr,
  187. unsigned port_index,
  188. grpc_tcp_listener **listener) {
  189. grpc_tcp_listener *sp = NULL;
  190. int port = -1;
  191. int status;
  192. grpc_error *error;
  193. grpc_resolved_address sockname_temp;
  194. // The last argument to uv_tcp_bind is flags
  195. status = uv_tcp_bind(handle, (struct sockaddr *)addr->addr, 0);
  196. if (status != 0) {
  197. error = GRPC_ERROR_CREATE("Failed to bind to port");
  198. error =
  199. grpc_error_set_str(error, GRPC_ERROR_STR_OS_ERROR, uv_strerror(status));
  200. return error;
  201. }
  202. status = uv_listen((uv_stream_t *)handle, SOMAXCONN, on_connect);
  203. if (status != 0) {
  204. error = GRPC_ERROR_CREATE("Failed to listen to port");
  205. error =
  206. grpc_error_set_str(error, GRPC_ERROR_STR_OS_ERROR, uv_strerror(status));
  207. return error;
  208. }
  209. sockname_temp.len = (int)sizeof(struct sockaddr_storage);
  210. status = uv_tcp_getsockname(handle, (struct sockaddr *)&sockname_temp.addr,
  211. (int *)&sockname_temp.len);
  212. if (status != 0) {
  213. error = GRPC_ERROR_CREATE("getsockname failed");
  214. error =
  215. grpc_error_set_str(error, GRPC_ERROR_STR_OS_ERROR, uv_strerror(status));
  216. return error;
  217. }
  218. port = grpc_sockaddr_get_port(&sockname_temp);
  219. GPR_ASSERT(port >= 0);
  220. GPR_ASSERT(!s->on_accept_cb && "must add ports before starting server");
  221. sp = gpr_malloc(sizeof(grpc_tcp_listener));
  222. sp->next = NULL;
  223. if (s->head == NULL) {
  224. s->head = sp;
  225. } else {
  226. s->tail->next = sp;
  227. }
  228. s->tail = sp;
  229. sp->server = s;
  230. sp->handle = handle;
  231. sp->port = port;
  232. sp->port_index = port_index;
  233. handle->data = sp;
  234. s->open_ports++;
  235. GPR_ASSERT(sp->handle);
  236. *listener = sp;
  237. return GRPC_ERROR_NONE;
  238. }
  239. grpc_error *grpc_tcp_server_add_port(grpc_tcp_server *s,
  240. const grpc_resolved_address *addr,
  241. int *port) {
  242. // This function is mostly copied from tcp_server_windows.c
  243. grpc_tcp_listener *sp = NULL;
  244. uv_tcp_t *handle;
  245. grpc_resolved_address addr6_v4mapped;
  246. grpc_resolved_address wildcard;
  247. grpc_resolved_address *allocated_addr = NULL;
  248. grpc_resolved_address sockname_temp;
  249. unsigned port_index = 0;
  250. int status;
  251. grpc_error *error = GRPC_ERROR_NONE;
  252. if (s->tail != NULL) {
  253. port_index = s->tail->port_index + 1;
  254. }
  255. /* Check if this is a wildcard port, and if so, try to keep the port the same
  256. as some previously created listener. */
  257. if (grpc_sockaddr_get_port(addr) == 0) {
  258. for (sp = s->head; sp; sp = sp->next) {
  259. sockname_temp.len = sizeof(struct sockaddr_storage);
  260. if (0 == uv_tcp_getsockname(sp->handle,
  261. (struct sockaddr *)&sockname_temp.addr,
  262. (int *)&sockname_temp.len)) {
  263. *port = grpc_sockaddr_get_port(&sockname_temp);
  264. if (*port > 0) {
  265. allocated_addr = gpr_malloc(sizeof(grpc_resolved_address));
  266. memcpy(allocated_addr, addr, sizeof(grpc_resolved_address));
  267. grpc_sockaddr_set_port(allocated_addr, *port);
  268. addr = allocated_addr;
  269. break;
  270. }
  271. }
  272. }
  273. }
  274. if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) {
  275. addr = &addr6_v4mapped;
  276. }
  277. /* Treat :: or 0.0.0.0 as a family-agnostic wildcard. */
  278. if (grpc_sockaddr_is_wildcard(addr, port)) {
  279. grpc_sockaddr_make_wildcard6(*port, &wildcard);
  280. addr = &wildcard;
  281. }
  282. handle = gpr_malloc(sizeof(uv_tcp_t));
  283. status = uv_tcp_init(uv_default_loop(), handle);
  284. if (status == 0) {
  285. error = add_socket_to_server(s, handle, addr, port_index, &sp);
  286. } else {
  287. error = GRPC_ERROR_CREATE("Failed to initialize UV tcp handle");
  288. error =
  289. grpc_error_set_str(error, GRPC_ERROR_STR_OS_ERROR, uv_strerror(status));
  290. }
  291. gpr_free(allocated_addr);
  292. if (error != GRPC_ERROR_NONE) {
  293. grpc_error *error_out = GRPC_ERROR_CREATE_REFERENCING(
  294. "Failed to add port to server", &error, 1);
  295. GRPC_ERROR_UNREF(error);
  296. error = error_out;
  297. *port = -1;
  298. } else {
  299. GPR_ASSERT(sp != NULL);
  300. *port = sp->port;
  301. }
  302. return error;
  303. }
  304. void grpc_tcp_server_start(grpc_exec_ctx *exec_ctx, grpc_tcp_server *server,
  305. grpc_pollset **pollsets, size_t pollset_count,
  306. grpc_tcp_server_cb on_accept_cb, void *cb_arg) {
  307. grpc_tcp_listener *sp;
  308. (void)pollsets;
  309. (void)pollset_count;
  310. GPR_ASSERT(on_accept_cb);
  311. GPR_ASSERT(!server->on_accept_cb);
  312. server->on_accept_cb = on_accept_cb;
  313. server->on_accept_cb_arg = cb_arg;
  314. for (sp = server->head; sp; sp = sp->next) {
  315. GPR_ASSERT(uv_listen((uv_stream_t *)sp->handle, SOMAXCONN, on_connect) ==
  316. 0);
  317. }
  318. }
  319. void grpc_tcp_server_shutdown_listeners(grpc_exec_ctx *exec_ctx,
  320. grpc_tcp_server *s) {}
  321. #endif /* GRPC_UV */