hs-test: more debug output in http3 test
[vpp.git] / src / plugins / cnat / cnat_session.c
1 /*
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:
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/ip/ip.h>
17 #include <cnat/cnat_session.h>
18 #include <cnat/cnat_inline.h>
19
20 #include <vppinfra/bihash_template.h>
21 #include <vppinfra/bihash_template.c>
22
23 cnat_bihash_t cnat_session_db;
24 void (*cnat_free_port_cb) (u16 port, ip_protocol_t iproto);
25
26 typedef struct cnat_session_walk_ctx_t_
27 {
28   cnat_session_walk_cb_t cb;
29   void *ctx;
30 } cnat_session_walk_ctx_t;
31
32 static int
33 cnat_session_walk_cb (BVT (clib_bihash_kv) * kv, void *arg)
34 {
35   cnat_session_t *session = (cnat_session_t *) kv;
36   cnat_session_walk_ctx_t *ctx = arg;
37
38   ctx->cb (session, ctx->ctx);
39
40   return (BIHASH_WALK_CONTINUE);
41 }
42
43 void
44 cnat_session_walk (cnat_session_walk_cb_t cb, void *ctx)
45 {
46   cnat_session_walk_ctx_t wctx = {
47     .cb = cb,
48     .ctx = ctx,
49   };
50   BV (clib_bihash_foreach_key_value_pair) (&cnat_session_db,
51                                            cnat_session_walk_cb, &wctx);
52 }
53
54 typedef struct cnat_session_purge_walk_t_
55 {
56   cnat_bihash_kv_t *keys;
57 } cnat_session_purge_walk_ctx_t;
58
59 static int
60 cnat_session_purge_walk (BVT (clib_bihash_kv) * key, void *arg)
61 {
62   cnat_session_purge_walk_ctx_t *ctx = arg;
63
64   vec_add1 (ctx->keys, *key);
65
66   return (BIHASH_WALK_CONTINUE);
67 }
68
69 u8 *
70 format_cnat_session_location (u8 *s, va_list *args)
71 {
72   u8 location = va_arg (*args, int);
73   switch (location)
74     {
75     case CNAT_LOCATION_INPUT:
76       s = format (s, "input");
77       break;
78     case CNAT_LOCATION_OUTPUT:
79       s = format (s, "output");
80       break;
81     case CNAT_LOCATION_FIB:
82       s = format (s, "fib");
83       break;
84     default:
85       s = format (s, "unknown");
86       break;
87     }
88   return (s);
89 }
90
91 u8 *
92 format_cnat_session (u8 * s, va_list * args)
93 {
94   cnat_session_t *sess = va_arg (*args, cnat_session_t *);
95   CLIB_UNUSED (int verbose) = va_arg (*args, int);
96   f64 ts = 0;
97
98   if (!cnat_ts_is_free_index (sess->value.cs_ts_index))
99     ts = cnat_timestamp_exp (sess->value.cs_ts_index);
100
101   s = format (
102     s, "session:[%U;%d -> %U;%d, %U] => %U;%d -> %U;%d %U lb:%d age:%f",
103     format_ip46_address, &sess->key.cs_ip[VLIB_RX], IP46_TYPE_ANY,
104     clib_host_to_net_u16 (sess->key.cs_port[VLIB_RX]), format_ip46_address,
105     &sess->key.cs_ip[VLIB_TX], IP46_TYPE_ANY,
106     clib_host_to_net_u16 (sess->key.cs_port[VLIB_TX]), format_ip_protocol,
107     sess->key.cs_proto, format_ip46_address, &sess->value.cs_ip[VLIB_RX],
108     IP46_TYPE_ANY, clib_host_to_net_u16 (sess->value.cs_port[VLIB_RX]),
109     format_ip46_address, &sess->value.cs_ip[VLIB_TX], IP46_TYPE_ANY,
110     clib_host_to_net_u16 (sess->value.cs_port[VLIB_TX]),
111     format_cnat_session_location, sess->key.cs_loc, sess->value.cs_lbi, ts);
112
113   return (s);
114 }
115
116 static clib_error_t *
117 cnat_session_show (vlib_main_t * vm,
118                    unformat_input_t * input, vlib_cli_command_t * cmd)
119 {
120   u8 verbose = 0;
121   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
122     {
123       if (unformat (input, "verbose"))
124         verbose = 1;
125       else
126         return (clib_error_return (0, "unknown input '%U'",
127                                    format_unformat_error, input));
128     }
129
130   vlib_cli_output (vm, "CNat Sessions: now:%f\n%U\n",
131                    vlib_time_now (vm),
132                    BV (format_bihash), &cnat_session_db, verbose);
133
134   return (NULL);
135 }
136
137 VLIB_CLI_COMMAND (cnat_session_show_cmd_node, static) = {
138   .path = "show cnat session",
139   .function = cnat_session_show,
140   .short_help = "show cnat session",
141   .is_mp_safe = 1,
142 };
143
144 void
145 cnat_session_free (cnat_session_t * session)
146 {
147   cnat_bihash_kv_t *bkey = (cnat_bihash_kv_t *) session;
148   /* age it */
149   if (session->value.flags & CNAT_SESSION_FLAG_ALLOC_PORT)
150     cnat_free_port_cb (session->value.cs_port[VLIB_RX],
151                        session->key.cs_proto);
152   if (!(session->value.flags & CNAT_SESSION_FLAG_NO_CLIENT))
153     cnat_client_free_by_ip (&session->key.cs_ip[VLIB_TX], session->key.cs_af);
154   cnat_timestamp_free (session->value.cs_ts_index);
155
156   cnat_bihash_add_del (&cnat_session_db, bkey, 0 /* is_add */);
157 }
158
159 int
160 cnat_session_purge (void)
161 {
162   /* flush all the session from the DB */
163   cnat_session_purge_walk_ctx_t ctx = { };
164   cnat_bihash_kv_t *key;
165
166   BV (clib_bihash_foreach_key_value_pair) (&cnat_session_db,
167                                            cnat_session_purge_walk, &ctx);
168
169   vec_foreach (key, ctx.keys) cnat_session_free ((cnat_session_t *) key);
170
171   vec_free (ctx.keys);
172
173   return (0);
174 }
175
176 void
177 cnat_reverse_session_free (cnat_session_t *session)
178 {
179   cnat_bihash_kv_t bkey, bvalue;
180   cnat_session_t *rsession = (cnat_session_t *) &bkey;
181   int rv;
182
183   ip46_address_copy (&rsession->key.cs_ip[VLIB_RX],
184                      &session->value.cs_ip[VLIB_TX]);
185   ip46_address_copy (&rsession->key.cs_ip[VLIB_TX],
186                      &session->value.cs_ip[VLIB_RX]);
187   rsession->key.cs_proto = session->key.cs_proto;
188   rsession->key.cs_loc = session->key.cs_loc == CNAT_LOCATION_OUTPUT ?
189                            CNAT_LOCATION_INPUT :
190                            CNAT_LOCATION_OUTPUT;
191   rsession->key.__cs_pad = 0;
192   rsession->key.cs_af = session->key.cs_af;
193   rsession->key.cs_port[VLIB_RX] = session->value.cs_port[VLIB_TX];
194   rsession->key.cs_port[VLIB_TX] = session->value.cs_port[VLIB_RX];
195
196   rv = cnat_bihash_search_i2 (&cnat_session_db, &bkey, &bvalue);
197   if (!rv)
198     {
199       /* other session is in bihash */
200       cnat_session_t *rsession = (cnat_session_t *) &bvalue;
201       cnat_session_free (rsession);
202     }
203 }
204
205 u64
206 cnat_session_scan (vlib_main_t * vm, f64 start_time, int i)
207 {
208   BVT (clib_bihash) * h = &cnat_session_db;
209   int j, k;
210
211   if (alloc_arena (h) == 0)
212     return 0;
213
214   for ( /* caller saves starting point */ ; i < h->nbuckets; i++)
215     {
216       /* allow no more than 100us without a pause */
217       if ((vlib_time_now (vm) - start_time) > 10e-5)
218         return (i);
219
220       if (i < (h->nbuckets - 3))
221         {
222           BVT (clib_bihash_bucket) * b =
223             BV (clib_bihash_get_bucket) (h, i + 3);
224           clib_prefetch_load (b);
225           b = BV (clib_bihash_get_bucket) (h, i + 1);
226           if (!BV (clib_bihash_bucket_is_empty) (b))
227             {
228               BVT (clib_bihash_value) * v =
229                 BV (clib_bihash_get_value) (h, b->offset);
230               clib_prefetch_load (v);
231             }
232         }
233
234       BVT (clib_bihash_bucket) * b = BV (clib_bihash_get_bucket) (h, i);
235       if (BV (clib_bihash_bucket_is_empty) (b))
236         continue;
237       BVT (clib_bihash_value) * v = BV (clib_bihash_get_value) (h, b->offset);
238       for (j = 0; j < (1 << b->log2_pages); j++)
239         {
240           for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
241             {
242               if (BV (clib_bihash_is_free) (&v->kvp[k]))
243                 continue;
244
245               cnat_session_t *session = (cnat_session_t *) & v->kvp[k];
246
247               if (start_time >
248                   cnat_timestamp_exp (session->value.cs_ts_index))
249                 {
250                   /* age it */
251                   cnat_reverse_session_free (session);
252                   /* this should be last as deleting the session memset it to
253                    * 0xff */
254                   cnat_session_free (session);
255
256                   /*
257                    * Note: we may have just freed the bucket's backing
258                    * storage, so check right here...
259                    */
260                   if (BV (clib_bihash_bucket_is_empty) (b))
261                     goto doublebreak;
262                 }
263             }
264           v++;
265         }
266     doublebreak:
267       ;
268     }
269
270   /* start again */
271   return (0);
272 }
273
274 static clib_error_t *
275 cnat_session_init (vlib_main_t * vm)
276 {
277   cnat_main_t *cm = &cnat_main;
278   BV (clib_bihash_init) (&cnat_session_db,
279                          "CNat Session DB", cm->session_hash_buckets,
280                          cm->session_hash_memory);
281   BV (clib_bihash_set_kvp_format_fn) (&cnat_session_db, format_cnat_session);
282
283   cnat_timestamps.next_empty_pool_idx = 0;
284   clib_bitmap_alloc (cnat_timestamps.ts_free, 1 << CNAT_TS_MPOOL_BITS);
285   clib_bitmap_set_region (cnat_timestamps.ts_free, 0, 1,
286                           1 << CNAT_TS_MPOOL_BITS);
287   clib_spinlock_init (&cnat_timestamps.ts_lock);
288
289   return (NULL);
290 }
291
292 VLIB_INIT_FUNCTION (cnat_session_init);
293
294 static clib_error_t *
295 cnat_timestamp_show (vlib_main_t * vm,
296                      unformat_input_t * input, vlib_cli_command_t * cmd)
297 {
298   cnat_timestamp_t *ts;
299   int ts_cnt = 0, cnt;
300   u8 verbose = 0;
301   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
302     {
303       if (unformat (input, "verbose"))
304         verbose = 1;
305       else
306         return (clib_error_return (0, "unknown input '%U'",
307                                    format_unformat_error, input));
308     }
309
310   for (int i = 0; i < cnat_timestamps.next_empty_pool_idx; i++)
311     {
312       cnt = pool_elts (cnat_timestamps.ts_pools[i]);
313       ts_cnt += cnt;
314       vlib_cli_output (vm, "-- Pool %d [%d/%d]", i, cnt,
315                        pool_header (cnat_timestamps.ts_pools[i])->max_elts);
316       if (!verbose)
317         continue;
318       pool_foreach (ts, cnat_timestamps.ts_pools[i])
319         vlib_cli_output (vm, "[%d] last_seen:%f lifetime:%u ref:%u",
320                          ts - cnat_timestamps.ts_pools[i], ts->last_seen,
321                          ts->lifetime, ts->refcnt);
322     }
323   vlib_cli_output (vm, "Total timestamps %d", ts_cnt);
324   return (NULL);
325 }
326
327 VLIB_CLI_COMMAND (cnat_timestamp_show_cmd, static) = {
328   .path = "show cnat timestamp",
329   .function = cnat_timestamp_show,
330   .short_help = "show cnat timestamp [verbose]",
331   .is_mp_safe = 1,
332 };
333
334 /*
335  * fd.io coding-style-patch-verification: ON
336  *
337  * Local Variables:
338  * eval: (c-set-style "gnu")
339  * End:
340  */