FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
snat.c
Go to the documentation of this file.
1 /*
2  * snat.c - simple nat plugin
3  *
4  * Copyright (c) 2016 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <vlibapi/api.h>
21 #include <snat/snat.h>
22 
23 #include <vlibapi/api.h>
24 #include <vlibmemory/api.h>
25 #include <vlibsocket/api.h>
26 
28 
29 /* define message IDs */
30 #include <snat/snat_msg_enum.h>
31 
32 /* define message structures */
33 #define vl_typedefs
34 #include <snat/snat_all_api_h.h>
35 #undef vl_typedefs
36 
37 /* define generated endian-swappers */
38 #define vl_endianfun
39 #include <snat/snat_all_api_h.h>
40 #undef vl_endianfun
41 
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 
44 /* Get the API version number */
45 #define vl_api_version(n,v) static u32 api_version=(v);
46 #include <snat/snat_all_api_h.h>
47 #undef vl_api_version
48 
49 /* Macro to finish up custom dump fns */
50 #define FINISH \
51  vec_add1 (s, 0); \
52  vl_print (handle, (char *)s); \
53  vec_free (s); \
54  return handle;
55 
56 /*
57  * A handy macro to set up a message reply.
58  * Assumes that the following variables are available:
59  * mp - pointer to request message
60  * rmp - pointer to reply message type
61  * rv - return value
62  */
63 
64 #define REPLY_MACRO(t) \
65 do { \
66  unix_shared_memory_queue_t * q = \
67  vl_api_client_index_to_input_queue (mp->client_index); \
68  if (!q) \
69  return; \
70  \
71  rmp = vl_msg_api_alloc (sizeof (*rmp)); \
72  rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
73  rmp->context = mp->context; \
74  rmp->retval = ntohl(rv); \
75  \
76  vl_msg_api_send_shmem (q, (u8 *)&rmp); \
77 } while(0);
78 
79 #define REPLY_MACRO2(t, body) \
80 do { \
81  unix_shared_memory_queue_t * q = \
82  vl_api_client_index_to_input_queue (mp->client_index); \
83  if (!q) \
84  return; \
85  \
86  rmp = vl_msg_api_alloc (sizeof (*rmp)); \
87  rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
88  rmp->context = mp->context; \
89  rmp->retval = ntohl(rv); \
90  do {body;} while (0); \
91  vl_msg_api_send_shmem (q, (u8 *)&rmp); \
92 } while(0);
93 
94 
95 /* Hook up input features */
96 VNET_IP4_UNICAST_FEATURE_INIT (ip4_snat_in2out, static) = {
97  .node_name = "snat-in2out",
98  .runs_before = (char *[]){"snat-out2in", 0},
99  .feature_index = &snat_main.rx_feature_in2out,
100 };
101 VNET_IP4_UNICAST_FEATURE_INIT (ip4_snat_out2in, static) = {
102  .node_name = "snat-out2in",
103  .runs_before = (char *[]){"ip4-lookup", 0},
104  .feature_index = &snat_main.rx_feature_out2in,
105 };
106 VNET_IP4_UNICAST_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
107  .node_name = "snat-in2out-fast",
108  .runs_before = (char *[]){"snat-out2in-fast", 0},
109  .feature_index = &snat_main.rx_feature_in2out_fast,
110 };
111 VNET_IP4_UNICAST_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
112  .node_name = "snat-out2in-fast",
113  .runs_before = (char *[]){"ip4-lookup", 0},
114  .feature_index = &snat_main.rx_feature_out2in_fast,
115 };
116 
117 
118 /*
119  * This routine exists to convince the vlib plugin framework that
120  * we haven't accidentally copied a random .dll into the plugin directory.
121  *
122  * Also collects global variable pointers passed from the vpp engine
123  */
124 
125 clib_error_t *
127  int from_early_init)
128 {
129  snat_main_t * sm = &snat_main;
130  clib_error_t * error = 0;
131 
132  sm->vlib_main = vm;
133  sm->vnet_main = h->vnet_main;
134  sm->ethernet_main = h->ethernet_main;
135 
136  return error;
137 }
138 
139 /*$$$$$ move to an installed header file */
140 #if (1 || CLIB_DEBUG > 0) /* "trust, but verify" */
141 
142 #define VALIDATE_SW_IF_INDEX(mp) \
143  do { u32 __sw_if_index = ntohl(mp->sw_if_index); \
144  vnet_main_t *__vnm = vnet_get_main(); \
145  if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
146  __sw_if_index)) { \
147  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
148  goto bad_sw_if_index; \
149  } \
150 } while(0);
151 
152 #define BAD_SW_IF_INDEX_LABEL \
153 do { \
154 bad_sw_if_index: \
155  ; \
156 } while (0);
157 
158 #define VALIDATE_RX_SW_IF_INDEX(mp) \
159  do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index); \
160  vnet_main_t *__vnm = vnet_get_main(); \
161  if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
162  __rx_sw_if_index)) { \
163  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
164  goto bad_rx_sw_if_index; \
165  } \
166 } while(0);
167 
168 #define BAD_RX_SW_IF_INDEX_LABEL \
169 do { \
170 bad_rx_sw_if_index: \
171  ; \
172 } while (0);
173 
174 #define VALIDATE_TX_SW_IF_INDEX(mp) \
175  do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index); \
176  vnet_main_t *__vnm = vnet_get_main(); \
177  if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
178  __tx_sw_if_index)) { \
179  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
180  goto bad_tx_sw_if_index; \
181  } \
182 } while(0);
183 
184 #define BAD_TX_SW_IF_INDEX_LABEL \
185 do { \
186 bad_tx_sw_if_index: \
187  ; \
188 } while (0);
189 
190 #else
191 
192 #define VALIDATE_SW_IF_INDEX(mp)
193 #define BAD_SW_IF_INDEX_LABEL
194 #define VALIDATE_RX_SW_IF_INDEX(mp)
195 #define BAD_RX_SW_IF_INDEX_LABEL
196 #define VALIDATE_TX_SW_IF_INDEX(mp)
197 #define BAD_TX_SW_IF_INDEX_LABEL
198 
199 #endif /* CLIB_DEBUG > 0 */
200 
202 {
203  snat_address_t * ap;
204 
205  /* Check if address already exists */
206  vec_foreach (ap, sm->addresses)
207  {
208  if (ap->addr.as_u32 == addr->as_u32)
209  return;
210  }
211 
212  vec_add2 (sm->addresses, ap, 1);
213  ap->addr = *addr;
214 
215 }
216 
219 {
222  ({
223  if (m->external_addr.as_u32 == addr.as_u32)
224  return 1;
225  }));
226 
227  return 0;
228 }
229 
231 {
232  clib_warning("%U", format_ip4_address, &addr);
233  snat_address_t *a = 0;
234  snat_session_t *ses;
235  u32 *ses_to_be_removed = 0, *ses_index;
236  clib_bihash_kv_8_8_t kv, value;
237  snat_user_key_t user_key;
238  snat_user_t *u;
239 
240  int i;
241 
242  /* Find SNAT address */
243  for (i=0; i < vec_len (sm->addresses); i++)
244  {
245  if (sm->addresses[i].addr.as_u32 == addr.as_u32)
246  {
247  a = sm->addresses + i;
248  break;
249  }
250  }
251  if (!a)
252  return VNET_API_ERROR_NO_SUCH_ENTRY;
253 
254  /* Check if address is used in some static mapping */
256  {
257  clib_warning ("address used in static mapping");
258  return VNET_API_ERROR_UNSPECIFIED;
259  }
260 
261  /* Delete sessions using address */
262  if (a->busy_ports)
263  {
264  pool_foreach (ses, sm->sessions, ({
265  if (ses->out2in.addr.as_u32 == addr.as_u32)
266  {
267  vec_add1 (ses_to_be_removed, ses - sm->sessions);
268  kv.key = ses->in2out.as_u64;
269  clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
270  kv.key = ses->out2in.as_u64;
271  clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
272  clib_dlist_remove (sm->list_pool, ses->per_user_index);
273  user_key.addr = ses->in2out.addr;
274  user_key.fib_index = ses->in2out.fib_index;
275  kv.key = user_key.as_u64;
276  if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
277  {
278  u = pool_elt_at_index (sm->users, value.value);
279  u->nsessions--;
280  }
281  }
282  }));
283 
284  vec_foreach (ses_index, ses_to_be_removed)
285  pool_put_index (sm->sessions, ses_index[0]);
286 
287  vec_free (ses_to_be_removed);
288  }
289 
290  vec_del1 (sm->addresses, i);
291 
292  return 0;
293 }
294 
296 {
297  u32 v;
298 
299  v = clib_net_to_host_u32(a->as_u32) + 1;
300  a->as_u32 = clib_host_to_net_u32(v);
301 }
302 
303 /**
304  * @brief Add static mapping.
305  *
306  * Create static mapping between local addr+port and external addr+port.
307  *
308  * @param l_addr Local IPv4 address.
309  * @param e_addr External IPv4 address.
310  * @param l_port Local port number.
311  * @param e_port External port number.
312  * @param vrf_id VRF ID.
313  * @param addr_only If 0 address port and pair mapping, otherwise address only.
314  * @param is_add If 0 delete static mapping, otherwise add.
315  *
316  * @returns
317  */
319  u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
320  int is_add)
321 {
322  snat_main_t * sm = &snat_main;
325  clib_bihash_kv_8_8_t kv, value;
326  snat_address_t *a = 0;
327  u32 fib_index = ~0;
328  uword * p;
329  int i;
330 
331  /* If outside FIB index is not resolved yet */
332  if (sm->outside_fib_index == ~0)
333  {
335  if (!p)
336  return VNET_API_ERROR_NO_SUCH_FIB;
337  sm->outside_fib_index = p[0];
338  }
339 
340  m_key.addr = e_addr;
341  m_key.port = addr_only ? 0 : e_port;
342  m_key.fib_index = sm->outside_fib_index;
343  kv.key = m_key.as_u64;
344  if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
345  m = 0;
346  else
347  m = pool_elt_at_index (sm->static_mappings, value.value);
348 
349  if (is_add)
350  {
351  if (m)
352  return VNET_API_ERROR_VALUE_EXIST;
353 
354  /* Convert VRF id to FIB index */
355  if (vrf_id != ~0)
356  {
357  p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
358  if (!p)
359  return VNET_API_ERROR_NO_SUCH_FIB;
360  fib_index = p[0];
361  }
362  /* If not specified use inside VRF id from SNAT plugin startup config */
363  else
364  {
365  if (sm->inside_fib_index == ~0)
366  {
368  if (!p)
369  return VNET_API_ERROR_NO_SUCH_FIB;
370  fib_index = p[0];
371  sm->inside_fib_index = fib_index;
372  }
373  else
374  fib_index = sm->inside_fib_index;
375 
376  vrf_id = sm->inside_vrf_id;
377  }
378 
379  /* Find external address in allocated addresses and reserve port for
380  address and port pair mapping when dynamic translations enabled */
381  if (!addr_only && !(sm->static_mapping_only))
382  {
383  for (i = 0; i < vec_len (sm->addresses); i++)
384  {
385  if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
386  {
387  a = sm->addresses + i;
388  /* External port must be unused */
389  if (clib_bitmap_get (a->busy_port_bitmap, e_port))
390  return VNET_API_ERROR_INVALID_VALUE;
392  e_port, 1);
393  if (e_port > 1024)
394  a->busy_ports++;
395 
396  break;
397  }
398  }
399  /* External address must be allocated */
400  if (!a)
401  return VNET_API_ERROR_NO_SUCH_ENTRY;
402  }
403 
404  pool_get (sm->static_mappings, m);
405  memset (m, 0, sizeof (*m));
406  m->local_addr = l_addr;
407  m->external_addr = e_addr;
408  m->addr_only = addr_only;
409  m->vrf_id = vrf_id;
410  m->fib_index = fib_index;
411  if (!addr_only)
412  {
413  m->local_port = l_port;
414  m->external_port = e_port;
415  }
416 
417  m_key.addr = m->local_addr;
418  m_key.port = m->local_port;
419  m_key.fib_index = m->fib_index;
420  kv.key = m_key.as_u64;
421  kv.value = m - sm->static_mappings;
422  clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
423 
424  m_key.addr = m->external_addr;
425  m_key.port = m->external_port;
426  m_key.fib_index = sm->outside_fib_index;
427  kv.key = m_key.as_u64;
428  kv.value = m - sm->static_mappings;
429  clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
430  }
431  else
432  {
433  if (!m)
434  return VNET_API_ERROR_NO_SUCH_ENTRY;
435 
436  /* Free external address port */
437  if (!addr_only && !(sm->static_mapping_only))
438  {
439  for (i = 0; i < vec_len (sm->addresses); i++)
440  {
441  if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
442  {
443  a = sm->addresses + i;
445  e_port, 0);
446  a->busy_ports--;
447 
448  break;
449  }
450  }
451  }
452 
453  m_key.addr = m->local_addr;
454  m_key.port = m->local_port;
455  m_key.fib_index = m->fib_index;
456  kv.key = m_key.as_u64;
457  clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
458 
459  m_key.addr = m->external_addr;
460  m_key.port = m->external_port;
461  m_key.fib_index = sm->outside_fib_index;
462  kv.key = m_key.as_u64;
463  clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
464 
465  /* Delete session(s) for static mapping if exist */
466  if (!(sm->static_mapping_only) ||
468  {
469  snat_user_key_t u_key;
470  snat_user_t *u;
471  dlist_elt_t * head, * elt;
472  u32 elt_index, head_index;
473  u32 ses_index;
474  snat_session_t * s;
475 
476  u_key.addr = m->local_addr;
477  u_key.fib_index = m->fib_index;
478  kv.key = u_key.as_u64;
479  if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
480  {
481  u = pool_elt_at_index (sm->users, value.value);
482  if (u->nstaticsessions)
483  {
484  head_index = u->sessions_per_user_list_head_index;
485  head = pool_elt_at_index (sm->list_pool, head_index);
486  elt_index = head->next;
487  elt = pool_elt_at_index (sm->list_pool, elt_index);
488  ses_index = elt->value;
489  while (ses_index != ~0)
490  {
491  s = pool_elt_at_index (sm->sessions, ses_index);
492 
493  if (!addr_only)
494  {
495  if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
496  (clib_net_to_host_u16 (s->out2in.port) != e_port))
497  continue;
498  }
499  value.key = s->in2out.as_u64;
500  clib_bihash_add_del_8_8 (&sm->in2out, &value, 0);
501  value.key = s->out2in.as_u64;
502  clib_bihash_add_del_8_8 (&sm->out2in, &value, 0);
503  pool_put (sm->sessions, s);
504 
505  if (!addr_only)
506  break;
507 
508  elt_index = elt->next;
509  elt = pool_elt_at_index (sm->list_pool, elt_index);
510  ses_index = elt->value;
511  }
512  if (addr_only)
513  {
514  while ((elt_index = clib_dlist_remove_head(sm->list_pool, head_index)) != ~0)
515  pool_put_index (sm->list_pool, elt_index);
516  pool_put (sm->users, u);
517  clib_bihash_add_del_8_8 (&sm->user_hash, &kv, 0);
518  }
519  else
520  {
521  if (ses_index != ~0)
522  {
523  clib_dlist_remove (sm->list_pool, elt_index);
524  pool_put (sm->list_pool, elt);
525  u->nstaticsessions--;
526  }
527  }
528  }
529  }
530  }
531 
532  /* Delete static mapping from pool */
533  pool_put (sm->static_mappings, m);
534  }
535 
536  return 0;
537 }
538 
539 static int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
540 {
541  snat_main_t *sm = &snat_main;
543  u32 ci;
544  ip4_main_t * im = &ip4_main;
545  ip_lookup_main_t * lm = &im->lookup_main;
547  u32 feature_index;
548 
550  feature_index = is_inside ? sm->rx_feature_in2out_fast
552  else
553  feature_index = is_inside ? sm->rx_feature_in2out
554  : sm->rx_feature_out2in;
555 
556  ci = rx_cm->config_index_by_sw_if_index[sw_if_index];
557  ci = (is_del
560  (sm->vlib_main, &rx_cm->config_main,
561  ci,
562  feature_index,
563  0 /* config struct */,
564  0 /* sizeof config struct*/);
565  rx_cm->config_index_by_sw_if_index[sw_if_index] = ci;
566 
567  pool_foreach (i, sm->interfaces,
568  ({
569  if (i->sw_if_index == sw_if_index)
570  {
571  if (is_del)
572  pool_put (sm->interfaces, i);
573  else
574  return VNET_API_ERROR_VALUE_EXIST;
575 
576  return 0;
577  }
578  }));
579 
580  if (is_del)
581  return VNET_API_ERROR_NO_SUCH_ENTRY;
582 
583  pool_get (sm->interfaces, i);
584  i->sw_if_index = sw_if_index;
585  i->is_inside = is_inside;
586 
587  return 0;
588 }
589 
590 static void
593 {
594  snat_main_t * sm = &snat_main;
596  ip4_address_t this_addr;
597  u32 start_host_order, end_host_order;
598  int i, count;
599  int rv = 0;
600  u32 * tmp;
601 
602  if (mp->is_ip4 != 1)
603  {
604  rv = VNET_API_ERROR_UNIMPLEMENTED;
605  goto send_reply;
606  }
607 
608  if (sm->static_mapping_only)
609  {
610  rv = VNET_API_ERROR_FEATURE_DISABLED;
611  goto send_reply;
612  }
613 
614  tmp = (u32 *) mp->first_ip_address;
615  start_host_order = clib_host_to_net_u32 (tmp[0]);
616  tmp = (u32 *) mp->last_ip_address;
617  end_host_order = clib_host_to_net_u32 (tmp[0]);
618 
619  count = (end_host_order - start_host_order) + 1;
620 
621  if (count > 1024)
622  clib_warning ("%U - %U, %d addresses...",
625  count);
626 
627  memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
628 
629  for (i = 0; i < count; i++)
630  {
631  if (mp->is_add)
632  snat_add_address (sm, &this_addr);
633  else
634  rv = snat_del_address (sm, this_addr);
635 
636  if (rv)
637  goto send_reply;
638 
639  increment_v4_address (&this_addr);
640  }
641 
642  send_reply:
643  REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
644 }
645 
648 {
649  u8 * s;
650 
651  s = format (0, "SCRIPT: snat_add_address_range ");
652  s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
653  if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
654  {
655  s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
656  }
657  FINISH;
658 }
659 
660 static void
663 {
665  snat_main_t * sm = &snat_main;
666 
667  rmp = vl_msg_api_alloc (sizeof (*rmp));
668  memset (rmp, 0, sizeof (*rmp));
669  rmp->_vl_msg_id = ntohs (VL_API_SNAT_ADDRESS_DETAILS+sm->msg_id_base);
670  rmp->is_ip4 = 1;
671  clib_memcpy (rmp->ip_address, &(a->addr), 4);
672  rmp->context = context;
673 
674  vl_msg_api_send_shmem (q, (u8 *) & rmp);
675 }
676 
677 static void
680 {
682  snat_main_t * sm = &snat_main;
683  snat_address_t * a;
684 
686  if (q == 0)
687  return;
688 
689  vec_foreach (a, sm->addresses)
691 }
692 
694 (vl_api_snat_address_dump_t *mp, void * handle)
695 {
696  u8 *s;
697 
698  s = format (0, "SCRIPT: snat_address_dump ");
699 
700  FINISH;
701 }
702 
703 static void
706 {
707  snat_main_t * sm = &snat_main;
709  u8 is_del = mp->is_add == 0;
710  u32 sw_if_index = ntohl(mp->sw_if_index);
711  int rv = 0;
712 
714 
715  rv = snat_interface_add_del (sw_if_index, mp->is_inside, is_del);
716 
718 
719  REPLY_MACRO(VL_API_SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY);
720 }
721 
724 {
725  u8 * s;
726 
727  s = format (0, "SCRIPT: snat_interface_add_del_feature ");
728  s = format (s, "sw_if_index %d %s %s",
729  clib_host_to_net_u32(mp->sw_if_index),
730  mp->is_inside ? "in":"out",
731  mp->is_add ? "" : "del");
732 
733  FINISH;
734 }
735 
736 static void
739 {
741  snat_main_t * sm = &snat_main;
742 
743  rmp = vl_msg_api_alloc (sizeof (*rmp));
744  memset (rmp, 0, sizeof (*rmp));
745  rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_DETAILS+sm->msg_id_base);
746  rmp->sw_if_index = ntohl (i->sw_if_index);
747  rmp->is_inside = i->is_inside;
748  rmp->context = context;
749 
750  vl_msg_api_send_shmem (q, (u8 *) & rmp);
751 }
752 
753 static void
756 {
758  snat_main_t * sm = &snat_main;
760 
762  if (q == 0)
763  return;
764 
765  pool_foreach (i, sm->interfaces,
766  ({
767  send_snat_interface_details(i, q, mp->context);
768  }));
769 }
770 
772 (vl_api_snat_interface_dump_t *mp, void * handle)
773 {
774  u8 *s;
775 
776  s = format (0, "SCRIPT: snat_interface_dump ");
777 
778  FINISH;
779 }static void
780 
783 {
784  snat_main_t * sm = &snat_main;
786  ip4_address_t local_addr, external_addr;
787  u16 local_port = 0, external_port = 0;
788  u32 vrf_id;
789  int rv = 0;
790 
791  if (mp->is_ip4 != 1)
792  {
793  rv = VNET_API_ERROR_UNIMPLEMENTED;
794  goto send_reply;
795  }
796 
797  memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
798  memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
799  if (mp->addr_only == 0)
800  {
801  local_port = clib_net_to_host_u16 (mp->local_port);
802  external_port = clib_net_to_host_u16 (mp->external_port);
803  }
804  vrf_id = clib_net_to_host_u32 (mp->vrf_id);
805 
806  rv = snat_add_static_mapping(local_addr, external_addr, local_port,
807  external_port, vrf_id, mp->addr_only,
808  mp->is_add);
809 
810  send_reply:
811  REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
812 }
813 
816 {
817  u8 * s;
818 
819  s = format (0, "SCRIPT: snat_add_static_mapping ");
820  s = format (s, "local_addr %U external_addr %U ",
823 
824  if (mp->addr_only == 0)
825  s = format (s, "local_port %d external_port %d ",
826  clib_net_to_host_u16 (mp->local_port),
827  clib_net_to_host_u16 (mp->external_port));
828 
829  if (mp->vrf_id != ~0)
830  s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
831 
832  FINISH;
833 }
834 
835 static void
838 {
840  snat_main_t * sm = &snat_main;
841 
842  rmp = vl_msg_api_alloc (sizeof (*rmp));
843  memset (rmp, 0, sizeof (*rmp));
844  rmp->_vl_msg_id = ntohs (VL_API_SNAT_STATIC_MAPPING_DETAILS+sm->msg_id_base);
845  rmp->is_ip4 = 1;
846  rmp->addr_only = m->addr_only;
847  clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
849  rmp->local_port = htons (m->local_port);
850  rmp->external_port = htons (m->external_port);
851  rmp->vrf_id = htonl (m->vrf_id);
852  rmp->context = context;
853 
854  vl_msg_api_send_shmem (q, (u8 *) & rmp);
855 }
856 
857 static void
860 {
862  snat_main_t * sm = &snat_main;
864 
866  if (q == 0)
867  return;
868 
870  ({
871  send_snat_static_mapping_details (m, q, mp->context);
872  }));
873 }
874 
877 {
878  u8 *s;
879 
880  s = format (0, "SCRIPT: snat_static_mapping_dump ");
881 
882  FINISH;
883 }
884 
885 static void
888 {
890  snat_main_t * sm = &snat_main;
891  int rv = 0;
892 
893  REPLY_MACRO2(VL_API_SNAT_CONTROL_PING_REPLY,
894  ({
895  rmp->vpe_pid = ntohl (getpid());
896  }));
897 }
898 
900 (vl_api_snat_control_ping_t *mp, void * handle)
901 {
902  u8 *s;
903 
904  s = format (0, "SCRIPT: snat_control_ping ");
905 
906  FINISH;
907 }
908 
909 static void
912 {
914  snat_main_t * sm = &snat_main;
915  int rv = 0;
916 
917  REPLY_MACRO2(VL_API_SNAT_SHOW_CONFIG_REPLY,
918  ({
919  rmp->translation_buckets = htons (sm->translation_buckets);
921  rmp->user_buckets = htons (sm->user_buckets);
922  rmp->user_memory_size = htons (sm->user_memory_size);
924  rmp->outside_vrf_id = htons (sm->outside_vrf_id);
925  rmp->inside_vrf_id = htons (sm->inside_vrf_id);
929  }));
930 }
931 
933 (vl_api_snat_show_config_t *mp, void * handle)
934 {
935  u8 *s;
936 
937  s = format (0, "SCRIPT: snat_show_config ");
938 
939  FINISH;
940 }
941 
942 /* List of message types that this plugin understands */
943 #define foreach_snat_plugin_api_msg \
944 _(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range) \
945 _(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature) \
946 _(SNAT_ADD_STATIC_MAPPING, snat_add_static_mapping) \
947 _(SNAT_CONTROL_PING, snat_control_ping) \
948 _(SNAT_STATIC_MAPPING_DUMP, snat_static_mapping_dump) \
949 _(SNAT_SHOW_CONFIG, snat_show_config) \
950 _(SNAT_ADDRESS_DUMP, snat_address_dump) \
951 _(SNAT_INTERFACE_DUMP, snat_interface_dump)
952 
953 /* Set up the API message handling tables */
954 static clib_error_t *
956 {
957  snat_main_t * sm __attribute__ ((unused)) = &snat_main;
958 #define _(N,n) \
959  vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
960  #n, \
961  vl_api_##n##_t_handler, \
962  vl_noop_handler, \
963  vl_api_##n##_t_endian, \
964  vl_api_##n##_t_print, \
965  sizeof(vl_api_##n##_t), 1);
967 #undef _
968 
969  return 0;
970 }
971 
973 {
974 #define _(n,f) sm->api_main->msg_print_handlers \
975  [VL_API_##n + sm->msg_id_base] \
976  = (void *) vl_api_##f##_t_print;
978 #undef _
979 }
980 
982 {
983  snat_main_t * sm = &snat_main;
984  clib_error_t * error = 0;
985  ip4_main_t * im = &ip4_main;
986  ip_lookup_main_t * lm = &im->lookup_main;
987  u8 * name;
988 
989  name = format (0, "snat_%08x%c", api_version, 0);
990 
991  /* Ask for a correctly-sized block of API message decode slots */
993  ((char *) name, VL_MSG_FIRST_AVAILABLE);
994 
995  sm->vlib_main = vm;
996  sm->vnet_main = vnet_get_main();
997  sm->ip4_main = im;
998  sm->ip4_lookup_main = lm;
999  sm->api_main = &api_main;
1000 
1001  error = snat_plugin_api_hookup (vm);
1003  vec_free(name);
1004 
1005  return error;
1006 }
1007 
1009 
1011  snat_session_key_t * k,
1012  u32 address_index)
1013 {
1014  snat_address_t *a;
1015  u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1016 
1017  ASSERT (address_index < vec_len (sm->addresses));
1018 
1019  a = sm->addresses + address_index;
1020 
1021  ASSERT (clib_bitmap_get (a->busy_port_bitmap, port_host_byte_order) == 1);
1022 
1024  port_host_byte_order, 0);
1025  a->busy_ports--;
1026 }
1027 
1028 /**
1029  * @brief Match SNAT static mapping.
1030  *
1031  * @param sm SNAT main.
1032  * @param match Address and port to match.
1033  * @param mapping External or local address and port of the matched mapping.
1034  * @param by_external If 0 match by local address otherwise match by external
1035  * address.
1036  *
1037  * @returns 0 if match found otherwise 1.
1038  */
1040  snat_session_key_t match,
1041  snat_session_key_t * mapping,
1042  u8 by_external)
1043 {
1044  clib_bihash_kv_8_8_t kv, value;
1047  clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1048 
1049  if (by_external)
1050  mapping_hash = &sm->static_mapping_by_external;
1051 
1052  m_key.addr = match.addr;
1053  m_key.port = clib_net_to_host_u16 (match.port);
1054  m_key.fib_index = match.fib_index;
1055 
1056  kv.key = m_key.as_u64;
1057 
1058  if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1059  {
1060  /* Try address only mapping */
1061  m_key.port = 0;
1062  kv.key = m_key.as_u64;
1063  if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1064  return 1;
1065  }
1066 
1067  m = pool_elt_at_index (sm->static_mappings, value.value);
1068 
1069  if (by_external)
1070  {
1071  mapping->addr = m->local_addr;
1072  /* Address only mapping doesn't change port */
1073  mapping->port = m->addr_only ? match.port
1074  : clib_host_to_net_u16 (m->local_port);
1075  mapping->fib_index = m->fib_index;
1076  }
1077  else
1078  {
1079  mapping->addr = m->external_addr;
1080  /* Address only mapping doesn't change port */
1081  mapping->port = m->addr_only ? match.port
1082  : clib_host_to_net_u16 (m->external_port);
1083  mapping->fib_index = sm->outside_fib_index;
1084  }
1085 
1086  return 0;
1087 }
1088 
1090  snat_session_key_t * k,
1091  u32 * address_indexp)
1092 {
1093  int i;
1094  snat_address_t *a;
1095  u32 portnum;
1096 
1097  for (i = 0; i < vec_len (sm->addresses); i++)
1098  {
1099  if (sm->addresses[i].busy_ports < (65535-1024))
1100  {
1101  a = sm->addresses + i;
1102 
1103  while (1)
1104  {
1105  portnum = random_u32 (&sm->random_seed);
1106  portnum &= 0xFFFF;
1107  if (portnum < 1024)
1108  continue;
1109  if (clib_bitmap_get (a->busy_port_bitmap, portnum))
1110  continue;
1112  portnum, 1);
1113  a->busy_ports++;
1114  /* Caller sets protocol and fib index */
1115  k->addr = a->addr;
1116  k->port = clib_host_to_net_u16(portnum);
1117  *address_indexp = i;
1118  return 0;
1119  }
1120  }
1121  }
1122  /* Totally out of translations to use... */
1123  return 1;
1124 }
1125 
1126 
1127 static clib_error_t *
1129  unformat_input_t * input,
1130  vlib_cli_command_t * cmd)
1131 {
1132  unformat_input_t _line_input, *line_input = &_line_input;
1133  snat_main_t * sm = &snat_main;
1134  ip4_address_t start_addr, end_addr, this_addr;
1135  u32 start_host_order, end_host_order;
1136  int i, count;
1137  int is_add = 1;
1138  int rv = 0;
1139 
1140  /* Get a line of input. */
1141  if (!unformat_user (input, unformat_line_input, line_input))
1142  return 0;
1143 
1144  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1145  {
1146  if (unformat (line_input, "%U - %U",
1147  unformat_ip4_address, &start_addr,
1148  unformat_ip4_address, &end_addr))
1149  ;
1150  else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
1151  end_addr = start_addr;
1152  else if (unformat (line_input, "del"))
1153  is_add = 0;
1154  else
1155  return clib_error_return (0, "unknown input '%U'",
1156  format_unformat_error, input);
1157  }
1158  unformat_free (line_input);
1159 
1160  if (sm->static_mapping_only)
1161  return clib_error_return (0, "static mapping only mode");
1162 
1163  start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
1164  end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
1165 
1166  if (end_host_order < start_host_order)
1167  return clib_error_return (0, "end address less than start address");
1168 
1169  count = (end_host_order - start_host_order) + 1;
1170 
1171  if (count > 1024)
1172  clib_warning ("%U - %U, %d addresses...",
1173  format_ip4_address, &start_addr,
1174  format_ip4_address, &end_addr,
1175  count);
1176 
1177  this_addr = start_addr;
1178 
1179  for (i = 0; i < count; i++)
1180  {
1181  if (is_add)
1182  snat_add_address (sm, &this_addr);
1183  else
1184  rv = snat_del_address (sm, this_addr);
1185 
1186  switch (rv)
1187  {
1188  case VNET_API_ERROR_NO_SUCH_ENTRY:
1189  return clib_error_return (0, "S-NAT address not exist.");
1190  break;
1191  case VNET_API_ERROR_UNSPECIFIED:
1192  return clib_error_return (0, "S-NAT address used in static mapping.");
1193  break;
1194  default:
1195  break;
1196  }
1197 
1198  increment_v4_address (&this_addr);
1199  }
1200 
1201  return 0;
1202 }
1203 
1205  .path = "snat add address",
1206  .short_help = "snat add addresses <ip4-range-start> [- <ip4-range-end>] [del]",
1207  .function = add_address_command_fn,
1208 };
1209 
1210 static clib_error_t *
1212  unformat_input_t * input,
1213  vlib_cli_command_t * cmd)
1214 {
1215  unformat_input_t _line_input, *line_input = &_line_input;
1216  vnet_main_t * vnm = vnet_get_main();
1217  clib_error_t * error = 0;
1218  u32 sw_if_index;
1219  u32 * inside_sw_if_indices = 0;
1220  u32 * outside_sw_if_indices = 0;
1221  int is_del = 0;
1222  int i;
1223 
1224  sw_if_index = ~0;
1225 
1226  /* Get a line of input. */
1227  if (!unformat_user (input, unformat_line_input, line_input))
1228  return 0;
1229 
1230  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1231  {
1232  if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
1233  vnm, &sw_if_index))
1234  vec_add1 (inside_sw_if_indices, sw_if_index);
1235  else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
1236  vnm, &sw_if_index))
1237  vec_add1 (outside_sw_if_indices, sw_if_index);
1238  else if (unformat (line_input, "del"))
1239  is_del = 1;
1240  else
1241  return clib_error_return (0, "unknown input '%U'",
1242  format_unformat_error, input);
1243  }
1244  unformat_free (line_input);
1245 
1246  if (vec_len (inside_sw_if_indices))
1247  {
1248  for (i = 0; i < vec_len(inside_sw_if_indices); i++)
1249  {
1250  sw_if_index = inside_sw_if_indices[i];
1251  snat_interface_add_del (sw_if_index, 1, is_del);
1252  }
1253  }
1254 
1255  if (vec_len (outside_sw_if_indices))
1256  {
1257  for (i = 0; i < vec_len(outside_sw_if_indices); i++)
1258  {
1259  sw_if_index = outside_sw_if_indices[i];
1260  snat_interface_add_del (sw_if_index, 0, is_del);
1261  }
1262  }
1263 
1264  vec_free (inside_sw_if_indices);
1265  vec_free (outside_sw_if_indices);
1266 
1267  return error;
1268 }
1269 
1271  .path = "set interface snat",
1272  .function = snat_feature_command_fn,
1273  .short_help = "set interface snat in <intfc> out <intfc> [del]",
1274 };
1275 
1276 static clib_error_t *
1278  unformat_input_t * input,
1279  vlib_cli_command_t * cmd)
1280 {
1281  unformat_input_t _line_input, *line_input = &_line_input;
1282  clib_error_t * error = 0;
1283  ip4_address_t l_addr, e_addr;
1284  u32 l_port = 0, e_port = 0, vrf_id = ~0;
1285  int is_add = 1;
1286  int addr_only = 1;
1287  int rv;
1288 
1289  /* Get a line of input. */
1290  if (!unformat_user (input, unformat_line_input, line_input))
1291  return 0;
1292 
1293  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1294  {
1295  if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1296  &l_port))
1297  addr_only = 0;
1298  else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1299  ;
1300  else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1301  &e_addr, &e_port))
1302  addr_only = 0;
1303  else if (unformat (line_input, "external %U", unformat_ip4_address,
1304  &e_addr))
1305  ;
1306  else if (unformat (line_input, "vrf %u", &vrf_id))
1307  ;
1308  else if (unformat (line_input, "del"))
1309  is_add = 0;
1310  else
1311  return clib_error_return (0, "unknown input: '%U'",
1312  format_unformat_error, line_input);
1313  }
1314  unformat_free (line_input);
1315 
1316  rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
1317  vrf_id, addr_only, is_add);
1318 
1319  switch (rv)
1320  {
1321  case VNET_API_ERROR_INVALID_VALUE:
1322  return clib_error_return (0, "External port already in use.");
1323  break;
1324  case VNET_API_ERROR_NO_SUCH_ENTRY:
1325  if (is_add)
1326  return clib_error_return (0, "External addres must be allocated.");
1327  else
1328  return clib_error_return (0, "Mapping not exist.");
1329  break;
1330  case VNET_API_ERROR_NO_SUCH_FIB:
1331  return clib_error_return (0, "No such VRF id.");
1332  case VNET_API_ERROR_VALUE_EXIST:
1333  return clib_error_return (0, "Mapping already exist.");
1334  default:
1335  break;
1336  }
1337 
1338  return error;
1339 }
1340 
1341 /*?
1342  * @cliexpar
1343  * @cliexstart{snat add static mapping}
1344  * Static mapping allows hosts on the external network to initiate connection
1345  * to to the local network host.
1346  * To create static mapping between local host address 10.0.0.3 port 6303 and
1347  * external address 4.4.4.4 port 3606 use:
1348  * vpp# snat add static mapping local 10.0.0.3 6303 external 4.4.4.4 3606
1349  * If not runnig "static mapping only" S-NAT plugin mode use before:
1350  * vpp# snat add address 4.4.4.4
1351  * To create static mapping between local and external address use:
1352  * vpp# snat add static mapping local 10.0.0.3 external 4.4.4.4
1353  * @cliexend
1354 ?*/
1356  .path = "snat add static mapping",
1357  .function = add_static_mapping_command_fn,
1358  .short_help =
1359  "snat add static mapping local <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
1360 };
1361 
1362 static clib_error_t *
1364 {
1365  snat_main_t * sm = &snat_main;
1366  u32 translation_buckets = 1024;
1367  u32 translation_memory_size = 128<<20;
1368  u32 user_buckets = 128;
1369  u32 user_memory_size = 64<<20;
1370  u32 max_translations_per_user = 100;
1371  u32 outside_vrf_id = 0;
1372  u32 inside_vrf_id = 0;
1373  u32 static_mapping_buckets = 1024;
1374  u32 static_mapping_memory_size = 64<<20;
1375  u8 static_mapping_only = 0;
1376  u8 static_mapping_connection_tracking = 0;
1377 
1378  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1379  {
1380  if (unformat (input, "translation hash buckets %d", &translation_buckets))
1381  ;
1382  else if (unformat (input, "translation hash memory %d",
1383  &translation_memory_size));
1384  else if (unformat (input, "user hash buckets %d", &user_buckets))
1385  ;
1386  else if (unformat (input, "user hash memory %d",
1387  &user_memory_size))
1388  ;
1389  else if (unformat (input, "max translations per user %d",
1390  &max_translations_per_user))
1391  ;
1392  else if (unformat (input, "outside VRF id %d",
1393  &outside_vrf_id))
1394  ;
1395  else if (unformat (input, "inside VRF id %d",
1396  &inside_vrf_id))
1397  ;
1398  else if (unformat (input, "static mapping only"))
1399  {
1400  static_mapping_only = 1;
1401  if (unformat (input, "connection tracking"))
1402  static_mapping_connection_tracking = 1;
1403  }
1404  else
1405  return clib_error_return (0, "unknown input '%U'",
1406  format_unformat_error, input);
1407  }
1408 
1409  /* for show commands, etc. */
1410  sm->translation_buckets = translation_buckets;
1411  sm->translation_memory_size = translation_memory_size;
1412  sm->user_buckets = user_buckets;
1413  sm->user_memory_size = user_memory_size;
1414  sm->max_translations_per_user = max_translations_per_user;
1415  sm->outside_vrf_id = outside_vrf_id;
1416  sm->outside_fib_index = ~0;
1417  sm->inside_vrf_id = inside_vrf_id;
1418  sm->inside_fib_index = ~0;
1419  sm->static_mapping_only = static_mapping_only;
1420  sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
1421 
1422  if (!static_mapping_only ||
1423  (static_mapping_only && static_mapping_connection_tracking))
1424  {
1425  clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
1426  translation_memory_size);
1427 
1428  clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
1429  translation_memory_size);
1430 
1431  clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
1432  user_memory_size);
1433  }
1434  clib_bihash_init_8_8 (&sm->static_mapping_by_local,
1435  "static_mapping_by_local", static_mapping_buckets,
1436  static_mapping_memory_size);
1437 
1438  clib_bihash_init_8_8 (&sm->static_mapping_by_external,
1439  "static_mapping_by_external", static_mapping_buckets,
1440  static_mapping_memory_size);
1441  return 0;
1442 }
1443 
1445 
1446 u8 * format_snat_key (u8 * s, va_list * args)
1447 {
1448  snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
1449  char * protocol_string = "unknown";
1450  static char *protocol_strings[] = {
1451  "UDP",
1452  "TCP",
1453  "ICMP",
1454  };
1455 
1456  if (key->protocol < ARRAY_LEN(protocol_strings))
1457  protocol_string = protocol_strings[key->protocol];
1458 
1459  s = format (s, "%U proto %s port %d fib %d",
1460  format_ip4_address, &key->addr, protocol_string,
1461  clib_net_to_host_u16 (key->port), key->fib_index);
1462  return s;
1463 }
1464 
1465 u8 * format_snat_session (u8 * s, va_list * args)
1466 {
1467  snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
1468  snat_session_t * sess = va_arg (*args, snat_session_t *);
1469 
1470  s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
1471  s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
1472  s = format (s, " last heard %.2f\n", sess->last_heard);
1473  s = format (s, " total pkts %d, total bytes %lld\n",
1474  sess->total_pkts, sess->total_bytes);
1475  if (snat_is_session_static (sess))
1476  s = format (s, " static translation\n");
1477  else
1478  s = format (s, " dynamic translation\n");
1479 
1480  return s;
1481 }
1482 
1483 u8 * format_snat_user (u8 * s, va_list * args)
1484 {
1485  snat_main_t * sm = va_arg (*args, snat_main_t *);
1486  snat_user_t * u = va_arg (*args, snat_user_t *);
1487  int verbose = va_arg (*args, int);
1488  dlist_elt_t * head, * elt;
1489  u32 elt_index, head_index;
1490  u32 session_index;
1491  snat_session_t * sess;
1492 
1493  s = format (s, "%U: %d dynamic translations, %d static translations\n",
1495 
1496  if (verbose == 0)
1497  return s;
1498 
1499  if (u->nsessions || u->nstaticsessions)
1500  {
1501  head_index = u->sessions_per_user_list_head_index;
1502  head = pool_elt_at_index (sm->list_pool, head_index);
1503 
1504  elt_index = head->next;
1505  elt = pool_elt_at_index (sm->list_pool, elt_index);
1506  session_index = elt->value;
1507 
1508  while (session_index != ~0)
1509  {
1510  sess = pool_elt_at_index (sm->sessions, session_index);
1511 
1512  s = format (s, " %U\n", format_snat_session, sm, sess);
1513 
1514  elt_index = elt->next;
1515  elt = pool_elt_at_index (sm->list_pool, elt_index);
1516  session_index = elt->value;
1517  }
1518  }
1519 
1520  return s;
1521 }
1522 
1523 u8 * format_snat_static_mapping (u8 * s, va_list * args)
1524 {
1525  snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
1526 
1527  if (m->addr_only)
1528  s = format (s, "local %U external %U vrf %d",
1531  m->vrf_id);
1532  else
1533  s = format (s, "local %U:%d external %U:%d vrf %d",
1536  m->vrf_id);
1537 
1538  return s;
1539 }
1540 
1541 static clib_error_t *
1543  unformat_input_t * input,
1544  vlib_cli_command_t * cmd)
1545 {
1546  int verbose = 0;
1547  snat_main_t * sm = &snat_main;
1548  snat_user_t * u;
1551  vnet_main_t *vnm = vnet_get_main();
1552 
1553  if (unformat (input, "detail"))
1554  verbose = 1;
1555  else if (unformat (input, "verbose"))
1556  verbose = 2;
1557 
1558  if (sm->static_mapping_only)
1559  {
1561  vlib_cli_output (vm, "SNAT mode: static mapping only connection "
1562  "tracking");
1563  else
1564  vlib_cli_output (vm, "SNAT mode: static mapping only");
1565  }
1566  else
1567  {
1568  vlib_cli_output (vm, "SNAT mode: dynamic translations enabled");
1569  }
1570 
1571  if (verbose > 0)
1572  {
1573  pool_foreach (i, sm->interfaces,
1574  ({
1575  vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
1576  vnet_get_sw_interface (vnm, i->sw_if_index),
1577  i->is_inside ? "in" : "out");
1578  }));
1579  }
1580 
1582  {
1583  vlib_cli_output (vm, "%d static mappings",
1584  pool_elts (sm->static_mappings));
1585 
1586  if (verbose > 0)
1587  {
1588  pool_foreach (m, sm->static_mappings,
1589  ({
1590  vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
1591  }));
1592  }
1593  }
1594  else
1595  {
1596  vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
1597  " %d static mappings",
1598  pool_elts (sm->users),
1599  vec_len (sm->addresses),
1600  pool_elts (sm->sessions),
1601  pool_elts (sm->static_mappings));
1602 
1603  if (verbose > 0)
1604  {
1605  vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
1606  verbose - 1);
1607  vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
1608  verbose - 1);
1609  vlib_cli_output (vm, "%d list pool elements",
1610  pool_elts (sm->list_pool));
1611 
1612  pool_foreach (u, sm->users,
1613  ({
1614  vlib_cli_output (vm, "%U", format_snat_user, sm, u, verbose - 1);
1615  }));
1616 
1617  if (pool_elts (sm->static_mappings))
1618  {
1619  vlib_cli_output (vm, "static mappings:");
1620  pool_foreach (m, sm->static_mappings,
1621  ({
1622  vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
1623  }));
1624  }
1625  }
1626  }
1627 
1628  return 0;
1629 }
1630 
1632  .path = "show snat",
1633  .short_help = "show snat",
1634  .function = show_snat_command_fn,
1635 };
ip4_address_t external_addr
Definition: snat.h:121
vnet_config_main_t config_main
Definition: feature.h:55
u32 translation_memory_size
Definition: snat.h:179
u32 next
Definition: dlist.h:30
clib_bihash_8_8_t static_mapping_by_external
Definition: snat.h:146
Add/del S-NAT address range.
Definition: snat.api:31
static vlib_cli_command_t set_interface_snat_command
(constructor) VLIB_CLI_COMMAND (set_interface_snat_command)
Definition: snat.c:1270
u32 sessions_per_user_list_head_index
Definition: snat.h:108
u32 max_translations_per_user
Definition: snat.h:182
static void vl_api_snat_interface_add_del_feature_t_handler(vl_api_snat_interface_add_del_feature_t *mp)
Definition: snat.c:705
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
static void * vl_api_snat_add_static_mapping_t_print(vl_api_snat_add_static_mapping_t *mp, void *handle)
Definition: snat.c:815
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
vlib_main_t * vlib_main
Definition: snat.h:192
#define REPLY_MACRO2(t, body)
Definition: snat.c:79
a
Definition: bitmap.h:516
static void * vl_api_snat_interface_add_del_feature_t_print(vl_api_snat_interface_add_del_feature_t *mp, void *handle)
Definition: snat.c:723
u32 busy_ports
Definition: snat.h:115
void vl_msg_api_send_shmem(unix_shared_memory_queue_t *q, u8 *elem)
u32 nsessions
Definition: snat.h:109
static void vl_api_snat_add_address_range_t_handler(vl_api_snat_add_address_range_t *mp)
Definition: snat.c:592
clib_bihash_8_8_t out2in
Definition: snat.h:136
u8 static_mapping_connection_tracking
Definition: snat.h:177
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
u32 vnet_config_del_feature(vlib_main_t *vm, vnet_config_main_t *cm, u32 config_string_heap_index, u32 feature_index, void *feature_config, u32 n_feature_config_bytes)
Definition: config.c:300
ip_lookup_main_t * ip4_lookup_main
Definition: snat.h:195
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:482
u32 nstaticsessions
Definition: snat.h:110
unix_shared_memory_queue_t * vl_api_client_index_to_input_queue(u32 index)
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:521
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap. ...
Definition: bitmap.h:167
ip_lookup_main_t lookup_main
Definition: ip4.h:96
void snat_add_address(snat_main_t *sm, ip4_address_t *addr)
Definition: snat.c:201
unformat_function_t unformat_vnet_sw_interface
snat_main_t snat_main
Definition: snat.c:27
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:200
format_function_t format_ip4_address
Definition: format.h:78
u32 rx_feature_out2in_fast
Definition: snat.h:173
u32 rx_feature_in2out
Definition: snat.h:170
static clib_error_t * snat_init(vlib_main_t *vm)
Definition: snat.c:981
snat_interface_t * interfaces
Definition: snat.h:158
u32 inside_vrf_id
Definition: snat.h:185
static clib_error_t * add_address_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: snat.c:1128
static void vl_api_snat_add_static_mapping_t_handler(vl_api_snat_add_static_mapping_t *mp)
Definition: snat.c:782
ethernet_main_t * ethernet_main
Definition: plugin.h:27
static void vl_api_snat_control_ping_t_handler(vl_api_snat_control_ping_t *mp)
Definition: snat.c:887
api_main_t api_main
Definition: api.h:185
u32 random_seed
Definition: snat.h:167
static int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del)
Definition: snat.c:539
u8 * format_snat_key(u8 *s, va_list *args)
Definition: snat.c:1446
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:348
static void * vl_api_snat_control_ping_t_print(vl_api_snat_control_ping_t *mp, void *handle)
Definition: snat.c:900
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
u8 * format_snat_user(u8 *s, va_list *args)
Definition: snat.c:1483
ip4_address_t addr
Definition: snat.h:107
int snat_static_mapping_match(snat_main_t *sm, snat_session_key_t match, snat_session_key_t *mapping, u8 by_external)
Match SNAT static mapping.
Definition: snat.c:1039
#define foreach_snat_plugin_api_msg
Definition: snat.c:943
ip4_main_t * ip4_main
Definition: snat.h:194
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
snat_static_mapping_t * static_mappings
Definition: snat.h:155
#define VALIDATE_SW_IF_INDEX(mp)
Definition: snat.c:142
#define clib_warning(format, args...)
Definition: error.h:59
static clib_error_t * snat_plugin_api_hookup(vlib_main_t *vm)
Definition: snat.c:955
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:977
ip4_address_t local_addr
Definition: snat.h:120
Control ping from the client to the server response.
Definition: snat.api:196
snat_user_t * users
Definition: snat.h:149
unformat_function_t unformat_ip4_address
Definition: format.h:75
#define REPLY_MACRO(t)
Definition: snat.c:64
S-NAT static mapping details response.
Definition: snat.api:169
#define hash_get(h, key)
Definition: hash.h:248
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
VNET_IP4_UNICAST_FEATURE_INIT(ip4_snat_in2out, static)
uword * fib_index_by_table_id
Hash table mapping table id to fib index.
Definition: ip4.h:111
u32 rx_feature_in2out_fast
Definition: snat.h:172
u64 key
the key
Definition: bihash_8_8.h:35
u32 translation_buckets
Definition: snat.h:178
static void send_snat_static_mapping_details(snat_static_mapping_t *m, unix_shared_memory_queue_t *q, u32 context)
Definition: snat.c:837
static int is_snat_address_used_in_static_mapping(snat_main_t *sm, ip4_address_t addr)
Definition: snat.c:217
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:214
Enable/disable S-NAT feature on the interface reply.
Definition: snat.api:90
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:118
int snat_del_address(snat_main_t *sm, ip4_address_t addr)
Definition: snat.c:230
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:765
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:575
static void * vl_api_snat_address_dump_t_print(vl_api_snat_address_dump_t *mp, void *handle)
Definition: snat.c:694
static void increment_v4_address(ip4_address_t *a)
Definition: snat.c:295
u8 * format_snat_session(u8 *s, va_list *args)
Definition: snat.c:1465
Dump S-NAT static mappings.
Definition: snat.api:154
u64 value
the value
Definition: bihash_8_8.h:36
vnet_feature_config_main_t feature_config_mains[VNET_N_IP_FEAT]
rx unicast, multicast, tx interface/feature configuration.
Definition: lookup.h:360
void * vl_msg_api_alloc(int nbytes)
api_main_t * api_main
Definition: snat.h:197
static clib_error_t * show_snat_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: snat.c:1542
static void * vl_api_snat_static_mapping_dump_t_print(vl_api_snat_static_mapping_dump_t *mp, void *handle)
Definition: snat.c:876
u8 static_mapping_only
Definition: snat.h:176
clib_bihash_8_8_t user_hash
Definition: snat.h:140
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
snat_address_t * addresses
Definition: snat.h:161
static clib_error_t * add_static_mapping_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: snat.c:1277
Dump interfaces with S-NAT feature.
Definition: snat.api:99
u16 msg_id_base
Definition: jvpp_snat.h:28
int snat_alloc_outside_address_and_port(snat_main_t *sm, snat_session_key_t *k, u32 *address_indexp)
Definition: snat.c:1089
#define FINISH
Definition: snat.c:50
#define clib_memcpy(a, b, c)
Definition: string.h:64
clib_error_t * vlib_plugin_register(vlib_main_t *vm, vnet_plugin_handoff_t *h, int from_early_init)
Definition: snat.c:126
static void send_snat_interface_details(snat_interface_t *i, unix_shared_memory_queue_t *q, u32 context)
Definition: snat.c:738
static clib_error_t * snat_config(vlib_main_t *vm, unformat_input_t *input)
Definition: snat.c:1363
u32 inside_fib_index
Definition: snat.h:186
8 octet key, 8 octet key value pair
Definition: bihash_8_8.h:33
#define ARRAY_LEN(x)
Definition: clib.h:59
ip4_address_t addr
Definition: snat.h:37
static uword clib_bitmap_get(uword *ai, uword i)
Gets the ith bit value from a bitmap.
Definition: bitmap.h:197
u32 sw_if_index
Definition: snat.h:130
clib_bihash_8_8_t static_mapping_by_local
Definition: snat.h:143
S-NAT address details response.
Definition: snat.api:64
Add S-NAT address range reply.
Definition: snat.api:45
Add/delete S-NAT static mapping.
Definition: snat.api:127
Enable/disable S-NAT feature on the interface.
Definition: snat.api:77
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:228
#define ASSERT(truth)
Dump S-NAT addresses.
Definition: snat.api:54
unsigned int u32
Definition: types.h:88
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr, u16 l_port, u16 e_port, u32 vrf_id, int addr_only, int is_add)
Add static mapping.
Definition: snat.c:318
dlist_elt_t * list_pool
Definition: snat.h:164
IPv4 main type.
Definition: ip4.h:95
u8 * format_snat_static_mapping(u8 *s, va_list *args)
Definition: snat.c:1523
u64 as_u64
Definition: snat.h:54
static void vl_api_snat_show_config_t_handler(vl_api_snat_show_config_t *mp)
Definition: snat.c:911
clib_bihash_8_8_t in2out
Definition: snat.h:137
ip4_address_t addr
Definition: snat.h:51
u32 vnet_config_add_feature(vlib_main_t *vm, vnet_config_main_t *cm, u32 config_string_heap_index, u32 feature_index, void *feature_config, u32 n_feature_config_bytes)
Definition: config.c:239
static void clib_dlist_remove(dlist_elt_t *pool, u32 index)
Definition: dlist.h:99
#define BAD_SW_IF_INDEX_LABEL
Definition: snat.c:152
ip4_address_t addr
Definition: snat.h:114
snat_session_t * sessions
Definition: snat.h:152
static vlib_cli_command_t show_snat_command
(constructor) VLIB_CLI_COMMAND (show_snat_command)
Definition: snat.c:1631
u32 value
Definition: dlist.h:32
u64 uword
Definition: types.h:112
static void vl_api_snat_static_mapping_dump_t_handler(vl_api_snat_static_mapping_dump_t *mp)
Definition: snat.c:859
u32 user_buckets
Definition: snat.h:180
unsigned short u16
Definition: types.h:57
static void vl_api_snat_interface_dump_t_handler(vl_api_snat_interface_dump_t *mp)
Definition: snat.c:755
vnet_main_t * vnet_main
Definition: snat.h:193
uword * busy_port_bitmap
Definition: snat.h:116
u32 outside_fib_index
Definition: snat.h:184
static void * vl_api_snat_add_address_range_t_print(vl_api_snat_add_address_range_t *mp, void *handle)
Definition: snat.c:647
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
static vlib_cli_command_t add_address_command
(constructor) VLIB_CLI_COMMAND (add_address_command)
Definition: snat.c:1204
static void * vl_api_snat_show_config_t_print(vl_api_snat_show_config_t *mp, void *handle)
Definition: snat.c:933
u32 fib_index
Definition: snat.h:52
u32 user_memory_size
Definition: snat.h:181
void snat_free_outside_address_and_port(snat_main_t *sm, snat_session_key_t *k, u32 address_index)
Definition: snat.c:1010
vnet_main_t * vnet_main
Definition: plugin.h:26
static vlib_cli_command_t add_static_mapping_command
(constructor) VLIB_CLI_COMMAND (add_static_mapping_command)
Definition: snat.c:1355
ip4_address_t addr
Definition: snat.h:63
S-NAT interface details response.
Definition: snat.api:109
u32 rx_feature_out2in
Definition: snat.h:171
static void send_snat_address_details(snat_address_t *a, unix_shared_memory_queue_t *q, u32 context)
Definition: snat.c:662
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
Show S-NAT plugin startup config reply.
Definition: snat.api:227
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1060
Control ping from client to api server request.
Definition: snat.api:184
#define vec_foreach(var, vec)
Vector iterator.
static void vl_api_snat_address_dump_t_handler(vl_api_snat_address_dump_t *mp)
Definition: snat.c:679
static void plugin_custom_dump_configure(snat_main_t *sm)
Definition: snat.c:972
u16 vl_msg_api_get_msg_ids(char *name, int n)
Definition: api_shared.c:1269
vhost_vring_addr_t addr
Definition: vhost-user.h:81
#define clib_error_return(e, args...)
Definition: error.h:111
Add/delete S-NAT static mapping reply.
Definition: snat.api:145
struct _unformat_input_t unformat_input_t
ethernet_main_t * ethernet_main
Definition: snat.h:196
static void * vl_api_snat_interface_dump_t_print(vl_api_snat_interface_dump_t *mp, void *handle)
Definition: snat.c:772
Show S-NAT plugin startup config.
Definition: snat.api:208
unformat_function_t unformat_line_input
Definition: format.h:281
static clib_error_t * snat_feature_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: snat.c:1211
#define snat_is_session_static(s)
Check if SNAT session is created from static mapping.
Definition: snat.h:230
u32 outside_vrf_id
Definition: snat.h:183
struct _unix_shared_memory_queue unix_shared_memory_queue_t
static u32 clib_dlist_remove_head(dlist_elt_t *pool, u32 head_index)
Definition: dlist.h:117
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:109