2 // Copyright (c) 2010-2020 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"
26 #include "prox_compat.h"
28 #define INIT_HASH_TABLE_SIZE 8192
31 struct rte_hash *hash;
35 struct prox_shared sh_system;
36 struct prox_shared sh_socket[MAX_SOCKETS];
37 struct prox_shared sh_core[RTE_MAX_LCORE];
39 static char* get_sh_name(void)
41 static char name[] = "prox_sh";
47 struct rte_hash_parameters param = {
49 .hash_func = rte_hash_crc,
50 .hash_func_init_val = 0,
54 static void prox_sh_create_hash(struct prox_shared *ps, size_t size)
57 param.name = get_sh_name();
58 ps->hash = rte_hash_create(¶m);
59 PROX_PANIC(ps->hash == NULL, "Failed to create hash table for shared data");
61 if (ps->size == INIT_HASH_TABLE_SIZE)
62 plog_info("\tShared data tracking hash table created with size %zu\n", ps->size);
64 plog_info("\tShared data tracking hash table grew to %zu\n", ps->size);
67 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,0)
68 static int copy_hash(struct rte_hash *new_hash, struct rte_hash *old_hash)
74 while (rte_hash_iterate(old_hash, &next_key, &next_data, &iter) >= 0) {
75 if (rte_hash_add_key_data(new_hash, next_key, next_data) < 0)
83 static int prox_sh_add(struct prox_shared *ps, const char *name, void *data)
88 prox_strncpy(key, name, sizeof(key));
90 prox_sh_create_hash(ps, INIT_HASH_TABLE_SIZE);
93 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,0)
95 ret = rte_hash_add_key_data(ps->hash, key, data);
97 struct rte_hash *old = ps->hash;
100 prox_sh_create_hash(ps, ps->size * 2);
101 success = !copy_hash(ps->hash, old);
105 rte_hash_free(ps->hash);
110 PROX_PANIC(1, "DPDK < 2.1 not fully supported");
115 static void *prox_sh_find(struct prox_shared *sh, const char *name)
117 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,0)
125 prox_strncpy(key, name, sizeof(key));
126 ret = rte_hash_lookup_data(sh->hash, key, &data);
130 PROX_PANIC(1, "DPDK < 2.1 not fully supported");
135 int prox_sh_add_system(const char *name, void *data)
137 return prox_sh_add(&sh_system, name, data);
140 int prox_sh_add_socket(const int socket_id, const char *name, void *data)
142 if (socket_id >= MAX_SOCKETS)
145 return prox_sh_add(&sh_socket[socket_id], name, data);
148 int prox_sh_add_core(const int core_id, const char *name, void *data)
150 if (core_id >= RTE_MAX_LCORE)
153 return prox_sh_add(&sh_core[core_id], name, data);
156 void *prox_sh_find_system(const char *name)
158 return prox_sh_find(&sh_system, name);
161 void *prox_sh_find_socket(const int socket_id, const char *name)
163 if (socket_id >= MAX_SOCKETS)
166 return prox_sh_find(&sh_socket[socket_id], name);
169 void *prox_sh_find_core(const int core_id, const char *name)
171 if (core_id >= RTE_MAX_LCORE)
174 return prox_sh_find(&sh_core[core_id], name);