2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
19 #include <rte_hash_crc.h>
20 #include <rte_version.h>
24 #include "prox_shared.h"
25 #include "prox_globals.h"
27 #define INIT_HASH_TABLE_SIZE 8192
30 struct rte_hash *hash;
34 struct prox_shared sh_system;
35 struct prox_shared sh_socket[MAX_SOCKETS];
36 struct prox_shared sh_core[RTE_MAX_LCORE];
38 static char* get_sh_name(void)
40 static char name[] = "prox_sh";
46 struct rte_hash_parameters param = {
48 .hash_func = rte_hash_crc,
49 .hash_func_init_val = 0,
53 static void prox_sh_create_hash(struct prox_shared *ps, size_t size)
56 param.name = get_sh_name();
57 ps->hash = rte_hash_create(¶m);
58 PROX_PANIC(ps->hash == NULL, "Failed to create hash table for shared data");
60 if (ps->size == INIT_HASH_TABLE_SIZE)
61 plog_info("Shared data tracking hash table created with size %zu\n", ps->size);
63 plog_info("Shared data tracking hash table grew to %zu\n", ps->size);
66 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,0)
67 static int copy_hash(struct rte_hash *new_hash, struct rte_hash *old_hash)
73 while (rte_hash_iterate(old_hash, &next_key, &next_data, &iter) >= 0) {
74 if (rte_hash_add_key_data(new_hash, next_key, next_data) < 0)
82 static int prox_sh_add(struct prox_shared *ps, const char *name, void *data)
87 strncpy(key, name, sizeof(key));
89 prox_sh_create_hash(ps, INIT_HASH_TABLE_SIZE);
92 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,0)
94 ret = rte_hash_add_key_data(ps->hash, key, data);
96 struct rte_hash *old = ps->hash;
99 prox_sh_create_hash(ps, ps->size * 2);
100 success = !copy_hash(ps->hash, old);
104 rte_hash_free(ps->hash);
109 PROX_PANIC(1, "DPDK < 2.1 not fully supported");
114 static void *prox_sh_find(struct prox_shared *sh, const char *name)
116 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,0)
124 strncpy(key, name, sizeof(key));
125 ret = rte_hash_lookup_data(sh->hash, key, &data);
129 PROX_PANIC(1, "DPDK < 2.1 not fully supported");
134 int prox_sh_add_system(const char *name, void *data)
136 return prox_sh_add(&sh_system, name, data);
139 int prox_sh_add_socket(const int socket_id, const char *name, void *data)
141 if (socket_id >= MAX_SOCKETS)
144 return prox_sh_add(&sh_socket[socket_id], name, data);
147 int prox_sh_add_core(const int core_id, const char *name, void *data)
149 if (core_id >= RTE_MAX_LCORE)
152 return prox_sh_add(&sh_core[core_id], name, data);
155 void *prox_sh_find_system(const char *name)
157 return prox_sh_find(&sh_system, name);
160 void *prox_sh_find_socket(const int socket_id, const char *name)
162 if (socket_id >= MAX_SOCKETS)
165 return prox_sh_find(&sh_socket[socket_id], name);
168 void *prox_sh_find_core(const int core_id, const char *name)
170 if (core_id >= RTE_MAX_LCORE)
173 return prox_sh_find(&sh_core[core_id], name);