summaryrefslogtreecommitdiffstats
path: root/private/ntos/tdi/st/rcveng.c
blob: 8e104a954538b91d63b6a2b399eb9136472340ad (plain) (blame)
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
/*++

Copyright (c) 1989-1993 Microsoft Corporation

Module Name:

    rcveng.c

Abstract:

    This module contains code that implements the receive engine for the
    Sample transport provider.

Environment:

    Kernel mode

Revision History:

--*/

#include "st.h"


VOID
ActivateReceive(
    PTP_CONNECTION Connection
    )

/*++

Routine Description:

    This routine activates the next TdiReceive request on the specified
    connection object if there is no active request on that connection
    already.  This allows the request to accept data on the connection.

Arguments:

    Connection - Pointer to a TP_CONNECTION object.

Return Value:

    none.

--*/

{
    KIRQL oldirql;
    PTP_REQUEST Request;

    //
    // The ACTIVE_RECEIVE bitflag will be set on the connection if
    // the receive-fields in the CONNECTION object are valid.  If
    // this flag is cleared, then we try to make the next TdiReceive
    // request in the ReceiveQueue the active request.
    //

    ACQUIRE_SPIN_LOCK (&Connection->SpinLock, &oldirql);
    if (!(Connection->Flags & CONNECTION_FLAGS_ACTIVE_RECEIVE)) {
        if (!IsListEmpty (&Connection->ReceiveQueue)) {

            //
            // Found a receive, so make it the active one.
            //

            Connection->Flags |= CONNECTION_FLAGS_ACTIVE_RECEIVE;

            Request = CONTAINING_RECORD (
                          Connection->ReceiveQueue.Flink,
                          TP_REQUEST,
                          Linkage);
            Connection->MessageBytesReceived = 0;
            Connection->MessageBytesAcked = 0;
            Connection->CurrentReceiveRequest = Request;
            Connection->CurrentReceiveMdl = Request->Buffer2;
            Connection->ReceiveLength = Request->Buffer2Length;
            Connection->ReceiveByteOffset = 0;
        }
    }

    RELEASE_SPIN_LOCK (&Connection->SpinLock, oldirql);

} /* ActivateReceive */


VOID
AwakenReceive(
    PTP_CONNECTION Connection
    )

/*++

Routine Description:

    This routine is called to reactivate a sleeping connection with the
    RECEIVE_WAKEUP bitflag set because data arrived for which no receive
    was available.  The caller has made a receive available at the connection,
    so here we activate the next receive, and send the appropriate protocol
    to restart the message at the first byte offset past the one received
    by the last receive.

Arguments:

    Connection - Pointer to a TP_CONNECTION object.

Return Value:

    none.

--*/

{
    KIRQL oldirql;

    //
    // If the RECEIVE_WAKEUP bitflag is set, then awaken the connection.
    //

    ACQUIRE_SPIN_LOCK (&Connection->SpinLock, &oldirql);
    if (Connection->Flags & CONNECTION_FLAGS_RECEIVE_WAKEUP) {
        if (Connection->ReceiveQueue.Flink != &Connection->ReceiveQueue) {
            Connection->Flags &= ~CONNECTION_FLAGS_RECEIVE_WAKEUP;

            //
            // Found a receive, so turn off the wakeup flag, and activate
            // the next receive.
            //

            RELEASE_SPIN_LOCK (&Connection->SpinLock, oldirql);
            ActivateReceive (Connection);

            return;
        }
    }
    RELEASE_SPIN_LOCK (&Connection->SpinLock, oldirql);
} /* AwakenReceive */


VOID
CompleteReceive(
    PTP_CONNECTION Connection,
    BOOLEAN EndOfRecord,
    KIRQL ConnectionIrql,
    KIRQL CancelIrql
    )

/*++

Routine Description:

    This routine is called by ProcessIncomingData when the current receive
    must be completed.  Depending on whether the current frame being
    processed is a DATA_FIRST_MIDDLE or DATA_ONLY_LAST, and also whether
    all of the data was processed, the EndOfRecord flag will be set accordingly
    by the caller to indicate that a message boundary was received.

    NOTE: This function is called with the connection and cancel
    IRQLs held, and returns with them released.

Arguments:

    Connection - Pointer to a TP_CONNECTION object.

    EndOfRecord - BOOLEAN set to true if TDI_END_OF_RECORD should be reported.

    ConnectionIrql - The IRQL at which the connection spinlock was acquired.

    CancelIrql - The IRQL at which the cancel spinlock was acquired.

Return Value:

    none.

--*/

{
    PLIST_ENTRY p;
    PTP_REQUEST Request;
    KIRQL oldirql = ConnectionIrql;
    KIRQL cancelirql = CancelIrql;
    ULONG BytesReceived;


    if (IsListEmpty (&Connection->ReceiveQueue)) {

        ASSERT ((Connection->Flags & CONNECTION_FLAGS_STOPPING) != 0);

        RELEASE_SPIN_LOCK (&Connection->SpinLock, oldirql);
        IoReleaseCancelSpinLock(cancelirql);
        return;
    }

    Connection->Flags &= ~CONNECTION_FLAGS_ACTIVE_RECEIVE;
    BytesReceived = Connection->MessageBytesReceived;


    //
    // Complete the TdiReceive request at the head of the
    // connection's ReceiveQueue.
    //

    p = RemoveHeadList (&Connection->ReceiveQueue);
    Request = CONTAINING_RECORD (p, TP_REQUEST, Linkage);

    Request->IoRequestPacket->CancelRoutine = (PDRIVER_CANCEL)NULL;

    RELEASE_SPIN_LOCK (&Connection->SpinLock, oldirql);
    IoReleaseCancelSpinLock(cancelirql);


    Request->Flags |= REQUEST_FLAGS_DELAY;

    StCompleteRequest(
        Request,
        EndOfRecord ? STATUS_SUCCESS : STATUS_BUFFER_OVERFLOW,
        BytesReceived);

} /* CompleteReceive */


VOID
StCancelReceive(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    )

/*++

Routine Description:

    This routine is called by the I/O system to cancel a receive.
    The receive is found on the connection's receive queue; if it
    is the current request it is cancelled and the connection
    goes into "cancelled receive" mode, otherwise it is cancelled
    silently.

    In "cancelled receive" mode the connection makes it appear to
    the remote the data is being received, but in fact it is not
    indicated to the transport or buffered on our end

    NOTE: This routine is called with the CancelSpinLock held and
    is responsible for releasing it.

Arguments:

    DeviceObject - Pointer to the device object for this driver.

    Irp - Pointer to the request packet representing the I/O request.

Return Value:

    none.

--*/

{
    KIRQL oldirql;
    PIO_STACK_LOCATION IrpSp;
    PTP_CONNECTION Connection;
    PTP_REQUEST Request;
    PLIST_ENTRY p;
    ULONG BytesReceived;
    BOOLEAN Found;

    UNREFERENCED_PARAMETER (DeviceObject);

    //
    // Get a pointer to the current stack location in the IRP.  This is where
    // the function codes and parameters are stored.
    //

    IrpSp = IoGetCurrentIrpStackLocation (Irp);

    ASSERT ((IrpSp->MajorFunction == IRP_MJ_INTERNAL_DEVICE_CONTROL) &&
            (IrpSp->MinorFunction == TDI_RECEIVE));

    Connection = IrpSp->FileObject->FsContext;

    //
    // Since this IRP is still in the cancellable state, we know
    // that the connection is still around (although it may be in
    // the process of being torn down).
    //

    //
    // See if this is the IRP for the current receive request.
    //

    ACQUIRE_SPIN_LOCK (&Connection->SpinLock, &oldirql);

    BytesReceived = Connection->MessageBytesReceived;

    p = Connection->ReceiveQueue.Flink;

    //
    // If there is a receive active, then see if this is it.
    //

    if ((Connection->Flags & CONNECTION_FLAGS_ACTIVE_RECEIVE) != 0) {

        Request = CONTAINING_RECORD (p, TP_REQUEST, Linkage);

        if (Request->IoRequestPacket == Irp) {

            //
            // yes, it is the active receive. Turn on the RCV_CANCELLED
            // bit instructing the connection to drop the rest of the
            // data received (until the DOL comes in).
            //

            Connection->Flags2 |= CONNECTION_FLAGS2_RCV_CANCELLED;
            Connection->Flags &= ~CONNECTION_FLAGS_ACTIVE_RECEIVE;

            (VOID)RemoveHeadList (&Connection->ReceiveQueue);

            RELEASE_SPIN_LOCK (&Connection->SpinLock, oldirql);
            IoReleaseCancelSpinLock (Irp->CancelIrql);

            //
            // The following dereference will complete the I/O, provided removes
            // the last reference on the request object.  The I/O will complete
            // with the status and information stored in the Irp.  Therefore,
            // we set those values here before the dereference.
            //

            StCompleteRequest (Request, STATUS_CANCELLED, 0);
            return;

        }

    }


    //
    // If we fall through to here, the IRP was not the active receive.
    // Scan through the list, looking for this IRP.
    //

    Found = FALSE;

    while (p != &Connection->ReceiveQueue) {

        Request = CONTAINING_RECORD (p, TP_REQUEST, Linkage);
        if (Request->IoRequestPacket == Irp) {

            //
            // Found it, remove it from the list here.
            //

            RemoveEntryList (p);
            Found = TRUE;

            RELEASE_SPIN_LOCK (&Connection->SpinLock, oldirql);
            IoReleaseCancelSpinLock (Irp->CancelIrql);

            //
            // The following dereference will complete the I/O, provided removes
            // the last reference on the request object.  The I/O will complete
            // with the status and information stored in the Irp.  Therefore,
            // we set those values here before the dereference.
            //

            StCompleteRequest (Request, STATUS_CANCELLED, 0);
            break;

        }

        p = p->Flink;

    }

    if (!Found) {

        //
        // We didn't find it!
        //

        RELEASE_SPIN_LOCK (&Connection->SpinLock, oldirql);
        IoReleaseCancelSpinLock (Irp->CancelIrql);
    }

}