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 param.socket_id = rte_socket_id();
59 ps->hash = rte_hash_create(¶m);
60 PROX_PANIC(ps->hash == NULL, "Failed to create hash table for shared data");
62 if (ps->size == INIT_HASH_TABLE_SIZE)
63 plog_info("\tShared data tracking hash table created with size %zu\n", ps->size);
65 plog_info("\tShared data tracking hash table grew to %zu\n", ps->size);
68 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,0)
69 static int copy_hash(struct rte_hash *new_hash, struct rte_hash *old_hash)
75 while (rte_hash_iterate(old_hash, &next_key, &next_data, &iter) >= 0) {
76 if (rte_hash_add_key_data(new_hash, next_key, next_data) < 0)
84 static int prox_sh_add(struct prox_shared *ps, const char *name, void *data)
89 prox_strncpy(key, name, sizeof(key));
91 prox_sh_create_hash(ps, INIT_HASH_TABLE_SIZE);
94 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,0)
96 ret = rte_hash_add_key_data(ps->hash, key, data);
98 struct rte_hash *old = ps->hash;
101 prox_sh_create_hash(ps, ps->size * 2);
102 success = !copy_hash(ps->hash, old);
106 rte_hash_free(ps->hash);
111 PROX_PANIC(1, "DPDK < 2.1 not fully supported");
116 static void *prox_sh_find(struct prox_shared *sh, const char *name)
118 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,0)
126 prox_strncpy(key, name, sizeof(key));
127 ret = rte_hash_lookup_data(sh->hash, key, &data);
131 PROX_PANIC(1, "DPDK < 2.1 not fully supported");
136 int prox_sh_add_system(const char *name, void *data)
138 return prox_sh_add(&sh_system, name, data);
141 int prox_sh_add_socket(const int socket_id, const char *name, void *data)
143 if (socket_id >= MAX_SOCKETS)
146 return prox_sh_add(&sh_socket[socket_id], name, data);
149 int prox_sh_add_core(const int core_id, const char *name, void *data)
151 if (core_id >= RTE_MAX_LCORE)
154 return prox_sh_add(&sh_core[core_id], name, data);
157 void *prox_sh_find_system(const char *name)
159 return prox_sh_find(&sh_system, name);
162 void *prox_sh_find_socket(const int socket_id, const char *name)
164 if (socket_id >= MAX_SOCKETS)
167 return prox_sh_find(&sh_socket[socket_id], name);
170 void *prox_sh_find_core(const int core_id, const char *name)
172 if (core_id >= RTE_MAX_LCORE)
175 return prox_sh_find(&sh_core[core_id], name);