ethdev: minor changes
[dpdk.git] / lib / librte_hash / rte_hash.c
index e558cd9..007ec44 100644 (file)
 #include <rte_string_fns.h>
 #include <rte_cpuflags.h>
 #include <rte_log.h>
+#include <rte_rwlock.h>
+#include <rte_spinlock.h>
 
 #include "rte_hash.h"
-#include "rte_jhash.h"
-#include "rte_hash_crc.h"
 
 
 TAILQ_HEAD(rte_hash_list, rte_hash);
@@ -73,7 +73,13 @@ TAILQ_HEAD(rte_hash_list, rte_hash);
 #endif
 
 /* Hash function used if none is specified */
+#ifdef RTE_MACHINE_CPUFLAG_SSE4_2
+#include <rte_hash_crc.h>
 #define DEFAULT_HASH_FUNC       rte_hash_crc
+#else
+#include <rte_jhash.h>
+#define DEFAULT_HASH_FUNC       rte_jhash
+#endif
 
 /* Signature bucket size is a multiple of this value */
 #define SIG_BUCKET_ALIGNMENT    16
@@ -145,10 +151,13 @@ rte_hash_find_existing(const char *name)
                return NULL;
        }
 
+       rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
        TAILQ_FOREACH(h, hash_list, next) {
                if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
                        break;
        }
+       rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
+
        if (h == NULL)
                rte_errno = ENOENT;
        return h;
@@ -197,36 +206,26 @@ rte_hash_create(const struct rte_hash_parameters *params)
                                  CACHE_LINE_SIZE);
        key_tbl_size = align_size(num_buckets * key_size *
                                  params->bucket_entries, CACHE_LINE_SIZE);
-
+       
        /* Total memory required for hash context */
        mem_size = hash_tbl_size + sig_tbl_size + key_tbl_size;
 
+       rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
+
        /* guarantee there's no existing */
        TAILQ_FOREACH(h, hash_list, next) {
                if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
                        break;
        }
        if (h != NULL)
-               return NULL;
+               goto exit;
 
-       /* Allocate as a memzone, or in normal memory space */
-#if defined(RTE_LIBRTE_HASH_USE_MEMZONE)
-       const struct rte_memzone *mz;
-       mz = rte_memzone_reserve(hash_name, mem_size, params->socket_id, 0);
-       if (mz == NULL) {
-               RTE_LOG(ERR, HASH, "memzone reservation failed\n");
-               return NULL;
-       }
-       memset(mz->addr, 0, mem_size);
-       h = (struct rte_hash *)mz->addr;
-#else
-       h = (struct rte_hash *)rte_zmalloc(hash_name, mem_size,
-                                          CACHE_LINE_SIZE);
+       h = (struct rte_hash *)rte_zmalloc_socket(hash_name, mem_size,
+                                          CACHE_LINE_SIZE, params->socket_id);
        if (h == NULL) {
                RTE_LOG(ERR, HASH, "memory allocation failed\n");
-               return NULL;
+               goto exit;
        }
-#endif
 
        /* Setup hash context */
        rte_snprintf(h->name, sizeof(h->name), "%s", params->name);
@@ -244,15 +243,11 @@ rte_hash_create(const struct rte_hash_parameters *params)
        h->hash_func = (params->hash_func == NULL) ?
                DEFAULT_HASH_FUNC : params->hash_func;
 
-       if (h->hash_func == rte_hash_crc &&
-                       !rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_2)) {
-               RTE_LOG(WARNING, HASH, "CRC32 instruction requires SSE4.2, "
-                               "which is not supported on this system. "
-                               "Falling back to software hash\n.");
-               h->hash_func = rte_jhash;
-       }
-
        TAILQ_INSERT_TAIL(hash_list, h, next);
+
+exit:
+       rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
+
        return h;
 }
 
@@ -262,12 +257,8 @@ rte_hash_free(struct rte_hash *h)
        if (h == NULL)
                return;
 
-#if !defined(RTE_LIBRTE_HASH_USE_MEMZONE)
        RTE_EAL_TAILQ_REMOVE(RTE_TAILQ_HASH, rte_hash_list, h);
        rte_free(h);
-#endif
-       /* No way to deallocate memzones */
-       return;
 }
 
 int32_t