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 <snat/snat_det.h>
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/fib/ip4_fib.h>
29 #include <vlibapi/api.h>
30 #include <vlibmemory/api.h>
31 #include <vlibsocket/api.h>
32 #include <vpp/app/version.h>
34 snat_main_t snat_main;
36 /* define message IDs */
37 #include <snat/snat_msg_enum.h>
39 /* define message structures */
41 #include <snat/snat_all_api_h.h>
44 /* define generated endian-swappers */
46 #include <snat/snat_all_api_h.h>
49 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
51 /* Get the API version number */
52 #define vl_api_version(n,v) static u32 api_version=(v);
53 #include <snat/snat_all_api_h.h>
56 /* Macro to finish up custom dump fns */
59 vl_print (handle, (char *)s); \
64 * A handy macro to set up a message reply.
65 * Assumes that the following variables are available:
66 * mp - pointer to request message
67 * rmp - pointer to reply message type
71 #define REPLY_MACRO(t) \
73 unix_shared_memory_queue_t * q = \
74 vl_api_client_index_to_input_queue (mp->client_index); \
78 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
79 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
80 rmp->context = mp->context; \
81 rmp->retval = ntohl(rv); \
83 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
86 #define REPLY_MACRO2(t, body) \
88 unix_shared_memory_queue_t * q = \
89 vl_api_client_index_to_input_queue (mp->client_index); \
93 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
94 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
95 rmp->context = mp->context; \
96 rmp->retval = ntohl(rv); \
97 do {body;} while (0); \
98 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
102 /* Hook up input features */
103 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
104 .arc_name = "ip4-unicast",
105 .node_name = "snat-in2out",
106 .runs_before = VNET_FEATURES ("snat-out2in"),
108 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
109 .arc_name = "ip4-unicast",
110 .node_name = "snat-out2in",
111 .runs_before = VNET_FEATURES ("ip4-lookup"),
113 VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
114 .arc_name = "ip4-unicast",
115 .node_name = "snat-det-in2out",
116 .runs_before = VNET_FEATURES ("snat-det-out2in"),
118 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
119 .arc_name = "ip4-unicast",
120 .node_name = "snat-det-out2in",
121 .runs_before = VNET_FEATURES ("ip4-lookup"),
123 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
124 .arc_name = "ip4-unicast",
125 .node_name = "snat-in2out-worker-handoff",
126 .runs_before = VNET_FEATURES ("snat-out2in-worker-handoff"),
128 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
129 .arc_name = "ip4-unicast",
130 .node_name = "snat-out2in-worker-handoff",
131 .runs_before = VNET_FEATURES ("ip4-lookup"),
133 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
134 .arc_name = "ip4-unicast",
135 .node_name = "snat-in2out-fast",
136 .runs_before = VNET_FEATURES ("snat-out2in-fast"),
138 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
139 .arc_name = "ip4-unicast",
140 .node_name = "snat-out2in-fast",
141 .runs_before = VNET_FEATURES ("ip4-lookup"),
145 VLIB_PLUGIN_REGISTER () = {
146 .version = VPP_BUILD_VER,
150 /*$$$$$ move to an installed header file */
151 #if (1 || CLIB_DEBUG > 0) /* "trust, but verify" */
153 #define VALIDATE_SW_IF_INDEX(mp) \
154 do { u32 __sw_if_index = ntohl(mp->sw_if_index); \
155 vnet_main_t *__vnm = vnet_get_main(); \
156 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
158 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
159 goto bad_sw_if_index; \
163 #define BAD_SW_IF_INDEX_LABEL \
169 #define VALIDATE_RX_SW_IF_INDEX(mp) \
170 do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index); \
171 vnet_main_t *__vnm = vnet_get_main(); \
172 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
173 __rx_sw_if_index)) { \
174 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
175 goto bad_rx_sw_if_index; \
179 #define BAD_RX_SW_IF_INDEX_LABEL \
181 bad_rx_sw_if_index: \
185 #define VALIDATE_TX_SW_IF_INDEX(mp) \
186 do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index); \
187 vnet_main_t *__vnm = vnet_get_main(); \
188 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
189 __tx_sw_if_index)) { \
190 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
191 goto bad_tx_sw_if_index; \
195 #define BAD_TX_SW_IF_INDEX_LABEL \
197 bad_tx_sw_if_index: \
203 #define VALIDATE_SW_IF_INDEX(mp)
204 #define BAD_SW_IF_INDEX_LABEL
205 #define VALIDATE_RX_SW_IF_INDEX(mp)
206 #define BAD_RX_SW_IF_INDEX_LABEL
207 #define VALIDATE_TX_SW_IF_INDEX(mp)
208 #define BAD_TX_SW_IF_INDEX_LABEL
210 #endif /* CLIB_DEBUG > 0 */
213 * @brief Add/del NAT address to FIB.
215 * Add the external NAT address to the FIB as receive entries. This ensures
216 * that VPP will reply to ARP for this address and we don't need to enable
217 * proxy ARP on the outside interface.
219 * @param addr IPv4 address.
220 * @param plen address prefix length
221 * @param sw_if_index Interface.
222 * @param is_add If 0 delete, otherwise add.
225 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
228 fib_prefix_t prefix = {
230 .fp_proto = FIB_PROTOCOL_IP4,
232 .ip4.as_u32 = addr->as_u32,
235 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
238 fib_table_entry_update_one_path(fib_index,
240 FIB_SOURCE_PLUGIN_HI,
241 (FIB_ENTRY_FLAG_CONNECTED |
242 FIB_ENTRY_FLAG_LOCAL |
243 FIB_ENTRY_FLAG_EXCLUSIVE),
250 FIB_ROUTE_PATH_FLAG_NONE);
252 fib_table_entry_delete(fib_index,
254 FIB_SOURCE_PLUGIN_HI);
257 void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id)
265 /* Check if address already exists */
266 vec_foreach (ap, sm->addresses)
268 if (ap->addr.as_u32 == addr->as_u32)
272 vec_add2 (sm->addresses, ap, 1);
274 ap->fib_index = ip4_fib_index_from_table_id(vrf_id);
275 #define _(N, i, n, s) \
276 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535);
277 foreach_snat_protocol
280 /* Add external address to FIB */
281 pool_foreach (i, sm->interfaces,
286 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
291 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
294 snat_static_mapping_t *m;
295 pool_foreach (m, sm->static_mappings,
297 if (m->external_addr.as_u32 == addr.as_u32)
304 static void increment_v4_address (ip4_address_t * a)
308 v = clib_net_to_host_u32(a->as_u32) + 1;
309 a->as_u32 = clib_host_to_net_u32(v);
313 snat_add_static_mapping_when_resolved (snat_main_t * sm,
314 ip4_address_t l_addr,
319 snat_protocol_t proto,
323 snat_static_map_resolve_t *rp;
325 vec_add2 (sm->to_resolve, rp, 1);
326 rp->l_addr.as_u32 = l_addr.as_u32;
328 rp->sw_if_index = sw_if_index;
332 rp->addr_only = addr_only;
337 * @brief Add static mapping.
339 * Create static mapping between local addr+port and external addr+port.
341 * @param l_addr Local IPv4 address.
342 * @param e_addr External IPv4 address.
343 * @param l_port Local port number.
344 * @param e_port External port number.
345 * @param vrf_id VRF ID.
346 * @param addr_only If 0 address port and pair mapping, otherwise address only.
347 * @param sw_if_index External port instead of specific IP address.
348 * @param is_add If 0 delete static mapping, otherwise add.
352 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
353 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
354 u32 sw_if_index, snat_protocol_t proto, int is_add)
356 snat_main_t * sm = &snat_main;
357 snat_static_mapping_t *m;
358 snat_session_key_t m_key;
359 clib_bihash_kv_8_8_t kv, value;
360 snat_address_t *a = 0;
363 snat_interface_t *interface;
366 /* If the external address is a specific interface address */
367 if (sw_if_index != ~0)
369 ip4_address_t * first_int_addr;
371 /* Might be already set... */
372 first_int_addr = ip4_interface_first_address
373 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
375 /* DHCP resolution required? */
376 if (first_int_addr == 0)
378 snat_add_static_mapping_when_resolved
379 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
384 e_addr.as_u32 = first_int_addr->as_u32;
388 m_key.port = addr_only ? 0 : e_port;
389 m_key.protocol = addr_only ? 0 : proto;
390 m_key.fib_index = sm->outside_fib_index;
391 kv.key = m_key.as_u64;
392 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
395 m = pool_elt_at_index (sm->static_mappings, value.value);
400 return VNET_API_ERROR_VALUE_EXIST;
402 /* Convert VRF id to FIB index */
405 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
407 return VNET_API_ERROR_NO_SUCH_FIB;
410 /* If not specified use inside VRF id from SNAT plugin startup config */
413 fib_index = sm->inside_fib_index;
414 vrf_id = sm->inside_vrf_id;
417 /* Find external address in allocated addresses and reserve port for
418 address and port pair mapping when dynamic translations enabled */
419 if (!addr_only && !(sm->static_mapping_only))
421 for (i = 0; i < vec_len (sm->addresses); i++)
423 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
425 a = sm->addresses + i;
426 /* External port must be unused */
429 #define _(N, j, n, s) \
430 case SNAT_PROTOCOL_##N: \
431 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
432 return VNET_API_ERROR_INVALID_VALUE; \
433 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
435 a->busy_##n##_ports++; \
437 foreach_snat_protocol
440 clib_warning("unknown_protocol");
441 return VNET_API_ERROR_INVALID_VALUE_2;
446 /* External address must be allocated */
448 return VNET_API_ERROR_NO_SUCH_ENTRY;
451 pool_get (sm->static_mappings, m);
452 memset (m, 0, sizeof (*m));
453 m->local_addr = l_addr;
454 m->external_addr = e_addr;
455 m->addr_only = addr_only;
457 m->fib_index = fib_index;
460 m->local_port = l_port;
461 m->external_port = e_port;
465 m_key.addr = m->local_addr;
466 m_key.port = m->local_port;
467 m_key.protocol = m->proto;
468 m_key.fib_index = m->fib_index;
469 kv.key = m_key.as_u64;
470 kv.value = m - sm->static_mappings;
471 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
473 m_key.addr = m->external_addr;
474 m_key.port = m->external_port;
475 m_key.fib_index = sm->outside_fib_index;
476 kv.key = m_key.as_u64;
477 kv.value = m - sm->static_mappings;
478 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
483 snat_user_key_t w_key0;
484 snat_worker_key_t w_key1;
486 w_key0.addr = m->local_addr;
487 w_key0.fib_index = m->fib_index;
488 kv.key = w_key0.as_u64;
490 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
492 kv.value = sm->first_worker_index +
493 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
495 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv, 1);
499 kv.value = value.value;
502 w_key1.addr = m->external_addr;
503 w_key1.port = clib_host_to_net_u16 (m->external_port);
504 w_key1.fib_index = sm->outside_fib_index;
505 kv.key = w_key1.as_u64;
506 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv, 1);
512 return VNET_API_ERROR_NO_SUCH_ENTRY;
514 /* Free external address port */
515 if (!addr_only && !(sm->static_mapping_only))
517 for (i = 0; i < vec_len (sm->addresses); i++)
519 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
521 a = sm->addresses + i;
524 #define _(N, j, n, s) \
525 case SNAT_PROTOCOL_##N: \
526 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
528 a->busy_##n##_ports--; \
530 foreach_snat_protocol
533 clib_warning("unknown_protocol");
534 return VNET_API_ERROR_INVALID_VALUE_2;
541 m_key.addr = m->local_addr;
542 m_key.port = m->local_port;
543 m_key.protocol = m->proto;
544 m_key.fib_index = m->fib_index;
545 kv.key = m_key.as_u64;
546 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
548 m_key.addr = m->external_addr;
549 m_key.port = m->external_port;
550 m_key.fib_index = sm->outside_fib_index;
551 kv.key = m_key.as_u64;
552 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
554 /* Delete session(s) for static mapping if exist */
555 if (!(sm->static_mapping_only) ||
556 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
558 snat_user_key_t u_key;
560 dlist_elt_t * head, * elt;
561 u32 elt_index, head_index, del_elt_index;
565 snat_main_per_thread_data_t *tsm;
567 u_key.addr = m->local_addr;
568 u_key.fib_index = m->fib_index;
569 kv.key = u_key.as_u64;
570 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
572 user_index = value.value;
573 if (!clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
574 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
576 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
577 u = pool_elt_at_index (tsm->users, user_index);
578 if (u->nstaticsessions)
580 head_index = u->sessions_per_user_list_head_index;
581 head = pool_elt_at_index (tsm->list_pool, head_index);
582 elt_index = head->next;
583 elt = pool_elt_at_index (tsm->list_pool, elt_index);
584 ses_index = elt->value;
585 while (ses_index != ~0)
587 s = pool_elt_at_index (tsm->sessions, ses_index);
588 del_elt_index = elt_index;
589 elt_index = elt->next;
590 elt = pool_elt_at_index (tsm->list_pool, elt_index);
591 ses_index = elt->value;
595 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
596 (clib_net_to_host_u16 (s->out2in.port) != e_port))
601 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
602 s->out2in.addr.as_u32,
606 s->in2out.fib_index);
608 value.key = s->in2out.as_u64;
609 clib_bihash_add_del_8_8 (&sm->in2out, &value, 0);
610 value.key = s->out2in.as_u64;
611 clib_bihash_add_del_8_8 (&sm->out2in, &value, 0);
612 pool_put (tsm->sessions, s);
614 clib_dlist_remove (tsm->list_pool, del_elt_index);
615 pool_put_index (tsm->list_pool, del_elt_index);
616 u->nstaticsessions--;
623 pool_put (tsm->users, u);
624 clib_bihash_add_del_8_8 (&sm->user_hash, &kv, 0);
630 /* Delete static mapping from pool */
631 pool_put (sm->static_mappings, m);
637 /* Add/delete external address to FIB */
638 pool_foreach (interface, sm->interfaces,
640 if (interface->is_inside)
643 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
650 int snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm)
652 snat_address_t *a = 0;
654 u32 *ses_to_be_removed = 0, *ses_index;
655 clib_bihash_kv_8_8_t kv, value;
656 snat_user_key_t user_key;
658 snat_main_per_thread_data_t *tsm;
659 snat_static_mapping_t *m;
660 snat_interface_t *interface;
663 /* Find SNAT address */
664 for (i=0; i < vec_len (sm->addresses); i++)
666 if (sm->addresses[i].addr.as_u32 == addr.as_u32)
668 a = sm->addresses + i;
673 return VNET_API_ERROR_NO_SUCH_ENTRY;
677 pool_foreach (m, sm->static_mappings,
679 if (m->external_addr.as_u32 == addr.as_u32)
680 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
681 m->local_port, m->external_port,
682 m->vrf_id, m->addr_only, ~0,
688 /* Check if address is used in some static mapping */
689 if (is_snat_address_used_in_static_mapping(sm, addr))
691 clib_warning ("address used in static mapping");
692 return VNET_API_ERROR_UNSPECIFIED;
696 /* Delete sessions using address */
697 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
699 vec_foreach (tsm, sm->per_thread_data)
701 pool_foreach (ses, tsm->sessions, ({
702 if (ses->out2in.addr.as_u32 == addr.as_u32)
705 snat_ipfix_logging_nat44_ses_delete(ses->in2out.addr.as_u32,
706 ses->out2in.addr.as_u32,
707 ses->in2out.protocol,
710 ses->in2out.fib_index);
711 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
712 kv.key = ses->in2out.as_u64;
713 clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
714 kv.key = ses->out2in.as_u64;
715 clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
716 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
717 user_key.addr = ses->in2out.addr;
718 user_key.fib_index = ses->in2out.fib_index;
719 kv.key = user_key.as_u64;
720 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
722 u = pool_elt_at_index (tsm->users, value.value);
728 vec_foreach (ses_index, ses_to_be_removed)
729 pool_put_index (tsm->sessions, ses_index[0]);
731 vec_free (ses_to_be_removed);
735 vec_del1 (sm->addresses, i);
737 /* Delete external address from FIB */
738 pool_foreach (interface, sm->interfaces,
740 if (interface->is_inside)
743 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
750 static int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
752 snat_main_t *sm = &snat_main;
754 const char * feature_name;
756 snat_static_mapping_t * m;
759 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
760 feature_name = is_inside ? "snat-in2out-fast" : "snat-out2in-fast";
763 if (sm->num_workers > 1 && !sm->deterministic)
764 feature_name = is_inside ? "snat-in2out-worker-handoff" : "snat-out2in-worker-handoff";
765 else if (sm->deterministic)
766 feature_name = is_inside ? "snat-det-in2out" : "snat-det-out2in";
768 feature_name = is_inside ? "snat-in2out" : "snat-out2in";
771 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
774 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
775 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
777 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
778 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
780 pool_foreach (i, sm->interfaces,
782 if (i->sw_if_index == sw_if_index)
785 pool_put (sm->interfaces, i);
787 return VNET_API_ERROR_VALUE_EXIST;
794 return VNET_API_ERROR_NO_SUCH_ENTRY;
796 pool_get (sm->interfaces, i);
797 i->sw_if_index = sw_if_index;
798 i->is_inside = is_inside;
800 /* Add/delete external addresses to FIB */
805 vec_foreach (ap, sm->addresses)
806 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
808 pool_foreach (m, sm->static_mappings,
813 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
816 pool_foreach (dm, sm->det_maps,
818 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
824 static int snat_set_workers (uword * bitmap)
826 snat_main_t *sm = &snat_main;
829 if (sm->num_workers < 2)
830 return VNET_API_ERROR_FEATURE_DISABLED;
832 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
833 return VNET_API_ERROR_INVALID_WORKER;
835 vec_free (sm->workers);
836 clib_bitmap_foreach (i, bitmap,
838 vec_add1(sm->workers, i);
845 vl_api_snat_add_address_range_t_handler
846 (vl_api_snat_add_address_range_t * mp)
848 snat_main_t * sm = &snat_main;
849 vl_api_snat_add_address_range_reply_t * rmp;
850 ip4_address_t this_addr;
851 u32 start_host_order, end_host_order;
859 rv = VNET_API_ERROR_UNIMPLEMENTED;
863 if (sm->static_mapping_only)
865 rv = VNET_API_ERROR_FEATURE_DISABLED;
869 tmp = (u32 *) mp->first_ip_address;
870 start_host_order = clib_host_to_net_u32 (tmp[0]);
871 tmp = (u32 *) mp->last_ip_address;
872 end_host_order = clib_host_to_net_u32 (tmp[0]);
874 count = (end_host_order - start_host_order) + 1;
876 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
879 clib_warning ("%U - %U, %d addresses...",
880 format_ip4_address, mp->first_ip_address,
881 format_ip4_address, mp->last_ip_address,
884 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
886 for (i = 0; i < count; i++)
889 snat_add_address (sm, &this_addr, vrf_id);
891 rv = snat_del_address (sm, this_addr, 0);
896 increment_v4_address (&this_addr);
900 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
903 static void *vl_api_snat_add_address_range_t_print
904 (vl_api_snat_add_address_range_t *mp, void * handle)
908 s = format (0, "SCRIPT: snat_add_address_range ");
909 s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
910 if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
912 s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
918 send_snat_address_details
919 (snat_address_t * a, unix_shared_memory_queue_t * q, u32 context)
921 vl_api_snat_address_details_t *rmp;
922 snat_main_t * sm = &snat_main;
924 rmp = vl_msg_api_alloc (sizeof (*rmp));
925 memset (rmp, 0, sizeof (*rmp));
926 rmp->_vl_msg_id = ntohs (VL_API_SNAT_ADDRESS_DETAILS+sm->msg_id_base);
928 clib_memcpy (rmp->ip_address, &(a->addr), 4);
929 if (a->fib_index != ~0)
930 rmp->vrf_id = ntohl(ip4_fib_get(a->fib_index)->table_id);
933 rmp->context = context;
935 vl_msg_api_send_shmem (q, (u8 *) & rmp);
939 vl_api_snat_address_dump_t_handler
940 (vl_api_snat_address_dump_t * mp)
942 unix_shared_memory_queue_t *q;
943 snat_main_t * sm = &snat_main;
946 q = vl_api_client_index_to_input_queue (mp->client_index);
950 vec_foreach (a, sm->addresses)
951 send_snat_address_details (a, q, mp->context);
954 static void *vl_api_snat_address_dump_t_print
955 (vl_api_snat_address_dump_t *mp, void * handle)
959 s = format (0, "SCRIPT: snat_address_dump ");
965 vl_api_snat_interface_add_del_feature_t_handler
966 (vl_api_snat_interface_add_del_feature_t * mp)
968 snat_main_t * sm = &snat_main;
969 vl_api_snat_interface_add_del_feature_reply_t * rmp;
970 u8 is_del = mp->is_add == 0;
971 u32 sw_if_index = ntohl(mp->sw_if_index);
974 VALIDATE_SW_IF_INDEX(mp);
976 rv = snat_interface_add_del (sw_if_index, mp->is_inside, is_del);
978 BAD_SW_IF_INDEX_LABEL;
980 REPLY_MACRO(VL_API_SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY);
983 static void *vl_api_snat_interface_add_del_feature_t_print
984 (vl_api_snat_interface_add_del_feature_t * mp, void *handle)
988 s = format (0, "SCRIPT: snat_interface_add_del_feature ");
989 s = format (s, "sw_if_index %d %s %s",
990 clib_host_to_net_u32(mp->sw_if_index),
991 mp->is_inside ? "in":"out",
992 mp->is_add ? "" : "del");
998 send_snat_interface_details
999 (snat_interface_t * i, unix_shared_memory_queue_t * q, u32 context)
1001 vl_api_snat_interface_details_t *rmp;
1002 snat_main_t * sm = &snat_main;
1004 rmp = vl_msg_api_alloc (sizeof (*rmp));
1005 memset (rmp, 0, sizeof (*rmp));
1006 rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_DETAILS+sm->msg_id_base);
1007 rmp->sw_if_index = ntohl (i->sw_if_index);
1008 rmp->is_inside = i->is_inside;
1009 rmp->context = context;
1011 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1015 vl_api_snat_interface_dump_t_handler
1016 (vl_api_snat_interface_dump_t * mp)
1018 unix_shared_memory_queue_t *q;
1019 snat_main_t * sm = &snat_main;
1020 snat_interface_t * i;
1022 q = vl_api_client_index_to_input_queue (mp->client_index);
1026 pool_foreach (i, sm->interfaces,
1028 send_snat_interface_details(i, q, mp->context);
1032 static void *vl_api_snat_interface_dump_t_print
1033 (vl_api_snat_interface_dump_t *mp, void * handle)
1037 s = format (0, "SCRIPT: snat_interface_dump ");
1042 vl_api_snat_add_static_mapping_t_handler
1043 (vl_api_snat_add_static_mapping_t * mp)
1045 snat_main_t * sm = &snat_main;
1046 vl_api_snat_add_static_mapping_reply_t * rmp;
1047 ip4_address_t local_addr, external_addr;
1048 u16 local_port = 0, external_port = 0;
1049 u32 vrf_id, external_sw_if_index;
1051 snat_protocol_t proto;
1053 if (mp->is_ip4 != 1)
1055 rv = VNET_API_ERROR_UNIMPLEMENTED;
1059 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
1060 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
1061 if (mp->addr_only == 0)
1063 local_port = clib_net_to_host_u16 (mp->local_port);
1064 external_port = clib_net_to_host_u16 (mp->external_port);
1066 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1067 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
1068 proto = ip_proto_to_snat_proto (mp->protocol);
1070 rv = snat_add_static_mapping(local_addr, external_addr, local_port,
1071 external_port, vrf_id, mp->addr_only,
1072 external_sw_if_index, proto, mp->is_add);
1075 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
1078 static void *vl_api_snat_add_static_mapping_t_print
1079 (vl_api_snat_add_static_mapping_t *mp, void * handle)
1083 s = format (0, "SCRIPT: snat_add_static_mapping ");
1084 s = format (s, "protocol %d local_addr %U external_addr %U ",
1086 format_ip4_address, mp->local_ip_address,
1087 format_ip4_address, mp->external_ip_address);
1089 if (mp->addr_only == 0)
1090 s = format (s, "local_port %d external_port %d ",
1091 clib_net_to_host_u16 (mp->local_port),
1092 clib_net_to_host_u16 (mp->external_port));
1094 if (mp->vrf_id != ~0)
1095 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1097 if (mp->external_sw_if_index != ~0)
1098 s = format (s, "external_sw_if_index %d",
1099 clib_net_to_host_u32 (mp->external_sw_if_index));
1104 send_snat_static_mapping_details
1105 (snat_static_mapping_t * m, unix_shared_memory_queue_t * q, u32 context)
1107 vl_api_snat_static_mapping_details_t *rmp;
1108 snat_main_t * sm = &snat_main;
1110 rmp = vl_msg_api_alloc (sizeof (*rmp));
1111 memset (rmp, 0, sizeof (*rmp));
1112 rmp->_vl_msg_id = ntohs (VL_API_SNAT_STATIC_MAPPING_DETAILS+sm->msg_id_base);
1114 rmp->addr_only = m->addr_only;
1115 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
1116 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
1117 rmp->local_port = htons (m->local_port);
1118 rmp->external_port = htons (m->external_port);
1119 rmp->external_sw_if_index = ~0;
1120 rmp->vrf_id = htonl (m->vrf_id);
1121 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1122 rmp->context = context;
1124 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1128 send_snat_static_map_resolve_details
1129 (snat_static_map_resolve_t * m, unix_shared_memory_queue_t * q, u32 context)
1131 vl_api_snat_static_mapping_details_t *rmp;
1132 snat_main_t * sm = &snat_main;
1134 rmp = vl_msg_api_alloc (sizeof (*rmp));
1135 memset (rmp, 0, sizeof (*rmp));
1136 rmp->_vl_msg_id = ntohs (VL_API_SNAT_STATIC_MAPPING_DETAILS+sm->msg_id_base);
1138 rmp->addr_only = m->addr_only;
1139 clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
1140 rmp->local_port = htons (m->l_port);
1141 rmp->external_port = htons (m->e_port);
1142 rmp->external_sw_if_index = htonl (m->sw_if_index);
1143 rmp->vrf_id = htonl (m->vrf_id);
1144 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1145 rmp->context = context;
1147 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1151 vl_api_snat_static_mapping_dump_t_handler
1152 (vl_api_snat_static_mapping_dump_t * mp)
1154 unix_shared_memory_queue_t *q;
1155 snat_main_t * sm = &snat_main;
1156 snat_static_mapping_t * m;
1157 snat_static_map_resolve_t *rp;
1160 q = vl_api_client_index_to_input_queue (mp->client_index);
1164 pool_foreach (m, sm->static_mappings,
1166 send_snat_static_mapping_details (m, q, mp->context);
1169 for (j = 0; j < vec_len (sm->to_resolve); j++)
1171 rp = sm->to_resolve + j;
1172 send_snat_static_map_resolve_details (rp, q, mp->context);
1176 static void *vl_api_snat_static_mapping_dump_t_print
1177 (vl_api_snat_static_mapping_dump_t *mp, void * handle)
1181 s = format (0, "SCRIPT: snat_static_mapping_dump ");
1187 vl_api_snat_control_ping_t_handler
1188 (vl_api_snat_control_ping_t * mp)
1190 vl_api_snat_control_ping_reply_t *rmp;
1191 snat_main_t * sm = &snat_main;
1194 REPLY_MACRO2(VL_API_SNAT_CONTROL_PING_REPLY,
1196 rmp->vpe_pid = ntohl (getpid());
1200 static void *vl_api_snat_control_ping_t_print
1201 (vl_api_snat_control_ping_t *mp, void * handle)
1205 s = format (0, "SCRIPT: snat_control_ping ");
1211 vl_api_snat_show_config_t_handler
1212 (vl_api_snat_show_config_t * mp)
1214 vl_api_snat_show_config_reply_t *rmp;
1215 snat_main_t * sm = &snat_main;
1218 REPLY_MACRO2(VL_API_SNAT_SHOW_CONFIG_REPLY,
1220 rmp->translation_buckets = htonl (sm->translation_buckets);
1221 rmp->translation_memory_size = htonl (sm->translation_memory_size);
1222 rmp->user_buckets = htonl (sm->user_buckets);
1223 rmp->user_memory_size = htonl (sm->user_memory_size);
1224 rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
1225 rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
1226 rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
1227 rmp->static_mapping_only = sm->static_mapping_only;
1228 rmp->static_mapping_connection_tracking =
1229 sm->static_mapping_connection_tracking;
1230 rmp->deterministic = sm->deterministic;
1234 static void *vl_api_snat_show_config_t_print
1235 (vl_api_snat_show_config_t *mp, void * handle)
1239 s = format (0, "SCRIPT: snat_show_config ");
1245 vl_api_snat_set_workers_t_handler
1246 (vl_api_snat_set_workers_t * mp)
1248 snat_main_t * sm = &snat_main;
1249 vl_api_snat_set_workers_reply_t * rmp;
1252 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
1254 if (sm->num_workers < 2)
1256 rv = VNET_API_ERROR_FEATURE_DISABLED;
1260 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
1261 rv = snat_set_workers(bitmap);
1262 clib_bitmap_free (bitmap);
1265 REPLY_MACRO (VL_API_SNAT_SET_WORKERS_REPLY);
1268 static void *vl_api_snat_set_workers_t_print
1269 (vl_api_snat_set_workers_t *mp, void * handle)
1275 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
1277 s = format (0, "SCRIPT: snat_set_workers ");
1278 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
1279 clib_bitmap_foreach (i, bitmap,
1282 s = format (s, "%d", i);
1284 s = format (s, ",%d", i);
1287 clib_bitmap_free (bitmap);
1292 send_snat_worker_details
1293 (u32 worker_index, unix_shared_memory_queue_t * q, u32 context)
1295 vl_api_snat_worker_details_t *rmp;
1296 snat_main_t * sm = &snat_main;
1297 vlib_worker_thread_t *w =
1298 vlib_worker_threads + worker_index + sm->first_worker_index;
1300 rmp = vl_msg_api_alloc (sizeof (*rmp));
1301 memset (rmp, 0, sizeof (*rmp));
1302 rmp->_vl_msg_id = ntohs (VL_API_SNAT_WORKER_DETAILS+sm->msg_id_base);
1303 rmp->context = context;
1304 rmp->worker_index = htonl (worker_index);
1305 rmp->lcore_id = htonl (w->lcore_id);
1306 strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
1308 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1312 vl_api_snat_worker_dump_t_handler
1313 (vl_api_snat_worker_dump_t * mp)
1315 unix_shared_memory_queue_t *q;
1316 snat_main_t * sm = &snat_main;
1319 q = vl_api_client_index_to_input_queue (mp->client_index);
1323 vec_foreach (worker_index, sm->workers)
1325 send_snat_worker_details(*worker_index, q, mp->context);
1329 static void *vl_api_snat_worker_dump_t_print
1330 (vl_api_snat_worker_dump_t *mp, void * handle)
1334 s = format (0, "SCRIPT: snat_worker_dump ");
1339 static int snat_add_interface_address(snat_main_t *sm,
1344 vl_api_snat_add_del_interface_addr_t_handler
1345 (vl_api_snat_add_del_interface_addr_t * mp)
1347 snat_main_t * sm = &snat_main;
1348 vl_api_snat_add_del_interface_addr_reply_t * rmp;
1349 u8 is_del = mp->is_add == 0;
1350 u32 sw_if_index = ntohl(mp->sw_if_index);
1353 VALIDATE_SW_IF_INDEX(mp);
1355 rv = snat_add_interface_address (sm, sw_if_index, is_del);
1357 BAD_SW_IF_INDEX_LABEL;
1359 REPLY_MACRO(VL_API_SNAT_ADD_DEL_INTERFACE_ADDR_REPLY);
1362 static void *vl_api_snat_add_del_interface_addr_t_print
1363 (vl_api_snat_add_del_interface_addr_t * mp, void *handle)
1367 s = format (0, "SCRIPT: snat_add_del_interface_addr ");
1368 s = format (s, "sw_if_index %d %s",
1369 clib_host_to_net_u32(mp->sw_if_index),
1370 mp->is_add ? "" : "del");
1376 send_snat_interface_addr_details
1377 (u32 sw_if_index, unix_shared_memory_queue_t * q, u32 context)
1379 vl_api_snat_interface_addr_details_t *rmp;
1380 snat_main_t * sm = &snat_main;
1382 rmp = vl_msg_api_alloc (sizeof (*rmp));
1383 memset (rmp, 0, sizeof (*rmp));
1384 rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_ADDR_DETAILS+sm->msg_id_base);
1385 rmp->sw_if_index = ntohl (sw_if_index);
1386 rmp->context = context;
1388 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1392 vl_api_snat_interface_addr_dump_t_handler
1393 (vl_api_snat_interface_addr_dump_t * mp)
1395 unix_shared_memory_queue_t *q;
1396 snat_main_t * sm = &snat_main;
1399 q = vl_api_client_index_to_input_queue (mp->client_index);
1403 vec_foreach (i, sm->auto_add_sw_if_indices)
1404 send_snat_interface_addr_details(*i, q, mp->context);
1407 static void *vl_api_snat_interface_addr_dump_t_print
1408 (vl_api_snat_interface_addr_dump_t *mp, void * handle)
1412 s = format (0, "SCRIPT: snat_interface_addr_dump ");
1418 vl_api_snat_ipfix_enable_disable_t_handler
1419 (vl_api_snat_ipfix_enable_disable_t * mp)
1421 snat_main_t * sm = &snat_main;
1422 vl_api_snat_ipfix_enable_disable_reply_t * rmp;
1425 rv = snat_ipfix_logging_enable_disable(mp->enable,
1426 clib_host_to_net_u32 (mp->domain_id),
1427 clib_host_to_net_u16 (mp->src_port));
1429 REPLY_MACRO (VL_API_SNAT_IPFIX_ENABLE_DISABLE_REPLY);
1432 static void *vl_api_snat_ipfix_enable_disable_t_print
1433 (vl_api_snat_ipfix_enable_disable_t *mp, void * handle)
1437 s = format (0, "SCRIPT: snat_ipfix_enable_disable ");
1439 s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
1441 s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
1443 s = format (s, "disable ");
1449 send_snat_user_details
1450 (snat_user_t * u, unix_shared_memory_queue_t * q, u32 context)
1452 vl_api_snat_user_details_t * rmp;
1453 snat_main_t * sm = &snat_main;
1454 ip4_fib_t * fib_table;
1456 rmp = vl_msg_api_alloc (sizeof (*rmp));
1457 memset (rmp, 0, sizeof (*rmp));
1458 rmp->_vl_msg_id = ntohs (VL_API_SNAT_USER_DETAILS+sm->msg_id_base);
1460 fib_table = ip4_fib_get(u->fib_index);
1461 rmp->vrf_id = ntohl (fib_table->table_id);
1464 clib_memcpy(rmp->ip_address, &(u->addr), 4);
1465 rmp->nsessions = ntohl (u->nsessions);
1466 rmp->nstaticsessions = ntohl (u->nstaticsessions);
1467 rmp->context = context;
1469 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1473 vl_api_snat_user_dump_t_handler
1474 (vl_api_snat_user_dump_t * mp)
1476 unix_shared_memory_queue_t *q;
1477 snat_main_t * sm = &snat_main;
1478 snat_main_per_thread_data_t * tsm;
1481 q = vl_api_client_index_to_input_queue (mp->client_index);
1485 vec_foreach (tsm, sm->per_thread_data)
1486 vec_foreach (u, tsm->users)
1487 send_snat_user_details (u, q, mp->context);
1490 static void *vl_api_snat_user_dump_t_print
1491 (vl_api_snat_user_dump_t *mp, void * handle)
1495 s = format (0, "SCRIPT: snat_user_dump ");
1501 send_snat_user_session_details
1502 (snat_session_t * s, unix_shared_memory_queue_t * q, u32 context)
1504 vl_api_snat_user_session_details_t * rmp;
1505 snat_main_t * sm = &snat_main;
1507 rmp = vl_msg_api_alloc (sizeof(*rmp));
1508 memset (rmp, 0, sizeof (*rmp));
1509 rmp->_vl_msg_id = ntohs (VL_API_SNAT_USER_SESSION_DETAILS+sm->msg_id_base);
1511 clib_memcpy(rmp->outside_ip_address, (&s->out2in.addr), 4);
1512 rmp->outside_port = s->out2in.port;
1513 clib_memcpy(rmp->inside_ip_address, (&s->in2out.addr), 4);
1514 rmp->inside_port = s->in2out.port;
1515 rmp->protocol = ntohs(snat_proto_to_ip_proto(s->in2out.protocol));
1516 rmp->is_static = s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING ? 1 : 0;
1517 rmp->last_heard = clib_host_to_net_u64((u64)s->last_heard);
1518 rmp->total_bytes = clib_host_to_net_u64(s->total_bytes);
1519 rmp->total_pkts = ntohl(s->total_pkts);
1520 rmp->context = context;
1522 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1526 vl_api_snat_user_session_dump_t_handler
1527 (vl_api_snat_user_session_dump_t * mp)
1529 unix_shared_memory_queue_t *q;
1530 snat_main_t * sm = &snat_main;
1531 snat_main_per_thread_data_t *tsm;
1533 clib_bihash_kv_8_8_t key, value;
1534 snat_user_key_t ukey;
1536 u32 session_index, head_index, elt_index;
1537 dlist_elt_t * head, * elt;
1539 q = vl_api_client_index_to_input_queue (mp->client_index);
1543 clib_memcpy (&ukey.addr, mp->ip_address, 4);
1544 ukey.fib_index = ip4_fib_index_from_table_id (ntohl(mp->vrf_id));
1545 key.key = ukey.as_u64;
1546 if (!clib_bihash_search_8_8 (&sm->worker_by_in, &key, &value))
1547 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
1549 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1550 if (clib_bihash_search_8_8 (&sm->user_hash, &key, &value))
1552 u = pool_elt_at_index (tsm->users, value.value);
1553 if (!u->nsessions && !u->nstaticsessions)
1556 head_index = u->sessions_per_user_list_head_index;
1557 head = pool_elt_at_index (tsm->list_pool, head_index);
1558 elt_index = head->next;
1559 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1560 session_index = elt->value;
1561 while (session_index != ~0)
1563 s = pool_elt_at_index (tsm->sessions, session_index);
1565 send_snat_user_session_details (s, q, mp->context);
1567 elt_index = elt->next;
1568 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1569 session_index = elt->value;
1573 static void *vl_api_snat_user_session_dump_t_print
1574 (vl_api_snat_user_session_dump_t *mp, void * handle)
1578 s = format (0, "SCRIPT: snat_user_session_dump ");
1579 s = format (s, "ip_address %U vrf_id %d\n",
1580 format_ip4_address, mp->ip_address,
1581 clib_net_to_host_u32 (mp->vrf_id));
1587 vl_api_snat_add_det_map_t_handler
1588 (vl_api_snat_add_det_map_t * mp)
1590 snat_main_t * sm = &snat_main;
1591 vl_api_snat_add_det_map_reply_t * rmp;
1593 ip4_address_t in_addr, out_addr;
1595 clib_memcpy(&in_addr, mp->in_addr, 4);
1596 clib_memcpy(&out_addr, mp->out_addr, 4);
1597 rv = snat_det_add_map(sm, &in_addr, mp->in_plen, &out_addr,
1598 mp->out_plen, mp->is_add);
1600 REPLY_MACRO (VL_API_SNAT_ADD_DET_MAP_REPLY);
1603 static void *vl_api_snat_add_det_map_t_print
1604 (vl_api_snat_add_det_map_t *mp, void * handle)
1608 s = format (0, "SCRIPT: snat_add_det_map ");
1609 s = format (s, "inside address %U/%d outside address %U/%d\n",
1610 format_ip4_address, mp->in_addr, mp->in_plen,
1611 format_ip4_address, mp->out_addr, mp->out_plen);
1617 vl_api_snat_det_forward_t_handler
1618 (vl_api_snat_det_forward_t * mp)
1620 snat_main_t * sm = &snat_main;
1621 vl_api_snat_det_forward_reply_t * rmp;
1624 snat_det_map_t * dm;
1625 ip4_address_t in_addr, out_addr;
1627 out_addr.as_u32 = 0;
1628 clib_memcpy(&in_addr, mp->in_addr, 4);
1629 dm = snat_det_map_by_user(sm, &in_addr);
1632 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1636 snat_det_forward(dm, &in_addr, &out_addr, &lo_port);
1639 REPLY_MACRO2(VL_API_SNAT_DET_FORWARD_REPLY,
1641 rmp->out_port_lo = ntohs(lo_port);
1642 rmp->out_port_hi = ntohs(lo_port + dm->ports_per_host - 1);
1644 memset(rmp->out_addr, 0, 16);
1645 clib_memcpy(rmp->out_addr, &out_addr, 4);
1649 static void *vl_api_snat_det_forward_t_print
1650 (vl_api_snat_det_forward_t * mp, void * handle)
1654 s = format (0, "SCRIPT: smat_det_forward_t");
1655 s = format (s, "inside ip address %U\n",
1656 format_ip4_address, mp->in_addr);
1662 vl_api_snat_det_reverse_t_handler
1663 (vl_api_snat_det_reverse_t * mp)
1665 snat_main_t * sm = &snat_main;
1666 vl_api_snat_det_reverse_reply_t * rmp;
1668 ip4_address_t out_addr, in_addr;
1669 snat_det_map_t * dm;
1672 clib_memcpy(&out_addr, mp->out_addr, 4);
1673 dm = snat_det_map_by_out(sm, &out_addr);
1676 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1680 snat_det_reverse(dm, &out_addr, htons(mp->out_port), &in_addr);
1683 REPLY_MACRO2(VL_API_SNAT_DET_REVERSE_REPLY,
1686 memset(rmp->in_addr, 0, 16);
1687 clib_memcpy(rmp->in_addr, &in_addr, 4);
1691 static void *vl_api_snat_det_reverse_t_print
1692 (vl_api_snat_det_reverse_t * mp, void * handle)
1696 s = format(0, "SCRIPT: smat_det_reverse_t");
1697 s = format(s, "outside ip address %U outside port %d",
1698 format_ip4_address, mp->out_addr, ntohs(mp->out_port));
1703 /* List of message types that this plugin understands */
1704 #define foreach_snat_plugin_api_msg \
1705 _(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range) \
1706 _(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature) \
1707 _(SNAT_ADD_STATIC_MAPPING, snat_add_static_mapping) \
1708 _(SNAT_CONTROL_PING, snat_control_ping) \
1709 _(SNAT_STATIC_MAPPING_DUMP, snat_static_mapping_dump) \
1710 _(SNAT_SHOW_CONFIG, snat_show_config) \
1711 _(SNAT_ADDRESS_DUMP, snat_address_dump) \
1712 _(SNAT_INTERFACE_DUMP, snat_interface_dump) \
1713 _(SNAT_SET_WORKERS, snat_set_workers) \
1714 _(SNAT_WORKER_DUMP, snat_worker_dump) \
1715 _(SNAT_ADD_DEL_INTERFACE_ADDR, snat_add_del_interface_addr) \
1716 _(SNAT_INTERFACE_ADDR_DUMP, snat_interface_addr_dump) \
1717 _(SNAT_IPFIX_ENABLE_DISABLE, snat_ipfix_enable_disable) \
1718 _(SNAT_USER_DUMP, snat_user_dump) \
1719 _(SNAT_USER_SESSION_DUMP, snat_user_session_dump) \
1720 _(SNAT_ADD_DET_MAP, snat_add_det_map) \
1721 _(SNAT_DET_FORWARD, snat_det_forward) \
1722 _(SNAT_DET_REVERSE, snat_det_reverse)
1724 /* Set up the API message handling tables */
1725 static clib_error_t *
1726 snat_plugin_api_hookup (vlib_main_t *vm)
1728 snat_main_t * sm __attribute__ ((unused)) = &snat_main;
1730 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
1732 vl_api_##n##_t_handler, \
1734 vl_api_##n##_t_endian, \
1735 vl_api_##n##_t_print, \
1736 sizeof(vl_api_##n##_t), 1);
1737 foreach_snat_plugin_api_msg;
1743 #define vl_msg_name_crc_list
1744 #include <snat/snat_all_api_h.h>
1745 #undef vl_msg_name_crc_list
1748 setup_message_id_table (snat_main_t * sm, api_main_t * am)
1750 #define _(id,n,crc) \
1751 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
1752 foreach_vl_msg_name_crc_snat;
1756 static void plugin_custom_dump_configure (snat_main_t * sm)
1758 #define _(n,f) sm->api_main->msg_print_handlers \
1759 [VL_API_##n + sm->msg_id_base] \
1760 = (void *) vl_api_##f##_t_print;
1761 foreach_snat_plugin_api_msg;
1767 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1770 ip4_address_t * address,
1772 u32 if_address_index,
1775 static clib_error_t * snat_init (vlib_main_t * vm)
1777 snat_main_t * sm = &snat_main;
1778 clib_error_t * error = 0;
1779 ip4_main_t * im = &ip4_main;
1780 ip_lookup_main_t * lm = &im->lookup_main;
1783 vlib_thread_registration_t *tr;
1784 vlib_thread_main_t *tm = vlib_get_thread_main ();
1787 ip4_add_del_interface_address_callback_t cb4;
1789 name = format (0, "snat_%08x%c", api_version, 0);
1791 /* Ask for a correctly-sized block of API message decode slots */
1792 sm->msg_id_base = vl_msg_api_get_msg_ids
1793 ((char *) name, VL_MSG_FIRST_AVAILABLE);
1796 sm->vnet_main = vnet_get_main();
1798 sm->ip4_lookup_main = lm;
1799 sm->api_main = &api_main;
1800 sm->first_worker_index = 0;
1801 sm->next_worker = 0;
1802 sm->num_workers = 0;
1804 sm->fq_in2out_index = ~0;
1805 sm->fq_out2in_index = ~0;
1807 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1810 tr = (vlib_thread_registration_t *) p[0];
1813 sm->num_workers = tr->count;
1814 sm->first_worker_index = tr->first_index;
1818 /* Use all available workers by default */
1819 if (sm->num_workers > 1)
1821 for (i=0; i < sm->num_workers; i++)
1822 bitmap = clib_bitmap_set (bitmap, i, 1);
1823 snat_set_workers(bitmap);
1824 clib_bitmap_free (bitmap);
1827 error = snat_plugin_api_hookup (vm);
1829 /* Add our API messages to the global name_crc hash table */
1830 setup_message_id_table (sm, &api_main);
1832 plugin_custom_dump_configure (sm);
1835 /* Set up the interface address add/del callback */
1836 cb4.function = snat_ip4_add_del_interface_address_cb;
1837 cb4.function_opaque = 0;
1839 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1841 /* Init IPFIX logging */
1842 snat_ipfix_logging_init(vm);
1847 VLIB_INIT_FUNCTION (snat_init);
1849 void snat_free_outside_address_and_port (snat_main_t * sm,
1850 snat_session_key_t * k,
1854 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1856 ASSERT (address_index < vec_len (sm->addresses));
1858 a = sm->addresses + address_index;
1860 switch (k->protocol)
1862 #define _(N, i, n, s) \
1863 case SNAT_PROTOCOL_##N: \
1864 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
1865 port_host_byte_order) == 1); \
1866 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
1867 port_host_byte_order, 0); \
1868 a->busy_##n##_ports--; \
1870 foreach_snat_protocol
1873 clib_warning("unknown_protocol");
1879 * @brief Match SNAT static mapping.
1881 * @param sm SNAT main.
1882 * @param match Address and port to match.
1883 * @param mapping External or local address and port of the matched mapping.
1884 * @param by_external If 0 match by local address otherwise match by external
1887 * @returns 0 if match found otherwise 1.
1889 int snat_static_mapping_match (snat_main_t * sm,
1890 snat_session_key_t match,
1891 snat_session_key_t * mapping,
1894 clib_bihash_kv_8_8_t kv, value;
1895 snat_static_mapping_t *m;
1896 snat_session_key_t m_key;
1897 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1900 mapping_hash = &sm->static_mapping_by_external;
1902 m_key.addr = match.addr;
1903 m_key.port = clib_net_to_host_u16 (match.port);
1904 m_key.protocol = match.protocol;
1905 m_key.fib_index = match.fib_index;
1907 kv.key = m_key.as_u64;
1909 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1911 /* Try address only mapping */
1914 kv.key = m_key.as_u64;
1915 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1919 m = pool_elt_at_index (sm->static_mappings, value.value);
1923 mapping->addr = m->local_addr;
1924 /* Address only mapping doesn't change port */
1925 mapping->port = m->addr_only ? match.port
1926 : clib_host_to_net_u16 (m->local_port);
1927 mapping->fib_index = m->fib_index;
1931 mapping->addr = m->external_addr;
1932 /* Address only mapping doesn't change port */
1933 mapping->port = m->addr_only ? match.port
1934 : clib_host_to_net_u16 (m->external_port);
1935 mapping->fib_index = sm->outside_fib_index;
1941 int snat_alloc_outside_address_and_port (snat_main_t * sm,
1943 snat_session_key_t * k,
1944 u32 * address_indexp)
1950 for (i = 0; i < vec_len (sm->addresses); i++)
1952 a = sm->addresses + i;
1953 if (sm->vrf_mode && a->fib_index != ~0 && a->fib_index != fib_index)
1955 switch (k->protocol)
1957 #define _(N, j, n, s) \
1958 case SNAT_PROTOCOL_##N: \
1959 if (a->busy_##n##_ports < (65535-1024)) \
1963 portnum = random_u32 (&sm->random_seed); \
1964 portnum &= 0xFFFF; \
1965 if (portnum < 1024) \
1967 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
1969 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
1970 a->busy_##n##_ports++; \
1971 k->addr = a->addr; \
1972 k->port = clib_host_to_net_u16(portnum); \
1973 *address_indexp = i; \
1978 foreach_snat_protocol
1981 clib_warning("unknown protocol");
1986 /* Totally out of translations to use... */
1987 snat_ipfix_logging_addresses_exhausted(0);
1992 static clib_error_t *
1993 add_address_command_fn (vlib_main_t * vm,
1994 unformat_input_t * input,
1995 vlib_cli_command_t * cmd)
1997 unformat_input_t _line_input, *line_input = &_line_input;
1998 snat_main_t * sm = &snat_main;
1999 ip4_address_t start_addr, end_addr, this_addr;
2000 u32 start_host_order, end_host_order;
2005 clib_error_t *error = 0;
2007 /* Get a line of input. */
2008 if (!unformat_user (input, unformat_line_input, line_input))
2011 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2013 if (unformat (line_input, "%U - %U",
2014 unformat_ip4_address, &start_addr,
2015 unformat_ip4_address, &end_addr))
2017 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
2019 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
2020 end_addr = start_addr;
2021 else if (unformat (line_input, "del"))
2025 error = clib_error_return (0, "unknown input '%U'",
2026 format_unformat_error, line_input);
2031 if (sm->static_mapping_only)
2033 error = clib_error_return (0, "static mapping only mode");
2037 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
2038 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
2040 if (end_host_order < start_host_order)
2042 error = clib_error_return (0, "end address less than start address");
2046 count = (end_host_order - start_host_order) + 1;
2049 clib_warning ("%U - %U, %d addresses...",
2050 format_ip4_address, &start_addr,
2051 format_ip4_address, &end_addr,
2054 this_addr = start_addr;
2056 for (i = 0; i < count; i++)
2059 snat_add_address (sm, &this_addr, vrf_id);
2061 rv = snat_del_address (sm, this_addr, 0);
2065 case VNET_API_ERROR_NO_SUCH_ENTRY:
2066 error = clib_error_return (0, "S-NAT address not exist.");
2068 case VNET_API_ERROR_UNSPECIFIED:
2069 error = clib_error_return (0, "S-NAT address used in static mapping.");
2075 increment_v4_address (&this_addr);
2079 unformat_free (line_input);
2084 VLIB_CLI_COMMAND (add_address_command, static) = {
2085 .path = "snat add address",
2086 .short_help = "snat add addresses <ip4-range-start> [- <ip4-range-end>] "
2087 "[tenant-vrf <vrf-id>] [del]",
2088 .function = add_address_command_fn,
2091 static clib_error_t *
2092 snat_feature_command_fn (vlib_main_t * vm,
2093 unformat_input_t * input,
2094 vlib_cli_command_t * cmd)
2096 unformat_input_t _line_input, *line_input = &_line_input;
2097 vnet_main_t * vnm = vnet_get_main();
2098 clib_error_t * error = 0;
2100 u32 * inside_sw_if_indices = 0;
2101 u32 * outside_sw_if_indices = 0;
2107 /* Get a line of input. */
2108 if (!unformat_user (input, unformat_line_input, line_input))
2111 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2113 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
2115 vec_add1 (inside_sw_if_indices, sw_if_index);
2116 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
2118 vec_add1 (outside_sw_if_indices, sw_if_index);
2119 else if (unformat (line_input, "del"))
2123 error = clib_error_return (0, "unknown input '%U'",
2124 format_unformat_error, line_input);
2129 if (vec_len (inside_sw_if_indices))
2131 for (i = 0; i < vec_len(inside_sw_if_indices); i++)
2133 sw_if_index = inside_sw_if_indices[i];
2134 snat_interface_add_del (sw_if_index, 1, is_del);
2138 if (vec_len (outside_sw_if_indices))
2140 for (i = 0; i < vec_len(outside_sw_if_indices); i++)
2142 sw_if_index = outside_sw_if_indices[i];
2143 snat_interface_add_del (sw_if_index, 0, is_del);
2148 unformat_free (line_input);
2149 vec_free (inside_sw_if_indices);
2150 vec_free (outside_sw_if_indices);
2155 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
2156 .path = "set interface snat",
2157 .function = snat_feature_command_fn,
2158 .short_help = "set interface snat in <intfc> out <intfc> [del]",
2162 unformat_snat_protocol (unformat_input_t * input, va_list * args)
2164 u32 *r = va_arg (*args, u32 *);
2167 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
2168 foreach_snat_protocol
2176 format_snat_protocol (u8 * s, va_list * args)
2178 u32 i = va_arg (*args, u32);
2183 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
2184 foreach_snat_protocol
2187 s = format (s, "unknown");
2189 s = format (s, "%s", t);
2193 static clib_error_t *
2194 add_static_mapping_command_fn (vlib_main_t * vm,
2195 unformat_input_t * input,
2196 vlib_cli_command_t * cmd)
2198 unformat_input_t _line_input, *line_input = &_line_input;
2199 clib_error_t * error = 0;
2200 ip4_address_t l_addr, e_addr;
2201 u32 l_port = 0, e_port = 0, vrf_id = ~0;
2204 u32 sw_if_index = ~0;
2205 vnet_main_t * vnm = vnet_get_main();
2207 snat_protocol_t proto;
2210 /* Get a line of input. */
2211 if (!unformat_user (input, unformat_line_input, line_input))
2214 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2216 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
2219 else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
2221 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
2224 else if (unformat (line_input, "external %U", unformat_ip4_address,
2227 else if (unformat (line_input, "external %U %u",
2228 unformat_vnet_sw_interface, vnm, &sw_if_index,
2232 else if (unformat (line_input, "external %U",
2233 unformat_vnet_sw_interface, vnm, &sw_if_index))
2235 else if (unformat (line_input, "vrf %u", &vrf_id))
2237 else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
2239 else if (unformat (line_input, "del"))
2243 error = clib_error_return (0, "unknown input: '%U'",
2244 format_unformat_error, line_input);
2249 if (!addr_only && !proto_set)
2251 error = clib_error_return (0, "missing protocol");
2255 rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
2256 vrf_id, addr_only, sw_if_index, proto, is_add);
2260 case VNET_API_ERROR_INVALID_VALUE:
2261 error = clib_error_return (0, "External port already in use.");
2263 case VNET_API_ERROR_NO_SUCH_ENTRY:
2265 error = clib_error_return (0, "External addres must be allocated.");
2267 error = clib_error_return (0, "Mapping not exist.");
2269 case VNET_API_ERROR_NO_SUCH_FIB:
2270 error = clib_error_return (0, "No such VRF id.");
2272 case VNET_API_ERROR_VALUE_EXIST:
2273 error = clib_error_return (0, "Mapping already exist.");
2280 unformat_free (line_input);
2287 * @cliexstart{snat add static mapping}
2288 * Static mapping allows hosts on the external network to initiate connection
2289 * to to the local network host.
2290 * To create static mapping between local host address 10.0.0.3 port 6303 and
2291 * external address 4.4.4.4 port 3606 for TCP protocol use:
2292 * vpp# snat add static mapping local tcp 10.0.0.3 6303 external 4.4.4.4 3606
2293 * If not runnig "static mapping only" S-NAT plugin mode use before:
2294 * vpp# snat add address 4.4.4.4
2295 * To create static mapping between local and external address use:
2296 * vpp# snat add static mapping local 10.0.0.3 external 4.4.4.4
2299 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
2300 .path = "snat add static mapping",
2301 .function = add_static_mapping_command_fn,
2303 "snat add static mapping local tcp|udp|icmp <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
2306 static clib_error_t *
2307 set_workers_command_fn (vlib_main_t * vm,
2308 unformat_input_t * input,
2309 vlib_cli_command_t * cmd)
2311 unformat_input_t _line_input, *line_input = &_line_input;
2314 clib_error_t *error = 0;
2316 /* Get a line of input. */
2317 if (!unformat_user (input, unformat_line_input, line_input))
2320 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2322 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
2326 error = clib_error_return (0, "unknown input '%U'",
2327 format_unformat_error, line_input);
2334 error = clib_error_return (0, "List of workers must be specified.");
2338 rv = snat_set_workers(bitmap);
2340 clib_bitmap_free (bitmap);
2344 case VNET_API_ERROR_INVALID_WORKER:
2345 error = clib_error_return (0, "Invalid worker(s).");
2347 case VNET_API_ERROR_FEATURE_DISABLED:
2348 error = clib_error_return (0,
2349 "Supported only if 2 or more workes available.");
2356 unformat_free (line_input);
2363 * @cliexstart{set snat workers}
2364 * Set SNAT workers if 2 or more workers available, use:
2365 * vpp# set snat workers 0-2,5
2368 VLIB_CLI_COMMAND (set_workers_command, static) = {
2369 .path = "set snat workers",
2370 .function = set_workers_command_fn,
2372 "set snat workers <workers-list>",
2375 static clib_error_t *
2376 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
2377 unformat_input_t * input,
2378 vlib_cli_command_t * cmd)
2380 unformat_input_t _line_input, *line_input = &_line_input;
2385 clib_error_t *error = 0;
2387 /* Get a line of input. */
2388 if (!unformat_user (input, unformat_line_input, line_input))
2391 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2393 if (unformat (line_input, "domain %d", &domain_id))
2395 else if (unformat (line_input, "src-port %d", &src_port))
2397 else if (unformat (line_input, "disable"))
2401 error = clib_error_return (0, "unknown input '%U'",
2402 format_unformat_error, line_input);
2407 rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
2411 error = clib_error_return (0, "ipfix logging enable failed");
2416 unformat_free (line_input);
2423 * @cliexstart{snat ipfix logging}
2424 * To enable SNAT IPFIX logging use:
2425 * vpp# snat ipfix logging
2426 * To set IPFIX exporter use:
2427 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
2430 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
2431 .path = "snat ipfix logging",
2432 .function = snat_ipfix_logging_enable_disable_command_fn,
2433 .short_help = "snat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
2437 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2439 snat_main_t *sm = &snat_main;
2440 snat_user_key_t key0;
2441 clib_bihash_kv_8_8_t kv0, value0;
2442 u32 next_worker_index = 0;
2444 key0.addr = ip0->src_address;
2445 key0.fib_index = rx_fib_index0;
2447 kv0.key = key0.as_u64;
2449 /* Ever heard of of the "user" before? */
2450 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv0, &value0))
2452 /* No, assign next available worker (RR) */
2453 next_worker_index = sm->first_worker_index;
2454 if (vec_len (sm->workers))
2456 next_worker_index +=
2457 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
2460 /* add non-traslated packets worker lookup */
2461 kv0.value = next_worker_index;
2462 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv0, 1);
2465 next_worker_index = value0.value;
2467 return next_worker_index;
2471 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2473 snat_main_t *sm = &snat_main;
2474 snat_worker_key_t key0;
2475 clib_bihash_kv_8_8_t kv0, value0;
2476 udp_header_t * udp0;
2477 u32 next_worker_index = 0;
2479 udp0 = ip4_next_header (ip0);
2481 key0.addr = ip0->dst_address;
2482 key0.port = udp0->dst_port;
2483 key0.fib_index = rx_fib_index0;
2485 if (PREDICT_FALSE(ip0->protocol == IP_PROTOCOL_ICMP))
2487 icmp46_header_t * icmp0 = (icmp46_header_t *) udp0;
2488 icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
2489 key0.port = echo0->identifier;
2492 kv0.key = key0.as_u64;
2494 /* Ever heard of of the "user" before? */
2495 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
2498 kv0.key = key0.as_u64;
2500 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
2502 /* No, assign next available worker (RR) */
2503 next_worker_index = sm->first_worker_index;
2504 if (vec_len (sm->workers))
2506 next_worker_index +=
2507 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
2512 /* Static mapping without port */
2513 next_worker_index = value0.value;
2516 /* Add to translated packets worker lookup */
2517 kv0.value = next_worker_index;
2518 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv0, 1);
2521 next_worker_index = value0.value;
2523 return next_worker_index;
2526 static clib_error_t *
2527 snat_config (vlib_main_t * vm, unformat_input_t * input)
2529 snat_main_t * sm = &snat_main;
2530 u32 translation_buckets = 1024;
2531 u32 translation_memory_size = 128<<20;
2532 u32 user_buckets = 128;
2533 u32 user_memory_size = 64<<20;
2534 u32 max_translations_per_user = 100;
2535 u32 outside_vrf_id = 0;
2536 u32 inside_vrf_id = 0;
2537 u32 static_mapping_buckets = 1024;
2538 u32 static_mapping_memory_size = 64<<20;
2539 u8 static_mapping_only = 0;
2540 u8 static_mapping_connection_tracking = 0;
2541 vlib_thread_main_t *tm = vlib_get_thread_main ();
2543 sm->deterministic = 0;
2545 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2547 if (unformat (input, "translation hash buckets %d", &translation_buckets))
2549 else if (unformat (input, "translation hash memory %d",
2550 &translation_memory_size));
2551 else if (unformat (input, "user hash buckets %d", &user_buckets))
2553 else if (unformat (input, "user hash memory %d",
2556 else if (unformat (input, "max translations per user %d",
2557 &max_translations_per_user))
2559 else if (unformat (input, "outside VRF id %d",
2562 else if (unformat (input, "inside VRF id %d",
2565 else if (unformat (input, "static mapping only"))
2567 static_mapping_only = 1;
2568 if (unformat (input, "connection tracking"))
2569 static_mapping_connection_tracking = 1;
2571 else if (unformat (input, "deterministic"))
2572 sm->deterministic = 1;
2574 return clib_error_return (0, "unknown input '%U'",
2575 format_unformat_error, input);
2578 /* for show commands, etc. */
2579 sm->translation_buckets = translation_buckets;
2580 sm->translation_memory_size = translation_memory_size;
2581 sm->user_buckets = user_buckets;
2582 sm->user_memory_size = user_memory_size;
2583 sm->max_translations_per_user = max_translations_per_user;
2584 sm->outside_vrf_id = outside_vrf_id;
2585 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2587 sm->inside_vrf_id = inside_vrf_id;
2588 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2590 sm->static_mapping_only = static_mapping_only;
2591 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
2593 if (sm->deterministic)
2595 sm->in2out_node_index = snat_det_in2out_node.index;
2596 sm->out2in_node_index = snat_det_out2in_node.index;
2600 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2601 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
2602 sm->in2out_node_index = snat_in2out_node.index;
2603 sm->out2in_node_index = snat_out2in_node.index;
2604 if (!static_mapping_only ||
2605 (static_mapping_only && static_mapping_connection_tracking))
2607 clib_bihash_init_8_8 (&sm->worker_by_in, "worker-by-in", user_buckets,
2610 clib_bihash_init_8_8 (&sm->worker_by_out, "worker-by-out", user_buckets,
2613 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
2615 clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
2616 translation_memory_size);
2618 clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
2619 translation_memory_size);
2621 clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
2624 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2625 "static_mapping_by_local", static_mapping_buckets,
2626 static_mapping_memory_size);
2628 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2629 "static_mapping_by_external", static_mapping_buckets,
2630 static_mapping_memory_size);
2636 VLIB_CONFIG_FUNCTION (snat_config, "snat");
2638 u8 * format_snat_session_state (u8 * s, va_list * args)
2640 u32 i = va_arg (*args, u32);
2645 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
2646 foreach_snat_session_state
2649 t = format (t, "unknown");
2651 s = format (s, "%s", t);
2655 u8 * format_snat_key (u8 * s, va_list * args)
2657 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
2658 char * protocol_string = "unknown";
2659 static char *protocol_strings[] = {
2665 if (key->protocol < ARRAY_LEN(protocol_strings))
2666 protocol_string = protocol_strings[key->protocol];
2668 s = format (s, "%U proto %s port %d fib %d",
2669 format_ip4_address, &key->addr, protocol_string,
2670 clib_net_to_host_u16 (key->port), key->fib_index);
2674 u8 * format_snat_session (u8 * s, va_list * args)
2676 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
2677 snat_session_t * sess = va_arg (*args, snat_session_t *);
2679 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
2680 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
2681 s = format (s, " last heard %.2f\n", sess->last_heard);
2682 s = format (s, " total pkts %d, total bytes %lld\n",
2683 sess->total_pkts, sess->total_bytes);
2684 if (snat_is_session_static (sess))
2685 s = format (s, " static translation\n");
2687 s = format (s, " dynamic translation\n");
2692 u8 * format_snat_user (u8 * s, va_list * args)
2694 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
2695 snat_user_t * u = va_arg (*args, snat_user_t *);
2696 int verbose = va_arg (*args, int);
2697 dlist_elt_t * head, * elt;
2698 u32 elt_index, head_index;
2700 snat_session_t * sess;
2702 s = format (s, "%U: %d dynamic translations, %d static translations\n",
2703 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
2708 if (u->nsessions || u->nstaticsessions)
2710 head_index = u->sessions_per_user_list_head_index;
2711 head = pool_elt_at_index (sm->list_pool, head_index);
2713 elt_index = head->next;
2714 elt = pool_elt_at_index (sm->list_pool, elt_index);
2715 session_index = elt->value;
2717 while (session_index != ~0)
2719 sess = pool_elt_at_index (sm->sessions, session_index);
2721 s = format (s, " %U\n", format_snat_session, sm, sess);
2723 elt_index = elt->next;
2724 elt = pool_elt_at_index (sm->list_pool, elt_index);
2725 session_index = elt->value;
2732 u8 * format_snat_static_mapping (u8 * s, va_list * args)
2734 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
2737 s = format (s, "local %U external %U vrf %d",
2738 format_ip4_address, &m->local_addr,
2739 format_ip4_address, &m->external_addr,
2742 s = format (s, "%U local %U:%d external %U:%d vrf %d",
2743 format_snat_protocol, m->proto,
2744 format_ip4_address, &m->local_addr, m->local_port,
2745 format_ip4_address, &m->external_addr, m->external_port,
2751 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
2753 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
2754 vnet_main_t *vnm = vnet_get_main();
2757 s = format (s, "local %U external %U vrf %d",
2758 format_ip4_address, &m->l_addr,
2759 format_vnet_sw_interface_name, vnm,
2760 vnet_get_sw_interface (vnm, m->sw_if_index),
2763 s = format (s, "%U local %U:%d external %U:%d vrf %d",
2764 format_snat_protocol, m->proto,
2765 format_ip4_address, &m->l_addr, m->l_port,
2766 format_vnet_sw_interface_name, vnm,
2767 vnet_get_sw_interface (vnm, m->sw_if_index), m->e_port,
2773 u8 * format_det_map_ses (u8 * s, va_list * args)
2775 snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
2776 ip4_address_t in_addr, out_addr;
2777 u32 in_offset, out_offset;
2778 snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
2779 u32 * i = va_arg (*args, u32 *);
2781 u32 user_index = *i / SNAT_DET_SES_PER_USER;
2782 in_addr.as_u32 = clib_host_to_net_u32 (
2783 clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
2784 in_offset = clib_net_to_host_u32(in_addr.as_u32) -
2785 clib_net_to_host_u32(det_map->in_addr.as_u32);
2786 out_offset = in_offset / det_map->sharing_ratio;
2787 out_addr.as_u32 = clib_host_to_net_u32(
2788 clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
2789 s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
2790 format_ip4_address, &in_addr,
2791 clib_net_to_host_u16 (ses->in_port),
2792 format_ip4_address, &out_addr,
2793 clib_net_to_host_u16 (ses->out.out_port),
2794 format_ip4_address, &ses->out.ext_host_addr,
2795 clib_net_to_host_u16 (ses->out.ext_host_port),
2796 format_snat_session_state, ses->state,
2802 static clib_error_t *
2803 show_snat_command_fn (vlib_main_t * vm,
2804 unformat_input_t * input,
2805 vlib_cli_command_t * cmd)
2808 snat_main_t * sm = &snat_main;
2810 snat_static_mapping_t *m;
2811 snat_interface_t *i;
2812 snat_address_t * ap;
2813 vnet_main_t *vnm = vnet_get_main();
2814 snat_main_per_thread_data_t *tsm;
2815 u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
2817 snat_static_map_resolve_t *rp;
2818 snat_det_map_t * dm;
2819 snat_det_session_t * ses;
2821 if (unformat (input, "detail"))
2823 else if (unformat (input, "verbose"))
2826 if (sm->static_mapping_only)
2828 if (sm->static_mapping_connection_tracking)
2829 vlib_cli_output (vm, "SNAT mode: static mapping only connection "
2832 vlib_cli_output (vm, "SNAT mode: static mapping only");
2834 else if (sm->deterministic)
2836 vlib_cli_output (vm, "SNAT mode: deterministic mapping");
2840 vlib_cli_output (vm, "SNAT mode: dynamic translations enabled");
2845 pool_foreach (i, sm->interfaces,
2847 vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
2848 vnet_get_sw_interface (vnm, i->sw_if_index),
2849 i->is_inside ? "in" : "out");
2852 if (vec_len (sm->auto_add_sw_if_indices))
2854 vlib_cli_output (vm, "SNAT pool addresses interfaces:");
2855 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
2857 vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
2858 vnet_get_sw_interface (vnm, *sw_if_index));
2862 vec_foreach (ap, sm->addresses)
2864 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
2865 if (ap->fib_index != ~0)
2866 vlib_cli_output (vm, " tenant VRF: %u",
2867 ip4_fib_get(ap->fib_index)->table_id);
2869 vlib_cli_output (vm, " tenant VRF independent");
2870 #define _(N, i, n, s) \
2871 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
2872 foreach_snat_protocol
2877 if (sm->num_workers > 1)
2879 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
2882 vec_foreach (worker, sm->workers)
2884 vlib_worker_thread_t *w =
2885 vlib_worker_threads + *worker + sm->first_worker_index;
2886 vlib_cli_output (vm, " %s", w->name);
2891 if (sm->deterministic)
2893 vlib_cli_output (vm, "%d deterministic mappings",
2894 pool_elts (sm->det_maps));
2897 pool_foreach (dm, sm->det_maps,
2899 vlib_cli_output (vm, "in %U/%d out %U/%d\n",
2900 format_ip4_address, &dm->in_addr, dm->in_plen,
2901 format_ip4_address, &dm->out_addr, dm->out_plen);
2902 vlib_cli_output (vm, " outside address sharing ratio: %d\n",
2904 vlib_cli_output (vm, " number of ports per inside host: %d\n",
2905 dm->ports_per_host);
2906 vlib_cli_output (vm, " sessions number: %d\n", dm->ses_num);
2909 vec_foreach_index (j, dm->sessions)
2911 ses = vec_elt_at_index (dm->sessions, j);
2913 vlib_cli_output (vm, " %U", format_det_map_ses, dm, ses,
2922 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2924 vlib_cli_output (vm, "%d static mappings",
2925 pool_elts (sm->static_mappings));
2929 pool_foreach (m, sm->static_mappings,
2931 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2937 vec_foreach (tsm, sm->per_thread_data)
2939 users_num += pool_elts (tsm->users);
2940 sessions_num += pool_elts (tsm->sessions);
2943 vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
2944 " %d static mappings",
2946 vec_len (sm->addresses),
2948 pool_elts (sm->static_mappings));
2952 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
2954 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
2956 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_in,
2958 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_out,
2960 vec_foreach_index (j, sm->per_thread_data)
2962 tsm = vec_elt_at_index (sm->per_thread_data, j);
2964 if (pool_elts (tsm->users) == 0)
2967 vlib_worker_thread_t *w = vlib_worker_threads + j;
2968 vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
2970 vlib_cli_output (vm, " %d list pool elements",
2971 pool_elts (tsm->list_pool));
2973 pool_foreach (u, tsm->users,
2975 vlib_cli_output (vm, " %U", format_snat_user, tsm, u,
2980 if (pool_elts (sm->static_mappings))
2982 vlib_cli_output (vm, "static mappings:");
2983 pool_foreach (m, sm->static_mappings,
2985 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2987 for (j = 0; j < vec_len (sm->to_resolve); j++)
2989 rp = sm->to_resolve + j;
2990 vlib_cli_output (vm, "%U",
2991 format_snat_static_map_to_resolve, rp);
3000 VLIB_CLI_COMMAND (show_snat_command, static) = {
3001 .path = "show snat",
3002 .short_help = "show snat",
3003 .function = show_snat_command_fn,
3008 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
3011 ip4_address_t * address,
3013 u32 if_address_index,
3016 snat_main_t *sm = &snat_main;
3017 snat_static_map_resolve_t *rp;
3018 u32 *indices_to_delete = 0;
3022 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
3024 if (sw_if_index == sm->auto_add_sw_if_indices[i])
3028 /* Don't trip over lease renewal, static config */
3029 for (j = 0; j < vec_len(sm->addresses); j++)
3030 if (sm->addresses[j].addr.as_u32 == address->as_u32)
3033 snat_add_address (sm, address, ~0);
3034 /* Scan static map resolution vector */
3035 for (j = 0; j < vec_len (sm->to_resolve); j++)
3037 rp = sm->to_resolve + j;
3038 /* On this interface? */
3039 if (rp->sw_if_index == sw_if_index)
3041 /* Add the static mapping */
3042 rv = snat_add_static_mapping (rp->l_addr,
3048 ~0 /* sw_if_index */,
3052 clib_warning ("snat_add_static_mapping returned %d",
3054 vec_add1 (indices_to_delete, j);
3057 /* If we resolved any of the outstanding static mappings */
3058 if (vec_len(indices_to_delete))
3061 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
3062 vec_delete(sm->to_resolve, 1, j);
3063 vec_free(indices_to_delete);
3069 (void) snat_del_address(sm, address[0], 1);
3077 static int snat_add_interface_address (snat_main_t *sm,
3081 ip4_main_t * ip4_main = sm->ip4_main;
3082 ip4_address_t * first_int_addr;
3083 snat_static_map_resolve_t *rp;
3084 u32 *indices_to_delete = 0;
3087 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
3088 0 /* just want the address*/);
3090 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
3092 if (sm->auto_add_sw_if_indices[i] == sw_if_index)
3096 /* if have address remove it */
3098 (void) snat_del_address (sm, first_int_addr[0], 1);
3101 for (j = 0; j < vec_len (sm->to_resolve); j++)
3103 rp = sm->to_resolve + j;
3104 if (rp->sw_if_index == sw_if_index)
3105 vec_add1 (indices_to_delete, j);
3107 if (vec_len(indices_to_delete))
3109 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
3110 vec_del1(sm->to_resolve, j);
3111 vec_free(indices_to_delete);
3114 vec_del1(sm->auto_add_sw_if_indices, i);
3117 return VNET_API_ERROR_VALUE_EXIST;
3124 return VNET_API_ERROR_NO_SUCH_ENTRY;
3126 /* add to the auto-address list */
3127 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
3129 /* If the address is already bound - or static - add it now */
3131 snat_add_address (sm, first_int_addr, ~0);
3136 static clib_error_t *
3137 snat_add_interface_address_command_fn (vlib_main_t * vm,
3138 unformat_input_t * input,
3139 vlib_cli_command_t * cmd)
3141 snat_main_t *sm = &snat_main;
3142 unformat_input_t _line_input, *line_input = &_line_input;
3146 clib_error_t *error = 0;
3148 /* Get a line of input. */
3149 if (!unformat_user (input, unformat_line_input, line_input))
3152 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3154 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
3155 sm->vnet_main, &sw_if_index))
3157 else if (unformat (line_input, "del"))
3161 error = clib_error_return (0, "unknown input '%U'",
3162 format_unformat_error, line_input);
3167 rv = snat_add_interface_address (sm, sw_if_index, is_del);
3175 error = clib_error_return (0, "snat_add_interface_address returned %d",
3181 unformat_free (line_input);
3186 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
3187 .path = "snat add interface address",
3188 .short_help = "snat add interface address <interface> [del]",
3189 .function = snat_add_interface_address_command_fn,
3192 static clib_error_t *
3193 snat_det_map_command_fn (vlib_main_t * vm,
3194 unformat_input_t * input,
3195 vlib_cli_command_t * cmd)
3197 snat_main_t *sm = &snat_main;
3198 unformat_input_t _line_input, *line_input = &_line_input;
3199 ip4_address_t in_addr, out_addr;
3200 u32 in_plen, out_plen;
3202 clib_error_t *error = 0;
3204 /* Get a line of input. */
3205 if (!unformat_user (input, unformat_line_input, line_input))
3208 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3210 if (unformat (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
3212 else if (unformat (line_input, "out %U/%u", unformat_ip4_address, &out_addr, &out_plen))
3214 else if (unformat (line_input, "del"))
3218 error = clib_error_return (0, "unknown input '%U'",
3219 format_unformat_error, line_input);
3224 unformat_free (line_input);
3226 rv = snat_det_add_map(sm, &in_addr, (u8) in_plen, &out_addr, (u8)out_plen,
3231 error = clib_error_return (0, "snat_det_add_map return %d", rv);
3236 unformat_free (line_input);
3243 * @cliexstart{snat deterministic add}
3244 * Create bijective mapping of inside address to outside address and port range
3245 * pairs, with the purpose of enabling deterministic NAT to reduce logging in
3247 * To create deterministic mapping between inside network 10.0.0.0/18 and
3248 * outside network 1.1.1.0/30 use:
3249 * # vpp# snat deterministic add in 10.0.0.0/18 out 1.1.1.0/30
3252 VLIB_CLI_COMMAND (snat_det_map_command, static) = {
3253 .path = "snat deterministic add",
3254 .short_help = "snat deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
3255 .function = snat_det_map_command_fn,
3258 static clib_error_t *
3259 snat_det_forward_command_fn (vlib_main_t * vm,
3260 unformat_input_t * input,
3261 vlib_cli_command_t * cmd)
3263 snat_main_t *sm = &snat_main;
3264 unformat_input_t _line_input, *line_input = &_line_input;
3265 ip4_address_t in_addr, out_addr;
3267 snat_det_map_t * dm;
3268 clib_error_t *error = 0;
3270 /* Get a line of input. */
3271 if (!unformat_user (input, unformat_line_input, line_input))
3274 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3276 if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
3280 error = clib_error_return (0, "unknown input '%U'",
3281 format_unformat_error, line_input);
3286 unformat_free (line_input);
3288 dm = snat_det_map_by_user(sm, &in_addr);
3290 vlib_cli_output (vm, "no match");
3293 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
3294 vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
3295 lo_port, lo_port + dm->ports_per_host - 1);
3299 unformat_free (line_input);
3306 * @cliexstart{snat deterministic forward}
3307 * Return outside address and port range from inside address for deterministic
3309 * To obtain outside address and port of inside host use:
3310 * vpp# snat deterministic forward 10.0.0.2
3311 * 1.1.1.0:<1054-1068>
3314 VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
3315 .path = "snat deterministic forward",
3316 .short_help = "snat deterministic forward <addr>",
3317 .function = snat_det_forward_command_fn,
3320 static clib_error_t *
3321 snat_det_reverse_command_fn (vlib_main_t * vm,
3322 unformat_input_t * input,
3323 vlib_cli_command_t * cmd)
3325 snat_main_t *sm = &snat_main;
3326 unformat_input_t _line_input, *line_input = &_line_input;
3327 ip4_address_t in_addr, out_addr;
3329 snat_det_map_t * dm;
3330 clib_error_t *error = 0;
3332 /* Get a line of input. */
3333 if (!unformat_user (input, unformat_line_input, line_input))
3336 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3338 if (unformat (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
3342 error = clib_error_return (0, "unknown input '%U'",
3343 format_unformat_error, line_input);
3347 unformat_free (line_input);
3349 if (out_port < 1024 || out_port > 65535)
3351 error = clib_error_return (0, "wrong port, must be <1024-65535>");
3355 dm = snat_det_map_by_out(sm, &out_addr);
3357 vlib_cli_output (vm, "no match");
3360 snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
3361 vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
3365 unformat_free (line_input);
3372 * @cliexstart{snat deterministic reverse}
3373 * Return inside address from outside address and port for deterministic NAT.
3374 * To obtain inside host address from outside address and port use:
3375 * #vpp snat deterministic reverse 1.1.1.1:1276
3379 VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
3380 .path = "snat deterministic reverse",
3381 .short_help = "snat deterministic reverse <addr>:<port>",
3382 .function = snat_det_reverse_command_fn,