FD.io VPP  v21.01.1
Vector Packet Processing
nat_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/nat.h>
21 #include <nat/nat_inlines.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",
88  format_ip4_address, &addr,
89  format_nat_protocol, protocol,
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 = format (s, " o2i %U proto %u fib %u\n",
125  format_ip4_address, &sess->out2in.addr,
126  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,
137  format_nat_protocol, sess->nat_proto,
138  clib_net_to_host_u16 (sess->out2in.port),
139  sess->out2in.fib_index);
140  }
141  if (is_ed_session (sess) || is_fwd_bypass_session (sess))
142  {
143  if (is_twice_nat_session (sess))
144  {
145  s = format (s, " external host o2i %U:%d i2o %U:%d\n",
146  format_ip4_address, &sess->ext_host_addr,
147  clib_net_to_host_u16 (sess->ext_host_port),
148  format_ip4_address, &sess->ext_host_nat_addr,
149  clib_net_to_host_u16 (sess->ext_host_nat_port));
150  }
151  else
152  {
153  if (sess->ext_host_addr.as_u32)
154  s = format (s, " external host %U:%u\n",
155  format_ip4_address, &sess->ext_host_addr,
156  clib_net_to_host_u16 (sess->ext_host_port));
157  }
158  }
159  s = format (s, " index %llu\n", sess - tsm->sessions);
160  s = format (s, " last heard %.2f\n", sess->last_heard);
161  s = format (s, " total pkts %d, total bytes %lld\n",
162  sess->total_pkts, sess->total_bytes);
163  if (snat_is_session_static (sess))
164  s = format (s, " static translation\n");
165  else
166  s = format (s, " dynamic translation\n");
167  if (is_fwd_bypass_session (sess))
168  s = format (s, " forwarding-bypass\n");
169  if (is_lb_session (sess))
170  s = format (s, " load-balancing\n");
171  if (is_twice_nat_session (sess))
172  s = format (s, " twice-nat\n");
173 
174  return s;
175 }
176 
177 u8 *
178 format_snat_user (u8 * s, va_list * args)
179 {
181  va_arg (*args, snat_main_per_thread_data_t *);
182  snat_user_t *u = va_arg (*args, snat_user_t *);
183  int verbose = va_arg (*args, int);
184  dlist_elt_t *head, *elt;
185  u32 elt_index, head_index;
186  u32 session_index;
187  snat_session_t *sess;
188 
189  s = format (s, "%U: %d dynamic translations, %d static translations\n",
191 
192  if (verbose == 0)
193  return s;
194 
195  if (u->nsessions || u->nstaticsessions)
196  {
197  head_index = u->sessions_per_user_list_head_index;
198  head = pool_elt_at_index (tsm->list_pool, head_index);
199 
200  elt_index = head->next;
201  elt = pool_elt_at_index (tsm->list_pool, elt_index);
202  session_index = elt->value;
203 
204  while (session_index != ~0)
205  {
206  sess = pool_elt_at_index (tsm->sessions, session_index);
207 
208  s = format (s, " %U\n", format_snat_session, tsm, sess);
209 
210  elt_index = elt->next;
211  elt = pool_elt_at_index (tsm->list_pool, elt_index);
212  session_index = elt->value;
213  }
214  }
215 
216  return s;
217 }
218 
219 u8 *
220 format_snat_static_mapping (u8 * s, va_list * args)
221 {
222  snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
223  nat44_lb_addr_port_t *local;
224 
226  {
228  s = format (s, "identity mapping %U",
230  else
231  s = format (s, "identity mapping %U %U:%d",
234  clib_net_to_host_u16 (m->local_port));
235 
236  /* *INDENT-OFF* */
237  pool_foreach (local, m->locals)
238  {
239  s = format (s, " vrf %d", local->vrf_id);
240  }
241  /* *INDENT-ON* */
242 
243  return s;
244  }
245 
247  s = format (s, "local %U external %U vrf %d %s %s",
250  m->vrf_id,
251  m->twice_nat == TWICE_NAT ? "twice-nat" :
252  m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
253  is_out2in_only_static_mapping (m) ? "out2in-only" : "");
254  else
255  {
256  if (is_lb_static_mapping (m))
257  {
258  s = format (s, "%U external %U:%d %s %s",
261  clib_net_to_host_u16 (m->external_port),
262  m->twice_nat == TWICE_NAT ? "twice-nat" :
263  m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
264  is_out2in_only_static_mapping (m) ? "out2in-only" : "");
265 
266  /* *INDENT-OFF* */
267  pool_foreach (local, m->locals)
268  {
269  s = format (s, "\n local %U:%d vrf %d probability %d\%",
270  format_ip4_address, &local->addr,
271  clib_net_to_host_u16 (local->port),
272  local->vrf_id, local->probability);
273  }
274  /* *INDENT-ON* */
275 
276  }
277  else
278  s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
281  clib_net_to_host_u16 (m->local_port),
283  clib_net_to_host_u16 (m->external_port),
284  m->vrf_id,
285  m->twice_nat == TWICE_NAT ? "twice-nat" :
286  m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
287  is_out2in_only_static_mapping (m) ? "out2in-only" : "");
288  }
289  return s;
290 }
291 
292 u8 *
294 {
296  vnet_main_t *vnm = vnet_get_main ();
297 
298  if (m->addr_only)
299  s = format (s, "local %U external %U vrf %d",
302  else
303  s = format (s, "%U local %U:%d external %U:%d vrf %d",
306  clib_net_to_host_u16 (m->l_port),
308  clib_net_to_host_u16 (m->e_port), m->vrf_id);
309 
310  return s;
311 }
312 
313 /*
314  * fd.io coding-style-patch-verification: ON
315  *
316  * Local Variables:
317  * eval: (c-set-style "gnu")
318  * End:
319  */
ip4_address_t external_addr
Definition: nat.h:386
u32 next
Definition: dlist.h:30
#define snat_is_session_static(s)
Check if SNAT session is created from static mapping.
Definition: nat.h:786
u32 sessions_per_user_list_head_index
Definition: nat.h:314
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:527
u32 nsessions
Definition: nat.h:315
#define is_ed_session(s)
Check if NAT session is endpoint dependent.
Definition: nat.h:816
unsigned long u64
Definition: types.h:89
ip4_address_t addr
Definition: nat.h:348
vl_api_ip_proto_t protocol
Definition: lb_types.api:72
u8 * format_snat_session_state(u8 *s, va_list *args)
Definition: nat_format.c:95
u32 nstaticsessions
Definition: nat.h:316
nat_protocol_t proto
Definition: nat.h:397
nat_protocol_t
Definition: lib.h:63
dlist_elt_t * list_pool
Definition: nat.h:456
#define snat_is_unk_proto_session(s)
Check if SNAT session for unknown protocol.
Definition: nat.h:792
vhost_vring_addr_t addr
Definition: vhost_user.h:111
u8 * format_snat_static_map_to_resolve(u8 *s, va_list *args)
Definition: nat_format.c:293
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
u8 * format_snat_key(u8 *s, va_list *args)
Definition: nat_format.c:76
nat44_lb_addr_port_t * locals
Definition: nat.h:405
static void split_nat_key(u64 key, ip4_address_t *addr, u16 *port, u32 *fib_index, nat_protocol_t *proto)
Definition: nat_inlines.h:36
format_function_t format_ip4_address
Definition: format.h:73
u8 * format_nat_protocol(u8 *s, va_list *args)
Definition: nat_format.c:38
description fragment has unexpected format
Definition: map.api:433
ip4_address_t addr
Definition: nat.h:312
#define is_fwd_bypass_session(s)
Check if NAT session is forwarding bypass.
Definition: nat.h:810
unsigned int u32
Definition: types.h:88
ip4_address_t local_addr
Definition: nat.h:384
twice_nat_type_t twice_nat
Definition: nat.h:392
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:546
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
#define is_lb_session(s)
Check if NAT session is load-balancing.
Definition: nat.h:804
u8 * format_nat_addr_and_port_alloc_alg(u8 *s, va_list *args)
Definition: nat_format.c:57
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define is_addr_only_static_mapping(sm)
Check if NAT static mapping is address only (1:1NAT).
Definition: nat.h:852
Definition: nat.h:364
#define is_identity_static_mapping(sm)
Check if NAT static mapping is identity NAT.
Definition: nat.h:864
ip4_address_t l_addr
Definition: nat.h:420
u8 * format_snat_user(u8 *s, va_list *args)
Definition: nat_format.c:178
u32 value
Definition: dlist.h:32
uword unformat_nat_protocol(unformat_input_t *input, va_list *args)
Definition: nat_format.c:24
typedef key
Definition: ipsec_types.api:86
#define is_lb_static_mapping(sm)
Check if NAT static mapping is load-balancing.
Definition: nat.h:870
#define is_out2in_only_static_mapping(sm)
Check if NAT static mapping match only out2in direction.
Definition: nat.h:858
u64 uword
Definition: types.h:112
u8 * format_snat_static_mapping(u8 *s, va_list *args)
Definition: nat_format.c:220
u16 port
Definition: lb_types.api:73
nat_protocol_t proto
Definition: nat.h:426
#define is_twice_nat_session(s)
Check if NAT session is twice NAT.
Definition: nat.h:798
snat_session_t * sessions
Definition: nat.h:453
u8 * format_snat_session(u8 *s, va_list *args)
Definition: nat_format.c:113