grpc 1.6.7 → 1.7.0.pre1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +579 -77
  3. data/include/grpc/byte_buffer.h +1 -63
  4. data/include/grpc/compression.h +27 -5
  5. data/include/grpc/fork.h +24 -0
  6. data/include/grpc/grpc.h +12 -6
  7. data/include/grpc/grpc_security.h +28 -7
  8. data/include/grpc/impl/codegen/atm.h +1 -0
  9. data/include/grpc/impl/codegen/byte_buffer.h +86 -0
  10. data/include/grpc/impl/codegen/compression_types.h +63 -5
  11. data/include/grpc/impl/codegen/fork.h +48 -0
  12. data/include/grpc/impl/codegen/grpc_types.h +26 -9
  13. data/include/grpc/impl/codegen/port_platform.h +11 -4
  14. data/include/grpc/impl/codegen/slice.h +6 -1
  15. data/include/grpc/impl/codegen/sync.h +3 -1
  16. data/include/grpc/impl/codegen/sync_custom.h +36 -0
  17. data/include/grpc/module.modulemap +75 -3
  18. data/include/grpc/slice.h +1 -5
  19. data/include/grpc/support/sync_custom.h +24 -0
  20. data/src/core/ext/census/base_resources.c +14 -14
  21. data/src/core/ext/census/context.c +7 -5
  22. data/src/core/ext/census/grpc_filter.c +12 -14
  23. data/src/core/ext/census/mlog.c +2 -1
  24. data/src/core/ext/census/resource.c +13 -9
  25. data/src/core/ext/filters/client_channel/channel_connectivity.c +15 -8
  26. data/src/core/ext/filters/client_channel/client_channel.c +418 -439
  27. data/src/core/ext/filters/client_channel/client_channel_factory.c +4 -5
  28. data/src/core/ext/filters/client_channel/client_channel_plugin.c +2 -2
  29. data/src/core/ext/filters/client_channel/http_connect_handshaker.c +7 -5
  30. data/src/core/ext/filters/client_channel/http_proxy.c +17 -21
  31. data/src/core/ext/filters/client_channel/lb_policy.c +1 -1
  32. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.c +7 -7
  33. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.c +371 -257
  34. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.c +7 -5
  35. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.c +25 -14
  36. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.c +16 -16
  37. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.c +33 -28
  38. data/src/core/ext/filters/client_channel/lb_policy_factory.c +10 -8
  39. data/src/core/ext/filters/client_channel/lb_policy_factory.h +1 -1
  40. data/src/core/ext/filters/client_channel/proxy_mapper_registry.c +1 -1
  41. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.c +7 -6
  42. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_posix.c +62 -28
  43. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.c +29 -23
  44. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.c +25 -14
  45. data/src/core/ext/filters/client_channel/retry_throttle.c +9 -6
  46. data/src/core/ext/filters/client_channel/subchannel.c +30 -30
  47. data/src/core/ext/filters/client_channel/subchannel.h +1 -4
  48. data/src/core/ext/filters/client_channel/subchannel_index.c +31 -15
  49. data/src/core/ext/filters/client_channel/subchannel_index.h +7 -0
  50. data/src/core/ext/filters/client_channel/uri_parser.c +4 -3
  51. data/src/core/ext/filters/deadline/deadline_filter.c +78 -39
  52. data/src/core/ext/filters/deadline/deadline_filter.h +7 -1
  53. data/src/core/ext/filters/http/client/http_client_filter.c +14 -14
  54. data/src/core/ext/filters/http/http_filters_plugin.c +1 -1
  55. data/src/core/ext/filters/http/message_compress/message_compress_filter.c +240 -175
  56. data/src/core/ext/filters/http/server/http_server_filter.c +48 -36
  57. data/src/core/ext/filters/load_reporting/{load_reporting_filter.c → server_load_reporting_filter.c} +11 -12
  58. data/src/core/ext/filters/load_reporting/{load_reporting_filter.h → server_load_reporting_filter.h} +6 -5
  59. data/src/core/ext/filters/load_reporting/{load_reporting.c → server_load_reporting_plugin.c} +19 -13
  60. data/src/core/ext/filters/load_reporting/{load_reporting.h → server_load_reporting_plugin.h} +4 -3
  61. data/src/core/ext/filters/max_age/max_age_filter.c +2 -3
  62. data/src/core/ext/filters/message_size/message_size_filter.c +4 -2
  63. data/src/core/ext/filters/workarounds/workaround_cronet_compression_filter.c +0 -1
  64. data/src/core/ext/transport/chttp2/client/chttp2_connector.c +5 -5
  65. data/src/core/ext/transport/chttp2/client/insecure/channel_create.c +1 -1
  66. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.c +1 -1
  67. data/src/core/ext/transport/chttp2/server/chttp2_server.c +20 -18
  68. data/src/core/ext/transport/chttp2/transport/chttp2_plugin.c +1 -0
  69. data/src/core/ext/transport/chttp2/transport/chttp2_transport.c +493 -210
  70. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +1 -0
  71. data/src/core/ext/transport/chttp2/transport/flow_control.c +9 -8
  72. data/src/core/ext/transport/chttp2/transport/frame_data.c +2 -2
  73. data/src/core/ext/transport/chttp2/transport/frame_goaway.c +2 -2
  74. data/src/core/ext/transport/chttp2/transport/frame_ping.c +5 -4
  75. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.c +1 -1
  76. data/src/core/ext/transport/chttp2/transport/frame_settings.c +10 -9
  77. data/src/core/ext/transport/chttp2/transport/frame_window_update.c +9 -5
  78. data/src/core/ext/transport/chttp2/transport/hpack_encoder.c +62 -41
  79. data/src/core/ext/transport/chttp2/transport/hpack_parser.c +52 -8
  80. data/src/core/ext/transport/chttp2/transport/hpack_table.c +2 -2
  81. data/src/core/ext/transport/chttp2/transport/incoming_metadata.c +3 -2
  82. data/src/core/ext/transport/chttp2/transport/internal.h +60 -30
  83. data/src/core/ext/transport/chttp2/transport/parsing.c +16 -5
  84. data/src/core/ext/transport/chttp2/transport/stream_lists.c +36 -16
  85. data/src/core/ext/transport/chttp2/transport/stream_map.c +6 -4
  86. data/src/core/ext/transport/chttp2/transport/writing.c +133 -105
  87. data/src/core/ext/transport/inproc/inproc_transport.c +61 -65
  88. data/src/core/lib/channel/channel_args.c +112 -12
  89. data/src/core/lib/channel/channel_args.h +31 -0
  90. data/src/core/lib/channel/channel_stack.c +1 -15
  91. data/src/core/lib/channel/channel_stack.h +3 -10
  92. data/src/core/lib/channel/channel_stack_builder.c +41 -10
  93. data/src/core/lib/channel/channel_stack_builder.h +10 -0
  94. data/src/core/lib/channel/connected_channel.c +94 -23
  95. data/src/core/lib/channel/handshaker.c +8 -6
  96. data/src/core/lib/channel/handshaker_registry.c +1 -1
  97. data/src/core/lib/compression/algorithm_metadata.h +14 -0
  98. data/src/core/lib/compression/compression.c +101 -1
  99. data/src/core/lib/compression/stream_compression.c +32 -146
  100. data/src/core/lib/compression/stream_compression.h +28 -4
  101. data/src/core/lib/compression/stream_compression_gzip.c +228 -0
  102. data/src/core/lib/{iomgr/ev_epoll_thread_pool_linux.h → compression/stream_compression_gzip.h} +5 -7
  103. data/src/core/lib/compression/stream_compression_identity.c +94 -0
  104. data/src/core/lib/{iomgr/ev_epoll_limited_pollers_linux.h → compression/stream_compression_identity.h} +7 -8
  105. data/src/core/lib/debug/stats.c +174 -0
  106. data/src/core/lib/debug/stats.h +61 -0
  107. data/src/core/lib/debug/stats_data.c +687 -0
  108. data/src/core/lib/debug/stats_data.h +470 -0
  109. data/src/core/lib/debug/trace.c +3 -3
  110. data/src/core/lib/debug/trace.h +1 -1
  111. data/src/core/lib/http/format_request.c +1 -1
  112. data/src/core/lib/http/httpcli.c +8 -7
  113. data/src/core/lib/http/httpcli_security_connector.c +2 -1
  114. data/src/core/lib/http/parser.c +4 -3
  115. data/src/core/lib/iomgr/call_combiner.c +202 -0
  116. data/src/core/lib/iomgr/call_combiner.h +121 -0
  117. data/src/core/lib/iomgr/closure.c +18 -4
  118. data/src/core/lib/iomgr/combiner.c +11 -4
  119. data/src/core/lib/iomgr/error.c +26 -24
  120. data/src/core/lib/iomgr/ev_epoll1_linux.c +395 -212
  121. data/src/core/lib/iomgr/ev_epollex_linux.c +141 -128
  122. data/src/core/lib/iomgr/ev_epollsig_linux.c +44 -41
  123. data/src/core/lib/iomgr/ev_poll_posix.c +99 -75
  124. data/src/core/lib/iomgr/ev_posix.c +5 -9
  125. data/src/core/lib/iomgr/ev_posix.h +1 -1
  126. data/src/core/lib/iomgr/exec_ctx.h +6 -1
  127. data/src/core/lib/iomgr/executor.c +142 -36
  128. data/src/core/lib/iomgr/executor.h +6 -1
  129. data/src/core/lib/iomgr/fork_posix.c +88 -0
  130. data/src/core/lib/iomgr/fork_windows.c +39 -0
  131. data/src/core/lib/iomgr/iocp_windows.c +2 -0
  132. data/src/core/lib/iomgr/iomgr.c +2 -8
  133. data/src/core/lib/iomgr/is_epollexclusive_available.c +6 -6
  134. data/src/core/lib/iomgr/load_file.c +2 -1
  135. data/src/core/lib/iomgr/polling_entity.c +9 -9
  136. data/src/core/lib/iomgr/polling_entity.h +7 -1
  137. data/src/core/lib/iomgr/pollset.h +1 -1
  138. data/src/core/lib/iomgr/pollset_uv.c +1 -1
  139. data/src/core/lib/iomgr/pollset_windows.c +3 -3
  140. data/src/core/lib/iomgr/port.h +4 -0
  141. data/src/core/lib/iomgr/resolve_address_posix.c +8 -7
  142. data/src/core/lib/iomgr/resolve_address_windows.c +1 -1
  143. data/src/core/lib/iomgr/resource_quota.c +24 -19
  144. data/src/core/lib/iomgr/socket_factory_posix.c +4 -4
  145. data/src/core/lib/iomgr/socket_mutator.c +4 -4
  146. data/src/core/lib/iomgr/socket_utils_windows.c +0 -4
  147. data/src/core/lib/iomgr/tcp_client_posix.c +5 -4
  148. data/src/core/lib/iomgr/tcp_posix.c +181 -20
  149. data/src/core/lib/iomgr/tcp_server_posix.c +8 -7
  150. data/src/core/lib/iomgr/tcp_server_utils_posix_common.c +1 -1
  151. data/src/core/lib/iomgr/timer.h +4 -0
  152. data/src/core/lib/iomgr/timer_generic.c +138 -3
  153. data/src/core/lib/iomgr/timer_generic.h +3 -0
  154. data/src/core/lib/iomgr/timer_heap.c +4 -4
  155. data/src/core/lib/iomgr/timer_manager.c +2 -2
  156. data/src/core/lib/iomgr/timer_uv.c +2 -0
  157. data/src/core/lib/iomgr/udp_server.c +10 -8
  158. data/src/core/lib/iomgr/unix_sockets_posix.c +4 -2
  159. data/src/core/lib/iomgr/wakeup_fd_cv.c +9 -8
  160. data/src/core/lib/iomgr/wakeup_fd_cv.h +2 -2
  161. data/src/core/lib/json/json.c +1 -1
  162. data/src/core/lib/json/json_string.c +13 -13
  163. data/src/core/lib/profiling/timers.h +18 -8
  164. data/src/core/lib/security/credentials/composite/composite_credentials.c +4 -10
  165. data/src/core/lib/security/credentials/google_default/google_default_credentials.c +2 -1
  166. data/src/core/lib/security/credentials/jwt/jwt_verifier.c +11 -6
  167. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.c +4 -4
  168. data/src/core/lib/security/credentials/plugin/plugin_credentials.c +132 -50
  169. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +2 -0
  170. data/src/core/lib/security/transport/client_auth_filter.c +68 -135
  171. data/src/core/lib/security/transport/secure_endpoint.c +110 -90
  172. data/src/core/lib/security/transport/secure_endpoint.h +8 -3
  173. data/src/core/lib/security/transport/security_connector.c +10 -12
  174. data/src/core/lib/security/transport/security_handshaker.c +45 -24
  175. data/src/core/lib/security/transport/server_auth_filter.c +71 -20
  176. data/src/core/lib/slice/b64.c +2 -2
  177. data/src/core/lib/slice/slice.c +16 -14
  178. data/src/core/lib/slice/slice_buffer.c +5 -4
  179. data/src/core/lib/slice/slice_hash_table.c +3 -2
  180. data/src/core/lib/slice/slice_intern.c +8 -5
  181. data/src/core/lib/support/block_annotate.h +22 -0
  182. data/src/core/lib/support/fork.c +62 -0
  183. data/src/core/lib/support/fork.h +35 -0
  184. data/src/core/lib/support/log_linux.c +1 -1
  185. data/src/core/lib/support/string.c +15 -1
  186. data/src/core/lib/support/string.h +3 -0
  187. data/src/core/lib/support/thd_internal.h +6 -0
  188. data/src/core/lib/support/thd_posix.c +56 -0
  189. data/src/core/lib/support/thd_windows.c +2 -0
  190. data/src/core/lib/surface/alarm.c +22 -15
  191. data/src/core/lib/surface/byte_buffer.c +4 -2
  192. data/src/core/lib/surface/call.c +442 -141
  193. data/src/core/lib/surface/call.h +6 -6
  194. data/src/core/lib/surface/call_log_batch.c +1 -1
  195. data/src/core/lib/surface/call_test_only.h +12 -0
  196. data/src/core/lib/surface/channel.c +39 -4
  197. data/src/core/lib/surface/channel_init.c +6 -6
  198. data/src/core/lib/surface/channel_ping.c +2 -2
  199. data/src/core/lib/surface/completion_queue.c +56 -57
  200. data/src/core/lib/surface/init.c +17 -3
  201. data/src/core/lib/surface/init_secure.c +5 -1
  202. data/src/core/lib/surface/lame_client.cc +9 -10
  203. data/src/core/lib/surface/server.c +81 -72
  204. data/src/core/lib/surface/version.c +2 -2
  205. data/src/core/lib/transport/byte_stream.c +1 -0
  206. data/src/core/lib/transport/byte_stream.h +3 -1
  207. data/src/core/lib/transport/connectivity_state.c +2 -1
  208. data/src/core/lib/transport/metadata.c +7 -4
  209. data/src/core/lib/transport/metadata_batch.c +18 -16
  210. data/src/core/lib/transport/metadata_batch.h +1 -0
  211. data/src/core/lib/transport/service_config.c +5 -3
  212. data/src/core/lib/transport/static_metadata.c +395 -614
  213. data/src/core/lib/transport/static_metadata.h +165 -133
  214. data/src/core/lib/transport/status_conversion.c +1 -1
  215. data/src/core/lib/transport/transport.c +20 -20
  216. data/src/core/lib/transport/transport.h +8 -5
  217. data/src/core/lib/transport/transport_impl.h +0 -3
  218. data/src/core/lib/transport/transport_op_string.c +8 -1
  219. data/src/core/plugin_registry/grpc_plugin_registry.c +4 -4
  220. data/src/core/tsi/fake_transport_security.c +133 -2
  221. data/src/core/tsi/fake_transport_security.h +5 -0
  222. data/src/core/tsi/ssl_transport_security.c +105 -8
  223. data/src/core/tsi/ssl_transport_security.h +30 -7
  224. data/src/core/tsi/transport_security.h +8 -2
  225. data/src/core/tsi/transport_security_grpc.c +20 -13
  226. data/src/core/tsi/transport_security_grpc.h +13 -9
  227. data/src/ruby/ext/grpc/rb_call_credentials.c +6 -2
  228. data/src/ruby/ext/grpc/rb_grpc.c +1 -1
  229. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +30 -20
  230. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +50 -35
  231. data/src/ruby/lib/grpc.rb +1 -0
  232. data/src/ruby/lib/grpc/generic/active_call.rb +34 -9
  233. data/src/ruby/lib/grpc/generic/bidi_call.rb +19 -10
  234. data/src/ruby/lib/grpc/generic/client_stub.rb +95 -38
  235. data/src/ruby/lib/grpc/generic/interceptor_registry.rb +53 -0
  236. data/src/ruby/lib/grpc/generic/interceptors.rb +186 -0
  237. data/src/ruby/lib/grpc/generic/rpc_desc.rb +66 -20
  238. data/src/ruby/lib/grpc/generic/rpc_server.rb +15 -3
  239. data/src/ruby/lib/grpc/google_rpc_status_utils.rb +1 -2
  240. data/src/ruby/lib/grpc/version.rb +1 -1
  241. data/src/ruby/pb/grpc/testing/duplicate/echo_duplicate_services_pb.rb +1 -0
  242. data/src/ruby/spec/channel_connection_spec.rb +1 -34
  243. data/src/ruby/spec/client_server_spec.rb +188 -82
  244. data/src/ruby/spec/generic/active_call_spec.rb +65 -11
  245. data/src/ruby/spec/generic/client_interceptors_spec.rb +153 -0
  246. data/src/ruby/spec/generic/interceptor_registry_spec.rb +65 -0
  247. data/src/ruby/spec/generic/rpc_desc_spec.rb +38 -0
  248. data/src/ruby/spec/generic/rpc_server_spec.rb +1 -34
  249. data/src/ruby/spec/generic/server_interceptors_spec.rb +218 -0
  250. data/src/ruby/spec/spec_helper.rb +4 -0
  251. data/src/ruby/spec/support/helpers.rb +73 -0
  252. data/src/ruby/spec/support/services.rb +147 -0
  253. data/third_party/cares/ares_build.h +21 -62
  254. data/third_party/cares/cares/ares.h +23 -1
  255. data/third_party/cares/cares/ares__close_sockets.c +2 -2
  256. data/third_party/cares/cares/ares_create_query.c +3 -3
  257. data/third_party/cares/cares/ares_expand_name.c +6 -2
  258. data/third_party/cares/cares/ares_expand_string.c +1 -1
  259. data/third_party/cares/cares/ares_getnameinfo.c +27 -7
  260. data/third_party/cares/cares/ares_init.c +407 -39
  261. data/third_party/cares/cares/ares_library_init.c +10 -0
  262. data/third_party/cares/cares/ares_library_init.h +2 -1
  263. data/third_party/cares/cares/ares_nowarn.c +6 -6
  264. data/third_party/cares/cares/ares_nowarn.h +2 -2
  265. data/third_party/cares/cares/ares_parse_naptr_reply.c +6 -1
  266. data/third_party/cares/cares/ares_private.h +11 -0
  267. data/third_party/cares/cares/ares_process.c +126 -37
  268. data/third_party/cares/cares/ares_version.h +2 -2
  269. data/third_party/cares/cares/ares_writev.c +2 -2
  270. data/third_party/cares/cares/config-win32.h +8 -34
  271. data/third_party/cares/cares/inet_net_pton.c +2 -2
  272. data/third_party/cares/cares/setup_once.h +5 -5
  273. data/third_party/cares/config_darwin/ares_config.h +98 -196
  274. data/third_party/cares/config_linux/ares_config.h +103 -203
  275. metadata +47 -20
  276. data/src/core/lib/iomgr/ev_epoll_limited_pollers_linux.c +0 -1957
  277. data/src/core/lib/iomgr/ev_epoll_thread_pool_linux.c +0 -1182
@@ -34,7 +34,7 @@
34
34
  #include "src/core/lib/slice/slice_internal.h"
35
35
  #include "src/core/lib/slice/slice_string_helpers.h"
36
36
  #include "src/core/lib/support/string.h"
37
- #include "src/core/tsi/transport_security_interface.h"
37
+ #include "src/core/tsi/transport_security_grpc.h"
38
38
 
39
39
  #define STAGING_BUFFER_SIZE 8192
40
40
 
@@ -42,6 +42,7 @@ typedef struct {
42
42
  grpc_endpoint base;
43
43
  grpc_endpoint *wrapped_ep;
44
44
  struct tsi_frame_protector *protector;
45
+ struct tsi_zero_copy_grpc_protector *zero_copy_protector;
45
46
  gpr_mu protector_mu;
46
47
  /* saved upper level callbacks and user_data. */
47
48
  grpc_closure *read_cb;
@@ -67,6 +68,7 @@ static void destroy(grpc_exec_ctx *exec_ctx, secure_endpoint *secure_ep) {
67
68
  secure_endpoint *ep = secure_ep;
68
69
  grpc_endpoint_destroy(exec_ctx, ep->wrapped_ep);
69
70
  tsi_frame_protector_destroy(ep->protector);
71
+ tsi_zero_copy_grpc_protector_destroy(exec_ctx, ep->zero_copy_protector);
70
72
  grpc_slice_buffer_destroy_internal(exec_ctx, &ep->leftover_bytes);
71
73
  grpc_slice_unref_internal(exec_ctx, ep->read_staging_buffer);
72
74
  grpc_slice_unref_internal(exec_ctx, ep->write_staging_buffer);
@@ -159,51 +161,58 @@ static void on_read(grpc_exec_ctx *exec_ctx, void *user_data,
159
161
  return;
160
162
  }
161
163
 
162
- /* TODO(yangg) check error, maybe bail out early */
163
- for (i = 0; i < ep->source_buffer.count; i++) {
164
- grpc_slice encrypted = ep->source_buffer.slices[i];
165
- uint8_t *message_bytes = GRPC_SLICE_START_PTR(encrypted);
166
- size_t message_size = GRPC_SLICE_LENGTH(encrypted);
167
-
168
- while (message_size > 0 || keep_looping) {
169
- size_t unprotected_buffer_size_written = (size_t)(end - cur);
170
- size_t processed_message_size = message_size;
171
- gpr_mu_lock(&ep->protector_mu);
172
- result = tsi_frame_protector_unprotect(ep->protector, message_bytes,
173
- &processed_message_size, cur,
174
- &unprotected_buffer_size_written);
175
- gpr_mu_unlock(&ep->protector_mu);
176
- if (result != TSI_OK) {
177
- gpr_log(GPR_ERROR, "Decryption error: %s",
178
- tsi_result_to_string(result));
179
- break;
180
- }
181
- message_bytes += processed_message_size;
182
- message_size -= processed_message_size;
183
- cur += unprotected_buffer_size_written;
184
-
185
- if (cur == end) {
186
- flush_read_staging_buffer(ep, &cur, &end);
187
- /* Force to enter the loop again to extract buffered bytes in protector.
188
- The bytes could be buffered because of running out of staging_buffer.
189
- If this happens at the end of all slices, doing another unprotect
190
- avoids leaving data in the protector. */
191
- keep_looping = 1;
192
- } else if (unprotected_buffer_size_written > 0) {
193
- keep_looping = 1;
194
- } else {
195
- keep_looping = 0;
164
+ if (ep->zero_copy_protector != NULL) {
165
+ // Use zero-copy grpc protector to unprotect.
166
+ result = tsi_zero_copy_grpc_protector_unprotect(
167
+ exec_ctx, ep->zero_copy_protector, &ep->source_buffer, ep->read_buffer);
168
+ } else {
169
+ // Use frame protector to unprotect.
170
+ /* TODO(yangg) check error, maybe bail out early */
171
+ for (i = 0; i < ep->source_buffer.count; i++) {
172
+ grpc_slice encrypted = ep->source_buffer.slices[i];
173
+ uint8_t *message_bytes = GRPC_SLICE_START_PTR(encrypted);
174
+ size_t message_size = GRPC_SLICE_LENGTH(encrypted);
175
+
176
+ while (message_size > 0 || keep_looping) {
177
+ size_t unprotected_buffer_size_written = (size_t)(end - cur);
178
+ size_t processed_message_size = message_size;
179
+ gpr_mu_lock(&ep->protector_mu);
180
+ result = tsi_frame_protector_unprotect(
181
+ ep->protector, message_bytes, &processed_message_size, cur,
182
+ &unprotected_buffer_size_written);
183
+ gpr_mu_unlock(&ep->protector_mu);
184
+ if (result != TSI_OK) {
185
+ gpr_log(GPR_ERROR, "Decryption error: %s",
186
+ tsi_result_to_string(result));
187
+ break;
188
+ }
189
+ message_bytes += processed_message_size;
190
+ message_size -= processed_message_size;
191
+ cur += unprotected_buffer_size_written;
192
+
193
+ if (cur == end) {
194
+ flush_read_staging_buffer(ep, &cur, &end);
195
+ /* Force to enter the loop again to extract buffered bytes in
196
+ protector. The bytes could be buffered because of running out of
197
+ staging_buffer. If this happens at the end of all slices, doing
198
+ another unprotect avoids leaving data in the protector. */
199
+ keep_looping = 1;
200
+ } else if (unprotected_buffer_size_written > 0) {
201
+ keep_looping = 1;
202
+ } else {
203
+ keep_looping = 0;
204
+ }
196
205
  }
206
+ if (result != TSI_OK) break;
197
207
  }
198
- if (result != TSI_OK) break;
199
- }
200
208
 
201
- if (cur != GRPC_SLICE_START_PTR(ep->read_staging_buffer)) {
202
- grpc_slice_buffer_add(
203
- ep->read_buffer,
204
- grpc_slice_split_head(
205
- &ep->read_staging_buffer,
206
- (size_t)(cur - GRPC_SLICE_START_PTR(ep->read_staging_buffer))));
209
+ if (cur != GRPC_SLICE_START_PTR(ep->read_staging_buffer)) {
210
+ grpc_slice_buffer_add(
211
+ ep->read_buffer,
212
+ grpc_slice_split_head(
213
+ &ep->read_staging_buffer,
214
+ (size_t)(cur - GRPC_SLICE_START_PTR(ep->read_staging_buffer))));
215
+ }
207
216
  }
208
217
 
209
218
  /* TODO(yangg) experiment with moving this block after read_cb to see if it
@@ -270,54 +279,62 @@ static void endpoint_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *secure_ep,
270
279
  }
271
280
  }
272
281
 
273
- for (i = 0; i < slices->count; i++) {
274
- grpc_slice plain = slices->slices[i];
275
- uint8_t *message_bytes = GRPC_SLICE_START_PTR(plain);
276
- size_t message_size = GRPC_SLICE_LENGTH(plain);
277
- while (message_size > 0) {
278
- size_t protected_buffer_size_to_send = (size_t)(end - cur);
279
- size_t processed_message_size = message_size;
280
- gpr_mu_lock(&ep->protector_mu);
281
- result = tsi_frame_protector_protect(ep->protector, message_bytes,
282
- &processed_message_size, cur,
283
- &protected_buffer_size_to_send);
284
- gpr_mu_unlock(&ep->protector_mu);
285
- if (result != TSI_OK) {
286
- gpr_log(GPR_ERROR, "Encryption error: %s",
287
- tsi_result_to_string(result));
288
- break;
289
- }
290
- message_bytes += processed_message_size;
291
- message_size -= processed_message_size;
292
- cur += protected_buffer_size_to_send;
293
-
294
- if (cur == end) {
295
- flush_write_staging_buffer(ep, &cur, &end);
282
+ if (ep->zero_copy_protector != NULL) {
283
+ // Use zero-copy grpc protector to protect.
284
+ result = tsi_zero_copy_grpc_protector_protect(
285
+ exec_ctx, ep->zero_copy_protector, slices, &ep->output_buffer);
286
+ } else {
287
+ // Use frame protector to protect.
288
+ for (i = 0; i < slices->count; i++) {
289
+ grpc_slice plain = slices->slices[i];
290
+ uint8_t *message_bytes = GRPC_SLICE_START_PTR(plain);
291
+ size_t message_size = GRPC_SLICE_LENGTH(plain);
292
+ while (message_size > 0) {
293
+ size_t protected_buffer_size_to_send = (size_t)(end - cur);
294
+ size_t processed_message_size = message_size;
295
+ gpr_mu_lock(&ep->protector_mu);
296
+ result = tsi_frame_protector_protect(ep->protector, message_bytes,
297
+ &processed_message_size, cur,
298
+ &protected_buffer_size_to_send);
299
+ gpr_mu_unlock(&ep->protector_mu);
300
+ if (result != TSI_OK) {
301
+ gpr_log(GPR_ERROR, "Encryption error: %s",
302
+ tsi_result_to_string(result));
303
+ break;
304
+ }
305
+ message_bytes += processed_message_size;
306
+ message_size -= processed_message_size;
307
+ cur += protected_buffer_size_to_send;
308
+
309
+ if (cur == end) {
310
+ flush_write_staging_buffer(ep, &cur, &end);
311
+ }
296
312
  }
297
- }
298
- if (result != TSI_OK) break;
299
- }
300
- if (result == TSI_OK) {
301
- size_t still_pending_size;
302
- do {
303
- size_t protected_buffer_size_to_send = (size_t)(end - cur);
304
- gpr_mu_lock(&ep->protector_mu);
305
- result = tsi_frame_protector_protect_flush(ep->protector, cur,
306
- &protected_buffer_size_to_send,
307
- &still_pending_size);
308
- gpr_mu_unlock(&ep->protector_mu);
309
313
  if (result != TSI_OK) break;
310
- cur += protected_buffer_size_to_send;
311
- if (cur == end) {
312
- flush_write_staging_buffer(ep, &cur, &end);
314
+ }
315
+ if (result == TSI_OK) {
316
+ size_t still_pending_size;
317
+ do {
318
+ size_t protected_buffer_size_to_send = (size_t)(end - cur);
319
+ gpr_mu_lock(&ep->protector_mu);
320
+ result = tsi_frame_protector_protect_flush(
321
+ ep->protector, cur, &protected_buffer_size_to_send,
322
+ &still_pending_size);
323
+ gpr_mu_unlock(&ep->protector_mu);
324
+ if (result != TSI_OK) break;
325
+ cur += protected_buffer_size_to_send;
326
+ if (cur == end) {
327
+ flush_write_staging_buffer(ep, &cur, &end);
328
+ }
329
+ } while (still_pending_size > 0);
330
+ if (cur != GRPC_SLICE_START_PTR(ep->write_staging_buffer)) {
331
+ grpc_slice_buffer_add(
332
+ &ep->output_buffer,
333
+ grpc_slice_split_head(
334
+ &ep->write_staging_buffer,
335
+ (size_t)(cur -
336
+ GRPC_SLICE_START_PTR(ep->write_staging_buffer))));
313
337
  }
314
- } while (still_pending_size > 0);
315
- if (cur != GRPC_SLICE_START_PTR(ep->write_staging_buffer)) {
316
- grpc_slice_buffer_add(
317
- &ep->output_buffer,
318
- grpc_slice_split_head(
319
- &ep->write_staging_buffer,
320
- (size_t)(cur - GRPC_SLICE_START_PTR(ep->write_staging_buffer))));
321
338
  }
322
339
  }
323
340
 
@@ -389,13 +406,16 @@ static const grpc_endpoint_vtable vtable = {endpoint_read,
389
406
  endpoint_get_fd};
390
407
 
391
408
  grpc_endpoint *grpc_secure_endpoint_create(
392
- struct tsi_frame_protector *protector, grpc_endpoint *transport,
393
- grpc_slice *leftover_slices, size_t leftover_nslices) {
409
+ struct tsi_frame_protector *protector,
410
+ struct tsi_zero_copy_grpc_protector *zero_copy_protector,
411
+ grpc_endpoint *transport, grpc_slice *leftover_slices,
412
+ size_t leftover_nslices) {
394
413
  size_t i;
395
414
  secure_endpoint *ep = (secure_endpoint *)gpr_malloc(sizeof(secure_endpoint));
396
415
  ep->base.vtable = &vtable;
397
416
  ep->wrapped_ep = transport;
398
417
  ep->protector = protector;
418
+ ep->zero_copy_protector = zero_copy_protector;
399
419
  grpc_slice_buffer_init(&ep->leftover_bytes);
400
420
  for (i = 0; i < leftover_nslices; i++) {
401
421
  grpc_slice_buffer_add(&ep->leftover_bytes,
@@ -23,12 +23,17 @@
23
23
  #include "src/core/lib/iomgr/endpoint.h"
24
24
 
25
25
  struct tsi_frame_protector;
26
+ struct tsi_zero_copy_grpc_protector;
26
27
 
27
28
  extern grpc_tracer_flag grpc_trace_secure_endpoint;
28
29
 
29
- /* Takes ownership of protector and to_wrap, and refs leftover_slices. */
30
+ /* Takes ownership of protector, zero_copy_protector, and to_wrap, and refs
31
+ * leftover_slices. If zero_copy_protector is not NULL, protector will never be
32
+ * used. */
30
33
  grpc_endpoint *grpc_secure_endpoint_create(
31
- struct tsi_frame_protector *protector, grpc_endpoint *to_wrap,
32
- grpc_slice *leftover_slices, size_t leftover_nslices);
34
+ struct tsi_frame_protector *protector,
35
+ struct tsi_zero_copy_grpc_protector *zero_copy_protector,
36
+ grpc_endpoint *to_wrap, grpc_slice *leftover_slices,
37
+ size_t leftover_nslices);
33
38
 
34
39
  #endif /* GRPC_CORE_LIB_SECURITY_TRANSPORT_SECURE_ENDPOINT_H */
@@ -455,14 +455,14 @@ grpc_server_security_connector *grpc_fake_server_security_connector_create(
455
455
 
456
456
  typedef struct {
457
457
  grpc_channel_security_connector base;
458
- tsi_ssl_client_handshaker_factory *handshaker_factory;
458
+ tsi_ssl_client_handshaker_factory *client_handshaker_factory;
459
459
  char *target_name;
460
460
  char *overridden_target_name;
461
461
  } grpc_ssl_channel_security_connector;
462
462
 
463
463
  typedef struct {
464
464
  grpc_server_security_connector base;
465
- tsi_ssl_server_handshaker_factory *handshaker_factory;
465
+ tsi_ssl_server_handshaker_factory *server_handshaker_factory;
466
466
  } grpc_ssl_server_security_connector;
467
467
 
468
468
  static void ssl_channel_destroy(grpc_exec_ctx *exec_ctx,
@@ -470,9 +470,8 @@ static void ssl_channel_destroy(grpc_exec_ctx *exec_ctx,
470
470
  grpc_ssl_channel_security_connector *c =
471
471
  (grpc_ssl_channel_security_connector *)sc;
472
472
  grpc_call_credentials_unref(exec_ctx, c->base.request_metadata_creds);
473
- if (c->handshaker_factory != NULL) {
474
- tsi_ssl_client_handshaker_factory_destroy(c->handshaker_factory);
475
- }
473
+ tsi_ssl_client_handshaker_factory_unref(c->client_handshaker_factory);
474
+ c->client_handshaker_factory = NULL;
476
475
  if (c->target_name != NULL) gpr_free(c->target_name);
477
476
  if (c->overridden_target_name != NULL) gpr_free(c->overridden_target_name);
478
477
  gpr_free(sc);
@@ -482,9 +481,8 @@ static void ssl_server_destroy(grpc_exec_ctx *exec_ctx,
482
481
  grpc_security_connector *sc) {
483
482
  grpc_ssl_server_security_connector *c =
484
483
  (grpc_ssl_server_security_connector *)sc;
485
- if (c->handshaker_factory != NULL) {
486
- tsi_ssl_server_handshaker_factory_destroy(c->handshaker_factory);
487
- }
484
+ tsi_ssl_server_handshaker_factory_unref(c->server_handshaker_factory);
485
+ c->server_handshaker_factory = NULL;
488
486
  gpr_free(sc);
489
487
  }
490
488
 
@@ -496,7 +494,7 @@ static void ssl_channel_add_handshakers(grpc_exec_ctx *exec_ctx,
496
494
  // Instantiate TSI handshaker.
497
495
  tsi_handshaker *tsi_hs = NULL;
498
496
  tsi_result result = tsi_ssl_client_handshaker_factory_create_handshaker(
499
- c->handshaker_factory,
497
+ c->client_handshaker_factory,
500
498
  c->overridden_target_name != NULL ? c->overridden_target_name
501
499
  : c->target_name,
502
500
  &tsi_hs);
@@ -521,7 +519,7 @@ static void ssl_server_add_handshakers(grpc_exec_ctx *exec_ctx,
521
519
  // Instantiate TSI handshaker.
522
520
  tsi_handshaker *tsi_hs = NULL;
523
521
  tsi_result result = tsi_ssl_server_handshaker_factory_create_handshaker(
524
- c->handshaker_factory, &tsi_hs);
522
+ c->server_handshaker_factory, &tsi_hs);
525
523
  if (result != TSI_OK) {
526
524
  gpr_log(GPR_ERROR, "Handshaker creation failed with error %s.",
527
525
  tsi_result_to_string(result));
@@ -852,7 +850,7 @@ grpc_security_status grpc_ssl_channel_security_connector_create(
852
850
  result = tsi_create_ssl_client_handshaker_factory(
853
851
  has_key_cert_pair ? &config->pem_key_cert_pair : NULL, pem_root_certs,
854
852
  ssl_cipher_suites(), alpn_protocol_strings, (uint16_t)num_alpn_protocols,
855
- &c->handshaker_factory);
853
+ &c->client_handshaker_factory);
856
854
  if (result != TSI_OK) {
857
855
  gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.",
858
856
  tsi_result_to_string(result));
@@ -897,7 +895,7 @@ grpc_security_status grpc_ssl_server_security_connector_create(
897
895
  config->pem_root_certs, get_tsi_client_certificate_request_type(
898
896
  config->client_certificate_request),
899
897
  ssl_cipher_suites(), alpn_protocol_strings, (uint16_t)num_alpn_protocols,
900
- &c->handshaker_factory);
898
+ &c->server_handshaker_factory);
901
899
  if (result != TSI_OK) {
902
900
  gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.",
903
901
  tsi_result_to_string(result));
@@ -32,6 +32,7 @@
32
32
  #include "src/core/lib/security/transport/secure_endpoint.h"
33
33
  #include "src/core/lib/security/transport/tsi_error.h"
34
34
  #include "src/core/lib/slice/slice_internal.h"
35
+ #include "src/core/tsi/transport_security_grpc.h"
35
36
 
36
37
  #define GRPC_INITIAL_HANDSHAKE_BUFFER_SIZE 256
37
38
 
@@ -127,24 +128,36 @@ static void security_handshake_failed_locked(grpc_exec_ctx *exec_ctx,
127
128
  GRPC_CLOSURE_SCHED(exec_ctx, h->on_handshake_done, error);
128
129
  }
129
130
 
130
- static void on_peer_checked(grpc_exec_ctx *exec_ctx, void *arg,
131
- grpc_error *error) {
132
- security_handshaker *h = arg;
133
- gpr_mu_lock(&h->mu);
131
+ static void on_peer_checked_inner(grpc_exec_ctx *exec_ctx,
132
+ security_handshaker *h, grpc_error *error) {
134
133
  if (error != GRPC_ERROR_NONE || h->shutdown) {
135
134
  security_handshake_failed_locked(exec_ctx, h, GRPC_ERROR_REF(error));
136
- goto done;
135
+ return;
137
136
  }
138
- // Create frame protector.
139
- tsi_frame_protector *protector;
140
- tsi_result result = tsi_handshaker_result_create_frame_protector(
141
- h->handshaker_result, NULL, &protector);
142
- if (result != TSI_OK) {
137
+ // Create zero-copy frame protector, if implemented.
138
+ tsi_zero_copy_grpc_protector *zero_copy_protector = NULL;
139
+ tsi_result result = tsi_handshaker_result_create_zero_copy_grpc_protector(
140
+ exec_ctx, h->handshaker_result, NULL, &zero_copy_protector);
141
+ if (result != TSI_OK && result != TSI_UNIMPLEMENTED) {
143
142
  error = grpc_set_tsi_error_result(
144
- GRPC_ERROR_CREATE_FROM_STATIC_STRING("Frame protector creation failed"),
143
+ GRPC_ERROR_CREATE_FROM_STATIC_STRING(
144
+ "Zero-copy frame protector creation failed"),
145
145
  result);
146
146
  security_handshake_failed_locked(exec_ctx, h, error);
147
- goto done;
147
+ return;
148
+ }
149
+ // Create frame protector if zero-copy frame protector is NULL.
150
+ tsi_frame_protector *protector = NULL;
151
+ if (zero_copy_protector == NULL) {
152
+ result = tsi_handshaker_result_create_frame_protector(h->handshaker_result,
153
+ NULL, &protector);
154
+ if (result != TSI_OK) {
155
+ error = grpc_set_tsi_error_result(GRPC_ERROR_CREATE_FROM_STATIC_STRING(
156
+ "Frame protector creation failed"),
157
+ result);
158
+ security_handshake_failed_locked(exec_ctx, h, error);
159
+ return;
160
+ }
148
161
  }
149
162
  // Get unused bytes.
150
163
  const unsigned char *unused_bytes = NULL;
@@ -155,12 +168,12 @@ static void on_peer_checked(grpc_exec_ctx *exec_ctx, void *arg,
155
168
  if (unused_bytes_size > 0) {
156
169
  grpc_slice slice =
157
170
  grpc_slice_from_copied_buffer((char *)unused_bytes, unused_bytes_size);
158
- h->args->endpoint =
159
- grpc_secure_endpoint_create(protector, h->args->endpoint, &slice, 1);
171
+ h->args->endpoint = grpc_secure_endpoint_create(
172
+ protector, zero_copy_protector, h->args->endpoint, &slice, 1);
160
173
  grpc_slice_unref_internal(exec_ctx, slice);
161
174
  } else {
162
- h->args->endpoint =
163
- grpc_secure_endpoint_create(protector, h->args->endpoint, NULL, 0);
175
+ h->args->endpoint = grpc_secure_endpoint_create(
176
+ protector, zero_copy_protector, h->args->endpoint, NULL, 0);
164
177
  }
165
178
  tsi_handshaker_result_destroy(h->handshaker_result);
166
179
  h->handshaker_result = NULL;
@@ -177,7 +190,13 @@ static void on_peer_checked(grpc_exec_ctx *exec_ctx, void *arg,
177
190
  // Set shutdown to true so that subsequent calls to
178
191
  // security_handshaker_shutdown() do nothing.
179
192
  h->shutdown = true;
180
- done:
193
+ }
194
+
195
+ static void on_peer_checked(grpc_exec_ctx *exec_ctx, void *arg,
196
+ grpc_error *error) {
197
+ security_handshaker *h = (security_handshaker *)arg;
198
+ gpr_mu_lock(&h->mu);
199
+ on_peer_checked_inner(exec_ctx, h, error);
181
200
  gpr_mu_unlock(&h->mu);
182
201
  security_handshaker_unref(exec_ctx, h);
183
202
  }
@@ -239,7 +258,7 @@ static grpc_error *on_handshake_next_done_locked(
239
258
  static void on_handshake_next_done_grpc_wrapper(
240
259
  tsi_result result, void *user_data, const unsigned char *bytes_to_send,
241
260
  size_t bytes_to_send_size, tsi_handshaker_result *handshaker_result) {
242
- security_handshaker *h = user_data;
261
+ security_handshaker *h = (security_handshaker *)user_data;
243
262
  // This callback will be invoked by TSI in a non-grpc thread, so it's
244
263
  // safe to create our own exec_ctx here.
245
264
  grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
@@ -281,7 +300,7 @@ static grpc_error *do_handshaker_next_locked(
281
300
 
282
301
  static void on_handshake_data_received_from_peer(grpc_exec_ctx *exec_ctx,
283
302
  void *arg, grpc_error *error) {
284
- security_handshaker *h = arg;
303
+ security_handshaker *h = (security_handshaker *)arg;
285
304
  gpr_mu_lock(&h->mu);
286
305
  if (error != GRPC_ERROR_NONE || h->shutdown) {
287
306
  security_handshake_failed_locked(
@@ -298,7 +317,8 @@ static void on_handshake_data_received_from_peer(grpc_exec_ctx *exec_ctx,
298
317
  bytes_received_size += GRPC_SLICE_LENGTH(h->args->read_buffer->slices[i]);
299
318
  }
300
319
  if (bytes_received_size > h->handshake_buffer_size) {
301
- h->handshake_buffer = gpr_realloc(h->handshake_buffer, bytes_received_size);
320
+ h->handshake_buffer =
321
+ (uint8_t *)gpr_realloc(h->handshake_buffer, bytes_received_size);
302
322
  h->handshake_buffer_size = bytes_received_size;
303
323
  }
304
324
  size_t offset = 0;
@@ -323,7 +343,7 @@ static void on_handshake_data_received_from_peer(grpc_exec_ctx *exec_ctx,
323
343
 
324
344
  static void on_handshake_data_sent_to_peer(grpc_exec_ctx *exec_ctx, void *arg,
325
345
  grpc_error *error) {
326
- security_handshaker *h = arg;
346
+ security_handshaker *h = (security_handshaker *)arg;
327
347
  gpr_mu_lock(&h->mu);
328
348
  if (error != GRPC_ERROR_NONE || h->shutdown) {
329
349
  security_handshake_failed_locked(
@@ -400,14 +420,15 @@ static const grpc_handshaker_vtable security_handshaker_vtable = {
400
420
  static grpc_handshaker *security_handshaker_create(
401
421
  grpc_exec_ctx *exec_ctx, tsi_handshaker *handshaker,
402
422
  grpc_security_connector *connector) {
403
- security_handshaker *h = gpr_zalloc(sizeof(security_handshaker));
423
+ security_handshaker *h =
424
+ (security_handshaker *)gpr_zalloc(sizeof(security_handshaker));
404
425
  grpc_handshaker_init(&security_handshaker_vtable, &h->base);
405
426
  h->handshaker = handshaker;
406
427
  h->connector = GRPC_SECURITY_CONNECTOR_REF(connector, "handshake");
407
428
  gpr_mu_init(&h->mu);
408
429
  gpr_ref_init(&h->refs, 1);
409
430
  h->handshake_buffer_size = GRPC_INITIAL_HANDSHAKE_BUFFER_SIZE;
410
- h->handshake_buffer = gpr_malloc(h->handshake_buffer_size);
431
+ h->handshake_buffer = (uint8_t *)gpr_malloc(h->handshake_buffer_size);
411
432
  GRPC_CLOSURE_INIT(&h->on_handshake_data_sent_to_peer,
412
433
  on_handshake_data_sent_to_peer, h,
413
434
  grpc_schedule_on_exec_ctx);
@@ -450,7 +471,7 @@ static const grpc_handshaker_vtable fail_handshaker_vtable = {
450
471
  fail_handshaker_do_handshake};
451
472
 
452
473
  static grpc_handshaker *fail_handshaker_create() {
453
- grpc_handshaker *h = gpr_malloc(sizeof(*h));
474
+ grpc_handshaker *h = (grpc_handshaker *)gpr_malloc(sizeof(*h));
454
475
  grpc_handshaker_init(&fail_handshaker_vtable, h);
455
476
  return h;
456
477
  }