blob: 5dafe0e633cc4f5ef9e7cf8c89852c883b8c0210 [file] [log] [blame]
Florin Corascea194d2017-10-02 00:18:51 -07001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Florin Corascea194d2017-10-02 00:18:51 -07003 * 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>
Steven Luongc4b5d102024-07-30 13:44:01 -070018#include <vnet/session/session_rules_table.h>
Florin Corascea194d2017-10-02 00:18:51 -070019
20/**
21 * Pool of session tables
22 */
23static session_table_t *lookup_tables;
24
25session_table_t *
Florin Coras6c36f532017-11-03 18:32:34 -070026_get_session_tables (void)
27{
28 return lookup_tables;
29}
30
31session_table_t *
Florin Corascea194d2017-10-02 00:18:51 -070032session_table_alloc (void)
33{
34 session_table_t *slt;
35 pool_get_aligned (lookup_tables, slt, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -040036 clib_memset (slt, 0, sizeof (*slt));
Florin Corascea194d2017-10-02 00:18:51 -070037 return slt;
38}
39
40u32
41session_table_index (session_table_t * slt)
42{
43 return (slt - lookup_tables);
44}
45
46session_table_t *
47session_table_get (u32 table_index)
48{
Florin Coras7ce3d132018-05-29 01:03:16 -070049 if (pool_is_free_index (lookup_tables, table_index))
Florin Corascea194d2017-10-02 00:18:51 -070050 return 0;
Florin Coras7ce3d132018-05-29 01:03:16 -070051 return pool_elt_at_index (lookup_tables, table_index);
Florin Corascea194d2017-10-02 00:18:51 -070052}
53
54#define foreach_hash_table_parameter \
55 _(v4,session,buckets,20000) \
56 _(v4,session,memory,(64<<20)) \
57 _(v6,session,buckets,20000) \
58 _(v6,session,memory,(64<<20)) \
59 _(v4,halfopen,buckets,20000) \
60 _(v4,halfopen,memory,(64<<20)) \
61 _(v6,halfopen,buckets,20000) \
62 _(v6,halfopen,memory,(64<<20))
63
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +020064void
65session_table_free (session_table_t *slt, u8 fib_proto)
66{
67 u8 all = fib_proto > FIB_PROTOCOL_IP6 ? 1 : 0;
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +020068
Steven Luongc4b5d102024-07-30 13:44:01 -070069 session_rules_table_free (slt, fib_proto);
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +020070
71 if (fib_proto == FIB_PROTOCOL_IP4 || all)
72 {
73 clib_bihash_free_16_8 (&slt->v4_session_hash);
74 clib_bihash_free_16_8 (&slt->v4_half_open_hash);
75 }
76 if (fib_proto == FIB_PROTOCOL_IP6 || all)
77 {
78 clib_bihash_free_48_8 (&slt->v6_session_hash);
79 clib_bihash_free_48_8 (&slt->v6_half_open_hash);
80 }
81
82 pool_put (lookup_tables, slt);
83}
84
Florin Corascea194d2017-10-02 00:18:51 -070085/**
86 * Initialize session table hash tables
87 *
88 * If vpp configured with set of table parameters it uses them,
89 * otherwise it uses defaults above.
90 */
91void
Steven Luongc4b5d102024-07-30 13:44:01 -070092session_table_init (session_table_t *slt, u8 fib_proto)
Florin Corascea194d2017-10-02 00:18:51 -070093{
Florin Coras6c36f532017-11-03 18:32:34 -070094 u8 all = fib_proto > FIB_PROTOCOL_IP6 ? 1 : 0;
95
Florin Corascea194d2017-10-02 00:18:51 -070096#define _(af,table,parm,value) \
97 u32 configured_##af##_##table##_table_##parm = value;
98 foreach_hash_table_parameter;
99#undef _
100
101#define _(af,table,parm,value) \
Florin Coras31c99552019-03-01 13:00:58 -0800102 if (session_main.configured_##af##_##table##_table_##parm) \
Florin Corascea194d2017-10-02 00:18:51 -0700103 configured_##af##_##table##_table_##parm = \
Florin Coras31c99552019-03-01 13:00:58 -0800104 session_main.configured_##af##_##table##_table_##parm;
Florin Corascea194d2017-10-02 00:18:51 -0700105 foreach_hash_table_parameter;
106#undef _
107
Steven Luongc4b5d102024-07-30 13:44:01 -0700108 slt->srtg_handle = SESSION_SRTG_HANDLE_INVALID;
Florin Coras6c36f532017-11-03 18:32:34 -0700109 if (fib_proto == FIB_PROTOCOL_IP4 || all)
110 {
Florin Coras27eeb872019-09-03 13:06:11 -0700111 clib_bihash_init2_args_16_8_t _a, *a = &_a;
112
113 memset (a, 0, sizeof (*a));
114 a->h = &slt->v4_session_hash;
115 a->name = "v4 session table";
116 a->nbuckets = configured_v4_session_table_buckets;
117 a->memory_size = configured_v4_session_table_memory;
118 a->dont_add_to_all_bihash_list = 1;
119 a->instantiate_immediately = 1;
120 clib_bihash_init2_16_8 (a);
121
122 memset (a, 0, sizeof (*a));
123 a->h = &slt->v4_half_open_hash;
124 a->name = "v4 half-open table";
125 a->nbuckets = configured_v4_halfopen_table_buckets;
126 a->memory_size = configured_v4_halfopen_table_memory;
127 a->dont_add_to_all_bihash_list = 1;
128 a->instantiate_immediately = 1;
129 clib_bihash_init2_16_8 (a);
Florin Coras6c36f532017-11-03 18:32:34 -0700130 }
131 if (fib_proto == FIB_PROTOCOL_IP6 || all)
132 {
Florin Coras27eeb872019-09-03 13:06:11 -0700133 clib_bihash_init2_args_48_8_t _a, *a = &_a;
134
135 memset (a, 0, sizeof (*a));
136 a->h = &slt->v6_session_hash;
137 a->name = "v6 session table";
138 a->nbuckets = configured_v6_session_table_buckets;
139 a->memory_size = configured_v6_session_table_memory;
140 a->dont_add_to_all_bihash_list = 1;
141 a->instantiate_immediately = 1;
142 clib_bihash_init2_48_8 (a);
143
144 memset (a, 0, sizeof (*a));
145 a->h = &slt->v6_half_open_hash;
146 a->name = "v6 half-open table";
147 a->nbuckets = configured_v6_halfopen_table_buckets;
148 a->memory_size = configured_v6_halfopen_table_memory;
149 a->dont_add_to_all_bihash_list = 1;
150 a->instantiate_immediately = 1;
151 clib_bihash_init2_48_8 (a);
Florin Coras6c36f532017-11-03 18:32:34 -0700152 }
Florin Corascea194d2017-10-02 00:18:51 -0700153}
154
155typedef struct _ip4_session_table_walk_ctx_t
156{
157 ip4_session_table_walk_fn_t fn;
158 void *ctx;
159} ip4_session_table_walk_ctx_t;
160
Neale Rannsf50bac12019-12-06 05:53:17 +0000161static int
Florin Corascea194d2017-10-02 00:18:51 -0700162ip4_session_table_walk_cb (clib_bihash_kv_16_8_t * kvp, void *arg)
163{
164 ip4_session_table_walk_ctx_t *ctx = arg;
165 ctx->fn (kvp, ctx->ctx);
Neale Rannsf50bac12019-12-06 05:53:17 +0000166 return (BIHASH_WALK_CONTINUE);
Florin Corascea194d2017-10-02 00:18:51 -0700167}
168
169void
170ip4_session_table_walk (clib_bihash_16_8_t * hash,
171 ip4_session_table_walk_fn_t fn, void *arg)
172{
173 ip4_session_table_walk_ctx_t ctx = {
174 .fn = fn,
175 .ctx = arg,
176 };
177 clib_bihash_foreach_key_value_pair_16_8 (hash, ip4_session_table_walk_cb,
178 &ctx);
179}
180
Florin Coras894d0a62023-11-17 16:35:04 -0800181u32
182session_table_memory_size (session_table_t *st)
183{
184 u64 total_size = 0;
185
186 if (clib_bihash_is_initialised_16_8 (&st->v4_session_hash))
187 {
188 clib_bihash_alloc_chunk_16_8_t *c = st->v4_session_hash.chunks;
189 while (c)
190 {
191 total_size += c->size;
192 c = c->next;
193 }
194 c = st->v4_half_open_hash.chunks;
195 while (c)
196 {
197 total_size += c->size;
198 c = c->next;
199 }
200 }
201
202 if (clib_bihash_is_initialised_48_8 (&st->v6_session_hash))
203 {
204 clib_bihash_alloc_chunk_48_8_t *c = st->v6_session_hash.chunks;
205 while (c)
206 {
207 total_size += c->size;
208 c = c->next;
209 }
210 c = st->v6_half_open_hash.chunks;
211 while (c)
212 {
213 total_size += c->size;
214 c = c->next;
215 }
216 }
217
218 return total_size;
219}
220
221u8 *
222format_session_table (u8 *s, va_list *args)
223{
224 session_table_t *st = va_arg (*args, session_table_t *);
225
226 if (clib_bihash_is_initialised_16_8 (&st->v4_session_hash))
227 {
228 s = format (s, "%U", format_bihash_16_8, &st->v4_session_hash, 0);
229 s = format (s, "%U", format_bihash_16_8, &st->v4_half_open_hash, 0);
230 }
231
232 if (clib_bihash_is_initialised_48_8 (&st->v6_session_hash))
233 {
234 s = format (s, "%U", format_bihash_48_8, &st->v6_session_hash, 0);
235 s = format (s, "%U", format_bihash_48_8, &st->v6_half_open_hash, 0);
236 }
237
238 return s;
239}
240
Florin Corascea194d2017-10-02 00:18:51 -0700241/*
242 * fd.io coding-style-patch-verification: ON
243 *
244 * Local Variables:
245 * eval: (c-set-style "gnu")
246 * End:
247 */