-
Notifications
You must be signed in to change notification settings - Fork 14
/
send.c
1734 lines (1287 loc) · 45.7 KB
/
send.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) 1989 Microsoft Corporation
Module Name:
send.c
Abstract:
This module contains the code for passing on send IRPs to
TDI providers.
Author:
David Treadwell (davidtr) 13-Mar-1992
Revision History:
--*/
#include "afdp.h"
VOID
AfdCancelSend (
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
);
NTSTATUS
AfdRestartSend (
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp,
IN PVOID Context
);
NTSTATUS
AfdRestartSendConnDatagram (
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp,
IN PVOID Context
);
NTSTATUS
AfdRestartSendDatagram (
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp,
IN PVOID Context
);
typedef struct _AFD_SEND_CONN_DATAGRAM_CONTEXT {
PAFD_ENDPOINT Endpoint;
TDI_CONNECTION_INFORMATION ConnectionInformation;
} AFD_SEND_CONN_DATAGRAM_CONTEXT, *PAFD_SEND_CONN_DATAGRAM_CONTEXT;
#ifdef ALLOC_PRAGMA
#pragma alloc_text( PAGEAFD, AfdSend )
#pragma alloc_text( PAGEAFD, AfdSendDatagram )
#pragma alloc_text( PAGEAFD, AfdCancelSend )
#pragma alloc_text( PAGEAFD, AfdRestartSend )
#pragma alloc_text( PAGEAFD, AfdRestartBufferSend )
#pragma alloc_text( PAGEAFD, AfdRestartSendConnDatagram )
#pragma alloc_text( PAGEAFD, AfdRestartSendDatagram )
#pragma alloc_text( PAGEAFD, AfdSendPossibleEventHandler )
#endif
//
// Macros to make the send restart code more maintainable.
//
#define AfdRestartSendInfo DeviceIoControl
#define AfdMdlChain Type3InputBuffer
#define AfdSendFlags InputBufferLength
#define AfdOriginalLength OutputBufferLength
#define AfdCurrentLength IoControlCode
NTSTATUS
AfdSend (
IN PIRP Irp,
IN PIO_STACK_LOCATION IrpSp
)
{
NTSTATUS status;
PAFD_ENDPOINT endpoint;
ULONG sendLength;
PAFD_CONNECTION connection;
BOOLEAN doSendBufferring;
PAFD_BUFFER afdBuffer;
ULONG sendFlags;
ULONG afdFlags;
BOOLEAN pendedIrp = FALSE;
PAFD_SEND_INFO sendInfo;
//
// Make sure that the endpoint is in the correct state.
//
endpoint = IrpSp->FileObject->FsContext;
ASSERT( IS_AFD_ENDPOINT_TYPE( endpoint ) );
if ( endpoint->State != AfdEndpointStateConnected ) {
status = STATUS_INVALID_CONNECTION;
goto complete;
}
//
// If send has been shut down on this endpoint, fail. We need to be
// careful about what error code we return here: if the connection
// has been aborted, be sure to return the apprpriate error code.
//
if ( (endpoint->DisconnectMode & AFD_PARTIAL_DISCONNECT_SEND) != 0 ) {
if ( (endpoint->DisconnectMode & AFD_ABORTIVE_DISCONNECT) != 0 ) {
status = STATUS_LOCAL_DISCONNECT;
} else {
status = STATUS_PIPE_DISCONNECTED;
}
goto complete;
}
//
// Set up the IRP on the assumption that it will complete successfully.
//
Irp->IoStatus.Status = STATUS_SUCCESS;
//
// If this is an IOCTL_AFD_SEND, then grab the parameters from the
// supplied AFD_SEND_INFO structure, build an MDL chain describing
// the WSABUF array, and attach the MDL chain to the IRP.
//
// If this is an IRP_MJ_WRITE IRP, just grab the length from the IRP
// and set the flags to zero.
//
if( IrpSp->MajorFunction == IRP_MJ_DEVICE_CONTROL ) {
//
// Sanity check.
//
ASSERT( IrpSp->Parameters.DeviceIoControl.IoControlCode == IOCTL_AFD_SEND );
if( IrpSp->Parameters.DeviceIoControl.InputBufferLength >=
sizeof(*sendInfo) ) {
try {
//
// Probe the input structure.
//
sendInfo = IrpSp->Parameters.DeviceIoControl.Type3InputBuffer;
if( Irp->RequestorMode != KernelMode ) {
ProbeForRead(
sendInfo,
sizeof(*sendInfo),
sizeof(ULONG)
);
}
//
// Snag the send flags.
//
sendFlags = sendInfo->TdiFlags;
afdFlags = sendInfo->AfdFlags;
//
// Validate the WSABUF parameters.
//
if( sendInfo->BufferArray != NULL &&
sendInfo->BufferCount > 0 ) {
//
// Create the MDL chain describing the WSABUF array.
//
status = AfdAllocateMdlChain(
Irp,
sendInfo->BufferArray,
sendInfo->BufferCount,
IoReadAccess,
&sendLength
);
} else {
//
// Invalid BufferArray or BufferCount fields.
//
status = STATUS_INVALID_PARAMETER;
}
} except( EXCEPTION_EXECUTE_HANDLER ) {
//
// Exception accessing input structure.
//
status = GetExceptionCode();
}
} else {
//
// Invalid input buffer length.
//
status = STATUS_INVALID_PARAMETER;
}
if( !NT_SUCCESS(status) ) {
goto complete;
}
} else {
ASSERT( IrpSp->MajorFunction == IRP_MJ_WRITE );
sendFlags = 0;
afdFlags = AFD_OVERLAPPED;
sendLength = IrpSp->Parameters.Write.Length;
}
//
// AfdSend() will either complete fully or will fail.
//
Irp->IoStatus.Information = sendLength;
//
// Setup for possible restart if the transport completes
// the send partially.
//
IrpSp->Parameters.AfdRestartSendInfo.AfdMdlChain = Irp->MdlAddress;
IrpSp->Parameters.AfdRestartSendInfo.AfdSendFlags = sendFlags;
IrpSp->Parameters.AfdRestartSendInfo.AfdOriginalLength = sendLength;
IrpSp->Parameters.AfdRestartSendInfo.AfdCurrentLength = sendLength;
//
// Buffer sends if the TDI provider does not buffer.
//
if ( endpoint->TdiBufferring ) {
doSendBufferring = FALSE;
//
// If this is a nonblocking endpoint, set the TDI nonblocking
// send flag so that the request will fail if the send cannot be
// performed immediately.
//
if ( endpoint->NonBlocking ) {
sendFlags |= TDI_SEND_NON_BLOCKING;
}
} else {
doSendBufferring = TRUE;
}
//
// If this is a datagram endpoint, format up a send datagram request
// and pass it on to the TDI provider.
//
if ( IS_DGRAM_ENDPOINT(endpoint) ) {
PAFD_SEND_CONN_DATAGRAM_CONTEXT context;
//
// It is illegal to send expedited data on a datagram socket.
//
if ( (sendFlags & TDI_SEND_EXPEDITED) != 0 ) {
status = STATUS_NOT_SUPPORTED;
goto complete;
}
//
// Allocate space to hold the connection information structure
// we'll use on input.
//
context = AFD_ALLOCATE_POOL(
NonPagedPool,
sizeof(*context),
AFD_TDI_POOL_TAG
);
if ( context == NULL ) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto complete;
}
context->Endpoint = endpoint;
context->ConnectionInformation.UserDataLength = 0;
context->ConnectionInformation.UserData = NULL;
context->ConnectionInformation.OptionsLength = 0;
context->ConnectionInformation.Options = NULL;
context->ConnectionInformation.RemoteAddressLength =
endpoint->Common.Datagram.RemoteAddressLength;
context->ConnectionInformation.RemoteAddress =
endpoint->Common.Datagram.RemoteAddress;
//
// Build a send datagram request.
//
TdiBuildSendDatagram(
Irp,
endpoint->AddressDeviceObject,
endpoint->AddressFileObject,
AfdRestartSendConnDatagram,
context,
Irp->MdlAddress,
sendLength,
&context->ConnectionInformation
);
//
// Call the transport to actually perform the send operation.
//
return AfdIoCallDriver(
endpoint,
endpoint->AddressDeviceObject,
Irp
);
}
//
// Get a pointer to the relevent AFD connection structure.
//
connection = AFD_CONNECTION_FROM_ENDPOINT( endpoint );
ASSERT( connection != NULL );
ASSERT( connection->Type == AfdBlockTypeConnection );
ASSERT( !connection->CleanupBegun );
//
// If the connection has been aborted, do not pend the IRP.
//
if ( connection->AbortIndicated ) {
status = STATUS_CONNECTION_RESET;
goto complete;
}
//
// If we need to buffer the send, do so.
//
if ( doSendBufferring && connection->MaxBufferredSendCount != 0 ) {
KIRQL cancelIrql;
KIRQL oldIrql;
ULONG bytesCopied;
ASSERT( !endpoint->TdiBufferring );
ASSERT( !connection->TdiBufferring );
//
// First make sure that we don't have too many bytes of send
// data already outstanding and that someone else isn't already
// in the process of completing pended send IRPs. We can't
// issue the send here if someone else is completing pended
// sends because we have to preserve ordering of the sends.
//
// Note that we'll give the send data to the TDI provider even
// if we have exceeded our send buffer limits, but that we don't
// complete the user's IRP until some send buffer space has
// freed up. This effects flow control by blocking the user's
// thread while ensuring that the TDI provider always has lots
// of data available to be sent.
//
IoAcquireCancelSpinLock( &cancelIrql );
AfdAcquireSpinLock( &endpoint->SpinLock, &oldIrql );
if ( connection->VcBufferredSendBytes >= connection->MaxBufferredSendBytes
||
connection->VcBufferredSendCount >= connection->MaxBufferredSendCount
) {
//
// There is already as much send data bufferred on the
// connection as is allowed. If this is a nonblocking
// endpoint and this is not an overlapped operation, fail the
// request.
//
if ( endpoint->NonBlocking && !( afdFlags & AFD_OVERLAPPED ) ) {
//
// Enable the send event.
//
endpoint->EventsActive &= ~AFD_POLL_SEND;
IF_DEBUG(EVENT_SELECT) {
KdPrint((
"AfdSend: Endp %08lX, Active %08lX\n",
endpoint,
endpoint->EventsActive
));
}
AfdReleaseSpinLock( &endpoint->SpinLock, oldIrql );
IoReleaseCancelSpinLock( cancelIrql );
status = STATUS_DEVICE_NOT_READY;
goto complete;
}
//
// We're going to have to pend the request here in AFD.
// Place the IRP on the connection's list of pended send
// IRPs and mark the IRP as pended.
//
InsertTailList(
&connection->VcSendIrpListHead,
&Irp->Tail.Overlay.ListEntry
);
//
// Set up the cancellation routine in the IRP. If the IRP
// has already been cancelled, just call the cancellation
// routine here.
//
if ( Irp->Cancel ) {
AfdReleaseSpinLock( &endpoint->SpinLock, oldIrql );
Irp->CancelIrql = cancelIrql;
AfdCancelSend( IrpSp->DeviceObject, Irp );
return STATUS_CANCELLED;
}
IoSetCancelRoutine( Irp, AfdCancelSend );
//
// Remember that we pended the IRP so that we do not
// complete it later.
//
pendedIrp = TRUE;
}
//
// We don't need the IO cancel spin lock any more, so release it
// while being careful to swap the IRQLs. We have to hold on to
// the endpoint spin lock until after we have copied the data
// out of the IRP in order to prevent the IRP from being
// completed in our send completion routine.
//
IoReleaseCancelSpinLock( oldIrql );
oldIrql = cancelIrql;
//
// Next get an AFD buffer structure that contains an IRP and a
// buffer to hold the data.
//
afdBuffer = AfdGetBuffer( sendLength, 0 );
if ( afdBuffer == NULL && sendLength > AfdBufferLengthForOnePage ) {
IF_DEBUG(SEND) {
KdPrint(( "AfdSend: cannot allocate %lu, trying chain\n",
sendLength ));
}
afdBuffer = AfdGetBufferChain( sendLength );
}
if ( afdBuffer == NULL ) {
if ( pendedIrp ) {
RemoveEntryList( &Irp->Tail.Overlay.ListEntry );
}
AfdReleaseSpinLock( &endpoint->SpinLock, oldIrql );
IoAcquireCancelSpinLock( &cancelIrql );
IoSetCancelRoutine( Irp, NULL );
IoReleaseCancelSpinLock( cancelIrql );
status = STATUS_INSUFFICIENT_RESOURCES;
goto complete;
}
//
// If we're pending the user's IRP, then mark it so.
//
if( pendedIrp ) {
IoMarkIrpPending( Irp );
}
//
// Update count of send bytes pending on the connection.
//
connection->VcBufferredSendBytes += sendLength;
connection->VcBufferredSendCount += 1;
//
// We have to rebuild the MDL in the AFD buffer structure to
// represent exactly the number of bytes we're going to be
// sending.
//
if( afdBuffer->NextBuffer == NULL ) {
afdBuffer->Mdl->ByteCount = sendLength;
SET_CHAIN_LENGTH( afdBuffer, sendLength );
}
//
// Remember the endpoint in the AFD buffer structure. We need
// this in order to access the endpoint in the restart routine.
//
afdBuffer->Context = endpoint;
//
// Copy the user's data into the AFD buffer. If the MDL in the
// IRP is NULL, then don't bother doing the copy--this is a
// send of length 0.
//
if ( Irp->MdlAddress != NULL ) {
TdiCopyMdlToBuffer(
Irp->MdlAddress,
0,
afdBuffer->Buffer,
0,
sendLength,
&bytesCopied
);
ASSERT( bytesCopied == sendLength );
//
// Now that we've capture the send data, we can free the
// MDL chain associated with the incoming IRP.
//
// !!! Is this really a wise thing to do?
//
AfdDestroyMdlChain( Irp );
} else {
ASSERT( IrpSp->Parameters.AfdRestartSendInfo.AfdOriginalLength == 0 );
}
//
// Release the endpoint lock AFTER we are 100% done with the IRP
// (if pended). This prevents the user's pended IRP from being
// completed in our send completion routine while we're still
// looking at it.
//
AfdReleaseSpinLock( &endpoint->SpinLock, oldIrql );
//
// Use the IRP in the AFD buffer structure to give to the TDI
// provider. Build the TDI send request.
//
TdiBuildSend(
afdBuffer->Irp,
connection->DeviceObject,
connection->FileObject,
AfdRestartBufferSend,
afdBuffer,
afdBuffer->Mdl,
sendFlags,
sendLength
);
//
// Add a reference to the connection object since the send
// request will complete asynchronously.
//
REFERENCE_CONNECTION2( connection, (PVOID)(0xafdafd02), afdBuffer->Irp );
//
// Call the transport to actually perform the send.
//
status = IoCallDriver( connection->DeviceObject, afdBuffer->Irp );
//
// Complete the user's IRP as appropriate if we didn't already
// pend it. Note that we change the status code from what was
// returned by the TDI provider into STATUS_SUCCESS. This is
// because we don't want to complete the IRP with STATUS_PENDING
// etc.
//
if ( NT_SUCCESS(status) && !pendedIrp ) {
ASSERT( Irp->IoStatus.Information == sendLength );
IoCompleteRequest( Irp, AfdPriorityBoost );
return STATUS_SUCCESS;
}
//
// If we pended the user's IRP, return appropriate status. Note
// that in this case we ignore an IoCallDriver() failure.
//
if ( pendedIrp ) {
return STATUS_PENDING;
}
//
// The send request to the TDI provider failed immediately.
// Propagate the failure to the user.
//
goto complete;
} else {
//
// Build the TDI send request.
//
connection = AFD_CONNECTION_FROM_ENDPOINT( endpoint );
ASSERT( connection != NULL );
TdiBuildSend(
Irp,
connection->DeviceObject,
connection->FileObject,
AfdRestartSend,
endpoint,
Irp->MdlAddress,
sendFlags,
sendLength
);
//
// Add a reference to the connection object since the send
// request will complete asynchronously.
//
REFERENCE_CONNECTION2( connection, (PVOID)(0xafdafd03), Irp );
//
// Call the transport to actually perform the send.
//
return AfdIoCallDriver( endpoint, connection->DeviceObject, Irp );
}
complete:
Irp->IoStatus.Information = 0;
Irp->IoStatus.Status = status;
IoCompleteRequest( Irp, AfdPriorityBoost );
return status;
} // AfdSend
NTSTATUS
AfdRestartSend (
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp,
IN PVOID Context
)
{
PIO_STACK_LOCATION irpSp;
PAFD_ENDPOINT endpoint = Context;
PAFD_CONNECTION connection;
PMDL mdlChain;
PMDL nextMdl;
NTSTATUS status;
PIRP disconnectIrp;
KIRQL oldIrql;
ASSERT( endpoint != NULL );
ASSERT( endpoint->Type == AfdBlockTypeVcConnecting );
connection = endpoint->Common.VcConnecting.Connection;
ASSERT( connection != NULL );
ASSERT( connection->Type == AfdBlockTypeConnection );
IF_DEBUG(SEND) {
KdPrint(( "AfdRestartSend: send completed for IRP %lx, endpoint %lx, "
"status = %X\n",
Irp, Context, Irp->IoStatus.Status ));
}
//
// If the request failed indicating that the send would have blocked,
// and the client issues a nonblocking send, remember that nonblocking
// sends won't work until we get a send possible indication. This
// is required for write polls to work correctly.
//
// If the status code is STATUS_REQUEST_NOT_ACCEPTED, then the
// transport does not want us to update our internal variable that
// remembers that nonblocking sends are possible. The transport
// will tell us when sends are or are not possible.
//
// !!! should we also say that nonblocking sends are not possible if
// a send is completed with fewer bytes than were requested?
if ( Irp->IoStatus.Status == STATUS_DEVICE_NOT_READY ) {
//
// Reenable the send event.
//
AfdAcquireSpinLock( &endpoint->SpinLock, &oldIrql );
endpoint->EventsActive &= ~AFD_POLL_SEND;
IF_DEBUG(EVENT_SELECT) {
KdPrint((
"AfdRestartSend: Endp %08lX, Active %08lX\n",
endpoint,
endpoint->EventsActive
));
}
AfdReleaseSpinLock( &endpoint->SpinLock, oldIrql );
connection->VcNonBlockingSendPossible = FALSE;
}
//
// If this is a send IRP on a nonblocking endpoint and fewer bytes
// were actually sent than were requested to be sent, reissue
// another send for the remaining buffer space.
//
irpSp = IoGetCurrentIrpStackLocation( Irp );
if ( !endpoint->NonBlocking && NT_SUCCESS(Irp->IoStatus.Status) &&
Irp->IoStatus.Information <
irpSp->Parameters.AfdRestartSendInfo.AfdCurrentLength ) {
ASSERT( Irp->MdlAddress != NULL );
//
// Advance the MDL chain by the number of bytes actually sent.
//
mdlChain = AfdAdvanceMdlChain(
Irp->MdlAddress,
Irp->IoStatus.Information
);
//
// If the first MDL referenced by the IRP has the MDL_PARTIAL
// flag set, then it's one of ours from a previous partial
// send and must be freed.
//
if ( Irp->MdlAddress->MdlFlags & MDL_PARTIAL ) {
nextMdl = Irp->MdlAddress->Next;
IoFreeMdl( Irp->MdlAddress );
Irp->MdlAddress = nextMdl;
}
if ( mdlChain != NULL ) {
Irp->MdlAddress = mdlChain;
//
// Update our restart info.
//
irpSp->Parameters.AfdRestartSendInfo.AfdCurrentLength -=
Irp->IoStatus.Information;
//
// Reissue the send.
//
TdiBuildSend(
Irp,
connection->FileObject->DeviceObject,
connection->FileObject,
AfdRestartSend,
endpoint,
Irp->MdlAddress,
irpSp->Parameters.AfdRestartSendInfo.AfdSendFlags,
irpSp->Parameters.AfdRestartSendInfo.AfdCurrentLength
);
status = AfdIoCallDriver(
endpoint,
connection->FileObject->DeviceObject,
Irp
);
IF_DEBUG(SEND) {
if ( !NT_SUCCESS(status) ) {
KdPrint((
"AfdRestartSend: AfdIoCallDriver returned %lx\n",
status
));
}
}
return STATUS_MORE_PROCESSING_REQUIRED;
} else {
//
// Bad news, could not allocate a new partial MDL.
//
Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
disconnectIrp = connection->VcDisconnectIrp;
connection->VcDisconnectIrp = NULL;
AfdBeginAbort( connection );
//
// If there was a disconnect IRP, rather than just freeing it
// give it to the transport. This will cause the correct cleanup
// stuff (dereference objects, free IRP and disconnect context)
// to occur. Note that we do this AFTER starting to abort the
// connection so that we do not confuse the other side.
//
if ( disconnectIrp != NULL ) {
IoCallDriver( connection->FileObject->DeviceObject, disconnectIrp );
}
AfdDeleteConnectedReference( connection, FALSE );
//
// Remove the reference added just before calling the transport.
//
DEREFERENCE_CONNECTION2( connection, (PVOID)(0xafd11105), Irp );
return STATUS_MORE_PROCESSING_REQUIRED;
}
}
//
// If the first MDL referenced by the IRP has the MDL_PARTIAL
// flag set, then it's one of ours from a previous partial
// send and must be freed.
//
if( Irp->MdlAddress != NULL &&
Irp->MdlAddress->MdlFlags & MDL_PARTIAL ) {
IoFreeMdl( Irp->MdlAddress );
}
//
// Restore the IRP to its former glory before completing it.
//
Irp->MdlAddress = irpSp->Parameters.AfdRestartSendInfo.AfdMdlChain;
Irp->IoStatus.Information = irpSp->Parameters.AfdRestartSendInfo.AfdOriginalLength;
AfdCompleteOutstandingIrp( endpoint, Irp );
//
// If pending has be returned for this irp then mark the current
// stack as pending.
//
if ( Irp->PendingReturned ) {
IoMarkIrpPending(Irp);
}
//
// Remove the reference added just before calling the transport.
//
DEREFERENCE_CONNECTION2( connection, (PVOID)(0xafd11100), Irp );
return STATUS_SUCCESS;
} // AfdRestartSend
NTSTATUS
AfdRestartBufferSend (
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp,
IN PVOID Context
)
{
PAFD_BUFFER afdBuffer = Context;
PAFD_ENDPOINT endpoint;
PAFD_CONNECTION connection;
KIRQL cancelIrql;
KIRQL oldIrql;
PLIST_ENTRY listEntry;
PIRP irp;
ULONG sendCount;
PIRP disconnectIrp;
LIST_ENTRY irpsToComplete;
endpoint = afdBuffer->Context;
ASSERT( endpoint != NULL );
ASSERT( endpoint->Type == AfdBlockTypeVcConnecting );
ASSERT( !endpoint->TdiBufferring );
connection = endpoint->Common.VcConnecting.Connection;
ASSERT( connection != NULL );
ASSERT( connection->Type == AfdBlockTypeConnection );
ASSERT( connection->ReferenceCount > 0 );
IF_DEBUG(SEND) {
KdPrint(( "AfdRestartBufferSend: send completed for IRP %lx, endpoint %lx, "
"status = %X\n",
Irp, Context, Irp->IoStatus.Status ));
}
UPDATE_CONN( connection, Irp->IoStatus.Status );
//
// Make a special test here to see whether this send was from the
// TransmitFile fast path with MDL file I/O. If there is a file
// object pointer in the AFD buffer structure, then we need to
// return the MDL chain to the cache manager and derefence the file
// object.
//
if ( afdBuffer->FileObject != NULL ) {
ASSERT( afdBuffer->NextBuffer == NULL );
ASSERT( afdBuffer->Mdl->Next != NULL );
AfdMdlReadComplete(
afdBuffer->FileObject,
afdBuffer->Mdl->Next,
afdBuffer->FileOffset,
afdBuffer->ReadLength
);
afdBuffer->Mdl->Next = NULL;
ObDereferenceObject( afdBuffer->FileObject );
afdBuffer->FileObject = NULL;
}
//
// Update the count of send bytes outstanding on the connection.
// Note that we must do this BEFORE we check to see whether there
// are any pended sends--otherwise, there is a timing window where
// a new send could come in, get pended, and we would not kick
// the sends here.
//
AfdAcquireSpinLock( &endpoint->SpinLock, &oldIrql );
ASSERT( connection->VcBufferredSendBytes >= Irp->IoStatus.Information );
ASSERT( (connection->VcBufferredSendCount & 0x8000) == 0 );
ASSERT( connection->VcBufferredSendCount != 0 );
connection->VcBufferredSendBytes -= Irp->IoStatus.Information;
connection->VcBufferredSendCount -= 1;