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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @brief NAT formatting
20 #include <nat/nat44-ed/nat44_ed.h>
21 #include <nat/nat44-ed/nat44_ed_inlines.h>
24 unformat_nat_protocol (unformat_input_t * input, va_list * args)
26 u32 *r = va_arg (*args, u32 *);
29 #define _(N, i, n, s) else if (unformat (input, s)) *r = NAT_PROTOCOL_##N;
38 format_nat_protocol (u8 * s, va_list * args)
40 u32 i = va_arg (*args, u32);
45 #define _(N, j, n, str) case NAT_PROTOCOL_##N: t = (u8 *) str; break;
49 s = format (s, "unknown");
52 s = format (s, "%s", t);
57 format_nat_addr_and_port_alloc_alg (u8 * s, va_list * args)
59 u32 i = va_arg (*args, u32);
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
68 s = format (s, "unknown");
71 s = format (s, "%s", t);
76 format_snat_key (u8 * s, va_list * args)
78 u64 key = va_arg (*args, u64);
82 nat_protocol_t protocol;
85 split_nat_key (key, &addr, &port, &fib_index, &protocol);
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);
95 format_snat_session_state (u8 * s, va_list * args)
97 u32 i = va_arg (*args, u32);
102 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
103 foreach_snat_session_state
106 t = format (t, "unknown");
108 s = format (s, "%s", t);
113 format_snat_session (u8 * s, va_list * args)
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 *);
119 if (snat_is_unk_proto_session (sess))
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);
125 format (s, " o2i %U proto %u fib %u\n", format_ip4_address,
126 &sess->out2in.addr, sess->out2in.port, sess->out2in.fib_index);
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);
140 if (is_ed_session (sess) || is_fwd_bypass_session (sess))
142 if (is_twice_nat_session (sess))
144 s = format (s, " external host o2i %U:%d i2o %U:%d\n",
145 format_ip4_address, &sess->ext_host_addr,
146 clib_net_to_host_u16 (sess->ext_host_port),
147 format_ip4_address, &sess->ext_host_nat_addr,
148 clib_net_to_host_u16 (sess->ext_host_nat_port));
152 if (sess->ext_host_addr.as_u32)
153 s = format (s, " external host %U:%u\n",
154 format_ip4_address, &sess->ext_host_addr,
155 clib_net_to_host_u16 (sess->ext_host_port));
157 s = format (s, " i2o flow: %U\n", format_nat_6t_flow, &sess->i2o);
158 s = format (s, " o2i flow: %U\n", format_nat_6t_flow, &sess->o2i);
160 s = format (s, " index %llu\n", sess - tsm->sessions);
161 s = format (s, " last heard %.2f\n", sess->last_heard);
162 s = format (s, " total pkts %d, total bytes %lld\n",
163 sess->total_pkts, sess->total_bytes);
164 if (snat_is_session_static (sess))
165 s = format (s, " static translation\n");
167 s = format (s, " dynamic translation\n");
168 if (is_fwd_bypass_session (sess))
169 s = format (s, " forwarding-bypass\n");
170 if (is_lb_session (sess))
171 s = format (s, " load-balancing\n");
172 if (is_twice_nat_session (sess))
173 s = format (s, " twice-nat\n");
178 format_snat_static_mapping (u8 * s, va_list * args)
180 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
181 nat44_lb_addr_port_t *local;
183 if (is_identity_static_mapping (m))
185 if (is_addr_only_static_mapping (m))
186 s = format (s, "identity mapping %U",
187 format_ip4_address, &m->local_addr);
189 s = format (s, "identity mapping %U %U:%d",
190 format_nat_protocol, m->proto,
191 format_ip4_address, &m->local_addr,
192 clib_net_to_host_u16 (m->local_port));
195 pool_foreach (local, m->locals)
197 s = format (s, " vrf %d", local->vrf_id);
204 if (is_addr_only_static_mapping (m))
205 s = format (s, "local %U external %U vrf %d %s %s",
206 format_ip4_address, &m->local_addr,
207 format_ip4_address, &m->external_addr,
209 m->twice_nat == TWICE_NAT ? "twice-nat" :
210 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
211 is_out2in_only_static_mapping (m) ? "out2in-only" : "");
214 if (is_lb_static_mapping (m))
216 s = format (s, "%U external %U:%d %s %s",
217 format_nat_protocol, m->proto,
218 format_ip4_address, &m->external_addr,
219 clib_net_to_host_u16 (m->external_port),
220 m->twice_nat == TWICE_NAT ? "twice-nat" :
221 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
222 is_out2in_only_static_mapping (m) ? "out2in-only" : "");
225 pool_foreach (local, m->locals)
227 s = format (s, "\n local %U:%d vrf %d probability %d\%",
228 format_ip4_address, &local->addr,
229 clib_net_to_host_u16 (local->port),
230 local->vrf_id, local->probability);
236 s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
237 format_nat_protocol, m->proto,
238 format_ip4_address, &m->local_addr,
239 clib_net_to_host_u16 (m->local_port),
240 format_ip4_address, &m->external_addr,
241 clib_net_to_host_u16 (m->external_port),
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" : "");
251 format_snat_static_map_to_resolve (u8 * s, va_list * args)
253 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
254 vnet_main_t *vnm = vnet_get_main ();
257 s = format (s, "local %U external %U vrf %d",
258 format_ip4_address, &m->l_addr,
259 format_vnet_sw_if_index_name, vnm, m->sw_if_index, m->vrf_id);
261 s = format (s, "%U local %U:%d external %U:%d vrf %d",
262 format_nat_protocol, m->proto,
263 format_ip4_address, &m->l_addr,
264 clib_net_to_host_u16 (m->l_port),
265 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
266 clib_net_to_host_u16 (m->e_port), m->vrf_id);
272 * fd.io coding-style-patch-verification: ON
275 * eval: (c-set-style "gnu")