tcp_posix.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 <grpc/support/port_platform.h>
  34. #ifdef GPR_POSIX_SOCKET
  35. #include "src/core/lib/iomgr/network_status_tracker.h"
  36. #include "src/core/lib/iomgr/tcp_posix.h"
  37. #include <errno.h>
  38. #include <stdbool.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <sys/socket.h>
  42. #include <sys/types.h>
  43. #include <unistd.h>
  44. #include <grpc/support/alloc.h>
  45. #include <grpc/support/log.h>
  46. #include <grpc/support/slice.h>
  47. #include <grpc/support/string_util.h>
  48. #include <grpc/support/sync.h>
  49. #include <grpc/support/time.h>
  50. #include "src/core/lib/debug/trace.h"
  51. #include "src/core/lib/iomgr/ev_posix.h"
  52. #include "src/core/lib/profiling/timers.h"
  53. #include "src/core/lib/support/string.h"
  54. #ifdef GPR_HAVE_MSG_NOSIGNAL
  55. #define SENDMSG_FLAGS MSG_NOSIGNAL
  56. #else
  57. #define SENDMSG_FLAGS 0
  58. #endif
  59. #ifdef GPR_MSG_IOVLEN_TYPE
  60. typedef GPR_MSG_IOVLEN_TYPE msg_iovlen_type;
  61. #else
  62. typedef size_t msg_iovlen_type;
  63. #endif
  64. int grpc_tcp_trace = 0;
  65. typedef struct {
  66. grpc_endpoint base;
  67. grpc_fd *em_fd;
  68. int fd;
  69. bool finished_edge;
  70. msg_iovlen_type iov_size; /* Number of slices to allocate per read attempt */
  71. size_t slice_size;
  72. gpr_refcount refcount;
  73. gpr_atm shutdown_count;
  74. /* garbage after the last read */
  75. gpr_slice_buffer last_read_buffer;
  76. gpr_slice_buffer *incoming_buffer;
  77. gpr_slice_buffer *outgoing_buffer;
  78. /** slice within outgoing_buffer to write next */
  79. size_t outgoing_slice_idx;
  80. /** byte within outgoing_buffer->slices[outgoing_slice_idx] to write next */
  81. size_t outgoing_byte_idx;
  82. grpc_closure *read_cb;
  83. grpc_closure *write_cb;
  84. grpc_closure *release_fd_cb;
  85. int *release_fd;
  86. grpc_closure read_closure;
  87. grpc_closure write_closure;
  88. char *peer_string;
  89. grpc_buffer_user buffer_user;
  90. grpc_buffer_user_slice_allocator slice_allocator;
  91. } grpc_tcp;
  92. static void tcp_handle_read(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */,
  93. grpc_error *error);
  94. static void tcp_handle_write(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */,
  95. grpc_error *error);
  96. static void tcp_unref_closure(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */,
  97. grpc_error *error);
  98. static void tcp_maybe_shutdown_buffer_user(grpc_exec_ctx *exec_ctx,
  99. grpc_tcp *tcp) {
  100. if (gpr_atm_full_fetch_add(&tcp->shutdown_count, 1) == 0) {
  101. grpc_buffer_user_shutdown(exec_ctx, &tcp->buffer_user,
  102. grpc_closure_create(tcp_unref_closure, tcp));
  103. }
  104. }
  105. static void tcp_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
  106. grpc_tcp *tcp = (grpc_tcp *)ep;
  107. tcp_maybe_shutdown_buffer_user(exec_ctx, tcp);
  108. grpc_fd_shutdown(exec_ctx, tcp->em_fd);
  109. }
  110. static void tcp_free(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
  111. grpc_fd_orphan(exec_ctx, tcp->em_fd, tcp->release_fd_cb, tcp->release_fd,
  112. "tcp_unref_orphan");
  113. gpr_slice_buffer_destroy(&tcp->last_read_buffer);
  114. gpr_free(tcp->peer_string);
  115. gpr_free(tcp);
  116. }
  117. /*#define GRPC_TCP_REFCOUNT_DEBUG*/
  118. #ifdef GRPC_TCP_REFCOUNT_DEBUG
  119. #define TCP_UNREF(cl, tcp, reason) \
  120. tcp_unref((cl), (tcp), (reason), __FILE__, __LINE__)
  121. #define TCP_REF(tcp, reason) tcp_ref((tcp), (reason), __FILE__, __LINE__)
  122. static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp,
  123. const char *reason, const char *file, int line) {
  124. gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP unref %p : %s %d -> %d", tcp,
  125. reason, tcp->refcount.count, tcp->refcount.count - 1);
  126. if (gpr_unref(&tcp->refcount)) {
  127. tcp_free(exec_ctx, tcp);
  128. }
  129. }
  130. static void tcp_ref(grpc_tcp *tcp, const char *reason, const char *file,
  131. int line) {
  132. gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP ref %p : %s %d -> %d", tcp,
  133. reason, tcp->refcount.count, tcp->refcount.count + 1);
  134. gpr_ref(&tcp->refcount);
  135. }
  136. #else
  137. #define TCP_UNREF(cl, tcp, reason) tcp_unref((cl), (tcp))
  138. #define TCP_REF(tcp, reason) tcp_ref((tcp))
  139. static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
  140. if (gpr_unref(&tcp->refcount)) {
  141. tcp_free(exec_ctx, tcp);
  142. }
  143. }
  144. static void tcp_ref(grpc_tcp *tcp) { gpr_ref(&tcp->refcount); }
  145. #endif
  146. static void tcp_unref_closure(grpc_exec_ctx *exec_ctx, void *arg,
  147. grpc_error *error) {
  148. TCP_UNREF(exec_ctx, arg, "buffer_user");
  149. }
  150. static void tcp_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
  151. grpc_network_status_unregister_endpoint(ep);
  152. grpc_tcp *tcp = (grpc_tcp *)ep;
  153. tcp_maybe_shutdown_buffer_user(exec_ctx, tcp);
  154. gpr_slice_buffer_reset_and_unref(&tcp->last_read_buffer);
  155. TCP_UNREF(exec_ctx, tcp, "destroy");
  156. }
  157. static void call_read_cb(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp,
  158. grpc_error *error) {
  159. grpc_closure *cb = tcp->read_cb;
  160. if (grpc_tcp_trace) {
  161. size_t i;
  162. const char *str = grpc_error_string(error);
  163. gpr_log(GPR_DEBUG, "read: error=%s", str);
  164. grpc_error_free_string(str);
  165. for (i = 0; i < tcp->incoming_buffer->count; i++) {
  166. char *dump = gpr_dump_slice(tcp->incoming_buffer->slices[i],
  167. GPR_DUMP_HEX | GPR_DUMP_ASCII);
  168. gpr_log(GPR_DEBUG, "READ %p (peer=%s): %s", tcp, tcp->peer_string, dump);
  169. gpr_free(dump);
  170. }
  171. }
  172. tcp->read_cb = NULL;
  173. tcp->incoming_buffer = NULL;
  174. grpc_closure_run(exec_ctx, cb, error);
  175. }
  176. #define MAX_READ_IOVEC 4
  177. static void tcp_do_read(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
  178. struct msghdr msg;
  179. struct iovec iov[MAX_READ_IOVEC];
  180. ssize_t read_bytes;
  181. size_t i;
  182. GPR_ASSERT(!tcp->finished_edge);
  183. GPR_ASSERT(tcp->iov_size <= MAX_READ_IOVEC);
  184. GPR_ASSERT(tcp->incoming_buffer->count <= MAX_READ_IOVEC);
  185. GPR_TIMER_BEGIN("tcp_continue_read", 0);
  186. for (i = 0; i < tcp->incoming_buffer->count; i++) {
  187. iov[i].iov_base = GPR_SLICE_START_PTR(tcp->incoming_buffer->slices[i]);
  188. iov[i].iov_len = GPR_SLICE_LENGTH(tcp->incoming_buffer->slices[i]);
  189. }
  190. msg.msg_name = NULL;
  191. msg.msg_namelen = 0;
  192. msg.msg_iov = iov;
  193. msg.msg_iovlen = tcp->iov_size;
  194. msg.msg_control = NULL;
  195. msg.msg_controllen = 0;
  196. msg.msg_flags = 0;
  197. GPR_TIMER_BEGIN("recvmsg", 0);
  198. do {
  199. read_bytes = recvmsg(tcp->fd, &msg, 0);
  200. } while (read_bytes < 0 && errno == EINTR);
  201. GPR_TIMER_END("recvmsg", read_bytes >= 0);
  202. if (read_bytes < 0) {
  203. /* NB: After calling call_read_cb a parallel call of the read handler may
  204. * be running. */
  205. if (errno == EAGAIN) {
  206. if (tcp->iov_size > 1) {
  207. tcp->iov_size /= 2;
  208. }
  209. /* We've consumed the edge, request a new one */
  210. grpc_fd_notify_on_read(exec_ctx, tcp->em_fd, &tcp->read_closure);
  211. } else {
  212. gpr_slice_buffer_reset_and_unref(tcp->incoming_buffer);
  213. call_read_cb(exec_ctx, tcp, GRPC_OS_ERROR(errno, "recvmsg"));
  214. TCP_UNREF(exec_ctx, tcp, "read");
  215. }
  216. } else if (read_bytes == 0) {
  217. /* 0 read size ==> end of stream */
  218. gpr_slice_buffer_reset_and_unref(tcp->incoming_buffer);
  219. call_read_cb(exec_ctx, tcp, GRPC_ERROR_CREATE("EOF"));
  220. TCP_UNREF(exec_ctx, tcp, "read");
  221. } else {
  222. GPR_ASSERT((size_t)read_bytes <= tcp->incoming_buffer->length);
  223. if ((size_t)read_bytes < tcp->incoming_buffer->length) {
  224. gpr_slice_buffer_trim_end(
  225. tcp->incoming_buffer,
  226. tcp->incoming_buffer->length - (size_t)read_bytes,
  227. &tcp->last_read_buffer);
  228. } else if (tcp->iov_size < MAX_READ_IOVEC) {
  229. ++tcp->iov_size;
  230. }
  231. GPR_ASSERT((size_t)read_bytes == tcp->incoming_buffer->length);
  232. call_read_cb(exec_ctx, tcp, GRPC_ERROR_NONE);
  233. TCP_UNREF(exec_ctx, tcp, "read");
  234. }
  235. GPR_TIMER_END("tcp_continue_read", 0);
  236. }
  237. static void tcp_read_allocation_done(grpc_exec_ctx *exec_ctx, void *tcp,
  238. grpc_error *error) {
  239. tcp_do_read(exec_ctx, tcp);
  240. }
  241. static void tcp_continue_read(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
  242. if (tcp->incoming_buffer->count < (size_t)tcp->iov_size) {
  243. grpc_buffer_user_alloc_slices(
  244. exec_ctx, &tcp->slice_allocator, tcp->slice_size,
  245. (size_t)tcp->iov_size - tcp->incoming_buffer->count,
  246. tcp->incoming_buffer);
  247. } else {
  248. tcp_do_read(exec_ctx, tcp);
  249. }
  250. }
  251. static void tcp_handle_read(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */,
  252. grpc_error *error) {
  253. grpc_tcp *tcp = (grpc_tcp *)arg;
  254. GPR_ASSERT(!tcp->finished_edge);
  255. if (error != GRPC_ERROR_NONE) {
  256. gpr_slice_buffer_reset_and_unref(tcp->incoming_buffer);
  257. gpr_slice_buffer_reset_and_unref(&tcp->last_read_buffer);
  258. call_read_cb(exec_ctx, tcp, GRPC_ERROR_REF(error));
  259. TCP_UNREF(exec_ctx, tcp, "read");
  260. } else {
  261. tcp_continue_read(exec_ctx, tcp);
  262. }
  263. }
  264. static void tcp_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
  265. gpr_slice_buffer *incoming_buffer, grpc_closure *cb) {
  266. grpc_tcp *tcp = (grpc_tcp *)ep;
  267. GPR_ASSERT(tcp->read_cb == NULL);
  268. tcp->read_cb = cb;
  269. tcp->incoming_buffer = incoming_buffer;
  270. gpr_slice_buffer_reset_and_unref(incoming_buffer);
  271. gpr_slice_buffer_swap(incoming_buffer, &tcp->last_read_buffer);
  272. TCP_REF(tcp, "read");
  273. if (tcp->finished_edge) {
  274. tcp->finished_edge = false;
  275. grpc_fd_notify_on_read(exec_ctx, tcp->em_fd, &tcp->read_closure);
  276. } else {
  277. grpc_exec_ctx_sched(exec_ctx, &tcp->read_closure, GRPC_ERROR_NONE, NULL);
  278. }
  279. }
  280. /* returns true if done, false if pending; if returning true, *error is set */
  281. #define MAX_WRITE_IOVEC 1000
  282. static bool tcp_flush(grpc_tcp *tcp, grpc_error **error) {
  283. struct msghdr msg;
  284. struct iovec iov[MAX_WRITE_IOVEC];
  285. msg_iovlen_type iov_size;
  286. ssize_t sent_length;
  287. size_t sending_length;
  288. size_t trailing;
  289. size_t unwind_slice_idx;
  290. size_t unwind_byte_idx;
  291. for (;;) {
  292. sending_length = 0;
  293. unwind_slice_idx = tcp->outgoing_slice_idx;
  294. unwind_byte_idx = tcp->outgoing_byte_idx;
  295. for (iov_size = 0; tcp->outgoing_slice_idx != tcp->outgoing_buffer->count &&
  296. iov_size != MAX_WRITE_IOVEC;
  297. iov_size++) {
  298. iov[iov_size].iov_base =
  299. GPR_SLICE_START_PTR(
  300. tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]) +
  301. tcp->outgoing_byte_idx;
  302. iov[iov_size].iov_len =
  303. GPR_SLICE_LENGTH(
  304. tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]) -
  305. tcp->outgoing_byte_idx;
  306. sending_length += iov[iov_size].iov_len;
  307. tcp->outgoing_slice_idx++;
  308. tcp->outgoing_byte_idx = 0;
  309. }
  310. GPR_ASSERT(iov_size > 0);
  311. msg.msg_name = NULL;
  312. msg.msg_namelen = 0;
  313. msg.msg_iov = iov;
  314. msg.msg_iovlen = iov_size;
  315. msg.msg_control = NULL;
  316. msg.msg_controllen = 0;
  317. msg.msg_flags = 0;
  318. GPR_TIMER_BEGIN("sendmsg", 1);
  319. do {
  320. /* TODO(klempner): Cork if this is a partial write */
  321. sent_length = sendmsg(tcp->fd, &msg, SENDMSG_FLAGS);
  322. } while (sent_length < 0 && errno == EINTR);
  323. GPR_TIMER_END("sendmsg", 0);
  324. if (sent_length < 0) {
  325. if (errno == EAGAIN) {
  326. tcp->outgoing_slice_idx = unwind_slice_idx;
  327. tcp->outgoing_byte_idx = unwind_byte_idx;
  328. return false;
  329. } else {
  330. *error = GRPC_OS_ERROR(errno, "sendmsg");
  331. return true;
  332. }
  333. }
  334. GPR_ASSERT(tcp->outgoing_byte_idx == 0);
  335. trailing = sending_length - (size_t)sent_length;
  336. while (trailing > 0) {
  337. size_t slice_length;
  338. tcp->outgoing_slice_idx--;
  339. slice_length = GPR_SLICE_LENGTH(
  340. tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]);
  341. if (slice_length > trailing) {
  342. tcp->outgoing_byte_idx = slice_length - trailing;
  343. break;
  344. } else {
  345. trailing -= slice_length;
  346. }
  347. }
  348. if (tcp->outgoing_slice_idx == tcp->outgoing_buffer->count) {
  349. *error = GRPC_ERROR_NONE;
  350. return true;
  351. }
  352. };
  353. }
  354. static void tcp_handle_write(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */,
  355. grpc_error *error) {
  356. grpc_tcp *tcp = (grpc_tcp *)arg;
  357. grpc_closure *cb;
  358. if (error != GRPC_ERROR_NONE) {
  359. cb = tcp->write_cb;
  360. tcp->write_cb = NULL;
  361. cb->cb(exec_ctx, cb->cb_arg, error);
  362. TCP_UNREF(exec_ctx, tcp, "write");
  363. return;
  364. }
  365. if (!tcp_flush(tcp, &error)) {
  366. if (grpc_tcp_trace) {
  367. gpr_log(GPR_DEBUG, "write: delayed");
  368. }
  369. grpc_fd_notify_on_write(exec_ctx, tcp->em_fd, &tcp->write_closure);
  370. } else {
  371. cb = tcp->write_cb;
  372. tcp->write_cb = NULL;
  373. if (grpc_tcp_trace) {
  374. const char *str = grpc_error_string(error);
  375. gpr_log(GPR_DEBUG, "write: %s", str);
  376. grpc_error_free_string(str);
  377. }
  378. grpc_closure_run(exec_ctx, cb, error);
  379. TCP_UNREF(exec_ctx, tcp, "write");
  380. }
  381. }
  382. static void tcp_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
  383. gpr_slice_buffer *buf, grpc_closure *cb) {
  384. grpc_tcp *tcp = (grpc_tcp *)ep;
  385. grpc_error *error = GRPC_ERROR_NONE;
  386. if (grpc_tcp_trace) {
  387. size_t i;
  388. for (i = 0; i < buf->count; i++) {
  389. char *data =
  390. gpr_dump_slice(buf->slices[i], GPR_DUMP_HEX | GPR_DUMP_ASCII);
  391. gpr_log(GPR_DEBUG, "WRITE %p (peer=%s): %s", tcp, tcp->peer_string, data);
  392. gpr_free(data);
  393. }
  394. }
  395. GPR_TIMER_BEGIN("tcp_write", 0);
  396. GPR_ASSERT(tcp->write_cb == NULL);
  397. if (buf->length == 0) {
  398. GPR_TIMER_END("tcp_write", 0);
  399. grpc_exec_ctx_sched(exec_ctx, cb, grpc_fd_is_shutdown(tcp->em_fd)
  400. ? GRPC_ERROR_CREATE("EOF")
  401. : GRPC_ERROR_NONE,
  402. NULL);
  403. return;
  404. }
  405. tcp->outgoing_buffer = buf;
  406. tcp->outgoing_slice_idx = 0;
  407. tcp->outgoing_byte_idx = 0;
  408. if (!tcp_flush(tcp, &error)) {
  409. TCP_REF(tcp, "write");
  410. tcp->write_cb = cb;
  411. if (grpc_tcp_trace) {
  412. gpr_log(GPR_DEBUG, "write: delayed");
  413. }
  414. grpc_fd_notify_on_write(exec_ctx, tcp->em_fd, &tcp->write_closure);
  415. } else {
  416. if (grpc_tcp_trace) {
  417. const char *str = grpc_error_string(error);
  418. gpr_log(GPR_DEBUG, "write: %s", str);
  419. grpc_error_free_string(str);
  420. }
  421. grpc_exec_ctx_sched(exec_ctx, cb, error, NULL);
  422. }
  423. GPR_TIMER_END("tcp_write", 0);
  424. }
  425. static void tcp_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
  426. grpc_pollset *pollset) {
  427. grpc_tcp *tcp = (grpc_tcp *)ep;
  428. grpc_pollset_add_fd(exec_ctx, pollset, tcp->em_fd);
  429. }
  430. static void tcp_add_to_pollset_set(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
  431. grpc_pollset_set *pollset_set) {
  432. grpc_tcp *tcp = (grpc_tcp *)ep;
  433. grpc_pollset_set_add_fd(exec_ctx, pollset_set, tcp->em_fd);
  434. }
  435. static char *tcp_get_peer(grpc_endpoint *ep) {
  436. grpc_tcp *tcp = (grpc_tcp *)ep;
  437. return gpr_strdup(tcp->peer_string);
  438. }
  439. static grpc_workqueue *tcp_get_workqueue(grpc_endpoint *ep) {
  440. grpc_tcp *tcp = (grpc_tcp *)ep;
  441. return grpc_fd_get_workqueue(tcp->em_fd);
  442. }
  443. static grpc_buffer_user *tcp_get_buffer_user(grpc_endpoint *ep) {
  444. grpc_tcp *tcp = (grpc_tcp *)ep;
  445. return &tcp->buffer_user;
  446. }
  447. static const grpc_endpoint_vtable vtable = {tcp_read,
  448. tcp_write,
  449. tcp_get_workqueue,
  450. tcp_add_to_pollset,
  451. tcp_add_to_pollset_set,
  452. tcp_shutdown,
  453. tcp_destroy,
  454. tcp_get_buffer_user,
  455. tcp_get_peer};
  456. grpc_endpoint *grpc_tcp_create(grpc_fd *em_fd, grpc_buffer_pool *buffer_pool,
  457. size_t slice_size, const char *peer_string) {
  458. grpc_tcp *tcp = (grpc_tcp *)gpr_malloc(sizeof(grpc_tcp));
  459. tcp->base.vtable = &vtable;
  460. tcp->peer_string = gpr_strdup(peer_string);
  461. tcp->fd = grpc_fd_wrapped_fd(em_fd);
  462. tcp->read_cb = NULL;
  463. tcp->write_cb = NULL;
  464. tcp->release_fd_cb = NULL;
  465. tcp->release_fd = NULL;
  466. tcp->incoming_buffer = NULL;
  467. tcp->slice_size = slice_size;
  468. tcp->iov_size = 1;
  469. tcp->finished_edge = true;
  470. /* paired with unref in grpc_tcp_destroy, and with the shutdown for our
  471. * buffer_user */
  472. gpr_ref_init(&tcp->refcount, 2);
  473. gpr_atm_no_barrier_store(&tcp->shutdown_count, 0);
  474. tcp->em_fd = em_fd;
  475. tcp->read_closure.cb = tcp_handle_read;
  476. tcp->read_closure.cb_arg = tcp;
  477. tcp->write_closure.cb = tcp_handle_write;
  478. tcp->write_closure.cb_arg = tcp;
  479. gpr_slice_buffer_init(&tcp->last_read_buffer);
  480. grpc_buffer_user_init(&tcp->buffer_user, buffer_pool);
  481. grpc_buffer_user_slice_allocator_init(
  482. &tcp->slice_allocator, &tcp->buffer_user, tcp_read_allocation_done, tcp);
  483. /* Tell network status tracker about new endpoint */
  484. grpc_network_status_register_endpoint(&tcp->base);
  485. return &tcp->base;
  486. }
  487. int grpc_tcp_fd(grpc_endpoint *ep) {
  488. grpc_tcp *tcp = (grpc_tcp *)ep;
  489. GPR_ASSERT(ep->vtable == &vtable);
  490. return grpc_fd_wrapped_fd(tcp->em_fd);
  491. }
  492. void grpc_tcp_destroy_and_release_fd(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
  493. int *fd, grpc_closure *done) {
  494. grpc_network_status_unregister_endpoint(ep);
  495. grpc_tcp *tcp = (grpc_tcp *)ep;
  496. GPR_ASSERT(ep->vtable == &vtable);
  497. tcp->release_fd = fd;
  498. tcp->release_fd_cb = done;
  499. tcp_maybe_shutdown_buffer_user(exec_ctx, tcp);
  500. gpr_slice_buffer_reset_and_unref(&tcp->last_read_buffer);
  501. TCP_UNREF(exec_ctx, tcp, "destroy");
  502. }
  503. #endif