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>
31 #include <vpp/app/version.h>
33 snat_main_t snat_main;
35 /* define message IDs */
36 #include <snat/snat_msg_enum.h>
38 /* define message structures */
40 #include <snat/snat_all_api_h.h>
43 /* define generated endian-swappers */
45 #include <snat/snat_all_api_h.h>
48 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
50 /* Get the API version number */
51 #define vl_api_version(n,v) static u32 api_version=(v);
52 #include <snat/snat_all_api_h.h>
55 /* Macro to finish up custom dump fns */
58 vl_print (handle, (char *)s); \
63 * A handy macro to set up a message reply.
64 * Assumes that the following variables are available:
65 * mp - pointer to request message
66 * rmp - pointer to reply message type
70 #define REPLY_MACRO(t) \
72 unix_shared_memory_queue_t * q = \
73 vl_api_client_index_to_input_queue (mp->client_index); \
77 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
78 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
79 rmp->context = mp->context; \
80 rmp->retval = ntohl(rv); \
82 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
85 #define REPLY_MACRO2(t, body) \
87 unix_shared_memory_queue_t * q = \
88 vl_api_client_index_to_input_queue (mp->client_index); \
92 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
93 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
94 rmp->context = mp->context; \
95 rmp->retval = ntohl(rv); \
96 do {body;} while (0); \
97 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
101 /* Hook up input features */
102 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
103 .arc_name = "ip4-unicast",
104 .node_name = "snat-in2out",
105 .runs_before = VNET_FEATURES ("snat-out2in"),
107 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
108 .arc_name = "ip4-unicast",
109 .node_name = "snat-out2in",
110 .runs_before = VNET_FEATURES ("ip4-lookup"),
112 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
113 .arc_name = "ip4-unicast",
114 .node_name = "snat-in2out-worker-handoff",
115 .runs_before = VNET_FEATURES ("snat-out2in-worker-handoff"),
117 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
118 .arc_name = "ip4-unicast",
119 .node_name = "snat-out2in-worker-handoff",
120 .runs_before = VNET_FEATURES ("ip4-lookup"),
122 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
123 .arc_name = "ip4-unicast",
124 .node_name = "snat-in2out-fast",
125 .runs_before = VNET_FEATURES ("snat-out2in-fast"),
127 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
128 .arc_name = "ip4-unicast",
129 .node_name = "snat-out2in-fast",
130 .runs_before = VNET_FEATURES ("ip4-lookup"),
134 VLIB_PLUGIN_REGISTER () = {
135 .version = VPP_BUILD_VER,
139 /*$$$$$ move to an installed header file */
140 #if (1 || CLIB_DEBUG > 0) /* "trust, but verify" */
142 #define VALIDATE_SW_IF_INDEX(mp) \
143 do { u32 __sw_if_index = ntohl(mp->sw_if_index); \
144 vnet_main_t *__vnm = vnet_get_main(); \
145 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
147 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
148 goto bad_sw_if_index; \
152 #define BAD_SW_IF_INDEX_LABEL \
158 #define VALIDATE_RX_SW_IF_INDEX(mp) \
159 do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index); \
160 vnet_main_t *__vnm = vnet_get_main(); \
161 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
162 __rx_sw_if_index)) { \
163 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
164 goto bad_rx_sw_if_index; \
168 #define BAD_RX_SW_IF_INDEX_LABEL \
170 bad_rx_sw_if_index: \
174 #define VALIDATE_TX_SW_IF_INDEX(mp) \
175 do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index); \
176 vnet_main_t *__vnm = vnet_get_main(); \
177 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
178 __tx_sw_if_index)) { \
179 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
180 goto bad_tx_sw_if_index; \
184 #define BAD_TX_SW_IF_INDEX_LABEL \
186 bad_tx_sw_if_index: \
192 #define VALIDATE_SW_IF_INDEX(mp)
193 #define BAD_SW_IF_INDEX_LABEL
194 #define VALIDATE_RX_SW_IF_INDEX(mp)
195 #define BAD_RX_SW_IF_INDEX_LABEL
196 #define VALIDATE_TX_SW_IF_INDEX(mp)
197 #define BAD_TX_SW_IF_INDEX_LABEL
199 #endif /* CLIB_DEBUG > 0 */
202 * @brief Add/del NAT address to FIB.
204 * Add the external NAT address to the FIB as receive entries. This ensures
205 * that VPP will reply to ARP for this address and we don't need to enable
206 * proxy ARP on the outside interface.
208 * @param addr IPv4 address.
209 * @param sw_if_index Interface.
210 * @param is_add If 0 delete, otherwise add.
213 snat_add_del_addr_to_fib (ip4_address_t * addr, u32 sw_if_index, int is_add)
215 fib_prefix_t prefix = {
217 .fp_proto = FIB_PROTOCOL_IP4,
219 .ip4.as_u32 = addr->as_u32,
222 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
225 fib_table_entry_update_one_path(fib_index,
227 FIB_SOURCE_PLUGIN_HI,
228 (FIB_ENTRY_FLAG_CONNECTED |
229 FIB_ENTRY_FLAG_LOCAL |
230 FIB_ENTRY_FLAG_EXCLUSIVE),
237 FIB_ROUTE_PATH_FLAG_NONE);
239 fib_table_entry_delete(fib_index,
241 FIB_SOURCE_PLUGIN_HI);
244 void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id)
252 /* Check if address already exists */
253 vec_foreach (ap, sm->addresses)
255 if (ap->addr.as_u32 == addr->as_u32)
259 vec_add2 (sm->addresses, ap, 1);
261 ap->fib_index = ip4_fib_index_from_table_id(vrf_id);
262 #define _(N, i, n, s) \
263 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535);
264 foreach_snat_protocol
267 /* Add external address to FIB */
268 pool_foreach (i, sm->interfaces,
273 snat_add_del_addr_to_fib(addr, i->sw_if_index, 1);
278 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
281 snat_static_mapping_t *m;
282 pool_foreach (m, sm->static_mappings,
284 if (m->external_addr.as_u32 == addr.as_u32)
291 static void increment_v4_address (ip4_address_t * a)
295 v = clib_net_to_host_u32(a->as_u32) + 1;
296 a->as_u32 = clib_host_to_net_u32(v);
300 snat_add_static_mapping_when_resolved (snat_main_t * sm,
301 ip4_address_t l_addr,
306 snat_protocol_t proto,
310 snat_static_map_resolve_t *rp;
312 vec_add2 (sm->to_resolve, rp, 1);
313 rp->l_addr.as_u32 = l_addr.as_u32;
315 rp->sw_if_index = sw_if_index;
319 rp->addr_only = addr_only;
324 * @brief Add static mapping.
326 * Create static mapping between local addr+port and external addr+port.
328 * @param l_addr Local IPv4 address.
329 * @param e_addr External IPv4 address.
330 * @param l_port Local port number.
331 * @param e_port External port number.
332 * @param vrf_id VRF ID.
333 * @param addr_only If 0 address port and pair mapping, otherwise address only.
334 * @param sw_if_index External port instead of specific IP address.
335 * @param is_add If 0 delete static mapping, otherwise add.
339 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
340 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
341 u32 sw_if_index, snat_protocol_t proto, int is_add)
343 snat_main_t * sm = &snat_main;
344 snat_static_mapping_t *m;
345 snat_session_key_t m_key;
346 clib_bihash_kv_8_8_t kv, value;
347 snat_address_t *a = 0;
350 snat_interface_t *interface;
353 /* If the external address is a specific interface address */
354 if (sw_if_index != ~0)
356 ip4_address_t * first_int_addr;
358 /* Might be already set... */
359 first_int_addr = ip4_interface_first_address
360 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
362 /* DHCP resolution required? */
363 if (first_int_addr == 0)
365 snat_add_static_mapping_when_resolved
366 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
371 e_addr.as_u32 = first_int_addr->as_u32;
375 m_key.port = addr_only ? 0 : e_port;
376 m_key.protocol = addr_only ? 0 : proto;
377 m_key.fib_index = sm->outside_fib_index;
378 kv.key = m_key.as_u64;
379 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
382 m = pool_elt_at_index (sm->static_mappings, value.value);
387 return VNET_API_ERROR_VALUE_EXIST;
389 /* Convert VRF id to FIB index */
392 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
394 return VNET_API_ERROR_NO_SUCH_FIB;
397 /* If not specified use inside VRF id from SNAT plugin startup config */
400 fib_index = sm->inside_fib_index;
401 vrf_id = sm->inside_vrf_id;
404 /* Find external address in allocated addresses and reserve port for
405 address and port pair mapping when dynamic translations enabled */
406 if (!addr_only && !(sm->static_mapping_only))
408 for (i = 0; i < vec_len (sm->addresses); i++)
410 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
412 a = sm->addresses + i;
413 /* External port must be unused */
416 #define _(N, j, n, s) \
417 case SNAT_PROTOCOL_##N: \
418 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
419 return VNET_API_ERROR_INVALID_VALUE; \
420 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
422 a->busy_##n##_ports++; \
424 foreach_snat_protocol
427 clib_warning("unknown_protocol");
428 return VNET_API_ERROR_INVALID_VALUE_2;
433 /* External address must be allocated */
435 return VNET_API_ERROR_NO_SUCH_ENTRY;
438 pool_get (sm->static_mappings, m);
439 memset (m, 0, sizeof (*m));
440 m->local_addr = l_addr;
441 m->external_addr = e_addr;
442 m->addr_only = addr_only;
444 m->fib_index = fib_index;
447 m->local_port = l_port;
448 m->external_port = e_port;
452 m_key.addr = m->local_addr;
453 m_key.port = m->local_port;
454 m_key.protocol = m->proto;
455 m_key.fib_index = m->fib_index;
456 kv.key = m_key.as_u64;
457 kv.value = m - sm->static_mappings;
458 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
460 m_key.addr = m->external_addr;
461 m_key.port = m->external_port;
462 m_key.fib_index = sm->outside_fib_index;
463 kv.key = m_key.as_u64;
464 kv.value = m - sm->static_mappings;
465 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
470 snat_user_key_t w_key0;
471 snat_worker_key_t w_key1;
473 w_key0.addr = m->local_addr;
474 w_key0.fib_index = m->fib_index;
475 kv.key = w_key0.as_u64;
477 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
479 kv.value = sm->first_worker_index +
480 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
482 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv, 1);
486 kv.value = value.value;
489 w_key1.addr = m->external_addr;
490 w_key1.port = clib_host_to_net_u16 (m->external_port);
491 w_key1.fib_index = sm->outside_fib_index;
492 kv.key = w_key1.as_u64;
493 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv, 1);
499 return VNET_API_ERROR_NO_SUCH_ENTRY;
501 /* Free external address port */
502 if (!addr_only && !(sm->static_mapping_only))
504 for (i = 0; i < vec_len (sm->addresses); i++)
506 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
508 a = sm->addresses + i;
511 #define _(N, j, n, s) \
512 case SNAT_PROTOCOL_##N: \
513 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
515 a->busy_##n##_ports--; \
517 foreach_snat_protocol
520 clib_warning("unknown_protocol");
521 return VNET_API_ERROR_INVALID_VALUE_2;
528 m_key.addr = m->local_addr;
529 m_key.port = m->local_port;
530 m_key.protocol = m->proto;
531 m_key.fib_index = m->fib_index;
532 kv.key = m_key.as_u64;
533 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
535 m_key.addr = m->external_addr;
536 m_key.port = m->external_port;
537 m_key.fib_index = sm->outside_fib_index;
538 kv.key = m_key.as_u64;
539 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
541 /* Delete session(s) for static mapping if exist */
542 if (!(sm->static_mapping_only) ||
543 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
545 snat_user_key_t u_key;
547 dlist_elt_t * head, * elt;
548 u32 elt_index, head_index, del_elt_index;
552 snat_main_per_thread_data_t *tsm;
554 u_key.addr = m->local_addr;
555 u_key.fib_index = m->fib_index;
556 kv.key = u_key.as_u64;
557 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
559 user_index = value.value;
560 if (!clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
561 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
563 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
564 u = pool_elt_at_index (tsm->users, user_index);
565 if (u->nstaticsessions)
567 head_index = u->sessions_per_user_list_head_index;
568 head = pool_elt_at_index (tsm->list_pool, head_index);
569 elt_index = head->next;
570 elt = pool_elt_at_index (tsm->list_pool, elt_index);
571 ses_index = elt->value;
572 while (ses_index != ~0)
574 s = pool_elt_at_index (tsm->sessions, ses_index);
575 del_elt_index = elt_index;
576 elt_index = elt->next;
577 elt = pool_elt_at_index (tsm->list_pool, elt_index);
578 ses_index = elt->value;
582 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
583 (clib_net_to_host_u16 (s->out2in.port) != e_port))
588 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
589 s->out2in.addr.as_u32,
593 s->in2out.fib_index);
595 value.key = s->in2out.as_u64;
596 clib_bihash_add_del_8_8 (&sm->in2out, &value, 0);
597 value.key = s->out2in.as_u64;
598 clib_bihash_add_del_8_8 (&sm->out2in, &value, 0);
599 pool_put (tsm->sessions, s);
601 clib_dlist_remove (tsm->list_pool, del_elt_index);
602 pool_put_index (tsm->list_pool, del_elt_index);
603 u->nstaticsessions--;
610 pool_put (tsm->users, u);
611 clib_bihash_add_del_8_8 (&sm->user_hash, &kv, 0);
617 /* Delete static mapping from pool */
618 pool_put (sm->static_mappings, m);
624 /* Add/delete external address to FIB */
625 pool_foreach (interface, sm->interfaces,
627 if (interface->is_inside)
630 snat_add_del_addr_to_fib(&e_addr, interface->sw_if_index, is_add);
637 int snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm)
639 snat_address_t *a = 0;
641 u32 *ses_to_be_removed = 0, *ses_index;
642 clib_bihash_kv_8_8_t kv, value;
643 snat_user_key_t user_key;
645 snat_main_per_thread_data_t *tsm;
646 snat_static_mapping_t *m;
647 snat_interface_t *interface;
650 /* Find SNAT address */
651 for (i=0; i < vec_len (sm->addresses); i++)
653 if (sm->addresses[i].addr.as_u32 == addr.as_u32)
655 a = sm->addresses + i;
660 return VNET_API_ERROR_NO_SUCH_ENTRY;
664 pool_foreach (m, sm->static_mappings,
666 if (m->external_addr.as_u32 == addr.as_u32)
667 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
668 m->local_port, m->external_port,
669 m->vrf_id, m->addr_only, ~0,
675 /* Check if address is used in some static mapping */
676 if (is_snat_address_used_in_static_mapping(sm, addr))
678 clib_warning ("address used in static mapping");
679 return VNET_API_ERROR_UNSPECIFIED;
683 /* Delete sessions using address */
684 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
686 vec_foreach (tsm, sm->per_thread_data)
688 pool_foreach (ses, tsm->sessions, ({
689 if (ses->out2in.addr.as_u32 == addr.as_u32)
692 snat_ipfix_logging_nat44_ses_delete(ses->in2out.addr.as_u32,
693 ses->out2in.addr.as_u32,
694 ses->in2out.protocol,
697 ses->in2out.fib_index);
698 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
699 kv.key = ses->in2out.as_u64;
700 clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
701 kv.key = ses->out2in.as_u64;
702 clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
703 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
704 user_key.addr = ses->in2out.addr;
705 user_key.fib_index = ses->in2out.fib_index;
706 kv.key = user_key.as_u64;
707 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
709 u = pool_elt_at_index (tsm->users, value.value);
715 vec_foreach (ses_index, ses_to_be_removed)
716 pool_put_index (tsm->sessions, ses_index[0]);
718 vec_free (ses_to_be_removed);
722 vec_del1 (sm->addresses, i);
724 /* Delete external address from FIB */
725 pool_foreach (interface, sm->interfaces,
727 if (interface->is_inside)
730 snat_add_del_addr_to_fib(&addr, interface->sw_if_index, 0);
737 static int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
739 snat_main_t *sm = &snat_main;
741 const char * feature_name;
743 snat_static_mapping_t * m;
745 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
746 feature_name = is_inside ? "snat-in2out-fast" : "snat-out2in-fast";
749 if (sm->num_workers > 1)
750 feature_name = is_inside ? "snat-in2out-worker-handoff" : "snat-out2in-worker-handoff";
752 feature_name = is_inside ? "snat-in2out" : "snat-out2in";
755 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
758 if (sm->fq_in2out_index == ~0)
759 sm->fq_in2out_index = vlib_frame_queue_main_init (snat_in2out_node.index, 0);
761 if (sm->fq_out2in_index == ~0)
762 sm->fq_out2in_index = vlib_frame_queue_main_init (snat_out2in_node.index, 0);
764 pool_foreach (i, sm->interfaces,
766 if (i->sw_if_index == sw_if_index)
769 pool_put (sm->interfaces, i);
771 return VNET_API_ERROR_VALUE_EXIST;
778 return VNET_API_ERROR_NO_SUCH_ENTRY;
780 pool_get (sm->interfaces, i);
781 i->sw_if_index = sw_if_index;
782 i->is_inside = is_inside;
784 /* Add/delete external addresses to FIB */
789 vec_foreach (ap, sm->addresses)
790 snat_add_del_addr_to_fib(&ap->addr, sw_if_index, !is_del);
792 pool_foreach (m, sm->static_mappings,
797 snat_add_del_addr_to_fib(&m->external_addr, sw_if_index, !is_del);
803 static int snat_set_workers (uword * bitmap)
805 snat_main_t *sm = &snat_main;
808 if (sm->num_workers < 2)
809 return VNET_API_ERROR_FEATURE_DISABLED;
811 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
812 return VNET_API_ERROR_INVALID_WORKER;
814 vec_free (sm->workers);
815 clib_bitmap_foreach (i, bitmap,
817 vec_add1(sm->workers, i);
824 vl_api_snat_add_address_range_t_handler
825 (vl_api_snat_add_address_range_t * mp)
827 snat_main_t * sm = &snat_main;
828 vl_api_snat_add_address_range_reply_t * rmp;
829 ip4_address_t this_addr;
830 u32 start_host_order, end_host_order;
838 rv = VNET_API_ERROR_UNIMPLEMENTED;
842 if (sm->static_mapping_only)
844 rv = VNET_API_ERROR_FEATURE_DISABLED;
848 tmp = (u32 *) mp->first_ip_address;
849 start_host_order = clib_host_to_net_u32 (tmp[0]);
850 tmp = (u32 *) mp->last_ip_address;
851 end_host_order = clib_host_to_net_u32 (tmp[0]);
853 count = (end_host_order - start_host_order) + 1;
855 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
858 clib_warning ("%U - %U, %d addresses...",
859 format_ip4_address, mp->first_ip_address,
860 format_ip4_address, mp->last_ip_address,
863 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
865 for (i = 0; i < count; i++)
868 snat_add_address (sm, &this_addr, vrf_id);
870 rv = snat_del_address (sm, this_addr, 0);
875 increment_v4_address (&this_addr);
879 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
882 static void *vl_api_snat_add_address_range_t_print
883 (vl_api_snat_add_address_range_t *mp, void * handle)
887 s = format (0, "SCRIPT: snat_add_address_range ");
888 s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
889 if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
891 s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
897 send_snat_address_details
898 (snat_address_t * a, unix_shared_memory_queue_t * q, u32 context)
900 vl_api_snat_address_details_t *rmp;
901 snat_main_t * sm = &snat_main;
903 rmp = vl_msg_api_alloc (sizeof (*rmp));
904 memset (rmp, 0, sizeof (*rmp));
905 rmp->_vl_msg_id = ntohs (VL_API_SNAT_ADDRESS_DETAILS+sm->msg_id_base);
907 clib_memcpy (rmp->ip_address, &(a->addr), 4);
908 if (a->fib_index != ~0)
909 rmp->vrf_id = ntohl(ip4_fib_get(a->fib_index)->table_id);
912 rmp->context = context;
914 vl_msg_api_send_shmem (q, (u8 *) & rmp);
918 vl_api_snat_address_dump_t_handler
919 (vl_api_snat_address_dump_t * mp)
921 unix_shared_memory_queue_t *q;
922 snat_main_t * sm = &snat_main;
925 q = vl_api_client_index_to_input_queue (mp->client_index);
929 vec_foreach (a, sm->addresses)
930 send_snat_address_details (a, q, mp->context);
933 static void *vl_api_snat_address_dump_t_print
934 (vl_api_snat_address_dump_t *mp, void * handle)
938 s = format (0, "SCRIPT: snat_address_dump ");
944 vl_api_snat_interface_add_del_feature_t_handler
945 (vl_api_snat_interface_add_del_feature_t * mp)
947 snat_main_t * sm = &snat_main;
948 vl_api_snat_interface_add_del_feature_reply_t * rmp;
949 u8 is_del = mp->is_add == 0;
950 u32 sw_if_index = ntohl(mp->sw_if_index);
953 VALIDATE_SW_IF_INDEX(mp);
955 rv = snat_interface_add_del (sw_if_index, mp->is_inside, is_del);
957 BAD_SW_IF_INDEX_LABEL;
959 REPLY_MACRO(VL_API_SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY);
962 static void *vl_api_snat_interface_add_del_feature_t_print
963 (vl_api_snat_interface_add_del_feature_t * mp, void *handle)
967 s = format (0, "SCRIPT: snat_interface_add_del_feature ");
968 s = format (s, "sw_if_index %d %s %s",
969 clib_host_to_net_u32(mp->sw_if_index),
970 mp->is_inside ? "in":"out",
971 mp->is_add ? "" : "del");
977 send_snat_interface_details
978 (snat_interface_t * i, unix_shared_memory_queue_t * q, u32 context)
980 vl_api_snat_interface_details_t *rmp;
981 snat_main_t * sm = &snat_main;
983 rmp = vl_msg_api_alloc (sizeof (*rmp));
984 memset (rmp, 0, sizeof (*rmp));
985 rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_DETAILS+sm->msg_id_base);
986 rmp->sw_if_index = ntohl (i->sw_if_index);
987 rmp->is_inside = i->is_inside;
988 rmp->context = context;
990 vl_msg_api_send_shmem (q, (u8 *) & rmp);
994 vl_api_snat_interface_dump_t_handler
995 (vl_api_snat_interface_dump_t * mp)
997 unix_shared_memory_queue_t *q;
998 snat_main_t * sm = &snat_main;
999 snat_interface_t * i;
1001 q = vl_api_client_index_to_input_queue (mp->client_index);
1005 pool_foreach (i, sm->interfaces,
1007 send_snat_interface_details(i, q, mp->context);
1011 static void *vl_api_snat_interface_dump_t_print
1012 (vl_api_snat_interface_dump_t *mp, void * handle)
1016 s = format (0, "SCRIPT: snat_interface_dump ");
1021 vl_api_snat_add_static_mapping_t_handler
1022 (vl_api_snat_add_static_mapping_t * mp)
1024 snat_main_t * sm = &snat_main;
1025 vl_api_snat_add_static_mapping_reply_t * rmp;
1026 ip4_address_t local_addr, external_addr;
1027 u16 local_port = 0, external_port = 0;
1028 u32 vrf_id, external_sw_if_index;
1030 snat_protocol_t proto;
1032 if (mp->is_ip4 != 1)
1034 rv = VNET_API_ERROR_UNIMPLEMENTED;
1038 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
1039 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
1040 if (mp->addr_only == 0)
1042 local_port = clib_net_to_host_u16 (mp->local_port);
1043 external_port = clib_net_to_host_u16 (mp->external_port);
1045 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1046 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
1047 proto = ip_proto_to_snat_proto (mp->protocol);
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, proto, mp->is_add);
1054 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
1057 static void *vl_api_snat_add_static_mapping_t_print
1058 (vl_api_snat_add_static_mapping_t *mp, void * handle)
1062 s = format (0, "SCRIPT: snat_add_static_mapping ");
1063 s = format (s, "protocol %d 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->external_sw_if_index = ~0;
1099 rmp->vrf_id = htonl (m->vrf_id);
1100 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1101 rmp->context = context;
1103 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1107 send_snat_static_map_resolve_details
1108 (snat_static_map_resolve_t * m, unix_shared_memory_queue_t * q, u32 context)
1110 vl_api_snat_static_mapping_details_t *rmp;
1111 snat_main_t * sm = &snat_main;
1113 rmp = vl_msg_api_alloc (sizeof (*rmp));
1114 memset (rmp, 0, sizeof (*rmp));
1115 rmp->_vl_msg_id = ntohs (VL_API_SNAT_STATIC_MAPPING_DETAILS+sm->msg_id_base);
1117 rmp->addr_only = m->addr_only;
1118 clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
1119 rmp->local_port = htons (m->l_port);
1120 rmp->external_port = htons (m->e_port);
1121 rmp->external_sw_if_index = htonl (m->sw_if_index);
1122 rmp->vrf_id = htonl (m->vrf_id);
1123 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1124 rmp->context = context;
1126 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1130 vl_api_snat_static_mapping_dump_t_handler
1131 (vl_api_snat_static_mapping_dump_t * mp)
1133 unix_shared_memory_queue_t *q;
1134 snat_main_t * sm = &snat_main;
1135 snat_static_mapping_t * m;
1136 snat_static_map_resolve_t *rp;
1139 q = vl_api_client_index_to_input_queue (mp->client_index);
1143 pool_foreach (m, sm->static_mappings,
1145 send_snat_static_mapping_details (m, q, mp->context);
1148 for (j = 0; j < vec_len (sm->to_resolve); j++)
1150 rp = sm->to_resolve + j;
1151 send_snat_static_map_resolve_details (rp, q, mp->context);
1155 static void *vl_api_snat_static_mapping_dump_t_print
1156 (vl_api_snat_static_mapping_dump_t *mp, void * handle)
1160 s = format (0, "SCRIPT: snat_static_mapping_dump ");
1166 vl_api_snat_control_ping_t_handler
1167 (vl_api_snat_control_ping_t * mp)
1169 vl_api_snat_control_ping_reply_t *rmp;
1170 snat_main_t * sm = &snat_main;
1173 REPLY_MACRO2(VL_API_SNAT_CONTROL_PING_REPLY,
1175 rmp->vpe_pid = ntohl (getpid());
1179 static void *vl_api_snat_control_ping_t_print
1180 (vl_api_snat_control_ping_t *mp, void * handle)
1184 s = format (0, "SCRIPT: snat_control_ping ");
1190 vl_api_snat_show_config_t_handler
1191 (vl_api_snat_show_config_t * mp)
1193 vl_api_snat_show_config_reply_t *rmp;
1194 snat_main_t * sm = &snat_main;
1197 REPLY_MACRO2(VL_API_SNAT_SHOW_CONFIG_REPLY,
1199 rmp->translation_buckets = htonl (sm->translation_buckets);
1200 rmp->translation_memory_size = htonl (sm->translation_memory_size);
1201 rmp->user_buckets = htonl (sm->user_buckets);
1202 rmp->user_memory_size = htonl (sm->user_memory_size);
1203 rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
1204 rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
1205 rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
1206 rmp->static_mapping_only = sm->static_mapping_only;
1207 rmp->static_mapping_connection_tracking =
1208 sm->static_mapping_connection_tracking;
1212 static void *vl_api_snat_show_config_t_print
1213 (vl_api_snat_show_config_t *mp, void * handle)
1217 s = format (0, "SCRIPT: snat_show_config ");
1223 vl_api_snat_set_workers_t_handler
1224 (vl_api_snat_set_workers_t * mp)
1226 snat_main_t * sm = &snat_main;
1227 vl_api_snat_set_workers_reply_t * rmp;
1230 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
1232 if (sm->num_workers < 2)
1234 rv = VNET_API_ERROR_FEATURE_DISABLED;
1238 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
1239 rv = snat_set_workers(bitmap);
1240 clib_bitmap_free (bitmap);
1243 REPLY_MACRO (VL_API_SNAT_SET_WORKERS_REPLY);
1246 static void *vl_api_snat_set_workers_t_print
1247 (vl_api_snat_set_workers_t *mp, void * handle)
1253 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
1255 s = format (0, "SCRIPT: snat_set_workers ");
1256 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
1257 clib_bitmap_foreach (i, bitmap,
1260 s = format (s, "%d", i);
1262 s = format (s, ",%d", i);
1265 clib_bitmap_free (bitmap);
1270 send_snat_worker_details
1271 (u32 worker_index, unix_shared_memory_queue_t * q, u32 context)
1273 vl_api_snat_worker_details_t *rmp;
1274 snat_main_t * sm = &snat_main;
1275 vlib_worker_thread_t *w =
1276 vlib_worker_threads + worker_index + sm->first_worker_index;
1278 rmp = vl_msg_api_alloc (sizeof (*rmp));
1279 memset (rmp, 0, sizeof (*rmp));
1280 rmp->_vl_msg_id = ntohs (VL_API_SNAT_WORKER_DETAILS+sm->msg_id_base);
1281 rmp->context = context;
1282 rmp->worker_index = htonl (worker_index);
1283 rmp->lcore_id = htonl (w->lcore_id);
1284 strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
1286 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1290 vl_api_snat_worker_dump_t_handler
1291 (vl_api_snat_worker_dump_t * mp)
1293 unix_shared_memory_queue_t *q;
1294 snat_main_t * sm = &snat_main;
1297 q = vl_api_client_index_to_input_queue (mp->client_index);
1301 vec_foreach (worker_index, sm->workers)
1303 send_snat_worker_details(*worker_index, q, mp->context);
1307 static void *vl_api_snat_worker_dump_t_print
1308 (vl_api_snat_worker_dump_t *mp, void * handle)
1312 s = format (0, "SCRIPT: snat_worker_dump ");
1317 static int snat_add_interface_address(snat_main_t *sm,
1322 vl_api_snat_add_del_interface_addr_t_handler
1323 (vl_api_snat_add_del_interface_addr_t * mp)
1325 snat_main_t * sm = &snat_main;
1326 vl_api_snat_add_del_interface_addr_reply_t * rmp;
1327 u8 is_del = mp->is_add == 0;
1328 u32 sw_if_index = ntohl(mp->sw_if_index);
1331 VALIDATE_SW_IF_INDEX(mp);
1333 rv = snat_add_interface_address (sm, sw_if_index, is_del);
1335 BAD_SW_IF_INDEX_LABEL;
1337 REPLY_MACRO(VL_API_SNAT_ADD_DEL_INTERFACE_ADDR_REPLY);
1340 static void *vl_api_snat_add_del_interface_addr_t_print
1341 (vl_api_snat_add_del_interface_addr_t * mp, void *handle)
1345 s = format (0, "SCRIPT: snat_add_del_interface_addr ");
1346 s = format (s, "sw_if_index %d %s",
1347 clib_host_to_net_u32(mp->sw_if_index),
1348 mp->is_add ? "" : "del");
1354 send_snat_interface_addr_details
1355 (u32 sw_if_index, unix_shared_memory_queue_t * q, u32 context)
1357 vl_api_snat_interface_addr_details_t *rmp;
1358 snat_main_t * sm = &snat_main;
1360 rmp = vl_msg_api_alloc (sizeof (*rmp));
1361 memset (rmp, 0, sizeof (*rmp));
1362 rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_ADDR_DETAILS+sm->msg_id_base);
1363 rmp->sw_if_index = ntohl (sw_if_index);
1364 rmp->context = context;
1366 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1370 vl_api_snat_interface_addr_dump_t_handler
1371 (vl_api_snat_interface_addr_dump_t * mp)
1373 unix_shared_memory_queue_t *q;
1374 snat_main_t * sm = &snat_main;
1377 q = vl_api_client_index_to_input_queue (mp->client_index);
1381 vec_foreach (i, sm->auto_add_sw_if_indices)
1382 send_snat_interface_addr_details(*i, q, mp->context);
1385 static void *vl_api_snat_interface_addr_dump_t_print
1386 (vl_api_snat_interface_addr_dump_t *mp, void * handle)
1390 s = format (0, "SCRIPT: snat_interface_addr_dump ");
1396 vl_api_snat_ipfix_enable_disable_t_handler
1397 (vl_api_snat_ipfix_enable_disable_t * mp)
1399 snat_main_t * sm = &snat_main;
1400 vl_api_snat_ipfix_enable_disable_reply_t * rmp;
1403 rv = snat_ipfix_logging_enable_disable(mp->enable,
1404 clib_host_to_net_u32 (mp->domain_id),
1405 clib_host_to_net_u16 (mp->src_port));
1407 REPLY_MACRO (VL_API_SNAT_IPFIX_ENABLE_DISABLE_REPLY);
1410 static void *vl_api_snat_ipfix_enable_disable_t_print
1411 (vl_api_snat_ipfix_enable_disable_t *mp, void * handle)
1415 s = format (0, "SCRIPT: snat_ipfix_enable_disable ");
1417 s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
1419 s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
1421 s = format (s, "disable ");
1427 send_snat_user_details
1428 (snat_user_t * u, unix_shared_memory_queue_t * q, u32 context)
1430 vl_api_snat_user_details_t * rmp;
1431 snat_main_t * sm = &snat_main;
1432 ip4_fib_t * fib_table;
1434 rmp = vl_msg_api_alloc (sizeof (*rmp));
1435 memset (rmp, 0, sizeof (*rmp));
1436 rmp->_vl_msg_id = ntohs (VL_API_SNAT_USER_DETAILS+sm->msg_id_base);
1438 fib_table = ip4_fib_get(u->fib_index);
1439 rmp->vrf_id = ntohl (fib_table->table_id);
1442 clib_memcpy(rmp->ip_address, &(u->addr), 4);
1443 rmp->nsessions = ntohl (u->nsessions);
1444 rmp->nstaticsessions = ntohl (u->nstaticsessions);
1445 rmp->context = context;
1447 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1451 vl_api_snat_user_dump_t_handler
1452 (vl_api_snat_user_dump_t * mp)
1454 unix_shared_memory_queue_t *q;
1455 snat_main_t * sm = &snat_main;
1456 snat_main_per_thread_data_t * tsm;
1459 q = vl_api_client_index_to_input_queue (mp->client_index);
1463 vec_foreach (tsm, sm->per_thread_data)
1464 vec_foreach (u, tsm->users)
1465 send_snat_user_details (u, q, mp->context);
1468 static void *vl_api_snat_user_dump_t_print
1469 (vl_api_snat_user_dump_t *mp, void * handle)
1473 s = format (0, "SCRIPT: snat_user_dump ");
1479 send_snat_user_session_details
1480 (snat_session_t * s, unix_shared_memory_queue_t * q, u32 context)
1482 vl_api_snat_user_session_details_t * rmp;
1483 snat_main_t * sm = &snat_main;
1485 rmp = vl_msg_api_alloc (sizeof(*rmp));
1486 memset (rmp, 0, sizeof (*rmp));
1487 rmp->_vl_msg_id = ntohs (VL_API_SNAT_USER_SESSION_DETAILS+sm->msg_id_base);
1489 clib_memcpy(rmp->outside_ip_address, (&s->out2in.addr), 4);
1490 rmp->outside_port = s->out2in.port;
1491 clib_memcpy(rmp->inside_ip_address, (&s->in2out.addr), 4);
1492 rmp->inside_port = s->in2out.port;
1493 rmp->protocol = ntohs(snat_proto_to_ip_proto(s->in2out.protocol));
1494 rmp->is_static = s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING ? 1 : 0;
1495 rmp->last_heard = clib_host_to_net_u64((u64)s->last_heard);
1496 rmp->total_bytes = clib_host_to_net_u64(s->total_bytes);
1497 rmp->total_pkts = ntohl(s->total_pkts);
1498 rmp->context = context;
1500 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1504 vl_api_snat_user_session_dump_t_handler
1505 (vl_api_snat_user_session_dump_t * mp)
1507 unix_shared_memory_queue_t *q;
1508 snat_main_t * sm = &snat_main;
1509 snat_main_per_thread_data_t *tsm;
1511 clib_bihash_kv_8_8_t key, value;
1512 snat_user_key_t ukey;
1514 u32 session_index, head_index, elt_index;
1515 dlist_elt_t * head, * elt;
1517 q = vl_api_client_index_to_input_queue (mp->client_index);
1521 clib_memcpy (&ukey.addr, mp->ip_address, 4);
1522 ukey.fib_index = ip4_fib_index_from_table_id (ntohl(mp->vrf_id));
1523 key.key = ukey.as_u64;
1524 if (!clib_bihash_search_8_8 (&sm->worker_by_in, &key, &value))
1525 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
1527 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1528 if (clib_bihash_search_8_8 (&sm->user_hash, &key, &value))
1530 u = pool_elt_at_index (tsm->users, value.value);
1531 if (!u->nsessions && !u->nstaticsessions)
1534 head_index = u->sessions_per_user_list_head_index;
1535 head = pool_elt_at_index (tsm->list_pool, head_index);
1536 elt_index = head->next;
1537 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1538 session_index = elt->value;
1539 while (session_index != ~0)
1541 s = pool_elt_at_index (tsm->sessions, session_index);
1543 send_snat_user_session_details (s, q, mp->context);
1545 elt_index = elt->next;
1546 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1547 session_index = elt->value;
1551 static void *vl_api_snat_user_session_dump_t_print
1552 (vl_api_snat_user_session_dump_t *mp, void * handle)
1556 s = format (0, "SCRIPT: snat_user_session_dump ");
1557 s = format (s, "ip_address %U vrf_id %d\n",
1558 format_ip4_address, mp->ip_address,
1559 clib_net_to_host_u32 (mp->vrf_id));
1564 /* List of message types that this plugin understands */
1565 #define foreach_snat_plugin_api_msg \
1566 _(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range) \
1567 _(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature) \
1568 _(SNAT_ADD_STATIC_MAPPING, snat_add_static_mapping) \
1569 _(SNAT_CONTROL_PING, snat_control_ping) \
1570 _(SNAT_STATIC_MAPPING_DUMP, snat_static_mapping_dump) \
1571 _(SNAT_SHOW_CONFIG, snat_show_config) \
1572 _(SNAT_ADDRESS_DUMP, snat_address_dump) \
1573 _(SNAT_INTERFACE_DUMP, snat_interface_dump) \
1574 _(SNAT_SET_WORKERS, snat_set_workers) \
1575 _(SNAT_WORKER_DUMP, snat_worker_dump) \
1576 _(SNAT_ADD_DEL_INTERFACE_ADDR, snat_add_del_interface_addr) \
1577 _(SNAT_INTERFACE_ADDR_DUMP, snat_interface_addr_dump) \
1578 _(SNAT_IPFIX_ENABLE_DISABLE, snat_ipfix_enable_disable) \
1579 _(SNAT_USER_DUMP, snat_user_dump) \
1580 _(SNAT_USER_SESSION_DUMP, snat_user_session_dump)
1582 /* Set up the API message handling tables */
1583 static clib_error_t *
1584 snat_plugin_api_hookup (vlib_main_t *vm)
1586 snat_main_t * sm __attribute__ ((unused)) = &snat_main;
1588 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
1590 vl_api_##n##_t_handler, \
1592 vl_api_##n##_t_endian, \
1593 vl_api_##n##_t_print, \
1594 sizeof(vl_api_##n##_t), 1);
1595 foreach_snat_plugin_api_msg;
1601 #define vl_msg_name_crc_list
1602 #include <snat/snat_all_api_h.h>
1603 #undef vl_msg_name_crc_list
1606 setup_message_id_table (snat_main_t * sm, api_main_t * am)
1608 #define _(id,n,crc) \
1609 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
1610 foreach_vl_msg_name_crc_snat;
1614 static void plugin_custom_dump_configure (snat_main_t * sm)
1616 #define _(n,f) sm->api_main->msg_print_handlers \
1617 [VL_API_##n + sm->msg_id_base] \
1618 = (void *) vl_api_##f##_t_print;
1619 foreach_snat_plugin_api_msg;
1625 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1628 ip4_address_t * address,
1630 u32 if_address_index,
1633 static clib_error_t * snat_init (vlib_main_t * vm)
1635 snat_main_t * sm = &snat_main;
1636 clib_error_t * error = 0;
1637 ip4_main_t * im = &ip4_main;
1638 ip_lookup_main_t * lm = &im->lookup_main;
1641 vlib_thread_registration_t *tr;
1642 vlib_thread_main_t *tm = vlib_get_thread_main ();
1645 ip4_add_del_interface_address_callback_t cb4;
1647 name = format (0, "snat_%08x%c", api_version, 0);
1649 /* Ask for a correctly-sized block of API message decode slots */
1650 sm->msg_id_base = vl_msg_api_get_msg_ids
1651 ((char *) name, VL_MSG_FIRST_AVAILABLE);
1654 sm->vnet_main = vnet_get_main();
1656 sm->ip4_lookup_main = lm;
1657 sm->api_main = &api_main;
1658 sm->first_worker_index = 0;
1659 sm->next_worker = 0;
1660 sm->num_workers = 0;
1662 sm->fq_in2out_index = ~0;
1663 sm->fq_out2in_index = ~0;
1665 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1668 tr = (vlib_thread_registration_t *) p[0];
1671 sm->num_workers = tr->count;
1672 sm->first_worker_index = tr->first_index;
1676 /* Use all available workers by default */
1677 if (sm->num_workers > 1)
1679 for (i=0; i < sm->num_workers; i++)
1680 bitmap = clib_bitmap_set (bitmap, i, 1);
1681 snat_set_workers(bitmap);
1682 clib_bitmap_free (bitmap);
1685 error = snat_plugin_api_hookup (vm);
1687 /* Add our API messages to the global name_crc hash table */
1688 setup_message_id_table (sm, &api_main);
1690 plugin_custom_dump_configure (sm);
1693 /* Set up the interface address add/del callback */
1694 cb4.function = snat_ip4_add_del_interface_address_cb;
1695 cb4.function_opaque = 0;
1697 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1699 /* Init IPFIX logging */
1700 snat_ipfix_logging_init(vm);
1705 VLIB_INIT_FUNCTION (snat_init);
1707 void snat_free_outside_address_and_port (snat_main_t * sm,
1708 snat_session_key_t * k,
1712 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1714 ASSERT (address_index < vec_len (sm->addresses));
1716 a = sm->addresses + address_index;
1718 switch (k->protocol)
1720 #define _(N, i, n, s) \
1721 case SNAT_PROTOCOL_##N: \
1722 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
1723 port_host_byte_order) == 1); \
1724 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
1725 port_host_byte_order, 0); \
1726 a->busy_##n##_ports--; \
1728 foreach_snat_protocol
1731 clib_warning("unknown_protocol");
1737 * @brief Match SNAT static mapping.
1739 * @param sm SNAT main.
1740 * @param match Address and port to match.
1741 * @param mapping External or local address and port of the matched mapping.
1742 * @param by_external If 0 match by local address otherwise match by external
1745 * @returns 0 if match found otherwise 1.
1747 int snat_static_mapping_match (snat_main_t * sm,
1748 snat_session_key_t match,
1749 snat_session_key_t * mapping,
1752 clib_bihash_kv_8_8_t kv, value;
1753 snat_static_mapping_t *m;
1754 snat_session_key_t m_key;
1755 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1758 mapping_hash = &sm->static_mapping_by_external;
1760 m_key.addr = match.addr;
1761 m_key.port = clib_net_to_host_u16 (match.port);
1762 m_key.protocol = match.protocol;
1763 m_key.fib_index = match.fib_index;
1765 kv.key = m_key.as_u64;
1767 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1769 /* Try address only mapping */
1772 kv.key = m_key.as_u64;
1773 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1777 m = pool_elt_at_index (sm->static_mappings, value.value);
1781 mapping->addr = m->local_addr;
1782 /* Address only mapping doesn't change port */
1783 mapping->port = m->addr_only ? match.port
1784 : clib_host_to_net_u16 (m->local_port);
1785 mapping->fib_index = m->fib_index;
1789 mapping->addr = m->external_addr;
1790 /* Address only mapping doesn't change port */
1791 mapping->port = m->addr_only ? match.port
1792 : clib_host_to_net_u16 (m->external_port);
1793 mapping->fib_index = sm->outside_fib_index;
1799 int snat_alloc_outside_address_and_port (snat_main_t * sm,
1801 snat_session_key_t * k,
1802 u32 * address_indexp)
1808 for (i = 0; i < vec_len (sm->addresses); i++)
1810 a = sm->addresses + i;
1811 if (sm->vrf_mode && a->fib_index != ~0 && a->fib_index != fib_index)
1813 switch (k->protocol)
1815 #define _(N, j, n, s) \
1816 case SNAT_PROTOCOL_##N: \
1817 if (a->busy_##n##_ports < (65535-1024)) \
1821 portnum = random_u32 (&sm->random_seed); \
1822 portnum &= 0xFFFF; \
1823 if (portnum < 1024) \
1825 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
1827 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
1828 a->busy_##n##_ports++; \
1829 k->addr = a->addr; \
1830 k->port = clib_host_to_net_u16(portnum); \
1831 *address_indexp = i; \
1836 foreach_snat_protocol
1839 clib_warning("unknown protocol");
1844 /* Totally out of translations to use... */
1845 snat_ipfix_logging_addresses_exhausted(0);
1850 static clib_error_t *
1851 add_address_command_fn (vlib_main_t * vm,
1852 unformat_input_t * input,
1853 vlib_cli_command_t * cmd)
1855 unformat_input_t _line_input, *line_input = &_line_input;
1856 snat_main_t * sm = &snat_main;
1857 ip4_address_t start_addr, end_addr, this_addr;
1858 u32 start_host_order, end_host_order;
1863 clib_error_t *error = 0;
1865 /* Get a line of input. */
1866 if (!unformat_user (input, unformat_line_input, line_input))
1869 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1871 if (unformat (line_input, "%U - %U",
1872 unformat_ip4_address, &start_addr,
1873 unformat_ip4_address, &end_addr))
1875 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
1877 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
1878 end_addr = start_addr;
1879 else if (unformat (line_input, "del"))
1883 error = clib_error_return (0, "unknown input '%U'",
1884 format_unformat_error, line_input);
1889 if (sm->static_mapping_only)
1891 error = clib_error_return (0, "static mapping only mode");
1895 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
1896 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
1898 if (end_host_order < start_host_order)
1900 error = clib_error_return (0, "end address less than start address");
1904 count = (end_host_order - start_host_order) + 1;
1907 clib_warning ("%U - %U, %d addresses...",
1908 format_ip4_address, &start_addr,
1909 format_ip4_address, &end_addr,
1912 this_addr = start_addr;
1914 for (i = 0; i < count; i++)
1917 snat_add_address (sm, &this_addr, vrf_id);
1919 rv = snat_del_address (sm, this_addr, 0);
1923 case VNET_API_ERROR_NO_SUCH_ENTRY:
1924 error = clib_error_return (0, "S-NAT address not exist.");
1926 case VNET_API_ERROR_UNSPECIFIED:
1927 error = clib_error_return (0, "S-NAT address used in static mapping.");
1933 increment_v4_address (&this_addr);
1937 unformat_free (line_input);
1942 VLIB_CLI_COMMAND (add_address_command, static) = {
1943 .path = "snat add address",
1944 .short_help = "snat add addresses <ip4-range-start> [- <ip4-range-end>] "
1945 "[tenant-vrf <vrf-id>] [del]",
1946 .function = add_address_command_fn,
1949 static clib_error_t *
1950 snat_feature_command_fn (vlib_main_t * vm,
1951 unformat_input_t * input,
1952 vlib_cli_command_t * cmd)
1954 unformat_input_t _line_input, *line_input = &_line_input;
1955 vnet_main_t * vnm = vnet_get_main();
1956 clib_error_t * error = 0;
1958 u32 * inside_sw_if_indices = 0;
1959 u32 * outside_sw_if_indices = 0;
1965 /* Get a line of input. */
1966 if (!unformat_user (input, unformat_line_input, line_input))
1969 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1971 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
1973 vec_add1 (inside_sw_if_indices, sw_if_index);
1974 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
1976 vec_add1 (outside_sw_if_indices, sw_if_index);
1977 else if (unformat (line_input, "del"))
1981 error = clib_error_return (0, "unknown input '%U'",
1982 format_unformat_error, line_input);
1987 if (vec_len (inside_sw_if_indices))
1989 for (i = 0; i < vec_len(inside_sw_if_indices); i++)
1991 sw_if_index = inside_sw_if_indices[i];
1992 snat_interface_add_del (sw_if_index, 1, is_del);
1996 if (vec_len (outside_sw_if_indices))
1998 for (i = 0; i < vec_len(outside_sw_if_indices); i++)
2000 sw_if_index = outside_sw_if_indices[i];
2001 snat_interface_add_del (sw_if_index, 0, is_del);
2006 unformat_free (line_input);
2007 vec_free (inside_sw_if_indices);
2008 vec_free (outside_sw_if_indices);
2013 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
2014 .path = "set interface snat",
2015 .function = snat_feature_command_fn,
2016 .short_help = "set interface snat in <intfc> out <intfc> [del]",
2020 unformat_snat_protocol (unformat_input_t * input, va_list * args)
2022 u32 *r = va_arg (*args, u32 *);
2025 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
2026 foreach_snat_protocol
2034 format_snat_protocol (u8 * s, va_list * args)
2036 u32 i = va_arg (*args, u32);
2041 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
2042 foreach_snat_protocol
2045 s = format (s, "unknown");
2047 s = format (s, "%s", t);
2051 static clib_error_t *
2052 add_static_mapping_command_fn (vlib_main_t * vm,
2053 unformat_input_t * input,
2054 vlib_cli_command_t * cmd)
2056 unformat_input_t _line_input, *line_input = &_line_input;
2057 clib_error_t * error = 0;
2058 ip4_address_t l_addr, e_addr;
2059 u32 l_port = 0, e_port = 0, vrf_id = ~0;
2062 u32 sw_if_index = ~0;
2063 vnet_main_t * vnm = vnet_get_main();
2065 snat_protocol_t proto;
2068 /* Get a line of input. */
2069 if (!unformat_user (input, unformat_line_input, line_input))
2072 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2074 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
2077 else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
2079 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
2082 else if (unformat (line_input, "external %U", unformat_ip4_address,
2085 else if (unformat (line_input, "external %U %u",
2086 unformat_vnet_sw_interface, vnm, &sw_if_index,
2090 else if (unformat (line_input, "external %U",
2091 unformat_vnet_sw_interface, vnm, &sw_if_index))
2093 else if (unformat (line_input, "vrf %u", &vrf_id))
2095 else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
2097 else if (unformat (line_input, "del"))
2101 error = clib_error_return (0, "unknown input: '%U'",
2102 format_unformat_error, line_input);
2107 if (!addr_only && !proto_set)
2109 error = clib_error_return (0, "missing protocol");
2113 rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
2114 vrf_id, addr_only, sw_if_index, proto, is_add);
2118 case VNET_API_ERROR_INVALID_VALUE:
2119 error = clib_error_return (0, "External port already in use.");
2121 case VNET_API_ERROR_NO_SUCH_ENTRY:
2123 error = clib_error_return (0, "External addres must be allocated.");
2125 error = clib_error_return (0, "Mapping not exist.");
2127 case VNET_API_ERROR_NO_SUCH_FIB:
2128 error = clib_error_return (0, "No such VRF id.");
2130 case VNET_API_ERROR_VALUE_EXIST:
2131 error = clib_error_return (0, "Mapping already exist.");
2138 unformat_free (line_input);
2145 * @cliexstart{snat add static mapping}
2146 * Static mapping allows hosts on the external network to initiate connection
2147 * to to the local network host.
2148 * To create static mapping between local host address 10.0.0.3 port 6303 and
2149 * external address 4.4.4.4 port 3606 for TCP protocol use:
2150 * vpp# snat add static mapping local tcp 10.0.0.3 6303 external 4.4.4.4 3606
2151 * If not runnig "static mapping only" S-NAT plugin mode use before:
2152 * vpp# snat add address 4.4.4.4
2153 * To create static mapping between local and external address use:
2154 * vpp# snat add static mapping local 10.0.0.3 external 4.4.4.4
2157 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
2158 .path = "snat add static mapping",
2159 .function = add_static_mapping_command_fn,
2161 "snat add static mapping local tcp|udp|icmp <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
2164 static clib_error_t *
2165 set_workers_command_fn (vlib_main_t * vm,
2166 unformat_input_t * input,
2167 vlib_cli_command_t * cmd)
2169 unformat_input_t _line_input, *line_input = &_line_input;
2172 clib_error_t *error = 0;
2174 /* Get a line of input. */
2175 if (!unformat_user (input, unformat_line_input, line_input))
2178 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2180 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
2184 error = clib_error_return (0, "unknown input '%U'",
2185 format_unformat_error, line_input);
2192 error = clib_error_return (0, "List of workers must be specified.");
2196 rv = snat_set_workers(bitmap);
2198 clib_bitmap_free (bitmap);
2202 case VNET_API_ERROR_INVALID_WORKER:
2203 error = clib_error_return (0, "Invalid worker(s).");
2205 case VNET_API_ERROR_FEATURE_DISABLED:
2206 error = clib_error_return (0,
2207 "Supported only if 2 or more workes available.");
2214 unformat_free (line_input);
2221 * @cliexstart{set snat workers}
2222 * Set SNAT workers if 2 or more workers available, use:
2223 * vpp# set snat workers 0-2,5
2226 VLIB_CLI_COMMAND (set_workers_command, static) = {
2227 .path = "set snat workers",
2228 .function = set_workers_command_fn,
2230 "set snat workers <workers-list>",
2233 static clib_error_t *
2234 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
2235 unformat_input_t * input,
2236 vlib_cli_command_t * cmd)
2238 unformat_input_t _line_input, *line_input = &_line_input;
2243 clib_error_t *error = 0;
2245 /* Get a line of input. */
2246 if (!unformat_user (input, unformat_line_input, line_input))
2249 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2251 if (unformat (line_input, "domain %d", &domain_id))
2253 else if (unformat (line_input, "src-port %d", &src_port))
2255 else if (unformat (line_input, "disable"))
2259 error = clib_error_return (0, "unknown input '%U'",
2260 format_unformat_error, line_input);
2265 rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
2269 error = clib_error_return (0, "ipfix logging enable failed");
2274 unformat_free (line_input);
2281 * @cliexstart{snat ipfix logging}
2282 * To enable SNAT IPFIX logging use:
2283 * vpp# snat ipfix logging
2284 * To set IPFIX exporter use:
2285 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
2288 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
2289 .path = "snat ipfix logging",
2290 .function = snat_ipfix_logging_enable_disable_command_fn,
2291 .short_help = "snat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
2294 static clib_error_t *
2295 snat_config (vlib_main_t * vm, unformat_input_t * input)
2297 snat_main_t * sm = &snat_main;
2298 u32 translation_buckets = 1024;
2299 u32 translation_memory_size = 128<<20;
2300 u32 user_buckets = 128;
2301 u32 user_memory_size = 64<<20;
2302 u32 max_translations_per_user = 100;
2303 u32 outside_vrf_id = 0;
2304 u32 inside_vrf_id = 0;
2305 u32 static_mapping_buckets = 1024;
2306 u32 static_mapping_memory_size = 64<<20;
2307 u8 static_mapping_only = 0;
2308 u8 static_mapping_connection_tracking = 0;
2309 vlib_thread_main_t *tm = vlib_get_thread_main ();
2311 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2313 if (unformat (input, "translation hash buckets %d", &translation_buckets))
2315 else if (unformat (input, "translation hash memory %d",
2316 &translation_memory_size));
2317 else if (unformat (input, "user hash buckets %d", &user_buckets))
2319 else if (unformat (input, "user hash memory %d",
2322 else if (unformat (input, "max translations per user %d",
2323 &max_translations_per_user))
2325 else if (unformat (input, "outside VRF id %d",
2328 else if (unformat (input, "inside VRF id %d",
2331 else if (unformat (input, "static mapping only"))
2333 static_mapping_only = 1;
2334 if (unformat (input, "connection tracking"))
2335 static_mapping_connection_tracking = 1;
2338 return clib_error_return (0, "unknown input '%U'",
2339 format_unformat_error, input);
2342 /* for show commands, etc. */
2343 sm->translation_buckets = translation_buckets;
2344 sm->translation_memory_size = translation_memory_size;
2345 sm->user_buckets = user_buckets;
2346 sm->user_memory_size = user_memory_size;
2347 sm->max_translations_per_user = max_translations_per_user;
2348 sm->outside_vrf_id = outside_vrf_id;
2349 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2351 sm->inside_vrf_id = inside_vrf_id;
2352 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2354 sm->static_mapping_only = static_mapping_only;
2355 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
2357 if (!static_mapping_only ||
2358 (static_mapping_only && static_mapping_connection_tracking))
2360 clib_bihash_init_8_8 (&sm->worker_by_in, "worker-by-in", user_buckets,
2363 clib_bihash_init_8_8 (&sm->worker_by_out, "worker-by-out", user_buckets,
2366 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
2368 clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
2369 translation_memory_size);
2371 clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
2372 translation_memory_size);
2374 clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
2377 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2378 "static_mapping_by_local", static_mapping_buckets,
2379 static_mapping_memory_size);
2381 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2382 "static_mapping_by_external", static_mapping_buckets,
2383 static_mapping_memory_size);
2387 VLIB_CONFIG_FUNCTION (snat_config, "snat");
2389 u8 * format_snat_key (u8 * s, va_list * args)
2391 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
2392 char * protocol_string = "unknown";
2393 static char *protocol_strings[] = {
2399 if (key->protocol < ARRAY_LEN(protocol_strings))
2400 protocol_string = protocol_strings[key->protocol];
2402 s = format (s, "%U proto %s port %d fib %d",
2403 format_ip4_address, &key->addr, protocol_string,
2404 clib_net_to_host_u16 (key->port), key->fib_index);
2408 u8 * format_snat_session (u8 * s, va_list * args)
2410 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
2411 snat_session_t * sess = va_arg (*args, snat_session_t *);
2413 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
2414 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
2415 s = format (s, " last heard %.2f\n", sess->last_heard);
2416 s = format (s, " total pkts %d, total bytes %lld\n",
2417 sess->total_pkts, sess->total_bytes);
2418 if (snat_is_session_static (sess))
2419 s = format (s, " static translation\n");
2421 s = format (s, " dynamic translation\n");
2426 u8 * format_snat_user (u8 * s, va_list * args)
2428 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
2429 snat_user_t * u = va_arg (*args, snat_user_t *);
2430 int verbose = va_arg (*args, int);
2431 dlist_elt_t * head, * elt;
2432 u32 elt_index, head_index;
2434 snat_session_t * sess;
2436 s = format (s, "%U: %d dynamic translations, %d static translations\n",
2437 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
2442 if (u->nsessions || u->nstaticsessions)
2444 head_index = u->sessions_per_user_list_head_index;
2445 head = pool_elt_at_index (sm->list_pool, head_index);
2447 elt_index = head->next;
2448 elt = pool_elt_at_index (sm->list_pool, elt_index);
2449 session_index = elt->value;
2451 while (session_index != ~0)
2453 sess = pool_elt_at_index (sm->sessions, session_index);
2455 s = format (s, " %U\n", format_snat_session, sm, sess);
2457 elt_index = elt->next;
2458 elt = pool_elt_at_index (sm->list_pool, elt_index);
2459 session_index = elt->value;
2466 u8 * format_snat_static_mapping (u8 * s, va_list * args)
2468 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
2471 s = format (s, "local %U external %U vrf %d",
2472 format_ip4_address, &m->local_addr,
2473 format_ip4_address, &m->external_addr,
2476 s = format (s, "%U local %U:%d external %U:%d vrf %d",
2477 format_snat_protocol, m->proto,
2478 format_ip4_address, &m->local_addr, m->local_port,
2479 format_ip4_address, &m->external_addr, m->external_port,
2485 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
2487 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
2488 vnet_main_t *vnm = vnet_get_main();
2491 s = format (s, "local %U external %U vrf %d",
2492 format_ip4_address, &m->l_addr,
2493 format_vnet_sw_interface_name, vnm,
2494 vnet_get_sw_interface (vnm, m->sw_if_index),
2497 s = format (s, "%U local %U:%d external %U:%d vrf %d",
2498 format_snat_protocol, m->proto,
2499 format_ip4_address, &m->l_addr, m->l_port,
2500 format_vnet_sw_interface_name, vnm,
2501 vnet_get_sw_interface (vnm, m->sw_if_index), m->e_port,
2507 static clib_error_t *
2508 show_snat_command_fn (vlib_main_t * vm,
2509 unformat_input_t * input,
2510 vlib_cli_command_t * cmd)
2513 snat_main_t * sm = &snat_main;
2515 snat_static_mapping_t *m;
2516 snat_interface_t *i;
2517 snat_address_t * ap;
2518 vnet_main_t *vnm = vnet_get_main();
2519 snat_main_per_thread_data_t *tsm;
2520 u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
2522 snat_static_map_resolve_t *rp;
2524 if (unformat (input, "detail"))
2526 else if (unformat (input, "verbose"))
2529 if (sm->static_mapping_only)
2531 if (sm->static_mapping_connection_tracking)
2532 vlib_cli_output (vm, "SNAT mode: static mapping only connection "
2535 vlib_cli_output (vm, "SNAT mode: static mapping only");
2539 vlib_cli_output (vm, "SNAT mode: dynamic translations enabled");
2544 pool_foreach (i, sm->interfaces,
2546 vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
2547 vnet_get_sw_interface (vnm, i->sw_if_index),
2548 i->is_inside ? "in" : "out");
2551 if (vec_len (sm->auto_add_sw_if_indices))
2553 vlib_cli_output (vm, "SNAT pool addresses interfaces:");
2554 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
2556 vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
2557 vnet_get_sw_interface (vnm, *sw_if_index));
2561 vec_foreach (ap, sm->addresses)
2563 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
2564 if (ap->fib_index != ~0)
2565 vlib_cli_output (vm, " tenant VRF: %u",
2566 ip4_fib_get(ap->fib_index)->table_id);
2568 vlib_cli_output (vm, " tenant VRF independent");
2569 #define _(N, i, n, s) \
2570 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
2571 foreach_snat_protocol
2576 if (sm->num_workers > 1)
2578 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
2581 vec_foreach (worker, sm->workers)
2583 vlib_worker_thread_t *w =
2584 vlib_worker_threads + *worker + sm->first_worker_index;
2585 vlib_cli_output (vm, " %s", w->name);
2590 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2592 vlib_cli_output (vm, "%d static mappings",
2593 pool_elts (sm->static_mappings));
2597 pool_foreach (m, sm->static_mappings,
2599 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2605 vec_foreach (tsm, sm->per_thread_data)
2607 users_num += pool_elts (tsm->users);
2608 sessions_num += pool_elts (tsm->sessions);
2611 vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
2612 " %d static mappings",
2614 vec_len (sm->addresses),
2616 pool_elts (sm->static_mappings));
2620 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
2622 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
2624 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_in,
2626 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_out,
2628 vec_foreach_index (j, sm->per_thread_data)
2630 tsm = vec_elt_at_index (sm->per_thread_data, j);
2632 if (pool_elts (tsm->users) == 0)
2635 vlib_worker_thread_t *w = vlib_worker_threads + j;
2636 vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
2638 vlib_cli_output (vm, " %d list pool elements",
2639 pool_elts (tsm->list_pool));
2641 pool_foreach (u, tsm->users,
2643 vlib_cli_output (vm, " %U", format_snat_user, tsm, u,
2648 if (pool_elts (sm->static_mappings) || vec_len (sm->to_resolve))
2650 vlib_cli_output (vm, "static mappings:");
2651 pool_foreach (m, sm->static_mappings,
2653 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2655 for (j = 0; j < vec_len (sm->to_resolve); j++)
2657 rp = sm->to_resolve + j;
2658 vlib_cli_output (vm, "%U", format_snat_static_map_to_resolve,
2668 VLIB_CLI_COMMAND (show_snat_command, static) = {
2669 .path = "show snat",
2670 .short_help = "show snat",
2671 .function = show_snat_command_fn,
2676 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2679 ip4_address_t * address,
2681 u32 if_address_index,
2684 snat_main_t *sm = &snat_main;
2685 snat_static_map_resolve_t *rp;
2686 u32 *indices_to_delete = 0;
2690 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2692 if (sw_if_index == sm->auto_add_sw_if_indices[i])
2696 /* Don't trip over lease renewal, static config */
2697 for (j = 0; j < vec_len(sm->addresses); j++)
2698 if (sm->addresses[j].addr.as_u32 == address->as_u32)
2701 snat_add_address (sm, address, ~0);
2702 /* Scan static map resolution vector */
2703 for (j = 0; j < vec_len (sm->to_resolve); j++)
2705 rp = sm->to_resolve + j;
2706 /* On this interface? */
2707 if (rp->sw_if_index == sw_if_index)
2709 /* Add the static mapping */
2710 rv = snat_add_static_mapping (rp->l_addr,
2716 ~0 /* sw_if_index */,
2720 clib_warning ("snat_add_static_mapping returned %d",
2722 vec_add1 (indices_to_delete, j);
2725 /* If we resolved any of the outstanding static mappings */
2726 if (vec_len(indices_to_delete))
2729 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2730 vec_delete(sm->to_resolve, 1, j);
2731 vec_free(indices_to_delete);
2737 (void) snat_del_address(sm, address[0], 1);
2745 static int snat_add_interface_address (snat_main_t *sm,
2749 ip4_main_t * ip4_main = sm->ip4_main;
2750 ip4_address_t * first_int_addr;
2751 snat_static_map_resolve_t *rp;
2752 u32 *indices_to_delete = 0;
2755 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2756 0 /* just want the address*/);
2758 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2760 if (sm->auto_add_sw_if_indices[i] == sw_if_index)
2764 /* if have address remove it */
2766 (void) snat_del_address (sm, first_int_addr[0], 1);
2769 for (j = 0; j < vec_len (sm->to_resolve); j++)
2771 rp = sm->to_resolve + j;
2772 if (rp->sw_if_index == sw_if_index)
2773 vec_add1 (indices_to_delete, j);
2775 if (vec_len(indices_to_delete))
2777 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2778 vec_del1(sm->to_resolve, j);
2779 vec_free(indices_to_delete);
2782 vec_del1(sm->auto_add_sw_if_indices, i);
2785 return VNET_API_ERROR_VALUE_EXIST;
2792 return VNET_API_ERROR_NO_SUCH_ENTRY;
2794 /* add to the auto-address list */
2795 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
2797 /* If the address is already bound - or static - add it now */
2799 snat_add_address (sm, first_int_addr, ~0);
2804 static clib_error_t *
2805 snat_add_interface_address_command_fn (vlib_main_t * vm,
2806 unformat_input_t * input,
2807 vlib_cli_command_t * cmd)
2809 snat_main_t *sm = &snat_main;
2810 unformat_input_t _line_input, *line_input = &_line_input;
2814 clib_error_t *error = 0;
2816 /* Get a line of input. */
2817 if (!unformat_user (input, unformat_line_input, line_input))
2820 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2822 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
2823 sm->vnet_main, &sw_if_index))
2825 else if (unformat (line_input, "del"))
2829 error = clib_error_return (0, "unknown input '%U'",
2830 format_unformat_error, line_input);
2835 rv = snat_add_interface_address (sm, sw_if_index, is_del);
2843 error = clib_error_return (0, "snat_add_interface_address returned %d",
2849 unformat_free (line_input);
2854 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2855 .path = "snat add interface address",
2856 .short_help = "snat add interface address <interface> [del]",
2857 .function = snat_add_interface_address_command_fn,