8287968e029b25d32954d85848e3d3b0863accde
[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_nat_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 = NAT_PROTOCOL_##N;
31   foreach_nat_protocol
32 #undef _
33     else
34     return 0;
35   return 1;
36 }
37
38 u8 *
39 format_nat_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 NAT_PROTOCOL_##N: t = (u8 *) str; break;
47       foreach_nat_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   u64 key = va_arg (*args, u64);
80
81   ip4_address_t addr;
82   u16 port;
83   nat_protocol_t protocol;
84   u32 fib_index;
85
86   split_nat_key (key, &addr, &port, &fib_index, &protocol);
87
88   s = format (s, "%U proto %U port %d fib %d",
89               format_ip4_address, &addr,
90               format_nat_protocol, protocol,
91               clib_net_to_host_u16 (port), fib_index);
92   return s;
93 }
94
95 u8 *
96 format_snat_session_state (u8 * s, va_list * args)
97 {
98   u32 i = va_arg (*args, u32);
99   u8 *t = 0;
100
101   switch (i)
102     {
103 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
104       foreach_snat_session_state
105 #undef _
106     default:
107       t = format (t, "unknown");
108     }
109   s = format (s, "%s", t);
110   return s;
111 }
112
113 u8 *
114 format_snat_session (u8 * s, va_list * args)
115 {
116   snat_main_per_thread_data_t *tsm =
117     va_arg (*args, snat_main_per_thread_data_t *);
118   snat_session_t *sess = va_arg (*args, snat_session_t *);
119
120   if (snat_is_unk_proto_session (sess))
121     {
122       s = format (s, "  i2o %U proto %u fib %u\n",
123                   format_ip4_address, &sess->in2out.addr,
124                   sess->in2out.port, sess->in2out.fib_index);
125       s = format (s, "    o2i %U proto %u fib %u\n",
126                   format_ip4_address, &sess->out2in.addr,
127                   sess->out2in.port, sess->out2in.fib_index);
128     }
129   else
130     {
131       s = format (s, "  i2o %U\n", format_snat_key, &sess->in2out);
132       s = format (s, "    o2i %U\n", format_snat_key, &sess->out2in);
133     }
134   if (is_ed_session (sess) || is_fwd_bypass_session (sess))
135     {
136       if (is_twice_nat_session (sess))
137         {
138           s = format (s, "       external host o2i %U:%d i2o %U:%d\n",
139                       format_ip4_address, &sess->ext_host_addr,
140                       clib_net_to_host_u16 (sess->ext_host_port),
141                       format_ip4_address, &sess->ext_host_nat_addr,
142                       clib_net_to_host_u16 (sess->ext_host_nat_port));
143         }
144       else
145         {
146           if (sess->ext_host_addr.as_u32)
147             s = format (s, "       external host %U:%u\n",
148                         format_ip4_address, &sess->ext_host_addr,
149                         clib_net_to_host_u16 (sess->ext_host_port));
150         }
151     }
152   s = format (s, "       index %llu\n", sess - tsm->sessions);
153   s = format (s, "       last heard %.2f\n", sess->last_heard);
154   s = format (s, "       total pkts %d, total bytes %lld\n",
155               sess->total_pkts, sess->total_bytes);
156   if (snat_is_session_static (sess))
157     s = format (s, "       static translation\n");
158   else
159     s = format (s, "       dynamic translation\n");
160   if (is_fwd_bypass_session (sess))
161     s = format (s, "       forwarding-bypass\n");
162   if (is_lb_session (sess))
163     s = format (s, "       load-balancing\n");
164   if (is_twice_nat_session (sess))
165     s = format (s, "       twice-nat\n");
166
167   return s;
168 }
169
170 u8 *
171 format_snat_user (u8 * s, va_list * args)
172 {
173   snat_main_per_thread_data_t *tsm =
174     va_arg (*args, snat_main_per_thread_data_t *);
175   snat_user_t *u = va_arg (*args, snat_user_t *);
176   int verbose = va_arg (*args, int);
177   dlist_elt_t *head, *elt;
178   u32 elt_index, head_index;
179   u32 session_index;
180   snat_session_t *sess;
181
182   s = format (s, "%U: %d dynamic translations, %d static translations\n",
183               format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
184
185   if (verbose == 0)
186     return s;
187
188   if (u->nsessions || u->nstaticsessions)
189     {
190       head_index = u->sessions_per_user_list_head_index;
191       head = pool_elt_at_index (tsm->list_pool, head_index);
192
193       elt_index = head->next;
194       elt = pool_elt_at_index (tsm->list_pool, elt_index);
195       session_index = elt->value;
196
197       while (session_index != ~0)
198         {
199           sess = pool_elt_at_index (tsm->sessions, session_index);
200
201           s = format (s, "  %U\n", format_snat_session, tsm, sess);
202
203           elt_index = elt->next;
204           elt = pool_elt_at_index (tsm->list_pool, elt_index);
205           session_index = elt->value;
206         }
207     }
208
209   return s;
210 }
211
212 u8 *
213 format_snat_static_mapping (u8 * s, va_list * args)
214 {
215   snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
216   nat44_lb_addr_port_t *local;
217
218   if (is_identity_static_mapping (m))
219     {
220       if (is_addr_only_static_mapping (m))
221         s = format (s, "identity mapping %U",
222                     format_ip4_address, &m->local_addr);
223       else
224         s = format (s, "identity mapping %U %U:%d",
225                     format_nat_protocol, m->proto,
226                     format_ip4_address, &m->local_addr, m->local_port);
227
228       /* *INDENT-OFF* */
229       pool_foreach (local, m->locals,
230       ({
231         s = format (s, " vrf %d", local->vrf_id);
232       }));
233       /* *INDENT-ON* */
234
235       return s;
236     }
237
238   if (is_addr_only_static_mapping (m))
239     s = format (s, "local %U external %U vrf %d %s %s",
240                 format_ip4_address, &m->local_addr,
241                 format_ip4_address, &m->external_addr,
242                 m->vrf_id,
243                 m->twice_nat == TWICE_NAT ? "twice-nat" :
244                 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
245                 is_out2in_only_static_mapping (m) ? "out2in-only" : "");
246   else
247     {
248       if (is_lb_static_mapping (m))
249         {
250           s = format (s, "%U external %U:%d %s %s",
251                       format_nat_protocol, m->proto,
252                       format_ip4_address, &m->external_addr, m->external_port,
253                       m->twice_nat == TWICE_NAT ? "twice-nat" :
254                       m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
255                       is_out2in_only_static_mapping (m) ? "out2in-only" : "");
256
257           /* *INDENT-OFF* */
258           pool_foreach (local, m->locals,
259           ({
260             s = format (s, "\n  local %U:%d vrf %d probability %d\%",
261                         format_ip4_address, &local->addr, local->port,
262                         local->vrf_id, local->probability);
263           }));
264           /* *INDENT-ON* */
265
266         }
267       else
268         s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
269                     format_nat_protocol, m->proto,
270                     format_ip4_address, &m->local_addr, m->local_port,
271                     format_ip4_address, &m->external_addr, m->external_port,
272                     m->vrf_id,
273                     m->twice_nat == TWICE_NAT ? "twice-nat" :
274                     m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
275                     is_out2in_only_static_mapping (m) ? "out2in-only" : "");
276     }
277   return s;
278 }
279
280 u8 *
281 format_snat_static_map_to_resolve (u8 * s, va_list * args)
282 {
283   snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
284   vnet_main_t *vnm = vnet_get_main ();
285
286   if (m->addr_only)
287     s = format (s, "local %U external %U vrf %d",
288                 format_ip4_address, &m->l_addr,
289                 format_vnet_sw_if_index_name, vnm, m->sw_if_index, m->vrf_id);
290   else
291     s = format (s, "%U local %U:%d external %U:%d vrf %d",
292                 format_nat_protocol, m->proto,
293                 format_ip4_address, &m->l_addr, m->l_port,
294                 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
295                 m->e_port, m->vrf_id);
296
297   return s;
298 }
299
300 u8 *
301 format_det_map_ses (u8 * s, va_list * args)
302 {
303   snat_det_map_t *det_map = va_arg (*args, snat_det_map_t *);
304   ip4_address_t in_addr, out_addr;
305   u32 in_offset, out_offset;
306   snat_det_session_t *ses = va_arg (*args, snat_det_session_t *);
307   u32 *i = va_arg (*args, u32 *);
308
309   u32 user_index = *i / SNAT_DET_SES_PER_USER;
310   in_addr.as_u32 =
311     clib_host_to_net_u32 (clib_net_to_host_u32 (det_map->in_addr.as_u32) +
312                           user_index);
313   in_offset =
314     clib_net_to_host_u32 (in_addr.as_u32) -
315     clib_net_to_host_u32 (det_map->in_addr.as_u32);
316   out_offset = in_offset / det_map->sharing_ratio;
317   out_addr.as_u32 =
318     clib_host_to_net_u32 (clib_net_to_host_u32 (det_map->out_addr.as_u32) +
319                           out_offset);
320   s =
321     format (s,
322             "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
323             format_ip4_address, &in_addr, clib_net_to_host_u16 (ses->in_port),
324             format_ip4_address, &out_addr,
325             clib_net_to_host_u16 (ses->out.out_port), format_ip4_address,
326             &ses->out.ext_host_addr,
327             clib_net_to_host_u16 (ses->out.ext_host_port),
328             format_snat_session_state, ses->state, ses->expire);
329
330   return s;
331 }
332
333 /*
334  * fd.io coding-style-patch-verification: ON
335  *
336  * Local Variables:
337  * eval: (c-set-style "gnu")
338  * End:
339  */