Added initial support for NDP (IPv6)
[samplevnf.git] / VNFs / DPPD-PROX / prox_shared.c
1 /*
2 // Copyright (c) 2010-2020 Intel Corporation
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 */
16
17 #include <stdio.h>
18 #include <rte_hash.h>
19 #include <rte_hash_crc.h>
20 #include <rte_version.h>
21
22 #include "quit.h"
23 #include "log.h"
24 #include "prox_shared.h"
25 #include "prox_globals.h"
26 #include "prox_compat.h"
27
28 #define INIT_HASH_TABLE_SIZE 8192
29
30 struct prox_shared {
31         struct rte_hash *hash;
32         size_t          size;
33 };
34
35 struct prox_shared sh_system;
36 struct prox_shared sh_socket[MAX_SOCKETS];
37 struct prox_shared sh_core[RTE_MAX_LCORE];
38
39 static char* get_sh_name(void)
40 {
41         static char name[] = "prox_sh";
42
43         name[0]++;
44         return name;
45 }
46
47 struct rte_hash_parameters param = {
48         .key_len = 256,
49         .hash_func = rte_hash_crc,
50         .hash_func_init_val = 0,
51         .socket_id = 0,
52 };
53
54 static void prox_sh_create_hash(struct prox_shared *ps, size_t size)
55 {
56         param.entries = size;
57         param.name = get_sh_name();
58         ps->hash = rte_hash_create(&param);
59         PROX_PANIC(ps->hash == NULL, "Failed to create hash table for shared data");
60         ps->size = size;
61         if (ps->size == INIT_HASH_TABLE_SIZE)
62                 plog_info("\tShared data tracking hash table created with size %zu\n", ps->size);
63         else
64                 plog_info("\tShared data tracking hash table grew to %zu\n", ps->size);
65 }
66
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)
69 {
70         const void *next_key;
71         void *next_data;
72         uint32_t iter = 0;
73
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)
76                         return -1;
77         }
78
79         return 0;
80 }
81 #endif
82
83 static int prox_sh_add(struct prox_shared *ps, const char *name, void *data)
84 {
85         char key[256] = {0};
86         int ret;
87
88         prox_strncpy(key, name, sizeof(key));
89         if (ps->size == 0) {
90                 prox_sh_create_hash(ps, INIT_HASH_TABLE_SIZE);
91         }
92
93 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,0)
94         do {
95                 ret = rte_hash_add_key_data(ps->hash, key, data);
96                 if (ret < 0) {
97                         struct rte_hash *old = ps->hash;
98                         int success;
99                         do {
100                                 prox_sh_create_hash(ps, ps->size * 2);
101                                 success = !copy_hash(ps->hash, old);
102                                 if (success)
103                                         rte_hash_free(old);
104                                 else
105                                         rte_hash_free(ps->hash);
106                         } while (!success);
107                 }
108         } while (ret < 0);
109 #else
110                 PROX_PANIC(1, "DPDK < 2.1 not fully supported");
111 #endif
112         return 0;
113 }
114
115 static void *prox_sh_find(struct prox_shared *sh, const char *name)
116 {
117 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,0)
118         char key[256] = {0};
119         int ret;
120         void *data;
121
122         if (!sh->hash)
123                 return NULL;
124
125         prox_strncpy(key, name, sizeof(key));
126         ret = rte_hash_lookup_data(sh->hash, key, &data);
127         if (ret >= 0)
128                 return data;
129 #else
130                 PROX_PANIC(1, "DPDK < 2.1 not fully supported");
131 #endif
132         return NULL;
133 }
134
135 int prox_sh_add_system(const char *name, void *data)
136 {
137         return prox_sh_add(&sh_system, name, data);
138 }
139
140 int prox_sh_add_socket(const int socket_id, const char *name, void *data)
141 {
142         if (socket_id >= MAX_SOCKETS)
143                 return -1;
144
145         return prox_sh_add(&sh_socket[socket_id], name, data);
146 }
147
148 int prox_sh_add_core(const int core_id, const char *name, void *data)
149 {
150         if (core_id >= RTE_MAX_LCORE)
151                 return -1;
152
153         return prox_sh_add(&sh_core[core_id], name, data);
154 }
155
156 void *prox_sh_find_system(const char *name)
157 {
158         return prox_sh_find(&sh_system, name);
159 }
160
161 void *prox_sh_find_socket(const int socket_id, const char *name)
162 {
163         if (socket_id >= MAX_SOCKETS)
164                 return NULL;
165
166         return prox_sh_find(&sh_socket[socket_id], name);
167 }
168
169 void *prox_sh_find_core(const int core_id, const char *name)
170 {
171         if (core_id >= RTE_MAX_LCORE)
172                 return NULL;
173
174         return prox_sh_find(&sh_core[core_id], name);
175 }