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 int snat_del_address (snat_main_t *sm, ip4_address_t addr)
245 snat_address_t *a = 0;
247 u32 *ses_to_be_removed = 0, *ses_index;
248 clib_bihash_kv_8_8_t kv, value;
249 snat_user_key_t user_key;
251 snat_main_per_thread_data_t *tsm;
255 /* Find SNAT address */
256 for (i=0; i < vec_len (sm->addresses); i++)
258 if (sm->addresses[i].addr.as_u32 == addr.as_u32)
260 a = sm->addresses + i;
265 return VNET_API_ERROR_NO_SUCH_ENTRY;
267 /* Check if address is used in some static mapping */
268 if (is_snat_address_used_in_static_mapping(sm, addr))
270 clib_warning ("address used in static mapping");
271 return VNET_API_ERROR_UNSPECIFIED;
274 /* Delete sessions using address */
277 vec_foreach (tsm, sm->per_thread_data)
279 pool_foreach (ses, tsm->sessions, ({
280 if (ses->out2in.addr.as_u32 == addr.as_u32)
283 snat_ipfix_logging_nat44_ses_delete(ses->in2out.addr.as_u32,
284 ses->out2in.addr.as_u32,
285 ses->in2out.protocol,
288 ses->in2out.fib_index);
289 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
290 kv.key = ses->in2out.as_u64;
291 clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
292 kv.key = ses->out2in.as_u64;
293 clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
294 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
295 user_key.addr = ses->in2out.addr;
296 user_key.fib_index = ses->in2out.fib_index;
297 kv.key = user_key.as_u64;
298 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
300 u = pool_elt_at_index (tsm->users, value.value);
306 vec_foreach (ses_index, ses_to_be_removed)
307 pool_put_index (tsm->sessions, ses_index[0]);
309 vec_free (ses_to_be_removed);
313 vec_del1 (sm->addresses, i);
318 static void increment_v4_address (ip4_address_t * a)
322 v = clib_net_to_host_u32(a->as_u32) + 1;
323 a->as_u32 = clib_host_to_net_u32(v);
327 * @brief Add static mapping.
329 * Create static mapping between local addr+port and external addr+port.
331 * @param l_addr Local IPv4 address.
332 * @param e_addr External IPv4 address.
333 * @param l_port Local port number.
334 * @param e_port External port number.
335 * @param vrf_id VRF ID.
336 * @param addr_only If 0 address port and pair mapping, otherwise address only.
337 * @param is_add If 0 delete static mapping, otherwise add.
341 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
342 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
345 snat_main_t * sm = &snat_main;
346 snat_static_mapping_t *m;
347 snat_static_mapping_key_t m_key;
348 clib_bihash_kv_8_8_t kv, value;
349 snat_address_t *a = 0;
354 /* If outside FIB index is not resolved yet */
355 if (sm->outside_fib_index == ~0)
357 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->outside_vrf_id);
359 return VNET_API_ERROR_NO_SUCH_FIB;
360 sm->outside_fib_index = p[0];
364 m_key.port = addr_only ? 0 : e_port;
365 m_key.fib_index = sm->outside_fib_index;
366 kv.key = m_key.as_u64;
367 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
370 m = pool_elt_at_index (sm->static_mappings, value.value);
375 return VNET_API_ERROR_VALUE_EXIST;
377 /* Convert VRF id to FIB index */
380 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
382 return VNET_API_ERROR_NO_SUCH_FIB;
385 /* If not specified use inside VRF id from SNAT plugin startup config */
388 if (sm->inside_fib_index == ~0)
390 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->inside_vrf_id);
392 return VNET_API_ERROR_NO_SUCH_FIB;
394 sm->inside_fib_index = fib_index;
397 fib_index = sm->inside_fib_index;
399 vrf_id = sm->inside_vrf_id;
402 /* Find external address in allocated addresses and reserve port for
403 address and port pair mapping when dynamic translations enabled */
404 if (!addr_only && !(sm->static_mapping_only))
406 for (i = 0; i < vec_len (sm->addresses); i++)
408 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
410 a = sm->addresses + i;
411 /* External port must be unused */
412 if (clib_bitmap_get_no_check (a->busy_port_bitmap, e_port))
413 return VNET_API_ERROR_INVALID_VALUE;
414 clib_bitmap_set_no_check (a->busy_port_bitmap, e_port, 1);
421 /* External address must be allocated */
423 return VNET_API_ERROR_NO_SUCH_ENTRY;
426 pool_get (sm->static_mappings, m);
427 memset (m, 0, sizeof (*m));
428 m->local_addr = l_addr;
429 m->external_addr = e_addr;
430 m->addr_only = addr_only;
432 m->fib_index = fib_index;
435 m->local_port = l_port;
436 m->external_port = e_port;
439 m_key.addr = m->local_addr;
440 m_key.port = m->local_port;
441 m_key.fib_index = m->fib_index;
442 kv.key = m_key.as_u64;
443 kv.value = m - sm->static_mappings;
444 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
446 m_key.addr = m->external_addr;
447 m_key.port = m->external_port;
448 m_key.fib_index = sm->outside_fib_index;
449 kv.key = m_key.as_u64;
450 kv.value = m - sm->static_mappings;
451 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
456 snat_user_key_t w_key0;
457 snat_static_mapping_key_t w_key1;
459 w_key0.addr = m->local_addr;
460 w_key0.fib_index = m->fib_index;
461 kv.key = w_key0.as_u64;
463 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
465 kv.value = sm->first_worker_index +
466 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
468 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv, 1);
472 kv.value = value.value;
475 w_key1.addr = m->external_addr;
476 w_key1.port = clib_host_to_net_u16 (m->external_port);
477 w_key1.fib_index = sm->outside_fib_index;
478 kv.key = w_key1.as_u64;
479 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv, 1);
485 return VNET_API_ERROR_NO_SUCH_ENTRY;
487 /* Free external address port */
488 if (!addr_only && !(sm->static_mapping_only))
490 for (i = 0; i < vec_len (sm->addresses); i++)
492 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
494 a = sm->addresses + i;
495 clib_bitmap_set_no_check (a->busy_port_bitmap, e_port, 0);
503 m_key.addr = m->local_addr;
504 m_key.port = m->local_port;
505 m_key.fib_index = m->fib_index;
506 kv.key = m_key.as_u64;
507 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
509 m_key.addr = m->external_addr;
510 m_key.port = m->external_port;
511 m_key.fib_index = sm->outside_fib_index;
512 kv.key = m_key.as_u64;
513 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
515 /* Delete session(s) for static mapping if exist */
516 if (!(sm->static_mapping_only) ||
517 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
519 snat_user_key_t u_key;
521 dlist_elt_t * head, * elt;
522 u32 elt_index, head_index, del_elt_index;
526 snat_main_per_thread_data_t *tsm;
528 u_key.addr = m->local_addr;
529 u_key.fib_index = m->fib_index;
530 kv.key = u_key.as_u64;
531 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
533 user_index = value.value;
534 if (!clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
535 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
537 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
538 u = pool_elt_at_index (tsm->users, user_index);
539 if (u->nstaticsessions)
541 head_index = u->sessions_per_user_list_head_index;
542 head = pool_elt_at_index (tsm->list_pool, head_index);
543 elt_index = head->next;
544 elt = pool_elt_at_index (tsm->list_pool, elt_index);
545 ses_index = elt->value;
546 while (ses_index != ~0)
548 s = pool_elt_at_index (tsm->sessions, ses_index);
549 del_elt_index = elt_index;
550 elt_index = elt->next;
551 elt = pool_elt_at_index (tsm->list_pool, elt_index);
552 ses_index = elt->value;
556 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
557 (clib_net_to_host_u16 (s->out2in.port) != e_port))
562 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
563 s->out2in.addr.as_u32,
567 s->in2out.fib_index);
569 value.key = s->in2out.as_u64;
570 clib_bihash_add_del_8_8 (&sm->in2out, &value, 0);
571 value.key = s->out2in.as_u64;
572 clib_bihash_add_del_8_8 (&sm->out2in, &value, 0);
573 pool_put (tsm->sessions, s);
575 clib_dlist_remove (tsm->list_pool, del_elt_index);
576 pool_put_index (tsm->list_pool, del_elt_index);
577 u->nstaticsessions--;
584 pool_put (tsm->users, u);
585 clib_bihash_add_del_8_8 (&sm->user_hash, &kv, 0);
591 /* Delete static mapping from pool */
592 pool_put (sm->static_mappings, m);
598 static int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
600 snat_main_t *sm = &snat_main;
602 const char * feature_name;
604 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
605 feature_name = is_inside ? "snat-in2out-fast" : "snat-out2in-fast";
608 if (sm->num_workers > 1)
609 feature_name = is_inside ? "snat-in2out-worker-handoff" : "snat-out2in-worker-handoff";
611 feature_name = is_inside ? "snat-in2out" : "snat-out2in";
614 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
617 if (sm->fq_in2out_index == ~0)
618 sm->fq_in2out_index = vlib_frame_queue_main_init (snat_in2out_node.index, 0);
620 if (sm->fq_out2in_index == ~0)
621 sm->fq_out2in_index = vlib_frame_queue_main_init (snat_out2in_node.index, 0);
623 pool_foreach (i, sm->interfaces,
625 if (i->sw_if_index == sw_if_index)
628 pool_put (sm->interfaces, i);
630 return VNET_API_ERROR_VALUE_EXIST;
637 return VNET_API_ERROR_NO_SUCH_ENTRY;
639 pool_get (sm->interfaces, i);
640 i->sw_if_index = sw_if_index;
641 i->is_inside = is_inside;
646 static int snat_set_workers (uword * bitmap)
648 snat_main_t *sm = &snat_main;
651 if (sm->num_workers < 2)
652 return VNET_API_ERROR_FEATURE_DISABLED;
654 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
655 return VNET_API_ERROR_INVALID_WORKER;
657 vec_free (sm->workers);
658 clib_bitmap_foreach (i, bitmap,
660 vec_add1(sm->workers, i);
667 vl_api_snat_add_address_range_t_handler
668 (vl_api_snat_add_address_range_t * mp)
670 snat_main_t * sm = &snat_main;
671 vl_api_snat_add_address_range_reply_t * rmp;
672 ip4_address_t this_addr;
673 u32 start_host_order, end_host_order;
680 rv = VNET_API_ERROR_UNIMPLEMENTED;
684 if (sm->static_mapping_only)
686 rv = VNET_API_ERROR_FEATURE_DISABLED;
690 tmp = (u32 *) mp->first_ip_address;
691 start_host_order = clib_host_to_net_u32 (tmp[0]);
692 tmp = (u32 *) mp->last_ip_address;
693 end_host_order = clib_host_to_net_u32 (tmp[0]);
695 count = (end_host_order - start_host_order) + 1;
698 clib_warning ("%U - %U, %d addresses...",
699 format_ip4_address, mp->first_ip_address,
700 format_ip4_address, mp->last_ip_address,
703 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
705 for (i = 0; i < count; i++)
708 snat_add_address (sm, &this_addr);
710 rv = snat_del_address (sm, this_addr);
715 increment_v4_address (&this_addr);
719 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
722 static void *vl_api_snat_add_address_range_t_print
723 (vl_api_snat_add_address_range_t *mp, void * handle)
727 s = format (0, "SCRIPT: snat_add_address_range ");
728 s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
729 if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
731 s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
737 send_snat_address_details
738 (snat_address_t * a, unix_shared_memory_queue_t * q, u32 context)
740 vl_api_snat_address_details_t *rmp;
741 snat_main_t * sm = &snat_main;
743 rmp = vl_msg_api_alloc (sizeof (*rmp));
744 memset (rmp, 0, sizeof (*rmp));
745 rmp->_vl_msg_id = ntohs (VL_API_SNAT_ADDRESS_DETAILS+sm->msg_id_base);
747 clib_memcpy (rmp->ip_address, &(a->addr), 4);
748 rmp->context = context;
750 vl_msg_api_send_shmem (q, (u8 *) & rmp);
754 vl_api_snat_address_dump_t_handler
755 (vl_api_snat_address_dump_t * mp)
757 unix_shared_memory_queue_t *q;
758 snat_main_t * sm = &snat_main;
761 q = vl_api_client_index_to_input_queue (mp->client_index);
765 vec_foreach (a, sm->addresses)
766 send_snat_address_details (a, q, mp->context);
769 static void *vl_api_snat_address_dump_t_print
770 (vl_api_snat_address_dump_t *mp, void * handle)
774 s = format (0, "SCRIPT: snat_address_dump ");
780 vl_api_snat_interface_add_del_feature_t_handler
781 (vl_api_snat_interface_add_del_feature_t * mp)
783 snat_main_t * sm = &snat_main;
784 vl_api_snat_interface_add_del_feature_reply_t * rmp;
785 u8 is_del = mp->is_add == 0;
786 u32 sw_if_index = ntohl(mp->sw_if_index);
789 VALIDATE_SW_IF_INDEX(mp);
791 rv = snat_interface_add_del (sw_if_index, mp->is_inside, is_del);
793 BAD_SW_IF_INDEX_LABEL;
795 REPLY_MACRO(VL_API_SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY);
798 static void *vl_api_snat_interface_add_del_feature_t_print
799 (vl_api_snat_interface_add_del_feature_t * mp, void *handle)
803 s = format (0, "SCRIPT: snat_interface_add_del_feature ");
804 s = format (s, "sw_if_index %d %s %s",
805 clib_host_to_net_u32(mp->sw_if_index),
806 mp->is_inside ? "in":"out",
807 mp->is_add ? "" : "del");
813 send_snat_interface_details
814 (snat_interface_t * i, unix_shared_memory_queue_t * q, u32 context)
816 vl_api_snat_interface_details_t *rmp;
817 snat_main_t * sm = &snat_main;
819 rmp = vl_msg_api_alloc (sizeof (*rmp));
820 memset (rmp, 0, sizeof (*rmp));
821 rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_DETAILS+sm->msg_id_base);
822 rmp->sw_if_index = ntohl (i->sw_if_index);
823 rmp->is_inside = i->is_inside;
824 rmp->context = context;
826 vl_msg_api_send_shmem (q, (u8 *) & rmp);
830 vl_api_snat_interface_dump_t_handler
831 (vl_api_snat_interface_dump_t * mp)
833 unix_shared_memory_queue_t *q;
834 snat_main_t * sm = &snat_main;
835 snat_interface_t * i;
837 q = vl_api_client_index_to_input_queue (mp->client_index);
841 pool_foreach (i, sm->interfaces,
843 send_snat_interface_details(i, q, mp->context);
847 static void *vl_api_snat_interface_dump_t_print
848 (vl_api_snat_interface_dump_t *mp, void * handle)
852 s = format (0, "SCRIPT: snat_interface_dump ");
857 vl_api_snat_add_static_mapping_t_handler
858 (vl_api_snat_add_static_mapping_t * mp)
860 snat_main_t * sm = &snat_main;
861 vl_api_snat_add_static_mapping_reply_t * rmp;
862 ip4_address_t local_addr, external_addr;
863 u16 local_port = 0, external_port = 0;
869 rv = VNET_API_ERROR_UNIMPLEMENTED;
873 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
874 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
875 if (mp->addr_only == 0)
877 local_port = clib_net_to_host_u16 (mp->local_port);
878 external_port = clib_net_to_host_u16 (mp->external_port);
880 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
882 rv = snat_add_static_mapping(local_addr, external_addr, local_port,
883 external_port, vrf_id, mp->addr_only,
887 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
890 static void *vl_api_snat_add_static_mapping_t_print
891 (vl_api_snat_add_static_mapping_t *mp, void * handle)
895 s = format (0, "SCRIPT: snat_add_static_mapping ");
896 s = format (s, "local_addr %U external_addr %U ",
897 format_ip4_address, mp->local_ip_address,
898 format_ip4_address, mp->external_ip_address);
900 if (mp->addr_only == 0)
901 s = format (s, "local_port %d external_port %d ",
902 clib_net_to_host_u16 (mp->local_port),
903 clib_net_to_host_u16 (mp->external_port));
905 if (mp->vrf_id != ~0)
906 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
912 send_snat_static_mapping_details
913 (snat_static_mapping_t * m, unix_shared_memory_queue_t * q, u32 context)
915 vl_api_snat_static_mapping_details_t *rmp;
916 snat_main_t * sm = &snat_main;
918 rmp = vl_msg_api_alloc (sizeof (*rmp));
919 memset (rmp, 0, sizeof (*rmp));
920 rmp->_vl_msg_id = ntohs (VL_API_SNAT_STATIC_MAPPING_DETAILS+sm->msg_id_base);
922 rmp->addr_only = m->addr_only;
923 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
924 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
925 rmp->local_port = htons (m->local_port);
926 rmp->external_port = htons (m->external_port);
927 rmp->vrf_id = htonl (m->vrf_id);
928 rmp->context = context;
930 vl_msg_api_send_shmem (q, (u8 *) & rmp);
934 vl_api_snat_static_mapping_dump_t_handler
935 (vl_api_snat_static_mapping_dump_t * mp)
937 unix_shared_memory_queue_t *q;
938 snat_main_t * sm = &snat_main;
939 snat_static_mapping_t * m;
941 q = vl_api_client_index_to_input_queue (mp->client_index);
945 pool_foreach (m, sm->static_mappings,
947 send_snat_static_mapping_details (m, q, mp->context);
951 static void *vl_api_snat_static_mapping_dump_t_print
952 (vl_api_snat_static_mapping_dump_t *mp, void * handle)
956 s = format (0, "SCRIPT: snat_static_mapping_dump ");
962 vl_api_snat_control_ping_t_handler
963 (vl_api_snat_control_ping_t * mp)
965 vl_api_snat_control_ping_reply_t *rmp;
966 snat_main_t * sm = &snat_main;
969 REPLY_MACRO2(VL_API_SNAT_CONTROL_PING_REPLY,
971 rmp->vpe_pid = ntohl (getpid());
975 static void *vl_api_snat_control_ping_t_print
976 (vl_api_snat_control_ping_t *mp, void * handle)
980 s = format (0, "SCRIPT: snat_control_ping ");
986 vl_api_snat_show_config_t_handler
987 (vl_api_snat_show_config_t * mp)
989 vl_api_snat_show_config_reply_t *rmp;
990 snat_main_t * sm = &snat_main;
993 REPLY_MACRO2(VL_API_SNAT_SHOW_CONFIG_REPLY,
995 rmp->translation_buckets = htonl (sm->translation_buckets);
996 rmp->translation_memory_size = htonl (sm->translation_memory_size);
997 rmp->user_buckets = htonl (sm->user_buckets);
998 rmp->user_memory_size = htonl (sm->user_memory_size);
999 rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
1000 rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
1001 rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
1002 rmp->static_mapping_only = sm->static_mapping_only;
1003 rmp->static_mapping_connection_tracking =
1004 sm->static_mapping_connection_tracking;
1008 static void *vl_api_snat_show_config_t_print
1009 (vl_api_snat_show_config_t *mp, void * handle)
1013 s = format (0, "SCRIPT: snat_show_config ");
1019 vl_api_snat_set_workers_t_handler
1020 (vl_api_snat_set_workers_t * mp)
1022 snat_main_t * sm = &snat_main;
1023 vl_api_snat_set_workers_reply_t * rmp;
1026 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
1028 if (sm->num_workers < 2)
1030 rv = VNET_API_ERROR_FEATURE_DISABLED;
1034 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
1035 rv = snat_set_workers(bitmap);
1036 clib_bitmap_free (bitmap);
1039 REPLY_MACRO (VL_API_SNAT_SET_WORKERS_REPLY);
1042 static void *vl_api_snat_set_workers_t_print
1043 (vl_api_snat_set_workers_t *mp, void * handle)
1049 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
1051 s = format (0, "SCRIPT: snat_set_workers ");
1052 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
1053 clib_bitmap_foreach (i, bitmap,
1056 s = format (s, "%d", i);
1058 s = format (s, ",%d", i);
1061 clib_bitmap_free (bitmap);
1066 send_snat_worker_details
1067 (u32 worker_index, unix_shared_memory_queue_t * q, u32 context)
1069 vl_api_snat_worker_details_t *rmp;
1070 snat_main_t * sm = &snat_main;
1071 vlib_worker_thread_t *w =
1072 vlib_worker_threads + worker_index + sm->first_worker_index;
1074 rmp = vl_msg_api_alloc (sizeof (*rmp));
1075 memset (rmp, 0, sizeof (*rmp));
1076 rmp->_vl_msg_id = ntohs (VL_API_SNAT_WORKER_DETAILS+sm->msg_id_base);
1077 rmp->context = context;
1078 rmp->worker_index = htonl (worker_index);
1079 rmp->lcore_id = htonl (w->lcore_id);
1080 strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
1082 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1086 vl_api_snat_worker_dump_t_handler
1087 (vl_api_snat_worker_dump_t * mp)
1089 unix_shared_memory_queue_t *q;
1090 snat_main_t * sm = &snat_main;
1093 q = vl_api_client_index_to_input_queue (mp->client_index);
1097 vec_foreach (worker_index, sm->workers)
1099 send_snat_worker_details(*worker_index, q, mp->context);
1103 static void *vl_api_snat_worker_dump_t_print
1104 (vl_api_snat_worker_dump_t *mp, void * handle)
1108 s = format (0, "SCRIPT: snat_worker_dump ");
1113 static int snat_add_interface_address(snat_main_t *sm,
1118 vl_api_snat_add_del_interface_addr_t_handler
1119 (vl_api_snat_add_del_interface_addr_t * mp)
1121 snat_main_t * sm = &snat_main;
1122 vl_api_snat_add_del_interface_addr_reply_t * rmp;
1123 u8 is_del = mp->is_add == 0;
1124 u32 sw_if_index = ntohl(mp->sw_if_index);
1127 VALIDATE_SW_IF_INDEX(mp);
1129 rv = snat_add_interface_address (sm, sw_if_index, is_del);
1131 BAD_SW_IF_INDEX_LABEL;
1133 REPLY_MACRO(VL_API_SNAT_ADD_DEL_INTERFACE_ADDR_REPLY);
1136 static void *vl_api_snat_add_del_interface_addr_t_print
1137 (vl_api_snat_add_del_interface_addr_t * mp, void *handle)
1141 s = format (0, "SCRIPT: snat_add_del_interface_addr ");
1142 s = format (s, "sw_if_index %d %s",
1143 clib_host_to_net_u32(mp->sw_if_index),
1144 mp->is_add ? "" : "del");
1150 send_snat_interface_addr_details
1151 (u32 sw_if_index, unix_shared_memory_queue_t * q, u32 context)
1153 vl_api_snat_interface_addr_details_t *rmp;
1154 snat_main_t * sm = &snat_main;
1156 rmp = vl_msg_api_alloc (sizeof (*rmp));
1157 memset (rmp, 0, sizeof (*rmp));
1158 rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_ADDR_DETAILS+sm->msg_id_base);
1159 rmp->sw_if_index = ntohl (sw_if_index);
1160 rmp->context = context;
1162 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1166 vl_api_snat_interface_addr_dump_t_handler
1167 (vl_api_snat_interface_addr_dump_t * mp)
1169 unix_shared_memory_queue_t *q;
1170 snat_main_t * sm = &snat_main;
1173 q = vl_api_client_index_to_input_queue (mp->client_index);
1177 vec_foreach (i, sm->auto_add_sw_if_indices)
1178 send_snat_interface_addr_details(*i, q, mp->context);
1181 static void *vl_api_snat_interface_addr_dump_t_print
1182 (vl_api_snat_interface_addr_dump_t *mp, void * handle)
1186 s = format (0, "SCRIPT: snat_interface_addr_dump ");
1192 vl_api_snat_ipfix_enable_disable_t_handler
1193 (vl_api_snat_ipfix_enable_disable_t * mp)
1195 snat_main_t * sm = &snat_main;
1196 vl_api_snat_ipfix_enable_disable_reply_t * rmp;
1199 rv = snat_ipfix_logging_enable_disable(mp->enable,
1200 clib_host_to_net_u32 (mp->domain_id),
1201 clib_host_to_net_u16 (mp->src_port));
1203 REPLY_MACRO (VL_API_SNAT_IPFIX_ENABLE_DISABLE_REPLY);
1206 static void *vl_api_snat_ipfix_enable_disable_t_print
1207 (vl_api_snat_ipfix_enable_disable_t *mp, void * handle)
1211 s = format (0, "SCRIPT: snat_ipfix_enable_disable ");
1213 s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
1215 s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
1217 s = format (s, "disable ");
1222 /* List of message types that this plugin understands */
1223 #define foreach_snat_plugin_api_msg \
1224 _(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range) \
1225 _(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature) \
1226 _(SNAT_ADD_STATIC_MAPPING, snat_add_static_mapping) \
1227 _(SNAT_CONTROL_PING, snat_control_ping) \
1228 _(SNAT_STATIC_MAPPING_DUMP, snat_static_mapping_dump) \
1229 _(SNAT_SHOW_CONFIG, snat_show_config) \
1230 _(SNAT_ADDRESS_DUMP, snat_address_dump) \
1231 _(SNAT_INTERFACE_DUMP, snat_interface_dump) \
1232 _(SNAT_SET_WORKERS, snat_set_workers) \
1233 _(SNAT_WORKER_DUMP, snat_worker_dump) \
1234 _(SNAT_ADD_DEL_INTERFACE_ADDR, snat_add_del_interface_addr) \
1235 _(SNAT_INTERFACE_ADDR_DUMP, snat_interface_addr_dump) \
1236 _(SNAT_IPFIX_ENABLE_DISABLE, snat_ipfix_enable_disable)
1238 /* Set up the API message handling tables */
1239 static clib_error_t *
1240 snat_plugin_api_hookup (vlib_main_t *vm)
1242 snat_main_t * sm __attribute__ ((unused)) = &snat_main;
1244 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
1246 vl_api_##n##_t_handler, \
1248 vl_api_##n##_t_endian, \
1249 vl_api_##n##_t_print, \
1250 sizeof(vl_api_##n##_t), 1);
1251 foreach_snat_plugin_api_msg;
1257 #define vl_msg_name_crc_list
1258 #include <snat/snat_all_api_h.h>
1259 #undef vl_msg_name_crc_list
1262 setup_message_id_table (snat_main_t * sm, api_main_t * am)
1264 #define _(id,n,crc) \
1265 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
1266 foreach_vl_msg_name_crc_snat;
1270 static void plugin_custom_dump_configure (snat_main_t * sm)
1272 #define _(n,f) sm->api_main->msg_print_handlers \
1273 [VL_API_##n + sm->msg_id_base] \
1274 = (void *) vl_api_##f##_t_print;
1275 foreach_snat_plugin_api_msg;
1280 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1283 ip4_address_t * address,
1285 u32 if_address_index,
1288 static clib_error_t * snat_init (vlib_main_t * vm)
1290 snat_main_t * sm = &snat_main;
1291 clib_error_t * error = 0;
1292 ip4_main_t * im = &ip4_main;
1293 ip_lookup_main_t * lm = &im->lookup_main;
1296 vlib_thread_registration_t *tr;
1297 vlib_thread_main_t *tm = vlib_get_thread_main ();
1300 ip4_add_del_interface_address_callback_t cb4;
1302 name = format (0, "snat_%08x%c", api_version, 0);
1304 /* Ask for a correctly-sized block of API message decode slots */
1305 sm->msg_id_base = vl_msg_api_get_msg_ids
1306 ((char *) name, VL_MSG_FIRST_AVAILABLE);
1309 sm->vnet_main = vnet_get_main();
1311 sm->ip4_lookup_main = lm;
1312 sm->api_main = &api_main;
1313 sm->first_worker_index = 0;
1314 sm->next_worker = 0;
1315 sm->num_workers = 0;
1317 sm->fq_in2out_index = ~0;
1318 sm->fq_out2in_index = ~0;
1320 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1323 tr = (vlib_thread_registration_t *) p[0];
1326 sm->num_workers = tr->count;
1327 sm->first_worker_index = tr->first_index;
1331 /* Use all available workers by default */
1332 if (sm->num_workers > 1)
1334 for (i=0; i < sm->num_workers; i++)
1335 bitmap = clib_bitmap_set (bitmap, i, 1);
1336 snat_set_workers(bitmap);
1337 clib_bitmap_free (bitmap);
1340 error = snat_plugin_api_hookup (vm);
1342 /* Add our API messages to the global name_crc hash table */
1343 setup_message_id_table (sm, &api_main);
1345 plugin_custom_dump_configure (sm);
1348 /* Set up the interface address add/del callback */
1349 cb4.function = snat_ip4_add_del_interface_address_cb;
1350 cb4.function_opaque = 0;
1352 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1354 /* Init IPFIX logging */
1355 snat_ipfix_logging_init(vm);
1360 VLIB_INIT_FUNCTION (snat_init);
1362 void snat_free_outside_address_and_port (snat_main_t * sm,
1363 snat_session_key_t * k,
1367 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1369 ASSERT (address_index < vec_len (sm->addresses));
1371 a = sm->addresses + address_index;
1373 ASSERT (clib_bitmap_get_no_check (a->busy_port_bitmap,
1374 port_host_byte_order) == 1);
1376 clib_bitmap_set_no_check (a->busy_port_bitmap, port_host_byte_order, 0);
1381 * @brief Match SNAT static mapping.
1383 * @param sm SNAT main.
1384 * @param match Address and port to match.
1385 * @param mapping External or local address and port of the matched mapping.
1386 * @param by_external If 0 match by local address otherwise match by external
1389 * @returns 0 if match found otherwise 1.
1391 int snat_static_mapping_match (snat_main_t * sm,
1392 snat_session_key_t match,
1393 snat_session_key_t * mapping,
1396 clib_bihash_kv_8_8_t kv, value;
1397 snat_static_mapping_t *m;
1398 snat_static_mapping_key_t m_key;
1399 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1402 mapping_hash = &sm->static_mapping_by_external;
1404 m_key.addr = match.addr;
1405 m_key.port = clib_net_to_host_u16 (match.port);
1406 m_key.fib_index = match.fib_index;
1408 kv.key = m_key.as_u64;
1410 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1412 /* Try address only mapping */
1414 kv.key = m_key.as_u64;
1415 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1419 m = pool_elt_at_index (sm->static_mappings, value.value);
1423 mapping->addr = m->local_addr;
1424 /* Address only mapping doesn't change port */
1425 mapping->port = m->addr_only ? match.port
1426 : clib_host_to_net_u16 (m->local_port);
1427 mapping->fib_index = m->fib_index;
1431 mapping->addr = m->external_addr;
1432 /* Address only mapping doesn't change port */
1433 mapping->port = m->addr_only ? match.port
1434 : clib_host_to_net_u16 (m->external_port);
1435 mapping->fib_index = sm->outside_fib_index;
1441 int snat_alloc_outside_address_and_port (snat_main_t * sm,
1442 snat_session_key_t * k,
1443 u32 * address_indexp)
1449 for (i = 0; i < vec_len (sm->addresses); i++)
1451 if (sm->addresses[i].busy_ports < (65535-1024))
1453 a = sm->addresses + i;
1457 portnum = random_u32 (&sm->random_seed);
1461 if (clib_bitmap_get_no_check (a->busy_port_bitmap, portnum))
1463 clib_bitmap_set_no_check (a->busy_port_bitmap, portnum, 1);
1465 /* Caller sets protocol and fib index */
1467 k->port = clib_host_to_net_u16(portnum);
1468 *address_indexp = i;
1473 /* Totally out of translations to use... */
1474 snat_ipfix_logging_addresses_exhausted(0);
1479 static clib_error_t *
1480 add_address_command_fn (vlib_main_t * vm,
1481 unformat_input_t * input,
1482 vlib_cli_command_t * cmd)
1484 unformat_input_t _line_input, *line_input = &_line_input;
1485 snat_main_t * sm = &snat_main;
1486 ip4_address_t start_addr, end_addr, this_addr;
1487 u32 start_host_order, end_host_order;
1492 /* Get a line of input. */
1493 if (!unformat_user (input, unformat_line_input, line_input))
1496 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1498 if (unformat (line_input, "%U - %U",
1499 unformat_ip4_address, &start_addr,
1500 unformat_ip4_address, &end_addr))
1502 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
1503 end_addr = start_addr;
1504 else if (unformat (line_input, "del"))
1507 return clib_error_return (0, "unknown input '%U'",
1508 format_unformat_error, input);
1510 unformat_free (line_input);
1512 if (sm->static_mapping_only)
1513 return clib_error_return (0, "static mapping only mode");
1515 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
1516 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
1518 if (end_host_order < start_host_order)
1519 return clib_error_return (0, "end address less than start address");
1521 count = (end_host_order - start_host_order) + 1;
1524 clib_warning ("%U - %U, %d addresses...",
1525 format_ip4_address, &start_addr,
1526 format_ip4_address, &end_addr,
1529 this_addr = start_addr;
1531 for (i = 0; i < count; i++)
1534 snat_add_address (sm, &this_addr);
1536 rv = snat_del_address (sm, this_addr);
1540 case VNET_API_ERROR_NO_SUCH_ENTRY:
1541 return clib_error_return (0, "S-NAT address not exist.");
1543 case VNET_API_ERROR_UNSPECIFIED:
1544 return clib_error_return (0, "S-NAT address used in static mapping.");
1550 increment_v4_address (&this_addr);
1556 VLIB_CLI_COMMAND (add_address_command, static) = {
1557 .path = "snat add address",
1558 .short_help = "snat add addresses <ip4-range-start> [- <ip4-range-end>] [del]",
1559 .function = add_address_command_fn,
1562 static clib_error_t *
1563 snat_feature_command_fn (vlib_main_t * vm,
1564 unformat_input_t * input,
1565 vlib_cli_command_t * cmd)
1567 unformat_input_t _line_input, *line_input = &_line_input;
1568 vnet_main_t * vnm = vnet_get_main();
1569 clib_error_t * error = 0;
1571 u32 * inside_sw_if_indices = 0;
1572 u32 * outside_sw_if_indices = 0;
1578 /* Get a line of input. */
1579 if (!unformat_user (input, unformat_line_input, line_input))
1582 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1584 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
1586 vec_add1 (inside_sw_if_indices, sw_if_index);
1587 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
1589 vec_add1 (outside_sw_if_indices, sw_if_index);
1590 else if (unformat (line_input, "del"))
1593 return clib_error_return (0, "unknown input '%U'",
1594 format_unformat_error, input);
1596 unformat_free (line_input);
1598 if (vec_len (inside_sw_if_indices))
1600 for (i = 0; i < vec_len(inside_sw_if_indices); i++)
1602 sw_if_index = inside_sw_if_indices[i];
1603 snat_interface_add_del (sw_if_index, 1, is_del);
1607 if (vec_len (outside_sw_if_indices))
1609 for (i = 0; i < vec_len(outside_sw_if_indices); i++)
1611 sw_if_index = outside_sw_if_indices[i];
1612 snat_interface_add_del (sw_if_index, 0, is_del);
1616 vec_free (inside_sw_if_indices);
1617 vec_free (outside_sw_if_indices);
1622 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1623 .path = "set interface snat",
1624 .function = snat_feature_command_fn,
1625 .short_help = "set interface snat in <intfc> out <intfc> [del]",
1628 static clib_error_t *
1629 add_static_mapping_command_fn (vlib_main_t * vm,
1630 unformat_input_t * input,
1631 vlib_cli_command_t * cmd)
1633 unformat_input_t _line_input, *line_input = &_line_input;
1634 clib_error_t * error = 0;
1635 ip4_address_t l_addr, e_addr;
1636 u32 l_port = 0, e_port = 0, vrf_id = ~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, "local %U %u", unformat_ip4_address, &l_addr,
1650 else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1652 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1655 else if (unformat (line_input, "external %U", unformat_ip4_address,
1658 else if (unformat (line_input, "vrf %u", &vrf_id))
1660 else if (unformat (line_input, "del"))
1663 return clib_error_return (0, "unknown input: '%U'",
1664 format_unformat_error, line_input);
1666 unformat_free (line_input);
1668 rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
1669 vrf_id, addr_only, is_add);
1673 case VNET_API_ERROR_INVALID_VALUE:
1674 return clib_error_return (0, "External port already in use.");
1676 case VNET_API_ERROR_NO_SUCH_ENTRY:
1678 return clib_error_return (0, "External addres must be allocated.");
1680 return clib_error_return (0, "Mapping not exist.");
1682 case VNET_API_ERROR_NO_SUCH_FIB:
1683 return clib_error_return (0, "No such VRF id.");
1684 case VNET_API_ERROR_VALUE_EXIST:
1685 return clib_error_return (0, "Mapping already exist.");
1695 * @cliexstart{snat add static mapping}
1696 * Static mapping allows hosts on the external network to initiate connection
1697 * to to the local network host.
1698 * To create static mapping between local host address 10.0.0.3 port 6303 and
1699 * external address 4.4.4.4 port 3606 use:
1700 * vpp# snat add static mapping local 10.0.0.3 6303 external 4.4.4.4 3606
1701 * If not runnig "static mapping only" S-NAT plugin mode use before:
1702 * vpp# snat add address 4.4.4.4
1703 * To create static mapping between local and external address use:
1704 * vpp# snat add static mapping local 10.0.0.3 external 4.4.4.4
1707 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1708 .path = "snat add static mapping",
1709 .function = add_static_mapping_command_fn,
1711 "snat add static mapping local <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
1714 static clib_error_t *
1715 set_workers_command_fn (vlib_main_t * vm,
1716 unformat_input_t * input,
1717 vlib_cli_command_t * cmd)
1719 unformat_input_t _line_input, *line_input = &_line_input;
1723 /* Get a line of input. */
1724 if (!unformat_user (input, unformat_line_input, line_input))
1727 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1729 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
1732 return clib_error_return (0, "unknown input '%U'",
1733 format_unformat_error, input);
1735 unformat_free (line_input);
1738 return clib_error_return (0, "List of workers must be specified.");
1740 rv = snat_set_workers(bitmap);
1742 clib_bitmap_free (bitmap);
1746 case VNET_API_ERROR_INVALID_WORKER:
1747 return clib_error_return (0, "Invalid worker(s).");
1749 case VNET_API_ERROR_FEATURE_DISABLED:
1750 return clib_error_return (0,
1751 "Supported only if 2 or more workes available.");
1762 * @cliexstart{set snat workers}
1763 * Set SNAT workers if 2 or more workers available, use:
1764 * vpp# set snat workers 0-2,5
1767 VLIB_CLI_COMMAND (set_workers_command, static) = {
1768 .path = "set snat workers",
1769 .function = set_workers_command_fn,
1771 "set snat workers <workers-list>",
1774 static clib_error_t *
1775 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
1776 unformat_input_t * input,
1777 vlib_cli_command_t * cmd)
1779 unformat_input_t _line_input, *line_input = &_line_input;
1785 /* Get a line of input. */
1786 if (!unformat_user (input, unformat_line_input, line_input))
1789 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1791 if (unformat (line_input, "domain %d", &domain_id))
1793 else if (unformat (line_input, "src-port %d", &src_port))
1795 else if (unformat (line_input, "disable"))
1798 return clib_error_return (0, "unknown input '%U'",
1799 format_unformat_error, input);
1801 unformat_free (line_input);
1803 rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
1806 return clib_error_return (0, "ipfix logging enable failed");
1813 * @cliexstart{snat ipfix logging}
1814 * To enable SNAT IPFIX logging use:
1815 * vpp# snat ipfix logging
1816 * To set IPFIX exporter use:
1817 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1820 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1821 .path = "snat ipfix logging",
1822 .function = snat_ipfix_logging_enable_disable_command_fn,
1823 .short_help = "snat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
1826 static clib_error_t *
1827 snat_config (vlib_main_t * vm, unformat_input_t * input)
1829 snat_main_t * sm = &snat_main;
1830 u32 translation_buckets = 1024;
1831 u32 translation_memory_size = 128<<20;
1832 u32 user_buckets = 128;
1833 u32 user_memory_size = 64<<20;
1834 u32 max_translations_per_user = 100;
1835 u32 outside_vrf_id = 0;
1836 u32 inside_vrf_id = 0;
1837 u32 static_mapping_buckets = 1024;
1838 u32 static_mapping_memory_size = 64<<20;
1839 u8 static_mapping_only = 0;
1840 u8 static_mapping_connection_tracking = 0;
1841 vlib_thread_main_t *tm = vlib_get_thread_main ();
1843 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1845 if (unformat (input, "translation hash buckets %d", &translation_buckets))
1847 else if (unformat (input, "translation hash memory %d",
1848 &translation_memory_size));
1849 else if (unformat (input, "user hash buckets %d", &user_buckets))
1851 else if (unformat (input, "user hash memory %d",
1854 else if (unformat (input, "max translations per user %d",
1855 &max_translations_per_user))
1857 else if (unformat (input, "outside VRF id %d",
1860 else if (unformat (input, "inside VRF id %d",
1863 else if (unformat (input, "static mapping only"))
1865 static_mapping_only = 1;
1866 if (unformat (input, "connection tracking"))
1867 static_mapping_connection_tracking = 1;
1870 return clib_error_return (0, "unknown input '%U'",
1871 format_unformat_error, input);
1874 /* for show commands, etc. */
1875 sm->translation_buckets = translation_buckets;
1876 sm->translation_memory_size = translation_memory_size;
1877 sm->user_buckets = user_buckets;
1878 sm->user_memory_size = user_memory_size;
1879 sm->max_translations_per_user = max_translations_per_user;
1880 sm->outside_vrf_id = outside_vrf_id;
1881 sm->outside_fib_index = ~0;
1882 sm->inside_vrf_id = inside_vrf_id;
1883 sm->inside_fib_index = ~0;
1884 sm->static_mapping_only = static_mapping_only;
1885 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
1887 if (!static_mapping_only ||
1888 (static_mapping_only && static_mapping_connection_tracking))
1890 clib_bihash_init_8_8 (&sm->worker_by_in, "worker-by-in", user_buckets,
1893 clib_bihash_init_8_8 (&sm->worker_by_out, "worker-by-out", user_buckets,
1896 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1898 clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
1899 translation_memory_size);
1901 clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
1902 translation_memory_size);
1904 clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
1907 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
1908 "static_mapping_by_local", static_mapping_buckets,
1909 static_mapping_memory_size);
1911 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
1912 "static_mapping_by_external", static_mapping_buckets,
1913 static_mapping_memory_size);
1917 VLIB_CONFIG_FUNCTION (snat_config, "snat");
1919 u8 * format_snat_key (u8 * s, va_list * args)
1921 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
1922 char * protocol_string = "unknown";
1923 static char *protocol_strings[] = {
1929 if (key->protocol < ARRAY_LEN(protocol_strings))
1930 protocol_string = protocol_strings[key->protocol];
1932 s = format (s, "%U proto %s port %d fib %d",
1933 format_ip4_address, &key->addr, protocol_string,
1934 clib_net_to_host_u16 (key->port), key->fib_index);
1938 u8 * format_snat_session (u8 * s, va_list * args)
1940 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
1941 snat_session_t * sess = va_arg (*args, snat_session_t *);
1943 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
1944 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
1945 s = format (s, " last heard %.2f\n", sess->last_heard);
1946 s = format (s, " total pkts %d, total bytes %lld\n",
1947 sess->total_pkts, sess->total_bytes);
1948 if (snat_is_session_static (sess))
1949 s = format (s, " static translation\n");
1951 s = format (s, " dynamic translation\n");
1956 u8 * format_snat_user (u8 * s, va_list * args)
1958 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
1959 snat_user_t * u = va_arg (*args, snat_user_t *);
1960 int verbose = va_arg (*args, int);
1961 dlist_elt_t * head, * elt;
1962 u32 elt_index, head_index;
1964 snat_session_t * sess;
1966 s = format (s, "%U: %d dynamic translations, %d static translations\n",
1967 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
1972 if (u->nsessions || u->nstaticsessions)
1974 head_index = u->sessions_per_user_list_head_index;
1975 head = pool_elt_at_index (sm->list_pool, head_index);
1977 elt_index = head->next;
1978 elt = pool_elt_at_index (sm->list_pool, elt_index);
1979 session_index = elt->value;
1981 while (session_index != ~0)
1983 sess = pool_elt_at_index (sm->sessions, session_index);
1985 s = format (s, " %U\n", format_snat_session, sm, sess);
1987 elt_index = elt->next;
1988 elt = pool_elt_at_index (sm->list_pool, elt_index);
1989 session_index = elt->value;
1996 u8 * format_snat_static_mapping (u8 * s, va_list * args)
1998 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
2001 s = format (s, "local %U external %U vrf %d",
2002 format_ip4_address, &m->local_addr,
2003 format_ip4_address, &m->external_addr,
2006 s = format (s, "local %U:%d external %U:%d vrf %d",
2007 format_ip4_address, &m->local_addr, m->local_port,
2008 format_ip4_address, &m->external_addr, m->external_port,
2014 static clib_error_t *
2015 show_snat_command_fn (vlib_main_t * vm,
2016 unformat_input_t * input,
2017 vlib_cli_command_t * cmd)
2020 snat_main_t * sm = &snat_main;
2022 snat_static_mapping_t *m;
2023 snat_interface_t *i;
2024 snat_address_t * ap;
2025 vnet_main_t *vnm = vnet_get_main();
2026 snat_main_per_thread_data_t *tsm;
2027 u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
2030 if (unformat (input, "detail"))
2032 else if (unformat (input, "verbose"))
2035 if (sm->static_mapping_only)
2037 if (sm->static_mapping_connection_tracking)
2038 vlib_cli_output (vm, "SNAT mode: static mapping only connection "
2041 vlib_cli_output (vm, "SNAT mode: static mapping only");
2045 vlib_cli_output (vm, "SNAT mode: dynamic translations enabled");
2050 pool_foreach (i, sm->interfaces,
2052 vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
2053 vnet_get_sw_interface (vnm, i->sw_if_index),
2054 i->is_inside ? "in" : "out");
2057 if (vec_len (sm->auto_add_sw_if_indices))
2059 vlib_cli_output (vm, "SNAT pool addresses interfaces:");
2060 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
2062 vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
2063 vnet_get_sw_interface (vnm, *sw_if_index));
2067 vec_foreach (ap, sm->addresses)
2069 u8 * s = format (0, "");
2070 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
2071 clib_bitmap_foreach (j, ap->busy_port_bitmap,
2073 s = format (s, " %d", j);
2075 vlib_cli_output (vm, " %d busy ports:%s", ap->busy_ports, s);
2079 if (sm->num_workers > 1)
2081 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
2084 vec_foreach (worker, sm->workers)
2086 vlib_worker_thread_t *w =
2087 vlib_worker_threads + *worker + sm->first_worker_index;
2088 vlib_cli_output (vm, " %s", w->name);
2093 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2095 vlib_cli_output (vm, "%d static mappings",
2096 pool_elts (sm->static_mappings));
2100 pool_foreach (m, sm->static_mappings,
2102 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2108 vec_foreach (tsm, sm->per_thread_data)
2110 users_num += pool_elts (tsm->users);
2111 sessions_num += pool_elts (tsm->sessions);
2114 vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
2115 " %d static mappings",
2117 vec_len (sm->addresses),
2119 pool_elts (sm->static_mappings));
2123 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
2125 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
2127 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_in,
2129 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_out,
2131 vec_foreach_index (j, sm->per_thread_data)
2133 tsm = vec_elt_at_index (sm->per_thread_data, j);
2135 if (pool_elts (tsm->users) == 0)
2138 vlib_worker_thread_t *w = vlib_worker_threads + j;
2139 vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
2141 vlib_cli_output (vm, " %d list pool elements",
2142 pool_elts (tsm->list_pool));
2144 pool_foreach (u, tsm->users,
2146 vlib_cli_output (vm, " %U", format_snat_user, tsm, u,
2151 if (pool_elts (sm->static_mappings))
2153 vlib_cli_output (vm, "static mappings:");
2154 pool_foreach (m, sm->static_mappings,
2156 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2165 VLIB_CLI_COMMAND (show_snat_command, static) = {
2166 .path = "show snat",
2167 .short_help = "show snat",
2168 .function = show_snat_command_fn,
2173 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2176 ip4_address_t * address,
2178 u32 if_address_index,
2181 snat_main_t *sm = &snat_main;
2184 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2186 if (sw_if_index == sm->auto_add_sw_if_indices[i])
2190 /* Don't trip over lease renewal, static config */
2191 for (j = 0; j < vec_len(sm->addresses); j++)
2192 if (sm->addresses[j].addr.as_u32 == address->as_u32)
2195 snat_add_address (sm, address);
2200 (void) snat_del_address(sm, address[0]);
2208 static int snat_add_interface_address (snat_main_t *sm,
2212 ip4_main_t * ip4_main = sm->ip4_main;
2213 ip4_address_t * first_int_addr;
2216 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2217 0 /* just want the address*/);
2219 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2221 if (sm->auto_add_sw_if_indices[i] == sw_if_index)
2225 /* if have address remove it */
2227 (void) snat_del_address (sm, first_int_addr[0]);
2228 vec_del1(sm->auto_add_sw_if_indices, i);
2231 return VNET_API_ERROR_VALUE_EXIST;
2238 return VNET_API_ERROR_NO_SUCH_ENTRY;
2240 /* add to the auto-address list */
2241 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
2243 /* If the address is already bound - or static - add it now */
2245 snat_add_address (sm, first_int_addr);
2250 static clib_error_t *
2251 snat_add_interface_address_command_fn (vlib_main_t * vm,
2252 unformat_input_t * input,
2253 vlib_cli_command_t * cmd)
2255 snat_main_t *sm = &snat_main;
2256 unformat_input_t _line_input, *line_input = &_line_input;
2261 /* Get a line of input. */
2262 if (!unformat_user (input, unformat_line_input, line_input))
2265 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2267 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
2268 sm->vnet_main, &sw_if_index))
2270 else if (unformat (line_input, "del"))
2273 return clib_error_return (0, "unknown input '%U'",
2274 format_unformat_error, line_input);
2277 rv = snat_add_interface_address (sm, sw_if_index, is_del);
2285 return clib_error_return (0, "snat_add_interface_address returned %d",
2291 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2292 .path = "snat add interface address",
2293 .short_help = "snat add interface address <interface> [del]",
2294 .function = snat_add_interface_address_command_fn,