-
Notifications
You must be signed in to change notification settings - Fork 639
/
Copy pathquic.c
2953 lines (2595 loc) · 88 KB
/
quic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2019 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <sys/socket.h>
#include <vnet/session/application.h>
#include <vnet/session/transport.h>
#include <vnet/session/session.h>
#include <vlib/unix/plugin.h>
#include <vpp/app/version.h>
#include <vppinfra/lock.h>
#include <quic/quic.h>
#include <quic/certs.h>
#include <quic/error.h>
#include <quicly/constants.h>
#include <quicly/defaults.h>
#include <picotls.h>
#include <quic/quic_crypto.h>
extern quicly_crypto_engine_t quic_crypto_engine;
static char *quic_error_strings[] = {
#define quic_error(n,s) s,
#include <quic/quic_error.def>
#undef quic_error
};
#define DEFAULT_MAX_PACKETS_PER_KEY 16777216
quic_main_t quic_main;
static void quic_update_timer (quic_ctx_t * ctx);
static void quic_check_quic_session_connected (quic_ctx_t * ctx);
static int quic_reset_connection (u64 udp_session_handle,
quic_rx_packet_ctx_t * pctx);
static void quic_proto_on_close (u32 ctx_index, u32 thread_index);
static quicly_stream_open_t on_stream_open;
static quicly_closed_by_peer_t on_closed_by_peer;
static quicly_now_t quicly_vpp_now_cb;
/* Crypto contexts */
static inline void
quic_crypto_context_make_key_from_ctx (clib_bihash_kv_24_8_t * kv,
quic_ctx_t * ctx)
{
application_t *app = application_get (ctx->parent_app_id);
kv->key[0] = ((u64) ctx->ckpair_index) << 32 | (u64) ctx->crypto_engine;
kv->key[1] = app->sm_properties.rx_fifo_size - 1;
kv->key[2] = app->sm_properties.tx_fifo_size - 1;
}
static inline void
quic_crypto_context_make_key_from_crctx (clib_bihash_kv_24_8_t * kv,
crypto_context_t * crctx)
{
quic_crypto_context_data_t *data =
(quic_crypto_context_data_t *) crctx->data;
kv->key[0] = ((u64) crctx->ckpair_index) << 32 | (u64) crctx->crypto_engine;
kv->key[1] = data->quicly_ctx.transport_params.max_stream_data.bidi_local;
kv->key[2] = data->quicly_ctx.transport_params.max_stream_data.bidi_remote;
}
static void
quic_crypto_context_free_if_needed (crypto_context_t * crctx, u8 thread_index)
{
quic_main_t *qm = &quic_main;
clib_bihash_kv_24_8_t kv;
if (crctx->n_subscribers)
return;
quic_crypto_context_make_key_from_crctx (&kv, crctx);
clib_bihash_add_del_24_8 (&qm->wrk_ctx[thread_index].crypto_context_hash,
&kv, 0 /* is_add */ );
clib_mem_free (crctx->data);
pool_put (qm->wrk_ctx[thread_index].crypto_ctx_pool, crctx);
}
static quicly_datagram_t *
quic_alloc_packet (quicly_packet_allocator_t * self, size_t payloadsize)
{
quicly_datagram_t *packet;
if ((packet =
clib_mem_alloc (sizeof (*packet) + payloadsize +
sizeof (quic_encrypt_cb_ctx))) == NULL)
return NULL;
packet->data.base =
(uint8_t *) packet + sizeof (*packet) + sizeof (quic_encrypt_cb_ctx);
quic_encrypt_cb_ctx *encrypt_cb_ctx =
(quic_encrypt_cb_ctx *) ((uint8_t *) packet + sizeof (*packet));
clib_memset (encrypt_cb_ctx, 0, sizeof (*encrypt_cb_ctx));
return packet;
}
static void
quic_free_packet (quicly_packet_allocator_t * self,
quicly_datagram_t * packet)
{
clib_mem_free (packet);
}
quicly_packet_allocator_t quic_packet_allocator =
{ quic_alloc_packet, quic_free_packet };
static int
quic_app_cert_key_pair_delete_callback (app_cert_key_pair_t * ckpair)
{
quic_main_t *qm = &quic_main;
crypto_context_t *crctx;
clib_bihash_kv_24_8_t kv;
vlib_thread_main_t *vtm = vlib_get_thread_main ();
int num_threads = 1 /* main thread */ + vtm->n_threads;
int i;
for (i = 0; i < num_threads; i++)
{
/* *INDENT-OFF* */
pool_foreach (crctx, qm->wrk_ctx[i].crypto_ctx_pool, ({
if (crctx->ckpair_index == ckpair->cert_key_index)
{
quic_crypto_context_make_key_from_crctx (&kv, crctx);
clib_bihash_add_del_24_8 (&qm->wrk_ctx[i].crypto_context_hash, &kv, 0 /* is_add */ );
}
}));
/* *INDENT-ON* */
}
return 0;
}
static crypto_context_t *
quic_crypto_context_alloc (u8 thread_index)
{
quic_main_t *qm = &quic_main;
crypto_context_t *crctx;
u32 idx;
pool_get (qm->wrk_ctx[thread_index].crypto_ctx_pool, crctx);
clib_memset (crctx, 0, sizeof (*crctx));
idx = (crctx - qm->wrk_ctx[thread_index].crypto_ctx_pool);
crctx->ctx_index = ((u32) thread_index) << 24 | idx;
return crctx;
}
static crypto_context_t *
quic_crypto_context_get (u32 cr_index, u32 thread_index)
{
quic_main_t *qm = &quic_main;
ASSERT (cr_index >> 24 == thread_index);
return pool_elt_at_index (qm->wrk_ctx[thread_index].crypto_ctx_pool,
cr_index & 0x00ffffff);
}
static clib_error_t *
quic_list_crypto_context_command_fn (vlib_main_t * vm,
unformat_input_t * input,
vlib_cli_command_t * cmd)
{
quic_main_t *qm = &quic_main;
crypto_context_t *crctx;
vlib_thread_main_t *vtm = vlib_get_thread_main ();
int i, num_threads = 1 /* main thread */ + vtm->n_threads;
for (i = 0; i < num_threads; i++)
{
/* *INDENT-OFF* */
pool_foreach (crctx, qm->wrk_ctx[i].crypto_ctx_pool, ({
vlib_cli_output (vm, "[%d][Q]%U", i, format_crypto_context, crctx);
}));
/* *INDENT-ON* */
}
return 0;
}
static clib_error_t *
quic_set_max_packets_per_key_fn (vlib_main_t * vm,
unformat_input_t * input,
vlib_cli_command_t * cmd)
{
quic_main_t *qm = &quic_main;
unformat_input_t _line_input, *line_input = &_line_input;
u64 tmp;
if (!unformat_user (input, unformat_line_input, line_input))
return 0;
while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
{
if (unformat (line_input, "%U", unformat_memory_size, &tmp))
{
qm->max_packets_per_key = tmp;
}
else
return clib_error_return (0, "unknown input '%U'",
format_unformat_error, line_input);
}
return 0;
}
static void
quic_release_crypto_context (u32 crypto_context_index, u8 thread_index)
{
crypto_context_t *crctx;
crctx = quic_crypto_context_get (crypto_context_index, thread_index);
crctx->n_subscribers--;
quic_crypto_context_free_if_needed (crctx, thread_index);
}
static int
quic_init_crypto_context (crypto_context_t * crctx, quic_ctx_t * ctx)
{
quic_main_t *qm = &quic_main;
quicly_context_t *quicly_ctx;
ptls_iovec_t key_vec;
app_cert_key_pair_t *ckpair;
application_t *app;
quic_crypto_context_data_t *data;
ptls_context_t *ptls_ctx;
QUIC_DBG (2, "Init quic crctx %d thread %d", crctx->ctx_index,
ctx->c_thread_index);
data = clib_mem_alloc (sizeof (*data));
/* picotls depends on data being zeroed */
clib_memset (data, 0, sizeof (*data));
crctx->data = (void *) data;
quicly_ctx = &data->quicly_ctx;
ptls_ctx = &data->ptls_ctx;
ptls_ctx->random_bytes = ptls_openssl_random_bytes;
ptls_ctx->get_time = &ptls_get_time;
ptls_ctx->key_exchanges = ptls_openssl_key_exchanges;
ptls_ctx->cipher_suites = qm->quic_ciphers[ctx->crypto_engine];
ptls_ctx->certificates.list = NULL;
ptls_ctx->certificates.count = 0;
ptls_ctx->esni = NULL;
ptls_ctx->on_client_hello = NULL;
ptls_ctx->emit_certificate = NULL;
ptls_ctx->sign_certificate = NULL;
ptls_ctx->verify_certificate = NULL;
ptls_ctx->ticket_lifetime = 86400;
ptls_ctx->max_early_data_size = 8192;
ptls_ctx->hkdf_label_prefix__obsolete = NULL;
ptls_ctx->require_dhe_on_psk = 1;
ptls_ctx->encrypt_ticket = &qm->session_cache.super;
clib_memcpy (quicly_ctx, &quicly_spec_context, sizeof (quicly_context_t));
quicly_ctx->max_packet_size = QUIC_MAX_PACKET_SIZE;
quicly_ctx->max_packets_per_key = qm->max_packets_per_key;
quicly_ctx->tls = ptls_ctx;
quicly_ctx->stream_open = &on_stream_open;
quicly_ctx->closed_by_peer = &on_closed_by_peer;
quicly_ctx->now = &quicly_vpp_now_cb;
quicly_amend_ptls_context (quicly_ctx->tls);
quicly_ctx->packet_allocator = &quic_packet_allocator;
quicly_ctx->crypto_engine = &quic_crypto_engine;
quicly_ctx->transport_params.max_data = QUIC_INT_MAX;
quicly_ctx->transport_params.max_streams_uni = (uint64_t) 1 << 60;
quicly_ctx->transport_params.max_streams_bidi = (uint64_t) 1 << 60;
quicly_ctx->transport_params.max_idle_timeout = qm->connection_timeout;
app = application_get (ctx->parent_app_id);
quicly_ctx->transport_params.max_stream_data.bidi_local =
app->sm_properties.rx_fifo_size - 1;
quicly_ctx->transport_params.max_stream_data.bidi_remote =
app->sm_properties.tx_fifo_size - 1;
quicly_ctx->transport_params.max_stream_data.uni = QUIC_INT_MAX;
if (!app->quic_iv_set)
{
ptls_openssl_random_bytes (app->quic_iv, QUIC_IV_LEN - 1);
app->quic_iv[QUIC_IV_LEN - 1] = 0;
app->quic_iv_set = 1;
}
clib_memcpy (data->cid_key, app->quic_iv, QUIC_IV_LEN);
key_vec = ptls_iovec_init (data->cid_key, QUIC_IV_LEN);
quicly_ctx->cid_encryptor =
quicly_new_default_cid_encryptor (&ptls_openssl_bfecb,
&ptls_openssl_aes128ecb,
&ptls_openssl_sha256, key_vec);
ckpair = app_cert_key_pair_get_if_valid (crctx->ckpair_index);
if (!ckpair || !ckpair->key || !ckpair->cert)
{
QUIC_DBG (1, "Wrong ckpair id %d\n", crctx->ckpair_index);
return -1;
}
if (load_bio_private_key (quicly_ctx->tls, (char *) ckpair->key))
{
QUIC_DBG (1, "failed to read private key from app configuration\n");
return -1;
}
if (load_bio_certificate_chain (quicly_ctx->tls, (char *) ckpair->cert))
{
QUIC_DBG (1, "failed to load certificate\n");
return -1;
}
return 0;
}
static int
quic_acquire_crypto_context (quic_ctx_t * ctx)
{
quic_main_t *qm = &quic_main;
crypto_context_t *crctx;
clib_bihash_kv_24_8_t kv;
if (ctx->crypto_engine == CRYPTO_ENGINE_NONE)
{
QUIC_DBG (2, "No crypto engine specified, using %d",
qm->default_crypto_engine);
ctx->crypto_engine = qm->default_crypto_engine;
}
if (!clib_bitmap_get (qm->available_crypto_engines, ctx->crypto_engine))
{
QUIC_DBG (1, "Quic does not support crypto engine %d",
ctx->crypto_engine);
return VNET_API_ERROR_MISSING_CERT_KEY;
}
/* Check for exisiting crypto ctx */
quic_crypto_context_make_key_from_ctx (&kv, ctx);
if (clib_bihash_search_24_8
(&qm->wrk_ctx[ctx->c_thread_index].crypto_context_hash, &kv, &kv) == 0)
{
crctx = quic_crypto_context_get (kv.value, ctx->c_thread_index);
QUIC_DBG (2, "Found exisiting crypto context %d", kv.value);
ctx->crypto_context_index = kv.value;
crctx->n_subscribers++;
return 0;
}
crctx = quic_crypto_context_alloc (ctx->c_thread_index);
ctx->crypto_context_index = crctx->ctx_index;
kv.value = crctx->ctx_index;
crctx->crypto_engine = ctx->crypto_engine;
crctx->ckpair_index = ctx->ckpair_index;
if (quic_init_crypto_context (crctx, ctx))
goto error;
if (vnet_app_add_cert_key_interest (ctx->ckpair_index, qm->app_index))
goto error;
crctx->n_subscribers++;
clib_bihash_add_del_24_8 (&qm->
wrk_ctx[ctx->c_thread_index].crypto_context_hash,
&kv, 1 /* is_add */ );
return 0;
error:
quic_crypto_context_free_if_needed (crctx, ctx->c_thread_index);
return VNET_API_ERROR_MISSING_CERT_KEY;
}
/* Helper functions */
static u32
quic_ctx_alloc (u32 thread_index)
{
quic_main_t *qm = &quic_main;
quic_ctx_t *ctx;
pool_get (qm->ctx_pool[thread_index], ctx);
clib_memset (ctx, 0, sizeof (quic_ctx_t));
ctx->c_thread_index = thread_index;
ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
QUIC_DBG (3, "Allocated quic_ctx %u on thread %u",
ctx - qm->ctx_pool[thread_index], thread_index);
return ctx - qm->ctx_pool[thread_index];
}
static void
quic_ctx_free (quic_ctx_t * ctx)
{
QUIC_DBG (2, "Free ctx %u %x", ctx->c_thread_index, ctx->c_c_index);
u32 thread_index = ctx->c_thread_index;
QUIC_ASSERT (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID);
if (CLIB_DEBUG)
clib_memset (ctx, 0xfb, sizeof (*ctx));
pool_put (quic_main.ctx_pool[thread_index], ctx);
}
static quic_ctx_t *
quic_ctx_get (u32 ctx_index, u32 thread_index)
{
return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
}
static quic_ctx_t *
quic_ctx_get_if_valid (u32 ctx_index, u32 thread_index)
{
if (pool_is_free_index (quic_main.ctx_pool[thread_index], ctx_index))
return 0;
return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
}
quic_ctx_t *
quic_get_conn_ctx (quicly_conn_t * conn)
{
u64 conn_data;
conn_data = (u64) * quicly_get_data (conn);
return quic_ctx_get (conn_data & UINT32_MAX, conn_data >> 32);
}
static void
quic_store_conn_ctx (quicly_conn_t * conn, quic_ctx_t * ctx)
{
*quicly_get_data (conn) =
(void *) (((u64) ctx->c_thread_index) << 32 | (u64) ctx->c_c_index);
}
static inline int
quic_ctx_is_stream (quic_ctx_t * ctx)
{
return (ctx->flags & QUIC_F_IS_STREAM);
}
static inline int
quic_ctx_is_listener (quic_ctx_t * ctx)
{
return (ctx->flags & QUIC_F_IS_LISTENER);
}
static inline int
quic_ctx_is_conn (quic_ctx_t * ctx)
{
return !(quic_ctx_is_listener (ctx) || quic_ctx_is_stream (ctx));
}
static inline session_t *
get_stream_session_and_ctx_from_stream (quicly_stream_t * stream,
quic_ctx_t ** ctx)
{
quic_stream_data_t *stream_data;
stream_data = (quic_stream_data_t *) stream->data;
*ctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
return session_get ((*ctx)->c_s_index, stream_data->thread_index);
}
static inline void
quic_make_connection_key (clib_bihash_kv_16_8_t * kv,
const quicly_cid_plaintext_t * id)
{
kv->key[0] = ((u64) id->master_id) << 32 | (u64) id->thread_id;
kv->key[1] = id->node_id;
}
static int
quic_sendable_packet_count (session_t * udp_session)
{
u32 max_enqueue;
u32 packet_size = QUIC_MAX_PACKET_SIZE + SESSION_CONN_HDR_LEN;
max_enqueue = svm_fifo_max_enqueue (udp_session->tx_fifo);
return clib_min (max_enqueue / packet_size, QUIC_SEND_PACKET_VEC_SIZE);
}
static quicly_context_t *
quic_get_quicly_ctx_from_ctx (quic_ctx_t * ctx)
{
crypto_context_t *crctx =
quic_crypto_context_get (ctx->crypto_context_index, ctx->c_thread_index);
quic_crypto_context_data_t *data =
(quic_crypto_context_data_t *) crctx->data;
return &data->quicly_ctx;
}
static quicly_context_t *
quic_get_quicly_ctx_from_udp (u64 udp_session_handle)
{
session_t *udp_session = session_get_from_handle (udp_session_handle);
quic_ctx_t *ctx =
quic_ctx_get (udp_session->opaque, udp_session->thread_index);
return quic_get_quicly_ctx_from_ctx (ctx);
}
static inline void
quic_set_udp_tx_evt (session_t * udp_session)
{
int rv = 0;
if (svm_fifo_set_event (udp_session->tx_fifo))
rv = session_send_io_evt_to_thread (udp_session->tx_fifo,
SESSION_IO_EVT_TX);
if (PREDICT_FALSE (rv))
clib_warning ("Event enqueue errored %d", rv);
}
static inline void
quic_stop_ctx_timer (quic_ctx_t * ctx)
{
tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
return;
tw = &quic_main.wrk_ctx[ctx->c_thread_index].timer_wheel;
tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
QUIC_DBG (4, "Stopping timer for ctx %u", ctx->c_c_index);
}
/* QUIC protocol actions */
static void
quic_ack_rx_data (session_t * stream_session)
{
u32 max_deq;
quic_ctx_t *sctx;
svm_fifo_t *f;
quicly_stream_t *stream;
quic_stream_data_t *stream_data;
sctx = quic_ctx_get (stream_session->connection_index,
stream_session->thread_index);
QUIC_ASSERT (quic_ctx_is_stream (sctx));
stream = sctx->stream;
stream_data = (quic_stream_data_t *) stream->data;
f = stream_session->rx_fifo;
max_deq = svm_fifo_max_dequeue (f);
QUIC_ASSERT (stream_data->app_rx_data_len >= max_deq);
quicly_stream_sync_recvbuf (stream, stream_data->app_rx_data_len - max_deq);
QUIC_DBG (3, "Acking %u bytes", stream_data->app_rx_data_len - max_deq);
stream_data->app_rx_data_len = max_deq;
}
static void
quic_disconnect_transport (quic_ctx_t * ctx)
{
QUIC_DBG (2, "Disconnecting transport 0x%lx", ctx->udp_session_handle);
vnet_disconnect_args_t a = {
.handle = ctx->udp_session_handle,
.app_index = quic_main.app_index,
};
if (vnet_disconnect_session (&a))
clib_warning ("UDP session 0x%lx disconnect errored",
ctx->udp_session_handle);
}
static void
quic_connection_delete (quic_ctx_t * ctx)
{
clib_bihash_kv_16_8_t kv;
quicly_conn_t *conn;
QUIC_DBG (2, "Deleting connection %u", ctx->c_c_index);
QUIC_ASSERT (!quic_ctx_is_stream (ctx));
quic_stop_ctx_timer (ctx);
/* Delete the connection from the connection map */
conn = ctx->conn;
ctx->conn = NULL;
quic_make_connection_key (&kv, quicly_get_master_id (conn));
QUIC_DBG (2, "Deleting conn with id %lu %lu from map", kv.key[0],
kv.key[1]);
clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 0 /* is_add */ );
quic_disconnect_transport (ctx);
if (ctx->conn)
quicly_free (ctx->conn);
session_transport_delete_notify (&ctx->connection);
}
void
quic_increment_counter (u8 evt, u8 val)
{
vlib_main_t *vm = vlib_get_main ();
vlib_node_increment_counter (vm, quic_input_node.index, evt, val);
}
/**
* Called when quicly return an error
* This function interacts tightly with quic_proto_on_close
*/
static void
quic_connection_closed (quic_ctx_t * ctx)
{
QUIC_DBG (2, "QUIC connection %u/%u closed", ctx->c_thread_index,
ctx->c_c_index);
/* TODO if connection is not established, just delete the session? */
/* Actually should send connect or accept error */
switch (ctx->conn_state)
{
case QUIC_CONN_STATE_READY:
/* Error on an opened connection (timeout...)
This puts the session in closing state, we should receive a notification
when the app has closed its session */
session_transport_reset_notify (&ctx->connection);
/* This ensures we delete the connection when the app confirms the close */
ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
break;
case QUIC_CONN_STATE_PASSIVE_CLOSING:
ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
/* quic_proto_on_close will eventually be called when the app confirms the close
, we delete the connection at that point */
break;
case QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED:
/* App already confirmed close, we can delete the connection */
quic_connection_delete (ctx);
break;
case QUIC_CONN_STATE_OPENED:
case QUIC_CONN_STATE_HANDSHAKE:
case QUIC_CONN_STATE_ACTIVE_CLOSING:
quic_connection_delete (ctx);
break;
default:
QUIC_DBG (0, "BUG %d", ctx->conn_state);
break;
}
}
static int
quic_send_datagram (session_t * udp_session, quicly_datagram_t * packet)
{
u32 max_enqueue;
session_dgram_hdr_t hdr;
u32 len, ret;
svm_fifo_t *f;
transport_connection_t *tc;
len = packet->data.len;
f = udp_session->tx_fifo;
tc = session_get_transport (udp_session);
max_enqueue = svm_fifo_max_enqueue (f);
if (max_enqueue < SESSION_CONN_HDR_LEN + len)
{
QUIC_ERR ("Too much data to send, max_enqueue %u, len %u",
max_enqueue, len + SESSION_CONN_HDR_LEN);
return QUIC_ERROR_FULL_FIFO;
}
/* Build packet header for fifo */
hdr.data_length = len;
hdr.data_offset = 0;
hdr.is_ip4 = tc->is_ip4;
clib_memcpy (&hdr.lcl_ip, &tc->lcl_ip, sizeof (ip46_address_t));
hdr.lcl_port = tc->lcl_port;
/* Read dest address from quicly-provided sockaddr */
if (hdr.is_ip4)
{
QUIC_ASSERT (packet->dest.sa.sa_family == AF_INET);
struct sockaddr_in *sa4 = (struct sockaddr_in *) &packet->dest.sa;
hdr.rmt_port = sa4->sin_port;
hdr.rmt_ip.ip4.as_u32 = sa4->sin_addr.s_addr;
}
else
{
QUIC_ASSERT (packet->dest.sa.sa_family == AF_INET6);
struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &packet->dest.sa;
hdr.rmt_port = sa6->sin6_port;
clib_memcpy (&hdr.rmt_ip.ip6, &sa6->sin6_addr, 16);
}
ret = svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
if (ret != sizeof (hdr))
{
QUIC_ERR ("Not enough space to enqueue header");
return QUIC_ERROR_FULL_FIFO;
}
ret = svm_fifo_enqueue (f, len, packet->data.base);
if (ret != len)
{
QUIC_ERR ("Not enough space to enqueue payload");
return QUIC_ERROR_FULL_FIFO;
}
quic_increment_counter (QUIC_ERROR_TX_PACKETS, 1);
return 0;
}
static int
quic_send_packets (quic_ctx_t * ctx)
{
quic_main_t *qm = &quic_main;
quicly_datagram_t *packets[QUIC_SEND_PACKET_VEC_SIZE];
session_t *udp_session;
quicly_conn_t *conn;
size_t num_packets, i, max_packets;
quicly_packet_allocator_t *pa;
int err = 0;
u32 thread_index = vlib_get_thread_index ();
/* We have sctx, get qctx */
if (quic_ctx_is_stream (ctx))
ctx = quic_ctx_get (ctx->quic_connection_ctx_id, ctx->c_thread_index);
QUIC_ASSERT (!quic_ctx_is_stream (ctx));
udp_session = session_get_from_handle_if_valid (ctx->udp_session_handle);
if (!udp_session)
goto quicly_error;
conn = ctx->conn;
if (!conn)
return 0;
/* TODO : quicly can assert it can send min_packets up to 2 */
if (quic_sendable_packet_count (udp_session) < 2)
goto stop_sending;
pa = quic_get_quicly_ctx_from_ctx (ctx)->packet_allocator;
do
{
max_packets = quic_sendable_packet_count (udp_session);
if (max_packets < 2)
break;
num_packets = max_packets;
if ((err = quicly_send (conn, packets, &num_packets)))
goto quicly_error;
quic_crypto_batch_tx_packets (&qm->wrk_ctx
[thread_index].crypto_context_batch);
for (i = 0; i != num_packets; ++i)
{
quic_crypto_finalize_send_packet (packets[i]);
if ((err = quic_send_datagram (udp_session, packets[i])))
goto quicly_error;
pa->free_packet (pa, packets[i]);
}
}
while (num_packets > 0 && num_packets == max_packets);
stop_sending:
quic_set_udp_tx_evt (udp_session);
QUIC_DBG (3, "%u[TX] %u[RX]", svm_fifo_max_dequeue (udp_session->tx_fifo),
svm_fifo_max_dequeue (udp_session->rx_fifo));
quic_update_timer (ctx);
return 0;
quicly_error:
if (err && err != QUICLY_ERROR_PACKET_IGNORED
&& err != QUICLY_ERROR_FREE_CONNECTION)
clib_warning ("Quic error '%U'.", quic_format_err, err);
quic_connection_closed (ctx);
return 1;
}
/* Quicly callbacks */
static void
quic_on_stream_destroy (quicly_stream_t * stream, int err)
{
quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
stream_data->thread_index);
session_t *stream_session = session_get (sctx->c_s_index,
sctx->c_thread_index);
QUIC_DBG (2, "DESTROYED_STREAM: session 0x%lx (%U)",
session_handle (stream_session), quic_format_err, err);
stream_session->session_state = SESSION_STATE_CLOSED;
session_transport_delete_notify (&sctx->connection);
quic_increment_counter (QUIC_ERROR_CLOSED_STREAM, 1);
quic_ctx_free (sctx);
clib_mem_free (stream->data);
}
static void
quic_on_stop_sending (quicly_stream_t * stream, int err)
{
#if QUIC_DEBUG >= 2
quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
stream_data->thread_index);
session_t *stream_session = session_get (sctx->c_s_index,
sctx->c_thread_index);
clib_warning ("(NOT IMPLEMENTD) STOP_SENDING: session 0x%lx (%U)",
session_handle (stream_session), quic_format_err, err);
#endif
/* TODO : handle STOP_SENDING */
}
static void
quic_on_receive_reset (quicly_stream_t * stream, int err)
{
quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
stream_data->thread_index);
#if QUIC_DEBUG >= 2
session_t *stream_session = session_get (sctx->c_s_index,
sctx->c_thread_index);
clib_warning ("RESET_STREAM: session 0x%lx (%U)",
session_handle (stream_session), quic_format_err, err);
#endif
session_transport_closing_notify (&sctx->connection);
}
static void
quic_on_receive (quicly_stream_t * stream, size_t off, const void *src,
size_t len)
{
QUIC_DBG (3, "received data: %lu bytes, offset %lu", len, off);
u32 max_enq, rlen, rv;
quic_ctx_t *sctx;
session_t *stream_session;
app_worker_t *app_wrk;
svm_fifo_t *f;
quic_stream_data_t *stream_data;
if (!len)
return;
stream_data = (quic_stream_data_t *) stream->data;
sctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
stream_session = session_get (sctx->c_s_index, stream_data->thread_index);
f = stream_session->rx_fifo;
max_enq = svm_fifo_max_enqueue_prod (f);
QUIC_DBG (3, "Enqueuing %u at off %u in %u space", len, off, max_enq);
/* Handle duplicate packet/chunk from quicly */
if (off < stream_data->app_rx_data_len)
{
QUIC_DBG (3, "Session [idx %u, app_wrk %u, thread %u, rx-fifo 0x%llx]: "
"DUPLICATE PACKET (max_enq %u, len %u, "
"app_rx_data_len %u, off %u, ToBeNQ %u)",
stream_session->session_index,
stream_session->app_wrk_index,
stream_session->thread_index, f,
max_enq, len, stream_data->app_rx_data_len, off,
off - stream_data->app_rx_data_len + len);
return;
}
if (PREDICT_FALSE ((off - stream_data->app_rx_data_len + len) > max_enq))
{
QUIC_ERR ("Session [idx %u, app_wrk %u, thread %u, rx-fifo 0x%llx]: "
"RX FIFO IS FULL (max_enq %u, len %u, "
"app_rx_data_len %u, off %u, ToBeNQ %u)",
stream_session->session_index,
stream_session->app_wrk_index,
stream_session->thread_index, f,
max_enq, len, stream_data->app_rx_data_len, off,
off - stream_data->app_rx_data_len + len);
return; /* This shouldn't happen */
}
if (off == stream_data->app_rx_data_len)
{
/* Streams live on the same thread so (f, stream_data) should stay consistent */
rlen = svm_fifo_enqueue (f, len, (u8 *) src);
QUIC_DBG (3, "Session [idx %u, app_wrk %u, ti %u, rx-fifo 0x%llx]: "
"Enqueuing %u (rlen %u) at off %u in %u space, ",
stream_session->session_index,
stream_session->app_wrk_index,
stream_session->thread_index, f, len, rlen, off, max_enq);
stream_data->app_rx_data_len += rlen;
QUIC_ASSERT (rlen >= len);
app_wrk = app_worker_get_if_valid (stream_session->app_wrk_index);
if (PREDICT_TRUE (app_wrk != 0))
{
rv = app_worker_lock_and_send_event (app_wrk, stream_session,
SESSION_IO_EVT_RX);
if (rv)
QUIC_ERR ("Failed to ping app for RX");
}
quic_ack_rx_data (stream_session);
}
else
{
rlen = svm_fifo_enqueue_with_offset (f,
off - stream_data->app_rx_data_len,
len, (u8 *) src);
QUIC_ASSERT (rlen == 0);
}
return;
}
void
quic_fifo_egress_shift (quicly_stream_t * stream, size_t delta)
{
quic_stream_data_t *stream_data;
session_t *stream_session;
quic_ctx_t *ctx;
svm_fifo_t *f;
u32 rv;
stream_data = (quic_stream_data_t *) stream->data;
stream_session = get_stream_session_and_ctx_from_stream (stream, &ctx);
f = stream_session->tx_fifo;
QUIC_ASSERT (stream_data->app_tx_data_len >= delta);
stream_data->app_tx_data_len -= delta;
ctx->bytes_written += delta;
rv = svm_fifo_dequeue_drop (f, delta);
QUIC_ASSERT (rv == delta);
rv = quicly_stream_sync_sendbuf (stream, 0);
QUIC_ASSERT (!rv);
}
void
quic_fifo_egress_emit (quicly_stream_t * stream, size_t off, void *dst,
size_t * len, int *wrote_all)
{
quic_stream_data_t *stream_data;
quic_ctx_t *ctx;
session_t *stream_session;
svm_fifo_t *f;
u32 deq_max;
stream_data = (quic_stream_data_t *) stream->data;
stream_session = get_stream_session_and_ctx_from_stream (stream, &ctx);
f = stream_session->tx_fifo;
QUIC_DBG (3, "Emitting %u, offset %u", *len, off);
deq_max = svm_fifo_max_dequeue_cons (f);
QUIC_ASSERT (off <= deq_max);
if (off + *len < deq_max)
{
*wrote_all = 0;
}
else
{
*wrote_all = 1;
*len = deq_max - off;
}
QUIC_ASSERT (*len > 0);
if (off + *len > stream_data->app_tx_data_len)
stream_data->app_tx_data_len = off + *len;
svm_fifo_peek (f, off, *len, dst);
}
static const quicly_stream_callbacks_t quic_stream_callbacks = {
.on_destroy = quic_on_stream_destroy,
.on_send_shift = quic_fifo_egress_shift,
.on_send_emit = quic_fifo_egress_emit,
.on_send_stop = quic_on_stop_sending,
.on_receive = quic_on_receive,
.on_receive_reset = quic_on_receive_reset
};
static int
quic_on_stream_open (quicly_stream_open_t * self, quicly_stream_t * stream)
{
/* Return code for this function ends either
* - in quicly_receive : if not QUICLY_ERROR_PACKET_IGNORED, will close connection
* - in quicly_open_stream, returned directly
*/
session_t *stream_session, *quic_session;
quic_stream_data_t *stream_data;
app_worker_t *app_wrk;
quic_ctx_t *qctx, *sctx;
u32 sctx_id;
int rv;
QUIC_DBG (2, "on_stream_open called");
stream->data = clib_mem_alloc (sizeof (quic_stream_data_t));
stream->callbacks = &quic_stream_callbacks;
/* Notify accept on parent qsession, but only if this is not a locally
* initiated stream */
if (quicly_stream_is_self_initiated (stream))
return 0;
sctx_id = quic_ctx_alloc (vlib_get_thread_index ());
qctx = quic_get_conn_ctx (stream->conn);
/* Might need to signal that the connection is ready if the first thing the
* server does is open a stream */
quic_check_quic_session_connected (qctx);
/* ctx might be invalidated */
qctx = quic_get_conn_ctx (stream->conn);
stream_session = session_alloc (qctx->c_thread_index);
QUIC_DBG (2, "ACCEPTED stream_session 0x%lx ctx %u",
session_handle (stream_session), sctx_id);
sctx = quic_ctx_get (sctx_id, qctx->c_thread_index);
sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
sctx->parent_app_id = qctx->parent_app_id;
sctx->quic_connection_ctx_id = qctx->c_c_index;