wireguard: add barrier to sync data
[vpp.git] / src / plugins / wireguard / wireguard_index_table.c
1 /*
2  * Copyright (c) 2020 Doc.ai and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vlib/vlib.h>
17 #include <vppinfra/hash.h>
18 #include <vppinfra/pool.h>
19 #include <vppinfra/random.h>
20 #include <wireguard/wireguard_index_table.h>
21
22 u32
23 wg_index_table_add (vlib_main_t *vm, wg_index_table_t *table,
24                     u32 peer_pool_idx, u32 rnd_seed)
25 {
26   u32 key;
27
28   while (1)
29     {
30       key = random_u32 (&rnd_seed);
31       if (hash_get (table->hash, key))
32         continue;
33
34       vlib_worker_thread_barrier_sync (vm);
35       hash_set (table->hash, key, peer_pool_idx);
36       vlib_worker_thread_barrier_release (vm);
37       break;
38     }
39   return key;
40 }
41
42 void
43 wg_index_table_del (vlib_main_t *vm, wg_index_table_t *table, u32 key)
44 {
45   uword *p;
46   p = hash_get (table->hash, key);
47   if (p)
48     {
49       vlib_worker_thread_barrier_sync (vm);
50       hash_unset (table->hash, key);
51       vlib_worker_thread_barrier_release (vm);
52     }
53 }
54
55 u32 *
56 wg_index_table_lookup (const wg_index_table_t * table, u32 key)
57 {
58   uword *p;
59
60   p = hash_get (table->hash, key);
61   return (u32 *) p;
62 }
63
64 /*
65  * fd.io coding-style-patch-verification: ON
66  *
67  * Local Variables:
68  * eval: (c-set-style "gnu")
69  * End:
70  */