nat: timed out session scavenging upgrade
[vpp.git] / src / plugins / nat / nat_format.c
1 /*
2  * Copyright (c) 2018 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  * @file
17  * @brief NAT formatting
18  */
19
20 #include <nat/nat.h>
21 #include <nat/nat_inlines.h>
22 #include <nat/nat_det.h>
23
24 uword
25 unformat_snat_protocol (unformat_input_t * input, va_list * args)
26 {
27   u32 *r = va_arg (*args, u32 *);
28
29   if (0);
30 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
31   foreach_snat_protocol
32 #undef _
33     else
34     return 0;
35   return 1;
36 }
37
38 u8 *
39 format_snat_protocol (u8 * s, va_list * args)
40 {
41   u32 i = va_arg (*args, u32);
42   u8 *t = 0;
43
44   switch (i)
45     {
46 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
47       foreach_snat_protocol
48 #undef _
49     default:
50       s = format (s, "unknown");
51       return s;
52     }
53   s = format (s, "%s", t);
54   return s;
55 }
56
57 u8 *
58 format_nat_addr_and_port_alloc_alg (u8 * s, va_list * args)
59 {
60   u32 i = va_arg (*args, u32);
61   u8 *t = 0;
62
63   switch (i)
64     {
65 #define _(v, N, s) case NAT_ADDR_AND_PORT_ALLOC_ALG_##N: t = (u8 *) s; break;
66       foreach_nat_addr_and_port_alloc_alg
67 #undef _
68     default:
69       s = format (s, "unknown");
70       return s;
71     }
72   s = format (s, "%s", t);
73   return s;
74 }
75
76 u8 *
77 format_snat_key (u8 * s, va_list * args)
78 {
79   snat_session_key_t *key = va_arg (*args, snat_session_key_t *);
80
81   s = format (s, "%U proto %U port %d fib %d",
82               format_ip4_address, &key->addr,
83               format_snat_protocol, key->protocol,
84               clib_net_to_host_u16 (key->port), key->fib_index);
85   return s;
86 }
87
88 u8 *
89 format_static_mapping_key (u8 * s, va_list * args)
90 {
91   snat_session_key_t *key = va_arg (*args, snat_session_key_t *);
92
93   s = format (s, "%U proto %U port %d fib %d",
94               format_ip4_address, &key->addr,
95               format_snat_protocol, key->protocol, key->port, key->fib_index);
96   return s;
97 }
98
99 u8 *
100 format_snat_session_state (u8 * s, va_list * args)
101 {
102   u32 i = va_arg (*args, u32);
103   u8 *t = 0;
104
105   switch (i)
106     {
107 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
108       foreach_snat_session_state
109 #undef _
110     default:
111       t = format (t, "unknown");
112     }
113   s = format (s, "%s", t);
114   return s;
115 }
116
117 u8 *
118 format_snat_session (u8 * s, va_list * args)
119 {
120   snat_main_per_thread_data_t *sm =
121     va_arg (*args, snat_main_per_thread_data_t *);
122   snat_session_t *sess = va_arg (*args, snat_session_t *);
123
124   if (snat_is_unk_proto_session (sess))
125     {
126       s = format (s, "  i2o %U proto %u fib %u\n",
127                   format_ip4_address, &sess->in2out.addr,
128                   sess->in2out.port, sess->in2out.fib_index);
129       s = format (s, "    o2i %U proto %u fib %u\n",
130                   format_ip4_address, &sess->out2in.addr,
131                   sess->out2in.port, sess->out2in.fib_index);
132     }
133   else
134     {
135       s = format (s, "  i2o %U\n", format_snat_key, &sess->in2out);
136       s = format (s, "    o2i %U\n", format_snat_key, &sess->out2in);
137     }
138   if (is_ed_session (sess) || is_fwd_bypass_session (sess))
139     {
140       if (is_twice_nat_session (sess))
141         {
142           s = format (s, "       external host o2i %U:%d i2o %U:%d\n",
143                       format_ip4_address, &sess->ext_host_addr,
144                       clib_net_to_host_u16 (sess->ext_host_port),
145                       format_ip4_address, &sess->ext_host_nat_addr,
146                       clib_net_to_host_u16 (sess->ext_host_nat_port));
147         }
148       else
149         {
150           if (sess->ext_host_addr.as_u32)
151             s = format (s, "       external host %U:%u\n",
152                         format_ip4_address, &sess->ext_host_addr,
153                         clib_net_to_host_u16 (sess->ext_host_port));
154         }
155     }
156   s = format (s, "       index %llu\n", sess - sm->sessions);
157   s = format (s, "       last heard %.2f\n", sess->last_heard);
158   s = format (s, "       total pkts %d, total bytes %lld\n",
159               sess->total_pkts, sess->total_bytes);
160   if (snat_is_session_static (sess))
161     s = format (s, "       static translation\n");
162   else
163     s = format (s, "       dynamic translation\n");
164   if (is_fwd_bypass_session (sess))
165     s = format (s, "       forwarding-bypass\n");
166   if (is_lb_session (sess))
167     s = format (s, "       load-balancing\n");
168   if (is_twice_nat_session (sess))
169     s = format (s, "       twice-nat\n");
170
171   return s;
172 }
173
174 u8 *
175 format_snat_user (u8 * s, va_list * args)
176 {
177
178   snat_main_t *sm = &snat_main;
179   snat_main_per_thread_data_t *tsm =
180     va_arg (*args, snat_main_per_thread_data_t *);
181   snat_user_t *u = va_arg (*args, snat_user_t *);
182   int verbose = va_arg (*args, int);
183   dlist_elt_t *head, *elt;
184   u32 elt_index, head_index;
185   u32 session_index;
186   snat_session_t *sess;
187
188   s = format (s, "%U: %d dynamic translations, %d static translations\n",
189               format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
190
191   if (verbose == 0)
192     return s;
193
194   if (u->nsessions || u->nstaticsessions)
195     {
196       head_index = u->sessions_per_user_list_head_index;
197       head = pool_elt_at_index (tsm->list_pool, head_index);
198
199       elt_index = head->next;
200       elt = pool_elt_at_index (tsm->list_pool, elt_index);
201       session_index = elt->value;
202
203       while (session_index != ~0)
204         {
205           sess = pool_elt_at_index (tsm->sessions, session_index);
206
207           s = format (s, "  %U\n", format_snat_session, sm, sess);
208
209           elt_index = elt->next;
210           elt = pool_elt_at_index (tsm->list_pool, elt_index);
211           session_index = elt->value;
212         }
213     }
214
215   return s;
216 }
217
218 u8 *
219 format_snat_user_v2 (u8 * s, va_list * args)
220 {
221
222   snat_main_t *sm = &snat_main;
223   snat_main_per_thread_data_t *tsm =
224     va_arg (*args, snat_main_per_thread_data_t *);
225   snat_user_t *u = va_arg (*args, snat_user_t *);
226   u64 now = va_arg (*args, u64);
227
228   dlist_elt_t *head, *elt;
229   u32 elt_index, head_index;
230   u32 session_index;
231   snat_session_t *sess;
232
233   u32 udp_sessions = 0;
234   u32 tcp_sessions = 0;
235   u32 icmp_sessions = 0;
236
237   u32 timed_out = 0;
238   u32 transitory = 0;
239   u32 established = 0;
240
241   u64 sess_timeout_time;
242
243   if (u->nsessions || u->nstaticsessions)
244     {
245       head_index = u->sessions_per_user_list_head_index;
246       head = pool_elt_at_index (tsm->list_pool, head_index);
247
248       elt_index = head->next;
249       elt = pool_elt_at_index (tsm->list_pool, elt_index);
250       session_index = elt->value;
251
252       while (session_index != ~0)
253         {
254           sess = pool_elt_at_index (tsm->sessions, session_index);
255
256           sess_timeout_time = sess->last_heard +
257             (f64) nat44_session_get_timeout (sm, sess);
258           if (now >= sess_timeout_time)
259             timed_out++;
260
261           switch (sess->in2out.protocol)
262             {
263             case SNAT_PROTOCOL_ICMP:
264               icmp_sessions++;
265               break;
266             case SNAT_PROTOCOL_TCP:
267               tcp_sessions++;
268               if (sess->state)
269                 transitory++;
270               else
271                 established++;
272               break;
273             case SNAT_PROTOCOL_UDP:
274             default:
275               udp_sessions++;
276               break;
277
278             }
279
280           elt_index = elt->next;
281           elt = pool_elt_at_index (tsm->list_pool, elt_index);
282           session_index = elt->value;
283         }
284     }
285
286   s = format (s, "%U: %d dynamic translations, %d static translations\n",
287               format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
288   s = format (s, "\t%u timed out, %u transitory, %u established\n",
289               timed_out, transitory, established);
290   s = format (s, "\t%u tcp sessions, %u udp sessions, %u icmp sessions\n",
291               tcp_sessions, udp_sessions, icmp_sessions);
292
293   return s;
294 }
295
296 u8 *
297 format_snat_static_mapping (u8 * s, va_list * args)
298 {
299   snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
300   nat44_lb_addr_port_t *local;
301
302   if (is_identity_static_mapping (m))
303     {
304       if (is_addr_only_static_mapping (m))
305         s = format (s, "identity mapping %U",
306                     format_ip4_address, &m->local_addr);
307       else
308         s = format (s, "identity mapping %U %U:%d",
309                     format_snat_protocol, m->proto,
310                     format_ip4_address, &m->local_addr, m->local_port);
311
312       /* *INDENT-OFF* */
313       pool_foreach (local, m->locals,
314       ({
315         s = format (s, " vrf %d", local->vrf_id);
316       }));
317       /* *INDENT-ON* */
318
319       return s;
320     }
321
322   if (is_addr_only_static_mapping (m))
323     s = format (s, "local %U external %U vrf %d %s %s",
324                 format_ip4_address, &m->local_addr,
325                 format_ip4_address, &m->external_addr,
326                 m->vrf_id,
327                 m->twice_nat == TWICE_NAT ? "twice-nat" :
328                 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
329                 is_out2in_only_static_mapping (m) ? "out2in-only" : "");
330   else
331     {
332       if (is_lb_static_mapping (m))
333         {
334           s = format (s, "%U external %U:%d %s %s",
335                       format_snat_protocol, m->proto,
336                       format_ip4_address, &m->external_addr, m->external_port,
337                       m->twice_nat == TWICE_NAT ? "twice-nat" :
338                       m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
339                       is_out2in_only_static_mapping (m) ? "out2in-only" : "");
340
341           /* *INDENT-OFF* */
342           pool_foreach (local, m->locals,
343           ({
344             s = format (s, "\n  local %U:%d vrf %d probability %d\%",
345                         format_ip4_address, &local->addr, local->port,
346                         local->vrf_id, local->probability);
347           }));
348           /* *INDENT-ON* */
349
350         }
351       else
352         s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
353                     format_snat_protocol, m->proto,
354                     format_ip4_address, &m->local_addr, m->local_port,
355                     format_ip4_address, &m->external_addr, m->external_port,
356                     m->vrf_id,
357                     m->twice_nat == TWICE_NAT ? "twice-nat" :
358                     m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
359                     is_out2in_only_static_mapping (m) ? "out2in-only" : "");
360     }
361   return s;
362 }
363
364 u8 *
365 format_snat_static_map_to_resolve (u8 * s, va_list * args)
366 {
367   snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
368   vnet_main_t *vnm = vnet_get_main ();
369
370   if (m->addr_only)
371     s = format (s, "local %U external %U vrf %d",
372                 format_ip4_address, &m->l_addr,
373                 format_vnet_sw_if_index_name, vnm, m->sw_if_index, m->vrf_id);
374   else
375     s = format (s, "%U local %U:%d external %U:%d vrf %d",
376                 format_snat_protocol, m->proto,
377                 format_ip4_address, &m->l_addr, m->l_port,
378                 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
379                 m->e_port, m->vrf_id);
380
381   return s;
382 }
383
384 u8 *
385 format_det_map_ses (u8 * s, va_list * args)
386 {
387   snat_det_map_t *det_map = va_arg (*args, snat_det_map_t *);
388   ip4_address_t in_addr, out_addr;
389   u32 in_offset, out_offset;
390   snat_det_session_t *ses = va_arg (*args, snat_det_session_t *);
391   u32 *i = va_arg (*args, u32 *);
392
393   u32 user_index = *i / SNAT_DET_SES_PER_USER;
394   in_addr.as_u32 =
395     clib_host_to_net_u32 (clib_net_to_host_u32 (det_map->in_addr.as_u32) +
396                           user_index);
397   in_offset =
398     clib_net_to_host_u32 (in_addr.as_u32) -
399     clib_net_to_host_u32 (det_map->in_addr.as_u32);
400   out_offset = in_offset / det_map->sharing_ratio;
401   out_addr.as_u32 =
402     clib_host_to_net_u32 (clib_net_to_host_u32 (det_map->out_addr.as_u32) +
403                           out_offset);
404   s =
405     format (s,
406             "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
407             format_ip4_address, &in_addr, clib_net_to_host_u16 (ses->in_port),
408             format_ip4_address, &out_addr,
409             clib_net_to_host_u16 (ses->out.out_port), format_ip4_address,
410             &ses->out.ext_host_addr,
411             clib_net_to_host_u16 (ses->out.ext_host_port),
412             format_snat_session_state, ses->state, ses->expire);
413
414   return s;
415 }
416
417 /*
418  * fd.io coding-style-patch-verification: ON
419  *
420  * Local Variables:
421  * eval: (c-set-style "gnu")
422  * End:
423  */