nat: move deterministic nat to det44 sub feature
[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
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 = format (s, "    o2i %U proto %u fib %u\n",
125                   format_ip4_address, &sess->out2in.addr,
126                   sess->out2in.port, sess->out2in.fib_index);
127     }
128   else
129     {
130       s = format (s, "  i2o %U\n", format_snat_key, &sess->in2out);
131       s = format (s, "    o2i %U\n", format_snat_key, &sess->out2in);
132     }
133   if (is_ed_session (sess) || is_fwd_bypass_session (sess))
134     {
135       if (is_twice_nat_session (sess))
136         {
137           s = format (s, "       external host o2i %U:%d i2o %U:%d\n",
138                       format_ip4_address, &sess->ext_host_addr,
139                       clib_net_to_host_u16 (sess->ext_host_port),
140                       format_ip4_address, &sess->ext_host_nat_addr,
141                       clib_net_to_host_u16 (sess->ext_host_nat_port));
142         }
143       else
144         {
145           if (sess->ext_host_addr.as_u32)
146             s = format (s, "       external host %U:%u\n",
147                         format_ip4_address, &sess->ext_host_addr,
148                         clib_net_to_host_u16 (sess->ext_host_port));
149         }
150     }
151   s = format (s, "       index %llu\n", sess - tsm->sessions);
152   s = format (s, "       last heard %.2f\n", sess->last_heard);
153   s = format (s, "       total pkts %d, total bytes %lld\n",
154               sess->total_pkts, sess->total_bytes);
155   if (snat_is_session_static (sess))
156     s = format (s, "       static translation\n");
157   else
158     s = format (s, "       dynamic translation\n");
159   if (is_fwd_bypass_session (sess))
160     s = format (s, "       forwarding-bypass\n");
161   if (is_lb_session (sess))
162     s = format (s, "       load-balancing\n");
163   if (is_twice_nat_session (sess))
164     s = format (s, "       twice-nat\n");
165
166   return s;
167 }
168
169 u8 *
170 format_snat_user (u8 * s, va_list * args)
171 {
172   snat_main_per_thread_data_t *tsm =
173     va_arg (*args, snat_main_per_thread_data_t *);
174   snat_user_t *u = va_arg (*args, snat_user_t *);
175   int verbose = va_arg (*args, int);
176   dlist_elt_t *head, *elt;
177   u32 elt_index, head_index;
178   u32 session_index;
179   snat_session_t *sess;
180
181   s = format (s, "%U: %d dynamic translations, %d static translations\n",
182               format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
183
184   if (verbose == 0)
185     return s;
186
187   if (u->nsessions || u->nstaticsessions)
188     {
189       head_index = u->sessions_per_user_list_head_index;
190       head = pool_elt_at_index (tsm->list_pool, head_index);
191
192       elt_index = head->next;
193       elt = pool_elt_at_index (tsm->list_pool, elt_index);
194       session_index = elt->value;
195
196       while (session_index != ~0)
197         {
198           sess = pool_elt_at_index (tsm->sessions, session_index);
199
200           s = format (s, "  %U\n", format_snat_session, tsm, sess);
201
202           elt_index = elt->next;
203           elt = pool_elt_at_index (tsm->list_pool, elt_index);
204           session_index = elt->value;
205         }
206     }
207
208   return s;
209 }
210
211 u8 *
212 format_snat_static_mapping (u8 * s, va_list * args)
213 {
214   snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
215   nat44_lb_addr_port_t *local;
216
217   if (is_identity_static_mapping (m))
218     {
219       if (is_addr_only_static_mapping (m))
220         s = format (s, "identity mapping %U",
221                     format_ip4_address, &m->local_addr);
222       else
223         s = format (s, "identity mapping %U %U:%d",
224                     format_nat_protocol, m->proto,
225                     format_ip4_address, &m->local_addr, m->local_port);
226
227       /* *INDENT-OFF* */
228       pool_foreach (local, m->locals,
229       ({
230         s = format (s, " vrf %d", local->vrf_id);
231       }));
232       /* *INDENT-ON* */
233
234       return s;
235     }
236
237   if (is_addr_only_static_mapping (m))
238     s = format (s, "local %U external %U vrf %d %s %s",
239                 format_ip4_address, &m->local_addr,
240                 format_ip4_address, &m->external_addr,
241                 m->vrf_id,
242                 m->twice_nat == TWICE_NAT ? "twice-nat" :
243                 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
244                 is_out2in_only_static_mapping (m) ? "out2in-only" : "");
245   else
246     {
247       if (is_lb_static_mapping (m))
248         {
249           s = format (s, "%U external %U:%d %s %s",
250                       format_nat_protocol, m->proto,
251                       format_ip4_address, &m->external_addr, m->external_port,
252                       m->twice_nat == TWICE_NAT ? "twice-nat" :
253                       m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
254                       is_out2in_only_static_mapping (m) ? "out2in-only" : "");
255
256           /* *INDENT-OFF* */
257           pool_foreach (local, m->locals,
258           ({
259             s = format (s, "\n  local %U:%d vrf %d probability %d\%",
260                         format_ip4_address, &local->addr, local->port,
261                         local->vrf_id, local->probability);
262           }));
263           /* *INDENT-ON* */
264
265         }
266       else
267         s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
268                     format_nat_protocol, m->proto,
269                     format_ip4_address, &m->local_addr, m->local_port,
270                     format_ip4_address, &m->external_addr, m->external_port,
271                     m->vrf_id,
272                     m->twice_nat == TWICE_NAT ? "twice-nat" :
273                     m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
274                     is_out2in_only_static_mapping (m) ? "out2in-only" : "");
275     }
276   return s;
277 }
278
279 u8 *
280 format_snat_static_map_to_resolve (u8 * s, va_list * args)
281 {
282   snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
283   vnet_main_t *vnm = vnet_get_main ();
284
285   if (m->addr_only)
286     s = format (s, "local %U external %U vrf %d",
287                 format_ip4_address, &m->l_addr,
288                 format_vnet_sw_if_index_name, vnm, m->sw_if_index, m->vrf_id);
289   else
290     s = format (s, "%U local %U:%d external %U:%d vrf %d",
291                 format_nat_protocol, m->proto,
292                 format_ip4_address, &m->l_addr, m->l_port,
293                 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
294                 m->e_port, m->vrf_id);
295
296   return s;
297 }
298
299 /*
300  * fd.io coding-style-patch-verification: ON
301  *
302  * Local Variables:
303  * eval: (c-set-style "gnu")
304  * End:
305  */