2 * snat.c - simple nat plugin
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/ip4.h>
21 #include <vnet/plugin/plugin.h>
22 #include <vlibapi/api.h>
23 #include <snat/snat.h>
24 #include <snat/snat_ipfix_logging.h>
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 #include <vlibsocket/api.h>
30 snat_main_t snat_main;
32 /* define message IDs */
33 #include <snat/snat_msg_enum.h>
35 /* define message structures */
37 #include <snat/snat_all_api_h.h>
40 /* define generated endian-swappers */
42 #include <snat/snat_all_api_h.h>
45 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47 /* Get the API version number */
48 #define vl_api_version(n,v) static u32 api_version=(v);
49 #include <snat/snat_all_api_h.h>
52 /* Macro to finish up custom dump fns */
55 vl_print (handle, (char *)s); \
60 * A handy macro to set up a message reply.
61 * Assumes that the following variables are available:
62 * mp - pointer to request message
63 * rmp - pointer to reply message type
67 #define REPLY_MACRO(t) \
69 unix_shared_memory_queue_t * q = \
70 vl_api_client_index_to_input_queue (mp->client_index); \
74 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
75 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
76 rmp->context = mp->context; \
77 rmp->retval = ntohl(rv); \
79 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
82 #define REPLY_MACRO2(t, body) \
84 unix_shared_memory_queue_t * q = \
85 vl_api_client_index_to_input_queue (mp->client_index); \
89 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
90 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
91 rmp->context = mp->context; \
92 rmp->retval = ntohl(rv); \
93 do {body;} while (0); \
94 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
98 /* Hook up input features */
99 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
100 .arc_name = "ip4-unicast",
101 .node_name = "snat-in2out",
102 .runs_before = VNET_FEATURES ("snat-out2in"),
104 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
105 .arc_name = "ip4-unicast",
106 .node_name = "snat-out2in",
107 .runs_before = VNET_FEATURES ("ip4-lookup"),
109 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
110 .arc_name = "ip4-unicast",
111 .node_name = "snat-in2out-worker-handoff",
112 .runs_before = VNET_FEATURES ("snat-out2in-worker-handoff"),
114 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
115 .arc_name = "ip4-unicast",
116 .node_name = "snat-out2in-worker-handoff",
117 .runs_before = VNET_FEATURES ("ip4-lookup"),
119 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
120 .arc_name = "ip4-unicast",
121 .node_name = "snat-in2out-fast",
122 .runs_before = VNET_FEATURES ("snat-out2in-fast"),
124 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
125 .arc_name = "ip4-unicast",
126 .node_name = "snat-out2in-fast",
127 .runs_before = VNET_FEATURES ("ip4-lookup"),
132 * This routine exists to convince the vlib plugin framework that
133 * we haven't accidentally copied a random .dll into the plugin directory.
135 * Also collects global variable pointers passed from the vpp engine
139 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
142 snat_main_t * sm = &snat_main;
143 clib_error_t * error = 0;
146 sm->vnet_main = h->vnet_main;
147 sm->ethernet_main = h->ethernet_main;
152 /*$$$$$ move to an installed header file */
153 #if (1 || CLIB_DEBUG > 0) /* "trust, but verify" */
155 #define VALIDATE_SW_IF_INDEX(mp) \
156 do { u32 __sw_if_index = ntohl(mp->sw_if_index); \
157 vnet_main_t *__vnm = vnet_get_main(); \
158 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
160 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
161 goto bad_sw_if_index; \
165 #define BAD_SW_IF_INDEX_LABEL \
171 #define VALIDATE_RX_SW_IF_INDEX(mp) \
172 do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index); \
173 vnet_main_t *__vnm = vnet_get_main(); \
174 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
175 __rx_sw_if_index)) { \
176 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
177 goto bad_rx_sw_if_index; \
181 #define BAD_RX_SW_IF_INDEX_LABEL \
183 bad_rx_sw_if_index: \
187 #define VALIDATE_TX_SW_IF_INDEX(mp) \
188 do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index); \
189 vnet_main_t *__vnm = vnet_get_main(); \
190 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
191 __tx_sw_if_index)) { \
192 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
193 goto bad_tx_sw_if_index; \
197 #define BAD_TX_SW_IF_INDEX_LABEL \
199 bad_tx_sw_if_index: \
205 #define VALIDATE_SW_IF_INDEX(mp)
206 #define BAD_SW_IF_INDEX_LABEL
207 #define VALIDATE_RX_SW_IF_INDEX(mp)
208 #define BAD_RX_SW_IF_INDEX_LABEL
209 #define VALIDATE_TX_SW_IF_INDEX(mp)
210 #define BAD_TX_SW_IF_INDEX_LABEL
212 #endif /* CLIB_DEBUG > 0 */
214 void snat_add_address (snat_main_t *sm, ip4_address_t *addr)
218 /* Check if address already exists */
219 vec_foreach (ap, sm->addresses)
221 if (ap->addr.as_u32 == addr->as_u32)
225 vec_add2 (sm->addresses, ap, 1);
227 clib_bitmap_alloc (ap->busy_port_bitmap, 65535);
230 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
233 snat_static_mapping_t *m;
234 pool_foreach (m, sm->static_mappings,
236 if (m->external_addr.as_u32 == addr.as_u32)
243 static void increment_v4_address (ip4_address_t * a)
247 v = clib_net_to_host_u32(a->as_u32) + 1;
248 a->as_u32 = clib_host_to_net_u32(v);
252 snat_add_static_mapping_when_resolved (snat_main_t * sm,
253 ip4_address_t l_addr,
261 snat_static_map_resolve_t *rp;
263 vec_add2 (sm->to_resolve, rp, 1);
264 rp->l_addr.as_u32 = l_addr.as_u32;
266 rp->sw_if_index = sw_if_index;
269 rp->addr_only = addr_only;
274 * @brief Add static mapping.
276 * Create static mapping between local addr+port and external addr+port.
278 * @param l_addr Local IPv4 address.
279 * @param e_addr External IPv4 address.
280 * @param l_port Local port number.
281 * @param e_port External port number.
282 * @param vrf_id VRF ID.
283 * @param addr_only If 0 address port and pair mapping, otherwise address only.
284 * @param sw_if_index External port instead of specific IP address.
285 * @param is_add If 0 delete static mapping, otherwise add.
289 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
290 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
291 u32 sw_if_index, int is_add)
293 snat_main_t * sm = &snat_main;
294 snat_static_mapping_t *m;
295 snat_static_mapping_key_t m_key;
296 clib_bihash_kv_8_8_t kv, value;
297 snat_address_t *a = 0;
302 /* If outside FIB index is not resolved yet */
303 if (sm->outside_fib_index == ~0)
305 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->outside_vrf_id);
307 return VNET_API_ERROR_NO_SUCH_FIB;
308 sm->outside_fib_index = p[0];
311 /* If the external address is a specific interface address */
312 if (sw_if_index != ~0)
314 ip4_address_t * first_int_addr;
316 /* Might be already set... */
317 first_int_addr = ip4_interface_first_address
318 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
320 /* DHCP resolution required? */
321 if (first_int_addr == 0)
323 snat_add_static_mapping_when_resolved
324 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id,
329 e_addr.as_u32 = first_int_addr->as_u32;
333 m_key.port = addr_only ? 0 : e_port;
334 m_key.fib_index = sm->outside_fib_index;
335 kv.key = m_key.as_u64;
336 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
339 m = pool_elt_at_index (sm->static_mappings, value.value);
344 return VNET_API_ERROR_VALUE_EXIST;
346 /* Convert VRF id to FIB index */
349 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
351 return VNET_API_ERROR_NO_SUCH_FIB;
354 /* If not specified use inside VRF id from SNAT plugin startup config */
357 if (sm->inside_fib_index == ~0)
359 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->inside_vrf_id);
361 return VNET_API_ERROR_NO_SUCH_FIB;
363 sm->inside_fib_index = fib_index;
366 fib_index = sm->inside_fib_index;
368 vrf_id = sm->inside_vrf_id;
371 /* Find external address in allocated addresses and reserve port for
372 address and port pair mapping when dynamic translations enabled */
373 if (!addr_only && !(sm->static_mapping_only))
375 for (i = 0; i < vec_len (sm->addresses); i++)
377 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
379 a = sm->addresses + i;
380 /* External port must be unused */
381 if (clib_bitmap_get_no_check (a->busy_port_bitmap, e_port))
382 return VNET_API_ERROR_INVALID_VALUE;
383 clib_bitmap_set_no_check (a->busy_port_bitmap, e_port, 1);
390 /* External address must be allocated */
392 return VNET_API_ERROR_NO_SUCH_ENTRY;
395 pool_get (sm->static_mappings, m);
396 memset (m, 0, sizeof (*m));
397 m->local_addr = l_addr;
398 m->external_addr = e_addr;
399 m->addr_only = addr_only;
401 m->fib_index = fib_index;
404 m->local_port = l_port;
405 m->external_port = e_port;
408 m_key.addr = m->local_addr;
409 m_key.port = m->local_port;
410 m_key.fib_index = m->fib_index;
411 kv.key = m_key.as_u64;
412 kv.value = m - sm->static_mappings;
413 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
415 m_key.addr = m->external_addr;
416 m_key.port = m->external_port;
417 m_key.fib_index = sm->outside_fib_index;
418 kv.key = m_key.as_u64;
419 kv.value = m - sm->static_mappings;
420 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
425 snat_user_key_t w_key0;
426 snat_static_mapping_key_t w_key1;
428 w_key0.addr = m->local_addr;
429 w_key0.fib_index = m->fib_index;
430 kv.key = w_key0.as_u64;
432 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
434 kv.value = sm->first_worker_index +
435 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
437 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv, 1);
441 kv.value = value.value;
444 w_key1.addr = m->external_addr;
445 w_key1.port = clib_host_to_net_u16 (m->external_port);
446 w_key1.fib_index = sm->outside_fib_index;
447 kv.key = w_key1.as_u64;
448 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv, 1);
454 return VNET_API_ERROR_NO_SUCH_ENTRY;
456 /* Free external address port */
457 if (!addr_only && !(sm->static_mapping_only))
459 for (i = 0; i < vec_len (sm->addresses); i++)
461 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
463 a = sm->addresses + i;
464 clib_bitmap_set_no_check (a->busy_port_bitmap, e_port, 0);
472 m_key.addr = m->local_addr;
473 m_key.port = m->local_port;
474 m_key.fib_index = m->fib_index;
475 kv.key = m_key.as_u64;
476 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
478 m_key.addr = m->external_addr;
479 m_key.port = m->external_port;
480 m_key.fib_index = sm->outside_fib_index;
481 kv.key = m_key.as_u64;
482 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
484 /* Delete session(s) for static mapping if exist */
485 if (!(sm->static_mapping_only) ||
486 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
488 snat_user_key_t u_key;
490 dlist_elt_t * head, * elt;
491 u32 elt_index, head_index, del_elt_index;
495 snat_main_per_thread_data_t *tsm;
497 u_key.addr = m->local_addr;
498 u_key.fib_index = m->fib_index;
499 kv.key = u_key.as_u64;
500 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
502 user_index = value.value;
503 if (!clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
504 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
506 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
507 u = pool_elt_at_index (tsm->users, user_index);
508 if (u->nstaticsessions)
510 head_index = u->sessions_per_user_list_head_index;
511 head = pool_elt_at_index (tsm->list_pool, head_index);
512 elt_index = head->next;
513 elt = pool_elt_at_index (tsm->list_pool, elt_index);
514 ses_index = elt->value;
515 while (ses_index != ~0)
517 s = pool_elt_at_index (tsm->sessions, ses_index);
518 del_elt_index = elt_index;
519 elt_index = elt->next;
520 elt = pool_elt_at_index (tsm->list_pool, elt_index);
521 ses_index = elt->value;
525 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
526 (clib_net_to_host_u16 (s->out2in.port) != e_port))
531 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
532 s->out2in.addr.as_u32,
536 s->in2out.fib_index);
538 value.key = s->in2out.as_u64;
539 clib_bihash_add_del_8_8 (&sm->in2out, &value, 0);
540 value.key = s->out2in.as_u64;
541 clib_bihash_add_del_8_8 (&sm->out2in, &value, 0);
542 pool_put (tsm->sessions, s);
544 clib_dlist_remove (tsm->list_pool, del_elt_index);
545 pool_put_index (tsm->list_pool, del_elt_index);
546 u->nstaticsessions--;
553 pool_put (tsm->users, u);
554 clib_bihash_add_del_8_8 (&sm->user_hash, &kv, 0);
560 /* Delete static mapping from pool */
561 pool_put (sm->static_mappings, m);
567 int snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm)
569 snat_address_t *a = 0;
571 u32 *ses_to_be_removed = 0, *ses_index;
572 clib_bihash_kv_8_8_t kv, value;
573 snat_user_key_t user_key;
575 snat_main_per_thread_data_t *tsm;
576 snat_static_mapping_t *m;
579 /* Find SNAT address */
580 for (i=0; i < vec_len (sm->addresses); i++)
582 if (sm->addresses[i].addr.as_u32 == addr.as_u32)
584 a = sm->addresses + i;
589 return VNET_API_ERROR_NO_SUCH_ENTRY;
593 pool_foreach (m, sm->static_mappings,
595 if (m->external_addr.as_u32 == addr.as_u32)
596 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
597 m->local_port, m->external_port,
598 m->vrf_id, m->addr_only, ~0, 0);
603 /* Check if address is used in some static mapping */
604 if (is_snat_address_used_in_static_mapping(sm, addr))
606 clib_warning ("address used in static mapping");
607 return VNET_API_ERROR_UNSPECIFIED;
611 /* Delete sessions using address */
614 vec_foreach (tsm, sm->per_thread_data)
616 pool_foreach (ses, tsm->sessions, ({
617 if (ses->out2in.addr.as_u32 == addr.as_u32)
620 snat_ipfix_logging_nat44_ses_delete(ses->in2out.addr.as_u32,
621 ses->out2in.addr.as_u32,
622 ses->in2out.protocol,
625 ses->in2out.fib_index);
626 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
627 kv.key = ses->in2out.as_u64;
628 clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
629 kv.key = ses->out2in.as_u64;
630 clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
631 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
632 user_key.addr = ses->in2out.addr;
633 user_key.fib_index = ses->in2out.fib_index;
634 kv.key = user_key.as_u64;
635 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
637 u = pool_elt_at_index (tsm->users, value.value);
643 vec_foreach (ses_index, ses_to_be_removed)
644 pool_put_index (tsm->sessions, ses_index[0]);
646 vec_free (ses_to_be_removed);
650 vec_del1 (sm->addresses, i);
655 static int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
657 snat_main_t *sm = &snat_main;
659 const char * feature_name;
661 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
662 feature_name = is_inside ? "snat-in2out-fast" : "snat-out2in-fast";
665 if (sm->num_workers > 1)
666 feature_name = is_inside ? "snat-in2out-worker-handoff" : "snat-out2in-worker-handoff";
668 feature_name = is_inside ? "snat-in2out" : "snat-out2in";
671 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
674 if (sm->fq_in2out_index == ~0)
675 sm->fq_in2out_index = vlib_frame_queue_main_init (snat_in2out_node.index, 0);
677 if (sm->fq_out2in_index == ~0)
678 sm->fq_out2in_index = vlib_frame_queue_main_init (snat_out2in_node.index, 0);
680 pool_foreach (i, sm->interfaces,
682 if (i->sw_if_index == sw_if_index)
685 pool_put (sm->interfaces, i);
687 return VNET_API_ERROR_VALUE_EXIST;
694 return VNET_API_ERROR_NO_SUCH_ENTRY;
696 pool_get (sm->interfaces, i);
697 i->sw_if_index = sw_if_index;
698 i->is_inside = is_inside;
703 static int snat_set_workers (uword * bitmap)
705 snat_main_t *sm = &snat_main;
708 if (sm->num_workers < 2)
709 return VNET_API_ERROR_FEATURE_DISABLED;
711 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
712 return VNET_API_ERROR_INVALID_WORKER;
714 vec_free (sm->workers);
715 clib_bitmap_foreach (i, bitmap,
717 vec_add1(sm->workers, i);
724 vl_api_snat_add_address_range_t_handler
725 (vl_api_snat_add_address_range_t * mp)
727 snat_main_t * sm = &snat_main;
728 vl_api_snat_add_address_range_reply_t * rmp;
729 ip4_address_t this_addr;
730 u32 start_host_order, end_host_order;
737 rv = VNET_API_ERROR_UNIMPLEMENTED;
741 if (sm->static_mapping_only)
743 rv = VNET_API_ERROR_FEATURE_DISABLED;
747 tmp = (u32 *) mp->first_ip_address;
748 start_host_order = clib_host_to_net_u32 (tmp[0]);
749 tmp = (u32 *) mp->last_ip_address;
750 end_host_order = clib_host_to_net_u32 (tmp[0]);
752 count = (end_host_order - start_host_order) + 1;
755 clib_warning ("%U - %U, %d addresses...",
756 format_ip4_address, mp->first_ip_address,
757 format_ip4_address, mp->last_ip_address,
760 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
762 for (i = 0; i < count; i++)
765 snat_add_address (sm, &this_addr);
767 rv = snat_del_address (sm, this_addr, 0);
772 increment_v4_address (&this_addr);
776 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
779 static void *vl_api_snat_add_address_range_t_print
780 (vl_api_snat_add_address_range_t *mp, void * handle)
784 s = format (0, "SCRIPT: snat_add_address_range ");
785 s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
786 if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
788 s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
794 send_snat_address_details
795 (snat_address_t * a, unix_shared_memory_queue_t * q, u32 context)
797 vl_api_snat_address_details_t *rmp;
798 snat_main_t * sm = &snat_main;
800 rmp = vl_msg_api_alloc (sizeof (*rmp));
801 memset (rmp, 0, sizeof (*rmp));
802 rmp->_vl_msg_id = ntohs (VL_API_SNAT_ADDRESS_DETAILS+sm->msg_id_base);
804 clib_memcpy (rmp->ip_address, &(a->addr), 4);
805 rmp->context = context;
807 vl_msg_api_send_shmem (q, (u8 *) & rmp);
811 vl_api_snat_address_dump_t_handler
812 (vl_api_snat_address_dump_t * mp)
814 unix_shared_memory_queue_t *q;
815 snat_main_t * sm = &snat_main;
818 q = vl_api_client_index_to_input_queue (mp->client_index);
822 vec_foreach (a, sm->addresses)
823 send_snat_address_details (a, q, mp->context);
826 static void *vl_api_snat_address_dump_t_print
827 (vl_api_snat_address_dump_t *mp, void * handle)
831 s = format (0, "SCRIPT: snat_address_dump ");
837 vl_api_snat_interface_add_del_feature_t_handler
838 (vl_api_snat_interface_add_del_feature_t * mp)
840 snat_main_t * sm = &snat_main;
841 vl_api_snat_interface_add_del_feature_reply_t * rmp;
842 u8 is_del = mp->is_add == 0;
843 u32 sw_if_index = ntohl(mp->sw_if_index);
846 VALIDATE_SW_IF_INDEX(mp);
848 rv = snat_interface_add_del (sw_if_index, mp->is_inside, is_del);
850 BAD_SW_IF_INDEX_LABEL;
852 REPLY_MACRO(VL_API_SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY);
855 static void *vl_api_snat_interface_add_del_feature_t_print
856 (vl_api_snat_interface_add_del_feature_t * mp, void *handle)
860 s = format (0, "SCRIPT: snat_interface_add_del_feature ");
861 s = format (s, "sw_if_index %d %s %s",
862 clib_host_to_net_u32(mp->sw_if_index),
863 mp->is_inside ? "in":"out",
864 mp->is_add ? "" : "del");
870 send_snat_interface_details
871 (snat_interface_t * i, unix_shared_memory_queue_t * q, u32 context)
873 vl_api_snat_interface_details_t *rmp;
874 snat_main_t * sm = &snat_main;
876 rmp = vl_msg_api_alloc (sizeof (*rmp));
877 memset (rmp, 0, sizeof (*rmp));
878 rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_DETAILS+sm->msg_id_base);
879 rmp->sw_if_index = ntohl (i->sw_if_index);
880 rmp->is_inside = i->is_inside;
881 rmp->context = context;
883 vl_msg_api_send_shmem (q, (u8 *) & rmp);
887 vl_api_snat_interface_dump_t_handler
888 (vl_api_snat_interface_dump_t * mp)
890 unix_shared_memory_queue_t *q;
891 snat_main_t * sm = &snat_main;
892 snat_interface_t * i;
894 q = vl_api_client_index_to_input_queue (mp->client_index);
898 pool_foreach (i, sm->interfaces,
900 send_snat_interface_details(i, q, mp->context);
904 static void *vl_api_snat_interface_dump_t_print
905 (vl_api_snat_interface_dump_t *mp, void * handle)
909 s = format (0, "SCRIPT: snat_interface_dump ");
914 vl_api_snat_add_static_mapping_t_handler
915 (vl_api_snat_add_static_mapping_t * mp)
917 snat_main_t * sm = &snat_main;
918 vl_api_snat_add_static_mapping_reply_t * rmp;
919 ip4_address_t local_addr, external_addr;
920 u16 local_port = 0, external_port = 0;
921 u32 vrf_id, external_sw_if_index;
926 rv = VNET_API_ERROR_UNIMPLEMENTED;
930 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
931 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
932 if (mp->addr_only == 0)
934 local_port = clib_net_to_host_u16 (mp->local_port);
935 external_port = clib_net_to_host_u16 (mp->external_port);
937 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
938 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
940 rv = snat_add_static_mapping(local_addr, external_addr, local_port,
941 external_port, vrf_id, mp->addr_only,
942 external_sw_if_index,
946 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
949 static void *vl_api_snat_add_static_mapping_t_print
950 (vl_api_snat_add_static_mapping_t *mp, void * handle)
954 s = format (0, "SCRIPT: snat_add_static_mapping ");
955 s = format (s, "local_addr %U external_addr %U ",
956 format_ip4_address, mp->local_ip_address,
957 format_ip4_address, mp->external_ip_address);
959 if (mp->addr_only == 0)
960 s = format (s, "local_port %d external_port %d ",
961 clib_net_to_host_u16 (mp->local_port),
962 clib_net_to_host_u16 (mp->external_port));
964 if (mp->vrf_id != ~0)
965 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
967 if (mp->external_sw_if_index != ~0)
968 s = format (s, "external_sw_if_index %d",
969 clib_net_to_host_u32 (mp->external_sw_if_index));
974 send_snat_static_mapping_details
975 (snat_static_mapping_t * m, unix_shared_memory_queue_t * q, u32 context)
977 vl_api_snat_static_mapping_details_t *rmp;
978 snat_main_t * sm = &snat_main;
980 rmp = vl_msg_api_alloc (sizeof (*rmp));
981 memset (rmp, 0, sizeof (*rmp));
982 rmp->_vl_msg_id = ntohs (VL_API_SNAT_STATIC_MAPPING_DETAILS+sm->msg_id_base);
984 rmp->addr_only = m->addr_only;
985 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
986 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
987 rmp->local_port = htons (m->local_port);
988 rmp->external_port = htons (m->external_port);
989 rmp->vrf_id = htonl (m->vrf_id);
990 rmp->context = context;
992 vl_msg_api_send_shmem (q, (u8 *) & rmp);
996 vl_api_snat_static_mapping_dump_t_handler
997 (vl_api_snat_static_mapping_dump_t * mp)
999 unix_shared_memory_queue_t *q;
1000 snat_main_t * sm = &snat_main;
1001 snat_static_mapping_t * m;
1003 q = vl_api_client_index_to_input_queue (mp->client_index);
1007 pool_foreach (m, sm->static_mappings,
1009 send_snat_static_mapping_details (m, q, mp->context);
1013 static void *vl_api_snat_static_mapping_dump_t_print
1014 (vl_api_snat_static_mapping_dump_t *mp, void * handle)
1018 s = format (0, "SCRIPT: snat_static_mapping_dump ");
1024 vl_api_snat_control_ping_t_handler
1025 (vl_api_snat_control_ping_t * mp)
1027 vl_api_snat_control_ping_reply_t *rmp;
1028 snat_main_t * sm = &snat_main;
1031 REPLY_MACRO2(VL_API_SNAT_CONTROL_PING_REPLY,
1033 rmp->vpe_pid = ntohl (getpid());
1037 static void *vl_api_snat_control_ping_t_print
1038 (vl_api_snat_control_ping_t *mp, void * handle)
1042 s = format (0, "SCRIPT: snat_control_ping ");
1048 vl_api_snat_show_config_t_handler
1049 (vl_api_snat_show_config_t * mp)
1051 vl_api_snat_show_config_reply_t *rmp;
1052 snat_main_t * sm = &snat_main;
1055 REPLY_MACRO2(VL_API_SNAT_SHOW_CONFIG_REPLY,
1057 rmp->translation_buckets = htonl (sm->translation_buckets);
1058 rmp->translation_memory_size = htonl (sm->translation_memory_size);
1059 rmp->user_buckets = htonl (sm->user_buckets);
1060 rmp->user_memory_size = htonl (sm->user_memory_size);
1061 rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
1062 rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
1063 rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
1064 rmp->static_mapping_only = sm->static_mapping_only;
1065 rmp->static_mapping_connection_tracking =
1066 sm->static_mapping_connection_tracking;
1070 static void *vl_api_snat_show_config_t_print
1071 (vl_api_snat_show_config_t *mp, void * handle)
1075 s = format (0, "SCRIPT: snat_show_config ");
1081 vl_api_snat_set_workers_t_handler
1082 (vl_api_snat_set_workers_t * mp)
1084 snat_main_t * sm = &snat_main;
1085 vl_api_snat_set_workers_reply_t * rmp;
1088 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
1090 if (sm->num_workers < 2)
1092 rv = VNET_API_ERROR_FEATURE_DISABLED;
1096 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
1097 rv = snat_set_workers(bitmap);
1098 clib_bitmap_free (bitmap);
1101 REPLY_MACRO (VL_API_SNAT_SET_WORKERS_REPLY);
1104 static void *vl_api_snat_set_workers_t_print
1105 (vl_api_snat_set_workers_t *mp, void * handle)
1111 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
1113 s = format (0, "SCRIPT: snat_set_workers ");
1114 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
1115 clib_bitmap_foreach (i, bitmap,
1118 s = format (s, "%d", i);
1120 s = format (s, ",%d", i);
1123 clib_bitmap_free (bitmap);
1128 send_snat_worker_details
1129 (u32 worker_index, unix_shared_memory_queue_t * q, u32 context)
1131 vl_api_snat_worker_details_t *rmp;
1132 snat_main_t * sm = &snat_main;
1133 vlib_worker_thread_t *w =
1134 vlib_worker_threads + worker_index + sm->first_worker_index;
1136 rmp = vl_msg_api_alloc (sizeof (*rmp));
1137 memset (rmp, 0, sizeof (*rmp));
1138 rmp->_vl_msg_id = ntohs (VL_API_SNAT_WORKER_DETAILS+sm->msg_id_base);
1139 rmp->context = context;
1140 rmp->worker_index = htonl (worker_index);
1141 rmp->lcore_id = htonl (w->lcore_id);
1142 strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
1144 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1148 vl_api_snat_worker_dump_t_handler
1149 (vl_api_snat_worker_dump_t * mp)
1151 unix_shared_memory_queue_t *q;
1152 snat_main_t * sm = &snat_main;
1155 q = vl_api_client_index_to_input_queue (mp->client_index);
1159 vec_foreach (worker_index, sm->workers)
1161 send_snat_worker_details(*worker_index, q, mp->context);
1165 static void *vl_api_snat_worker_dump_t_print
1166 (vl_api_snat_worker_dump_t *mp, void * handle)
1170 s = format (0, "SCRIPT: snat_worker_dump ");
1175 static int snat_add_interface_address(snat_main_t *sm,
1180 vl_api_snat_add_del_interface_addr_t_handler
1181 (vl_api_snat_add_del_interface_addr_t * mp)
1183 snat_main_t * sm = &snat_main;
1184 vl_api_snat_add_del_interface_addr_reply_t * rmp;
1185 u8 is_del = mp->is_add == 0;
1186 u32 sw_if_index = ntohl(mp->sw_if_index);
1189 VALIDATE_SW_IF_INDEX(mp);
1191 rv = snat_add_interface_address (sm, sw_if_index, is_del);
1193 BAD_SW_IF_INDEX_LABEL;
1195 REPLY_MACRO(VL_API_SNAT_ADD_DEL_INTERFACE_ADDR_REPLY);
1198 static void *vl_api_snat_add_del_interface_addr_t_print
1199 (vl_api_snat_add_del_interface_addr_t * mp, void *handle)
1203 s = format (0, "SCRIPT: snat_add_del_interface_addr ");
1204 s = format (s, "sw_if_index %d %s",
1205 clib_host_to_net_u32(mp->sw_if_index),
1206 mp->is_add ? "" : "del");
1212 send_snat_interface_addr_details
1213 (u32 sw_if_index, unix_shared_memory_queue_t * q, u32 context)
1215 vl_api_snat_interface_addr_details_t *rmp;
1216 snat_main_t * sm = &snat_main;
1218 rmp = vl_msg_api_alloc (sizeof (*rmp));
1219 memset (rmp, 0, sizeof (*rmp));
1220 rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_ADDR_DETAILS+sm->msg_id_base);
1221 rmp->sw_if_index = ntohl (sw_if_index);
1222 rmp->context = context;
1224 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1228 vl_api_snat_interface_addr_dump_t_handler
1229 (vl_api_snat_interface_addr_dump_t * mp)
1231 unix_shared_memory_queue_t *q;
1232 snat_main_t * sm = &snat_main;
1235 q = vl_api_client_index_to_input_queue (mp->client_index);
1239 vec_foreach (i, sm->auto_add_sw_if_indices)
1240 send_snat_interface_addr_details(*i, q, mp->context);
1243 static void *vl_api_snat_interface_addr_dump_t_print
1244 (vl_api_snat_interface_addr_dump_t *mp, void * handle)
1248 s = format (0, "SCRIPT: snat_interface_addr_dump ");
1254 vl_api_snat_ipfix_enable_disable_t_handler
1255 (vl_api_snat_ipfix_enable_disable_t * mp)
1257 snat_main_t * sm = &snat_main;
1258 vl_api_snat_ipfix_enable_disable_reply_t * rmp;
1261 rv = snat_ipfix_logging_enable_disable(mp->enable,
1262 clib_host_to_net_u32 (mp->domain_id),
1263 clib_host_to_net_u16 (mp->src_port));
1265 REPLY_MACRO (VL_API_SNAT_IPFIX_ENABLE_DISABLE_REPLY);
1268 static void *vl_api_snat_ipfix_enable_disable_t_print
1269 (vl_api_snat_ipfix_enable_disable_t *mp, void * handle)
1273 s = format (0, "SCRIPT: snat_ipfix_enable_disable ");
1275 s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
1277 s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
1279 s = format (s, "disable ");
1284 /* List of message types that this plugin understands */
1285 #define foreach_snat_plugin_api_msg \
1286 _(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range) \
1287 _(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature) \
1288 _(SNAT_ADD_STATIC_MAPPING, snat_add_static_mapping) \
1289 _(SNAT_CONTROL_PING, snat_control_ping) \
1290 _(SNAT_STATIC_MAPPING_DUMP, snat_static_mapping_dump) \
1291 _(SNAT_SHOW_CONFIG, snat_show_config) \
1292 _(SNAT_ADDRESS_DUMP, snat_address_dump) \
1293 _(SNAT_INTERFACE_DUMP, snat_interface_dump) \
1294 _(SNAT_SET_WORKERS, snat_set_workers) \
1295 _(SNAT_WORKER_DUMP, snat_worker_dump) \
1296 _(SNAT_ADD_DEL_INTERFACE_ADDR, snat_add_del_interface_addr) \
1297 _(SNAT_INTERFACE_ADDR_DUMP, snat_interface_addr_dump) \
1298 _(SNAT_IPFIX_ENABLE_DISABLE, snat_ipfix_enable_disable)
1300 /* Set up the API message handling tables */
1301 static clib_error_t *
1302 snat_plugin_api_hookup (vlib_main_t *vm)
1304 snat_main_t * sm __attribute__ ((unused)) = &snat_main;
1306 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
1308 vl_api_##n##_t_handler, \
1310 vl_api_##n##_t_endian, \
1311 vl_api_##n##_t_print, \
1312 sizeof(vl_api_##n##_t), 1);
1313 foreach_snat_plugin_api_msg;
1319 #define vl_msg_name_crc_list
1320 #include <snat/snat_all_api_h.h>
1321 #undef vl_msg_name_crc_list
1324 setup_message_id_table (snat_main_t * sm, api_main_t * am)
1326 #define _(id,n,crc) \
1327 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
1328 foreach_vl_msg_name_crc_snat;
1332 static void plugin_custom_dump_configure (snat_main_t * sm)
1334 #define _(n,f) sm->api_main->msg_print_handlers \
1335 [VL_API_##n + sm->msg_id_base] \
1336 = (void *) vl_api_##f##_t_print;
1337 foreach_snat_plugin_api_msg;
1343 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1346 ip4_address_t * address,
1348 u32 if_address_index,
1351 static clib_error_t * snat_init (vlib_main_t * vm)
1353 snat_main_t * sm = &snat_main;
1354 clib_error_t * error = 0;
1355 ip4_main_t * im = &ip4_main;
1356 ip_lookup_main_t * lm = &im->lookup_main;
1359 vlib_thread_registration_t *tr;
1360 vlib_thread_main_t *tm = vlib_get_thread_main ();
1363 ip4_add_del_interface_address_callback_t cb4;
1365 name = format (0, "snat_%08x%c", api_version, 0);
1367 /* Ask for a correctly-sized block of API message decode slots */
1368 sm->msg_id_base = vl_msg_api_get_msg_ids
1369 ((char *) name, VL_MSG_FIRST_AVAILABLE);
1372 sm->vnet_main = vnet_get_main();
1374 sm->ip4_lookup_main = lm;
1375 sm->api_main = &api_main;
1376 sm->first_worker_index = 0;
1377 sm->next_worker = 0;
1378 sm->num_workers = 0;
1380 sm->fq_in2out_index = ~0;
1381 sm->fq_out2in_index = ~0;
1383 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1386 tr = (vlib_thread_registration_t *) p[0];
1389 sm->num_workers = tr->count;
1390 sm->first_worker_index = tr->first_index;
1394 /* Use all available workers by default */
1395 if (sm->num_workers > 1)
1397 for (i=0; i < sm->num_workers; i++)
1398 bitmap = clib_bitmap_set (bitmap, i, 1);
1399 snat_set_workers(bitmap);
1400 clib_bitmap_free (bitmap);
1403 error = snat_plugin_api_hookup (vm);
1405 /* Add our API messages to the global name_crc hash table */
1406 setup_message_id_table (sm, &api_main);
1408 plugin_custom_dump_configure (sm);
1411 /* Set up the interface address add/del callback */
1412 cb4.function = snat_ip4_add_del_interface_address_cb;
1413 cb4.function_opaque = 0;
1415 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1417 /* Init IPFIX logging */
1418 snat_ipfix_logging_init(vm);
1423 VLIB_INIT_FUNCTION (snat_init);
1425 void snat_free_outside_address_and_port (snat_main_t * sm,
1426 snat_session_key_t * k,
1430 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1432 ASSERT (address_index < vec_len (sm->addresses));
1434 a = sm->addresses + address_index;
1436 ASSERT (clib_bitmap_get_no_check (a->busy_port_bitmap,
1437 port_host_byte_order) == 1);
1439 clib_bitmap_set_no_check (a->busy_port_bitmap, port_host_byte_order, 0);
1444 * @brief Match SNAT static mapping.
1446 * @param sm SNAT main.
1447 * @param match Address and port to match.
1448 * @param mapping External or local address and port of the matched mapping.
1449 * @param by_external If 0 match by local address otherwise match by external
1452 * @returns 0 if match found otherwise 1.
1454 int snat_static_mapping_match (snat_main_t * sm,
1455 snat_session_key_t match,
1456 snat_session_key_t * mapping,
1459 clib_bihash_kv_8_8_t kv, value;
1460 snat_static_mapping_t *m;
1461 snat_static_mapping_key_t m_key;
1462 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1465 mapping_hash = &sm->static_mapping_by_external;
1467 m_key.addr = match.addr;
1468 m_key.port = clib_net_to_host_u16 (match.port);
1469 m_key.fib_index = match.fib_index;
1471 kv.key = m_key.as_u64;
1473 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1475 /* Try address only mapping */
1477 kv.key = m_key.as_u64;
1478 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1482 m = pool_elt_at_index (sm->static_mappings, value.value);
1486 mapping->addr = m->local_addr;
1487 /* Address only mapping doesn't change port */
1488 mapping->port = m->addr_only ? match.port
1489 : clib_host_to_net_u16 (m->local_port);
1490 mapping->fib_index = m->fib_index;
1494 mapping->addr = m->external_addr;
1495 /* Address only mapping doesn't change port */
1496 mapping->port = m->addr_only ? match.port
1497 : clib_host_to_net_u16 (m->external_port);
1498 mapping->fib_index = sm->outside_fib_index;
1504 int snat_alloc_outside_address_and_port (snat_main_t * sm,
1505 snat_session_key_t * k,
1506 u32 * address_indexp)
1512 for (i = 0; i < vec_len (sm->addresses); i++)
1514 if (sm->addresses[i].busy_ports < (65535-1024))
1516 a = sm->addresses + i;
1520 portnum = random_u32 (&sm->random_seed);
1524 if (clib_bitmap_get_no_check (a->busy_port_bitmap, portnum))
1526 clib_bitmap_set_no_check (a->busy_port_bitmap, portnum, 1);
1528 /* Caller sets protocol and fib index */
1530 k->port = clib_host_to_net_u16(portnum);
1531 *address_indexp = i;
1536 /* Totally out of translations to use... */
1537 snat_ipfix_logging_addresses_exhausted(0);
1542 static clib_error_t *
1543 add_address_command_fn (vlib_main_t * vm,
1544 unformat_input_t * input,
1545 vlib_cli_command_t * cmd)
1547 unformat_input_t _line_input, *line_input = &_line_input;
1548 snat_main_t * sm = &snat_main;
1549 ip4_address_t start_addr, end_addr, this_addr;
1550 u32 start_host_order, end_host_order;
1555 /* Get a line of input. */
1556 if (!unformat_user (input, unformat_line_input, line_input))
1559 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1561 if (unformat (line_input, "%U - %U",
1562 unformat_ip4_address, &start_addr,
1563 unformat_ip4_address, &end_addr))
1565 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
1566 end_addr = start_addr;
1567 else if (unformat (line_input, "del"))
1570 return clib_error_return (0, "unknown input '%U'",
1571 format_unformat_error, input);
1573 unformat_free (line_input);
1575 if (sm->static_mapping_only)
1576 return clib_error_return (0, "static mapping only mode");
1578 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
1579 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
1581 if (end_host_order < start_host_order)
1582 return clib_error_return (0, "end address less than start address");
1584 count = (end_host_order - start_host_order) + 1;
1587 clib_warning ("%U - %U, %d addresses...",
1588 format_ip4_address, &start_addr,
1589 format_ip4_address, &end_addr,
1592 this_addr = start_addr;
1594 for (i = 0; i < count; i++)
1597 snat_add_address (sm, &this_addr);
1599 rv = snat_del_address (sm, this_addr, 0);
1603 case VNET_API_ERROR_NO_SUCH_ENTRY:
1604 return clib_error_return (0, "S-NAT address not exist.");
1606 case VNET_API_ERROR_UNSPECIFIED:
1607 return clib_error_return (0, "S-NAT address used in static mapping.");
1613 increment_v4_address (&this_addr);
1619 VLIB_CLI_COMMAND (add_address_command, static) = {
1620 .path = "snat add address",
1621 .short_help = "snat add addresses <ip4-range-start> [- <ip4-range-end>] [del]",
1622 .function = add_address_command_fn,
1625 static clib_error_t *
1626 snat_feature_command_fn (vlib_main_t * vm,
1627 unformat_input_t * input,
1628 vlib_cli_command_t * cmd)
1630 unformat_input_t _line_input, *line_input = &_line_input;
1631 vnet_main_t * vnm = vnet_get_main();
1632 clib_error_t * error = 0;
1634 u32 * inside_sw_if_indices = 0;
1635 u32 * outside_sw_if_indices = 0;
1641 /* Get a line of input. */
1642 if (!unformat_user (input, unformat_line_input, line_input))
1645 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1647 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
1649 vec_add1 (inside_sw_if_indices, sw_if_index);
1650 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
1652 vec_add1 (outside_sw_if_indices, sw_if_index);
1653 else if (unformat (line_input, "del"))
1656 return clib_error_return (0, "unknown input '%U'",
1657 format_unformat_error, input);
1659 unformat_free (line_input);
1661 if (vec_len (inside_sw_if_indices))
1663 for (i = 0; i < vec_len(inside_sw_if_indices); i++)
1665 sw_if_index = inside_sw_if_indices[i];
1666 snat_interface_add_del (sw_if_index, 1, is_del);
1670 if (vec_len (outside_sw_if_indices))
1672 for (i = 0; i < vec_len(outside_sw_if_indices); i++)
1674 sw_if_index = outside_sw_if_indices[i];
1675 snat_interface_add_del (sw_if_index, 0, is_del);
1679 vec_free (inside_sw_if_indices);
1680 vec_free (outside_sw_if_indices);
1685 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1686 .path = "set interface snat",
1687 .function = snat_feature_command_fn,
1688 .short_help = "set interface snat in <intfc> out <intfc> [del]",
1691 static clib_error_t *
1692 add_static_mapping_command_fn (vlib_main_t * vm,
1693 unformat_input_t * input,
1694 vlib_cli_command_t * cmd)
1696 unformat_input_t _line_input, *line_input = &_line_input;
1697 clib_error_t * error = 0;
1698 ip4_address_t l_addr, e_addr;
1699 u32 l_port = 0, e_port = 0, vrf_id = ~0;
1702 u32 sw_if_index = ~0;
1703 vnet_main_t * vnm = vnet_get_main();
1706 /* Get a line of input. */
1707 if (!unformat_user (input, unformat_line_input, line_input))
1710 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1712 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1715 else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1717 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1720 else if (unformat (line_input, "external %U", unformat_ip4_address,
1723 else if (unformat (line_input, "external %U %u",
1724 unformat_vnet_sw_interface, vnm, &sw_if_index,
1728 else if (unformat (line_input, "external %U",
1729 unformat_vnet_sw_interface, vnm, &sw_if_index))
1731 else if (unformat (line_input, "vrf %u", &vrf_id))
1733 else if (unformat (line_input, "del"))
1736 return clib_error_return (0, "unknown input: '%U'",
1737 format_unformat_error, line_input);
1739 unformat_free (line_input);
1741 rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
1742 vrf_id, addr_only, sw_if_index, is_add);
1746 case VNET_API_ERROR_INVALID_VALUE:
1747 return clib_error_return (0, "External port already in use.");
1749 case VNET_API_ERROR_NO_SUCH_ENTRY:
1751 return clib_error_return (0, "External addres must be allocated.");
1753 return clib_error_return (0, "Mapping not exist.");
1755 case VNET_API_ERROR_NO_SUCH_FIB:
1756 return clib_error_return (0, "No such VRF id.");
1757 case VNET_API_ERROR_VALUE_EXIST:
1758 return clib_error_return (0, "Mapping already exist.");
1768 * @cliexstart{snat add static mapping}
1769 * Static mapping allows hosts on the external network to initiate connection
1770 * to to the local network host.
1771 * To create static mapping between local host address 10.0.0.3 port 6303 and
1772 * external address 4.4.4.4 port 3606 use:
1773 * vpp# snat add static mapping local 10.0.0.3 6303 external 4.4.4.4 3606
1774 * If not runnig "static mapping only" S-NAT plugin mode use before:
1775 * vpp# snat add address 4.4.4.4
1776 * To create static mapping between local and external address use:
1777 * vpp# snat add static mapping local 10.0.0.3 external 4.4.4.4
1780 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1781 .path = "snat add static mapping",
1782 .function = add_static_mapping_command_fn,
1784 "snat add static mapping local <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
1787 static clib_error_t *
1788 set_workers_command_fn (vlib_main_t * vm,
1789 unformat_input_t * input,
1790 vlib_cli_command_t * cmd)
1792 unformat_input_t _line_input, *line_input = &_line_input;
1796 /* Get a line of input. */
1797 if (!unformat_user (input, unformat_line_input, line_input))
1800 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1802 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
1805 return clib_error_return (0, "unknown input '%U'",
1806 format_unformat_error, input);
1808 unformat_free (line_input);
1811 return clib_error_return (0, "List of workers must be specified.");
1813 rv = snat_set_workers(bitmap);
1815 clib_bitmap_free (bitmap);
1819 case VNET_API_ERROR_INVALID_WORKER:
1820 return clib_error_return (0, "Invalid worker(s).");
1822 case VNET_API_ERROR_FEATURE_DISABLED:
1823 return clib_error_return (0,
1824 "Supported only if 2 or more workes available.");
1835 * @cliexstart{set snat workers}
1836 * Set SNAT workers if 2 or more workers available, use:
1837 * vpp# set snat workers 0-2,5
1840 VLIB_CLI_COMMAND (set_workers_command, static) = {
1841 .path = "set snat workers",
1842 .function = set_workers_command_fn,
1844 "set snat workers <workers-list>",
1847 static clib_error_t *
1848 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
1849 unformat_input_t * input,
1850 vlib_cli_command_t * cmd)
1852 unformat_input_t _line_input, *line_input = &_line_input;
1858 /* Get a line of input. */
1859 if (!unformat_user (input, unformat_line_input, line_input))
1862 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1864 if (unformat (line_input, "domain %d", &domain_id))
1866 else if (unformat (line_input, "src-port %d", &src_port))
1868 else if (unformat (line_input, "disable"))
1871 return clib_error_return (0, "unknown input '%U'",
1872 format_unformat_error, input);
1874 unformat_free (line_input);
1876 rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
1879 return clib_error_return (0, "ipfix logging enable failed");
1886 * @cliexstart{snat ipfix logging}
1887 * To enable SNAT IPFIX logging use:
1888 * vpp# snat ipfix logging
1889 * To set IPFIX exporter use:
1890 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1893 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1894 .path = "snat ipfix logging",
1895 .function = snat_ipfix_logging_enable_disable_command_fn,
1896 .short_help = "snat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
1899 static clib_error_t *
1900 snat_config (vlib_main_t * vm, unformat_input_t * input)
1902 snat_main_t * sm = &snat_main;
1903 u32 translation_buckets = 1024;
1904 u32 translation_memory_size = 128<<20;
1905 u32 user_buckets = 128;
1906 u32 user_memory_size = 64<<20;
1907 u32 max_translations_per_user = 100;
1908 u32 outside_vrf_id = 0;
1909 u32 inside_vrf_id = 0;
1910 u32 static_mapping_buckets = 1024;
1911 u32 static_mapping_memory_size = 64<<20;
1912 u8 static_mapping_only = 0;
1913 u8 static_mapping_connection_tracking = 0;
1914 vlib_thread_main_t *tm = vlib_get_thread_main ();
1916 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1918 if (unformat (input, "translation hash buckets %d", &translation_buckets))
1920 else if (unformat (input, "translation hash memory %d",
1921 &translation_memory_size));
1922 else if (unformat (input, "user hash buckets %d", &user_buckets))
1924 else if (unformat (input, "user hash memory %d",
1927 else if (unformat (input, "max translations per user %d",
1928 &max_translations_per_user))
1930 else if (unformat (input, "outside VRF id %d",
1933 else if (unformat (input, "inside VRF id %d",
1936 else if (unformat (input, "static mapping only"))
1938 static_mapping_only = 1;
1939 if (unformat (input, "connection tracking"))
1940 static_mapping_connection_tracking = 1;
1943 return clib_error_return (0, "unknown input '%U'",
1944 format_unformat_error, input);
1947 /* for show commands, etc. */
1948 sm->translation_buckets = translation_buckets;
1949 sm->translation_memory_size = translation_memory_size;
1950 sm->user_buckets = user_buckets;
1951 sm->user_memory_size = user_memory_size;
1952 sm->max_translations_per_user = max_translations_per_user;
1953 sm->outside_vrf_id = outside_vrf_id;
1954 sm->outside_fib_index = ~0;
1955 sm->inside_vrf_id = inside_vrf_id;
1956 sm->inside_fib_index = ~0;
1957 sm->static_mapping_only = static_mapping_only;
1958 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
1960 if (!static_mapping_only ||
1961 (static_mapping_only && static_mapping_connection_tracking))
1963 clib_bihash_init_8_8 (&sm->worker_by_in, "worker-by-in", user_buckets,
1966 clib_bihash_init_8_8 (&sm->worker_by_out, "worker-by-out", user_buckets,
1969 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1971 clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
1972 translation_memory_size);
1974 clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
1975 translation_memory_size);
1977 clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
1980 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
1981 "static_mapping_by_local", static_mapping_buckets,
1982 static_mapping_memory_size);
1984 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
1985 "static_mapping_by_external", static_mapping_buckets,
1986 static_mapping_memory_size);
1990 VLIB_CONFIG_FUNCTION (snat_config, "snat");
1992 u8 * format_snat_key (u8 * s, va_list * args)
1994 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
1995 char * protocol_string = "unknown";
1996 static char *protocol_strings[] = {
2002 if (key->protocol < ARRAY_LEN(protocol_strings))
2003 protocol_string = protocol_strings[key->protocol];
2005 s = format (s, "%U proto %s port %d fib %d",
2006 format_ip4_address, &key->addr, protocol_string,
2007 clib_net_to_host_u16 (key->port), key->fib_index);
2011 u8 * format_snat_session (u8 * s, va_list * args)
2013 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
2014 snat_session_t * sess = va_arg (*args, snat_session_t *);
2016 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
2017 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
2018 s = format (s, " last heard %.2f\n", sess->last_heard);
2019 s = format (s, " total pkts %d, total bytes %lld\n",
2020 sess->total_pkts, sess->total_bytes);
2021 if (snat_is_session_static (sess))
2022 s = format (s, " static translation\n");
2024 s = format (s, " dynamic translation\n");
2029 u8 * format_snat_user (u8 * s, va_list * args)
2031 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
2032 snat_user_t * u = va_arg (*args, snat_user_t *);
2033 int verbose = va_arg (*args, int);
2034 dlist_elt_t * head, * elt;
2035 u32 elt_index, head_index;
2037 snat_session_t * sess;
2039 s = format (s, "%U: %d dynamic translations, %d static translations\n",
2040 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
2045 if (u->nsessions || u->nstaticsessions)
2047 head_index = u->sessions_per_user_list_head_index;
2048 head = pool_elt_at_index (sm->list_pool, head_index);
2050 elt_index = head->next;
2051 elt = pool_elt_at_index (sm->list_pool, elt_index);
2052 session_index = elt->value;
2054 while (session_index != ~0)
2056 sess = pool_elt_at_index (sm->sessions, session_index);
2058 s = format (s, " %U\n", format_snat_session, sm, sess);
2060 elt_index = elt->next;
2061 elt = pool_elt_at_index (sm->list_pool, elt_index);
2062 session_index = elt->value;
2069 u8 * format_snat_static_mapping (u8 * s, va_list * args)
2071 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
2074 s = format (s, "local %U external %U vrf %d",
2075 format_ip4_address, &m->local_addr,
2076 format_ip4_address, &m->external_addr,
2079 s = format (s, "local %U:%d external %U:%d vrf %d",
2080 format_ip4_address, &m->local_addr, m->local_port,
2081 format_ip4_address, &m->external_addr, m->external_port,
2087 static clib_error_t *
2088 show_snat_command_fn (vlib_main_t * vm,
2089 unformat_input_t * input,
2090 vlib_cli_command_t * cmd)
2093 snat_main_t * sm = &snat_main;
2095 snat_static_mapping_t *m;
2096 snat_interface_t *i;
2097 snat_address_t * ap;
2098 vnet_main_t *vnm = vnet_get_main();
2099 snat_main_per_thread_data_t *tsm;
2100 u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
2103 if (unformat (input, "detail"))
2105 else if (unformat (input, "verbose"))
2108 if (sm->static_mapping_only)
2110 if (sm->static_mapping_connection_tracking)
2111 vlib_cli_output (vm, "SNAT mode: static mapping only connection "
2114 vlib_cli_output (vm, "SNAT mode: static mapping only");
2118 vlib_cli_output (vm, "SNAT mode: dynamic translations enabled");
2123 pool_foreach (i, sm->interfaces,
2125 vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
2126 vnet_get_sw_interface (vnm, i->sw_if_index),
2127 i->is_inside ? "in" : "out");
2130 if (vec_len (sm->auto_add_sw_if_indices))
2132 vlib_cli_output (vm, "SNAT pool addresses interfaces:");
2133 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
2135 vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
2136 vnet_get_sw_interface (vnm, *sw_if_index));
2140 vec_foreach (ap, sm->addresses)
2142 u8 * s = format (0, "");
2143 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
2144 clib_bitmap_foreach (j, ap->busy_port_bitmap,
2146 s = format (s, " %d", j);
2148 vlib_cli_output (vm, " %d busy ports:%s", ap->busy_ports, s);
2152 if (sm->num_workers > 1)
2154 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
2157 vec_foreach (worker, sm->workers)
2159 vlib_worker_thread_t *w =
2160 vlib_worker_threads + *worker + sm->first_worker_index;
2161 vlib_cli_output (vm, " %s", w->name);
2166 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2168 vlib_cli_output (vm, "%d static mappings",
2169 pool_elts (sm->static_mappings));
2173 pool_foreach (m, sm->static_mappings,
2175 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2181 vec_foreach (tsm, sm->per_thread_data)
2183 users_num += pool_elts (tsm->users);
2184 sessions_num += pool_elts (tsm->sessions);
2187 vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
2188 " %d static mappings",
2190 vec_len (sm->addresses),
2192 pool_elts (sm->static_mappings));
2196 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
2198 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
2200 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_in,
2202 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_out,
2204 vec_foreach_index (j, sm->per_thread_data)
2206 tsm = vec_elt_at_index (sm->per_thread_data, j);
2208 if (pool_elts (tsm->users) == 0)
2211 vlib_worker_thread_t *w = vlib_worker_threads + j;
2212 vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
2214 vlib_cli_output (vm, " %d list pool elements",
2215 pool_elts (tsm->list_pool));
2217 pool_foreach (u, tsm->users,
2219 vlib_cli_output (vm, " %U", format_snat_user, tsm, u,
2224 if (pool_elts (sm->static_mappings))
2226 vlib_cli_output (vm, "static mappings:");
2227 pool_foreach (m, sm->static_mappings,
2229 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2238 VLIB_CLI_COMMAND (show_snat_command, static) = {
2239 .path = "show snat",
2240 .short_help = "show snat",
2241 .function = show_snat_command_fn,
2246 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2249 ip4_address_t * address,
2251 u32 if_address_index,
2254 snat_main_t *sm = &snat_main;
2255 snat_static_map_resolve_t *rp;
2256 u32 *indices_to_delete = 0;
2260 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2262 if (sw_if_index == sm->auto_add_sw_if_indices[i])
2266 /* Don't trip over lease renewal, static config */
2267 for (j = 0; j < vec_len(sm->addresses); j++)
2268 if (sm->addresses[j].addr.as_u32 == address->as_u32)
2271 snat_add_address (sm, address);
2272 /* Scan static map resolution vector */
2273 for (j = 0; j < vec_len (sm->to_resolve); j++)
2275 rp = sm->to_resolve + j;
2276 /* On this interface? */
2277 if (rp->sw_if_index == sw_if_index)
2279 /* Add the static mapping */
2280 rv = snat_add_static_mapping (rp->l_addr,
2286 ~0 /* sw_if_index */,
2289 clib_warning ("snat_add_static_mapping returned %d",
2291 vec_add1 (indices_to_delete, j);
2294 /* If we resolved any of the outstanding static mappings */
2295 if (vec_len(indices_to_delete))
2298 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2299 vec_delete(sm->to_resolve, 1, j);
2300 vec_free(indices_to_delete);
2306 (void) snat_del_address(sm, address[0], 1);
2314 static int snat_add_interface_address (snat_main_t *sm,
2318 ip4_main_t * ip4_main = sm->ip4_main;
2319 ip4_address_t * first_int_addr;
2320 snat_static_map_resolve_t *rp;
2321 u32 *indices_to_delete = 0;
2324 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2325 0 /* just want the address*/);
2327 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2329 if (sm->auto_add_sw_if_indices[i] == sw_if_index)
2333 /* if have address remove it */
2335 (void) snat_del_address (sm, first_int_addr[0], 1);
2338 for (j = 0; j < vec_len (sm->to_resolve); j++)
2340 rp = sm->to_resolve + j;
2341 if (rp->sw_if_index == sw_if_index)
2342 vec_add1 (indices_to_delete, j);
2344 if (vec_len(indices_to_delete))
2346 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2347 vec_del1(sm->to_resolve, j);
2348 vec_free(indices_to_delete);
2351 vec_del1(sm->auto_add_sw_if_indices, i);
2354 return VNET_API_ERROR_VALUE_EXIST;
2361 return VNET_API_ERROR_NO_SUCH_ENTRY;
2363 /* add to the auto-address list */
2364 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
2366 /* If the address is already bound - or static - add it now */
2368 snat_add_address (sm, first_int_addr);
2373 static clib_error_t *
2374 snat_add_interface_address_command_fn (vlib_main_t * vm,
2375 unformat_input_t * input,
2376 vlib_cli_command_t * cmd)
2378 snat_main_t *sm = &snat_main;
2379 unformat_input_t _line_input, *line_input = &_line_input;
2384 /* Get a line of input. */
2385 if (!unformat_user (input, unformat_line_input, line_input))
2388 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2390 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
2391 sm->vnet_main, &sw_if_index))
2393 else if (unformat (line_input, "del"))
2396 return clib_error_return (0, "unknown input '%U'",
2397 format_unformat_error, line_input);
2400 rv = snat_add_interface_address (sm, sw_if_index, is_del);
2408 return clib_error_return (0, "snat_add_interface_address returned %d",
2414 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2415 .path = "snat add interface address",
2416 .short_help = "snat add interface address <interface> [del]",
2417 .function = snat_add_interface_address_command_fn,