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 format_nat_addr_and_port_alloc_alg (u8 * s, va_list * args)
26 u32 i = va_arg (*args, u32);
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
35 s = format (s, "unknown");
38 s = format (s, "%s", t);
43 format_snat_session_state (u8 * s, va_list * args)
45 u32 i = va_arg (*args, u32);
50 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
51 foreach_snat_session_state
54 t = format (t, "unknown");
56 s = format (s, "%s", t);
61 format_snat_session (u8 * s, va_list * args)
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 *);
67 if (nat44_ed_is_unk_proto (sess->proto))
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);
73 format (s, " o2i %U proto %u fib %u\n", format_ip4_address,
74 &sess->out2in.addr, sess->out2in.port, sess->out2in.fib_index);
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);
87 if (nat44_ed_is_twice_nat_session (sess))
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));
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));
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");
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");
122 format_snat_static_mapping (u8 * s, va_list * args)
124 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
125 nat44_lb_addr_port_t *local;
127 if (is_sm_identity_nat (m->flags))
129 if (is_sm_addr_only (m->flags))
130 s = format (s, "identity mapping %U",
131 format_ip4_address, &m->local_addr);
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));
137 pool_foreach (local, m->locals)
139 s = format (s, " vrf %d", local->vrf_id);
145 if (is_sm_addr_only (m->flags))
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) ?
151 is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
152 is_sm_out2in_only (m->flags) ? "out2in-only" : "");
155 if (is_sm_lb (m->flags))
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) ?
163 is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
164 is_sm_out2in_only (m->flags) ? "out2in-only" : "");
166 pool_foreach (local, m->locals)
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,
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) ?
182 is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
183 is_sm_out2in_only (m->flags) ? "out2in-only" : "");
189 format_snat_static_map_to_resolve (u8 * s, va_list * args)
191 snat_static_mapping_resolve_t *m =
192 va_arg (*args, snat_static_mapping_resolve_t *);
193 vnet_main_t *vnm = vnet_get_main ();
195 if (is_sm_addr_only (m->flags))
196 s = format (s, "local %U external %U vrf %d",
197 format_ip4_address, &m->l_addr,
198 format_vnet_sw_if_index_name, vnm, m->sw_if_index, m->vrf_id);
200 s = format (s, "%U local %U:%d external %U:%d vrf %d", format_ip_protocol,
201 m->proto, format_ip4_address, &m->l_addr,
202 clib_net_to_host_u16 (m->l_port), format_vnet_sw_if_index_name,
203 vnm, m->sw_if_index, clib_net_to_host_u16 (m->e_port),
210 * fd.io coding-style-patch-verification: ON
213 * eval: (c-set-style "gnu")