blob: d0b576fda7b63d2b34127c000c04dfe4da69e705 [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>
18
19/**
20 * Pool of session tables
21 */
22static session_table_t *lookup_tables;
23
24session_table_t *
Florin Coras6c36f532017-11-03 18:32:34 -070025_get_session_tables (void)
26{
27 return lookup_tables;
28}
29
30session_table_t *
Florin Corascea194d2017-10-02 00:18:51 -070031session_table_alloc (void)
32{
33 session_table_t *slt;
34 pool_get_aligned (lookup_tables, slt, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -040035 clib_memset (slt, 0, sizeof (*slt));
Florin Corascea194d2017-10-02 00:18:51 -070036 return slt;
37}
38
39u32
40session_table_index (session_table_t * slt)
41{
42 return (slt - lookup_tables);
43}
44
45session_table_t *
46session_table_get (u32 table_index)
47{
Florin Coras7ce3d132018-05-29 01:03:16 -070048 if (pool_is_free_index (lookup_tables, table_index))
Florin Corascea194d2017-10-02 00:18:51 -070049 return 0;
Florin Coras7ce3d132018-05-29 01:03:16 -070050 return pool_elt_at_index (lookup_tables, table_index);
Florin Corascea194d2017-10-02 00:18:51 -070051}
52
53#define foreach_hash_table_parameter \
54 _(v4,session,buckets,20000) \
55 _(v4,session,memory,(64<<20)) \
56 _(v6,session,buckets,20000) \
57 _(v6,session,memory,(64<<20)) \
58 _(v4,halfopen,buckets,20000) \
59 _(v4,halfopen,memory,(64<<20)) \
60 _(v6,halfopen,buckets,20000) \
61 _(v6,halfopen,memory,(64<<20))
62
63/**
64 * Initialize session table hash tables
65 *
66 * If vpp configured with set of table parameters it uses them,
67 * otherwise it uses defaults above.
68 */
69void
Florin Coras6c36f532017-11-03 18:32:34 -070070session_table_init (session_table_t * slt, u8 fib_proto)
Florin Corascea194d2017-10-02 00:18:51 -070071{
Florin Coras6c36f532017-11-03 18:32:34 -070072 u8 all = fib_proto > FIB_PROTOCOL_IP6 ? 1 : 0;
Florin Corasc97a7392017-11-05 23:07:07 -080073 int i;
Florin Coras6c36f532017-11-03 18:32:34 -070074
Florin Corascea194d2017-10-02 00:18:51 -070075#define _(af,table,parm,value) \
76 u32 configured_##af##_##table##_table_##parm = value;
77 foreach_hash_table_parameter;
78#undef _
79
80#define _(af,table,parm,value) \
Florin Coras31c99552019-03-01 13:00:58 -080081 if (session_main.configured_##af##_##table##_table_##parm) \
Florin Corascea194d2017-10-02 00:18:51 -070082 configured_##af##_##table##_table_##parm = \
Florin Coras31c99552019-03-01 13:00:58 -080083 session_main.configured_##af##_##table##_table_##parm;
Florin Corascea194d2017-10-02 00:18:51 -070084 foreach_hash_table_parameter;
85#undef _
86
Florin Coras6c36f532017-11-03 18:32:34 -070087 if (fib_proto == FIB_PROTOCOL_IP4 || all)
88 {
Florin Coras27eeb872019-09-03 13:06:11 -070089 clib_bihash_init2_args_16_8_t _a, *a = &_a;
90
91 memset (a, 0, sizeof (*a));
92 a->h = &slt->v4_session_hash;
93 a->name = "v4 session table";
94 a->nbuckets = configured_v4_session_table_buckets;
95 a->memory_size = configured_v4_session_table_memory;
96 a->dont_add_to_all_bihash_list = 1;
97 a->instantiate_immediately = 1;
98 clib_bihash_init2_16_8 (a);
99
100 memset (a, 0, sizeof (*a));
101 a->h = &slt->v4_half_open_hash;
102 a->name = "v4 half-open table";
103 a->nbuckets = configured_v4_halfopen_table_buckets;
104 a->memory_size = configured_v4_halfopen_table_memory;
105 a->dont_add_to_all_bihash_list = 1;
106 a->instantiate_immediately = 1;
107 clib_bihash_init2_16_8 (a);
Florin Coras6c36f532017-11-03 18:32:34 -0700108 }
109 if (fib_proto == FIB_PROTOCOL_IP6 || all)
110 {
Florin Coras27eeb872019-09-03 13:06:11 -0700111 clib_bihash_init2_args_48_8_t _a, *a = &_a;
112
113 memset (a, 0, sizeof (*a));
114 a->h = &slt->v6_session_hash;
115 a->name = "v6 session table";
116 a->nbuckets = configured_v6_session_table_buckets;
117 a->memory_size = configured_v6_session_table_memory;
118 a->dont_add_to_all_bihash_list = 1;
119 a->instantiate_immediately = 1;
120 clib_bihash_init2_48_8 (a);
121
122 memset (a, 0, sizeof (*a));
123 a->h = &slt->v6_half_open_hash;
124 a->name = "v6 half-open table";
125 a->nbuckets = configured_v6_halfopen_table_buckets;
126 a->memory_size = configured_v6_halfopen_table_memory;
127 a->dont_add_to_all_bihash_list = 1;
128 a->instantiate_immediately = 1;
129 clib_bihash_init2_48_8 (a);
Florin Coras6c36f532017-11-03 18:32:34 -0700130 }
Florin Corasc97a7392017-11-05 23:07:07 -0800131
Florin Coras07063b82020-03-13 04:44:51 +0000132 vec_validate (slt->session_rules, TRANSPORT_N_PROTOS - 1);
133 for (i = 0; i < TRANSPORT_N_PROTOS; i++)
Florin Corasc97a7392017-11-05 23:07:07 -0800134 session_rules_table_init (&slt->session_rules[i]);
Florin Corascea194d2017-10-02 00:18:51 -0700135}
136
137typedef struct _ip4_session_table_walk_ctx_t
138{
139 ip4_session_table_walk_fn_t fn;
140 void *ctx;
141} ip4_session_table_walk_ctx_t;
142
Neale Rannsf50bac12019-12-06 05:53:17 +0000143static int
Florin Corascea194d2017-10-02 00:18:51 -0700144ip4_session_table_walk_cb (clib_bihash_kv_16_8_t * kvp, void *arg)
145{
146 ip4_session_table_walk_ctx_t *ctx = arg;
147 ctx->fn (kvp, ctx->ctx);
Neale Rannsf50bac12019-12-06 05:53:17 +0000148 return (BIHASH_WALK_CONTINUE);
Florin Corascea194d2017-10-02 00:18:51 -0700149}
150
151void
152ip4_session_table_walk (clib_bihash_16_8_t * hash,
153 ip4_session_table_walk_fn_t fn, void *arg)
154{
155 ip4_session_table_walk_ctx_t ctx = {
156 .fn = fn,
157 .ctx = arg,
158 };
159 clib_bihash_foreach_key_value_pair_16_8 (hash, ip4_session_table_walk_cb,
160 &ctx);
161}
162
163/* *INDENT-ON* */
164/*
165 * fd.io coding-style-patch-verification: ON
166 *
167 * Local Variables:
168 * eval: (c-set-style "gnu")
169 * End:
170 */