summaryrefslogblamecommitdiffstats
path: root/private/ntos/tdi/nbf/linktree.c
blob: 5c62fd8b803555e40e762ed805ee1ac2841f2a24 (plain) (tree)
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
























































































































































































































































































































































































































































































































































                                                                                                                   
/*++

Copyright (c) 1990, 1991  Microsoft Corporation

Module Name:

    linktree.c

Abstract:

    This module contains code which implements the management of the link
    splay tree. This splay tree is maintained to minimize the lookup time
    needed with each individual packet that comes in. To this end, we create a
    ULARGE_INTEGER that contains the transport address of the remote and
    do a ULARGE_INTEGER comaprison of the addresses (rather than comparing
    the bytes 1 by 1). Assuming that the ULARGE_INTEGER comparison routines are
    optimized for the hardware on the machine, this should be as fast as or
    faster than comparing bytes.

    DEBUG: there is currently code in the comparison routines that will let
           me fine-tune the search and ordering algorithm as we gain more
           experience with it.

Author:

    David Beaver (dbeaver) 1-July-1991

Environment:

    Kernel mode

Revision History:


--*/

#include "precomp.h"
#pragma hdrstop


NTSTATUS
NbfAddLinkToTree(
    IN PDEVICE_CONTEXT DeviceContext,
    IN PTP_LINK Link
    )

/*++

Routine Description:

    This routine adds a link to the tree of links maintained for this device.
    Note that since this routine needs to modify the link tree, it is called
    in the context of a deferred processing routine, and must have exclusive
    access to the tree. The spinlock is taken by the routine that calls this
    one, as this operation must be atomic in the eyes of the rest of NBF.
    Note further that this routine insists that there not be a link with this
    address in the tree.

    As the final operation of this insertion, the splay tree is balanced.

Arguments:

    Link - Pointer to a transport link object.

Return Value:

    STATUS_SUCCESS if the link is successfully added,
    STATUS_DRIVER_INTERNAL_ERROR if there was a problem adding
        the link (implying the tree was in a bad state).

--*/
{
    PTP_LINK treeLink;
    PRTL_SPLAY_LINKS linkLink;

    //
    // initialize the link and check for the trivial case.
    //

    RtlInitializeSplayLinks (Link);
    linkLink = DeviceContext->LinkTreeRoot;
    if (linkLink == NULL) { // null tree, make this the parent
        DeviceContext->LinkTreeRoot = (PRTL_SPLAY_LINKS)Link;
        DeviceContext->LinkTreeElements++;
        DeviceContext->LastLink = Link;
        return STATUS_SUCCESS;
    }

    //
    // Wasn't a null tree, so set up for the addition
    //

    treeLink = (PTP_LINK) linkLink;

    IF_NBFDBG(NBF_DEBUG_LINKTREE) {
        NbfPrint1 ("NbfAddLinkToTree: starting insert, Elements: %ld \n",DeviceContext->LinkTreeElements);
    }

    //
    // find the proper spot to put this link.
    //

    do {
        IF_NBFDBG(NBF_DEBUG_LINKTREE) {
            NbfPrint3 ("NbfAddLinkToTree: searching, Link: %lx LC: %lx RC: %lx\n",
                linkLink, RtlLeftChild (linkLink), RtlRightChild (linkLink));
        }

        //
        // Bad news == means we already have this link, someone is messed up.
        // it's possible to be adding and deleting things at the same time;
        // that's
        //

        if ((treeLink->MagicAddress).QuadPart == (Link->MagicAddress).QuadPart) {

            //
            // First make sure we don't have the splay tree in a loop.
            //

            ASSERT (treeLink != Link);

            //
            // This link is already in the tree. This is OK if it is
            // due to be deleted; we can just do the delete right now,
            // since AddLinkToTree is only called from the deferred
            // timer routine.
            //

            if (treeLink->DeferredFlags & LINK_FLAGS_DEFERRED_DELETE) {

                //
                // It will be in the deferred list. We remove it,
                // we don't worry about LinkDeferredActive since
                // the timeout routine that is calling us handles
                // that.
                //

                RemoveEntryList (&treeLink->DeferredList);

                treeLink->DeferredFlags &= ~LINK_FLAGS_DEFERRED_DELETE;
                NbfRemoveLinkFromTree (DeviceContext, treeLink);
                NbfDestroyLink (treeLink);

#if DBG
                NbfPrint2 ("NbfAddLinkToTree: Link %lx removed for %lx\n",
                        treeLink, Link);
#endif

                //
                // Now that that link is out of the tree, call
                // ourselves recursively to do the insert.
                //

                return NbfAddLinkToTree (DeviceContext, Link);

            } else {

                ASSERTMSG ("NbfAddLinkToTree: Found identical Link in tree!\n", FALSE);
                return STATUS_DRIVER_INTERNAL_ERROR;

            }

        }

        //
        // traverse the tree for the correct spot
        //

        if ((Link->MagicAddress).QuadPart < (treeLink->MagicAddress).QuadPart) {
            if ((linkLink = RtlLeftChild (linkLink)) == NULL) {
                IF_NBFDBG(NBF_DEBUG_LINKTREE) {
                    NbfPrint0 ("NbfAddLinkToTree: Adding link as LC.\n");
                }
                RtlInsertAsLeftChild ((PRTL_SPLAY_LINKS)treeLink,
                                       (PRTL_SPLAY_LINKS)Link);
                // DeviceContext->LinkTreeRoot = RtlSplay (DeviceContext->LinkTreeRoot);
                DeviceContext->LinkTreeElements++;
                return STATUS_SUCCESS;

            } else {
                treeLink = (PTP_LINK) linkLink;
                continue;
            } // Left Child

        } else { // is greater
            if ((linkLink = RtlRightChild (linkLink)) == NULL) {
                IF_NBFDBG(NBF_DEBUG_LINKTREE) {
                    NbfPrint0 ("NbfAddLinkToTree: Adding link as RC.\n");
                }
                RtlInsertAsRightChild ((PRTL_SPLAY_LINKS)treeLink,
                                       (PRTL_SPLAY_LINKS)Link);
                // DeviceContext->LinkTreeRoot = RtlSplay (DeviceContext->LinkTreeRoot);
                DeviceContext->LinkTreeElements++;
                return STATUS_SUCCESS;

            } else {
                treeLink = (PTP_LINK) linkLink;
                continue;
            } // Right Child

        } // end else addresses comparison

    } while (TRUE);


} // NbfAddLinkToTree


NTSTATUS
NbfRemoveLinkFromTree(
    IN PDEVICE_CONTEXT DeviceContext,
    IN PTP_LINK Link
    )

/*++

Routine Description:

    This routine removes a link from the tree of links.
    Note that since this routine needs to modify the link tree, it is called
    in the context of a deferred processing routine, and must have exclusive
    access to the tree. The spinlock is taken by the routine that calls this
    one, as this operation must be atomic in the eyes of the rest of NBF.
    Note further that this routine insists that there not be a link with this
    address in the tree.

Arguments:

    Link - Pointer to a transport link object.
    DeviceContext - pointer to the device context on which this

Return Value:

    STATUS_SUCCESS if the link was removed,
    STATUS_DRIVER_INTERNAL_ERROR if there was a problem removing
        the link (implying the tree was in a bad state).

--*/
{
    DeviceContext->LinkTreeRoot = RtlDelete ((PRTL_SPLAY_LINKS)Link);
    DeviceContext->LinkTreeElements--;
    if (DeviceContext->LastLink == Link) {
        DeviceContext->LastLink = (PTP_LINK)DeviceContext->LinkTreeRoot;
    }
    return STATUS_SUCCESS;

} //NbfRemoveLinkFromTree



PTP_LINK
NbfFindLinkInTree(
    IN PDEVICE_CONTEXT DeviceContext,
    IN PUCHAR Remote
    )

/*++

Routine Description:

    This routine traverses the link tree looking for the given remote address.
    The link tree spinlock is held while looking for the link. After the link
    is found, it's reference count is incremented.

    NOTE: This function is called with the device context LinkSpinLock
    held.

Arguments:

    DeviceContext - pointer to the device this address is associated with.

    Remote - pointer to the hardware address of the remote node.

Return Value:

    Pointer to the link in the tree that matches this remote address. If
    no link is found, NULL is returned.

--*/
{
    PTP_LINK link;
    PRTL_SPLAY_LINKS linkLink;
    ULARGE_INTEGER Magic = {0,0};


    //
    // Are there even any links in the tree?
    //

    if (DeviceContext->LinkTreeElements <= 0) {
        return NULL;
    }

    linkLink = DeviceContext->LinkTreeRoot;

    //
    // Make a magic number for this link
    //

    MacReturnMagicAddress (&DeviceContext->MacInfo, Remote, &Magic);

    IF_NBFDBG(NBF_DEBUG_LINKTREE) {
        NbfPrint1 ("NbfFindLinkInTree: starting search, Elements: %ld \n",
            DeviceContext->LinkTreeElements);
    }

    //
    // Do a quick check if the last link found is this one.
    //

    ASSERT (DeviceContext->LastLink != NULL);

    if ((DeviceContext->LastLink->MagicAddress).QuadPart == Magic.QuadPart) {

        link = DeviceContext->LastLink;

    } else {

        //
        // find the link.
        //

        link = (PTP_LINK) linkLink;     // depends upon splay links being first
                                        // subfield in link!
        IF_NBFDBG(NBF_DEBUG_LINKTREE) {
            NbfPrint3 ("NbfFindLinkInTree: searching, Link: %lx LC: %lx RC: %lx \n",
                linkLink, RtlLeftChild (linkLink), RtlRightChild (linkLink));
        }

        do {

            IF_NBFDBG(NBF_DEBUG_LINKTREE) {
                NbfPrint4 ("NbfFindLinkInTree: Comparing: %lx%lx to %lx%lx\n",
                    link->MagicAddress.HighPart,link->MagicAddress.LowPart,
                    Magic.HighPart, Magic.LowPart);
            }

            if ((link->MagicAddress).QuadPart == Magic.QuadPart) {
                IF_NBFDBG(NBF_DEBUG_LINKTREE) {
                    NbfPrint0 ("NbfFindLinkInTree: equal, going to end.\n");
                }
                break;

            } else {
                if ((link->MagicAddress).QuadPart < Magic.QuadPart) {
                    if ((linkLink = RtlRightChild (linkLink)) == NULL) {

                        IF_NBFDBG(NBF_DEBUG_LINKTREE) {
                            NbfPrint0 ("NbfFindLinkInTree: Link Not Found.\n");
                        }
                        return NULL;

                    } else {
                        link = (PTP_LINK) linkLink;
                        IF_NBFDBG(NBF_DEBUG_LINKTREE) {
                            NbfPrint3 ("NbfFindLinkInTree: less, took right child, Link: %lx LC: %lx RC: %lx \n",
                                linkLink, RtlLeftChild (linkLink), RtlRightChild (linkLink));
                        }
                        continue;
                    }

                } else { // is greater
                    if ((linkLink = RtlLeftChild (linkLink)) == NULL) {
                        IF_NBFDBG(NBF_DEBUG_LINKTREE) {
                            NbfPrint0 ("NbfFindLinkInTree: Link Not Found.\n");
                        }
                        return NULL;

                    } else {
                        link = (PTP_LINK) linkLink;
                        IF_NBFDBG(NBF_DEBUG_LINKTREE) {
                            NbfPrint3 ("NbfFindLinkInTree: greater, took left child, Link: %lx LC: %lx RC: %lx \n",
                                linkLink, RtlLeftChild (linkLink), RtlRightChild (linkLink));
                        }
                        continue;
                    } // got left child branch
                } // greater branch
            } // equal to branch
        } while (TRUE);

        DeviceContext->LastLink = link;

    }

    //
    // Only break out when we've actually found a match..
    //

    if ((link->DeferredFlags & LINK_FLAGS_DEFERRED_DELETE) != 0) {
       IF_NBFDBG(NBF_DEBUG_LINKTREE) {
           NbfPrint0 ("NbfFindLinkInTree: Link Found but delete pending.\n");
       }
       return NULL;
    }

    //
    // Mark the link as in use and say we don't need the tree stable any more.
    //

    NbfReferenceLink ("Found in tree", link, LREF_TREE);

    IF_NBFDBG(NBF_DEBUG_LINKTREE) {
        NbfPrint0 ("NbfFindLinkInTree: Link Found.\n");
    }

    return link;

} // NbfFindLinkInTree


PTP_LINK
NbfFindLink(
    IN PDEVICE_CONTEXT DeviceContext,
    IN PUCHAR Remote
    )

/*++

Routine Description:

    This routine looks for a link in the link tree, and if
    not found there in the deferred queue.

Arguments:

    DeviceContext - pointer to the device this address is associated with.

    Remote - pointer to the hardware address of the remote node.

Return Value:

    Pointer to the link in the tree that matches this remote address. If
    no link is found, NULL is returned.

--*/

{
    PTP_LINK Link;
    BOOLEAN MatchedLink;
    PLIST_ENTRY p;
    UINT i;

    ACQUIRE_DPC_SPIN_LOCK (&DeviceContext->LinkSpinLock);

    Link = NbfFindLinkInTree (DeviceContext, Remote);

    if (Link == NULL) {

        //
        // Not found there, try in deferred queue.
        //

        MatchedLink = FALSE;        // Assume failure

        //
        // Hold the spinlock while we walk the deferred list. We need
        // TimerSpinLock to stop the list from changing, and we need
        // LinkSpinLock to synchronize checking DEFERRED_DELETE and
        // referencing the link.
        //

        ACQUIRE_DPC_SPIN_LOCK (&DeviceContext->TimerSpinLock);

        for (p = DeviceContext->LinkDeferred.Flink;
             p != &DeviceContext->LinkDeferred;
             p = p->Flink) {

            //
            // BUGBUG: What about taking a lock while we walk
            // this list? It won't be removed from at the front,
            // but it may be added to at the back.
            //

            //
            // We're probably still getting this link to the splay tree.
            // find it and process normally.
            //

            Link = CONTAINING_RECORD (p, TP_LINK, DeferredList);

            //
            // NOTE: We know that the link is not going to be destroyed
            // now, because we have increased the semaphore. We
            // reference the link if DEFERRED_DELETE is not on; the
            // setting of this flag is synchronized (also using
            // DeviceContext->LinkSpinLock) with the refcount going
            // to 0).
            //

            if ((Link->DeferredFlags & LINK_FLAGS_DEFERRED_DELETE) != 0) {
                continue;      // we're deleting link, can't handle
            }

            for (i=0; i<(UINT)DeviceContext->MacInfo.AddressLength; i++) {
                if (Remote[i] != Link->HardwareAddress.Address[i]){
                    break;
                }
            }

            if (i == (UINT)DeviceContext->MacInfo.AddressLength) { // addresses match.  Deliver packet.
                IF_NBFDBG (NBF_DEBUG_DLC) {
                    NbfPrint1 ("NbfFindLink: Found link on deferred queue, Link: %lx\n",
                                Link);
                }
                NbfReferenceLink ("Got Frame on Deferred", Link, LREF_TREE);
                MatchedLink = TRUE;
                break;
            }

        }

        RELEASE_DPC_SPIN_LOCK (&DeviceContext->TimerSpinLock);

        //
        // If this didn't find the link, make note of that.
        //

        if (MatchedLink == FALSE) {

            Link = (PTP_LINK)NULL;

        }

    } else {

        IF_NBFDBG (NBF_DEBUG_DLC) {
            NbfPrint1 ("NbfFindLink: Found link in tree, Link: %lx\n", Link);
        }

    }

    RELEASE_DPC_SPIN_LOCK (&DeviceContext->LinkSpinLock);

    return Link;

}