session: add api to dump rules
[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 _get_session_tables (void)
26 {
27   return lookup_tables;
28 }
29
30 session_table_t *
31 session_table_alloc (void)
32 {
33   session_table_t *slt;
34   pool_get_aligned (lookup_tables, slt, CLIB_CACHE_LINE_BYTES);
35   memset (slt, 0, sizeof (*slt));
36   return slt;
37 }
38
39 u32
40 session_table_index (session_table_t * slt)
41 {
42   return (slt - lookup_tables);
43 }
44
45 session_table_t *
46 session_table_get (u32 table_index)
47 {
48   if (vec_len (lookup_tables) <= table_index)
49     return 0;
50   return vec_elt_at_index (lookup_tables, table_index);
51 }
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  */
69 void
70 session_table_init (session_table_t * slt, u8 fib_proto)
71 {
72   u8 all = fib_proto > FIB_PROTOCOL_IP6 ? 1 : 0;
73
74 #define _(af,table,parm,value)                                          \
75   u32 configured_##af##_##table##_table_##parm = value;
76   foreach_hash_table_parameter;
77 #undef _
78
79 #define _(af,table,parm,value)                                          \
80   if (session_manager_main.configured_##af##_##table##_table_##parm)    \
81     configured_##af##_##table##_table_##parm =                          \
82       session_manager_main.configured_##af##_##table##_table_##parm;
83   foreach_hash_table_parameter;
84 #undef _
85
86   if (fib_proto == FIB_PROTOCOL_IP4 || all)
87     {
88       clib_bihash_init_16_8 (&slt->v4_session_hash, "v4 session table",
89                              configured_v4_session_table_buckets,
90                              configured_v4_session_table_memory);
91       clib_bihash_init_16_8 (&slt->v4_half_open_hash, "v4 half-open table",
92                              configured_v4_halfopen_table_buckets,
93                              configured_v4_halfopen_table_memory);
94     }
95   if (fib_proto == FIB_PROTOCOL_IP6 || all)
96     {
97       clib_bihash_init_48_8 (&slt->v6_session_hash, "v6 session table",
98                              configured_v6_session_table_buckets,
99                              configured_v6_session_table_memory);
100       clib_bihash_init_48_8 (&slt->v6_half_open_hash, "v6 half-open table",
101                              configured_v6_halfopen_table_buckets,
102                              configured_v6_halfopen_table_memory);
103     }
104   session_rules_table_init (&slt->session_rules);
105 }
106
107 typedef struct _ip4_session_table_walk_ctx_t
108 {
109   ip4_session_table_walk_fn_t fn;
110   void *ctx;
111 } ip4_session_table_walk_ctx_t;
112
113 void
114 ip4_session_table_walk_cb (clib_bihash_kv_16_8_t * kvp, void *arg)
115 {
116   ip4_session_table_walk_ctx_t *ctx = arg;
117   ctx->fn (kvp, ctx->ctx);
118 }
119
120 void
121 ip4_session_table_walk (clib_bihash_16_8_t * hash,
122                         ip4_session_table_walk_fn_t fn, void *arg)
123 {
124   ip4_session_table_walk_ctx_t ctx = {
125     .fn = fn,
126     .ctx = arg,
127   };
128   clib_bihash_foreach_key_value_pair_16_8 (hash, ip4_session_table_walk_cb,
129                                            &ctx);
130 }
131
132 /* *INDENT-ON* */
133 /*
134  * fd.io coding-style-patch-verification: ON
135  *
136  * Local Variables:
137  * eval: (c-set-style "gnu")
138  * End:
139  */