3 * nat.c - skeleton vpp-api-test plug-in
5 * Copyright (c) <current-year> <your-organization>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include <vlibapi/api.h>
20 #include <vlibmemory/api.h>
22 #include <vppinfra/error.h>
23 #include <vnet/ip/ip.h>
26 #define __plugin_msg_base snat_test_main.msg_id_base
27 #include <vlibapi/vat_helper_macros.h>
29 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
31 /* Declare message IDs */
32 #include <nat/nat_msg_enum.h>
34 /* define message structures */
36 #include <nat/nat_all_api_h.h>
39 /* declare message handlers for each api */
41 #define vl_endianfun /* define message structures */
42 #include <nat/nat_all_api_h.h>
45 /* instantiate all the print functions we know about */
46 #define vl_print(handle, ...)
48 #include <nat/nat_all_api_h.h>
51 /* Get the API version number. */
52 #define vl_api_version(n,v) static u32 api_version=(v);
53 #include <nat/nat_all_api_h.h>
57 /* API message ID base */
62 snat_test_main_t snat_test_main;
64 #define foreach_standard_reply_retval_handler \
65 _(nat44_add_del_address_range_reply) \
66 _(nat44_interface_add_del_feature_reply) \
67 _(nat44_add_del_static_mapping_reply) \
68 _(nat_set_workers_reply) \
69 _(nat44_add_del_interface_addr_reply) \
70 _(nat_ipfix_enable_disable_reply) \
71 _(nat_det_add_del_map_reply) \
72 _(nat_det_set_timeouts_reply) \
73 _(nat_det_close_session_out_reply) \
74 _(nat_det_close_session_in_reply)
77 static void vl_api_##n##_t_handler \
78 (vl_api_##n##_t * mp) \
80 vat_main_t * vam = snat_test_main.vat_main; \
81 i32 retval = ntohl(mp->retval); \
82 if (vam->async_mode) { \
83 vam->async_errors += (retval < 0); \
85 vam->retval = retval; \
86 vam->result_ready = 1; \
89 foreach_standard_reply_retval_handler;
93 * Table of message reply handlers, must include boilerplate handlers
96 #define foreach_vpe_api_reply_msg \
97 _(NAT44_ADD_DEL_ADDRESS_RANGE_REPLY, \
98 nat44_add_del_address_range_reply) \
99 _(NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY, \
100 nat44_interface_add_del_feature_reply) \
101 _(NAT44_ADD_DEL_STATIC_MAPPING_REPLY, \
102 nat44_add_del_static_mapping_reply) \
103 _(NAT_CONTROL_PING_REPLY, nat_control_ping_reply) \
104 _(NAT44_STATIC_MAPPING_DETAILS, nat44_static_mapping_details) \
105 _(NAT_SHOW_CONFIG_REPLY, nat_show_config_reply) \
106 _(NAT44_ADDRESS_DETAILS, nat44_address_details) \
107 _(NAT44_INTERFACE_DETAILS, nat44_interface_details) \
108 _(NAT_SET_WORKERS_REPLY, nat_set_workers_reply) \
109 _(NAT_WORKER_DETAILS, nat_worker_details) \
110 _(NAT44_ADD_DEL_INTERFACE_ADDR_REPLY, \
111 nat44_add_del_interface_addr_reply) \
112 _(NAT44_INTERFACE_ADDR_DETAILS, nat44_interface_addr_details) \
113 _(NAT_IPFIX_ENABLE_DISABLE_REPLY, \
114 nat_ipfix_enable_disable_reply) \
115 _(NAT44_USER_DETAILS, nat44_user_details) \
116 _(NAT44_USER_SESSION_DETAILS, nat44_user_session_details) \
117 _(NAT_DET_ADD_DEL_MAP_REPLY, nat_det_add_del_map_reply) \
118 _(NAT_DET_FORWARD_REPLY, nat_det_forward_reply) \
119 _(NAT_DET_REVERSE_REPLY, nat_det_reverse_reply) \
120 _(NAT_DET_MAP_DETAILS, nat_det_map_details) \
121 _(NAT_DET_SET_TIMEOUTS_REPLY, nat_det_set_timeouts_reply) \
122 _(NAT_DET_GET_TIMEOUTS_REPLY, nat_det_get_timeouts_reply) \
123 _(NAT_DET_CLOSE_SESSION_OUT_REPLY, \
124 nat_det_close_session_out_reply) \
125 _(NAT_DET_CLOSE_SESSION_IN_REPLY, \
126 nat_det_close_session_in_reply) \
127 _(NAT_DET_SESSION_DETAILS, nat_det_session_details)
129 static int api_nat44_add_del_address_range (vat_main_t * vam)
131 unformat_input_t * i = vam->input;
132 ip4_address_t start_addr, end_addr;
133 u32 start_host_order, end_host_order;
134 vl_api_nat44_add_del_address_range_t * mp;
139 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
141 if (unformat (i, "%U - %U",
142 unformat_ip4_address, &start_addr,
143 unformat_ip4_address, &end_addr))
145 else if (unformat (i, "%U", unformat_ip4_address, &start_addr))
146 end_addr = start_addr;
147 else if (unformat (i, "del"))
151 clib_warning("unknown input '%U'", format_unformat_error, i);
156 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
157 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
159 if (end_host_order < start_host_order)
161 errmsg ("end address less than start address\n");
165 count = (end_host_order - start_host_order) + 1;
169 errmsg ("%U - %U, %d addresses...\n",
170 format_ip4_address, &start_addr,
171 format_ip4_address, &end_addr,
175 M(NAT44_ADD_DEL_ADDRESS_RANGE, mp);
177 memcpy (mp->first_ip_address, &start_addr, 4);
178 memcpy (mp->last_ip_address, &end_addr, 4);
186 static int api_nat44_interface_add_del_feature (vat_main_t * vam)
188 unformat_input_t * i = vam->input;
189 vl_api_nat44_interface_add_del_feature_t * mp;
191 u8 sw_if_index_set = 0;
196 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
198 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
200 else if (unformat (i, "sw_if_index %d", &sw_if_index))
202 else if (unformat (i, "out"))
204 else if (unformat (i, "in"))
206 else if (unformat (i, "del"))
210 clib_warning("unknown input '%U'", format_unformat_error, i);
215 if (sw_if_index_set == 0)
217 errmsg ("interface / sw_if_index required\n");
221 M(NAT44_INTERFACE_ADD_DEL_FEATURE, mp);
222 mp->sw_if_index = ntohl(sw_if_index);
224 mp->is_inside = is_inside;
231 static int api_nat44_add_del_static_mapping(vat_main_t * vam)
233 unformat_input_t * i = vam->input;
234 vl_api_nat44_add_del_static_mapping_t * mp;
235 u8 external_addr_set = 0;
236 u8 local_addr_set = 0;
239 ip4_address_t local_addr, external_addr;
240 u32 local_port = 0, external_port = 0, vrf_id = ~0;
241 u32 sw_if_index = ~0;
242 u8 sw_if_index_set = 0;
247 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
249 if (unformat (i, "local_addr %U", unformat_ip4_address, &local_addr))
251 else if (unformat (i, "external_addr %U", unformat_ip4_address,
253 external_addr_set = 1;
254 else if (unformat (i, "local_port %u", &local_port))
256 else if (unformat (i, "external_port %u", &external_port))
258 else if (unformat (i, "external_if %U", unformat_sw_if_index, vam,
261 else if (unformat (i, "external_sw_if_index %d", &sw_if_index))
263 else if (unformat (i, "vrf %u", &vrf_id))
265 else if (unformat (i, "protocol %u", &proto))
267 else if (unformat (i, "del"))
271 clib_warning("unknown input '%U'", format_unformat_error, i);
276 if (!addr_only && !proto_set)
278 errmsg ("protocol required\n");
284 errmsg ("local addr required\n");
287 if (!external_addr_set && !sw_if_index_set)
289 errmsg ("external addr or interface required\n");
293 M(NAT44_ADD_DEL_STATIC_MAPPING, mp);
295 mp->addr_only = addr_only;
296 mp->local_port = ntohs ((u16) local_port);
297 mp->external_port = ntohs ((u16) external_port);
298 mp->external_sw_if_index = ntohl (sw_if_index);
299 mp->vrf_id = ntohl (vrf_id);
300 mp->protocol = (u8) proto;
301 memcpy (mp->local_ip_address, &local_addr, 4);
302 memcpy (mp->external_ip_address, &external_addr, 4);
309 static void vl_api_nat_control_ping_reply_t_handler
310 (vl_api_nat_control_ping_reply_t * mp)
312 vat_main_t *vam = &vat_main;
313 i32 retval = ntohl (mp->retval);
316 vam->async_errors += (retval < 0);
320 vam->retval = retval;
321 vam->result_ready = 1;
325 static void vl_api_nat44_static_mapping_details_t_handler
326 (vl_api_nat44_static_mapping_details_t *mp)
328 snat_test_main_t * sm = &snat_test_main;
329 vat_main_t *vam = sm->vat_main;
331 if (mp->addr_only && mp->external_sw_if_index != ~0)
332 fformat (vam->ofp, "%15U%6s%15d%6s%11d%6d\n",
333 format_ip4_address, &mp->local_ip_address, "",
334 ntohl (mp->external_sw_if_index), "",
337 else if (mp->addr_only && mp->external_sw_if_index == ~0)
338 fformat (vam->ofp, "%15U%6s%15U%6s%11d%6d\n",
339 format_ip4_address, &mp->local_ip_address, "",
340 format_ip4_address, &mp->external_ip_address, "",
343 else if (!mp->addr_only && mp->external_sw_if_index != ~0)
344 fformat (vam->ofp, "%15U%6d%15d%6d%11d%6d\n",
345 format_ip4_address, &mp->local_ip_address,
346 ntohs (mp->local_port),
347 ntohl (mp->external_sw_if_index),
348 ntohs (mp->external_port),
352 fformat (vam->ofp, "%15U%6d%15U%6d%11d%6d\n",
353 format_ip4_address, &mp->local_ip_address,
354 ntohs (mp->local_port),
355 format_ip4_address, &mp->external_ip_address,
356 ntohs (mp->external_port),
362 static int api_nat44_static_mapping_dump(vat_main_t * vam)
364 vl_api_nat44_static_mapping_dump_t * mp;
365 vl_api_nat_control_ping_t *mp_ping;
368 if (vam->json_output)
370 clib_warning ("JSON output not supported for nat44_static_mapping_dump");
374 fformat (vam->ofp, "%21s%21s\n", "local", "external");
375 fformat (vam->ofp, "%15s%6s%15s%6s%11s%6s\n", "address", "port",
376 "address/if_idx", "port", "vrf", "proto");
378 M(NAT44_STATIC_MAPPING_DUMP, mp);
381 /* Use a control ping for synchronization */
382 M(NAT_CONTROL_PING, mp_ping);
389 static void vl_api_nat_show_config_reply_t_handler
390 (vl_api_nat_show_config_reply_t *mp)
392 snat_test_main_t * sm = &snat_test_main;
393 vat_main_t *vam = sm->vat_main;
394 i32 retval = ntohl (mp->retval);
398 fformat (vam->ofp, "translation hash buckets %d\n",
399 ntohl (mp->translation_buckets));
400 fformat (vam->ofp, "translation hash memory %d\n",
401 ntohl (mp->translation_memory_size));
402 fformat (vam->ofp, "user hash buckets %d\n", ntohl (mp->user_buckets));
403 fformat (vam->ofp, "user hash memory %d\n", ntohl (mp->user_memory_size));
404 fformat (vam->ofp, "max translations per user %d\n",
405 ntohl (mp->max_translations_per_user));
406 fformat (vam->ofp, "outside VRF id %d\n", ntohl (mp->outside_vrf_id));
407 fformat (vam->ofp, "inside VRF id %d\n", ntohl (mp->inside_vrf_id));
408 if (mp->static_mapping_only)
410 fformat (vam->ofp, "static mapping only");
411 if (mp->static_mapping_connection_tracking)
412 fformat (vam->ofp, " connection tracking");
413 fformat (vam->ofp, "\n");
416 vam->retval = retval;
417 vam->result_ready = 1;
420 static int api_nat_show_config(vat_main_t * vam)
422 vl_api_nat_show_config_t * mp;
425 if (vam->json_output)
427 clib_warning ("JSON output not supported for nat_show_config");
431 M(NAT_SHOW_CONFIG, mp);
437 static void vl_api_nat44_address_details_t_handler
438 (vl_api_nat44_address_details_t *mp)
440 snat_test_main_t * sm = &snat_test_main;
441 vat_main_t *vam = sm->vat_main;
443 fformat (vam->ofp, "%U\n", format_ip4_address, &mp->ip_address);
446 static int api_nat44_address_dump(vat_main_t * vam)
448 vl_api_nat44_address_dump_t * mp;
449 vl_api_nat_control_ping_t *mp_ping;
452 if (vam->json_output)
454 clib_warning ("JSON output not supported for nat44_address_dump");
458 M(NAT44_ADDRESS_DUMP, mp);
461 /* Use a control ping for synchronization */
462 M(NAT_CONTROL_PING, mp_ping);
469 static void vl_api_nat44_interface_details_t_handler
470 (vl_api_nat44_interface_details_t *mp)
472 snat_test_main_t * sm = &snat_test_main;
473 vat_main_t *vam = sm->vat_main;
475 fformat (vam->ofp, "sw_if_index %d %s\n", ntohl (mp->sw_if_index),
476 mp->is_inside ? "in" : "out");
479 static int api_nat44_interface_dump(vat_main_t * vam)
481 vl_api_nat44_interface_dump_t * mp;
482 vl_api_nat_control_ping_t *mp_ping;
485 if (vam->json_output)
487 clib_warning ("JSON output not supported for nat44_address_dump");
491 M(NAT44_INTERFACE_DUMP, mp);
494 /* Use a control ping for synchronization */
495 M(NAT_CONTROL_PING, mp_ping);
502 static int api_nat_set_workers (vat_main_t * vam)
504 unformat_input_t * i = vam->input;
505 vl_api_nat_set_workers_t * mp;
509 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
511 if (unformat (i, "%U", unformat_bitmap_list, &bitmap))
515 clib_warning("unknown input '%U'", format_unformat_error, i);
520 M(NAT_SET_WORKERS, mp);
521 mp->worker_mask = clib_host_to_net_u64 (bitmap[0]);
528 static void vl_api_nat_worker_details_t_handler
529 (vl_api_nat_worker_details_t *mp)
531 snat_test_main_t * sm = &snat_test_main;
532 vat_main_t *vam = sm->vat_main;
534 fformat (vam->ofp, "worker_index %d (%s at lcore %u)\n",
535 ntohl (mp->worker_index), mp->name, ntohl (mp->lcore_id));
538 static int api_nat_worker_dump(vat_main_t * vam)
540 vl_api_nat_worker_dump_t * mp;
541 vl_api_nat_control_ping_t *mp_ping;
544 if (vam->json_output)
546 clib_warning ("JSON output not supported for nat_address_dump");
550 M(NAT_WORKER_DUMP, mp);
553 /* Use a control ping for synchronization */
554 M(NAT_CONTROL_PING, mp_ping);
561 static int api_nat44_add_del_interface_addr (vat_main_t * vam)
563 unformat_input_t * i = vam->input;
564 vl_api_nat44_add_del_interface_addr_t * mp;
566 u8 sw_if_index_set = 0;
570 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
572 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
574 else if (unformat (i, "sw_if_index %d", &sw_if_index))
576 else if (unformat (i, "del"))
580 clib_warning("unknown input '%U'", format_unformat_error, i);
585 if (sw_if_index_set == 0)
587 errmsg ("interface / sw_if_index required\n");
591 M(NAT44_ADD_DEL_INTERFACE_ADDR, mp);
592 mp->sw_if_index = ntohl(sw_if_index);
600 static void vl_api_nat44_interface_addr_details_t_handler
601 (vl_api_nat44_interface_addr_details_t *mp)
603 snat_test_main_t * sm = &snat_test_main;
604 vat_main_t *vam = sm->vat_main;
606 fformat (vam->ofp, "sw_if_index %d\n", ntohl (mp->sw_if_index));
609 static int api_nat44_interface_addr_dump(vat_main_t * vam)
611 vl_api_nat44_interface_addr_dump_t * mp;
612 vl_api_nat_control_ping_t *mp_ping;
615 if (vam->json_output)
617 clib_warning ("JSON output not supported for nat44_address_dump");
621 M(NAT44_INTERFACE_ADDR_DUMP, mp);
624 /* Use a control ping for synchronization */
625 M(NAT_CONTROL_PING, mp_ping);
632 static int api_nat_ipfix_enable_disable (vat_main_t * vam)
634 unformat_input_t * i = vam->input;
635 vl_api_nat_ipfix_enable_disable_t * mp;
641 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
643 if (unformat (i, "domain %d", &domain_id))
645 else if (unformat (i, "src_port %d", &src_port))
647 else if (unformat (i, "disable"))
651 clib_warning("unknown input '%U'", format_unformat_error, i);
656 M(NAT_IPFIX_ENABLE_DISABLE, mp);
657 mp->domain_id = htonl(domain_id);
658 mp->src_port = htons((u16) src_port);
666 static void vl_api_nat44_user_session_details_t_handler
667 (vl_api_nat44_user_session_details_t *mp)
669 snat_test_main_t * sm = &snat_test_main;
670 vat_main_t *vam = sm->vat_main;
672 fformat(vam->ofp, "%s session %U:%d to %U:%d protocol id %d "
673 "total packets %d total bytes %d\n",
674 mp->is_static ? "static" : "dynamic",
675 format_ip4_address, mp->inside_ip_address, ntohl(mp->inside_port),
676 format_ip4_address, mp->outside_ip_address, ntohl(mp->outside_port),
677 ntohl(mp->protocol), ntohl(mp->total_pkts), ntohl(mp->total_bytes));
680 static int api_nat44_user_session_dump(vat_main_t * vam)
682 unformat_input_t* i = vam->input;
683 vl_api_nat44_user_session_dump_t * mp;
684 vl_api_nat_control_ping_t *mp_ping;
689 if (vam->json_output)
691 clib_warning ("JSON output not supported for nat44_address_dump");
695 if (unformat (i, "ip_address %U vrf_id %d",
696 unformat_ip4_address, &addr, &vrf_id))
700 clib_warning("unknown input '%U'", format_unformat_error, i);
704 M(NAT44_USER_SESSION_DUMP, mp);
707 /* Use a control ping for synchronization */
708 M(NAT_CONTROL_PING, mp_ping);
709 memset(mp->ip_address, 0, 16);
710 clib_memcpy(mp->ip_address, &addr, 4);
711 mp->vrf_id = htonl(vrf_id);
718 static void vl_api_nat44_user_details_t_handler
719 (vl_api_nat44_user_details_t *mp)
721 snat_test_main_t * sm = &snat_test_main;
722 vat_main_t *vam = sm->vat_main;
724 fformat(vam->ofp, "user with ip %U with vrf_id %d "
725 "with %d sessions and %d static sessions\n",
726 format_ip4_address, mp->ip_address, ntohl(mp->vrf_id),
727 ntohl(mp->nsessions), ntohl(mp->nstaticsessions));
730 static int api_nat44_user_dump(vat_main_t * vam)
732 vl_api_nat44_user_dump_t * mp;
733 vl_api_nat_control_ping_t *mp_ping;
736 if (vam->json_output)
738 clib_warning ("JSON output not supported for nat44_address_dump");
742 M(NAT44_USER_DUMP, mp);
745 /* Use a control ping for synchronization */
746 M(NAT_CONTROL_PING, mp_ping);
753 static int api_nat_det_add_del_map (vat_main_t * vam)
755 unformat_input_t * i = vam->input;
756 vl_api_nat_det_add_del_map_t * mp;
757 ip4_address_t in_addr, out_addr;
758 u32 in_plen, out_plen;
762 if (unformat (i, "in %U/%d out %U/%d",
763 unformat_ip4_address, &in_addr, &in_plen,
764 unformat_ip4_address, &out_addr, &out_plen))
766 else if (unformat (i, "del"))
770 clib_warning("unknown input '%U'", format_unformat_error, i);
774 M(NAT_DET_ADD_DEL_MAP, mp);
775 clib_memcpy(mp->in_addr, &in_addr, 4);
776 mp->in_plen = in_plen;
777 clib_memcpy(mp->out_addr, &out_addr, 4);
778 mp->out_plen = out_plen;
786 static void vl_api_nat_det_forward_reply_t_handler
787 (vl_api_nat_det_forward_reply_t *mp)
789 snat_test_main_t * sm = &snat_test_main;
790 vat_main_t *vam = sm->vat_main;
791 i32 retval = ntohl(mp->retval);
795 fformat (vam->ofp, "outside address %U", format_ip4_address, &mp->out_addr);
796 fformat (vam->ofp, " outside port range start %d", ntohs(mp->out_port_lo));
797 fformat (vam->ofp, " outside port range end %d\n", ntohs(mp->out_port_hi));
800 vam->retval = retval;
801 vam->result_ready = 1;
804 static int api_nat_det_forward (vat_main_t * vam)
806 unformat_input_t * i = vam->input;
807 vl_api_nat_det_forward_t * mp;
808 ip4_address_t in_addr;
811 if (unformat (i, "%U", unformat_ip4_address, &in_addr))
815 clib_warning("unknown input '%U'", format_unformat_error, i);
819 M(NAT_DET_FORWARD, mp);
820 clib_memcpy(mp->in_addr, &in_addr, 4);
827 static void vl_api_nat_det_reverse_reply_t_handler
828 (vl_api_nat_det_reverse_reply_t *mp)
830 snat_test_main_t * sm = &snat_test_main;
831 vat_main_t *vam = sm->vat_main;
832 i32 retval = ntohl(mp->retval);
836 fformat (vam->ofp, "inside address %U\n", format_ip4_address, &mp->in_addr);
839 vam->retval = retval;
840 vam->result_ready = 1;
843 static int api_nat_det_reverse (vat_main_t * vam)
845 unformat_input_t * i = vam->input;
846 vl_api_nat_det_reverse_t * mp;
847 ip4_address_t out_addr;
851 if (unformat (i, "%U %d", unformat_ip4_address, &out_addr, &out_port))
855 clib_warning("unknown input '%U'", format_unformat_error, i);
859 M(NAT_DET_REVERSE, mp);
860 clib_memcpy(mp->out_addr, &out_addr, 4);
861 mp->out_port = htons((u16)out_port);
868 static void vl_api_nat_det_map_details_t_handler
869 (vl_api_nat_det_map_details_t *mp)
871 snat_test_main_t * sm = &snat_test_main;
872 vat_main_t *vam = sm->vat_main;
874 fformat (vam->ofp, "Deterministic S-NAT mapping in %U/%d out %U/%d "
875 "ports per host %d sharing ratio %d "
876 "number of sessions %d",
877 format_ip4_address, mp->in_addr, mp->in_plen,
878 format_ip4_address, mp->out_addr, mp->out_plen,
879 ntohs(mp->ports_per_host), ntohl(mp->sharing_ratio),
883 static int api_nat_det_map_dump(vat_main_t * vam)
885 vl_api_nat_det_map_dump_t * mp;
886 vl_api_nat_control_ping_t *mp_ping;
889 if (vam->json_output)
891 clib_warning ("JSON output not supported for nat_det_map_dump");
895 M(NAT_DET_MAP_DUMP, mp);
898 /* Use a control ping for synchronization */
899 M(NAT_CONTROL_PING, mp_ping);
906 static int api_nat_det_set_timeouts (vat_main_t * vam)
908 unformat_input_t * i = vam->input;
909 vl_api_nat_det_set_timeouts_t * mp;
910 u32 udp = SNAT_UDP_TIMEOUT;
911 u32 tcp_established = SNAT_TCP_ESTABLISHED_TIMEOUT;
912 u32 tcp_transitory = SNAT_TCP_TRANSITORY_TIMEOUT;
913 u32 icmp = SNAT_ICMP_TIMEOUT;
916 if (unformat (i, "udp %d", &udp))
918 else if (unformat (i, "tcp_established %d", &tcp_established))
920 else if (unformat (i, "tcp_transitory %d", &tcp_transitory))
922 else if (unformat (i, "icmp %d", &icmp))
926 clib_warning("unknown input '%U'", format_unformat_error, i);
930 M(NAT_DET_SET_TIMEOUTS, mp);
931 mp->udp = htonl(udp);
932 mp->tcp_established = htonl(tcp_established);
933 mp->tcp_transitory = htonl(tcp_transitory);
934 mp->icmp = htonl(icmp);
941 static void vl_api_nat_det_get_timeouts_reply_t_handler
942 (vl_api_nat_det_get_timeouts_reply_t *mp)
944 snat_test_main_t * sm = &snat_test_main;
945 vat_main_t *vam = sm->vat_main;
946 i32 retval = ntohl (mp->retval);
950 fformat (vam->ofp, "udp timeout: %dsec\n", ntohl (mp->udp));
951 fformat (vam->ofp, "tcp-established timeout: %dsec",
952 ntohl (mp->tcp_established));
953 fformat (vam->ofp, "tcp-transitory timeout: %dsec",
954 ntohl (mp->tcp_transitory));
955 fformat (vam->ofp, "icmp timeout: %dsec", ntohl (mp->icmp));
957 vam->retval = retval;
958 vam->result_ready = 1;
961 static int api_nat_det_get_timeouts(vat_main_t * vam)
963 vl_api_nat_det_get_timeouts_t * mp;
966 if (vam->json_output)
968 clib_warning ("JSON output not supported for nat_show_config");
972 M(NAT_DET_GET_TIMEOUTS, mp);
978 static int api_nat_det_close_session_out (vat_main_t * vam)
980 unformat_input_t * i = vam->input;
981 vl_api_nat_det_close_session_out_t * mp;
982 ip4_address_t out_addr, ext_addr;
983 u32 out_port, ext_port;
986 if (unformat (i, "%U:%d %U:%d",
987 unformat_ip4_address, &out_addr, &out_port,
988 unformat_ip4_address, &ext_addr, &ext_port))
992 clib_warning("unknown input '%U'", format_unformat_error, i);
996 M(NAT_DET_CLOSE_SESSION_OUT, mp);
997 clib_memcpy(mp->out_addr, &out_addr, 4);
998 mp->out_port = ntohs((u16)out_port);
999 clib_memcpy(mp->ext_addr, &ext_addr, 4);
1000 mp->ext_port = ntohs((u16)ext_port);
1007 static int api_nat_det_close_session_in (vat_main_t * vam)
1009 unformat_input_t * i = vam->input;
1010 vl_api_nat_det_close_session_in_t * mp;
1011 ip4_address_t in_addr, ext_addr;
1012 u32 in_port, ext_port;
1015 if (unformat (i, "%U:%d %U:%d",
1016 unformat_ip4_address, &in_addr, &in_port,
1017 unformat_ip4_address, &ext_addr, &ext_port))
1021 clib_warning("unknown input '%U'", format_unformat_error, i);
1025 M(NAT_DET_CLOSE_SESSION_IN, mp);
1026 clib_memcpy(mp->in_addr, &in_addr, 4);
1027 mp->in_port = ntohs((u16)in_port);
1028 clib_memcpy(mp->ext_addr, &ext_addr, 4);
1029 mp->ext_port = ntohs((u16)ext_port);
1036 static void vl_api_nat_det_session_details_t_handler
1037 (vl_api_nat_det_session_details_t *mp)
1039 snat_test_main_t * sm = &snat_test_main;
1040 vat_main_t *vam = sm->vat_main;
1042 fformat(vam->ofp, "deterministic session, external host address %U, "
1043 "external host port %d, outer port %d, inside port %d",
1044 format_ip4_address, mp->ext_addr, mp->ext_port,
1045 mp->out_port, mp->in_port);
1048 static int api_nat_det_session_dump(vat_main_t * vam)
1050 unformat_input_t* i = vam->input;
1051 vl_api_nat_det_session_dump_t * mp;
1052 vl_api_nat_control_ping_t *mp_ping;
1053 ip4_address_t user_addr;
1056 if (vam->json_output)
1058 clib_warning ("JSON output not supported for nat_det_session_dump");
1062 if (unformat (i, "user_addr %U", unformat_ip4_address, &user_addr))
1066 clib_warning ("unknown input '%U'", format_unformat_error, i);
1070 M(NAT_DET_SESSION_DUMP, mp);
1071 clib_memcpy (&mp->user_addr, &user_addr, 4);
1074 /* Use a control ping for synchronization */
1075 M(NAT_CONTROL_PING, mp_ping);
1083 * List of messages that the api test plugin sends,
1084 * and that the data plane plugin processes
1086 #define foreach_vpe_api_msg \
1087 _(nat44_add_del_address_range, "<start-addr> [- <end-addr] [del]")\
1088 _(nat44_interface_add_del_feature, \
1089 "<intfc> | sw_if_index <id> [in] [out] [del]") \
1090 _(nat44_add_del_static_mapping, "local_addr <ip>" \
1091 " (external_addr <ip> | external_if <intfc> |" \
1092 " external_sw_if_ndex <id>) [local_port <n>]" \
1093 " [external_port <n>] [vrf <table-id>] [del] protocol <n>") \
1094 _(nat_set_workers, "<wokrers_bitmap>") \
1095 _(nat44_static_mapping_dump, "") \
1096 _(nat_show_config, "") \
1097 _(nat44_address_dump, "") \
1098 _(nat44_interface_dump, "") \
1099 _(nat_worker_dump, "") \
1100 _(nat44_add_del_interface_addr, \
1101 "<intfc> | sw_if_index <id> [del]") \
1102 _(nat44_interface_addr_dump, "") \
1103 _(nat_ipfix_enable_disable, "[domain <id>] [src_port <n>] " \
1105 _(nat44_user_dump, "") \
1106 _(nat44_user_session_dump, "ip_address <ip> vrf_id <table-id>") \
1107 _(nat_det_add_del_map, "in <in_addr>/<in_plen> out " \
1108 "<out_addr>/<out_plen> [del]") \
1109 _(nat_det_forward, "<in_addr>") \
1110 _(nat_det_reverse, "<out_addr> <out_port>") \
1111 _(nat_det_map_dump, "") \
1112 _(nat_det_set_timeouts, "[udp <sec> | tcp_established <sec> | " \
1113 "tcp_transitory <sec> | icmp <sec>]") \
1114 _(nat_det_get_timeouts, "") \
1115 _(nat_det_close_session_out, "<out_addr>:<out_port> " \
1116 "<ext_addr>:<ext_port>") \
1117 _(nat_det_close_session_in, "<in_addr>:<in_port> " \
1118 "<out_addr>:<out_port>") \
1119 _(nat_det_session_dump, "ip_address <user_addr>")
1122 snat_vat_api_hookup (vat_main_t *vam)
1124 snat_test_main_t * sm __attribute__((unused)) = &snat_test_main;
1125 /* Hook up handlers for replies from the data plane plug-in */
1127 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
1129 vl_api_##n##_t_handler, \
1131 vl_api_##n##_t_endian, \
1132 vl_api_##n##_t_print, \
1133 sizeof(vl_api_##n##_t), 1);
1134 foreach_vpe_api_reply_msg;
1137 /* API messages we can send */
1139 hash_set_mem (vam->function_by_name, #n, api_##n);
1140 foreach_vpe_api_msg;
1144 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
1145 foreach_vpe_api_msg;
1149 clib_error_t * vat_plugin_register (vat_main_t *vam)
1151 snat_test_main_t * sm = &snat_test_main;
1156 /* Ask the vpp engine for the first assigned message-id */
1157 name = format (0, "nat_%08x%c", api_version, 0);
1158 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
1160 if (sm->msg_id_base != (u16) ~0)
1161 snat_vat_api_hookup (vam);