tcp_posix.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. *
  3. * Copyright 2015 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. #include "src/core/lib/iomgr/port.h"
  19. #ifdef GRPC_POSIX_SOCKET
  20. #include "src/core/lib/iomgr/network_status_tracker.h"
  21. #include "src/core/lib/iomgr/tcp_posix.h"
  22. #include <errno.h>
  23. #include <stdbool.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/socket.h>
  27. #include <sys/types.h>
  28. #include <unistd.h>
  29. #include <grpc/slice.h>
  30. #include <grpc/support/alloc.h>
  31. #include <grpc/support/log.h>
  32. #include <grpc/support/string_util.h>
  33. #include <grpc/support/sync.h>
  34. #include <grpc/support/time.h>
  35. #include <grpc/support/useful.h>
  36. #include "src/core/lib/channel/channel_args.h"
  37. #include "src/core/lib/debug/stats.h"
  38. #include "src/core/lib/debug/trace.h"
  39. #include "src/core/lib/iomgr/ev_posix.h"
  40. #include "src/core/lib/profiling/timers.h"
  41. #include "src/core/lib/slice/slice_internal.h"
  42. #include "src/core/lib/slice/slice_string_helpers.h"
  43. #include "src/core/lib/support/string.h"
  44. #ifdef GRPC_HAVE_MSG_NOSIGNAL
  45. #define SENDMSG_FLAGS MSG_NOSIGNAL
  46. #else
  47. #define SENDMSG_FLAGS 0
  48. #endif
  49. #ifdef GRPC_MSG_IOVLEN_TYPE
  50. typedef GRPC_MSG_IOVLEN_TYPE msg_iovlen_type;
  51. #else
  52. typedef size_t msg_iovlen_type;
  53. #endif
  54. grpc_tracer_flag grpc_tcp_trace = GRPC_TRACER_INITIALIZER(false, "tcp");
  55. typedef struct {
  56. grpc_endpoint base;
  57. grpc_fd *em_fd;
  58. int fd;
  59. bool finished_edge;
  60. double target_length;
  61. double bytes_read_this_round;
  62. gpr_refcount refcount;
  63. gpr_atm shutdown_count;
  64. int min_read_chunk_size;
  65. int max_read_chunk_size;
  66. /* garbage after the last read */
  67. grpc_slice_buffer last_read_buffer;
  68. grpc_slice_buffer *incoming_buffer;
  69. grpc_slice_buffer *outgoing_buffer;
  70. /** slice within outgoing_buffer to write next */
  71. size_t outgoing_slice_idx;
  72. /** byte within outgoing_buffer->slices[outgoing_slice_idx] to write next */
  73. size_t outgoing_byte_idx;
  74. grpc_closure *read_cb;
  75. grpc_closure *write_cb;
  76. grpc_closure *release_fd_cb;
  77. int *release_fd;
  78. grpc_closure read_closure;
  79. grpc_closure write_closure;
  80. char *peer_string;
  81. grpc_resource_user *resource_user;
  82. grpc_resource_user_slice_allocator slice_allocator;
  83. } grpc_tcp;
  84. static void add_to_estimate(grpc_tcp *tcp, size_t bytes) {
  85. tcp->bytes_read_this_round += (double)bytes;
  86. }
  87. static void finish_estimate(grpc_tcp *tcp) {
  88. /* If we read >80% of the target buffer in one read loop, increase the size
  89. of the target buffer to either the amount read, or twice its previous
  90. value */
  91. if (tcp->bytes_read_this_round > tcp->target_length * 0.8) {
  92. tcp->target_length =
  93. GPR_MAX(2 * tcp->target_length, tcp->bytes_read_this_round);
  94. } else {
  95. tcp->target_length =
  96. 0.99 * tcp->target_length + 0.01 * tcp->bytes_read_this_round;
  97. }
  98. tcp->bytes_read_this_round = 0;
  99. }
  100. static size_t get_target_read_size(grpc_tcp *tcp) {
  101. grpc_resource_quota *rq = grpc_resource_user_quota(tcp->resource_user);
  102. double pressure = grpc_resource_quota_get_memory_pressure(rq);
  103. double target =
  104. tcp->target_length * (pressure > 0.8 ? (1.0 - pressure) / 0.2 : 1.0);
  105. size_t sz = (((size_t)GPR_CLAMP(target, tcp->min_read_chunk_size,
  106. tcp->max_read_chunk_size)) +
  107. 255) &
  108. ~(size_t)255;
  109. /* don't use more than 1/16th of the overall resource quota for a single read
  110. * alloc */
  111. size_t rqmax = grpc_resource_quota_peek_size(rq);
  112. if (sz > rqmax / 16 && rqmax > 1024) {
  113. sz = rqmax / 16;
  114. }
  115. return sz;
  116. }
  117. static grpc_error *tcp_annotate_error(grpc_error *src_error, grpc_tcp *tcp) {
  118. return grpc_error_set_str(
  119. grpc_error_set_int(src_error, GRPC_ERROR_INT_FD, tcp->fd),
  120. GRPC_ERROR_STR_TARGET_ADDRESS,
  121. grpc_slice_from_copied_string(tcp->peer_string));
  122. }
  123. static void tcp_handle_read(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */,
  124. grpc_error *error);
  125. static void tcp_handle_write(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */,
  126. grpc_error *error);
  127. static void tcp_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
  128. grpc_error *why) {
  129. grpc_tcp *tcp = (grpc_tcp *)ep;
  130. grpc_fd_shutdown(exec_ctx, tcp->em_fd, why);
  131. grpc_resource_user_shutdown(exec_ctx, tcp->resource_user);
  132. }
  133. static void tcp_free(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
  134. grpc_fd_orphan(exec_ctx, tcp->em_fd, tcp->release_fd_cb, tcp->release_fd,
  135. false /* already_closed */, "tcp_unref_orphan");
  136. grpc_slice_buffer_destroy_internal(exec_ctx, &tcp->last_read_buffer);
  137. grpc_resource_user_unref(exec_ctx, tcp->resource_user);
  138. gpr_free(tcp->peer_string);
  139. gpr_free(tcp);
  140. }
  141. #ifndef NDEBUG
  142. #define TCP_UNREF(cl, tcp, reason) \
  143. tcp_unref((cl), (tcp), (reason), __FILE__, __LINE__)
  144. #define TCP_REF(tcp, reason) tcp_ref((tcp), (reason), __FILE__, __LINE__)
  145. static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp,
  146. const char *reason, const char *file, int line) {
  147. if (GRPC_TRACER_ON(grpc_tcp_trace)) {
  148. gpr_atm val = gpr_atm_no_barrier_load(&tcp->refcount.count);
  149. gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
  150. "TCP unref %p : %s %" PRIdPTR " -> %" PRIdPTR, tcp, reason, val,
  151. val - 1);
  152. }
  153. if (gpr_unref(&tcp->refcount)) {
  154. tcp_free(exec_ctx, tcp);
  155. }
  156. }
  157. static void tcp_ref(grpc_tcp *tcp, const char *reason, const char *file,
  158. int line) {
  159. if (GRPC_TRACER_ON(grpc_tcp_trace)) {
  160. gpr_atm val = gpr_atm_no_barrier_load(&tcp->refcount.count);
  161. gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
  162. "TCP ref %p : %s %" PRIdPTR " -> %" PRIdPTR, tcp, reason, val,
  163. val + 1);
  164. }
  165. gpr_ref(&tcp->refcount);
  166. }
  167. #else
  168. #define TCP_UNREF(cl, tcp, reason) tcp_unref((cl), (tcp))
  169. #define TCP_REF(tcp, reason) tcp_ref((tcp))
  170. static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
  171. if (gpr_unref(&tcp->refcount)) {
  172. tcp_free(exec_ctx, tcp);
  173. }
  174. }
  175. static void tcp_ref(grpc_tcp *tcp) { gpr_ref(&tcp->refcount); }
  176. #endif
  177. static void tcp_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
  178. grpc_network_status_unregister_endpoint(ep);
  179. grpc_tcp *tcp = (grpc_tcp *)ep;
  180. grpc_slice_buffer_reset_and_unref_internal(exec_ctx, &tcp->last_read_buffer);
  181. TCP_UNREF(exec_ctx, tcp, "destroy");
  182. }
  183. static void call_read_cb(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp,
  184. grpc_error *error) {
  185. grpc_closure *cb = tcp->read_cb;
  186. if (GRPC_TRACER_ON(grpc_tcp_trace)) {
  187. size_t i;
  188. const char *str = grpc_error_string(error);
  189. gpr_log(GPR_DEBUG, "read: error=%s", str);
  190. for (i = 0; i < tcp->incoming_buffer->count; i++) {
  191. char *dump = grpc_dump_slice(tcp->incoming_buffer->slices[i],
  192. GPR_DUMP_HEX | GPR_DUMP_ASCII);
  193. gpr_log(GPR_DEBUG, "READ %p (peer=%s): %s", tcp, tcp->peer_string, dump);
  194. gpr_free(dump);
  195. }
  196. }
  197. tcp->read_cb = NULL;
  198. tcp->incoming_buffer = NULL;
  199. GRPC_CLOSURE_RUN(exec_ctx, cb, error);
  200. }
  201. #define MAX_READ_IOVEC 4
  202. static void tcp_do_read(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
  203. struct msghdr msg;
  204. struct iovec iov[MAX_READ_IOVEC];
  205. ssize_t read_bytes;
  206. size_t i;
  207. GPR_ASSERT(!tcp->finished_edge);
  208. GPR_ASSERT(tcp->incoming_buffer->count <= MAX_READ_IOVEC);
  209. GPR_TIMER_BEGIN("tcp_continue_read", 0);
  210. for (i = 0; i < tcp->incoming_buffer->count; i++) {
  211. iov[i].iov_base = GRPC_SLICE_START_PTR(tcp->incoming_buffer->slices[i]);
  212. iov[i].iov_len = GRPC_SLICE_LENGTH(tcp->incoming_buffer->slices[i]);
  213. }
  214. msg.msg_name = NULL;
  215. msg.msg_namelen = 0;
  216. msg.msg_iov = iov;
  217. msg.msg_iovlen = tcp->incoming_buffer->count;
  218. msg.msg_control = NULL;
  219. msg.msg_controllen = 0;
  220. msg.msg_flags = 0;
  221. GRPC_STATS_INC_TCP_READ_IOV_SIZE(exec_ctx, tcp->incoming_buffer->count);
  222. GPR_TIMER_BEGIN("recvmsg", 0);
  223. do {
  224. GRPC_STATS_INC_SYSCALL_READ(exec_ctx);
  225. read_bytes = recvmsg(tcp->fd, &msg, 0);
  226. } while (read_bytes < 0 && errno == EINTR);
  227. GPR_TIMER_END("recvmsg", read_bytes >= 0);
  228. if (read_bytes < 0) {
  229. /* NB: After calling call_read_cb a parallel call of the read handler may
  230. * be running. */
  231. if (errno == EAGAIN) {
  232. finish_estimate(tcp);
  233. /* We've consumed the edge, request a new one */
  234. grpc_fd_notify_on_read(exec_ctx, tcp->em_fd, &tcp->read_closure);
  235. } else {
  236. grpc_slice_buffer_reset_and_unref_internal(exec_ctx,
  237. tcp->incoming_buffer);
  238. call_read_cb(exec_ctx, tcp,
  239. tcp_annotate_error(GRPC_OS_ERROR(errno, "recvmsg"), tcp));
  240. TCP_UNREF(exec_ctx, tcp, "read");
  241. }
  242. } else if (read_bytes == 0) {
  243. /* 0 read size ==> end of stream */
  244. grpc_slice_buffer_reset_and_unref_internal(exec_ctx, tcp->incoming_buffer);
  245. call_read_cb(
  246. exec_ctx, tcp,
  247. tcp_annotate_error(
  248. GRPC_ERROR_CREATE_FROM_STATIC_STRING("Socket closed"), tcp));
  249. TCP_UNREF(exec_ctx, tcp, "read");
  250. } else {
  251. GRPC_STATS_INC_TCP_READ_SIZE(exec_ctx, read_bytes);
  252. add_to_estimate(tcp, (size_t)read_bytes);
  253. GPR_ASSERT((size_t)read_bytes <= tcp->incoming_buffer->length);
  254. if ((size_t)read_bytes < tcp->incoming_buffer->length) {
  255. grpc_slice_buffer_trim_end(
  256. tcp->incoming_buffer,
  257. tcp->incoming_buffer->length - (size_t)read_bytes,
  258. &tcp->last_read_buffer);
  259. }
  260. GPR_ASSERT((size_t)read_bytes == tcp->incoming_buffer->length);
  261. call_read_cb(exec_ctx, tcp, GRPC_ERROR_NONE);
  262. TCP_UNREF(exec_ctx, tcp, "read");
  263. }
  264. GPR_TIMER_END("tcp_continue_read", 0);
  265. }
  266. static void tcp_read_allocation_done(grpc_exec_ctx *exec_ctx, void *tcpp,
  267. grpc_error *error) {
  268. grpc_tcp *tcp = tcpp;
  269. if (error != GRPC_ERROR_NONE) {
  270. grpc_slice_buffer_reset_and_unref_internal(exec_ctx, tcp->incoming_buffer);
  271. grpc_slice_buffer_reset_and_unref_internal(exec_ctx,
  272. &tcp->last_read_buffer);
  273. call_read_cb(exec_ctx, tcp, GRPC_ERROR_REF(error));
  274. TCP_UNREF(exec_ctx, tcp, "read");
  275. } else {
  276. tcp_do_read(exec_ctx, tcp);
  277. }
  278. }
  279. static void tcp_continue_read(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
  280. size_t target_read_size = get_target_read_size(tcp);
  281. if (tcp->incoming_buffer->length < target_read_size &&
  282. tcp->incoming_buffer->count < MAX_READ_IOVEC) {
  283. grpc_resource_user_alloc_slices(exec_ctx, &tcp->slice_allocator,
  284. target_read_size, 1, tcp->incoming_buffer);
  285. } else {
  286. tcp_do_read(exec_ctx, tcp);
  287. }
  288. }
  289. static void tcp_handle_read(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */,
  290. grpc_error *error) {
  291. grpc_tcp *tcp = (grpc_tcp *)arg;
  292. GPR_ASSERT(!tcp->finished_edge);
  293. if (error != GRPC_ERROR_NONE) {
  294. grpc_slice_buffer_reset_and_unref_internal(exec_ctx, tcp->incoming_buffer);
  295. grpc_slice_buffer_reset_and_unref_internal(exec_ctx,
  296. &tcp->last_read_buffer);
  297. call_read_cb(exec_ctx, tcp, GRPC_ERROR_REF(error));
  298. TCP_UNREF(exec_ctx, tcp, "read");
  299. } else {
  300. tcp_continue_read(exec_ctx, tcp);
  301. }
  302. }
  303. static void tcp_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
  304. grpc_slice_buffer *incoming_buffer, grpc_closure *cb) {
  305. grpc_tcp *tcp = (grpc_tcp *)ep;
  306. GPR_ASSERT(tcp->read_cb == NULL);
  307. tcp->read_cb = cb;
  308. tcp->incoming_buffer = incoming_buffer;
  309. grpc_slice_buffer_reset_and_unref_internal(exec_ctx, incoming_buffer);
  310. grpc_slice_buffer_swap(incoming_buffer, &tcp->last_read_buffer);
  311. TCP_REF(tcp, "read");
  312. if (tcp->finished_edge) {
  313. tcp->finished_edge = false;
  314. grpc_fd_notify_on_read(exec_ctx, tcp->em_fd, &tcp->read_closure);
  315. } else {
  316. GRPC_CLOSURE_SCHED(exec_ctx, &tcp->read_closure, GRPC_ERROR_NONE);
  317. }
  318. }
  319. /* returns true if done, false if pending; if returning true, *error is set */
  320. #define MAX_WRITE_IOVEC 1000
  321. static bool tcp_flush(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp,
  322. grpc_error **error) {
  323. struct msghdr msg;
  324. struct iovec iov[MAX_WRITE_IOVEC];
  325. msg_iovlen_type iov_size;
  326. ssize_t sent_length;
  327. size_t sending_length;
  328. size_t trailing;
  329. size_t unwind_slice_idx;
  330. size_t unwind_byte_idx;
  331. for (;;) {
  332. sending_length = 0;
  333. unwind_slice_idx = tcp->outgoing_slice_idx;
  334. unwind_byte_idx = tcp->outgoing_byte_idx;
  335. for (iov_size = 0; tcp->outgoing_slice_idx != tcp->outgoing_buffer->count &&
  336. iov_size != MAX_WRITE_IOVEC;
  337. iov_size++) {
  338. iov[iov_size].iov_base =
  339. GRPC_SLICE_START_PTR(
  340. tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]) +
  341. tcp->outgoing_byte_idx;
  342. iov[iov_size].iov_len =
  343. GRPC_SLICE_LENGTH(
  344. tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]) -
  345. tcp->outgoing_byte_idx;
  346. sending_length += iov[iov_size].iov_len;
  347. tcp->outgoing_slice_idx++;
  348. tcp->outgoing_byte_idx = 0;
  349. }
  350. GPR_ASSERT(iov_size > 0);
  351. msg.msg_name = NULL;
  352. msg.msg_namelen = 0;
  353. msg.msg_iov = iov;
  354. msg.msg_iovlen = iov_size;
  355. msg.msg_control = NULL;
  356. msg.msg_controllen = 0;
  357. msg.msg_flags = 0;
  358. GRPC_STATS_INC_TCP_WRITE_SIZE(exec_ctx, sending_length);
  359. GRPC_STATS_INC_TCP_WRITE_IOV_SIZE(exec_ctx, iov_size);
  360. GPR_TIMER_BEGIN("sendmsg", 1);
  361. do {
  362. /* TODO(klempner): Cork if this is a partial write */
  363. GRPC_STATS_INC_SYSCALL_WRITE(exec_ctx);
  364. sent_length = sendmsg(tcp->fd, &msg, SENDMSG_FLAGS);
  365. } while (sent_length < 0 && errno == EINTR);
  366. GPR_TIMER_END("sendmsg", 0);
  367. if (sent_length < 0) {
  368. if (errno == EAGAIN) {
  369. tcp->outgoing_slice_idx = unwind_slice_idx;
  370. tcp->outgoing_byte_idx = unwind_byte_idx;
  371. return false;
  372. } else if (errno == EPIPE) {
  373. *error = grpc_error_set_int(GRPC_OS_ERROR(errno, "sendmsg"),
  374. GRPC_ERROR_INT_GRPC_STATUS,
  375. GRPC_STATUS_UNAVAILABLE);
  376. return true;
  377. } else {
  378. *error = tcp_annotate_error(GRPC_OS_ERROR(errno, "sendmsg"), tcp);
  379. return true;
  380. }
  381. }
  382. GPR_ASSERT(tcp->outgoing_byte_idx == 0);
  383. trailing = sending_length - (size_t)sent_length;
  384. while (trailing > 0) {
  385. size_t slice_length;
  386. tcp->outgoing_slice_idx--;
  387. slice_length = GRPC_SLICE_LENGTH(
  388. tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]);
  389. if (slice_length > trailing) {
  390. tcp->outgoing_byte_idx = slice_length - trailing;
  391. break;
  392. } else {
  393. trailing -= slice_length;
  394. }
  395. }
  396. if (tcp->outgoing_slice_idx == tcp->outgoing_buffer->count) {
  397. *error = GRPC_ERROR_NONE;
  398. return true;
  399. }
  400. };
  401. }
  402. static void tcp_handle_write(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */,
  403. grpc_error *error) {
  404. grpc_tcp *tcp = (grpc_tcp *)arg;
  405. grpc_closure *cb;
  406. if (error != GRPC_ERROR_NONE) {
  407. cb = tcp->write_cb;
  408. tcp->write_cb = NULL;
  409. cb->cb(exec_ctx, cb->cb_arg, error);
  410. TCP_UNREF(exec_ctx, tcp, "write");
  411. return;
  412. }
  413. if (!tcp_flush(exec_ctx, tcp, &error)) {
  414. if (GRPC_TRACER_ON(grpc_tcp_trace)) {
  415. gpr_log(GPR_DEBUG, "write: delayed");
  416. }
  417. grpc_fd_notify_on_write(exec_ctx, tcp->em_fd, &tcp->write_closure);
  418. } else {
  419. cb = tcp->write_cb;
  420. tcp->write_cb = NULL;
  421. if (GRPC_TRACER_ON(grpc_tcp_trace)) {
  422. const char *str = grpc_error_string(error);
  423. gpr_log(GPR_DEBUG, "write: %s", str);
  424. }
  425. GRPC_CLOSURE_RUN(exec_ctx, cb, error);
  426. TCP_UNREF(exec_ctx, tcp, "write");
  427. }
  428. }
  429. static void tcp_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
  430. grpc_slice_buffer *buf, grpc_closure *cb) {
  431. grpc_tcp *tcp = (grpc_tcp *)ep;
  432. grpc_error *error = GRPC_ERROR_NONE;
  433. if (GRPC_TRACER_ON(grpc_tcp_trace)) {
  434. size_t i;
  435. for (i = 0; i < buf->count; i++) {
  436. char *data =
  437. grpc_dump_slice(buf->slices[i], GPR_DUMP_HEX | GPR_DUMP_ASCII);
  438. gpr_log(GPR_DEBUG, "WRITE %p (peer=%s): %s", tcp, tcp->peer_string, data);
  439. gpr_free(data);
  440. }
  441. }
  442. GPR_TIMER_BEGIN("tcp_write", 0);
  443. GPR_ASSERT(tcp->write_cb == NULL);
  444. if (buf->length == 0) {
  445. GPR_TIMER_END("tcp_write", 0);
  446. GRPC_CLOSURE_SCHED(
  447. exec_ctx, cb,
  448. grpc_fd_is_shutdown(tcp->em_fd)
  449. ? tcp_annotate_error(GRPC_ERROR_CREATE_FROM_STATIC_STRING("EOF"),
  450. tcp)
  451. : GRPC_ERROR_NONE);
  452. return;
  453. }
  454. tcp->outgoing_buffer = buf;
  455. tcp->outgoing_slice_idx = 0;
  456. tcp->outgoing_byte_idx = 0;
  457. if (!tcp_flush(exec_ctx, tcp, &error)) {
  458. TCP_REF(tcp, "write");
  459. tcp->write_cb = cb;
  460. if (GRPC_TRACER_ON(grpc_tcp_trace)) {
  461. gpr_log(GPR_DEBUG, "write: delayed");
  462. }
  463. grpc_fd_notify_on_write(exec_ctx, tcp->em_fd, &tcp->write_closure);
  464. } else {
  465. if (GRPC_TRACER_ON(grpc_tcp_trace)) {
  466. const char *str = grpc_error_string(error);
  467. gpr_log(GPR_DEBUG, "write: %s", str);
  468. }
  469. GRPC_CLOSURE_SCHED(exec_ctx, cb, error);
  470. }
  471. GPR_TIMER_END("tcp_write", 0);
  472. }
  473. static void tcp_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
  474. grpc_pollset *pollset) {
  475. grpc_tcp *tcp = (grpc_tcp *)ep;
  476. grpc_pollset_add_fd(exec_ctx, pollset, tcp->em_fd);
  477. }
  478. static void tcp_add_to_pollset_set(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
  479. grpc_pollset_set *pollset_set) {
  480. grpc_tcp *tcp = (grpc_tcp *)ep;
  481. grpc_pollset_set_add_fd(exec_ctx, pollset_set, tcp->em_fd);
  482. }
  483. static char *tcp_get_peer(grpc_endpoint *ep) {
  484. grpc_tcp *tcp = (grpc_tcp *)ep;
  485. return gpr_strdup(tcp->peer_string);
  486. }
  487. static int tcp_get_fd(grpc_endpoint *ep) {
  488. grpc_tcp *tcp = (grpc_tcp *)ep;
  489. return tcp->fd;
  490. }
  491. static grpc_resource_user *tcp_get_resource_user(grpc_endpoint *ep) {
  492. grpc_tcp *tcp = (grpc_tcp *)ep;
  493. return tcp->resource_user;
  494. }
  495. static const grpc_endpoint_vtable vtable = {
  496. tcp_read, tcp_write, tcp_add_to_pollset, tcp_add_to_pollset_set,
  497. tcp_shutdown, tcp_destroy, tcp_get_resource_user, tcp_get_peer,
  498. tcp_get_fd};
  499. #define MAX_CHUNK_SIZE 32 * 1024 * 1024
  500. grpc_endpoint *grpc_tcp_create(grpc_exec_ctx *exec_ctx, grpc_fd *em_fd,
  501. const grpc_channel_args *channel_args,
  502. const char *peer_string) {
  503. int tcp_read_chunk_size = GRPC_TCP_DEFAULT_READ_SLICE_SIZE;
  504. int tcp_max_read_chunk_size = 4 * 1024 * 1024;
  505. int tcp_min_read_chunk_size = 256;
  506. grpc_resource_quota *resource_quota = grpc_resource_quota_create(NULL);
  507. if (channel_args != NULL) {
  508. for (size_t i = 0; i < channel_args->num_args; i++) {
  509. if (0 ==
  510. strcmp(channel_args->args[i].key, GRPC_ARG_TCP_READ_CHUNK_SIZE)) {
  511. grpc_integer_options options = {(int)tcp_read_chunk_size, 1,
  512. MAX_CHUNK_SIZE};
  513. tcp_read_chunk_size =
  514. grpc_channel_arg_get_integer(&channel_args->args[i], options);
  515. } else if (0 == strcmp(channel_args->args[i].key,
  516. GRPC_ARG_TCP_MIN_READ_CHUNK_SIZE)) {
  517. grpc_integer_options options = {(int)tcp_read_chunk_size, 1,
  518. MAX_CHUNK_SIZE};
  519. tcp_min_read_chunk_size =
  520. grpc_channel_arg_get_integer(&channel_args->args[i], options);
  521. } else if (0 == strcmp(channel_args->args[i].key,
  522. GRPC_ARG_TCP_MAX_READ_CHUNK_SIZE)) {
  523. grpc_integer_options options = {(int)tcp_read_chunk_size, 1,
  524. MAX_CHUNK_SIZE};
  525. tcp_max_read_chunk_size =
  526. grpc_channel_arg_get_integer(&channel_args->args[i], options);
  527. } else if (0 ==
  528. strcmp(channel_args->args[i].key, GRPC_ARG_RESOURCE_QUOTA)) {
  529. grpc_resource_quota_unref_internal(exec_ctx, resource_quota);
  530. resource_quota = grpc_resource_quota_ref_internal(
  531. channel_args->args[i].value.pointer.p);
  532. }
  533. }
  534. }
  535. if (tcp_min_read_chunk_size > tcp_max_read_chunk_size) {
  536. tcp_min_read_chunk_size = tcp_max_read_chunk_size;
  537. }
  538. tcp_read_chunk_size = GPR_CLAMP(tcp_read_chunk_size, tcp_min_read_chunk_size,
  539. tcp_max_read_chunk_size);
  540. grpc_tcp *tcp = (grpc_tcp *)gpr_malloc(sizeof(grpc_tcp));
  541. tcp->base.vtable = &vtable;
  542. tcp->peer_string = gpr_strdup(peer_string);
  543. tcp->fd = grpc_fd_wrapped_fd(em_fd);
  544. tcp->read_cb = NULL;
  545. tcp->write_cb = NULL;
  546. tcp->release_fd_cb = NULL;
  547. tcp->release_fd = NULL;
  548. tcp->incoming_buffer = NULL;
  549. tcp->target_length = (double)tcp_read_chunk_size;
  550. tcp->min_read_chunk_size = tcp_min_read_chunk_size;
  551. tcp->max_read_chunk_size = tcp_max_read_chunk_size;
  552. tcp->bytes_read_this_round = 0;
  553. tcp->finished_edge = true;
  554. /* paired with unref in grpc_tcp_destroy */
  555. gpr_ref_init(&tcp->refcount, 1);
  556. gpr_atm_no_barrier_store(&tcp->shutdown_count, 0);
  557. tcp->em_fd = em_fd;
  558. GRPC_CLOSURE_INIT(&tcp->read_closure, tcp_handle_read, tcp,
  559. grpc_schedule_on_exec_ctx);
  560. GRPC_CLOSURE_INIT(&tcp->write_closure, tcp_handle_write, tcp,
  561. grpc_schedule_on_exec_ctx);
  562. grpc_slice_buffer_init(&tcp->last_read_buffer);
  563. tcp->resource_user = grpc_resource_user_create(resource_quota, peer_string);
  564. grpc_resource_user_slice_allocator_init(
  565. &tcp->slice_allocator, tcp->resource_user, tcp_read_allocation_done, tcp);
  566. /* Tell network status tracker about new endpoint */
  567. grpc_network_status_register_endpoint(&tcp->base);
  568. grpc_resource_quota_unref_internal(exec_ctx, resource_quota);
  569. return &tcp->base;
  570. }
  571. int grpc_tcp_fd(grpc_endpoint *ep) {
  572. grpc_tcp *tcp = (grpc_tcp *)ep;
  573. GPR_ASSERT(ep->vtable == &vtable);
  574. return grpc_fd_wrapped_fd(tcp->em_fd);
  575. }
  576. void grpc_tcp_destroy_and_release_fd(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
  577. int *fd, grpc_closure *done) {
  578. grpc_network_status_unregister_endpoint(ep);
  579. grpc_tcp *tcp = (grpc_tcp *)ep;
  580. GPR_ASSERT(ep->vtable == &vtable);
  581. tcp->release_fd = fd;
  582. tcp->release_fd_cb = done;
  583. grpc_slice_buffer_reset_and_unref_internal(exec_ctx, &tcp->last_read_buffer);
  584. TCP_UNREF(exec_ctx, tcp, "destroy");
  585. }
  586. #endif