summaryrefslogtreecommitdiff
path: root/arch/arm/cpu/ixp/npe/IxEthDBHashtable.c
blob: f1b18e6b48273f88c26b8158459643680efa17ce (plain)
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
/**
 * @file ethHash.c
 *
 * @brief Hashtable implementation
 * 
 * @par
 * IXP400 SW Release version 2.0
 * 
 * -- Copyright Notice --
 * 
 * @par
 * Copyright 2001-2005, Intel Corporation.
 * All rights reserved.
 * 
 * @par
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Intel Corporation nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * @par
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 * @par
 * -- End of Copyright Notice --
 */


#include "IxEthDB_p.h"
#include "IxEthDBLocks_p.h"

/**
 * @addtogroup EthDB
 *
 * @{
 */

/**
 * @brief initializes a hash table object
 *
 * @param hashTable uninitialized hash table structure
 * @param numBuckets number of buckets to use
 * @param entryHashFunction hash function used 
 * to hash entire hash node data block (for adding)
 * @param matchFunctions array of match functions, indexed on type,
 * used to differentiate records with the same hash value
 * @param freeFunction function used to free node data blocks
 *
 * Initializes the given hash table object.
 *
 * @internal
 */
void ixEthDBInitHash(HashTable *hashTable, 
                     UINT32 numBuckets, 
                     HashFunction entryHashFunction, 
                     MatchFunction *matchFunctions, 
                     FreeFunction freeFunction)
{
    UINT32 bucketIndex;
    UINT32 hashSize = numBuckets * sizeof(HashNode *);

    /* entry hashing, matching and freeing methods */
    hashTable->entryHashFunction  = entryHashFunction;
    hashTable->matchFunctions     = matchFunctions;
    hashTable->freeFunction       = freeFunction;

    /* buckets */
    hashTable->numBuckets = numBuckets;

    /* set to 0 all buckets */
    memset(hashTable->hashBuckets, 0, hashSize);

    /* init bucket locks - note that initially all mutexes are unlocked after MutexInit()*/
    for (bucketIndex = 0 ; bucketIndex < numBuckets ; bucketIndex++)
    {
        ixOsalFastMutexInit(&hashTable->bucketLocks[bucketIndex]);
    }
}

/**
 * @brief adds an entry to the hash table
 *
 * @param hashTable hash table to add the entry to
 * @param entry entry to add
 *
 * The entry will be hashed using the entry hashing function and added to the
 * hash table, unless a locking blockage occurs, in which case the caller
 * should retry.
 *
 * @retval IX_ETH_DB_SUCCESS if adding <i>entry</i> has succeeded
 * @retval IX_ETH_DB_NOMEM if there's no memory left in the hash node pool
 * @retval IX_ETH_DB_BUSY if there's a locking failure on the insertion path
 *
 * @internal
 */
IX_ETH_DB_PUBLIC IxEthDBStatus ixEthDBAddHashEntry(HashTable *hashTable, void *entry)
{
    UINT32 hashValue   = hashTable->entryHashFunction(entry);
    UINT32 bucketIndex = hashValue % hashTable->numBuckets;
    HashNode *bucket   = hashTable->hashBuckets[bucketIndex];
    HashNode *newNode;

    LockStack locks;

    INIT_STACK(&locks);

    /* lock bucket */
    PUSH_LOCK(&locks, &hashTable->bucketLocks[bucketIndex]);

    /* lock insertion element (first in chain), if any */
    if (bucket != NULL)
    {
        PUSH_LOCK(&locks, &bucket->lock);
    }

    /* get new node */
    newNode = ixEthDBAllocHashNode();

    if (newNode == NULL)
    {
        /* unlock everything */
        UNROLL_STACK(&locks);

        return IX_ETH_DB_NOMEM;
    }

    /* init lock - note that mutexes are unlocked after MutexInit */
    ixOsalFastMutexInit(&newNode->lock);

    /* populate new link */
    newNode->data = entry;

    /* add to bucket */
    newNode->next                       = bucket;
    hashTable->hashBuckets[bucketIndex] = newNode;

    /* unlock bucket and insertion point */
    UNROLL_STACK(&locks);

    return IX_ETH_DB_SUCCESS;
}

/**
 * @brief removes an entry from the hashtable
 *
 * @param hashTable hash table to remove entry from
 * @param keyType type of record key used for matching
 * @param reference reference key used to identify the entry
 *
 * The reference key will be hashed using the key hashing function,
 * the entry is searched using the hashed value and then examined
 * against the reference entry using the match function. A positive
 * match will trigger the deletion of the entry.
 * Locking failures are reported and the caller should retry.
 *
 * @retval IX_ETH_DB_SUCCESS if the removal was successful
 * @retval IX_ETH_DB_NO_SUCH_ADDR if the entry was not found
 * @retval IX_ETH_DB_BUSY if a locking failure occured during the process
 *
 * @internal
 */
IxEthDBStatus ixEthDBRemoveHashEntry(HashTable *hashTable, int keyType, void *reference)
{
    UINT32 hashValue       = hashTable->entryHashFunction(reference);
    UINT32 bucketIndex     = hashValue % hashTable->numBuckets;
    HashNode *node         = hashTable->hashBuckets[bucketIndex];
    HashNode *previousNode = NULL;
    
    LockStack locks;

    INIT_STACK(&locks);

    while (node != NULL)
    {
        /* try to lock node */
        PUSH_LOCK(&locks, &node->lock);

        if (hashTable->matchFunctions[keyType](reference, node->data))
        {
            /* found entry */
            if (node->next != NULL)
            {
                PUSH_LOCK(&locks, &node->next->lock);
            }

            if (previousNode == NULL)
            {
                /* node is head of chain */
                PUSH_LOCK(&locks, &hashTable->bucketLocks[bucketIndex]);

                hashTable->hashBuckets[bucketIndex] = node->next;

                POP_LOCK(&locks);
            }
            else
            {
                /* relink */
                previousNode->next = node->next;
            }

            UNROLL_STACK(&locks);

            /* free node */
            hashTable->freeFunction(node->data);
            ixEthDBFreeHashNode(node);

            return IX_ETH_DB_SUCCESS;
        }
        else
        {
            if (previousNode != NULL)
            {
                /* unlock previous node */
                SHIFT_STACK(&locks);
            }

            /* advance to next element in chain */
            previousNode = node;
            node         = node->next;
        }
    }

    UNROLL_STACK(&locks);

    /* not found */
    return IX_ETH_DB_NO_SUCH_ADDR;
}

/**
 * @brief retrieves an entry from the hash table
 *
 * @param hashTable hash table to perform the search into
 * @param reference search key (a MAC address)
 * @param keyType type of record key used for matching
 * @param searchResult pointer where a reference to the located hash node 
 * is placed
 *
 * Searches the entry with the same key as <i>reference</i> and places the
 * pointer to the resulting node in <i>searchResult</i>.
 * An implicit write access lock is granted after a search, which gives the 
 * caller the opportunity to modify the entry.
 * Access should be released as soon as possible using @ref ixEthDBReleaseHashNode().
 *
 * @see ixEthDBReleaseHashNode()
 *
 * @retval IX_ETH_DB_SUCCESS if the search was completed successfully
 * @retval IX_ETH_DB_NO_SUCH_ADDRESS if no entry with the given key was found
 * @retval IX_ETH_DB_BUSY if a locking failure has occured, in which case
 * the caller should retry
 *
 * @warning unless the return value is <b>IX_ETH_DB_SUCCESS</b> the searchResult
 * location is NOT modified and therefore using a NULL comparison test when the
 * value was not properly initialized would be an error
 *
 * @internal
 */
IxEthDBStatus ixEthDBSearchHashEntry(HashTable *hashTable, int keyType, void *reference, HashNode **searchResult)
{
    UINT32 hashValue;
    HashNode *node;

    hashValue = hashTable->entryHashFunction(reference);
    node      = hashTable->hashBuckets[hashValue % hashTable->numBuckets];

    while (node != NULL)
    {
        TRY_LOCK(&node->lock);

        if (hashTable->matchFunctions[keyType](reference, node->data))
        {
            *searchResult = node;

            return IX_ETH_DB_SUCCESS;
        }
        else
        {
            UNLOCK(&node->lock);

            node = node->next;
        }
    }

    /* not found */
    return IX_ETH_DB_NO_SUCH_ADDR;
}

/**
 * @brief reports the existence of an entry in the hash table
 *
 * @param hashTable hash table to perform the search into
 * @param reference search key (a MAC address)
 * @param keyType type of record key used for matching
 *
 * Searches the entry with the same key as <i>reference</i>.
 * No implicit write access lock is granted after a search, hence the 
 * caller cannot access or modify the entry. The result is only temporary.
 *
 * @see ixEthDBReleaseHashNode()
 *
 * @retval IX_ETH_DB_SUCCESS if the search was completed successfully
 * @retval IX_ETH_DB_NO_SUCH_ADDRESS if no entry with the given key was found
 * @retval IX_ETH_DB_BUSY if a locking failure has occured, in which case
 * the caller should retry
 *
 * @internal
 */
IxEthDBStatus ixEthDBPeekHashEntry(HashTable *hashTable, int keyType, void *reference)
{
    UINT32 hashValue;
    HashNode *node;

    hashValue = hashTable->entryHashFunction(reference);
    node      = hashTable->hashBuckets[hashValue % hashTable->numBuckets];

    while (node != NULL)
    {
        TRY_LOCK(&node->lock);

        if (hashTable->matchFunctions[keyType](reference, node->data))
        {
            UNLOCK(&node->lock);

            return IX_ETH_DB_SUCCESS;
        }
        else
        {
            UNLOCK(&node->lock);

            node = node->next;
        }
    }

    /* not found */
    return IX_ETH_DB_NO_SUCH_ADDR;
}

/**
 * @brief releases the write access lock
 *
 * @pre the node should have been obtained via @ref ixEthDBSearchHashEntry()
 *
 * @see ixEthDBSearchHashEntry()
 *
 * @internal
 */
void ixEthDBReleaseHashNode(HashNode *node)
{
    UNLOCK(&node->lock);
}

/**
 * @brief initializes a hash iterator
 *
 * @param hashTable hash table to be iterated
 * @param iterator iterator object
 *
 * If the initialization is successful the iterator will point to the
 * first hash table record (if any).
 * Testing if the iterator has not passed the end of the table should be
 * done using the IS_ITERATOR_VALID(iteratorPtr) macro.
 * An implicit write access lock is granted on the entry pointed by the iterator.
 * The access is automatically revoked when the iterator is incremented.
 * If the caller decides to terminate the iteration before the end of the table is
 * passed then the manual access release method, @ref ixEthDBReleaseHashIterator,
 * must be called.
 *
 * @see ixEthDBReleaseHashIterator()
 *
 * @retval IX_ETH_DB_SUCCESS if initialization was successful and the iterator points
 * to the first valid table node
 * @retval IX_ETH_DB_FAIL if the table is empty
 * @retval IX_ETH_DB_BUSY if a locking failure has occured, in which case the caller
 * should retry
 *
 * @warning do not use ixEthDBReleaseHashNode() on entries pointed by the iterator, as this
 * might place the database in a permanent invalid lock state
 *
 * @internal
 */
IxEthDBStatus ixEthDBInitHashIterator(HashTable *hashTable, HashIterator *iterator)
{
    iterator->bucketIndex  = 0;
    iterator->node         = NULL;
    iterator->previousNode = NULL;

    return ixEthDBIncrementHashIterator(hashTable, iterator);
}

/**
 * @brief releases the write access locks of the iterator nodes
 *
 * @warning use of this function is required only when the caller terminates an iteration
 * before reaching the end of the table
 *
 * @see ixEthDBInitHashIterator()
 * @see ixEthDBIncrementHashIterator()
 *
 * @param iterator iterator whose node(s) should be unlocked
 *
 * @internal
 */
void ixEthDBReleaseHashIterator(HashIterator *iterator)
{
    if (iterator->previousNode != NULL)
    {
        UNLOCK(&iterator->previousNode->lock);
    }

    if (iterator->node != NULL)
    {
        UNLOCK(&iterator->node->lock);
    }
}

/**
 * @brief incremenents an iterator so that it points to the next valid entry of the table
 * (if any)
 *
 * @param hashTable hash table to iterate
 * @param iterator iterator object
 *
 * @pre the iterator object must be initialized using @ref ixEthDBInitHashIterator()
 *
 * If the increment operation is successful the iterator will point to the
 * next hash table record (if any).
 * Testing if the iterator has not passed the end of the table should be
 * done using the IS_ITERATOR_VALID(iteratorPtr) macro.
 * An implicit write access lock is granted on the entry pointed by the iterator.
 * The access is automatically revoked when the iterator is re-incremented.
 * If the caller decides to terminate the iteration before the end of the table is
 * passed then the manual access release method, @ref ixEthDBReleaseHashIterator,
 * must be called.
 * Is is guaranteed that no other thread can remove or change the iterated entry until
 * the iterator is incremented successfully.
 *
 * @see ixEthDBReleaseHashIterator()
 *
 * @retval IX_ETH_DB_SUCCESS if the operation was successful and the iterator points
 * to the next valid table node
 * @retval IX_ETH_DB_FAIL if the iterator has passed the end of the table
 * @retval IX_ETH_DB_BUSY if a locking failure has occured, in which case the caller
 * should retry
 *
 * @warning do not use ixEthDBReleaseHashNode() on entries pointed by the iterator, as this
 * might place the database in a permanent invalid lock state
 *
 * @internal
 */
IxEthDBStatus ixEthDBIncrementHashIterator(HashTable *hashTable, HashIterator *iterator)
{
    /* unless iterator is just initialized... */
    if (iterator->node != NULL)
    {
        /* try next in chain */
        if (iterator->node->next != NULL)
        {
            TRY_LOCK(&iterator->node->next->lock);

            if (iterator->previousNode != NULL)
            {
                UNLOCK(&iterator->previousNode->lock);
            }

            iterator->previousNode = iterator->node;
            iterator->node         = iterator->node->next;

            return IX_ETH_DB_SUCCESS;
        }
        else
        {
            /* last in chain, prepare for next bucket */
            iterator->bucketIndex++;
        }
    }

   /* try next used bucket */
    for (; iterator->bucketIndex < hashTable->numBuckets ; iterator->bucketIndex++)
    {
        HashNode **nodePtr = &(hashTable->hashBuckets[iterator->bucketIndex]);
        HashNode *node = *nodePtr;
#if (CPU!=SIMSPARCSOLARIS) && !defined (__wince)
        if (((iterator->bucketIndex & IX_ETHDB_BUCKET_INDEX_MASK) == 0) &&
            (iterator->bucketIndex < (hashTable->numBuckets - IX_ETHDB_BUCKETPTR_AHEAD)))
        {
            /* preload next cache line (2 cache line ahead) */
            nodePtr += IX_ETHDB_BUCKETPTR_AHEAD;
            __asm__ ("pld [%0];\n": : "r" (nodePtr));
        }
#endif
        if (node != NULL)
        {
            TRY_LOCK(&node->lock);

            /* unlock last one or two nodes in the previous chain */
            if (iterator->node != NULL)
            {
                UNLOCK(&iterator->node->lock);

                if (iterator->previousNode != NULL)
                {
                    UNLOCK(&iterator->previousNode->lock);
                }
            }
            
            /* redirect iterator */
            iterator->previousNode = NULL;
            iterator->node         = node;

            return IX_ETH_DB_SUCCESS;
        }
    }

    /* could not advance iterator */
    if (iterator->node != NULL)
    {
        UNLOCK(&iterator->node->lock);

        if (iterator->previousNode != NULL)
        {
            UNLOCK(&iterator->previousNode->lock);
        }

        iterator->node = NULL;
    }

    return IX_ETH_DB_END;
}

/**
 * @brief removes an entry pointed by an iterator
 *
 * @param hashTable iterated hash table
 * @param iterator iterator object
 *
 * Removes the entry currently pointed by the iterator and repositions the iterator
 * on the next valid entry (if any). Handles locking issues automatically and
 * implicitely grants write access lock to the new pointed entry.
 * Failures due to concurrent threads having write access locks in the same region
 * preserve the state of the database and the iterator object, leaving the caller
 * free to retry without loss of access. It is guaranteed that only the thread owning
 * the iterator can remove the object pointed by the iterator.
 *
 * @retval IX_ETH_DB_SUCCESS if removal has succeeded
 * @retval IX_ETH_DB_BUSY if a locking failure has occured, in which case the caller
 * should retry
 *
 * @internal
 */
IxEthDBStatus ixEthDBRemoveEntryAtHashIterator(HashTable *hashTable, HashIterator *iterator)
{
    HashIterator nextIteratorPos;
    LockStack locks;

    INIT_STACK(&locks);

    /* set initial bucket index for next position */
    nextIteratorPos.bucketIndex = iterator->bucketIndex;

    /* compute iterator position before removing anything and lock ahead */
    if (iterator->node->next != NULL)
    {
        PUSH_LOCK(&locks, &iterator->node->next->lock);

        /* reposition on the next node in the chain */
        nextIteratorPos.node         = iterator->node->next;
        nextIteratorPos.previousNode = iterator->previousNode;
    }
    else
    {
        /* try next chain - don't know yet if we'll find anything */
        nextIteratorPos.node = NULL;

        /* if we find something it's a chain head */
        nextIteratorPos.previousNode = NULL;

        /* browse up in the buckets to find a non-null chain */
        while (++nextIteratorPos.bucketIndex < hashTable->numBuckets)
        {
            nextIteratorPos.node = hashTable->hashBuckets[nextIteratorPos.bucketIndex];

            if (nextIteratorPos.node != NULL)
            {
                /* found a non-empty chain, try to lock head */
                PUSH_LOCK(&locks, &nextIteratorPos.node->lock);

                break;
            }
        }
    }

    /* restore links over the to-be-deleted item */
    if (iterator->previousNode == NULL)
    {
        /* first in chain, lock bucket */
        PUSH_LOCK(&locks, &hashTable->bucketLocks[iterator->bucketIndex]);

        hashTable->hashBuckets[iterator->bucketIndex] = iterator->node->next;

        POP_LOCK(&locks);
    }
    else
    {
        /* relink */
        iterator->previousNode->next = iterator->node->next;

        /* unlock last remaining node in current chain when moving between chains */
        if (iterator->node->next == NULL)
        {
            UNLOCK(&iterator->previousNode->lock);
        }
    }

    /* delete entry */
    hashTable->freeFunction(iterator->node->data);
    ixEthDBFreeHashNode(iterator->node);

    /* reposition iterator */
    *iterator = nextIteratorPos;

    return IX_ETH_DB_SUCCESS;
}

/**
 * @}
 */