diff options
Diffstat (limited to 'src/ipcpd/unicast')
| -rw-r--r-- | src/ipcpd/unicast/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/ipcpd/unicast/pol/alternate_pff.c | 2 | ||||
| -rw-r--r-- | src/ipcpd/unicast/pol/hashtable.c | 222 | ||||
| -rw-r--r-- | src/ipcpd/unicast/pol/hashtable.h | 55 | ||||
| -rw-r--r-- | src/ipcpd/unicast/pol/simple_pff.c | 2 | ||||
| -rw-r--r-- | src/ipcpd/unicast/pol/tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/ipcpd/unicast/pol/tests/hashtable_test.c | 129 | 
7 files changed, 410 insertions, 2 deletions
| diff --git a/src/ipcpd/unicast/CMakeLists.txt b/src/ipcpd/unicast/CMakeLists.txt index c9344f89..192b6592 100644 --- a/src/ipcpd/unicast/CMakeLists.txt +++ b/src/ipcpd/unicast/CMakeLists.txt @@ -44,6 +44,7 @@ set(SOURCE_FILES    routing.c    psched.c    # Add policies last +  pol/hashtable.c    pol/alternate_pff.c    pol/flat.c    pol/link_state.c diff --git a/src/ipcpd/unicast/pol/alternate_pff.c b/src/ipcpd/unicast/pol/alternate_pff.c index 131bc2d7..36d8d513 100644 --- a/src/ipcpd/unicast/pol/alternate_pff.c +++ b/src/ipcpd/unicast/pol/alternate_pff.c @@ -24,7 +24,6 @@  #include "config.h" -#include <ouroboros/hashtable.h>  #include <ouroboros/errno.h>  #include <ouroboros/list.h> @@ -32,6 +31,7 @@  #include <assert.h>  #include <pthread.h> +#include "hashtable.h"  #include "alternate_pff.h"  struct nhop { diff --git a/src/ipcpd/unicast/pol/hashtable.c b/src/ipcpd/unicast/pol/hashtable.c new file mode 100644 index 00000000..596219f3 --- /dev/null +++ b/src/ipcpd/unicast/pol/hashtable.c @@ -0,0 +1,222 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2020 + * + * Hash table with integer keys with separate chaining on collisions + * + *    Dimitri Staessens <dimitri.staessens@ugent.be> + *    Sander Vrijders   <sander.vrijders@ugent.be> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., http://www.fsf.org/about/contact/. + */ + +#if defined(__linux__) || defined(__CYGWIN__) +#define _DEFAULT_SOURCE +#endif + +#include <ouroboros/list.h> +#include <ouroboros/errno.h> +#include <ouroboros/hash.h> + +#include "hashtable.h" + +#include <assert.h> +#include <string.h> + +struct htable_entry { +        struct list_head next; +        uint64_t         key; +        void *           val; +        size_t           len; +}; + +struct htable { +        struct list_head * buckets; +        bool               hash_key; +        uint64_t           buckets_size; +}; + +struct htable * htable_create(uint64_t buckets, +                              bool     hash_key) +{ +        struct htable * tmp; +        unsigned int i; + +        if (buckets == 0) +                return NULL; + +        buckets--; +        buckets |= buckets >> 1; +        buckets |= buckets >> 2; +        buckets |= buckets >> 4; +        buckets |= buckets >> 8; +        buckets |= buckets >> 16; +        buckets |= buckets >> 32; +        buckets++; + +        tmp = malloc(sizeof(*tmp)); +        if (tmp == NULL) +                return NULL; + +        tmp->hash_key = hash_key; +        tmp->buckets_size = buckets; + +        tmp->buckets = malloc(buckets * sizeof(*tmp->buckets)); +        if (tmp->buckets == NULL) { +                free(tmp); +                return NULL; +        } + +        for (i = 0; i < buckets; i++) +                list_head_init(&(tmp->buckets[i])); + +        return tmp; +} + +void htable_destroy(struct htable * table) +{ +        assert(table); +        assert(table->buckets); + +        htable_flush(table); +        free(table->buckets); +        free(table); +} + +void htable_flush(struct htable * table) +{ +        unsigned int          i; +        struct list_head *    pos = NULL; +        struct list_head *    n = NULL; +        struct htable_entry * entry; + +        assert(table); + +        for (i = 0; i < table->buckets_size; i++) { +                list_for_each_safe(pos, n, &(table->buckets[i])) { +                        entry = list_entry(pos, struct htable_entry, next); +                        list_del(&entry->next); +                        free(entry->val); +                        free(entry); +                } +        } +} + +static uint64_t hash(uint64_t key) +{ +        void *   res; +        uint64_t ret; +        uint8_t  keys[4]; + +        memcpy(keys, &key, 4); + +        mem_hash(HASH_MD5, &res, keys, 4); + +        ret = (* (uint64_t *) res); + +        free(res); + +        return ret; +} + +static uint64_t calc_key(struct htable * table, +                         uint64_t        key) +{ +        if (table->hash_key) +                key = hash(key); + +        return (key & (table->buckets_size - 1)); +} + +int htable_insert(struct htable * table, +                  uint64_t        key, +                  void *          val, +                  size_t          len) +{ +        struct htable_entry * entry; +        uint64_t              lookup_key; +        struct list_head *    pos = NULL; + +        assert(table); + +        lookup_key = calc_key(table, key); + +        list_for_each(pos, &(table->buckets[lookup_key])) { +                entry = list_entry(pos, struct htable_entry, next); +                if (entry->key == key) +                        return -1; +        } + +        entry = malloc(sizeof(*entry)); +        if (entry == NULL) +                return -ENOMEM; + +        entry->key = key; +        entry->val = val; +        entry->len = len; +        list_head_init(&entry->next); + +        list_add(&entry->next, &(table->buckets[lookup_key])); + +        return 0; +} + +int htable_lookup(struct htable * table, +                  uint64_t        key, +                  void **         val, +                  size_t *        len) +{ +        struct htable_entry * entry; +        struct list_head *    pos = NULL; +        uint64_t              lookup_key; + +        assert(table); + +        lookup_key = calc_key(table, key); + +        list_for_each(pos, &(table->buckets[lookup_key])) { +                entry = list_entry(pos, struct htable_entry, next); +                if (entry->key == key) { +                        *val = entry->val; +                        *len = entry->len; +                        return 0; +                } +        } + +        return -1; +} + +int htable_delete(struct htable * table, +                  uint64_t        key) +{ +        struct htable_entry * entry; +        uint64_t              lookup_key; +        struct list_head *    pos = NULL; +        struct list_head *    n = NULL; + +        assert(table); + +        lookup_key = calc_key(table, key); + +        list_for_each_safe(pos, n, &(table->buckets[lookup_key])) { +                entry = list_entry(pos, struct htable_entry, next); +                if (entry->key == key) { +                        list_del(&entry->next); +                        free(entry->val); +                        free(entry); +                        return 0; +                } +        } + +        return -1; +} diff --git a/src/ipcpd/unicast/pol/hashtable.h b/src/ipcpd/unicast/pol/hashtable.h new file mode 100644 index 00000000..b6a36180 --- /dev/null +++ b/src/ipcpd/unicast/pol/hashtable.h @@ -0,0 +1,55 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2020 + * +* Hash table with integer keys with separate chaining on collisions +  * + *    Dimitri Staessens <dimitri.staessens@ugent.be> + *    Sander Vrijders   <sander.vrijders@ugent.be> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., http://www.fsf.org/about/contact/. + */ + +#ifndef OUROBOROS_HASHTABLE_H +#define OUROBOROS_HASHTABLE_H + +#include <stdint.h> +#include <stdbool.h> +#include <stdlib.h> + +struct htable; + +/* Buckets is rounded up to the nearest power of 2 */ +struct htable * htable_create(uint64_t buckets, +                              bool     hash_key); + +void            htable_destroy(struct htable * table); + +void            htable_flush(struct htable * table); + +/* Passes ownership of the block of memory */ +int             htable_insert(struct htable * table, +                              uint64_t        key, +                              void *          val, +                              size_t          len); + +/* The block of memory returned is no copy */ +int             htable_lookup(struct htable * table, +                              uint64_t        key, +                              void **         val, +                              size_t *        len); + +int             htable_delete(struct htable * table, +                              uint64_t        key); + +#endif /* OUROBOROS_HASHTABLE_H */ diff --git a/src/ipcpd/unicast/pol/simple_pff.c b/src/ipcpd/unicast/pol/simple_pff.c index c34a4fdc..3d294d90 100644 --- a/src/ipcpd/unicast/pol/simple_pff.c +++ b/src/ipcpd/unicast/pol/simple_pff.c @@ -24,12 +24,12 @@  #include "config.h" -#include <ouroboros/hashtable.h>  #include <ouroboros/errno.h>  #include <assert.h>  #include <pthread.h> +#include "hashtable.h"  #include "simple_pff.h"  struct pff_i { diff --git a/src/ipcpd/unicast/pol/tests/CMakeLists.txt b/src/ipcpd/unicast/pol/tests/CMakeLists.txt index d0652533..86c2d948 100644 --- a/src/ipcpd/unicast/pol/tests/CMakeLists.txt +++ b/src/ipcpd/unicast/pol/tests/CMakeLists.txt @@ -18,6 +18,7 @@ get_filename_component(PARENT_DIR ${PARENT_PATH} NAME)  create_test_sourcelist(${PARENT_DIR}_tests test_suite.c    # Add new tests here    graph_test.c +  hashtable_test.c    )  add_executable(${PARENT_DIR}_test EXCLUDE_FROM_ALL ${${PARENT_DIR}_tests}) diff --git a/src/ipcpd/unicast/pol/tests/hashtable_test.c b/src/ipcpd/unicast/pol/tests/hashtable_test.c new file mode 100644 index 00000000..f84fee63 --- /dev/null +++ b/src/ipcpd/unicast/pol/tests/hashtable_test.c @@ -0,0 +1,129 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2020 + * + * Test of the hash table + * + *    Dimitri Staessens <dimitri.staessens@ugent.be> + *    Sander Vrijders   <sander.vrijders@ugent.be> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., http://www.fsf.org/about/contact/. + */ + +#include "hashtable.c" + +#include <stdio.h> + +#define HASHTABLE_SIZE 256 +#define INT_TEST 4 + +int hashtable_test(int argc, char ** argv) +{ +        struct htable * table; +        int             i; +        int *           j; +        void *          el; +        size_t          len; + +        (void) argc; +        (void) argv; + +        table = htable_create(HASHTABLE_SIZE, true); +        if (table == NULL) { +                printf("Failed to create.\n"); +                return -1; +        } + +        htable_destroy(table); + +        table = htable_create(HASHTABLE_SIZE, false); +        if (table == NULL) { +                printf("Failed to create.\n"); +                return -1; +        } + +        for (i = 0; i < HASHTABLE_SIZE + INT_TEST + 2; i++) { +                j = malloc(sizeof(*j)); +                if (j == NULL) { +                        printf("Failed to malloc.\n"); +                        htable_destroy(table); +                        return -1; +                } +                *j = i; + +                if (htable_insert(table, i, (void *) j, 1)) { +                        printf("Failed to insert.\n"); +                        htable_destroy(table); +                        free(j); +                        return -1; +                } +        } + +        if (htable_lookup(table, INT_TEST, &el, &len)) { +                printf("Failed to lookup.\n"); +                htable_destroy(table); +                return -1; +        } + +        j = (int *) el; +        if (*j != INT_TEST) { +                printf("Lookup returned wrong value (%d != %d).\n", +                       INT_TEST, *j); +                htable_destroy(table); +                return -1; +        } + +        if (htable_lookup(table, HASHTABLE_SIZE + INT_TEST, &el, &len)) { +                printf("Failed to lookup.\n"); +                htable_destroy(table); +                return -1; +        } + +        j = (int *) el; +        if (*j != HASHTABLE_SIZE + INT_TEST) { +                printf("Lookup returned wrong value (%d != %d).\n", +                       INT_TEST, *j); +                htable_destroy(table); +                return -1; +        } + +        if (htable_delete(table, INT_TEST)) { +                printf("Failed to delete.\n"); +                htable_destroy(table); +                return -1; +        } + +        if (htable_lookup(table, INT_TEST, &el, &len) == 0) { +                printf("Failed to delete properly.\n"); +                htable_destroy(table); +                return -1; +        } + +        if (htable_lookup(table, HASHTABLE_SIZE + INT_TEST, &el, &len)) { +                printf("Failed to lookup after deletion.\n"); +                htable_destroy(table); +                return -1; +        } + +        j = (int *) el; +        if (*j != HASHTABLE_SIZE + INT_TEST) { +                printf("Lookup returned wrong value (%d != %d).\n", +                       INT_TEST, *j); +                htable_destroy(table); +                return -1; +        } + +        htable_destroy(table); + +        return 0; +} | 
