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 (u8 * s, va_list * args)
45 snat_main_t *sm = va_arg (*args, snat_main_t *);
46 snat_main_per_thread_data_t *tsm =
47 va_arg (*args, snat_main_per_thread_data_t *);
48 snat_session_t *sess = va_arg (*args, snat_session_t *);
49 f64 now = va_arg (*args, f64);
51 if (nat44_ed_is_unk_proto (sess->proto))
53 s = format (s, " i2o %U proto %u fib %u\n",
54 format_ip4_address, &sess->in2out.addr,
55 sess->in2out.port, sess->in2out.fib_index);
57 format (s, " o2i %U proto %u fib %u\n", format_ip4_address,
58 &sess->out2in.addr, sess->out2in.port, sess->out2in.fib_index);
62 s = format (s, " i2o %U proto %U port %d fib %d\n", format_ip4_address,
63 &sess->in2out.addr, format_ip_protocol, sess->proto,
64 clib_net_to_host_u16 (sess->in2out.port),
65 sess->in2out.fib_index);
66 s = format (s, " o2i %U proto %U port %d fib %d\n",
67 format_ip4_address, &sess->out2in.addr, format_ip_protocol,
68 sess->proto, clib_net_to_host_u16 (sess->out2in.port),
69 sess->out2in.fib_index);
71 if (nat44_ed_is_twice_nat_session (sess))
73 s = format (s, " external host o2i %U:%d i2o %U:%d\n",
74 format_ip4_address, &sess->ext_host_addr,
75 clib_net_to_host_u16 (sess->ext_host_port),
76 format_ip4_address, &sess->ext_host_nat_addr,
77 clib_net_to_host_u16 (sess->ext_host_nat_port));
81 if (sess->ext_host_addr.as_u32)
82 s = format (s, " external host %U:%u\n",
83 format_ip4_address, &sess->ext_host_addr,
84 clib_net_to_host_u16 (sess->ext_host_port));
86 s = format (s, " i2o flow: %U\n", format_nat_6t_flow, &sess->i2o);
87 s = format (s, " o2i flow: %U\n", format_nat_6t_flow, &sess->o2i);
88 s = format (s, " index %llu\n", sess - tsm->sessions);
89 s = format (s, " last heard %.2f\n", sess->last_heard);
90 s = format (s, " timeout in %.2f\n",
91 nat44_session_get_timeout (sm, sess) - (now - sess->last_heard));
92 s = format (s, " total pkts %d, total bytes %lld\n", sess->total_pkts,
94 if (nat44_ed_is_session_static (sess))
95 s = format (s, " static translation\n");
97 s = format (s, " dynamic translation\n");
98 if (na44_ed_is_fwd_bypass_session (sess))
99 s = format (s, " forwarding-bypass\n");
100 if (nat44_ed_is_lb_session (sess))
101 s = format (s, " load-balancing\n");
102 if (nat44_ed_is_twice_nat_session (sess))
103 s = format (s, " twice-nat\n");
108 format_snat_static_mapping (u8 * s, va_list * args)
110 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
111 nat44_lb_addr_port_t *local;
113 if (is_sm_identity_nat (m->flags))
115 if (is_sm_addr_only (m->flags))
116 s = format (s, "identity mapping %U",
117 format_ip4_address, &m->local_addr);
119 s = format (s, "identity mapping %U %U:%d", format_ip_protocol,
120 m->proto, format_ip4_address, &m->local_addr,
121 clib_net_to_host_u16 (m->local_port));
123 pool_foreach (local, m->locals)
125 s = format (s, " vrf %d", local->vrf_id);
131 if (is_sm_addr_only (m->flags))
133 format (s, "local %U external %U vrf %d %s %s", format_ip4_address,
134 &m->local_addr, format_ip4_address, &m->external_addr, m->vrf_id,
135 is_sm_twice_nat (m->flags) ?
137 is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
138 is_sm_out2in_only (m->flags) ? "out2in-only" : "");
141 if (is_sm_lb (m->flags))
144 format (s, "%U external %U:%d %s %s", format_ip_protocol, m->proto,
145 format_ip4_address, &m->external_addr,
146 clib_net_to_host_u16 (m->external_port),
147 is_sm_twice_nat (m->flags) ?
149 is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
150 is_sm_out2in_only (m->flags) ? "out2in-only" : "");
152 pool_foreach (local, m->locals)
154 s = format (s, "\n local %U:%d vrf %d probability %d\%",
155 format_ip4_address, &local->addr,
156 clib_net_to_host_u16 (local->port), local->vrf_id,
161 s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
162 format_ip_protocol, m->proto, format_ip4_address,
163 &m->local_addr, clib_net_to_host_u16 (m->local_port),
164 format_ip4_address, &m->external_addr,
165 clib_net_to_host_u16 (m->external_port), m->vrf_id,
166 is_sm_twice_nat (m->flags) ?
168 is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
169 is_sm_out2in_only (m->flags) ? "out2in-only" : "");
175 format_snat_static_map_to_resolve (u8 * s, va_list * args)
177 snat_static_mapping_resolve_t *m =
178 va_arg (*args, snat_static_mapping_resolve_t *);
179 vnet_main_t *vnm = vnet_get_main ();
181 if (is_sm_addr_only (m->flags))
182 s = format (s, "local %U external %U vrf %d",
183 format_ip4_address, &m->l_addr,
184 format_vnet_sw_if_index_name, vnm, m->sw_if_index, m->vrf_id);
186 s = format (s, "%U local %U:%d external %U:%d vrf %d", format_ip_protocol,
187 m->proto, format_ip4_address, &m->l_addr,
188 clib_net_to_host_u16 (m->l_port), format_vnet_sw_if_index_name,
189 vnm, m->sw_if_index, clib_net_to_host_u16 (m->e_port),
196 * fd.io coding-style-patch-verification: ON
199 * eval: (c-set-style "gnu")