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