2 * Copyright (c) 2020 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/ip/ip.h>
17 #include <cnat/cnat_session.h>
18 #include <cnat/cnat_inline.h>
20 #include <vppinfra/bihash_template.h>
21 #include <vppinfra/bihash_template.c>
24 clib_bihash_40_48_t cnat_session_db;
25 void (*cnat_free_port_cb) (u16 port, ip_protocol_t iproto);
27 typedef struct cnat_session_walk_ctx_t_
29 cnat_session_walk_cb_t cb;
31 } cnat_session_walk_ctx_t;
34 cnat_session_walk_cb (BVT (clib_bihash_kv) * kv, void *arg)
36 cnat_session_t *session = (cnat_session_t *) kv;
37 cnat_session_walk_ctx_t *ctx = arg;
39 ctx->cb (session, ctx->ctx);
41 return (BIHASH_WALK_CONTINUE);
45 cnat_session_walk (cnat_session_walk_cb_t cb, void *ctx)
47 cnat_session_walk_ctx_t wctx = {
51 BV (clib_bihash_foreach_key_value_pair) (&cnat_session_db,
52 cnat_session_walk_cb, &wctx);
55 typedef struct cnat_session_purge_walk_t_
57 clib_bihash_kv_40_48_t *keys;
58 } cnat_session_purge_walk_ctx_t;
61 cnat_session_purge_walk (BVT (clib_bihash_kv) * key, void *arg)
63 cnat_session_purge_walk_ctx_t *ctx = arg;
65 vec_add1 (ctx->keys, *key);
67 return (BIHASH_WALK_CONTINUE);
71 format_cnat_session (u8 * s, va_list * args)
73 cnat_session_t *sess = va_arg (*args, cnat_session_t *);
74 CLIB_UNUSED (int verbose) = va_arg (*args, int);
76 if (!pool_is_free_index (cnat_timestamps, sess->value.cs_ts_index))
77 ts = cnat_timestamp_exp (sess->value.cs_ts_index);
81 "session:[%U;%d -> %U;%d, %U] => %U;%d -> %U;%d lb:%d age:%f",
82 format_ip46_address, &sess->key.cs_ip[VLIB_RX], IP46_TYPE_ANY,
83 clib_host_to_net_u16 (sess->key.cs_port[VLIB_RX]),
84 format_ip46_address, &sess->key.cs_ip[VLIB_TX], IP46_TYPE_ANY,
85 clib_host_to_net_u16 (sess->key.cs_port[VLIB_TX]),
86 format_ip_protocol, sess->key.cs_proto, format_ip46_address,
87 &sess->value.cs_ip[VLIB_RX], IP46_TYPE_ANY,
88 clib_host_to_net_u16 (sess->value.cs_port[VLIB_RX]),
89 format_ip46_address, &sess->value.cs_ip[VLIB_TX], IP46_TYPE_ANY,
90 clib_host_to_net_u16 (sess->value.cs_port[VLIB_TX]),
91 sess->value.cs_lbi, ts);
97 cnat_session_show (vlib_main_t * vm,
98 unformat_input_t * input, vlib_cli_command_t * cmd)
101 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
103 if (unformat (input, "verbose"))
106 return (clib_error_return (0, "unknown input '%U'",
107 format_unformat_error, input));
110 vlib_cli_output (vm, "CNat Sessions: now:%f\n%U\n",
112 BV (format_bihash), &cnat_session_db, verbose);
118 VLIB_CLI_COMMAND (cnat_session_show_cmd_node, static) = {
119 .path = "show cnat session",
120 .function = cnat_session_show,
121 .short_help = "show cnat session",
127 cnat_session_free (cnat_session_t * session)
129 clib_bihash_kv_40_48_t *bkey = (clib_bihash_kv_40_48_t *) session;
131 if (session->value.flags & CNAT_SESSION_FLAG_ALLOC_PORT)
132 cnat_free_port_cb (session->value.cs_port[VLIB_RX],
133 session->key.cs_proto);
134 if (!(session->value.flags & CNAT_SESSION_FLAG_NO_CLIENT))
135 cnat_client_free_by_ip (&session->key.cs_ip[VLIB_TX], session->key.cs_af);
136 cnat_timestamp_free (session->value.cs_ts_index);
138 clib_bihash_add_del_40_48 (&cnat_session_db, bkey, 0 /* is_add */ );
142 cnat_session_purge (void)
144 /* flush all the session from the DB */
145 cnat_session_purge_walk_ctx_t ctx = { };
146 clib_bihash_kv_40_48_t *key;
148 BV (clib_bihash_foreach_key_value_pair) (&cnat_session_db,
149 cnat_session_purge_walk, &ctx);
151 vec_foreach (key, ctx.keys) cnat_session_free ((cnat_session_t *) key);
159 cnat_session_scan (vlib_main_t * vm, f64 start_time, int i)
161 BVT (clib_bihash) * h = &cnat_session_db;
164 /* Don't scan the l2 fib if it hasn't been instantiated yet */
165 if (alloc_arena (h) == 0)
168 for (i = 0; i < h->nbuckets; i++)
170 /* allow no more than 100us without a pause */
171 if ((vlib_time_now (vm) - start_time) > 10e-5)
174 if (i < (h->nbuckets - 3))
176 BVT (clib_bihash_bucket) * b =
177 BV (clib_bihash_get_bucket) (h, i + 3);
178 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
179 b = BV (clib_bihash_get_bucket) (h, i + 1);
180 if (!BV (clib_bihash_bucket_is_empty) (b))
182 BVT (clib_bihash_value) * v =
183 BV (clib_bihash_get_value) (h, b->offset);
184 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
188 BVT (clib_bihash_bucket) * b = BV (clib_bihash_get_bucket) (h, i);
189 if (BV (clib_bihash_bucket_is_empty) (b))
191 BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset);
192 for (j = 0; j < (1 << b->log2_pages); j++)
194 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
196 if (v->kvp[k].key[0] == ~0ULL && v->kvp[k].value[0] == ~0ULL)
199 cnat_session_t *session = (cnat_session_t *) & v->kvp[k];
202 cnat_timestamp_exp (session->value.cs_ts_index))
205 cnat_session_free (session);
208 * Note: we may have just freed the bucket's backing
209 * storage, so check right here...
211 if (BV (clib_bihash_bucket_is_empty) (b))
225 static clib_error_t *
226 cnat_session_init (vlib_main_t * vm)
228 cnat_main_t *cm = &cnat_main;
229 BV (clib_bihash_init) (&cnat_session_db,
230 "CNat Session DB", cm->session_hash_buckets,
231 cm->session_hash_memory);
232 BV (clib_bihash_set_kvp_format_fn) (&cnat_session_db, format_cnat_session);
237 VLIB_INIT_FUNCTION (cnat_session_init);
239 static clib_error_t *
240 cnat_timestamp_show (vlib_main_t * vm,
241 unformat_input_t * input, vlib_cli_command_t * cmd)
243 cnat_timestamp_t *ts;
244 clib_rwlock_reader_lock (&cnat_main.ts_lock);
246 pool_foreach (ts, cnat_timestamps, ({
247 vlib_cli_output (vm, "[%d] last_seen:%f lifetime:%u ref:%u",
248 ts - cnat_timestamps,
249 ts->last_seen, ts->lifetime, ts->refcnt);
252 clib_rwlock_reader_unlock (&cnat_main.ts_lock);
257 VLIB_CLI_COMMAND (cnat_timestamp_show_cmd, static) = {
258 .path = "show cnat timestamp",
259 .function = cnat_timestamp_show,
260 .short_help = "show cnat timestamp",
266 * fd.io coding-style-patch-verification: ON
269 * eval: (c-set-style "gnu")