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
21 #include <nat/nat_det.h>
24 unformat_snat_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 = SNAT_PROTOCOL_##N;
38 format_snat_protocol (u8 * s, va_list * args)
40 u32 i = va_arg (*args, u32);
45 #define _(N, j, n, str) case SNAT_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 snat_session_key_t *key = va_arg (*args, snat_session_key_t *);
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);
88 format_static_mapping_key (u8 * s, va_list * args)
90 snat_session_key_t *key = va_arg (*args, snat_session_key_t *);
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);
99 format_snat_session_state (u8 * s, va_list * args)
101 u32 i = va_arg (*args, u32);
106 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
107 foreach_snat_session_state
110 t = format (t, "unknown");
112 s = format (s, "%s", t);
117 format_snat_session (u8 * s, va_list * args)
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 *);
123 if (snat_is_unk_proto_session (sess))
125 s = format (s, " i2o %U proto %u fib %u\n",
126 format_ip4_address, &sess->in2out.addr,
127 clib_net_to_host_u16 (sess->in2out.port),
128 sess->in2out.fib_index);
129 s = format (s, " o2i %U proto %u fib %u\n",
130 format_ip4_address, &sess->out2in.addr,
131 clib_net_to_host_u16 (sess->out2in.port),
132 sess->out2in.fib_index);
136 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
137 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
139 if (is_ed_session (sess) || is_fwd_bypass_session (sess))
141 if (is_twice_nat_session (sess))
143 s = format (s, " external host o2i %U:%d i2o %U:%d\n",
144 format_ip4_address, &sess->ext_host_addr,
145 clib_net_to_host_u16 (sess->ext_host_port),
146 format_ip4_address, &sess->ext_host_nat_addr,
147 clib_net_to_host_u16 (sess->ext_host_nat_port));
151 if (sess->ext_host_addr.as_u32)
152 s = format (s, " external host %U:%u\n",
153 format_ip4_address, &sess->ext_host_addr,
154 clib_net_to_host_u16 (sess->ext_host_port));
157 s = format (s, " index %llu\n", sess - sm->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 (snat_is_session_static (sess))
162 s = format (s, " static translation\n");
164 s = format (s, " dynamic translation\n");
165 if (is_fwd_bypass_session (sess))
166 s = format (s, " forwarding-bypass\n");
167 if (is_lb_session (sess))
168 s = format (s, " load-balancing\n");
169 if (is_twice_nat_session (sess))
170 s = format (s, " twice-nat\n");
176 format_snat_user (u8 * s, va_list * args)
178 snat_main_per_thread_data_t *sm =
179 va_arg (*args, snat_main_per_thread_data_t *);
180 snat_user_t *u = va_arg (*args, snat_user_t *);
181 int verbose = va_arg (*args, int);
182 dlist_elt_t *head, *elt;
183 u32 elt_index, head_index;
185 snat_session_t *sess;
187 s = format (s, "%U: %d dynamic translations, %d static translations\n",
188 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
193 if (u->nsessions || u->nstaticsessions)
195 head_index = u->sessions_per_user_list_head_index;
196 head = pool_elt_at_index (sm->list_pool, head_index);
198 elt_index = head->next;
199 elt = pool_elt_at_index (sm->list_pool, elt_index);
200 session_index = elt->value;
202 while (session_index != ~0)
204 sess = pool_elt_at_index (sm->sessions, session_index);
206 s = format (s, " %U\n", format_snat_session, sm, sess);
208 elt_index = elt->next;
209 elt = pool_elt_at_index (sm->list_pool, elt_index);
210 session_index = elt->value;
218 format_snat_static_mapping (u8 * s, va_list * args)
220 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
221 nat44_lb_addr_port_t *local;
223 if (is_identity_static_mapping (m))
225 if (is_addr_only_static_mapping (m))
226 s = format (s, "identity mapping %U",
227 format_ip4_address, &m->local_addr);
229 s = format (s, "identity mapping %U:%d",
230 format_ip4_address, &m->local_addr, m->local_port);
233 vec_foreach (local, m->locals)
234 s = format (s, " vrf %d", local->vrf_id);
240 if (is_addr_only_static_mapping (m))
241 s = format (s, "local %U external %U vrf %d %s %s",
242 format_ip4_address, &m->local_addr,
243 format_ip4_address, &m->external_addr,
245 m->twice_nat == TWICE_NAT ? "twice-nat" :
246 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
247 is_out2in_only_static_mapping (m) ? "out2in-only" : "");
250 if (is_lb_static_mapping (m))
252 s = format (s, "%U external %U:%d %s %s",
253 format_snat_protocol, m->proto,
254 format_ip4_address, &m->external_addr, m->external_port,
255 m->twice_nat == TWICE_NAT ? "twice-nat" :
256 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
257 is_out2in_only_static_mapping (m) ? "out2in-only" : "");
258 vec_foreach (local, m->locals)
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);
264 s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
265 format_snat_protocol, m->proto,
266 format_ip4_address, &m->local_addr, m->local_port,
267 format_ip4_address, &m->external_addr, m->external_port,
269 m->twice_nat == TWICE_NAT ? "twice-nat" :
270 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
271 is_out2in_only_static_mapping (m) ? "out2in-only" : "");
277 format_snat_static_map_to_resolve (u8 * s, va_list * args)
279 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
280 vnet_main_t *vnm = vnet_get_main ();
283 s = format (s, "local %U external %U vrf %d",
284 format_ip4_address, &m->l_addr,
285 format_vnet_sw_if_index_name, vnm, m->sw_if_index, m->vrf_id);
287 s = format (s, "%U local %U:%d external %U:%d vrf %d",
288 format_snat_protocol, m->proto,
289 format_ip4_address, &m->l_addr, m->l_port,
290 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
291 m->e_port, m->vrf_id);
297 format_det_map_ses (u8 * s, va_list * args)
299 snat_det_map_t *det_map = va_arg (*args, snat_det_map_t *);
300 ip4_address_t in_addr, out_addr;
301 u32 in_offset, out_offset;
302 snat_det_session_t *ses = va_arg (*args, snat_det_session_t *);
303 u32 *i = va_arg (*args, u32 *);
305 u32 user_index = *i / SNAT_DET_SES_PER_USER;
307 clib_host_to_net_u32 (clib_net_to_host_u32 (det_map->in_addr.as_u32) +
310 clib_net_to_host_u32 (in_addr.as_u32) -
311 clib_net_to_host_u32 (det_map->in_addr.as_u32);
312 out_offset = in_offset / det_map->sharing_ratio;
314 clib_host_to_net_u32 (clib_net_to_host_u32 (det_map->out_addr.as_u32) +
318 "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
319 format_ip4_address, &in_addr, clib_net_to_host_u16 (ses->in_port),
320 format_ip4_address, &out_addr,
321 clib_net_to_host_u16 (ses->out.out_port), format_ip4_address,
322 &ses->out.ext_host_addr,
323 clib_net_to_host_u16 (ses->out.ext_host_port),
324 format_snat_session_state, ses->state, ses->expire);
330 format_nat44_reass_trace (u8 * s, va_list * args)
332 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
333 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
334 nat44_reass_trace_t *t = va_arg (*args, nat44_reass_trace_t *);
336 s = format (s, "NAT44_REASS: sw_if_index %d, next index %d, status %s",
337 t->sw_if_index, t->next_index,
338 t->cached ? "cached" : "translated");
344 * fd.io coding-style-patch-verification: ON
347 * eval: (c-set-style "gnu")