forked from microsoft/msquic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatapath_winkernel.c
3103 lines (2689 loc) · 85.6 KB
/
datapath_winkernel.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) Microsoft Corporation.
Licensed under the MIT License.
Abstract:
QUIC Datapath Implementation (Kernel Mode)
--*/
#include "platform_internal.h"
#ifdef QUIC_CLOG
#include "datapath_winkernel.c.clog.h"
#endif
//
// Not yet available in the WDK. When available this code can be removed.
//
#if 1
#define UDP_SEND_MSG_SIZE 2
#define UDP_RECV_MAX_COALESCED_SIZE 3
#define UDP_COALESCED_INFO 3
#endif
typedef enum {
ICMP4_ECHO_REPLY = 0, // Echo Reply.
ICMP4_DST_UNREACH = 3, // Destination Unreachable.
ICMP4_SOURCE_QUENCH = 4, // Source Quench.
ICMP4_REDIRECT = 5, // Redirect.
ICMP4_ECHO_REQUEST = 8, // Echo Request.
ICMP4_ROUTER_ADVERT = 9, // Router Advertisement.
ICMP4_ROUTER_SOLICIT = 10, // Router Solicitation.
ICMP4_TIME_EXCEEDED = 11, // Time Exceeded.
ICMP4_PARAM_PROB = 12, // Parameter Problem.
ICMP4_TIMESTAMP_REQUEST = 13, // Timestamp Request.
ICMP4_TIMESTAMP_REPLY = 14, // Timestamp Reply.
ICMP4_MASK_REQUEST = 17, // Address Mask Request.
ICMP4_MASK_REPLY = 18, // Address Mask Reply.
} ICMP4_TYPE, *PICMP4_TYPE;
typedef enum {
ICMP6_DST_UNREACH = 1,
ICMP6_PACKET_TOO_BIG = 2,
ICMP6_TIME_EXCEEDED = 3,
ICMP6_PARAM_PROB = 4,
ICMP6_ECHO_REQUEST = 128,
ICMP6_ECHO_REPLY = 129,
ICMP6_MEMBERSHIP_QUERY = 130,
ICMP6_MEMBERSHIP_REPORT = 131,
ICMP6_MEMBERSHIP_REDUCTION = 132,
ND_ROUTER_SOLICIT = 133,
ND_ROUTER_ADVERT = 134,
ND_NEIGHBOR_SOLICIT = 135,
ND_NEIGHBOR_ADVERT = 136,
ND_REDIRECT = 137,
ICMP6_V2_MEMBERSHIP_REPORT = 143,
} ICMP6_TYPE, *PICMP6_TYPE;
//
// The maximum UDP receive coalescing payload.
//
#define MAX_URO_PAYLOAD_LENGTH (UINT16_MAX - CXPLAT_UDP_HEADER_SIZE)
//
// 60K is the largest buffer most NICs can offload without any software
// segmentation. Current generation NICs advertise (60K < limit <= 64K).
//
#define CXPLAT_LARGE_SEND_BUFFER_SIZE 0xF000
//
// The maximum number of pages that memory allocated for our UDP payload
// buffers might span.
//
#define MAX_BUFFER_PAGE_USAGE ((CXPLAT_LARGE_SEND_BUFFER_SIZE / PAGE_SIZE) + 2)
//
// The maximum size of the MDL to accomodate the maximum UDP payload buffer.
//
#define MDL_SIZE (sizeof(MDL) + (sizeof(PFN_NUMBER) * MAX_BUFFER_PAGE_USAGE))
//
// The maximum number of UDP datagrams that can be sent with one call.
//
#define CXPLAT_MAX_BATCH_SEND 1
//
// The maximum number of UDP datagrams to preallocate for URO.
//
#define URO_MAX_DATAGRAMS_PER_INDICATION 64
//
// The maximum allowed pending WSK buffers per proc before copying.
//
#define PENDING_BUFFER_LIMIT 0
CXPLAT_STATIC_ASSERT(
sizeof(QUIC_BUFFER) == sizeof(WSABUF),
"WSABUF is assumed to be interchangeable for QUIC_BUFFER");
CXPLAT_STATIC_ASSERT(
FIELD_OFFSET(QUIC_BUFFER, Length) == FIELD_OFFSET(WSABUF, len),
"WSABUF is assumed to be interchangeable for QUIC_BUFFER");
CXPLAT_STATIC_ASSERT(
FIELD_OFFSET(QUIC_BUFFER, Buffer) == FIELD_OFFSET(WSABUF, buf),
"WSABUF is assumed to be interchangeable for QUIC_BUFFER");
typedef struct CXPLAT_DATAPATH_PROC_CONTEXT CXPLAT_DATAPATH_PROC_CONTEXT;
//
// Internal receive allocation context.
//
typedef struct CXPLAT_DATAPATH_INTERNAL_RECV_CONTEXT {
//
// The per proc context for this receive context.
//
CXPLAT_DATAPATH_PROC_CONTEXT* ProcContext;
union {
//
// The start of the data buffer, or the cached data indication from wsk.
//
uint8_t* DataBufferStart;
PWSK_DATAGRAM_INDICATION DataIndication;
};
CXPLAT_SOCKET* Binding;
ULONG ReferenceCount;
//
// Contains the network route.
//
CXPLAT_ROUTE Route;
int32_t DataIndicationSize;
uint8_t DatagramPoolIndex : 1;
uint8_t BufferPoolIndex : 1;
uint8_t IsCopiedBuffer : 1;
} CXPLAT_DATAPATH_INTERNAL_RECV_CONTEXT;
BOOLEAN
CxPlatMdlMapChain(
_In_ PMDL Mdl
)
{
do {
if (!(Mdl->MdlFlags & (MDL_MAPPED_TO_SYSTEM_VA | MDL_SOURCE_IS_NONPAGED_POOL))) {
if (MmMapLockedPagesSpecifyCache(
Mdl, KernelMode, MmCached, NULL, FALSE, LowPagePriority | MdlMappingNoExecute)) {
} else {
return FALSE;
}
CXPLAT_DBG_ASSERT(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA);
}
CXPLAT_DBG_ASSERT(Mdl->MappedSystemVa != NULL);
} while ((Mdl = Mdl->Next) != NULL);
return TRUE;
}
//
// Internal receive buffer context.
//
typedef struct CXPLAT_DATAPATH_INTERNAL_RECV_BUFFER_CONTEXT {
//
// The internal receive context owning the data indication and allocation
// chain.
//
CXPLAT_DATAPATH_INTERNAL_RECV_CONTEXT* RecvContext;
} CXPLAT_DATAPATH_INTERNAL_RECV_BUFFER_CONTEXT;
typedef struct CXPLAT_DATAPATH_SEND_BUFFER {
//
// A link in the Send Context's list of WSK buffers.
//
WSK_BUF_LIST Link;
//
// The MDL buffer.
//
union {
MDL Mdl;
UCHAR MdlBuffer[MDL_SIZE];
};
//
// Storage for the raw bytes.
//
UINT8 RawBuffer[0];
} CXPLAT_DATAPATH_SEND_BUFFER;
//
// Send context.
//
typedef struct CXPLAT_SEND_DATA {
CXPLAT_SOCKET* Binding;
//
// The owning processor context.
//
CXPLAT_DATAPATH_PROC_CONTEXT* Owner;
//
// The IRP buffer for the async WskSendMessages call.
//
union {
IRP Irp;
UCHAR IrpBuffer[sizeof(IRP) + sizeof(IO_STACK_LOCATION)];
};
//
// Contains the list of CXPLAT_DATAPATH_SEND_BUFFER.
//
PWSK_BUF_LIST WskBufs;
//
// The tail of the buffer list.
//
CXPLAT_DATAPATH_SEND_BUFFER* TailBuf;
//
// The total buffer size for WsaBuffers.
//
uint32_t TotalSize;
//
// The type of ECN markings needed for send.
//
CXPLAT_ECN_TYPE ECN;
//
// The number of WSK buffers allocated.
//
UINT8 WskBufferCount;
//
// The send segmentation size; zero if segmentation is not performed.
//
UINT16 SegmentSize;
//
// The QUIC_BUFFER returned to the client for segmented sends.
//
QUIC_BUFFER ClientBuffer;
} CXPLAT_SEND_DATA;
//
// WSK Client version
//
const WSK_CLIENT_DISPATCH WskAppDispatch = {
MAKE_WSK_VERSION(1,0), // Use WSK version 1.0
0, // Reserved
NULL // WskClientEvent callback not required for WSK version 1.0
};
//
// Callback for WSK to indicate received datagrams.
//
_IRQL_requires_max_(DISPATCH_LEVEL)
_Must_inspect_result_
QUIC_STATUS
NTAPI
CxPlatDataPathSocketReceive(
_In_opt_ void* Binding,
_In_ ULONG Flags,
_In_opt_ PWSK_DATAGRAM_INDICATION DataIndication
);
typedef struct _WSK_DATAGRAM_SOCKET {
const WSK_PROVIDER_DATAGRAM_DISPATCH* Dispatch;
} WSK_DATAGRAM_SOCKET, * PWSK_DATAGRAM_SOCKET;
//
// Per-port state.
//
typedef struct CXPLAT_SOCKET {
//
// Flag indicates the binding has a default remote destination.
//
BOOLEAN Connected : 1;
//
// Flag indicates the binding is being used for PCP.
//
BOOLEAN PcpBinding : 1;
//
// Parent datapath.
//
CXPLAT_DATAPATH* Datapath;
//
// UDP socket used for sending/receiving datagrams.
//
union {
PWSK_SOCKET Socket;
PWSK_DATAGRAM_SOCKET DgrmSocket;
};
//
// Event used to wait for completion of socket functions.
//
CXPLAT_EVENT WskCompletionEvent;
//
// The local address and UDP port.
//
SOCKADDR_INET LocalAddress;
//
// The remote address and UDP port.
//
SOCKADDR_INET RemoteAddress;
//
// The local interface's MTU.
//
UINT16 Mtu;
//
// Client context pointer.
//
void *ClientContext;
//
// IRP used for socket functions.
//
union {
IRP Irp;
UCHAR IrpBuffer[sizeof(IRP) + sizeof(IO_STACK_LOCATION)];
};
CXPLAT_RUNDOWN_REF Rundown[0]; // Per-proc
} CXPLAT_SOCKET;
//
// Represents the per-processor state of the datapath context.
//
typedef struct CXPLAT_DATAPATH_PROC_CONTEXT {
//
// Pool of send contexts to be shared by all sockets on this core.
//
CXPLAT_POOL SendDataPool;
//
// Pool of send buffers to be shared by all sockets on this core.
//
CXPLAT_POOL SendBufferPool;
//
// Pool of large segmented send buffers to be shared by all sockets on this
// core.
//
CXPLAT_POOL LargeSendBufferPool;
//
// Pool of receive datagram contexts and buffers to be shared by all sockets
// on this core. Index 0 is regular, Index 1 is URO.
//
//
CXPLAT_POOL RecvDatagramPools[2];
//
// Pool of receive data buffers. Index 0 is 4096, Index 1 is 65536.
//
CXPLAT_POOL RecvBufferPools[2];
int64_t OutstandingPendingBytes;
} CXPLAT_DATAPATH_PROC_CONTEXT;
//
// Structure that maintains all the internal state for the
// CxPlatDataPath interface.
//
typedef struct CXPLAT_DATAPATH {
//
// Set of supported features.
//
uint32_t Features;
//
// The registration with WinSock Kernel.
//
WSK_REGISTRATION WskRegistration;
WSK_PROVIDER_NPI WskProviderNpi;
WSK_CLIENT_DATAGRAM_DISPATCH WskDispatch;
//
// The UDP callback function pointers.
//
CXPLAT_UDP_DATAPATH_CALLBACKS UdpHandlers;
//
// The size of the buffer to allocate for client's receive context structure.
//
uint32_t ClientRecvContextLength;
//
// The size of each receive datagram array element, including client context,
// internal context, and padding.
//
uint32_t DatagramStride;
//
// The number of processors.
//
uint32_t ProcCount;
//
// Per-processor completion contexts.
//
CXPLAT_DATAPATH_PROC_CONTEXT ProcContexts[0];
} CXPLAT_DATAPATH;
_IRQL_requires_same_
_Function_class_(ALLOCATE_FUNCTION_EX)
PVOID
CxPlatSendBufferPoolAlloc(
_In_ POOL_TYPE PoolType,
_In_ SIZE_T NumberOfBytes,
_In_ ULONG Tag,
_Inout_ PLOOKASIDE_LIST_EX Lookaside
);
#define QuicSendBufferPoolInitialize(Size, Tag, Pool) \
ExInitializeLookasideListEx( \
Pool, \
CxPlatSendBufferPoolAlloc, \
NULL, \
NonPagedPoolNx, \
0, \
Size, \
Tag, \
0)
CXPLAT_RECV_DATA*
CxPlatDataPathRecvPacketToRecvData(
_In_ const CXPLAT_RECV_PACKET* const Context
)
{
return (CXPLAT_RECV_DATA*)
(((PUCHAR)Context) -
sizeof(CXPLAT_DATAPATH_INTERNAL_RECV_BUFFER_CONTEXT) -
sizeof(CXPLAT_RECV_DATA));
}
CXPLAT_RECV_PACKET*
CxPlatDataPathRecvDataToRecvPacket(
_In_ const CXPLAT_RECV_DATA* const Datagram
)
{
return (CXPLAT_RECV_PACKET*)
(((PUCHAR)Datagram) +
sizeof(CXPLAT_RECV_DATA) +
sizeof(CXPLAT_DATAPATH_INTERNAL_RECV_BUFFER_CONTEXT));
}
CXPLAT_DATAPATH_INTERNAL_RECV_BUFFER_CONTEXT*
CxPlatDataPathDatagramToInternalDatagramContext(
_In_ CXPLAT_RECV_DATA* Datagram
)
{
return (CXPLAT_DATAPATH_INTERNAL_RECV_BUFFER_CONTEXT*)
(((PUCHAR)Datagram) + sizeof(CXPLAT_RECV_DATA));
}
IO_COMPLETION_ROUTINE CxPlatDataPathIoCompletion;
//
// Used for all WSK IoCompletion routines
//
_Use_decl_annotations_
QUIC_STATUS
CxPlatDataPathIoCompletion(
PDEVICE_OBJECT DeviceObject,
PIRP Irp,
void* Context
)
{
UNREFERENCED_PARAMETER(DeviceObject);
UNREFERENCED_PARAMETER(Irp);
CXPLAT_DBG_ASSERT(Context != NULL);
KeSetEvent((KEVENT*)Context, IO_NO_INCREMENT, FALSE);
//
// Always return STATUS_MORE_PROCESSING_REQUIRED to
// terminate the completion processing of the IRP.
//
return STATUS_MORE_PROCESSING_REQUIRED;
}
void
CxPlatDataPathQueryRssScalabilityInfo(
_In_ CXPLAT_DATAPATH* Datapath
)
{
NTSTATUS Status;
PWSK_SOCKET RssSocket = NULL;
PWSK_PROVIDER_BASIC_DISPATCH Dispatch = NULL;
SIZE_T OutputSizeReturned;
RSS_SCALABILITY_INFO RssInfo = { 0 };
CXPLAT_EVENT CompletionEvent;
CxPlatEventInitialize(&CompletionEvent, FALSE, FALSE);
uint8_t IrpBuffer[sizeof(IRP) + sizeof(IO_STACK_LOCATION)];
PIRP Irp = (PIRP)IrpBuffer;
CxPlatZeroMemory(Irp, sizeof(IrpBuffer));
IoInitializeIrp(Irp, sizeof(IrpBuffer), 1);
IoSetCompletionRoutine(
Irp,
CxPlatDataPathIoCompletion,
&CompletionEvent,
TRUE,
TRUE,
TRUE);
Status =
Datapath->WskProviderNpi.Dispatch->
WskSocket(
Datapath->WskProviderNpi.Client,
AF_INET6,
SOCK_STREAM,
IPPROTO_TCP,
WSK_FLAG_BASIC_SOCKET,
NULL,
NULL,
NULL,
NULL,
NULL,
Irp);
if (Status == STATUS_PENDING) {
CxPlatEventWaitForever(CompletionEvent);
} else if (QUIC_FAILED(Status)) {
QuicTraceLogWarning(
DatapathOpenTcpSocketFailed,
"[data] RSS helper socket failed to open, 0x%x",
Status);
goto Error;
}
Status = Irp->IoStatus.Status;
if (QUIC_FAILED(Status)) {
QuicTraceLogWarning(
DatapathOpenTcpSocketFailedAsync,
"[data] RSS helper socket failed to open (async), 0x%x",
Status);
goto Error;
}
RssSocket = (PWSK_SOCKET)(Irp->IoStatus.Information);
Dispatch = (PWSK_PROVIDER_BASIC_DISPATCH)(RssSocket->Dispatch);
IoReuseIrp(Irp, STATUS_SUCCESS);
IoSetCompletionRoutine(
Irp,
CxPlatDataPathIoCompletion,
&CompletionEvent,
TRUE,
TRUE,
TRUE);
CxPlatEventReset(CompletionEvent);
Status =
Dispatch->WskControlSocket(
RssSocket,
WskIoctl,
SIO_QUERY_RSS_SCALABILITY_INFO,
SOL_SOCKET,
0,
NULL,
sizeof(RssInfo),
&RssInfo,
&OutputSizeReturned,
Irp);
if (Status == STATUS_PENDING) {
CxPlatEventWaitForever(CompletionEvent);
} else if (QUIC_FAILED(Status)) {
QuicTraceLogWarning(
DatapathQueryRssScalabilityInfoFailed,
"[data] Query for SIO_QUERY_RSS_SCALABILITY_INFO failed, 0x%x",
Status);
goto Error;
}
Status = Irp->IoStatus.Status;
if (QUIC_FAILED(Status)) {
QuicTraceLogWarning(
DatapathQueryRssScalabilityInfoFailedAsync,
"[data] Query for SIO_QUERY_RSS_SCALABILITY_INFO failed (async), 0x%x",
Status);
goto Error;
}
if (RssInfo.RssEnabled) {
Datapath->Features |= CXPLAT_DATAPATH_FEATURE_RECV_SIDE_SCALING;
}
Error:
if (RssSocket != NULL) {
IoReuseIrp(Irp, STATUS_SUCCESS);
IoSetCompletionRoutine(
Irp,
CxPlatDataPathIoCompletion,
&CompletionEvent,
TRUE,
TRUE,
TRUE);
CxPlatEventReset(CompletionEvent);
Status = Dispatch->WskCloseSocket(RssSocket, Irp);
CXPLAT_DBG_ASSERT(NT_SUCCESS(Status));
if (Status == STATUS_PENDING) {
CxPlatEventWaitForever(CompletionEvent);
}
}
IoCleanupIrp(Irp);
}
VOID
CxPlatDataPathQuerySockoptSupport(
_Inout_ CXPLAT_DATAPATH* Datapath
)
{
NTSTATUS Status;
PWSK_SOCKET UdpSocket = NULL;
PWSK_PROVIDER_BASIC_DISPATCH Dispatch = NULL;
SIZE_T OutputSizeReturned;
CXPLAT_EVENT CompletionEvent;
CxPlatEventInitialize(&CompletionEvent, FALSE, FALSE);
uint8_t IrpBuffer[sizeof(IRP) + sizeof(IO_STACK_LOCATION)];
PIRP Irp = (PIRP)IrpBuffer;
CxPlatZeroMemory(Irp, sizeof(IrpBuffer));
IoInitializeIrp(Irp, sizeof(IrpBuffer), 1);
IoSetCompletionRoutine(
Irp,
CxPlatDataPathIoCompletion,
&CompletionEvent,
TRUE,
TRUE,
TRUE);
Status =
Datapath->WskProviderNpi.Dispatch->
WskSocket(
Datapath->WskProviderNpi.Client,
AF_INET,
SOCK_DGRAM,
IPPROTO_UDP,
WSK_FLAG_BASIC_SOCKET,
NULL,
NULL,
NULL,
NULL,
NULL,
Irp);
if (Status == STATUS_PENDING) {
CxPlatEventWaitForever(CompletionEvent);
} else if (QUIC_FAILED(Status)) {
QuicTraceLogWarning(
DatapathOpenUdpSocketFailed,
"[data] UDP send segmentation helper socket failed to open, 0x%x",
Status);
goto Error;
}
Status = Irp->IoStatus.Status;
if (QUIC_FAILED(Status)) {
QuicTraceLogWarning(
DatapathOpenUdpSocketFailedAsync,
"[data] UDP send segmentation helper socket failed to open (async), 0x%x",
Status);
goto Error;
}
UdpSocket = (PWSK_SOCKET)(Irp->IoStatus.Information);
Dispatch = (PWSK_PROVIDER_BASIC_DISPATCH)(UdpSocket->Dispatch);
do {
DWORD SegmentSize;
IoReuseIrp(Irp, STATUS_SUCCESS);
IoSetCompletionRoutine(
Irp,
CxPlatDataPathIoCompletion,
&CompletionEvent,
TRUE,
TRUE,
TRUE);
CxPlatEventReset(CompletionEvent);
Status =
Dispatch->WskControlSocket(
UdpSocket,
WskGetOption,
UDP_SEND_MSG_SIZE,
IPPROTO_UDP,
0,
NULL,
sizeof(SegmentSize),
&SegmentSize,
&OutputSizeReturned,
Irp);
if (Status == STATUS_PENDING) {
CxPlatEventWaitForever(CompletionEvent);
} else if (QUIC_FAILED(Status)) {
QuicTraceLogWarning(
DatapathQueryUdpSendMsgFailed,
"[data] Query for UDP_SEND_MSG_SIZE failed, 0x%x",
Status);
break;
}
Status = Irp->IoStatus.Status;
if (QUIC_FAILED(Status)) {
QuicTraceLogWarning(
DatapathQueryUdpSendMsgFailedAsync,
"[data] Query for UDP_SEND_MSG_SIZE failed (async), 0x%x",
Status);
break;
}
Datapath->Features |= CXPLAT_DATAPATH_FEATURE_SEND_SEGMENTATION;
} while (FALSE);
do {
DWORD UroMaxCoalescedMsgSize;
IoReuseIrp(Irp, STATUS_SUCCESS);
IoSetCompletionRoutine(
Irp,
CxPlatDataPathIoCompletion,
&CompletionEvent,
TRUE,
TRUE,
TRUE);
CxPlatEventReset(CompletionEvent);
Status =
Dispatch->WskControlSocket(
UdpSocket,
WskGetOption,
UDP_RECV_MAX_COALESCED_SIZE,
IPPROTO_UDP,
0,
NULL,
sizeof(UroMaxCoalescedMsgSize),
&UroMaxCoalescedMsgSize,
&OutputSizeReturned,
Irp);
if (Status == STATUS_PENDING) {
CxPlatEventWaitForever(CompletionEvent);
} else if (QUIC_FAILED(Status)) {
QuicTraceLogWarning(
DatapathQueryRecvMaxCoalescedSizeFailed,
"[data] Query for UDP_RECV_MAX_COALESCED_SIZE failed, 0x%x",
Status);
break;
}
Status = Irp->IoStatus.Status;
if (QUIC_FAILED(Status)) {
QuicTraceLogWarning(
DatapathQueryRecvMaxCoalescedSizeFailedAsync,
"[data] Query for UDP_RECV_MAX_COALESCED_SIZE failed (async), 0x%x",
Status);
break;
}
Datapath->Features |= CXPLAT_DATAPATH_FEATURE_RECV_COALESCING;
} while (FALSE);
Error:
if (UdpSocket != NULL) {
IoReuseIrp(Irp, STATUS_SUCCESS);
IoSetCompletionRoutine(
Irp,
CxPlatDataPathIoCompletion,
&CompletionEvent,
TRUE,
TRUE,
TRUE);
CxPlatEventReset(CompletionEvent);
Status = Dispatch->WskCloseSocket(UdpSocket, Irp);
CXPLAT_DBG_ASSERT(NT_SUCCESS(Status));
if (Status == STATUS_PENDING) {
CxPlatEventWaitForever(CompletionEvent);
}
}
IoCleanupIrp(Irp);
}
_IRQL_requires_max_(PASSIVE_LEVEL)
QUIC_STATUS
CxPlatDataPathInitialize(
_In_ uint32_t ClientRecvContextLength,
_In_opt_ const CXPLAT_UDP_DATAPATH_CALLBACKS* UdpCallbacks,
_In_opt_ const CXPLAT_TCP_DATAPATH_CALLBACKS* TcpCallbacks,
_In_opt_ CXPLAT_DATAPATH_CONFIG* Config,
_Out_ CXPLAT_DATAPATH* *NewDataPath
)
{
QUIC_STATUS Status;
WSK_CLIENT_NPI WskClientNpi = { NULL, &WskAppDispatch };
uint32_t DatapathLength;
CXPLAT_DATAPATH* Datapath;
BOOLEAN WskRegistered = FALSE;
WSK_EVENT_CALLBACK_CONTROL CallbackControl =
{
&NPI_WSK_INTERFACE_ID,
WSK_EVENT_RECEIVE_FROM
};
ULONG NoTdi = WSK_TDI_BEHAVIOR_BYPASS_TDI;
UNREFERENCED_PARAMETER(TcpCallbacks);
UNREFERENCED_PARAMETER(Config);
if (NewDataPath == NULL) {
Status = QUIC_STATUS_INVALID_PARAMETER;
Datapath = NULL;
goto Exit;
}
if (UdpCallbacks != NULL) {
if (UdpCallbacks->Receive == NULL || UdpCallbacks->Unreachable == NULL) {
Status = QUIC_STATUS_INVALID_PARAMETER;
Datapath = NULL;
goto Exit;
}
}
DatapathLength =
sizeof(CXPLAT_DATAPATH) +
CxPlatProcMaxCount() * sizeof(CXPLAT_DATAPATH_PROC_CONTEXT);
Datapath = CXPLAT_ALLOC_NONPAGED(DatapathLength, QUIC_POOL_DATAPATH);
if (Datapath == NULL) {
QuicTraceEvent(
AllocFailure,
"Allocation of '%s' failed. (%llu bytes)",
"CXPLAT_DATAPATH",
DatapathLength);
Status = QUIC_STATUS_OUT_OF_MEMORY;
goto Exit;
}
RtlZeroMemory(Datapath, DatapathLength);
if (UdpCallbacks) {
Datapath->UdpHandlers = *UdpCallbacks;
}
Datapath->ClientRecvContextLength = ClientRecvContextLength;
Datapath->ProcCount = (uint32_t)CxPlatProcMaxCount();
Datapath->WskDispatch.WskReceiveFromEvent = CxPlatDataPathSocketReceive;
Datapath->DatagramStride =
ALIGN_UP(
sizeof(CXPLAT_RECV_DATA) +
sizeof(CXPLAT_DATAPATH_INTERNAL_RECV_BUFFER_CONTEXT) +
ClientRecvContextLength,
PVOID);
uint32_t RecvDatagramLength =
sizeof(CXPLAT_DATAPATH_INTERNAL_RECV_CONTEXT) +
Datapath->DatagramStride;
uint32_t UroDatagramLength =
sizeof(CXPLAT_DATAPATH_INTERNAL_RECV_CONTEXT) +
URO_MAX_DATAGRAMS_PER_INDICATION * Datapath->DatagramStride;
for (uint32_t i = 0; i < Datapath->ProcCount; i++) {
CxPlatPoolInitialize(
FALSE,
sizeof(CXPLAT_SEND_DATA),
QUIC_POOL_PLATFORM_SENDCTX,
&Datapath->ProcContexts[i].SendDataPool);
QuicSendBufferPoolInitialize(
sizeof(CXPLAT_DATAPATH_SEND_BUFFER) + MAX_UDP_PAYLOAD_LENGTH,
QUIC_POOL_DATA,
&Datapath->ProcContexts[i].SendBufferPool);
QuicSendBufferPoolInitialize(
sizeof(CXPLAT_DATAPATH_SEND_BUFFER) + CXPLAT_LARGE_SEND_BUFFER_SIZE,
QUIC_POOL_DATA,
&Datapath->ProcContexts[i].LargeSendBufferPool);
CxPlatPoolInitialize(
FALSE,
RecvDatagramLength,
QUIC_POOL_DATA,
&Datapath->ProcContexts[i].RecvDatagramPools[0]);
CxPlatPoolInitialize(
FALSE,
UroDatagramLength,
QUIC_POOL_DATA,
&Datapath->ProcContexts[i].RecvDatagramPools[1]);
CxPlatPoolInitialize(
FALSE,
4096,
QUIC_POOL_DATA,
&Datapath->ProcContexts[i].RecvBufferPools[0]);
CxPlatPoolInitialize(
FALSE,
65536,
QUIC_POOL_DATA,
&Datapath->ProcContexts[i].RecvBufferPools[1]);
Datapath->ProcContexts[i].OutstandingPendingBytes = 0;
}
Status = WskRegister(&WskClientNpi, &Datapath->WskRegistration);
if (QUIC_FAILED(Status)) {
QuicTraceEvent(
LibraryErrorStatus,
"[ lib] ERROR, %u, %s.",
Status,
"WskRegister");
goto Error;
}
WskRegistered = TRUE;
//
// Capture the WSK Provider NPI. If WSK subsystem is not ready yet,
// wait until it becomes ready.
//
Status =
WskCaptureProviderNPI(
&Datapath->WskRegistration,
WSK_INFINITE_WAIT,
&Datapath->WskProviderNpi);
if (QUIC_FAILED(Status)) {
QuicTraceEvent(
LibraryErrorStatus,
"[ lib] ERROR, %u, %s.",
Status,
"WskCaptureProviderNPI");
goto Error;
}
Status =
Datapath->WskProviderNpi.Dispatch->
WskControlClient(
Datapath->WskProviderNpi.Client,
WSK_TDI_BEHAVIOR,
sizeof(NoTdi),
&NoTdi,
0,
NULL,
NULL,
NULL);
if (QUIC_FAILED(Status)) {
QuicTraceEvent(
LibraryErrorStatus,
"[ lib] ERROR, %u, %s.",
Status,
"WskControlClient WSK_TDI_BEHAVIOR");
// We don't "goto Error;" here, because MSDN says that this may be removed
// in the future, at which point it presumably won't be needed.
}
Status =
Datapath->WskProviderNpi.Dispatch->
WskControlClient(
Datapath->WskProviderNpi.Client,
WSK_SET_STATIC_EVENT_CALLBACKS,
sizeof(CallbackControl),
&CallbackControl,
0,
NULL,
NULL,
NULL);