FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
nat44_ed_format.c
Go to the documentation of this file.
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>
22 
23 uword
24 unformat_nat_protocol (unformat_input_t * input, va_list * args)
25 {
26  u32 *r = va_arg (*args, u32 *);
27 
28  if (0);
29 #define _(N, i, n, s) else if (unformat (input, s)) *r = NAT_PROTOCOL_##N;
31 #undef _
32  else
33  return 0;
34  return 1;
35 }
36 
37 u8 *
38 format_nat_protocol (u8 * s, va_list * args)
39 {
40  u32 i = va_arg (*args, u32);
41  u8 *t = 0;
42 
43  switch (i)
44  {
45 #define _(N, j, n, str) case NAT_PROTOCOL_##N: t = (u8 *) str; break;
47 #undef _
48  default:
49  s = format (s, "unknown");
50  return s;
51  }
52  s = format (s, "%s", t);
53  return s;
54 }
55 
56 u8 *
58 {
59  u32 i = va_arg (*args, u32);
60  u8 *t = 0;
61 
62  switch (i)
63  {
64 #define _(v, N, s) case NAT_ADDR_AND_PORT_ALLOC_ALG_##N: t = (u8 *) s; break;
66 #undef _
67  default:
68  s = format (s, "unknown");
69  return s;
70  }
71  s = format (s, "%s", t);
72  return s;
73 }
74 
75 u8 *
76 format_snat_key (u8 * s, va_list * args)
77 {
78  u64 key = va_arg (*args, u64);
79 
81  u16 port;
83  u32 fib_index;
84 
85  split_nat_key (key, &addr, &port, &fib_index, &protocol);
86 
87  s = format (s, "%U proto %U port %d fib %d",
90  clib_net_to_host_u16 (port), fib_index);
91  return s;
92 }
93 
94 u8 *
95 format_snat_session_state (u8 * s, va_list * args)
96 {
97  u32 i = va_arg (*args, u32);
98  u8 *t = 0;
99 
100  switch (i)
101  {
102 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
104 #undef _
105  default:
106  t = format (t, "unknown");
107  }
108  s = format (s, "%s", t);
109  return s;
110 }
111 
112 u8 *
113 format_snat_session (u8 * s, va_list * args)
114 {
116  va_arg (*args, snat_main_per_thread_data_t *);
117  snat_session_t *sess = va_arg (*args, snat_session_t *);
118 
119  if (snat_is_unk_proto_session (sess))
120  {
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);
124  s =
125  format (s, " o2i %U proto %u fib %u\n", format_ip4_address,
126  &sess->out2in.addr, sess->out2in.port, sess->out2in.fib_index);
127  }
128  else
129  {
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);
139  }
140  if (is_ed_session (sess) || is_fwd_bypass_session (sess))
141  {
142  if (is_twice_nat_session (sess))
143  {
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));
149  }
150  else
151  {
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));
156  }
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);
159  }
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");
166  else
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");
174  return s;
175 }
176 
177 u8 *
178 format_snat_static_mapping (u8 * s, va_list * args)
179 {
180  snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
181  nat44_lb_addr_port_t *local;
182 
184  {
186  s = format (s, "identity mapping %U",
188  else
189  s = format (s, "identity mapping %U %U:%d",
192  clib_net_to_host_u16 (m->local_port));
193 
194  /* *INDENT-OFF* */
195  pool_foreach (local, m->locals)
196  {
197  s = format (s, " vrf %d", local->vrf_id);
198  }
199  /* *INDENT-ON* */
200 
201  return s;
202  }
203 
205  s = format (s, "local %U external %U vrf %d %s %s",
208  m->vrf_id,
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" : "");
212  else
213  {
214  if (is_lb_static_mapping (m))
215  {
216  s = format (s, "%U external %U:%d %s %s",
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" : "");
223 
224  /* *INDENT-OFF* */
225  pool_foreach (local, m->locals)
226  {
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);
231  }
232  /* *INDENT-ON* */
233 
234  }
235  else
236  s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
239  clib_net_to_host_u16 (m->local_port),
241  clib_net_to_host_u16 (m->external_port),
242  m->vrf_id,
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" : "");
246  }
247  return s;
248 }
249 
250 u8 *
252 {
254  vnet_main_t *vnm = vnet_get_main ();
255 
256  if (m->addr_only)
257  s = format (s, "local %U external %U vrf %d",
260  else
261  s = format (s, "%U local %U:%d external %U:%d vrf %d",
264  clib_net_to_host_u16 (m->l_port),
266  clib_net_to_host_u16 (m->e_port), m->vrf_id);
267 
268  return s;
269 }
270 
271 /*
272  * fd.io coding-style-patch-verification: ON
273  *
274  * Local Variables:
275  * eval: (c-set-style "gnu")
276  * End:
277  */
format_snat_key
u8 * format_snat_key(u8 *s, va_list *args)
Definition: nat44_ed_format.c:76
snat_static_mapping_t::locals
nat44_lb_addr_port_t * locals
Definition: nat44_ed.h:434
nat44_lb_addr_port_t::addr
ip4_address_t addr
Definition: nat44_ed.h:377
format_ip4_address
format_function_t format_ip4_address
Definition: format.h:73
snat_static_mapping_t::vrf_id
u32 vrf_id
Definition: nat44_ed.h:423
is_out2in_only_static_mapping
#define is_out2in_only_static_mapping(sm)
Check if NAT static mapping match only out2in direction.
Definition: nat44_ed.h:796
is_ed_session
#define is_ed_session(s)
Check if NAT session is endpoint dependent.
Definition: nat44_ed.h:754
foreach_snat_session_state
@ foreach_snat_session_state
Definition: nat44_ed.h:123
format_snat_session
u8 * format_snat_session(u8 *s, va_list *args)
Definition: nat44_ed_format.c:113
nat_protocol_t
nat_protocol_t
Definition: lib.h:63
snat_static_map_resolve_t::l_port
u16 l_port
Definition: nat44_ed.h:451
u16
unsigned short u16
Definition: types.h:57
port
u16 port
Definition: lb_types.api:73
snat_static_mapping_t::proto
nat_protocol_t proto
Definition: nat44_ed.h:426
nat44_lb_addr_port_t::port
u16 port
Definition: nat44_ed.h:379
snat_static_mapping_t::local_addr
ip4_address_t local_addr
Definition: nat44_ed.h:413
unformat_input_t
struct _unformat_input_t unformat_input_t
addr
vhost_vring_addr_t addr
Definition: vhost_user.h:130
r
vnet_hw_if_output_node_runtime_t * r
Definition: interface_output.c:1071
is_lb_static_mapping
#define is_lb_static_mapping(sm)
Check if NAT static mapping is load-balancing.
Definition: nat44_ed.h:808
snat_is_session_static
#define snat_is_session_static(s)
Check if SNAT session is created from static mapping.
Definition: nat44_ed.h:724
key
typedef key
Definition: ipsec_types.api:88
format_nat_protocol
u8 * format_nat_protocol(u8 *s, va_list *args)
Definition: nat44_ed_format.c:38
foreach_nat_protocol
@ foreach_nat_protocol
Definition: lib.h:66
pool_foreach
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
nat44_lb_addr_port_t::probability
u8 probability
Definition: nat44_ed.h:381
uword
u64 uword
Definition: types.h:112
TWICE_NAT_SELF
@ TWICE_NAT_SELF
Definition: nat44_ed.h:395
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
snat_is_unk_proto_session
#define snat_is_unk_proto_session(s)
Check if SNAT session for unknown protocol.
Definition: nat44_ed.h:730
nat44_lb_addr_port_t
Definition: nat44_ed.h:374
snat_static_mapping_t::external_port
u16 external_port
Definition: nat44_ed.h:419
snat_static_map_resolve_t::addr_only
int addr_only
Definition: nat44_ed.h:457
ip4_address_t
Definition: ip4_packet.h:50
snat_static_mapping_t::twice_nat
twice_nat_type_t twice_nat
Definition: nat44_ed.h:421
foreach_nat_addr_and_port_alloc_alg
@ foreach_nat_addr_and_port_alloc_alg
Definition: nat44_ed.h:103
snat_main_per_thread_data_t::sessions
snat_session_t * sessions
Definition: nat44_ed.h:468
format_nat_addr_and_port_alloc_alg
u8 * format_nat_addr_and_port_alloc_alg(u8 *s, va_list *args)
Definition: nat44_ed_format.c:57
format_snat_static_mapping
u8 * format_snat_static_mapping(u8 *s, va_list *args)
Definition: nat44_ed_format.c:178
vnet_main_t
Definition: vnet.h:76
nat44_ed_inlines.h
nat44_lb_addr_port_t::vrf_id
u32 vrf_id
Definition: nat44_ed.h:384
u64
unsigned long u64
Definition: types.h:89
format_vnet_sw_if_index_name
format_function_t format_vnet_sw_if_index_name
Definition: interface_funcs.h:455
format
description fragment has unexpected format
Definition: map.api:433
TWICE_NAT
@ TWICE_NAT
Definition: nat44_ed.h:393
u32
unsigned int u32
Definition: types.h:88
protocol
vl_api_ip_proto_t protocol
Definition: lb_types.api:72
unformat_nat_protocol
uword unformat_nat_protocol(unformat_input_t *input, va_list *args)
Definition: nat44_ed_format.c:24
is_identity_static_mapping
#define is_identity_static_mapping(sm)
Check if NAT static mapping is identity NAT.
Definition: nat44_ed.h:802
is_addr_only_static_mapping
#define is_addr_only_static_mapping(sm)
Check if NAT static mapping is address only (1:1NAT).
Definition: nat44_ed.h:790
nat44_ed.h
snat_static_map_resolve_t::vrf_id
u32 vrf_id
Definition: nat44_ed.h:454
format_nat_6t_flow
format_function_t format_nat_6t_flow
Definition: nat44_ed.h:1162
snat_static_map_resolve_t::proto
nat_protocol_t proto
Definition: nat44_ed.h:455
snat_static_map_resolve_t::sw_if_index
u32 sw_if_index
Definition: nat44_ed.h:453
u8
unsigned char u8
Definition: types.h:56
is_twice_nat_session
#define is_twice_nat_session(s)
Check if NAT session is twice NAT.
Definition: nat44_ed.h:736
split_nat_key
static void split_nat_key(u64 key, ip4_address_t *addr, u16 *port, u32 *fib_index, nat_protocol_t *proto)
Definition: nat44_ei_inlines.h:34
snat_static_mapping_t::external_addr
ip4_address_t external_addr
Definition: nat44_ed.h:415
is_fwd_bypass_session
#define is_fwd_bypass_session(s)
Check if NAT session is forwarding bypass.
Definition: nat44_ed.h:748
snat_static_mapping_t
Definition: nat44_ed.h:408
snat_static_map_resolve_t
Definition: nat44_ed.h:447
snat_main_per_thread_data_t
Definition: nat44_ed.h:465
is_lb_session
#define is_lb_session(s)
Check if NAT session is load-balancing.
Definition: nat44_ed.h:742
snat_static_mapping_t::local_port
u16 local_port
Definition: nat44_ed.h:417
snat_static_map_resolve_t::e_port
u16 e_port
Definition: nat44_ed.h:452
snat_static_map_resolve_t::l_addr
ip4_address_t l_addr
Definition: nat44_ed.h:449
format_snat_session_state
u8 * format_snat_session_state(u8 *s, va_list *args)
Definition: nat44_ed_format.c:95
format_snat_static_map_to_resolve
u8 * format_snat_static_map_to_resolve(u8 *s, va_list *args)
Definition: nat44_ed_format.c:251