FD.io VPP  v19.04.4-rc0-5-ge88582fac
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_det.h>
22 
23 uword
24 unformat_snat_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 = SNAT_PROTOCOL_##N;
31 #undef _
32  else
33  return 0;
34  return 1;
35 }
36 
37 u8 *
38 format_snat_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 SNAT_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  snat_session_key_t *key = va_arg (*args, snat_session_key_t *);
79 
80  s = format (s, "%U proto %U port %d fib %d",
81  format_ip4_address, &key->addr,
83  clib_net_to_host_u16 (key->port), key->fib_index);
84  return s;
85 }
86 
87 u8 *
88 format_static_mapping_key (u8 * s, va_list * args)
89 {
90  snat_session_key_t *key = va_arg (*args, snat_session_key_t *);
91 
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);
95  return s;
96 }
97 
98 u8 *
99 format_snat_session_state (u8 * s, va_list * args)
100 {
101  u32 i = va_arg (*args, u32);
102  u8 *t = 0;
103 
104  switch (i)
105  {
106 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
108 #undef _
109  default:
110  t = format (t, "unknown");
111  }
112  s = format (s, "%s", t);
113  return s;
114 }
115 
116 u8 *
117 format_snat_session (u8 * s, va_list * args)
118 {
120  va_arg (*args, snat_main_per_thread_data_t *);
121  snat_session_t *sess = va_arg (*args, snat_session_t *);
122 
123  if (snat_is_unk_proto_session (sess))
124  {
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);
133  }
134  else
135  {
136  s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
137  s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
138  }
139  if (is_ed_session (sess) || is_fwd_bypass_session (sess))
140  {
141  if (is_twice_nat_session (sess))
142  {
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));
148  }
149  else
150  {
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));
155  }
156  }
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");
163  else
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");
171 
172  return s;
173 }
174 
175 u8 *
176 format_snat_user (u8 * s, va_list * args)
177 {
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;
184  u32 session_index;
185  snat_session_t *sess;
186 
187  s = format (s, "%U: %d dynamic translations, %d static translations\n",
189 
190  if (verbose == 0)
191  return s;
192 
193  if (u->nsessions || u->nstaticsessions)
194  {
195  head_index = u->sessions_per_user_list_head_index;
196  head = pool_elt_at_index (sm->list_pool, head_index);
197 
198  elt_index = head->next;
199  elt = pool_elt_at_index (sm->list_pool, elt_index);
200  session_index = elt->value;
201 
202  while (session_index != ~0)
203  {
204  sess = pool_elt_at_index (sm->sessions, session_index);
205 
206  s = format (s, " %U\n", format_snat_session, sm, sess);
207 
208  elt_index = elt->next;
209  elt = pool_elt_at_index (sm->list_pool, elt_index);
210  session_index = elt->value;
211  }
212  }
213 
214  return s;
215 }
216 
217 u8 *
218 format_snat_static_mapping (u8 * s, va_list * args)
219 {
220  snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
221  nat44_lb_addr_port_t *local;
222 
224  {
226  s = format (s, "identity mapping %U",
228  else
229  s = format (s, "identity mapping %U %U:%d",
232 
233  /* *INDENT-OFF* */
234  pool_foreach (local, m->locals,
235  ({
236  s = format (s, " vrf %d", local->vrf_id);
237  }));
238  /* *INDENT-ON* */
239 
240  return s;
241  }
242 
244  s = format (s, "local %U external %U vrf %d %s %s",
247  m->vrf_id,
248  m->twice_nat == TWICE_NAT ? "twice-nat" :
249  m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
250  is_out2in_only_static_mapping (m) ? "out2in-only" : "");
251  else
252  {
253  if (is_lb_static_mapping (m))
254  {
255  s = format (s, "%U external %U:%d %s %s",
258  m->twice_nat == TWICE_NAT ? "twice-nat" :
259  m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
260  is_out2in_only_static_mapping (m) ? "out2in-only" : "");
261 
262  /* *INDENT-OFF* */
263  pool_foreach (local, m->locals,
264  ({
265  s = format (s, "\n local %U:%d vrf %d probability %d\%",
266  format_ip4_address, &local->addr, local->port,
267  local->vrf_id, local->probability);
268  }));
269  /* *INDENT-ON* */
270 
271  }
272  else
273  s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
277  m->vrf_id,
278  m->twice_nat == TWICE_NAT ? "twice-nat" :
279  m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
280  is_out2in_only_static_mapping (m) ? "out2in-only" : "");
281  }
282  return s;
283 }
284 
285 u8 *
287 {
289  vnet_main_t *vnm = vnet_get_main ();
290 
291  if (m->addr_only)
292  s = format (s, "local %U external %U vrf %d",
295  else
296  s = format (s, "%U local %U:%d external %U:%d vrf %d",
300  m->e_port, m->vrf_id);
301 
302  return s;
303 }
304 
305 u8 *
306 format_det_map_ses (u8 * s, va_list * args)
307 {
308  snat_det_map_t *det_map = va_arg (*args, snat_det_map_t *);
309  ip4_address_t in_addr, out_addr;
310  u32 in_offset, out_offset;
311  snat_det_session_t *ses = va_arg (*args, snat_det_session_t *);
312  u32 *i = va_arg (*args, u32 *);
313 
314  u32 user_index = *i / SNAT_DET_SES_PER_USER;
315  in_addr.as_u32 =
316  clib_host_to_net_u32 (clib_net_to_host_u32 (det_map->in_addr.as_u32) +
317  user_index);
318  in_offset =
319  clib_net_to_host_u32 (in_addr.as_u32) -
320  clib_net_to_host_u32 (det_map->in_addr.as_u32);
321  out_offset = in_offset / det_map->sharing_ratio;
322  out_addr.as_u32 =
323  clib_host_to_net_u32 (clib_net_to_host_u32 (det_map->out_addr.as_u32) +
324  out_offset);
325  s =
326  format (s,
327  "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
328  format_ip4_address, &in_addr, clib_net_to_host_u16 (ses->in_port),
329  format_ip4_address, &out_addr,
330  clib_net_to_host_u16 (ses->out.out_port), format_ip4_address,
331  &ses->out.ext_host_addr,
332  clib_net_to_host_u16 (ses->out.ext_host_port),
334 
335  return s;
336 }
337 
338 u8 *
339 format_nat44_reass_trace (u8 * s, va_list * args)
340 {
341  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
342  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
343  nat44_reass_trace_t *t = va_arg (*args, nat44_reass_trace_t *);
344 
345  s = format (s, "NAT44_REASS: sw_if_index %d, next index %d, status %s",
346  t->sw_if_index, t->next_index,
347  t->cached ? "cached" : "translated");
348 
349  return s;
350 }
351 
352 /*
353  * fd.io coding-style-patch-verification: ON
354  *
355  * Local Variables:
356  * eval: (c-set-style "gnu")
357  * End:
358  */
ip4_address_t external_addr
Definition: nat.h:333
u32 next
Definition: dlist.h:30
#define snat_is_session_static(s)
Check if SNAT session is created from static mapping.
Definition: nat.h:631
u32 sessions_per_user_list_head_index
Definition: nat.h:239
#define CLIB_UNUSED(x)
Definition: clib.h:82
u16 ext_host_port
Definition: nat.h:85
u16 out_port
Definition: nat.h:86
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
u32 nsessions
Definition: nat.h:240
#define is_ed_session(s)
Check if NAT session is endpoint dependent.
Definition: nat.h:661
snat_protocol_t proto
Definition: nat.h:344
u8 * format_snat_session_state(u8 *s, va_list *args)
Definition: nat_format.c:99
u32 nstaticsessions
Definition: nat.h:241
uword unformat_snat_protocol(unformat_input_t *input, va_list *args)
Definition: nat_format.c:24
int i
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
dlist_elt_t * list_pool
Definition: nat.h:402
#define snat_is_unk_proto_session(s)
Check if SNAT session for unknown protocol.
Definition: nat.h:637
u8 * format_snat_static_map_to_resolve(u8 *s, va_list *args)
Definition: nat_format.c:286
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:352
format_function_t format_ip4_address
Definition: format.h:75
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
ip4_address_t ext_host_addr
Definition: nat.h:84
u8 * format_snat_protocol(u8 *s, va_list *args)
Definition: nat_format.c:38
ip4_address_t addr
Definition: nat.h:237
#define SNAT_DET_SES_PER_USER
Definition: nat_det.h:30
#define is_fwd_bypass_session(s)
Check if NAT session is forwarding bypass.
Definition: nat.h:655
unsigned int u32
Definition: types.h:88
ip4_address_t local_addr
Definition: nat.h:331
snat_protocol_t proto
Definition: nat.h:372
twice_nat_type_t twice_nat
Definition: nat.h:339
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
struct _unformat_input_t unformat_input_t
u16 protocol
Definition: nat.h:54
#define is_lb_session(s)
Check if NAT session is load-balancing.
Definition: nat.h:649
u8 * format_nat_addr_and_port_alloc_alg(u8 *s, va_list *args)
Definition: nat_format.c:57
vlib_main_t * vm
Definition: buffer.c:312
deterministic NAT definitions
u32 sharing_ratio
Definition: nat.h:285
ip4_address_t out_addr
Definition: nat.h:282
ip4_address_t addr
Definition: nat.h:52
#define is_addr_only_static_mapping(sm)
Check if NAT static mapping is address only (1:1NAT).
Definition: nat.h:691
Definition: nat.h:313
#define is_identity_static_mapping(sm)
Check if NAT static mapping is identity NAT.
Definition: nat.h:703
ip4_address_t l_addr
Definition: nat.h:367
ip4_address_t in_addr
Definition: nat.h:279
u8 * format_snat_user(u8 *s, va_list *args)
Definition: nat_format.c:176
u32 value
Definition: dlist.h:32
#define is_lb_static_mapping(sm)
Check if NAT static mapping is load-balancing.
Definition: nat.h:709
#define is_out2in_only_static_mapping(sm)
Check if NAT static mapping match only out2in direction.
Definition: nat.h:697
u64 uword
Definition: types.h:112
u8 * format_det_map_ses(u8 *s, va_list *args)
Definition: nat_format.c:306
typedef key
Definition: ipsec.api:244
snat_det_out_key_t out
Definition: nat.h:269
u8 * format_snat_static_mapping(u8 *s, va_list *args)
Definition: nat_format.c:218
#define is_twice_nat_session(s)
Check if NAT session is twice NAT.
Definition: nat.h:643
u8 * format_nat44_reass_trace(u8 *s, va_list *args)
Definition: nat_format.c:339
snat_session_t * sessions
Definition: nat.h:399
u8 * format_snat_session(u8 *s, va_list *args)
Definition: nat_format.c:117
u16 fib_index
Definition: nat.h:54
u8 * format_static_mapping_key(u8 *s, va_list *args)
Definition: nat_format.c:88