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