81e743fd27691078b2467bf415229837de970b9c
[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 u8 *
24 format_nat_addr_and_port_alloc_alg (u8 * s, va_list * args)
25 {
26   u32 i = va_arg (*args, u32);
27   u8 *t = 0;
28
29   switch (i)
30     {
31 #define _(v, N, s) case NAT_ADDR_AND_PORT_ALLOC_ALG_##N: t = (u8 *) s; break;
32       foreach_nat_addr_and_port_alloc_alg
33 #undef _
34     default:
35       s = format (s, "unknown");
36       return s;
37     }
38   s = format (s, "%s", t);
39   return s;
40 }
41
42 u8 *
43 format_snat_session_state (u8 * s, va_list * args)
44 {
45   u32 i = va_arg (*args, u32);
46   u8 *t = 0;
47
48   switch (i)
49     {
50 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
51       foreach_snat_session_state
52 #undef _
53     default:
54       t = format (t, "unknown");
55     }
56   s = format (s, "%s", t);
57   return s;
58 }
59
60 u8 *
61 format_snat_session (u8 * s, va_list * args)
62 {
63   snat_main_per_thread_data_t *tsm =
64     va_arg (*args, snat_main_per_thread_data_t *);
65   snat_session_t *sess = va_arg (*args, snat_session_t *);
66
67   if (nat44_ed_is_unk_proto (sess->proto))
68     {
69       s = format (s, "  i2o %U proto %u fib %u\n",
70                   format_ip4_address, &sess->in2out.addr,
71                   sess->in2out.port, sess->in2out.fib_index);
72       s =
73         format (s, "    o2i %U proto %u fib %u\n", format_ip4_address,
74                 &sess->out2in.addr, sess->out2in.port, sess->out2in.fib_index);
75     }
76   else
77     {
78       s = format (s, "  i2o %U proto %U port %d fib %d\n", format_ip4_address,
79                   &sess->in2out.addr, format_ip_protocol, sess->proto,
80                   clib_net_to_host_u16 (sess->in2out.port),
81                   sess->in2out.fib_index);
82       s = format (s, "    o2i %U proto %U port %d fib %d\n",
83                   format_ip4_address, &sess->out2in.addr, format_ip_protocol,
84                   sess->proto, clib_net_to_host_u16 (sess->out2in.port),
85                   sess->out2in.fib_index);
86     }
87   if (nat44_ed_is_twice_nat_session (sess))
88     {
89       s = format (s, "       external host o2i %U:%d i2o %U:%d\n",
90                   format_ip4_address, &sess->ext_host_addr,
91                   clib_net_to_host_u16 (sess->ext_host_port),
92                   format_ip4_address, &sess->ext_host_nat_addr,
93                   clib_net_to_host_u16 (sess->ext_host_nat_port));
94     }
95       else
96         {
97           if (sess->ext_host_addr.as_u32)
98             s = format (s, "       external host %U:%u\n",
99                         format_ip4_address, &sess->ext_host_addr,
100                         clib_net_to_host_u16 (sess->ext_host_port));
101         }
102       s = format (s, "       i2o flow: %U\n", format_nat_6t_flow, &sess->i2o);
103       s = format (s, "       o2i flow: %U\n", format_nat_6t_flow, &sess->o2i);
104   s = format (s, "       index %llu\n", sess - tsm->sessions);
105   s = format (s, "       last heard %.2f\n", sess->last_heard);
106   s = format (s, "       total pkts %d, total bytes %lld\n",
107               sess->total_pkts, sess->total_bytes);
108   if (nat44_ed_is_session_static (sess))
109     s = format (s, "       static translation\n");
110   else
111     s = format (s, "       dynamic translation\n");
112   if (na44_ed_is_fwd_bypass_session (sess))
113     s = format (s, "       forwarding-bypass\n");
114   if (nat44_ed_is_lb_session (sess))
115     s = format (s, "       load-balancing\n");
116   if (nat44_ed_is_twice_nat_session (sess))
117     s = format (s, "       twice-nat\n");
118   return s;
119 }
120
121 u8 *
122 format_snat_static_mapping (u8 * s, va_list * args)
123 {
124   snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
125   nat44_lb_addr_port_t *local;
126
127   if (is_sm_identity_nat (m->flags))
128     {
129       if (is_sm_addr_only (m->flags))
130         s = format (s, "identity mapping %U",
131                     format_ip4_address, &m->local_addr);
132       else
133         s = format (s, "identity mapping %U %U:%d", format_ip_protocol,
134                     m->proto, format_ip4_address, &m->local_addr,
135                     clib_net_to_host_u16 (m->local_port));
136
137       pool_foreach (local, m->locals)
138        {
139         s = format (s, " vrf %d", local->vrf_id);
140       }
141
142       return s;
143     }
144
145   if (is_sm_addr_only (m->flags))
146     s =
147       format (s, "local %U external %U vrf %d %s %s", format_ip4_address,
148               &m->local_addr, format_ip4_address, &m->external_addr, m->vrf_id,
149               is_sm_twice_nat (m->flags) ?
150                 "twice-nat" :
151                 is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
152               is_sm_out2in_only (m->flags) ? "out2in-only" : "");
153   else
154     {
155       if (is_sm_lb (m->flags))
156         {
157           s =
158             format (s, "%U external %U:%d %s %s", format_ip_protocol, m->proto,
159                     format_ip4_address, &m->external_addr,
160                     clib_net_to_host_u16 (m->external_port),
161                     is_sm_twice_nat (m->flags) ?
162                       "twice-nat" :
163                       is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
164                     is_sm_out2in_only (m->flags) ? "out2in-only" : "");
165
166           pool_foreach (local, m->locals)
167             {
168               s = format (s, "\n  local %U:%d vrf %d probability %d\%",
169                           format_ip4_address, &local->addr,
170                           clib_net_to_host_u16 (local->port), local->vrf_id,
171                           local->probability);
172             }
173         }
174       else
175         s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
176                     format_ip_protocol, m->proto, format_ip4_address,
177                     &m->local_addr, clib_net_to_host_u16 (m->local_port),
178                     format_ip4_address, &m->external_addr,
179                     clib_net_to_host_u16 (m->external_port), m->vrf_id,
180                     is_sm_twice_nat (m->flags) ?
181                       "twice-nat" :
182                       is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
183                     is_sm_out2in_only (m->flags) ? "out2in-only" : "");
184     }
185   return s;
186 }
187
188 u8 *
189 format_snat_static_map_to_resolve (u8 * s, va_list * args)
190 {
191   snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
192   vnet_main_t *vnm = vnet_get_main ();
193
194   if (is_sm_addr_only (m->flags))
195     s = format (s, "local %U external %U vrf %d",
196                 format_ip4_address, &m->l_addr,
197                 format_vnet_sw_if_index_name, vnm, m->sw_if_index, m->vrf_id);
198   else
199     s = format (s, "%U local %U:%d external %U:%d vrf %d", format_ip_protocol,
200                 m->proto, format_ip4_address, &m->l_addr,
201                 clib_net_to_host_u16 (m->l_port), format_vnet_sw_if_index_name,
202                 vnm, m->sw_if_index, clib_net_to_host_u16 (m->e_port),
203                 m->vrf_id);
204
205   return s;
206 }
207
208 /*
209  * fd.io coding-style-patch-verification: ON
210  *
211  * Local Variables:
212  * eval: (c-set-style "gnu")
213  * End:
214  */