session: add support for application namespacing
[vpp.git] / src / vnet / session / session_table.c
1 /*
2  * Copyright (c) 2017 Cisco 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 <vnet/session/session_table.h>
17 #include <vnet/session/session.h>
18
19 /**
20  * Pool of session tables
21  */
22 static session_table_t *lookup_tables;
23
24 session_table_t *
25 session_table_alloc (void)
26 {
27   session_table_t *slt;
28   pool_get_aligned (lookup_tables, slt, CLIB_CACHE_LINE_BYTES);
29   memset (slt, 0, sizeof (*slt));
30   return slt;
31 }
32
33 u32
34 session_table_index (session_table_t * slt)
35 {
36   return (slt - lookup_tables);
37 }
38
39 session_table_t *
40 session_table_get (u32 table_index)
41 {
42   if (vec_len (lookup_tables) <= table_index)
43     return 0;
44   return vec_elt_at_index (lookup_tables, table_index);
45 }
46
47 #define foreach_hash_table_parameter            \
48   _(v4,session,buckets,20000)                   \
49   _(v4,session,memory,(64<<20))                 \
50   _(v6,session,buckets,20000)                   \
51   _(v6,session,memory,(64<<20))                 \
52   _(v4,halfopen,buckets,20000)                  \
53   _(v4,halfopen,memory,(64<<20))                \
54   _(v6,halfopen,buckets,20000)                  \
55   _(v6,halfopen,memory,(64<<20))
56
57 /**
58  * Initialize session table hash tables
59  *
60  * If vpp configured with set of table parameters it uses them,
61  * otherwise it uses defaults above.
62  */
63 void
64 session_table_init (session_table_t * slt)
65 {
66 #define _(af,table,parm,value)                                          \
67   u32 configured_##af##_##table##_table_##parm = value;
68   foreach_hash_table_parameter;
69 #undef _
70
71 #define _(af,table,parm,value)                                          \
72   if (session_manager_main.configured_##af##_##table##_table_##parm)    \
73     configured_##af##_##table##_table_##parm =                          \
74       session_manager_main.configured_##af##_##table##_table_##parm;
75   foreach_hash_table_parameter;
76 #undef _
77
78   clib_bihash_init_16_8 (&slt->v4_session_hash, "v4 session table",
79                          configured_v4_session_table_buckets,
80                          configured_v4_session_table_memory);
81   clib_bihash_init_48_8 (&slt->v6_session_hash, "v6 session table",
82                          configured_v6_session_table_buckets,
83                          configured_v6_session_table_memory);
84   clib_bihash_init_16_8 (&slt->v4_half_open_hash, "v4 half-open table",
85                          configured_v4_halfopen_table_buckets,
86                          configured_v4_halfopen_table_memory);
87   clib_bihash_init_48_8 (&slt->v6_half_open_hash, "v6 half-open table",
88                          configured_v6_halfopen_table_buckets,
89                          configured_v6_halfopen_table_memory);
90 }
91
92 typedef struct _ip4_session_table_walk_ctx_t
93 {
94   ip4_session_table_walk_fn_t fn;
95   void *ctx;
96 } ip4_session_table_walk_ctx_t;
97
98 void
99 ip4_session_table_walk_cb (clib_bihash_kv_16_8_t * kvp, void *arg)
100 {
101   ip4_session_table_walk_ctx_t *ctx = arg;
102   ctx->fn (kvp, ctx->ctx);
103 }
104
105 void
106 ip4_session_table_walk (clib_bihash_16_8_t * hash,
107                         ip4_session_table_walk_fn_t fn, void *arg)
108 {
109   ip4_session_table_walk_ctx_t ctx = {
110     .fn = fn,
111     .ctx = arg,
112   };
113   clib_bihash_foreach_key_value_pair_16_8 (hash, ip4_session_table_walk_cb,
114                                            &ctx);
115 }
116
117 /* *INDENT-ON* */
118 /*
119  * fd.io coding-style-patch-verification: ON
120  *
121  * Local Variables:
122  * eval: (c-set-style "gnu")
123  * End:
124  */