summaryrefslogtreecommitdiffstats
path: root/private/ntos/tdi/isn/ipx/loopback.c
blob: be44bae5b9351fa6976fdb8e10c9978f686bbfb6 (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
/*++

Copyright (c) 1989-1993  Microsoft Corporation

Module Name:

    loopback.c

Abstract:

    This module contains the routines to implement loopback

Author:

    Sanjay Anand (SanjayAn) 2/6/96

Environment:

    Kernel mode

Revision History:

--*/

#include "precomp.h"
#pragma hdrstop

//
// Global lock to control access to the Loopback queue
//
DEFINE_LOCK_STRUCTURE(LoopLock)

//
// Head and tail of the Loopback queue
//
PNDIS_PACKET    LoopXmitHead = (PNDIS_PACKET)NULL;
PNDIS_PACKET    LoopXmitTail = (PNDIS_PACKET)NULL;

CTEEvent        LoopXmitEvent;
BOOLEAN         LoopXmitRtnRunning = 0;

//
// MaximumPacket sized buffer to hold the lookahead data.
//
// ZZBUGBUG: In PnP this value can change
//
// PUCHAR   LookaheadBuffer=NULL;
#define LOOP_LOOKAHEAD_SIZE   128 + sizeof(IPX_HEADER) + 8 + 34


VOID
IpxDoLoopback(
    IN  CTEEvent    *Event,
    IN  PVOID       Context
    )
/*++

Routine Description:

    Does the actual loopback.

Arguments:

    Event - Pointer to event structure.

    Context - Pointer to ZZ

Return Value:

    None.

--*/
{
    PNDIS_PACKET    Packet;     // Pointer to packet being transmitted
    PNDIS_BUFFER    Buffer;     // Current NDIS buffer being processed.
    ULONG   TotalLength; // Total length of send.
    ULONG   LookaheadLength; // Bytes in lookahead.
    ULONG   Copied;     // Bytes copied so far.
    PUCHAR  CopyPtr;   // Pointer to buffer being copied into.
    PUCHAR  SrcPtr;    // Pointer to buffer being copied from.
    ULONG   SrcLength;  // Length of src buffer.
    BOOLEAN Rcvd = FALSE;
    PIPX_SEND_RESERVED Reserved;
    ULONG   MacSize;
    PNDIS_PACKET    *PacketPtr;
    UCHAR   LookaheadBuffer[LOOP_LOOKAHEAD_SIZE];

	IPX_DEFINE_LOCK_HANDLE(Handle)

    KIRQL   OldIrql;

	CTEAssert(KeGetCurrentIrql() < DISPATCH_LEVEL);

	//
	// Raise IRQL so we can acquire locks at DPC level in the receive code.
	// Also to be able to ReceiveIndicate at DPC
    //
    KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);

    IPX_GET_LOCK(&LoopLock, &Handle);

	if (LoopXmitRtnRunning) {
        IPX_FREE_LOCK(&LoopLock, Handle);
        KeLowerIrql(OldIrql);
		return;
	}

	LoopXmitRtnRunning = 1;

    for (;;) {

        //
        // Get the next packet from the list.
        //
        Packet = LoopXmitHead;

        if (Packet != (PNDIS_PACKET)NULL) {
            Reserved = (PIPX_SEND_RESERVED)(Packet->ProtocolReserved);
            LoopXmitHead = (PNDIS_PACKET)(Reserved->PaddingBuffer);
            IPX_FREE_LOCK(&LoopLock, Handle);
        } else {                            // Nothing left to do.
		    LoopXmitRtnRunning = 0;
            IPX_FREE_LOCK(&LoopLock, Handle);
            break;
        }

        //
        // We use the PaddingBuffer section as the next ptr.
        //
        Reserved->PaddingBuffer = NULL;

        IPX_DEBUG(LOOPB, ("Packet: %lx\n", Packet));

        NdisQueryPacket(Packet, NULL, NULL, &Buffer, &TotalLength);

        NdisQueryBuffer(Buffer, NULL, &MacSize);

        IPX_DEBUG(LOOPB, ("Buffer: %lx Totalpktlen: %lx MacSize: %lx\n", Buffer, TotalLength, MacSize));

        LookaheadLength = MIN(LOOP_LOOKAHEAD_SIZE, TotalLength);
        Copied = 0;
        CopyPtr = LookaheadBuffer;
        while (Copied < LookaheadLength) {
            ULONG    ThisCopy;   // Bytes to copy this time.

#ifdef  DBG
            if (!Buffer) {
                DbgBreakPoint();
                IPX_GET_LOCK(&LoopLock, &Handle);
                LoopXmitRtnRunning = 0;
                IPX_FREE_LOCK(&LoopLock, Handle);
                KeLowerIrql(OldIrql);
                return;
            }
#endif

            NdisQueryBuffer(Buffer, &SrcPtr, &SrcLength);
            ThisCopy = MIN(SrcLength, LookaheadLength - Copied);
            CTEMemCopy(CopyPtr, SrcPtr, ThisCopy);
            Copied += ThisCopy;
            CopyPtr += ThisCopy;
            NdisGetNextBuffer(Buffer, &Buffer);
        }

        Rcvd = TRUE;

#ifdef  BACK_FILL
        //
        // For Backfill packets, the MAC header is not yet set up; for others, it is the size
        // of the first MDL (17).
        //
        if ((Reserved->Identifier == IDENTIFIER_IPX) &&
            (Reserved->BackFill)) {
            MacSize = 0;
        }
#endif
        IpxReceiveIndication(   (NDIS_HANDLE)IPX_LOOPBACK_COOKIE,    // BindingContext
                                Packet,                 // ReceiveContext
                                (MacSize) ? LookaheadBuffer : NULL,        // HeaderBuffer
                                MacSize,        // HeaderBufferSize
                                LookaheadBuffer+MacSize,        // LookAheadBuffer
                                LookaheadLength-MacSize,        // LookAheadBufferSize
                                TotalLength-MacSize);           // PacketSize

        IpxSendComplete(Context, Packet, NDIS_STATUS_SUCCESS);

        //
		// Give other threads a chance to run.
		//
        KeLowerIrql(OldIrql);
        KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
        IPX_GET_LOCK(&LoopLock, &Handle);
    }

    if (Rcvd) {
        IpxReceiveComplete(Context);
	}

    KeLowerIrql(OldIrql);
}


VOID
IpxInitLoopback()
/*++

Routine Description:

    Initializes various loopback structures.

Arguments:

Return Value:

    None.

--*/
{
    CTEInitLock(&LoopLock);
    CTEInitEvent(&LoopXmitEvent, IpxDoLoopback);
    return;
}


VOID
IpxLoopbackEnque(
    IN PNDIS_PACKET Packet,
    IN PVOID    Context
    )

/*++

Routine Description:

    Enqueues a packet to the loopbackQ

Arguments:

    Packet - The packet to be enqueued.

    Context -  Pointer to the adapter corresp to the first binding.

Return Value:

    None.

--*/
{
    PIPX_SEND_RESERVED  Reserved = (PIPX_SEND_RESERVED)(Packet->ProtocolReserved);
	IPX_DEFINE_LOCK_HANDLE(LockHandle)

    //
    // We use the PaddingBuffer as the next ptr.
    //
    Reserved->PaddingBuffer = NULL;

    IPX_GET_LOCK(&LoopLock, &LockHandle);

    //
    // LoopbackQ is empty
    //
    if (LoopXmitHead == (PNDIS_PACKET)NULL) {
        LoopXmitHead = Packet;
    } else {
        Reserved = (PIPX_SEND_RESERVED)(LoopXmitTail->ProtocolReserved);
        (PNDIS_PACKET)(Reserved->PaddingBuffer) = Packet;
    }
    LoopXmitTail = Packet;

    IPX_DEBUG(LOOPB, ("Enqued packet: %lx, Reserved: %lx\n", Packet, Reserved));

    //
    // If this routine is not already running, schedule it as a work item.
    //
    if (!LoopXmitRtnRunning) {
        CTEScheduleEvent(&LoopXmitEvent, Context);
    }

    IPX_FREE_LOCK(&LoopLock, LockHandle);
}