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>
25 #include <vnet/fib/fib_table.h>
26 #include <vnet/fib/ip4_fib.h>
28 #include <vlibapi/api.h>
29 #include <vlibmemory/api.h>
30 #include <vlibsocket/api.h>
32 snat_main_t snat_main;
34 /* define message IDs */
35 #include <snat/snat_msg_enum.h>
37 /* define message structures */
39 #include <snat/snat_all_api_h.h>
42 /* define generated endian-swappers */
44 #include <snat/snat_all_api_h.h>
47 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <snat/snat_all_api_h.h>
54 /* Macro to finish up custom dump fns */
57 vl_print (handle, (char *)s); \
62 * A handy macro to set up a message reply.
63 * Assumes that the following variables are available:
64 * mp - pointer to request message
65 * rmp - pointer to reply message type
69 #define REPLY_MACRO(t) \
71 unix_shared_memory_queue_t * q = \
72 vl_api_client_index_to_input_queue (mp->client_index); \
76 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
77 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
78 rmp->context = mp->context; \
79 rmp->retval = ntohl(rv); \
81 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
84 #define REPLY_MACRO2(t, body) \
86 unix_shared_memory_queue_t * q = \
87 vl_api_client_index_to_input_queue (mp->client_index); \
91 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
92 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
93 rmp->context = mp->context; \
94 rmp->retval = ntohl(rv); \
95 do {body;} while (0); \
96 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
100 /* Hook up input features */
101 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
102 .arc_name = "ip4-unicast",
103 .node_name = "snat-in2out",
104 .runs_before = VNET_FEATURES ("snat-out2in"),
106 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
107 .arc_name = "ip4-unicast",
108 .node_name = "snat-out2in",
109 .runs_before = VNET_FEATURES ("ip4-lookup"),
111 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
112 .arc_name = "ip4-unicast",
113 .node_name = "snat-in2out-worker-handoff",
114 .runs_before = VNET_FEATURES ("snat-out2in-worker-handoff"),
116 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
117 .arc_name = "ip4-unicast",
118 .node_name = "snat-out2in-worker-handoff",
119 .runs_before = VNET_FEATURES ("ip4-lookup"),
121 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
122 .arc_name = "ip4-unicast",
123 .node_name = "snat-in2out-fast",
124 .runs_before = VNET_FEATURES ("snat-out2in-fast"),
126 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
127 .arc_name = "ip4-unicast",
128 .node_name = "snat-out2in-fast",
129 .runs_before = VNET_FEATURES ("ip4-lookup"),
134 * This routine exists to convince the vlib plugin framework that
135 * we haven't accidentally copied a random .dll into the plugin directory.
137 * Also collects global variable pointers passed from the vpp engine
141 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
144 snat_main_t * sm = &snat_main;
145 clib_error_t * error = 0;
148 sm->vnet_main = h->vnet_main;
149 sm->ethernet_main = h->ethernet_main;
154 /*$$$$$ move to an installed header file */
155 #if (1 || CLIB_DEBUG > 0) /* "trust, but verify" */
157 #define VALIDATE_SW_IF_INDEX(mp) \
158 do { u32 __sw_if_index = ntohl(mp->sw_if_index); \
159 vnet_main_t *__vnm = vnet_get_main(); \
160 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
162 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
163 goto bad_sw_if_index; \
167 #define BAD_SW_IF_INDEX_LABEL \
173 #define VALIDATE_RX_SW_IF_INDEX(mp) \
174 do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index); \
175 vnet_main_t *__vnm = vnet_get_main(); \
176 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
177 __rx_sw_if_index)) { \
178 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
179 goto bad_rx_sw_if_index; \
183 #define BAD_RX_SW_IF_INDEX_LABEL \
185 bad_rx_sw_if_index: \
189 #define VALIDATE_TX_SW_IF_INDEX(mp) \
190 do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index); \
191 vnet_main_t *__vnm = vnet_get_main(); \
192 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
193 __tx_sw_if_index)) { \
194 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
195 goto bad_tx_sw_if_index; \
199 #define BAD_TX_SW_IF_INDEX_LABEL \
201 bad_tx_sw_if_index: \
207 #define VALIDATE_SW_IF_INDEX(mp)
208 #define BAD_SW_IF_INDEX_LABEL
209 #define VALIDATE_RX_SW_IF_INDEX(mp)
210 #define BAD_RX_SW_IF_INDEX_LABEL
211 #define VALIDATE_TX_SW_IF_INDEX(mp)
212 #define BAD_TX_SW_IF_INDEX_LABEL
214 #endif /* CLIB_DEBUG > 0 */
217 * @brief Add/del NAT address to FIB.
219 * Add the external NAT address to the FIB as receive entries. This ensures
220 * that VPP will reply to ARP for this address and we don't need to enable
221 * proxy ARP on the outside interface.
223 * @param sm SNAT main
224 * @param addr IPv4 address.
225 * @param sw_if_index Interface.
226 * @param is_add If 0 delete, otherwise add.
229 snat_add_del_addr_to_fib (snat_main_t *sm,
230 ip4_address_t * addr,
234 ip4_main_t * ip4_main = sm->ip4_main;
235 ip4_address_t * first_int_addr;
236 fib_prefix_t prefix = {
238 .fp_proto = FIB_PROTOCOL_IP4,
240 .ip4.as_u32 = addr->as_u32,
243 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
245 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0);
248 if (first_int_addr->as_u32 == addr->as_u32)
253 fib_table_entry_update_one_path(fib_index,
255 FIB_SOURCE_INTERFACE,
256 (FIB_ENTRY_FLAG_CONNECTED |
257 FIB_ENTRY_FLAG_LOCAL |
258 FIB_ENTRY_FLAG_EXCLUSIVE),
265 FIB_ROUTE_PATH_FLAG_NONE);
267 fib_table_entry_delete(fib_index,
269 FIB_SOURCE_INTERFACE);
272 void snat_add_address (snat_main_t *sm, ip4_address_t *addr)
277 /* Check if address already exists */
278 vec_foreach (ap, sm->addresses)
280 if (ap->addr.as_u32 == addr->as_u32)
284 vec_add2 (sm->addresses, ap, 1);
286 clib_bitmap_alloc (ap->busy_port_bitmap, 65535);
288 /* Add external address to FIB */
289 pool_foreach (i, sm->interfaces,
294 snat_add_del_addr_to_fib(sm, addr, i->sw_if_index, 1);
298 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
301 snat_static_mapping_t *m;
302 pool_foreach (m, sm->static_mappings,
304 if (m->external_addr.as_u32 == addr.as_u32)
311 static void increment_v4_address (ip4_address_t * a)
315 v = clib_net_to_host_u32(a->as_u32) + 1;
316 a->as_u32 = clib_host_to_net_u32(v);
320 snat_add_static_mapping_when_resolved (snat_main_t * sm,
321 ip4_address_t l_addr,
329 snat_static_map_resolve_t *rp;
331 vec_add2 (sm->to_resolve, rp, 1);
332 rp->l_addr.as_u32 = l_addr.as_u32;
334 rp->sw_if_index = sw_if_index;
337 rp->addr_only = addr_only;
342 * @brief Add static mapping.
344 * Create static mapping between local addr+port and external addr+port.
346 * @param l_addr Local IPv4 address.
347 * @param e_addr External IPv4 address.
348 * @param l_port Local port number.
349 * @param e_port External port number.
350 * @param vrf_id VRF ID.
351 * @param addr_only If 0 address port and pair mapping, otherwise address only.
352 * @param sw_if_index External port instead of specific IP address.
353 * @param is_add If 0 delete static mapping, otherwise add.
357 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
358 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
359 u32 sw_if_index, int is_add)
361 snat_main_t * sm = &snat_main;
362 snat_static_mapping_t *m;
363 snat_static_mapping_key_t m_key;
364 clib_bihash_kv_8_8_t kv, value;
365 snat_address_t *a = 0;
368 snat_interface_t *interface;
371 /* If outside FIB index is not resolved yet */
372 if (sm->outside_fib_index == ~0)
374 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->outside_vrf_id);
376 return VNET_API_ERROR_NO_SUCH_FIB;
377 sm->outside_fib_index = p[0];
380 /* If the external address is a specific interface address */
381 if (sw_if_index != ~0)
383 ip4_address_t * first_int_addr;
385 /* Might be already set... */
386 first_int_addr = ip4_interface_first_address
387 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
389 /* DHCP resolution required? */
390 if (first_int_addr == 0)
392 snat_add_static_mapping_when_resolved
393 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id,
398 e_addr.as_u32 = first_int_addr->as_u32;
402 m_key.port = addr_only ? 0 : e_port;
403 m_key.fib_index = sm->outside_fib_index;
404 kv.key = m_key.as_u64;
405 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
408 m = pool_elt_at_index (sm->static_mappings, value.value);
413 return VNET_API_ERROR_VALUE_EXIST;
415 /* Convert VRF id to FIB index */
418 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
420 return VNET_API_ERROR_NO_SUCH_FIB;
423 /* If not specified use inside VRF id from SNAT plugin startup config */
426 if (sm->inside_fib_index == ~0)
428 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->inside_vrf_id);
430 return VNET_API_ERROR_NO_SUCH_FIB;
432 sm->inside_fib_index = fib_index;
435 fib_index = sm->inside_fib_index;
437 vrf_id = sm->inside_vrf_id;
440 /* Find external address in allocated addresses and reserve port for
441 address and port pair mapping when dynamic translations enabled */
442 if (!addr_only && !(sm->static_mapping_only))
444 for (i = 0; i < vec_len (sm->addresses); i++)
446 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
448 a = sm->addresses + i;
449 /* External port must be unused */
450 if (clib_bitmap_get_no_check (a->busy_port_bitmap, e_port))
451 return VNET_API_ERROR_INVALID_VALUE;
452 clib_bitmap_set_no_check (a->busy_port_bitmap, e_port, 1);
459 /* External address must be allocated */
461 return VNET_API_ERROR_NO_SUCH_ENTRY;
464 pool_get (sm->static_mappings, m);
465 memset (m, 0, sizeof (*m));
466 m->local_addr = l_addr;
467 m->external_addr = e_addr;
468 m->addr_only = addr_only;
470 m->fib_index = fib_index;
473 m->local_port = l_port;
474 m->external_port = e_port;
477 m_key.addr = m->local_addr;
478 m_key.port = m->local_port;
479 m_key.fib_index = m->fib_index;
480 kv.key = m_key.as_u64;
481 kv.value = m - sm->static_mappings;
482 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
484 m_key.addr = m->external_addr;
485 m_key.port = m->external_port;
486 m_key.fib_index = sm->outside_fib_index;
487 kv.key = m_key.as_u64;
488 kv.value = m - sm->static_mappings;
489 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
494 snat_user_key_t w_key0;
495 snat_static_mapping_key_t w_key1;
497 w_key0.addr = m->local_addr;
498 w_key0.fib_index = m->fib_index;
499 kv.key = w_key0.as_u64;
501 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
503 kv.value = sm->first_worker_index +
504 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
506 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv, 1);
510 kv.value = value.value;
513 w_key1.addr = m->external_addr;
514 w_key1.port = clib_host_to_net_u16 (m->external_port);
515 w_key1.fib_index = sm->outside_fib_index;
516 kv.key = w_key1.as_u64;
517 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv, 1);
523 return VNET_API_ERROR_NO_SUCH_ENTRY;
525 /* Free external address port */
526 if (!addr_only && !(sm->static_mapping_only))
528 for (i = 0; i < vec_len (sm->addresses); i++)
530 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
532 a = sm->addresses + i;
533 clib_bitmap_set_no_check (a->busy_port_bitmap, e_port, 0);
541 m_key.addr = m->local_addr;
542 m_key.port = m->local_port;
543 m_key.fib_index = m->fib_index;
544 kv.key = m_key.as_u64;
545 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
547 m_key.addr = m->external_addr;
548 m_key.port = m->external_port;
549 m_key.fib_index = sm->outside_fib_index;
550 kv.key = m_key.as_u64;
551 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
553 /* Delete session(s) for static mapping if exist */
554 if (!(sm->static_mapping_only) ||
555 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
557 snat_user_key_t u_key;
559 dlist_elt_t * head, * elt;
560 u32 elt_index, head_index, del_elt_index;
564 snat_main_per_thread_data_t *tsm;
566 u_key.addr = m->local_addr;
567 u_key.fib_index = m->fib_index;
568 kv.key = u_key.as_u64;
569 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
571 user_index = value.value;
572 if (!clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
573 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
575 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
576 u = pool_elt_at_index (tsm->users, user_index);
577 if (u->nstaticsessions)
579 head_index = u->sessions_per_user_list_head_index;
580 head = pool_elt_at_index (tsm->list_pool, head_index);
581 elt_index = head->next;
582 elt = pool_elt_at_index (tsm->list_pool, elt_index);
583 ses_index = elt->value;
584 while (ses_index != ~0)
586 s = pool_elt_at_index (tsm->sessions, ses_index);
587 del_elt_index = elt_index;
588 elt_index = elt->next;
589 elt = pool_elt_at_index (tsm->list_pool, elt_index);
590 ses_index = elt->value;
594 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
595 (clib_net_to_host_u16 (s->out2in.port) != e_port))
600 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
601 s->out2in.addr.as_u32,
605 s->in2out.fib_index);
607 value.key = s->in2out.as_u64;
608 clib_bihash_add_del_8_8 (&sm->in2out, &value, 0);
609 value.key = s->out2in.as_u64;
610 clib_bihash_add_del_8_8 (&sm->out2in, &value, 0);
611 pool_put (tsm->sessions, s);
613 clib_dlist_remove (tsm->list_pool, del_elt_index);
614 pool_put_index (tsm->list_pool, del_elt_index);
615 u->nstaticsessions--;
622 pool_put (tsm->users, u);
623 clib_bihash_add_del_8_8 (&sm->user_hash, &kv, 0);
629 /* Delete static mapping from pool */
630 pool_put (sm->static_mappings, m);
636 /* Add/delete external address to FIB */
637 pool_foreach (interface, sm->interfaces,
639 if (interface->is_inside)
642 snat_add_del_addr_to_fib(sm, &e_addr, interface->sw_if_index, is_add);
648 int snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm)
650 snat_address_t *a = 0;
652 u32 *ses_to_be_removed = 0, *ses_index;
653 clib_bihash_kv_8_8_t kv, value;
654 snat_user_key_t user_key;
656 snat_main_per_thread_data_t *tsm;
657 snat_static_mapping_t *m;
658 snat_interface_t *interface;
661 /* Find SNAT address */
662 for (i=0; i < vec_len (sm->addresses); i++)
664 if (sm->addresses[i].addr.as_u32 == addr.as_u32)
666 a = sm->addresses + i;
671 return VNET_API_ERROR_NO_SUCH_ENTRY;
675 pool_foreach (m, sm->static_mappings,
677 if (m->external_addr.as_u32 == addr.as_u32)
678 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
679 m->local_port, m->external_port,
680 m->vrf_id, m->addr_only, ~0, 0);
685 /* Check if address is used in some static mapping */
686 if (is_snat_address_used_in_static_mapping(sm, addr))
688 clib_warning ("address used in static mapping");
689 return VNET_API_ERROR_UNSPECIFIED;
693 /* Delete sessions using address */
696 vec_foreach (tsm, sm->per_thread_data)
698 pool_foreach (ses, tsm->sessions, ({
699 if (ses->out2in.addr.as_u32 == addr.as_u32)
702 snat_ipfix_logging_nat44_ses_delete(ses->in2out.addr.as_u32,
703 ses->out2in.addr.as_u32,
704 ses->in2out.protocol,
707 ses->in2out.fib_index);
708 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
709 kv.key = ses->in2out.as_u64;
710 clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
711 kv.key = ses->out2in.as_u64;
712 clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
713 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
714 user_key.addr = ses->in2out.addr;
715 user_key.fib_index = ses->in2out.fib_index;
716 kv.key = user_key.as_u64;
717 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
719 u = pool_elt_at_index (tsm->users, value.value);
725 vec_foreach (ses_index, ses_to_be_removed)
726 pool_put_index (tsm->sessions, ses_index[0]);
728 vec_free (ses_to_be_removed);
732 vec_del1 (sm->addresses, i);
734 /* Delete external address from FIB */
735 pool_foreach (interface, sm->interfaces,
737 if (interface->is_inside)
740 snat_add_del_addr_to_fib(sm, &addr, interface->sw_if_index, 0);
746 static int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
748 snat_main_t *sm = &snat_main;
750 const char * feature_name;
752 snat_static_mapping_t * m;
754 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
755 feature_name = is_inside ? "snat-in2out-fast" : "snat-out2in-fast";
758 if (sm->num_workers > 1)
759 feature_name = is_inside ? "snat-in2out-worker-handoff" : "snat-out2in-worker-handoff";
761 feature_name = is_inside ? "snat-in2out" : "snat-out2in";
764 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
767 if (sm->fq_in2out_index == ~0)
768 sm->fq_in2out_index = vlib_frame_queue_main_init (snat_in2out_node.index, 0);
770 if (sm->fq_out2in_index == ~0)
771 sm->fq_out2in_index = vlib_frame_queue_main_init (snat_out2in_node.index, 0);
773 pool_foreach (i, sm->interfaces,
775 if (i->sw_if_index == sw_if_index)
778 pool_put (sm->interfaces, i);
780 return VNET_API_ERROR_VALUE_EXIST;
787 return VNET_API_ERROR_NO_SUCH_ENTRY;
789 pool_get (sm->interfaces, i);
790 i->sw_if_index = sw_if_index;
791 i->is_inside = is_inside;
793 /* Add/delete external addresses to FIB */
798 vec_foreach (ap, sm->addresses)
799 snat_add_del_addr_to_fib(sm, &ap->addr, sw_if_index, !is_del);
801 pool_foreach (m, sm->static_mappings,
806 snat_add_del_addr_to_fib(sm, &m->external_addr, sw_if_index, !is_del);
812 static int snat_set_workers (uword * bitmap)
814 snat_main_t *sm = &snat_main;
817 if (sm->num_workers < 2)
818 return VNET_API_ERROR_FEATURE_DISABLED;
820 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
821 return VNET_API_ERROR_INVALID_WORKER;
823 vec_free (sm->workers);
824 clib_bitmap_foreach (i, bitmap,
826 vec_add1(sm->workers, i);
833 vl_api_snat_add_address_range_t_handler
834 (vl_api_snat_add_address_range_t * mp)
836 snat_main_t * sm = &snat_main;
837 vl_api_snat_add_address_range_reply_t * rmp;
838 ip4_address_t this_addr;
839 u32 start_host_order, end_host_order;
846 rv = VNET_API_ERROR_UNIMPLEMENTED;
850 if (sm->static_mapping_only)
852 rv = VNET_API_ERROR_FEATURE_DISABLED;
856 tmp = (u32 *) mp->first_ip_address;
857 start_host_order = clib_host_to_net_u32 (tmp[0]);
858 tmp = (u32 *) mp->last_ip_address;
859 end_host_order = clib_host_to_net_u32 (tmp[0]);
861 count = (end_host_order - start_host_order) + 1;
864 clib_warning ("%U - %U, %d addresses...",
865 format_ip4_address, mp->first_ip_address,
866 format_ip4_address, mp->last_ip_address,
869 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
871 for (i = 0; i < count; i++)
874 snat_add_address (sm, &this_addr);
876 rv = snat_del_address (sm, this_addr, 0);
881 increment_v4_address (&this_addr);
885 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
888 static void *vl_api_snat_add_address_range_t_print
889 (vl_api_snat_add_address_range_t *mp, void * handle)
893 s = format (0, "SCRIPT: snat_add_address_range ");
894 s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
895 if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
897 s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
903 send_snat_address_details
904 (snat_address_t * a, unix_shared_memory_queue_t * q, u32 context)
906 vl_api_snat_address_details_t *rmp;
907 snat_main_t * sm = &snat_main;
909 rmp = vl_msg_api_alloc (sizeof (*rmp));
910 memset (rmp, 0, sizeof (*rmp));
911 rmp->_vl_msg_id = ntohs (VL_API_SNAT_ADDRESS_DETAILS+sm->msg_id_base);
913 clib_memcpy (rmp->ip_address, &(a->addr), 4);
914 rmp->context = context;
916 vl_msg_api_send_shmem (q, (u8 *) & rmp);
920 vl_api_snat_address_dump_t_handler
921 (vl_api_snat_address_dump_t * mp)
923 unix_shared_memory_queue_t *q;
924 snat_main_t * sm = &snat_main;
927 q = vl_api_client_index_to_input_queue (mp->client_index);
931 vec_foreach (a, sm->addresses)
932 send_snat_address_details (a, q, mp->context);
935 static void *vl_api_snat_address_dump_t_print
936 (vl_api_snat_address_dump_t *mp, void * handle)
940 s = format (0, "SCRIPT: snat_address_dump ");
946 vl_api_snat_interface_add_del_feature_t_handler
947 (vl_api_snat_interface_add_del_feature_t * mp)
949 snat_main_t * sm = &snat_main;
950 vl_api_snat_interface_add_del_feature_reply_t * rmp;
951 u8 is_del = mp->is_add == 0;
952 u32 sw_if_index = ntohl(mp->sw_if_index);
955 VALIDATE_SW_IF_INDEX(mp);
957 rv = snat_interface_add_del (sw_if_index, mp->is_inside, is_del);
959 BAD_SW_IF_INDEX_LABEL;
961 REPLY_MACRO(VL_API_SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY);
964 static void *vl_api_snat_interface_add_del_feature_t_print
965 (vl_api_snat_interface_add_del_feature_t * mp, void *handle)
969 s = format (0, "SCRIPT: snat_interface_add_del_feature ");
970 s = format (s, "sw_if_index %d %s %s",
971 clib_host_to_net_u32(mp->sw_if_index),
972 mp->is_inside ? "in":"out",
973 mp->is_add ? "" : "del");
979 send_snat_interface_details
980 (snat_interface_t * i, unix_shared_memory_queue_t * q, u32 context)
982 vl_api_snat_interface_details_t *rmp;
983 snat_main_t * sm = &snat_main;
985 rmp = vl_msg_api_alloc (sizeof (*rmp));
986 memset (rmp, 0, sizeof (*rmp));
987 rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_DETAILS+sm->msg_id_base);
988 rmp->sw_if_index = ntohl (i->sw_if_index);
989 rmp->is_inside = i->is_inside;
990 rmp->context = context;
992 vl_msg_api_send_shmem (q, (u8 *) & rmp);
996 vl_api_snat_interface_dump_t_handler
997 (vl_api_snat_interface_dump_t * mp)
999 unix_shared_memory_queue_t *q;
1000 snat_main_t * sm = &snat_main;
1001 snat_interface_t * i;
1003 q = vl_api_client_index_to_input_queue (mp->client_index);
1007 pool_foreach (i, sm->interfaces,
1009 send_snat_interface_details(i, q, mp->context);
1013 static void *vl_api_snat_interface_dump_t_print
1014 (vl_api_snat_interface_dump_t *mp, void * handle)
1018 s = format (0, "SCRIPT: snat_interface_dump ");
1023 vl_api_snat_add_static_mapping_t_handler
1024 (vl_api_snat_add_static_mapping_t * mp)
1026 snat_main_t * sm = &snat_main;
1027 vl_api_snat_add_static_mapping_reply_t * rmp;
1028 ip4_address_t local_addr, external_addr;
1029 u16 local_port = 0, external_port = 0;
1030 u32 vrf_id, external_sw_if_index;
1033 if (mp->is_ip4 != 1)
1035 rv = VNET_API_ERROR_UNIMPLEMENTED;
1039 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
1040 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
1041 if (mp->addr_only == 0)
1043 local_port = clib_net_to_host_u16 (mp->local_port);
1044 external_port = clib_net_to_host_u16 (mp->external_port);
1046 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1047 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
1049 rv = snat_add_static_mapping(local_addr, external_addr, local_port,
1050 external_port, vrf_id, mp->addr_only,
1051 external_sw_if_index,
1055 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
1058 static void *vl_api_snat_add_static_mapping_t_print
1059 (vl_api_snat_add_static_mapping_t *mp, void * handle)
1063 s = format (0, "SCRIPT: snat_add_static_mapping ");
1064 s = format (s, "local_addr %U external_addr %U ",
1065 format_ip4_address, mp->local_ip_address,
1066 format_ip4_address, mp->external_ip_address);
1068 if (mp->addr_only == 0)
1069 s = format (s, "local_port %d external_port %d ",
1070 clib_net_to_host_u16 (mp->local_port),
1071 clib_net_to_host_u16 (mp->external_port));
1073 if (mp->vrf_id != ~0)
1074 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1076 if (mp->external_sw_if_index != ~0)
1077 s = format (s, "external_sw_if_index %d",
1078 clib_net_to_host_u32 (mp->external_sw_if_index));
1083 send_snat_static_mapping_details
1084 (snat_static_mapping_t * m, unix_shared_memory_queue_t * q, u32 context)
1086 vl_api_snat_static_mapping_details_t *rmp;
1087 snat_main_t * sm = &snat_main;
1089 rmp = vl_msg_api_alloc (sizeof (*rmp));
1090 memset (rmp, 0, sizeof (*rmp));
1091 rmp->_vl_msg_id = ntohs (VL_API_SNAT_STATIC_MAPPING_DETAILS+sm->msg_id_base);
1093 rmp->addr_only = m->addr_only;
1094 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
1095 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
1096 rmp->local_port = htons (m->local_port);
1097 rmp->external_port = htons (m->external_port);
1098 rmp->vrf_id = htonl (m->vrf_id);
1099 rmp->context = context;
1101 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1105 vl_api_snat_static_mapping_dump_t_handler
1106 (vl_api_snat_static_mapping_dump_t * mp)
1108 unix_shared_memory_queue_t *q;
1109 snat_main_t * sm = &snat_main;
1110 snat_static_mapping_t * m;
1112 q = vl_api_client_index_to_input_queue (mp->client_index);
1116 pool_foreach (m, sm->static_mappings,
1118 send_snat_static_mapping_details (m, q, mp->context);
1122 static void *vl_api_snat_static_mapping_dump_t_print
1123 (vl_api_snat_static_mapping_dump_t *mp, void * handle)
1127 s = format (0, "SCRIPT: snat_static_mapping_dump ");
1133 vl_api_snat_control_ping_t_handler
1134 (vl_api_snat_control_ping_t * mp)
1136 vl_api_snat_control_ping_reply_t *rmp;
1137 snat_main_t * sm = &snat_main;
1140 REPLY_MACRO2(VL_API_SNAT_CONTROL_PING_REPLY,
1142 rmp->vpe_pid = ntohl (getpid());
1146 static void *vl_api_snat_control_ping_t_print
1147 (vl_api_snat_control_ping_t *mp, void * handle)
1151 s = format (0, "SCRIPT: snat_control_ping ");
1157 vl_api_snat_show_config_t_handler
1158 (vl_api_snat_show_config_t * mp)
1160 vl_api_snat_show_config_reply_t *rmp;
1161 snat_main_t * sm = &snat_main;
1164 REPLY_MACRO2(VL_API_SNAT_SHOW_CONFIG_REPLY,
1166 rmp->translation_buckets = htonl (sm->translation_buckets);
1167 rmp->translation_memory_size = htonl (sm->translation_memory_size);
1168 rmp->user_buckets = htonl (sm->user_buckets);
1169 rmp->user_memory_size = htonl (sm->user_memory_size);
1170 rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
1171 rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
1172 rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
1173 rmp->static_mapping_only = sm->static_mapping_only;
1174 rmp->static_mapping_connection_tracking =
1175 sm->static_mapping_connection_tracking;
1179 static void *vl_api_snat_show_config_t_print
1180 (vl_api_snat_show_config_t *mp, void * handle)
1184 s = format (0, "SCRIPT: snat_show_config ");
1190 vl_api_snat_set_workers_t_handler
1191 (vl_api_snat_set_workers_t * mp)
1193 snat_main_t * sm = &snat_main;
1194 vl_api_snat_set_workers_reply_t * rmp;
1197 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
1199 if (sm->num_workers < 2)
1201 rv = VNET_API_ERROR_FEATURE_DISABLED;
1205 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
1206 rv = snat_set_workers(bitmap);
1207 clib_bitmap_free (bitmap);
1210 REPLY_MACRO (VL_API_SNAT_SET_WORKERS_REPLY);
1213 static void *vl_api_snat_set_workers_t_print
1214 (vl_api_snat_set_workers_t *mp, void * handle)
1220 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
1222 s = format (0, "SCRIPT: snat_set_workers ");
1223 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
1224 clib_bitmap_foreach (i, bitmap,
1227 s = format (s, "%d", i);
1229 s = format (s, ",%d", i);
1232 clib_bitmap_free (bitmap);
1237 send_snat_worker_details
1238 (u32 worker_index, unix_shared_memory_queue_t * q, u32 context)
1240 vl_api_snat_worker_details_t *rmp;
1241 snat_main_t * sm = &snat_main;
1242 vlib_worker_thread_t *w =
1243 vlib_worker_threads + worker_index + sm->first_worker_index;
1245 rmp = vl_msg_api_alloc (sizeof (*rmp));
1246 memset (rmp, 0, sizeof (*rmp));
1247 rmp->_vl_msg_id = ntohs (VL_API_SNAT_WORKER_DETAILS+sm->msg_id_base);
1248 rmp->context = context;
1249 rmp->worker_index = htonl (worker_index);
1250 rmp->lcore_id = htonl (w->lcore_id);
1251 strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
1253 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1257 vl_api_snat_worker_dump_t_handler
1258 (vl_api_snat_worker_dump_t * mp)
1260 unix_shared_memory_queue_t *q;
1261 snat_main_t * sm = &snat_main;
1264 q = vl_api_client_index_to_input_queue (mp->client_index);
1268 vec_foreach (worker_index, sm->workers)
1270 send_snat_worker_details(*worker_index, q, mp->context);
1274 static void *vl_api_snat_worker_dump_t_print
1275 (vl_api_snat_worker_dump_t *mp, void * handle)
1279 s = format (0, "SCRIPT: snat_worker_dump ");
1284 static int snat_add_interface_address(snat_main_t *sm,
1289 vl_api_snat_add_del_interface_addr_t_handler
1290 (vl_api_snat_add_del_interface_addr_t * mp)
1292 snat_main_t * sm = &snat_main;
1293 vl_api_snat_add_del_interface_addr_reply_t * rmp;
1294 u8 is_del = mp->is_add == 0;
1295 u32 sw_if_index = ntohl(mp->sw_if_index);
1298 VALIDATE_SW_IF_INDEX(mp);
1300 rv = snat_add_interface_address (sm, sw_if_index, is_del);
1302 BAD_SW_IF_INDEX_LABEL;
1304 REPLY_MACRO(VL_API_SNAT_ADD_DEL_INTERFACE_ADDR_REPLY);
1307 static void *vl_api_snat_add_del_interface_addr_t_print
1308 (vl_api_snat_add_del_interface_addr_t * mp, void *handle)
1312 s = format (0, "SCRIPT: snat_add_del_interface_addr ");
1313 s = format (s, "sw_if_index %d %s",
1314 clib_host_to_net_u32(mp->sw_if_index),
1315 mp->is_add ? "" : "del");
1321 send_snat_interface_addr_details
1322 (u32 sw_if_index, unix_shared_memory_queue_t * q, u32 context)
1324 vl_api_snat_interface_addr_details_t *rmp;
1325 snat_main_t * sm = &snat_main;
1327 rmp = vl_msg_api_alloc (sizeof (*rmp));
1328 memset (rmp, 0, sizeof (*rmp));
1329 rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_ADDR_DETAILS+sm->msg_id_base);
1330 rmp->sw_if_index = ntohl (sw_if_index);
1331 rmp->context = context;
1333 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1337 vl_api_snat_interface_addr_dump_t_handler
1338 (vl_api_snat_interface_addr_dump_t * mp)
1340 unix_shared_memory_queue_t *q;
1341 snat_main_t * sm = &snat_main;
1344 q = vl_api_client_index_to_input_queue (mp->client_index);
1348 vec_foreach (i, sm->auto_add_sw_if_indices)
1349 send_snat_interface_addr_details(*i, q, mp->context);
1352 static void *vl_api_snat_interface_addr_dump_t_print
1353 (vl_api_snat_interface_addr_dump_t *mp, void * handle)
1357 s = format (0, "SCRIPT: snat_interface_addr_dump ");
1363 vl_api_snat_ipfix_enable_disable_t_handler
1364 (vl_api_snat_ipfix_enable_disable_t * mp)
1366 snat_main_t * sm = &snat_main;
1367 vl_api_snat_ipfix_enable_disable_reply_t * rmp;
1370 rv = snat_ipfix_logging_enable_disable(mp->enable,
1371 clib_host_to_net_u32 (mp->domain_id),
1372 clib_host_to_net_u16 (mp->src_port));
1374 REPLY_MACRO (VL_API_SNAT_IPFIX_ENABLE_DISABLE_REPLY);
1377 static void *vl_api_snat_ipfix_enable_disable_t_print
1378 (vl_api_snat_ipfix_enable_disable_t *mp, void * handle)
1382 s = format (0, "SCRIPT: snat_ipfix_enable_disable ");
1384 s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
1386 s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
1388 s = format (s, "disable ");
1393 /* List of message types that this plugin understands */
1394 #define foreach_snat_plugin_api_msg \
1395 _(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range) \
1396 _(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature) \
1397 _(SNAT_ADD_STATIC_MAPPING, snat_add_static_mapping) \
1398 _(SNAT_CONTROL_PING, snat_control_ping) \
1399 _(SNAT_STATIC_MAPPING_DUMP, snat_static_mapping_dump) \
1400 _(SNAT_SHOW_CONFIG, snat_show_config) \
1401 _(SNAT_ADDRESS_DUMP, snat_address_dump) \
1402 _(SNAT_INTERFACE_DUMP, snat_interface_dump) \
1403 _(SNAT_SET_WORKERS, snat_set_workers) \
1404 _(SNAT_WORKER_DUMP, snat_worker_dump) \
1405 _(SNAT_ADD_DEL_INTERFACE_ADDR, snat_add_del_interface_addr) \
1406 _(SNAT_INTERFACE_ADDR_DUMP, snat_interface_addr_dump) \
1407 _(SNAT_IPFIX_ENABLE_DISABLE, snat_ipfix_enable_disable)
1409 /* Set up the API message handling tables */
1410 static clib_error_t *
1411 snat_plugin_api_hookup (vlib_main_t *vm)
1413 snat_main_t * sm __attribute__ ((unused)) = &snat_main;
1415 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
1417 vl_api_##n##_t_handler, \
1419 vl_api_##n##_t_endian, \
1420 vl_api_##n##_t_print, \
1421 sizeof(vl_api_##n##_t), 1);
1422 foreach_snat_plugin_api_msg;
1428 #define vl_msg_name_crc_list
1429 #include <snat/snat_all_api_h.h>
1430 #undef vl_msg_name_crc_list
1433 setup_message_id_table (snat_main_t * sm, api_main_t * am)
1435 #define _(id,n,crc) \
1436 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
1437 foreach_vl_msg_name_crc_snat;
1441 static void plugin_custom_dump_configure (snat_main_t * sm)
1443 #define _(n,f) sm->api_main->msg_print_handlers \
1444 [VL_API_##n + sm->msg_id_base] \
1445 = (void *) vl_api_##f##_t_print;
1446 foreach_snat_plugin_api_msg;
1452 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1455 ip4_address_t * address,
1457 u32 if_address_index,
1460 static clib_error_t * snat_init (vlib_main_t * vm)
1462 snat_main_t * sm = &snat_main;
1463 clib_error_t * error = 0;
1464 ip4_main_t * im = &ip4_main;
1465 ip_lookup_main_t * lm = &im->lookup_main;
1468 vlib_thread_registration_t *tr;
1469 vlib_thread_main_t *tm = vlib_get_thread_main ();
1472 ip4_add_del_interface_address_callback_t cb4;
1474 name = format (0, "snat_%08x%c", api_version, 0);
1476 /* Ask for a correctly-sized block of API message decode slots */
1477 sm->msg_id_base = vl_msg_api_get_msg_ids
1478 ((char *) name, VL_MSG_FIRST_AVAILABLE);
1481 sm->vnet_main = vnet_get_main();
1483 sm->ip4_lookup_main = lm;
1484 sm->api_main = &api_main;
1485 sm->first_worker_index = 0;
1486 sm->next_worker = 0;
1487 sm->num_workers = 0;
1489 sm->fq_in2out_index = ~0;
1490 sm->fq_out2in_index = ~0;
1492 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1495 tr = (vlib_thread_registration_t *) p[0];
1498 sm->num_workers = tr->count;
1499 sm->first_worker_index = tr->first_index;
1503 /* Use all available workers by default */
1504 if (sm->num_workers > 1)
1506 for (i=0; i < sm->num_workers; i++)
1507 bitmap = clib_bitmap_set (bitmap, i, 1);
1508 snat_set_workers(bitmap);
1509 clib_bitmap_free (bitmap);
1512 error = snat_plugin_api_hookup (vm);
1514 /* Add our API messages to the global name_crc hash table */
1515 setup_message_id_table (sm, &api_main);
1517 plugin_custom_dump_configure (sm);
1520 /* Set up the interface address add/del callback */
1521 cb4.function = snat_ip4_add_del_interface_address_cb;
1522 cb4.function_opaque = 0;
1524 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1526 /* Init IPFIX logging */
1527 snat_ipfix_logging_init(vm);
1532 VLIB_INIT_FUNCTION (snat_init);
1534 void snat_free_outside_address_and_port (snat_main_t * sm,
1535 snat_session_key_t * k,
1539 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1541 ASSERT (address_index < vec_len (sm->addresses));
1543 a = sm->addresses + address_index;
1545 ASSERT (clib_bitmap_get_no_check (a->busy_port_bitmap,
1546 port_host_byte_order) == 1);
1548 clib_bitmap_set_no_check (a->busy_port_bitmap, port_host_byte_order, 0);
1553 * @brief Match SNAT static mapping.
1555 * @param sm SNAT main.
1556 * @param match Address and port to match.
1557 * @param mapping External or local address and port of the matched mapping.
1558 * @param by_external If 0 match by local address otherwise match by external
1561 * @returns 0 if match found otherwise 1.
1563 int snat_static_mapping_match (snat_main_t * sm,
1564 snat_session_key_t match,
1565 snat_session_key_t * mapping,
1568 clib_bihash_kv_8_8_t kv, value;
1569 snat_static_mapping_t *m;
1570 snat_static_mapping_key_t m_key;
1571 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1574 mapping_hash = &sm->static_mapping_by_external;
1576 m_key.addr = match.addr;
1577 m_key.port = clib_net_to_host_u16 (match.port);
1578 m_key.fib_index = match.fib_index;
1580 kv.key = m_key.as_u64;
1582 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1584 /* Try address only mapping */
1586 kv.key = m_key.as_u64;
1587 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1591 m = pool_elt_at_index (sm->static_mappings, value.value);
1595 mapping->addr = m->local_addr;
1596 /* Address only mapping doesn't change port */
1597 mapping->port = m->addr_only ? match.port
1598 : clib_host_to_net_u16 (m->local_port);
1599 mapping->fib_index = m->fib_index;
1603 mapping->addr = m->external_addr;
1604 /* Address only mapping doesn't change port */
1605 mapping->port = m->addr_only ? match.port
1606 : clib_host_to_net_u16 (m->external_port);
1607 mapping->fib_index = sm->outside_fib_index;
1613 int snat_alloc_outside_address_and_port (snat_main_t * sm,
1614 snat_session_key_t * k,
1615 u32 * address_indexp)
1621 for (i = 0; i < vec_len (sm->addresses); i++)
1623 if (sm->addresses[i].busy_ports < (65535-1024))
1625 a = sm->addresses + i;
1629 portnum = random_u32 (&sm->random_seed);
1633 if (clib_bitmap_get_no_check (a->busy_port_bitmap, portnum))
1635 clib_bitmap_set_no_check (a->busy_port_bitmap, portnum, 1);
1637 /* Caller sets protocol and fib index */
1639 k->port = clib_host_to_net_u16(portnum);
1640 *address_indexp = i;
1645 /* Totally out of translations to use... */
1646 snat_ipfix_logging_addresses_exhausted(0);
1651 static clib_error_t *
1652 add_address_command_fn (vlib_main_t * vm,
1653 unformat_input_t * input,
1654 vlib_cli_command_t * cmd)
1656 unformat_input_t _line_input, *line_input = &_line_input;
1657 snat_main_t * sm = &snat_main;
1658 ip4_address_t start_addr, end_addr, this_addr;
1659 u32 start_host_order, end_host_order;
1664 /* Get a line of input. */
1665 if (!unformat_user (input, unformat_line_input, line_input))
1668 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1670 if (unformat (line_input, "%U - %U",
1671 unformat_ip4_address, &start_addr,
1672 unformat_ip4_address, &end_addr))
1674 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
1675 end_addr = start_addr;
1676 else if (unformat (line_input, "del"))
1679 return clib_error_return (0, "unknown input '%U'",
1680 format_unformat_error, input);
1682 unformat_free (line_input);
1684 if (sm->static_mapping_only)
1685 return clib_error_return (0, "static mapping only mode");
1687 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
1688 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
1690 if (end_host_order < start_host_order)
1691 return clib_error_return (0, "end address less than start address");
1693 count = (end_host_order - start_host_order) + 1;
1696 clib_warning ("%U - %U, %d addresses...",
1697 format_ip4_address, &start_addr,
1698 format_ip4_address, &end_addr,
1701 this_addr = start_addr;
1703 for (i = 0; i < count; i++)
1706 snat_add_address (sm, &this_addr);
1708 rv = snat_del_address (sm, this_addr, 0);
1712 case VNET_API_ERROR_NO_SUCH_ENTRY:
1713 return clib_error_return (0, "S-NAT address not exist.");
1715 case VNET_API_ERROR_UNSPECIFIED:
1716 return clib_error_return (0, "S-NAT address used in static mapping.");
1722 increment_v4_address (&this_addr);
1728 VLIB_CLI_COMMAND (add_address_command, static) = {
1729 .path = "snat add address",
1730 .short_help = "snat add addresses <ip4-range-start> [- <ip4-range-end>] [del]",
1731 .function = add_address_command_fn,
1734 static clib_error_t *
1735 snat_feature_command_fn (vlib_main_t * vm,
1736 unformat_input_t * input,
1737 vlib_cli_command_t * cmd)
1739 unformat_input_t _line_input, *line_input = &_line_input;
1740 vnet_main_t * vnm = vnet_get_main();
1741 clib_error_t * error = 0;
1743 u32 * inside_sw_if_indices = 0;
1744 u32 * outside_sw_if_indices = 0;
1750 /* Get a line of input. */
1751 if (!unformat_user (input, unformat_line_input, line_input))
1754 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1756 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
1758 vec_add1 (inside_sw_if_indices, sw_if_index);
1759 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
1761 vec_add1 (outside_sw_if_indices, sw_if_index);
1762 else if (unformat (line_input, "del"))
1765 return clib_error_return (0, "unknown input '%U'",
1766 format_unformat_error, input);
1768 unformat_free (line_input);
1770 if (vec_len (inside_sw_if_indices))
1772 for (i = 0; i < vec_len(inside_sw_if_indices); i++)
1774 sw_if_index = inside_sw_if_indices[i];
1775 snat_interface_add_del (sw_if_index, 1, is_del);
1779 if (vec_len (outside_sw_if_indices))
1781 for (i = 0; i < vec_len(outside_sw_if_indices); i++)
1783 sw_if_index = outside_sw_if_indices[i];
1784 snat_interface_add_del (sw_if_index, 0, is_del);
1788 vec_free (inside_sw_if_indices);
1789 vec_free (outside_sw_if_indices);
1794 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1795 .path = "set interface snat",
1796 .function = snat_feature_command_fn,
1797 .short_help = "set interface snat in <intfc> out <intfc> [del]",
1800 static clib_error_t *
1801 add_static_mapping_command_fn (vlib_main_t * vm,
1802 unformat_input_t * input,
1803 vlib_cli_command_t * cmd)
1805 unformat_input_t _line_input, *line_input = &_line_input;
1806 clib_error_t * error = 0;
1807 ip4_address_t l_addr, e_addr;
1808 u32 l_port = 0, e_port = 0, vrf_id = ~0;
1811 u32 sw_if_index = ~0;
1812 vnet_main_t * vnm = vnet_get_main();
1815 /* Get a line of input. */
1816 if (!unformat_user (input, unformat_line_input, line_input))
1819 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1821 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1824 else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1826 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1829 else if (unformat (line_input, "external %U", unformat_ip4_address,
1832 else if (unformat (line_input, "external %U %u",
1833 unformat_vnet_sw_interface, vnm, &sw_if_index,
1837 else if (unformat (line_input, "external %U",
1838 unformat_vnet_sw_interface, vnm, &sw_if_index))
1840 else if (unformat (line_input, "vrf %u", &vrf_id))
1842 else if (unformat (line_input, "del"))
1845 return clib_error_return (0, "unknown input: '%U'",
1846 format_unformat_error, line_input);
1848 unformat_free (line_input);
1850 rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
1851 vrf_id, addr_only, sw_if_index, is_add);
1855 case VNET_API_ERROR_INVALID_VALUE:
1856 return clib_error_return (0, "External port already in use.");
1858 case VNET_API_ERROR_NO_SUCH_ENTRY:
1860 return clib_error_return (0, "External addres must be allocated.");
1862 return clib_error_return (0, "Mapping not exist.");
1864 case VNET_API_ERROR_NO_SUCH_FIB:
1865 return clib_error_return (0, "No such VRF id.");
1866 case VNET_API_ERROR_VALUE_EXIST:
1867 return clib_error_return (0, "Mapping already exist.");
1877 * @cliexstart{snat add static mapping}
1878 * Static mapping allows hosts on the external network to initiate connection
1879 * to to the local network host.
1880 * To create static mapping between local host address 10.0.0.3 port 6303 and
1881 * external address 4.4.4.4 port 3606 use:
1882 * vpp# snat add static mapping local 10.0.0.3 6303 external 4.4.4.4 3606
1883 * If not runnig "static mapping only" S-NAT plugin mode use before:
1884 * vpp# snat add address 4.4.4.4
1885 * To create static mapping between local and external address use:
1886 * vpp# snat add static mapping local 10.0.0.3 external 4.4.4.4
1889 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1890 .path = "snat add static mapping",
1891 .function = add_static_mapping_command_fn,
1893 "snat add static mapping local <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
1896 static clib_error_t *
1897 set_workers_command_fn (vlib_main_t * vm,
1898 unformat_input_t * input,
1899 vlib_cli_command_t * cmd)
1901 unformat_input_t _line_input, *line_input = &_line_input;
1905 /* Get a line of input. */
1906 if (!unformat_user (input, unformat_line_input, line_input))
1909 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1911 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
1914 return clib_error_return (0, "unknown input '%U'",
1915 format_unformat_error, input);
1917 unformat_free (line_input);
1920 return clib_error_return (0, "List of workers must be specified.");
1922 rv = snat_set_workers(bitmap);
1924 clib_bitmap_free (bitmap);
1928 case VNET_API_ERROR_INVALID_WORKER:
1929 return clib_error_return (0, "Invalid worker(s).");
1931 case VNET_API_ERROR_FEATURE_DISABLED:
1932 return clib_error_return (0,
1933 "Supported only if 2 or more workes available.");
1944 * @cliexstart{set snat workers}
1945 * Set SNAT workers if 2 or more workers available, use:
1946 * vpp# set snat workers 0-2,5
1949 VLIB_CLI_COMMAND (set_workers_command, static) = {
1950 .path = "set snat workers",
1951 .function = set_workers_command_fn,
1953 "set snat workers <workers-list>",
1956 static clib_error_t *
1957 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
1958 unformat_input_t * input,
1959 vlib_cli_command_t * cmd)
1961 unformat_input_t _line_input, *line_input = &_line_input;
1967 /* Get a line of input. */
1968 if (!unformat_user (input, unformat_line_input, line_input))
1971 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1973 if (unformat (line_input, "domain %d", &domain_id))
1975 else if (unformat (line_input, "src-port %d", &src_port))
1977 else if (unformat (line_input, "disable"))
1980 return clib_error_return (0, "unknown input '%U'",
1981 format_unformat_error, input);
1983 unformat_free (line_input);
1985 rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
1988 return clib_error_return (0, "ipfix logging enable failed");
1995 * @cliexstart{snat ipfix logging}
1996 * To enable SNAT IPFIX logging use:
1997 * vpp# snat ipfix logging
1998 * To set IPFIX exporter use:
1999 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
2002 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
2003 .path = "snat ipfix logging",
2004 .function = snat_ipfix_logging_enable_disable_command_fn,
2005 .short_help = "snat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
2008 static clib_error_t *
2009 snat_config (vlib_main_t * vm, unformat_input_t * input)
2011 snat_main_t * sm = &snat_main;
2012 u32 translation_buckets = 1024;
2013 u32 translation_memory_size = 128<<20;
2014 u32 user_buckets = 128;
2015 u32 user_memory_size = 64<<20;
2016 u32 max_translations_per_user = 100;
2017 u32 outside_vrf_id = 0;
2018 u32 inside_vrf_id = 0;
2019 u32 static_mapping_buckets = 1024;
2020 u32 static_mapping_memory_size = 64<<20;
2021 u8 static_mapping_only = 0;
2022 u8 static_mapping_connection_tracking = 0;
2023 vlib_thread_main_t *tm = vlib_get_thread_main ();
2025 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2027 if (unformat (input, "translation hash buckets %d", &translation_buckets))
2029 else if (unformat (input, "translation hash memory %d",
2030 &translation_memory_size));
2031 else if (unformat (input, "user hash buckets %d", &user_buckets))
2033 else if (unformat (input, "user hash memory %d",
2036 else if (unformat (input, "max translations per user %d",
2037 &max_translations_per_user))
2039 else if (unformat (input, "outside VRF id %d",
2042 else if (unformat (input, "inside VRF id %d",
2045 else if (unformat (input, "static mapping only"))
2047 static_mapping_only = 1;
2048 if (unformat (input, "connection tracking"))
2049 static_mapping_connection_tracking = 1;
2052 return clib_error_return (0, "unknown input '%U'",
2053 format_unformat_error, input);
2056 /* for show commands, etc. */
2057 sm->translation_buckets = translation_buckets;
2058 sm->translation_memory_size = translation_memory_size;
2059 sm->user_buckets = user_buckets;
2060 sm->user_memory_size = user_memory_size;
2061 sm->max_translations_per_user = max_translations_per_user;
2062 sm->outside_vrf_id = outside_vrf_id;
2063 sm->outside_fib_index = ~0;
2064 sm->inside_vrf_id = inside_vrf_id;
2065 sm->inside_fib_index = ~0;
2066 sm->static_mapping_only = static_mapping_only;
2067 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
2069 if (!static_mapping_only ||
2070 (static_mapping_only && static_mapping_connection_tracking))
2072 clib_bihash_init_8_8 (&sm->worker_by_in, "worker-by-in", user_buckets,
2075 clib_bihash_init_8_8 (&sm->worker_by_out, "worker-by-out", user_buckets,
2078 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
2080 clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
2081 translation_memory_size);
2083 clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
2084 translation_memory_size);
2086 clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
2089 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2090 "static_mapping_by_local", static_mapping_buckets,
2091 static_mapping_memory_size);
2093 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2094 "static_mapping_by_external", static_mapping_buckets,
2095 static_mapping_memory_size);
2099 VLIB_CONFIG_FUNCTION (snat_config, "snat");
2101 u8 * format_snat_key (u8 * s, va_list * args)
2103 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
2104 char * protocol_string = "unknown";
2105 static char *protocol_strings[] = {
2111 if (key->protocol < ARRAY_LEN(protocol_strings))
2112 protocol_string = protocol_strings[key->protocol];
2114 s = format (s, "%U proto %s port %d fib %d",
2115 format_ip4_address, &key->addr, protocol_string,
2116 clib_net_to_host_u16 (key->port), key->fib_index);
2120 u8 * format_snat_session (u8 * s, va_list * args)
2122 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
2123 snat_session_t * sess = va_arg (*args, snat_session_t *);
2125 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
2126 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
2127 s = format (s, " last heard %.2f\n", sess->last_heard);
2128 s = format (s, " total pkts %d, total bytes %lld\n",
2129 sess->total_pkts, sess->total_bytes);
2130 if (snat_is_session_static (sess))
2131 s = format (s, " static translation\n");
2133 s = format (s, " dynamic translation\n");
2138 u8 * format_snat_user (u8 * s, va_list * args)
2140 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
2141 snat_user_t * u = va_arg (*args, snat_user_t *);
2142 int verbose = va_arg (*args, int);
2143 dlist_elt_t * head, * elt;
2144 u32 elt_index, head_index;
2146 snat_session_t * sess;
2148 s = format (s, "%U: %d dynamic translations, %d static translations\n",
2149 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
2154 if (u->nsessions || u->nstaticsessions)
2156 head_index = u->sessions_per_user_list_head_index;
2157 head = pool_elt_at_index (sm->list_pool, head_index);
2159 elt_index = head->next;
2160 elt = pool_elt_at_index (sm->list_pool, elt_index);
2161 session_index = elt->value;
2163 while (session_index != ~0)
2165 sess = pool_elt_at_index (sm->sessions, session_index);
2167 s = format (s, " %U\n", format_snat_session, sm, sess);
2169 elt_index = elt->next;
2170 elt = pool_elt_at_index (sm->list_pool, elt_index);
2171 session_index = elt->value;
2178 u8 * format_snat_static_mapping (u8 * s, va_list * args)
2180 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
2183 s = format (s, "local %U external %U vrf %d",
2184 format_ip4_address, &m->local_addr,
2185 format_ip4_address, &m->external_addr,
2188 s = format (s, "local %U:%d external %U:%d vrf %d",
2189 format_ip4_address, &m->local_addr, m->local_port,
2190 format_ip4_address, &m->external_addr, m->external_port,
2196 static clib_error_t *
2197 show_snat_command_fn (vlib_main_t * vm,
2198 unformat_input_t * input,
2199 vlib_cli_command_t * cmd)
2202 snat_main_t * sm = &snat_main;
2204 snat_static_mapping_t *m;
2205 snat_interface_t *i;
2206 snat_address_t * ap;
2207 vnet_main_t *vnm = vnet_get_main();
2208 snat_main_per_thread_data_t *tsm;
2209 u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
2212 if (unformat (input, "detail"))
2214 else if (unformat (input, "verbose"))
2217 if (sm->static_mapping_only)
2219 if (sm->static_mapping_connection_tracking)
2220 vlib_cli_output (vm, "SNAT mode: static mapping only connection "
2223 vlib_cli_output (vm, "SNAT mode: static mapping only");
2227 vlib_cli_output (vm, "SNAT mode: dynamic translations enabled");
2232 pool_foreach (i, sm->interfaces,
2234 vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
2235 vnet_get_sw_interface (vnm, i->sw_if_index),
2236 i->is_inside ? "in" : "out");
2239 if (vec_len (sm->auto_add_sw_if_indices))
2241 vlib_cli_output (vm, "SNAT pool addresses interfaces:");
2242 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
2244 vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
2245 vnet_get_sw_interface (vnm, *sw_if_index));
2249 vec_foreach (ap, sm->addresses)
2251 u8 * s = format (0, "");
2252 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
2253 clib_bitmap_foreach (j, ap->busy_port_bitmap,
2255 s = format (s, " %d", j);
2257 vlib_cli_output (vm, " %d busy ports:%s", ap->busy_ports, s);
2261 if (sm->num_workers > 1)
2263 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
2266 vec_foreach (worker, sm->workers)
2268 vlib_worker_thread_t *w =
2269 vlib_worker_threads + *worker + sm->first_worker_index;
2270 vlib_cli_output (vm, " %s", w->name);
2275 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2277 vlib_cli_output (vm, "%d static mappings",
2278 pool_elts (sm->static_mappings));
2282 pool_foreach (m, sm->static_mappings,
2284 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2290 vec_foreach (tsm, sm->per_thread_data)
2292 users_num += pool_elts (tsm->users);
2293 sessions_num += pool_elts (tsm->sessions);
2296 vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
2297 " %d static mappings",
2299 vec_len (sm->addresses),
2301 pool_elts (sm->static_mappings));
2305 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
2307 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
2309 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_in,
2311 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_out,
2313 vec_foreach_index (j, sm->per_thread_data)
2315 tsm = vec_elt_at_index (sm->per_thread_data, j);
2317 if (pool_elts (tsm->users) == 0)
2320 vlib_worker_thread_t *w = vlib_worker_threads + j;
2321 vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
2323 vlib_cli_output (vm, " %d list pool elements",
2324 pool_elts (tsm->list_pool));
2326 pool_foreach (u, tsm->users,
2328 vlib_cli_output (vm, " %U", format_snat_user, tsm, u,
2333 if (pool_elts (sm->static_mappings))
2335 vlib_cli_output (vm, "static mappings:");
2336 pool_foreach (m, sm->static_mappings,
2338 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2347 VLIB_CLI_COMMAND (show_snat_command, static) = {
2348 .path = "show snat",
2349 .short_help = "show snat",
2350 .function = show_snat_command_fn,
2355 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2358 ip4_address_t * address,
2360 u32 if_address_index,
2363 snat_main_t *sm = &snat_main;
2364 snat_static_map_resolve_t *rp;
2365 u32 *indices_to_delete = 0;
2369 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2371 if (sw_if_index == sm->auto_add_sw_if_indices[i])
2375 /* Don't trip over lease renewal, static config */
2376 for (j = 0; j < vec_len(sm->addresses); j++)
2377 if (sm->addresses[j].addr.as_u32 == address->as_u32)
2380 snat_add_address (sm, address);
2381 /* Scan static map resolution vector */
2382 for (j = 0; j < vec_len (sm->to_resolve); j++)
2384 rp = sm->to_resolve + j;
2385 /* On this interface? */
2386 if (rp->sw_if_index == sw_if_index)
2388 /* Add the static mapping */
2389 rv = snat_add_static_mapping (rp->l_addr,
2395 ~0 /* sw_if_index */,
2398 clib_warning ("snat_add_static_mapping returned %d",
2400 vec_add1 (indices_to_delete, j);
2403 /* If we resolved any of the outstanding static mappings */
2404 if (vec_len(indices_to_delete))
2407 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2408 vec_delete(sm->to_resolve, 1, j);
2409 vec_free(indices_to_delete);
2415 (void) snat_del_address(sm, address[0], 1);
2423 static int snat_add_interface_address (snat_main_t *sm,
2427 ip4_main_t * ip4_main = sm->ip4_main;
2428 ip4_address_t * first_int_addr;
2429 snat_static_map_resolve_t *rp;
2430 u32 *indices_to_delete = 0;
2433 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2434 0 /* just want the address*/);
2436 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2438 if (sm->auto_add_sw_if_indices[i] == sw_if_index)
2442 /* if have address remove it */
2444 (void) snat_del_address (sm, first_int_addr[0], 1);
2447 for (j = 0; j < vec_len (sm->to_resolve); j++)
2449 rp = sm->to_resolve + j;
2450 if (rp->sw_if_index == sw_if_index)
2451 vec_add1 (indices_to_delete, j);
2453 if (vec_len(indices_to_delete))
2455 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2456 vec_del1(sm->to_resolve, j);
2457 vec_free(indices_to_delete);
2460 vec_del1(sm->auto_add_sw_if_indices, i);
2463 return VNET_API_ERROR_VALUE_EXIST;
2470 return VNET_API_ERROR_NO_SUCH_ENTRY;
2472 /* add to the auto-address list */
2473 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
2475 /* If the address is already bound - or static - add it now */
2477 snat_add_address (sm, first_int_addr);
2482 static clib_error_t *
2483 snat_add_interface_address_command_fn (vlib_main_t * vm,
2484 unformat_input_t * input,
2485 vlib_cli_command_t * cmd)
2487 snat_main_t *sm = &snat_main;
2488 unformat_input_t _line_input, *line_input = &_line_input;
2493 /* Get a line of input. */
2494 if (!unformat_user (input, unformat_line_input, line_input))
2497 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2499 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
2500 sm->vnet_main, &sw_if_index))
2502 else if (unformat (line_input, "del"))
2505 return clib_error_return (0, "unknown input '%U'",
2506 format_unformat_error, line_input);
2509 rv = snat_add_interface_address (sm, sw_if_index, is_del);
2517 return clib_error_return (0, "snat_add_interface_address returned %d",
2523 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2524 .path = "snat add interface address",
2525 .short_help = "snat add interface address <interface> [del]",
2526 .function = snat_add_interface_address_command_fn,