• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2017 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include "test/core/end2end/end2end_tests.h"
20 
21 #include <string.h>
22 
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/sync.h>
26 #include <grpc/support/time.h>
27 
28 #include "src/core/lib/gpr/useful.h"
29 #include "test/core/end2end/cq_verifier.h"
30 
31 #define MAX_PING_STRIKES 2
32 
tag(intptr_t t)33 static void* tag(intptr_t t) { return (void*)t; }
34 
drain_cq(grpc_completion_queue * cq)35 static void drain_cq(grpc_completion_queue* cq) {
36   grpc_event ev;
37   do {
38     ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5),
39                                     nullptr);
40   } while (ev.type != GRPC_QUEUE_SHUTDOWN);
41 }
42 
shutdown_server(grpc_end2end_test_fixture * f)43 static void shutdown_server(grpc_end2end_test_fixture* f) {
44   if (!f->server) return;
45   grpc_server_destroy(f->server);
46   f->server = nullptr;
47 }
48 
shutdown_client(grpc_end2end_test_fixture * f)49 static void shutdown_client(grpc_end2end_test_fixture* f) {
50   if (!f->client) return;
51   grpc_channel_destroy(f->client);
52   f->client = nullptr;
53 }
54 
end_test(grpc_end2end_test_fixture * f)55 static void end_test(grpc_end2end_test_fixture* f) {
56   shutdown_server(f);
57   shutdown_client(f);
58 
59   grpc_completion_queue_shutdown(f->cq);
60   drain_cq(f->cq);
61   grpc_completion_queue_destroy(f->cq);
62   grpc_completion_queue_destroy(f->shutdown_cq);
63 }
64 
65 // Send more pings than server allows to trigger server's GOAWAY.
test_bad_ping(grpc_end2end_test_config config)66 static void test_bad_ping(grpc_end2end_test_config config) {
67   grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr);
68   cq_verifier* cqv = cq_verifier_create(f.cq);
69   grpc_arg client_a[3];
70   client_a[0].type = GRPC_ARG_INTEGER;
71   client_a[0].key =
72       const_cast<char*>(GRPC_ARG_HTTP2_MIN_SENT_PING_INTERVAL_WITHOUT_DATA_MS);
73   client_a[0].value.integer = 10;
74   client_a[1].type = GRPC_ARG_INTEGER;
75   client_a[1].key = const_cast<char*>(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA);
76   client_a[1].value.integer = 0;
77   client_a[2].type = GRPC_ARG_INTEGER;
78   client_a[2].key = const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE);
79   client_a[2].value.integer = 0;
80   grpc_arg server_a[3];
81   server_a[0].type = GRPC_ARG_INTEGER;
82   server_a[0].key =
83       const_cast<char*>(GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS);
84   server_a[0].value.integer = 300000 /* 5 minutes */;
85   server_a[1].type = GRPC_ARG_INTEGER;
86   server_a[1].key = const_cast<char*>(GRPC_ARG_HTTP2_MAX_PING_STRIKES);
87   server_a[1].value.integer = MAX_PING_STRIKES;
88   server_a[2].type = GRPC_ARG_INTEGER;
89   server_a[2].key = const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE);
90   server_a[2].value.integer = 0;
91   grpc_channel_args client_args = {GPR_ARRAY_SIZE(client_a), client_a};
92   grpc_channel_args server_args = {GPR_ARRAY_SIZE(server_a), server_a};
93 
94   config.init_client(&f, &client_args);
95   config.init_server(&f, &server_args);
96 
97   grpc_call* c;
98   grpc_call* s;
99   gpr_timespec deadline = grpc_timeout_seconds_to_deadline(10);
100   grpc_op ops[6];
101   grpc_op* op;
102   grpc_metadata_array initial_metadata_recv;
103   grpc_metadata_array trailing_metadata_recv;
104   grpc_metadata_array request_metadata_recv;
105   grpc_call_details call_details;
106   grpc_status_code status;
107   grpc_call_error error;
108   grpc_slice details;
109   int was_cancelled = 2;
110 
111   c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
112                                grpc_slice_from_static_string("/foo"), nullptr,
113                                deadline, nullptr);
114   GPR_ASSERT(c);
115 
116   grpc_metadata_array_init(&initial_metadata_recv);
117   grpc_metadata_array_init(&trailing_metadata_recv);
118   grpc_metadata_array_init(&request_metadata_recv);
119   grpc_call_details_init(&call_details);
120 
121   memset(ops, 0, sizeof(ops));
122   op = ops;
123   op->op = GRPC_OP_SEND_INITIAL_METADATA;
124   op->data.send_initial_metadata.count = 0;
125   op->data.send_initial_metadata.metadata = nullptr;
126   op->flags = 0;
127   op->reserved = nullptr;
128   op++;
129   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
130   op->flags = 0;
131   op->reserved = nullptr;
132   op++;
133   op->op = GRPC_OP_RECV_INITIAL_METADATA;
134   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
135   op->flags = 0;
136   op->reserved = nullptr;
137   op++;
138   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
139   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
140   op->data.recv_status_on_client.status = &status;
141   op->data.recv_status_on_client.status_details = &details;
142   op->flags = 0;
143   op->reserved = nullptr;
144   op++;
145   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
146                                 nullptr);
147   GPR_ASSERT(GRPC_CALL_OK == error);
148 
149   error =
150       grpc_server_request_call(f.server, &s, &call_details,
151                                &request_metadata_recv, f.cq, f.cq, tag(101));
152   GPR_ASSERT(GRPC_CALL_OK == error);
153   CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
154   cq_verify(cqv);
155 
156   // Send too many pings to the server to trigger the punishment:
157   // The first ping will let server mark its last_recv time. Afterwards, each
158   // ping will trigger a ping strike, and we need at least MAX_PING_STRIKES
159   // strikes to trigger the punishment. So (MAX_PING_STRIKES + 2) pings are
160   // needed here.
161   int i;
162   for (i = 1; i <= MAX_PING_STRIKES + 2; i++) {
163     grpc_channel_ping(f.client, f.cq, tag(200 + i), nullptr);
164     CQ_EXPECT_COMPLETION(cqv, tag(200 + i), 1);
165     if (i == MAX_PING_STRIKES + 2) {
166       CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
167     }
168     cq_verify(cqv);
169   }
170 
171   memset(ops, 0, sizeof(ops));
172   op = ops;
173   op->op = GRPC_OP_SEND_INITIAL_METADATA;
174   op->data.send_initial_metadata.count = 0;
175   op->flags = 0;
176   op->reserved = nullptr;
177   op++;
178   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
179   op->data.send_status_from_server.trailing_metadata_count = 0;
180   op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
181   grpc_slice status_details = grpc_slice_from_static_string("xyz");
182   op->data.send_status_from_server.status_details = &status_details;
183   op->flags = 0;
184   op->reserved = nullptr;
185   op++;
186   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
187   op->data.recv_close_on_server.cancelled = &was_cancelled;
188   op->flags = 0;
189   op->reserved = nullptr;
190   op++;
191   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
192                                 nullptr);
193   GPR_ASSERT(GRPC_CALL_OK == error);
194 
195   CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
196   cq_verify(cqv);
197 
198   grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead));
199   CQ_EXPECT_COMPLETION(cqv, tag(0xdead), 1);
200   cq_verify(cqv);
201 
202   grpc_call_unref(s);
203 
204   // The connection should be closed immediately after the misbehaved pings,
205   // the in-progress RPC should fail.
206   GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE);
207   GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
208   GPR_ASSERT(was_cancelled == 1);
209 
210   grpc_slice_unref(details);
211   grpc_metadata_array_destroy(&initial_metadata_recv);
212   grpc_metadata_array_destroy(&trailing_metadata_recv);
213   grpc_metadata_array_destroy(&request_metadata_recv);
214   grpc_call_details_destroy(&call_details);
215   grpc_call_unref(c);
216   cq_verifier_destroy(cqv);
217   end_test(&f);
218   config.tear_down_data(&f);
219 }
220 
221 // Try sending more pings than server allows, but server should be fine because
222 // max_pings_without_data should limit pings sent out on wire.
test_pings_without_data(grpc_end2end_test_config config)223 static void test_pings_without_data(grpc_end2end_test_config config) {
224   grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr);
225   cq_verifier* cqv = cq_verifier_create(f.cq);
226   grpc_arg client_a[3];
227   client_a[0].type = GRPC_ARG_INTEGER;
228   client_a[0].key =
229       const_cast<char*>(GRPC_ARG_HTTP2_MIN_SENT_PING_INTERVAL_WITHOUT_DATA_MS);
230   client_a[0].value.integer = 10;
231   // Only allow MAX_PING_STRIKES pings without data (DATA/HEADERS/WINDOW_UPDATE)
232   // so that the transport will throttle the excess pings.
233   client_a[1].type = GRPC_ARG_INTEGER;
234   client_a[1].key = const_cast<char*>(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA);
235   client_a[1].value.integer = MAX_PING_STRIKES;
236   client_a[2].type = GRPC_ARG_INTEGER;
237   client_a[2].key = const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE);
238   client_a[2].value.integer = 0;
239   grpc_arg server_a[3];
240   server_a[0].type = GRPC_ARG_INTEGER;
241   server_a[0].key =
242       const_cast<char*>(GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS);
243   server_a[0].value.integer = 300000 /* 5 minutes */;
244   server_a[1].type = GRPC_ARG_INTEGER;
245   server_a[1].key = const_cast<char*>(GRPC_ARG_HTTP2_MAX_PING_STRIKES);
246   server_a[1].value.integer = MAX_PING_STRIKES;
247   server_a[2].type = GRPC_ARG_INTEGER;
248   server_a[2].key = const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE);
249   server_a[2].value.integer = 0;
250   grpc_channel_args client_args = {GPR_ARRAY_SIZE(client_a), client_a};
251   grpc_channel_args server_args = {GPR_ARRAY_SIZE(server_a), server_a};
252 
253   config.init_client(&f, &client_args);
254   config.init_server(&f, &server_args);
255 
256   grpc_call* c;
257   grpc_call* s;
258   gpr_timespec deadline = grpc_timeout_seconds_to_deadline(10);
259   grpc_op ops[6];
260   grpc_op* op;
261   grpc_metadata_array initial_metadata_recv;
262   grpc_metadata_array trailing_metadata_recv;
263   grpc_metadata_array request_metadata_recv;
264   grpc_call_details call_details;
265   grpc_status_code status;
266   grpc_call_error error;
267   grpc_slice details;
268   int was_cancelled = 2;
269 
270   c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
271                                grpc_slice_from_static_string("/foo"), nullptr,
272                                deadline, nullptr);
273   GPR_ASSERT(c);
274 
275   grpc_metadata_array_init(&initial_metadata_recv);
276   grpc_metadata_array_init(&trailing_metadata_recv);
277   grpc_metadata_array_init(&request_metadata_recv);
278   grpc_call_details_init(&call_details);
279 
280   memset(ops, 0, sizeof(ops));
281   op = ops;
282   op->op = GRPC_OP_SEND_INITIAL_METADATA;
283   op->data.send_initial_metadata.count = 0;
284   op->data.send_initial_metadata.metadata = nullptr;
285   op->flags = 0;
286   op->reserved = nullptr;
287   op++;
288   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
289   op->flags = 0;
290   op->reserved = nullptr;
291   op++;
292   op->op = GRPC_OP_RECV_INITIAL_METADATA;
293   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
294   op->flags = 0;
295   op->reserved = nullptr;
296   op++;
297   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
298   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
299   op->data.recv_status_on_client.status = &status;
300   op->data.recv_status_on_client.status_details = &details;
301   op->flags = 0;
302   op->reserved = nullptr;
303   op++;
304   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
305                                 nullptr);
306   GPR_ASSERT(GRPC_CALL_OK == error);
307 
308   error =
309       grpc_server_request_call(f.server, &s, &call_details,
310                                &request_metadata_recv, f.cq, f.cq, tag(101));
311   GPR_ASSERT(GRPC_CALL_OK == error);
312   CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
313   cq_verify(cqv);
314 
315   // Send too many pings to the server similar to the prevous test case.
316   // However, since we set the MAX_PINGS_WITHOUT_DATA at the client side, only
317   // MAX_PING_STRIKES will actually be sent and the rpc will still succeed.
318   int i;
319   for (i = 1; i <= MAX_PING_STRIKES + 2; i++) {
320     grpc_channel_ping(f.client, f.cq, tag(200 + i), nullptr);
321     if (i <= MAX_PING_STRIKES) {
322       CQ_EXPECT_COMPLETION(cqv, tag(200 + i), 1);
323     }
324     cq_verify(cqv);
325   }
326 
327   memset(ops, 0, sizeof(ops));
328   op = ops;
329   op->op = GRPC_OP_SEND_INITIAL_METADATA;
330   op->data.send_initial_metadata.count = 0;
331   op->flags = 0;
332   op->reserved = nullptr;
333   op++;
334   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
335   op->data.send_status_from_server.trailing_metadata_count = 0;
336   op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
337   grpc_slice status_details = grpc_slice_from_static_string("xyz");
338   op->data.send_status_from_server.status_details = &status_details;
339   op->flags = 0;
340   op->reserved = nullptr;
341   op++;
342   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
343   op->data.recv_close_on_server.cancelled = &was_cancelled;
344   op->flags = 0;
345   op->reserved = nullptr;
346   op++;
347   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
348                                 nullptr);
349   GPR_ASSERT(GRPC_CALL_OK == error);
350 
351   CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
352   // Client call should return.
353   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
354   cq_verify(cqv);
355 
356   grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead));
357   CQ_EXPECT_COMPLETION(cqv, tag(0xdead), 1);
358 
359   // Also expect the previously blocked pings to complete with an error
360   CQ_EXPECT_COMPLETION(cqv, tag(200 + MAX_PING_STRIKES + 1), 0);
361   CQ_EXPECT_COMPLETION(cqv, tag(200 + MAX_PING_STRIKES + 2), 0);
362 
363   cq_verify(cqv);
364 
365   grpc_call_unref(s);
366 
367   // The rpc should be successful.
368   GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
369   GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
370 
371   grpc_slice_unref(details);
372   grpc_metadata_array_destroy(&initial_metadata_recv);
373   grpc_metadata_array_destroy(&trailing_metadata_recv);
374   grpc_metadata_array_destroy(&request_metadata_recv);
375   grpc_call_details_destroy(&call_details);
376   grpc_call_unref(c);
377   cq_verifier_destroy(cqv);
378   end_test(&f);
379   config.tear_down_data(&f);
380 }
381 
bad_ping(grpc_end2end_test_config config)382 void bad_ping(grpc_end2end_test_config config) {
383   GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION);
384   test_bad_ping(config);
385   test_pings_without_data(config);
386 }
387 
bad_ping_pre_init(void)388 void bad_ping_pre_init(void) {}
389