nat: NAT44 ED improvements and fixes
[vpp.git] / src / plugins / nat / nat44-ed / nat44_ed_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/nat44-ed/nat44_ed.h>
21 #include <nat/nat44-ed/nat44_ed_inlines.h>
22
23 uword
24 unformat_nat_protocol (unformat_input_t * input, va_list * args)
25 {
26   u32 *r = va_arg (*args, u32 *);
27
28   if (0);
29 #define _(N, i, n, s) else if (unformat (input, s)) *r = NAT_PROTOCOL_##N;
30   foreach_nat_protocol
31 #undef _
32     else
33     return 0;
34   return 1;
35 }
36
37 u8 *
38 format_nat_protocol (u8 * s, va_list * args)
39 {
40   u32 i = va_arg (*args, u32);
41   u8 *t = 0;
42
43   switch (i)
44     {
45 #define _(N, j, n, str) case NAT_PROTOCOL_##N: t = (u8 *) str; break;
46       foreach_nat_protocol
47 #undef _
48     default:
49       s = format (s, "unknown");
50       return s;
51     }
52   s = format (s, "%s", t);
53   return s;
54 }
55
56 u8 *
57 format_nat_addr_and_port_alloc_alg (u8 * s, va_list * args)
58 {
59   u32 i = va_arg (*args, u32);
60   u8 *t = 0;
61
62   switch (i)
63     {
64 #define _(v, N, s) case NAT_ADDR_AND_PORT_ALLOC_ALG_##N: t = (u8 *) s; break;
65       foreach_nat_addr_and_port_alloc_alg
66 #undef _
67     default:
68       s = format (s, "unknown");
69       return s;
70     }
71   s = format (s, "%s", t);
72   return s;
73 }
74
75 u8 *
76 format_snat_key (u8 * s, va_list * args)
77 {
78   u64 key = va_arg (*args, u64);
79
80   ip4_address_t addr;
81   u16 port;
82   nat_protocol_t protocol;
83   u32 fib_index;
84
85   split_nat_key (key, &addr, &port, &fib_index, &protocol);
86
87   s = format (s, "%U proto %U port %d fib %d",
88               format_ip4_address, &addr,
89               format_nat_protocol, protocol,
90               clib_net_to_host_u16 (port), fib_index);
91   return s;
92 }
93
94 u8 *
95 format_snat_session_state (u8 * s, va_list * args)
96 {
97   u32 i = va_arg (*args, u32);
98   u8 *t = 0;
99
100   switch (i)
101     {
102 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
103       foreach_snat_session_state
104 #undef _
105     default:
106       t = format (t, "unknown");
107     }
108   s = format (s, "%s", t);
109   return s;
110 }
111
112 u8 *
113 format_snat_session (u8 * s, va_list * args)
114 {
115   snat_main_per_thread_data_t *tsm =
116     va_arg (*args, snat_main_per_thread_data_t *);
117   snat_session_t *sess = va_arg (*args, snat_session_t *);
118
119   if (snat_is_unk_proto_session (sess))
120     {
121       s = format (s, "  i2o %U proto %u fib %u\n",
122                   format_ip4_address, &sess->in2out.addr,
123                   sess->in2out.port, sess->in2out.fib_index);
124       s =
125         format (s, "    o2i %U proto %u fib %u\n", format_ip4_address,
126                 &sess->out2in.addr, sess->out2in.port, sess->out2in.fib_index);
127     }
128   else
129     {
130       s = format (s, "  i2o %U proto %U port %d fib %d\n",
131                   format_ip4_address, &sess->in2out.addr,
132                   format_nat_protocol, sess->nat_proto,
133                   clib_net_to_host_u16 (sess->in2out.port),
134                   sess->in2out.fib_index);
135       s = format (s, "    o2i %U proto %U port %d fib %d\n",
136                   format_ip4_address, &sess->out2in.addr, format_nat_protocol,
137                   sess->nat_proto, clib_net_to_host_u16 (sess->out2in.port),
138                   sess->out2in.fib_index);
139     }
140   if (nat44_ed_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       s = format (s, "       i2o flow: %U\n", format_nat_6t_flow, &sess->i2o);
156       s = format (s, "       o2i flow: %U\n", format_nat_6t_flow, &sess->o2i);
157   s = format (s, "       index %llu\n", sess - tsm->sessions);
158   s = format (s, "       last heard %.2f\n", sess->last_heard);
159   s = format (s, "       total pkts %d, total bytes %lld\n",
160               sess->total_pkts, sess->total_bytes);
161   if (nat44_ed_is_session_static (sess))
162     s = format (s, "       static translation\n");
163   else
164     s = format (s, "       dynamic translation\n");
165   if (na44_ed_is_fwd_bypass_session (sess))
166     s = format (s, "       forwarding-bypass\n");
167   if (nat44_ed_is_lb_session (sess))
168     s = format (s, "       load-balancing\n");
169   if (nat44_ed_is_twice_nat_session (sess))
170     s = format (s, "       twice-nat\n");
171   return s;
172 }
173
174 u8 *
175 format_snat_static_mapping (u8 * s, va_list * args)
176 {
177   snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
178   nat44_lb_addr_port_t *local;
179
180   if (is_sm_identity_nat (m->flags))
181     {
182       if (is_sm_addr_only (m->flags))
183         s = format (s, "identity mapping %U",
184                     format_ip4_address, &m->local_addr);
185       else
186         s = format (s, "identity mapping %U %U:%d",
187                     format_nat_protocol, m->proto,
188                     format_ip4_address, &m->local_addr,
189                     clib_net_to_host_u16 (m->local_port));
190
191       pool_foreach (local, m->locals)
192        {
193         s = format (s, " vrf %d", local->vrf_id);
194       }
195
196       return s;
197     }
198
199   if (is_sm_addr_only (m->flags))
200     s =
201       format (s, "local %U external %U vrf %d %s %s", format_ip4_address,
202               &m->local_addr, format_ip4_address, &m->external_addr, m->vrf_id,
203               is_sm_twice_nat (m->flags) ?
204                 "twice-nat" :
205                 is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
206               is_sm_out2in_only (m->flags) ? "out2in-only" : "");
207   else
208     {
209       if (is_sm_lb (m->flags))
210         {
211           s =
212             format (s, "%U external %U:%d %s %s", format_nat_protocol,
213                     m->proto, format_ip4_address, &m->external_addr,
214                     clib_net_to_host_u16 (m->external_port),
215                     is_sm_twice_nat (m->flags) ?
216                       "twice-nat" :
217                       is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
218                     is_sm_out2in_only (m->flags) ? "out2in-only" : "");
219
220           pool_foreach (local, m->locals)
221             {
222               s = format (s, "\n  local %U:%d vrf %d probability %d\%",
223                           format_ip4_address, &local->addr,
224                           clib_net_to_host_u16 (local->port), local->vrf_id,
225                           local->probability);
226             }
227         }
228       else
229         s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
230                     format_nat_protocol, m->proto, format_ip4_address,
231                     &m->local_addr, clib_net_to_host_u16 (m->local_port),
232                     format_ip4_address, &m->external_addr,
233                     clib_net_to_host_u16 (m->external_port), m->vrf_id,
234                     is_sm_twice_nat (m->flags) ?
235                       "twice-nat" :
236                       is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
237                     is_sm_out2in_only (m->flags) ? "out2in-only" : "");
238     }
239   return s;
240 }
241
242 u8 *
243 format_snat_static_map_to_resolve (u8 * s, va_list * args)
244 {
245   snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
246   vnet_main_t *vnm = vnet_get_main ();
247
248   if (m->addr_only)
249     s = format (s, "local %U external %U vrf %d",
250                 format_ip4_address, &m->l_addr,
251                 format_vnet_sw_if_index_name, vnm, m->sw_if_index, m->vrf_id);
252   else
253     s = format (s, "%U local %U:%d external %U:%d vrf %d",
254                 format_nat_protocol, m->proto,
255                 format_ip4_address, &m->l_addr,
256                 clib_net_to_host_u16 (m->l_port),
257                 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
258                 clib_net_to_host_u16 (m->e_port), m->vrf_id);
259
260   return s;
261 }
262
263 /*
264  * fd.io coding-style-patch-verification: ON
265  *
266  * Local Variables:
267  * eval: (c-set-style "gnu")
268  * End:
269  */