-
Notifications
You must be signed in to change notification settings - Fork 686
Expand file tree
/
Copy pathdatapath_epoll.c
More file actions
2835 lines (2557 loc) · 90.7 KB
/
Copy pathdatapath_epoll.c
File metadata and controls
2835 lines (2557 loc) · 90.7 KB
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) Microsoft Corporation.
Licensed under the MIT License.
Abstract:
QUIC datapath Abstraction Layer.
Environment:
Linux
--*/
#include "platform_internal.h"
#include <fcntl.h>
#include <linux/filter.h>
#include <linux/in6.h>
#include <netinet/udp.h>
#ifdef QUIC_CLOG
#include "datapath_epoll.c.clog.h"
#endif
CXPLAT_STATIC_ASSERT((SIZEOF_STRUCT_MEMBER(QUIC_BUFFER, Length) <= sizeof(size_t)), "(sizeof(QUIC_BUFFER.Length) == sizeof(size_t) must be TRUE.");
CXPLAT_STATIC_ASSERT((SIZEOF_STRUCT_MEMBER(QUIC_BUFFER, Buffer) == sizeof(void*)), "(sizeof(QUIC_BUFFER.Buffer) == sizeof(void*) must be TRUE.");
//
// The maximum single buffer size for single packet/datagram IO payloads.
//
#define CXPLAT_SMALL_IO_BUFFER_SIZE MAX_UDP_PAYLOAD_LENGTH
//
// The maximum single buffer size for coalesced IO payloads.
// Payload size: 65535 - 8 (UDP header) - 20 (IP header) = 65507 bytes.
//
#define CXPLAT_LARGE_IO_BUFFER_SIZE 0xFFE3
//
// The maximum batch size of IOs in that can use a single coalesced IO buffer.
// This is calculated base on the number of the smallest possible single
// packet/datagram payloads (i.e. IPv6) that can fit in the large buffer.
//
const uint16_t CXPLAT_MAX_IO_BATCH_SIZE =
(CXPLAT_LARGE_IO_BUFFER_SIZE / (1280 - CXPLAT_MIN_IPV6_HEADER_SIZE - CXPLAT_UDP_HEADER_SIZE));
//
// Contains all the info for a single RX IO operation. Multiple RX packets may
// come from a single IO operation.
//
typedef struct __attribute__((aligned(16))) DATAPATH_RX_IO_BLOCK {
//
// Represents the network route.
//
CXPLAT_ROUTE Route;
//
// Ref count of receive data/packets that are using this block.
//
long RefCount;
//
// An array of packets to represent the datagram and metadata returned to
// the app.
//
//DATAPATH_RX_PACKET Packets[0];
//
// Buffer that actually stores the UDP payload.
//
//uint8_t Buffer[]; // CXPLAT_SMALL_IO_BUFFER_SIZE or CXPLAT_LARGE_IO_BUFFER_SIZE
} DATAPATH_RX_IO_BLOCK;
typedef struct __attribute__((aligned(16))) DATAPATH_RX_PACKET {
//
// The IO block that owns the packet.
//
DATAPATH_RX_IO_BLOCK* IoBlock;
//
// Publicly visible receive data.
//
CXPLAT_RECV_DATA Data;
} DATAPATH_RX_PACKET;
//
// Send context.
//
typedef struct CXPLAT_SEND_DATA {
CXPLAT_SEND_DATA_COMMON;
//
// The socket context owning this send.
//
struct CXPLAT_SOCKET_CONTEXT* SocketContext;
//
// Entry in the pending send list.
//
CXPLAT_LIST_ENTRY TxEntry;
//
// The local address to bind to.
//
QUIC_ADDR LocalAddress;
//
// The remote address to send to.
//
QUIC_ADDR RemoteAddress;
//
// The current QUIC_BUFFER returned to the client for segmented sends.
//
QUIC_BUFFER ClientBuffer;
//
// Total number of packet buffers allocated (and iovecs used if !GSO).
//
uint16_t BufferCount;
//
// The number of iovecs that have been sent out. Only relavent if not doing
// GSO.
//
uint16_t AlreadySentCount;
//
// Length of the calculated ControlBuffer. Value is zero until the data is
// computed.
//
uint8_t ControlBufferLength;
//
// Set of flags set to configure the send behavior.
//
uint8_t Flags; // CXPLAT_SEND_FLAGS
//
// Indicates that send is on a connected socket.
//
uint8_t OnConnectedSocket : 1;
//
// Indicates that segmentation is supported for the send data.
//
uint8_t SegmentationSupported : 1;
//
// Space for ancillary control data.
//
alignas(8)
char ControlBuffer[
CMSG_SPACE(sizeof(int)) + // IP_TOS || IPV6_TCLASS
CMSG_SPACE(sizeof(struct in6_pktinfo)) // IP_PKTINFO || IPV6_PKTINFO
#ifdef UDP_SEGMENT
+ CMSG_SPACE(sizeof(uint16_t)) // UDP_SEGMENT
#endif
];
CXPLAT_STATIC_ASSERT(
CMSG_SPACE(sizeof(struct in6_pktinfo)) >= CMSG_SPACE(sizeof(struct in_pktinfo)),
"sizeof(struct in6_pktinfo) >= sizeof(struct in_pktinfo) failed");
//
// Space for all the packet buffers.
//
uint8_t Buffer[CXPLAT_LARGE_IO_BUFFER_SIZE];
//
// The total number of bytes buffer sent (only used for TCP).
//
uint32_t TotalBytesSent;
//
// IO vectors used for sends on the socket.
//
struct iovec Iovs[1]; // variable length, depends on if GSO is being used
// if GSO is used, only 1 is needed
// if GSO is not used, then N are needed
} CXPLAT_SEND_DATA;
typedef struct CXPLAT_RECV_MSG_CONTROL_BUFFER {
char Data[CMSG_SPACE(sizeof(struct in6_pktinfo)) + // IP_PKTINFO
3 * CMSG_SPACE(sizeof(int))]; // TOS + IP_TTL
} CXPLAT_RECV_MSG_CONTROL_BUFFER;
#ifdef DEBUG
#define CXPLAT_DBG_ASSERT_CMSG(CMsg, type) \
if (CMsg->cmsg_len < CMSG_LEN(sizeof(type))) { \
printf("%u: cmsg[%u:%u] len (%u) < exp_len (%u)\n", \
(uint32_t)__LINE__, \
(uint32_t)CMsg->cmsg_level, (uint32_t)CMsg->cmsg_type, \
(uint32_t)CMsg->cmsg_len, (uint32_t)CMSG_LEN(sizeof(type))); \
}
#else
#define CXPLAT_DBG_ASSERT_CMSG(CMsg, type)
#endif
CXPLAT_EVENT_COMPLETION CxPlatSocketContextUninitializeEventComplete;
CXPLAT_EVENT_COMPLETION CxPlatSocketContextFlushTxEventComplete;
CXPLAT_EVENT_COMPLETION CxPlatSocketContextIoEventComplete;
void
CxPlatDataPathCalculateFeatureSupport(
_Inout_ CXPLAT_DATAPATH* Datapath,
_In_ uint32_t ClientRecvDataLength
)
{
#ifdef UDP_SEGMENT
//
// Open up two sockets and send with GSO and receive with GRO, and make sure
// everything **actually** works, so that we can be sure we can leverage
// GRO.
//
int SendSocket = INVALID_SOCKET, RecvSocket = INVALID_SOCKET;
struct sockaddr_in RecvAddr = {0}, RecvAddr2 = {0};
socklen_t RecvAddrSize = sizeof(RecvAddr), RecvAddr2Size = sizeof(RecvAddr2);
int PktInfoEnabled = 1, TosEnabled = 1, GroEnabled = 1;
uint8_t Buffer[8 * 1476] = {0};
struct iovec IoVec;
IoVec.iov_base = Buffer;
IoVec.iov_len = sizeof(Buffer);
char SendControlBuffer[CMSG_SPACE(sizeof(int)) + CMSG_SPACE(sizeof(uint16_t))] = {0};
struct msghdr SendMsg = {0};
SendMsg.msg_name = &RecvAddr;
SendMsg.msg_namelen = RecvAddrSize;
SendMsg.msg_iov = &IoVec;
SendMsg.msg_iovlen = 1;
SendMsg.msg_control = SendControlBuffer;
SendMsg.msg_controllen = sizeof(SendControlBuffer);
struct cmsghdr *CMsg = CMSG_FIRSTHDR(&SendMsg);
CMsg->cmsg_level = IPPROTO_IP;
CMsg->cmsg_type = IP_TOS;
CMsg->cmsg_len = CMSG_LEN(sizeof(int));
*(int*)CMSG_DATA(CMsg) = 0x1;
CMsg = CMSG_NXTHDR(&SendMsg, CMsg);
CMsg->cmsg_level = SOL_UDP;
CMsg->cmsg_type = UDP_SEGMENT;
CMsg->cmsg_len = CMSG_LEN(sizeof(uint16_t));
*((uint16_t*)CMSG_DATA(CMsg)) = 1476;
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
char RecvControlBuffer[CMSG_SPACE(sizeof(int)) + CMSG_SPACE(sizeof(int)) + CMSG_SPACE(sizeof(struct in6_pktinfo))] = {0};
struct msghdr RecvMsg = {0};
RecvMsg.msg_name = &RecvAddr2;
RecvMsg.msg_namelen = RecvAddr2Size;
RecvMsg.msg_iov = &IoVec;
RecvMsg.msg_iovlen = 1;
RecvMsg.msg_control = RecvControlBuffer;
RecvMsg.msg_controllen = sizeof(RecvControlBuffer);
#define VERIFY(X) if (!(X)) { goto Error; }
SendSocket = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP);
VERIFY(SendSocket != INVALID_SOCKET)
RecvSocket = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP);
VERIFY(RecvSocket != INVALID_SOCKET)
VERIFY(setsockopt(SendSocket, IPPROTO_IP, IP_PKTINFO, &PktInfoEnabled, sizeof(PktInfoEnabled)) != SOCKET_ERROR)
VERIFY(setsockopt(RecvSocket, IPPROTO_IP, IP_PKTINFO, &PktInfoEnabled, sizeof(PktInfoEnabled)) != SOCKET_ERROR)
VERIFY(setsockopt(SendSocket, IPPROTO_IP, IP_RECVTOS, &TosEnabled, sizeof(TosEnabled)) != SOCKET_ERROR)
VERIFY(setsockopt(RecvSocket, IPPROTO_IP, IP_RECVTOS, &TosEnabled, sizeof(TosEnabled)) != SOCKET_ERROR)
VERIFY(bind(RecvSocket, (struct sockaddr*)&RecvAddr, RecvAddrSize) != SOCKET_ERROR)
#ifdef UDP_GRO
VERIFY(setsockopt(RecvSocket, SOL_UDP, UDP_GRO, &GroEnabled, sizeof(GroEnabled)) != SOCKET_ERROR)
#endif
VERIFY(getsockname(RecvSocket, (struct sockaddr*)&RecvAddr, &RecvAddrSize) != SOCKET_ERROR)
VERIFY(connect(SendSocket, (struct sockaddr*)&RecvAddr, RecvAddrSize) != SOCKET_ERROR)
VERIFY(sendmsg(SendSocket, &SendMsg, 0) == sizeof(Buffer))
//
// We were able to at least send successfully, so indicate the send
// segmentation feature as available.
//
Datapath->Features |= CXPLAT_DATAPATH_FEATURE_SEND_SEGMENTATION;
#ifdef UDP_GRO
VERIFY(recvmsg(RecvSocket, &RecvMsg, 0) == sizeof(Buffer))
BOOLEAN FoundPKTINFO = FALSE, FoundTOS = FALSE, FoundGRO = FALSE;
for (CMsg = CMSG_FIRSTHDR(&RecvMsg); CMsg != NULL; CMsg = CMSG_NXTHDR(&RecvMsg, CMsg)) {
if (CMsg->cmsg_level == IPPROTO_IP) {
if (CMsg->cmsg_type == IP_PKTINFO) {
FoundPKTINFO = TRUE;
} else if (CMsg->cmsg_type == IP_TOS) {
CXPLAT_DBG_ASSERT_CMSG(CMsg, uint8_t);
VERIFY(0x1 == *(uint8_t*)CMSG_DATA(CMsg))
FoundTOS = TRUE;
}
} else if (CMsg->cmsg_level == IPPROTO_UDP) {
if (CMsg->cmsg_type == UDP_GRO) {
CXPLAT_DBG_ASSERT_CMSG(CMsg, uint16_t);
VERIFY(1476 == *(uint16_t*)CMSG_DATA(CMsg))
FoundGRO = TRUE;
}
}
}
VERIFY(FoundPKTINFO)
VERIFY(FoundTOS)
VERIFY(FoundGRO)
//
// We were able receive everything successfully so we can indicate the
// receive coalescing feature as available.
//
Datapath->Features |= CXPLAT_DATAPATH_FEATURE_RECV_COALESCING;
#endif // UDP_GRO
Error:
if (RecvSocket != INVALID_SOCKET) { close(RecvSocket); }
if (SendSocket != INVALID_SOCKET) { close(SendSocket); }
#endif // UDP_SEGMENT
if (Datapath->Features & CXPLAT_DATAPATH_FEATURE_SEND_SEGMENTATION) {
Datapath->SendDataSize = sizeof(CXPLAT_SEND_DATA);
Datapath->SendIoVecCount = 1;
} else {
const uint32_t SendDataSize =
sizeof(CXPLAT_SEND_DATA) + (CXPLAT_MAX_IO_BATCH_SIZE - 1) * sizeof(struct iovec);
Datapath->SendDataSize = SendDataSize;
Datapath->SendIoVecCount = CXPLAT_MAX_IO_BATCH_SIZE;
}
Datapath->RecvBlockStride =
sizeof(DATAPATH_RX_PACKET) + ClientRecvDataLength;
if (Datapath->Features & CXPLAT_DATAPATH_FEATURE_RECV_COALESCING) {
Datapath->RecvBlockBufferOffset =
sizeof(DATAPATH_RX_IO_BLOCK) +
CXPLAT_MAX_IO_BATCH_SIZE * Datapath->RecvBlockStride;
Datapath->RecvBlockSize =
Datapath->RecvBlockBufferOffset + CXPLAT_LARGE_IO_BUFFER_SIZE;
} else {
Datapath->RecvBlockBufferOffset =
sizeof(DATAPATH_RX_IO_BLOCK) + Datapath->RecvBlockStride;
Datapath->RecvBlockSize =
Datapath->RecvBlockBufferOffset + CXPLAT_SMALL_IO_BUFFER_SIZE;
}
Datapath->Features |= CXPLAT_DATAPATH_FEATURE_TCP;
Datapath->Features |= CXPLAT_DATAPATH_FEATURE_TTL;
Datapath->Features |= CXPLAT_DATAPATH_FEATURE_SEND_DSCP;
}
void
CxPlatProcessorContextInitialize(
_In_ CXPLAT_DATAPATH* Datapath,
_In_ uint16_t PartitionIndex,
_Out_ CXPLAT_DATAPATH_PARTITION* DatapathPartition
)
{
CXPLAT_DBG_ASSERT(Datapath != NULL);
DatapathPartition->Datapath = Datapath;
DatapathPartition->PartitionIndex = PartitionIndex;
DatapathPartition->EventQ = CxPlatWorkerPoolGetEventQ(Datapath->WorkerPool, PartitionIndex);
CxPlatRefInitialize(&DatapathPartition->RefCount);
CxPlatPoolInitialize(TRUE, Datapath->RecvBlockSize, QUIC_POOL_DATA, &DatapathPartition->RecvBlockPool);
CxPlatPoolInitialize(TRUE, Datapath->SendDataSize, QUIC_POOL_DATA, &DatapathPartition->SendBlockPool);
}
QUIC_STATUS
DataPathInitialize(
_In_ uint32_t ClientRecvDataLength,
_In_opt_ const CXPLAT_UDP_DATAPATH_CALLBACKS* UdpCallbacks,
_In_opt_ const CXPLAT_TCP_DATAPATH_CALLBACKS* TcpCallbacks,
_In_ CXPLAT_WORKER_POOL* WorkerPool,
_In_opt_ QUIC_GLOBAL_EXECUTION_CONFIG* Config,
_Out_ CXPLAT_DATAPATH** NewDatapath
)
{
UNREFERENCED_PARAMETER(TcpCallbacks);
UNREFERENCED_PARAMETER(Config);
if (NewDatapath == NULL) {
return QUIC_STATUS_INVALID_PARAMETER;
}
if (UdpCallbacks != NULL) {
if (UdpCallbacks->Receive == NULL || UdpCallbacks->Unreachable == NULL) {
return QUIC_STATUS_INVALID_PARAMETER;
}
}
if (TcpCallbacks != NULL) {
if (TcpCallbacks->Accept == NULL ||
TcpCallbacks->Connect == NULL ||
TcpCallbacks->Receive == NULL ||
TcpCallbacks->SendComplete == NULL) {
return QUIC_STATUS_INVALID_PARAMETER;
}
}
if (WorkerPool == NULL) {
return QUIC_STATUS_INVALID_PARAMETER;
}
const size_t DatapathLength =
sizeof(CXPLAT_DATAPATH) +
CxPlatWorkerPoolGetCount(WorkerPool) * sizeof(CXPLAT_DATAPATH_PARTITION);
CXPLAT_DATAPATH* Datapath =
(CXPLAT_DATAPATH*)CXPLAT_ALLOC_PAGED(DatapathLength, QUIC_POOL_DATAPATH);
if (Datapath == NULL) {
QuicTraceEvent(
AllocFailure,
"Allocation of '%s' failed. (%llu bytes)",
"CXPLAT_DATAPATH",
DatapathLength);
return QUIC_STATUS_OUT_OF_MEMORY;
}
CxPlatZeroMemory(Datapath, DatapathLength);
if (UdpCallbacks) {
Datapath->UdpHandlers = *UdpCallbacks;
}
if (TcpCallbacks) {
Datapath->TcpHandlers = *TcpCallbacks;
}
Datapath->WorkerPool = WorkerPool;
Datapath->PartitionCount = (uint16_t)CxPlatWorkerPoolGetCount(WorkerPool);
Datapath->Features = CXPLAT_DATAPATH_FEATURE_LOCAL_PORT_SHARING;
CxPlatRefInitializeEx(&Datapath->RefCount, Datapath->PartitionCount);
CxPlatDataPathCalculateFeatureSupport(Datapath, ClientRecvDataLength);
//
// Initialize the per processor contexts.
//
for (uint32_t i = 0; i < Datapath->PartitionCount; i++) {
CxPlatProcessorContextInitialize(
Datapath, i, &Datapath->Partitions[i]);
}
CXPLAT_FRE_ASSERT(CxPlatWorkerPoolAddRef(WorkerPool));
*NewDatapath = Datapath;
return QUIC_STATUS_SUCCESS;
}
_IRQL_requires_max_(PASSIVE_LEVEL)
void
CxPlatDataPathRelease(
_In_ CXPLAT_DATAPATH* Datapath
)
{
if (CxPlatRefDecrement(&Datapath->RefCount)) {
#if DEBUG
CXPLAT_DBG_ASSERT(!Datapath->Freed);
CXPLAT_DBG_ASSERT(Datapath->Uninitialized);
Datapath->Freed = TRUE;
#endif
CxPlatWorkerPoolRelease(Datapath->WorkerPool);
CXPLAT_FREE(Datapath, QUIC_POOL_DATAPATH);
}
}
_IRQL_requires_max_(PASSIVE_LEVEL)
void
CxPlatProcessorContextRelease(
_In_ CXPLAT_DATAPATH_PARTITION* DatapathPartition
)
{
if (CxPlatRefDecrement(&DatapathPartition->RefCount)) {
#if DEBUG
CXPLAT_DBG_ASSERT(!DatapathPartition->Uninitialized);
DatapathPartition->Uninitialized = TRUE;
#endif
CxPlatPoolUninitialize(&DatapathPartition->SendBlockPool);
CxPlatPoolUninitialize(&DatapathPartition->RecvBlockPool);
CxPlatDataPathRelease(DatapathPartition->Datapath);
}
}
void
DataPathUninitialize(
_In_ CXPLAT_DATAPATH* Datapath
)
{
if (Datapath != NULL) {
#if DEBUG
CXPLAT_DBG_ASSERT(!Datapath->Uninitialized);
Datapath->Uninitialized = TRUE;
#endif
const uint16_t PartitionCount = Datapath->PartitionCount;
for (uint32_t i = 0; i < PartitionCount; i++) {
CxPlatProcessorContextRelease(&Datapath->Partitions[i]);
}
}
}
_IRQL_requires_max_(PASSIVE_LEVEL)
void
DataPathUpdateConfig(
_In_ CXPLAT_DATAPATH* Datapath,
_In_ QUIC_GLOBAL_EXECUTION_CONFIG* Config
)
{
UNREFERENCED_PARAMETER(Datapath);
UNREFERENCED_PARAMETER(Config);
}
_IRQL_requires_max_(DISPATCH_LEVEL)
uint32_t
DataPathGetSupportedFeatures(
_In_ CXPLAT_DATAPATH* Datapath
)
{
return Datapath->Features;
}
BOOLEAN
DataPathIsPaddingPreferred(
_In_ CXPLAT_DATAPATH* Datapath
)
{
return !!(Datapath->Features & CXPLAT_DATAPATH_FEATURE_SEND_SEGMENTATION);
}
QUIC_STATUS
CxPlatSocketConfigureRss(
_In_ CXPLAT_SOCKET_CONTEXT* SocketContext,
_In_ uint32_t SocketCount
)
{
#ifdef SO_ATTACH_REUSEPORT_CBPF
QUIC_STATUS Status = QUIC_STATUS_SUCCESS;
int Result = 0;
struct sock_filter BpfCode[] = {
{BPF_LD | BPF_W | BPF_ABS, 0, 0, SKF_AD_OFF | SKF_AD_CPU}, // Load CPU number
{BPF_ALU | BPF_MOD, 0, 0, SocketCount}, // MOD by SocketCount
{BPF_RET | BPF_A, 0, 0, 0} // Return
};
struct sock_fprog BpfConfig = {0};
BpfConfig.len = ARRAYSIZE(BpfCode);
BpfConfig.filter = BpfCode;
Result =
setsockopt(
SocketContext->SocketFd,
SOL_SOCKET,
SO_ATTACH_REUSEPORT_CBPF,
(const void*)&BpfConfig,
sizeof(BpfConfig));
if (Result == SOCKET_ERROR) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
SocketContext->Binding,
Status,
"setsockopt(SO_ATTACH_REUSEPORT_CBPF) failed");
}
return Status;
#else
UNREFERENCED_PARAMETER(SocketContext);
UNREFERENCED_PARAMETER(SocketCount);
return QUIC_STATUS_NOT_SUPPORTED;
#endif
}
QUIC_STATUS
CxPlatSocketContextSqeInitialize(
_Inout_ CXPLAT_SOCKET_CONTEXT* SocketContext
)
{
QUIC_STATUS Status = QUIC_STATUS_SUCCESS;
CXPLAT_SOCKET* Binding = SocketContext->Binding;
BOOLEAN ShutdownSqeInitialized = FALSE;
BOOLEAN IoSqeInitialized = FALSE;
if (!CxPlatSqeInitialize(
SocketContext->DatapathPartition->EventQ,
CxPlatSocketContextUninitializeEventComplete,
&SocketContext->ShutdownSqe)) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"CxPlatSqeInitialize failed");
goto Exit;
}
ShutdownSqeInitialized = TRUE;
if (!CxPlatSqeInitialize(
SocketContext->DatapathPartition->EventQ,
CxPlatSocketContextIoEventComplete,
&SocketContext->IoSqe)) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"CxPlatSqeInitialize failed");
goto Exit;
}
IoSqeInitialized = TRUE;
if (!CxPlatSqeInitialize(
SocketContext->DatapathPartition->EventQ,
CxPlatSocketContextFlushTxEventComplete,
&SocketContext->FlushTxSqe)) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"CxPlatSqeInitialize failed");
goto Exit;
}
SocketContext->SqeInitialized = TRUE;
return QUIC_STATUS_SUCCESS;
Exit:
if (ShutdownSqeInitialized) {
CxPlatSqeCleanup(SocketContext->DatapathPartition->EventQ, &SocketContext->ShutdownSqe);
}
if (IoSqeInitialized) {
CxPlatSqeCleanup(SocketContext->DatapathPartition->EventQ, &SocketContext->IoSqe);
}
return Status;
}
//
// Socket context interface. It abstracts a (generally per-processor) UDP socket
// and the corresponding logic/functionality like send and receive processing.
//
QUIC_STATUS
CxPlatSocketContextInitialize(
_Inout_ CXPLAT_SOCKET_CONTEXT* SocketContext,
_In_ const CXPLAT_UDP_CONFIG* Config,
_In_ const uint16_t PartitionIndex,
_In_ CXPLAT_SOCKET_TYPE SocketType
)
{
QUIC_STATUS Status = QUIC_STATUS_SUCCESS;
int Result = 0;
int Option = 0;
QUIC_ADDR MappedAddress = {0};
socklen_t AssignedLocalAddressLength = 0;
CXPLAT_SOCKET* Binding = SocketContext->Binding;
CXPLAT_DATAPATH* Datapath = Binding->Datapath;
CXPLAT_DBG_ASSERT(PartitionIndex < Datapath->PartitionCount);
SocketContext->DatapathPartition = &Datapath->Partitions[PartitionIndex];
CxPlatRefIncrement(&SocketContext->DatapathPartition->RefCount);
Status = CxPlatSocketContextSqeInitialize(SocketContext);
if (QUIC_FAILED(Status) || SocketType == CXPLAT_SOCKET_TCP_SERVER) {
goto Exit;
}
//
// Create datagram socket.
//
SocketContext->SocketFd =
socket(
AF_INET6,
(SocketType == CXPLAT_SOCKET_UDP ? SOCK_DGRAM : SOCK_STREAM) |
SOCK_NONBLOCK,
SocketType == CXPLAT_SOCKET_UDP ? IPPROTO_UDP : IPPROTO_TCP);
if (SocketContext->SocketFd == INVALID_SOCKET) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"socket failed");
goto Exit;
}
//
// Set dual (IPv4 & IPv6) socket mode.
//
Option = FALSE;
Result =
setsockopt(
SocketContext->SocketFd,
IPPROTO_IPV6,
IPV6_V6ONLY,
(const void*)&Option,
sizeof(Option));
if (Result == SOCKET_ERROR) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"setsockopt(IPV6_V6ONLY) failed");
goto Exit;
}
if (SocketType == CXPLAT_SOCKET_UDP) {
//
// Set DON'T FRAG socket option.
//
//
// Windows: setsockopt IPPROTO_IP IP_DONTFRAGMENT TRUE.
// Linux: IP_DONTFRAGMENT option is not available. IP_MTU_DISCOVER/IPV6_MTU_DISCOVER
// is the apparent alternative.
//
Option = IP_PMTUDISC_PROBE;
Result =
setsockopt(
SocketContext->SocketFd,
IPPROTO_IP,
IP_MTU_DISCOVER,
(const void*)&Option,
sizeof(Option));
if (Result == SOCKET_ERROR) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"setsockopt(IP_MTU_DISCOVER) failed");
goto Exit;
}
Result =
setsockopt(
SocketContext->SocketFd,
IPPROTO_IPV6,
IPV6_MTU_DISCOVER,
(const void*)&Option,
sizeof(Option));
if (Result == SOCKET_ERROR) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"setsockopt(IPV6_MTU_DISCOVER) failed");
goto Exit;
}
Option = TRUE;
Result =
setsockopt(
SocketContext->SocketFd,
IPPROTO_IPV6,
IPV6_DONTFRAG,
(const void*)&Option,
sizeof(Option));
if (Result == SOCKET_ERROR) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"setsockopt(IPV6_DONTFRAG) failed");
goto Exit;
}
//
// Set socket option to receive ancillary data about the incoming packets.
//
//
// Windows: setsockopt IPPROTO_IPV6 IPV6_PKTINFO TRUE.
// Android: Returns EINVAL. IPV6_PKTINFO option is not present in documentation.
// IPV6_RECVPKTINFO seems like is the alternative.
// TODO: Check if this works as expected?
//
Option = TRUE;
Result =
setsockopt(
SocketContext->SocketFd,
IPPROTO_IPV6,
IPV6_RECVPKTINFO,
(const void*)&Option,
sizeof(Option));
if (Result == SOCKET_ERROR) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"setsockopt(IPV6_RECVPKTINFO) failed");
goto Exit;
}
//
// Set socket option to receive TOS (= DSCP + ECN) information from the
// incoming packet.
//
Option = TRUE;
Result =
setsockopt(
SocketContext->SocketFd,
IPPROTO_IPV6,
IPV6_RECVTCLASS,
(const void*)&Option,
sizeof(Option));
if (Result == SOCKET_ERROR) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"setsockopt(IPV6_RECVTCLASS) failed");
goto Exit;
}
Option = TRUE;
Result =
setsockopt(
SocketContext->SocketFd,
IPPROTO_IP,
IP_RECVTOS,
(const void*)&Option,
sizeof(Option));
if (Result == SOCKET_ERROR) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"setsockopt(IP_RECVTOS) failed");
goto Exit;
}
//
// On Linux, IP_HOPLIMIT does not exist. So we will use IP_RECVTTL, IPV6_RECVHOPLIMIT instead.
//
Option = TRUE;
Result =
setsockopt(
SocketContext->SocketFd,
IPPROTO_IP,
IP_RECVTTL,
(const void*)&Option,
sizeof(Option));
if (Result == SOCKET_ERROR) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"setsockopt(IP_RECVTTL) failed");
goto Exit;
}
Option = TRUE;
Result =
setsockopt(
SocketContext->SocketFd,
IPPROTO_IPV6,
IPV6_RECVHOPLIMIT,
(const void*)&Option,
sizeof(Option));
if (Result == SOCKET_ERROR) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"setsockopt(IPV6_RECVHOPLIMIT) failed");
goto Exit;
}
#ifdef UDP_GRO
if (SocketContext->DatapathPartition->Datapath->Features & CXPLAT_DATAPATH_FEATURE_RECV_COALESCING) {
Option = TRUE;
Result =
setsockopt(
SocketContext->SocketFd,
SOL_UDP,
UDP_GRO,
(const void*)&Option,
sizeof(Option));
if (Result == SOCKET_ERROR) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"setsockopt(UDP_GRO) failed");
goto Exit;
}
}
#endif
//
// The socket is shared by multiple QUIC endpoints, so increase the receive
// buffer size.
//
Option = INT32_MAX;
Result =
setsockopt(
SocketContext->SocketFd,
SOL_SOCKET,
SO_RCVBUF,
(const void*)&Option,
sizeof(Option));
if (Result == SOCKET_ERROR) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"setsockopt(SO_RCVBUF) failed");
goto Exit;
}
//
// Only set SO_REUSEPORT on a server socket, otherwise the client could be
// assigned a server port (unless it's forcing sharing).
//
if ((Config->Flags & CXPLAT_SOCKET_FLAG_SHARE || Config->RemoteAddress == NULL) &&
SocketContext->Binding->Datapath->PartitionCount > 1) {
//
// The port is shared across processors.
//
Option = TRUE;
Result =
setsockopt(
SocketContext->SocketFd,
SOL_SOCKET,
SO_REUSEPORT,
(const void*)&Option,
sizeof(Option));
if (Result == SOCKET_ERROR) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"setsockopt(SO_REUSEPORT) failed");
goto Exit;
}
}
} else if (SocketType == CXPLAT_SOCKET_TCP_LISTENER) {
//
// Set SO_REUSEPORT to allow multiple TCP listeners to
// bind to the same port and load balance the connections across them.
// Meanwhile, it allows us to bind to the port that's held by
// passive connections.
//
Option = TRUE;
Result =
setsockopt(
SocketContext->SocketFd,
SOL_SOCKET,
SO_REUSEPORT,
(const void*)&Option,
sizeof(Option));
if (Result == SOCKET_ERROR) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"setsockopt(SO_REUSEPORT) failed");
goto Exit;
}
//
// Prevent the socket from entering TIME_WAIT state when closed.
//
struct linger LingerOpt;
LingerOpt.l_onoff = TRUE; // Enable linger
LingerOpt.l_linger = 0; // Linger time of 0 seconds (immediate reset)
Result = setsockopt(SocketContext->SocketFd, SOL_SOCKET, SO_LINGER, &LingerOpt, sizeof(LingerOpt));
if (Result == SOCKET_ERROR) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"setsockopt(SO_LINGER) failed");
goto Exit;
}
} else if (SocketType == CXPLAT_SOCKET_TCP) {
//
// Prevent the socket from entering TIME_WAIT state when closed.
//
struct linger LingerOpt;
LingerOpt.l_onoff = TRUE; // Enable linger
LingerOpt.l_linger = 0; // Linger time of 0 seconds (immediate reset)
Result = setsockopt(SocketContext->SocketFd, SOL_SOCKET, SO_LINGER, &LingerOpt, sizeof(LingerOpt));
if (Result == SOCKET_ERROR) {
Status = errno;